[FFmpeg-cvslog] avcodec/ylc: Use only one temporary buffer

Andreas Rheinhardt git at videolan.org
Mon Mar 8 18:39:36 EET 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Mon Mar  8 14:12:06 2021 +0100| [50645e50448bed39172af90d92bde92ee84a6055] | committer: Andreas Rheinhardt

avcodec/ylc: Use only one temporary buffer

The effective lifetime of the buffer used to build the VLCs and
the buffer containing the bitstream is disjoint, so that one can use
a common buffer for both.

Reviewed-by: Paul B Mahol <onemda at gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=50645e50448bed39172af90d92bde92ee84a6055
---

 libavcodec/ylc.c | 44 ++++++++++++++++++--------------------------
 1 file changed, 18 insertions(+), 26 deletions(-)

diff --git a/libavcodec/ylc.c b/libavcodec/ylc.c
index a200d04acd..6dc3e60fec 100644
--- a/libavcodec/ylc.c
+++ b/libavcodec/ylc.c
@@ -39,10 +39,8 @@
 typedef struct YLCContext {
     VLC vlc[4];
     uint32_t table[1024];
-    uint8_t *table_bits;
-    uint8_t *bitstream_bits;
-    int table_bits_size;
-    int bitstream_bits_size;
+    uint8_t *buffer;
+    int buffer_size;
     BswapDSPContext bdsp;
 } YLCContext;
 
@@ -312,17 +310,18 @@ static int decode_frame(AVCodecContext *avctx,
     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
         return ret;
 
-    av_fast_malloc(&s->table_bits, &s->table_bits_size,
-                   boffset - toffset + AV_INPUT_BUFFER_PADDING_SIZE);
-    if (!s->table_bits)
+    av_fast_malloc(&s->buffer, &s->buffer_size,
+                   FFMAX(boffset - toffset, avpkt->size - boffset)
+                       + AV_INPUT_BUFFER_PADDING_SIZE);
+    if (!s->buffer)
         return AVERROR(ENOMEM);
 
-    memcpy(s->table_bits, avpkt->data + toffset, boffset - toffset);
-    memset(s->table_bits + boffset - toffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
-    s->bdsp.bswap_buf((uint32_t *) s->table_bits,
-                      (uint32_t *) s->table_bits,
+    memcpy(s->buffer, avpkt->data + toffset, boffset - toffset);
+    memset(s->buffer + boffset - toffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+    s->bdsp.bswap_buf((uint32_t *) s->buffer,
+                      (uint32_t *) s->buffer,
                       (boffset - toffset + 3) >> 2);
-    if ((ret = init_get_bits8(&gb, s->table_bits, boffset - toffset)) < 0)
+    if ((ret = init_get_bits8(&gb, s->buffer, boffset - toffset)) < 0)
         return ret;
 
     for (x = 0; x < 1024; x++) {
@@ -345,17 +344,12 @@ static int decode_frame(AVCodecContext *avctx,
     if (ret < 0)
         return ret;
 
-    av_fast_malloc(&s->bitstream_bits, &s->bitstream_bits_size,
-                   avpkt->size - boffset + AV_INPUT_BUFFER_PADDING_SIZE);
-    if (!s->bitstream_bits)
-        return AVERROR(ENOMEM);
-
-    memcpy(s->bitstream_bits, avpkt->data + boffset, avpkt->size - boffset);
-    memset(s->bitstream_bits + avpkt->size - boffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
-    s->bdsp.bswap_buf((uint32_t *) s->bitstream_bits,
-                      (uint32_t *) s->bitstream_bits,
+    memcpy(s->buffer, avpkt->data + boffset, avpkt->size - boffset);
+    memset(s->buffer + avpkt->size - boffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+    s->bdsp.bswap_buf((uint32_t *) s->buffer,
+                      (uint32_t *) s->buffer,
                       (avpkt->size - boffset) >> 2);
-    if ((ret = init_get_bits8(&gb, s->bitstream_bits, avpkt->size - boffset)) < 0)
+    if ((ret = init_get_bits8(&gb, s->buffer, avpkt->size - boffset)) < 0)
         return ret;
 
     dst = p->data[0];
@@ -461,10 +455,8 @@ static av_cold int decode_end(AVCodecContext *avctx)
     ff_free_vlc(&s->vlc[1]);
     ff_free_vlc(&s->vlc[2]);
     ff_free_vlc(&s->vlc[3]);
-    av_freep(&s->table_bits);
-    s->table_bits_size = 0;
-    av_freep(&s->bitstream_bits);
-    s->bitstream_bits_size = 0;
+    av_freep(&s->buffer);
+    s->buffer_size = 0;
 
     return 0;
 }



More information about the ffmpeg-cvslog mailing list