FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 279 405 68.9%
Branches: 213 381 55.9%

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 "error_resilience.h"
35 #include "flvdec.h"
36 #include "h263.h"
37 #include "h263dec.h"
38 #if FF_API_FLAG_TRUNCATED
39 #include "h263_parser.h"
40 #endif
41 #include "hwconfig.h"
42 #include "internal.h"
43 #include "mpeg_er.h"
44 #include "mpeg4video.h"
45 #include "mpeg4videodec.h"
46 #if FF_API_FLAG_TRUNCATED
47 #include "mpeg4video_parser.h"
48 #endif
49 #include "mpegutils.h"
50 #include "mpegvideo.h"
51 #include "mpegvideodec.h"
52 #include "msmpeg4dec.h"
53 #include "qpeldsp.h"
54 #include "thread.h"
55 #include "wmv2dec.h"
56
57 335 static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
58 {
59 /* MPEG-4 Studio Profile only, not supported by hardware */
60
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 333 times.
335 if (avctx->bits_per_raw_sample > 8) {
61 av_assert1(((MpegEncContext *)avctx->priv_data)->studio_profile);
62 2 return avctx->pix_fmt;
63 }
64
65
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 323 times.
333 if (avctx->codec->id == AV_CODEC_ID_MSS2)
66 10 return AV_PIX_FMT_YUV420P;
67
68 if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY)) {
69 if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
70 avctx->color_range = AVCOL_RANGE_MPEG;
71 return AV_PIX_FMT_GRAY8;
72 }
73
74 323 return avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
75 }
76
77 296 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
78 {
79 296 MpegEncContext *s = avctx->priv_data;
80 int ret;
81
82 296 s->out_format = FMT_H263;
83
84 // set defaults
85 296 ff_mpv_decode_init(s, avctx);
86
87 296 s->quant_precision = 5;
88 296 s->decode_mb = ff_h263_decode_mb;
89 296 s->low_delay = 1;
90
91 /* select sub codec */
92
9/11
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 169 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 42 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 20 times.
✗ Branch 10 not taken.
296 switch (avctx->codec->id) {
93 20 case AV_CODEC_ID_H263:
94 case AV_CODEC_ID_H263P:
95 20 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
96 20 break;
97 169 case AV_CODEC_ID_MPEG4:
98 169 break;
99 2 case AV_CODEC_ID_MSMPEG4V1:
100 2 s->h263_pred = 1;
101 2 s->msmpeg4_version = 1;
102 2 break;
103 9 case AV_CODEC_ID_MSMPEG4V2:
104 9 s->h263_pred = 1;
105 9 s->msmpeg4_version = 2;
106 9 break;
107 13 case AV_CODEC_ID_MSMPEG4V3:
108 13 s->h263_pred = 1;
109 13 s->msmpeg4_version = 3;
110 13 break;
111 9 case AV_CODEC_ID_WMV1:
112 9 s->h263_pred = 1;
113 9 s->msmpeg4_version = 4;
114 9 break;
115 12 case AV_CODEC_ID_WMV2:
116 12 s->h263_pred = 1;
117 12 s->msmpeg4_version = 5;
118 12 break;
119 42 case AV_CODEC_ID_VC1:
120 case AV_CODEC_ID_WMV3:
121 case AV_CODEC_ID_VC1IMAGE:
122 case AV_CODEC_ID_WMV3IMAGE:
123 case AV_CODEC_ID_MSS2:
124 42 s->h263_pred = 1;
125 42 s->msmpeg4_version = 6;
126 42 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
127 42 break;
128 case AV_CODEC_ID_H263I:
129 break;
130 20 case AV_CODEC_ID_FLV1:
131 20 s->h263_flv = 1;
132 20 break;
133 default:
134 av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
135 avctx->codec->id);
136 return AVERROR(ENOSYS);
137 }
138
139
2/4
✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 296 times.
296 if (avctx->codec_tag == AV_RL32("L263") || avctx->codec_tag == AV_RL32("S263"))
140 if (avctx->extradata_size == 56 && avctx->extradata[0] == 1)
141 s->ehc_mode = 1;
142
143 /* for H.263, we allocate the images after having read the header */
144
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 20 times.
296 if (avctx->codec->id != AV_CODEC_ID_H263 &&
145
1/2
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
276 avctx->codec->id != AV_CODEC_ID_H263P &&
146
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 169 times.
276 avctx->codec->id != AV_CODEC_ID_MPEG4) {
147 107 avctx->pix_fmt = h263_get_format(avctx);
148 107 ff_mpv_idct_init(s);
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107 times.
107 if ((ret = ff_mpv_common_init(s)) < 0)
150 return ret;
151 }
152
153 296 ff_h263dsp_init(&s->h263dsp);
154 296 ff_qpeldsp_init(&s->qdsp);
155 296 ff_h263_decode_init_vlc();
156
157 296 return 0;
158 }
159
160 254 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
161 {
162 254 MpegEncContext *s = avctx->priv_data;
163
164 254 ff_mpv_common_end(s);
165 254 return 0;
166 }
167
168 /**
169 * Return the number of bytes consumed for building the current frame.
170 */
171 5598 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
172 {
173 5598 int pos = (get_bits_count(&s->gb) + 7) >> 3;
174
175
3/4
✓ Branch 0 taken 5581 times.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5581 times.
5598 if (s->divx_packed || s->avctx->hwaccel) {
176 /* We would have to scan through the whole buf to handle the weird
177 * reordering ... */
178 17 return buf_size;
179 #if FF_API_FLAG_TRUNCATED
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5581 times.
5581 } else if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
181 pos -= s->parse_context.last_index;
182 // padding is not really read so this might be -1
183 if (pos < 0)
184 pos = 0;
185 return pos;
186 #endif
187 } else {
188 // avoid infinite loops (maybe not needed...)
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5581 times.
5581 if (pos == 0)
190 pos = 1;
191 // oops ;)
192
2/2
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 164 times.
5581 if (pos + 10 > buf_size)
193 5417 pos = buf_size;
194
195 5581 return pos;
196 }
197 }
198
199 11869 static int decode_slice(MpegEncContext *s)
200 {
201 23738 const int part_mask = s->partitioned_frame
202
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 10141 times.
11869 ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
203 11869 const int mb_size = 16 >> s->avctx->lowres;
204 int ret;
205
206 11869 s->last_resync_gb = s->gb;
207 11869 s->first_slice_line = 1;
208 11869 s->resync_mb_x = s->mb_x;
209 11869 s->resync_mb_y = s->mb_y;
210
211 11869 ff_set_qscale(s, s->qscale);
212
213
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 11839 times.
11869 if (s->studio_profile) {
214
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)
215 return ret;
216 }
217
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11869 times.
11869 if (s->avctx->hwaccel) {
219 const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
220 ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
221 // ensure we exit decode loop
222 s->mb_y = s->mb_height;
223 return ret;
224 }
225
226
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 10141 times.
11869 if (s->partitioned_frame) {
227 1728 const int qscale = s->qscale;
228
229
1/2
✓ Branch 0 taken 1728 times.
✗ Branch 1 not taken.
1728 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1728 times.
1728 if ((ret = ff_mpeg4_decode_partitions(s->avctx->priv_data)) < 0)
231 return ret;
232
233 /* restore variables which were modified */
234 1728 s->first_slice_line = 1;
235 1728 s->mb_x = s->resync_mb_x;
236 1728 s->mb_y = s->resync_mb_y;
237 1728 ff_set_qscale(s, qscale);
238 }
239
240
2/2
✓ Branch 0 taken 92085 times.
✓ Branch 1 taken 1346 times.
93431 for (; s->mb_y < s->mb_height; s->mb_y++) {
241 /* per-row end of slice checks */
242
2/2
✓ Branch 0 taken 19665 times.
✓ Branch 1 taken 72420 times.
92085 if (s->msmpeg4_version) {
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19665 times.
19665 if (s->resync_mb_y + s->slice_height == s->mb_y) {
244 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
245 s->mb_x - 1, s->mb_y, ER_MB_END);
246
247 return 0;
248 }
249 }
250
251
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 91335 times.
92085 if (s->msmpeg4_version == 1) {
252 750 s->last_dc[0] =
253 750 s->last_dc[1] =
254 750 s->last_dc[2] = 128;
255 }
256
257 92085 ff_init_block_index(s);
258
2/2
✓ Branch 0 taken 2114408 times.
✓ Branch 1 taken 81562 times.
2195970 for (; s->mb_x < s->mb_width; s->mb_x++) {
259 int ret;
260
261 2114408 ff_update_block_index(s);
262
263
4/4
✓ Branch 0 taken 91268 times.
✓ Branch 1 taken 2023140 times.
✓ Branch 2 taken 7270 times.
✓ Branch 3 taken 83998 times.
2114408 if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
264 7270 s->first_slice_line = 0;
265
266 /* DCT & quantize */
267
268 2114408 s->mv_dir = MV_DIR_FORWARD;
269 2114408 s->mv_type = MV_TYPE_16X16;
270 ff_dlog(s, "%d %06X\n",
271 get_bits_count(&s->gb), show_bits(&s->gb, 24));
272
273 ff_tlog(NULL, "Decoding MB at %dx%d\n", s->mb_x, s->mb_y);
274 2114408 ret = s->decode_mb(s, s->block);
275
276
2/2
✓ Branch 0 taken 1874020 times.
✓ Branch 1 taken 240388 times.
2114408 if (s->pict_type != AV_PICTURE_TYPE_B)
277 1874020 ff_h263_update_motion_val(s);
278
279
2/2
✓ Branch 0 taken 10523 times.
✓ Branch 1 taken 2103885 times.
2114408 if (ret < 0) {
280 10523 const int xy = s->mb_x + s->mb_y * s->mb_stride;
281
1/2
✓ Branch 0 taken 10523 times.
✗ Branch 1 not taken.
10523 if (ret == SLICE_END) {
282 10523 ff_mpv_reconstruct_mb(s, s->block);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (s->loop_filter)
284 ff_h263_loop_filter(s);
285
286 10523 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
287 s->mb_x, s->mb_y, ER_MB_END & part_mask);
288
289 10523 s->padding_bug_score--;
290
291
2/2
✓ Branch 0 taken 7570 times.
✓ Branch 1 taken 2953 times.
10523 if (++s->mb_x >= s->mb_width) {
292 7570 s->mb_x = 0;
293 7570 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
294 7570 ff_mpv_report_decode_progress(s);
295 7570 s->mb_y++;
296 }
297 10523 return 0;
298 } else if (ret == SLICE_NOEND) {
299 av_log(s->avctx, AV_LOG_ERROR,
300 "Slice mismatch at MB: %d\n", xy);
301 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
302 s->mb_x + 1, s->mb_y,
303 ER_MB_END & part_mask);
304 return AVERROR_INVALIDDATA;
305 }
306 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
307 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
308 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
309
310 if (s->avctx->err_recognition & AV_EF_IGNORE_ERR)
311 continue;
312 return AVERROR_INVALIDDATA;
313 }
314
315 2103885 ff_mpv_reconstruct_mb(s, s->block);
316
2/2
✓ Branch 0 taken 141300 times.
✓ Branch 1 taken 1962585 times.
2103885 if (s->loop_filter)
317 141300 ff_h263_loop_filter(s);
318 }
319
320 81562 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
321 81562 ff_mpv_report_decode_progress(s);
322
323 81562 s->mb_x = 0;
324 }
325
326 av_assert1(s->mb_x == 0 && s->mb_y == s->mb_height);
327
328 // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
330 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
331 get_bits_left(&s->gb) >= 48 &&
332 show_bits(&s->gb, 24) == 0x4010 &&
333 !s->data_partitioning)
334 s->padding_bug_score += 32;
335
336 /* try to detect the padding bug */
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
338 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
339 get_bits_left(&s->gb) >= 0 &&
340 get_bits_left(&s->gb) < 137 &&
341 !s->data_partitioning) {
342 const int bits_count = get_bits_count(&s->gb);
343 const int bits_left = s->gb.size_in_bits - bits_count;
344
345 if (bits_left == 0) {
346 s->padding_bug_score += 16;
347 } else if (bits_left != 1) {
348 int v = show_bits(&s->gb, 8);
349 v |= 0x7F >> (7 - (bits_count & 7));
350
351 if (v == 0x7F && bits_left <= 8)
352 s->padding_bug_score--;
353 else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
354 bits_left <= 16)
355 s->padding_bug_score += 4;
356 else
357 s->padding_bug_score++;
358 }
359 }
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_H263 &&
362 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
363 get_bits_left(&s->gb) >= 8 &&
364 get_bits_left(&s->gb) < 300 &&
365 s->pict_type == AV_PICTURE_TYPE_I &&
366 show_bits(&s->gb, 8) == 0 &&
367 !s->data_partitioning) {
368
369 s->padding_bug_score += 32;
370 }
371
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (s->codec_id == AV_CODEC_ID_H263 &&
373 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
374 get_bits_left(&s->gb) >= 64 &&
375 AV_RB64(s->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) {
376
377 s->padding_bug_score += 32;
378 }
379
380
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
381 1346 if (
382
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))
383 1346 s->workaround_bugs |= FF_BUG_NO_PADDING;
384 else
385 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
386 }
387
388 // handle formats which don't have unique end markers
389
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1346 if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
390 1346 int left = get_bits_left(&s->gb);
391 1346 int max_extra = 7;
392
393 /* no markers in M$ crap */
394
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 && s->pict_type == AV_PICTURE_TYPE_I)
395 90 max_extra += 17;
396
397 /* buggy padding but the frame should still end approximately at
398 * the bitstream end */
399
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 (s->avctx->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
401 max_extra += 48;
402
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
403 1346 max_extra += 256 * 256 * 256 * 64;
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (left > max_extra)
406 av_log(s->avctx, AV_LOG_ERROR,
407 "discarding %d junk bits at end, next would be %X\n",
408 left, show_bits(&s->gb, 24));
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 else if (left < 0)
410 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
411 else
412 1346 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
413 1346 s->mb_x - 1, s->mb_y, ER_MB_END);
414
415 1346 return 0;
416 }
417
418 av_log(s->avctx, AV_LOG_ERROR,
419 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
420 get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
421
422 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
423 ER_MB_END & part_mask);
424
425 return AVERROR_INVALIDDATA;
426 }
427
428 5701 int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict,
429 int *got_frame, AVPacket *avpkt)
430 {
431 5701 const uint8_t *buf = avpkt->data;
432 5701 int buf_size = avpkt->size;
433 5701 MpegEncContext *s = avctx->priv_data;
434 int ret;
435 5701 int slice_ret = 0;
436
437 /* no supplementary picture */
438
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 5598 times.
5701 if (buf_size == 0) {
439 /* special case for last picture */
440
4/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 23 times.
103 if (s->low_delay == 0 && s->next_picture_ptr) {
441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
442 return ret;
443 27 s->next_picture_ptr = NULL;
444
445 27 *got_frame = 1;
446 }
447
448 103 return 0;
449 }
450
451 #if FF_API_FLAG_TRUNCATED
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5598 times.
5598 if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
453 int next;
454
455 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
456 next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
457 } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
458 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
459 } else if (CONFIG_H263P_DECODER && s->codec_id == AV_CODEC_ID_H263P) {
460 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
461 } else {
462 av_log(s->avctx, AV_LOG_ERROR,
463 "this codec does not support truncated bitstreams\n");
464 return AVERROR(ENOSYS);
465 }
466
467 if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
468 &buf_size) < 0)
469 return buf_size;
470 }
471 #endif
472
473 5598 retry:
474
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5587 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 8 times.
5602 if (s->divx_packed && s->bitstream_buffer_size) {
475 int i;
476
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 for(i=0; i < buf_size-3; i++) {
477
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) {
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (buf[i+3]==0xB0) {
479 av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
480 s->bitstream_buffer_size = 0;
481 }
482 7 break;
483 }
484 }
485 }
486
487
3/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5595 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5602 if (s->bitstream_buffer_size && (s->divx_packed || buf_size <= MAX_NVOP_SIZE)) // divx 5.01+/xvid frame reorder
488 7 ret = init_get_bits8(&s->gb, s->bitstream_buffer,
489 s->bitstream_buffer_size);
490 else
491 5595 ret = init_get_bits8(&s->gb, buf, buf_size);
492
493 5602 s->bitstream_buffer_size = 0;
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5602 times.
5602 if (ret < 0)
495 return ret;
496
497
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 5415 times.
5602 if (!s->context_initialized)
498 // we need the idct permutation for reading a custom matrix
499 187 ff_mpv_idct_init(s);
500
501 /* let's go :-) */
502
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4928 times.
5602 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
503 674 ret = ff_wmv2_decode_picture_header(s);
504
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 4253 times.
4928 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
505 675 ret = ff_msmpeg4_decode_picture_header(s);
506
2/2
✓ Branch 0 taken 3477 times.
✓ Branch 1 taken 776 times.
4253 } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
507
4/4
✓ Branch 0 taken 3475 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 165 times.
✓ Branch 3 taken 3310 times.
3477 if (s->avctx->extradata_size && s->picture_number == 0) {
508 GetBitContext gb;
509
510
1/2
✓ Branch 1 taken 165 times.
✗ Branch 2 not taken.
165 if (init_get_bits8(&gb, s->avctx->extradata, s->avctx->extradata_size) >= 0 )
511 165 ff_mpeg4_decode_picture_header(avctx->priv_data, &gb, 1, 0);
512 }
513 3477 ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb, 0, 0);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 776 times.
776 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
515 ret = ff_intel_h263_decode_picture_header(s);
516
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 461 times.
776 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
517 315 ret = ff_flv_decode_picture_header(s);
518 } else {
519 461 ret = ff_h263_decode_picture_header(s);
520 }
521
522
2/4
✓ Branch 0 taken 5602 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5602 times.
5602 if (ret < 0 || ret == FRAME_SKIPPED) {
523 if ( s->width != avctx->coded_width
524 || s->height != avctx->coded_height) {
525 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
526 s->width = avctx->coded_width;
527 s->height= avctx->coded_height;
528 }
529 }
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5602 times.
5602 if (ret == FRAME_SKIPPED)
531 return get_consumed_bytes(s, buf_size);
532
533 /* skip if the header was thrashed */
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5602 times.
5602 if (ret < 0) {
535 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
536 return ret;
537 }
538
539
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 5415 times.
5602 if (!s->context_initialized) {
540 187 avctx->pix_fmt = h263_get_format(avctx);
541
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 187 times.
187 if ((ret = ff_mpv_common_init(s)) < 0)
542 return ret;
543 }
544
545
4/4
✓ Branch 0 taken 5377 times.
✓ Branch 1 taken 225 times.
✓ Branch 2 taken 5327 times.
✓ Branch 3 taken 50 times.
5602 if (!s->current_picture_ptr || s->current_picture_ptr->f->data[0]) {
546 5552 int i = ff_find_unused_picture(s->avctx, s->picture, 0);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5552 times.
5552 if (i < 0)
548 return i;
549 5552 s->current_picture_ptr = &s->picture[i];
550 }
551
552 5602 avctx->has_b_frames = !s->low_delay;
553
554
2/2
✓ Branch 0 taken 3477 times.
✓ Branch 1 taken 2125 times.
5602 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
555
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3473 times.
3477 if (ff_mpeg4_workaround_bugs(avctx) == 1)
556 4 goto retry;
557
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3472 times.
3473 if (s->studio_profile != (s->idsp.idct == NULL))
558 1 ff_mpv_idct_init(s);
559 }
560
561 /* After H.263 & MPEG-4 header decode we have the height, width,
562 * and other parameters. So then we could init the picture.
563 * FIXME: By the way H.263 decoder is evolving it should have
564 * an H263EncContext */
565
2/2
✓ Branch 0 taken 5558 times.
✓ Branch 1 taken 40 times.
5598 if (s->width != avctx->coded_width ||
566
1/2
✓ Branch 0 taken 5558 times.
✗ Branch 1 not taken.
5558 s->height != avctx->coded_height ||
567
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5557 times.
5558 s->context_reinit) {
568 /* H.263 could change picture size any time */
569 41 s->context_reinit = 0;
570
571 41 ret = ff_set_dimensions(avctx, s->width, s->height);
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (ret < 0)
573 return ret;
574
575 41 ff_set_sar(avctx, avctx->sample_aspect_ratio);
576
577
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if ((ret = ff_mpv_common_frame_size_change(s)))
578 return ret;
579
580
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (avctx->pix_fmt != h263_get_format(avctx)) {
581 av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
582 avctx->pix_fmt = AV_PIX_FMT_NONE;
583 return AVERROR_UNKNOWN;
584 }
585 }
586
587
2/2
✓ Branch 0 taken 5137 times.
✓ Branch 1 taken 461 times.
5598 if (s->codec_id == AV_CODEC_ID_H263 ||
588
1/2
✓ Branch 0 taken 5137 times.
✗ Branch 1 not taken.
5137 s->codec_id == AV_CODEC_ID_H263P ||
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5137 times.
5137 s->codec_id == AV_CODEC_ID_H263I)
590
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);
591
592 // for skipping the frame
593 5598 s->current_picture.f->pict_type = s->pict_type;
594 5598 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
595
596 /* skip B-frames if we don't have reference frames */
597
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 5236 times.
5598 if (!s->last_picture_ptr &&
598
2/4
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 362 times.
362 (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
599 return get_consumed_bytes(s, buf_size);
600
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 5429 times.
5598 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
601
1/2
✓ Branch 0 taken 169 times.
✗ Branch 1 not taken.
169 s->pict_type == AV_PICTURE_TYPE_B) ||
602
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 5429 times.
5598 (avctx->skip_frame >= AVDISCARD_NONKEY &&
603
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 47 times.
169 s->pict_type != AV_PICTURE_TYPE_I) ||
604
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 5432 times.
5551 avctx->skip_frame >= AVDISCARD_ALL)
605 166 return get_consumed_bytes(s, buf_size);
606
607
3/4
✓ Branch 0 taken 1945 times.
✓ Branch 1 taken 3487 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1945 times.
5432 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
608 3487 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
609 3487 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
610 } else {
611 1945 s->me.qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
612 1945 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
613 }
614
615
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5432 times.
5432 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
616 return ret;
617
618
3/4
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 5417 times.
✗ Branch 3 not taken.
5432 if (!s->divx_packed && !avctx->hwaccel)
619 5417 ff_thread_finish_setup(avctx);
620
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5432 times.
5432 if (avctx->hwaccel) {
622 ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
623 s->gb.buffer_end - s->gb.buffer);
624 if (ret < 0 )
625 return ret;
626 }
627
628 5432 ff_mpeg_er_frame_start(s);
629
630 /* the second part of the wmv2 header contains the MB skip bits which
631 * are stored in current_picture->mb_type which is not available before
632 * ff_mpv_frame_start() */
633
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4758 times.
5432 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
634 674 ret = ff_wmv2_decode_secondary_picture_header(s);
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
674 if (ret < 0)
636 return ret;
637
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 671 times.
674 if (ret == 1)
638 3 goto frame_end;
639 }
640
641 /* decode each macroblock */
642 5429 s->mb_x = 0;
643 5429 s->mb_y = 0;
644
645 5429 slice_ret = decode_slice(s);
646
2/2
✓ Branch 0 taken 6440 times.
✓ Branch 1 taken 5429 times.
11869 while (s->mb_y < s->mb_height) {
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6440 times.
6440 if (s->msmpeg4_version) {
648 if (s->slice_height == 0 || s->mb_x != 0 || slice_ret < 0 ||
649 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
650 break;
651 } else {
652 6440 int prev_x = s->mb_x, prev_y = s->mb_y;
653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6440 times.
6440 if (ff_h263_resync(s) < 0)
654 break;
655
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 6411 times.
6440 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
656 29 s->er.error_occurred = 1;
657 }
658
659
3/4
✓ Branch 0 taken 6440 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3896 times.
✓ Branch 3 taken 2544 times.
6440 if (s->msmpeg4_version < 4 && s->h263_pred)
660 3896 ff_mpeg4_clean_buffers(s);
661
662
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6440 times.
6440 if (decode_slice(s) < 0)
663 slice_ret = AVERROR_INVALIDDATA;
664 }
665
666
4/4
✓ Branch 0 taken 4083 times.
✓ Branch 1 taken 1346 times.
✓ Branch 2 taken 871 times.
✓ Branch 3 taken 475 times.
5429 if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
667
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 44 times.
475 s->pict_type == AV_PICTURE_TYPE_I)
668
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!CONFIG_MSMPEG4_DECODER ||
669 44 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
670 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
671
672 av_assert1(s->bitstream_buffer_size == 0);
673 5429 frame_end:
674
2/2
✓ Branch 0 taken 5431 times.
✓ Branch 1 taken 1 times.
5432 if (!s->studio_profile)
675 5431 ff_er_frame_end(&s->er);
676
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5432 times.
5432 if (avctx->hwaccel) {
678 ret = avctx->hwaccel->end_frame(avctx);
679 if (ret < 0)
680 return ret;
681 }
682
683 5432 ff_mpv_frame_end(s);
684
685
2/2
✓ Branch 0 taken 3330 times.
✓ Branch 1 taken 2102 times.
5432 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
686 3330 ff_mpeg4_frame_end(avctx, buf, buf_size);
687
688
3/4
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5417 times.
5432 if (!s->divx_packed && avctx->hwaccel)
689 ff_thread_finish_setup(avctx);
690
691 av_assert1(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
692 av_assert1(s->current_picture.f->pict_type == s->pict_type);
693
4/4
✓ Branch 0 taken 4749 times.
✓ Branch 1 taken 683 times.
✓ Branch 2 taken 4353 times.
✓ Branch 3 taken 396 times.
5432 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
694
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5036 times.
5036 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
695 return ret;
696 5036 ff_print_debug_info(s, s->current_picture_ptr, pict);
697 5036 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_MPV_QSCALE_TYPE_MPEG1);
698
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 27 times.
396 } else if (s->last_picture_ptr) {
699
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 369 times.
369 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
700 return ret;
701 369 ff_print_debug_info(s, s->last_picture_ptr, pict);
702 369 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_MPV_QSCALE_TYPE_MPEG1);
703 }
704
705
4/4
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 5316 times.
✓ Branch 2 taken 89 times.
✓ Branch 3 taken 27 times.
5432 if (s->last_picture_ptr || s->low_delay) {
706
2/2
✓ Branch 0 taken 5404 times.
✓ Branch 1 taken 1 times.
5405 if ( pict->format == AV_PIX_FMT_YUV420P
707
2/4
✓ Branch 0 taken 5404 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5404 times.
5404 && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) {
708 for (int p = 0; p < 3; p++) {
709 int h = AV_CEIL_RSHIFT(pict->height, !!p);
710
711 pict->data[p] += (h - 1) * pict->linesize[p];
712 pict->linesize[p] *= -1;
713 }
714 }
715 5405 *got_frame = 1;
716 }
717
718
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5432 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5432 if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
719 return slice_ret;
720 else
721 5432 return get_consumed_bytes(s, buf_size);
722 }
723
724 const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
725 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
726 AV_PIX_FMT_VAAPI,
727 #endif
728 #if CONFIG_MPEG4_NVDEC_HWACCEL
729 AV_PIX_FMT_CUDA,
730 #endif
731 #if CONFIG_MPEG4_VDPAU_HWACCEL
732 AV_PIX_FMT_VDPAU,
733 #endif
734 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
735 AV_PIX_FMT_VIDEOTOOLBOX,
736 #endif
737 AV_PIX_FMT_YUV420P,
738 AV_PIX_FMT_NONE
739 };
740
741 static const AVCodecHWConfigInternal *const h263_hw_config_list[] = {
742 #if CONFIG_H263_VAAPI_HWACCEL
743 HWACCEL_VAAPI(h263),
744 #endif
745 #if CONFIG_MPEG4_NVDEC_HWACCEL
746 HWACCEL_NVDEC(mpeg4),
747 #endif
748 #if CONFIG_MPEG4_VDPAU_HWACCEL
749 HWACCEL_VDPAU(mpeg4),
750 #endif
751 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
752 HWACCEL_VIDEOTOOLBOX(h263),
753 #endif
754 NULL
755 };
756
757 const FFCodec ff_h263_decoder = {
758 .p.name = "h263",
759 .p.long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
760 .p.type = AVMEDIA_TYPE_VIDEO,
761 .p.id = AV_CODEC_ID_H263,
762 .priv_data_size = sizeof(MpegEncContext),
763 .init = ff_h263_decode_init,
764 .close = ff_h263_decode_end,
765 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
766 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
767 #if FF_API_FLAG_TRUNCATED
768 AV_CODEC_CAP_TRUNCATED |
769 #endif
770 AV_CODEC_CAP_DELAY,
771 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
772 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
773 .flush = ff_mpeg_flush,
774 .p.max_lowres = 3,
775 .p.pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
776 .hw_configs = h263_hw_config_list,
777 };
778
779 const FFCodec ff_h263p_decoder = {
780 .p.name = "h263p",
781 .p.long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
782 .p.type = AVMEDIA_TYPE_VIDEO,
783 .p.id = AV_CODEC_ID_H263P,
784 .priv_data_size = sizeof(MpegEncContext),
785 .init = ff_h263_decode_init,
786 .close = ff_h263_decode_end,
787 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
788 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
789 #if FF_API_FLAG_TRUNCATED
790 AV_CODEC_CAP_TRUNCATED |
791 #endif
792 AV_CODEC_CAP_DELAY,
793 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
794 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
795 .flush = ff_mpeg_flush,
796 .p.max_lowres = 3,
797 .p.pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
798 .hw_configs = h263_hw_config_list,
799 };
800