~siretart/xine-lib/ubuntu

« back to all changes in this revision

Viewing changes to src/libffmpeg/libavcodec/mpeg12.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-12-15 13:13:45 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051215131345-8n4osv1j7fy9c1s1
* SECURITY UPDATE: Fix arbitrary code execution with crafted PNG images in
  embedded ffmpeg copy.
* src/libffmpeg/libavcodec/utils.c, avcodec_default_get_buffer(): Apply
  upstream patch to fix buffer overflow on decoding of small PIX_FMT_PAL8
  PNG files.
* References:
  CVE-2005-4048
  http://mplayerhq.hu/pipermail/ffmpeg-devel/2005-November/005333.html
  http://www1.mplayerhq.hu/cgi-bin/cvsweb.cgi/ffmpeg/libavcodec/
  utils.c.diff?r1=1.161&r2=1.162&cvsroot=FFMpeg

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
                         int component);
66
66
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code);    // RAL: f_code parameter added
67
67
#endif //CONFIG_ENCODERS
 
68
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
 
69
                              DCTELEM *block, 
 
70
                              int n);
 
71
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
 
72
                              DCTELEM *block, 
 
73
                              int n);
 
74
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
 
75
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
 
76
                                        DCTELEM *block, 
 
77
                                        int n);
 
78
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
 
79
                                    DCTELEM *block, 
 
80
                                    int n);
 
81
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
 
82
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
68
83
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
69
84
static void exchange_uv(MpegEncContext *s);
70
85
 
97
112
static int8_t mpeg1_max_level[2][64];
98
113
#endif //CONFIG_ENCODERS
99
114
 
100
 
static void init_2d_vlc_rl(RLTable *rl)
 
115
static void init_2d_vlc_rl(RLTable *rl, int use_static)
101
116
{
102
117
    int i;
103
118
    
104
119
    init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, 
105
120
             &rl->table_vlc[0][1], 4, 2,
106
 
             &rl->table_vlc[0][0], 4, 2);
107
 
 
108
 
    
109
 
    rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
 
121
             &rl->table_vlc[0][0], 4, 2, use_static);
 
122
 
 
123
    if(use_static)    
 
124
        rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
 
125
    else
 
126
        rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
 
127
 
110
128
    for(i=0; i<rl->vlc.table_size; i++){
111
129
        int code= rl->vlc.table[i][0];
112
130
        int len = rl->vlc.table[i][1];
190
208
    int64_t d;
191
209
 
192
210
    for(i=1;i<14;i++) {
193
 
        int64_t n0= 1001LL/frame_rate_tab[i].den*frame_rate_tab[i].num*s->avctx->frame_rate_base;
194
 
        int64_t n1= 1001LL*s->avctx->frame_rate;
195
 
        if(s->avctx->strict_std_compliance >= 0 && i>=9) break;
 
211
        int64_t n0= 1001LL/frame_rate_tab[i].den*frame_rate_tab[i].num*s->avctx->time_base.num;
 
212
        int64_t n1= 1001LL*s->avctx->time_base.den;
 
213
        if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
196
214
 
197
215
        d = ABS(n0 - n1);
198
216
        if(d < dmin){
214
232
        return -1;
215
233
 
216
234
    if(find_frame_rate_index(s) < 0){
217
 
        if(s->strict_std_compliance >=0){
218
 
            av_log(avctx, AV_LOG_ERROR, "MPEG1/2 doesnt support %d/%d fps\n", avctx->frame_rate, avctx->frame_rate_base);
 
235
        if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
 
236
            av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
219
237
            return -1;
220
238
        }else{
221
 
            av_log(avctx, AV_LOG_INFO, "MPEG1/2 doesnt support %d/%d fps, there may be AV sync issues\n", avctx->frame_rate, avctx->frame_rate_base);
 
239
            av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
222
240
        }
223
241
    }
224
242
    
296
314
                s->mb_width * s->mb_height <= 396 &&
297
315
                s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
298
316
                framerate.num <= framerate.den*30 &&
 
317
                s->avctx->me_range && s->avctx->me_range < 128 &&
299
318
                vbv_buffer_size <= 20 &&
300
319
                v <= 1856000/400 &&
301
320
                s->codec_id == CODEC_ID_MPEG1VIDEO;
309
328
                put_header(s, EXT_START_CODE);
310
329
                put_bits(&s->pb, 4, 1); //seq ext
311
330
                put_bits(&s->pb, 1, 0); //esc
312
 
                put_bits(&s->pb, 3, 4); //profile
313
 
                put_bits(&s->pb, 4, 8); //level
 
331
                
 
332
                if(s->avctx->profile == FF_PROFILE_UNKNOWN){
 
333
                    put_bits(&s->pb, 3, 4); //profile
 
334
                }else{
 
335
                    put_bits(&s->pb, 3, s->avctx->profile); //profile
 
336
                }
 
337
 
 
338
                if(s->avctx->level == FF_LEVEL_UNKNOWN){
 
339
                    put_bits(&s->pb, 4, 8); //level
 
340
                }else{
 
341
                    put_bits(&s->pb, 4, s->avctx->level); //level
 
342
                }
 
343
 
314
344
                put_bits(&s->pb, 1, s->progressive_sequence);
315
345
                put_bits(&s->pb, 2, 1); //chroma format 4:2:0
316
346
                put_bits(&s->pb, 2, 0); //horizontal size ext
445
475
        put_bits(&s->pb, 1, s->intra_vlc_format);
446
476
        put_bits(&s->pb, 1, s->alternate_scan);
447
477
        put_bits(&s->pb, 1, s->repeat_first_field);
448
 
        put_bits(&s->pb, 1, s->chroma_420_type=1);
449
478
        s->progressive_frame = s->progressive_sequence;
 
479
        put_bits(&s->pb, 1, s->chroma_420_type=s->progressive_frame);
450
480
        put_bits(&s->pb, 1, s->progressive_frame);
451
481
        put_bits(&s->pb, 1, 0); //composite_display_flag
452
482
    }
691
721
// RAL: Parameter added: f_or_b_code
692
722
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
693
723
{
694
 
    int code, bit_size, l, m, bits, range, sign;
 
724
    int code, bit_size, l, bits, range, sign;
695
725
 
696
726
    if (val == 0) {
697
727
        /* zero vector */
703
733
        bit_size = f_or_b_code - 1;
704
734
        range = 1 << bit_size;
705
735
        /* modulo encoding */
706
 
        l = 16 * range;
707
 
        m = 2 * l;
708
 
        if (val < -l) {
709
 
            val += m;
710
 
        } else if (val >= l) {
711
 
            val -= m;
712
 
        }
 
736
        l= INT_BIT - 5 - bit_size;
 
737
        val= (val<<l)>>l;
713
738
 
714
739
        if (val >= 0) {
715
740
            val--;
749
774
        int i;
750
775
 
751
776
        done=1;
752
 
        init_rl(&rl_mpeg1);
 
777
        init_rl(&rl_mpeg1, 1);
753
778
 
754
779
        for(i=0; i<64; i++)
755
780
        {
977
1002
 
978
1003
        init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, 
979
1004
                 vlc_dc_lum_bits, 1, 1,
980
 
                 vlc_dc_lum_code, 2, 2);
 
1005
                 vlc_dc_lum_code, 2, 2, 1);
981
1006
        init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12, 
982
1007
                 vlc_dc_chroma_bits, 1, 1,
983
 
                 vlc_dc_chroma_code, 2, 2);
 
1008
                 vlc_dc_chroma_code, 2, 2, 1);
984
1009
        init_vlc(&mv_vlc, MV_VLC_BITS, 17, 
985
1010
                 &mbMotionVectorTable[0][1], 2, 1,
986
 
                 &mbMotionVectorTable[0][0], 2, 1);
 
1011
                 &mbMotionVectorTable[0][0], 2, 1, 1);
987
1012
        init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36, 
988
1013
                 &mbAddrIncrTable[0][1], 2, 1,
989
 
                 &mbAddrIncrTable[0][0], 2, 1);
 
1014
                 &mbAddrIncrTable[0][0], 2, 1, 1);
