[FFmpeg-devel] [PATCH 13/16] avcodec/wmaenc: Move transient PutBitContext from Context to stack

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat May 25 03:48:27 EEST 2024


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/wma.h    |  2 --
 libavcodec/wmaenc.c | 58 ++++++++++++++++++++++-----------------------
 2 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/libavcodec/wma.h b/libavcodec/wma.h
index 3d0d872ea3..a909f5baa7 100644
--- a/libavcodec/wma.h
+++ b/libavcodec/wma.h
@@ -28,7 +28,6 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
-#include "put_bits.h"
 
 /* size of blocks */
 #define BLOCK_MIN_BITS 7
@@ -68,7 +67,6 @@ typedef struct CoefVLCTable {
 typedef struct WMACodecContext {
     AVCodecContext *avctx;
     GetBitContext gb;
-    PutBitContext pb;
     int version;                            ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
     int use_bit_reservoir;
     int use_variable_block_len;
diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c
index eaf0498ea2..7240c0895c 100644
--- a/libavcodec/wmaenc.c
+++ b/libavcodec/wmaenc.c
@@ -28,6 +28,7 @@
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
+#include "put_bits.h"
 #include "wma.h"
 #include "libavutil/avassert.h"
 
@@ -161,7 +162,7 @@ static void init_exp(WMACodecContext *s, int ch, const int *exp_param)
     s->max_exponent[ch] = max_scale;
 }
 
-static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
+static void encode_exp_vlc(WMACodecContext *s, PutBitContext *pb, int ch, const int *exp_param)
 {
     int last_exp;
     const uint16_t *ptr;
@@ -173,7 +174,7 @@ static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
     if (s->version == 1) {
         last_exp = *exp_param++;
         av_assert0(last_exp - 10 >= 0 && last_exp - 10 < 32);
-        put_bits(&s->pb, 5, last_exp - 10);
+        put_bits(pb, 5, last_exp - 10);
         q += *ptr++;
     } else
         last_exp = 36;
@@ -181,7 +182,7 @@ static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
         int exp  = *exp_param++;
         int code = exp - last_exp + 60;
         av_assert1(code >= 0 && code < 120);
-        put_bits(&s->pb, ff_aac_scalefactor_bits[code],
+        put_bits(pb, ff_aac_scalefactor_bits[code],
                  ff_aac_scalefactor_code[code]);
         /* XXX: use a table */
         q       += *ptr++;
@@ -189,7 +190,8 @@ static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
     }
 }
 
-static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
+static int encode_block(WMACodecContext *s, PutBitContext *pb,
+                        float (*src_coefs)[BLOCK_MAX_SIZE],
                         int total_gain)
 {
     int channels = s->avctx->ch_layout.nb_channels;
@@ -230,7 +232,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     }
 
     if (channels == 2)
-        put_bits(&s->pb, 1, !!s->ms_stereo);
+        put_bits(pb, 1, !!s->ms_stereo);
 
     for (ch = 0; ch < channels; ch++) {
         // FIXME only set channel_coded when needed, instead of always
@@ -269,7 +271,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     v = 0;
     for (ch = 0; ch < channels; ch++) {
         int a = s->channel_coded[ch];
-        put_bits(&s->pb, 1, a);
+        put_bits(pb, 1, a);
         v |= a;
     }
 
@@ -277,8 +279,8 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
         return 1;
 
     for (v = total_gain - 1; v >= 127; v -= 127)
-        put_bits(&s->pb, 7, 127);
-    put_bits(&s->pb, 7, v);
+        put_bits(pb, 7, 127);
+    put_bits(pb, 7, v);
 
     coef_nb_bits = ff_wma_total_gain_to_bits(total_gain);
 
@@ -288,7 +290,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                 int i, n;
                 n = s->exponent_high_sizes[bsize];
                 for (i = 0; i < n; i++) {
-                    put_bits(&s->pb, 1, s->high_band_coded[ch][i] = 0);
+                    put_bits(pb, 1, s->high_band_coded[ch][i] = 0);
                     if (0)
                         nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
                 }
@@ -298,13 +300,13 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
 
     parse_exponents = 1;
     if (s->block_len_bits != s->frame_len_bits)
-        put_bits(&s->pb, 1, parse_exponents);
+        put_bits(pb, 1, parse_exponents);
 
     if (parse_exponents) {
         for (ch = 0; ch < channels; ch++) {
             if (s->channel_coded[ch]) {
                 if (s->use_exp_vlc) {
-                    encode_exp_vlc(s, ch, fixed_exp);
+                    encode_exp_vlc(s, pb, ch, fixed_exp);
                 } else {
                     av_assert0(0); // FIXME not implemented
 //                    encode_exp_lsp(s, ch);
@@ -333,28 +335,28 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                             code = run + s->int_table[tindex][abs_level - 1];
 
                     av_assert2(code < s->coef_vlcs[tindex]->n);
-                    put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code],
+                    put_bits(pb, s->coef_vlcs[tindex]->huffbits[code],
                              s->coef_vlcs[tindex]->huffcodes[code]);
 
                     if (code == 0) {
                         if (1 << coef_nb_bits <= abs_level)
                             return -1;
 
-                        put_bits(&s->pb, coef_nb_bits, abs_level);
-                        put_bits(&s->pb, s->frame_len_bits, run);
+                        put_bits(pb, coef_nb_bits, abs_level);
+                        put_bits(pb, s->frame_len_bits, run);
                     }
                     // FIXME the sign is flipped somewhere
-                    put_bits(&s->pb, 1, level < 0);
+                    put_bits(pb, 1, level < 0);
                     run = 0;
                 } else
                     run++;
             }
             if (run)
-                put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1],
+                put_bits(pb, s->coef_vlcs[tindex]->huffbits[1],
                          s->coef_vlcs[tindex]->huffcodes[1]);
         }
         if (s->version == 1 && channels >= 2)
-            align_put_bits(&s->pb);
+            align_put_bits(pb);
     }
     return 0;
 }
