PR #25936 opened by Kacper Michajłow (kasper93)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25936
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25936.patch


From 4e7f13af8fd7b061d58c49c474cfeaf55a50ef91 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 10 Nov 2026 17:07:01 +0100
Subject: [PATCH 1/2] avformat: add a Super Audio CD demuxer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Read a Super Audio CD from a disc image or a device, with 2048 bytes per
sector or with the raw sectors of 2064 bytes.

A disc holds its tracks as a multi channel mix, as a 2-channel stereo mix,
or as both. Each mix is a program with one audio stream, and the tracks
are the chapters. The sectors of a mix are only read while its stream is
not discarded.

The audio is plain DSD or DST and comes as one frame of 1/75 s per packet,
from the start of the first track. Seeking is exact, the frames of plain
DSD are at fixed places and DST comes with an Access List.

The texts of the disc and of the tracks are read from the first Text
Channel, if its character set is ISO 646 or ISO 8859-1.

The layout is taken from the (expired) patents and documents listed in
the file.

Signed-off-by: Kacper Michajłow <[email protected]>
---
 Changelog                 |   1 +
 doc/demuxers.texi         |  12 +
 doc/general_contents.texi |   1 +
 libavformat/Makefile      |   1 +
 libavformat/allformats.c  |   1 +
 libavformat/sacddec.c     | 805 ++++++++++++++++++++++++++++++++++++++
 libavformat/version.h     |   2 +-
 7 files changed, 822 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/sacddec.c

diff --git a/Changelog b/Changelog
index bce79b181f7..0e5578ac28c 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@ version <next>:
 - LCEVC raw demuxer and muxer
 - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder
 - PCM PAMF decoder
+- Super Audio CD (SACD) demuxer
 
 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 
muxers and demuxers
diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index e3e2018471e..e143123d22c 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -1151,6 +1151,18 @@ Note that the SCC format does not support all of the 
possible CC extensions
 that can be stored in RCWT (such as EIA-708).
 @end itemize
 
+@section sacd
+
+Super Audio CD (SACD) demuxer.
+
+It reads a disc image or a device. The multi channel and the 2-channel stereo
+version of a disc are programs with one audio stream each, and the tracks are
+chapters.
+
+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.
+
 @section sbg
 
 SBaGen script demuxer.
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 0176cd08750..ffbe33be0b0 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -838,6 +838,7 @@ library:
 @item SoX native format         @tab X @tab X
 @item SUN AU format             @tab X @tab X
 @item SUP raw PGS subtitles     @tab X @tab X
+@item Super Audio CD (SACD)     @tab   @tab X
 @item SVAG                      @tab   @tab X
     @tab Audio format used in Konami PS2 games.
 @item TDSC                      @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 17da02eee7f..caae8b34244 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -559,6 +559,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o rtspdec.o httpauth.o
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o
 OBJS-$(CONFIG_S337M_DEMUXER)             += s337m.o spdif.o
+OBJS-$(CONFIG_SACD_DEMUXER)              += sacddec.o
 OBJS-$(CONFIG_SAMI_DEMUXER)              += samidec.o subtitles.o
 OBJS-$(CONFIG_SAP_DEMUXER)               += sapdec.o
 OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 3306d71bf7b..db5656c7dbc 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -427,6 +427,7 @@ extern const FFOutputFormat ff_rtp_mpegts_muxer;
 extern const FFInputFormat  ff_rtsp_demuxer;
 extern const FFOutputFormat ff_rtsp_muxer;
 extern const FFInputFormat  ff_s337m_demuxer;
+extern const FFInputFormat  ff_sacd_demuxer;
 extern const FFInputFormat  ff_sami_demuxer;
 extern const FFInputFormat  ff_sap_demuxer;
 extern const FFOutputFormat ff_sap_muxer;