990
1015
        init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
991
1016
                 &mbPatTable[0][1], 2, 1,
992
 
                 &mbPatTable[0][0], 2, 1);
 
1017
                 &mbPatTable[0][0], 2, 1, 1);
993
1018
        
994
1019
        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, 
995
1020
                 &table_mb_ptype[0][1], 2, 1,
996
 
                 &table_mb_ptype[0][0], 2, 1);
 
1021
                 &table_mb_ptype[0][0], 2, 1, 1);
997
1022
        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, 
998
1023
                 &table_mb_btype[0][1], 2, 1,
999
 
                 &table_mb_btype[0][0], 2, 1);
1000
 
        init_rl(&rl_mpeg1);
1001
 
        init_rl(&rl_mpeg2);
 
1024
                 &table_mb_btype[0][0], 2, 1, 1);
 
1025
        init_rl(&rl_mpeg1, 1);
 
1026
        init_rl(&rl_mpeg2, 1);
1002
1027
 
1003
 
        init_2d_vlc_rl(&rl_mpeg1);
1004
 
        init_2d_vlc_rl(&rl_mpeg2);
 
1028
        init_2d_vlc_rl(&rl_mpeg1, 1);
 
1029
        init_2d_vlc_rl(&rl_mpeg2, 1);
1005
1030
    }
1006
1031
}
1007
1032
 
1023
1048
    }
1024
1049
}
1025
1050
 
1026
 
static inline int decode_dc(GetBitContext *gb, int component)
1027
 
{
1028
 
    int code, diff;
1029
 
 
1030
 
    if (component == 0) {
1031
 
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1032
 
    } else {
1033
 
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1034
 
    }
1035
 
    if (code < 0){
1036
 
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1037
 
        return 0xffff;
1038
 
    }
1039
 
    if (code == 0) {
1040
 
        diff = 0;
1041
 
    } else {
1042
 
        diff = get_xbits(gb, code);
1043
 
    }
1044
 
    return diff;
1045
 
}
1046
 
 
1047
 
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
1048
 
                               DCTELEM *block, 
1049
 
                               int n)
1050
 
{
1051
 
    int level, dc, diff, i, j, run;
1052
 
    int component;
1053
 
    RLTable *rl = &rl_mpeg1;
1054
 
    uint8_t * const scantable= s->intra_scantable.permutated;
1055
 
    const uint16_t *quant_matrix= s->intra_matrix;
1056
 
    const int qscale= s->qscale;
1057
 
 
1058
 
    /* DC coef */
1059
 
    component = (n <= 3 ? 0 : n - 4 + 1);
1060
 
    diff = decode_dc(&s->gb, component);
1061
 
    if (diff >= 0xffff)
1062
 
        return -1;
1063
 
    dc = s->last_dc[component];
1064
 
    dc += diff;
1065
 
    s->last_dc[component] = dc;
1066
 
    block[0] = dc<<3;
1067
 
    dprintf("dc=%d diff=%d\n", dc, diff);
1068
 
    i = 0;
1069
 
    {
1070
 
        OPEN_READER(re, &s->gb);    
1071
 
        /* now quantify & encode AC coefs */
1072
 
        for(;;) {
1073
 
            UPDATE_CACHE(re, &s->gb);
1074
 
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1075
 
            
1076
 
            if(level == 127){
1077
 
                break;
1078
 
            } else if(level != 0) {
1079
 
                i += run;
1080
 
                j = scantable[i];
1081
 
                level= (level*qscale*quant_matrix[j])>>4;
1082
 
                level= (level-1)|1;
1083
 
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1084
 
                LAST_SKIP_BITS(re, &s->gb, 1);
1085
 
            } else {
1086
 
                /* escape */
1087
 
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1088
 
                UPDATE_CACHE(re, &s->gb);
1089
 
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1090
 
                if (level == -128) {
1091
 
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1092
 
                } else if (level == 0) {
1093
 
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1094
 
                }
1095
 
                i += run;
1096
 
                j = scantable[i];
1097
 
                if(level<0){
1098
 
                    level= -level;
1099
 
                    level= (level*qscale*quant_matrix[j])>>4;
1100
 
                    level= (level-1)|1;
1101
 
                    level= -level;
1102
 
                }else{
1103
 
                    level= (level*qscale*quant_matrix[j])>>4;
1104
 
                    level= (level-1)|1;
1105
 
                }
1106
 
            }
1107
 
            if (i > 63){
1108
 
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1109
 
                return -1;
1110
 
            }
1111
 
 
1112
 
            block[j] = level;
1113
 
        }
1114
 
        CLOSE_READER(re, &s->gb);
1115
 
    }
1116
 
    s->block_last_index[n] = i;
1117
 
   return 0;
1118
 
}
1119
 
 
1120
 
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
1121
 
                               DCTELEM *block, 
1122
 
                               int n)
1123
 
{
1124
 
    int level, i, j, run;
1125
 
    RLTable *rl = &rl_mpeg1;
1126
 
    uint8_t * const scantable= s->intra_scantable.permutated;
1127
 
    const uint16_t *quant_matrix= s->inter_matrix;
1128
 
    const int qscale= s->qscale;
1129
 
 
1130
 
    {
1131
 
        int v;
1132
 
        OPEN_READER(re, &s->gb);
1133
 
        i = -1;
1134
 
        /* special case for the first coef. no need to add a second vlc table */
1135
 
        UPDATE_CACHE(re, &s->gb);
1136
 
        v= SHOW_UBITS(re, &s->gb, 2);
1137
 
        if (v & 2) {
1138
 
            LAST_SKIP_BITS(re, &s->gb, 2);
1139
 
            level= (3*qscale*quant_matrix[0])>>5;
1140
 
            level= (level-1)|1;
1141
 
            if(v&1)
1142
 
                level= -level;
1143
 
            block[0] = level;
1144
 
            i++;
1145
 
        }
1146
 
 
1147
 
        /* now quantify & encode AC coefs */
1148
 
        for(;;) {
1149
 
            UPDATE_CACHE(re, &s->gb);
1150
 
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1151
 
            
1152
 
            if(level == 127){
1153
 
                break;
1154
 
            } else if(level != 0) {
1155
 
                i += run;
1156
 
                j = scantable[i];
1157
 
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1158
 
                level= (level-1)|1;
1159
 
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1160
 
                LAST_SKIP_BITS(re, &s->gb, 1);
1161
 
            } else {
1162
 
                /* escape */
1163
 
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1164
 
                UPDATE_CACHE(re, &s->gb);
1165
 
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1166
 
                if (level == -128) {
1167
 
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1168
 
                } else if (level == 0) {
1169
 
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1170
 
                }
1171
 
                i += run;
1172
 
                j = scantable[i];
1173
 
                if(level<0){
1174
 
                    level= -level;
1175
 
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1176
 
                    level= (level-1)|1;
1177
 
                    level= -level;
1178
 
                }else{
1179
 
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1180
 
                    level= (level-1)|1;
1181
 
                }
1182
 
            }
1183
 
            if (i > 63){
1184
 
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1185
 
                return -1;
1186
 
            }
1187
 
 
1188
 
            block[j] = level;
1189
 
        }
1190
 
        CLOSE_READER(re, &s->gb);
1191
 
    }
1192
 
    s->block_last_index[n] = i;
1193
 
    return 0;
1194
 
}
1195
 
 
1196
 
/* Also does unquantization here, since I will never support mpeg2
1197
 
   encoding */
1198
 
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1199
 
                               DCTELEM *block, 
1200
 
                               int n)
1201
 
