GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/msmpeg4dec.c Lines: 412 480 85.8 %
Date: 2019-11-20 04:07:19 Branches: 235 309 76.1 %

Line Branch Exec Source
1
/*
2
 * MSMPEG4 backend for encoder and decoder
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7
 *
8
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
#include "avcodec.h"
26
#include "internal.h"
27
#include "mpegutils.h"
28
#include "mpegvideo.h"
29
#include "msmpeg4.h"
30
#include "libavutil/imgutils.h"
31
#include "h263.h"
32
#include "mpeg4video.h"
33
#include "msmpeg4data.h"
34
#include "vc1data.h"
35
#include "wmv2.h"
36
37
#define DC_VLC_BITS 9
38
#define V2_INTRA_CBPC_VLC_BITS 3
39
#define V2_MB_TYPE_VLC_BITS 7
40
#define MV_VLC_BITS 9
41
#define V2_MV_VLC_BITS 9
42
#define TEX_VLC_BITS 9
43
44
#define DEFAULT_INTER_INDEX 3
45
46
5490
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
47
                                    int32_t **dc_val_ptr)
48
{
49
    int i;
50
51
5490
    if (n < 4) {
52
3660
        i= 0;
53
    } else {
54
1830
        i= n-3;
55
    }
56
57
5490
    *dc_val_ptr= &s->last_dc[i];
58
5490
    return s->last_dc[i];
59
}
60
61
/****************************************/
62
/* decoding stuff */
63
64
VLC ff_mb_non_intra_vlc[4];
65
static VLC v2_dc_lum_vlc;
66
static VLC v2_dc_chroma_vlc;
67
static VLC v2_intra_cbpc_vlc;
68
static VLC v2_mb_type_vlc;
69
static VLC v2_mv_vlc;
70
VLC ff_inter_intra_vlc;
71
72
/* This is identical to H.263 except that its range is multiplied by 2. */
73
132268
static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
74
{
75
    int code, val, sign, shift;
76
77
132268
    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
78
    ff_dlog(s, "MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
79
132268
    if (code < 0)
80
        return 0xffff;
81
82
132268
    if (code == 0)
83
68571
        return pred;
84
63697
    sign = get_bits1(&s->gb);
85
63697
    shift = f_code - 1;
86
63697
    val = code;
87
63697
    if (shift) {
88
        val = (val - 1) << shift;
89
        val |= get_bits(&s->gb, shift);
90
        val++;
91
    }
92
63697
    if (sign)
93
26289
        val = -val;
94
95
63697
    val += pred;
96
63697
    if (val <= -64)
97
138
        val += 64;
98
63559
    else if (val >= 64)
99
13
        val -= 64;
100
101
63697
    return val;
102
}
103
104
76350
static int msmpeg4v12_decode_mb(MpegEncContext *s, int16_t block[6][64])
105
{
106
    int cbp, code, i;
107
76350
    uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
108
109
76350
    if (s->pict_type == AV_PICTURE_TYPE_P) {
110
70035
        if (s->use_skip_mb_code) {
111
70035
            if (get_bits1(&s->gb)) {
112
                /* skip mb */
113
1159
                s->mb_intra = 0;
114
8113
                for(i=0;i<6;i++)
115
6954
                    s->block_last_index[i] = -1;
116
1159
                s->mv_dir = MV_DIR_FORWARD;
117
1159
                s->mv_type = MV_TYPE_16X16;
118
1159
                s->mv[0][0][0] = 0;
119
1159
                s->mv[0][0][1] = 0;
120
1159
                s->mb_skipped = 1;
121
1159
                *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
122
1159
                return 0;
123
            }
124
        }
125
126
68876
        if(s->msmpeg4_version==2)
127
53160
            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
128
        else
129
15716
            code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
130

68876
        if(code<0 || code>7){
131
            av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
132
            return -1;
133
        }
134
135
68876
        s->mb_intra = code >>2;
136
137
68876
        cbp = code & 0x3;
138
    } else {
139
6315
        s->mb_intra = 1;
140
6315
        if(s->msmpeg4_version==2)
141
5985
            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
142
        else
143
330
            cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
144

6315
        if(cbp<0 || cbp>3){
145
            av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
146
            return -1;
147
        }
148
    }
149
150
75191
    if (!s->mb_intra) {
151
        int mx, my, cbpy;
152
153
66134
        cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
154
66134
        if(cbpy<0){
155
            av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
156
            return -1;
157
        }
158
159
66134
        cbp|= cbpy<<2;
160

66134
        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
161
162
66134
        ff_h263_pred_motion(s, 0, 0, &mx, &my);
163
66134
        mx= msmpeg4v2_decode_motion(s, mx, 1);
164
66134
        my= msmpeg4v2_decode_motion(s, my, 1);
165
166
66134
        s->mv_dir = MV_DIR_FORWARD;
167
66134
        s->mv_type = MV_TYPE_16X16;
168
66134
        s->mv[0][0][0] = mx;
169
66134
        s->mv[0][0][1] = my;
170
66134
        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
171
    } else {
172
        int v;
173
9057
        if(s->msmpeg4_version==2){
174
8142
            s->ac_pred = get_bits1(&s->gb);
175
8142
            v = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
176
8142
            if (v < 0) {
177
                av_log(s->avctx, AV_LOG_ERROR, "cbpy vlc invalid\n");
178
                return -1;
179
            }
180
8142
            cbp|= v<<2;
181
        } else{
182
915
            s->ac_pred = 0;
183
915
            v = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
184
915
            if (v < 0) {
185
                av_log(s->avctx, AV_LOG_ERROR, "cbpy vlc invalid\n");
186
                return -1;
187
            }
188
915
            cbp|= v<<2;
189
915
            if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
190
        }
191
9057
        *mb_type_ptr = MB_TYPE_INTRA;
192
    }
193
194
75191
    s->bdsp.clear_blocks(s->block[0]);
195
526337
    for (i = 0; i < 6; i++) {
196
451146
        if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
197
        {
198
             av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
199
             return -1;
200
        }
201
    }
202
75191
    return 0;
203
}
204
205
129600
static int msmpeg4v34_decode_mb(MpegEncContext *s, int16_t block[6][64])
206
{
207
    int cbp, code, i;
208
    uint8_t *coded_val;
209
129600
    uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
210
211
129600
    if (get_bits_left(&s->gb) <= 0)
212
        return AVERROR_INVALIDDATA;
213
214
129600
    if (s->pict_type == AV_PICTURE_TYPE_P) {
215
116442
        if (s->use_skip_mb_code) {
216
116442
            if (get_bits1(&s->gb)) {
217
                /* skip mb */
218
1554
                s->mb_intra = 0;
219
10878
                for(i=0;i<6;i++)
220
9324
                    s->block_last_index[i] = -1;
221
1554
                s->mv_dir = MV_DIR_FORWARD;
222
1554
                s->mv_type = MV_TYPE_16X16;
223
1554
                s->mv[0][0][0] = 0;
224
1554
                s->mv[0][0][1] = 0;
225
1554
                s->mb_skipped = 1;
226
1554
                *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
227
228
1554
                return 0;
229
            }
230
        }
231
232
114888
        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
233
114888
        if (code < 0)
234
            return -1;
235
        //s->mb_intra = (code & 0x40) ? 0 : 1;
236
114888
        s->mb_intra = (~code & 0x40) >> 6;
237
238
114888
        cbp = code & 0x3f;
239
    } else {
240
13158
        s->mb_intra = 1;
241
13158
        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
242
13158
        if (code < 0)
243
            return -1;
244
        /* predict coded block pattern */
245
13158
        cbp = 0;
246
92106
        for(i=0;i<6;i++) {
247
78948
            int val = ((code >> (5 - i)) & 1);
248
78948
            if (i < 4) {
249
52632
                int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
250
52632
                val = val ^ pred;
251
52632
                *coded_val = val;
252
            }
253
78948
            cbp |= val << (5 - i);
254
        }
255
    }
256
257
128046
    if (!s->mb_intra) {
258
        int mx, my;
259

109652
        if(s->per_mb_rl_table && cbp){
260
            s->rl_table_index = decode012(&s->gb);
261
            s->rl_chroma_table_index = s->rl_table_index;
262
        }
263
109652
        ff_h263_pred_motion(s, 0, 0, &mx, &my);
264
109652
        if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
265
            return -1;
266
109652
        s->mv_dir = MV_DIR_FORWARD;
267
109652
        s->mv_type = MV_TYPE_16X16;
268
109652
        s->mv[0][0][0] = mx;
269
109652
        s->mv[0][0][1] = my;
270
109652
        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
271
    } else {
272
        ff_dlog(s, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
273
                ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
274
                show_bits(&s->gb, 24));
275
18394
        s->ac_pred = get_bits1(&s->gb);
276
18394
        *mb_type_ptr = MB_TYPE_INTRA;
277
18394
        if(s->inter_intra_pred){
278
            s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
279
            ff_dlog(s, "%d%d %d %d/",
280
                    s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
281
        }
282

18394
        if(s->per_mb_rl_table && cbp){
283
            s->rl_table_index = decode012(&s->gb);
284
            s->rl_chroma_table_index = s->rl_table_index;
285
        }
286
    }
287
288
128046
    s->bdsp.clear_blocks(s->block[0]);
289
896322
    for (i = 0; i < 6; i++) {
290
768276
        if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
291
        {
292
            av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
293
            return -1;
294
        }
295
    }
296
297
128046
    return 0;
298
}
299
300
/* init all vlc decoding tables */
301
87
av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
302
{
303
87
    MpegEncContext *s = avctx->priv_data;
304
    static volatile int done = 0;
305
    int i, ret;
306
    MVTable *mv;
307
308
87
    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
309
        return ret;
310
311
87
    if (ff_h263_decode_init(avctx) < 0)
312
        return -1;
313
314
87
    ff_msmpeg4_common_init(s);
315
316
87
    if (!done) {
317
301
        for(i=0;i<NB_RL_TABLES;i++) {
318
258
            ff_rl_init(&ff_rl_table[i], ff_static_rl_table_store[i]);
319
        }
320

1419
        INIT_VLC_RL(ff_rl_table[0], 642);
321

1419
        INIT_VLC_RL(ff_rl_table[1], 1104);
322

1419
        INIT_VLC_RL(ff_rl_table[2], 554);
323

1419
        INIT_VLC_RL(ff_rl_table[3], 940);
324

1419
        INIT_VLC_RL(ff_rl_table[4], 962);
325

1419
        INIT_VLC_RL(ff_rl_table[5], 554);
326
327
43
        mv = &ff_mv_tables[0];
328
43
        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
329
                    mv->table_mv_bits, 1, 1,
330
                    mv->table_mv_code, 2, 2, 3714);
331
43
        mv = &ff_mv_tables[1];
332
43
        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
333
                    mv->table_mv_bits, 1, 1,
334
                    mv->table_mv_code, 2, 2, 2694);
335
336
43
        INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
337
                 &ff_table0_dc_lum[0][1], 8, 4,
338
                 &ff_table0_dc_lum[0][0], 8, 4, 1158);
339
43
        INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
340
                 &ff_table0_dc_chroma[0][1], 8, 4,
341
                 &ff_table0_dc_chroma[0][0], 8, 4, 1118);
