GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/h261dec.c Lines: 269 338 79.6 %
Date: 2019-11-18 18:00:01 Branches: 123 186 66.1 %

Line Branch Exec Source
1
/*
2
 * H.261 decoder
3
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4
 * Copyright (c) 2004 Maarten Daniels
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * H.261 decoder.
26
 */
27
28
#include "libavutil/avassert.h"
29
#include "avcodec.h"
30
#include "mpeg_er.h"
31
#include "mpegutils.h"
32
#include "mpegvideo.h"
33
#include "h263.h"
34
#include "h261.h"
35
#include "internal.h"
36
37
#define H261_MBA_VLC_BITS 9
38
#define H261_MTYPE_VLC_BITS 6
39
#define H261_MV_VLC_BITS 7
40
#define H261_CBP_VLC_BITS 9
41
#define TCOEFF_VLC_BITS 9
42
#define MBA_STUFFING 33
43
#define MBA_STARTCODE 34
44
45
static VLC h261_mba_vlc;
46
static VLC h261_mtype_vlc;
47
static VLC h261_mv_vlc;
48
static VLC h261_cbp_vlc;
49
50
13
static av_cold void h261_decode_init_vlc(H261Context *h)
51
{
52
    static int done = 0;
53
54
13
    if (!done) {
55
7
        done = 1;
56
7
        INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
57
                        ff_h261_mba_bits, 1, 1,
58
                        ff_h261_mba_code, 1, 1, 662);
59
7
        INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
60
                        ff_h261_mtype_bits, 1, 1,
61
                        ff_h261_mtype_code, 1, 1, 80);
62
7
        INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
63
                        &ff_h261_mv_tab[0][1], 2, 1,
64
                        &ff_h261_mv_tab[0][0], 2, 1, 144);
65
7
        INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
66
                        &ff_h261_cbp_tab[0][1], 2, 1,
67
                        &ff_h261_cbp_tab[0][0], 2, 1, 512);
68

231
        INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
69
    }
70
13
}
71
72
13
static av_cold int h261_decode_init(AVCodecContext *avctx)
73
{
74
13
    H261Context *h          = avctx->priv_data;
75
13
    MpegEncContext *const s = &h->s;
76
77
    // set defaults
78
13
    ff_mpv_decode_defaults(s);
79
13
    ff_mpv_decode_init(s, avctx);
80
81
13
    s->out_format  = FMT_H261;
82
13
    s->low_delay   = 1;
83
13
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
84
85
13
    ff_h261_common_init();
86
13
    h261_decode_init_vlc(h);
87
88
13
    h->gob_start_code_skipped = 0;
89
90
13
    return 0;
91
}
92
93
/**
94
 * Decode the group of blocks header or slice header.
95
 * @return <0 if an error occurred
96
 */
97
3600
static int h261_decode_gob_header(H261Context *h)
98
{
99
    unsigned int val;
100
3600
    MpegEncContext *const s = &h->s;
101
102
3600
    if (!h->gob_start_code_skipped) {
103
        /* Check for GOB Start Code */
104
300
        val = show_bits(&s->gb, 15);
105
300
        if (val)
106
            return -1;
107
108
        /* We have a GBSC */
109
300
        skip_bits(&s->gb, 16);
110
    }
111
112
3600
    h->gob_start_code_skipped = 0;
113
114
3600
    h->gob_number = get_bits(&s->gb, 4); /* GN */
115
3600
    s->qscale     = get_bits(&s->gb, 5); /* GQUANT */
116
117
    /* Check if gob_number is valid */
118
3600
    if (s->mb_height == 18) { // CIF
119

3600
        if ((h->gob_number <= 0) || (h->gob_number > 12))
120
            return -1;
121
    } else { // QCIF
122
        if ((h->gob_number != 1) && (h->gob_number != 3) &&
123
            (h->gob_number != 5))
124
            return -1;
125
    }
126
127
    /* GEI */
128
3600
    if (skip_1stop_8data_bits(&s->gb) < 0)
129
        return AVERROR_INVALIDDATA;
130
131
3600
    if (s->qscale == 0) {
132
        av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
133
        if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
134
            return -1;
135
    }
136
137
    /* For the first transmitted macroblock in a GOB, MBA is the absolute
138
     * address. For subsequent macroblocks, MBA is the difference between
139
     * the absolute addresses of the macroblock and the last transmitted
140
     * macroblock. */
141
3600
    h->current_mba = 0;
142
3600
    h->mba_diff    = 0;
143
144
3600
    return 0;
145
}
146
147
/**
148
 * Decode the group of blocks / video packet header.
149
 * @return <0 if no resync found
150
 */
