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