diff --git a/libavformat/sacddec.c b/libavformat/sacddec.c
new file mode 100644
index 00000000000..0cbbcb8d073
--- /dev/null
+++ b/libavformat/sacddec.c
@@ -0,0 +1,805 @@
+/*
+ * Super Audio CD (SACD) demuxer
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * Super Audio CD (SACD) demuxer
+ *
+ * A disc holds a Master TOC and up to two Audio Areas, a multi channel one
+ * and a 2-channel stereo one, which are two mixes of the same tracks. Each
+ * area has an Area TOC in front of and behind its Track Area. The Track Area
+ * is a run of Audio Sectors, whose headers cut it into the packets of
+ * Multiplexed Frames of 1/75 s, which carry either plain DSD or one DST
+ * frame.
+ *
+ * Each area is a program with one stream here, and the tracks are the
+ * chapters.
+ *
+ * The structure and field names are the ones of the Super Audio CD System
+ * Description, as far as they are public.
+ *
+ *   Master TOC                              EP 0956557 B1
+ *   Audio Area, Audio Sector, Time Code     US 6522695 B1
+ *   Audio_Header, Packet_Info               US 6661762 B1
+ *   Frame_Info                              US 7028152 B2
+ *   frame formats with 14 and 16 sectors    US 6275457 B1
+ *   Track_List_2                            EP 0987709 A2
+ *   Access_List                             US 6643228 B1
+ *   channel order, pauses                   Philips, DSDIFF 1.5
+ *   character sets, text items              Sonic Studio, SACD Text Overview
+ */
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/common.h"
+#include "libavutil/dict.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
+
+#include "avformat.h"
+#include "avio_internal.h"
+#include "demux.h"
+#include "internal.h"
+
+#define SECTOR_SIZE          2048
+/* An image may also keep the whole data frame of each sector, with the ID,
+ * IED and CPR_MAI in front of the data and the EDC behind it (ECMA-267, 16). 
*/
+#define RAW_SECTOR_SIZE      2064
+#define RAW_SECTOR_HEADER      12
+
+#define MASTER_TOC_LSN        510
+
+#define FRAMES_PER_SECOND      75
+#define BITS_PER_SAMPLE         8
+/* DSD has samples of one bit at 64 * 44100 Hz */
+#define SAMPLE_RATE       (64 * 44100 / BITS_PER_SAMPLE)
+#define FRAME_SAMPLES     (SAMPLE_RATE / FRAMES_PER_SECOND)
+
+#define MAX_FRAME_SECTORS      16
+#define MAX_PACKET_SIZE   (SECTOR_SIZE - 3)
+#define MAX_FRAME_SIZE    (MAX_FRAME_SECTORS * MAX_PACKET_SIZE)
+
+#define MAX_TRACKS            255
+#define MAX_TRACK_TEXT_SECTORS 32
+
+static const struct {
+    int toc_address;            /* offset of the address in Master_TOC_0 */
+    const char *signature;
+    const char *name;
+} areas[] = {
+    { 0x48, "MULCHTOC", "Multi Channel"    },
+    { 0x40, "TWOCHTOC", "2-Channel Stereo" },
+};
+
+enum {
+    FRAME_FORMAT_DST         = 0,
+    FRAME_FORMAT_DSD_3_IN_14 = 2,
+    FRAME_FORMAT_DSD_3_IN_16 = 3,
+};
+
+#define DATA_TYPE_AUDIO         2
+
+typedef struct SACDArea {
+    int frame_format;
+    uint32_t start;             /* first sector of the Track Area */
+    uint32_t end;               /* last sector of the Track Area */
+    int nb_frames;              /* play time of the Track Area */
+    int first_frame;            /* time code of the start of the first track */
+
+    uint8_t *access_list;       /* entries of 5 bytes */
+    int access_step;            /* frames per entry */
+
+    int64_t lsn;                /* next sector to read */
+    int frame;                  /* frames before this time code are dropped */
+    int idle;                   /* not read, its stream is discarded */
+    int nb_packets;             /* packets of the sector read last */
+    int packet;                 /* the next one of them */
+    int info_pos;               /* offset of the next Frame_Info */
+    int data_pos;               /* offset of the data of the next packet */
+    uint8_t sector[SECTOR_SIZE];
+} SACDArea;
+
+typedef struct SACDContext {
+    int sector_size;            /* bytes per sector in the image */
+    int sector_offset;          /* offset of the data in a sector */
+
+    int64_t pos;                /* next frame, counted from the first track */
+    uint8_t frame[MAX_FRAME_SIZE];
+} SACDContext;
+
+static int sacd_probe(const AVProbeData *p)
+{
+    static const int offsets[] = {
+        MASTER_TOC_LSN * SECTOR_SIZE,
+        MASTER_TOC_LSN * RAW_SECTOR_SIZE + RAW_SECTOR_HEADER,
+    };
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(offsets); i++)
+        if (p->buf_size >= offsets[i] + 8 &&
+            !memcmp(p->buf + offsets[i], "SACDMTOC", 8))
+            return AVPROBE_SCORE_MAX;
+    return 0;
+}
+
+static int read_sectors(AVFormatContext *s, int64_t lsn, uint8_t *buf, int nb)
+{
+    SACDContext *c = s->priv_data;
+
+    for (int i = 0; i < nb; i++) {
+        int64_t pos = (lsn + i) * c->sector_size + c->sector_offset;
+        int64_t ret = avio_seek(s->pb, pos, SEEK_SET);
+        if (ret < 0)
+            return ret;
+        ret = ffio_read_size(s->pb, buf + i * SECTOR_SIZE, SECTOR_SIZE);
+        if (ret < 0)
+            return ret;
+    }
+    return 0;
+}
+
+static int time_code(const uint8_t *tc)
+{
+    if (tc[1] >= 60 || tc[2] >= FRAMES_PER_SECOND)
+        return -1;
+    return (tc[0] * 60 + tc[1]) * FRAMES_PER_SECOND + tc[2];
+}
+
+/* The text is ISO 8859-1, ends at a NUL or with its field and may be padded
+ * with spaces. */
+static void set_string(AVDictionary **m, const char *key, const uint8_t *str,
+                       int max_len, int flags)
+{
+    AVBPrint bp;
+    int len = 0;
+
+    for (int i = 0; i < max_len && str[i]; i++)
+        if (str[i] != ' ')
+            len = i + 1;
+    if (!len)
+        return;
+
+    av_bprint_init(&bp, len + 1, AV_BPRINT_SIZE_UNLIMITED);
+    for (int i = 0; i < len; i++) {
+        uint8_t tmp;
+        PUT_UTF8(str[i], tmp, av_bprint_chars(&bp, tmp, 1);)
+    }
+    if (av_bprint_is_complete(&bp))
+        av_dict_set(m, key, bp.str, flags);
+    av_bprint_finalize(&bp, NULL);
+}
+
+static void set_text(AVDictionary **m, const char *key, const uint8_t *buf,
+                     int size, int offset, int flags)
+{
+    if (offset && offset < size)
+        set_string(m, key, buf + offset, size - offset, flags);
+}
+
+/* The General Genre Table has the genre codes of CD-TEXT. */
+static const char *const genres[] = {
+    [ 2] = "Adult Contemporary",
+    [ 3] = "Alternative Rock",
+    [ 4] = "Children's Music",
+    [ 5] = "Classical",
+    [ 6] = "Contemporary Christian",
+    [ 7] = "Country",
+    [ 8] = "Dance",
+    [ 9] = "Easy Listening",
+    [10] = "Erotic",
+    [11] = "Folk",
+    [12] = "Gospel",
+    [13] = "Hip Hop",
+    [14] = "Jazz",
+    [15] = "Latin",
+    [16] = "Musical",
+    [17] = "New Age",
+    [18] = "Opera",
+    [19] = "Operetta",
+    [20] = "Pop Music",
+    [21] = "Rap",
+    [22] = "Reggae",
+    [23] = "Rock Music",
+    [24] = "Rhythm & Blues",
+    [25] = "Sound Effects",
+    [26] = "Sound Track",
+    [27] = "Spoken Word",
+    [28] = "World Music",
+    [29] = "Blues",
+};
+
+static void set_genre(AVDictionary **m, const uint8_t *genre)
+{
+    enum { GENERAL_GENRE_TABLE = 1 };
+    unsigned index = AV_RB16(genre + 2);
+
+    if (genre[0] == GENERAL_GENRE_TABLE && index < FF_ARRAY_ELEMS(genres) &&
+        genres[index])
+        av_dict_set(m, "genre", genres[index], AV_DICT_DONT_OVERWRITE);
+}
+
+static int text_supported(AVFormatContext *s, const uint8_t *text_channels)
+{
+    enum {
+        CHARSET_ISO646        = 1,
+        CHARSET_ISO8859_1     = 2,
+        CHARSET_ISO8859_1_ESC = 7,
+    };
+    int charset = text_channels[8 + 2];
+
+    if (!text_channels[0])
+        return 0;
+    if (charset == CHARSET_ISO646 || charset == CHARSET_ISO8859_1 ||
+        charset == CHARSET_ISO8859_1_ESC)
+        return 1;
+    avpriv_request_sample(s, "Text in character set %d", charset);
+    return 0;
+}
+
+static int read_master_toc(AVFormatContext *s, uint32_t *toc_lsn)
+{
+    /* the texts of the disc, then those of the album, which only fill in
+     * what the disc does not have */
+    static const struct {
+        int offset;
+        const char *key;
+    } texts[] = {
+        { 0x20, "title"        },
+        { 0x22, "artist"       },
+        { 0x24, "publisher"    },
+        { 0x26, "copyright"    },
+        { 0x10, "album"        },
+        { 0x12, "album_artist" },
+        { 0x14, "publisher"    },
+        { 0x16, "copyright"    },
+    };
+    SACDContext *c = s->priv_data;
+    uint8_t buf[SECTOR_SIZE];
+    char str[32];
+    int ret;
+
+    c->sector_size = SECTOR_SIZE;
+    if ((ret = read_sectors(s, MASTER_TOC_LSN, buf, 1)) < 0)
+        return ret;
+    if (memcmp(buf, "SACDMTOC", 8)) {
+        c->sector_size   = RAW_SECTOR_SIZE;
+        c->sector_offset = RAW_SECTOR_HEADER;
+        if ((ret = read_sectors(s, MASTER_TOC_LSN, buf, 1)) < 0)
+            return ret;
+        if (memcmp(buf, "SACDMTOC", 8))
+            return AVERROR_INVALIDDATA;
+    }
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(areas); i++)
+        toc_lsn[i] = AV_RB32(buf + areas[i].toc_address);
+
+    if (AV_RB16(buf + 0x10) && AV_RB16(buf + 0x12)) {
+        snprintf(str, sizeof(str), "%d/%d", AV_RB16(buf + 0x12),
+                 AV_RB16(buf + 0x10));
+        av_dict_set(&s->metadata, "disc", str, 0);
+    }
+    set_string(&s->metadata, "album_catalog_number", buf + 0x18, 16, 0);
+    set_string(&s->metadata, "catalog_number",       buf + 0x58, 16, 0);
+    for (int i = 0; i < 4; i++)
+        set_genre(&s->metadata, buf + 0x68 + 4 * i);
+    if (AV_RB16(buf + 0x78) && buf[0x7a] && buf[0x7b]) {
+        snprintf(str, sizeof(str), "%04d-%02d-%02d", AV_RB16(buf + 0x78),
+                 buf[0x7a], buf[0x7b]);
+        av_dict_set(&s->metadata, "date", str, 0);
+    }
+
+    /* only the first Text Channel is read, its texts are in the next sector */
+    if (!text_supported(s, buf + 0x80))
+        return 0;
+    if ((ret = read_sectors(s, MASTER_TOC_LSN + 1, buf, 1)) < 0)
+        return ret;
+    if (memcmp(buf, "SACDText", 8))
+        return 0;
+    for (int i = 0; i < FF_ARRAY_ELEMS(texts); i++)
+        set_text(&s->metadata, texts[i].key, buf, sizeof(buf),
+                 AV_RB16(buf + texts[i].offset), AV_DICT_DONT_OVERWRITE);
+
+    return 0;
+}
+
+static void read_track_text(AVDictionary **m, const uint8_t *buf, int size,
+                            int track)
+{
+    static const char *const keys[] = {
+        [1] = "title",
+        [2] = "artist",
+        [3] = "songwriter",
+        [4] = "composer",
+        [5] = "arranger",
+        [6] = "comment",
+    };
+    int pos = AV_RB16(buf + 8 + 2 * track);
+    int nb_items;
+
+    /* the items of a track follow their count, an item is its type, one more
+     * byte and the text, padded to a multiple of 4 bytes */
+    if (!pos || pos + 4 > size)
+        return;
+    nb_items = buf[pos];
+    pos     += 4;
+    for (int i = 0; i < nb_items && pos + 2 < size; i++) {
+        int type = buf[pos];
+        if (type < FF_ARRAY_ELEMS(keys) && keys[type])
+            set_string(m, keys[type], buf + pos + 2, size - pos - 2, 0);
+        pos += 2 + av_strnlen((const char *)buf + pos + 2, size - pos - 2) + 1;
+        pos  = FFALIGN(pos, 4);
+    }
+}
+
+static int read_access_list(AVFormatContext *s, SACDArea *a, int64_t lsn)
+{
+    uint8_t *buf;
+    int nb_entries, nb_sectors, ret;
+
+    buf = av_malloc(SECTOR_SIZE);
+    if (!buf)
+        return AVERROR(ENOMEM);
+    if ((ret = read_sectors(s, lsn, buf, 1)) < 0)
+        goto end;
+
+    nb_entries     = AV_RB16(buf + 8);
+    a->access_step = buf[10];
+    if (memcmp(buf, "SACD_ACC", 8) || !a->access_step ||
+        nb_entries != (a->nb_frames - 1) / a->access_step + 1) {
+        av_log(s, AV_LOG_WARNING, "No valid Access List, cannot seek\n");
+        goto end;
+    }
+
+    nb_sectors = (16 + 5 * nb_entries + SECTOR_SIZE - 1) / SECTOR_SIZE;
+    if ((ret = av_reallocp(&buf, nb_sectors * SECTOR_SIZE)) < 0)
+        return ret;
+    if ((ret = read_sectors(s, lsn + 1, buf + SECTOR_SIZE, nb_sectors - 1)) < 
0)
+        goto end;
+
+    memmove(buf, buf + 16, 5 * nb_entries);
+    a->access_list = buf;
+    return 0;
+
+end:
+    av_free(buf);
+    return ret;
+}
+
+static void seek_frame(AVFormatContext *s, AVStream *st, int64_t pos)
+{
+    SACDArea *a = st->priv_data;
+    int frame   = a->first_frame +
+                  FFMIN(pos, a->nb_frames - a->first_frame - 1);
+    int64_t lsn = a->start;
+
+    if (a->frame_format == FRAME_FORMAT_DSD_3_IN_14) {
+        lsn += frame / 3 * 14;
+    } else if (a->frame_format == FRAME_FORMAT_DSD_3_IN_16) {
+        lsn += frame / 3 * 16;
+    } else if (a->access_list) {
+        /* An entry has flags and the sector in which the first frame of its
+         * interval starts. The frames after it are interpolated up to the
+         * next entry, which lands at most Access_Margin sectors too far.
+         * With the Sub_Access flag there is no margin to go by. */
+        const uint8_t *entry = a->access_list + 5 * (frame / a->access_step);
+        int flags            = AV_RB16(entry);
+        int64_t start        = AV_RB24(entry + 2);
+        int64_t next, nb;
+
+        if (frame / a->access_step < (a->nb_frames - 1) / a->access_step) {
+            next = AV_RB24(entry + 5 + 2);
+            nb   = a->access_step;
+        } else {
+            next = a->end;
+            nb   = (a->nb_frames - 1) % a->access_step + 1;
+        }
+        lsn = start;
+        if (!(flags & 0x8000) && next > start)
+            lsn = FFMAX(start, start + frame % a->access_step *
+                                       (next - start) / nb - (flags & 0x7fff));
+        lsn = FFMAX(lsn, a->start);
+    }
+
+    av_log(s, AV_LOG_TRACE, "Stream %d goes on with frame %d from sector "
+           "%"PRId64"\n", st->index, frame, lsn);
+
+    a->lsn        = lsn;
+    a->frame      = frame;
+    a->nb_packets = a->packet = 0;
+}
+
+static int read_area_toc(AVFormatContext *s, int area, int64_t toc_lsn,
+                         int only)
+{
+    /* the channels are in the order of standard layouts */
+    static const AVChannelLayout layouts[] = {
+        [2] = AV_CHANNEL_LAYOUT_STEREO,
+        [5] = AV_CHANNEL_LAYOUT_5POINT0,
+        [6] = AV_CHANNEL_LAYOUT_5POINT1,
+    };
+    uint8_t buf[2 * SECTOR_SIZE], tracks[SECTOR_SIZE];
+    uint8_t *track_text = NULL;
+    int toc_size, nb_channels, nb_tracks, has_text;
+    int track_text_pos, track_text_size = 0, access_list_pos;
+    AVProgram *program;
+    AVStream *st;
+    SACDArea *a;
+    int ret;
+
+    if ((ret = read_sectors(s, toc_lsn, buf, 1)) < 0)
+        return ret;
+    if (memcmp(buf, areas[area].signature, 8))
+        return AVERROR_INVALIDDATA;
+
+    a = av_mallocz(sizeof(*a));
+    if (!a)
+        return AVERROR(ENOMEM);
+    st = avformat_new_stream(s, NULL);
+    if (!st) {
+        av_free(a);
+        return AVERROR(ENOMEM);
+    }
+    st->priv_data = a;
+
+    program = av_new_program(s, area + 1);
+    if (!program)
+        return AVERROR(ENOMEM);
+    av_program_add_stream_index(s, program->id, st->index);
+    av_dict_set(&program->metadata, "title", areas[area].name, 0);
+
+    toc_size        = AV_RB16(buf + 0x0a);
+    a->frame_format = buf[0x15] & 0xf;
+    nb_channels     = buf[0x20];
+    a->nb_frames    = time_code(buf + 0x40);
+    /* the tracks are the chapters, and as the areas are two mixes of the
+     * same tracks, those of the first area stand for both */
+    nb_tracks       = s->nb_streams > 1 ? 0 : buf[0x45];
+    a->start        = AV_RB32(buf + 0x48);
+    a->end          = AV_RB32(buf + 0x4c);
+    has_text        = text_supported(s, buf + 0x50);
+    track_text_pos  = AV_RB16(buf + 0x80);
+    access_list_pos = AV_RB16(buf + 0x84);
+
+    if (buf[0x14] != 4) {
+        avpriv_request_sample(s, "Sampling frequency code %d", buf[0x14]);
+        return AVERROR_PATCHWELCOME;
+    }
+    if (a->frame_format != FRAME_FORMAT_DST &&
+        a->frame_format != FRAME_FORMAT_DSD_3_IN_14 &&
+        a->frame_format != FRAME_FORMAT_DSD_3_IN_16) {
+        avpriv_request_sample(s, "Frame format %d", a->frame_format);
+        return AVERROR_PATCHWELCOME;
+    }
+    if (nb_channels >= FF_ARRAY_ELEMS(layouts) ||
+        !layouts[nb_channels].nb_channels ||
+        (nb_channels != 2 && a->frame_format != FRAME_FORMAT_DST) ||
+        a->start > a->end)
+        return AVERROR_INVALIDDATA;
+
+    if (has_text) {
+        set_text(&program->metadata, "description", buf, SECTOR_SIZE,
+                 AV_RB16(buf + 0x90), 0);
+        set_text(&program->metadata, "copyright", buf, SECTOR_SIZE,
+                 AV_RB16(buf + 0x92), 0);
+    }
+
+    if ((ret = read_sectors(s, toc_lsn + 2, tracks, 1)) < 0)
+        return ret;
+    if (memcmp(tracks, "SACDTRL2", 8))
+        return AVERROR_INVALIDDATA;
+    /* the first track has to start within the play time, which also turns
+     * down an area that has none */
+    a->first_frame = time_code(tracks + 8);
+    if (a->first_frame < 0 || a->first_frame >= a->nb_frames)
+        return AVERROR_INVALIDDATA;
+
+    if (nb_tracks) {
+        /* the ISRCs and the genres of the tracks */
+        if ((ret = read_sectors(s, toc_lsn + 3, buf, 2)) < 0)
+            return ret;
+        if (memcmp(buf, "SACD_IGL", 8))
+            return AVERROR_INVALIDDATA;
+    }
+
+    /* the lists are read in their order on the disc, which also works with
+     * an input that cannot seek back */
+    if (a->frame_format == FRAME_FORMAT_DST &&
+        (ret = read_access_list(s, a, toc_lsn + access_list_pos)) < 0)
+        return ret;
+
+    if (nb_tracks && has_text && track_text_pos && track_text_pos < toc_size) {
+        track_text_size = FFMIN(toc_size - track_text_pos,
+                                MAX_TRACK_TEXT_SECTORS) * SECTOR_SIZE;
+        track_text      = av_malloc(track_text_size);
+        if (!track_text)
+            return AVERROR(ENOMEM);
+        ret = read_sectors(s, toc_lsn + track_text_pos, track_text,
+                           track_text_size / SECTOR_SIZE);
+        if (ret < 0)
+            goto end;
+        if (memcmp(track_text, "SACDTTxt", 8))
+            track_text_size = 0;
+    }
+
+    for (int i = 0; i < nb_tracks; i++) {
+        int start  = time_code(tracks + 8 + 4 * i);
+        int length = time_code(tracks + 8 + 4 * MAX_TRACKS + 4 * i);
+        AVChapter *chapter;
+
+        if (start < a->first_frame || length < 0)
+            continue;
+        chapter = avpriv_new_chapter(s, i + 1,
+                                     (AVRational){ 1, FRAMES_PER_SECOND },
+                                     start - a->first_frame,
+                                     start + length - a->first_frame, NULL);
+        if (!chapter) {
+            ret = AVERROR(ENOMEM);
+            goto end;
+        }
+        if (track_text_size)
+            read_track_text(&chapter->metadata, track_text, track_text_size, 
i);
+        /* each mix is a recording with ISRCs of its own */
+        if (only)
+            set_string(&chapter->metadata, "isrc", buf + 8 + 12 * i, 12, 0);
+        set_genre(&chapter->metadata, buf + 8 + 12 * MAX_TRACKS + 4 + 4 * i);
+    }
+
+    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->format      = AV_SAMPLE_FMT_DSD;
+    st->codecpar->sample_rate = SAMPLE_RATE;
+    st->codecpar->ch_layout   = layouts[nb_channels];
+    if (a->frame_format == FRAME_FORMAT_DST) {
+        st->codecpar->codec_id = AV_CODEC_ID_DST;
+        st->codecpar->bit_rate = av_rescale(a->end - a->start + 1LL,
+                                            SECTOR_SIZE * 8 * 
FRAMES_PER_SECOND,
+                                            a->nb_frames);
+    } else {
+        st->codecpar->codec_id    = AV_CODEC_ID_DSD_MSBF;
+        st->codecpar->block_align = nb_channels;
+        st->codecpar->bit_rate    = nb_channels * BITS_PER_SAMPLE * 
SAMPLE_RATE;
+    }
+    avpriv_set_pts_info(st, 64, 1, SAMPLE_RATE);
+    st->start_time = 0;
+    st->duration   = (a->nb_frames - a->first_frame) * (int64_t)FRAME_SAMPLES;
+
+    seek_frame(s, st, 0);
+    ret = 0;
+
+end:
+    av_free(track_text);
+    return ret;
+}
+
+static int sacd_read_header(AVFormatContext *s)
+{
+    uint32_t toc_lsn[FF_ARRAY_ELEMS(areas)];
+    int ret;
+
+    if ((ret = read_master_toc(s, toc_lsn)) < 0)
+        return ret;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(areas); i++) {
+        if (!toc_lsn[i])
+            continue;
+        ret = read_area_toc(s, i, toc_lsn[i], !toc_lsn[0] || !toc_lsn[1]);
+        if (ret < 0)
+            return ret;
+    }
+
+    return s->nb_streams ? 0 : AVERROR_INVALIDDATA;
+}
+
+/* A sector starts with the number of its packets and of the frames that
+ * start in it, and a flag for DST. Then come a Packet_Info per packet, with
+ * a flag for the start of a frame, the type of the data and its length, a
+ * Frame_Info per frame start, with a fourth byte for DST, and the data. */
+static int read_audio_header(SACDArea *a)
+{
+    const uint8_t *buf = a->sector;
+    int dst         = a->frame_format == FRAME_FORMAT_DST;
+    int nb_packets  = buf[0] >> 5;
+    int nb_starts   = buf[0] >> 2 & 7;
+    int size        = 1 + 2 * nb_packets + (dst ? 4 : 3) * nb_starts;
+
+    if (!nb_packets || (buf[0] & 1) != dst)
+        return AVERROR_INVALIDDATA;
+
+    a->info_pos = 1 + 2 * nb_packets;
+    a->data_pos = size;
+    for (int i = 0; i < nb_packets; i++) {
+        int info = AV_RB16(buf + 1 + 2 * i);
+        if (!(info & 0x7ff))
+            return AVERROR_INVALIDDATA;
+        nb_starts -= info >> 15;
+        size      += info & 0x7ff;
+    }
+    if (nb_starts || size > SECTOR_SIZE)
+        return AVERROR_INVALIDDATA;
+
+    a->nb_packets = nb_packets;
+    a->packet     = 0;
+    return 0;
+}
+
+static int read_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
+{
+    SACDContext *c = s->priv_data;
+    SACDArea *a    = st->priv_data;
+    /* size of the frame collected so far, negative while there is none */
+    int size = -1;
+    int frame = 0;
+    int64_t pos = 0;
+    /* a run of invalid sectors is reported once */
+    int invalid = 0;
+    int ret;
+
+    for (;;) {
+        int info, length;
+
+        if (a->packet == a->nb_packets) {
+            if (a->lsn > a->end)
+                break;
+            if ((ret = read_sectors(s, a->lsn, a->sector, 1)) < 0)
+                return ret;
+            a->lsn++;
+            if (read_audio_header(a) < 0) {
+                if (!invalid)
+                    av_log(s, AV_LOG_WARNING, "Invalid Audio Sector "
+                           "%"PRId64"\n", a->lsn - 1);
+                invalid = 1;
+                size    = -1;
+                continue;
+            }
+            invalid = 0;
+        }
+
+        info   = AV_RB16(a->sector + 1 + 2 * a->packet);
+        length = info & 0x7ff;
+
+        if (info & 0x8000) {
+            /* a frame lasts up to the start of the next one, and a frame
+             * start that no audio packet follows is padding */
+            if (size > 0)
+                break;
+
+            frame        = time_code(a->sector + a->info_pos);
+            a->info_pos += a->frame_format == FRAME_FORMAT_DST ? 4 : 3;
+            size         = -1;
+            if (frame >= a->frame) {
+                pos  = (a->lsn - 1) * c->sector_size + c->sector_offset;
+                size = 0;
+            }
+        }
+
+        if (size >= 0 && (info >> 11 & 7) == DATA_TYPE_AUDIO) {
+            if (size + length > MAX_FRAME_SIZE) {
+                av_log(s, AV_LOG_WARNING, "Frame at sector %"PRId64" is too "
+                       "large\n", a->lsn - 1);
+                size = -1;
+            } else {
+                memcpy(c->frame + size, a->sector + a->data_pos, length);
+                size += length;
+            }
+        }
+
+        a->data_pos += length;
+        a->packet++;
+    }
+
+    if (size <= 0)
+        return AVERROR_EOF;
+
+    if ((ret = av_new_packet(pkt, size)) < 0)
+        return ret;
+    memcpy(pkt->data, c->frame, size);
+    pkt->stream_index = st->index;
+    pkt->pos          = pos;
+    pkt->pts          = (frame - (int64_t)a->first_frame) * FRAME_SAMPLES;
+    pkt->duration     = FRAME_SAMPLES;
+
+    a->frame = frame + 1;
+    c->pos   = a->frame - a->first_frame;
+    return 0;
+}
+
+static int sacd_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    SACDContext *c = s->priv_data;
+
+    for (;;) {
+        AVStream *st = NULL;
+        SACDArea *a  = NULL;
+        int ret;
+
+        /* the areas are read in the order of time */
+        for (unsigned i = 0; i < s->nb_streams; i++) {
+            AVStream *cur = s->streams[i];
+            SACDArea *b   = cur->priv_data;
+
+            if (cur->discard >= AVDISCARD_ALL) {
+                b->idle = 1;
+                continue;
+            }
+            /* an area that was not read goes on where the other one is */
+            if (b->idle)
+                seek_frame(s, cur, c->pos);
+            b->idle = 0;
+
+            if (b->frame < b->nb_frames &&
+                (!a || b->frame - b->first_frame < a->frame - a->first_frame)) 
{
+                st = cur;
+                a  = b;
+            }
+        }
+        if (!a)
+            return AVERROR_EOF;
+
+        ret = read_frame(s, st, pkt);
+        if (ret != AVERROR_EOF)
+            return ret;
+        /* this area has ended, go on with the other one */
+        a->frame = a->nb_frames;
+    }
+}
+
+static int sacd_read_seek(AVFormatContext *s, int stream_index,
+                          int64_t timestamp, int flags)
+{
+    SACDContext *c = s->priv_data;
+
+    for (unsigned i = 0; i < s->nb_streams; i++) {
+        const SACDArea *a = s->streams[i]->priv_data;
+        if (a->frame_format == FRAME_FORMAT_DST && !a->access_list)
+            return AVERROR(ENOSYS);
+    }
+
+    if (!(flags & AVSEEK_FLAG_BACKWARD))
+        timestamp = av_sat_add64(timestamp, FRAME_SAMPLES - 1);
+    c->pos = FFMAX(timestamp / FRAME_SAMPLES, 0);
+    for (unsigned i = 0; i < s->nb_streams; i++)
+        seek_frame(s, s->streams[i], c->pos);
+    return 0;
+}
+
+static int sacd_read_close(AVFormatContext *s)
+{
+    for (unsigned i = 0; i < s->nb_streams; i++) {
+        SACDArea *a = s->streams[i]->priv_data;
+        av_freep(&a->access_list);
+    }
+    return 0;
+}
+
+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,
+    .priv_data_size = sizeof(SACDContext),
+    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
+    .read_probe     = sacd_probe,
+    .read_header    = sacd_read_header,
+    .read_packet    = sacd_read_packet,
+    .read_seek      = sacd_read_seek,
+    .read_close     = sacd_read_close,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 6a80f3ac4e6..e063e12b983 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  18
+#define LIBAVFORMAT_VERSION_MINOR  19
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.54.0