151
3600
static int h261_resync(H261Context *h)
152
{
153
3600
    MpegEncContext *const s = &h->s;
154
    int left, ret;
155
156
3600
    if (h->gob_start_code_skipped) {
157
3300
        ret = h261_decode_gob_header(h);
158
3300
        if (ret >= 0)
159
3300
            return 0;
160
    } else {
161
300
        if (show_bits(&s->gb, 15) == 0) {
162
300
            ret = h261_decode_gob_header(h);
163
300
            if (ret >= 0)
164
300
                return 0;
165
        }
166
        // OK, it is not where it is supposed to be ...
167
        s->gb = s->last_resync_gb;
168
        align_get_bits(&s->gb);
169
        left = get_bits_left(&s->gb);
170
171
        for (; left > 15 + 1 + 4 + 5; left -= 8) {
172
            if (show_bits(&s->gb, 15) == 0) {
173
                GetBitContext bak = s->gb;
174
175
                ret = h261_decode_gob_header(h);
176
                if (ret >= 0)
177
                    return 0;
178
179
                s->gb = bak;
180
            }
181
            skip_bits(&s->gb, 8);
182
        }
183
    }
184
185
    return -1;
186
}
187
188
/**
189
 * Decode skipped macroblocks.
190
 * @return 0
191
 */
192
121514
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
193
{
194
121514
    MpegEncContext *const s = &h->s;
195
    int i;
196
197
121514
    s->mb_intra = 0;
198
199
122400
    for (i = mba1; i < mba2; i++) {
200
        int j, xy;
201
202
886
        s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
203
886
        s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
204
886
        xy      = s->mb_x + s->mb_y * s->mb_stride;
205
886
        ff_init_block_index(s);
206
886
        ff_update_block_index(s);
207
208
6202
        for (j = 0; j < 6; j++)
209
5316
            s->block_last_index[j] = -1;
210
211
886
        s->mv_dir                      = MV_DIR_FORWARD;
212
886
        s->mv_type                     = MV_TYPE_16X16;
213
886
        s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
214
886
        s->mv[0][0][0]                 = 0;
215
886
        s->mv[0][0][1]                 = 0;
216
886
        s->mb_skipped                  = 1;
217
886
        h->mtype                      &= ~MB_TYPE_H261_FIL;
218
219
886
        if (s->current_picture.motion_val[0]) {
220
            int b_stride = 2*s->mb_width + 1;
221
            int b_xy     = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
222
            s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
223
            s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
224
        }
225
226
886
        ff_mpv_reconstruct_mb(s, s->block);
227
    }
228
229
121514
    return 0;
230
}
231
232
static const int mvmap[17] = {
233
    0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
234
};
235
236
193928
static int decode_mv_component(GetBitContext *gb, int v)
237
{
238
193928
    int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
239
240
    /* check if mv_diff is valid */
241
193928
    if (mv_diff < 0)
242
        return v;
243
244
193928
    mv_diff = mvmap[mv_diff];
245
246

193928
    if (mv_diff && !get_bits1(gb))
247
37209
        mv_diff = -mv_diff;
248
249
193928
    v += mv_diff;
250
193928
    if (v <= -16)
251
36
        v += 32;
252
193892
    else if (v >= 16)
253
30
        v -= 32;
254
255
193928
    return v;
256
}
257
258
/**
259
 * Decode a macroblock.
260
 * @return <0 if an error occurred
261
 */