342
43
        INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
343
                 &ff_table1_dc_lum[0][1], 8, 4,
344
                 &ff_table1_dc_lum[0][0], 8, 4, 1476);
345
43
        INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
346
                 &ff_table1_dc_chroma[0][1], 8, 4,
347
                 &ff_table1_dc_chroma[0][0], 8, 4, 1216);
348
349
43
        INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
350
                 &ff_v2_dc_lum_table[0][1], 8, 4,
351
                 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
352
43
        INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
353
                 &ff_v2_dc_chroma_table[0][1], 8, 4,
354
                 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
355
356
43
        INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
357
                 &ff_v2_intra_cbpc[0][1], 2, 1,
358
                 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
359
43
        INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
360
                 &ff_v2_mb_type[0][1], 2, 1,
361
                 &ff_v2_mb_type[0][0], 2, 1, 128);
362
43
        INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
363
                 &ff_mvtab[0][1], 2, 1,
364
                 &ff_mvtab[0][0], 2, 1, 538);
365
366
43
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
367
                     &ff_wmv2_inter_table[0][0][1], 8, 4,
368
                     &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
369
43
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
370
                     &ff_wmv2_inter_table[1][0][1], 8, 4,
371
                     &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
372
43
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
373
                     &ff_wmv2_inter_table[2][0][1], 8, 4,
