[FFmpeg-devel] [PATCH 24/48] avformat/movenc: use av_packet_alloc() to allocate packets

James Almer jamrial at gmail.com
Fri Mar 5 18:33:15 EET 2021


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/movenc.c     | 101 +++++++++++++++++++++++++--------------
 libavformat/movenc.h     |   4 +-
 libavformat/movenchint.c |  19 ++++----
 3 files changed, 77 insertions(+), 47 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ccb9768564..7aa0a9888a 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -365,7 +365,7 @@ static int mov_write_ac3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *trac
 }
 
 struct eac3_info {
-    AVPacket pkt;
+    AVPacket *pkt;
     uint8_t ec3_done;
     uint8_t num_blocks;
 
@@ -407,6 +407,9 @@ static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
         return AVERROR(ENOMEM);
     info = track->eac3_priv;
 
+    if (!info->pkt && !(info->pkt = av_packet_alloc()))
+        return AVERROR(ENOMEM);
+
     if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) < 0) {
         /* drop the packets until we see a good one */
         if (!track->entry) {
@@ -511,20 +514,20 @@ concatenate:
     }
 
     if (!info->num_blocks) {
-        ret = av_packet_ref(&info->pkt, pkt);
+        ret = av_packet_ref(info->pkt, pkt);
         if (!ret)
             info->num_blocks = num_blocks;
         goto end;
     } else {
-        if ((ret = av_grow_packet(&info->pkt, pkt->size)) < 0)
+        if ((ret = av_grow_packet(info->pkt, pkt->size)) < 0)
             goto end;
-        memcpy(info->pkt.data + info->pkt.size - pkt->size, pkt->data, pkt->size);
+        memcpy(info->pkt->data + info->pkt->size - pkt->size, pkt->data, pkt->size);
         info->num_blocks += num_blocks;
-        info->pkt.duration += pkt->duration;
+        info->pkt->duration += pkt->duration;
         if (info->num_blocks != 6)
             goto end;
         av_packet_unref(pkt);
-        av_packet_move_ref(pkt, &info->pkt);
+        av_packet_move_ref(pkt, info->pkt);
         info->num_blocks = 0;
     }
     ret = pkt->size;
@@ -3732,7 +3735,7 @@ static int mov_write_covr(AVIOContext *pb, AVFormatContext *s)
     for (i = 0; i < s->nb_streams; i++) {
         MOVTrack *trk = &mov->tracks[i];
 
-        if (!is_cover_image(trk->st) || trk->cover_image.size <= 0)
+        if (!is_cover_image(trk->st) || trk->cover_image->size <= 0)
             continue;
 
         if (!pos) {
@@ -3740,11 +3743,11 @@ static int mov_write_covr(AVIOContext *pb, AVFormatContext *s)
             avio_wb32(pb, 0);
             ffio_wfourcc(pb, "covr");
         }
-        avio_wb32(pb, 16 + trk->cover_image.size);
+        avio_wb32(pb, 16 + trk->cover_image->size);
         ffio_wfourcc(pb, "data");
         avio_wb32(pb, trk->tag);
         avio_wb32(pb , 0);
-        avio_write(pb, trk->cover_image.data, trk->cover_image.size);
+        avio_write(pb, trk->cover_image->data, trk->cover_image->size);
     }
 
     return pos ? update_size(pb, pos) : 0;
@@ -5967,20 +5970,20 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
 static int mov_write_subtitle_end_packet(AVFormatContext *s,
                                          int stream_index,
                                          int64_t dts) {
-    AVPacket end;
+    MOVMuxContext *mov = s->priv_data;
+    AVPacket *end = mov->pkt;
     uint8_t data[2] = {0};
     int ret;
 
-    av_init_packet(&end);
-    end.size = sizeof(data);
-    end.data = data;
-    end.pts = dts;
-    end.dts = dts;
-    end.duration = 0;
-    end.stream_index = stream_index;
+    end->size = sizeof(data);
+    end->data = data;
+    end->pts = dts;
+    end->dts = dts;
+    end->duration = 0;
+    end->stream_index = stream_index;
 
-    ret = mov_write_single_packet(s, &end);
-    av_packet_unref(&end);
+    ret = mov_write_single_packet(s, end);
+    av_packet_unref(end);
 
     return ret;
 }
@@ -6007,7 +6010,7 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
             return 0;
         }
 
