changeset 1218:358bbc952e27 libavcodec

10l (returning negative number of consumed bytes if the first startcode of a frame was split between 2 buffers)
author michaelni
date Sun, 27 Apr 2003 01:11:26 +0000
parents eb2affe57a2a
children ad21a7b2349b
files mpeg12.c mpegvideo.c mpegvideo.h
diffstat 3 files changed, 39 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mpeg12.c	Fri Apr 25 20:03:17 2003 +0000
+++ b/mpeg12.c	Sun Apr 27 01:11:26 2003 +0000
@@ -2190,7 +2190,7 @@
         }
     }        
     pc->state= state;
-    return -1;
+    return END_NOT_FOUND;
 }
 
 /* handle buffering and image synchronisation */
@@ -2218,9 +2218,7 @@
     }
 
     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 @@
                         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);
--- a/mpegvideo.c	Fri Apr 25 20:03:17 2003 +0000
+++ b/mpegvideo.c	Sun Apr 27 01:11:26 2003 +0000
@@ -2770,17 +2770,33 @@
  */
 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 @@
         *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;
 }
 
--- a/mpegvideo.h	Fri Apr 25 20:03:17 2003 +0000
+++ b/mpegvideo.h	Sun Apr 27 01:11:26 2003 +0000
@@ -205,8 +205,10 @@
     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_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);