From 3b6e598ae19517f9a43fd5639b03506d96f6f769 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 10 Nov 2026 18:17:50 +0100
Subject: [PATCH 2/2] tests/fate: add tests for the Super Audio CD demuxer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The sample is a generated disc image with 2-channel plain DSD and 5
channel DST.

Signed-off-by: Kacper Michajłow <[email protected]>
---
 tests/fate/audio.mak      |   3 +
 tests/fate/demux.mak      |   7 +++
 tests/fate/seek.mak       |   2 +
 tests/ref/fate/sacd-demux | 119 ++++++++++++++++++++++++++++++++++++++
 tests/ref/fate/sacd-dst   |  74 ++++++++++++++++++++++++
 tests/ref/fate/sacd-toc   |  68 ++++++++++++++++++++++
 tests/ref/seek/sacd       |  53 +++++++++++++++++
 7 files changed, 326 insertions(+)
 create mode 100644 tests/ref/fate/sacd-demux
 create mode 100644 tests/ref/fate/sacd-dst
 create mode 100644 tests/ref/fate/sacd-toc
 create mode 100644 tests/ref/seek/sacd

diff --git a/tests/fate/audio.mak b/tests/fate/audio.mak
index 35ea8861a6e..035441af017 100644
--- a/tests/fate/audio.mak
+++ b/tests/fate/audio.mak
@@ -99,6 +99,9 @@ fate-paf-audio: CMD = framecrc -i 
$(TARGET_SAMPLES)/paf/hod1-partial.paf -vn
 FATE_SAMPLES_AUDIO-$(call FRAMECRC, WAV, ADPCM_RHETOREX) += fate-rhetorex
 fate-rhetorex: CMD = framecrc -i 