{
1202
 
    int level, i, j, run;
1203
 
    RLTable *rl = &rl_mpeg1;
1204
 
    uint8_t * const scantable= s->intra_scantable.permutated;
1205
 
    const uint16_t *quant_matrix;
1206
 
    const int qscale= s->qscale;
1207
 
    int mismatch;
1208
 
 
1209
 
    mismatch = 1;
1210
 
 
1211
 
    {
1212
 
        int v;
1213
 
        OPEN_READER(re, &s->gb);
1214
 
        i = -1;
1215
 
        if (n < 4)
1216
 
            quant_matrix = s->inter_matrix;
1217
 
        else
1218
 
            quant_matrix = s->chroma_inter_matrix;
1219
 
 
1220
 
        /* special case for the first coef. no need to add a second vlc table */
1221
 
        UPDATE_CACHE(re, &s->gb);
1222
 
        v= SHOW_UBITS(re, &s->gb, 2);
1223
 
        if (v & 2) {
1224
 
            LAST_SKIP_BITS(re, &s->gb, 2);
1225
 
            level= (3*qscale*quant_matrix[0])>>5;
1226
 
            if(v&1)
1227
 
                level= -level;
1228
 
            block[0] = level;
1229
 
            mismatch ^= level;
1230
 
            i++;
1231
 
        }
1232
 
 
1233
 
        /* now quantify & encode AC coefs */
1234
 
        for(;;) {
1235
 
            UPDATE_CACHE(re, &s->gb);
1236
 
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1237
 
            
1238
 
            if(level == 127){
1239
 
                break;
1240
 
            } else if(level != 0) {
1241
 
                i += run;
1242
 
                j = scantable[i];
1243
 
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1244
 
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1245
 
                LAST_SKIP_BITS(re, &s->gb, 1);
1246
 
            } else {
1247
 
                /* escape */
1248
 
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1249
 
                UPDATE_CACHE(re, &s->gb);
1250
 
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1251
 
 
1252
 
                i += run;
1253
 
                j = scantable[i];
1254
 
                if(level<0){
1255
 
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1256
 
                    level= -level;
1257
 
                }else{
1258
 
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1259
 
                }
1260
 
            }
1261
 
            if (i > 63){
1262
 
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1263
 
                return -1;
1264
 
            }
1265
 
            
1266
 
            mismatch ^= level;
1267
 
            block[j] = level;
1268
 
        }
1269
 
        CLOSE_READER(re, &s->gb);
1270
 
    }
1271
 
    block[63] ^= (mismatch & 1);
1272
 
    
1273
 
    s->block_last_index[n] = i;
1274
 
    return 0;
1275
 
}
1276
 
 
1277
 
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
1278
 
                               DCTELEM *block, 
1279
 
                               int n)
1280
 
{
1281
 
    int level, dc, diff, i, j, run;
1282
 
    int component;
1283
 
    RLTable *rl;
1284
 
    uint8_t * const scantable= s->intra_scantable.permutated;
1285
 
    const uint16_t *quant_matrix;
1286
 
    const int qscale= s->qscale;
1287
 
    int mismatch;
1288
 
 
1289
 
    /* DC coef */
1290
 
    if (n < 4){
1291
 
        quant_matrix = s->intra_matrix;
1292
 
        component = 0; 
1293
 
    }else{
1294
 
        quant_matrix = s->chroma_intra_matrix;
1295
 
        component = (n&1) + 1;
1296
 
    }
1297
 
    diff = decode_dc(&s->gb, component);
1298
 
    if (diff >= 0xffff)
1299
 
        return -1;
1300
 
    dc = s->last_dc[component];
1301
 
    dc += diff;
1302
 
    s->last_dc[component] = dc;
1303
 
    block[0] = dc << (3 - s->intra_dc_precision);
1304
 
    dprintf("dc=%d\n", block[0]);
1305
 
    mismatch = block[0] ^ 1;
1306
 
    i = 0;
1307
 
    if (s->intra_vlc_format)
1308
 
        rl = &rl_mpeg2;
1309
 
    else
1310
 
        rl = &rl_mpeg1;
1311
 
 
1312
 
    {
1313
 
        OPEN_READER(re, &s->gb);    
1314
 
        /* now quantify & encode AC coefs */
1315
 
        for(;;) {
1316
 
            UPDATE_CACHE(re, &s->gb);
1317
 
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1318
 
            
1319
 
            if(level == 127){
1320
 
                break;
1321
 
            } else if(level != 0) {
1322
 
                i += run;
1323
 
                j = scantable[i];
1324
 
                level= (level*qscale*quant_matrix[j])>>4;
1325
 
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1326
 
                LAST_SKIP_BITS(re, &s->gb, 1);
1327
 
            } else {
1328
 
                /* escape */
1329
 
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1330
 
                UPDATE_CACHE(re, &s->gb);
1331
 
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1332
 
                i += run;
1333
 
                j = scantable[i];
1334
 
                if(level<0){
1335
 
                    level= (-level*qscale*quant_matrix[j])>>4;
1336
 
                    level= -level;
1337
 
                }else{
1338
 
                    level= (level*qscale*quant_matrix[j])>>4;
1339
 
                }
1340
 
            }
1341
 
            if (i > 63){
1342
 
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1343
 
                return -1;
1344
 
            }
1345
 
            
1346
 
            mismatch^= level;
1347
 
            block[j] = level;
1348
 
        }
1349
 
        CLOSE_READER(re, &s->gb);
1350
 
    }
1351
 
    block[63]^= mismatch&1;
1352
 
    
1353
 
    s->block_last_index[n] = i;
1354
 
    return 0;
1355
 
}
1356
 
 
1357
1051
/* motion type (for mpeg2) */
1358
1052
#define MT_FIELD 1
1359
1053
#define MT_FRAME 2
1364
1058
                          DCTELEM block[12][64])
