Luca Abeni <[EMAIL PROTECTED]> added the comment:

If it can help, this is the (almost untested) patch on which I was working:

diff --git a/libavformat/avc.c b/libavformat/avc.c
index 0c180c8..4c17518 100644
--- a/libavformat/avc.c
+++ b/libavformat/avc.c
@@ -58,15 +58,11 @@ const uint8_t *ff_avc_find_startcode(const uint8_t *p, const
uint8_t *end)
     return end + 3;
 }
 
-int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t **buf, int *size)
+void ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size)
 {
-    ByteIOContext *pb;
     const uint8_t *p = buf_in;
-    const uint8_t *end = p + *size;
+    const uint8_t *end = p + size;
     const uint8_t *nal_start, *nal_end;
-    int ret = url_open_dyn_buf(&pb);
-    if(ret < 0)
-        return ret;
 
     nal_start = ff_avc_find_startcode(p, end);
     while (nal_start < end) {
@@ -76,9 +72,6 @@ int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t
**buf, int *size)
         put_buffer(pb, nal_start, nal_end - nal_start);
         nal_start = nal_end;
     }
-    av_freep(buf);
-    *size = url_close_dyn_buf(pb, buf);
-    return 0;
 }
 
 int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len)
@@ -86,14 +79,16 @@ int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t
*data, int len)
     if (len > 6) {
         /* check for h264 start code */
         if (AV_RB32(data) == 0x00000001) {
-            uint8_t *buf=NULL, *end, *start;
+            uint8_t *buf=NULL, *end;
             uint32_t sps_size=0, pps_size=0;
             uint8_t *sps=0, *pps=0;
-
-            int ret = ff_avc_parse_nal_units(data, &buf, &len);
-            if (ret < 0)
+            ByteIOContext *pb1;
+            int ret = url_open_dyn_buf(&pb1);
+            if(ret < 0)
                 return ret;
-            start = buf;
+
+            ff_avc_parse_nal_units(pb1, data, len);
+            len = url_close_dyn_buf(pb1, &buf);
             end = buf + len;
 
             /* look for sps and pps */
@@ -126,7 +121,7 @@ int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t
*data, int len)
             put_byte(pb, 1); /* number of pps */
             put_be16(pb, pps_size);
             put_buffer(pb, pps, pps_size);
-            av_free(start);
+            av_free(buf);
         } else {
             put_buffer(pb, data, len);
         }
diff --git a/libavformat/avc.h b/libavformat/avc.h
index 334aa43..f675be7 100644
--- a/libavformat/avc.h
+++ b/libavformat/avc.h
@@ -25,7 +25,7 @@
 #include <stdint.h>
 #include "avio.h"
 
-int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t **buf, int *size);
+void ff_avc_parse_nal_units(ByteIOContext *s, const uint8_t *buf, int size);
 int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len);
 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end);
 
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 2941356..3b730bb 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -339,10 +339,6 @@ static int flv_write_packet(AVFormatContext *s, AVPacket 
*pkt)
     if (enc->codec_id == CODEC_ID_H264 &&
         /* check if extradata looks like mp4 formated */
         enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
-        if (ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size) < 0)
-            return -1;
-        assert(pkt->size);
-        size = pkt->size;
         /* cast needed to get negative value */
         if (!flv->delay && (int32_t)pkt->dts < 0)
             flv->delay = -(int32_t)pkt->dts;
@@ -364,7 +360,13 @@ static int flv_write_packet(AVFormatContext *s, AVPacket 
*pkt)
         put_byte(pb,1); // AVC NALU
         put_be24(pb,pkt->pts - (int32_t)pkt->dts);
     }
-    put_buffer(pb, pkt->data, size);
+    if (enc->codec_id == CODEC_ID_H264 &&
+        /* check if extradata looks like mp4 formated */
+        enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
+        ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
+    } else {
+        put_buffer(pb, pkt->data, size);
+    }
     put_be32(pb,size+flags_size+11); // previous tag size
     flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + 
pkt->duration);
 
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index a138437..9be4339 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -713,6 +713,7 @@ static void mkv_write_block(AVFormatContext *s, unsigned int
blockid, AVPacket *
 {
     MatroskaMuxContext *mkv = s->priv_data;
     ByteIOContext *pb = s->pb;
+    AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
 
     av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
            "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
@@ -722,7 +723,14 @@ static void mkv_write_block(AVFormatContext *s, unsigned
int blockid, AVPacket *
     put_byte(pb, 0x80 | (pkt->stream_index + 1));     // this assumes
stream_index is less than 126
     put_be16(pb, pkt->pts - mkv->cluster_pts);
     put_byte(pb, flags);
-    put_buffer(pb, pkt->data, pkt->size);
+    if (codec->codec_id == CODEC_ID_H264 &&
+        codec->extradata_size > 0 && AV_RB32(codec->extradata) == 0x00000001) {
+        /* from x264 or from bytestream h264 */
+        /* nal reformating needed */
+        ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
+    } else {
+        put_buffer(pb, pkt->data, pkt->size);
+    }
 }
 
 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
@@ -749,16 +757,6 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket 
*pkt)
         av_md5_update(mkv->md5_ctx, pkt->data, FFMIN(200, pkt->size));
     }
 
-    if (codec->codec_id == CODEC_ID_H264 &&
-        codec->extradata_size > 0 && AV_RB32(codec->extradata) == 0x00000001) {
-        /* from x264 or from bytestream h264 */
-        /* nal reformating needed */
-        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
-        if (ret < 0)
-            return ret;
-        assert(pkt->size);
-    }
-
     if (codec->codec_type != CODEC_TYPE_SUBTITLE) {
         mkv_write_block(s, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
     } else {
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index d41fd97..c5c6e7c 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1603,15 +1603,7 @@ static int mov_write_packet(AVFormatContext *s, AVPacket
*pkt)
         memcpy(trk->vosData, enc->extradata, trk->vosLen);
     }
 
-    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t
*)trk->vosData != 1) {
-        /* from x264 or from bytestream h264 */
-        /* nal reformating needed */
-        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
-        if (ret < 0)
-            return ret;
-        assert(pkt->size);
-        size = pkt->size;
-    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
+    if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
         /* copy frame header to create needed atoms */
         if (size < 640)
             return -1;
@@ -1647,7 +1639,13 @@ static int mov_write_packet(AVFormatContext *s, AVPacket
*pkt)
     trk->sampleCount += samplesInChunk;
     mov->mdat_size += size;
 
-    put_buffer(pb, pkt->data, size);
+    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t
*)trk->vosData != 1) {
+        /* from x264 or from bytestream h264 */
+        /* nal reformating needed */
+        ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
+    } else {
+        put_buffer(pb, pkt->data, size);
+    }
 
     put_flush_packet(pb);
     return 0;

______________________________________________________
FFmpeg issue tracker <[EMAIL PROTECTED]>
<https://roundup.mplayerhq.hu/roundup/ffmpeg/issue535>
______________________________________________________

Reply via email to