00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "dsputil.h"
00023 #include "mpegvideo.h"
00024 #include "mathops.h"
00025 #include "msmpeg4.h"
00026 #include "msmpeg4data.h"
00027 #include "intrax8.h"
00028 #include "wmv2.h"
00029
00030
00031 static void parse_mb_skip(Wmv2Context * w){
00032 int mb_x, mb_y;
00033 MpegEncContext * const s= &w->s;
00034 uint32_t * const mb_type= s->current_picture_ptr->mb_type;
00035
00036 w->skip_type= get_bits(&s->gb, 2);
00037 switch(w->skip_type){
00038 case SKIP_TYPE_NONE:
00039 for(mb_y=0; mb_y<s->mb_height; mb_y++){
00040 for(mb_x=0; mb_x<s->mb_width; mb_x++){
00041 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_16x16 | MB_TYPE_L0;
00042 }
00043 }
00044 break;
00045 case SKIP_TYPE_MPEG:
00046 for(mb_y=0; mb_y<s->mb_height; mb_y++){
00047 for(mb_x=0; mb_x<s->mb_width; mb_x++){
00048 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
00049 }
00050 }
00051 break;
00052 case SKIP_TYPE_ROW:
00053 for(mb_y=0; mb_y<s->mb_height; mb_y++){
00054 if(get_bits1(&s->gb)){
00055 for(mb_x=0; mb_x<s->mb_width; mb_x++){
00056 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00057 }
00058 }else{
00059 for(mb_x=0; mb_x<s->mb_width; mb_x++){
00060 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
00061 }
00062 }
00063 }
00064 break;
00065 case SKIP_TYPE_COL:
00066 for(mb_x=0; mb_x<s->mb_width; mb_x++){
00067 if(get_bits1(&s->gb)){
00068 for(mb_y=0; mb_y<s->mb_height; mb_y++){
00069 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00070 }
00071 }else{
00072 for(mb_y=0; mb_y<s->mb_height; mb_y++){
00073 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
00074 }
00075 }
00076 }
00077 break;
00078 }
00079 }
00080
00081 static int decode_ext_header(Wmv2Context *w){
00082 MpegEncContext * const s= &w->s;
00083 GetBitContext gb;
00084 int fps;
00085 int code;
00086
00087 if(s->avctx->extradata_size<4) return -1;
00088
00089 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
00090
00091 fps = get_bits(&gb, 5);
00092 s->bit_rate = get_bits(&gb, 11)*1024;
00093 w->mspel_bit = get_bits1(&gb);
00094 s->loop_filter = get_bits1(&gb);
00095 w->abt_flag = get_bits1(&gb);
00096 w->j_type_bit = get_bits1(&gb);
00097 w->top_left_mv_flag= get_bits1(&gb);
00098 w->per_mb_rl_bit = get_bits1(&gb);
00099 code = get_bits(&gb, 3);
00100
00101 if(code==0) return -1;
00102
00103 s->slice_height = s->mb_height / code;
00104
00105 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00106 av_log(s->avctx, AV_LOG_DEBUG, "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, slices:%d\n",
00107 fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit, w->top_left_mv_flag, w->per_mb_rl_bit, code, s->loop_filter,
00108 code);
00109 }
00110 return 0;
00111 }
00112
00113 int ff_wmv2_decode_picture_header(MpegEncContext * s)
00114 {
00115 Wmv2Context * const w= (Wmv2Context*)s;
00116 int code;
00117
00118 #if 0
00119 {
00120 int i;
00121 for(i=0; i<s->gb.size*8; i++)
00122 printf("%d", get_bits1(&s->gb));
00123
00124 printf("END\n");
00125 return -1;
00126 }
00127 #endif
00128 if(s->picture_number==0)
00129 decode_ext_header(w);
00130
00131 s->pict_type = get_bits1(&s->gb) + 1;
00132 if(s->pict_type == FF_I_TYPE){
00133 code = get_bits(&s->gb, 7);
00134 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
00135 }
00136 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
00137 if(s->qscale <= 0)
00138 return -1;
00139
00140 return 0;
00141 }
00142
00143 int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s)
00144 {
00145 Wmv2Context * const w= (Wmv2Context*)s;
00146
00147 if (s->pict_type == FF_I_TYPE) {
00148 if(w->j_type_bit) w->j_type= get_bits1(&s->gb);
00149 else w->j_type= 0;
00150
00151 if(!w->j_type){
00152 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
00153 else s->per_mb_rl_table= 0;
00154
00155 if(!s->per_mb_rl_table){
00156 s->rl_chroma_table_index = decode012(&s->gb);
00157 s->rl_table_index = decode012(&s->gb);
00158 }
00159
00160 s->dc_table_index = get_bits1(&s->gb);
00161 }
00162 s->inter_intra_pred= 0;
00163 s->no_rounding = 1;
00164 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00165 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
00166 s->qscale,
00167 s->rl_chroma_table_index,
00168 s->rl_table_index,
00169 s->dc_table_index,
00170 s->per_mb_rl_table,
00171 w->j_type);
00172 }
00173 }else{
00174 int cbp_index;
00175 w->j_type=0;
00176
00177 parse_mb_skip(w);
00178 cbp_index= decode012(&s->gb);
00179 if(s->qscale <= 10){
00180 int map[3]= {0,2,1};
00181 w->cbp_table_index= map[cbp_index];
00182 }else if(s->qscale <= 20){
00183 int map[3]= {1,0,2};
00184 w->cbp_table_index= map[cbp_index];
00185 }else{
00186 int map[3]= {2,1,0};
00187 w->cbp_table_index= map[cbp_index];
00188 }
00189
00190 if(w->mspel_bit) s->mspel= get_bits1(&s->gb);
00191 else s->mspel= 0;
00192
00193 if(w->abt_flag){
00194 w->per_mb_abt= get_bits1(&s->gb)^1;
00195 if(!w->per_mb_abt){
00196 w->abt_type= decode012(&s->gb);
00197 }
00198 }
00199
00200 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
00201 else s->per_mb_rl_table= 0;
00202
00203 if(!s->per_mb_rl_table){
00204 s->rl_table_index = decode012(&s->gb);
00205 s->rl_chroma_table_index = s->rl_table_index;
00206 }
00207
00208 s->dc_table_index = get_bits1(&s->gb);
00209 s->mv_table_index = get_bits1(&s->gb);
00210
00211 s->inter_intra_pred= 0;
00212 s->no_rounding ^= 1;
00213
00214 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00215 av_log(s->avctx, AV_LOG_DEBUG, "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
00216 s->rl_table_index,
00217 s->rl_chroma_table_index,
00218 s->dc_table_index,
00219 s->mv_table_index,
00220 s->per_mb_rl_table,
00221 s->qscale,
00222 s->mspel,
00223 w->per_mb_abt,
00224 w->abt_type,
00225 w->cbp_table_index,
00226 s->inter_intra_pred);
00227 }
00228 }
00229 s->esc3_level_length= 0;
00230 s->esc3_run_length= 0;
00231
00232 s->picture_number++;
00233
00234
00235 if(w->j_type){
00236 ff_intrax8_decode_picture(&w->x8, 2*s->qscale, (s->qscale-1)|1 );
00237 return 1;
00238 }
00239
00240 return 0;
00241 }
00242
00243 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){
00244 MpegEncContext * const s= &w->s;
00245 int ret;
00246
00247 ret= ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
00248
00249 if(ret<0) return -1;
00250
00251 if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel)
00252 w->hshift= get_bits1(&s->gb);
00253 else
00254 w->hshift= 0;
00255
00256
00257
00258 return 0;
00259 }
00260
00261 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){
00262 MpegEncContext * const s= &w->s;
00263 int xy, wrap, diff, type;
00264 int16_t *A, *B, *C, *mot_val;
00265
00266 wrap = s->b8_stride;
00267 xy = s->block_index[0];
00268
00269 mot_val = s->current_picture.motion_val[0][xy];
00270
00271 A = s->current_picture.motion_val[0][xy - 1];
00272 B = s->current_picture.motion_val[0][xy - wrap];
00273 C = s->current_picture.motion_val[0][xy + 2 - wrap];
00274
00275 if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
00276 diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
00277 else
00278 diff=0;
00279
00280 if(diff >= 8)
00281 type= get_bits1(&s->gb);
00282 else
00283 type= 2;
00284
00285 if(type == 0){
00286 *px= A[0];
00287 *py= A[1];
00288 }else if(type == 1){
00289 *px= B[0];
00290 *py= B[1];
00291 }else{
00292
00293 if (s->first_slice_line) {
00294 *px = A[0];
00295 *py = A[1];
00296 } else {
00297 *px = mid_pred(A[0], B[0], C[0]);
00298 *py = mid_pred(A[1], B[1], C[1]);
00299 }
00300 }
00301
00302 return mot_val;
00303 }
00304
00305 static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){
00306 MpegEncContext * const s= &w->s;
00307 static const int sub_cbp_table[3]= {2,3,1};
00308 int sub_cbp;
00309
00310 if(!cbp){
00311 s->block_last_index[n] = -1;
00312
00313 return 0;
00314 }
00315
00316 if(w->per_block_abt)
00317 w->abt_type= decode012(&s->gb);
00318 #if 0
00319 if(w->per_block_abt)
00320 printf("B%d", w->abt_type);
00321 #endif
00322 w->abt_type_table[n]= w->abt_type;
00323
00324 if(w->abt_type){
00325
00326 const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable;
00327
00328
00329 sub_cbp= sub_cbp_table[ decode012(&s->gb) ];
00330
00331
00332 if(sub_cbp&1){
00333 if (ff_msmpeg4_decode_block(s, block, n, 1, scantable) < 0)
00334 return -1;
00335 }
00336
00337 if(sub_cbp&2){
00338 if (ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0)
00339 return -1;
00340 }
00341 s->block_last_index[n] = 63;
00342
00343 return 0;
00344 }else{
00345 return ff_msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated);
00346 }
00347 }
00348
00349
00350 int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
00351 {
00352 Wmv2Context * const w= (Wmv2Context*)s;
00353 int cbp, code, i;
00354 uint8_t *coded_val;
00355
00356 if(w->j_type) return 0;
00357
00358 if (s->pict_type == FF_P_TYPE) {
00359 if(IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])){
00360
00361 s->mb_intra = 0;
00362 for(i=0;i<6;i++)
00363 s->block_last_index[i] = -1;
00364 s->mv_dir = MV_DIR_FORWARD;
00365 s->mv_type = MV_TYPE_16X16;
00366 s->mv[0][0][0] = 0;
00367 s->mv[0][0][1] = 0;
00368 s->mb_skipped = 1;
00369 w->hshift=0;
00370 return 0;
00371 }
00372
00373 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3);
00374 if (code < 0)
00375 return -1;
00376 s->mb_intra = (~code & 0x40) >> 6;
00377
00378 cbp = code & 0x3f;
00379 } else {
00380 s->mb_intra = 1;
00381 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
00382 if (code < 0){
00383 av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
00384 return -1;
00385 }
00386
00387 cbp = 0;
00388 for(i=0;i<6;i++) {
00389 int val = ((code >> (5 - i)) & 1);
00390 if (i < 4) {
00391 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
00392 val = val ^ pred;
00393 *coded_val = val;
00394 }
00395 cbp |= val << (5 - i);
00396 }
00397 }
00398
00399 if (!s->mb_intra) {
00400 int mx, my;
00401
00402 wmv2_pred_motion(w, &mx, &my);
00403
00404 if(cbp){
00405 s->dsp.clear_blocks(s->block[0]);
00406 if(s->per_mb_rl_table){
00407 s->rl_table_index = decode012(&s->gb);
00408 s->rl_chroma_table_index = s->rl_table_index;
00409 }
00410
00411 if(w->abt_flag && w->per_mb_abt){
00412 w->per_block_abt= get_bits1(&s->gb);
00413 if(!w->per_block_abt)
00414 w->abt_type= decode012(&s->gb);
00415 }else
00416 w->per_block_abt=0;
00417 }
00418
00419 if (wmv2_decode_motion(w, &mx, &my) < 0)
00420 return -1;
00421
00422 s->mv_dir = MV_DIR_FORWARD;
00423 s->mv_type = MV_TYPE_16X16;
00424 s->mv[0][0][0] = mx;
00425 s->mv[0][0][1] = my;
00426
00427 for (i = 0; i < 6; i++) {
00428 if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0)
00429 {
00430 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
00431 return -1;
00432 }
00433 }
00434 } else {
00435
00436
00437
00438 s->ac_pred = get_bits1(&s->gb);
00439 if(s->inter_intra_pred){
00440 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
00441
00442 }
00443 if(s->per_mb_rl_table && cbp){
00444 s->rl_table_index = decode012(&s->gb);
00445 s->rl_chroma_table_index = s->rl_table_index;
00446 }
00447
00448 s->dsp.clear_blocks(s->block[0]);
00449 for (i = 0; i < 6; i++) {
00450 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
00451 {
00452 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
00453 return -1;
00454 }
00455 }
00456 }
00457
00458 return 0;
00459 }
00460
00461 static av_cold int wmv2_decode_init(AVCodecContext *avctx){
00462 Wmv2Context * const w= avctx->priv_data;
00463
00464 if(avctx->idct_algo==FF_IDCT_AUTO){
00465 avctx->idct_algo=FF_IDCT_WMV2;
00466 }
00467
00468 if(ff_h263_decode_init(avctx) < 0)
00469 return -1;
00470
00471 ff_wmv2_common_init(w);
00472
00473 ff_intrax8_common_init(&w->x8,&w->s);
00474
00475 return 0;
00476 }
00477
00478 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
00479 {
00480 Wmv2Context *w = avctx->priv_data;
00481
00482 ff_intrax8_common_end(&w->x8);
00483 return ff_h263_decode_end(avctx);
00484 }
00485
00486 AVCodec wmv2_decoder = {
00487 "wmv2",
00488 CODEC_TYPE_VIDEO,
00489 CODEC_ID_WMV2,
00490 sizeof(Wmv2Context),
00491 wmv2_decode_init,
00492 NULL,
00493 wmv2_decode_end,
00494 ff_h263_decode_frame,
00495 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
00496 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
00497 .pix_fmts= ff_pixfmt_list_420,
00498 };