diff mbox series

[FFmpeg-devel,v2] avformat/bethsoftvid: Avoid allocations and frees for palettes

Message ID 20200321033027.17668-1-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,v2] avformat/bethsoftvid: Avoid allocations and frees for palettes | expand

Checks

Context Check Description
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Andreas Rheinhardt March 21, 2020, 3:30 a.m. UTC
by putting the palette in the demuxer's context. This also allows to
remove this demuxer's read_close-function.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/bethsoftvid.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)

Comments

Paul B Mahol March 21, 2020, 9:22 a.m. UTC | #1
lgtm

On 3/21/20, Andreas Rheinhardt <andreas.rheinhardt@gmail.com> wrote:
> by putting the palette in the demuxer's context. This also allows to
> remove this demuxer's read_close-function.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/bethsoftvid.c | 25 +++++++------------------
>  1 file changed, 7 insertions(+), 18 deletions(-)
>
> diff --git a/libavformat/bethsoftvid.c b/libavformat/bethsoftvid.c
> index 4aefb04f14..2d5171a01c 100644
> --- a/libavformat/bethsoftvid.c
> +++ b/libavformat/bethsoftvid.c
> @@ -49,7 +49,8 @@ typedef struct BVID_DemuxContext
>      int bethsoft_global_delay;
>      int video_index;        /**< video stream index */
>      int audio_index;        /**< audio stream index */
> -    uint8_t *palette;
> +    int has_palette;
> +    uint8_t palette[BVID_PALETTE_SIZE];
>
>      int is_finished;
>
> @@ -188,7 +189,7 @@ static int read_frame(BVID_DemuxContext *vid,
> AVIOContext *pb, AVPacket *pkt,
>          pkt->flags |= AV_PKT_FLAG_KEY;
>
>      /* if there is a new palette available, add it to packet side data */
> -    if (vid->palette) {
> +    if (vid->has_palette) {
>          uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
>                                                   BVID_PALETTE_SIZE);
>          if (!pdata) {
> @@ -197,8 +198,7 @@ static int read_frame(BVID_DemuxContext *vid,
> AVIOContext *pb, AVPacket *pkt,
>              goto fail;
>          }
>          memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
> -
> -        av_freep(&vid->palette);
> +        vid->has_palette = 0;
>      }
>
>      vid->nframes--;  // used to check if all the frames were read
> @@ -222,17 +222,14 @@ static int vid_read_packet(AVFormatContext *s,
>      block_type = avio_r8(pb);
>      switch(block_type){
>          case PALETTE_BLOCK:
> -            if (vid->palette) {
> +            if (vid->has_palette) {
>                  av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
> -                av_freep(&vid->palette);
> +                vid->has_palette = 0;
>              }
> -            vid->palette = av_malloc(BVID_PALETTE_SIZE);
> -            if (!vid->palette)
> -                return AVERROR(ENOMEM);
>              if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) !=
> BVID_PALETTE_SIZE) {
> -                av_freep(&vid->palette);
>                  return AVERROR(EIO);
>              }
> +            vid->has_palette = 1;
>              return vid_read_packet(s, pkt);
>
>          case FIRST_AUDIO_BLOCK:
> @@ -284,13 +281,6 @@ static int vid_read_packet(AVFormatContext *s,
>      }
>  }
>
> -static int vid_read_close(AVFormatContext *s)
> -{
> -    BVID_DemuxContext *vid = s->priv_data;
> -    av_freep(&vid->palette);
> -    return 0;
> -}
> -
>  AVInputFormat ff_bethsoftvid_demuxer = {
>      .name           = "bethsoftvid",
>      .long_name      = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
> @@ -298,5 +288,4 @@ AVInputFormat ff_bethsoftvid_demuxer = {
>      .read_probe     = vid_probe,
>      .read_header    = vid_read_header,
>      .read_packet    = vid_read_packet,
> -    .read_close     = vid_read_close,
>  };
> --
> 2.20.1
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Michael Niedermayer March 22, 2020, 12:05 p.m. UTC | #2
On Sat, Mar 21, 2020 at 10:22:08AM +0100, Paul B Mahol wrote:
> lgtm

will apply

thx

[...]
diff mbox series

Patch

diff --git a/libavformat/bethsoftvid.c b/libavformat/bethsoftvid.c
index 4aefb04f14..2d5171a01c 100644
--- a/libavformat/bethsoftvid.c
+++ b/libavformat/bethsoftvid.c
@@ -49,7 +49,8 @@  typedef struct BVID_DemuxContext
     int bethsoft_global_delay;
     int video_index;        /**< video stream index */
     int audio_index;        /**< audio stream index */
-    uint8_t *palette;
+    int has_palette;
+    uint8_t palette[BVID_PALETTE_SIZE];
 
     int is_finished;
 
@@ -188,7 +189,7 @@  static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
         pkt->flags |= AV_PKT_FLAG_KEY;
 
     /* if there is a new palette available, add it to packet side data */
-    if (vid->palette) {
+    if (vid->has_palette) {
         uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
                                                  BVID_PALETTE_SIZE);
         if (!pdata) {
@@ -197,8 +198,7 @@  static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
             goto fail;
         }
         memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
-
-        av_freep(&vid->palette);
+        vid->has_palette = 0;
     }
 
     vid->nframes--;  // used to check if all the frames were read
@@ -222,17 +222,14 @@  static int vid_read_packet(AVFormatContext *s,
     block_type = avio_r8(pb);
     switch(block_type){
         case PALETTE_BLOCK:
-            if (vid->palette) {
+            if (vid->has_palette) {
                 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
-                av_freep(&vid->palette);
+                vid->has_palette = 0;
             }
-            vid->palette = av_malloc(BVID_PALETTE_SIZE);
-            if (!vid->palette)
-                return AVERROR(ENOMEM);
             if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
-                av_freep(&vid->palette);
                 return AVERROR(EIO);
             }
+            vid->has_palette = 1;
             return vid_read_packet(s, pkt);
 
         case FIRST_AUDIO_BLOCK:
@@ -284,13 +281,6 @@  static int vid_read_packet(AVFormatContext *s,
     }
 }
 
-static int vid_read_close(AVFormatContext *s)
-{
-    BVID_DemuxContext *vid = s->priv_data;
-    av_freep(&vid->palette);
-    return 0;
-}
-
 AVInputFormat ff_bethsoftvid_demuxer = {
     .name           = "bethsoftvid",
     .long_name      = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
@@ -298,5 +288,4 @@  AVInputFormat ff_bethsoftvid_demuxer = {
     .read_probe     = vid_probe,
     .read_header    = vid_read_header,
     .read_packet    = vid_read_packet,
-    .read_close     = vid_read_close,
 };