FFmpeg
Loading...
Searching...
No Matches
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
24#include "avformat.h"
25#include "demux.h"
26#include "internal.h"
27#include "id3v2.h"
28
29typedef struct {
30 uint64_t data_end;
31 uint64_t audio_size;
32 uint64_t data_size;
34
35static int dsf_probe(const AVProbeData *p)
36{
37 if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
38 return 0;
39 return AVPROBE_SCORE_MAX;
40}
41
52
53static void read_id3(AVFormatContext *s, uint64_t id3pos)
54{
55 ID3v2ExtraMeta *id3v2_extra_meta;
56 if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
57 return;
58
59 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
60 if (id3v2_extra_meta) {
61 ff_id3v2_parse_apic(s, id3v2_extra_meta);
62 ff_id3v2_parse_chapters(s, id3v2_extra_meta);
63 }
64 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
65}
66
68{
69 DSFContext *dsf = s->priv_data;
70 AVIOContext *pb = s->pb;
71 AVStream *st;
72 uint64_t id3pos;
73 unsigned int channel_type;
74 int channels;
75
76 avio_skip(pb, 4);
77 if (avio_rl64(pb) != 28)
79
80 /* create primary stream before any id3 coverart streams */
82 if (!st)
83 return AVERROR(ENOMEM);
84
85 avio_skip(pb, 8);
86 id3pos = avio_rl64(pb);
88 read_id3(s, id3pos);
89 avio_seek(pb, 28, SEEK_SET);
90 }
91
92 /* fmt chunk */
93
94 if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
96
97 if (avio_rl32(pb) != 1) {
98 avpriv_request_sample(s, "unknown format version");
100 }
101
102 if (avio_rl32(pb)) {
103 avpriv_request_sample(s, "unknown format id");
104 return AVERROR_INVALIDDATA;
105 }
106
107 channel_type = avio_rl32(pb);
108 if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
109 st->codecpar->ch_layout = dsf_channel_layout[channel_type];
111 avpriv_request_sample(s, "channel type %i", channel_type);
112
114 channels = avio_rl32(pb);
115 if (!st->codecpar->ch_layout.nb_channels) {
117 } else if (channels != st->codecpar->ch_layout.nb_channels) {
118 av_log(s, AV_LOG_ERROR, "Channel count mismatch\n");
119 return AVERROR(EINVAL);
120 }
121 st->codecpar->sample_rate = avio_rl32(pb) / 8;
122
123 if (st->codecpar->ch_layout.nb_channels <= 0)
124 return AVERROR_INVALIDDATA;
125
126 switch(avio_rl32(pb)) {
127 case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
128 case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
129 default:
130 avpriv_request_sample(s, "unknown most significant bit");
131 return AVERROR_INVALIDDATA;
132 }
133
134 dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->ch_layout.nb_channels;
135 st->codecpar->block_align = avio_rl32(pb);
136 if (st->codecpar->block_align > INT_MAX / st->codecpar->ch_layout.nb_channels ||
137 st->codecpar->block_align <= 0) {
138 avpriv_request_sample(s, "block_align invalid");
139 return AVERROR_INVALIDDATA;
140 }
144 avio_skip(pb, 4);
145
146 /* data chunk */
147
148 dsf->data_end = avio_tell(pb);
149 if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
150 return AVERROR_INVALIDDATA;
151 dsf->data_size = avio_rl64(pb) - 12;
152 dsf->data_end += dsf->data_size + 12;
153
154 return 0;
155}
156
158{
159 FFFormatContext *const si = ffformatcontext(s);
160 DSFContext *dsf = s->priv_data;
161 AVIOContext *pb = s->pb;
162 AVStream *st = s->streams[0];
163 int64_t pos = avio_tell(pb);
165 int ret;
166
167 if (pos >= dsf->data_end)
168 return AVERROR_EOF;
169
170 if (dsf->data_size > dsf->audio_size) {
171 int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
172
173 if (last_packet) {
174 int64_t data_pos = pos - si->data_offset;
175 int64_t packet_size = dsf->audio_size - data_pos;
176 int64_t skip_size = dsf->data_size - data_pos - packet_size;
177 uint8_t *dst;
178 int ch;
179
180 if (packet_size <= 0 || skip_size <= 0)
181 return AVERROR_INVALIDDATA;
182
183 if ((ret = av_new_packet(pkt, packet_size)) < 0)
184 return ret;
185 dst = pkt->data;
186 for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
187 ret = avio_read(pb, dst, packet_size / st->codecpar->ch_layout.nb_channels);
188 if (ret < packet_size / st->codecpar->ch_layout.nb_channels)
189 return AVERROR_EOF;
190
191 dst += ret;
192 avio_skip(pb, skip_size / st->codecpar->ch_layout.nb_channels);
193 }
194
195 pkt->pos = pos;
196 pkt->stream_index = 0;
197 pkt->pts = (pos - si->data_offset) / channels;
198 pkt->duration = packet_size / channels;
199 return 0;
200 }
201 }
202 ret = av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
203 if (ret < 0)
204 return ret;
205
206 pkt->stream_index = 0;
207 pkt->pts = (pos - si->data_offset) / channels;
208 pkt->duration = st->codecpar->block_align / channels;
209
210 return 0;
211}
212
214 .p.name = "dsf",
215 .p.long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
217 .priv_data_size = sizeof(DSFContext),
221};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFInputFormat ff_dsf_demuxer
Definition dsfdec.c:213
channels
Definition aptx.h:31
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:506
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
uint64_t avio_rl64(AVIOContext *s)
Definition aviobuf.c:741
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int dsf_probe(const AVProbeData *p)
Definition dsfdec.c:35
static const AVChannelLayout dsf_channel_layout[]
Definition dsfdec.c:42
static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dsfdec.c:157
static int dsf_read_header(AVFormatContext *s)
Definition dsfdec.c:67
static void read_id3(AVFormatContext *s, uint64_t id3pos)
Definition dsfdec.c:53
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_DSD_MSBF_PLANAR
Definition codec_id.h:529
@ AV_CODEC_ID_DSD_LSBF_PLANAR
Definition codec_id.h:528
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_QUAD
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
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:1180
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition id3v2.c:1186
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:1202
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition id3v2.c:1233
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
#define AV_RL64(p)
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
uint64_t data_size
Definition dsfdec.c:32
uint64_t audio_size
Definition dsfdec.c:31
uint64_t data_end
Definition dsfdec.c:30
int64_t data_offset
offset of the first packet
Definition internal.h:89
#define avpriv_request_sample(...)
#define av_log(a,...)