[FFmpeg-cvslog] avcodec/mpegaudioenc: Move PutBitContext to stack

Andreas Rheinhardt git at videolan.org
Mon Apr 14 00:18:34 EEST 2025


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Thu Apr 10 22:31:21 2025 +0200| [7915e2a095861e0fffa265a3aad80ca694552421] | committer: Andreas Rheinhardt

avcodec/mpegaudioenc: Move PutBitContext to stack

Avoids keeping dangling pointers in the context.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

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

 libavcodec/mpegaudioenc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index 49255b7f29..3b8912b695 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -50,7 +50,6 @@
 #define SAMPLES_BUF_SIZE 4096
 
 typedef struct MpegAudioContext {
-    PutBitContext pb;
     int nb_channels;
     int lsf;           /* 1 if mpeg2 low bitrate selected */
     int bitrate_index; /* bit rate */
@@ -669,13 +668,15 @@ static av_always_inline void encode_subbands(MpegAudioContext *const s,
  * Output the MPEG audio layer 2 frame. Note how the code is small
  * compared to other encoders :-)
  */
-static void encode_frame(MpegAudioContext *s,
+static void encode_frame(MpegAudioContext *s, uint8_t *buf, unsigned buf_size,
                          unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
                          int padding)
 {
     int i, j, bit_alloc_bits, ch;
     unsigned char *sf;
-    PutBitContext *p = &s->pb;
+    PutBitContext p0, *p = &p0;
+
+    init_put_bits(p, buf, buf_size);
 
     /* header */
 
@@ -777,9 +778,7 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     if (ret < 0)
         return ret;
 
-    init_put_bits(&s->pb, avpkt->data, avpkt->size);
-
-    encode_frame(s, bit_alloc, padding);
+    encode_frame(s, avpkt->data, frame_size, bit_alloc, padding);
 
     if (frame->pts != AV_NOPTS_VALUE)
         avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);



More information about the ffmpeg-cvslog mailing list