$(TARGET_SAMPLES)/rhetorex/rhetorex-mono-4bit-8000.wav
 
+FATE_SAMPLES_AUDIO-$(call FRAMECRC, SACD, DST, DSD_MSBF_ENCODER) += 
fate-sacd-dst
+fate-sacd-dst: CMD = framecrc -request_sample_fmt dsd -i 
$(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -map 0:p:1 -c:a dsd_msbf
+
 FATE_SAMPLES_AUDIO-$(call FRAMECRC, VMD, VMDAUDIO, ARESAMPLE_FILTER) += 
fate-sierra-vmd-audio
 fate-sierra-vmd-audio: CMD = framecrc -i $(TARGET_SAMPLES)/vmd/12.vmd -vn -af 
aresample
 
diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak
index 58db704a0b3..d8e9e8b48e2 100644
--- a/tests/fate/demux.mak
+++ b/tests/fate/demux.mak
@@ -130,6 +130,13 @@ fate-rsd-demux: CMD = crc -i 
$(TARGET_SAMPLES)/rsd/hum01_partial.rsd -c:a copy
 FATE_SAMPLES_DEMUX-$(call CRC, REDSPARK) += fate-redspark-demux
 fate-redspark-demux: CMD = crc -i 
$(TARGET_SAMPLES)/redspark/jingle04_partial.rsd -c:a copy
 
+FATE_SAMPLES_DEMUX-$(call FRAMECRC, SACD) += fate-sacd-demux
+fate-sacd-demux: CMD = framecrc -i $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso 
-map 0 -c:a copy
+
+SACD_TOC_ENTRIES = 
format_tags:program=program_id,nb_streams:program_tags:chapter=id,time_base,start,end:chapter_tags:stream=index,codec_name,sample_rate,channel_layout,duration_ts
+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_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/fate/seek.mak b/tests/fate/seek.mak
index d33cef4d928..192a95b8da5 100644
--- a/tests/fate/seek.mak
+++ b/tests/fate/seek.mak
@@ -189,6 +189,7 @@ FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += 
fate-seek-empty-edit-mp4
 FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += fate-seek-test-iibbibb-mp4
 FATE_SEEK_EXTRA-$(call ALLYES, MOV_DEMUXER) += 
fate-seek-test-iibbibb-neg-ctts-mp4
 FATE_SEEK_EXTRA-$(CONFIG_WAV_DEMUXER)       += fate-seek-bad-avg-byterate
+FATE_SEEK_EXTRA-$(CONFIG_SACD_DEMUXER)      += fate-seek-sacd
 
 fate-seek-extra-mp3:  CMD = run libavformat/tests/seek$(EXESUF) 
$(TARGET_SAMPLES)/gapless/gapless.mp3 -fastseek 1
 fate-seek-bad-avg-byterate: CMD = run libavformat/tests/seek$(EXESUF) 
$(TARGET_SAMPLES)/wav/wrong-avg-byterate.wav -seekback 500 -stream_id 0
@@ -198,6 +199,7 @@ fate-seek-test-iibbibb-mp4:  CMD = run 
libavformat/tests/seek$(EXESUF) $(TARGET_
 fate-seek-test-iibbibb-neg-ctts-mp4:  CMD = run 
libavformat/tests/seek$(EXESUF) $(TARGET_SAMPLES)/mov/test_iibbibb_neg_ctts.mp4 
-duration 13 -frames 4
 fate-seek-cache-pipe: CMD = cat $(SAMPLES)/gapless/gapless.mp3 | run 
libavformat/tests/seek$(EXESUF) cache:pipe:0 -read_ahead_limit -1
 fate-seek-mkv-codec-delay:   CMD = run libavformat/tests/seek$(EXESUF) 
$(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv
+fate-seek-sacd: CMD = run libavformat/tests/seek$(EXESUF) 
$(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -duration 2
 
 # A master playlist with separate audio and video media playlists with 
different DTS.
 tests/data/hls-seek.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | tests/data
diff --git a/tests/ref/fate/sacd-demux b/tests/ref/fate/sacd-demux
new file mode 100644
index 00000000000..9a5ca9380d8
--- /dev/null
+++ b/tests/ref/fate/sacd-demux
@@ -0,0 +1,119 @@
+#tb 0: 1/352800
+#media_type 0: audio
+#codec_id 0: dst
+#sample_rate 0: 352800
+#channel_layout_name 0: 5.0(side)
+#tb 1: 1/352800
+#media_type 1: audio
+#codec_id 1: dsd_msbf
+#sample_rate 1: 352800
+#channel_layout_name 1: stereo
+0,          0,          0,     4704,    12432, 0xc11f1536
+1,          0,          0,     4704,     9408, 0xa17278fd
+0,       4704,       4704,     4704,    12210, 0x4ee6c684
+1,       4704,       4704,     4704,     9408, 0x8d723e06
+0,       9408,       9408,     4704,    12391, 0x60c12893
+1,       9408,       9408,     4704,     9408, 0xd7ed0412
+0,      14112,      14112,     4704,    12325, 0x9008da42
+1,      14112,      14112,     4704,     9408, 0x6dca7205
+0,      18816,      18816,     4704,    12226, 0x75e7cdd2
+1,      18816,      18816,     4704,     9408, 0x0cbd33b4
+0,      23520,      23520,     4704,    12466, 0x693c072c
+1,      23520,      23520,     4704,     9408, 0x7836015c
+0,      28224,      28224,     4704,      192, 0xc39d31e2
+1,      28224,      28224,     4704,     9408, 0x09485f97
+0,      32928,      32928,     4704,      192, 0xc39d31e2
+1,      32928,      32928,     4704,     9408, 0xb11434d7
+0,      37632,      37632,     4704,      192, 0xc39d31e2
+1,      37632,      37632,     4704,     9408, 0xf84f0a4c
+0,      42336,      42336,     4704,      192, 0xc39d31e2
+1,      42336,      42336,     4704,     9408, 0x6c83741d
+0,      47040,      47040,     4704,      192, 0xc39d31e2
+1,      47040,      47040,     4704,     9408, 0x104c524b
+0,      51744,      51744,     4704,      192, 0xc39d31e2
+1,      51744,      51744,     4704,     9408, 0x8fbe0d27
+0,      56448,      56448,     4704,      192, 0xc39d31e2
+1,      56448,      56448,     4704,     9408, 0x11717072
+0,      61152,      61152,     4704,      192, 0xc39d31e2
+1,      61152,      61152,     4704,     9408, 0xcd854ba5
+0,      65856,      65856,     4704,      192, 0xc39d31e2
+1,      65856,      65856,     4704,     9408, 0x80edfc2c
+0,      70560,      70560,     4704,      192, 0xc39d31e2
+1,      70560,      70560,     4704,     9408, 0x143e7e0c
+0,      75264,      75264,     4704,      192, 0xc39d31e2
+1,      75264,      75264,     4704,     9408, 0x965e36ba
+0,      79968,      79968,     4704,      192, 0xc39d31e2
+1,      79968,      79968,     4704,     9408, 0x1ed8f05c
+0,      84672,      84672,     4704,      192, 0xc39d31e2
+1,      84672,      84672,     4704,     9408, 0xb7907db9
+0,      89376,      89376,     4704,      192, 0xc39d31e2
+1,      89376,      89376,     4704,     9408, 0x8d883171
+0,      94080,      94080,     4704,      192, 0xc39d31e2
+1,      94080,      94080,     4704,     9408, 0x4d5013a1
+0,      98784,      98784,     4704,      192, 0xc39d31e2
+1,      98784,      98784,     4704,     9408, 0x4d5013a1
+0,     103488,     103488,     4704,      192, 0xc39d31e2
+1,     103488,     103488,     4704,     9408, 0xeb0c0750
+0,     108192,     108192,     4704,      192, 0xc39d31e2
+1,     108192,     108192,     4704,     9408, 0xa16990aa
+0,     112896,     112896,     4704,      192, 0xc39d31e2
+1,     112896,     112896,     4704,     9408, 0x139a4654
+0,     117600,     117600,     4704,      192, 0xc39d31e2
+1,     117600,     117600,     4704,     9408, 0x9e4d19e8
+0,     122304,     122304,     4704,      192, 0xc39d31e2
+1,     122304,     122304,     4704,     9408, 0x8e30783d
+0,     127008,     127008,     4704,      192, 0xc39d31e2
+1,     127008,     127008,     4704,     9408, 0x035839fa
+0,     131712,     131712,     4704,      192, 0xc39d31e2
+1,     131712,     131712,     4704,     9408, 0x9b7d1cf8
+0,     136416,     136416,     4704,      192, 0xc39d31e2
+1,     136416,     136416,     4704,     9408, 0x41a773bb
+0,     141120,     141120,     4704,      192, 0xc39d31e2
+1,     141120,     141120,     4704,     9408, 0xdd4d3117
+0,     145824,     145824,     4704,      192, 0xc39d31e2
+1,     145824,     145824,     4704,     9408, 0x7d84154c
+0,     150528,     150528,     4704,      192, 0xc39d31e2
+1,     150528,     150528,     4704,     9408, 0x2b846d43
+0,     155232,     155232,     4704,      192, 0xc39d31e2
+1,     155232,     155232,     4704,     9408, 0xf1762c01
+0,     159936,     159936,     4704,      192, 0xc39d31e2
+1,     159936,     159936,     4704,     9408, 0x48243395
+0,     164640,     164640,     4704,      192, 0xc39d31e2
+1,     164640,     164640,     4704,     9408, 0xeed08737
+0,     169344,     169344,     4704,    12318, 0xeabd2e91
+1,     169344,     169344,     4704,     9408, 0x154140f0
+0,     174048,     174048,     4704,    12255, 0xf400f505
+1,     174048,     174048,     4704,     9408, 0xf3fb25e0
+0,     178752,     178752,     4704,    12473, 0xbc140cbb
+1,     178752,     178752,     4704,     9408, 0x4d5013a1
+0,     183456,     183456,     4704,    12419, 0x86fb2e16
+1,     183456,     183456,     4704,     9408, 0x4d5013a1
+0,     188160,     188160,     4704,    12188, 0x03ac75f2
+0,     192864,     192864,     4704,    12423, 0xad7b5b02
+0,     197568,     197568,     4704,      192, 0xc39d31e2
+0,     202272,     202272,     4704,      192, 0xc39d31e2
+0,     206976,     206976,     4704,      192, 0xc39d31e2
+0,     211680,     211680,     4704,      192, 0xc39d31e2
+0,     216384,     216384,     4704,      192, 0xc39d31e2
+0,     221088,     221088,     4704,      192, 0xc39d31e2
+0,     225792,     225792,     4704,      192, 0xc39d31e2
+0,     230496,     230496,     4704,      192, 0xc39d31e2
+0,     235200,     235200,     4704,      192, 0xc39d31e2
+0,     239904,     239904,     4704,      192, 0xc39d31e2
+0,     244608,     244608,     4704,      192, 0xc39d31e2
+0,     249312,     249312,     4704,      192, 0xc39d31e2
+0,     254016,     254016,     4704,      192, 0xc39d31e2
+0,     258720,     258720,     4704,      192, 0xc39d31e2
+0,     263424,     263424,     4704,      192, 0xc39d31e2
+0,     268128,     268128,     4704,      192, 0xc39d31e2
+0,     272832,     272832,     4704,      192, 0xc39d31e2
+0,     277536,     277536,     4704,      192, 0xc39d31e2
+0,     282240,     282240,     4704,      192, 0xc39d31e2
+0,     286944,     286944,     4704,      192, 0xc39d31e2
+0,     291648,     291648,     4704,      192, 0xc39d31e2
+0,     296352,     296352,     4704,      192, 0xc39d31e2
+0,     301056,     301056,     4704,      192, 0xc39d31e2
+0,     305760,     305760,     4704,      192, 0xc39d31e2
+0,     310464,     310464,     4704,      192, 0xc39d31e2
+0,     315168,     315168,     4704,      192, 0xc39d31e2
+0,     319872,     319872,     4704,      192, 0xc39d31e2
diff --git a/tests/ref/fate/sacd-dst b/tests/ref/fate/sacd-dst
new file mode 100644
index 00000000000..9778073df00
--- /dev/null
+++ b/tests/ref/fate/sacd-dst
@@ -0,0 +1,74 @@
+#tb 0: 1/352800
+#media_type 0: audio
+#codec_id 0: dsd_msbf
+#sample_rate 0: 352800
+#channel_layout_name 0: 5.0(side)
+0,          0,          0,     4704,    23520, 0xf57a2c21
+0,       4704,       4704,     4704,    23520, 0x6c0b9566
+0,       9408,       9408,     4704,    23520, 0x25ec3431
+0,      14112,      14112,     4704,    23520, 0xd4724881
+0,      18816,      18816,     4704,    23520, 0x2d2daabf
+0,      23520,      23520,     4704,    23520, 0x78b9375b
+0,      28224,      28224,     4704,    23520, 0x9e77b10b
+0,      32928,      32928,     4704,    23520, 0x9e77b10b
+0,      37632,      37632,     4704,    23520, 0x9e77b10b
+0,      42336,      42336,     4704,    23520, 0x9e77b10b
+0,      47040,      47040,     4704,    23520, 0x9e77b10b
+0,      51744,      51744,     4704,    23520, 0x9e77b10b
+0,      56448,      56448,     4704,    23520, 0x9e77b10b
+0,      61152,      61152,     4704,    23520, 0x9e77b10b
+0,      65856,      65856,     4704,    23520, 0x9e77b10b
+0,      70560,      70560,     4704,    23520, 0x9e77b10b
+0,      75264,      75264,     4704,    23520, 0x9e77b10b
+0,      79968,      79968,     4704,    23520, 0x9e77b10b
+0,      84672,      84672,     4704,    23520, 0x9e77b10b
+0,      89376,      89376,     4704,    23520, 0x9e77b10b
+0,      94080,      94080,     4704,    23520, 0x9e77b10b
+0,      98784,      98784,     4704,    23520, 0x9e77b10b
+0,     103488,     103488,     4704,    23520, 0x9e77b10b
+0,     108192,     108192,     4704,    23520, 0x9e77b10b
+0,     112896,     112896,     4704,    23520, 0x9e77b10b
+0,     117600,     117600,     4704,    23520, 0x9e77b10b
+0,     122304,     122304,     4704,    23520, 0x9e77b10b
+0,     127008,     127008,     4704,    23520, 0x9e77b10b
+0,     131712,     131712,     4704,    23520, 0x9e77b10b
+0,     136416,     136416,     4704,    23520, 0x9e77b10b
+0,     141120,     141120,     4704,    23520, 0x9e77b10b
+0,     145824,     145824,     4704,    23520, 0x9e77b10b
+0,     150528,     150528,     4704,    23520, 0x9e77b10b
+0,     155232,     155232,     4704,    23520, 0x9e77b10b
+0,     159936,     159936,     4704,    23520, 0x9e77b10b
+0,     164640,     164640,     4704,    23520, 0x9e77b10b
+0,     169344,     169344,     4704,    23520, 0x21f92c1d
+0,     174048,     174048,     4704,    23520, 0xe8c7b22b
+0,     178752,     178752,     4704,    23520, 0x4c59147f
+0,     183456,     183456,     4704,    23520, 0x5a5325dd
+0,     188160,     188160,     4704,    23520, 0xb736d402
+0,     192864,     192864,     4704,    23520, 0xaf844006
+0,     197568,     197568,     4704,    23520, 0x9e77b10b
+0,     202272,     202272,     4704,    23520, 0x9e77b10b
+0,     206976,     206976,     4704,    23520, 0x9e77b10b
+0,     211680,     211680,     4704,    23520, 0x9e77b10b
+0,     216384,     216384,     4704,    23520, 0x9e77b10b
+0,     221088,     221088,     4704,    23520, 0x9e77b10b
+0,     225792,     225792,     4704,    23520, 0x9e77b10b
+0,     230496,     230496,     4704,    23520, 0x9e77b10b
+0,     235200,     235200,     4704,    23520, 0x9e77b10b
+0,     239904,     239904,     4704,    23520, 0x9e77b10b
+0,     244608,     244608,     4704,    23520, 0x9e77b10b
+0,     249312,     249312,     4704,    23520, 0x9e77b10b
+0,     254016,     254016,     4704,    23520, 0x9e77b10b
+0,     258720,     258720,     4704,    23520, 0x9e77b10b
+0,     263424,     263424,     4704,    23520, 0x9e77b10b
+0,     268128,     268128,     4704,    23520, 0x9e77b10b
+0,     272832,     272832,     4704,    23520, 0x9e77b10b
+0,     277536,     277536,     4704,    23520, 0x9e77b10b
+0,     282240,     282240,     4704,    23520, 0x9e77b10b
+0,     286944,     286944,     4704,    23520, 0x9e77b10b
+0,     291648,     291648,     4704,    23520, 0x9e77b10b
+0,     296352,     296352,     4704,    23520, 0x9e77b10b
+0,     301056,     301056,     4704,    23520, 0x9e77b10b
+0,     305760,     305760,     4704,    23520, 0x9e77b10b
+0,     310464,     310464,     4704,    23520, 0x9e77b10b
+0,     315168,     315168,     4704,    23520, 0x9e77b10b
+0,     319872,     319872,     4704,    23520, 0x9e77b10b
diff --git a/tests/ref/fate/sacd-toc b/tests/ref/fate/sacd-toc
new file mode 100644
index 00000000000..3f2abcfe340
--- /dev/null
+++ b/tests/ref/fate/sacd-toc
@@ -0,0 +1,68 @@
+[PROGRAM]
+program_id=1
+nb_streams=1
+TAG:title=Multi Channel
+TAG:description=MC area
+[STREAM]
+index=0
+codec_name=dst
+sample_rate=352800
+channel_layout=5.0(side)
+duration_ts=324576
+[/STREAM]
+[/PROGRAM]
+[PROGRAM]
+program_id=2
+nb_streams=1
+TAG:title=2-Channel Stereo
+TAG:description=2ch area
+TAG:copyright=(c) stereo
+[STREAM]
+index=1
+codec_name=dsd_msbf
+sample_rate=352800
+channel_layout=stereo
+duration_ts=188160
+[/STREAM]
+[/PROGRAM]
+[STREAM]
+index=0
+codec_name=dst
+sample_rate=352800
+channel_layout=5.0(side)
+duration_ts=324576
+[/STREAM]
+[STREAM]
+index=1
+codec_name=dsd_msbf
+sample_rate=352800
+channel_layout=stereo
+duration_ts=188160
+[/STREAM]
+[CHAPTER]
+id=1
+time_base=1/75
+start=0
+end=34
+TAG:title=Surround one
+[/CHAPTER]
+[CHAPTER]
+id=2
+time_base=1/75
+start=36
+end=69
+TAG:title=Surround two
+[/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]
diff --git a/tests/ref/seek/sacd b/tests/ref/seek/sacd
new file mode 100644
index 00000000000..f0d7c0aaaf6
--- /dev/null
+++ b/tests/ref/seek/sacd
@@ -0,0 +1,53 @@
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st:-1 flags:0  ts:-1.000000
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st:-1 flags:1  ts:-0.105833
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 0 flags:0  ts: 0.788333
+ret: 0         st: 1 flags:1 dts: 0.520000 pts: 0.520000 pos:1560576 size:  
9408
+ret: 0         st: 0 flags:1  ts:-0.317500
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 1 flags:0  ts: 0.576667
+ret: 0         st: 1 flags:1 dts: 0.520000 pts: 0.520000 pos:1560576 size:  
9408
+ret: 0         st: 1 flags:1  ts:-0.529164
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st:-1 flags:0  ts: 0.365002
+ret: 0         st: 0 flags:1 dts: 0.373333 pts: 0.373333 pos:1751040 size:   
192
+ret: 0         st:-1 flags:1  ts:-0.740831
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 0 flags:0  ts: 0.153336
+ret: 0         st: 0 flags:1 dts: 0.160000 pts: 0.160000 pos:1744896 size:   
192
+ret: 0         st: 0 flags:1  ts:-0.952497
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 1 flags:0  ts:-0.058330
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 1 flags:1  ts: 0.835836
+ret: 0         st: 1 flags:1 dts: 0.520000 pts: 0.520000 pos:1560576 size:  
9408
+ret: 0         st:-1 flags:0  ts:-0.269996
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st:-1 flags:1  ts: 0.624171
+ret: 0         st: 1 flags:1 dts: 0.520000 pts: 0.520000 pos:1560576 size:  
9408
+ret: 0         st: 0 flags:0  ts:-0.481661
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 0 flags:1  ts: 0.412506
+ret: 0         st: 0 flags:1 dts: 0.400000 pts: 0.400000 pos:1751040 size:   
192
+ret: 0         st: 1 flags:0  ts:-0.693328
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 1 flags:1  ts: 0.200839
+ret: 0         st: 0 flags:1 dts: 0.200000 pts: 0.200000 pos:1746944 size:   
192
+ret: 0         st:-1 flags:0  ts:-0.904994
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st:-1 flags:1  ts:-0.010827
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 0 flags:0  ts: 0.883339
+ret: 0         st: 1 flags:1 dts: 0.520000 pts: 0.520000 pos:1560576 size:  
9408
+ret: 0         st: 0 flags:1  ts:-0.222494
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st: 1 flags:0  ts: 0.671675
+ret: 0         st: 1 flags:1 dts: 0.520000 pts: 0.520000 pos:1560576 size:  
9408
+ret: 0         st: 1 flags:1  ts:-0.434158
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
+ret: 0         st:-1 flags:0  ts: 0.460008
+ret: 0         st: 0 flags:1 dts: 0.466667 pts: 0.466667 pos:1753088 size:   
192
+ret: 0         st:-1 flags:1  ts:-0.645825
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:1669120 size: 
12432
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to