FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 251 358 70.1%
Functions: 5 5 100.0%
Branches: 189 334 56.6%

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