]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpeg12.c
intra_dc_precission>0 encoding support
[ffmpeg] / libavcodec / mpeg12.c
index 28a6791f30d3a5c0a214777a831a9177b1175bed..93e21af1009daf970c2fba98958f3b806f9c23c2 100644 (file)
@@ -242,7 +242,7 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s)
 {
         unsigned int vbv_buffer_size;
         unsigned int fps, v;
-        int n, i;
+        int i;
         uint64_t time_code;
         float best_aspect_error= 1E10;
         float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
@@ -358,8 +358,10 @@ static inline void encode_mb_skip_run(MpegEncContext *s, int run){
 
 static void common_init(MpegEncContext *s)
 {
+
     s->y_dc_scale_table=
-    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+    s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
+
 }
 
 void ff_mpeg1_clean_buffers(MpegEncContext *s){
@@ -493,8 +495,9 @@ void mpeg1_encode_mb(MpegEncContext *s,
             cbp |= 1 << (5 - i);
     }
     
-    if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && 
-        ((s->pict_type == P_TYPE && s->mv_type == MV_TYPE_16X16 && (motion_x | motion_y) == 0) ||
+    if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
+        (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && 
+        ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
         (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
         ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
         s->mb_skip_run++;
@@ -770,7 +773,7 @@ void ff_mpeg1_encode_init(MpegEncContext *s)
 
                adiff = ABS(diff);
                if(diff<0) diff--;
-               index = vlc_dc_table[adiff];
+               index = av_log2(2*adiff);
 
                bits= vlc_dc_lum_bits[index] + index;
                code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
@@ -791,7 +794,7 @@ void ff_mpeg1_encode_init(MpegEncContext *s)
                 else{
                     int val, bit_size, range, code;
 
-                    bit_size = s->f_code - 1;
+                    bit_size = f_code - 1;
                     range = 1 << bit_size;
 
                     val=mv;
@@ -834,6 +837,27 @@ void ff_mpeg1_encode_init(MpegEncContext *s)
 
 static inline void encode_dc(MpegEncContext *s, int diff, int component)
 {
+  if(((unsigned) (diff+255)) >= 511){
+        int index;
+
+        if(diff<0){
+            index= av_log2_16bit(-2*diff);
+            diff--;
+        }else{
+            index= av_log2_16bit(2*diff);
+        }
+        if (component == 0) {
+            put_bits(
+                &s->pb, 
+                vlc_dc_lum_bits[index] + index,
+                (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
+        }else{
+            put_bits(
+                &s->pb, 
+                vlc_dc_chroma_bits[index] + index,
+                (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
+        }
+  }else{
     if (component == 0) {
         put_bits(
            &s->pb, 
@@ -845,6 +869,7 @@ static inline void encode_dc(MpegEncContext *s, int diff, int component)
            mpeg1_chr_dc_uni[diff+255]&0xFF,
            mpeg1_chr_dc_uni[diff+255]>>8);
     }
+  }
 }
 
 static void mpeg1_encode_block(MpegEncContext *s, 
@@ -948,7 +973,7 @@ static VLC mb_ptype_vlc;
 static VLC mb_btype_vlc;
 static VLC mb_pat_vlc;
 
-static void init_vlcs()
+static void init_vlcs(void)
 {
     static int done = 0;
 
@@ -1747,11 +1772,24 @@ typedef struct Mpeg1Context {
     int repeat_field; /* true if we must repeat the field */
     AVPanScan pan_scan; /** some temporary storage for the panscan */
     int slice_count;
+    int swap_uv;//indicate VCR2
+    int save_aspect_info;
+
 } Mpeg1Context;
 
 static int mpeg_decode_init(AVCodecContext *avctx)
 {
     Mpeg1Context *s = avctx->priv_data;
+    MpegEncContext *s2 = &s->mpeg_enc_ctx;
+    int i;
+    
+    //we need some parmutation to store
+    //matrixes, until MPV_common_init()
+    //set the real permutatuon 
+    for(i=0;i<64;i++)
+       s2->dsp.idct_permutation[i]=i;
+
+    MPV_decode_defaults(s2);
     
     s->mpeg_enc_ctx.avctx= avctx;
     s->mpeg_enc_ctx.flags= avctx->flags;
@@ -1766,6 +1804,122 @@ static int mpeg_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
+static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, 
+                                     const uint8_t *new_perm){
+uint16_t temp_matrix[64];
+int i;
+
+    memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
+    
+    for(i=0;i<64;i++){
+        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
+    }      
+}
+
+//Call this function when we know all parameters
+//it may be called in different places for mpeg1 and mpeg2
+static int mpeg_decode_postinit(AVCodecContext *avctx){
+Mpeg1Context *s1 = avctx->priv_data;
+MpegEncContext *s = &s1->mpeg_enc_ctx;
+uint8_t old_permutation[64];
+
+
+    if (
+       (s1->mpeg_enc_ctx_allocated == 0)|| 
+        avctx->width  != s->width ||
+        avctx->height != s->height||
+//      s1->save_aspect_info != avctx->aspect_ratio_info||
+        0)
+    {
+    
+        if (s1->mpeg_enc_ctx_allocated) {
+            MPV_common_end(s);
+        }
+
+       if( (s->width == 0 )||(s->height == 0))
+           return -2;
+
+        avctx->width = s->width;
+        avctx->height = s->height;
+        avctx->bit_rate = s->bit_rate;
+        s1->save_aspect_info = s->aspect_ratio_info;
+
+     //low_delay may be forced, in this case we will have B frames
+     //that behave like P frames
+        avctx->has_b_frames = !(s->low_delay);
+
+        if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
+            //mpeg1 fps
+            avctx->frame_rate     = frame_rate_tab[s->frame_rate_index].num;
+            avctx->frame_rate_base= frame_rate_tab[s->frame_rate_index].den;
+            //mpeg1 aspect
+            avctx->sample_aspect_ratio= av_d2q(
+                    1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
+
+        }else{//mpeg2
+        //mpeg2 fps
+            av_reduce(
+                &s->avctx->frame_rate, 
+                &s->avctx->frame_rate_base, 
+                frame_rate_tab[s->frame_rate_index].num * (s->frame_rate_ext_n+1),
+                frame_rate_tab[s->frame_rate_index].den * (s->frame_rate_ext_d+1),
+                1<<30);
+        //mpeg2 aspect
+            if(s->aspect_ratio_info > 1){
+                if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
+                    s->avctx->sample_aspect_ratio= 
+                        av_div_q(
+                         mpeg2_aspect[s->aspect_ratio_info], 
+                         (AVRational){s->width, s->height}
+                         );
+                }else{
+                    s->avctx->sample_aspect_ratio= 
+                        av_div_q(
+                         mpeg2_aspect[s->aspect_ratio_info], 
+                         (AVRational){s1->pan_scan.width, s1->pan_scan.height}
+                        );
+               }
+            }else{
+                s->avctx->sample_aspect_ratio= 
+                    mpeg2_aspect[s->aspect_ratio_info];
+            }
+        }//mpeg2
+
+        if(avctx->xvmc_acceleration){
+            avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
+        }else{
+            if(s->chroma_format <  2){
+                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
+            }else
+            if(s->chroma_format == 2){
+                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
+            }else
+            if(s->chroma_format >  2){
+                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
+            }
+        }
+        //until then pix_fmt may be changed right after codec init
+        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
+            if( avctx->idct_algo == FF_IDCT_AUTO )
+                avctx->idct_algo = FF_IDCT_SIMPLE;
+
+        //quantization matrixes may need reordering 
+        //if dct permutation is changed
+        memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
+
+        if (MPV_common_init(s) < 0)
+            return -2;
+
+        quant_matrix_rebuild(s->intra_matrix,       old_permutation,s->dsp.idct_permutation);
+        quant_matrix_rebuild(s->inter_matrix,       old_permutation,s->dsp.idct_permutation);
+        quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
+        quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
+
+        s1->mpeg_enc_ctx_allocated = 1;
+    }
+    return 0;
+}
+
 /* return the 8 bit start code value and update the search
    state. Return -1 if no start code found */
 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
@@ -1800,6 +1954,9 @@ static int mpeg1_decode_picture(AVCodecContext *avctx,
     MpegEncContext *s = &s1->mpeg_enc_ctx;
     int ref, f_code, vbv_delay;
 
+    if(mpeg_decode_postinit(s->avctx) < 0) 
+       return -2;
+
     init_get_bits(&s->gb, buf, buf_size*8);
 
     ref = get_bits(&s->gb, 10); /* temporal ref */
@@ -1838,7 +1995,6 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s)
 {
     int horiz_size_ext, vert_size_ext;
     int bit_rate_ext;
-    int frame_rate_ext_n, frame_rate_ext_d;
     int level, profile;
 
     skip_bits(&s->gb, 1); /* profil and level esc*/
@@ -1858,32 +2014,17 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s)
     s->low_delay = get_bits1(&s->gb);
     if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
 
-    frame_rate_ext_n = get_bits(&s->gb, 2);
-    frame_rate_ext_d = get_bits(&s->gb, 5);
-    av_reduce(
-        &s->avctx->frame_rate, 
-        &s->avctx->frame_rate_base, 
-        frame_rate_tab[s->frame_rate_index].num * (frame_rate_ext_n+1),
-        frame_rate_tab[s->frame_rate_index].den * (frame_rate_ext_d+1),
-        1<<30);
+    s->frame_rate_ext_n = get_bits(&s->gb, 2);
+    s->frame_rate_ext_d = get_bits(&s->gb, 5);
 
     dprintf("sequence extension\n");
     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
     s->avctx->sub_id = 2; /* indicates mpeg2 found */
 
-    if(s->aspect_ratio_info <= 1)
-        s->avctx->sample_aspect_ratio= mpeg2_aspect[s->aspect_ratio_info];
-    else{
-        s->avctx->sample_aspect_ratio= 
-            av_div_q(
-                mpeg2_aspect[s->aspect_ratio_info], 
-                (AVRational){s->width, s->height}
-            );
-    }
-    
     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
         av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n", 
                profile, level, s->avctx->rc_buffer_size, s->bit_rate);
+
 }
 
 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
@@ -1905,14 +2046,7 @@ static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
     
     s1->pan_scan.width= 16*w;
     s1->pan_scan.height=16*h;
-
-    if(s->aspect_ratio_info > 1)
-        s->avctx->sample_aspect_ratio= 
-            av_div_q(
-                mpeg2_aspect[s->aspect_ratio_info], 
-                (AVRational){w, h}
-            );
-    
+        
     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
         av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
 }
@@ -1920,9 +2054,23 @@ static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
 {
     MpegEncContext *s= &s1->mpeg_enc_ctx;
-    int i;
-
-    for(i=0; i<1; i++){ //FIXME count
+    int i,nofco;
+
+    nofco = 1;
+    if(s->progressive_sequence){
+        if(s->repeat_first_field){
+           nofco++;
+           if(s->top_field_first)
+               nofco++;        
+       }
+    }else{
+        if(s->picture_structure == PICT_FRAME){
+            nofco++;
+           if(s->repeat_first_field)
+               nofco++;
+       }
+    }
+    for(i=0; i<nofco; i++){
         s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
         skip_bits(&s->gb, 1); //marker
         s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
@@ -2127,8 +2275,8 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
     s->resync_mb_x=
     s->resync_mb_y= -1;
 
-    if (mb_y >= s->mb_height){
-        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", s->mb_y, s->mb_height);
+    if (mb_y<<field_pic >= s->mb_height){
+        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
         return -1;
     }
     
@@ -2201,8 +2349,8 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
             return -1;
 
         if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
-            const int wrap = field_pic ? 2*s->block_wrap[0] : s->block_wrap[0];
-            int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
+            const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
+            int xy = s->mb_x*2 + s->mb_y*2*wrap;
             int motion_x, motion_y, dir, i;
             if(field_pic && !s->first_field)
                 xy += wrap/2;
@@ -2211,18 +2359,20 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
                 for(dir=0; dir<2; dir++){
                     if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
                         motion_x = motion_y = 0;
-                    }else if (s->mv_type == MV_TYPE_16X16){
+                    }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
                         motion_x = s->mv[dir][0][0];
                         motion_y = s->mv[dir][0][1];
                     } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
                         motion_x = s->mv[dir][i][0];
                         motion_y = s->mv[dir][i][1];
                     }
-                    
+
                     s->current_picture.motion_val[dir][xy    ][0] = motion_x;
                     s->current_picture.motion_val[dir][xy    ][1] = motion_y;
                     s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
                     s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
+                    s->current_picture.ref_index [dir][xy    ]=
+                    s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
                 }
                 xy += wrap;
             }
@@ -2372,59 +2522,27 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
 {
     Mpeg1Context *s1 = avctx->priv_data;
     MpegEncContext *s = &s1->mpeg_enc_ctx;
-    int width, height, i, v, j;
-    float aspect;
+    int width,height;
+    int i, v, j;
 
     init_get_bits(&s->gb, buf, buf_size*8);
 
     width = get_bits(&s->gb, 12);
     height = get_bits(&s->gb, 12);
+    if (width <= 0 || height <= 0 ||
+        (width % 2) != 0 || (height % 2) != 0)
+        return -1;
     s->aspect_ratio_info= get_bits(&s->gb, 4);
     if (s->aspect_ratio_info == 0)
         return -1;
-    aspect= 1.0/mpeg1_aspect[s->aspect_ratio_info];
-    avctx->sample_aspect_ratio= av_d2q(aspect, 255);
-
     s->frame_rate_index = get_bits(&s->gb, 4);
     if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
         return -1;
     s->bit_rate = get_bits(&s->gb, 18) * 400;
     if (get_bits1(&s->gb) == 0) /* marker */
         return -1;
-    if (width <= 0 || height <= 0 ||
-        (width % 2) != 0 || (height % 2) != 0)
-        return -1;
-    if (width != s->width ||
-        height != s->height) {
-        /* start new mpeg1 context decoding */
-        s->out_format = FMT_MPEG1;
-        if (s1->mpeg_enc_ctx_allocated) {
-            MPV_common_end(s);
-        }
-        s->width = width;
-        s->height = height;
-        avctx->has_b_frames= 1;
-        avctx->width = width;
-        avctx->height = height;
-        avctx->frame_rate     = frame_rate_tab[s->frame_rate_index].num;
-        avctx->frame_rate_base= frame_rate_tab[s->frame_rate_index].den;
-        avctx->bit_rate = s->bit_rate;
-        
-        if(avctx->xvmc_acceleration){
-            avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
-        }else{
-            avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
-        }
-       
-        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
-            if( avctx->idct_algo == FF_IDCT_AUTO )
-                avctx->idct_algo = FF_IDCT_SIMPLE;
-
-        if (MPV_common_init(s) < 0)
-            return -1;
-        s1->mpeg_enc_ctx_allocated = 1;
-        s->swap_uv = 0;//just in case vcr2 and mpeg2 stream have been concatinated
-    }
+    s->width = width;
+    s->height = height;
 
     s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
     skip_bits(&s->gb, 1);
@@ -2437,19 +2555,19 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
                 av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
                 return -1;
             }
-            j = s->intra_scantable.permutated[i];
+            j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
             s->intra_matrix[j] = v;
             s->chroma_intra_matrix[j] = v;
         }
 #ifdef DEBUG
         dprintf("intra matrix present\n");
         for(i=0;i<64;i++)
-            dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
+            dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]);
         printf("\n");
 #endif
     } else {
         for(i=0;i<64;i++) {
-            int j= s->dsp.idct_permutation[i];
+            = s->dsp.idct_permutation[i];
             v = ff_mpeg1_default_intra_matrix[i];
             s->intra_matrix[j] = v;
             s->chroma_intra_matrix[j] = v;
@@ -2462,14 +2580,14 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
                 av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
                 return -1;
             }
-            j = s->intra_scantable.permutated[i];
+            j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
             s->inter_matrix[j] = v;
             s->chroma_inter_matrix[j] = v;
         }
 #ifdef DEBUG
         dprintf("non intra matrix present\n");
         for(i=0;i<64;i++)
-            dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
+            dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]);
         printf("\n");
 #endif
     } else {
@@ -2494,6 +2612,8 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
     s->chroma_format = 1;
     s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
     avctx->sub_id = 1; /* indicates mpeg1 */
+    s->out_format = FMT_MPEG1;
+    s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
     if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
     
     if(s->avctx->debug & FF_DEBUG_PICT_INFO)
@@ -2596,8 +2716,6 @@ static void mpeg_decode_gop(AVCodecContext *avctx,
     int time_code_seconds, time_code_pictures;
     int broken_link;
 
-    s->first_field = 0;
-
     init_get_bits(&s->gb, buf, buf_size*8);
 
     drop_frame_flag = get_bits1(&s->gb);
@@ -2607,22 +2725,23 @@ static void mpeg_decode_gop(AVCodecContext *avctx,
     skip_bits1(&s->gb);//marker bit
     time_code_seconds = get_bits(&s->gb,6);
     time_code_pictures = get_bits(&s->gb,6);
+
     /*broken_link indicate that after editing the
       reference frames of the first B-Frames after GOP I-Frame
       are missing (open gop)*/
     broken_link = get_bits1(&s->gb);
-    if(broken_link == 1){
-//        avcodec_flush_buffers(avctx);
-        ff_mpeg_flush(avctx);
-    }
-}
 
+    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
+        av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
+           time_code_hours, time_code_minutes, time_code_seconds,
+           time_code_pictures, broken_link);
+}
 /**
  * finds the end of the current frame in the bitstream.
  * @return the position of the first byte of the next frame, or -1
  */
-static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
-    ParseContext *pc= &s->parse_context;
+int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
+{
     int i;
     uint32_t state;
     
@@ -2641,6 +2760,9 @@ static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
     }
     
     if(pc->frame_start_found){
+        /* EOF considered as end of frame */
+        if (buf_size == 0)
+            return 0;
         for(; i<buf_size; i++){
             state= (state<<8) | buf[i];
             if((state&0xFFFFFF00) == 0x100){
@@ -2681,9 +2803,9 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
     }
 
     if(s2->flags&CODEC_FLAG_TRUNCATED){
-        int next= mpeg1_find_frame_end(s2, buf, buf_size);
+        int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
         
-        if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
+        if( ff_combine_frame(&s2->parse_context, next, &buf, &buf_size) < 0 )
             return buf_size;
     }    
     
@@ -2730,7 +2852,7 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
         input_size = buf_end - buf_ptr;
 
         if(avctx->debug & FF_DEBUG_STARTCODE){
-            av_log(avctx, AV_LOG_DEBUG, "%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
+            av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
         }
 
                 /* prepare data for next start code */
@@ -2754,7 +2876,9 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
                                           buf_ptr, input_size);
                     break;
                 case GOP_START_CODE:
-                   mpeg_decode_gop(avctx, buf_ptr, input_size);
+                    s2->first_field=0;
+                    mpeg_decode_gop(avctx, 
+                                          buf_ptr, input_size);
                     break;
                 default:
                     if (start_code >= SLICE_MIN_START_CODE &&