Coverage Report

Created: 2026-05-16 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/h263dec.c
Line
Count
Source
1
/*
2
 * H.263 decoder
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
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.263 decoder.
26
 */
27
28
#define UNCHECKED_BITSTREAM_READER 1
29
30
#include "config_components.h"
31
32
#include "avcodec.h"
33
#include "codec_internal.h"
34
#include "decode.h"
35
#include "error_resilience.h"
36
#include "flvdec.h"
37
#include "h263.h"
38
#include "h263dec.h"
39
#include "hwaccel_internal.h"
40
#include "hwconfig.h"
41
#include "mpeg_er.h"
42
#include "mpeg4video.h"
43
#include "mpeg4videodec.h"
44
#include "mpegvideo.h"
45
#include "mpegvideodata.h"
46
#include "mpegvideodec.h"
47
#include "mpegvideo_unquantize.h"
48
#include "msmpeg4dec.h"
49
#include "thread.h"
50
#include "wmv2dec.h"
51
52
static const enum AVPixelFormat h263_hwaccel_pixfmt_list_420[] = {
53
#if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
54
    AV_PIX_FMT_VAAPI,
55
#endif
56
#if CONFIG_MPEG4_NVDEC_HWACCEL
57
    AV_PIX_FMT_CUDA,
58
#endif
59
#if CONFIG_MPEG4_VDPAU_HWACCEL
60
    AV_PIX_FMT_VDPAU,
61
#endif
62
#if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
63
    AV_PIX_FMT_VIDEOTOOLBOX,
64
#endif
65
    AV_PIX_FMT_YUV420P,
66
    AV_PIX_FMT_NONE
67
};
68
69
static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
70
150k
{
71
    /* MPEG-4 Studio Profile only, not supported by hardware */
72
150k
    if (avctx->bits_per_raw_sample > 8) {
73
6.26k
        av_assert1(((MpegEncContext *)avctx->priv_data)->studio_profile);
74
6.26k
        return avctx->pix_fmt;
75
6.26k
    }
76
77
144k
    if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY)) {
78
0
        if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
79
0
            avctx->color_range = AVCOL_RANGE_MPEG;
80
0
        return AV_PIX_FMT_GRAY8;
81
0
    }
82
83
144k
    if (avctx->codec_id == AV_CODEC_ID_H263  ||
84
109k
        avctx->codec_id == AV_CODEC_ID_H263P ||
85
92.7k
        avctx->codec_id == AV_CODEC_ID_MPEG4)
86
78.4k
        return avctx->pix_fmt = ff_get_format(avctx, h263_hwaccel_pixfmt_list_420);
87
88
66.0k
    return AV_PIX_FMT_YUV420P;
89
144k
}
90
91
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
92
51.9k
{
93
51.9k
    H263DecContext *const h = avctx->priv_data;
94
51.9k
    MPVContext *const s = &h->c;
95
51.9k
    MPVUnquantDSPContext unquant_dsp_ctx;
96
51.9k
    int ret;
97
98
51.9k
    s->out_format      = FMT_H263;
99
100
    // set defaults
101
51.9k
    ret = ff_mpv_decode_init(s, avctx);
102
51.9k
    if (ret < 0)
103
0
        return ret;
104
105
51.9k
    h->decode_mb       = ff_h263_decode_mb;
106
51.9k
    s->low_delay       = 1;
107
108
51.9k
    s->y_dc_scale_table =
109
51.9k
    s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
110
111
51.9k
    ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
112
51.9k
                         s->idsp.idct_permutation);
113
51.9k
    ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
114
51.9k
                         s->idsp.idct_permutation);
115
116
51.9k
    ff_mpv_unquantize_init(&unquant_dsp_ctx,
117
51.9k
                           avctx->flags & AV_CODEC_FLAG_BITEXACT, 0);
118
    // dct_unquantize defaults for H.263;
119
    // they might change on a per-frame basis for MPEG-4;
120
    // dct_unquantize_inter will be unset for MSMPEG4 codecs later.
121
51.9k
    s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
122
51.9k
    s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_h263_inter;
123
124
    /* select sub codec */
