2
2
* MSMPEG4 backend for ffmpeg encoder and decoder
3
3
* Copyright (c) 2001 Fabrice Bellard.
5
* This library is free software; you can redistribute it and/or
4
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6
* This file is part of FFmpeg.
8
* FFmpeg is free software; you can redistribute it and/or
6
9
* modify it under the terms of the GNU Lesser General Public
7
10
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
11
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
13
* FFmpeg is distributed in the hope that it will be useful,
11
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
16
* Lesser General Public License for more details.
15
18
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* License along with FFmpeg; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
22
* msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
532
479
uint8_t *coded_block;
534
481
handle_slices(s);
536
483
if (!s->mb_intra) {
538
set_stat(ST_INTER_MB);
540
for (i = 0; i < 6; i++) {
541
if (s->block_last_index[i] >= 0)
544
if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
545
/* skip macroblock */
546
put_bits(&s->pb, 1, 1);
486
for (i = 0; i < 6; i++) {
487
if (s->block_last_index[i] >= 0)
490
if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
491
/* skip macroblock */
492
put_bits(&s->pb, 1, 1);
553
499
if (s->use_skip_mb_code)
554
put_bits(&s->pb, 1, 0); /* mb coded */
500
put_bits(&s->pb, 1, 0); /* mb coded */
556
502
if(s->msmpeg4_version<=2){
558
v2_mb_type[cbp&3][1],
504
v2_mb_type[cbp&3][1],
559
505
v2_mb_type[cbp&3][0]);
560
506
if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
561
507
else coded_cbp= cbp;
564
cbpy_tab[coded_cbp>>2][1],
510
cbpy_tab[coded_cbp>>2][1],
565
511
cbpy_tab[coded_cbp>>2][0]);
567
513
s->misc_bits += get_bits_diff(s);
569
h263_pred_motion(s, 0, &pred_x, &pred_y);
515
h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
570
516
msmpeg4v2_encode_motion(s, motion_x - pred_x);
571
517
msmpeg4v2_encode_motion(s, motion_y - pred_y);
574
table_mb_non_intra[cbp + 64][1],
520
table_mb_non_intra[cbp + 64][1],
575
521
table_mb_non_intra[cbp + 64][0]);
577
523
s->misc_bits += get_bits_diff(s);
579
525
/* motion vector */
580
h263_pred_motion(s, 0, &pred_x, &pred_y);
581
msmpeg4_encode_motion(s, motion_x - pred_x,
526
h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
527
msmpeg4_encode_motion(s, motion_x - pred_x,
582
528
motion_y - pred_y);
612
558
if(s->msmpeg4_version<=2){
613
559
if (s->pict_type == I_TYPE) {
615
561
v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
617
563
if (s->use_skip_mb_code)
618
put_bits(&s->pb, 1, 0); /* mb coded */
620
v2_mb_type[(cbp&3) + 4][1],
564
put_bits(&s->pb, 1, 0); /* mb coded */
566
v2_mb_type[(cbp&3) + 4][1],
621
567
v2_mb_type[(cbp&3) + 4][0]);
623
put_bits(&s->pb, 1, 0); /* no AC prediction yet */
569
put_bits(&s->pb, 1, 0); /* no AC prediction yet */
626
572
cbpy_tab[cbp>>2][0]);
628
574
if (s->pict_type == I_TYPE) {
629
set_stat(ST_INTRA_MB);
631
table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
576
ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
633
578
if (s->use_skip_mb_code)
634
put_bits(&s->pb, 1, 0); /* mb coded */
636
table_mb_non_intra[cbp][1],
579
put_bits(&s->pb, 1, 0); /* mb coded */
581
table_mb_non_intra[cbp][1],
637
582
table_mb_non_intra[cbp][0]);
639
set_stat(ST_INTRA_MB);
640
put_bits(&s->pb, 1, 0); /* no AC prediction yet */
584
put_bits(&s->pb, 1, 0); /* no AC prediction yet */
641
585
if(s->inter_intra_pred){
642
586
s->h263_aic_dir=0;
643
587
put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
1127
1053
for(i=0;i<NB_RL_TABLES;i++) {
1128
init_rl(&rl_table[i]);
1129
init_vlc_rl(&rl_table[i]);
1054
init_rl(&rl_table[i], 1);
1055
init_vlc_rl(&rl_table[i], 1);
1131
1057
for(i=0;i<2;i++) {
1132
1058
mv = &mv_tables[i];
1133
init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1,
1059
init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1,
1134
1060
mv->table_mv_bits, 1, 1,
1135
mv->table_mv_code, 2, 2);
1061
mv->table_mv_code, 2, 2, 1);
1138
init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120,
1139
&table0_dc_lum[0][1], 8, 4,
1140
&table0_dc_lum[0][0], 8, 4);
1141
init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120,
1142
&table0_dc_chroma[0][1], 8, 4,
1143
&table0_dc_chroma[0][0], 8, 4);
1144
init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120,
1145
&table1_dc_lum[0][1], 8, 4,
1146
&table1_dc_lum[0][0], 8, 4);
1147
init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120,
1148
&table1_dc_chroma[0][1], 8, 4,
1149
&table1_dc_chroma[0][0], 8, 4);
1151
init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
1064
init_vlc(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
1065
&ff_table0_dc_lum[0][1], 8, 4,
1066
&ff_table0_dc_lum[0][0], 8, 4, 1);
1067
init_vlc(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
1068
&ff_table0_dc_chroma[0][1], 8, 4,
1069
&ff_table0_dc_chroma[0][0], 8, 4, 1);
1070
init_vlc(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
1071
&ff_table1_dc_lum[0][1], 8, 4,
1072
&ff_table1_dc_lum[0][0], 8, 4, 1);
1073
init_vlc(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
1074
&ff_table1_dc_chroma[0][1], 8, 4,
1075
&ff_table1_dc_chroma[0][0], 8, 4, 1);
1077
init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
1152
1078
&v2_dc_lum_table[0][1], 8, 4,
1153
&v2_dc_lum_table[0][0], 8, 4);
1154
init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
1079
&v2_dc_lum_table[0][0], 8, 4, 1);
1080
init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
1155
1081
&v2_dc_chroma_table[0][1], 8, 4,
1156
&v2_dc_chroma_table[0][0], 8, 4);
1082
&v2_dc_chroma_table[0][0], 8, 4, 1);
1158
1084
init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
1159
1085
&cbpy_tab[0][1], 2, 1,
1160
&cbpy_tab[0][0], 2, 1);
1086
&cbpy_tab[0][0], 2, 1, 1);
1161
1087
init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
1162
1088
&v2_intra_cbpc[0][1], 2, 1,
1163
&v2_intra_cbpc[0][0], 2, 1);
1089
&v2_intra_cbpc[0][0], 2, 1, 1);
1164
1090
init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
1165
1091
&v2_mb_type[0][1], 2, 1,
1166
&v2_mb_type[0][0], 2, 1);
1092
&v2_mb_type[0][0], 2, 1, 1);
1167
1093
init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
1168
1094
&mvtab[0][1], 2, 1,
1169
&mvtab[0][0], 2, 1);
1095
&mvtab[0][0], 2, 1, 1);
1171
1097
for(i=0; i<4; i++){
1172
init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
1098
init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
1173
1099
&wmv2_inter_table[i][0][1], 8, 4,
1174
&wmv2_inter_table[i][0][0], 8, 4); //FIXME name?
1100
&wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name?
1177
init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64,
1178
&table_mb_intra[0][1], 4, 2,
1179
&table_mb_intra[0][0], 4, 2);
1181
init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
1103
init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
1104
&ff_msmp4_mb_i_table[0][1], 4, 2,
1105
&ff_msmp4_mb_i_table[0][0], 4, 2, 1);
1107
init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
1182
1108
intra_MCBPC_bits, 1, 1,
1183
intra_MCBPC_code, 1, 1);
1184
init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
1109
intra_MCBPC_code, 1, 1, 1);
1110
init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
1185
1111
inter_MCBPC_bits, 1, 1,
1186
inter_MCBPC_code, 1, 1);
1188
init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
1112
inter_MCBPC_code, 1, 1, 1);
1114
init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
1189
1115
&table_inter_intra[0][1], 2, 1,
1190
&table_inter_intra[0][0], 2, 1);
1116
&table_inter_intra[0][0], 2, 1, 1);
1193
1119
switch(s->msmpeg4_version){
1532
1452
cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
1533
1453
if(cbp<0 || cbp>3){
1534
fprintf(stderr, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1454
av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1539
1459
if (!s->mb_intra) {
1540
1460
int mx, my, cbpy;
1542
1462
cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
1544
fprintf(stderr, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1464
av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
1549
1469
if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
1551
h263_pred_motion(s, 0, &mx, &my);
1471
h263_pred_motion(s, 0, 0, &mx, &my);
1552
1472
mx= msmpeg4v2_decode_motion(s, mx, 1);
1553
1473
my= msmpeg4v2_decode_motion(s, my, 1);
1555
1475
s->mv_dir = MV_DIR_FORWARD;
1556
1476
s->mv_type = MV_TYPE_16X16;
1557
1477
s->mv[0][0][0] = mx;
1788
1703
//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
1789
1704
UPDATE_CACHE(re, &s->gb);
1791
run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1706
run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
1792
1707
SKIP_BITS(re, &s->gb, s->esc3_run_length);
1794
sign= SHOW_UBITS(re, &s->gb, 1);
1709
sign= SHOW_UBITS(re, &s->gb, 1);
1795
1710
SKIP_BITS(re, &s->gb, 1);
1797
level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1712
level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
1798
1713
SKIP_BITS(re, &s->gb, s->esc3_level_length);
1799
1714
if(sign) level= -level;
1801
1716
//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
1802
1717
#if 0 // waste of time / this will detect very few errors
1804
const int abs_level= ABS(level);
1719
const int abs_level= FFABS(level);
1805
1720
const int run1= run - rl->max_run[last][abs_level] - run_diff;
1806
1721
if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
1807
1722
if(abs_level <= rl->max_level[last][run]){
1808
fprintf(stderr, "illegal 3. esc, vlc encoding possible\n");
1723
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1809
1724
return DECODING_AC_LOST;
1811
1726
if(abs_level <= rl->max_level[last][run]*2){
1812
fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n");
1727
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1813
1728
return DECODING_AC_LOST;
1815
1730
if(run1>=0 && abs_level <= rl->max_level[last][run1]){
1816
fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n");
1731
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1817
1732
return DECODING_AC_LOST;
1822
//level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1823
if (level>0) level= level * qmul + qadd;
1737
//level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
1738
if (level>0) level= level * qmul + qadd;
1824
1739
else level= level * qmul - qadd;
1825
1740
#if 0 // waste of time too :(
1826
1741
if(level>2048 || level<-2048){
1827
fprintf(stderr, "|level| overflow in 3. esc\n");
1742
av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
1828
1743
return DECODING_AC_LOST;