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
24 * @file libavcodec/h263dec.c
31 #include "mpegvideo.h"
32 #include "h263_parser.h"
33 #include "mpeg4video_parser.h"
35 #include "vdpau_internal.h"
38 //#define PRINT_FRAME_TIME
40 av_cold
int ff_h263_decode_init(AVCodecContext
*avctx
)
42 MpegEncContext
*s
= avctx
->priv_data
;
45 s
->out_format
= FMT_H263
;
47 s
->width
= avctx
->coded_width
;
48 s
->height
= avctx
->coded_height
;
49 s
->workaround_bugs
= avctx
->workaround_bugs
;
52 MPV_decode_defaults(s
);
54 s
->decode_mb
= ff_h263_decode_mb
;
56 avctx
->pix_fmt
= avctx
->get_format(avctx
, avctx
->codec
->pix_fmts
);
57 s
->unrestricted_mv
= 1;
59 /* select sub codec */
60 switch(avctx
->codec
->id
) {
62 s
->unrestricted_mv
= 0;
63 avctx
->chroma_sample_location
= AVCHROMA_LOC_CENTER
;
66 s
->decode_mb
= ff_mpeg4_decode_mb
;
67 s
->time_increment_bits
= 4; /* default value for broken headers */
69 s
->low_delay
= 0; //default, might be overriden in the vol header during header parsing
70 avctx
->chroma_sample_location
= AVCHROMA_LOC_LEFT
;
72 case CODEC_ID_MSMPEG4V1
:
77 case CODEC_ID_MSMPEG4V2
:
82 case CODEC_ID_MSMPEG4V3
:
101 s
->msmpeg4_version
=6;
102 avctx
->chroma_sample_location
= AVCHROMA_LOC_LEFT
;
112 s
->codec_id
= avctx
->codec
->id
;
113 avctx
->hwaccel
= ff_find_hwaccel(avctx
->codec
->id
, avctx
->pix_fmt
);
115 /* for h263, we allocate the images after having read the header */
116 if (avctx
->codec
->id
!= CODEC_ID_H263
&& avctx
->codec
->id
!= CODEC_ID_MPEG4
)
117 if (MPV_common_init(s
) < 0)
120 if (CONFIG_MSMPEG4_DECODER
&& s
->h263_msmpeg4
)
121 ff_msmpeg4_decode_init(s
);
123 h263_decode_init_vlc(s
);
128 av_cold
int ff_h263_decode_end(AVCodecContext
*avctx
)
130 MpegEncContext
*s
= avctx
->priv_data
;
137 * returns the number of bytes consumed for building the current frame
139 static int get_consumed_bytes(MpegEncContext
*s
, int buf_size
){
140 int pos
= (get_bits_count(&s
->gb
)+7)>>3;
142 if(s
->divx_packed
|| s
->avctx
->hwaccel
){
143 //we would have to scan through the whole buf to handle the weird reordering ...
145 }else if(s
->flags
&CODEC_FLAG_TRUNCATED
){
146 pos
-= s
->parse_context
.last_index
;
147 if(pos
<0) pos
=0; // padding is not really read so this might be -1
150 if(pos
==0) pos
=1; //avoid infinite loops (i doubt that is needed but ...)
151 if(pos
+10>buf_size
) pos
=buf_size
; // oops ;)
157 static int decode_slice(MpegEncContext
*s
){
158 const int part_mask
= s
->partitioned_frame
? (AC_END
|AC_ERROR
) : 0x7F;
159 const int mb_size
= 16>>s
->avctx
->lowres
;
160 s
->last_resync_gb
= s
->gb
;
161 s
->first_slice_line
= 1;
163 s
->resync_mb_x
= s
->mb_x
;
164 s
->resync_mb_y
= s
->mb_y
;
166 ff_set_qscale(s
, s
->qscale
);
168 if (s
->avctx
->hwaccel
) {
169 const uint8_t *start
= s
->gb
.buffer
+ get_bits_count(&s
->gb
)/8;
170 const uint8_t *end
= ff_h263_find_resync_marker(start
+ 1, s
->gb
.buffer_end
);
171 skip_bits_long(&s
->gb
, 8*(end
- start
));
172 return s
->avctx
->hwaccel
->decode_slice(s
->avctx
, start
, end
- start
);
175 if(s
->partitioned_frame
){
176 const int qscale
= s
->qscale
;
178 if(s
->codec_id
==CODEC_ID_MPEG4
){
179 if(ff_mpeg4_decode_partitions(s
) < 0)
183 /* restore variables which were modified */
184 s
->first_slice_line
=1;
185 s
->mb_x
= s
->resync_mb_x
;
186 s
->mb_y
= s
->resync_mb_y
;
187 ff_set_qscale(s
, qscale
);
190 for(; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
191 /* per-row end of slice checks */
192 if(s
->msmpeg4_version
){
193 if(s
->resync_mb_y
+ s
->slice_height
== s
->mb_y
){
194 ff_er_add_slice(s
, s
->resync_mb_x
, s
->resync_mb_y
, s
->mb_x
-1, s
->mb_y
, AC_END
|DC_END
|MV_END
);
200 if(s
->msmpeg4_version
==1){
206 ff_init_block_index(s
);
207 for(; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
210 ff_update_block_index(s
);
212 if(s
->resync_mb_x
== s
->mb_x
&& s
->resync_mb_y
+1 == s
->mb_y
){
213 s
->first_slice_line
=0;
218 s
->mv_dir
= MV_DIR_FORWARD
;
219 s
->mv_type
= MV_TYPE_16X16
;
220 // s->mb_skipped = 0;
221 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
222 ret
= s
->decode_mb(s
, s
->block
);
224 if (s
->pict_type
!=FF_B_TYPE
)
225 ff_h263_update_motion_val(s
);
228 const int xy
= s
->mb_x
+ s
->mb_y
*s
->mb_stride
;
230 MPV_decode_mb(s
, s
->block
);
232 ff_h263_loop_filter(s
);
234 //printf("%d %d %d %06X\n", s->mb_x, s->mb_y, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24));
235 ff_er_add_slice(s
, s
->resync_mb_x
, s
->resync_mb_y
, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
)&part_mask
);
237 s
->padding_bug_score
--;
239 if(++s
->mb_x
>= s
->mb_width
){
241 ff_draw_horiz_band(s
, s
->mb_y
*mb_size
, mb_size
);
245 }else if(ret
==SLICE_NOEND
){
246 av_log(s
->avctx
, AV_LOG_ERROR
, "Slice mismatch at MB: %d\n", xy
);
247 ff_er_add_slice(s
, s
->resync_mb_x
, s
->resync_mb_y
, s
->mb_x
+1, s
->mb_y
, (AC_END
|DC_END
|MV_END
)&part_mask
);
250 av_log(s
->avctx
, AV_LOG_ERROR
, "Error at MB: %d\n", xy
);
251 ff_er_add_slice(s
, s
->resync_mb_x
, s
->resync_mb_y
, s
->mb_x
, s
->mb_y
, (AC_ERROR
|DC_ERROR
|MV_ERROR
)&part_mask
);
256 MPV_decode_mb(s
, s
->block
);
258 ff_h263_loop_filter(s
);
261 ff_draw_horiz_band(s
, s
->mb_y
*mb_size
, mb_size
);
266 assert(s
->mb_x
==0 && s
->mb_y
==s
->mb_height
);
268 /* try to detect the padding bug */
269 if( s
->codec_id
==CODEC_ID_MPEG4
270 && (s
->workaround_bugs
&FF_BUG_AUTODETECT
)
271 && get_bits_left(&s
->gb
) >=0
272 && get_bits_left(&s
->gb
) < 48
273 // && !s->resync_marker
274 && !s
->data_partitioning
){
276 const int bits_count
= get_bits_count(&s
->gb
);
277 const int bits_left
= s
->gb
.size_in_bits
- bits_count
;
280 s
->padding_bug_score
+=16;
281 } else if(bits_left
!= 1){
282 int v
= show_bits(&s
->gb
, 8);
283 v
|= 0x7F >> (7-(bits_count
&7));
285 if(v
==0x7F && bits_left
<=8)
286 s
->padding_bug_score
--;
287 else if(v
==0x7F && ((get_bits_count(&s
->gb
)+8)&8) && bits_left
<=16)
288 s
->padding_bug_score
+= 4;
290 s
->padding_bug_score
++;
294 if(s
->workaround_bugs
&FF_BUG_AUTODETECT
){
295 if(s
->padding_bug_score
> -2 && !s
->data_partitioning
/*&& (s->divx_version || !s->resync_marker)*/)
296 s
->workaround_bugs
|= FF_BUG_NO_PADDING
;
298 s
->workaround_bugs
&= ~FF_BUG_NO_PADDING
;
301 // handle formats which don't have unique end markers
302 if(s
->msmpeg4_version
|| (s
->workaround_bugs
&FF_BUG_NO_PADDING
)){ //FIXME perhaps solve this more cleanly
303 int left
= get_bits_left(&s
->gb
);
306 /* no markers in M$ crap */
307 if(s
->msmpeg4_version
&& s
->pict_type
==FF_I_TYPE
)
310 /* buggy padding but the frame should still end approximately at the bitstream end */
311 if((s
->workaround_bugs
&FF_BUG_NO_PADDING
) && s
->error_recognition
>=3)
313 else if((s
->workaround_bugs
&FF_BUG_NO_PADDING
))
314 max_extra
+= 256*256*256*64;
317 av_log(s
->avctx
, AV_LOG_ERROR
, "discarding %d junk bits at end, next would be %X\n", left
, show_bits(&s
->gb
, 24));
320 av_log(s
->avctx
, AV_LOG_ERROR
, "overreading %d bits\n", -left
);
322 ff_er_add_slice(s
, s
->resync_mb_x
, s
->resync_mb_y
, s
->mb_x
-1, s
->mb_y
, AC_END
|DC_END
|MV_END
);
327 av_log(s
->avctx
, AV_LOG_ERROR
, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
328 get_bits_left(&s
->gb
),
329 show_bits(&s
->gb
, 24), s
->padding_bug_score
);
331 ff_er_add_slice(s
, s
->resync_mb_x
, s
->resync_mb_y
, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
)&part_mask
);
336 int ff_h263_decode_frame(AVCodecContext
*avctx
,
337 void *data
, int *data_size
,
340 const uint8_t *buf
= avpkt
->data
;
341 int buf_size
= avpkt
->size
;
342 MpegEncContext
*s
= avctx
->priv_data
;
344 AVFrame
*pict
= data
;
346 #ifdef PRINT_FRAME_TIME
347 uint64_t time
= rdtsc();
349 s
->flags
= avctx
->flags
;
350 s
->flags2
= avctx
->flags2
;
352 /* no supplementary picture */
354 /* special case for last picture */
355 if (s
->low_delay
==0 && s
->next_picture_ptr
) {
356 *pict
= *(AVFrame
*)s
->next_picture_ptr
;
357 s
->next_picture_ptr
= NULL
;
359 *data_size
= sizeof(AVFrame
);
365 if(s
->flags
&CODEC_FLAG_TRUNCATED
){
368 if(CONFIG_MPEG4_DECODER
&& s
->codec_id
==CODEC_ID_MPEG4
){
369 next
= ff_mpeg4_find_frame_end(&s
->parse_context
, buf
, buf_size
);
370 }else if(CONFIG_H263_DECODER
&& s
->codec_id
==CODEC_ID_H263
){
371 next
= ff_h263_find_frame_end(&s
->parse_context
, buf
, buf_size
);
373 av_log(s
->avctx
, AV_LOG_ERROR
, "this codec does not support truncated bitstreams\n");
377 if( ff_combine_frame(&s
->parse_context
, next
, (const uint8_t **)&buf
, &buf_size
) < 0 )
384 if(s
->bitstream_buffer_size
&& (s
->divx_packed
|| buf_size
<20)){ //divx 5.01+/xvid frame reorder
385 init_get_bits(&s
->gb
, s
->bitstream_buffer
, s
->bitstream_buffer_size
*8);
387 init_get_bits(&s
->gb
, buf
, buf_size
*8);
388 s
->bitstream_buffer_size
=0;
390 if (!s
->context_initialized
) {
391 if (MPV_common_init(s
) < 0) //we need the idct permutaton for reading a custom matrix
395 /* We need to set current_picture_ptr before reading the header,
396 * otherwise we cannot store anyting in there */
397 if(s
->current_picture_ptr
==NULL
|| s
->current_picture_ptr
->data
[0]){
398 int i
= ff_find_unused_picture(s
, 0);
399 s
->current_picture_ptr
= &s
->picture
[i
];
403 if (CONFIG_WMV2_DECODER
&& s
->msmpeg4_version
==5) {
404 ret
= ff_wmv2_decode_picture_header(s
);
405 } else if (CONFIG_MSMPEG4_DECODER
&& s
->msmpeg4_version
) {
406 ret
= msmpeg4_decode_picture_header(s
);
407 } else if (s
->h263_pred
) {
408 if(s
->avctx
->extradata_size
&& s
->picture_number
==0){
411 init_get_bits(&gb
, s
->avctx
->extradata
, s
->avctx
->extradata_size
*8);
412 ret
= ff_mpeg4_decode_picture_header(s
, &gb
);
414 ret
= ff_mpeg4_decode_picture_header(s
, &s
->gb
);
415 } else if (s
->codec_id
== CODEC_ID_H263I
) {
416 ret
= intel_h263_decode_picture_header(s
);
417 } else if (s
->h263_flv
) {
418 ret
= flv_h263_decode_picture_header(s
);
420 ret
= h263_decode_picture_header(s
);
423 if(ret
==FRAME_SKIPPED
) return get_consumed_bytes(s
, buf_size
);
425 /* skip if the header was thrashed */
427 av_log(s
->avctx
, AV_LOG_ERROR
, "header damaged\n");
431 avctx
->has_b_frames
= !s
->low_delay
;
433 if(s
->xvid_build
==0 && s
->divx_version
==0 && s
->lavc_build
==0){
434 if(s
->stream_codec_tag
== AV_RL32("XVID") ||
435 s
->codec_tag
== AV_RL32("XVID") || s
->codec_tag
== AV_RL32("XVIX") ||
436 s
->codec_tag
== AV_RL32("RMP4"))
439 if(s
->codec_tag
== AV_RL32("DIVX") && s
->vo_type
==0 && s
->vol_control_parameters
==1
440 && s
->padding_bug_score
> 0 && s
->low_delay
) // XVID with modified fourcc
445 if(s
->xvid_build
==0 && s
->divx_version
==0 && s
->lavc_build
==0){
446 if(s
->codec_tag
== AV_RL32("DIVX") && s
->vo_type
==0 && s
->vol_control_parameters
==0)
447 s
->divx_version
= 400; //divx 4
450 if(s
->xvid_build
&& s
->divx_version
){
455 if(s
->workaround_bugs
&FF_BUG_AUTODETECT
){
456 if(s
->codec_tag
== AV_RL32("XVIX"))
457 s
->workaround_bugs
|= FF_BUG_XVID_ILACE
;
459 if(s
->codec_tag
== AV_RL32("UMP4")){
460 s
->workaround_bugs
|= FF_BUG_UMP4
;
463 if(s
->divx_version
>=500 && s
->divx_build
<1814){
464 s
->workaround_bugs
|= FF_BUG_QPEL_CHROMA
;
467 if(s
->divx_version
>502 && s
->divx_build
<1814){
468 s
->workaround_bugs
|= FF_BUG_QPEL_CHROMA2
;
471 if(s
->xvid_build
&& s
->xvid_build
<=3)
472 s
->padding_bug_score
= 256*256*256*64;
474 if(s
->xvid_build
&& s
->xvid_build
<=1)
475 s
->workaround_bugs
|= FF_BUG_QPEL_CHROMA
;
477 if(s
->xvid_build
&& s
->xvid_build
<=12)
478 s
->workaround_bugs
|= FF_BUG_EDGE
;
480 if(s
->xvid_build
&& s
->xvid_build
<=32)
481 s
->workaround_bugs
|= FF_BUG_DC_CLIP
;
483 #define SET_QPEL_FUNC(postfix1, postfix2) \
484 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
485 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
486 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
488 if(s
->lavc_build
&& s
->lavc_build
<4653)
489 s
->workaround_bugs
|= FF_BUG_STD_QPEL
;
491 if(s
->lavc_build
&& s
->lavc_build
<4655)
492 s
->workaround_bugs
|= FF_BUG_DIRECT_BLOCKSIZE
;
494 if(s
->lavc_build
&& s
->lavc_build
<4670){
495 s
->workaround_bugs
|= FF_BUG_EDGE
;
498 if(s
->lavc_build
&& s
->lavc_build
<=4712)
499 s
->workaround_bugs
|= FF_BUG_DC_CLIP
;
502 s
->workaround_bugs
|= FF_BUG_DIRECT_BLOCKSIZE
;
503 //printf("padding_bug_score: %d\n", s->padding_bug_score);
504 if(s
->divx_version
==501 && s
->divx_build
==20020416)
505 s
->padding_bug_score
= 256*256*256*64;
507 if(s
->divx_version
&& s
->divx_version
<500){
508 s
->workaround_bugs
|= FF_BUG_EDGE
;
512 s
->workaround_bugs
|= FF_BUG_HPEL_CHROMA
;
514 if(s
->divx_version
==500)
515 s
->padding_bug_score
= 256*256*256*64;
517 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
518 * Let us hope this at least works.
520 if( s
->resync_marker
==0 && s
->data_partitioning
==0 && s
->divx_version
==0
521 && s
->codec_id
==CODEC_ID_MPEG4
&& s
->vo_type
==0)
522 s
->workaround_bugs
|= FF_BUG_NO_PADDING
;
524 if(s
->lavc_build
&& s
->lavc_build
<4609) //FIXME not sure about the version num but a 4609 file seems ok
525 s
->workaround_bugs
|= FF_BUG_NO_PADDING
;
529 if(s
->workaround_bugs
& FF_BUG_STD_QPEL
){
530 SET_QPEL_FUNC(qpel_pixels_tab
[0][ 5], qpel16_mc11_old_c
)
531 SET_QPEL_FUNC(qpel_pixels_tab
[0][ 7], qpel16_mc31_old_c
)
532 SET_QPEL_FUNC(qpel_pixels_tab
[0][ 9], qpel16_mc12_old_c
)
533 SET_QPEL_FUNC(qpel_pixels_tab
[0][11], qpel16_mc32_old_c
)
534 SET_QPEL_FUNC(qpel_pixels_tab
[0][13], qpel16_mc13_old_c
)
535 SET_QPEL_FUNC(qpel_pixels_tab
[0][15], qpel16_mc33_old_c
)
537 SET_QPEL_FUNC(qpel_pixels_tab
[1][ 5], qpel8_mc11_old_c
)
538 SET_QPEL_FUNC(qpel_pixels_tab
[1][ 7], qpel8_mc31_old_c
)
539 SET_QPEL_FUNC(qpel_pixels_tab
[1][ 9], qpel8_mc12_old_c
)
540 SET_QPEL_FUNC(qpel_pixels_tab
[1][11], qpel8_mc32_old_c
)
541 SET_QPEL_FUNC(qpel_pixels_tab
[1][13], qpel8_mc13_old_c
)
542 SET_QPEL_FUNC(qpel_pixels_tab
[1][15], qpel8_mc33_old_c
)
545 if(avctx
->debug
& FF_DEBUG_BUGS
)
546 av_log(s
->avctx
, AV_LOG_DEBUG
, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
547 s
->workaround_bugs
, s
->lavc_build
, s
->xvid_build
, s
->divx_version
, s
->divx_build
,
548 s
->divx_packed
? "p" : "");
550 #if 0 // dump bits per frame / qp / complexity
553 if(!f
) f
=fopen("rate_qp_cplx.txt", "w");
554 fprintf(f
, "%d %d %f\n", buf_size
, s
->qscale
, buf_size
*(double)s
->qscale
);
559 if(s
->codec_id
== CODEC_ID_MPEG4
&& s
->xvid_build
&& avctx
->idct_algo
== FF_IDCT_AUTO
&& (mm_flags
& FF_MM_MMX
)){
560 avctx
->idct_algo
= FF_IDCT_XVIDMMX
;
561 avctx
->coded_width
= 0; // force reinit
562 // dsputil_init(&s->dsp, avctx);
567 /* After H263 & mpeg4 header decode we have the height, width,*/
568 /* and other parameters. So then we could init the picture */
569 /* FIXME: By the way H263 decoder is evolving it should have */
570 /* an H263EncContext */
572 if ( s
->width
!= avctx
->coded_width
573 || s
->height
!= avctx
->coded_height
) {
574 /* H.263 could change picture size any time */
575 ParseContext pc
= s
->parse_context
; //FIXME move these demuxng hack to avformat
576 s
->parse_context
.buffer
=0;
578 s
->parse_context
= pc
;
580 if (!s
->context_initialized
) {
581 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
586 if((s
->codec_id
==CODEC_ID_H263
|| s
->codec_id
==CODEC_ID_H263P
|| s
->codec_id
== CODEC_ID_H263I
))
587 s
->gob_index
= ff_h263_get_gob_height(s
);
590 s
->current_picture
.pict_type
= s
->pict_type
;
591 s
->current_picture
.key_frame
= s
->pict_type
== FF_I_TYPE
;
593 /* skip B-frames if we don't have reference frames */
594 if(s
->last_picture_ptr
==NULL
&& (s
->pict_type
==FF_B_TYPE
|| s
->dropable
)) return get_consumed_bytes(s
, buf_size
);
595 /* skip b frames if we are in a hurry */
596 if(avctx
->hurry_up
&& s
->pict_type
==FF_B_TYPE
) return get_consumed_bytes(s
, buf_size
);
597 if( (avctx
->skip_frame
>= AVDISCARD_NONREF
&& s
->pict_type
==FF_B_TYPE
)
598 || (avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s
->pict_type
!=FF_I_TYPE
)
599 || avctx
->skip_frame
>= AVDISCARD_ALL
)
600 return get_consumed_bytes(s
, buf_size
);
601 /* skip everything if we are in a hurry>=5 */
602 if(avctx
->hurry_up
>=5) return get_consumed_bytes(s
, buf_size
);
604 if(s
->next_p_frame_damaged
){
605 if(s
->pict_type
==FF_B_TYPE
)
606 return get_consumed_bytes(s
, buf_size
);
608 s
->next_p_frame_damaged
=0;
611 if((s
->avctx
->flags2
& CODEC_FLAG2_FAST
) && s
->pict_type
==FF_B_TYPE
){
612 s
->me
.qpel_put
= s
->dsp
.put_2tap_qpel_pixels_tab
;
613 s
->me
.qpel_avg
= s
->dsp
.avg_2tap_qpel_pixels_tab
;
614 }else if((!s
->no_rounding
) || s
->pict_type
==FF_B_TYPE
){
615 s
->me
.qpel_put
= s
->dsp
.put_qpel_pixels_tab
;
616 s
->me
.qpel_avg
= s
->dsp
.avg_qpel_pixels_tab
;
618 s
->me
.qpel_put
= s
->dsp
.put_no_rnd_qpel_pixels_tab
;
619 s
->me
.qpel_avg
= s
->dsp
.avg_qpel_pixels_tab
;
622 if(MPV_frame_start(s
, avctx
) < 0)
625 if (CONFIG_MPEG4_VDPAU_DECODER
&& (s
->avctx
->codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
)) {
626 ff_vdpau_mpeg4_decode_picture(s
, buf
, buf_size
);
630 if (avctx
->hwaccel
) {
631 if (avctx
->hwaccel
->start_frame(avctx
, buf
, buf_size
) < 0)
635 ff_er_frame_start(s
);
637 //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
638 //which is not available before MPV_frame_start()
639 if (CONFIG_WMV2_DECODER
&& s
->msmpeg4_version
==5){
640 ret
= ff_wmv2_decode_secondary_picture_header(s
);
641 if(ret
<0) return ret
;
642 if(ret
==1) goto intrax8_decoded
;
645 /* decode each macroblock */
650 while(s
->mb_y
<s
->mb_height
){
651 if(s
->msmpeg4_version
){
652 if(s
->slice_height
==0 || s
->mb_x
!=0 || (s
->mb_y
%s
->slice_height
)!=0 || get_bits_count(&s
->gb
) > s
->gb
.size_in_bits
)
655 if(ff_h263_resync(s
)<0)
659 if(s
->msmpeg4_version
<4 && s
->h263_pred
)
660 ff_mpeg4_clean_buffers(s
);
665 if (s
->h263_msmpeg4
&& s
->msmpeg4_version
<4 && s
->pict_type
==FF_I_TYPE
)
666 if(!CONFIG_MSMPEG4_DECODER
|| msmpeg4_decode_ext_header(s
, buf_size
) < 0){
667 s
->error_status_table
[s
->mb_num
-1]= AC_ERROR
|DC_ERROR
|MV_ERROR
;
670 /* divx 5.01+ bistream reorder stuff */
671 if(s
->codec_id
==CODEC_ID_MPEG4
&& s
->bitstream_buffer_size
==0 && s
->divx_packed
){
672 int current_pos
= get_bits_count(&s
->gb
)>>3;
673 int startcode_found
=0;
675 if(buf_size
- current_pos
> 5){
677 for(i
=current_pos
; i
<buf_size
-3; i
++){
678 if(buf
[i
]==0 && buf
[i
+1]==0 && buf
[i
+2]==1 && buf
[i
+3]==0xB6){
684 if(s
->gb
.buffer
== s
->bitstream_buffer
&& buf_size
>20){ //xvid style
691 &s
->bitstream_buffer
,
692 &s
->allocated_bitstream_buffer_size
,
693 buf_size
- current_pos
+ FF_INPUT_BUFFER_PADDING_SIZE
);
694 if (!s
->bitstream_buffer
)
695 return AVERROR(ENOMEM
);
696 memcpy(s
->bitstream_buffer
, buf
+ current_pos
, buf_size
- current_pos
);
697 s
->bitstream_buffer_size
= buf_size
- current_pos
;
705 if (avctx
->hwaccel
) {
706 if (avctx
->hwaccel
->end_frame(avctx
) < 0)
712 assert(s
->current_picture
.pict_type
== s
->current_picture_ptr
->pict_type
);
713 assert(s
->current_picture
.pict_type
== s
->pict_type
);
714 if (s
->pict_type
== FF_B_TYPE
|| s
->low_delay
) {
715 *pict
= *(AVFrame
*)s
->current_picture_ptr
;
716 } else if (s
->last_picture_ptr
!= NULL
) {
717 *pict
= *(AVFrame
*)s
->last_picture_ptr
;
720 if(s
->last_picture_ptr
|| s
->low_delay
){
721 *data_size
= sizeof(AVFrame
);
722 ff_print_debug_info(s
, pict
);
725 #ifdef PRINT_FRAME_TIME
726 av_log(avctx
, AV_LOG_DEBUG
, "%"PRId64
"\n", rdtsc()-time
);
729 return get_consumed_bytes(s
, buf_size
);
732 AVCodec mpeg4_decoder
= {
736 sizeof(MpegEncContext
),
740 ff_h263_decode_frame
,
741 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
742 .flush
= ff_mpeg_flush
,
743 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
744 .pix_fmts
= ff_hwaccel_pixfmt_list_420
,
747 AVCodec h263_decoder
= {
751 sizeof(MpegEncContext
),
755 ff_h263_decode_frame
,
756 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
757 .flush
= ff_mpeg_flush
,
758 .long_name
= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
759 .pix_fmts
= ff_hwaccel_pixfmt_list_420
,
762 AVCodec msmpeg4v1_decoder
= {
766 sizeof(MpegEncContext
),
770 ff_h263_decode_frame
,
771 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
,
772 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
773 .pix_fmts
= ff_pixfmt_list_420
,
776 AVCodec msmpeg4v2_decoder
= {
780 sizeof(MpegEncContext
),
784 ff_h263_decode_frame
,
785 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
,
786 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
787 .pix_fmts
= ff_pixfmt_list_420
,
790 AVCodec msmpeg4v3_decoder
= {
794 sizeof(MpegEncContext
),
798 ff_h263_decode_frame
,
799 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
,
800 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
801 .pix_fmts
= ff_pixfmt_list_420
,
804 AVCodec wmv1_decoder
= {
808 sizeof(MpegEncContext
),
812 ff_h263_decode_frame
,
813 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
,
814 .long_name
= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
815 .pix_fmts
= ff_pixfmt_list_420
,
818 AVCodec h263i_decoder
= {
822 sizeof(MpegEncContext
),
826 ff_h263_decode_frame
,
827 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
,
828 .long_name
= NULL_IF_CONFIG_SMALL("Intel H.263"),
829 .pix_fmts
= ff_pixfmt_list_420
,
832 AVCodec flv_decoder
= {
836 sizeof(MpegEncContext
),
840 ff_h263_decode_frame
,
841 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
,
842 .long_name
= NULL_IF_CONFIG_SMALL("Flash Video (FLV) / Sorenson Spark / Sorenson H.263"),
843 .pix_fmts
= ff_pixfmt_list_420
,
846 #if CONFIG_MPEG4_VDPAU_DECODER
847 AVCodec mpeg4_vdpau_decoder
= {
851 sizeof(MpegEncContext
),
855 ff_h263_decode_frame
,
856 CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
| CODEC_CAP_HWACCEL_VDPAU
,
857 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
858 .pix_fmts
= (const enum PixelFormat
[]){PIX_FMT_VDPAU_MPEG4
, PIX_FMT_NONE
},