changeset 4222:0267c4299992 libavformat

Do not reallocate AVPacket's data when muxing a packet
author lucabe
date Thu, 15 Jan 2009 14:03:07 +0000
parents 55f448c99135
children 8f2cb573b5b0
files avc.c avc.h flvenc.c matroskaenc.c movenc.c
diffstat 5 files changed, 36 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/avc.c	Thu Jan 15 12:23:03 2009 +0000
+++ b/avc.c	Thu Jan 15 14:03:07 2009 +0000
@@ -60,15 +60,11 @@
     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) {
@@ -78,6 +74,17 @@
         put_buffer(pb, nal_start, nal_end - nal_start);
         nal_start = nal_end;
     }
+}
+
+static int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
+{
+    ByteIOContext *pb;
+    int ret = url_open_dyn_buf(&pb);
+    if(ret < 0)
+        return ret;
+
+    ff_avc_parse_nal_units(pb, buf_in, *size);
+
     av_freep(buf);
     *size = url_close_dyn_buf(pb, buf);
     return 0;
@@ -92,7 +99,7 @@
             uint32_t sps_size=0, pps_size=0;
             uint8_t *sps=0, *pps=0;
 
-            int ret = ff_avc_parse_nal_units(data, &buf, &len);
+            int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
             if (ret < 0)
                 return ret;
             start = buf;
--- a/avc.h	Thu Jan 15 12:23:03 2009 +0000
+++ b/avc.h	Thu Jan 15 14:03:07 2009 +0000
@@ -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);
 
--- a/flvenc.c	Thu Jan 15 12:23:03 2009 +0000
+++ b/flvenc.c	Thu Jan 15 14:03:07 2009 +0000
@@ -341,13 +341,6 @@
     }
 
     if (enc->codec_id == CODEC_ID_H264) {
-        /* check if extradata looks like mp4 formated */
-        if (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;
-        }
         if (!flv->delay && pkt->dts < 0)
             flv->delay = -pkt->dts;
     }
@@ -368,7 +361,13 @@
         put_byte(pb,1); // AVC NALU
         put_be24(pb,pkt->pts - pkt->dts);
     }
+    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);
 
--- a/matroskaenc.c	Thu Jan 15 12:23:03 2009 +0000
+++ b/matroskaenc.c	Thu Jan 15 14:03:07 2009 +0000
@@ -785,6 +785,7 @@
 {
     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",
@@ -794,7 +795,14 @@
     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);
+    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)
@@ -822,16 +830,6 @@
         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 if (codec->codec_id == CODEC_ID_SSA) {
--- a/movenc.c	Thu Jan 15 12:23:03 2009 +0000
+++ b/movenc.c	Thu Jan 15 14:03:07 2009 +0000
@@ -1732,15 +1732,7 @@
         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 ||
+    if ((enc->codec_id == CODEC_ID_DNXHD ||
                 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
         /* copy frame to create needed atoms */
         trk->vosLen = size;
@@ -1777,7 +1769,13 @@
     trk->sampleCount += samplesInChunk;
     mov->mdat_size += 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;