FFmpeg
dsfdec.c
Go to the documentation of this file.
1 /*
2  * DSD Stream File (DSF) demuxer
3  * Copyright (c) 2014 Peter Ross
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "id3v2.h"
27 
28 typedef struct {
29  uint64_t data_end;
30  uint64_t audio_size;
31  uint64_t data_size;
32 } DSFContext;
33 
34 static int dsf_probe(const AVProbeData *p)
35 {
36  if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
37  return 0;
38  return AVPROBE_SCORE_MAX;
39 }
40 
50 };
51 
52 static void read_id3(AVFormatContext *s, uint64_t id3pos)
53 {
54  ID3v2ExtraMeta *id3v2_extra_meta;
55  if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
56  return;
57 
58  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
59  if (id3v2_extra_meta) {
60  ff_id3v2_parse_apic(s, id3v2_extra_meta);
61  ff_id3v2_parse_chapters(s, id3v2_extra_meta);
62  }
63  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
64 }
65 
67 {
68  DSFContext *dsf = s->priv_data;
69  AVIOContext *pb = s->pb;
70  AVStream *st;
71  uint64_t id3pos;
72  unsigned int channel_type;
73  int channels;
74 
75  avio_skip(pb, 4);
76  if (avio_rl64(pb) != 28)
77  return AVERROR_INVALIDDATA;
78 
79  /* create primary stream before any id3 coverart streams */
80  st = avformat_new_stream(s, NULL);
81  if (!st)
82  return AVERROR(ENOMEM);
83 
84  avio_skip(pb, 8);
85  id3pos = avio_rl64(pb);
86  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
87  read_id3(s, id3pos);
88  avio_seek(pb, 28, SEEK_SET);
89  }
90 
91  /* fmt chunk */
92 
93  if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
94  return AVERROR_INVALIDDATA;
95 
96  if (avio_rl32(pb) != 1) {
97  avpriv_request_sample(s, "unknown format version");
98  return AVERROR_INVALIDDATA;
99  }
100 
101  if (avio_rl32(pb)) {
102  avpriv_request_sample(s, "unknown format id");
103  return AVERROR_INVALIDDATA;
104  }
105 
106  channel_type = avio_rl32(pb);
107  if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
108  st->codecpar->ch_layout = dsf_channel_layout[channel_type];
109  if (!st->codecpar->ch_layout.nb_channels)
110  avpriv_request_sample(s, "channel type %i", channel_type);
111 
113  channels = avio_rl32(pb);
114  if (!st->codecpar->ch_layout.nb_channels) {
116  } else if (channels != st->codecpar->ch_layout.nb_channels) {
117  av_log(s, AV_LOG_ERROR, "Channel count mismatch\n");
118  return AVERROR(EINVAL);
119  }
120  st->codecpar->sample_rate = avio_rl32(pb) / 8;
121 
122  if (st->codecpar->ch_layout.nb_channels <= 0)
123  return AVERROR_INVALIDDATA;
124 
125  switch(avio_rl32(pb)) {
126  case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
127  case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
128  default:
129  avpriv_request_sample(s, "unknown most significant bit");
130  return AVERROR_INVALIDDATA;
131  }
132 
133  dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->ch_layout.nb_channels;
134  st->codecpar->block_align = avio_rl32(pb);
135  if (st->codecpar->block_align > INT_MAX / st->codecpar->ch_layout.nb_channels ||
136  st->codecpar->block_align <= 0) {
137  avpriv_request_sample(s, "block_align invalid");
138  return AVERROR_INVALIDDATA;
139  }
142  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
143  avio_skip(pb, 4);
144 
145  /* data chunk */
146 
147  dsf->data_end = avio_tell(pb);
148  if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
149  return AVERROR_INVALIDDATA;
150  dsf->data_size = avio_rl64(pb) - 12;
151  dsf->data_end += dsf->data_size + 12;
152 
153  return 0;
154 }
155 
157 {
158  FFFormatContext *const si = ffformatcontext(s);
159  DSFContext *dsf = s->priv_data;
160  AVIOContext *pb = s->pb;
161  AVStream *st = s->streams[0];
162  int64_t pos = avio_tell(pb);
164  int ret;
165 
166  if (pos >= dsf->data_end)
167  return AVERROR_EOF;
168 
169  if (dsf->data_size > dsf->audio_size) {
170  int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
171 
172  if (last_packet) {
173  int64_t data_pos = pos - si->data_offset;
174  int64_t packet_size = dsf->audio_size - data_pos;
175  int64_t skip_size = dsf->data_size - data_pos - packet_size;
176  uint8_t *dst;
177  int ch, ret;
178 
179  if (packet_size <= 0 || skip_size <= 0)
180  return AVERROR_INVALIDDATA;
181 
182  if ((ret = av_new_packet(pkt, packet_size)) < 0)
183  return ret;
184  dst = pkt->data;
185  for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
186  ret = avio_read(pb, dst, packet_size / st->codecpar->ch_layout.nb_channels);
187  if (ret < packet_size / st->codecpar->ch_layout.nb_channels)
188  return AVERROR_EOF;
189 
190  dst += ret;
191  avio_skip(pb, skip_size / st->codecpar->ch_layout.nb_channels);
192  }
193 
194  pkt->pos = pos;
195  pkt->stream_index = 0;
196  pkt->pts = (pos - si->data_offset) / channels;
197  pkt->duration = packet_size / channels;
198  return 0;
199  }
200  }
201  ret = av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
202  if (ret < 0)
203  return ret;
204 
205  pkt->stream_index = 0;
206  pkt->pts = (pos - si->data_offset) / channels;
208 
209  return 0;
210 }
211 
213  .name = "dsf",
214  .long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
215  .priv_data_size = sizeof(DSFContext),
220 };
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:487
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:243
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:369
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:191
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
id3v2.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
dsf_channel_layout
static const AVChannelLayout dsf_channel_layout[]
Definition: dsfdec.c:41
ff_id3v2_read
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:1136
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:306
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
AV_CODEC_ID_DSD_MSBF_PLANAR
@ AV_CODEC_ID_DSD_MSBF_PLANAR
Definition: codec_id.h:514
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:771
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
DSFContext::audio_size
uint64_t audio_size
Definition: dsfdec.c:30
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:372
ff_id3v2_parse_chapters
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1189
DSFContext::data_size
uint64_t data_size
Definition: dsfdec.c:31
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:546
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ID3v2ExtraMeta
Definition: id3v2.h:84
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:374
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:97
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:551
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:112
AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:380
dsf_read_packet
static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dsfdec.c:156
FFFormatContext::data_offset
int64_t data_offset
offset of the first packet
Definition: internal.h:108
channels
channels
Definition: aptx.h:31
FFFormatContext
Definition: internal.h:71
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
NULL
#define NULL
Definition: coverity.c:32
ff_id3v2_parse_apic
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition: id3v2.c:1158
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:751
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
read_id3
static void read_id3(AVFormatContext *s, uint64_t id3pos)
Definition: dsfdec.c:52
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:267
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
DSFContext
Definition: dsfdec.c:28
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_QUAD
Definition: channel_layout.h:377
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:185
AV_CODEC_ID_DSD_LSBF_PLANAR
@ AV_CODEC_ID_DSD_LSBF_PLANAR
Definition: codec_id.h:513
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:102
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
ff_dsf_demuxer
const AVInputFormat ff_dsf_demuxer
Definition: dsfdec.c:212
channel_layout.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
DSFContext::data_end
uint64_t data_end
Definition: dsfdec.c:29
dsf_read_header
static int dsf_read_header(AVFormatContext *s)
Definition: dsfdec.c:66
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
AVPacket::stream_index
int stream_index
Definition: packet.h:376
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:339
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
dsf_probe
static int dsf_probe(const AVProbeData *p)
Definition: dsfdec.c:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
ff_id3v2_free_extra_meta
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1142
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:394
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:759
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:381
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:91
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55