3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
28 #define UNCHECKED_BITSTREAM_READER 1
30 #include "libavutil/cpu.h"
32 #include "error_resilience.h"
35 #include "h263_parser.h"
37 #include "mpeg4video.h"
38 #include "mpeg4video_parser.h"
39 #include "mpegvideo.h"
41 #include "vdpau_internal.h"
44 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
46 MpegEncContext *s = avctx->priv_data;
50 s->out_format = FMT_H263;
51 s->width = avctx->coded_width;
52 s->height = avctx->coded_height;
53 s->workaround_bugs = avctx->workaround_bugs;
56 ff_MPV_decode_defaults(s);
57 s->quant_precision = 5;
58 s->decode_mb = ff_h263_decode_mb;
60 if (avctx->codec->id == AV_CODEC_ID_MSS2)
61 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
63 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
64 s->unrestricted_mv = 1;
66 /* select sub codec */
67 switch (avctx->codec->id) {
68 case AV_CODEC_ID_H263:
69 case AV_CODEC_ID_H263P:
70 s->unrestricted_mv = 0;
71 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
73 case AV_CODEC_ID_MPEG4:
75 case AV_CODEC_ID_MSMPEG4V1:
77 s->msmpeg4_version = 1;
79 case AV_CODEC_ID_MSMPEG4V2:
81 s->msmpeg4_version = 2;
83 case AV_CODEC_ID_MSMPEG4V3:
85 s->msmpeg4_version = 3;
87 case AV_CODEC_ID_WMV1:
89 s->msmpeg4_version = 4;
91 case AV_CODEC_ID_WMV2:
93 s->msmpeg4_version = 5;
96 case AV_CODEC_ID_WMV3:
97 case AV_CODEC_ID_VC1IMAGE:
98 case AV_CODEC_ID_WMV3IMAGE:
99 case AV_CODEC_ID_MSS2:
101 s->msmpeg4_version = 6;
102 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
104 case AV_CODEC_ID_H263I:
106 case AV_CODEC_ID_FLV1:
110 av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
112 return AVERROR(ENOSYS);
114 s->codec_id = avctx->codec->id;
115 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
117 if (avctx->stream_codec_tag == AV_RL32("l263") && avctx->extradata_size == 56 && avctx->extradata[0] == 1)
120 /* for h263, we allocate the images after having read the header */
121 if (avctx->codec->id != AV_CODEC_ID_H263 &&
122 avctx->codec->id != AV_CODEC_ID_H263P &&
123 avctx->codec->id != AV_CODEC_ID_MPEG4)
124 if ((ret = ff_MPV_common_init(s)) < 0)
127 ff_h263_decode_init_vlc();
132 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
134 MpegEncContext *s = avctx->priv_data;
136 ff_MPV_common_end(s);
141 * Return the number of bytes consumed for building the current frame.
143 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
145 int pos = (get_bits_count(&s->gb) + 7) >> 3;
147 if (s->divx_packed || s->avctx->hwaccel) {
148 /* We would have to scan through the whole buf to handle the weird
151 } else if (s->flags & CODEC_FLAG_TRUNCATED) {
152 pos -= s->parse_context.last_index;
153 // padding is not really read so this might be -1
158 // avoid infinite loops (maybe not needed...)
162 if (pos + 10 > buf_size)
169 static int decode_slice(MpegEncContext *s)
171 const int part_mask = s->partitioned_frame
172 ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
173 const int mb_size = 16 >> s->avctx->lowres;
176 s->last_resync_gb = s->gb;
177 s->first_slice_line = 1;
178 s->resync_mb_x = s->mb_x;
179 s->resync_mb_y = s->mb_y;
181 ff_set_qscale(s, s->qscale);
183 if (s->avctx->hwaccel) {
184 const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
185 ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
186 // ensure we exit decode loop
187 s->mb_y = s->mb_height;
191 if (s->partitioned_frame) {
192 const int qscale = s->qscale;
194 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
195 if ((ret = ff_mpeg4_decode_partitions(s)) < 0)
198 /* restore variables which were modified */
199 s->first_slice_line = 1;
200 s->mb_x = s->resync_mb_x;
201 s->mb_y = s->resync_mb_y;
202 ff_set_qscale(s, qscale);
205 for (; s->mb_y < s->mb_height; s->mb_y++) {
206 /* per-row end of slice checks */
207 if (s->msmpeg4_version) {
208 if (s->resync_mb_y + s->slice_height == s->mb_y) {
209 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
210 s->mb_x - 1, s->mb_y, ER_MB_END);
216 if (s->msmpeg4_version == 1) {
222 ff_init_block_index(s);
223 for (; s->mb_x < s->mb_width; s->mb_x++) {
226 ff_update_block_index(s);
228 if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
229 s->first_slice_line = 0;
233 s->mv_dir = MV_DIR_FORWARD;
234 s->mv_type = MV_TYPE_16X16;
235 // s->mb_skipped = 0;
236 av_dlog(s, "%d %d %06X\n",
237 ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
238 ret = s->decode_mb(s, s->block);
240 if (s->pict_type != AV_PICTURE_TYPE_B)
241 ff_h263_update_motion_val(s);
244 const int xy = s->mb_x + s->mb_y * s->mb_stride;
245 if (ret == SLICE_END) {
246 ff_MPV_decode_mb(s, s->block);
248 ff_h263_loop_filter(s);
250 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
251 s->mb_x, s->mb_y, ER_MB_END & part_mask);
253 s->padding_bug_score--;
255 if (++s->mb_x >= s->mb_width) {
257 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
258 ff_MPV_report_decode_progress(s);
262 } else if (ret == SLICE_NOEND) {
263 av_log(s->avctx, AV_LOG_ERROR,
264 "Slice mismatch at MB: %d\n", xy);
265 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
266 s->mb_x + 1, s->mb_y,
267 ER_MB_END & part_mask);
268 return AVERROR_INVALIDDATA;
270 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
271 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
272 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
274 return AVERROR_INVALIDDATA;
277 ff_MPV_decode_mb(s, s->block);
279 ff_h263_loop_filter(s);
282 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
283 ff_MPV_report_decode_progress(s);
288 av_assert1(s->mb_x == 0 && s->mb_y == s->mb_height);
290 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
291 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
292 get_bits_left(&s->gb) >= 48 &&
293 show_bits(&s->gb, 24) == 0x4010 &&
294 !s->data_partitioning)
295 s->padding_bug_score += 32;
297 /* try to detect the padding bug */
298 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
299 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
300 get_bits_left(&s->gb) >= 0 &&
301 get_bits_left(&s->gb) < 137 &&
302 // !s->resync_marker &&
303 !s->data_partitioning) {
304 const int bits_count = get_bits_count(&s->gb);
305 const int bits_left = s->gb.size_in_bits - bits_count;
307 if (bits_left == 0) {
308 s->padding_bug_score += 16;
309 } else if (bits_left != 1) {
310 int v = show_bits(&s->gb, 8);
311 v |= 0x7F >> (7 - (bits_count & 7));
313 if (v == 0x7F && bits_left <= 8)
314 s->padding_bug_score--;
315 else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
317 s->padding_bug_score += 4;
319 s->padding_bug_score++;
323 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
324 if (s->padding_bug_score > -2 && !s->data_partitioning
325 /* && (s->divx_version >= 0 || !s->resync_marker) */)
326 s->workaround_bugs |= FF_BUG_NO_PADDING;
328 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
331 // handle formats which don't have unique end markers
332 if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
333 int left = get_bits_left(&s->gb);
336 /* no markers in M$ crap */
337 if (s->msmpeg4_version && s->pict_type == AV_PICTURE_TYPE_I)
340 /* buggy padding but the frame should still end approximately at
341 * the bitstream end */
342 if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
343 (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
345 else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
346 max_extra += 256 * 256 * 256 * 64;
348 if (left > max_extra)
349 av_log(s->avctx, AV_LOG_ERROR,
350 "discarding %d junk bits at end, next would be %X\n",
351 left, show_bits(&s->gb, 24));
353 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
355 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
356 s->mb_x - 1, s->mb_y, ER_MB_END);
361 av_log(s->avctx, AV_LOG_ERROR,
362 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
363 get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
365 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
366 ER_MB_END & part_mask);
368 return AVERROR_INVALIDDATA;
371 int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
374 const uint8_t *buf = avpkt->data;
375 int buf_size = avpkt->size;
376 MpegEncContext *s = avctx->priv_data;
379 AVFrame *pict = data;
381 s->flags = avctx->flags;
382 s->flags2 = avctx->flags2;
384 /* no supplementary picture */
386 /* special case for last picture */
387 if (s->low_delay == 0 && s->next_picture_ptr) {
388 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
390 s->next_picture_ptr = NULL;
398 if (s->flags & CODEC_FLAG_TRUNCATED) {
401 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
402 next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
403 } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
404 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
405 } else if (CONFIG_H263P_DECODER && s->codec_id == AV_CODEC_ID_H263P) {
406 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
408 av_log(s->avctx, AV_LOG_ERROR,
409 "this codec does not support truncated bitstreams\n");
410 return AVERROR(ENOSYS);
413 if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
419 if (s->divx_packed && s->bitstream_buffer_size) {
421 for(i=0; i < buf_size-3; i++) {
422 if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) {
423 if (buf[i+3]==0xB0) {
424 av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
425 s->bitstream_buffer_size = 0;
432 if (s->bitstream_buffer_size && (s->divx_packed || buf_size < 20)) // divx 5.01+/xvid frame reorder
433 ret = init_get_bits8(&s->gb, s->bitstream_buffer,
434 s->bitstream_buffer_size);
436 ret = init_get_bits8(&s->gb, buf, buf_size);
438 s->bitstream_buffer_size = 0;
442 if (!s->context_initialized)
443 // we need the idct permutaton for reading a custom matrix
444 if ((ret = ff_MPV_common_init(s)) < 0)
447 /* We need to set current_picture_ptr before reading the header,
448 * otherwise we cannot store anyting in there */
449 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
450 int i = ff_find_unused_picture(s, 0);
453 s->current_picture_ptr = &s->picture[i];
457 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
458 ret = ff_wmv2_decode_picture_header(s);
459 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
460 ret = ff_msmpeg4_decode_picture_header(s);
461 } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
462 if (s->avctx->extradata_size && s->picture_number == 0) {
465 if (init_get_bits8(&gb, s->avctx->extradata, s->avctx->extradata_size) >= 0 )
466 ff_mpeg4_decode_picture_header(s, &gb);
468 ret = ff_mpeg4_decode_picture_header(s, &s->gb);
469 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
470 ret = ff_intel_h263_decode_picture_header(s);
471 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
472 ret = ff_flv_decode_picture_header(s);
474 ret = ff_h263_decode_picture_header(s);
477 if (ret < 0 || ret == FRAME_SKIPPED) {
478 if ( s->width != avctx->coded_width
479 || s->height != avctx->coded_height) {
480 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
481 s->width = avctx->coded_width;
482 s->height= avctx->coded_height;
485 if (ret == FRAME_SKIPPED)
486 return get_consumed_bytes(s, buf_size);
488 /* skip if the header was thrashed */
490 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
494 avctx->has_b_frames = !s->low_delay;
496 if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1) {
497 if (s->stream_codec_tag == AV_RL32("XVID") ||
498 s->codec_tag == AV_RL32("XVID") ||
499 s->codec_tag == AV_RL32("XVIX") ||
500 s->codec_tag == AV_RL32("RMP4") ||
501 s->codec_tag == AV_RL32("ZMP4") ||
502 s->codec_tag == AV_RL32("SIPP"))
505 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
506 s->vol_control_parameters == 1 &&
507 s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
512 if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1)
513 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
514 s->vol_control_parameters == 0)
515 s->divx_version = 400; // divx 4
517 if (s->xvid_build >= 0 && s->divx_version >= 0) {
522 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
523 if (s->codec_tag == AV_RL32("XVIX"))
524 s->workaround_bugs |= FF_BUG_XVID_ILACE;
526 if (s->codec_tag == AV_RL32("UMP4"))
527 s->workaround_bugs |= FF_BUG_UMP4;
529 if (s->divx_version >= 500 && s->divx_build < 1814)
530 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
532 if (s->divx_version > 502 && s->divx_build < 1814)
533 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
535 if (s->xvid_build <= 3U)
536 s->padding_bug_score = 256 * 256 * 256 * 64;
538 if (s->xvid_build <= 1U)
539 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
541 if (s->xvid_build <= 12U)
542 s->workaround_bugs |= FF_BUG_EDGE;
544 if (s->xvid_build <= 32U)
545 s->workaround_bugs |= FF_BUG_DC_CLIP;
547 #define SET_QPEL_FUNC(postfix1, postfix2) \
548 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2; \
549 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
550 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
552 if (s->lavc_build < 4653U)
553 s->workaround_bugs |= FF_BUG_STD_QPEL;
555 if (s->lavc_build < 4655U)
556 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
558 if (s->lavc_build < 4670U)
559 s->workaround_bugs |= FF_BUG_EDGE;
561 if (s->lavc_build <= 4712U)
562 s->workaround_bugs |= FF_BUG_DC_CLIP;
564 if (s->divx_version >= 0)
565 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
566 if (s->divx_version == 501 && s->divx_build == 20020416)
567 s->padding_bug_score = 256 * 256 * 256 * 64;
569 if (s->divx_version < 500U)
570 s->workaround_bugs |= FF_BUG_EDGE;
572 if (s->divx_version >= 0)
573 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
575 if (s->divx_version == 500)
576 s->padding_bug_score = 256 * 256 * 256 * 64;
578 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
579 * Let us hope this at least works. */
580 if (s->resync_marker == 0 && s->data_partitioning == 0 &&
581 s->divx_version == -1 && s->codec_id == AV_CODEC_ID_MPEG4 &&
583 s->workaround_bugs |= FF_BUG_NO_PADDING;
585 // FIXME not sure about the version num but a 4609 file seems ok
586 if (s->lavc_build < 4609U)
587 s->workaround_bugs |= FF_BUG_NO_PADDING;
591 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
592 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
593 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
594 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
595 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
596 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
597 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
599 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
600 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
601 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
602 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
603 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
604 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
607 if (avctx->debug & FF_DEBUG_BUGS)
608 av_log(s->avctx, AV_LOG_DEBUG,
609 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
610 s->workaround_bugs, s->lavc_build, s->xvid_build,
611 s->divx_version, s->divx_build, s->divx_packed ? "p" : "");
614 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build >= 0 &&
615 avctx->idct_algo == FF_IDCT_AUTO &&
616 (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
617 avctx->idct_algo = FF_IDCT_XVIDMMX;
618 ff_dct_common_init(s);
623 /* After H263 & mpeg4 header decode we have the height, width,
624 * and other parameters. So then we could init the picture.
625 * FIXME: By the way H263 decoder is evolving it should have
626 * an H263EncContext */
627 if (s->width != avctx->coded_width ||
628 s->height != avctx->coded_height ||
630 /* H.263 could change picture size any time */
631 s->context_reinit = 0;
633 ret = ff_set_dimensions(avctx, s->width, s->height);
637 if ((ret = ff_MPV_common_frame_size_change(s)))
641 if (s->codec_id == AV_CODEC_ID_H263 ||
642 s->codec_id == AV_CODEC_ID_H263P ||
643 s->codec_id == AV_CODEC_ID_H263I)
644 s->gob_index = ff_h263_get_gob_height(s);
646 // for skipping the frame
647 s->current_picture.f.pict_type = s->pict_type;
648 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
650 /* skip B-frames if we don't have reference frames */
651 if (s->last_picture_ptr == NULL &&
652 (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
653 return get_consumed_bytes(s, buf_size);
654 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
655 s->pict_type == AV_PICTURE_TYPE_B) ||
656 (avctx->skip_frame >= AVDISCARD_NONKEY &&
657 s->pict_type != AV_PICTURE_TYPE_I) ||
658 avctx->skip_frame >= AVDISCARD_ALL)
659 return get_consumed_bytes(s, buf_size);
661 if (s->next_p_frame_damaged) {
662 if (s->pict_type == AV_PICTURE_TYPE_B)
663 return get_consumed_bytes(s, buf_size);
665 s->next_p_frame_damaged = 0;
668 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
669 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
670 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
672 s->me.qpel_put = s->dsp.put_no_rnd_qpel_pixels_tab;
673 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
676 if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
679 if (!s->divx_packed && !avctx->hwaccel)
680 ff_thread_finish_setup(avctx);
682 if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
683 ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
687 if (avctx->hwaccel) {
688 ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
689 s->gb.buffer_end - s->gb.buffer);
694 ff_mpeg_er_frame_start(s);
696 /* the second part of the wmv2 header contains the MB skip bits which
697 * are stored in current_picture->mb_type which is not available before
698 * ff_MPV_frame_start() */
699 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
700 ret = ff_wmv2_decode_secondary_picture_header(s);
707 /* decode each macroblock */
711 slice_ret = decode_slice(s);
712 while (s->mb_y < s->mb_height) {
713 if (s->msmpeg4_version) {
714 if (s->slice_height == 0 || s->mb_x != 0 ||
715 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
718 int prev_x = s->mb_x, prev_y = s->mb_y;
719 if (ff_h263_resync(s) < 0)
721 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
722 s->er.error_occurred = 1;
725 if (s->msmpeg4_version < 4 && s->h263_pred)
726 ff_mpeg4_clean_buffers(s);
728 if (decode_slice(s) < 0)
729 slice_ret = AVERROR_INVALIDDATA;
732 if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
733 s->pict_type == AV_PICTURE_TYPE_I)
734 if (!CONFIG_MSMPEG4_DECODER ||
735 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
736 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
738 av_assert1(s->bitstream_buffer_size == 0);
740 ff_er_frame_end(&s->er);
742 if (avctx->hwaccel) {
743 ret = avctx->hwaccel->end_frame(avctx);
750 /* divx 5.01+ bitstream reorder stuff */
751 /* Since this clobbers the input buffer and hwaccel codecs still need the
752 * data during hwaccel->end_frame we should not do this any earlier */
753 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->divx_packed) {
754 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
755 int startcode_found = 0;
757 if (buf_size - current_pos > 7) {
759 for (i = current_pos; i < buf_size - 4; i++)
763 buf[i + 3] == 0xB6) {
764 startcode_found = !(buf[i + 4] & 0x40);
769 if (startcode_found) {
770 av_fast_malloc(&s->bitstream_buffer,
771 &s->allocated_bitstream_buffer_size,
772 buf_size - current_pos +
773 FF_INPUT_BUFFER_PADDING_SIZE);
774 if (!s->bitstream_buffer)
775 return AVERROR(ENOMEM);
776 memcpy(s->bitstream_buffer, buf + current_pos,
777 buf_size - current_pos);
778 s->bitstream_buffer_size = buf_size - current_pos;
782 if (!s->divx_packed && avctx->hwaccel)
783 ff_thread_finish_setup(avctx);
785 av_assert1(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
786 av_assert1(s->current_picture.f.pict_type == s->pict_type);
787 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
788 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
790 ff_print_debug_info(s, s->current_picture_ptr, pict);
791 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
792 } else if (s->last_picture_ptr != NULL) {
793 if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
795 ff_print_debug_info(s, s->last_picture_ptr, pict);
796 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
799 if (s->last_picture_ptr || s->low_delay) {
800 if ( pict->format == AV_PIX_FMT_YUV420P
801 && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) {
803 av_frame_make_writable(pict);
804 for (p=0; p<3; p++) {
805 int w = FF_CEIL_RSHIFT(pict-> width, !!p);
806 int h = FF_CEIL_RSHIFT(pict->height, !!p);
807 int linesize = pict->linesize[p];
808 for (y=0; y<(h>>1); y++)
811 pict->data[p][x + y*linesize],
812 pict->data[p][x + (h-1-y)*linesize]);
818 if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
821 return get_consumed_bytes(s, buf_size);
824 const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
826 AV_PIX_FMT_VAAPI_VLD,
835 AVCodec ff_h263_decoder = {
837 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
838 .type = AVMEDIA_TYPE_VIDEO,
839 .id = AV_CODEC_ID_H263,
840 .priv_data_size = sizeof(MpegEncContext),
841 .init = ff_h263_decode_init,
842 .close = ff_h263_decode_end,
843 .decode = ff_h263_decode_frame,
844 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
845 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
846 .flush = ff_mpeg_flush,
848 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
851 AVCodec ff_h263p_decoder = {
853 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
854 .type = AVMEDIA_TYPE_VIDEO,
855 .id = AV_CODEC_ID_H263P,
856 .priv_data_size = sizeof(MpegEncContext),
857 .init = ff_h263_decode_init,
858 .close = ff_h263_decode_end,
859 .decode = ff_h263_decode_frame,
860 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
861 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
862 .flush = ff_mpeg_flush,
864 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,