Libav 0.7.1
|
00001 00024 #include <stdlib.h> 00025 #include <string.h> 00026 00027 #include "avcodec.h" 00028 #include "dsputil.h" 00029 #include "get_bits.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 ff_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 ff_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 if (!rows || !cols) { 00059 av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", 00060 cols << 4, rows << 4); 00061 return 0; 00062 } 00063 vp56_rac_gets(c, 8); /* number of displayed macroblock rows */ 00064 vp56_rac_gets(c, 8); /* number of displayed macroblock cols */ 00065 vp56_rac_gets(c, 2); 00066 if (!s->macroblocks || /* first frame */ 00067 16*cols != s->avctx->coded_width || 00068 16*rows != s->avctx->coded_height) { 00069 avcodec_set_dimensions(s->avctx, 16*cols, 16*rows); 00070 return 2; 00071 } 00072 } else if (!s->macroblocks) 00073 return 0; 00074 return 1; 00075 } 00076 00077 static void vp5_parse_vector_adjustment(VP56Context *s, VP56mv *vect) 00078 { 00079 VP56RangeCoder *c = &s->c; 00080 VP56Model *model = s->modelp; 00081 int comp, di; 00082 00083 for (comp=0; comp<2; comp++) { 00084 int delta = 0; 00085 if (vp56_rac_get_prob(c, model->vector_dct[comp])) { 00086 int sign = vp56_rac_get_prob(c, model->vector_sig[comp]); 00087 di = vp56_rac_get_prob(c, model->vector_pdi[comp][0]); 00088 di |= vp56_rac_get_prob(c, model->vector_pdi[comp][1]) << 1; 00089 delta = vp56_rac_get_tree(c, vp56_pva_tree, 00090 model->vector_pdv[comp]); 00091 delta = di | (delta << 2); 00092 delta = (delta ^ -sign) + sign; 00093 } 00094 if (!comp) 00095 vect->x = delta; 00096 else 00097 vect->y = delta; 00098 } 00099 } 00100 00101 static void vp5_parse_vector_models(VP56Context *s) 00102 { 00103 VP56RangeCoder *c = &s->c; 00104 VP56Model *model = s->modelp; 00105 int comp, node; 00106 00107 for (comp=0; comp<2; comp++) { 00108 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0])) 00109 model->vector_dct[comp] = vp56_rac_gets_nn(c, 7); 00110 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1])) 00111 model->vector_sig[comp] = vp56_rac_gets_nn(c, 7); 00112 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2])) 00113 model->vector_pdi[comp][0] = vp56_rac_gets_nn(c, 7); 00114 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3])) 00115 model->vector_pdi[comp][1] = vp56_rac_gets_nn(c, 7); 00116 } 00117 00118 for (comp=0; comp<2; comp++) 00119 for (node=0; node<7; node++) 00120 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node])) 00121 model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7); 00122 } 00123 00124 static int vp5_parse_coeff_models(VP56Context *s) 00125 { 00126 VP56RangeCoder *c = &s->c; 00127 VP56Model *model = s->modelp; 00128 uint8_t def_prob[11]; 00129 int node, cg, ctx; 00130 int ct; /* code type */ 00131 int pt; /* plane type (0 for Y, 1 for U or V) */ 00132 00133 memset(def_prob, 0x80, sizeof(def_prob)); 00134 00135 for (pt=0; pt<2; pt++) 00136 for (node=0; node<11; node++) 00137 if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) { 00138 def_prob[node] = vp56_rac_gets_nn(c, 7); 00139 model->coeff_dccv[pt][node] = def_prob[node]; 00140 } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) { 00141 model->coeff_dccv[pt][node] = def_prob[node]; 00142 } 00143 00144 for (ct=0; ct<3; ct++) 00145 for (pt=0; pt<2; pt++) 00146 for (cg=0; cg<6; cg++) 00147 for (node=0; node<11; node++) 00148 if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) { 00149 def_prob[node] = vp56_rac_gets_nn(c, 7); 00150 model->coeff_ract[pt][ct][cg][node] = def_prob[node]; 00151 } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) { 00152 model->coeff_ract[pt][ct][cg][node] = def_prob[node]; 00153 } 00154 00155 /* coeff_dcct is a linear combination of coeff_dccv */ 00156 for (pt=0; pt<2; pt++) 00157 for (ctx=0; ctx<36; ctx++) 00158 for (node=0; node<5; node++) 00159 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); 00160 00161 /* coeff_acct is a linear combination of coeff_ract */ 00162 for (ct=0; ct<3; ct++) 00163 for (pt=0; pt<2; pt++) 00164 for (cg=0; cg<3; cg++) 00165 for (ctx=0; ctx<6; ctx++) 00166 for (node=0; node<5; node++) 00167 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); 00168 return 0; 00169 } 00170 00171 static void vp5_parse_coeff(VP56Context *s) 00172 { 00173 VP56RangeCoder *c = &s->c; 00174 VP56Model *model = s->modelp; 00175 uint8_t *permute = s->scantable.permutated; 00176 uint8_t *model1, *model2; 00177 int coeff, sign, coeff_idx; 00178 int b, i, cg, idx, ctx, ctx_last; 00179 int pt = 0; /* plane type (0 for Y, 1 for U or V) */ 00180 00181 for (b=0; b<6; b++) { 00182 int ct = 1; /* code type */ 00183 00184 if (b > 3) pt = 1; 00185 00186 ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0] 00187 + s->above_blocks[s->above_block_idx[b]].not_null_dc; 00188 model1 = model->coeff_dccv[pt]; 00189 model2 = model->coeff_dcct[pt][ctx]; 00190 00191 for (coeff_idx=0; coeff_idx<64; ) { 00192 if (vp56_rac_get_prob(c, model2[0])) { 00193 if (vp56_rac_get_prob(c, model2[2])) { 00194 if (vp56_rac_get_prob(c, model2[3])) { 00195 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4; 00196 idx = vp56_rac_get_tree(c, vp56_pc_tree, model1); 00197 sign = vp56_rac_get(c); 00198 coeff = vp56_coeff_bias[idx+5]; 00199 for (i=vp56_coeff_bit_length[idx]; i>=0; i--) 00200 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i; 00201 } else { 00202 if (vp56_rac_get_prob(c, model2[4])) { 00203 coeff = 3 + vp56_rac_get_prob(c, model1[5]); 00204 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3; 00205 } else { 00206 coeff = 2; 00207 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2; 00208 } 00209 sign = vp56_rac_get(c); 00210 } 00211 ct = 2; 00212 } else { 00213 ct = 1; 00214 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1; 00215 sign = vp56_rac_get(c); 00216 coeff = 1; 00217 } 00218 coeff = (coeff ^ -sign) + sign; 00219 if (coeff_idx) 00220 coeff *= s->dequant_ac; 00221 s->block_coeff[b][permute[coeff_idx]] = coeff; 00222 } else { 00223 if (ct && !vp56_rac_get_prob(c, model2[1])) 00224 break; 00225 ct = 0; 00226 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0; 00227 } 00228 00229 cg = vp5_coeff_groups[++coeff_idx]; 00230 ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx]; 00231 model1 = model->coeff_ract[pt][ct][cg]; 00232 model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx]; 00233 } 00234 00235 ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24); 00236 s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx; 00237 if (coeff_idx < ctx_last) 00238 for (i=coeff_idx; i<=ctx_last; i++) 00239 s->coeff_ctx[vp56_b6to4[b]][i] = 5; 00240 s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0]; 00241 } 00242 } 00243 00244 static void vp5_default_models_init(VP56Context *s) 00245 { 00246 VP56Model *model = s->modelp; 00247 int i; 00248 00249 for (i=0; i<2; i++) { 00250 model->vector_sig[i] = 0x80; 00251 model->vector_dct[i] = 0x80; 00252 model->vector_pdi[i][0] = 0x55; 00253 model->vector_pdi[i][1] = 0x80; 00254 } 00255 memcpy(model->mb_types_stats, vp56_def_mb_types_stats, sizeof(model->mb_types_stats)); 00256 memset(model->vector_pdv, 0x80, sizeof(model->vector_pdv)); 00257 } 00258 00259 static av_cold int vp5_decode_init(AVCodecContext *avctx) 00260 { 00261 VP56Context *s = avctx->priv_data; 00262 00263 ff_vp56_init(avctx, 1, 0); 00264 s->vp56_coord_div = vp5_coord_div; 00265 s->parse_vector_adjustment = vp5_parse_vector_adjustment; 00266 s->parse_coeff = vp5_parse_coeff; 00267 s->default_models_init = vp5_default_models_init; 00268 s->parse_vector_models = vp5_parse_vector_models; 00269 s->parse_coeff_models = vp5_parse_coeff_models; 00270 s->parse_header = vp5_parse_header; 00271 00272 return 0; 00273 } 00274 00275 AVCodec ff_vp5_decoder = { 00276 "vp5", 00277 AVMEDIA_TYPE_VIDEO, 00278 CODEC_ID_VP5, 00279 sizeof(VP56Context), 00280 vp5_decode_init, 00281 NULL, 00282 ff_vp56_free, 00283 ff_vp56_decode_frame, 00284 CODEC_CAP_DR1, 00285 .long_name = NULL_IF_CONFIG_SMALL("On2 VP5"), 00286 };