Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 13/16] avcodec/wmaenc: Move transient PutBitContext from Context to stack
Date: Sat, 25 May 2024 02:48:27 +0200
Message-ID: <AS8P250MB07442950CAA9BE59520C11BB8FF62@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <AS8P250MB0744D0CB066E751A0A393A368FF52@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM>

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@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

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2024-05-25  0:48 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-24 21:58 [FFmpeg-devel] [PATCH 01/12] avutil/avassert: Add av_unreachable and av_assume() macros Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 02/12] avcodec/amrwbdec: Mark default switch as unreachable Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 03/12] avcodec/proresenc_anatoliy: Mark impossible case " Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 04/12] all: Use put_bytes_output() instead of put_bits_ptr - pb->buf Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 05/12] avcodec/mpeg4videodec: Mark impossible switch case as unreachable Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 06/12] avcodec/pcm-dvdenc: Mark unreachable default cases " Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 07/12] avcodec/vlc: Make code more readable with av_unreachable Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 08/12] avcodec/utvideoenc: Remove always-false pixel format check Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 09/12] avcodec/dolby_e_parse: Use av_unreachable instead of av_assert0(0) Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 10/12] avcodec/put_bits: Allow to mark places where PutBitContext is flushed Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 11/12] avcodec/e?ac3enc: Inform compiler about PutBitContext being blank Andreas Rheinhardt
2024-05-24 22:04 ` [FFmpeg-devel] [PATCH 12/12] avcodec/speedhqenc: Use av_unreachable for unreachable condition Andreas Rheinhardt
2024-05-25  0:48 ` Andreas Rheinhardt [this message]
2024-05-25  0:48 ` [FFmpeg-devel] [PATCH 14/16] avcodec/wma: Mark ff_wma_end() as av_cold Andreas Rheinhardt
2024-05-25  0:48 ` [FFmpeg-devel] [PATCH 15/16] avcodec/wmaenc: Use av_unreachable to mark unreachable codepath Andreas Rheinhardt
2024-05-25  0:48 ` [FFmpeg-devel] [PATCH 16/16] avcodec/wmaenc: Don't unnecessarily reset AVPacket.size Andreas Rheinhardt
2024-05-26  0:22 ` [FFmpeg-devel] [PATCH 01/12] avutil/avassert: Add av_unreachable and av_assume() macros Michael Niedermayer
2024-05-26  0:59   ` Andreas Rheinhardt
2024-05-26 17:55     ` Michael Niedermayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AS8P250MB07442950CAA9BE59520C11BB8FF62@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git