1365
1059
{
1366
1060
    int i, j, k, cbp, val, mb_type, motion_type;
1367
 
    
 
1061
    const int mb_block_count = 4 + (1<< s->chroma_format);
 
1062
 
1368
1063
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1369
1064
 
1370
 
    assert(s->mb_skiped==0);
 
1065
    assert(s->mb_skipped==0);
1371
1066
 
1372
1067
    if (s->mb_skip_run-- != 0) {
1373
1068
        if(s->pict_type == I_TYPE){
1374
 
            av_log(s->avctx, AV_LOG_ERROR, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
 
1069
            av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1375
1070
            return -1;
1376
1071
        }
1377
1072
    
1390
1085
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1391
1086
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1392
1087
            s->field_select[0][0]= s->picture_structure - 1;
1393
 
            s->mb_skiped = 1;
 
1088
            s->mb_skipped = 1;
1394
1089
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1395
1090
        } else {
 
1091
            int mb_type;
 
1092
            
 
1093
            if(s->mb_x)
 
1094
                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
 
1095
            else
 
1096
                mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all, 
 
1097
            if(IS_INTRA(mb_type))
 
1098
                return -1;
 
1099
            
1396
1100
            /* if B type, reuse previous vectors and directions */
1397
1101
            s->mv[0][0][0] = s->last_mv[0][0][0];
1398
1102
            s->mv[0][0][1] = s->last_mv[0][0][1];
1400
1104
            s->mv[1][0][1] = s->last_mv[1][0][1];
1401
1105
 
1402
1106
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= 
1403
 
                s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
 
1107
                mb_type | MB_TYPE_SKIP;
1404
1108
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1405
1109
 
1406
1110
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) 
1407
 
                s->mb_skiped = 1;
 
1111
                s->mb_skipped = 1;
1408
1112
        }
1409
1113
 
1410
1114
        return 0;
1443
1147
    dprintf("mb_type=%x\n", mb_type);
1444
1148
//    motion_type = 0; /* avoid warning */
1445
1149
    if (IS_INTRA(mb_type)) {
 
1150
        s->dsp.clear_blocks(s->block[0]);
 
1151
    
 
1152
        if(!s->chroma_y_shift){
 
1153
            s->dsp.clear_blocks(s->block[6]);
 
1154
        }
 
1155
    
1446
1156
        /* compute dct type */
1447
1157
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1448
1158
            !s->frame_pred_frame_dct) {
1477
1187
#endif
1478
1188
 
1479
1189
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1480
 
            for(i=0;i<4+(1<<s->chroma_format);i++) {
1481
 
                if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1482
 
                    return -1;
 
1190
            if(s->flags2 & CODEC_FLAG2_FAST){
 
1191
                for(i=0;i<6;i++) {
 
1192
                    mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
 
1193
                }
 
1194
            }else{
 
1195
                for(i=0;i<mb_block_count;i++) {
 
1196
                    if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
 
1197
                        return -1;
 
1198
                }
1483
1199
            }
1484
1200
        } else {
1485
1201
            for(i=0;i<6;i++) {
1654
1370
        }
1655
1371
        
1656
1372
        s->mb_intra = 0;
1657
 
 
1658
1373
        if (HAS_CBP(mb_type)) {
 
1374
            s->dsp.clear_blocks(s->block[0]);
 
1375
        
 
1376
            if(!s->chroma_y_shift){
 
1377
                s->dsp.clear_blocks(s->block[6]);
 
1378
            }
 
1379
 
1659
1380
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1660
1381
            if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
1661
1382
                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1662
1383
                return -1;
1663
1384
            }
1664
 
            if(s->chroma_format == 2){//CHROMA422
1665
 
                 cbp|= ( get_bits(&s->gb,2) ) << 6;
1666
 
            }else
1667
 
            if(s->chroma_format >  2){//CHROMA444
1668
 
                 cbp|= ( get_bits(&s->gb,6) ) << 6;
 
1385
            if(mb_block_count > 6){
 
1386
                 cbp<<= mb_block_count-6;
 
1387
                 cbp |= get_bits(&s->gb, mb_block_count-6);
1669
1388
            }
1670
1389
 
1671
1390
#ifdef HAVE_XVMC
1679
1398
#endif
1680
1399
 
1681
1400
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1682
 
                for(i=0;i<6;i++) {
1683
 
                    if (cbp & (1<<(5-i)) ) {
1684
 
                        if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1685
 
                            return -1;
1686
 
                    } else {
1687
 
                        s->block_last_index[i] = -1;
 
1401
                if(s->flags2 & CODEC_FLAG2_FAST){
 
1402
                    for(i=0;i<6;i++) {
 
1403
                        if(cbp & 32) {
 
1404
                            mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
 
1405
                        } else {
 
1406
                            s->block_last_index[i] = -1;
 
1407
                        }
 
1408
                        cbp+=cbp;
1688
1409
                    }
1689
 
                }
1690
 
                if (s->chroma_format >= 2) {
1691
 
                    if (s->chroma_format == 2) {//CHROMA_422)
1692
 
                        for(i=6;i<8;i++) {
1693
 
                            if (cbp & (1<<(6+7-i)) ) {
1694
 
                                if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1695
 
                                    return -1;
1696
 
                            } else {
1697
 
                                s->block_last_index[i] = -1;
1698
 
                            }
1699
 
                        }
1700
 
                    }else{ /*CHROMA_444*/
1701
 
                        for(i=6;i<12;i++) {
1702
 
                            if (cbp & (1<<(6+11-i)) ) {
1703
 
                                if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1704
 
                                    return -1;
1705
 
                            } else {
1706
 
                                s->block_last_index[i] = -1;
1707
 
                            }
1708
 
                        }
 
1410
                }else{
 
1411
                    cbp<<= 12-mb_block_count;
 
1412
    
 
1413
                    for(i=0;i<mb_block_count;i++) {
 
1414
                        if ( cbp & (1<<11) ) {
 
1415
                            if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
 
1416
                                return -1;
 
1417
                        } else {
 
1418
                            s->block_last_index[i] = -1;
 
1419
                        }
 
1420
                        cbp+=cbp;
1709
1421
                    }
1710
1422
                }
1711
1423
            } else {
1712
 
                for(i=0;i<6;i++) {
1713
 
                    if (cbp & 32) {
1714
 
                        if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1715
 
                            return -1;
1716
 
                    } else {
1717
 
                        s->block_last_index[i] = -1;
1718
 
                    }
1719
 
                    cbp+=cbp;
 
1424
                if(s->flags2 & CODEC_FLAG2_FAST){
 
1425
                    for(i=0;i<6;i++) {
 
1426
                        if (cbp & 32) {
 
1427
                            mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
 
1428
                        } else {
 
1429
                            s->block_last_index[i] = -1;
 
1430
                        }
 
1431
                        cbp+=cbp;
 
1432
                    }
 
1433
                }else{
 
1434
                    for(i=0;i<6;i++) {
 
1435
                        if (cbp & 32) {
 
1436
                            if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
 
1437
                                return -1;
 
1438
                        } else {
 
1439
                            s->block_last_index[i] = -1;
 
1440
                        }
 
1441
                        cbp+=cbp;
 
1442
                    }
1720
1443
                }
1721
1444
            }
1722
1445
        }else{
1756
1479
    val += pred;
1757
1480
    
1758
1481
    /* modulo decoding */
1759
 
    l = 1 << (shift+4);
1760
 
    val = ((val + l)&(l*2-1)) - l;
 
1482
    l= INT_BIT - 5 - shift;
 
1483
    val = (val<<l)>>l;
1761
1484
    return val;
1762
1485
}
1763
1486
 
 
1487
static inline int decode_dc(GetBitContext *gb, int component)
 
1488
{
 
1489
    int code, diff;
 
1490
 
 
1491
    if (component == 0) {
 
1492
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
 
1493
    } else {
 
1494
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
 
1495
    }
 
1496
    if (code < 0){
 
1497
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
 
1498
        return 0xffff;
 
1499
    }
 
1500
    if (code == 0) {
 
1501
        diff = 0;
 
1502
    } else {
 
1503
        diff = get_xbits(gb, code);
 
1504
    }
 
1505
    return diff;
 
1506
}
 
1507
 
 
1508
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
 
1509
                               DCTELEM *block, 
 
1510
                               int n)
 
1511
{
 
1512
    int level, dc, diff, i, j, run;
 
1513
    int component;
 
1514
    RLTable *rl = &rl_mpeg1;
 
1515
    uint8_t * const scantable= s->intra_scantable.permutated;
 
1516
    const uint16_t *quant_matrix= s->intra_matrix;
 
1517
    const int qscale= s->qscale;
 
1518
 
 
1519
    /* DC coef */
 
1520
    component = (n <= 3 ? 0 : n - 4 + 1);
 
1521
    diff = decode_dc(&s->gb, component);
 
1522
    if (diff >= 0xffff)
 
1523
        return -1;
 
1524
    dc = s->last_dc[component];
 
1525
    dc += diff;
 
1526
    s->last_dc[component] = dc;
 
1527
    block[0] = dc<<3;
 
1528
    dprintf("dc=%d diff=%d\n", dc, diff);
 
1529
    i = 0;
 
1530
    {
 
1531
        OPEN_READER(re, &s->gb);    
 
1532
        /* now quantify & encode AC coefs */
 
1533
        for(;;) {
 
1534
            UPDATE_CACHE(re, &s->gb);
 
1535
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1536
            
 
1537
            if(level == 127){
 
1538
                break;
 
1539
            } else if(level != 0) {
 
1540
                i += run;
 
1541
                j = scantable[i];
 
1542
                level= (level*qscale*quant_matrix[j])>>4;
 
1543
                level= (level-1)|1;
 
1544
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
1545
                LAST_SKIP_BITS(re, &s->gb, 1);
 
1546
            } else {
 
1547
                /* escape */
 
1548
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
1549
                UPDATE_CACHE(re, &s->gb);
 
1550
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
 
1551
                if (level == -128) {
 
1552
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
 
1553
                } else if (level == 0) {
 
1554
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
 
1555
                }
 
1556
                i += run;
 
1557
                j = scantable[i];
 
1558
                if(level<0){
 
1559
                    level= -level;
 
1560
                    level= (level*qscale*quant_matrix[j])>>4;
 
1561
                    level= (level-1)|1;
 
1562
                    level= -level;
 
1563
                }else{
 
1564
                    level= (level*qscale*quant_matrix[j])>>4;
 
1565
                    level= (level-1)|1;
 
1566
                }
 
1567
            }
 
1568
            if (i > 63){
 
1569
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
 
1570
                return -1;
 
1571
            }
 
1572
 
 
1573
            block[j] = level;
 
1574
        }
 
1575
        CLOSE_READER(re, &s->gb);
 
1576
    }
 
1577
    s->block_last_index[n] = i;
 
1578
   return 0;
 
1579
}
 
1580
 
 
1581
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
 
1582
                               DCTELEM *block, 
 
1583
                               int n)
 
1584
{
 
1585
    int level, i, j, run;
 
1586
    RLTable *rl = &rl_mpeg1;
 
1587
    uint8_t * const scantable= s->intra_scantable.permutated;
 
1588
    const uint16_t *quant_matrix= s->inter_matrix;
 
1589
    const int qscale= s->qscale;
 
1590
 
 
1591
    {
 
1592
        OPEN_READER(re, &s->gb);
 
1593
        i = -1;
 
1594
        /* special case for the first coef. no need to add a second vlc table */
 
1595
        UPDATE_CACHE(re, &s->gb);
 
1596
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
 
1597
            level= (3*qscale*quant_matrix[0])>>5;
 
1598
            level= (level-1)|1;
 
1599
            if(GET_CACHE(re, &s->gb)&0x40000000)
 
1600
                level= -level;
 
1601
            block[0] = level;
 
1602
            i++;
 
1603
            SKIP_BITS(re, &s->gb, 2);
 
1604
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1605
                goto end;
 
1606
        }
 
1607
 
 
1608
        /* now quantify & encode AC coefs */
 
1609
        for(;;) {
 
1610
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1611
            
 
1612
            if(level != 0) {
 
1613
                i += run;
 
1614
                j = scantable[i];
 
1615
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
 
1616
                level= (level-1)|1;
 
1617
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
1618
                SKIP_BITS(re, &s->gb, 1);
 
1619
            } else {
 
1620
                /* escape */
 
1621
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
1622
                UPDATE_CACHE(re, &s->gb);
 
1623
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
 
1624
                if (level == -128) {
 
1625
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
 
1626
                } else if (level == 0) {
 
1627
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
 
1628
                }
 
1629
                i += run;
 
1630
                j = scantable[i];
 
1631
                if(level<0){
 
1632
                    level= -level;
 
1633
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
 
1634
                    level= (level-1)|1;
 
1635
                    level= -level;
 
1636
                }else{
 
1637
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
 
1638
                    level= (level-1)|1;
 
1639
                }
 
1640
            }
 
1641
            if (i > 63){
 
1642
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
 
1643
                return -1;
 
1644
            }
 
1645
 
 
1646
            block[j] = level;
 
1647
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1648
                break;
 
1649
            UPDATE_CACHE(re, &s->gb);
 
1650
        }
 
1651
end:
 
1652
        LAST_SKIP_BITS(re, &s->gb, 2);
 
1653
        CLOSE_READER(re, &s->gb);
 
1654
    }
 
1655
    s->block_last_index[n] = i;
 
1656
    return 0;
 
1657
}
 
1658
 
 
1659
static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
 
1660
{
 
1661
    int level, i, j, run;
 
1662
    RLTable *rl = &rl_mpeg1;
 
1663
    uint8_t * const scantable= s->intra_scantable.permutated;
 
1664
    const int qscale= s->qscale;
 
1665
 
 
1666
    {
 
1667
        OPEN_READER(re, &s->gb);
 
1668
        i = -1;
 
1669
        /* special case for the first coef. no need to add a second vlc table */
 
1670
        UPDATE_CACHE(re, &s->gb);
 
1671
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
 
1672
            level= (3*qscale)>>1;
 
1673
            level= (level-1)|1;
 
1674
            if(GET_CACHE(re, &s->gb)&0x40000000)
 
1675
                level= -level;
 
1676
            block[0] = level;
 
1677
            i++;
 
1678
            SKIP_BITS(re, &s->gb, 2);
 
1679
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1680
                goto end;
 
1681
        }
 
1682
 
 
1683
        /* now quantify & encode AC coefs */
 
1684
        for(;;) {
 
1685
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1686
            
 
1687
            if(level != 0) {
 
1688
                i += run;
 
1689
                j = scantable[i];
 
1690
                level= ((level*2+1)*qscale)>>1;
 
1691
                level= (level-1)|1;
 
1692
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
1693
                SKIP_BITS(re, &s->gb, 1);
 
1694
            } else {
 
1695
                /* escape */
 
1696
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
1697
                UPDATE_CACHE(re, &s->gb);
 
1698
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
 
1699
                if (level == -128) {
 
1700
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
 
1701
                } else if (level == 0) {
 
1702
                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
 
1703
                }
 
1704
                i += run;
 
1705
                j = scantable[i];
 
1706
                if(level<0){
 
1707
                    level= -level;
 
1708
                    level= ((level*2+1)*qscale)>>1;
 
1709
                    level= (level-1)|1;
 
1710
                    level= -level;
 
1711
                }else{
 
1712
                    level= ((level*2+1)*qscale)>>1;
 
1713
                    level= (level-1)|1;
 
1714
                }
 
1715
            }
 
1716
 
 
1717
            block[j] = level;
 
1718
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1719
                break;
 
1720
            UPDATE_CACHE(re, &s->gb);
 
1721
        }
 
1722
end:
 
1723
        LAST_SKIP_BITS(re, &s->gb, 2);
 
1724
        CLOSE_READER(re, &s->gb);
 
1725
    }
 
1726
    s->block_last_index[n] = i;
 
1727
    return 0;
 
1728
}
 
1729
 
 
1730
 
 
1731
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
 
1732
                               DCTELEM *block, 
 
1733
                               int n)
 
1734
{
 
1735
    int level, i, j, run;
 
1736
    RLTable *rl = &rl_mpeg1;
 
1737
    uint8_t * const scantable= s->intra_scantable.permutated;
 
1738
    const uint16_t *quant_matrix;
 
1739
    const int qscale= s->qscale;
 
1740
    int mismatch;
 
1741
 
 
1742
    mismatch = 1;
 
1743
 
 
1744
    {
 
1745
        OPEN_READER(re, &s->gb);
 
1746
        i = -1;
 
1747
        if (n < 4)
 
1748
            quant_matrix = s->inter_matrix;
 
1749
        else
 
1750
            quant_matrix = s->chroma_inter_matrix;
 
1751
 
 
1752
        /* special case for the first coef. no need to add a second vlc table */
 
1753
        UPDATE_CACHE(re, &s->gb);
 
1754
        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
 
1755
            level= (3*qscale*quant_matrix[0])>>5;
 
1756
            if(GET_CACHE(re, &s->gb)&0x40000000)
 
1757
                level= -level;
 
1758
            block[0] = level;
 
1759
            mismatch ^= level;
 
1760
            i++;
 
1761
            SKIP_BITS(re, &s->gb, 2);
 
1762
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1763
                goto end;
 
1764
        }
 
1765
 
 
1766
        /* now quantify & encode AC coefs */
 
1767
        for(;;) {
 
1768
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1769
            
 
1770
            if(level != 0) {
 
1771
                i += run;
 
1772
                j = scantable[i];
 
1773
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
 
1774
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
1775
                SKIP_BITS(re, &s->gb, 1);
 
1776
            } else {
 
1777
                /* escape */
 
1778
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
1779
                UPDATE_CACHE(re, &s->gb);
 
1780
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
 
1781
 
 
1782
                i += run;
 
1783
                j = scantable[i];
 
1784
                if(level<0){
 
1785
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
 
1786
                    level= -level;
 
1787
                }else{
 
1788
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
 
1789
                }
 
1790
            }
 
1791
            if (i > 63){
 
1792
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
 
1793
                return -1;
 
1794
            }
 
1795
            
 
1796
            mismatch ^= level;
 
1797
            block[j] = level;
 
1798
            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1799
                break;
 
1800
            UPDATE_CACHE(re, &s->gb);
 
1801
        }
 
1802
end:
 
1803
        LAST_SKIP_BITS(re, &s->gb, 2);
 
1804
        CLOSE_READER(re, &s->gb);
 
1805
    }
 
1806
    block[63] ^= (mismatch & 1);
 
1807
    
 
1808
    s->block_last_index[n] = i;
 
1809
    return 0;
 
1810
}
 
