FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2026-08-29 16:27:19
Exec Total Coverage
Lines: 249 358 69.6%
Functions: 5 5 100.0%
Branches: 177 321 55.1%

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