FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h263dec.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 280 416 67.3%
Branches: 207 389 53.2%

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 328 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 328 times.
328 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 318 times.
328 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 318 return avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
71 }
72
73 290 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
74 {
75 290 MpegEncContext *s = avctx->priv_data;
76 int ret;
77
78 290 s->out_format = FMT_H263;
79
80 // set defaults
81 290 ff_mpv_decode_init(s, avctx);
82
83 290 s->quant_precision = 5;
84 290 s->decode_mb = ff_h263_decode_mb;
85 290 s->low_delay = 1;
86 290 s->unrestricted_mv = 1;
87
88 /* select sub codec */
89
9/11
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 165 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.
290 switch (avctx->codec->id) {
90 20 case AV_CODEC_ID_H263:
91 case AV_CODEC_ID_H263P:
92 20 s->unrestricted_mv = 0;
93 20 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
94 20 break;
95 165 case AV_CODEC_ID_MPEG4:
96 165 break;
97 2 case AV_CODEC_ID_MSMPEG4V1:
98 2 s->h263_pred = 1;
99 2 s->msmpeg4_version = 1;
100 2 break;
101 9 case AV_CODEC_ID_MSMPEG4V2:
102 9 s->h263_pred = 1;
103 9 s->msmpeg4_version = 2;
104 9 break;
105 13 case AV_CODEC_ID_MSMPEG4V3:
106 13 s->h263_pred = 1;
107 13 s->msmpeg4_version = 3;
108 13 break;
109 9 case AV_CODEC_ID_WMV1:
110 9 s->h263_pred = 1;
111 9 s->msmpeg4_version = 4;
112 9 break;
113 12 case AV_CODEC_ID_WMV2:
114 12 s->h263_pred = 1;
115 12 s->msmpeg4_version = 5;
116 12 break;
117 42 case AV_CODEC_ID_VC1:
118 case AV_CODEC_ID_WMV3:
119 case AV_CODEC_ID_VC1IMAGE:
120 case AV_CODEC_ID_WMV3IMAGE:
121 case AV_CODEC_ID_MSS2:
122 42 s->h263_pred = 1;
123 42 s->msmpeg4_version = 6;
124 42 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
125 42 break;
126 case AV_CODEC_ID_H263I:
127 break;
128 18 case AV_CODEC_ID_FLV1:
129 18 s->h263_flv = 1;
130 18 break;
131 default:
132 av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
133 avctx->codec->id);
134 return AVERROR(ENOSYS);
135 }
136
137
2/4
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 290 times.
290 if (avctx->codec_tag == AV_RL32("L263") || avctx->codec_tag == AV_RL32("S263"))
138 if (avctx->extradata_size == 56 && avctx->extradata[0] == 1)
139 s->ehc_mode = 1;
140
141 /* for H.263, we allocate the images after having read the header */
142
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 20 times.
290 if (avctx->codec->id != AV_CODEC_ID_H263 &&
143
1/2
✓ Branch 0 taken 270 times.
✗ Branch 1 not taken.
270 avctx->codec->id != AV_CODEC_ID_H263P &&
144
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 165 times.
270 avctx->codec->id != AV_CODEC_ID_MPEG4) {
145 105 avctx->pix_fmt = h263_get_format(avctx);
146 105 ff_mpv_idct_init(s);
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
105 if ((ret = ff_mpv_common_init(s)) < 0)
148 return ret;
149 }
150
151 290 ff_h263dsp_init(&s->h263dsp);
152 290 ff_qpeldsp_init(&s->qdsp);
153 290 ff_h263_decode_init_vlc();
154
155 290 return 0;
156 }
157
158 248 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
159 {
160 248 MpegEncContext *s = avctx->priv_data;
161
162 248 ff_mpv_common_end(s);
163 248 return 0;
164 }
165
166 /**
167 * Return the number of bytes consumed for building the current frame.
168 */
169 5594 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
170 {
171 5594 int pos = (get_bits_count(&s->gb) + 7) >> 3;
172
173
3/4
✓ Branch 0 taken 5577 times.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5577 times.
5594 if (s->divx_packed || s->avctx->hwaccel) {
174 /* We would have to scan through the whole buf to handle the weird
175 * reordering ... */
176 17 return buf_size;
177 #if FF_API_FLAG_TRUNCATED
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5577 times.
5577 } else if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
179 pos -= s->parse_context.last_index;
180 // padding is not really read so this might be -1
181 if (pos < 0)
182 pos = 0;
183 return pos;
184 #endif
185 } else {
186 // avoid infinite loops (maybe not needed...)
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5577 times.
5577 if (pos == 0)
188 pos = 1;
189 // oops ;)
190
2/2
✓ Branch 0 taken 5416 times.
✓ Branch 1 taken 161 times.
5577 if (pos + 10 > buf_size)
191 5416 pos = buf_size;
192
193 5577 return pos;
194 }
195 }
196
197 11839 static int decode_slice(MpegEncContext *s)
198 {
199 23678 const int part_mask = s->partitioned_frame
200
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 10111 times.
11839 ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
201 11839 const int mb_size = 16 >> s->avctx->lowres;
202 int ret;
203
204 11839 s->last_resync_gb = s->gb;
205 11839 s->first_slice_line = 1;
206 11839 s->resync_mb_x = s->mb_x;
207 11839 s->resync_mb_y = s->mb_y;
208
209 11839 ff_set_qscale(s, s->qscale);
210
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11839 times.
11839 if (s->studio_profile) {
212 if ((ret = ff_mpeg4_decode_studio_slice_header(s->avctx->priv_data)) < 0)
213 return ret;
214 }
215
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11839 times.
11839 if (s->avctx->hwaccel) {
217 const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
218 ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
219 // ensure we exit decode loop
220 s->mb_y = s->mb_height;
221 return ret;
222 }
223
224
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 10111 times.
11839 if (s->partitioned_frame) {
225 1728 const int qscale = s->qscale;
226
227
1/2
✓ Branch 0 taken 1728 times.
✗ Branch 1 not taken.
1728 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
228
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1728 times.
1728 if ((ret = ff_mpeg4_decode_partitions(s->avctx->priv_data)) < 0)
229 return ret;
230
231 /* restore variables which were modified */
232 1728 s->first_slice_line = 1;
233 1728 s->mb_x = s->resync_mb_x;
234 1728 s->mb_y = s->resync_mb_y;
235 1728 ff_set_qscale(s, qscale);
236 }
237
238
2/2
✓ Branch 0 taken 92055 times.
✓ Branch 1 taken 1346 times.
93401 for (; s->mb_y < s->mb_height; s->mb_y++) {
239 /* per-row end of slice checks */
240
2/2
✓ Branch 0 taken 19665 times.
✓ Branch 1 taken 72390 times.
92055 if (s->msmpeg4_version) {
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19665 times.
19665 if (s->resync_mb_y + s->slice_height == s->mb_y) {
242 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
243 s->mb_x - 1, s->mb_y, ER_MB_END);
244
245 return 0;
246 }
247 }
248
249
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 91305 times.
92055 if (s->msmpeg4_version == 1) {
250 750 s->last_dc[0] =
251 750 s->last_dc[1] =
252 750 s->last_dc[2] = 128;
253 }
254
255 92055 ff_init_block_index(s);
256
2/2
✓ Branch 0 taken 2113058 times.
✓ Branch 1 taken 81562 times.
2194620 for (; s->mb_x < s->mb_width; s->mb_x++) {
257 int ret;
258
259 2113058 ff_update_block_index(s);
260
261
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)
262 7270 s->first_slice_line = 0;
263
264 /* DCT & quantize */
265
266 2113058 s->mv_dir = MV_DIR_FORWARD;
267 2113058 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 2113058 ret = s->decode_mb(s, s->block);
273
274
2/2
✓ Branch 0 taken 1872670 times.
✓ Branch 1 taken 240388 times.
2113058 if (s->pict_type != AV_PICTURE_TYPE_B)
275 1872670 ff_h263_update_motion_val(s);
276
277
2/2
✓ Branch 0 taken 10493 times.
✓ Branch 1 taken 2102565 times.
2113058 if (ret < 0) {
278 10493 const int xy = s->mb_x + s->mb_y * s->mb_stride;
279
1/2
✓ Branch 0 taken 10493 times.
✗ Branch 1 not taken.
10493 if (ret == SLICE_END) {
280 10493 ff_mpv_reconstruct_mb(s, s->block);
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10493 times.
10493 if (s->loop_filter)
282 ff_h263_loop_filter(s);
283
284 10493 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 10493 s->padding_bug_score--;
288
289
2/2
✓ Branch 0 taken 7540 times.
✓ Branch 1 taken 2953 times.
10493 if (++s->mb_x >= s->mb_width) {
290 7540 s->mb_x = 0;
291 7540 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
292 7540 ff_mpv_report_decode_progress(s);
293 7540 s->mb_y++;
294 }
295 10493 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)
309 continue;
310 return AVERROR_INVALIDDATA;
311 }
312
313 2102565 ff_mpv_reconstruct_mb(s, s->block);
314
2/2
✓ Branch 0 taken 141300 times.
✓ Branch 1 taken 1961265 times.
2102565 if (s->loop_filter)
315 141300 ff_h263_loop_filter(s);
316 }
317
318 81562 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
319 81562 ff_mpv_report_decode_progress(s);
320
321 81562 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 || (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 && 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 5696 int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
427 AVPacket *avpkt)
428 {
429 5696 const uint8_t *buf = avpkt->data;
430 5696 int buf_size = avpkt->size;
431 5696 MpegEncContext *s = avctx->priv_data;
432 int ret;
433 5696 int slice_ret = 0;
434 5696 AVFrame *pict = data;
435
436 /* no supplementary picture */
437
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 5594 times.
5696 if (buf_size == 0) {
438 /* special case for last picture */
439
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) {
440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
441 return ret;
442 27 s->next_picture_ptr = NULL;
443
444 27 *got_frame = 1;
445 }
446
447 102 return 0;
448 }
449
450 #if FF_API_FLAG_TRUNCATED
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5594 times.
5594 if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
452 int next;
453
454 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
455 next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
456 } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
457 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
458 } else if (CONFIG_H263P_DECODER && s->codec_id == AV_CODEC_ID_H263P) {
459 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
460 } else {
461 av_log(s->avctx, AV_LOG_ERROR,
462 "this codec does not support truncated bitstreams\n");
463 return AVERROR(ENOSYS);
464 }
465
466 if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
467 &buf_size) < 0)
468 return buf_size;
469 }
470 #endif
471
472 5594 retry:
473
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5583 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 8 times.
5598 if (s->divx_packed && s->bitstream_buffer_size) {
474 int i;
475
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 for(i=0; i < buf_size-3; i++) {
476
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) {
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (buf[i+3]==0xB0) {
478 av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
479 s->bitstream_buffer_size = 0;
480 }
481 7 break;
482 }
483 }
484 }
485
486
3/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5591 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5598 if (s->bitstream_buffer_size && (s->divx_packed || buf_size <= MAX_NVOP_SIZE)) // divx 5.01+/xvid frame reorder
487 7 ret = init_get_bits8(&s->gb, s->bitstream_buffer,
488 s->bitstream_buffer_size);
489 else
490 5591 ret = init_get_bits8(&s->gb, buf, buf_size);
491
492 5598 s->bitstream_buffer_size = 0;
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5598 times.
5598 if (ret < 0)
494 return ret;
495
496
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 5414 times.
5598 if (!s->context_initialized)
497 // we need the idct permutation for reading a custom matrix
498 184 ff_mpv_idct_init(s);
499
500 /* let's go :-) */
501
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4924 times.
5598 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
502 674 ret = ff_wmv2_decode_picture_header(s);
503
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 4249 times.
4924 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
504 675 ret = ff_msmpeg4_decode_picture_header(s);
505
2/2
✓ Branch 0 taken 3474 times.
✓ Branch 1 taken 775 times.
4249 } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
506
4/4
✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 162 times.
✓ Branch 3 taken 3310 times.
3474 if (s->avctx->extradata_size && s->picture_number == 0) {
507 GetBitContext gb;
508
509
1/2
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
162 if (init_get_bits8(&gb, s->avctx->extradata, s->avctx->extradata_size) >= 0 )
510 162 ff_mpeg4_decode_picture_header(avctx->priv_data, &gb, 1);
511 }
512 3474 ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb, 0);
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 775 times.
775 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
514 ret = ff_intel_h263_decode_picture_header(s);
515
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 461 times.
775 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
516 314 ret = ff_flv_decode_picture_header(s);
517 } else {
518 461 ret = ff_h263_decode_picture_header(s);
519 }
520
521
2/4
✓ Branch 0 taken 5598 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5598 times.
5598 if (ret < 0 || ret == FRAME_SKIPPED) {
522 if ( s->width != avctx->coded_width
523 || s->height != avctx->coded_height) {
524 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
525 s->width = avctx->coded_width;
526 s->height= avctx->coded_height;
527 }
528 }
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5598 times.
5598 if (ret == FRAME_SKIPPED)
530 return get_consumed_bytes(s, buf_size);
531
532 /* skip if the header was thrashed */
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5598 times.
5598 if (ret < 0) {
534 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
535 return ret;
536 }
537
538
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 5414 times.
5598 if (!s->context_initialized) {
539 184 avctx->pix_fmt = h263_get_format(avctx);
540
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
184 if ((ret = ff_mpv_common_init(s)) < 0)
541 return ret;
542 }
543
544
4/4
✓ Branch 0 taken 5377 times.
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 5327 times.
✓ Branch 3 taken 50 times.
5598 if (!s->current_picture_ptr || s->current_picture_ptr->f->data[0]) {
545 5548 int i = ff_find_unused_picture(s->avctx, s->picture, 0);
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5548 times.
5548 if (i < 0)
547 return i;
548 5548 s->current_picture_ptr = &s->picture[i];
549 }
550
551 5598 avctx->has_b_frames = !s->low_delay;
552
553
2/2
✓ Branch 0 taken 3474 times.
✓ Branch 1 taken 2124 times.
5598 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
554
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3470 times.
3474 if (ff_mpeg4_workaround_bugs(avctx) == 1)
555 4 goto retry;
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3470 times.
3470 if (s->studio_profile != (s->idsp.idct == NULL))
557 ff_mpv_idct_init(s);
558 }
559
560 /* After H.263 & MPEG-4 header decode we have the height, width,
561 * and other parameters. So then we could init the picture.
562 * FIXME: By the way H.263 decoder is evolving it should have
563 * an H263EncContext */
564
2/2
✓ Branch 0 taken 5555 times.
✓ Branch 1 taken 39 times.
5594 if (s->width != avctx->coded_width ||
565
1/2
✓ Branch 0 taken 5555 times.
✗ Branch 1 not taken.
5555 s->height != avctx->coded_height ||
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5555 times.
5555 s->context_reinit) {
567 /* H.263 could change picture size any time */
568 39 s->context_reinit = 0;
569
570 39 ret = ff_set_dimensions(avctx, s->width, s->height);
571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret < 0)
572 return ret;
573
574 39 ff_set_sar(avctx, avctx->sample_aspect_ratio);
575
576
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = ff_mpv_common_frame_size_change(s)))
577 return ret;
578
579
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if (avctx->pix_fmt != h263_get_format(avctx)) {
580 av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
581 avctx->pix_fmt = AV_PIX_FMT_NONE;
582 return AVERROR_UNKNOWN;
583 }
584 }
585
586
2/2
✓ Branch 0 taken 5133 times.
✓ Branch 1 taken 461 times.
5594 if (s->codec_id == AV_CODEC_ID_H263 ||
587
1/2
✓ Branch 0 taken 5133 times.
✗ Branch 1 not taken.
5133 s->codec_id == AV_CODEC_ID_H263P ||
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5133 times.
5133 s->codec_id == AV_CODEC_ID_H263I)
589
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);
590
591 // for skipping the frame
592 5594 s->current_picture.f->pict_type = s->pict_type;
593 5594 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
594
595 /* skip B-frames if we don't have reference frames */
596
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 5236 times.
5594 if (!s->last_picture_ptr &&
597
2/4
✓ Branch 0 taken 358 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 358 times.
358 (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
598 return get_consumed_bytes(s, buf_size);
599
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 5428 times.
5594 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
600
1/2
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
166 s->pict_type == AV_PICTURE_TYPE_B) ||
601
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 5428 times.
5594 (avctx->skip_frame >= AVDISCARD_NONKEY &&
602
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 47 times.
166 s->pict_type != AV_PICTURE_TYPE_I) ||
603
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 5431 times.
5547 avctx->skip_frame >= AVDISCARD_ALL)
604 163 return get_consumed_bytes(s, buf_size);
605
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5431 times.
5431 if (s->next_p_frame_damaged) {
607 if (s->pict_type == AV_PICTURE_TYPE_B)
608 return get_consumed_bytes(s, buf_size);
609 else
610 s->next_p_frame_damaged = 0;
611 }
612
613
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) {
614 3486 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
615 3486 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
616 } else {
617 1945 s->me.qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
618 1945 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
619 }
620
621
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5431 times.
5431 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
622 return ret;
623
624
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)
625 5416 ff_thread_finish_setup(avctx);
626
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5431 times.
5431 if (avctx->hwaccel) {
628 ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
629 s->gb.buffer_end - s->gb.buffer);
630 if (ret < 0 )
631 return ret;
632 }
633
634 5431 ff_mpeg_er_frame_start(s);
635
636 /* the second part of the wmv2 header contains the MB skip bits which
637 * are stored in current_picture->mb_type which is not available before
638 * ff_mpv_frame_start() */
639
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 4757 times.
5431 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
640 674 ret = ff_wmv2_decode_secondary_picture_header(s);
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
674 if (ret < 0)
642 return ret;
643
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 671 times.
674 if (ret == 1)
644 3 goto frame_end;
645 }
646
647 /* decode each macroblock */
648 5428 s->mb_x = 0;
649 5428 s->mb_y = 0;
650
651 5428 slice_ret = decode_slice(s);
652
2/2
✓ Branch 0 taken 6411 times.
✓ Branch 1 taken 5428 times.
11839 while (s->mb_y < s->mb_height) {
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6411 times.
6411 if (s->msmpeg4_version) {
654 if (s->slice_height == 0 || s->mb_x != 0 || slice_ret < 0 ||
655 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
656 break;
657 } else {
658 6411 int prev_x = s->mb_x, prev_y = s->mb_y;
659
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6411 times.
6411 if (ff_h263_resync(s) < 0)
660 break;
661
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)
662 29 s->er.error_occurred = 1;
663 }
664
665
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)
666 3867 ff_mpeg4_clean_buffers(s);
667
668
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6411 times.
6411 if (decode_slice(s) < 0)
669 slice_ret = AVERROR_INVALIDDATA;
670 }
671
672
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 &&
673
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 44 times.
475 s->pict_type == AV_PICTURE_TYPE_I)
674
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!CONFIG_MSMPEG4_DECODER ||
675 44 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
676 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
677
678 av_assert1(s->bitstream_buffer_size == 0);
679 5428 frame_end:
680
1/2
✓ Branch 0 taken 5431 times.
✗ Branch 1 not taken.
5431 if (!s->studio_profile)
681 5431 ff_er_frame_end(&s->er);
682
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5431 times.
5431 if (avctx->hwaccel) {
684 ret = avctx->hwaccel->end_frame(avctx);
685 if (ret < 0)
686 return ret;
687 }
688
689 5431 ff_mpv_frame_end(s);
690
691
2/2
✓ Branch 0 taken 3329 times.
✓ Branch 1 taken 2102 times.
5431 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
692 3329 ff_mpeg4_frame_end(avctx, buf, buf_size);
693
694
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)
695 ff_thread_finish_setup(avctx);
696
697 av_assert1(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
698 av_assert1(s->current_picture.f->pict_type == s->pict_type);
699
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) {
700
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5035 times.
5035 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
701 return ret;
702 5035 ff_print_debug_info(s, s->current_picture_ptr, pict);
703 5035 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
704
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 27 times.
396 } else if (s->last_picture_ptr) {
705
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 369 times.
369 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
706 return ret;
707 369 ff_print_debug_info(s, s->last_picture_ptr, pict);
708 369 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
709 }
710
711
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) {
712
1/2
✓ Branch 0 taken 5404 times.
✗ Branch 1 not taken.
5404 if ( pict->format == AV_PIX_FMT_YUV420P
713
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"))) {
714 int x, y, p;
715 av_frame_make_writable(pict);
716 for (p=0; p<3; p++) {
717 int w = AV_CEIL_RSHIFT(pict-> width, !!p);
718 int h = AV_CEIL_RSHIFT(pict->height, !!p);
719 int linesize = pict->linesize[p];
720 for (y=0; y<(h>>1); y++)
721 for (x=0; x<w; x++)
722 FFSWAP(int,
723 pict->data[p][x + y*linesize],
724 pict->data[p][x + (h-1-y)*linesize]);
725 }
726 }
727 5404 *got_frame = 1;
728 }
729
730
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))
731 return slice_ret;
732 else
733 5431 return get_consumed_bytes(s, buf_size);
734 }
735
736 const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
737 #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
738 AV_PIX_FMT_VAAPI,
739 #endif
740 #if CONFIG_MPEG4_NVDEC_HWACCEL
741 AV_PIX_FMT_CUDA,
742 #endif
743 #if CONFIG_MPEG4_VDPAU_HWACCEL
744 AV_PIX_FMT_VDPAU,
745 #endif
746 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
747 AV_PIX_FMT_VIDEOTOOLBOX,
748 #endif
749 AV_PIX_FMT_YUV420P,
750 AV_PIX_FMT_NONE
751 };
752
753 static const AVCodecHWConfigInternal *const h263_hw_config_list[] = {
754 #if CONFIG_H263_VAAPI_HWACCEL
755 HWACCEL_VAAPI(h263),
756 #endif
757 #if CONFIG_MPEG4_NVDEC_HWACCEL
758 HWACCEL_NVDEC(mpeg4),
759 #endif
760 #if CONFIG_MPEG4_VDPAU_HWACCEL
761 HWACCEL_VDPAU(mpeg4),
762 #endif
763 #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
764 HWACCEL_VIDEOTOOLBOX(h263),
765 #endif
766 NULL
767 };
768
769 const AVCodec ff_h263_decoder = {
770 .name = "h263",
771 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
772 .type = AVMEDIA_TYPE_VIDEO,
773 .id = AV_CODEC_ID_H263,
774 .priv_data_size = sizeof(MpegEncContext),
775 .init = ff_h263_decode_init,
776 .close = ff_h263_decode_end,
777 .decode = ff_h263_decode_frame,
778 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
779 #if FF_API_FLAG_TRUNCATED
780 AV_CODEC_CAP_TRUNCATED |
781 #endif
782 AV_CODEC_CAP_DELAY,
783 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
784 .flush = ff_mpeg_flush,
785 .max_lowres = 3,
786 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
787 .hw_configs = h263_hw_config_list,
788 };
789
790 const AVCodec ff_h263p_decoder = {
791 .name = "h263p",
792 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
793 .type = AVMEDIA_TYPE_VIDEO,
794 .id = AV_CODEC_ID_H263P,
795 .priv_data_size = sizeof(MpegEncContext),
796 .init = ff_h263_decode_init,
797 .close = ff_h263_decode_end,
798 .decode = ff_h263_decode_frame,
799 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
800 #if FF_API_FLAG_TRUNCATED
801 AV_CODEC_CAP_TRUNCATED |
802 #endif
803 AV_CODEC_CAP_DELAY,
804 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
805 .flush = ff_mpeg_flush,
806 .max_lowres = 3,
807 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
808 .hw_configs = h263_hw_config_list,
809 };
810