• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/vp5.c

Go to the documentation of this file.
00001 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "bitstream.h"
00030 
00031 #include "vp56.h"
00032 #include "vp56data.h"
00033 #include "vp5data.h"
00034 
00035 
00036 static int vp5_parse_header(VP56Context *s, const uint8_t *buf, int buf_size,
00037                             int *golden_frame)
00038 {
00039     VP56RangeCoder *c = &s->c;
00040     int rows, cols;
00041 
00042     vp56_init_range_decoder(&s->c, buf, buf_size);
00043     s->framep[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
00044     vp56_rac_get(c);
00045     vp56_init_dequant(s, vp56_rac_gets(c, 6));
00046     if (s->framep[VP56_FRAME_CURRENT]->key_frame)
00047     {
00048         vp56_rac_gets(c, 8);
00049         if(vp56_rac_gets(c, 5) > 5)
00050             return 0;
00051         vp56_rac_gets(c, 2);
00052         if (vp56_rac_get(c)) {
00053             av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
00054             return 0;
00055         }
00056         rows = vp56_rac_gets(c, 8);  /* number of stored macroblock rows */
00057         cols = vp56_rac_gets(c, 8);  /* number of stored macroblock cols */
00058         vp56_rac_gets(c, 8);  /* number of displayed macroblock rows */
00059         vp56_rac_gets(c, 8);  /* number of displayed macroblock cols */
00060         vp56_rac_gets(c, 2);
00061         if (!s->macroblocks || /* first frame */
00062             16*cols != s->avctx->coded_width ||
00063             16*rows != s->avctx->coded_height) {
00064             avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
00065             return 2;
00066         }
00067     } else if (!s->macroblocks)
00068         return 0;
00069     return 1;
00070 }
00071 
00072 /* Gives very similar result than the vp6 version except in a few cases */
00073 static int vp5_adjust(int v, int t)
00074 {
00075     int s2, s1 = v >> 31;
00076     v ^= s1;
00077     v -= s1;
00078     v *= v < 2*t;
00079     v -= t;
00080     s2 = v >> 31;
00081     v ^= s2;
00082     v -= s2;
00083     v = t - v;
00084     v += s1;
00085     v ^= s1;
00086     return v;
00087 }
00088 
00089 static void vp5_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
00090 {
00091     VP56RangeCoder *c = &s->c;
00092     VP56Model *model = s->modelp;
00093     int comp, di;
00094 
00095     for (comp=0; comp<2; comp++) {
00096         int delta = 0;
00097         if (vp56_rac_get_prob(c, model->vector_dct[comp])) {
00098             int sign = vp56_rac_get_prob(c, model->vector_sig[comp]);
00099             di  = vp56_rac_get_prob(c, model->vector_pdi[comp][0]);
00100             di |= vp56_rac_get_prob(c, model->vector_pdi[comp][1]) << 1;
00101             delta = vp56_rac_get_tree(c, vp56_pva_tree,
00102                                       model->vector_pdv[comp]);
00103             delta = di | (delta << 2);
00104             delta = (delta ^ -sign) + sign;
00105         }
00106         if (!comp)
00107             vect->x = delta;
00108         else
00109             vect->y = delta;
00110     }
00111 }
00112 
00113 static void vp5_parse_vector_models(VP56Context *s)
00114 {
00115     VP56RangeCoder *c = &s->c;
00116     VP56Model *model = s->modelp;
00117     int comp, node;
00118 
00119     for (comp=0; comp<2; comp++) {
00120         if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
00121             model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
00122         if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
00123             model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
00124         if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
00125             model->vector_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
00126         if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
00127             model->vector_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
00128     }
00129 
00130     for (comp=0; comp<2; comp++)
00131         for (node=0; node<7; node++)
00132             if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
00133                 model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
00134 }
00135 
00136 static void vp5_parse_coeff_models(VP56Context *s)
00137 {
00138     VP56RangeCoder *c = &s->c;
00139     VP56Model *model = s->modelp;
00140     uint8_t def_prob[11];
00141     int node, cg, ctx;
00142     int ct;    /* code type */
00143     int pt;    /* plane type (0 for Y, 1 for U or V) */
00144 
00145     memset(def_prob, 0x80, sizeof(def_prob));
00146 
00147     for (pt=0; pt<2; pt++)
00148         for (node=0; node<11; node++)
00149             if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
00150                 def_prob[node] = vp56_rac_gets_nn(c, 7);
00151                 model->coeff_dccv[pt][node] = def_prob[node];
00152             } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
00153                 model->coeff_dccv[pt][node] = def_prob[node];
00154             }
00155 
00156     for (ct=0; ct<3; ct++)
00157         for (pt=0; pt<2; pt++)
00158             for (cg=0; cg<6; cg++)
00159                 for (node=0; node<11; node++)
00160                     if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
00161                         def_prob[node] = vp56_rac_gets_nn(c, 7);
00162                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
00163                     } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
00164                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
00165                     }
00166 
00167     /* coeff_dcct is a linear combination of coeff_dccv */
00168     for (pt=0; pt<2; pt++)
00169         for (ctx=0; ctx<36; ctx++)
00170             for (node=0; node<5; node++)
00171                 model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
00172 
00173     /* coeff_acct is a linear combination of coeff_ract */
00174     for (ct=0; ct<3; ct++)
00175         for (pt=0; pt<2; pt++)
00176             for (cg=0; cg<3; cg++)
00177                 for (ctx=0; ctx<6; ctx++)
00178                     for (node=0; node<5; node++)
00179                         model->coeff_acct[pt][ct][cg][ctx][node] = av_clip(((model->coeff_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254);
00180 }
00181 
00182 static void vp5_parse_coeff(VP56Context *s)
00183 {
00184     VP56RangeCoder *c = &s->c;
00185     VP56Model *model = s->modelp;
00186     uint8_t *permute = s->scantable.permutated;
00187     uint8_t *model1, *model2;
00188     int coeff, sign, coeff_idx;
00189     int b, i, cg, idx, ctx, ctx_last;
00190     int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
00191 
00192     for (b=0; b<6; b++) {
00193         int ct = 1;    /* code type */
00194 
00195         if (b > 3) pt = 1;
00196 
00197         ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
00198               + s->above_blocks[s->above_block_idx[b]].not_null_dc;
00199         model1 = model->coeff_dccv[pt];
00200         model2 = model->coeff_dcct[pt][ctx];
00201 
00202         for (coeff_idx=0; coeff_idx<64; ) {
00203             if (vp56_rac_get_prob(c, model2[0])) {
00204                 if (vp56_rac_get_prob(c, model2[2])) {
00205                     if (vp56_rac_get_prob(c, model2[3])) {
00206                         s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
00207                         idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
00208                         sign = vp56_rac_get(c);
00209                         coeff = vp56_coeff_bias[idx+5];
00210                         for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
00211                             coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
00212                     } else {
00213                         if (vp56_rac_get_prob(c, model2[4])) {
00214                             coeff = 3 + vp56_rac_get_prob(c, model1[5]);
00215                             s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
00216                         } else {
00217                             coeff = 2;
00218                             s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
00219                         }
00220                         sign = vp56_rac_get(c);
00221                     }
00222                     ct = 2;
00223                 } else {
00224                     ct = 1;
00225                     s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
00226                     sign = vp56_rac_get(c);
00227                     coeff = 1;
00228                 }
00229                 coeff = (coeff ^ -sign) + sign;
00230                 if (coeff_idx)
00231                     coeff *= s->dequant_ac;
00232                 s->block_coeff[b][permute[coeff_idx]] = coeff;
00233             } else {
00234                 if (ct && !vp56_rac_get_prob(c, model2[1]))
00235                     break;
00236                 ct = 0;
00237                 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
00238             }
00239 
00240             cg = vp5_coeff_groups[++coeff_idx];
00241             ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
00242             model1 = model->coeff_ract[pt][ct][cg];
00243             model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx];
00244         }
00245 
00246         ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
00247         s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
00248         if (coeff_idx < ctx_last)
00249             for (i=coeff_idx; i<=ctx_last; i++)
00250                 s->coeff_ctx[vp56_b6to4[b]][i] = 5;
00251         s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
00252     }
00253 }
00254 
00255 static void vp5_default_models_init(VP56Context *s)
00256 {
00257     VP56Model *model = s->modelp;
00258     int i;
00259 
00260     for (i=0; i<2; i++) {
00261         model->vector_sig[i] = 0x80;
00262         model->vector_dct[i] = 0x80;
00263         model->vector_pdi[i][0] = 0x55;
00264         model->vector_pdi[i][1] = 0x80;
00265     }
00266     memcpy(model->mb_types_stats, vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
00267     memset(model->vector_pdv, 0x80, sizeof(model->vector_pdv));
00268 }
00269 
00270 static av_cold int vp5_decode_init(AVCodecContext *avctx)
00271 {
00272     VP56Context *s = avctx->priv_data;
00273 
00274     vp56_init(avctx, 1, 0);
00275     s->vp56_coord_div = vp5_coord_div;
00276     s->parse_vector_adjustment = vp5_parse_vector_adjustment;
00277     s->adjust = vp5_adjust;
00278     s->parse_coeff = vp5_parse_coeff;
00279     s->default_models_init = vp5_default_models_init;
00280     s->parse_vector_models = vp5_parse_vector_models;
00281     s->parse_coeff_models = vp5_parse_coeff_models;
00282     s->parse_header = vp5_parse_header;
00283 
00284     return 0;
00285 }
00286 
00287 AVCodec vp5_decoder = {
00288     "vp5",
00289     CODEC_TYPE_VIDEO,
00290     CODEC_ID_VP5,
00291     sizeof(VP56Context),
00292     vp5_decode_init,
00293     NULL,
00294     vp56_free,
00295     vp56_decode_frame,
00296     CODEC_CAP_DR1,
00297     .long_name = NULL_IF_CONFIG_SMALL("On2 VP5"),
00298 };

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1