[FFmpeg-devel] [PATCH 4/7] avformat/dss: Avoid using intermediate buffer

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Apr 2 00:26:09 EEST 2021


All one needs is one byte beyond the end of the normal data; and because
the packet is padded, one already has it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavformat/dss.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/libavformat/dss.c b/libavformat/dss.c
index 0b6d68af99..f4545326d2 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -50,7 +50,6 @@ typedef struct DSSDemuxContext {
     int counter;
     int swap;
     int dss_sp_swap_byte;
-    int8_t dss_sp_buf[DSS_FRAME_SIZE + 1];
 
     int packet_size;
     int dss_header_size;
@@ -182,26 +181,23 @@ static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
     ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
 }
 
-static void dss_sp_byte_swap(DSSDemuxContext *ctx,
-                             uint8_t *dst, const uint8_t *src)
+static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
 {
     int i;
 
     if (ctx->swap) {
-        for (i = 3; i < DSS_FRAME_SIZE; i += 2)
-            dst[i] = src[i];
-
         for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
-            dst[i] = src[i + 4];
+            data[i] = data[i + 4];
 
-        dst[1] = ctx->dss_sp_swap_byte;
+        /* Zero the padding. */
+        data[DSS_FRAME_SIZE] = 0;
+        data[1] = ctx->dss_sp_swap_byte;
     } else {
-        memcpy(dst, src, DSS_FRAME_SIZE);
-        ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
+        ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
     }
 
     /* make sure byte 40 is always 0 */
-    dst[DSS_FRAME_SIZE - 2] = 0;
+    data[DSS_FRAME_SIZE - 2] = 0;
     ctx->swap             ^= 1;
 }
 
@@ -229,7 +225,7 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
     pkt->stream_index = 0;
 
     if (ctx->counter < read_size) {
-        ret = avio_read(s->pb, ctx->dss_sp_buf + buff_offset,
+        ret = avio_read(s->pb, pkt->data + buff_offset,
                         ctx->counter);
         if (ret < ctx->counter)
             goto error_eof;
@@ -239,12 +235,13 @@ static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
     ctx->counter -= read_size;
 
-    ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
+    /* This will read one byte into pkt's padding if buff_offset == 3 */
+    ret = avio_read(s->pb, pkt->data + offset + buff_offset,
                     read_size - offset);
     if (ret < read_size - offset)
         goto error_eof;
 
-    dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
+    dss_sp_byte_swap(ctx, pkt->data);
 
     if (ctx->dss_sp_swap_byte < 0) {
         return AVERROR(EAGAIN);
-- 
2.27.0



More information about the ffmpeg-devel mailing list