Commit 1a54c7b714 for ffmpeg
commit 1a54c7b714e1953902a78b8269259124c1161e1c
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Fri Nov 27 22:16:14 2026 -0600
avcodec/libshine: support the shine 4.0 API
shine 4.0 moves its API to shine/shine.h, renames the 3.x constants and
fields, and adds a psychoacoustic model behind mpeg.quality, which is
mapped to compression_level. configure picks shine.h when present;
shine 3.x keeps building through a fallback to the 3.x names.
Take the encoder delay from shine_encoder_delay(): it depends on the
quality.
Two channels are coded as joint stereo, as shineenc does by default.
diff --git a/configure b/configure
index 927c8d29ae..391eae937e 100755
--- a/configure
+++ b/configure
@@ -2464,6 +2464,7 @@ HEADERS_LIST="
malloc_h
poll_h
pthread_np_h
+ shine_shine_h
sys_hwprobe_h
sys_param_h
sys_resource_h
@@ -7418,7 +7419,8 @@ enabled librist && require_pkg_config librist "librist >= 0.2.7" libri
enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_new_from_data
enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket
enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && append librubberband_extralibs "-lstdc++"
-enabled libshine && require_pkg_config libshine shine shine/layer3.h shine_encode_buffer
+enabled libshine && { check_pkg_config libshine shine shine/shine.h shine_encoder_delay ||
+ require_pkg_config libshine shine shine/layer3.h shine_encode_buffer; }
enabled libsmbclient && { check_pkg_config libsmbclient smbclient libsmbclient.h smbc_init ||
require libsmbclient libsmbclient.h smbc_init -lsmbclient; }
enabled libsnappy && require libsnappy snappy-c.h snappy_compress -lsnappy -lstdc++
diff --git a/doc/encoders.texi b/doc/encoders.texi
index b452e806ec..88a5c8c8a2 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1171,6 +1171,11 @@ The following options are supported by the libshine wrapper. The
Set bitrate expressed in bits/s for CBR. @command{shineenc} @option{-b} option
is expressed in kilobits/s.
+@item compression_level (@emph{-Q})
+Set the quality with shine 4.0 and later: @samp{0} is the fast path without
+the psychoacoustic model, @samp{1} enables the model and is the default.
+Ignored with shine 3.x.
+
@end table
@section libtwolame
diff --git a/libavcodec/libshine.c b/libavcodec/libshine.c
index efd5665f57..f1cefc2dbe 100644
--- a/libavcodec/libshine.c
+++ b/libavcodec/libshine.c
@@ -19,7 +19,13 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "config.h"
+
+#if HAVE_SHINE_SHINE_H
+#include <shine/shine.h>
+#else
#include <shine/layer3.h>
+#endif
#include "libavutil/channel_layout.h"
#include "libavutil/intreadwrite.h"
@@ -32,11 +38,17 @@
#define BUFFER_SIZE (4096 * 20)
+/* shine 3.x, before its API moved to shine.h, under the 4.0 names. */
+#if !HAVE_SHINE_SHINE_H
+#define SHINE_MONO MONO
+#define SHINE_JOINT_STEREO JOINT_STEREO
+#define bitrate bitr
/**
* Delay of the analysis filterbank, polyphase (240) and MDCT (288), as broken
* down in LAME's libmp3lame/encoder.h. shine adds no padding of its own.
*/
-#define SHINE_ENCODER_DELAY 528
+#define shine_encoder_delay(shine) 528
+#endif
typedef struct SHINEContext {
shine_config_t config;
@@ -52,11 +64,16 @@ static av_cold int libshine_encode_init(AVCodecContext *avctx)
shine_set_config_mpeg_defaults(&s->config.mpeg);
if (avctx->bit_rate)
- s->config.mpeg.bitr = avctx->bit_rate / 1000;
- s->config.mpeg.mode = avctx->ch_layout.nb_channels == 2 ? STEREO : MONO;
+ s->config.mpeg.bitrate = avctx->bit_rate / 1000;
+#if HAVE_SHINE_SHINE_H
+ if (avctx->compression_level != FF_COMPRESSION_DEFAULT)
+ s->config.mpeg.quality = avctx->compression_level;
+#endif
+ s->config.mpeg.mode = avctx->ch_layout.nb_channels == 2 ? SHINE_JOINT_STEREO
+ : SHINE_MONO;
s->config.wave.samplerate = avctx->sample_rate;
- s->config.wave.channels = avctx->ch_layout.nb_channels == 2 ? PCM_STEREO : PCM_MONO;
- if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitr) < 0) {
+ s->config.wave.channels = avctx->ch_layout.nb_channels;
+ if (shine_check_config(s->config.wave.samplerate, s->config.mpeg.bitrate) < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid configuration\n");
return AVERROR(EINVAL);
}
@@ -64,7 +81,7 @@ static av_cold int libshine_encode_init(AVCodecContext *avctx)
if (!s->shine)
return AVERROR(ENOMEM);
avctx->frame_size = shine_samples_per_pass(s->shine);
- avctx->initial_padding = SHINE_ENCODER_DELAY + MP3_DECODER_DELAY;
+ avctx->initial_padding = shine_encoder_delay(s->shine) + MP3_DECODER_DELAY;
ff_af_queue_init(avctx, &s->afq);
return 0;
}
diff --git a/tests/fate/gapless.mak b/tests/fate/gapless.mak
index 571dc0cd90..5e2579256e 100644
--- a/tests/fate/gapless.mak
+++ b/tests/fate/gapless.mak
@@ -37,10 +37,10 @@ FATE_GAPLESSENC_PROBE-$(call ALLYES, MP3_DEMUXER MP3_MUXER NULL_MUXER) += fate-g
fate-gapless-mp3-itunes-remux: CMD = transcode mp3 $(TARGET_SAMPLES)/gapless/gapless-itunes.mp3 mp3 "-c copy" "-c copy" "-of compact -show_entries stream=start_pts,duration_ts" "" "" "" null
# Only the start is checked, as the encoded data and the end of the stream
-# depend on the shine version.
+# depend on the shine version. Level 0 has the same delay with all versions.
FATE_GAPLESSENC_LOCAL-$(call ALLYES, FFMPEG FFPROBE FILE_PROTOCOL LIBSHINE_ENCODER MP3_MUXER MP3_DEMUXER WAV_DEMUXER PCM_S16LE_DECODER ARESAMPLE_FILTER) += fate-gaplessenc-pcm-to-mp3-libshine
fate-gaplessenc-pcm-to-mp3-libshine: tests/data/asynth-44100-2.wav
-fate-gaplessenc-pcm-to-mp3-libshine: CMD = enc_external tests/data/asynth-44100-2.wav mp3 "-af aresample -c:a libshine" "-of compact -show_entries stream=start_pts"
+fate-gaplessenc-pcm-to-mp3-libshine: CMD = enc_external tests/data/asynth-44100-2.wav mp3 "-af aresample -c:a libshine -compression_level 0" "-of compact -show_entries stream=start_pts"
# libmp3lame at 22050 Hz spreads its 1105 samples of delay over the first two
# 576 sample packets.