125
51.9k
    switch (avctx->codec->id) {
126
6.38k
    case AV_CODEC_ID_H263:
127
12.2k
    case AV_CODEC_ID_H263P:
128
12.2k
        avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
129
12.2k
        h->decode_header = ff_h263_decode_picture_header;
130
12.2k
        break;
131
12.0k
    case AV_CODEC_ID_MPEG4:
132
12.0k
        break;
133
1.82k
    case AV_CODEC_ID_MSMPEG4V1:
134
1.82k
        s->h263_pred       = 1;
135
1.82k
        s->msmpeg4_version = MSMP4_V1;
136
1.82k
        break;
137
2.37k
    case AV_CODEC_ID_MSMPEG4V2:
138
2.37k
        s->h263_pred       = 1;
139
2.37k
        s->msmpeg4_version = MSMP4_V2;
140
2.37k
        break;
141
2.22k
    case AV_CODEC_ID_MSMPEG4V3:
142
2.22k
        s->h263_pred       = 1;
143
2.22k
        s->msmpeg4_version = MSMP4_V3;
144
2.22k
        break;
145
2.53k
    case AV_CODEC_ID_WMV1:
146
2.53k
        s->h263_pred       = 1;
147
2.53k
        s->msmpeg4_version = MSMP4_WMV1;
148
2.53k
        break;
149
2.70k
    case AV_CODEC_ID_WMV2:
150
2.70k
        s->h263_pred       = 1;
151
2.70k
        s->msmpeg4_version = MSMP4_WMV2;
152
2.70k
        break;
153
3.15k
    case AV_CODEC_ID_RV10:
154
7.11k
    case AV_CODEC_ID_RV20:
155
7.11k
        break;
156
0
#if CONFIG_H263I_DECODER
157
4.66k
    case AV_CODEC_ID_H263I:
158
4.66k
        h->decode_header = ff_intel_h263_decode_picture_header;
159
4.66k
        break;
160
0
#endif
161
0
#if CONFIG_FLV_DECODER
162
4.24k
    case AV_CODEC_ID_FLV1:
163
4.24k
        h->decode_header = ff_flv_decode_picture_header;
164
4.24k
        break;
165
0
#endif
166
0
    default:
167
0
        av_unreachable("Switch contains a case for every codec using ff_h263_decode_init()");
168
51.9k
    }
169
170
51.9k
    if (avctx->codec_tag == AV_RL32("L263") || avctx->codec_tag == AV_RL32("S263"))
171
66
        if (avctx->extradata_size == 56 && avctx->extradata[0] == 1)
172
10
            h->ehc_mode = 1;
173
174
    /* for H.263, we allocate the images after having read the header */
175
51.9k
    if (avctx->codec->id != AV_CODEC_ID_H263 &&
176
45.5k
        avctx->codec->id != AV_CODEC_ID_H263P &&
177
39.7k
        avctx->codec->id != AV_CODEC_ID_MPEG4) {
178
27.6k
        avctx->pix_fmt = h263_get_format(avctx);
179
27.6k
        if ((ret = ff_mpv_common_init(s)) < 0)
180
0
            return ret;
181
27.6k
    }
182
183
51.9k
    ff_h263dsp_init(&s->h263dsp);
184
51.9k
    ff_h263_decode_init_vlc();
185
186
51.9k
    return 0;
