From bb463d81020a2f3c5cf3403e18f980171773f48a Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michaelni@gmx.at>
Date: Sun, 27 Apr 2003 01:11:26 +0000
Subject: [PATCH] 10l (returning negative number of consumed bytes if the first
 startcode of a frame was split between 2 buffers)

Originally committed as revision 1824 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/mpeg12.c    |  8 +++-----
 libavcodec/mpegvideo.c | 35 ++++++++++++++++++++++++++++++++---
 libavcodec/mpegvideo.h |  5 ++++-
 3 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index d16eaa1da6c..d3cef315e4a 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -2190,7 +2190,7 @@ static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
         }
     }        
     pc->state= state;
-    return -1;
+    return END_NOT_FOUND;
 }
 
 /* handle buffering and image synchronisation */
@@ -2218,9 +2218,7 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
     }
 
     if(s2->flags&CODEC_FLAG_TRUNCATED){
-        int next;
-        
-        next= mpeg1_find_frame_end(s2, buf, buf_size);
+        int next= mpeg1_find_frame_end(s2, buf, buf_size);
         
         if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
             return buf_size;
@@ -2288,7 +2286,7 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
                         if (ret == DECODE_SLICE_EOP) {
                             if(s2->last_picture_ptr) //FIXME merge with the stuff in mpeg_decode_slice
                                 *data_size = sizeof(AVPicture);
-                            return FFMAX(1, buf_ptr - buf - s2->parse_context.last_index);
+                            return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
                         }else if(ret < 0){
                             if(ret == DECODE_SLICE_ERROR)
                                 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 2ec42aea399..b63a011246b 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -2770,17 +2770,33 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  */
 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
     ParseContext *pc= &s->parse_context;
-        
+
+#if 0
+    if(pc->overread){
+        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
+        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
+    }
+#endif
+
+    /* copy overreaded byes from last frame into buffer */
+    for(; pc->overread>0; pc->overread--){
+        pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
+    }
+    
     pc->last_index= pc->index;
 
-    if(next==-1){
+    /* copy into buffer end return */
+    if(next == END_NOT_FOUND){
         pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
 
         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
         pc->index += *buf_size;
         return -1;
     }
-
+    
+    pc->overread_index= pc->index + next;
+    
+    /* append to buffer */
     if(pc->index){
         pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
 
@@ -2790,6 +2806,19 @@ int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size)
         *buf_size= pc->last_index + next;
     }
 
+    /* store overread bytes */
+    for(;next < 0; next++){
+        pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
+        pc->overread++;
+    }
+
+#if 0
+    if(pc->overread){
+        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
+        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
+    }
+#endif
+
     return 0;
 }
 
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index dc15a51f89f..2cbc0ef1929 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -205,8 +205,10 @@ typedef struct ParseContext{
     int index;
     int last_index;
     int buffer_size;
-    int state;
+    uint32_t state;             ///< contains the last few bytes in MSB order
     int frame_start_found;
+    int overread;               ///< the number of bytes which where irreversibly read from the next frame
+    int overread_index;         ///< the index into ParseContext.buffer of the overreaded bytes
 } ParseContext;
 
 struct MpegEncContext;
@@ -709,6 +711,7 @@ void ff_draw_horiz_band(MpegEncContext *s, int y, int h);
 void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h, 
                                     int src_x, int src_y, int w, int h);
 char ff_get_pict_type_char(int pict_type);
+#define END_NOT_FOUND -100
 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size);
 void ff_print_debug_info(MpegEncContext *s, Picture *pict);
 
-- 
GitLab