diff mbox series

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

Message ID 20210204191005.48190-16-jamrial@gmail.com
State Superseded
Headers show
Series deprecate av_init_packet() and sizeof(AVPacket) as part of the ABI
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

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

Comments

Andreas Rheinhardt Feb. 8, 2021, 6:07 p.m. UTC | #1
James Almer:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavformat/avienc.c | 18 ++++++++++++------
>  1 file changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/libavformat/avienc.c b/libavformat/avienc.c
> index 1b2cb529b9..58bd081fcb 100644
> --- a/libavformat/avienc.c
> +++ b/libavformat/avienc.c
> @@ -85,6 +85,8 @@ typedef struct AVIStream {
>  
>      int64_t last_dts;
>  
> +    AVPacket *empty_packet;
> +
>      AVIIndex indexes;
>  
>      int64_t strh_flags_offset;
> @@ -275,9 +277,17 @@ static int avi_write_header(AVFormatContext *s)
>      }
>  
>      for (n = 0; n < s->nb_streams; n++) {
> +        AVIStream *avist;
> +
>          s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
>          if (!s->streams[n]->priv_data)
>              return AVERROR(ENOMEM);
> +
> +        avist = s->streams[n]->priv_data;
> +        avist->empty_packet = av_packet_alloc();
> +        if (!avist->empty_packet)
> +            return AVERROR(ENOMEM);
> +        avist->empty_packet->stream_index = n;

There is no need to allocate a packet per AVIStream.

>      }
>  
>      /* header list */
> @@ -745,18 +755,13 @@ static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
>      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_write_packet_internal(s, avist->empty_packet);
>          ff_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
>      }
>  
> @@ -985,6 +990,7 @@ static void avi_deinit(AVFormatContext *s)
>          for (int j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
>              av_freep(&avist->indexes.cluster[j]);
>          av_freep(&avist->indexes.cluster);
> +        av_packet_free(&avist->empty_packet);
>          avist->indexes.ents_allocated = avist->indexes.entry = 0;
>      }
>  }
>
diff mbox series

Patch

diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 1b2cb529b9..58bd081fcb 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -85,6 +85,8 @@  typedef struct AVIStream {
 
     int64_t last_dts;
 
+    AVPacket *empty_packet;
+
     AVIIndex indexes;
 
     int64_t strh_flags_offset;
@@ -275,9 +277,17 @@  static int avi_write_header(AVFormatContext *s)
     }
 
     for (n = 0; n < s->nb_streams; n++) {
+        AVIStream *avist;
+
         s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
         if (!s->streams[n]->priv_data)
             return AVERROR(ENOMEM);
+
+        avist = s->streams[n]->priv_data;
+        avist->empty_packet = av_packet_alloc();
+        if (!avist->empty_packet)
+            return AVERROR(ENOMEM);
+        avist->empty_packet->stream_index = n;
     }
 
     /* header list */
@@ -745,18 +755,13 @@  static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
     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_write_packet_internal(s, avist->empty_packet);
         ff_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
     }
 
@@ -985,6 +990,7 @@  static void avi_deinit(AVFormatContext *s)
         for (int j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
             av_freep(&avist->indexes.cluster[j]);
         av_freep(&avist->indexes.cluster);
+        av_packet_free(&avist->empty_packet);
         avist->indexes.ents_allocated = avist->indexes.entry = 0;
     }
 }