1811
 
 
1812
static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, 
 
1813
                               DCTELEM *block, 
 
1814
                               int n)
 
1815
{
 
1816
    int level, i, j, run;
 
1817
    RLTable *rl = &rl_mpeg1;
 
1818
    uint8_t * const scantable= s->intra_scantable.permutated;
 
1819
    const int qscale= s->qscale;
 
1820
    OPEN_READER(re, &s->gb);
 
1821
    i = -1;
 
1822
 
 
1823
    /* special case for the first coef. no need to add a second vlc table */
 
1824
    UPDATE_CACHE(re, &s->gb);
 
1825
    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
 
1826
        level= (3*qscale)>>1;
 
1827
        if(GET_CACHE(re, &s->gb)&0x40000000)
 
1828
            level= -level;
 
1829
        block[0] = level;
 
1830
        i++;
 
1831
        SKIP_BITS(re, &s->gb, 2);
 
1832
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1833
            goto end;
 
1834
    }
 
1835
 
 
1836
    /* now quantify & encode AC coefs */
 
1837
    for(;;) {
 
1838
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1839
        
 
1840
        if(level != 0) {
 
1841
            i += run;
 
1842
            j = scantable[i];
 
1843
            level= ((level*2+1)*qscale)>>1;
 
1844
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
1845
            SKIP_BITS(re, &s->gb, 1);
 
1846
        } else {
 
1847
            /* escape */
 
1848
            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
1849
            UPDATE_CACHE(re, &s->gb);
 
1850
            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
 
1851
 
 
1852
            i += run;
 
1853
            j = scantable[i];
 
1854
            if(level<0){
 
1855
                level= ((-level*2+1)*qscale)>>1;
 
1856
                level= -level;
 
1857
            }else{
 
1858
                level= ((level*2+1)*qscale)>>1;
 
1859
            }
 
1860
        }
 
1861
        
 
1862
        block[j] = level;
 
1863
        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
 
1864
            break;
 
1865
        UPDATE_CACHE(re, &s->gb);
 
1866
    }
 
