3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavutil/cpu.h"
30 #include "error_resilience.h"
33 #include "h263_parser.h"
35 #include "mpeg4video.h"
36 #include "mpeg4video_parser.h"
37 #include "mpegvideo.h"
41 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
43 MpegEncContext *s = avctx->priv_data;
47 s->out_format = FMT_H263;
48 s->width = avctx->coded_width;
49 s->height = avctx->coded_height;
50 s->workaround_bugs = avctx->workaround_bugs;
53 ff_MPV_decode_defaults(s);
54 s->quant_precision = 5;
55 s->decode_mb = ff_h263_decode_mb;
57 if (avctx->codec->id == AV_CODEC_ID_MSS2)
58 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
60 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
61 s->unrestricted_mv = 1;
63 /* select sub codec */
64 switch (avctx->codec->id) {
65 case AV_CODEC_ID_H263:
66 s->unrestricted_mv = 0;
67 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
69 case AV_CODEC_ID_MPEG4:
71 case AV_CODEC_ID_MSMPEG4V1:
73 s->msmpeg4_version = 1;
75 case AV_CODEC_ID_MSMPEG4V2:
77 s->msmpeg4_version = 2;
79 case AV_CODEC_ID_MSMPEG4V3:
81 s->msmpeg4_version = 3;
83 case AV_CODEC_ID_WMV1:
85 s->msmpeg4_version = 4;
87 case AV_CODEC_ID_WMV2:
89 s->msmpeg4_version = 5;
92 case AV_CODEC_ID_WMV3:
93 case AV_CODEC_ID_VC1IMAGE:
94 case AV_CODEC_ID_WMV3IMAGE:
95 case AV_CODEC_ID_MSS2:
97 s->msmpeg4_version = 6;
98 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
100 case AV_CODEC_ID_H263I:
102 case AV_CODEC_ID_FLV1:
106 av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
108 return AVERROR(ENOSYS);
110 s->codec_id = avctx->codec->id;
111 avctx->hwaccel = ff_find_hwaccel(avctx);
113 /* for h263, we allocate the images after having read the header */
114 if (avctx->codec->id != AV_CODEC_ID_H263 &&
115 avctx->codec->id != AV_CODEC_ID_MPEG4)
116 if ((ret = ff_MPV_common_init(s)) < 0)
119 ff_h263dsp_init(&s->h263dsp);
120 ff_h263_decode_init_vlc();
125 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
127 MpegEncContext *s = avctx->priv_data;
129 ff_MPV_common_end(s);
134 * Return the number of bytes consumed for building the current frame.
136 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
138 int pos = (get_bits_count(&s->gb) + 7) >> 3;
140 if (s->divx_packed || s->avctx->hwaccel) {
141 /* We would have to scan through the whole buf to handle the weird
144 } else if (s->flags & CODEC_FLAG_TRUNCATED) {
145 pos -= s->parse_context.last_index;
146 // padding is not really read so this might be -1
151 // avoid infinite loops (maybe not needed...)
155 if (pos + 10 > buf_size)
162 static int decode_slice(MpegEncContext *s)
164 const int part_mask = s->partitioned_frame
165 ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
166 const int mb_size = 16;
169 s->last_resync_gb = s->gb;
170 s->first_slice_line = 1;
171 s->resync_mb_x = s->mb_x;
172 s->resync_mb_y = s->mb_y;
174 ff_set_qscale(s, s->qscale);
176 if (s->avctx->hwaccel) {
177 const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
178 const uint8_t *end = ff_h263_find_resync_marker(start + 1,
180 skip_bits_long(&s->gb, 8 * (end - start));
181 return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
184 if (s->partitioned_frame) {
185 const int qscale = s->qscale;
187 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
188 if ((ret = ff_mpeg4_decode_partitions(s)) < 0)
191 /* restore variables which were modified */
192 s->first_slice_line = 1;
193 s->mb_x = s->resync_mb_x;
194 s->mb_y = s->resync_mb_y;
195 ff_set_qscale(s, qscale);
198 for (; s->mb_y < s->mb_height; s->mb_y++) {
199 /* per-row end of slice checks */
200 if (s->msmpeg4_version) {
201 if (s->resync_mb_y + s->slice_height == s->mb_y) {
202 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
203 s->mb_x - 1, s->mb_y, ER_MB_END);
209 if (s->msmpeg4_version == 1) {
215 ff_init_block_index(s);
216 for (; s->mb_x < s->mb_width; s->mb_x++) {
219 ff_update_block_index(s);
221 if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
222 s->first_slice_line = 0;
226 s->mv_dir = MV_DIR_FORWARD;
227 s->mv_type = MV_TYPE_16X16;
228 // s->mb_skipped = 0;
229 av_dlog(s, "%d %d %06X\n",
230 ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
231 ret = s->decode_mb(s, s->block);
233 if (s->pict_type != AV_PICTURE_TYPE_B)
234 ff_h263_update_motion_val(s);
237 const int xy = s->mb_x + s->mb_y * s->mb_stride;
238 if (ret == SLICE_END) {
239 ff_MPV_decode_mb(s, s->block);
241 ff_h263_loop_filter(s);
243 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
244 s->mb_x, s->mb_y, ER_MB_END & part_mask);
246 s->padding_bug_score--;
248 if (++s->mb_x >= s->mb_width) {
250 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
251 ff_MPV_report_decode_progress(s);
255 } else if (ret == SLICE_NOEND) {
256 av_log(s->avctx, AV_LOG_ERROR,
257 "Slice mismatch at MB: %d\n", xy);
258 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
259 s->mb_x + 1, s->mb_y,
260 ER_MB_END & part_mask);
261 return AVERROR_INVALIDDATA;
263 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
264 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
265 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
267 return AVERROR_INVALIDDATA;
270 ff_MPV_decode_mb(s, s->block);
272 ff_h263_loop_filter(s);
275 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
276 ff_MPV_report_decode_progress(s);
281 assert(s->mb_x == 0 && s->mb_y == s->mb_height);
283 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
284 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
285 get_bits_left(&s->gb) >= 48 &&
286 show_bits(&s->gb, 24) == 0x4010 &&
287 !s->data_partitioning)
288 s->padding_bug_score += 32;
290 /* try to detect the padding bug */
291 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
292 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
293 get_bits_left(&s->gb) >= 0 &&
294 get_bits_left(&s->gb) < 48 &&
295 // !s->resync_marker &&
296 !s->data_partitioning) {
297 const int bits_count = get_bits_count(&s->gb);
298 const int bits_left = s->gb.size_in_bits - bits_count;
300 if (bits_left == 0) {
301 s->padding_bug_score += 16;
302 } else if (bits_left != 1) {
303 int v = show_bits(&s->gb, 8);
304 v |= 0x7F >> (7 - (bits_count & 7));
306 if (v == 0x7F && bits_left <= 8)
307 s->padding_bug_score--;
308 else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
310 s->padding_bug_score += 4;
312 s->padding_bug_score++;
316 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
317 if (s->padding_bug_score > -2 && !s->data_partitioning
318 /* && (s->divx_version >= 0 || !s->resync_marker) */)
319 s->workaround_bugs |= FF_BUG_NO_PADDING;
321 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
324 // handle formats which don't have unique end markers
325 if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
326 int left = get_bits_left(&s->gb);
329 /* no markers in M$ crap */
330 if (s->msmpeg4_version && s->pict_type == AV_PICTURE_TYPE_I)
333 /* buggy padding but the frame should still end approximately at
334 * the bitstream end */
335 if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
336 (s->err_recognition & AV_EF_BUFFER))
338 else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
339 max_extra += 256 * 256 * 256 * 64;
341 if (left > max_extra)
342 av_log(s->avctx, AV_LOG_ERROR,
343 "discarding %d junk bits at end, next would be %X\n",
344 left, show_bits(&s->gb, 24));
346 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
348 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
349 s->mb_x - 1, s->mb_y, ER_MB_END);
354 av_log(s->avctx, AV_LOG_ERROR,
355 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
356 get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
358 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
359 ER_MB_END & part_mask);
361 return AVERROR_INVALIDDATA;
364 int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
367 const uint8_t *buf = avpkt->data;
368 int buf_size = avpkt->size;
369 MpegEncContext *s = avctx->priv_data;
371 AVFrame *pict = data;
373 s->flags = avctx->flags;
374 s->flags2 = avctx->flags2;
376 /* no supplementary picture */
378 /* special case for last picture */
379 if (s->low_delay == 0 && s->next_picture_ptr) {
380 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
382 s->next_picture_ptr = NULL;
390 if (s->flags & CODEC_FLAG_TRUNCATED) {
393 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
394 next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
395 } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
396 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
398 av_log(s->avctx, AV_LOG_ERROR,
399 "this codec does not support truncated bitstreams\n");
400 return AVERROR(ENOSYS);
403 if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
408 if (s->bitstream_buffer_size && (s->divx_packed || buf_size < 20)) // divx 5.01+/xvid frame reorder
409 ret = init_get_bits8(&s->gb, s->bitstream_buffer,
410 s->bitstream_buffer_size);
412 ret = init_get_bits8(&s->gb, buf, buf_size);
413 s->bitstream_buffer_size = 0;
418 if (!s->context_initialized)
419 // we need the idct permutaton for reading a custom matrix
420 if ((ret = ff_MPV_common_init(s)) < 0)
423 /* We need to set current_picture_ptr before reading the header,
424 * otherwise we cannot store anyting in there */
425 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
426 int i = ff_find_unused_picture(s, 0);
429 s->current_picture_ptr = &s->picture[i];
433 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
434 ret = ff_wmv2_decode_picture_header(s);
435 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
436 ret = ff_msmpeg4_decode_picture_header(s);
437 } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
438 if (s->avctx->extradata_size && s->picture_number == 0) {
441 ret = init_get_bits8(&gb, s->avctx->extradata,
442 s->avctx->extradata_size);
445 ff_mpeg4_decode_picture_header(s, &gb);
447 ret = ff_mpeg4_decode_picture_header(s, &s->gb);
448 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
449 ret = ff_intel_h263_decode_picture_header(s);
450 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
451 ret = ff_flv_decode_picture_header(s);
453 ret = ff_h263_decode_picture_header(s);
456 if (ret == FRAME_SKIPPED)
457 return get_consumed_bytes(s, buf_size);
459 /* skip if the header was thrashed */
461 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
465 avctx->has_b_frames = !s->low_delay;
467 if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1) {
468 if (s->stream_codec_tag == AV_RL32("XVID") ||
469 s->codec_tag == AV_RL32("XVID") ||
470 s->codec_tag == AV_RL32("XVIX") ||
471 s->codec_tag == AV_RL32("RMP4") ||
472 s->codec_tag == AV_RL32("ZMP4") ||
473 s->codec_tag == AV_RL32("SIPP"))
476 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
477 s->vol_control_parameters == 1 &&
478 s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
483 if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1)
484 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
485 s->vol_control_parameters == 0)
486 s->divx_version = 400; // divx 4
488 if (s->xvid_build >= 0 && s->divx_version >= 0) {
493 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
494 if (s->codec_tag == AV_RL32("XVIX"))
495 s->workaround_bugs |= FF_BUG_XVID_ILACE;
497 if (s->codec_tag == AV_RL32("UMP4"))
498 s->workaround_bugs |= FF_BUG_UMP4;
500 if (s->divx_version >= 500 && s->divx_build < 1814)
501 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
503 if (s->divx_version > 502 && s->divx_build < 1814)
504 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
506 if (s->xvid_build <= 3U)
507 s->padding_bug_score = 256 * 256 * 256 * 64;
509 if (s->xvid_build <= 1U)
510 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
512 if (s->xvid_build <= 12U)
513 s->workaround_bugs |= FF_BUG_EDGE;
515 if (s->xvid_build <= 32U)
516 s->workaround_bugs |= FF_BUG_DC_CLIP;
518 #define SET_QPEL_FUNC(postfix1, postfix2) \
519 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2; \
520 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
521 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
523 if (s->lavc_build < 4653U)
524 s->workaround_bugs |= FF_BUG_STD_QPEL;
526 if (s->lavc_build < 4655U)
527 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
529 if (s->lavc_build < 4670U)
530 s->workaround_bugs |= FF_BUG_EDGE;
532 if (s->lavc_build <= 4712U)
533 s->workaround_bugs |= FF_BUG_DC_CLIP;
535 if (s->divx_version >= 0)
536 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
537 if (s->divx_version == 501 && s->divx_build == 20020416)
538 s->padding_bug_score = 256 * 256 * 256 * 64;
540 if (s->divx_version < 500U)
541 s->workaround_bugs |= FF_BUG_EDGE;
543 if (s->divx_version >= 0)
544 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
546 if (s->divx_version == 500)
547 s->padding_bug_score = 256 * 256 * 256 * 64;
549 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
550 * Let us hope this at least works. */
551 if (s->resync_marker == 0 && s->data_partitioning == 0 &&
552 s->divx_version == -1 && s->codec_id == AV_CODEC_ID_MPEG4 &&
554 s->workaround_bugs |= FF_BUG_NO_PADDING;
556 // FIXME not sure about the version num but a 4609 file seems ok
557 if (s->lavc_build < 4609U)
558 s->workaround_bugs |= FF_BUG_NO_PADDING;
562 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
563 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
564 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
565 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
566 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
567 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
568 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
570 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
571 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
572 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
573 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
574 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
575 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
578 if (avctx->debug & FF_DEBUG_BUGS)
579 av_log(s->avctx, AV_LOG_DEBUG,
580 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
581 s->workaround_bugs, s->lavc_build, s->xvid_build,
582 s->divx_version, s->divx_build, s->divx_packed ? "p" : "");
585 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build >= 0 &&
586 avctx->idct_algo == FF_IDCT_AUTO &&
587 (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
588 avctx->idct_algo = FF_IDCT_XVIDMMX;
589 ff_dct_common_init(s);
590 s->picture_number = 0;
594 /* After H263 & mpeg4 header decode we have the height, width,
595 * and other parameters. So then we could init the picture.
596 * FIXME: By the way H263 decoder is evolving it should have
597 * an H263EncContext */
598 if (s->width != avctx->coded_width ||
599 s->height != avctx->coded_height ||
601 /* H.263 could change picture size any time */
602 s->context_reinit = 0;
604 ret = ff_set_dimensions(avctx, s->width, s->height);
608 if ((ret = ff_MPV_common_frame_size_change(s)))
612 if (s->codec_id == AV_CODEC_ID_H263 ||
613 s->codec_id == AV_CODEC_ID_H263P ||
614 s->codec_id == AV_CODEC_ID_H263I)
615 s->gob_index = ff_h263_get_gob_height(s);
617 // for skipping the frame
618 s->current_picture.f.pict_type = s->pict_type;
619 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
621 /* skip B-frames if we don't have reference frames */
622 if (s->last_picture_ptr == NULL &&
623 (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
624 return get_consumed_bytes(s, buf_size);
625 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
626 s->pict_type == AV_PICTURE_TYPE_B) ||
627 (avctx->skip_frame >= AVDISCARD_NONKEY &&
628 s->pict_type != AV_PICTURE_TYPE_I) ||
629 avctx->skip_frame >= AVDISCARD_ALL)
630 return get_consumed_bytes(s, buf_size);
632 if (s->next_p_frame_damaged) {
633 if (s->pict_type == AV_PICTURE_TYPE_B)
634 return get_consumed_bytes(s, buf_size);
636 s->next_p_frame_damaged = 0;
639 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
640 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
641 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
643 s->me.qpel_put = s->dsp.put_no_rnd_qpel_pixels_tab;
644 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
647 if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
650 if (!s->divx_packed && !avctx->hwaccel)
651 ff_thread_finish_setup(avctx);
653 if (avctx->hwaccel) {
654 ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
655 s->gb.buffer_end - s->gb.buffer);
660 ff_mpeg_er_frame_start(s);
662 /* the second part of the wmv2 header contains the MB skip bits which
663 * are stored in current_picture->mb_type which is not available before
664 * ff_MPV_frame_start() */
665 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
666 ret = ff_wmv2_decode_secondary_picture_header(s);
670 goto intrax8_decoded;
673 /* decode each macroblock */
677 ret = decode_slice(s);
678 while (s->mb_y < s->mb_height) {
679 if (s->msmpeg4_version) {
680 if (s->slice_height == 0 || s->mb_x != 0 ||
681 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
684 int prev_x = s->mb_x, prev_y = s->mb_y;
685 if (ff_h263_resync(s) < 0)
687 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
688 s->er.error_occurred = 1;
691 if (s->msmpeg4_version < 4 && s->h263_pred)
692 ff_mpeg4_clean_buffers(s);
694 if (decode_slice(s) < 0)
695 ret = AVERROR_INVALIDDATA;
698 if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
699 s->pict_type == AV_PICTURE_TYPE_I)
700 if (!CONFIG_MSMPEG4_DECODER ||
701 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
702 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
704 assert(s->bitstream_buffer_size == 0);
705 /* divx 5.01+ bistream reorder stuff */
706 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->divx_packed) {
707 int current_pos = get_bits_count(&s->gb) >> 3;
708 int startcode_found = 0;
710 if (buf_size - current_pos > 5) {
712 for (i = current_pos; i < buf_size - 3; i++)
716 buf[i + 3] == 0xB6) {
721 if (s->gb.buffer == s->bitstream_buffer && buf_size > 7 &&
722 s->xvid_build >= 0) { // xvid style
727 if (startcode_found) {
728 av_fast_malloc(&s->bitstream_buffer,
729 &s->allocated_bitstream_buffer_size,
730 buf_size - current_pos +
731 FF_INPUT_BUFFER_PADDING_SIZE);
732 if (!s->bitstream_buffer)
733 return AVERROR(ENOMEM);
734 memcpy(s->bitstream_buffer, buf + current_pos,
735 buf_size - current_pos);
736 s->bitstream_buffer_size = buf_size - current_pos;
741 ff_er_frame_end(&s->er);
743 if (avctx->hwaccel) {
744 ret = avctx->hwaccel->end_frame(avctx);
751 if (!s->divx_packed && avctx->hwaccel)
752 ff_thread_finish_setup(avctx);
754 assert(s->current_picture.f.pict_type ==
755 s->current_picture_ptr->f.pict_type);
756 assert(s->current_picture.f.pict_type == s->pict_type);
757 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
758 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
760 ff_print_debug_info(s, s->current_picture_ptr);
761 } else if (s->last_picture_ptr != NULL) {
762 if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
764 ff_print_debug_info(s, s->last_picture_ptr);
767 if (s->last_picture_ptr || s->low_delay)
770 if (ret && (avctx->err_recognition & AV_EF_EXPLODE))
773 return get_consumed_bytes(s, buf_size);
776 const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
778 AV_PIX_FMT_VAAPI_VLD,
787 AVCodec ff_h263_decoder = {
789 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
790 .type = AVMEDIA_TYPE_VIDEO,
791 .id = AV_CODEC_ID_H263,
792 .priv_data_size = sizeof(MpegEncContext),
793 .init = ff_h263_decode_init,
794 .close = ff_h263_decode_end,
795 .decode = ff_h263_decode_frame,
796 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
797 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
798 .flush = ff_mpeg_flush,
799 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,