diff mbox series

[FFmpeg-devel,15/50,v2] avformat/avienc: use av_packet_alloc() to allocate packets

Message ID 20210209142408.311-1-jamrial@gmail.com
State New
Headers show
Series Untitled series #3256
Related show

Commit Message

James Almer Feb. 9, 2021, 2:24 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/avienc.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 1b2cb529b9..aee168dd96 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -66,6 +66,7 @@  typedef struct AVIIndex {
 
 typedef struct AVIContext {
     const AVClass *class;
+    AVPacket *empty_packet;
     int64_t riff_start, movi_list, odml_list;
     int64_t frames_hdr_all;
     int riff_id;
@@ -274,6 +275,10 @@  static int avi_write_header(AVFormatContext *s)
         return AVERROR(EINVAL);
     }
 
+    avi->empty_packet = av_packet_alloc();
+    if (!avi->empty_packet)
+        return AVERROR(ENOMEM);
+
     for (n = 0; n < s->nb_streams; n++) {
         s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
         if (!s->streams[n]->priv_data)
@@ -739,24 +744,21 @@  static int avi_write_idx1(AVFormatContext *s)
 
 static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
 {
+    AVIContext *avi = s->priv_data;
     AVIStream *avist    = s->streams[stream_index]->priv_data;
     AVCodecParameters *par = s->streams[stream_index]->codecpar;
 
     ff_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
     while (par->block_align == 0 && dts != AV_NOPTS_VALUE &&
            dts > avist->packet_count && par->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
-        AVPacket empty_packet;
 
         if (dts - avist->packet_count > 60000) {
             av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
             return AVERROR(EINVAL);
         }
 
-        av_init_packet(&empty_packet);
-        empty_packet.size         = 0;
-        empty_packet.data         = NULL;
-        empty_packet.stream_index = stream_index;
-        avi_write_packet_internal(s, &empty_packet);
+        avi->empty_packet->stream_index = stream_index;
+        avi_write_packet_internal(s, avi->empty_packet);
         ff_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
     }
 
@@ -978,6 +980,10 @@  static int avi_write_trailer(AVFormatContext *s)
 
 static void avi_deinit(AVFormatContext *s)
 {
+    AVIContext *avi = s->priv_data;
+
+    av_packet_free(&avi->empty_packet);
+
     for (int i = 0; i < s->nb_streams; i++) {
         AVIStream *avist = s->streams[i]->priv_data;
         if (!avist)