FFmpeg coverage


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