-        if ((ret = av_packet_ref(&trk->cover_image, pkt)) < 0)
+        if ((ret = av_packet_ref(trk->cover_image, pkt)) < 0)
             return ret;
 
         return 0;
@@ -6093,7 +6096,7 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
 
     MOVMuxContext *mov = s->priv_data;
     MOVTrack *track = &mov->tracks[tracknum];
-    AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
+    AVPacket *pkt = mov->pkt;
     int i, len;
 
     track->mode = mov->mode;
@@ -6155,13 +6158,16 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
     }
 #endif
 
+    pkt->stream_index = tracknum;
+    pkt->flags = AV_PKT_FLAG_KEY;
+
     for (i = 0; i < s->nb_chapters; i++) {
         AVChapter *c = s->chapters[i];
         AVDictionaryEntry *t;
 
         int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
-        pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
-        pkt.duration = end - pkt.dts;
+        pkt->pts = pkt->dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
+        pkt->duration = end - pkt->dts;
 
         if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
             static const char encd[12] = {
@@ -6169,18 +6175,22 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
                 'e',  'n',  'c',  'd',
                 0x00, 0x00, 0x01, 0x00 };
             len      = strlen(t->value);
-            pkt.size = len + 2 + 12;
-            pkt.data = av_malloc(pkt.size);
-            if (!pkt.data)
+            pkt->size = len + 2 + 12;
+            pkt->data = av_malloc(pkt->size);
+            if (!pkt->data) {
+                av_packet_unref(pkt);
                 return AVERROR(ENOMEM);
-            AV_WB16(pkt.data, len);
-            memcpy(pkt.data + 2, t->value, len);
-            memcpy(pkt.data + len + 2, encd, sizeof(encd));
-            ff_mov_write_packet(s, &pkt);
-            av_freep(&pkt.data);
+            }
+            AV_WB16(pkt->data, len);
+            memcpy(pkt->data + 2, t->value, len);
+            memcpy(pkt->data + len + 2, encd, sizeof(encd));
+            ff_mov_write_packet(s, pkt);
+            av_freep(&pkt->data);
         }
     }
 
+    av_packet_unref(mov->pkt);
+
     return 0;
 }
 
