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 
22 #include "libavutil/channel_layout.h"
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 
dsf_probe(const AVProbeData *p)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 
41 static const AVChannelLayout dsf_channel_layout[] = {
42     { .order = AV_CHANNEL_ORDER_UNSPEC },
43     AV_CHANNEL_LAYOUT_MONO,
44     AV_CHANNEL_LAYOUT_STEREO,
45     AV_CHANNEL_LAYOUT_SURROUND,
46     AV_CHANNEL_LAYOUT_QUAD,
47     AV_CHANNEL_LAYOUT_4POINT0,
48     AV_CHANNEL_LAYOUT_5POINT0_BACK,
49     AV_CHANNEL_LAYOUT_5POINT1_BACK,
50 };
51 
read_id3(AVFormatContext *s, uint64_t id3pos)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 
dsf_read_header(AVFormatContext *s)66 static int dsf_read_header(AVFormatContext *s)
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 
112     st->codecpar->codec_type   = AVMEDIA_TYPE_AUDIO;
113     channels = avio_rl32(pb);
114     if (!st->codecpar->ch_layout.nb_channels) {
115         st->codecpar->ch_layout.nb_channels = 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     }
140     st->codecpar->block_align *= st->codecpar->ch_layout.nb_channels;
141     st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * 8LL * st->codecpar->sample_rate;
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 
dsf_read_packet(AVFormatContext *s, AVPacket *pkt)156 static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
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);
163     int channels = st->codecpar->ch_layout.nb_channels;
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;
207     pkt->duration = st->codecpar->block_align / channels;
208 
209     return 0;
210 }
211 
212 const AVInputFormat ff_dsf_demuxer = {
213     .name           = "dsf",
214     .long_name      = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
215     .priv_data_size = sizeof(DSFContext),
216     .read_probe     = dsf_probe,
217     .read_header    = dsf_read_header,
218     .read_packet    = dsf_read_packet,
219     .flags          = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
220 };
221