374
                     &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
375
43
        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
376
                     &ff_wmv2_inter_table[3][0][1], 8, 4,
377
                     &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
378
379
43
        INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
380
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
381
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 536);
382
383
43
        INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
384
                 &ff_table_inter_intra[0][1], 2, 1,
385
                 &ff_table_inter_intra[0][0], 2, 1, 8);
386
43
        done = 1;
387
    }
388
389

87
    switch(s->msmpeg4_version){
390
11
    case 1:
391
    case 2:
392
11
        s->decode_mb= msmpeg4v12_decode_mb;
393
11
        break;
394
22
    case 3:
395
    case 4:
396
22
        s->decode_mb= msmpeg4v34_decode_mb;
397
22
        break;
398
12
    case 5:
399
        if (CONFIG_WMV2_DECODER)
400
12
            s->decode_mb= ff_wmv2_decode_mb;
401
    case 6:
402
        //FIXME + TODO VC1 decode mb
403
54
        break;
404
    }
405
406
87
    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
407
408
87
    return 0;
409
}
410
411
675
int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
412
{
413
    int code;
414
415
    // at minimum one bit per macroblock is required at least in a valid frame,
416
    // we discard frames much smaller than this. Frames smaller than 1/8 of the
417
    // smallest "black/skip" frame generally contain not much recoverable content
418
    // while at the same time they have the highest computational requirements
419
    // per byte
420
675
    if (get_bits_left(&s->gb) * 8LL < (s->width+15)/16 * ((s->height+15)/16))
421
        return AVERROR_INVALIDDATA;
422
423
675
    if(s->msmpeg4_version==1){
424
50
        int start_code = get_bits_long(&s->gb, 32);
425
50
        if(start_code!=0x00000100){
426
            av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
427
            return -1;
428
        }
429
430
50
        skip_bits(&s->gb, 5); // frame number */
431
    }
432
433
675
    s->pict_type = get_bits(&s->gb, 2) + 1;
434
675
    if (s->pict_type != AV_PICTURE_TYPE_I &&
435
611
        s->pict_type != AV_PICTURE_TYPE_P){
436
        av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
437
        return -1;
438
    }
439
675
    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
440
675
    if(s->qscale==0){
441
        av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
442
        return -1;
443
    }
444
445
675
    if (s->pict_type == AV_PICTURE_TYPE_I) {
446
64
        code = get_bits(&s->gb, 5);
447
64
        if(s->msmpeg4_version==1){
448

1
            if(code==0 || code>s->mb_height){
449
                av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
450
                return -1;
451
            }
452
453
1
            s->slice_height = code;
454
        }else{
455
            /* 0x17: one slice, 0x18: two slices, ... */
456
63
            if (code < 0x17){
457
                av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
458
                return -1;
459
            }
460
461
63
            s->slice_height = s->mb_height / (code - 0x16);
462
        }
463
464

64
        switch(s->msmpeg4_version){
465
21
        case 1:
466
        case 2:
467
21
            s->rl_chroma_table_index = 2;
468
21
            s->rl_table_index = 2;
469
470
21
            s->dc_table_index = 0; //not used
471
21
            break;
472
23
        case 3:
473
23
            s->rl_chroma_table_index = decode012(&s->gb);
474
23
            s->rl_table_index = decode012(&s->gb);
475
476
23
            s->dc_table_index = get_bits1(&s->gb);
477
23
            break;
478
20
        case 4:
479
20
            ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
480
481
20
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
482
            else                           s->per_mb_rl_table= 0;
483
484
20
            if(!s->per_mb_rl_table){
485
20
                s->rl_chroma_table_index = decode012(&s->gb);
486
20
                s->rl_table_index = decode012(&s->gb);
487
            }
488
489
20
            s->dc_table_index = get_bits1(&s->gb);
490
20
            s->inter_intra_pred= 0;
491
20
            break;
492
        }
493
64
        s->no_rounding = 1;
494
64
        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
495
            av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n",
496
                s->qscale,
497
                s->rl_chroma_table_index,
498
                s->rl_table_index,
499
                s->dc_table_index,
500
                s->per_mb_rl_table,
501
                s->slice_height);
502
    } else {
503

611
        switch(s->msmpeg4_version){
504
229
        case 1:
505
        case 2:
506
229
            if(s->msmpeg4_version==1)
507
49
                s->use_skip_mb_code = 1;
508
            else
509
180
                s->use_skip_mb_code = get_bits1(&s->gb);
510
229
            s->rl_table_index = 2;
511
229
            s->rl_chroma_table_index = s->rl_table_index;
512
229
            s->dc_table_index = 0; //not used
513
229
            s->mv_table_index = 0;
514
229
            break;
515
202
        case 3:
516
202
            s->use_skip_mb_code = get_bits1(&s->gb);
517
202
            s->rl_table_index = decode012(&s->gb);
518
202
            s->rl_chroma_table_index = s->rl_table_index;
519
520
202
            s->dc_table_index = get_bits1(&s->gb);
521
522
202
            s->mv_table_index = get_bits1(&s->gb);
523
202
            break;
524
180
        case 4:
525
180
            s->use_skip_mb_code = get_bits1(&s->gb);
526
527
180
            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
528
            else                           s->per_mb_rl_table= 0;
529
530
180
            if(!s->per_mb_rl_table){
531
180
                s->rl_table_index = decode012(&s->gb);
532
180
                s->rl_chroma_table_index = s->rl_table_index;
533
            }
534
535
180
            s->dc_table_index = get_bits1(&s->gb);
536
537
180
            s->mv_table_index = get_bits1(&s->gb);
538

180
            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
539
180
            break;
540
        }
541
542
611
        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
543
            av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n",
544
                s->use_skip_mb_code,
545
                s->rl_table_index,
546
                s->rl_chroma_table_index,
547
                s->dc_table_index,
548
                s->mv_table_index,
549
                s->per_mb_rl_table,
550
                s->qscale);
