FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 277 410 67.6%
Branches: 206 385 53.5%

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