Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused
@ 2025-04-10 20:59 Andreas Rheinhardt
  2025-04-10 22:04 ` James Almer
  2025-04-13  6:17 ` Andreas Rheinhardt
  0 siblings, 2 replies; 5+ messages in thread
From: Andreas Rheinhardt @ 2025-04-10 20:59 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 29 bytes --]

Patches attached.

- Andreas

[-- Attachment #2: 0001-avcodec-motionpixels-Avoid-av_unused.patch --]
[-- Type: text/x-patch, Size: 1220 bytes --]

From 33d5388fc680997e3950652caf90410604922ca0 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 12:54:30 +0200
Subject: [PATCH 01/15] avcodec/motionpixels: Avoid av_unused

Easily possible now that -Wdeclaration-after-statement is gone.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/motionpixels.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index 287f35f305..e5c3daece6 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -70,7 +70,6 @@ static av_cold int mp_decode_end(AVCodecContext *avctx)
 
 static av_cold int mp_decode_init(AVCodecContext *avctx)
 {
-    av_unused static AVOnce init_static_once = AV_ONCE_INIT;
     MotionPixelsContext *mp = avctx->priv_data;
     int w4 = (avctx->width  + 3) & ~3;
     int h4 = (avctx->height + 3) & ~3;
@@ -95,6 +94,7 @@ static av_cold int mp_decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
 
 #if !CONFIG_HARDCODED_TABLES
+    static AVOnce init_static_once = AV_ONCE_INIT;
     ff_thread_once(&init_static_once, motionpixels_tableinit);
 #endif
 
-- 
2.45.2


[-- Attachment #3: 0002-avutil-aes-Make-aes_init_static-av_cold.patch --]
[-- Type: text/x-patch, Size: 958 bytes --]

From 2748c2445dd5dd4fb8b23e9f56ce08c29dd78015 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 13:03:29 +0200
Subject: [PATCH 02/15] avutil/aes: Make aes_init_static() av_cold

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavutil/aes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavutil/aes.c b/libavutil/aes.c
index 52a250bc00..13a34c3bf6 100644
--- a/libavutil/aes.c
+++ b/libavutil/aes.c
@@ -25,6 +25,7 @@
 #include "config.h"
 #include "aes.h"
 #include "aes_internal.h"
+#include "attributes.h"
 #include "error.h"
 #include "intreadwrite.h"
 #include "macros.h"
@@ -200,7 +201,7 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4],
 
 static AVOnce aes_static_init = AV_ONCE_INIT;
 
-static void aes_init_static(void)
+static av_cold void aes_init_static(void)
 {
     uint8_t log8[256];
     uint8_t alog8[512];
-- 
2.45.2


[-- Attachment #4: 0003-avutil-aes-Use-if-checks-instead-of-if-ARCH_X86.patch --]
[-- Type: text/x-patch, Size: 876 bytes --]

From 9f052c0377a5b46e48d928f11d1d10c389ea5e84 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 13:41:55 +0200
Subject: [PATCH 03/15] avutil/aes: Use #if checks instead of if (ARCH_X86)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavutil/aes.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libavutil/aes.c b/libavutil/aes.c
index 13a34c3bf6..3c8ac1c349 100644
--- a/libavutil/aes.c
+++ b/libavutil/aes.c
@@ -237,8 +237,9 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
 
     a->rounds = rounds;
     a->crypt = decrypt ? aes_decrypt : aes_encrypt;
-    if (ARCH_X86)
-        ff_init_aes_x86(a, decrypt);
+#if ARCH_X86
+    ff_init_aes_x86(a, decrypt);
+#endif
 
     ff_thread_once(&aes_static_init, aes_init_static);
 
-- 
2.45.2


[-- Attachment #5: 0004-avcodec-aacenc-Remove-always-false-check.patch --]
[-- Type: text/x-patch, Size: 3260 bytes --]

From e3b08a135f550d70d9cb8d883891512df1b54bcc Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 13:56:14 +0200
Subject: [PATCH 04/15] avcodec/aacenc: Remove always-false check

The sample rates have already been checked generically
via AVCodec.supported_samplerates.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aacenc.c                  | 13 ++++++-------
 libavcodec/aacenctab.c               |  3 ---
 libavcodec/aacenctab.h               |  2 --
 libavcodec/mpeg4audio_sample_rates.h |  4 ++++
 4 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 0f124408da..afb4478bfe 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -1235,14 +1235,13 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
     }
 
     /* Samplerate */
-    for (i = 0; i < 16; i++)
-        if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
+    for (int i = 0;; i++) {
+        av_assert1(i < 13);
+        if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) {
+            s->samplerate_index = i;
             break;
-    s->samplerate_index = i;
-    ERROR_IF(s->samplerate_index == 16 ||
-             s->samplerate_index >= ff_aac_swb_size_1024_len ||
-             s->samplerate_index >= ff_aac_swb_size_128_len,
-             "Unsupported sample rate %d\n", avctx->sample_rate);
+        }
+    }
 
     /* Bitrate limiting */
     WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
diff --git a/libavcodec/aacenctab.c b/libavcodec/aacenctab.c
index 874365a593..ca09e22ca8 100644
--- a/libavcodec/aacenctab.c
+++ b/libavcodec/aacenctab.c
@@ -103,6 +103,3 @@ const uint8_t *const ff_aac_swb_size_1024[] = {
     swb_size_1024_16, swb_size_1024_16, swb_size_1024_8,
     swb_size_1024_8
 };
-
-const int ff_aac_swb_size_128_len  = FF_ARRAY_ELEMS(ff_aac_swb_size_128);
-const int ff_aac_swb_size_1024_len = FF_ARRAY_ELEMS(ff_aac_swb_size_1024);
diff --git a/libavcodec/aacenctab.h b/libavcodec/aacenctab.h
index 60e1f22387..fee9c245d8 100644
--- a/libavcodec/aacenctab.h
+++ b/libavcodec/aacenctab.h
@@ -41,9 +41,7 @@
 #define AAC_MAX_CHANNELS 16
 
 extern const uint8_t *const ff_aac_swb_size_1024[];
-extern const int      ff_aac_swb_size_1024_len;
 extern const uint8_t *const ff_aac_swb_size_128[];
-extern const int      ff_aac_swb_size_128_len;
 
 /* Supported layouts without using a PCE */
 static const AVChannelLayout aac_normal_chan_layouts[7] = {
diff --git a/libavcodec/mpeg4audio_sample_rates.h b/libavcodec/mpeg4audio_sample_rates.h
index 0b8caa6d76..a847a97994 100644
--- a/libavcodec/mpeg4audio_sample_rates.h
+++ b/libavcodec/mpeg4audio_sample_rates.h
@@ -23,6 +23,10 @@
 #ifndef AVCODEC_MPEG4AUDIO_SAMPLE_RATES_H
 #define AVCODEC_MPEG4AUDIO_SAMPLE_RATES_H
 
+// This table contains only 13 real elements and is padded with zeroes.
+// It is used by the AAC encoder as sample rate table, so the encoder
+// needs to actually support all of these rates and it needs to have
+// a trailing zero.
 const int ff_mpeg4audio_sample_rates[16] = {
     96000, 88200, 64000, 48000, 44100, 32000,
     24000, 22050, 16000, 12000, 11025, 8000, 7350
-- 
2.45.2


[-- Attachment #6: 0005-avcodec-aac-enc-tab-Deduplicate-swb-tables.patch --]
[-- Type: text/x-patch, Size: 2428 bytes --]

From aab95f0ebef48b72be8a93bcd55c904a4c623050 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 14:30:30 +0200
Subject: [PATCH 05/15] avcodec/aac{enc,}tab: Deduplicate swb tables

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aacenctab.c |  4 +---
 libavcodec/aactab.c    | 12 +-----------
 libavcodec/aactab.h    |  2 +-
 3 files changed, 3 insertions(+), 15 deletions(-)

diff --git a/libavcodec/aacenctab.c b/libavcodec/aacenctab.c
index ca09e22ca8..fd40e076e2 100644
--- a/libavcodec/aacenctab.c
+++ b/libavcodec/aacenctab.c
@@ -25,9 +25,7 @@ static const uint8_t swb_size_128_96[] = {
     4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
 };
 
-static const uint8_t swb_size_128_64[] = {
-    4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
-};
+#define swb_size_128_64 swb_size_128_96
 
 static const uint8_t swb_size_128_48[] = {
     4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
diff --git a/libavcodec/aactab.c b/libavcodec/aactab.c
index 4e2739ecb1..128bacfdf1 100644
--- a/libavcodec/aactab.c
+++ b/libavcodec/aactab.c
@@ -170,10 +170,6 @@ const uint8_t ff_aac_num_swb_128[] = {
     12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15
 };
 
-const uint8_t ff_aac_num_swb_120[] = {
-    12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15
-};
-
 const uint8_t ff_aac_num_swb_96[] = {
     12, 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14
 };
@@ -1838,13 +1834,7 @@ static const uint16_t swb_offset_768_48[] =
     544, 576, 608, 640, 672, 704, 736, 768
 };
 
-static const uint16_t swb_offset_768_32[] =
-{
-    0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  48,
-    56,  64,  72,  80,  88,  96,  108, 120, 132, 144, 160, 176,
-    196, 216, 240, 264, 292, 320, 352, 384, 416, 448, 480, 512,
-    544, 576, 608, 640, 672, 704, 736, 768
-};
+#define swb_offset_768_32 swb_offset_768_48
 
 static const uint16_t swb_offset_768_24[] =
 {
diff --git a/libavcodec/aactab.h b/libavcodec/aactab.h
index e286d98385..e7dbbf1ecd 100644
--- a/libavcodec/aactab.h
+++ b/libavcodec/aactab.h
@@ -76,7 +76,7 @@ extern const uint8_t ff_aac_num_swb_768 [];
 extern const uint8_t ff_aac_num_swb_512 [];
 extern const uint8_t ff_aac_num_swb_480 [];
 extern const uint8_t ff_aac_num_swb_128 [];
-extern const uint8_t ff_aac_num_swb_120 [];
+#define ff_aac_num_swb_120 ff_aac_num_swb_128
 extern const uint8_t ff_aac_num_swb_96  [];
 // @}
 
-- 
2.45.2


[-- Attachment #7: 0006-swscale-x86-rgb2rgb-Deduplicate-ASM-constants.patch --]
[-- Type: text/x-patch, Size: 1528 bytes --]

From b1f5281bffa6b4a855208f6c6478f3fbdc3e1f4f Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 15:31:15 +0200
Subject: [PATCH 06/15] swscale/x86/rgb2rgb: Deduplicate ASM constants

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libswscale/x86/rgb2rgb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libswscale/x86/rgb2rgb.c b/libswscale/x86/rgb2rgb.c
index ea61c3c770..48a73bd0b2 100644
--- a/libswscale/x86/rgb2rgb.c
+++ b/libswscale/x86/rgb2rgb.c
@@ -60,12 +60,12 @@ DECLARE_ASM_CONST(8, uint64_t, mask15r)      = 0x7C007C007C007C00ULL;
 #define mask16b mask15b
 DECLARE_ASM_CONST(8, uint64_t, mask16g)      = 0x07E007E007E007E0ULL;
 DECLARE_ASM_CONST(8, uint64_t, mask16r)      = 0xF800F800F800F800ULL;
-DECLARE_ASM_CONST(8, uint64_t, red_16mask)   = 0x0000f8000000f800ULL;
+#define red_16mask mask3215g
 DECLARE_ASM_CONST(8, uint64_t, green_16mask) = 0x000007e0000007e0ULL;
 DECLARE_ASM_CONST(8, uint64_t, blue_16mask)  = 0x0000001f0000001fULL;
 DECLARE_ASM_CONST(8, uint64_t, red_15mask)   = 0x00007c0000007c00ULL;
 DECLARE_ASM_CONST(8, uint64_t, green_15mask) = 0x000003e0000003e0ULL;
-DECLARE_ASM_CONST(8, uint64_t, blue_15mask)  = 0x0000001f0000001fULL;
+#define blue_15mask blue_16mask
 DECLARE_ASM_CONST(8, uint64_t, mul15_mid)    = 0x4200420042004200ULL;
 DECLARE_ASM_CONST(8, uint64_t, mul15_hi)     = 0x0210021002100210ULL;
 DECLARE_ASM_CONST(8, uint64_t, mul16_mid)    = 0x2080208020802080ULL;
-- 
2.45.2


[-- Attachment #8: 0007-avcodec-opus-tab-Deduplicate-arrays.patch --]
[-- Type: text/x-patch, Size: 2780 bytes --]

From 79f5e8bc644d77081c376d3aa862189c700c7986 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 17:54:03 +0200
Subject: [PATCH 07/15] avcodec/opus/tab: Deduplicate arrays

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/opus/tab.c | 6 ------
 libavcodec/opus/tab.h | 6 +++---
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/libavcodec/opus/tab.c b/libavcodec/opus/tab.c
index e7d20d1688..d1f2f8c8c2 100644
--- a/libavcodec/opus/tab.c
+++ b/libavcodec/opus/tab.c
@@ -110,12 +110,8 @@ const uint16_t ff_silk_model_pitch_highbits[] = {
     216, 224, 231, 237, 241, 243, 245, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256
 };
 
-const uint16_t ff_silk_model_pitch_lowbits_nb[] = { 256, 64, 128, 192, 256 };
-
 const uint16_t ff_silk_model_pitch_lowbits_mb[] = { 256, 43, 85, 128, 171, 213, 256 };
 
-const uint16_t ff_silk_model_pitch_lowbits_wb[] = { 256, 32, 64, 96, 128, 160, 192, 224, 256 };
-
 const uint16_t ff_silk_model_pitch_delta[] = {
     256,  46,  48,  50,  53,  57,  63,  73,  88, 114, 152, 182, 204, 219, 229, 236,
     242, 246, 250, 252, 254, 256
@@ -763,8 +759,6 @@ const uint16_t ff_celt_model_alloc_trim[] = {
     128,   2,   4,   9,  19,  41,  87, 109, 119, 124, 126, 128
 };
 
-const uint16_t ff_celt_model_energy_small[] = { 4, 2, 3, 4 };
-
 const uint8_t ff_celt_freq_bands[] = { /* in steps of 200Hz */
     0,  1,  2,  3,  4,  5,  6,  7,  8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100
 };
diff --git a/libavcodec/opus/tab.h b/libavcodec/opus/tab.h
index 109a422b9f..ac140d5868 100644
--- a/libavcodec/opus/tab.h
+++ b/libavcodec/opus/tab.h
@@ -53,9 +53,9 @@ extern const uint16_t ff_silk_model_lsf_s2_ext[];
 extern const uint16_t ff_silk_model_lsf_interpolation_offset[];
 
 extern const uint16_t ff_silk_model_pitch_highbits[];
-extern const uint16_t ff_silk_model_pitch_lowbits_nb[];
+#define ff_silk_model_pitch_lowbits_nb ff_silk_model_lcg_seed
 extern const uint16_t ff_silk_model_pitch_lowbits_mb[];
-extern const uint16_t ff_silk_model_pitch_lowbits_wb[];
+#define ff_silk_model_pitch_lowbits_wb ff_silk_model_gain_lowbits
 extern const uint16_t ff_silk_model_pitch_delta[];
 extern const uint16_t ff_silk_model_pitch_contour_nb10ms[];
 extern const uint16_t ff_silk_model_pitch_contour_nb20ms[];
@@ -124,7 +124,7 @@ extern const int      ff_silk_stereo_interp_len[3];
 extern const uint16_t ff_celt_model_tapset[];
 extern const uint16_t ff_celt_model_spread[];
 extern const uint16_t ff_celt_model_alloc_trim[];
-extern const uint16_t ff_celt_model_energy_small[];
+#define ff_celt_model_energy_small ff_celt_model_tapset
 
 extern const uint8_t  ff_celt_freq_bands[];
 extern const uint8_t  ff_celt_freq_range[];
-- 
2.45.2


[-- Attachment #9: 0008-avformat-asf_tags-Deduplicate-tags.patch --]
[-- Type: text/x-patch, Size: 1707 bytes --]

From b71c5282502b28004efaf52ac181890e8c57c63c Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 18:00:46 +0200
Subject: [PATCH 08/15] avformat/asf_tags: Deduplicate tags

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/asf.h      | 2 +-
 libavformat/asf_tags.c | 4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/libavformat/asf.h b/libavformat/asf.h
index 01cc4f7a46..b77dabe1ff 100644
--- a/libavformat/asf.h
+++ b/libavformat/asf.h
@@ -98,7 +98,7 @@ extern const ff_asf_guid ff_asf_language_guid;
 extern const ff_asf_guid ff_asf_content_encryption;
 extern const ff_asf_guid ff_asf_ext_content_encryption;
 extern const ff_asf_guid ff_asf_digital_signature;
-extern const ff_asf_guid ff_asf_extended_stream_properties_object;
+#define ff_asf_extended_stream_properties_object ff_asf_ext_stream_header
 extern const ff_asf_guid ff_asf_group_mutual_exclusion_object;
 extern const ff_asf_guid ff_asf_mutex_language;
 
diff --git a/libavformat/asf_tags.c b/libavformat/asf_tags.c
index 53d886f181..240f7e4da0 100644
--- a/libavformat/asf_tags.c
+++ b/libavformat/asf_tags.c
@@ -137,10 +137,6 @@ const ff_asf_guid ff_asf_digital_signature = {
     0xfc, 0xb3, 0x11, 0x22, 0x23, 0xbd, 0xd2, 0x11, 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e
 };
 
-const ff_asf_guid ff_asf_extended_stream_properties_object = {
-    0xcb, 0xa5, 0xe6, 0x14, 0x72, 0xc6, 0x32, 0x43, 0x83, 0x99, 0xa9, 0x69, 0x52, 0x06, 0x5b, 0x5a
-};
-
 const ff_asf_guid ff_asf_group_mutual_exclusion_object = {
     0x40, 0x5a, 0x46, 0xd1, 0x79, 0x5a, 0x38, 0x43, 0xb7, 0x1b, 0xe3, 0x6b, 0x8f, 0xd6, 0xc2, 0x49
 };
-- 
2.45.2


[-- Attachment #10: 0009-avcodec-mpegaudioenc_template-Remove-always-false-br.patch --]
[-- Type: text/x-patch, Size: 1521 bytes --]

From ff287248ee963d69f9dd66b1aef0d7499a7222c5 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 19:46:04 +0200
Subject: [PATCH 09/15] avcodec/mpegaudioenc_template: Remove always-false
 branch

The sample rates here have already been checked generically
via CODEC_SAMPLERATES().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegaudioenc_template.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libavcodec/mpegaudioenc_template.c b/libavcodec/mpegaudioenc_template.c
index 396e8a4899..12f5c7b9cd 100644
--- a/libavcodec/mpegaudioenc_template.c
+++ b/libavcodec/mpegaudioenc_template.c
@@ -24,6 +24,7 @@
  * The simplest mpeg audio layer 2 encoder.
  */
 
+#include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 
 #include "avcodec.h"
@@ -89,7 +90,8 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx)
 
     /* encoding freq */
     s->lsf = 0;
-    for(i=0;i<3;i++) {
+    for (i = 0;; i++) {
+        av_assert1(i < 3);
         if (ff_mpa_freq_tab[i] == freq)
             break;
         if ((ff_mpa_freq_tab[i] / 2) == freq) {
@@ -97,10 +99,6 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx)
             break;
         }
     }
-    if (i == 3){
-        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
-        return AVERROR(EINVAL);
-    }
     s->freq_index = i;
 
     /* encoding bitrate & frequency */
-- 
2.45.2


[-- Attachment #11: 0010-avcodec-mpegaudioenc_-fixed-float-Merge-encoders.patch --]
[-- Type: text/x-patch, Size: 16947 bytes --]

From efa54c902f9413eee080dec3ec8cdc480c9ea6ce Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 21:07:00 +0200
Subject: [PATCH 10/15] avcodec/mpegaudioenc_{fixed,float}: Merge encoders

Most of the encoders is the same. So deduplicate them.
This reduces code size from 22410B to 12637B here.
The data in mpegaudiotab.h is also automatically deduplicated.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/Makefile                           |   4 +-
 ...mpegaudioenc_template.c => mpegaudioenc.c} | 207 +++++++++++-------
 libavcodec/mpegaudioenc_fixed.c               |  39 ----
 libavcodec/mpegaudioenc_float.c               |  40 ----
 4 files changed, 134 insertions(+), 156 deletions(-)
 rename libavcodec/{mpegaudioenc_template.c => mpegaudioenc.c} (84%)
 delete mode 100644 libavcodec/mpegaudioenc_fixed.c
 delete mode 100644 libavcodec/mpegaudioenc_float.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3c3ac640e0..98501f9797 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -529,10 +529,10 @@ OBJS-$(CONFIG_MOVTEXT_ENCODER)         += movtextenc.o ass_split.o
 OBJS-$(CONFIG_MP1_DECODER)             += mpegaudiodec_fixed.o
 OBJS-$(CONFIG_MP1FLOAT_DECODER)        += mpegaudiodec_float.o
 OBJS-$(CONFIG_MP2_DECODER)             += mpegaudiodec_fixed.o
-OBJS-$(CONFIG_MP2_ENCODER)             += mpegaudioenc_float.o mpegaudio.o \
+OBJS-$(CONFIG_MP2_ENCODER)             += mpegaudioenc.o mpegaudio.o \
                                           mpegaudiodata.o mpegaudiodsp_data.o \
                                           mpegaudiotabs.o
-OBJS-$(CONFIG_MP2FIXED_ENCODER)        += mpegaudioenc_fixed.o mpegaudio.o \
+OBJS-$(CONFIG_MP2FIXED_ENCODER)        += mpegaudioenc.o mpegaudio.o \
                                           mpegaudiodata.o mpegaudiodsp_data.o \
                                           mpegaudiotabs.o
 OBJS-$(CONFIG_MP2FLOAT_DECODER)        += mpegaudiodec_float.o
diff --git a/libavcodec/mpegaudioenc_template.c b/libavcodec/mpegaudioenc.c
similarity index 84%
rename from libavcodec/mpegaudioenc_template.c
rename to libavcodec/mpegaudioenc.c
index 12f5c7b9cd..35347d386c 100644
--- a/libavcodec/mpegaudioenc_template.c
+++ b/libavcodec/mpegaudioenc.c
@@ -24,10 +24,14 @@
  * The simplest mpeg audio layer 2 encoder.
  */
 
+#include "config.h"
+#include "config_components.h"
+
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 
 #include "avcodec.h"
+#include "codec_internal.h"
 #include "encode.h"
 #include "put_bits.h"
 
@@ -52,6 +56,7 @@ typedef struct MpegAudioContext {
     int bitrate_index; /* bit rate */
     int freq_index;
     int frame_size; /* frame size, in bits, without padding */
+    int is_fixed;
     /* padding computation */
     int frame_frac, frame_frac_incr, do_padding;
     short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
@@ -65,15 +70,18 @@ typedef struct MpegAudioContext {
     int16_t filter_bank[512];
     int scale_factor_table[64];
     unsigned char scale_diff_table[128];
-#if USE_FLOATS
-    float scale_factor_inv_table[64];
-#else
-    int8_t scale_factor_shift[64];
-    unsigned short scale_factor_mult[64];
-#endif
+    union {
+        float scale_factor_inv_table[64];
+        struct {
+            int8_t scale_factor_shift[64];
+            unsigned short scale_factor_mult[64];
+        };
+    };
     unsigned short total_quant_bits[17]; /* total number of bits per allocation group */
 } MpegAudioContext;
 
+#define IS_FIXED(s) (CONFIG_MP2_ENCODER && CONFIG_MP2FIXED_ENCODER ? (s)->is_fixed : CONFIG_MP2FIXED_ENCODER)
+
 static av_cold int MPA_encode_init(AVCodecContext *avctx)
 {
     MpegAudioContext *s = avctx->priv_data;
@@ -157,13 +165,13 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx)
         if (v <= 0)
             v = 1;
         s->scale_factor_table[i] = v;
-#if USE_FLOATS
-        s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
-#else
+        if (IS_FIXED(s)) {
 #define P 15
-        s->scale_factor_shift[i] = 21 - P - (i / 3);
-        s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
-#endif
+            s->scale_factor_shift[i] = 21 - P - (i / 3);
+            s->scale_factor_mult[i]  = (1 << P) * exp2((i % 3) / 3.0);
+        } else {
+            s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
+        }
     }
     for(i=0;i<128;i++) {
         v = i - 64;
@@ -592,6 +600,70 @@ static void compute_bit_allocation(MpegAudioContext *s,
     av_assert0(*padding >= 0);
 }
 
+/// Quantization & write sub band samples
+static av_always_inline void encode_subbands(MpegAudioContext *const s,
+                                             PutBitContext *const p,
+                                             const uint8_t bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
+                                             int is_fixed)
+{
+    for (int k = 0; k < 3; ++k) {
+        for (int l = 0; l < 12; l += 3) {
+            for (int i = 0, j = 0; i < s->sblimit; ++i) {
+                const int bit_alloc_bits = s->alloc_table[j];
+                for (int ch = 0; ch < s->nb_channels; ++ch) {
+                    const int b = bit_alloc[ch][i];
+                    if (b) {
+                        /* we encode 3 sub band samples of the same sub band at a time */
+                        const int qindex = s->alloc_table[j + b];
+                        const int steps  = ff_mpa_quant_steps[qindex];
+                        int q[3];
+
+                        for (int m = 0; m < 3; ++m) {
+                            const int sample = s->sb_samples[ch][k][l + m][i];
+                            /* divide by scale factor */
+                            if (!is_fixed) {
+                                float a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
+                                q[m] = (int)((a + 1.0) * steps * 0.5);
+                            } else {
+                                const int e     = s->scale_factors[ch][i][k];
+                                const int shift = s->scale_factor_shift[e];
+                                const int mult  = s->scale_factor_mult[e];
+                                int q1;
+
+                                /* normalize to P bits */
+                                if (shift < 0)
+                                    q1 = sample * (1 << -shift);
+                                else
+                                    q1 = sample >> shift;
+                                q1 = (q1 * mult) >> P;
+                                q1 += 1 << P;
+                                if (q1 < 0)
+                                    q1 = 0;
+                                q[m] = (q1 * (unsigned)steps) >> (P + 1);
+                            }
+                            if (q[m] >= steps)
+                                q[m] = steps - 1;
+                            av_assert2(q[m] >= 0 && q[m] < steps);
+                        }
+                        const int bits = ff_mpa_quant_bits[qindex];
+                        if (bits < 0) {
+                            /* group the 3 values to save bits */
+                            put_bits(p, -bits,
+                                     q[0] + steps * (q[1] + steps * q[2]));
+                        } else {
+                            put_bits(p, bits, q[0]);
+                            put_bits(p, bits, q[1]);
+                            put_bits(p, bits, q[2]);
+                        }
+                    }
+                }
+                /* next subband in alloc table */
+                j += 1 << bit_alloc_bits;
+            }
+        }
+    }
+}
+
 /*
  * Output the MPEG audio layer 2 frame. Note how the code is small
  * compared to other encoders :-)
@@ -600,9 +672,8 @@ static void encode_frame(MpegAudioContext *s,
                          unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
                          int padding)
 {
-    int i, j, k, l, bit_alloc_bits, b, ch;
+    int i, j, bit_alloc_bits, ch;
     unsigned char *sf;
-    int q[3];
     PutBitContext *p = &s->pb;
 
     /* header */
@@ -663,69 +734,14 @@ static void encode_frame(MpegAudioContext *s,
         }
     }
 
-    /* quantization & write sub band samples */
-
-    for(k=0;k<3;k++) {
-        for(l=0;l<12;l+=3) {
-            j = 0;
-            for(i=0;i<s->sblimit;i++) {
-                bit_alloc_bits = s->alloc_table[j];
-                for(ch=0;ch<s->nb_channels;ch++) {
-                    b = bit_alloc[ch][i];
-                    if (b) {
-                        int qindex, steps, m, sample, bits;
-                        /* we encode 3 sub band samples of the same sub band at a time */
-                        qindex = s->alloc_table[j+b];
-                        steps = ff_mpa_quant_steps[qindex];
-                        for(m=0;m<3;m++) {
-                            sample = s->sb_samples[ch][k][l + m][i];
-                            /* divide by scale factor */
-#if USE_FLOATS
-                            {
-                                float a;
-                                a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
-                                q[m] = (int)((a + 1.0) * steps * 0.5);
-                            }
+#if CONFIG_SMALL
+    encode_subbands(s, p, bit_alloc, IS_FIXED(s));
 #else
-                            {
-                                int q1, e, shift, mult;
-                                e = s->scale_factors[ch][i][k];
-                                shift = s->scale_factor_shift[e];
-                                mult = s->scale_factor_mult[e];
-
-                                /* normalize to P bits */
-                                if (shift < 0)
-                                    q1 = sample * (1 << -shift);
-                                else
-                                    q1 = sample >> shift;
-                                q1 = (q1 * mult) >> P;
-                                q1 += 1 << P;
-                                if (q1 < 0)
-                                    q1 = 0;
-                                q[m] = (q1 * (unsigned)steps) >> (P + 1);
-                            }
+    if (IS_FIXED(s))
+        encode_subbands(s, p, bit_alloc, 1);
+    else
+        encode_subbands(s, p, bit_alloc, 0);
 #endif
-                            if (q[m] >= steps)
-                                q[m] = steps - 1;
-                            av_assert2(q[m] >= 0 && q[m] < steps);
-                        }
-                        bits = ff_mpa_quant_bits[qindex];
-                        if (bits < 0) {
-                            /* group the 3 values to save bits */
-                            put_bits(p, -bits,
-                                     q[0] + steps * (q[1] + steps * q[2]));
-                        } else {
-                            put_bits(p, bits, q[0]);
-                            put_bits(p, bits, q[1]);
-                            put_bits(p, bits, q[2]);
-                        }
-                    }
-                }
-                /* next subband in alloc table */
-                j += 1 << bit_alloc_bits;
-            }
-        }
-    }
 
     /* padding */
     for(i=0;i<padding;i++)
@@ -777,3 +793,44 @@ static const FFCodecDefault mp2_defaults[] = {
     { NULL },
 };
 
+#if CONFIG_MP2_ENCODER
+const FFCodec ff_mp2_encoder = {
+    .p.name                = "mp2",
+    CODEC_LONG_NAME("MP2 (MPEG audio layer 2)"),
+    .p.type                = AVMEDIA_TYPE_AUDIO,
+    .p.id                  = AV_CODEC_ID_MP2,
+    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .priv_data_size        = sizeof(MpegAudioContext),
+    .init                  = MPA_encode_init,
+    FF_CODEC_ENCODE_CB(MPA_encode_frame),
+    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
+    CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
+    CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
+    .defaults              = mp2_defaults,
+};
+#endif
+
+#if CONFIG_MP2FIXED_ENCODER
+static av_cold int mpa_fixed_encode_init(AVCodecContext *avctx)
+{
+    MpegAudioContext *s = avctx->priv_data;
+
+    s->is_fixed = 1;
+    return MPA_encode_init(avctx);
+}
+
+const FFCodec ff_mp2fixed_encoder = {
+    .p.name                = "mp2fixed",
+    CODEC_LONG_NAME("MP2 fixed point (MPEG audio layer 2)"),
+    .p.type                = AVMEDIA_TYPE_AUDIO,
+    .p.id                  = AV_CODEC_ID_MP2,
+    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+    .priv_data_size        = sizeof(MpegAudioContext),
+    .init                  = mpa_fixed_encode_init,
+    FF_CODEC_ENCODE_CB(MPA_encode_frame),
+    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
+    CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
+    CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
+    .defaults              = mp2_defaults,
+};
+#endif
diff --git a/libavcodec/mpegaudioenc_fixed.c b/libavcodec/mpegaudioenc_fixed.c
deleted file mode 100644
index a9faa7e059..0000000000
--- a/libavcodec/mpegaudioenc_fixed.c
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * The simplest mpeg audio layer 2 encoder
- * Copyright (c) 2000, 2001 Fabrice Bellard
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "libavutil/channel_layout.h"
-#include "codec_internal.h"
-#include "mpegaudioenc_template.c"
-
-const FFCodec ff_mp2fixed_encoder = {
-    .p.name                = "mp2fixed",
-    CODEC_LONG_NAME("MP2 fixed point (MPEG audio layer 2)"),
-    .p.type                = AVMEDIA_TYPE_AUDIO,
-    .p.id                  = AV_CODEC_ID_MP2,
-    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
-    .priv_data_size        = sizeof(MpegAudioContext),
-    .init                  = MPA_encode_init,
-    FF_CODEC_ENCODE_CB(MPA_encode_frame),
-    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
-    CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
-    CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
-    .defaults              = mp2_defaults,
-};
diff --git a/libavcodec/mpegaudioenc_float.c b/libavcodec/mpegaudioenc_float.c
deleted file mode 100644
index 5ff67960b5..0000000000
--- a/libavcodec/mpegaudioenc_float.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * The simplest mpeg audio layer 2 encoder
- * Copyright (c) 2000, 2001 Fabrice Bellard
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "libavutil/channel_layout.h"
-#define USE_FLOATS 1
-#include "codec_internal.h"
-#include "mpegaudioenc_template.c"
-
-const FFCodec ff_mp2_encoder = {
-    .p.name                = "mp2",
-    CODEC_LONG_NAME("MP2 (MPEG audio layer 2)"),
-    .p.type                = AVMEDIA_TYPE_AUDIO,
-    .p.id                  = AV_CODEC_ID_MP2,
-    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
-    .priv_data_size        = sizeof(MpegAudioContext),
-    .init                  = MPA_encode_init,
-    FF_CODEC_ENCODE_CB(MPA_encode_frame),
-    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
-    CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
-    CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
-    .defaults              = mp2_defaults,
-};
-- 
2.45.2


[-- Attachment #12: 0011-avcodec-mpegaudioenc-Combine-writing-scale-factors.patch --]
[-- Type: text/x-patch, Size: 1299 bytes --]

From 0aba863eec472e09362850b49211bf5ca5a43bff Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 21:34:55 +0200
Subject: [PATCH 11/15] avcodec/mpegaudioenc: Combine writing scale factors

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegaudioenc.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index 35347d386c..b10487b7ff 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -717,14 +717,11 @@ static void encode_frame(MpegAudioContext *s,
                 sf = &s->scale_factors[ch][i][0];
                 switch(s->scale_code[ch][i]) {
                 case 0:
-                    put_bits(p, 6, sf[0]);
-                    put_bits(p, 6, sf[1]);
-                    put_bits(p, 6, sf[2]);
+                    put_bits(p, 18, sf[0] << 12 | sf[1] << 6 | sf[2]);
                     break;
                 case 3:
                 case 1:
-                    put_bits(p, 6, sf[0]);
-                    put_bits(p, 6, sf[2]);
+                    put_bits(p, 12, sf[0] << 6 | sf[2]);
                     break;
                 case 2:
                     put_bits(p, 6, sf[0]);
-- 
2.45.2


[-- Attachment #13: 0012-avcodec-mpegaudioenc-Avoid-intermediate-buffer.patch --]
[-- Type: text/x-patch, Size: 2659 bytes --]

From a122c8038424a5e53ffea6fd7d50e46a0e918ca7 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 22:13:17 +0200
Subject: [PATCH 12/15] avcodec/mpegaudioenc: Avoid intermediate buffer

We know the final size before encoding, so we can switch to
ff_get_encode_buffer() which avoids an implicit memcpy().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegaudioenc.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index b10487b7ff..9a6aae6f78 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -508,7 +508,7 @@ static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
 /* Try to maximize the smr while using a number of bits inferior to
    the frame size. I tried to make the code simpler, faster and
    smaller than other encoders :-) */
-static void compute_bit_allocation(MpegAudioContext *s,
+static unsigned compute_bit_allocation(MpegAudioContext *s,
                                    short smr1[MPA_MAX_CHANNELS][SBLIMIT],
                                    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
                                    int *padding)
@@ -598,6 +598,7 @@ static void compute_bit_allocation(MpegAudioContext *s,
     }
     *padding = max_frame_size - current_frame_size;
     av_assert0(*padding >= 0);
+    return max_frame_size / 8U;
 }
 
 /// Quantization & write sub band samples
@@ -740,6 +741,8 @@ static void encode_frame(MpegAudioContext *s,
         encode_subbands(s, p, bit_alloc, 0);
 #endif
 
+    av_assert1(put_bits_left(p) == padding);
+
     /* padding */
     for(i=0;i<padding;i++)
         put_bits(p, 1, 0);
@@ -765,9 +768,10 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     for(i=0;i<s->nb_channels;i++) {
         psycho_acoustic_model(s, smr[i]);
     }
-    compute_bit_allocation(s, smr, bit_alloc, &padding);
+    unsigned frame_size = compute_bit_allocation(s, smr, bit_alloc, &padding);
 
-    if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
+    ret = ff_get_encode_buffer(avctx, avpkt, frame_size, 0);
+    if (ret < 0)
         return ret;
 
     init_put_bits(&s->pb, avpkt->data, avpkt->size);
@@ -776,7 +780,6 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     /* flush */
     flush_put_bits(&s->pb);
-    avpkt->size = put_bytes_output(&s->pb);
 
     if (frame->pts != AV_NOPTS_VALUE)
         avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
-- 
2.45.2


[-- Attachment #14: 0013-avcodec-mpegaudioenc-Don-t-pad-one-bit-at-a-time.patch --]
[-- Type: text/x-patch, Size: 1297 bytes --]

From 3440a901baaf3307cc1f37b0590458bc08162e94 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 22:20:33 +0200
Subject: [PATCH 13/15] avcodec/mpegaudioenc: Don't pad one bit at a time

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegaudioenc.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index 9a6aae6f78..49255b7f29 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -743,9 +743,12 @@ static void encode_frame(MpegAudioContext *s,
 
     av_assert1(put_bits_left(p) == padding);
 
+    /* flush */
+    flush_put_bits(p);
+
     /* padding */
-    for(i=0;i<padding;i++)
-        put_bits(p, 1, 0);
+    if (put_bytes_left(p, 0))
+        memset(put_bits_ptr(p), 0, put_bytes_left(p, 0));
 }
 
 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
@@ -778,9 +781,6 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     encode_frame(s, bit_alloc, padding);
 
-    /* flush */
-    flush_put_bits(&s->pb);
-
     if (frame->pts != AV_NOPTS_VALUE)
         avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
 
-- 
2.45.2


[-- Attachment #15: 0014-avcodec-mpegaudioenc-Move-PutBitContext-to-stack.patch --]
[-- Type: text/x-patch, Size: 1928 bytes --]

From e9b4a0ecaf41efd23157940e4227c4148b7bb68e Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 22:31:21 +0200
Subject: [PATCH 14/15] avcodec/mpegaudioenc: Move PutBitContext to stack

Avoids keeping dangling pointers in the context.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 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);
-- 
2.45.2


[-- Attachment #16: 0015-avcodec-mpegaudioenc-Rename-MPA_encode_-mpa_encode_.patch --]
[-- Type: text/x-patch, Size: 2781 bytes --]

From 18d51a0257123b897e9b37bd6d0a7503e8dc23aa Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 10 Apr 2025 22:38:56 +0200
Subject: [PATCH 15/15] avcodec/mpegaudioenc: Rename MPA_encode_* ->
 mpa_encode_*

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegaudioenc.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index 3b8912b695..9727c3819d 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -81,7 +81,7 @@ typedef struct MpegAudioContext {
 
 #define IS_FIXED(s) (CONFIG_MP2_ENCODER && CONFIG_MP2FIXED_ENCODER ? (s)->is_fixed : CONFIG_MP2FIXED_ENCODER)
 
-static av_cold int MPA_encode_init(AVCodecContext *avctx)
+static av_cold int mpa_encode_init(AVCodecContext *avctx)
 {
     MpegAudioContext *s = avctx->priv_data;
     int freq = avctx->sample_rate;
@@ -752,7 +752,7 @@ static void encode_frame(MpegAudioContext *s, uint8_t *buf, unsigned buf_size,
         memset(put_bits_ptr(p), 0, put_bytes_left(p, 0));
 }
 
-static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
+static int mpa_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                             const AVFrame *frame, int *got_packet_ptr)
 {
     MpegAudioContext *s = avctx->priv_data;
@@ -800,8 +800,8 @@ const FFCodec ff_mp2_encoder = {
     .p.id                  = AV_CODEC_ID_MP2,
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .priv_data_size        = sizeof(MpegAudioContext),
-    .init                  = MPA_encode_init,
-    FF_CODEC_ENCODE_CB(MPA_encode_frame),
+    .init                  = mpa_encode_init,
+    FF_CODEC_ENCODE_CB(mpa_encode_frame),
     CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
     CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
     CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
@@ -815,7 +815,7 @@ static av_cold int mpa_fixed_encode_init(AVCodecContext *avctx)
     MpegAudioContext *s = avctx->priv_data;
 
     s->is_fixed = 1;
-    return MPA_encode_init(avctx);
+    return mpa_encode_init(avctx);
 }
 
 const FFCodec ff_mp2fixed_encoder = {
@@ -826,7 +826,7 @@ const FFCodec ff_mp2fixed_encoder = {
     .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .priv_data_size        = sizeof(MpegAudioContext),
     .init                  = mpa_fixed_encode_init,
-    FF_CODEC_ENCODE_CB(MPA_encode_frame),
+    FF_CODEC_ENCODE_CB(mpa_encode_frame),
     CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
     CODEC_SAMPLERATES(44100, 48000, 32000, 22050, 24000, 16000),
     CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
-- 
2.45.2


[-- Attachment #17: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused
  2025-04-10 20:59 [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused Andreas Rheinhardt
@ 2025-04-10 22:04 ` James Almer
  2025-04-10 22:36   ` Andreas Rheinhardt
  2025-04-13  6:17 ` Andreas Rheinhardt
  1 sibling, 1 reply; 5+ messages in thread
From: James Almer @ 2025-04-10 22:04 UTC (permalink / raw)
  To: ffmpeg-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 1002 bytes --]

> From 9f052c0377a5b46e48d928f11d1d10c389ea5e84 Mon Sep 17 00:00:00 2001
> From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> Date: Thu, 10 Apr 2025 13:41:55 +0200
> Subject: [PATCH 03/15] avutil/aes: Use #if checks instead of if (ARCH_X86)
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavutil/aes.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/libavutil/aes.c b/libavutil/aes.c
> index 13a34c3bf6..3c8ac1c349 100644
> --- a/libavutil/aes.c
> +++ b/libavutil/aes.c
> @@ -237,8 +237,9 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
>  
>      a->rounds = rounds;
>      a->crypt = decrypt ? aes_decrypt : aes_encrypt;
> -    if (ARCH_X86)
> -        ff_init_aes_x86(a, decrypt);
> +#if ARCH_X86
> +    ff_init_aes_x86(a, decrypt);
> +#endif
>  
>      ff_thread_once(&aes_static_init, aes_init_static);
>  
> -- 
> 2.45.2
> 

Why? DCE should handle this fine.


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused
  2025-04-10 22:04 ` James Almer
@ 2025-04-10 22:36   ` Andreas Rheinhardt
  2025-04-11  0:48     ` James Almer
  0 siblings, 1 reply; 5+ messages in thread
From: Andreas Rheinhardt @ 2025-04-10 22:36 UTC (permalink / raw)
  To: ffmpeg-devel

James Almer:
>> From 9f052c0377a5b46e48d928f11d1d10c389ea5e84 Mon Sep 17 00:00:00 2001
>> From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
>> Date: Thu, 10 Apr 2025 13:41:55 +0200
>> Subject: [PATCH 03/15] avutil/aes: Use #if checks instead of if
>> (ARCH_X86)
>>
>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
>> ---
>>  libavutil/aes.c | 5 +++--
>>  1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavutil/aes.c b/libavutil/aes.c
>> index 13a34c3bf6..3c8ac1c349 100644
>> --- a/libavutil/aes.c
>> +++ b/libavutil/aes.c
>> @@ -237,8 +237,9 @@ int av_aes_init(AVAES *a, const uint8_t *key, int
>> key_bits, int decrypt)
>>  
>>      a->rounds = rounds;
>>      a->crypt = decrypt ? aes_decrypt : aes_encrypt;
>> -    if (ARCH_X86)
>> -        ff_init_aes_x86(a, decrypt);
>> +#if ARCH_X86
>> +    ff_init_aes_x86(a, decrypt);
>> +#endif
>>  
>>      ff_thread_once(&aes_static_init, aes_init_static);
>>  
>> -- 
>> 2.45.2
>>
> 
> Why? DCE should handle this fine.
> 

DCE is not mandated by the spec. We should therefore not rely on it. See
also 40e6575aa3eed64cd32bf28c00ae57edc5acb25a.

- Andreas

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused
  2025-04-10 22:36   ` Andreas Rheinhardt
@ 2025-04-11  0:48     ` James Almer
  0 siblings, 0 replies; 5+ messages in thread
From: James Almer @ 2025-04-11  0:48 UTC (permalink / raw)
  To: ffmpeg-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 1342 bytes --]

On 4/10/2025 7:36 PM, Andreas Rheinhardt wrote:
> James Almer:
>>>  From 9f052c0377a5b46e48d928f11d1d10c389ea5e84 Mon Sep 17 00:00:00 2001
>>> From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
>>> Date: Thu, 10 Apr 2025 13:41:55 +0200
>>> Subject: [PATCH 03/15] avutil/aes: Use #if checks instead of if
>>> (ARCH_X86)
>>>
>>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
>>> ---
>>>   libavutil/aes.c | 5 +++--
>>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/libavutil/aes.c b/libavutil/aes.c
>>> index 13a34c3bf6..3c8ac1c349 100644
>>> --- a/libavutil/aes.c
>>> +++ b/libavutil/aes.c
>>> @@ -237,8 +237,9 @@ int av_aes_init(AVAES *a, const uint8_t *key, int
>>> key_bits, int decrypt)
>>>   
>>>       a->rounds = rounds;
>>>       a->crypt = decrypt ? aes_decrypt : aes_encrypt;
>>> -    if (ARCH_X86)
>>> -        ff_init_aes_x86(a, decrypt);
>>> +#if ARCH_X86
>>> +    ff_init_aes_x86(a, decrypt);
>>> +#endif
>>>   
>>>       ff_thread_once(&aes_static_init, aes_init_static);
>>>   
>>> -- 
>>> 2.45.2
>>>
>>
>> Why? DCE should handle this fine.
>>
> 
> DCE is not mandated by the spec. We should therefore not rely on it. See
> also 40e6575aa3eed64cd32bf28c00ae57edc5acb25a.
> 
> - Andreas

Ok, LGTM then.


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused
  2025-04-10 20:59 [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused Andreas Rheinhardt
  2025-04-10 22:04 ` James Almer
@ 2025-04-13  6:17 ` Andreas Rheinhardt
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Rheinhardt @ 2025-04-13  6:17 UTC (permalink / raw)
  To: ffmpeg-devel

Andreas Rheinhardt:
> Patches attached.
> 
> - Andreas
> 
Will apply this patchset tonight unless there are objections.

- Andreas

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-13  6:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-10 20:59 [FFmpeg-devel] [PATCH 01/15] avcodec/motionpixels: Avoid av_unused Andreas Rheinhardt
2025-04-10 22:04 ` James Almer
2025-04-10 22:36   ` Andreas Rheinhardt
2025-04-11  0:48     ` James Almer
2025-04-13  6:17 ` Andreas Rheinhardt

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