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