71#define SECTOR_SIZE 2048
74#define RAW_SECTOR_SIZE 2064
75#define RAW_SECTOR_HEADER 12
77#define MASTER_TOC_LSN 510
79#define FRAMES_PER_SECOND 75
80#define BITS_PER_SAMPLE 8
82#define SAMPLE_RATE (64 * 44100 / BITS_PER_SAMPLE)
83#define FRAME_SAMPLES (SAMPLE_RATE / FRAMES_PER_SECOND)
85#define MAX_FRAME_SECTORS 16
86#define MAX_PACKET_SIZE (SECTOR_SIZE - 3)
87#define MAX_FRAME_SIZE (MAX_FRAME_SECTORS * MAX_PACKET_SIZE)
90#define MAX_TRACK_TEXT_SECTORS 32
97 { 0x48,
"MULCHTOC",
"Multi Channel" },
98 { 0x40,
"TWOCHTOC",
"2-Channel Stereo" },
107#define DATA_TYPE_AUDIO 2
145 if (p->buf_size >=
offsets[
i] + 8 &&
146 !memcmp(p->buf +
offsets[
i],
"SACDMTOC", 8))
155 for (
int i = 0;
i < nb;
i++) {
177 [
'A'] = 1, [
'B'] = 2, [
'C'] = 3, [
'D'] = 4, [
'L'] = 5, [
'G'] = 6,
178 [
'F'] = 7, [
'H'] = 8, [
'M'] = 9, [
'V'] = 10, [
'T'] = 11, [
'Y'] = 13,
179 [
'_'] = 14, [
'b'] = 15, [
'f'] = 16,
185 for (
int i = 0;
i <
len;
i++) {
194 snprintf(
name,
sizeof(
name),
"ISO-8859-%d", part);
195 iconv_t cd = iconv_open(
"UTF-8",
name);
196 if (cd == (iconv_t)-1)
199 char *in = (
char *)str;
200 size_t in_size =
len;
205 if (iconv(cd, &in, &in_size, &
out, &
out_size) == (
size_t)-1 &&
221 const uint8_t *str,
int max_len,
int flags)
225 int len = 0, part = 1;
227 for (
int i = 0;
i < max_len && str[
i];
i++)
237 if (
len > 2 && str[1] ==
'-' &&
247 const uint8_t *esc = memchr(str,
ESC,
len);
248 n = esc ? esc - str :
len;
251 "Cannot convert text from ISO 8859-%d\n", part);
273 [ 2] =
"Adult Contemporary",
274 [ 3] =
"Alternative Rock",
275 [ 4] =
"Children's Music",
277 [ 6] =
"Contemporary Christian",
280 [ 9] =
"Easy Listening",
295 [24] =
"Rhythm & Blues",
296 [25] =
"Sound Effects",
297 [26] =
"Sound Track",
298 [27] =
"Spoken Word",
299 [28] =
"World Music",
305 enum { GENERAL_GENRE_TABLE = 1 };
317 CHARSET_ISO8859_1 = 2,
318 CHARSET_ISO8859_1_ESC = 7,
320 int charset = text_channels[8 + 2];
322 if (!text_channels[0])
324 if (charset == CHARSET_ISO646 || charset == CHARSET_ISO8859_1 ||
325 charset == CHARSET_ISO8859_1_ESC)
335 static const struct {
341 { 0x24,
"publisher" },
342 { 0x26,
"copyright" },
344 { 0x12,
"album_artist" },
345 { 0x14,
"publisher" },
346 { 0x16,
"copyright" },
356 if (memcmp(buf,
"SACDMTOC", 8)) {
361 if (memcmp(buf,
"SACDMTOC", 8))
369 snprintf(str,
sizeof(str),
"%d/%d",
AV_RB16(buf + 0x12),
373 set_string(
s, &
s->metadata,
"album_catalog_number", buf + 0x18, 16, 0);
374 set_string(
s, &
s->metadata,
"catalog_number", buf + 0x58, 16, 0);
375 for (
int i = 0;
i < 4;
i++)
377 if (
AV_RB16(buf + 0x78) && buf[0x7a] && buf[0x7b]) {
378 snprintf(str,
sizeof(str),
"%04d-%02d-%02d",
AV_RB16(buf + 0x78),
379 buf[0x7a], buf[0x7b]);
388 if (memcmp(buf,
"SACDText", 8))
391 set_text(
s, &
s->metadata, texts[
i].key, buf,
sizeof(buf),
398 const uint8_t *buf,
int size,
int track)
400 static const char *
const keys[] = {
417 for (
int i = 0;
i < nb_items &&
pos + 2 <
size;
i++) {
429 int nb_entries, nb_sectors,
ret;
438 a->access_step = buf[10];
439 if (memcmp(buf,
"SACD_ACC", 8) || !
a->access_step ||
440 nb_entries != (
a->nb_frames - 1) /
a->access_step + 1) {
451 memmove(buf, buf + 16, 5 * nb_entries);
452 a->access_list = buf;
468 if (
frame ==
a->nb_frames)
472 lsn +=
frame / 3 * 14;
474 lsn +=
frame / 3 * 16;
475 }
else if (
a->access_list) {
480 const uint8_t *
entry =
a->access_list + 5 * (
frame /
a->access_step);
485 if (
frame /
a->access_step < (
a->nb_frames - 1) /
a->access_step) {
490 nb = (
a->nb_frames - 1) %
a->access_step + 1;
493 if (!(
flags & 0x8000) && next > start)
494 lsn =
FFMAX(start, start +
frame %
a->access_step *
495 (next - start) / nb - (
flags & 0x7fff));
496 lsn =
FFMAX(lsn,
a->start);
503 a->nb_packets =
a->packet = 0;
516 uint8_t *track_text =
NULL;
517 int toc_size, nb_channels, nb_tracks, has_text;
518 int track_text_pos, track_text_size = 0, access_list_pos;
545 toc_size =
AV_RB16(buf + 0x0a);
546 a->frame_format = buf[0x15] & 0xf;
547 nb_channels = buf[0x20];
551 nb_tracks =
s->nb_streams > 1 ? 0 : buf[0x45];
555 track_text_pos =
AV_RB16(buf + 0x80);
556 access_list_pos =
AV_RB16(buf + 0x84);
558 if (buf[0x14] != 4) {
569 !
layouts[nb_channels].nb_channels ||
583 if (memcmp(tracks,
"SACDTRL2", 8))
588 if (
a->first_frame < 0 ||
a->first_frame >=
a->nb_frames)
595 if (memcmp(buf,
"SACD_IGL", 8))
600 if (nb_tracks && other_toc_lsn &&
610 if (nb_tracks && has_text && track_text_pos && track_text_pos < toc_size) {
611 track_text_size =
FFMIN(toc_size - track_text_pos,
620 if (memcmp(track_text,
"SACDTTxt", 8))
624 for (
int i = 0;
i < nb_tracks;
i++) {
629 if (start < a->first_frame || length < 0)
633 start -
a->first_frame,
634 start + length -
a->first_frame,
NULL);
643 if (!other_toc_lsn ||
644 !memcmp(buf + 8 + 12 *
i, other_isrc + 8 + 12 *
i, 12))
700 const uint8_t *buf =
a->sector;
702 int nb_packets = buf[0] >> 5;
703 int nb_starts = buf[0] >> 2 & 7;
704 int size = 1 + 2 * nb_packets + (
dst ? 4 : 3) * nb_starts;
706 if (!nb_packets || (buf[0] & 1) !=
dst)
709 a->info_pos = 1 + 2 * nb_packets;
711 for (
int i = 0;
i < nb_packets;
i++) {
712 int info =
AV_RB16(buf + 1 + 2 *
i);
715 nb_starts -= info >> 15;
716 size += info & 0x7ff;
721 a->nb_packets = nb_packets;
741 if (
a->packet ==
a->nb_packets) {
750 "%"PRId64
"\n",
a->lsn - 1);
758 info =
AV_RB16(
a->sector + 1 + 2 *
a->packet);
759 length = info & 0x7ff;
771 pos = (
a->lsn - 1) *
c->sector_size +
c->sector_offset;
779 "large\n",
a->lsn - 1);
782 memcpy(
c->frame +
size,
a->sector +
a->data_pos, length);
787 a->data_pos += length;
803 c->pos =
a->frame -
a->first_frame;
817 for (
unsigned i = 0;
i <
s->nb_streams;
i++) {
830 if (
b->frame <
b->nb_frames &&
831 (!
a ||
b->frame -
b->first_frame <
a->frame -
a->first_frame)) {
843 a->frame =
a->nb_frames;
852 for (
unsigned i = 0;
i <
s->nb_streams;
i++) {
863 const SACDArea *
a =
s->streams[stream_index]->priv_data;
864 c->pos =
FFMIN(
c->pos,
a->nb_frames -
a->first_frame - 1);
866 for (
unsigned i = 0;
i <
s->nb_streams;
i++)
873 for (
unsigned i = 0;
i <
s->nb_streams;
i++) {
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_UNLIMITED
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int read_probe(const AVProbeData *p)
Public libavutil channel layout APIs header.
common internal and external API header
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
static int read_header(FFV1Context *f, RangeCoder *c)
static double area(double x, const double *params)
Overlap between a destination pixel (unit box) and a source pixel of width params[0] <= 1,...
@ AVDISCARD_ALL
discard all
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
AVProgram * av_new_program(AVFormatContext *ac, int id)
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_5POINT1
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AVERROR_EOF
End of file.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
@ AV_SAMPLE_FMT_DSD
DSD (Direct Stream Digital) bitstream, interleaved.
size_t static size_t av_strnlen(const char *s, size_t len)
Get the count of continuous non zero chars starting from the beginning.
static const int offsets[]
static const char signature[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_cold int read_close(AVFormatContext *ctx)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Memory handling functions.
enum MovChannelLayoutTag * layouts
static int sacd_read_packet(AVFormatContext *s, AVPacket *pkt)
static void seek_frame(AVFormatContext *s, AVStream *st, int64_t pos)
#define MAX_TRACK_TEXT_SECTORS
static const struct @314340375322203031114317056164173260073220227203 areas[]
static int time_code(const uint8_t *tc)
static const char *const genres[]
static int read_area_toc(AVFormatContext *s, int area, int64_t toc_lsn, int64_t other_toc_lsn)
static void read_track_text(AVFormatContext *s, AVDictionary **m, const uint8_t *buf, int size, int track)
static void set_string(AVFormatContext *s, AVDictionary **m, const char *key, const uint8_t *str, int max_len, int flags)
static int sacd_read_header(AVFormatContext *s)
static int text_supported(AVFormatContext *s, const uint8_t *text_channels)
static int sacd_probe(const AVProbeData *p)
#define FRAMES_PER_SECOND
static int sacd_read_close(AVFormatContext *s)
static const uint8_t iso8859_parts[]
static int read_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
static int read_master_toc(AVFormatContext *s, uint32_t *toc_lsn)
#define RAW_SECTOR_HEADER
static int put_iso8859(AVBPrint *bp, int part, const uint8_t *str, int len)
static int sacd_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
static int read_sectors(AVFormatContext *s, int64_t lsn, uint8_t *buf, int nb)
static void set_text(AVFormatContext *s, AVDictionary **m, const char *key, const uint8_t *buf, int size, int offset, int flags)
static void set_genre(AVDictionary **m, const uint8_t *genre)
static int read_audio_header(SACDArea *a)
@ FRAME_FORMAT_DSD_3_IN_16
@ FRAME_FORMAT_DSD_3_IN_14
static int read_access_list(AVFormatContext *s, SACDArea *a, int64_t lsn)
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
AVChannelLayout ch_layout
The channel layout and number of channels.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
enum AVMediaType codec_type
General type of the encoded data.
int block_align
The number of bytes per coded audio frame, required by some formats.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int sample_rate
The number of audio samples per second.
This structure stores compressed data.
This structure contains the data a format has to probe a file.
New fields can be added to the end with minor version bumps.
Rational number (pair of numerator and denominator).
AVCodecParameters * codecpar
Codec parameters associated with this stream.
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
int64_t duration
Decoding: duration of the stream, in stream time base.
int index
stream index in AVFormatContext
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
uint8_t sector[SECTOR_SIZE]
uint8_t frame[MAX_FRAME_SIZE]
#define avpriv_request_sample(...)