Browse code

Do not reallocate AVPacket's data when muxing a packet

Originally committed as revision 16616 to svn://svn.ffmpeg.org/ffmpeg/trunk

Luca Abeni authored on 2009/01/15 23:03:07
Showing 5 changed files
... ...
@@ -60,15 +60,11 @@ const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end)
60 60
     return end + 3;
61 61
 }
62 62
 
63
-int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t **buf, int *size)
63
+void ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size)
64 64
 {
65
-    ByteIOContext *pb;
66 65
     const uint8_t *p = buf_in;
67
-    const uint8_t *end = p + *size;
66
+    const uint8_t *end = p + size;
68 67
     const uint8_t *nal_start, *nal_end;
69
-    int ret = url_open_dyn_buf(&pb);
70
-    if(ret < 0)
71
-        return ret;
72 68
 
73 69
     nal_start = ff_avc_find_startcode(p, end);
74 70
     while (nal_start < end) {
... ...
@@ -78,6 +74,17 @@ int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t **buf, int *size)
78 78
         put_buffer(pb, nal_start, nal_end - nal_start);
79 79
         nal_start = nal_end;
80 80
     }
81
+}
82
+
83
+static int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
84
+{
85
+    ByteIOContext *pb;
86
+    int ret = url_open_dyn_buf(&pb);
87
+    if(ret < 0)
88
+        return ret;
89
+
90
+    ff_avc_parse_nal_units(pb, buf_in, *size);
91
+
81 92
     av_freep(buf);
82 93
     *size = url_close_dyn_buf(pb, buf);
83 94
     return 0;
... ...
@@ -92,7 +99,7 @@ int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len)
92 92
             uint32_t sps_size=0, pps_size=0;
93 93
             uint8_t *sps=0, *pps=0;
94 94
 
95
-            int ret = ff_avc_parse_nal_units(data, &buf, &len);
95
+            int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
96 96
             if (ret < 0)
97 97
                 return ret;
98 98
             start = buf;
... ...
@@ -25,7 +25,7 @@
25 25
 #include <stdint.h>
26 26
 #include "avio.h"
27 27
 
28
-int ff_avc_parse_nal_units(const uint8_t *buf_in, uint8_t **buf, int *size);
28
+void ff_avc_parse_nal_units(ByteIOContext *s, const uint8_t *buf, int size);
29 29
 int ff_isom_write_avcc(ByteIOContext *pb, const uint8_t *data, int len);
30 30
 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end);
31 31
 
... ...
@@ -341,13 +341,6 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
341 341
     }
342 342
 
343 343
     if (enc->codec_id == CODEC_ID_H264) {
344
-        /* check if extradata looks like mp4 formated */
345
-        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
346
-            if (ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size) < 0)
347
-                return -1;
348
-            assert(pkt->size);
349
-            size = pkt->size;
350
-        }
351 344
         if (!flv->delay && pkt->dts < 0)
352 345
             flv->delay = -pkt->dts;
353 346
     }
... ...
@@ -368,7 +361,13 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
368 368
         put_byte(pb,1); // AVC NALU
369 369
         put_be24(pb,pkt->pts - pkt->dts);
370 370
     }
371
+    if (enc->codec_id == CODEC_ID_H264 &&
372
+        /* check if extradata looks like mp4 formated */
373
+        enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
374
+        ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
375
+    } else {
371 376
     put_buffer(pb, pkt->data, size);
377
+    }
372 378
     put_be32(pb,size+flags_size+11); // previous tag size
373 379
     flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
374 380
 
... ...
@@ -785,6 +785,7 @@ static void mkv_write_block(AVFormatContext *s, unsigned int blockid, AVPacket *
785 785
 {
786 786
     MatroskaMuxContext *mkv = s->priv_data;
787 787
     ByteIOContext *pb = s->pb;
788
+    AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
788 789
 
789 790
     av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
790 791
            "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
... ...
@@ -794,7 +795,14 @@ static void mkv_write_block(AVFormatContext *s, unsigned int blockid, AVPacket *
794 794
     put_byte(pb, 0x80 | (pkt->stream_index + 1));     // this assumes stream_index is less than 126
795 795
     put_be16(pb, pkt->pts - mkv->cluster_pts);
796 796
     put_byte(pb, flags);
797
+    if (codec->codec_id == CODEC_ID_H264 &&
798
+        codec->extradata_size > 0 && AV_RB32(codec->extradata) == 0x00000001) {
799
+        /* from x264 or from bytestream h264 */
800
+        /* nal reformating needed */
801
+        ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
802
+    } else {
797 803
     put_buffer(pb, pkt->data, pkt->size);
804
+    }
798 805
 }
799 806
 
800 807
 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
... ...
@@ -822,16 +830,6 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
822 822
         av_md5_update(mkv->md5_ctx, pkt->data, FFMIN(200, pkt->size));
823 823
     }
824 824
 
825
-    if (codec->codec_id == CODEC_ID_H264 &&
826
-        codec->extradata_size > 0 && AV_RB32(codec->extradata) == 0x00000001) {
827
-        /* from x264 or from bytestream h264 */
828
-        /* nal reformating needed */
829
-        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
830
-        if (ret < 0)
831
-            return ret;
832
-        assert(pkt->size);
833
-    }
834
-
835 825
     if (codec->codec_type != CODEC_TYPE_SUBTITLE) {
836 826
         mkv_write_block(s, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
837 827
     } else if (codec->codec_id == CODEC_ID_SSA) {
... ...
@@ -1732,15 +1732,7 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1732 1732
         memcpy(trk->vosData, enc->extradata, trk->vosLen);
1733 1733
     }
1734 1734
 
1735
-    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1736
-        /* from x264 or from bytestream h264 */
1737
-        /* nal reformating needed */
1738
-        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
1739
-        if (ret < 0)
1740
-            return ret;
1741
-        assert(pkt->size);
1742
-        size = pkt->size;
1743
-    } else if ((enc->codec_id == CODEC_ID_DNXHD ||
1735
+    if ((enc->codec_id == CODEC_ID_DNXHD ||
1744 1736
                 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
1745 1737
         /* copy frame to create needed atoms */
1746 1738
         trk->vosLen = size;
... ...
@@ -1777,7 +1769,13 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1777 1777
     trk->sampleCount += samplesInChunk;
1778 1778
     mov->mdat_size += size;
1779 1779
 
1780
+    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1781
+        /* from x264 or from bytestream h264 */
1782
+        /* nal reformating needed */
1783
+        ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1784
+    } else {
1780 1785
     put_buffer(pb, pkt->data, size);
1786
+    }
1781 1787
 
1782 1788
     put_flush_packet(pb);
1783 1789
     return 0;