551
552
611
        if(s->flipflop_rounding){
553
382
            s->no_rounding ^= 1;
554
        }else{
555
229
            s->no_rounding = 0;
556
        }
557
    }
558
    ff_dlog(s->avctx, "%d %"PRId64" %d %d %d\n", s->pict_type, s->bit_rate,
559
            s->inter_intra_pred, s->width, s->height);
560
561
675
    s->esc3_level_length= 0;
562
675
    s->esc3_run_length= 0;
563
564
675
    return 0;
565
}
566
567
64
int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
568
{
569
64
    int left= buf_size*8 - get_bits_count(&s->gb);
570
64
    int length= s->msmpeg4_version>=3 ? 17 : 16;
571
    /* the alt_bitstream reader could read over the end so we need to check it */
572

64
    if(left>=length && left<length+8)
573
    {
574
63
        skip_bits(&s->gb, 5); /* fps */
575
63
        s->bit_rate= get_bits(&s->gb, 11)*1024;
576
63
        if(s->msmpeg4_version>=3)
577
43
            s->flipflop_rounding= get_bits1(&s->gb);
578
        else
579
20
            s->flipflop_rounding= 0;
580
    }
581
1
    else if(left<length+8)
582
    {
583
1
        s->flipflop_rounding= 0;
584
1
        if(s->msmpeg4_version != 2)
585
1
            av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
586
    }
587
    else
588
    {
589
        av_log(s->avctx, AV_LOG_ERROR, "I-frame too long, ignoring ext header\n");
590
    }
591
592
64
    return 0;
593
}
594
595
230712
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
596
{
597
    int level, pred;
598
599
230712
    if(s->msmpeg4_version<=2){
600
54342
        if (n < 4) {
601
36228
            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
602
        } else {
603
18114
            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
604
        }
605
54342
        if (level < 0) {
606
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
607
            *dir_ptr = 0;
608
            return -1;
609
        }
610
54342
        level-=256;
611
    }else{  //FIXME optimize use unified tables & index
612
176370
        if (n < 4) {
613
117580
            level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
614
        } else {
615
58790
            level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
616
        }
617
176370
        if (level < 0){
618
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
619
            *dir_ptr = 0;
620
            return -1;
621
        }
622
623
176370
        if (level == DC_MAX) {
624
48
            level = get_bits(&s->gb, 8);
625
48
            if (get_bits1(&s->gb))
626
6
                level = -level;
627
176322
        } else if (level != 0) {
628
153813
            if (get_bits1(&s->gb))
629
74512
                level = -level;
630
        }
631
    }
632
633
230712
    if(s->msmpeg4_version==1){
634
        int32_t *dc_val;
635
5490
        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
636
5490
        level += pred;
637
638
        /* update predictor */
639
5490
        *dc_val= level;
640
    }else{
641
        int16_t *dc_val;
642
225222
        pred   = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
643
225222
        level += pred;
644
645
        /* update predictor */
646
225222
        if (n < 4) {
647
150148
            *dc_val = level * s->y_dc_scale;
648
        } else {
649
75074
            *dc_val = level * s->c_dc_scale;
650
        }
651
    }
652
653
230712
    return level;
654
}
655
656
1472610
int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
657
                              int n, int coded, const uint8_t *scan_table)
