Commit b43823c32e for ffmpeg

commit b43823c32ee7dced4a6775a842af9e9360009dd5
Author: Emilot <info@collybia.com>
Date:   Wed Dec 2 21:24:44 2026 +0200

    avformat/sacddec: add an option to select the audio area

    The two areas of a disc are mixes of their own, whose tracks can start at
    other times: track 4 of The Dark Side of the Moon starts 2.5 s earlier in
    the stereo area than in the multi channel one. The chapters are the tracks
    of the first area, the multi channel one, so they do not fit the stereo
    stream.

    The area option reads only the multi channel or only the stereo area,
    whose own tracks are then the chapters. An area that the disc does not
    have fails with AVERROR_STREAM_NOT_FOUND. The default, all, reads both
    areas as before.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
    Claude-Session: https://claude.ai/code/session_01XyLiriQxGzntjWpqQWboNV
    Signed-off-by: Emilot <info@collybia.com>

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index 60680a7149..b64a82a01d 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -1201,6 +1201,16 @@ Images with 2048 bytes per sector are detected automatically. Images that
 hold the raw sectors of 2064 bytes need the format to be forced with
 @code{-f sacd}, or a @option{formatprobesize} of at least 2M.

+@subsection Options
+
+@table @option
+@item area
+Select the audio area to read: @samp{all} (default), @samp{multichannel} or
+@samp{stereo}. The tracks of the two areas can differ, so select
+@samp{stereo} to get chapters that match the stereo mix. Opening the input
+fails if the disc does not have the selected area.
+@end table
+
 @section sbg

 SBaGen script demuxer.
diff --git a/libavformat/sacddec.c b/libavformat/sacddec.c
index da4c68aec5..2614ec8a4f 100644
--- a/libavformat/sacddec.c
+++ b/libavformat/sacddec.c
@@ -62,6 +62,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
+#include "libavutil/opt.h"

 #include "avformat.h"
 #include "avio_internal.h"
@@ -98,6 +99,12 @@ static const struct {
     { 0x40, "TWOCHTOC", "2-Channel Stereo" },
 };

