comparison msmpeg4.c @ 5882:51fc10d9fdff libavcodec

rename some msmpeg4 symbols and make them non-static so that they can be used in standalone wmv2.c
author aurel
date Wed, 07 Nov 2007 23:23:35 +0000
parents 56bc8fbd9aee
children 02007962faf4
comparison
equal deleted inserted replaced
5881:aa110f457c5a 5882:51fc10d9fdff
60 60
61 static uint32_t v2_dc_lum_table[512][2]; 61 static uint32_t v2_dc_lum_table[512][2];
62 static uint32_t v2_dc_chroma_table[512][2]; 62 static uint32_t v2_dc_chroma_table[512][2];
63 63
64 void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); 64 void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
65 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 65 int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
66 int n, int coded, const uint8_t *scantable); 66 int n, int coded, const uint8_t *scantable);
67 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); 67 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
68 static int msmpeg4_decode_motion(MpegEncContext * s, 68 int ff_msmpeg4_decode_motion(MpegEncContext * s,
69 int *mx_ptr, int *my_ptr); 69 int *mx_ptr, int *my_ptr);
70 static void init_h263_dc_for_msmpeg4(void); 70 static void init_h263_dc_for_msmpeg4(void);
71 static inline void msmpeg4_memsetw(short *tab, int val, int n); 71 static inline void msmpeg4_memsetw(short *tab, int val, int n);
72 #ifdef CONFIG_ENCODERS 72 #ifdef CONFIG_ENCODERS
73 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val); 73 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
74 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra); 74 static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
75 #endif //CONFIG_ENCODERS 75 #endif //CONFIG_ENCODERS
76 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); 76 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
77 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); 77 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
78 static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); 78 int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
79 79
80 /* vc1 externs */ 80 /* vc1 externs */
81 extern uint8_t wmv3_dc_scale_table[32]; 81 extern uint8_t wmv3_dc_scale_table[32];
82 82
83 #ifdef DEBUG 83 #ifdef DEBUG
159 y = tab->table_mvy[i]; 159 y = tab->table_mvy[i];
160 tab->table_mv_index[(x << 6) | y] = i; 160 tab->table_mv_index[(x << 6) | y] = i;
161 } 161 }
162 } 162 }
163 163
164 void ff_code012(PutBitContext *pb, int n) 164 void ff_msmpeg4_code012(PutBitContext *pb, int n)
165 { 165 {
166 if (n == 0) { 166 if (n == 0) {
167 put_bits(pb, 1, 0); 167 put_bits(pb, 1, 0);
168 } else { 168 } else {
169 put_bits(pb, 1, 1); 169 put_bits(pb, 1, 1);
345 put_bits(&s->pb, 1, s->per_mb_rl_table); 345 put_bits(&s->pb, 1, s->per_mb_rl_table);
346 } 346 }
347 347
348 if(s->msmpeg4_version>2){ 348 if(s->msmpeg4_version>2){
349 if(!s->per_mb_rl_table){ 349 if(!s->per_mb_rl_table){
350 ff_code012(&s->pb, s->rl_chroma_table_index); 350 ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
351 ff_code012(&s->pb, s->rl_table_index); 351 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
352 } 352 }
353 353
354 put_bits(&s->pb, 1, s->dc_table_index); 354 put_bits(&s->pb, 1, s->dc_table_index);
355 } 355 }
356 } else { 356 } else {
359 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE) 359 if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
360 put_bits(&s->pb, 1, s->per_mb_rl_table); 360 put_bits(&s->pb, 1, s->per_mb_rl_table);
361 361
362 if(s->msmpeg4_version>2){ 362 if(s->msmpeg4_version>2){
363 if(!s->per_mb_rl_table) 363 if(!s->per_mb_rl_table)
364 ff_code012(&s->pb, s->rl_table_index); 364 ff_msmpeg4_code012(&s->pb, s->rl_table_index);
365 365
366 put_bits(&s->pb, 1, s->dc_table_index); 366 put_bits(&s->pb, 1, s->dc_table_index);
367 367
368 put_bits(&s->pb, 1, s->mv_table_index); 368 put_bits(&s->pb, 1, s->mv_table_index);
369 } 369 }
391 } 391 }
392 392
393 #endif //CONFIG_ENCODERS 393 #endif //CONFIG_ENCODERS
394 394
395 /* predict coded block */ 395 /* predict coded block */
396 static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr) 396 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
397 { 397 {
398 int xy, wrap, pred, a, b, c; 398 int xy, wrap, pred, a, b, c;
399 399
400 xy = s->block_index[n]; 400 xy = s->block_index[n];
401 wrap = s->b8_stride; 401 wrap = s->b8_stride;
419 return pred; 419 return pred;
420 } 420 }
421 421
422 #ifdef CONFIG_ENCODERS 422 #ifdef CONFIG_ENCODERS
423 423
424 static void msmpeg4_encode_motion(MpegEncContext * s, 424 void ff_msmpeg4_encode_motion(MpegEncContext * s,
425 int mx, int my) 425 int mx, int my)
426 { 426 {
427 int code; 427 int code;
428 MVTable *mv; 428 MVTable *mv;
429 429
457 put_bits(&s->pb, 6, mx); 457 put_bits(&s->pb, 6, mx);
458 put_bits(&s->pb, 6, my); 458 put_bits(&s->pb, 6, my);
459 } 459 }
460 } 460 }
461 461
462 static inline void handle_slices(MpegEncContext *s){ 462 void ff_msmpeg4_handle_slices(MpegEncContext *s){
463 if (s->mb_x == 0) { 463 if (s->mb_x == 0) {
464 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { 464 if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
465 if(s->msmpeg4_version < 4){ 465 if(s->msmpeg4_version < 4){
466 ff_mpeg4_clean_buffers(s); 466 ff_mpeg4_clean_buffers(s);
467 } 467 }
478 { 478 {
479 int cbp, coded_cbp, i; 479 int cbp, coded_cbp, i;
480 int pred_x, pred_y; 480 int pred_x, pred_y;
481 uint8_t *coded_block; 481 uint8_t *coded_block;
482 482
483 handle_slices(s); 483 ff_msmpeg4_handle_slices(s);
484 484
485 if (!s->mb_intra) { 485 if (!s->mb_intra) {
486 /* compute cbp */ 486 /* compute cbp */
487 cbp = 0; 487 cbp = 0;
488 for (i = 0; i < 6; i++) { 488 for (i = 0; i < 6; i++) {
524 524
525 s->misc_bits += get_bits_diff(s); 525 s->misc_bits += get_bits_diff(s);
526 526
527 /* motion vector */ 527 /* motion vector */
528 h263_pred_motion(s, 0, 0, &pred_x, &pred_y); 528 h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
529 msmpeg4_encode_motion(s, motion_x - pred_x, 529 ff_msmpeg4_encode_motion(s, motion_x - pred_x,
530 motion_y - pred_y); 530 motion_y - pred_y);
531 } 531 }
532 532
533 s->mv_bits += get_bits_diff(s); 533 s->mv_bits += get_bits_diff(s);
534 534
544 int val, pred; 544 int val, pred;
545 val = (s->block_last_index[i] >= 1); 545 val = (s->block_last_index[i] >= 1);
546 cbp |= val << (5 - i); 546 cbp |= val << (5 - i);
547 if (i < 4) { 547 if (i < 4) {
548 /* predict value for close blocks only for luma */ 548 /* predict value for close blocks only for luma */
549 pred = coded_block_pred(s, i, &coded_block); 549 pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
550 *coded_block = val; 550 *coded_block = val;
551 val = val ^ pred; 551 val = val ^ pred;
552 } 552 }
553 coded_cbp |= val << (5 - i); 553 coded_cbp |= val << (5 - i);
554 } 554 }
988 } 988 }
989 989
990 /****************************************/ 990 /****************************************/
991 /* decoding stuff */ 991 /* decoding stuff */
992 992
993 static VLC mb_non_intra_vlc[4]; 993 VLC ff_mb_non_intra_vlc[4];
994 static VLC v2_dc_lum_vlc; 994 static VLC v2_dc_lum_vlc;
995 static VLC v2_dc_chroma_vlc; 995 static VLC v2_dc_chroma_vlc;
996 static VLC cbpy_vlc; 996 static VLC cbpy_vlc;
997 static VLC v2_intra_cbpc_vlc; 997 static VLC v2_intra_cbpc_vlc;
998 static VLC v2_mb_type_vlc; 998 static VLC v2_mb_type_vlc;
999 static VLC v2_mv_vlc; 999 static VLC v2_mv_vlc;
1000 static VLC v1_intra_cbpc_vlc; 1000 static VLC v1_intra_cbpc_vlc;
1001 static VLC v1_inter_cbpc_vlc; 1001 static VLC v1_inter_cbpc_vlc;
1002 static VLC inter_intra_vlc; 1002 VLC ff_inter_intra_vlc;
1003 1003
1004 /* This table is practically identical to the one from h263 1004 /* This table is practically identical to the one from h263
1005 * except that it is inverted. */ 1005 * except that it is inverted. */
1006 static void init_h263_dc_for_msmpeg4(void) 1006 static void init_h263_dc_for_msmpeg4(void)
1007 { 1007 {
1112 init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33, 1112 init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1113 &mvtab[0][1], 2, 1, 1113 &mvtab[0][1], 2, 1,
1114 &mvtab[0][0], 2, 1, 1); 1114 &mvtab[0][0], 2, 1, 1);
1115 1115
1116 for(i=0; i<4; i++){ 1116 for(i=0; i<4; i++){
1117 init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, 1117 init_vlc(&ff_mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
1118 &wmv2_inter_table[i][0][1], 8, 4, 1118 &wmv2_inter_table[i][0][1], 8, 4,
1119 &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name? 1119 &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name?
1120 } 1120 }
1121 1121
1122 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64, 1122 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
1128 intra_MCBPC_code, 1, 1, 1); 1128 intra_MCBPC_code, 1, 1, 1);
1129 init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, 1129 init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
1130 inter_MCBPC_bits, 1, 1, 1130 inter_MCBPC_bits, 1, 1,
1131 inter_MCBPC_code, 1, 1, 1); 1131 inter_MCBPC_code, 1, 1, 1);
1132 1132
1133 init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, 1133 init_vlc(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
1134 &table_inter_intra[0][1], 2, 1, 1134 &table_inter_intra[0][1], 2, 1,
1135 &table_inter_intra[0][0], 2, 1, 1); 1135 &table_inter_intra[0][0], 2, 1, 1);
1136 } 1136 }
1137 1137
1138 switch(s->msmpeg4_version){ 1138 switch(s->msmpeg4_version){
1143 case 3: 1143 case 3:
1144 case 4: 1144 case 4:
1145 s->decode_mb= msmpeg4v34_decode_mb; 1145 s->decode_mb= msmpeg4v34_decode_mb;
1146 break; 1146 break;
1147 case 5: 1147 case 5:
1148 s->decode_mb= wmv2_decode_mb; 1148 s->decode_mb= ff_wmv2_decode_mb;
1149 case 6: 1149 case 6:
1150 //FIXME + TODO VC1 decode mb 1150 //FIXME + TODO VC1 decode mb
1151 break; 1151 break;
1152 } 1152 }
1153 1153
1506 } 1506 }
1507 } 1507 }
1508 1508
1509 s->dsp.clear_blocks(s->block[0]); 1509 s->dsp.clear_blocks(s->block[0]);
1510 for (i = 0; i < 6; i++) { 1510 for (i = 0; i < 6; i++) {
1511 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) 1511 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1512 { 1512 {
1513 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); 1513 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1514 return -1; 1514 return -1;
1515 } 1515 }
1516 } 1516 }
1539 1539
1540 return 0; 1540 return 0;
1541 } 1541 }
1542 } 1542 }
1543 1543
1544 code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3); 1544 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
1545 if (code < 0) 1545 if (code < 0)
1546 return -1; 1546 return -1;
1547 //s->mb_intra = (code & 0x40) ? 0 : 1; 1547 //s->mb_intra = (code & 0x40) ? 0 : 1;
1548 s->mb_intra = (~code & 0x40) >> 6; 1548 s->mb_intra = (~code & 0x40) >> 6;
1549 1549
1556 /* predict coded block pattern */ 1556 /* predict coded block pattern */
1557 cbp = 0; 1557 cbp = 0;
1558 for(i=0;i<6;i++) { 1558 for(i=0;i<6;i++) {
1559 int val = ((code >> (5 - i)) & 1); 1559 int val = ((code >> (5 - i)) & 1);
1560 if (i < 4) { 1560 if (i < 4) {
1561 int pred = coded_block_pred(s, i, &coded_val); 1561 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
1562 val = val ^ pred; 1562 val = val ^ pred;
1563 *coded_val = val; 1563 *coded_val = val;
1564 } 1564 }
1565 cbp |= val << (5 - i); 1565 cbp |= val << (5 - i);
1566 } 1566 }
1572 if(s->per_mb_rl_table && cbp){ 1572 if(s->per_mb_rl_table && cbp){
1573 s->rl_table_index = decode012(&s->gb); 1573 s->rl_table_index = decode012(&s->gb);
1574 s->rl_chroma_table_index = s->rl_table_index; 1574 s->rl_chroma_table_index = s->rl_table_index;
1575 } 1575 }
1576 h263_pred_motion(s, 0, 0, &mx, &my); 1576 h263_pred_motion(s, 0, 0, &mx, &my);
1577 if (msmpeg4_decode_motion(s, &mx, &my) < 0) 1577 if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
1578 return -1; 1578 return -1;
1579 s->mv_dir = MV_DIR_FORWARD; 1579 s->mv_dir = MV_DIR_FORWARD;
1580 s->mv_type = MV_TYPE_16X16; 1580 s->mv_type = MV_TYPE_16X16;
1581 s->mv[0][0][0] = mx; 1581 s->mv[0][0][0] = mx;
1582 s->mv[0][0][1] = my; 1582 s->mv[0][0][1] = my;
1584 } else { 1584 } else {
1585 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24)); 1585 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
1586 s->ac_pred = get_bits1(&s->gb); 1586 s->ac_pred = get_bits1(&s->gb);
1587 *mb_type_ptr = MB_TYPE_INTRA; 1587 *mb_type_ptr = MB_TYPE_INTRA;
1588 if(s->inter_intra_pred){ 1588 if(s->inter_intra_pred){
1589 s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); 1589 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
1590 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); 1590 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
1591 } 1591 }
1592 if(s->per_mb_rl_table && cbp){ 1592 if(s->per_mb_rl_table && cbp){
1593 s->rl_table_index = decode012(&s->gb); 1593 s->rl_table_index = decode012(&s->gb);
1594 s->rl_chroma_table_index = s->rl_table_index; 1594 s->rl_chroma_table_index = s->rl_table_index;
1595 } 1595 }
1596 } 1596 }
1597 1597
1598 s->dsp.clear_blocks(s->block[0]); 1598 s->dsp.clear_blocks(s->block[0]);
1599 for (i = 0; i < 6; i++) { 1599 for (i = 0; i < 6; i++) {
1600 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) 1600 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
1601 { 1601 {
1602 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); 1602 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
1603 return -1; 1603 return -1;
1604 } 1604 }
1605 } 1605 }
1606 1606
1607 return 0; 1607 return 0;
1608 } 1608 }
1609 //#define ERROR_DETAILS 1609 //#define ERROR_DETAILS
1610 static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 1610 int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1611 int n, int coded, const uint8_t *scan_table) 1611 int n, int coded, const uint8_t *scan_table)
1612 { 1612 {
1613 int level, i, last, run, run_diff; 1613 int level, i, last, run, run_diff;
1614 int dc_pred_dir; 1614 int dc_pred_dir;
1615 RLTable *rl; 1615 RLTable *rl;
1909 } 1909 }
1910 1910
1911 return level; 1911 return level;
1912 } 1912 }
1913 1913
1914 static int msmpeg4_decode_motion(MpegEncContext * s, 1914 int ff_msmpeg4_decode_motion(MpegEncContext * s,
1915 int *mx_ptr, int *my_ptr) 1915 int *mx_ptr, int *my_ptr)
1916 { 1916 {
1917 MVTable *mv; 1917 MVTable *mv;
1918 int code, mx, my; 1918 int code, mx, my;
1919 1919