658
{
659
    int level, i, last, run, run_diff;
660
1472610
    int av_uninit(dc_pred_dir);
661
    RLTable *rl;
662
    RL_VLC_ELEM *rl_vlc;
663
    int qmul, qadd;
664
665
1472610
    if (s->mb_intra) {
666
230712
        qmul=1;
667
230712
        qadd=0;
668
669
        /* DC coef */
670
230712
        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
671
672
230712
        if (level < 0){
673
            av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
674
            if(s->inter_intra_pred) level=0;
675
        }
676
230712
        if (n < 4) {
677
153808
            rl = &ff_rl_table[s->rl_table_index];
678
153808
            if(level > 256*s->y_dc_scale){
679
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
680
                if(!s->inter_intra_pred) return -1;
681
            }
682
        } else {
683
76904
            rl = &ff_rl_table[3 + s->rl_chroma_table_index];
684
76904
            if(level > 256*s->c_dc_scale){
685
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
686
                if(!s->inter_intra_pred) return -1;
687
            }
688
        }
689
230712
        block[0] = level;
690
691
230712
        run_diff = s->msmpeg4_version >= 4;
692
230712
        i = 0;
693
230712
        if (!coded) {
694
42456
            goto not_coded;
695
        }
696
188256
        if (s->ac_pred) {
697
3224
            if (dc_pred_dir == 0)
698
1928
                scan_table = s->intra_v_scantable.permutated; /* left */
699
            else
700
1296
                scan_table = s->intra_h_scantable.permutated; /* top */
701
        } else {
702
185032
            scan_table = s->intra_scantable.permutated;
703
        }
704
188256
        rl_vlc= rl->rl_vlc[0];
705
    } else {
706
1241898
        qmul = s->qscale << 1;
707
1241898
        qadd = (s->qscale - 1) | 1;
708
1241898
        i = -1;
709
1241898
        rl = &ff_rl_table[3 + s->rl_table_index];
710
711
1241898
        if(s->msmpeg4_version==2)
712
306018
            run_diff = 0;
713
        else
714
935880
            run_diff = 1;
715
716
1241898
        if (!coded) {
717
633723
            s->block_last_index[n] = i;
718
633723
            return 0;
719
        }
720
608175
        if(!scan_table)
721
420993
            scan_table = s->inter_scantable.permutated;
722
608175
        rl_vlc= rl->rl_vlc[s->qscale];
723
    }
724
  {
725
796431
    OPEN_READER(re, &s->gb);
726
    for(;;) {
727
4986167
        UPDATE_CACHE(re, &s->gb);
728
4986167
        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
729
4986167
        if (level==0) {
730
            int cache;
731
162066
            cache= GET_CACHE(re, &s->gb);
732
            /* escape */
733

162066
            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
734

96392
                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
735
                    /* third escape */
736
41550
                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
737
41550
                    UPDATE_CACHE(re, &s->gb);
738
41550
                    if(s->msmpeg4_version<=3){
739
34734
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
740
34734
                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
741
34734
                        level= SHOW_SBITS(re, &s->gb, 8);
742
34734
                        SKIP_COUNTER(re, &s->gb, 1+6+8);
743
                    }else{
744
                        int sign;
745
6816
                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
746
6816
                        if(!s->esc3_level_length){
747
                            int ll;
748
                            ff_dlog(s->avctx, "ESC-3 %X at %d %d\n",
749
                                    show_bits(&s->gb, 24), s->mb_x, s->mb_y);
750
384
                            if(s->qscale<8){
751
78
                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
752
78
                                if(ll==0){
753
                                    ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
754
                                }
755
                            }else{
756
306
                                ll=2;
757

2014
                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
758
1708
                                    ll++;
759
1708
                                    SKIP_BITS(re, &s->gb, 1);
760
                                }
761
306
                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
762
                            }
763
764
384
                            s->esc3_level_length= ll;
765
384
                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
766
384
                            UPDATE_CACHE(re, &s->gb);
767
                        }
768
6816
                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);
769
6816
                        SKIP_BITS(re, &s->gb, s->esc3_run_length);
770
771
6816
                        sign=  SHOW_UBITS(re, &s->gb, 1);
772
6816
                        SKIP_BITS(re, &s->gb, 1);
773
774
6816
                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
775
6816
                        SKIP_BITS(re, &s->gb, s->esc3_level_length);
776
6816
                        if(sign) level= -level;
777
                    }
778
779
                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
780
41550
                    if (level>0) level= level * qmul + qadd;
781
20607
                    else         level= level * qmul - qadd;
782
41550
                    i+= run + 1;
783
41550
                    if(last) i+=192;
784
                } else {
785
                    /* second escape */
786
54842
                    SKIP_BITS(re, &s->gb, 2);
787
54842
                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
788
54842
                    i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
789
54842
                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
790
54842
                    LAST_SKIP_BITS(re, &s->gb, 1);
791
                }
792
            } else {
793
                /* first escape */
794
65674
                SKIP_BITS(re, &s->gb, 1);
795
65674
                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
796
65674
                i+= run;
797
65674
                level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
798
65674
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
799
65674
                LAST_SKIP_BITS(re, &s->gb, 1);
800
            }
