GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/h261dec.c Lines: 264 330 80.0 %
Date: 2021-04-20 15:25:36 Branches: 116 178 65.2 %

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 "libavutil/thread.h"
30
#include "avcodec.h"
31
#include "mpeg_er.h"
32
#include "mpegutils.h"
33
#include "mpegvideo.h"
34
#include "h263.h"
35
#include "h261.h"
36
#include "internal.h"
37
38
#define H261_MBA_VLC_BITS 8
39
#define H261_MTYPE_VLC_BITS 6
40
#define H261_MV_VLC_BITS 7
41
#define H261_CBP_VLC_BITS 9
42
#define TCOEFF_VLC_BITS 9
43
#define MBA_STUFFING 33
44
#define MBA_STARTCODE 34
45
46
static VLC h261_mba_vlc;
47
static VLC h261_mtype_vlc;
48
static VLC h261_mv_vlc;
49
static VLC h261_cbp_vlc;
50
51
7
static av_cold void h261_decode_init_static(void)
52
{
53
7
    INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
54
                    ff_h261_mba_bits, 1, 1,
55
                    ff_h261_mba_code, 1, 1, 540);
56
7
    INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
57
                    ff_h261_mtype_bits, 1, 1,
58
                    ff_h261_mtype_code, 1, 1, 80);
59
7
    INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
60
                    &ff_h261_mv_tab[0][1], 2, 1,
61
                    &ff_h261_mv_tab[0][0], 2, 1, 144);
62
7
    INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
63
                    &ff_h261_cbp_tab[0][1], 2, 1,
64
                    &ff_h261_cbp_tab[0][0], 2, 1, 512);
65
7
    INIT_FIRST_VLC_RL(ff_h261_rl_tcoeff, 552);
66
7
}
67
68
13
static av_cold int h261_decode_init(AVCodecContext *avctx)
69
{
70
    static AVOnce init_static_once = AV_ONCE_INIT;
71
13
    H261Context *h          = avctx->priv_data;
72
13
    MpegEncContext *const s = &h->s;
73
74
    // set defaults
75
13
    ff_mpv_decode_init(s, avctx);
76
77
13
    s->out_format  = FMT_H261;
78
13
    s->low_delay   = 1;
79
13
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
80
81
13
    h->gob_start_code_skipped = 0;
82
13
    ff_mpv_idct_init(s);
83
84
13
    ff_thread_once(&init_static_once, h261_decode_init_static);
85
86
13
    return 0;
87
}
88
89
/**
90
 * Decode the group of blocks header or slice header.
91
 * @return <0 if an error occurred
92
 */
93
3600
static int h261_decode_gob_header(H261Context *h)
94
{
95
    unsigned int val;
96
3600
    MpegEncContext *const s = &h->s;
97
98
3600
    if (!h->gob_start_code_skipped) {
99
        /* Check for GOB Start Code */
100
300
        val = show_bits(&s->gb, 15);
101
300
        if (val)
102
            return -1;
103
104
        /* We have a GBSC */
105
300
        skip_bits(&s->gb, 16);
106
    }
107
108
3600
    h->gob_start_code_skipped = 0;
109
110
3600
    h->gob_number = get_bits(&s->gb, 4); /* GN */
111
3600
    s->qscale     = get_bits(&s->gb, 5); /* GQUANT */
112
113
    /* Check if gob_number is valid */
114
3600
    if (s->mb_height == 18) { // CIF
115

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

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

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

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

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

306
    if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
608
        ff_mpv_common_end(s);
609
    }
610
611
306
    if (!s->context_initialized) {
612
6
        if ((ret = ff_mpv_common_init(s)) < 0)
613
            return ret;
614
615
6
        ret = ff_set_dimensions(avctx, s->width, s->height);
616
6
        if (ret < 0)
617
            return ret;
618
619
6
        goto retry;
620
    }
621
622
    // for skipping the frame
623
300
    s->current_picture.f->pict_type = s->pict_type;
624
300
    s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
625
626

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

300
        (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
628
300
         avctx->skip_frame >= AVDISCARD_ALL)
629
        return get_consumed_bytes(s, buf_size);
630
631
300
    if (ff_mpv_frame_start(s, avctx) < 0)
632
        return -1;
633
634
300
    ff_mpeg_er_frame_start(s);
635
636
    /* decode each macroblock */
637
300
    s->mb_x = 0;
638
300
    s->mb_y = 0;
639
640

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