+enum {
+    AREA_ALL = -1,
+    AREA_MULTI_CHANNEL,
+    AREA_STEREO,
+};
+
 enum {
     FRAME_FORMAT_DST         = 0,
     FRAME_FORMAT_DSD_3_IN_14 = 2,
@@ -127,6 +134,9 @@ typedef struct SACDArea {
 } SACDArea;

 typedef struct SACDContext {
+    const AVClass *class;
+    int area;                   /* the area to read, or AREA_ALL */
+
     int sector_size;            /* bytes per sector in the image */
     int sector_offset;          /* offset of the data in a sector */

@@ -674,16 +684,26 @@ end:

 static int sacd_read_header(AVFormatContext *s)
 {
+    SACDContext *c = s->priv_data;
     uint32_t toc_lsn[FF_ARRAY_ELEMS(areas)];
     int ret;

     if ((ret = read_master_toc(s, toc_lsn)) < 0)
         return ret;

+    if (c->area != AREA_ALL && !toc_lsn[c->area]) {
+        av_log(s, AV_LOG_ERROR, "The disc has no %s area\n",
+               areas[c->area].name);
+        return AVERROR_STREAM_NOT_FOUND;
+    }
+
+    /* the chapters are the tracks of the first area read, and the other
+     * area only matters for the ISRCs, when both are read */
     for (int i = 0; i < FF_ARRAY_ELEMS(areas); i++) {
-        if (!toc_lsn[i])
+        if (!toc_lsn[i] || (c->area != AREA_ALL && i != c->area))
             continue;
-        ret = read_area_toc(s, i, toc_lsn[i], toc_lsn[!i]);
+        ret = read_area_toc(s, i, toc_lsn[i],
+                            c->area == AREA_ALL ? toc_lsn[!i] : 0);
         if (ret < 0)
             return ret;
     }
@@ -877,10 +897,29 @@ static int sacd_read_close(AVFormatContext *s)
     return 0;
 }

+#define OFFSET(x) offsetof(SACDContext, x)
+#define DEC AV_OPT_FLAG_DECODING_PARAM
+static const AVOption sacd_options[] = {
+    { "area", "audio area to read", OFFSET(area), AV_OPT_TYPE_INT, { .i64 = AREA_ALL }, AREA_ALL, AREA_STEREO, DEC, .unit = "area" },
+        { "all",          "all areas",                  0, AV_OPT_TYPE_CONST, { .i64 = AREA_ALL },           0, 0, DEC, .unit = "area" },
+        { "multichannel", "only the multichannel area", 0, AV_OPT_TYPE_CONST, { .i64 = AREA_MULTI_CHANNEL }, 0, 0, DEC, .unit = "area" },
+        { "stereo",       "only the stereo area",       0, AV_OPT_TYPE_CONST, { .i64 = AREA_STEREO },        0, 0, DEC, .unit = "area" },
+    { NULL },
+};
+
+static const AVClass sacd_demuxer_class = {
+    .class_name = "SACD demuxer",
+    .item_name  = av_default_item_name,
+    .option     = sacd_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+    .category   = AV_CLASS_CATEGORY_DEMUXER,
+};
+
 const FFInputFormat ff_sacd_demuxer = {
     .p.name         = "sacd",
     .p.long_name    = NULL_IF_CONFIG_SMALL("Super Audio CD (SACD)"),
     .p.flags        = AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
+    .p.priv_class   = &sacd_demuxer_class,
     .priv_data_size = sizeof(SACDContext),
     .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
     .read_probe     = sacd_probe,
diff --git a/libavformat/version.h b/libavformat/version.h
index 683184d5da..2f54f57c02 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 #include "version_major.h"

 #define LIBAVFORMAT_VERSION_MINOR  21
-#define LIBAVFORMAT_VERSION_MICRO 100
+#define LIBAVFORMAT_VERSION_MICRO 101

 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak
index d8e9e8b48e..c12a713a98 100644
--- a/tests/fate/demux.mak
+++ b/tests/fate/demux.mak
@@ -137,6 +137,9 @@ SACD_TOC_ENTRIES = format_tags:program=program_id,nb_streams:program_tags:chapte
 FATE_FFPROBE_DEMUX-$(CONFIG_SACD_DEMUXER) += fate-sacd-toc
 fate-sacd-toc: CMD = probe -show_entries $(SACD_TOC_ENTRIES) $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso

+FATE_FFPROBE_DEMUX-$(CONFIG_SACD_DEMUXER) += fate-sacd-toc-stereo
+fate-sacd-toc-stereo: CMD = probe -area stereo -show_entries $(SACD_TOC_ENTRIES) $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso
+
 FATE_SAMPLES_DEMUX-$(call FRAMECRC, STR) += fate-psx-str-demux
 fate-psx-str-demux: CMD = framecrc -i $(TARGET_SAMPLES)/psx-str/descent-partial.str -c copy

diff --git a/tests/ref/fate/sacd-toc-stereo b/tests/ref/fate/sacd-toc-stereo
new file mode 100644
index 0000000000..435372f181
--- /dev/null
+++ b/tests/ref/fate/sacd-toc-stereo
@@ -0,0 +1,58 @@
+[PROGRAM]
+program_id=2
+nb_streams=1
+TAG:title=2-Channel Stereo
+TAG:description=2ch area
+TAG:copyright=(c) stereo
+[STREAM]
+index=0
+codec_name=dsd_msbf
+sample_rate=352800
+channel_layout=stereo
+duration_ts=188160
+[/STREAM]
+[/PROGRAM]
+[STREAM]
+index=0
+codec_name=dsd_msbf
+sample_rate=352800
+channel_layout=stereo
+duration_ts=188160
+[/STREAM]
+[CHAPTER]
+id=1
+time_base=1/75
+start=0
+end=20
+TAG:title=Tone 1 kHz
+TAG:artist=Señor Sine
+TAG:composer=Delta Sigma
+TAG:songwriter=Songwriter
+TAG:arranger=Arranger
+TAG:comment=Message
+TAG:isrc=XXABC2600001
+TAG:genre=Sound Effects
+[/CHAPTER]
+[CHAPTER]
+id=2
+time_base=1/75
+start=22
+end=40
+TAG:title=Tone 500 Hz
+TAG:artist=Señor Sine
+TAG:isrc=XXABC2600002
+TAG:genre=Sound Effects
+[/CHAPTER]
+[FORMAT]
+TAG:disc=1/2
+TAG:album_catalog_number=0123456789012
+TAG:catalog_number=TEST-0001
+TAG:genre=Sound Effects
+TAG:date=2026-11-10
+TAG:title=Test disc
+TAG:artist=Disc artist
+TAG:album=Test album
+TAG:album_artist=Album artist
+TAG:publisher=Album publisher
+TAG:copyright=© Album
+[/FORMAT]