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/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
28 #define DSS_HEAD_OFFSET_AUTHOR 0xc
29 #define DSS_AUTHOR_SIZE 16
31 #define DSS_HEAD_OFFSET_START_TIME 0x26
32 #define DSS_HEAD_OFFSET_END_TIME 0x32
33 #define DSS_TIME_SIZE 12
35 #define DSS_HEAD_OFFSET_ACODEC 0x2a4
36 #define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
37 #define DSS_ACODEC_G723_1 0x2 /* LP mode */
39 #define DSS_HEAD_OFFSET_COMMENT 0x31e
40 #define DSS_COMMENT_SIZE 64
42 #define DSS_BLOCK_SIZE 512
43 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
44 #define DSS_FRAME_SIZE 42
46 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
48 typedef struct DSSDemuxContext {
49 unsigned int audio_codec;
59 static int dss_probe(const AVProbeData *p)
61 if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
62 && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
65 return AVPROBE_SCORE_MAX;
68 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
71 AVIOContext *pb = s->pb;
72 char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
73 int y, month, d, h, minute, sec;
76 avio_seek(pb, offset, SEEK_SET);
78 ret = avio_read(s->pb, string, DSS_TIME_SIZE);
79 if (ret < DSS_TIME_SIZE)
80 return ret < 0 ? ret : AVERROR_EOF;
82 if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
83 return AVERROR_INVALIDDATA;
84 /* We deal with a two-digit year here, so set the default date to 2000
85 * and hope it will never be used in the next century. */
86 snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
87 y + 2000, month, d, h, minute, sec);
88 return av_dict_set(&s->metadata, key, datetime, 0);
91 static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
92 unsigned int size, const char *key)
94 AVIOContext *pb = s->pb;
98 avio_seek(pb, offset, SEEK_SET);
100 value = av_mallocz(size + 1);
102 return AVERROR(ENOMEM);
104 ret = avio_read(s->pb, value, size);
107 return ret < 0 ? ret : AVERROR_EOF;
110 return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
113 static int dss_read_header(AVFormatContext *s)
115 DSSDemuxContext *ctx = s->priv_data;
116 AVIOContext *pb = s->pb;
120 st = avformat_new_stream(s, NULL);
122 return AVERROR(ENOMEM);
124 version = avio_r8(pb);
125 ctx->dss_header_size = version * DSS_BLOCK_SIZE;
127 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
128 DSS_AUTHOR_SIZE, "author");
132 ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
136 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
137 DSS_COMMENT_SIZE, "comment");
141 avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
142 ctx->audio_codec = avio_r8(pb);
144 if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
145 st->codecpar->codec_id = AV_CODEC_ID_DSS_SP;
146 st->codecpar->sample_rate = 11025;
147 } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
148 st->codecpar->codec_id = AV_CODEC_ID_G723_1;
149 st->codecpar->sample_rate = 8000;
151 avpriv_request_sample(s, "Support for codec %x in DSS",
153 return AVERROR_PATCHWELCOME;
156 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
157 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
158 st->codecpar->channels = 1;
160 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
163 /* Jump over header */
165 if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
171 ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
172 if (!ctx->dss_sp_buf)
173 return AVERROR(ENOMEM);
178 static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
180 DSSDemuxContext *ctx = s->priv_data;
181 AVIOContext *pb = s->pb;
183 avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
184 ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
187 static void dss_sp_byte_swap(DSSDemuxContext *ctx,
188 uint8_t *dst, const uint8_t *src)
193 for (i = 3; i < DSS_FRAME_SIZE; i += 2)
196 for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
199 dst[1] = ctx->dss_sp_swap_byte;
201 memcpy(dst, src, DSS_FRAME_SIZE);
202 ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
205 /* make sure byte 40 is always 0 */
206 dst[DSS_FRAME_SIZE - 2] = 0;
210 static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
212 DSSDemuxContext *ctx = s->priv_data;
213 AVStream *st = s->streams[0];
214 int read_size, ret, offset = 0, buff_offset = 0;
215 int64_t pos = avio_tell(s->pb);
217 if (ctx->counter == 0)
218 dss_skip_audio_header(s, pkt);
221 read_size = DSS_FRAME_SIZE - 2;
224 read_size = DSS_FRAME_SIZE;
226 ctx->counter -= read_size;
227 ctx->packet_size = DSS_FRAME_SIZE - 1;
229 ret = av_new_packet(pkt, DSS_FRAME_SIZE);
235 pkt->stream_index = 0;
236 s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
238 if (ctx->counter < 0) {
239 int size2 = ctx->counter + read_size;
241 ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
243 if (ret < size2 - offset)
246 dss_skip_audio_header(s, pkt);
250 ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
252 if (ret < read_size - offset)
255 dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
257 if (ctx->dss_sp_swap_byte < 0) {
258 return AVERROR(EAGAIN);
264 return ret < 0 ? ret : AVERROR_EOF;
267 static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
269 DSSDemuxContext *ctx = s->priv_data;
270 AVStream *st = s->streams[0];
271 int size, byte, ret, offset;
272 int64_t pos = avio_tell(s->pb);
274 if (ctx->counter == 0)
275 dss_skip_audio_header(s, pkt);
277 /* We make one byte-step here. Don't forget to add offset. */
278 byte = avio_r8(s->pb);
280 return AVERROR_INVALIDDATA;
282 size = frame_size[byte & 3];
284 ctx->packet_size = size;
285 ctx->counter -= size;
287 ret = av_new_packet(pkt, size);
295 s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
297 pkt->stream_index = 0;
299 if (ctx->counter < 0) {
300 int size2 = ctx->counter + size;
302 ret = avio_read(s->pb, pkt->data + offset,
304 if (ret < size2 - offset) {
305 return ret < 0 ? ret : AVERROR_EOF;
308 dss_skip_audio_header(s, pkt);
312 ret = avio_read(s->pb, pkt->data + offset, size - offset);
313 if (ret < size - offset) {
314 return ret < 0 ? ret : AVERROR_EOF;
320 static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
322 DSSDemuxContext *ctx = s->priv_data;
324 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
325 return dss_sp_read_packet(s, pkt);
327 return dss_723_1_read_packet(s, pkt);
330 static int dss_read_close(AVFormatContext *s)
332 DSSDemuxContext *ctx = s->priv_data;
334 av_freep(&ctx->dss_sp_buf);
339 static int dss_read_seek(AVFormatContext *s, int stream_index,
340 int64_t timestamp, int flags)
342 DSSDemuxContext *ctx = s->priv_data;
344 uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
347 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
348 seekto = timestamp / 264 * 41 / 506 * 512;
350 seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
355 seekto += ctx->dss_header_size;
357 ret = avio_seek(s->pb, seekto, SEEK_SET);
361 avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
362 ctx->swap = !!(header[0] & 0x80);
363 offset = 2*header[1] + 2*ctx->swap;
364 if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
365 return AVERROR_INVALIDDATA;
366 if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
368 offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
370 ctx->counter = DSS_BLOCK_SIZE - offset;
371 offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
373 ctx->dss_sp_swap_byte = -1;
378 AVInputFormat ff_dss_demuxer = {
380 .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
381 .priv_data_size = sizeof(DSSDemuxContext),
382 .read_probe = dss_probe,
383 .read_header = dss_read_header,
384 .read_packet = dss_read_packet,
385 .read_close = dss_read_close,
386 .read_seek = dss_read_seek,