1867
end:
 
1868
    LAST_SKIP_BITS(re, &s->gb, 2);    
 
1869
    CLOSE_READER(re, &s->gb);
 
1870
    s->block_last_index[n] = i;
 
1871
    return 0;
 
1872
}
 
1873
 
 
1874
 
 
1875
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
 
1876
                               DCTELEM *block, 
 
1877
                               int n)
 
1878
{
 
1879
    int level, dc, diff, i, j, run;
 
1880
    int component;
 
1881
    RLTable *rl;
 
1882
    uint8_t * const scantable= s->intra_scantable.permutated;
 
1883
    const uint16_t *quant_matrix;
 
1884
    const int qscale= s->qscale;
 
1885
    int mismatch;
 
1886
 
 
1887
    /* DC coef */
 
1888
    if (n < 4){
 
1889
        quant_matrix = s->intra_matrix;
 
1890
        component = 0; 
 
1891
    }else{
 
1892
        quant_matrix = s->chroma_intra_matrix;
 
1893
        component = (n&1) + 1;
 
1894
    }
 
1895
    diff = decode_dc(&s->gb, component);
 
1896
    if (diff >= 0xffff)
 
1897
        return -1;
 
1898
    dc = s->last_dc[component];
 
1899
    dc += diff;
 
1900
    s->last_dc[component] = dc;
 
1901
    block[0] = dc << (3 - s->intra_dc_precision);
 
1902
    dprintf("dc=%d\n", block[0]);
 
1903
    mismatch = block[0] ^ 1;
 
1904
    i = 0;
 
1905
    if (s->intra_vlc_format)
 
1906
        rl = &rl_mpeg2;
 
1907
    else
 
1908
        rl = &rl_mpeg1;
 
1909
 
 
1910
    {
 
1911
        OPEN_READER(re, &s->gb);    
 
1912
        /* now quantify & encode AC coefs */
 
1913
        for(;;) {
 
1914
            UPDATE_CACHE(re, &s->gb);
 
1915
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1916
            
 
1917
            if(level == 127){
 
1918
                break;
 
1919
            } else if(level != 0) {
 
1920
                i += run;
 
1921
                j = scantable[i];
 
1922
                level= (level*qscale*quant_matrix[j])>>4;
 
1923
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
1924
                LAST_SKIP_BITS(re, &s->gb, 1);
 
1925
            } else {
 
1926
                /* escape */
 
1927
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
1928
                UPDATE_CACHE(re, &s->gb);
 
1929
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
 
1930
                i += run;
 
1931
                j = scantable[i];
 
1932
                if(level<0){
 
1933
                    level= (-level*qscale*quant_matrix[j])>>4;
 
1934
                    level= -level;
 
1935
                }else{
 
1936
                    level= (level*qscale*quant_matrix[j])>>4;
 
1937
                }
 
1938
            }
 
1939
            if (i > 63){
 
1940
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
 
1941
                return -1;
 
1942
            }
 
1943
            
 
1944
            mismatch^= level;
 
1945
            block[j] = level;
 
1946
        }
 
1947
        CLOSE_READER(re, &s->gb);
 
1948
    }
 
1949
    block[63]^= mismatch&1;
 
1950
    
 
1951
    s->block_last_index[n] = i;
 
1952
    return 0;
 
1953
}
 
1954
 
 
1955
static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, 
 
1956
                               DCTELEM *block, 
 
1957
                               int n)
 
1958
{
 
1959
    int level, dc, diff, j, run;
 
1960
    int component;
 
1961
    RLTable *rl;
 
1962
    uint8_t * scantable= s->intra_scantable.permutated;
 
1963
    const uint16_t *quant_matrix;
 
1964
    const int qscale= s->qscale;
 
1965
 
 
1966
    /* DC coef */
 
1967
    if (n < 4){
 
1968
        quant_matrix = s->intra_matrix;
 
1969
        component = 0; 
 
1970
    }else{
 
1971
        quant_matrix = s->chroma_intra_matrix;
 
1972
        component = (n&1) + 1;
 
1973
    }
 
1974
    diff = decode_dc(&s->gb, component);
 
1975
    if (diff >= 0xffff)
 
1976
        return -1;
 
1977
    dc = s->last_dc[component];
 
1978
    dc += diff;
 
1979
    s->last_dc[component] = dc;
 
1980
    block[0] = dc << (3 - s->intra_dc_precision);
 
1981
    if (s->intra_vlc_format)
 
1982
        rl = &rl_mpeg2;
 
1983
    else
 
1984
        rl = &rl_mpeg1;
 
1985
 
 
1986
    {
 
1987
        OPEN_READER(re, &s->gb);    
 
1988
        /* now quantify & encode AC coefs */
 
1989
        for(;;) {
 
1990
            UPDATE_CACHE(re, &s->gb);
 
1991
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
1992
            
 
1993
            if(level == 127){
 
1994
                break;
 
1995
            } else if(level != 0) {
 
1996
                scantable += run;
 
1997
                j = *scantable;
 
1998
                level= (level*qscale*quant_matrix[j])>>4;
 
1999
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
 
2000
                LAST_SKIP_BITS(re, &s->gb, 1);
 
2001
            } else {
 
2002
                /* escape */
 
2003
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
 
2004
                UPDATE_CACHE(re, &s->gb);
 
2005
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
 
2006
                scantable += run;
 
2007
                j = *scantable;
 
2008
                if(level<0){
 
2009
                    level= (-level*qscale*quant_matrix[j])>>4;
 
2010
                    level= -level;
 
2011
                }else{
 
2012
                    level= (level*qscale*quant_matrix[j])>>4;
 
2013
                }
 
2014
            }
 
2015
            
 
2016
            block[j] = level;
 
2017
        }
 
2018
        CLOSE_READER(re, &s->gb);
 
2019
    }
 
2020
    
 
2021
    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
 
2022
    return 0;
 
2023
}
 
2024
 
