[FFmpeg-devel] [PATCH] libavformat/avc.c annexb parsing corrupts packets

matteo.elli at m-elli.it matteo.elli
Mon Jul 27 17:10:33 CEST 2009


Hi,

code in libavformat/avc.c is seldom executed but it surely does when you transmux (vcodec=copy) from h264 in a ts container to h264 in an mp4/mov/mkv/avi container.

The code there fails to handle annexb 00 00 01 NAL three byets prefixes properly and cuts the last byte in the previous NAL corrupting the whole frame.

Here is my proposed fix.

Index: libavformat/avc.c
===================================================================
--- libavformat/avc.c	(revision 19515)
+++ libavformat/avc.c	(working copy)
@@ -39,15 +39,15 @@
         if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
             if( p[1] == 0 ) {
                 if( p[0] == 0 && p[2] == 1 )
-                    return p-1;
+                    return p;
                 if( p[2] == 0 && p[3] == 1 )
-                    return p;
+                    return p+1;
             }
             if( p[3] == 0 ) {
                 if( p[2] == 0 && p[4] == 1 )
-                    return p+1;
+                    return p+2;
                 if( p[4] == 0 && p[5] == 1 )
-                    return p+2;
+                    return p+3;
             }
         }
     }
@@ -69,8 +69,9 @@
     size = 0;
     nal_start = ff_avc_find_startcode(p, end);
     while (nal_start < end) {
-        while(!*(nal_start++));
+        while(nal_start < end && !*(nal_start++)); if(nal_start == end) break;
         nal_end = ff_avc_find_startcode(nal_start, end);
+		while(nal_end > nal_start && !*(nal_end - 1)) nal_end--;
         put_be32(pb, nal_end - nal_start);
         put_buffer(pb, nal_start, nal_end - nal_start);
         size += 4 + nal_end - nal_start;

----------

Matteo Elli  
-------------- next part --------------
A non-text attachment was scrubbed...
Name: submission.diff
Type: application/octet-stream
Size: 1294 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090727/3480c56d/attachment.obj>



More information about the ffmpeg-devel mailing list