3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mpegvideo.h"
24 #define PRINT_QP(a, b) {}
26 #define PRINT_QP(a, b) printf(a, b)
30 //#define PRINT_FRAME_TIME
31 #ifdef PRINT_FRAME_TIME
32 static inline long long rdtsc()
35 asm volatile( "rdtsc\n\t"
38 // printf("%d\n", int(l/1000));
43 const UINT16 ff_mpeg4_resync_prefix[8];
45 static int h263_decode_init(AVCodecContext *avctx)
47 MpegEncContext *s = avctx->priv_data;
50 s->out_format = FMT_H263;
52 s->width = avctx->width;
53 s->height = avctx->height;
54 s->workaround_bugs= avctx->workaround_bugs;
58 s->progressive_sequence=1;
59 s->decode_mb= ff_h263_decode_mb;
61 /* select sub codec */
62 switch(avctx->codec->id) {
65 s->first_slice_line = 0;
68 s->time_increment_bits = 4; /* default value for broken headers */
70 s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
72 case CODEC_ID_MSMPEG4V1:
77 case CODEC_ID_MSMPEG4V2:
82 case CODEC_ID_MSMPEG4V3:
103 s->codec_id= avctx->codec->id;
105 /* for h263, we allocate the images after having read the header */
106 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
107 if (MPV_common_init(s) < 0)
111 ff_msmpeg4_decode_init(s);
113 h263_decode_init_vlc(s);
118 static int h263_decode_end(AVCodecContext *avctx)
120 MpegEncContext *s = avctx->priv_data;
127 * retunrs the number of bytes consumed for building the current frame
129 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
130 int pos= (get_bits_count(&s->gb)+7)>>3;
131 if(s->divx_version>=500){
132 //we would have to scan through the whole buf to handle the weird reordering ...
135 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
136 if(pos+10>buf_size) pos=buf_size; // oops ;)
142 static int decode_slice(MpegEncContext *s){
143 s->last_resync_gb= s->gb;
144 s->first_slice_line= 1;
146 s->resync_mb_x= s->mb_x;
147 s->resync_mb_y= s->mb_y;
149 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
150 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
152 if(s->partitioned_frame){
153 const int qscale= s->qscale;
155 if(s->codec_id==CODEC_ID_MPEG4){
156 if(ff_mpeg4_decode_partitions(s) < 0)
160 /* restore variables which where modified */
161 s->first_slice_line=1;
162 s->mb_x= s->resync_mb_x;
163 s->mb_y= s->resync_mb_y;
165 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
166 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
169 for(; s->mb_y < s->mb_height; s->mb_y++) {
170 /* per-row end of slice checks */
171 if(s->msmpeg4_version){
172 if(s->resync_mb_y + s->slice_height == s->mb_y){
173 const int xy= s->mb_x + s->mb_y*s->mb_width;
174 s->error_status_table[xy-1]|= AC_END|DC_END|MV_END;
179 if(s->msmpeg4_version==1){
185 ff_init_block_index(s);
186 for(; s->mb_x < s->mb_width; s->mb_x++) {
189 ff_update_block_index(s);
191 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
192 s->first_slice_line=0;
196 clear_blocks(s->block[0]);
198 s->mv_dir = MV_DIR_FORWARD;
199 s->mv_type = MV_TYPE_16X16;
200 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
201 ret= s->decode_mb(s, s->block);
203 PRINT_QP("%2d", s->qscale);
204 MPV_decode_mb(s, s->block);
207 const int xy= s->mb_x + s->mb_y*s->mb_width;
209 //printf("%d %d %06X\n", s->mb_x, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24));
210 s->error_status_table[xy]|= AC_END;
211 if(!s->partitioned_frame)
212 s->error_status_table[xy]|= MV_END|DC_END;
214 if(++s->mb_x >= s->mb_width){
216 ff_draw_horiz_band(s);
220 }else if(ret==SLICE_NOEND){
221 fprintf(stderr,"Slice mismatch at MB: %d\n", xy);
224 fprintf(stderr,"Error at MB: %d\n", xy);
225 s->error_status_table[xy]|= AC_ERROR;
226 if(!s->partitioned_frame)
227 s->error_status_table[xy]|= DC_ERROR|MV_ERROR;
233 ff_draw_horiz_band(s);
235 PRINT_QP("%s", "\n");
240 assert(s->mb_x==0 && s->mb_y==s->mb_height);
242 // handle formats which dont have unique end markers
243 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
244 int left= s->gb.size*8 - get_bits_count(&s->gb);
247 /* no markers in M$ crap */
248 if(s->msmpeg4_version && s->pict_type==I_TYPE)
251 /* buggy padding but the frame should still end approximately at the bitstream end */
252 if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
254 else if((s->workaround_bugs&FF_BUG_NO_PADDING))
255 max_extra+= 256*256*256*64;
258 fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
261 fprintf(stderr, "overreading %d bits\n", -left);
263 s->error_status_table[s->mb_num-1]|= AC_END|MV_END|DC_END;
268 fprintf(stderr, "slice end not reached but screenspace end (%d left %06X)\n",
269 s->gb.size*8 - get_bits_count(&s->gb),
270 show_bits(&s->gb, 24));
274 static int h263_decode_frame(AVCodecContext *avctx,
275 void *data, int *data_size,
276 UINT8 *buf, int buf_size)
278 MpegEncContext *s = avctx->priv_data;
280 AVPicture *pict = data;
281 #ifdef PRINT_FRAME_TIME
282 uint64_t time= rdtsc();
285 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
286 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
289 s->hurry_up= avctx->hurry_up;
290 s->error_resilience= avctx->error_resilience;
292 s->flags= avctx->flags;
296 /* no supplementary picture */
301 if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
302 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
304 init_get_bits(&s->gb, buf, buf_size);
305 s->bitstream_buffer_size=0;
307 if (!s->context_initialized) {
308 if (DCT_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
313 if (s->h263_msmpeg4) {
314 ret = msmpeg4_decode_picture_header(s);
315 } else if (s->h263_pred) {
316 ret = mpeg4_decode_picture_header(s);
317 s->has_b_frames= !s->low_delay;
318 } else if (s->h263_intel) {
319 ret = intel_h263_decode_picture_header(s);
321 ret = h263_decode_picture_header(s);
323 avctx->has_b_frames= s->has_b_frames;
325 if(s->workaround_bugs&FF_BUG_AUTODETECT){
326 if(s->avctx->fourcc == ff_get_fourcc("XVIX"))
327 s->workaround_bugs|= FF_BUG_XVID_ILACE;
329 if(s->avctx->fourcc == ff_get_fourcc("MP4S"))
330 s->workaround_bugs|= FF_BUG_AC_VLC;
332 if(s->avctx->fourcc == ff_get_fourcc("M4S2"))
333 s->workaround_bugs|= FF_BUG_AC_VLC;
335 if(s->avctx->fourcc == ff_get_fourcc("UMP4")){
336 s->workaround_bugs|= FF_BUG_UMP4;
337 s->workaround_bugs|= FF_BUG_AC_VLC;
340 if(s->divx_version==500)
341 s->workaround_bugs|= FF_BUG_NO_PADDING;
343 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
344 * lets hope this at least works
346 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
347 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
348 s->workaround_bugs|= FF_BUG_NO_PADDING;
350 if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
351 s->workaround_bugs|= FF_BUG_NO_PADDING;
355 #if 0 // dump bits per frame / qp / complexity
358 if(!f) f=fopen("rate_qp_cplx.txt", "w");
359 fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
363 /* After H263 & mpeg4 header decode we have the height, width,*/
364 /* and other parameters. So then we could init the picture */
365 /* FIXME: By the way H263 decoder is evolving it should have */
366 /* an H263EncContext */
367 if ( s->width != avctx->width || s->height != avctx->height
368 || avctx->aspect_ratio_info != s->aspect_ratio_info
369 || avctx->aspected_width != s->aspected_width
370 || avctx->aspected_height != s->aspected_height) {
371 /* H.263 could change picture size any time */
373 s->context_initialized=0;
375 if (!s->context_initialized) {
376 avctx->width = s->width;
377 avctx->height = s->height;
378 avctx->aspect_ratio_info= s->aspect_ratio_info;
379 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
381 avctx->aspected_width = s->aspected_width;
382 avctx->aspected_height = s->aspected_height;
385 if (s->codec_id==CODEC_ID_H263 && s->codec_id==CODEC_ID_H263)
386 s->gob_index = ff_h263_get_gob_height(s);
388 if (MPV_common_init(s) < 0)
392 if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
393 /* skip if the header was thrashed */
395 fprintf(stderr, "header damaged\n");
398 /* skip b frames if we dont have reference frames */
399 if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
400 /* skip b frames if we are in a hurry */
401 if(s->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
403 if(s->next_p_frame_damaged){
404 if(s->pict_type==B_TYPE)
405 return get_consumed_bytes(s, buf_size);
407 s->next_p_frame_damaged=0;
410 MPV_frame_start(s, avctx);
413 printf("qscale=%d\n", s->qscale);
416 if(s->error_resilience)
417 memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_num*sizeof(UINT8));
419 /* decode each macroblock */
423 s->block_wrap[3]= s->mb_width*2 + 2;
425 s->block_wrap[5]= s->mb_width + 2;
430 s->error_status_table[0]|= VP_START;
431 while(s->mb_y<s->mb_height && s->gb.size*8 - get_bits_count(&s->gb)>32){
432 if(s->msmpeg4_version){
433 if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0)
436 if(ff_h263_resync(s)<0)
440 if(s->msmpeg4_version!=4)
441 ff_mpeg4_clean_buffers(s);
444 s->error_status_table[s->resync_mb_x + s->resync_mb_y*s->mb_width]|= VP_START;
447 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
448 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
450 /* divx 5.01+ bistream reorder stuff */
451 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_version>=500){
452 int current_pos= get_bits_count(&s->gb)>>3;
454 if( buf_size - current_pos > 5
455 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
457 int startcode_found=0;
458 for(i=current_pos; i<buf_size-3; i++){
459 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
465 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
466 s->bitstream_buffer_size= buf_size - current_pos;
471 if(s->error_resilience){
472 int error=0, num_end_markers=0;
473 for(i=0; i<s->mb_num; i++){
474 int status= s->error_status_table[i];
476 if(i%s->mb_width == 0) printf("\n");
477 printf("%2X ", status);
479 if(status==0) continue;
481 if(status&(DC_ERROR|AC_ERROR|MV_ERROR))
495 if(num_end_markers || error){
496 fprintf(stderr, "concealing errors\n");
497 //printf("type:%d\n", s->pict_type);
498 ff_error_resilience(s);
503 #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
507 for(mb_y=0; mb_y<s->mb_height; mb_y++){
510 for(mb_x=0; mb_x<s->mb_width; mb_x++){
512 uint8_t *ptr= s->last_picture[0];
513 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
514 int mx= (s->motion_val[xy][0]>>1) + x;
515 int my= (s->motion_val[xy][1]>>1) + y;
521 if(mx>=s->width) mx= s->width -1;
522 if(my>=s->height) my= s->height-1;
524 if(ABS(my-y) > max) max= ABS(my-y);
525 /* the ugliest linedrawing routine ... */
526 for(i=0; i<max; i++){
527 int x1= x + (mx-x)*i/max;
528 int y1= y + (my-y)*i/max;
529 ptr[y1*s->linesize + x1]+=100;
531 ptr[y*s->linesize + x]+=100;
532 s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
538 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
539 pict->data[0] = s->current_picture[0];
540 pict->data[1] = s->current_picture[1];
541 pict->data[2] = s->current_picture[2];
543 pict->data[0] = s->last_picture[0];
544 pict->data[1] = s->last_picture[1];
545 pict->data[2] = s->last_picture[2];
547 pict->linesize[0] = s->linesize;
548 pict->linesize[1] = s->uvlinesize;
549 pict->linesize[2] = s->uvlinesize;
551 avctx->quality = s->qscale;
553 /* Return the Picture timestamp as the frame number */
554 /* we substract 1 because it is added on utils.c */
555 avctx->frame_number = s->picture_number - 1;
557 /* dont output the last pic after seeking
558 note we allready added +1 for the current pix in MPV_frame_end(s) */
559 if(s->num_available_buffers>=2 || (!s->has_b_frames))
560 *data_size = sizeof(AVPicture);
561 #ifdef PRINT_FRAME_TIME
562 printf("%Ld\n", rdtsc()-time);
564 return get_consumed_bytes(s, buf_size);
567 AVCodec mpeg4_decoder = {
571 sizeof(MpegEncContext),
576 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
579 AVCodec h263_decoder = {
583 sizeof(MpegEncContext),
588 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
591 AVCodec msmpeg4v1_decoder = {
595 sizeof(MpegEncContext),
600 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
603 AVCodec msmpeg4v2_decoder = {
607 sizeof(MpegEncContext),
612 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
615 AVCodec msmpeg4v3_decoder = {
619 sizeof(MpegEncContext),
624 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
627 AVCodec wmv1_decoder = {
631 sizeof(MpegEncContext),
636 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
639 AVCodec wmv2_decoder = {
643 sizeof(MpegEncContext),
648 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
651 AVCodec h263i_decoder = {
655 sizeof(MpegEncContext),
660 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,