FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 250 357 70.0%
Functions: 5 5 100.0%
Branches: 187 332 56.3%

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 1741583 ff_h263_clean_intra_table_entries(s, mb_xy);
264 } else
265 235635 s->mbintra_table[mb_xy] = 1;
266 }
267
268
2/2
✓ Branch 0 taken 1963621 times.
✓ Branch 1 taken 233035 times.
2196656 if (s->pict_type != AV_PICTURE_TYPE_B)
269 1963621 ff_h263_update_motion_val(s);
270
271
2/2
✓ Branch 0 taken 10611 times.
✓ Branch 1 taken 2186045 times.
2196656 if (ret < 0) {
272 10611 const int xy = s->mb_x + s->mb_y * s->mb_stride;
273
1/2
✓ Branch 0 taken 10611 times.
✗ Branch 1 not taken.
10611 if (ret == SLICE_END) {
274 10611 ff_mpv_reconstruct_mb(s, s->block);
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10611 times.
10611 if (s->loop_filter)
276 ff_h263_loop_filter(s);
277
278 10611 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
279 s->mb_x, s->mb_y, ER_MB_END & part_mask);
280
281 10611 s->padding_bug_score--;
282
283
2/2
✓ Branch 0 taken 7658 times.
✓ Branch 1 taken 2953 times.
10611 if (++s->mb_x >= s->mb_width) {
284 7658 s->mb_x = 0;
285 7658 report_decode_progress(s);
286 7658 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
287 7658 s->mb_y++;
288 }
289 10611 return 0;
290 } else if (ret == SLICE_NOEND) {
291 av_log(s->avctx, AV_LOG_ERROR,
292 "Slice mismatch at MB: %d\n", xy);
293 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
294 s->mb_x + 1, s->mb_y,
295 ER_MB_END & part_mask);
296 return AVERROR_INVALIDDATA;
297 }
298 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
299 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
300 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
301
302 if ((s->avctx->err_recognition & AV_EF_IGNORE_ERR) && get_bits_left(&s->gb) > 0)
303 continue;
304 return AVERROR_INVALIDDATA;
305 }
306
307 2186045 ff_mpv_reconstruct_mb(s, s->block);
308
2/2
✓ Branch 0 taken 141300 times.
✓ Branch 1 taken 2044745 times.
2186045 if (s->loop_filter)
309 141300 ff_h263_loop_filter(s);
310 }
311
312 83358 report_decode_progress(s);
313 83358 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
314
315 83358 s->mb_x = 0;
316 }
317
318 av_assert1(s->mb_x == 0 && s->mb_y == s->mb_height);
319
320 // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
322 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
323 get_bits_left(&s->gb) >= 48 &&
324 show_bits(&s->gb, 24) == 0x4010 &&
325 !s->data_partitioning)
326 s->padding_bug_score += 32;
327
328 /* try to detect the padding bug */
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
330 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
331 get_bits_left(&s->gb) >= 0 &&
332 get_bits_left(&s->gb) < 137 &&
333 !s->data_partitioning) {
334 const int bits_count = get_bits_count(&s->gb);
335 const int bits_left = s->gb.size_in_bits - bits_count;
336
337 if (bits_left == 0) {
338 s->padding_bug_score += 16;
339 } else if (bits_left != 1) {
340 int v = show_bits(&s->gb, 8);
341 v |= 0x7F >> (7 - (bits_count & 7));
342
343 if (v == 0x7F && bits_left <= 8)
344 s->padding_bug_score--;
345 else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
346 bits_left <= 16)
347 s->padding_bug_score += 4;
348 else
349 s->padding_bug_score++;
350 }
351 }
352
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_H263 &&
354 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
355 get_bits_left(&s->gb) >= 8 &&
356 get_bits_left(&s->gb) < 300 &&
357 s->pict_type == AV_PICTURE_TYPE_I &&
358 show_bits(&s->gb, 8) == 0 &&
359 !s->data_partitioning) {
360
361 s->padding_bug_score += 32;
362 }
363
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_H263 &&
365 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
366 get_bits_left(&s->gb) >= 64 &&
367 AV_RB64(s->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) {
368
369 s->padding_bug_score += 32;
370 }
371
372
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
373 1346 if (
374
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))
375 1346 s->workaround_bugs |= FF_BUG_NO_PADDING;
376 else
377 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
378 }
379
380 // handle formats which don't have unique end markers
381
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
382 1346 int left = get_bits_left(&s->gb);
383 1346 int max_extra = 7;
384
385 /* no markers in M$ crap */
386
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)
387 90 max_extra += 17;
388
389 /* buggy padding but the frame should still end approximately at
390 * the bitstream end */
391
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 (s->avctx->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
393 max_extra += 48;
394
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
395 1346 max_extra += 256 * 256 * 256 * 64;
396
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (left > max_extra)
398 av_log(s->avctx, AV_LOG_ERROR,
399 "discarding %d junk bits at end, next would be %X\n",
400 left, show_bits(&s->gb, 24));
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 else if (left < 0)
402 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
403 else
404 1346 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
405 1346 s->mb_x - 1, s->mb_y, ER_MB_END);
406
407 1346 return 0;
408 }
409
410 av_log(s->avctx, AV_LOG_ERROR,
411 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
412 get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
413
414 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
415 ER_MB_END & part_mask);
416
417 return AVERROR_INVALIDDATA;
418 }
419
420 5760 int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict,
421 int *got_frame, AVPacket *avpkt)
422 {
423 5760 const uint8_t *buf = avpkt->data;
424 5760 int buf_size = avpkt->size;
425 5760 MpegEncContext *s = avctx->priv_data;
426 int ret;
427 5760 int slice_ret = 0;
428 int bak_width, bak_height;
429
430 /* no supplementary picture */
431
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 5651 times.
5760 if (buf_size == 0) {
432 /* special case for last picture */
433
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) {
434
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if ((ret = av_frame_ref(pict, s->next_pic.ptr->f)) < 0)
435 return ret;
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (s->skipped_last_frame) {
437 /* If the stream ended with an NVOP, we output the last frame
438 * in display order, but with the props from the last input
439 * packet so that the stream's end time is correct. */
440 ret = ff_decode_frame_props(avctx, pict);
441 if (ret < 0)
442 return ret;
443 }
444
445 23 ff_mpv_unref_picture(&s->next_pic);
446
447 23 *got_frame = 1;
448 }
449
450 109 return 0;
451 }
452
453 // s->gb might be overridden in ff_mpeg4_decode_picture_header() below.
454 5651 ret = init_get_bits8(&s->gb, buf, buf_size);
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5651 times.
5651 if (ret < 0)
456 return ret;
457
458 5651 bak_width = s->width;
459 5651 bak_height = s->height;
460
461 /* let's go :-) */
462
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4977 times.
5651 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == MSMP4_WMV2) {
463 674 ret = ff_wmv2_decode_picture_header(s);
464 #if CONFIG_MSMPEG4DEC
465
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 4302 times.
4977 } else if (s->msmpeg4_version != MSMP4_UNUSED) {
466 675 ret = ff_msmpeg4_decode_picture_header(s);
467 #endif
468
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) {
469 3526 ret = ff_mpeg4_decode_picture_header(s);
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 776 times.
776 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
471 ret = ff_intel_h263_decode_picture_header(s);
472
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 461 times.
776 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
473 315 ret = ff_flv_decode_picture_header(s);
474 } else {
475 461 ret = ff_h263_decode_picture_header(s);
476 }
477
478
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) {
479 if ( s->width != bak_width
480 || s->height != bak_height) {
481 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
482 s->width = bak_width;
483 s->height= bak_height;
484
485 }
486 }
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5651 times.
5651 if (ret == FRAME_SKIPPED)
488 return buf_size;
489
490 /* skip if the header was thrashed */
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5651 times.
5651 if (ret < 0) {
492 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
493 return ret;
494 }
495
496
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 5458 times.
5651 if (!s->context_initialized) {
497 193 avctx->pix_fmt = h263_get_format(avctx);
498
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 193 times.
193 if ((ret = ff_mpv_common_init(s)) < 0)
499 return ret;
500 }
501
502 5651 avctx->has_b_frames = !s->low_delay;
503
504
2/2
✓ Branch 0 taken 3526 times.
✓ Branch 1 taken 2125 times.
5651 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
505
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))
506 return AVERROR_INVALIDDATA;
507 3526 ff_mpeg4_workaround_bugs(avctx);
508
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3525 times.
3526 if (s->studio_profile != (s->idsp.idct == NULL))
509 1 ff_mpv_idct_init(s);
510 }
511
512 /* After H.263 & MPEG-4 header decode we have the height, width,
513 * and other parameters. So then we could init the picture.
514 * FIXME: By the way H.263 decoder is evolving it should have
515 * an H263EncContext */
516
2/2
✓ Branch 0 taken 5605 times.
✓ Branch 1 taken 46 times.
5651 if (s->width != avctx->coded_width ||
517
1/2
✓ Branch 0 taken 5605 times.
✗ Branch 1 not taken.
5605 s->height != avctx->coded_height ||
518
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5604 times.
5605 s->context_reinit) {
519 /* H.263 could change picture size any time */
520 47 s->context_reinit = 0;
521
522 47 ret = ff_set_dimensions(avctx, s->width, s->height);
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (ret < 0)
524 return ret;
525
526 47 ff_set_sar(avctx, avctx->sample_aspect_ratio);
527
528
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if ((ret = ff_mpv_common_frame_size_change(s)))
529 return ret;
530
531
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if (avctx->pix_fmt != h263_get_format(avctx)) {
532 av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
533 avctx->pix_fmt = AV_PIX_FMT_NONE;
534 return AVERROR_UNKNOWN;
535 }
536 }
537
538 /* skip B-frames if we don't have reference frames */
539
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 5282 times.
5651 if (!s->last_pic.ptr &&
540
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))
541 return buf_size;
542
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 5477 times.
5651 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
543
1/2
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
174 s->pict_type == AV_PICTURE_TYPE_B) ||
544
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 5477 times.
5651 (avctx->skip_frame >= AVDISCARD_NONKEY &&
545
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 47 times.
174 s->pict_type != AV_PICTURE_TYPE_I) ||
546
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 5480 times.
5604 avctx->skip_frame >= AVDISCARD_ALL)
547 171 return buf_size;
548
549
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5480 times.
5480 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
550 return ret;
551
552
2/2
✓ Branch 0 taken 5465 times.
✓ Branch 1 taken 15 times.
5480 if (!s->divx_packed)
553 5465 ff_thread_finish_setup(avctx);
554
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5480 times.
5480 if (avctx->hwaccel) {
556 ret = FF_HW_CALL(avctx, start_frame, NULL,
557 s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
558 if (ret < 0 )
559 return ret;
560 }
561
562 5480 ff_mpeg_er_frame_start(s);
563
564 /* the second part of the wmv2 header contains the MB skip bits which
565 * are stored in current_picture->mb_type which is not available before
566 * ff_mpv_frame_start() */
567 #if CONFIG_WMV2_DECODER
568
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4806 times.
5480 if (s->msmpeg4_version == MSMP4_WMV2) {
569 674 ret = ff_wmv2_decode_secondary_picture_header(s);
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
674 if (ret < 0)
571 return ret;
572
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 671 times.
674 if (ret == 1)
573 3 goto frame_end;
574 }
575 #endif
576
577 /* decode each macroblock */
578 5477 s->mb_x = 0;
579 5477 s->mb_y = 0;
580
581 5477 slice_ret = decode_slice(s);
582
2/2
✓ Branch 0 taken 6480 times.
✓ Branch 1 taken 5477 times.
11957 while (s->mb_y < s->mb_height) {
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6480 times.
6480 if (s->msmpeg4_version != MSMP4_UNUSED) {
584 if (s->slice_height == 0 || s->mb_x != 0 || slice_ret < 0 ||
585 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
586 break;
587 } else {
588 6480 int prev_x = s->mb_x, prev_y = s->mb_y;
589
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6480 times.
6480 if (ff_h263_resync(s) < 0)
590 break;
591
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)
592 29 s->er.error_occurred = 1;
593 }
594
595
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)
596 3936 ff_mpeg4_clean_buffers(s);
597
598
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6480 times.
6480 if (decode_slice(s) < 0)
599 slice_ret = AVERROR_INVALIDDATA;
600 }
601
602
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 &&
603
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 44 times.
475 s->pict_type == AV_PICTURE_TYPE_I)
604
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!CONFIG_MSMPEG4DEC ||
605 44 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
606 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
607
608 5477 frame_end:
609
2/2
✓ Branch 0 taken 5479 times.
✓ Branch 1 taken 1 times.
5480 if (!s->studio_profile)
610 5479 ff_er_frame_end(&s->er, NULL);
611
612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5480 times.
5480 if (avctx->hwaccel) {
613 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
614 if (ret < 0)
615 return ret;
616 }
617
618 5480 ff_mpv_frame_end(s);
619
620
2/2
✓ Branch 0 taken 3378 times.
✓ Branch 1 taken 2102 times.
5480 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
621 3378 ff_mpeg4_frame_end(avctx, avpkt);
622
623 av_assert1(s->pict_type == s->cur_pic.ptr->f->pict_type);
624
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) {
625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5028 times.
5028 if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
626 return ret;
627 5028 ff_print_debug_info(s, s->cur_pic.ptr, pict);
628 5028 ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
629
2/2
✓ Branch 0 taken 426 times.
✓ Branch 1 taken 26 times.
452 } else if (s->last_pic.ptr) {
630
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 426 times.
426 if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0)
631 return ret;
632 426 ff_print_debug_info(s, s->last_pic.ptr, pict);
633 426 ff_mpv_export_qp_table(s, pict, s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
634 }
635
636
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) {
637
2/2
✓ Branch 0 taken 5453 times.
✓ Branch 1 taken 1 times.
5454 if ( pict->format == AV_PIX_FMT_YUV420P
638
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"))) {
639 for (int p = 0; p < 3; p++) {
640 int h = AV_CEIL_RSHIFT(pict->height, !!p);
641
642 pict->data[p] += (h - 1) * pict->linesize[p];
643 pict->linesize[p] *= -1;
644 }
645 }
646 5454 *got_frame = 1;
647 }
648
649
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))
650 return slice_ret;
651 else
652 5480 return buf_size;
653 }
654
655 static const AVCodecHWConfigInternal *const h263_hw_config_list[] = {
656 #if CONFIG_H263_VAAPI_HWACCEL
657 HWACCEL_VAAPI(h263),
658 #endif
659 #if CONFIG_MPEG4_NVDEC_HWACCEL
660 HWACCEL_NVDEC(mpeg4),
661 #endif
662 #if CONFIG_MPEG4_VDPAU_HWACCEL
663 HWACCEL_VDPAU(mpeg4),
664 #endif
665 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
666 HWACCEL_VIDEOTOOLBOX(h263),
667 #endif
668 NULL
669 };
670
671 const FFCodec ff_h263_decoder = {
672 .p.name = "h263",
673 CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
674 .p.type = AVMEDIA_TYPE_VIDEO,
675 .p.id = AV_CODEC_ID_H263,
676 .priv_data_size = sizeof(MpegEncContext),
677 .init = ff_h263_decode_init,
678 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
679 .close = ff_mpv_decode_close,
680 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
681 AV_CODEC_CAP_DELAY,
682 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
683 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
684 .flush = ff_mpeg_flush,
685 .p.max_lowres = 3,
686 .hw_configs = h263_hw_config_list,
687 };
688
689 const FFCodec ff_h263p_decoder = {
690 .p.name = "h263p",
691 CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
692 .p.type = AVMEDIA_TYPE_VIDEO,
693 .p.id = AV_CODEC_ID_H263P,
694 .priv_data_size = sizeof(MpegEncContext),
695 .init = ff_h263_decode_init,
696 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
697 .close = ff_mpv_decode_close,
698 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
699 AV_CODEC_CAP_DELAY,
700 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
701 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
702 .flush = ff_mpeg_flush,
703 .p.max_lowres = 3,
704 .hw_configs = h263_hw_config_list,
705 };
706