187
51.9k
}
188
189
static void report_decode_progress(H263DecContext *const h)
190
5.28M
{
191
5.28M
    if (h->c.pict_type != AV_PICTURE_TYPE_B && !h->partitioned_frame && !h->c.er.error_occurred)
192
3.98M
        ff_thread_progress_report(&h->c.cur_pic.ptr->progress, h->c.mb_y);
193
5.28M
}
194
195
static int decode_slice(H263DecContext *const h)
196
2.37M
{
197
2.37M
    const int part_mask = h->partitioned_frame
198
2.37M
                          ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
199
2.37M
    const int mb_size   = 16 >> h->c.avctx->lowres;
200
2.37M
    int ret;
201
202
2.37M
    h->last_resync_gb     = h->gb;
203
2.37M
    h->c.first_slice_line = 1;
204
2.37M
    h->c.resync_mb_x      = h->c.mb_x;
205
2.37M
    h->c.resync_mb_y      = h->c.mb_y;
206
207
2.37M
    ff_set_qscale(&h->c, h->c.qscale);
208
209
2.37M
#if CONFIG_MPEG4_DECODER
210
2.37M
    if (h->c.studio_profile) {
211
37.7k
        if ((ret = ff_mpeg4_decode_studio_slice_header(h)) < 0)
212
2.88k
            return ret;
213
37.7k
    }
214
2.36M
#endif
215
216
2.36M
    if (h->c.avctx->hwaccel) {
217
0
        const uint8_t *start = h->gb.buffer + get_bits_count(&h->gb) / 8;
218
0
        ret = FF_HW_CALL(h->c.avctx, decode_slice, start,
219
0
                         get_bits_bytesize(&h->gb, 0) - get_bits_count(&h->gb) / 8);
220
        // ensure we exit decode loop
221
0
        h->c.mb_y = h->c.mb_height;
222
0
        return ret;
223
0
    }
224
225
2.36M
#if CONFIG_MPEG4_DECODER
226
2.36M
    if (h->partitioned_frame) {
227
53.6k
        const int qscale = h->c.qscale;
228
229
53.6k
        av_assert1(h->c.codec_id == AV_CODEC_ID_MPEG4);
230
231
53.6k
        ret = ff_mpeg4_decode_partitions(h);
232
53.6k
        if (ret < 0)
233
47.3k
            return ret;
234
235
        /* restore variables which were modified */
236
6.28k
        h->c.first_slice_line = 1;
237
6.28k
        h->c.mb_x             = h->c.resync_mb_x;
238
6.28k
        h->c.mb_y             = h->c.resync_mb_y;
239
6.28k
        ff_set_qscale(&h->c, qscale);
240
6.28k
    }
241
2.31M
#endif
242
243
7.58M
    for (; h->c.mb_y < h->c.mb_height; h->c.mb_y++) {
244
        /* per-row end of slice checks */
245
7.51M
        if (h->c.msmpeg4_version != MSMP4_UNUSED) {
246
5.25M
            if (h->c.resync_mb_y + h->slice_height == h->c.mb_y) {
247
434k
                ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
248
434k
                                h->c.mb_x - 1, h->c.mb_y, ER_MB_END);
249
250
434k
                return 0;
251
434k
            }
252
5.25M
        }
253
254
7.07M
        if (h->c.msmpeg4_version == MSMP4_V1) {
255
910k
            h->last_dc[0] =
256
910k
            h->last_dc[1] =
257
910k
            h->last_dc[2] = 128;
258
910k
        }
259
260
7.07M
        ff_init_block_index(&h->c);
261
47.7M
        for (; h->c.mb_x < h->c.mb_width; h->c.mb_x++) {
262
42.4M
            int ret;
263
264
42.4M
            ff_update_block_index(&h->c, h->c.avctx->bits_per_raw_sample,
265
42.4M
                                  h->c.avctx->lowres, h->c.chroma_x_shift);
266
267
42.4M
            if (h->c.resync_mb_x == h->c.mb_x && h->c.resync_mb_y + 1 == h->c.mb_y)
268
183k
                h->c.first_slice_line = 0;
269
270
            /* DCT & quantize */
271
272
42.4M
            h->c.mv_dir  = MV_DIR_FORWARD;
273
42.4M
            h->c.mv_type = MV_TYPE_16X16;
274
42.4M
            ff_dlog(h->c.avctx, "%d %06X\n",
275
42.4M
                    get_bits_count(&h->gb), show_bits(&h->gb, 24));
276
277
42.4M
            ff_tlog(NULL, "Decoding MB at %dx%d\n", h->c.mb_x, h->c.mb_y);
278
42.4M
            ret = h->decode_mb(h);
279
280
42.4M
            if (h->c.h263_pred || h->c.h263_aic) {
281
28.7M
                int mb_xy = h->c.mb_y * h->c.mb_stride + h->c.mb_x;
282
28.7M
                if (!h->c.mb_intra) {
283
23.2M
                    ff_h263_clean_intra_table_entries(&h->c, mb_xy);
284
23.2M
                } else
285
5.49M
                    h->c.mbintra_table[mb_xy] = 1;
286
28.7M
            }
287
288
42.4M
            if (h->c.pict_type != AV_PICTURE_TYPE_B)
289
41.9M
                ff_h263_update_motion_val(&h->c);
290
291
42.4M
            if (ret < 0) {
292
1.81M
                const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
293
1.81M
                if (ret == SLICE_END) {
294
214k
                    ff_mpv_reconstruct_mb(&h->c, h->block);
295
214k
                    if (h->loop_filter)
296
10.7k
                        ff_h263_loop_filter(&h->c);
297
298
214k
                    ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
299
214k
                                    h->c.mb_x, h->c.mb_y, ER_MB_END & part_mask);
300
301
214k
                    h->padding_bug_score--;
302
303
214k
                    if (++h->c.mb_x >= h->c.mb_width) {
304
20.3k
                        h->c.mb_x = 0;
305
20.3k
                        report_decode_progress(h);
306
20.3k
                        ff_mpeg_draw_horiz_band(&h->c, h->c.mb_y * mb_size, mb_size);
307
20.3k
                        h->c.mb_y++;
308
20.3k
                    }
309
214k
                    return 0;
310
1.59M
                } else if (ret == SLICE_NOEND) {
311
761
                    av_log(h->c.avctx, AV_LOG_ERROR,
312
761
                           "Slice mismatch at MB: %d\n", xy);
313
761
                    ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
314
761
                                    h->c.mb_x + 1, h->c.mb_y,
315
761
                                    ER_MB_END & part_mask);
316
761
                    return AVERROR_INVALIDDATA;
317
761
                }
318
1.59M
                av_log(h->c.avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
319
1.59M
                ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
320
1.59M
                                h->c.mb_x, h->c.mb_y, ER_MB_ERROR & part_mask);
321
322
1.59M
                if ((h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) && get_bits_left(&h->gb) > 0)
323
0
                    continue;
324
1.59M
                return AVERROR_INVALIDDATA;
325
1.59M
            }
