FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 264 380 69.5%
Functions: 5 5 100.0%
Branches: 206 370 55.7%

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 /**
174 * Return the number of bytes consumed for building the current frame.
175 */
176 5661 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
177 {
178 5661 int pos = (get_bits_count(&s->gb) + 7) >> 3;
179
180
3/4
✓ Branch 0 taken 5644 times.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5644 times.
5661 if (s->divx_packed || s->avctx->hwaccel) {
181 /* We would have to scan through the whole buf to handle the weird
182 * reordering ... */
183 17 return buf_size;
184 } else {
185 // avoid infinite loops (maybe not needed...)
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5644 times.
5644 if (pos == 0)
187 pos = 1;
188 // oops ;)
189
2/2
✓ Branch 0 taken 5474 times.
✓ Branch 1 taken 170 times.
5644 if (pos + 10 > buf_size)
190 5474 pos = buf_size;
191
192 5644 return pos;
193 }
194 }
195
196 11966 static int decode_slice(MpegEncContext *s)
197 {
198 23932 const int part_mask = s->partitioned_frame
199
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 10238 times.
11966 ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
200 11966 const int mb_size = 16 >> s->avctx->lowres;
201 int ret;
202
203 11966 s->last_resync_gb = s->gb;
204 11966 s->first_slice_line = 1;
205 11966 s->resync_mb_x = s->mb_x;
206 11966 s->resync_mb_y = s->mb_y;
207
208 11966 ff_set_qscale(s, s->qscale);
209
210
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 11936 times.
11966 if (s->studio_profile) {
211
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)
212 return ret;
213 }
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11966 times.
11966 if (s->avctx->hwaccel) {
216 const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
217 ret = FF_HW_CALL(s->avctx, decode_slice, start, s->gb.buffer_end - start);
218 // ensure we exit decode loop
219 s->mb_y = s->mb_height;
220 return ret;
221 }
222
223
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 10238 times.
11966 if (s->partitioned_frame) {
224 1728 const int qscale = s->qscale;
225
226
1/2
✓ Branch 0 taken 1728 times.
✗ Branch 1 not taken.
1728 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
227
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1728 times.
1728 if ((ret = ff_mpeg4_decode_partitions(s->avctx->priv_data)) < 0)
228 return ret;
229
230 /* restore variables which were modified */
231 1728 s->first_slice_line = 1;
232 1728 s->mb_x = s->resync_mb_x;
233 1728 s->mb_y = s->resync_mb_y;
234 1728 ff_set_qscale(s, qscale);
235 }
236
237
2/2
✓ Branch 0 taken 94131 times.
✓ Branch 1 taken 1346 times.
95477 for (; s->mb_y < s->mb_height; s->mb_y++) {
238 /* per-row end of slice checks */
239
2/2
✓ Branch 0 taken 19665 times.
✓ Branch 1 taken 74466 times.
94131 if (s->msmpeg4_version != MSMP4_UNUSED) {
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19665 times.
19665 if (s->resync_mb_y + s->slice_height == s->mb_y) {
241 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
242 s->mb_x - 1, s->mb_y, ER_MB_END);
243
244 return 0;
245 }
246 }
247
248
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 93381 times.
94131 if (s->msmpeg4_version == MSMP4_V1) {
249 750 s->last_dc[0] =
250 750 s->last_dc[1] =
251 750 s->last_dc[2] = 128;
252 }
253
254 94131 ff_init_block_index(s);
255
2/2
✓ Branch 0 taken 2200220 times.
✓ Branch 1 taken 83511 times.
2283731 for (; s->mb_x < s->mb_width; s->mb_x++) {
256 int ret;
257
258 2200220 ff_update_block_index(s, s->avctx->bits_per_raw_sample,
259 2200220 s->avctx->lowres, s->chroma_x_shift);
260
261
4/4
✓ Branch 0 taken 93314 times.
✓ Branch 1 taken 2106906 times.
✓ Branch 2 taken 7367 times.
✓ Branch 3 taken 85947 times.
2200220 if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
262 7367 s->first_slice_line = 0;
263
264 /* DCT & quantize */
265
266 2200220 s->mv_dir = MV_DIR_FORWARD;
267 2200220 s->mv_type = MV_TYPE_16X16;
268 ff_dlog(s, "%d %06X\n",
269 get_bits_count(&s->gb), show_bits(&s->gb, 24));
270
271 ff_tlog(NULL, "Decoding MB at %dx%d\n", s->mb_x, s->mb_y);
272 2200220 ret = s->decode_mb(s, s->block);
273
274
2/2
✓ Branch 0 taken 1953892 times.
✓ Branch 1 taken 246328 times.
2200220 if (s->pict_type != AV_PICTURE_TYPE_B)
275 1953892 ff_h263_update_motion_val(s);
276
277
2/2
✓ Branch 0 taken 10620 times.
✓ Branch 1 taken 2189600 times.
2200220 if (ret < 0) {
278 10620 const int xy = s->mb_x + s->mb_y * s->mb_stride;
279
1/2
✓ Branch 0 taken 10620 times.
✗ Branch 1 not taken.
10620 if (ret == SLICE_END) {
280 10620 ff_mpv_reconstruct_mb(s, s->block);
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10620 times.
10620 if (s->loop_filter)
282 ff_h263_loop_filter(s);
283
284 10620 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
285 s->mb_x, s->mb_y, ER_MB_END & part_mask);
286
287 10620 s->padding_bug_score--;
288
289
2/2
✓ Branch 0 taken 7667 times.
✓ Branch 1 taken 2953 times.
10620 if (++s->mb_x >= s->mb_width) {
290 7667 s->mb_x = 0;
291 7667 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
292 7667 ff_mpv_report_decode_progress(s);
293 7667 s->mb_y++;
294 }
295 10620 return 0;
296 } else if (ret == SLICE_NOEND) {
297 av_log(s->avctx, AV_LOG_ERROR,
298 "Slice mismatch at MB: %d\n", xy);
299 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
300 s->mb_x + 1, s->mb_y,
301 ER_MB_END & part_mask);
302 return AVERROR_INVALIDDATA;
303 }
304 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
305 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
306 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
307
308 if ((s->avctx->err_recognition & AV_EF_IGNORE_ERR) && get_bits_left(&s->gb) > 0)
309 continue;
310 return AVERROR_INVALIDDATA;
311 }
312
313 2189600 ff_mpv_reconstruct_mb(s, s->block);
314
2/2
✓ Branch 0 taken 141300 times.
✓ Branch 1 taken 2048300 times.
2189600 if (s->loop_filter)
315 141300 ff_h263_loop_filter(s);
316 }
317
318 83511 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
319 83511 ff_mpv_report_decode_progress(s);
320
321 83511 s->mb_x = 0;
322 }
323
324 av_assert1(s->mb_x == 0 && s->mb_y == s->mb_height);
325
326 // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
328 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
329 get_bits_left(&s->gb) >= 48 &&
330 show_bits(&s->gb, 24) == 0x4010 &&
331 !s->data_partitioning)
332 s->padding_bug_score += 32;
333
334 /* try to detect the padding bug */
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
336 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
337 get_bits_left(&s->gb) >= 0 &&
338 get_bits_left(&s->gb) < 137 &&
339 !s->data_partitioning) {
340 const int bits_count = get_bits_count(&s->gb);
341 const int bits_left = s->gb.size_in_bits - bits_count;
342
343 if (bits_left == 0) {
344 s->padding_bug_score += 16;
345 } else if (bits_left != 1) {
346 int v = show_bits(&s->gb, 8);
347 v |= 0x7F >> (7 - (bits_count & 7));
348
349 if (v == 0x7F && bits_left <= 8)
350 s->padding_bug_score--;
351 else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
352 bits_left <= 16)
353 s->padding_bug_score += 4;
354 else
355 s->padding_bug_score++;
356 }
357 }
358
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_H263 &&
360 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
361 get_bits_left(&s->gb) >= 8 &&
362 get_bits_left(&s->gb) < 300 &&
363 s->pict_type == AV_PICTURE_TYPE_I &&
364 show_bits(&s->gb, 8) == 0 &&
365 !s->data_partitioning) {
366
367 s->padding_bug_score += 32;
368 }
369
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_H263 &&
371 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
372 get_bits_left(&s->gb) >= 64 &&
373 AV_RB64(s->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) {
374
375 s->padding_bug_score += 32;
376 }
377
378
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
379 1346 if (
380
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))
381 1346 s->workaround_bugs |= FF_BUG_NO_PADDING;
382 else
383 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
384 }
385
386 // handle formats which don't have unique end markers
387
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
388 1346 int left = get_bits_left(&s->gb);
389 1346 int max_extra = 7;
390
391 /* no markers in M$ crap */
392
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)
393 90 max_extra += 17;
394
395 /* buggy padding but the frame should still end approximately at
396 * the bitstream end */
397
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 (s->avctx->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
399 max_extra += 48;
400
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
401 1346 max_extra += 256 * 256 * 256 * 64;
402
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (left > max_extra)
404 av_log(s->avctx, AV_LOG_ERROR,
405 "discarding %d junk bits at end, next would be %X\n",
406 left, show_bits(&s->gb, 24));
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 else if (left < 0)
408 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
409 else
410 1346 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
411 1346 s->mb_x - 1, s->mb_y, ER_MB_END);
412
413 1346 return 0;
414 }
415
416 av_log(s->avctx, AV_LOG_ERROR,
417 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
418 get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
419
420 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
421 ER_MB_END & part_mask);
422
423 return AVERROR_INVALIDDATA;
424 }
425
426 5770 int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict,
427 int *got_frame, AVPacket *avpkt)
428 {
429 5770 const uint8_t *buf = avpkt->data;
430 5770 int buf_size = avpkt->size;
431 5770 MpegEncContext *s = avctx->priv_data;
432 int ret;
433 5770 int slice_ret = 0;
434
435 /* no supplementary picture */
436
2/2
✓ Branch 0 taken 5661 times.
✓ Branch 1 taken 109 times.
5770 if (buf_size == 0) {
437 /* special case for last picture */
438
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) {
439
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if ((ret = av_frame_ref(pict, s->next_pic.ptr->f)) < 0)
440 return ret;
441 23 ff_mpv_unref_picture(&s->next_pic);
442
443 23 *got_frame = 1;
444
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) {
445 /* Output the last picture we decoded again if the stream ended with
446 * an NVOP */
447 if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
448 return ret;
449 /* Copy props from the last input packet. Otherwise, props from the last
450 * returned picture would be reused */
451 if ((ret = ff_decode_frame_props(avctx, pict)) < 0)
452 return ret;
453 ff_mpv_unref_picture(&s->cur_pic);
454
455 *got_frame = 1;
456 }
457
458 109 return 0;
459 }
460
461 5661 retry:
462
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 5647 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 11 times.
5665 if (s->divx_packed && s->bitstream_buffer_size) {
463 int i;
464
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 for(i=0; i < buf_size-3; i++) {
465
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) {
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (buf[i+3]==0xB0) {
467 av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
468 s->bitstream_buffer_size = 0;
469 }
470 7 break;
471 }
472 }
473 }
474
475
3/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5658 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5665 if (s->bitstream_buffer_size && (s->divx_packed || buf_size <= MAX_NVOP_SIZE)) // divx 5.01+/xvid frame reorder
476 7 ret = init_get_bits8(&s->gb, s->bitstream_buffer,
477 s->bitstream_buffer_size);
478 else
479 5658 ret = init_get_bits8(&s->gb, buf, buf_size);
480
481 5665 s->bitstream_buffer_size = 0;
482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5665 times.
5665 if (ret < 0)
483 return ret;
484
485 /* let's go :-) */
486
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4991 times.
5665 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == MSMP4_WMV2) {
487 674 ret = ff_wmv2_decode_picture_header(s);
488 #if CONFIG_MSMPEG4DEC
489
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 4316 times.
4991 } else if (s->msmpeg4_version != MSMP4_UNUSED) {
490 675 ret = ff_msmpeg4_decode_picture_header(s);
491 #endif
492
2/2
✓ Branch 0 taken 3540 times.
✓ Branch 1 taken 776 times.
4316 } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
493 3540 ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb, 0, 0);
494 3540 s->skipped_last_frame = (ret == FRAME_SKIPPED);
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 776 times.
776 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
496 ret = ff_intel_h263_decode_picture_header(s);
497
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 461 times.
776 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
498 315 ret = ff_flv_decode_picture_header(s);
499 } else {
500 461 ret = ff_h263_decode_picture_header(s);
501 }
502
503
2/4
✓ Branch 0 taken 5665 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5665 times.
5665 if (ret < 0 || ret == FRAME_SKIPPED) {
504 if ( s->width != avctx->coded_width
505 || s->height != avctx->coded_height) {
506 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
507 s->width = avctx->coded_width;
508 s->height= avctx->coded_height;
509 }
510 }
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5665 times.
5665 if (ret == FRAME_SKIPPED)
512 return get_consumed_bytes(s, buf_size);
513
514 /* skip if the header was thrashed */
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5665 times.
5665 if (ret < 0) {
516 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
517 return ret;
518 }
519
520
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 5470 times.
5665 if (!s->context_initialized) {
521 195 avctx->pix_fmt = h263_get_format(avctx);
522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 195 times.
195 if ((ret = ff_mpv_common_init(s)) < 0)
523 return ret;
524 }
525
526 5665 avctx->has_b_frames = !s->low_delay;
527
528
2/2
✓ Branch 0 taken 3540 times.
✓ Branch 1 taken 2125 times.
5665 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
529
3/4
✓ Branch 0 taken 2842 times.
✓ Branch 1 taken 698 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2842 times.
3540 if (s->pict_type != AV_PICTURE_TYPE_B && s->mb_num/2 > get_bits_left(&s->gb))
530 return AVERROR_INVALIDDATA;
531
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3536 times.
3540 if (ff_mpeg4_workaround_bugs(avctx) == 1)
532 4 goto retry;
533
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3535 times.
3536 if (s->studio_profile != (s->idsp.idct == NULL))
534 1 ff_mpv_idct_init(s);
535
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 3492 times.
3536 if (s->mpeg_quant) {
536 44 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
537 } else {
538 3492 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
539 }
540 }
541
542 /* After H.263 & MPEG-4 header decode we have the height, width,
543 * and other parameters. So then we could init the picture.
544 * FIXME: By the way H.263 decoder is evolving it should have
545 * an H263EncContext */
546
2/2
✓ Branch 0 taken 5615 times.
✓ Branch 1 taken 46 times.
5661 if (s->width != avctx->coded_width ||
547
1/2
✓ Branch 0 taken 5615 times.
✗ Branch 1 not taken.
5615 s->height != avctx->coded_height ||
548
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5614 times.
5615 s->context_reinit) {
549 /* H.263 could change picture size any time */
550 47 s->context_reinit = 0;
551
552 47 ret = ff_set_dimensions(avctx, s->width, s->height);
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (ret < 0)
554 return ret;
555
556 47 ff_set_sar(avctx, avctx->sample_aspect_ratio);
557
558
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if ((ret = ff_mpv_common_frame_size_change(s)))
559 return ret;
560
561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if (avctx->pix_fmt != h263_get_format(avctx)) {
562 av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
563 avctx->pix_fmt = AV_PIX_FMT_NONE;
564 return AVERROR_UNKNOWN;
565 }
566 }
567
568
2/2
✓ Branch 0 taken 5200 times.
✓ Branch 1 taken 461 times.
5661 if (s->codec_id == AV_CODEC_ID_H263 ||
569
1/2
✓ Branch 0 taken 5200 times.
✗ Branch 1 not taken.
5200 s->codec_id == AV_CODEC_ID_H263P ||
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5200 times.
5200 s->codec_id == AV_CODEC_ID_H263I)
571
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);
572
573 /* skip B-frames if we don't have reference frames */
574
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 5289 times.
5661 if (!s->last_pic.ptr &&
575
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))
576 return get_consumed_bytes(s, buf_size);
577
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 5486 times.
5661 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
578
1/2
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
175 s->pict_type == AV_PICTURE_TYPE_B) ||
579
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 5486 times.
5661 (avctx->skip_frame >= AVDISCARD_NONKEY &&
580
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 47 times.
175 s->pict_type != AV_PICTURE_TYPE_I) ||
581
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 5489 times.
5614 avctx->skip_frame >= AVDISCARD_ALL)
582 172 return get_consumed_bytes(s, buf_size);
583
584
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5489 times.
5489 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
585 return ret;
586
587
3/4
✓ Branch 0 taken 5474 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 5474 times.
✗ Branch 3 not taken.
5489 if (!s->divx_packed && !avctx->hwaccel)
588 5474 ff_thread_finish_setup(avctx);
589
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5489 times.
5489 if (avctx->hwaccel) {
591 ret = FF_HW_CALL(avctx, start_frame,
592 s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
593 if (ret < 0 )
594 return ret;
595 }
596
597 5489 ff_mpeg_er_frame_start(s);
598
599 /* the second part of the wmv2 header contains the MB skip bits which
600 * are stored in current_picture->mb_type which is not available before
601 * ff_mpv_frame_start() */
602 #if CONFIG_WMV2_DECODER
603
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4815 times.
5489 if (s->msmpeg4_version == MSMP4_WMV2) {
604 674 ret = ff_wmv2_decode_secondary_picture_header(s);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
674 if (ret < 0)
606 return ret;
607
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 671 times.
674 if (ret == 1)
608 3 goto frame_end;
609 }
610 #endif
611
612 /* decode each macroblock */
613 5486 s->mb_x = 0;
614 5486 s->mb_y = 0;
615
616 5486 slice_ret = decode_slice(s);
617
2/2
✓ Branch 0 taken 6480 times.
✓ Branch 1 taken 5486 times.
11966 while (s->mb_y < s->mb_height) {
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6480 times.
6480 if (s->msmpeg4_version != MSMP4_UNUSED) {
619 if (s->slice_height == 0 || s->mb_x != 0 || slice_ret < 0 ||
620 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
621 break;
622 } else {
623 6480 int prev_x = s->mb_x, prev_y = s->mb_y;
624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6480 times.
6480 if (ff_h263_resync(s) < 0)
625 break;
626
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)
627 29 s->er.error_occurred = 1;
628 }
629
630
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)
631 3936 ff_mpeg4_clean_buffers(s);
632
633
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6480 times.
6480 if (decode_slice(s) < 0)
634 slice_ret = AVERROR_INVALIDDATA;
635 }
636
637
4/4
✓ Branch 0 taken 4140 times.
✓ Branch 1 taken 1346 times.
✓ Branch 2 taken 871 times.
✓ Branch 3 taken 475 times.
5486 if (s->msmpeg4_version != MSMP4_UNUSED && s->msmpeg4_version < MSMP4_WMV1 &&
638
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 44 times.
475 s->pict_type == AV_PICTURE_TYPE_I)
639
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!CONFIG_MSMPEG4DEC ||
640 44 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
641 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
642
643 av_assert1(s->bitstream_buffer_size == 0);
644 5486 frame_end:
645
2/2
✓ Branch 0 taken 5488 times.
✓ Branch 1 taken 1 times.
5489 if (!s->studio_profile)
646 5488 ff_er_frame_end(&s->er, NULL);
647
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5489 times.
5489 if (avctx->hwaccel) {
649 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
650 if (ret < 0)
651 return ret;
652 }
653
654 5489 ff_mpv_frame_end(s);
655
656
2/2
✓ Branch 0 taken 3387 times.
✓ Branch 1 taken 2102 times.
5489 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
657 3387 ff_mpeg4_frame_end(avctx, buf, buf_size);
658
659
3/4
✓ Branch 0 taken 5474 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5474 times.
5489 if (!s->divx_packed && avctx->hwaccel)
660 ff_thread_finish_setup(avctx);
661
662 av_assert1(s->pict_type == s->cur_pic.ptr->f->pict_type);
663
4/4
✓ Branch 0 taken 4791 times.
✓ Branch 1 taken 698 times.
✓ Branch 2 taken 4388 times.
✓ Branch 3 taken 403 times.
5489 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
664
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5086 times.
5086 if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
665 return ret;
666 5086 ff_print_debug_info(s, s->cur_pic.ptr, pict);
667 5086 ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
668
2/2
✓ Branch 0 taken 376 times.
✓ Branch 1 taken 27 times.
403 } else if (s->last_pic.ptr) {
669
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 376 times.
376 if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0)
670 return ret;
671 376 ff_print_debug_info(s, s->last_pic.ptr, pict);
672 376 ff_mpv_export_qp_table(s, pict, s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
673 }
674
675
4/4
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 5371 times.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 27 times.
5489 if (s->last_pic.ptr || s->low_delay) {
676
2/2
✓ Branch 0 taken 5461 times.
✓ Branch 1 taken 1 times.
5462 if ( pict->format == AV_PIX_FMT_YUV420P
677
2/4
✓ Branch 0 taken 5461 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5461 times.
5461 && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) {
678 for (int p = 0; p < 3; p++) {
679 int h = AV_CEIL_RSHIFT(pict->height, !!p);
680
681 pict->data[p] += (h - 1) * pict->linesize[p];
682 pict->linesize[p] *= -1;
683 }
684 }
685 5462 *got_frame = 1;
686 }
687
688
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5489 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5489 if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
689 return slice_ret;
690 else
691 5489 return get_consumed_bytes(s, buf_size);
692 }
693
694 static const AVCodecHWConfigInternal *const h263_hw_config_list[] = {
695 #if CONFIG_H263_VAAPI_HWACCEL
696 HWACCEL_VAAPI(h263),
697 #endif
698 #if CONFIG_MPEG4_NVDEC_HWACCEL
699 HWACCEL_NVDEC(mpeg4),
700 #endif
701 #if CONFIG_MPEG4_VDPAU_HWACCEL
702 HWACCEL_VDPAU(mpeg4),
703 #endif
704 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
705 HWACCEL_VIDEOTOOLBOX(h263),
706 #endif
707 NULL
708 };
709
710 const FFCodec ff_h263_decoder = {
711 .p.name = "h263",
712 CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
713 .p.type = AVMEDIA_TYPE_VIDEO,
714 .p.id = AV_CODEC_ID_H263,
715 .priv_data_size = sizeof(MpegEncContext),
716 .init = ff_h263_decode_init,
717 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
718 .close = ff_mpv_decode_close,
719 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
720 AV_CODEC_CAP_DELAY,
721 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
722 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
723 .flush = ff_mpeg_flush,
724 .p.max_lowres = 3,
725 .hw_configs = h263_hw_config_list,
726 };
727
728 const FFCodec ff_h263p_decoder = {
729 .p.name = "h263p",
730 CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
731 .p.type = AVMEDIA_TYPE_VIDEO,
732 .p.id = AV_CODEC_ID_H263P,
733 .priv_data_size = sizeof(MpegEncContext),
734 .init = ff_h263_decode_init,
735 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
736 .close = ff_mpv_decode_close,
737 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
738 AV_CODEC_CAP_DELAY,
739 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
740 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
741 .flush = ff_mpeg_flush,
742 .p.max_lowres = 3,
743 .hw_configs = h263_hw_config_list,
744 };
745