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, 32);
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(s->picture_number==0)
00120 decode_ext_header(w);
00121
00122 s->pict_type = get_bits1(&s->gb) + 1;
00123 if(s->pict_type == AV_PICTURE_TYPE_I){
00124 code = get_bits(&s->gb, 7);
00125 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
00126 }
00127 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
00128 if(s->qscale <= 0)
00129 return -1;
00130
00131 return 0;
00132 }
00133
00134 int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s)
00135 {
00136 Wmv2Context * const w= (Wmv2Context*)s;
00137
00138 if (s->pict_type == AV_PICTURE_TYPE_I) {
00139 if(w->j_type_bit) w->j_type= get_bits1(&s->gb);
00140 else w->j_type= 0;
00141
00142 if(!w->j_type){
00143 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
00144 else s->per_mb_rl_table= 0;
00145
00146 if(!s->per_mb_rl_table){
00147 s->rl_chroma_table_index = decode012(&s->gb);
00148 s->rl_table_index = decode012(&s->gb);
00149 }
00150
00151 s->dc_table_index = get_bits1(&s->gb);
00152 }
00153 s->inter_intra_pred= 0;
00154 s->no_rounding = 1;
00155 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00156 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
00157 s->qscale,
00158 s->rl_chroma_table_index,
00159 s->rl_table_index,
00160 s->dc_table_index,
00161 s->per_mb_rl_table,
00162 w->j_type);
00163 }
00164 }else{
00165 int cbp_index;
00166 w->j_type=0;
00167
00168 parse_mb_skip(w);
00169 cbp_index= decode012(&s->gb);
00170 if(s->qscale <= 10){
00171 int map[3]= {0,2,1};
00172 w->cbp_table_index= map[cbp_index];
00173 }else if(s->qscale <= 20){
00174 int map[3]= {1,0,2};
00175 w->cbp_table_index= map[cbp_index];
00176 }else{
00177 int map[3]= {2,1,0};
00178 w->cbp_table_index= map[cbp_index];
00179 }
00180
00181 if(w->mspel_bit) s->mspel= get_bits1(&s->gb);
00182 else s->mspel= 0;
00183
00184 if(w->abt_flag){
00185 w->per_mb_abt= get_bits1(&s->gb)^1;
00186 if(!w->per_mb_abt){
00187 w->abt_type= decode012(&s->gb);
00188 }
00189 }
00190
00191 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
00192 else s->per_mb_rl_table= 0;
00193
00194 if(!s->per_mb_rl_table){
00195 s->rl_table_index = decode012(&s->gb);
00196 s->rl_chroma_table_index = s->rl_table_index;
00197 }
00198
00199 s->dc_table_index = get_bits1(&s->gb);
00200 s->mv_table_index = get_bits1(&s->gb);
00201
00202 s->inter_intra_pred= 0;
00203 s->no_rounding ^= 1;
00204
00205 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00206 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",
00207 s->rl_table_index,
00208 s->rl_chroma_table_index,
00209 s->dc_table_index,
00210 s->mv_table_index,
00211 s->per_mb_rl_table,
00212 s->qscale,
00213 s->mspel,
00214 w->per_mb_abt,
00215 w->abt_type,
00216 w->cbp_table_index,
00217 s->inter_intra_pred);
00218 }
00219 }
00220 s->esc3_level_length= 0;
00221 s->esc3_run_length= 0;
00222
00223 s->picture_number++;
00224
00225
00226 if(w->j_type){
00227 ff_intrax8_decode_picture(&w->x8, 2*s->qscale, (s->qscale-1)|1 );
00228 return 1;
00229 }
00230
00231 return 0;
00232 }
00233
00234 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){
00235 MpegEncContext * const s= &w->s;
00236 int ret;
00237
00238 ret= ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
00239
00240 if(ret<0) return -1;
00241
00242 if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel)
00243 w->hshift= get_bits1(&s->gb);
00244 else
00245 w->hshift= 0;
00246
00247
00248
00249 return 0;
00250 }
00251
00252 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){
00253 MpegEncContext * const s= &w->s;
00254 int xy, wrap, diff, type;
00255 int16_t *A, *B, *C, *mot_val;
00256
00257 wrap = s->b8_stride;
00258 xy = s->block_index[0];
00259
00260 mot_val = s->current_picture.motion_val[0][xy];
00261
00262 A = s->current_picture.motion_val[0][xy - 1];
00263 B = s->current_picture.motion_val[0][xy - wrap];
00264 C = s->current_picture.motion_val[0][xy + 2 - wrap];
00265
00266 if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
00267 diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
00268 else
00269 diff=0;
00270
00271 if(diff >= 8)
00272 type= get_bits1(&s->gb);
00273 else
00274 type= 2;
00275
00276 if(type == 0){
00277 *px= A[0];
00278 *py= A[1];
00279 }else if(type == 1){
00280 *px= B[0];
00281 *py= B[1];
00282 }else{
00283
00284 if (s->first_slice_line) {
00285 *px = A[0];
00286 *py = A[1];
00287 } else {
00288 *px = mid_pred(A[0], B[0], C[0]);
00289 *py = mid_pred(A[1], B[1], C[1]);
00290 }
00291 }
00292
00293 return mot_val;
00294 }
00295
00296 static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){
00297 MpegEncContext * const s= &w->s;
00298 static const int sub_cbp_table[3]= {2,3,1};
00299 int sub_cbp;
00300
00301 if(!cbp){
00302 s->block_last_index[n] = -1;
00303
00304 return 0;
00305 }
00306
00307 if(w->per_block_abt)
00308 w->abt_type= decode012(&s->gb);
00309 w->abt_type_table[n]= w->abt_type;
00310
00311 if(w->abt_type){
00312
00313 const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable;
00314
00315
00316 sub_cbp= sub_cbp_table[ decode012(&s->gb) ];
00317
00318
00319 if(sub_cbp&1){
00320 if (ff_msmpeg4_decode_block(s, block, n, 1, scantable) < 0)
00321 return -1;
00322 }
00323
00324 if(sub_cbp&2){
00325 if (ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0)
00326 return -1;
00327 }
00328 s->block_last_index[n] = 63;
00329
00330 return 0;
00331 }else{
00332 return ff_msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated);
00333 }
00334 }
00335
00336
00337 int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
00338 {
00339 Wmv2Context * const w= (Wmv2Context*)s;
00340 int cbp, code, i;
00341 uint8_t *coded_val;
00342
00343 if(w->j_type) return 0;
00344
00345 if (s->pict_type == AV_PICTURE_TYPE_P) {
00346 if(IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])){
00347
00348 s->mb_intra = 0;
00349 for(i=0;i<6;i++)
00350 s->block_last_index[i] = -1;
00351 s->mv_dir = MV_DIR_FORWARD;
00352 s->mv_type = MV_TYPE_16X16;
00353 s->mv[0][0][0] = 0;
00354 s->mv[0][0][1] = 0;
00355 s->mb_skipped = 1;
00356 w->hshift=0;
00357 return 0;
00358 }
00359
00360 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3);
00361 if (code < 0)
00362 return -1;
00363 s->mb_intra = (~code & 0x40) >> 6;
00364
00365 cbp = code & 0x3f;
00366 } else {
00367 s->mb_intra = 1;
00368 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
00369 if (code < 0){
00370 av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
00371 return -1;
00372 }
00373
00374 cbp = 0;
00375 for(i=0;i<6;i++) {
00376 int val = ((code >> (5 - i)) & 1);
00377 if (i < 4) {
00378 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
00379 val = val ^ pred;
00380 *coded_val = val;
00381 }
00382 cbp |= val << (5 - i);
00383 }
00384 }
00385
00386 if (!s->mb_intra) {
00387 int mx, my;
00388
00389 wmv2_pred_motion(w, &mx, &my);
00390
00391 if(cbp){
00392 s->dsp.clear_blocks(s->block[0]);
00393 if(s->per_mb_rl_table){
00394 s->rl_table_index = decode012(&s->gb);
00395 s->rl_chroma_table_index = s->rl_table_index;
00396 }
00397
00398 if(w->abt_flag && w->per_mb_abt){
00399 w->per_block_abt= get_bits1(&s->gb);
00400 if(!w->per_block_abt)
00401 w->abt_type= decode012(&s->gb);
00402 }else
00403 w->per_block_abt=0;
00404 }
00405
00406 if (wmv2_decode_motion(w, &mx, &my) < 0)
00407 return -1;
00408
00409 s->mv_dir = MV_DIR_FORWARD;
00410 s->mv_type = MV_TYPE_16X16;
00411 s->mv[0][0][0] = mx;
00412 s->mv[0][0][1] = my;
00413
00414 for (i = 0; i < 6; i++) {
00415 if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0)
00416 {
00417 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
00418 return -1;
00419 }
00420 }
00421 } else {
00422
00423
00424
00425 s->ac_pred = get_bits1(&s->gb);
00426 if(s->inter_intra_pred){
00427 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
00428
00429 }
00430 if(s->per_mb_rl_table && cbp){
00431 s->rl_table_index = decode012(&s->gb);
00432 s->rl_chroma_table_index = s->rl_table_index;
00433 }
00434
00435 s->dsp.clear_blocks(s->block[0]);
00436 for (i = 0; i < 6; i++) {
00437 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
00438 {
00439 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
00440 return -1;
00441 }
00442 }
00443 }
00444
00445 return 0;
00446 }
00447
00448 static av_cold int wmv2_decode_init(AVCodecContext *avctx){
00449 Wmv2Context * const w= avctx->priv_data;
00450
00451 if(avctx->idct_algo==FF_IDCT_AUTO){
00452 avctx->idct_algo=FF_IDCT_WMV2;
00453 }
00454
00455 if(ff_msmpeg4_decode_init(avctx) < 0)
00456 return -1;
00457
00458 ff_wmv2_common_init(w);
00459
00460 ff_intrax8_common_init(&w->x8,&w->s);
00461
00462 return 0;
00463 }
00464
00465 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
00466 {
00467 Wmv2Context *w = avctx->priv_data;
00468
00469 ff_intrax8_common_end(&w->x8);
00470 return ff_h263_decode_end(avctx);
00471 }
00472
00473 AVCodec ff_wmv2_decoder = {
00474 "wmv2",
00475 AVMEDIA_TYPE_VIDEO,
00476 CODEC_ID_WMV2,
00477 sizeof(Wmv2Context),
00478 wmv2_decode_init,
00479 NULL,
00480 wmv2_decode_end,
00481 ff_h263_decode_frame,
00482 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
00483 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
00484 .pix_fmts= ff_pixfmt_list_420,
00485 };