diff mbox series

[FFmpeg-devel,2/3] avcodec/adpcm_swf: remove memory allocation during trellis encoding

Message ID 20210401113449.12680-2-zane@zanevaniperen.com
State Accepted
Commit 9e89a23eac1d5ab6f20c5c281d224e9218312a0b
Headers show
Series [FFmpeg-devel,1/3] avcodec/adpcmenc: don't share a single AVClass between multiple AVCodecs. | expand

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

Zane van Iperen April 1, 2021, 11:34 a.m. UTC
The block size is hardcoded, so the buffer size is always known.
Statically allocate the buffer on the stack.

Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
---
 libavcodec/adpcmenc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Zane van Iperen April 3, 2021, 1:19 a.m. UTC | #1
On 1/4/21 9:34 pm, Zane van Iperen wrote:
> The block size is hardcoded, so the buffer size is always known.
> Statically allocate the buffer on the stack.
> 
> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
> ---
>   libavcodec/adpcmenc.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 

Ping on parts 2 and 3.

My concern with part 3 is the re-initialisation of fields from extradata. I'm not sure
if it should be done on only init, or on both init and flush.
Zane van Iperen April 8, 2021, 7:15 a.m. UTC | #2
On 3/4/21 11:19 am, Zane van Iperen wrote:
> 
> 
> On 1/4/21 9:34 pm, Zane van Iperen wrote:
>> The block size is hardcoded, so the buffer size is always known.
>> Statically allocate the buffer on the stack.
>>
>> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
>> ---
>>   libavcodec/adpcmenc.c | 7 ++++---
>>   1 file changed, 4 insertions(+), 3 deletions(-)
>>
> 
> Ping on parts 2 and 3.
> 
> My concern with part 3 is the re-initialisation of fields from extradata. I'm not sure
> if it should be done on only init, or on both init and flush.
> _______________________________________________

Ping again. Will apply in a few days if no objections.
diff mbox series

Patch

diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index 58308dae47..9dc77d519a 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -722,6 +722,9 @@  static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
         n = frame->nb_samples - 1;
 
+        /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
+        av_assert0(n == 4095);
+
         // store AdpcmCodeSize
         put_bits(&pb, 2, 2);    // set 4-bit flash adpcm format
 
@@ -735,8 +738,7 @@  static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         }
 
         if (avctx->trellis > 0) {
-            if (!(buf = av_malloc(2 * n)))
-                return AVERROR(ENOMEM);
+            uint8_t buf[8190 /* = 2 * n */];
             adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
                                    &c->status[0], n, avctx->channels);
             if (avctx->channels == 2)
@@ -748,7 +750,6 @@  static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                 if (avctx->channels == 2)
                     put_bits(&pb, 4, buf[n + i]);
             }
-            av_free(buf);
         } else {
             for (i = 1; i < frame->nb_samples; i++) {
                 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],