801
        } else {
802
4824101
            i+= run;
803
4824101
            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
804
4824101
            LAST_SKIP_BITS(re, &s->gb, 1);
805
        }
806
4986167
        if (i > 62){
807
796431
            i-= 192;
808
796431
            if(i&(~63)){
809
                const int left= get_bits_left(&s->gb);
810
                if (((i + 192 == 64 && level / qmul == -1) ||
811
                     !(s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) &&
812
                    left >= 0) {
813
                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
814
                    i = 63;
815
                    break;
816
                }else{
817
                    av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
818
                    return -1;
819
                }
820
            }
821
822
796431
            block[scan_table[i]] = level;
823
796431
            break;
824
        }
825
826
4189736
        block[scan_table[i]] = level;
827
    }
828
796431
    CLOSE_READER(re, &s->gb);
829
  }
830
838887
 not_coded:
831
838887
    if (s->mb_intra) {
832
230712
        ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
833
230712
        if (s->ac_pred) {
834
4194
            i = 63; /* XXX: not optimal */
835
        }
836
    }
837

838887
    if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
838
838887
    s->block_last_index[n] = i;
839
840
838887
    return 0;
841
}
842
843
228076
int ff_msmpeg4_decode_motion(MpegEncContext * s,
844
                                 int *mx_ptr, int *my_ptr)
