2 * Digital Speech Standard (DSS) demuxer
3 * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/attributes.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/intreadwrite.h"
30 #define DSS_HEAD_OFFSET_AUTHOR 0xc
31 #define DSS_AUTHOR_SIZE 16
33 #define DSS_HEAD_OFFSET_START_TIME 0x26
34 #define DSS_HEAD_OFFSET_END_TIME 0x32
35 #define DSS_TIME_SIZE 12
37 #define DSS_HEAD_OFFSET_ACODEC 0x2a4
38 #define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
39 #define DSS_ACODEC_G723_1 0x2 /* LP mode */
41 #define DSS_HEAD_OFFSET_COMMENT 0x31e
42 #define DSS_COMMENT_SIZE 64
44 #define DSS_BLOCK_SIZE 512
45 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
46 #define DSS_FRAME_SIZE 42
48 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
50 typedef struct DSSDemuxContext {
51 unsigned int audio_codec;
61 static int dss_probe(AVProbeData *p)
63 if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
64 && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
67 return AVPROBE_SCORE_MAX;
70 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
73 AVIOContext *pb = s->pb;
74 char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
75 int y, month, d, h, minute, sec;
78 avio_seek(pb, offset, SEEK_SET);
80 ret = avio_read(s->pb, string, DSS_TIME_SIZE);
81 if (ret < DSS_TIME_SIZE)
82 return ret < 0 ? ret : AVERROR_EOF;
84 if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
85 return AVERROR_INVALIDDATA;
86 /* We deal with a two-digit year here, so set the default date to 2000
87 * and hope it will never be used in the next century. */
88 snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
89 y + 2000, month, d, h, minute, sec);
90 return av_dict_set(&s->metadata, key, datetime, 0);
93 static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
94 unsigned int size, const char *key)
96 AVIOContext *pb = s->pb;
100 avio_seek(pb, offset, SEEK_SET);
102 value = av_mallocz(size + 1);
104 return AVERROR(ENOMEM);
106 ret = avio_read(s->pb, value, size);
108 ret = ret < 0 ? ret : AVERROR_EOF;
112 ret = av_dict_set(&s->metadata, key, value, 0);
119 static int dss_read_header(AVFormatContext *s)
121 DSSDemuxContext *ctx = s->priv_data;
122 AVIOContext *pb = s->pb;
126 st = avformat_new_stream(s, NULL);
128 return AVERROR(ENOMEM);
130 version = avio_r8(pb);
131 ctx->dss_header_size = version * DSS_BLOCK_SIZE;
133 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
134 DSS_AUTHOR_SIZE, "author");
138 ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
142 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
143 DSS_COMMENT_SIZE, "comment");
147 avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
148 ctx->audio_codec = avio_r8(pb);
150 if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
151 st->codecpar->codec_id = AV_CODEC_ID_DSS_SP;
152 st->codecpar->sample_rate = 11025;
153 } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
154 st->codecpar->codec_id = AV_CODEC_ID_G723_1;
155 st->codecpar->sample_rate = 8000;
157 avpriv_request_sample(s, "Support for codec %x in DSS",
159 return AVERROR_PATCHWELCOME;
162 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
163 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
164 st->codecpar->channels = 1;
166 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
169 /* Jump over header */
171 if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
177 ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
178 if (!ctx->dss_sp_buf)
179 return AVERROR(ENOMEM);
184 static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
186 DSSDemuxContext *ctx = s->priv_data;
187 AVIOContext *pb = s->pb;
189 avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
190 ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
193 static void dss_sp_byte_swap(DSSDemuxContext *ctx,
194 uint8_t *dst, const uint8_t *src)
199 for (i = 3; i < DSS_FRAME_SIZE; i += 2)
202 for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
205 dst[1] = ctx->dss_sp_swap_byte;
207 memcpy(dst, src, DSS_FRAME_SIZE);
208 ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
211 /* make sure byte 40 is always 0 */
212 dst[DSS_FRAME_SIZE - 2] = 0;
216 static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
218 DSSDemuxContext *ctx = s->priv_data;
219 AVStream *st = s->streams[0];
220 int read_size, ret, offset = 0, buff_offset = 0;
221 int64_t pos = avio_tell(s->pb);
223 if (ctx->counter == 0)
224 dss_skip_audio_header(s, pkt);
227 read_size = DSS_FRAME_SIZE - 2;
230 read_size = DSS_FRAME_SIZE;
232 ctx->counter -= read_size;
233 ctx->packet_size = DSS_FRAME_SIZE - 1;
235 ret = av_new_packet(pkt, DSS_FRAME_SIZE);
241 pkt->stream_index = 0;
242 s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
244 if (ctx->counter < 0) {
245 int size2 = ctx->counter + read_size;
247 ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
249 if (ret < size2 - offset)
252 dss_skip_audio_header(s, pkt);
256 ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
258 if (ret < read_size - offset)
261 dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
263 if (ctx->dss_sp_swap_byte < 0) {
264 ret = AVERROR(EAGAIN);
271 av_packet_unref(pkt);
272 return ret < 0 ? ret : AVERROR_EOF;
275 static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
277 DSSDemuxContext *ctx = s->priv_data;
278 AVStream *st = s->streams[0];
279 int size, byte, ret, offset;
280 int64_t pos = avio_tell(s->pb);
282 if (ctx->counter == 0)
283 dss_skip_audio_header(s, pkt);
285 /* We make one byte-step here. Don't forget to add offset. */
286 byte = avio_r8(s->pb);
288 return AVERROR_INVALIDDATA;
290 size = frame_size[byte & 3];
292 ctx->packet_size = size;
293 ctx->counter -= size;
295 ret = av_new_packet(pkt, size);
303 s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
305 pkt->stream_index = 0;
307 if (ctx->counter < 0) {
308 int size2 = ctx->counter + size;
310 ret = avio_read(s->pb, pkt->data + offset,
312 if (ret < size2 - offset) {
313 av_packet_unref(pkt);
314 return ret < 0 ? ret : AVERROR_EOF;
317 dss_skip_audio_header(s, pkt);
321 ret = avio_read(s->pb, pkt->data + offset, size - offset);
322 if (ret < size - offset) {
323 av_packet_unref(pkt);
324 return ret < 0 ? ret : AVERROR_EOF;
330 static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
332 DSSDemuxContext *ctx = s->priv_data;
334 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
335 return dss_sp_read_packet(s, pkt);
337 return dss_723_1_read_packet(s, pkt);
340 static int dss_read_close(AVFormatContext *s)
342 DSSDemuxContext *ctx = s->priv_data;
344 av_freep(&ctx->dss_sp_buf);
349 static int dss_read_seek(AVFormatContext *s, int stream_index,
350 int64_t timestamp, int flags)
352 DSSDemuxContext *ctx = s->priv_data;
354 uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
357 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
358 seekto = timestamp / 264 * 41 / 506 * 512;
360 seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
365 seekto += ctx->dss_header_size;
367 ret = avio_seek(s->pb, seekto, SEEK_SET);
371 avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
372 ctx->swap = !!(header[0] & 0x80);
373 offset = 2*header[1] + 2*ctx->swap;
374 if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
375 return AVERROR_INVALIDDATA;
376 if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
378 offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
380 ctx->counter = DSS_BLOCK_SIZE - offset;
381 offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
383 ctx->dss_sp_swap_byte = -1;
388 AVInputFormat ff_dss_demuxer = {
390 .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
391 .priv_data_size = sizeof(DSSDemuxContext),
392 .read_probe = dss_probe,
393 .read_header = dss_read_header,
394 .read_packet = dss_read_packet,
395 .read_close = dss_read_close,
396 .read_seek = dss_read_seek,