FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 255 367 69.5%
Functions: 4 4 100.0%
Branches: 192 344 55.8%

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