@@ -362,23 +364,24 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
 static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                         uint8_t *buf, int buf_size, int total_gain)
 {
-    init_put_bits(&s->pb, buf, buf_size);
+    PutBitContext pb;
+    init_put_bits(&pb, buf, buf_size);
 
     if (s->use_bit_reservoir)
         av_assert0(0); // FIXME not implemented
-    else if (encode_block(s, src_coefs, total_gain) < 0)
+    else if (encode_block(s, &pb, src_coefs, total_gain) < 0)
         return INT_MAX;
 
-    align_put_bits(&s->pb);
+    flush_put_bits(&pb);
 
-    return put_bits_count(&s->pb) / 8 - s->avctx->block_align;
+    return put_bytes_output(&pb) - s->avctx->block_align;
 }
 
 static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
                              const AVFrame *frame, int *got_packet_ptr)
 {
     WMACodecContext *s = avctx->priv_data;
-    int i, total_gain, ret, error;
+    int i, total_gain, ret, error, remaining;
 
     s->block_len_bits = s->frame_len_bits; // required by non variable block len
     s->block_len      = 1 << s->block_len_bits;
@@ -418,14 +421,9 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
         avpkt->size = 0;
         return AVERROR(EINVAL);
     }
-    av_assert0((put_bits_count(&s->pb) & 7) == 0);
-    i = avctx->block_align - put_bytes_count(&s->pb, 0);
-    av_assert0(i>=0);
-    while(i--)
-        put_bits(&s->pb, 8, 'N');
-
-    flush_put_bits(&s->pb);
-    av_assert0(put_bytes_output(&s->pb) == avctx->block_align);
+    remaining = -error;
+    // Add padding
+    memset(avpkt->data + avctx->block_align - remaining, 'N', remaining);
 
     if (frame->pts != AV_NOPTS_VALUE)
         avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
-- 
2.40.1



More information about the ffmpeg-devel mailing list