FFmpeg coverage


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