262
590082
static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
263
{
264
590082
    MpegEncContext *const s = &h->s;
265
    int level, i, j, run;
266
590082
    RLTable *rl = &ff_h261_rl_tcoeff;
267
    const uint8_t *scan_table;
268
269
    /* For the variable length encoding there are two code tables, one being
270
     * used for the first transmitted LEVEL in INTER, INTER + MC and
271
     * INTER + MC + FIL blocks, the second for all other LEVELs except the
272
     * first one in INTRA blocks which is fixed length coded with 8 bits.
273
     * NOTE: The two code tables only differ in one VLC so we handle that
274
     * manually. */
275
590082
    scan_table = s->intra_scantable.permutated;
276
590082
    if (s->mb_intra) {
277
        /* DC coef */
278
106194
        level = get_bits(&s->gb, 8);
279
        // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
280
106194
        if ((level & 0x7F) == 0) {
281
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
282
                   level, s->mb_x, s->mb_y);
283
            return -1;
284
        }
285
        /* The code 1000 0000 is not used, the reconstruction level of 1024
286
         * being coded as 1111 1111. */
287
106194
        if (level == 255)
288
1211
            level = 128;
289
106194
        block[0] = level;
290
106194
        i        = 1;
291
483888
    } else if (coded) {
292
        // Run  Level   Code
293
        // EOB          Not possible for first level when cbp is available (that's why the table is different)
294
        // 0    1       1s
295
        // *    *       0*
296
242540
        int check = show_bits(&s->gb, 2);
297
242540
        i = 0;
298
242540
        if (check & 0x2) {
299
81615
            skip_bits(&s->gb, 2);
300
81615
            block[0] = (check & 0x1) ? -1 : 1;
301
81615
            i        = 1;
302
        }
303
    } else {
304
241348
        i = 0;
305
    }
306
590082
    if (!coded) {
307
241348
        s->block_last_index[n] = i - 1;
308
241348
        return 0;
309
    }
310
    {
311
348734
    OPEN_READER(re, &s->gb);
312
348734
    i--; // offset by -1 to allow direct indexing of scan_table
313
    for (;;) {
314
2586037
        UPDATE_CACHE(re, &s->gb);
315
2586037
        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TCOEFF_VLC_BITS, 2, 0);
316
2586037
        if (run == 66) {
317
65180
            if (level) {
318
                CLOSE_READER(re, &s->gb);
319
                av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
320
                       s->mb_x, s->mb_y);
321
                return -1;
322
            }
323
            /* escape */
324
            /* The remaining combinations of (run, level) are encoded with a
325
             * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
326
             * level. */
327
65180
            run   = SHOW_UBITS(re, &s->gb, 6) + 1;
328
65180
            SKIP_CACHE(re, &s->gb, 6);
329
65180
            level = SHOW_SBITS(re, &s->gb, 8);
330
65180
            SKIP_COUNTER(re, &s->gb, 6 + 8);
331
2520857
        } else if (level == 0) {
332
348734
            break;
333
        } else {
334
2172123
            if (SHOW_UBITS(re, &s->gb, 1))
335
1087153
                level = -level;
336
2172123
            SKIP_COUNTER(re, &s->gb, 1);
337
        }
338
2237303
        i += run;
339
2237303
        if (i >= 64) {
340
            CLOSE_READER(re, &s->gb);
341
            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
342
                   s->mb_x, s->mb_y);
343
            return -1;
344
        }
345
2237303
        j        = scan_table[i];
346
2237303
        block[j] = level;
347
    }
348
348734
    CLOSE_READER(re, &s->gb);
349
    }