1764
2025
typedef struct Mpeg1Context {
1765
2026
    MpegEncContext mpeg_enc_ctx;
1766
2027
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1769
2030
    int slice_count;
1770
2031
    int swap_uv;//indicate VCR2
1771
2032
    int save_aspect_info;
 
2033
    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
1772
2034
 
1773
2035
} Mpeg1Context;
1774
2036
 
1801
2063
 
1802
2064
static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, 
1803
2065
                                     const uint8_t *new_perm){
1804
 
uint16_t temp_matrix[64];
1805
 
int i;
 
2066
    uint16_t temp_matrix[64];
 
2067
    int i;
1806
2068
 
1807
2069
    memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
1808
2070
    
1814
2076
//Call this function when we know all parameters
1815
2077
//it may be called in different places for mpeg1 and mpeg2
1816
2078
static int mpeg_decode_postinit(AVCodecContext *avctx){
1817
 
Mpeg1Context *s1 = avctx->priv_data;
1818
 
MpegEncContext *s = &s1->mpeg_enc_ctx;
1819
 
uint8_t old_permutation[64];
1820
 
 
 
2079
    Mpeg1Context *s1 = avctx->priv_data;
 
2080
    MpegEncContext *s = &s1->mpeg_enc_ctx;
 
2081
    uint8_t old_permutation[64];
1821
2082
 
1822
2083
    if (
1823
2084
        (s1->mpeg_enc_ctx_allocated == 0)|| 
1824
 
        avctx->width  != s->width ||
1825
 
        avctx->height != s->height||
1826
 
//      s1->save_aspect_info != avctx->aspect_ratio_info||
 
2085
        avctx->coded_width  != s->width ||
 
2086
        avctx->coded_height != s->height||
 
2087
        s1->save_aspect_info != s->aspect_ratio_info||
1827
2088
        0)
1828
2089
    {
1829
2090
    
1830
2091
        if (s1->mpeg_enc_ctx_allocated) {
 
2092
            ParseContext pc= s->parse_context;
 
2093
            s->parse_context.buffer=0;
1831
2094
            MPV_common_end(s);
 
2095
            s->parse_context= pc;
1832
2096
        }
1833
2097
 
1834
2098
        if( (s->width == 0 )||(s->height == 0))
1835
2099
            return -2;
1836
2100
 
1837
 
        avctx->width = s->width;
1838
 
        avctx->height = s->height;
 
2101
        avcodec_set_dimensions(avctx, s->width, s->height);
1839
2102
        avctx->bit_rate = s->bit_rate;
1840
2103
        s1->save_aspect_info = s->aspect_ratio_info;
1841
2104
 
1845
2108
 
1846
2109
        if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
1847
2110
            //mpeg1 fps
1848
 
            avctx->frame_rate     = frame_rate_tab[s->frame_rate_index].num;
1849
 
            avctx->frame_rate_base= frame_rate_tab[s->frame_rate_index].den;
 
2111
            avctx->time_base.den     = frame_rate_tab[s->frame_rate_index].num;
 
2112
            avctx->time_base.num= frame_rate_tab[s->frame_rate_index].den;
1850
2113
            //mpeg1 aspect
1851
2114
            avctx->sample_aspect_ratio= av_d2q(
1852
2115
                    1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
1854
2117
        }else{//mpeg2
1855
2118
        //mpeg2 fps
1856
2119
            av_reduce(
1857
 
                &s->avctx->frame_rate, 
1858
 
                &s->avctx->frame_rate_base, 
1859
 
                frame_rate_tab[s->frame_rate_index].num * (s->frame_rate_ext_n+1),
1860
 
                frame_rate_tab[s->frame_rate_index].den * (s->frame_rate_ext_d+1),
 
2120
                &s->avctx->time_base.den, 
 
2121
                &s->avctx->time_base.num, 
 
2122
                frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
 
2123
                frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1861
2124
                1<<30);
1862
2125
        //mpeg2 aspect
1863
2126
            if(s->aspect_ratio_info > 1){
1956
2219
 
1957
2220
    ref = get_bits(&s->gb, 10); /* temporal ref */
1958
2221
    s->pict_type = get_bits(&s->gb, 3);
 
2222
    if(s->pict_type == 0 || s->pict_type > 3)
 
2223
        return -1;
1959
2224
 
1960
2225
    vbv_delay= get_bits(&s->gb, 16);
1961
2226
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1962
2227
        s->full_pel[0] = get_bits1(&s->gb);
1963
2228
        f_code = get_bits(&s->gb, 3);
1964
 
        if (f_code == 0)
 
2229
        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
1965
2230
            return -1;
1966
2231
        s->mpeg_f_code[0][0] = f_code;
1967
2232
        s->mpeg_f_code[0][1] = f_code;
1969
2234
    if (s->pict_type == B_TYPE) {
1970
2235
        s->full_pel[1] = get_bits1(&s->gb);
1971
2236
        f_code = get_bits(&s->gb, 3);
1972
 
        if (f_code == 0)
 
2237
        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
1973
2238
            return -1;
1974
2239
        s->mpeg_f_code[1][0] = f_code;
1975
2240
        s->mpeg_f_code[1][1] = f_code;
1977
2242
    s->current_picture.pict_type= s->pict_type;
1978
2243
    s->current_picture.key_frame= s->pict_type == I_TYPE;
1979
2244
    
1980
 
//    if(avctx->debug & FF_DEBUG_PICT_INFO)
1981
 
//        av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d\n", vbv_delay, ref);
 
2245
    if(avctx->debug & FF_DEBUG_PICT_INFO)
 
2246
        av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1982
2247
    
1983
2248
    s->y_dc_scale = 8;
1984
2249
    s->c_dc_scale = 8;
1986
2251
    return 0;
1987
2252
}
1988
2253
 
1989
 
static void mpeg_decode_sequence_extension(MpegEncContext *s)
 
2254
static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1990
2255
{
 
2256
    MpegEncContext *s= &s1->mpeg_enc_ctx;
1991
2257
    int horiz_size_ext, vert_size_ext;
1992
2258
    int bit_rate_ext;
1993
 
    int level, profile;
1994
2259
 
1995
2260
    skip_bits(&s->gb, 1); /* profil and level esc*/
1996
 
    profile= get_bits(&s->gb, 3);
1997
 
    level= get_bits(&s->gb, 4);
 
2261
    s->avctx->profile= get_bits(&s->gb, 3);
 
2262
    s->avctx->level= get_bits(&s->gb, 4);
1998
2263
    s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1999
2264
    s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
2000
2265
    horiz_size_ext = get_bits(&s->gb, 2);
2002
2267
    s->width |= (horiz_size_ext << 12);
2003
2268
    s->height |= (vert_size_ext << 12);
2004
2269
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
2005
 
    s->bit_rate += (bit_rate_ext << 12) * 400;
 
2270
    s->bit_rate += (bit_rate_ext << 18) * 400;
2006
2271
    skip_bits1(&s->gb); /* marker */
2007
2272
    s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
2008
2273
 
2009
2274
    s->low_delay = get_bits1(&s->gb);
2010
2275
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2011
2276
 
2012
 
    s->frame_rate_ext_n = get_bits(&s->gb, 2);
2013
 
    s->frame_rate_ext_d = get_bits(&s->gb, 5);
 
2277
    s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
 
2278
    s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
2014
2279
 
2015
2280
    dprintf("sequence extension\n");
2016
2281
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2018
2283
 
2019
2284
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2020
2285
        av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n", 
2021
 
               profile, level, s->avctx->rc_buffer_size, s->bit_rate);
 
2286
               s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
2022
2287
 
2023
2288
}
2024
2289
 
2176
2441
    ext_type = get_bits(&s->gb, 4);
2177
2442
    switch(ext_type) {
2178
2443
    case 0x1:
2179
 
        mpeg_decode_sequence_extension(s);
 
2444
        mpeg_decode_sequence_extension(s1);
2180
2445
        break;
2181
2446
    case 0x2:
2182
2447
        mpeg_decode_sequence_display_extension(s1);
2194
2459
}
2195
2460
 
2196
2461
static void exchange_uv(MpegEncContext *s){
2197
 
short * tmp;
2198
 
 
2199
 
    tmp = s->pblocks[4];
 
2462
    short * tmp = s->pblocks[4];
2200
2463
    s->pblocks[4] = s->pblocks[5];
2201
2464
    s->pblocks[5] = tmp;
2202
2465
}
2266
2529
    AVCodecContext *avctx= s->avctx;
2267
2530
    int ret;
2268
2531
    const int field_pic= s->picture_structure != PICT_FRAME;
 
2532
    const int lowres= s->avctx->lowres;
2269
2533
 
2270
2534
    s->resync_mb_x=
2271
2535
    s->resync_mb_y= -1;
2334
2598
            XVMC_init_block(s);//set s->block
2335
2599
#endif
2336
2600
 
2337
 
        s->dsp.clear_blocks(s->block[0]);
2338
 
 
2339
2601
        ret = mpeg_decode_mb(s, s->block);
2340
2602
        s->chroma_qscale= s->qscale;
2341
2603
 
2368
2630
                    s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
2369
2631
                    s->current_picture.ref_index [dir][xy    ]=
2370
2632
                    s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
 
2633
                    assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
2371
2634
                }
2372
2635
                xy += wrap;
2373
2636
            }
2374
2637
        }
2375
2638
 
2376
 
        s->dest[0] += 16;
2377
 
        s->dest[1] += 8;
2378
 
        s->dest[2] += 8;
 
2639
        s->dest[0] += 16 >> lowres;
 
2640
        s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
 
2641
        s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
2379
2642
 
2380
2643
        MPV_decode_mb(s, s->block);
2381
2644
        
2382
2645
        if (++s->mb_x >= s->mb_width) {
 
2646
            const int mb_size= 16>>s->avctx->lowres;
2383
2647
 
2384
 
            ff_draw_horiz_band(s, 16*s->mb_y, 16);
 
2648
            ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
2385
2649
 
2386
2650
            s->mb_x = 0;
2387
2651
            s->mb_y++;
2555
2819
            s->chroma_intra_matrix[j] = v;
2556
2820
        }
2557
2821
#ifdef DEBUG
 
2822
/*
2558
2823
        dprintf("intra matrix present\n");
2559
2824
        for(i=0;i<64;i++)
2560
 
            dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]);
 
2825
            dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
2561
2826
        printf("\n");
 
2827
*/
2562
2828
#endif
2563
2829
    } else {
2564
2830
        for(i=0;i<64;i++) {
2580
2846
            s->chroma_inter_matrix[j] = v;
2581
2847
        }
2582
2848
#ifdef DEBUG
 
2849
/*
2583
2850
        dprintf("non intra matrix present\n");
2584
2851
        for(i=0;i<64;i++)
2585
 
            dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]);
 
2852
            dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
2586
2853
        printf("\n");
 
2854
*/
2587
2855
#endif
2588
2856
    } else {
2589
2857
        for(i=0;i<64;i++) {
2629
2897
    if (s1->mpeg_enc_ctx_allocated) {
2630
2898
        MPV_common_end(s);
2631
2899
    }
2632
 
    s->width = avctx->width;
2633
 
    s->height = avctx->height;
 
2900
    s->width  = avctx->coded_width;
 
2901
    s->height = avctx->coded_height;
2634
2902
    avctx->has_b_frames= 0; //true?
2635
2903
    s->low_delay= 1;
2636
2904
 
2786
3054
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
2787
3055
    dprintf("fill_buffer\n");
2788
3056
 
2789
 
    /* special case for last picture */
2790
 
    if (buf_size == 0 && s2->low_delay==0 && s2->next_picture_ptr) {
2791
 
        *picture= *(AVFrame*)s2->next_picture_ptr;
2792
 
        s2->next_picture_ptr= NULL;
 
3057
    if (buf_size == 0) {
 
3058
        /* special case for last picture */
 
3059
        if (s2->low_delay==0 && s2->next_picture_ptr) {
 
3060
            *picture= *(AVFrame*)s2->next_picture_ptr;
 
3061
            s2->next_picture_ptr= NULL;
2793
3062
 
2794
 
        *data_size = sizeof(AVFrame);
 
3063
            *data_size = sizeof(AVFrame);
 
3064
        }
2795
3065
        return 0;
2796
3066
    }
2797
3067
 
2826
3096
        /* find start next code */
2827
3097
        start_code = find_start_code(&buf_ptr, buf_end);
2828
3098
        if (start_code < 0){
2829
 
            if(s2->pict_type != B_TYPE || avctx->hurry_up==0){
 
3099
            if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
2830
3100
                if(avctx->thread_count > 1){
2831
3101
                    int i;
2832
3102
 
2878
3148
                        start_code <= SLICE_MAX_START_CODE) {
2879
3149
                        int mb_y= start_code - SLICE_MIN_START_CODE;
2880
3150
                        
 
3151
                        if(s2->last_picture_ptr==NULL){
2881
3152
                        /* skip b frames if we dont have reference frames */
2882
 
                        if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
 
3153
                            if(s2->pict_type==B_TYPE) break;
 
3154
                        /* skip P frames if we dont have reference frame no valid header */
 
3155
                            if(s2->pict_type==P_TYPE && !s2->first_slice) break;
 
3156
                        }
2883
3157
                        /* skip b frames if we are in a hurry */
2884
3158
                        if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
 
3159
                        if(  (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
 
3160
                           ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
 
3161
                           || avctx->skip_frame >= AVDISCARD_ALL)
 
3162
                            break;
2885
3163
                        /* skip everything if we are in a hurry>=5 */
2886
3164
                        if(avctx->hurry_up>=5) break;
2887
3165
                        
2888
3166
                        if (!s->mpeg_enc_ctx_allocated) break;
 
3167
 
 
3168
                        if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
 
3169
                            if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
 
3170
                                break;
 
3171
                        }
2889
3172
                        
2890
3173
                        if(s2->first_slice){
2891
3174
                            s2->first_slice=0;
2943
3226
    NULL,
2944
3227
    mpeg_decode_end,
2945
3228
    mpeg_decode_frame,
2946
 
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
 
3229
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2947
3230
    .flush= ff_mpeg_flush,
2948
3231
};
2949
3232
 
2956
3239
    NULL,
2957
3240
    mpeg_decode_end,
2958
3241
    mpeg_decode_frame,
2959
 
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
 
3242
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2960
3243
    .flush= ff_mpeg_flush,
2961
3244
};
2962
3245
 
2970
3253
    NULL,
2971
3254
    mpeg_decode_end,
2972
3255
    mpeg_decode_frame,
2973
 
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
 
3256
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2974
3257
    .flush= ff_mpeg_flush,
2975
3258
};
2976
3259
 
2985
3268
    MPV_encode_picture,
2986
3269
    MPV_encode_end,
2987
3270
    .supported_framerates= frame_rate_tab+1,
 
3271
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
 
3272
    .capabilities= CODEC_CAP_DELAY,
2988
3273
};
2989
3274
 
2990
 
#ifdef CONFIG_RISKY
2991
 
 
2992
3275
AVCodec mpeg2video_encoder = {
2993
3276
    "mpeg2video",
2994
3277
    CODEC_TYPE_VIDEO,
2998
3281
    MPV_encode_picture,
2999
3282
    MPV_encode_end,
3000
3283
    .supported_framerates= frame_rate_tab+1,
 
3284
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
 
3285
    .capabilities= CODEC_CAP_DELAY,
3001
3286
};
3002
3287
#endif
3003
 
#endif
3004
3288
 
3005
3289
#ifdef HAVE_XVMC
3006
3290
static int mpeg_mc_decode_init(AVCodecContext *avctx){
3007
3291
    Mpeg1Context *s;
3008
3292
 
 
3293
    if( avctx->thread_count > 1) 
 
3294
        return -1;
3009
3295
    if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
3010
3296
        return -1;
3011
3297
    if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
3029
3315
    NULL,
3030
3316
    mpeg_decode_end,
3031
3317
    mpeg_decode_frame,
3032
 
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL,
 
3318
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
3033
3319
    .flush= ff_mpeg_flush,
3034
3320
};
3035
3321