326
327
40.6M
            ff_mpv_reconstruct_mb(&h->c, h->block);
328
40.6M
            if (h->loop_filter)
329
4.14M
                ff_h263_loop_filter(&h->c);
330
40.6M
        }
331
332
5.26M
        report_decode_progress(h);
333
5.26M
        ff_mpeg_draw_horiz_band(&h->c, h->c.mb_y * mb_size, mb_size);
334
335
5.26M
        h->c.mb_x = 0;
336
5.26M
    }
337
338
73.4k
    av_assert1(h->c.mb_x == 0 && h->c.mb_y == h->c.mb_height);
339
340
    // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
341
73.4k
    if (h->c.codec_id == AV_CODEC_ID_MPEG4         &&
342
13.7k
        (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
343
10.2k
        get_bits_left(&h->gb) >= 48                &&
344
7.40k
        show_bits(&h->gb, 24) == 0x4010            &&
345
1.65k
        !h->data_partitioning)
346
278
        h->padding_bug_score += 32;
347
348
    /* try to detect the padding bug */
349
73.4k
    if (h->c.codec_id == AV_CODEC_ID_MPEG4         &&
350
13.7k
        (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
351
10.2k
        get_bits_left(&h->gb) >= 0                 &&
352
10.0k
        get_bits_left(&h->gb) < 137                &&
353
4.95k
        !h->data_partitioning) {
354
4.29k
        const int bits_count = get_bits_count(&h->gb);
355
4.29k
        const int bits_left  = h->gb.size_in_bits - bits_count;
356
357
4.29k
        if (bits_left == 0) {
358
221
            h->padding_bug_score += 16;
359
4.07k
        } else if (bits_left != 1) {
360
3.42k
            int v = show_bits(&h->gb, 8);
361
3.42k
            v |= 0x7F >> (7 - (bits_count & 7));
362
363
3.42k
            if (v == 0x7F && bits_left <= 8)
364
194
                h->padding_bug_score--;
365
3.23k
            else if (v == 0x7F && ((get_bits_count(&h->gb) + 8) & 8) &&
366
692
                     bits_left <= 16)
367
209
                h->padding_bug_score += 4;
368
3.02k
            else
369
3.02k
                h->padding_bug_score++;
370
3.42k
        }
371
4.29k
    }
372
373
73.4k
    if (h->c.codec_id == AV_CODEC_ID_H263          &&
374
7.78k
        (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
375
5.91k
        get_bits_left(&h->gb) >= 8                 &&
376
5.55k
        get_bits_left(&h->gb) < 300                &&
377
3.40k
        h->c.pict_type == AV_PICTURE_TYPE_I        &&
378
1.49k
        show_bits(&h->gb, 8) == 0                  &&
379
1.06k
        !h->data_partitioning) {
380
381
1.06k
        h->padding_bug_score += 32;
382
1.06k
    }
383
384
73.4k
    if (h->c.codec_id == AV_CODEC_ID_H263          &&
385
7.78k
        (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
386
5.91k
        get_bits_left(&h->gb) >= 64                &&
387
4.88k
        AV_RB64(h->gb.buffer + (get_bits_bytesize(&h->gb, 0) - 8)) == 0xCDCDCDCDFC7F0000) {
388
389
495
        h->padding_bug_score += 32;
390
495
    }
391
392
73.4k
    if (h->c.workaround_bugs & FF_BUG_AUTODETECT) {
393
30.2k
        if (
394
30.2k
            (h->padding_bug_score > -2 && !h->data_partitioning))
395
26.1k
            h->c.workaround_bugs |= FF_BUG_NO_PADDING;
396
4.16k
        else
397
4.16k
            h->c.workaround_bugs &= ~FF_BUG_NO_PADDING;
398
30.2k
    }
399
400
    // handle formats which don't have unique end markers
401
73.4k
    if (h->c.msmpeg4_version != MSMP4_UNUSED || (h->c.workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
402
65.2k
        int left      = get_bits_left(&h->gb);
403
65.2k
        int max_extra = 7;
404
405
        /* no markers in M$ crap */
406
65.2k
        if (h->c.msmpeg4_version != MSMP4_UNUSED && h->c.pict_type == AV_PICTURE_TYPE_I)
407
4.41k
            max_extra += 17;
408
409
        /* buggy padding but the frame should still end approximately at
410
         * the bitstream end */
411
65.2k
        if ((h->c.workaround_bugs & FF_BUG_NO_PADDING) &&
412
42.0k
            (h->c.avctx->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
413
16.3k
            max_extra += 48;
414
48.8k
        else if ((h->c.workaround_bugs & FF_BUG_NO_PADDING))
415
25.7k
            max_extra += 256 * 256 * 256 * 64;
416
417
65.2k
        if (left > max_extra)
418
16.7k
            av_log(h->c.avctx, AV_LOG_ERROR,
419
16.7k
                   "discarding %d junk bits at end, next would be %X\n",
420
16.7k
                   left, show_bits(&h->gb, 24));
421
48.4k
        else if (left < 0)
422
18.3k
            av_log(h->c.avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
423
30.0k
        else
424
30.0k
            ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
425
30.0k
                            h->c.mb_x - 1, h->c.mb_y, ER_MB_END);
426
427
65.2k
        return 0;
428
65.2k
    }
429
430
8.18k
    av_log(h->c.avctx, AV_LOG_ERROR,
431
8.18k
           "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
432
8.18k
           get_bits_left(&h->gb), show_bits(&h->gb, 24), h->padding_bug_score);
433
434
8.18k
    ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, h->c.mb_x, h->c.mb_y,
435
8.18k
                    ER_MB_END & part_mask);
436
437
8.18k
    return AVERROR_INVALIDDATA;
438
73.4k
}
439
440
int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict,
441
                         int *got_frame, AVPacket *avpkt)
442
2.85M
{
443
2.85M
    H263DecContext *const h = avctx->priv_data;
444
2.85M
    MPVContext *const s = &h->c;
445
2.85M
    const uint8_t *buf = avpkt->data;
446
2.85M
    int buf_size       = avpkt->size;
447
2.85M
    int ret;
448
2.85M
    int slice_ret = 0;
449
2.85M
    int bak_width, bak_height;
450
451
    /* no supplementary picture */
452
2.85M
    if (buf_size == 0) {
453
        /* special case for last picture */
454
32.0k
        if ((!h->c.low_delay || h->skipped_last_frame) && h->c.next_pic.ptr) {
455
8.10k
            if ((ret = av_frame_ref(pict, h->c.next_pic.ptr->f)) < 0)
456
0
                return ret;
457
8.10k
            if (h->skipped_last_frame) {
458
                /* If the stream ended with an NVOP, we output the last frame
459
                 * in display order, but with the props from the last input
460
                 * packet so that the stream's end time is correct. */
461
82
                ret = ff_decode_frame_props(avctx, pict);
462
82
                if (ret < 0)
463
0
                    return ret;
464
82
            }
465
466
8.10k
            ff_mpv_unref_picture(&h->c.next_pic);
467
468
8.10k
            *got_frame = 1;
469
8.10k
        }
470
471
32.0k
        return 0;
472
32.0k
    }
473
474
    // h->gb might be overridden in ff_mpeg4_decode_picture_header() below.
475
2.82M
    ret = init_get_bits8(&h->gb, buf, buf_size);
476
2.82M
    if (ret < 0)
477
0
        return ret;
478
479
2.82M
    bak_width  = h->c.width;
480
2.82M
    bak_height = h->c.height;
481
482
    /* let's go :-) */
483
2.82M
    ret = h->decode_header(h);
484
2.82M
    if (ret < 0 || ret == FRAME_SKIPPED) {
485
1.64M
        if (   h->c.width  != bak_width
486
1.61M
            || h->c.height != bak_height) {
487
25.6k
                av_log(h->c.avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
488
25.6k
                h->c.width = bak_width;
489
25.6k
                h->c.height= bak_height;
490
491
25.6k
        }
492
1.64M
    }
493
2.82M
    if (ret == FRAME_SKIPPED)
494
169k
        return buf_size;
495
496
    /* skip if the header was thrashed */
497
2.65M
    if (ret < 0) {
498
1.47M
        av_log(h->c.avctx, AV_LOG_ERROR, "header damaged\n");
499
1.47M
        return ret;
500
1.47M
    }
501
502
1.18M
    if (!h->c.context_initialized) {
503
23.8k
        avctx->pix_fmt = h263_get_format(avctx);
504
23.8k
        if ((ret = ff_mpv_common_init(s)) < 0)
505
3.48k
            return ret;
506
23.8k
    }
507
508
1.17M
    avctx->has_b_frames = !h->c.low_delay;
509
510
1.17M
#if CONFIG_MPEG4_DECODER
511
1.17M
    if (avctx->codec_id == AV_CODEC_ID_MPEG4) {
512
218k
        if (h->c.pict_type != AV_PICTURE_TYPE_B && h->c.mb_num/2 > get_bits_left(&h->gb))
513
27.2k
            return AVERROR_INVALIDDATA;
514
191k
        ff_mpeg4_workaround_bugs(avctx);
515
191k
        if (h->c.studio_profile != (h->c.idsp.idct == NULL))
516
16.0k
            ff_mpv_idct_init(s);
517
191k
    }
518
1.15M
#endif
519
520
    /* After H.263 & MPEG-4 header decode we have the height, width,
521
     * and other parameters. So then we could init the picture. */
522
1.15M
    if (h->c.width  != avctx->coded_width  ||
523
1.05M
        h->c.height != avctx->coded_height ||
524
1.05M
        h->c.context_reinit) {
525
        /* H.263 could change picture size any time */
526
101k
        h->c.context_reinit = 0;
527
528
101k
        ret = ff_set_dimensions(avctx, h->c.width, h->c.height);
529
101k
        if (ret < 0)
530
1.93k
            return ret;
531
532
99.2k
        ff_set_sar(avctx, avctx->sample_aspect_ratio);
533
534
99.2k
        if ((ret = ff_mpv_common_frame_size_change(s)))
535
0
            return ret;
536
537
99.2k
        if (avctx->pix_fmt != h263_get_format(avctx)) {
538
0
            av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
539
0
            avctx->pix_fmt = AV_PIX_FMT_NONE;
540
0
            return AVERROR_UNKNOWN;
541
0
        }
542
99.2k
    }
543
544
    /* skip B-frames if we don't have reference frames */
545
1.14M
    if (!h->c.last_pic.ptr &&
546
364k
        (h->c.pict_type == AV_PICTURE_TYPE_B || h->c.droppable))
547
29.2k
        return buf_size;
548
1.11M
    if ((avctx->skip_frame >= AVDISCARD_NONREF &&
549
65.4k
         h->c.pict_type == AV_PICTURE_TYPE_B)    ||
550
1.11M
        (avctx->skip_frame >= AVDISCARD_NONKEY &&
551
34.8k
         h->c.pict_type != AV_PICTURE_TYPE_I)    ||
552
1.11M
        avctx->skip_frame >= AVDISCARD_ALL)
553
31.8k
        return buf_size;
554
555
1.08M
    if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
556
27.2k
        return ret;
557
558
1.05M
    if (!h->divx_packed)
559
1.05M
        ff_thread_finish_setup(avctx);
560
561
1.05M
    if (avctx->hwaccel) {
562
0
        ret = FF_HW_CALL(avctx, start_frame, NULL,
563
0
                         h->gb.buffer, get_bits_bytesize(&h->gb, 0));
564
0
        if (ret < 0 )
565
0
            return ret;
566
0
    }
567
568
1.05M
    ff_mpv_er_frame_start_ext(s, h->partitioned_frame,
569
1.05M
                              s->pp_time, s->pb_time);
570
571
    /* the second part of the wmv2 header contains the MB skip bits which
572
     * are stored in current_picture->mb_type which is not available before
573
     * ff_mpv_frame_start() */
574
1.05M
#if CONFIG_WMV2_DECODER
575
1.05M
    if (h->c.msmpeg4_version == MSMP4_WMV2) {
576
101k
        ret = ff_wmv2_decode_secondary_picture_header(h);
577
101k
        if (ret < 0)
578
29.4k
            return ret;
579
72.5k
        if (ret == 1)
580
34.9k
            goto frame_end;
581
72.5k
    }
582
995k
#endif
583
584
    /* decode each macroblock */
585
995k
    h->c.mb_x = 0;
586
995k
    h->c.mb_y = 0;
587
588
995k
    slice_ret = decode_slice(h);
589
2.37M
    while (h->c.mb_y < h->c.mb_height) {
590
2.29M
        if (h->c.msmpeg4_version != MSMP4_UNUSED) {
591
715k
            if (h->slice_height == 0 || h->c.mb_x != 0 || slice_ret < 0 ||
592
411k
                (h->c.mb_y % h->slice_height) != 0 || get_bits_left(&h->gb) < 0)
593
308k
                break;
594
1.57M
        } else {
595
1.57M
            int prev_x = h->c.mb_x, prev_y = h->c.mb_y;
596
1.57M
            if (ff_h263_resync(h) < 0)
597
607k
                break;
598
967k
            if (prev_y * h->c.mb_width + prev_x < h->c.mb_y * h->c.mb_width + h->c.mb_x)
599
184k
                h->c.er.error_occurred = 1;
600
967k
        }
601
602
1.37M
        if (h->c.msmpeg4_version < MSMP4_WMV1 && h->c.h263_pred)
603
502k
            ff_mpeg4_clean_buffers(s);
604
605
1.37M
        if (decode_slice(h) < 0)
606
787k
            slice_ret = AVERROR_INVALIDDATA;
607
1.37M
    }
608
609
995k
    if (h->c.msmpeg4_version != MSMP4_UNUSED && h->c.msmpeg4_version < MSMP4_WMV1 &&
610
244k
        h->c.pict_type == AV_PICTURE_TYPE_I)
611
15.5k
        if (!CONFIG_MSMPEG4DEC ||
612
15.5k
            ff_msmpeg4_decode_ext_header(h, buf_size) < 0)
613
0
            h->c.er.error_status_table[h->c.mb_num - 1] = ER_MB_ERROR;
614
615
1.03M
frame_end:
616
1.03M
    if (!h->c.studio_profile)
617
1.02M
        ff_er_frame_end(&h->c.er, NULL);
618
619
1.03M
    if (avctx->hwaccel) {
620
0
        ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
621
0
        if (ret < 0)
622
0
            return ret;
623
0
    }
624
625
1.03M
    ff_mpv_frame_end(s);
626
627
1.03M
#if CONFIG_MPEG4_DECODER
628
1.03M
    if (avctx->codec_id == AV_CODEC_ID_MPEG4)
629
166k
        ff_mpeg4_frame_end(avctx, avpkt);
630
1.03M
#endif
631
632
1.03M
    av_assert1(h->c.pict_type == h->c.cur_pic.ptr->f->pict_type);
633
1.03M
    if (h->c.pict_type == AV_PICTURE_TYPE_B || h->c.low_delay) {
634
653k
        if ((ret = av_frame_ref(pict, h->c.cur_pic.ptr->f)) < 0)
635
0
            return ret;
636
653k
        ff_print_debug_info(s, h->c.cur_pic.ptr, pict);
637
653k
        ff_mpv_export_qp_table(s, pict, h->c.cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
638
653k
    } else if (h->c.last_pic.ptr) {
639
349k
        if ((ret = av_frame_ref(pict, h->c.last_pic.ptr->f)) < 0)
640
0
            return ret;
641
349k
        ff_print_debug_info(s, h->c.last_pic.ptr, pict);
642
349k
        ff_mpv_export_qp_table(s, pict, h->c.last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
643
349k
    }
644
645
1.03M
    if (h->c.last_pic.ptr || h->c.low_delay) {
646
1.00M
        if (   pict->format == AV_PIX_FMT_YUV420P
647
998k
            && (h->c.codec_tag == AV_RL32("GEOV") || h->c.codec_tag == AV_RL32("GEOX"))) {
648
45.8k
            for (int p = 0; p < 3; p++) {
649
34.4k
                int h = AV_CEIL_RSHIFT(pict->height, !!p);
650
651
34.4k
                pict->data[p]     += (h - 1) * pict->linesize[p];
652
34.4k
                pict->linesize[p] *= -1;
653
34.4k
            }
654
11.4k
        }
655
1.00M
        *got_frame = 1;
656
1.00M
    }
657
658
1.03M
    if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
659
81.5k
        return slice_ret;
660
948k
    else
661
948k
        return buf_size;
662
1.03M
}
663
664
static const AVCodecHWConfigInternal *const h263_hw_config_list[] = {
665
#if CONFIG_H263_VAAPI_HWACCEL
666
    HWACCEL_VAAPI(h263),
667
#endif
668
#if CONFIG_MPEG4_NVDEC_HWACCEL
669
    HWACCEL_NVDEC(mpeg4),
670
#endif
671
#if CONFIG_MPEG4_VDPAU_HWACCEL
672
    HWACCEL_VDPAU(mpeg4),
673
#endif
674
#if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
675
    HWACCEL_VIDEOTOOLBOX(h263),
676
#endif
677
    NULL
678
};
679
680
const FFCodec ff_h263_decoder = {
681
    .p.name         = "h263",
682
    CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
683
    .p.type         = AVMEDIA_TYPE_VIDEO,
684
    .p.id           = AV_CODEC_ID_H263,
685
    .priv_data_size = sizeof(H263DecContext),
686
    .init           = ff_h263_decode_init,
687
    FF_CODEC_DECODE_CB(ff_h263_decode_frame),
688
    .close          = ff_mpv_decode_close,
689
    .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
690
                      AV_CODEC_CAP_DELAY,
691
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
692
                      FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
693
    .flush          = ff_mpeg_flush,
694
    .p.max_lowres   = 3,
695
    .hw_configs     = h263_hw_config_list,
696
};
697
698
const FFCodec ff_h263p_decoder = {
699
    .p.name         = "h263p",
700
    CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
701
    .p.type         = AVMEDIA_TYPE_VIDEO,
702
    .p.id           = AV_CODEC_ID_H263P,
703
    .priv_data_size = sizeof(H263DecContext),
704
    .init           = ff_h263_decode_init,
705
    FF_CODEC_DECODE_CB(ff_h263_decode_frame),
706
    .close          = ff_mpv_decode_close,
707
    .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
708
                      AV_CODEC_CAP_DELAY,
709
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
710
                      FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
711
    .flush          = ff_mpeg_flush,
712
    .p.max_lowres   = 3,
713
    .hw_configs     = h263_hw_config_list,
714
};