350
348734
    s->block_last_index[n] = i;
351
348734
    return 0;
352
}
353
354
121514
static int h261_decode_mb(H261Context *h)
355
{
356
121514
    MpegEncContext *const s = &h->s;
357
    int i, cbp, xy;
358
359
121514
    cbp = 63;
360
    // Read mba
361
    do {
362
121514
        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
363
                               H261_MBA_VLC_BITS, 2);
364
365
        /* Check for slice end */
366
        /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
367
121514
        if (h->mba_diff == MBA_STARTCODE) { // start code
368
3300
            h->gob_start_code_skipped = 1;
369
3300
            return SLICE_END;
370
        }
371
118214
    } while (h->mba_diff == MBA_STUFFING); // stuffing
372
373
118214
    if (h->mba_diff < 0) {
374
300
        if (get_bits_left(&s->gb) <= 7)
375
300
            return SLICE_END;
376
377
        av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
378
        return SLICE_ERROR;
379
    }
380
381
117914
    h->mba_diff    += 1;
382
117914
    h->current_mba += h->mba_diff;
383
384
117914
    if (h->current_mba > MBA_STUFFING)
385
        return SLICE_ERROR;
386
387
117914
    s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
388
117914
    s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
389
117914
    xy      = s->mb_x + s->mb_y * s->mb_stride;
390
117914
    ff_init_block_index(s);
391
117914
    ff_update_block_index(s);
392
393
    // Read mtype
394
117914
    h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
395
117914
    if (h->mtype < 0) {
396
        av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
397
               h->mtype);
398
        return SLICE_ERROR;
399
    }
400
117914
    av_assert0(h->mtype < FF_ARRAY_ELEMS(ff_h261_mtype_map));
401
117914
    h->mtype = ff_h261_mtype_map[h->mtype];
402
403
    // Read mquant
404
117914
    if (IS_QUANT(h->mtype))
405
        ff_set_qscale(s, get_bits(&s->gb, 5));
406
407
117914
    s->mb_intra = IS_INTRA4x4(h->mtype);
408
409
    // Read mv
410
117914
    if (IS_16X16(h->mtype)) {
411
        /* Motion vector data is included for all MC macroblocks. MVD is
412
         * obtained from the macroblock vector by subtracting the vector
413
         * of the preceding macroblock. For this calculation the vector
414
         * of the preceding macroblock is regarded as zero in the
415
         * following three situations:
416
         * 1) evaluating MVD for macroblocks 1, 12 and 23;
417
         * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
418
         * 3) MTYPE of the previous macroblock was not MC. */
419

96964
        if ((h->current_mba ==  1) || (h->current_mba == 12) ||
420

91569
            (h->current_mba == 23) || (h->mba_diff != 1)) {
421
8682
            h->current_mv_x = 0;
422
8682
            h->current_mv_y = 0;
423
        }
424
425
96964
        h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
426
96964
        h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
427
    } else {
428
20950
        h->current_mv_x = 0;
429
20950
        h->current_mv_y = 0;
430
    }
431
432
    // Read cbp
433
117914
    if (HAS_CBP(h->mtype))
434
80648
        cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
435
436
117914
    if (s->mb_intra) {
437
17699
        s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
438
17699
        goto intra;
439
    }
440
441
    //set motion vectors
442
100215
    s->mv_dir                      = MV_DIR_FORWARD;
443
100215
    s->mv_type                     = MV_TYPE_16X16;
444
100215
    s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
445
100215
    s->mv[0][0][0]                 = h->current_mv_x * 2; // gets divided by 2 in motion compensation
446
100215
    s->mv[0][0][1]                 = h->current_mv_y * 2;
447
448
100215
    if (s->current_picture.motion_val[0]) {
449
        int b_stride = 2*s->mb_width + 1;
450
        int b_xy     = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
451
        s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
452
        s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
453
    }
454
455
100215
intra:
456
    /* decode each block */