845
{
846
    MVTable *mv;
847
    int code, mx, my;
848
849
228076
    mv = &ff_mv_tables[s->mv_table_index];
850
851
228076
    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
852
228076
    if (code < 0){
853
        av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
854
        return -1;
855
    }
856
228076
    if (code == mv->n) {
857
1856
        mx = get_bits(&s->gb, 6);
858
1856
        my = get_bits(&s->gb, 6);
859
    } else {
860
226220
        mx = mv->table_mvx[code];
861
226220
        my = mv->table_mvy[code];
862
    }
863
864
228076
    mx += *mx_ptr - 32;
865
228076
    my += *my_ptr - 32;
866
    /* WARNING : they do not do exactly modulo encoding */
867
228076
    if (mx <= -64)
868
22
        mx += 64;
869
228054
    else if (mx >= 64)
870
10
        mx -= 64;
871
872
228076
    if (my <= -64)
873
6
        my += 64;
874
228070
    else if (my >= 64)
875
4
        my -= 64;
876
228076
    *mx_ptr = mx;
877
228076
    *my_ptr = my;
878
228076
    return 0;
879
}
880
881
AVCodec ff_msmpeg4v1_decoder = {
882
    .name           = "msmpeg4v1",
883
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
884
    .type           = AVMEDIA_TYPE_VIDEO,
885
    .id             = AV_CODEC_ID_MSMPEG4V1,
886
    .priv_data_size = sizeof(MpegEncContext),
887
    .init           = ff_msmpeg4_decode_init,
888
    .close          = ff_h263_decode_end,
889
    .decode         = ff_h263_decode_frame,
890
    .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
891
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
892
    .max_lowres     = 3,
893
    .pix_fmts       = (const enum AVPixelFormat[]) {
894
        AV_PIX_FMT_YUV420P,
895
        AV_PIX_FMT_NONE
896
    },
897
};
898
899
AVCodec ff_msmpeg4v2_decoder = {
900
    .name           = "msmpeg4v2",
901
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
902
    .type           = AVMEDIA_TYPE_VIDEO,
903
    .id             = AV_CODEC_ID_MSMPEG4V2,
904
    .priv_data_size = sizeof(MpegEncContext),
905
    .init           = ff_msmpeg4_decode_init,
906
    .close          = ff_h263_decode_end,
907
    .decode         = ff_h263_decode_frame,
908
    .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
909
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
910
    .max_lowres     = 3,
911
    .pix_fmts       = (const enum AVPixelFormat[]) {
912
        AV_PIX_FMT_YUV420P,
913
        AV_PIX_FMT_NONE
914
    },
915
};
916
917
AVCodec ff_msmpeg4v3_decoder = {
918
    .name           = "msmpeg4",
919
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
920
    .type           = AVMEDIA_TYPE_VIDEO,
921
    .id             = AV_CODEC_ID_MSMPEG4V3,
922
    .priv_data_size = sizeof(MpegEncContext),
923
    .init           = ff_msmpeg4_decode_init,
924
    .close          = ff_h263_decode_end,
925
    .decode         = ff_h263_decode_frame,
926
    .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
927
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
928
    .max_lowres     = 3,
929
    .pix_fmts       = (const enum AVPixelFormat[]) {
930
        AV_PIX_FMT_YUV420P,
931
        AV_PIX_FMT_NONE
932
    },
933
};
934
935
AVCodec ff_wmv1_decoder = {
936
    .name           = "wmv1",
937
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
938
    .type           = AVMEDIA_TYPE_VIDEO,
939
    .id             = AV_CODEC_ID_WMV1,
940
    .priv_data_size = sizeof(MpegEncContext),
941
    .init           = ff_msmpeg4_decode_init,
942
    .close          = ff_h263_decode_end,
943
    .decode         = ff_h263_decode_frame,
944
    .capabilities   = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
945
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
946
    .max_lowres     = 3,
947
    .pix_fmts       = (const enum AVPixelFormat[]) {
948
        AV_PIX_FMT_YUV420P,
949
        AV_PIX_FMT_NONE
950
    },
951
};