@@ -6200,9 +6210,9 @@ static int mov_create_timecode_track(AVFormatContext *s, int index, int src_inde
     MOVTrack *track     = &mov->tracks[index];
     AVStream *src_st    = s->streams[src_index];
     uint8_t data[4];
-    AVPacket pkt    = { .data = data, .stream_index = index,
-                        .flags = AV_PKT_FLAG_KEY, .size = 4 };
+    AVPacket *pkt = mov->pkt;
     AVRational rate = find_fps(s, src_st);
+    int ret;
 
     /* tmcd track based on video stream */
     track->mode      = mov->mode;
@@ -6224,8 +6234,14 @@ static int mov_create_timecode_track(AVFormatContext *s, int index, int src_inde
     track->st->avg_frame_rate = av_inv_q(rate);
 
     /* the tmcd track just contains one packet with the frame number */
-    AV_WB32(pkt.data, tc.start);
-    return ff_mov_write_packet(s, &pkt);
+    pkt->data = data;
+    pkt->stream_index = index;
+    pkt->flags = AV_PKT_FLAG_KEY;
+    pkt->size = 4;
+    AV_WB32(pkt->data, tc.start);
+    ret = ff_mov_write_packet(s, pkt);
+    av_packet_unref(pkt);
+    return ret;
 }
 
 /*
@@ -6286,6 +6302,8 @@ static void mov_free(AVFormatContext *s)
     MOVMuxContext *mov = s->priv_data;
     int i;
 
+    av_packet_free(&mov->pkt);
+
     if (!mov->tracks)
         return;
 
@@ -6300,11 +6318,11 @@ static void mov_free(AVFormatContext *s)
             av_freep(&mov->tracks[i].par);
         av_freep(&mov->tracks[i].cluster);
         av_freep(&mov->tracks[i].frag_info);
-        av_packet_unref(&mov->tracks[i].cover_image);
+        av_packet_free(&mov->tracks[i].cover_image);
 
         if (mov->tracks[i].eac3_priv) {
             struct eac3_info *info = mov->tracks[i].eac3_priv;
-            av_packet_unref(&info->pkt);
+            av_packet_free(&info->pkt);
             av_freep(&mov->tracks[i].eac3_priv);
         }
         if (mov->tracks[i].vos_len)
@@ -6535,6 +6553,10 @@ static int mov_init(AVFormatContext *s)
         mov->nb_streams += mov->nb_meta_tmcd;
     }
 
+    mov->pkt = av_packet_alloc();
+    if (!mov->pkt)
+        return AVERROR(ENOMEM);
+
     // Reserve an extra stream for chapters for the case where chapters
     // are written in the trailer
     mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
@@ -6642,6 +6664,11 @@ static int mov_init(AVFormatContext *s)
                 av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n");
                 return AVERROR_PATCHWELCOME;
             }
+            if (is_cover_image(st)) {
+                track->cover_image = av_packet_alloc();
+                if (!track->cover_image)
+                    return AVERROR(ENOMEM);
+            }
         } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
             track->timescale = st->codecpar->sample_rate;
             if (!st->codecpar->frame_size && !av_get_bits_per_sample(st->codecpar->codec_id)) {
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 997b2d61c0..cdbc4074c3 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -134,7 +134,7 @@ typedef struct MOVTrack {
     uint32_t    default_size;
 
     HintSampleQueue sample_queue;
-    AVPacket cover_image;
+    AVPacket *cover_image;
 
     AVIOContext *mdat_buf;
     int64_t     data_offset;
@@ -215,6 +215,8 @@ typedef struct MOVMuxContext {
     int per_stream_grouping;
     AVFormatContext *fc;
 
+    AVPacket *pkt;
+
     int use_editlist;
     float gamma;
 
diff --git a/libavformat/movenchint.c b/libavformat/movenchint.c
index 964026ec71..47276091f3 100644
--- a/libavformat/movenchint.c
+++ b/libavformat/movenchint.c
@@ -408,7 +408,7 @@ int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
     uint8_t *buf = NULL;
     int size;
     AVIOContext *hintbuf = NULL;
-    AVPacket hint_pkt;
+    AVPacket *hint_pkt = mov->pkt;
     int ret = 0, count;
 
     if (!rtp_ctx)
@@ -437,21 +437,22 @@ int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
     /* Open a buffer for writing the hint */
     if ((ret = avio_open_dyn_buf(&hintbuf)) < 0)
         goto done;
-    av_init_packet(&hint_pkt);
-    count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt.dts);
+    av_packet_unref(hint_pkt);
+    count = write_hint_packets(hintbuf, buf, size, trk, &hint_pkt->dts);
     av_freep(&buf);
 
     /* Write the hint data into the hint track */
-    hint_pkt.size = size = avio_close_dyn_buf(hintbuf, &buf);
-    hint_pkt.data = buf;
-    hint_pkt.pts  = hint_pkt.dts;
-    hint_pkt.stream_index = track_index;
+    hint_pkt->size = size = avio_close_dyn_buf(hintbuf, &buf);
+    hint_pkt->data = buf;
+    hint_pkt->pts  = hint_pkt->dts;
+    hint_pkt->stream_index = track_index;
     if (pkt->flags & AV_PKT_FLAG_KEY)
-        hint_pkt.flags |= AV_PKT_FLAG_KEY;
+        hint_pkt->flags |= AV_PKT_FLAG_KEY;
     if (count > 0)
-        ff_mov_write_packet(s, &hint_pkt);
+        ff_mov_write_packet(s, hint_pkt);
 done:
     av_free(buf);
+    av_packet_unref(hint_pkt);
     sample_queue_retain(&trk->sample_queue);
     return ret;
 }
-- 
2.30.1



More information about the ffmpeg-devel mailing list