457

117914
    if (s->mb_intra || HAS_CBP(h->mtype)) {
458
98347
        s->bdsp.clear_blocks(s->block[0]);
459
688429
        for (i = 0; i < 6; i++) {
460
590082
            if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
461
                return SLICE_ERROR;
462
590082
            cbp += cbp;
463
        }
464
    } else {
465
136969
        for (i = 0; i < 6; i++)
466
117402
            s->block_last_index[i] = -1;
467
    }
468
469
117914
    ff_mpv_reconstruct_mb(s, s->block);
470
471
117914
    return SLICE_OK;
472
}
473
474
/**
475
 * Decode the H.261 picture header.
476
 * @return <0 if no startcode found
477
 */
478
306
static int h261_decode_picture_header(H261Context *h)
479
{
480
306
    MpegEncContext *const s = &h->s;
481
    int format, i;
482
306
    uint32_t startcode = 0;
483
484
6120
    for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
485
6120
        startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
486
487
6120
        if (startcode == 0x10)
488
306
            break;
489
    }
490
491
306
    if (startcode != 0x10) {
492
        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
493
        return -1;
494
    }
495
496
    /* temporal reference */
497
306
    i = get_bits(&s->gb, 5); /* picture timestamp */
498
306
    if (i < (s->picture_number & 31))
499
6
        i += 32;
500
306
    s->picture_number = (s->picture_number & ~31) + i;
501
502
306
    s->avctx->framerate = (AVRational) { 30000, 1001 };
503
504
    /* PTYPE starts here */
505
306
    skip_bits1(&s->gb); /* split screen off */
506
306
    skip_bits1(&s->gb); /* camera  off */
507
306
    skip_bits1(&s->gb); /* freeze picture release off */
508
509
306
    format = get_bits1(&s->gb);
510
511
    // only 2 formats possible
512
306
    if (format == 0) { // QCIF
513
        s->width     = 176;
514
        s->height    = 144;
515
        s->mb_width  = 11;
516
        s->mb_height = 9;
517
    } else { // CIF
518
306
        s->width     = 352;
519
306
        s->height    = 288;
520
306
        s->mb_width  = 22;
521
306
        s->mb_height = 18;
522
    }
523
524
306
    s->mb_num = s->mb_width * s->mb_height;
525
526
306
    skip_bits1(&s->gb); /* still image mode off */
527
306
    skip_bits1(&s->gb); /* Reserved */
528
529
    /* PEI */
530
306
    if (skip_1stop_8data_bits(&s->gb) < 0)
531
        return AVERROR_INVALIDDATA;
532
533
    /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
534
     * frame, the codec crashes if it does not contain all I-blocks
535
     * (e.g. when a packet is lost). */
536
306
    s->pict_type = AV_PICTURE_TYPE_P;
537
538
306
    h->gob_number = 0;
539
306
    return 0;
540
}
541
542
3600
static int h261_decode_gob(H261Context *h)
543
{
544
3600
    MpegEncContext *const s = &h->s;
545
546
3600
    ff_set_qscale(s, s->qscale);
547
548
    /* decode mb's */
549
121514
    while (h->current_mba <= MBA_STUFFING) {
550
        int ret;
551
        /* DCT & quantize */
552
121514
        ret = h261_decode_mb(h);
553
121514
        if (ret < 0) {
554
3600
            if (ret == SLICE_END) {
555
3600
                h261_decode_mb_skipped(h, h->current_mba, 33);
556
3600
                return 0;
557
            }
558
            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
559
                   s->mb_x + s->mb_y * s->mb_stride);
560
            return -1;
561
        }
562
563
117914
        h261_decode_mb_skipped(h,
564
117914
                               h->current_mba - h->mba_diff,
565
117914
                               h->current_mba - 1);
566
    }
567
568
    return -1;
569
}
570
571
/**
572
 * returns the number of bytes consumed for building the current frame
573
 */
574
300
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
575
{
576
300
    int pos = get_bits_count(&s->gb) >> 3;
577
300
    if (pos == 0)
578
        pos = 1;      // avoid infinite loops (i doubt that is needed but ...)
579
300
    if (pos + 10 > buf_size)
580
300
        pos = buf_size;               // oops ;)
581
582
300
    return pos;
583
}
584
585
300
static int h261_decode_frame(AVCodecContext *avctx, void *data,
586
                             int *got_frame, AVPacket *avpkt)
587
{
588
300
    const uint8_t *buf = avpkt->data;
589
300
    int buf_size       = avpkt->size;
590
300
    H261Context *h     = avctx->priv_data;
591
300
    MpegEncContext *s  = &h->s;
592
    int ret;
593
300
    AVFrame *pict = data;
594
595
    ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
596
    ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
597
598
300
    h->gob_start_code_skipped = 0;
599
600
306
retry:
601
306
    init_get_bits(&s->gb, buf, buf_size * 8);
602
603
306
    if (!s->context_initialized)
604
        // we need the IDCT permutation for reading a custom matrix
605
6
        ff_mpv_idct_init(s);
606
607
306
    ret = h261_decode_picture_header(h);
608
609
    /* skip if the header was thrashed */
610
306
    if (ret < 0) {
611
        av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
612
        return -1;
613
    }
614
615

306
    if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
616
        ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
617
        s->parse_context.buffer = 0;
618
        ff_mpv_common_end(s);
619
        s->parse_context = pc;
620
    }
621
622
306
    if (!s->context_initialized) {
623
6
        if ((ret = ff_mpv_common_init(s)) < 0)
624
            return ret;
625
626
6
        ret = ff_set_dimensions(avctx, s->width, s->height);
627
6
        if (ret < 0)
628
            return ret;
629
630
6
        goto retry;
631
    }
632
633
    // for skipping the frame
634
300
    s->current_picture.f->pict_type = s->pict_type;
635
300
    s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
636
637

300
    if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
638

300
        (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
639
300
         avctx->skip_frame >= AVDISCARD_ALL)
640
        return get_consumed_bytes(s, buf_size);
641
642
300
    if (ff_mpv_frame_start(s, avctx) < 0)
643
        return -1;
644
645
300
    ff_mpeg_er_frame_start(s);
646
647
    /* decode each macroblock */
648
300
    s->mb_x = 0;
649
300
    s->mb_y = 0;
650
651

3900
    while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
652
3600
        if (h261_resync(h) < 0)
653
            break;
654
3600
        h261_decode_gob(h);
655
    }
656
300
    ff_mpv_frame_end(s);
657
658
300
    av_assert0(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
659
300
    av_assert0(s->current_picture.f->pict_type == s->pict_type);
660
661
300
    if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
662
        return ret;
663
300
    ff_print_debug_info(s, s->current_picture_ptr, pict);
664
665
300
    *got_frame = 1;
666
667
300
    return get_consumed_bytes(s, buf_size);
668
}
669
670
13
static av_cold int h261_decode_end(AVCodecContext *avctx)
671
{
672
13
    H261Context *h    = avctx->priv_data;
673
13
    MpegEncContext *s = &h->s;
674
675
13
    ff_mpv_common_end(s);
676
13
    return 0;
677
}
678
679
AVCodec ff_h261_decoder = {
680
    .name           = "h261",
681
    .long_name      = NULL_IF_CONFIG_SMALL("H.261"),
682
    .type           = AVMEDIA_TYPE_VIDEO,
683
    .id             = AV_CODEC_ID_H261,
684
    .priv_data_size = sizeof(H261Context),
685
    .init           = h261_decode_init,
686
    .close          = h261_decode_end,
687
    .decode         = h261_decode_frame,
688
    .capabilities   = AV_CODEC_CAP_DR1,
689
    .max_lowres     = 3,
690
};