2 * Digital Speech Standard (DSS) demuxer
3 * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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_HEADER_SIZE (DSS_BLOCK_SIZE * 2)
46 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
47 #define DSS_FRAME_SIZE 42
49 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
51 typedef struct DSSDemuxContext {
52 unsigned int audio_codec;
59 static int dss_probe(AVProbeData *p)
61 if (AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's'))
64 return AVPROBE_SCORE_MAX;
67 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
70 AVIOContext *pb = s->pb;
71 char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
72 int y, month, d, h, minute, sec;
75 avio_seek(pb, offset, SEEK_SET);
77 ret = avio_read(s->pb, string, DSS_TIME_SIZE);
78 if (ret < DSS_TIME_SIZE)
79 return ret < 0 ? ret : AVERROR_EOF;
81 sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec);
82 /* We deal with a two-digit year here, so set the default date to 2000
83 * and hope it will never be used in the next century. */
84 snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
85 y + 2000, month, d, h, minute, sec);
86 return av_dict_set(&s->metadata, key, datetime, 0);
89 static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
90 unsigned int size, const char *key)
92 AVIOContext *pb = s->pb;
96 avio_seek(pb, offset, SEEK_SET);
98 value = av_mallocz(size + 1);
100 return AVERROR(ENOMEM);
102 ret = avio_read(s->pb, value, size);
104 ret = ret < 0 ? ret : AVERROR_EOF;
108 ret = av_dict_set(&s->metadata, key, value, 0);
115 static int dss_read_header(AVFormatContext *s)
117 DSSDemuxContext *ctx = s->priv_data;
118 AVIOContext *pb = s->pb;
122 st = avformat_new_stream(s, NULL);
124 return AVERROR(ENOMEM);
126 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
127 DSS_AUTHOR_SIZE, "author");
131 ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
135 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
136 DSS_COMMENT_SIZE, "comment");
140 avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
141 ctx->audio_codec = avio_r8(pb);
143 if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
144 st->codec->codec_id = AV_CODEC_ID_DSS_SP;
145 st->codec->sample_rate = 12000;
146 } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
147 st->codec->codec_id = AV_CODEC_ID_G723_1;
148 st->codec->sample_rate = 8000;
150 avpriv_request_sample(s, "Support for codec %x in DSS",
152 return AVERROR_PATCHWELCOME;
155 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
156 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
157 st->codec->channels = 1;
159 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
162 /* Jump over header */
164 if (avio_seek(pb, DSS_HEADER_SIZE, SEEK_SET) != DSS_HEADER_SIZE)
170 ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
171 if (!ctx->dss_sp_buf)
172 return AVERROR(ENOMEM);
177 static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
179 DSSDemuxContext *ctx = s->priv_data;
180 AVIOContext *pb = s->pb;
182 avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
183 ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
186 static void dss_sp_byte_swap(DSSDemuxContext *ctx,
187 uint8_t *dst, const uint8_t *src)
192 for (i = 3; i < DSS_FRAME_SIZE; i += 2)
195 for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
198 dst[1] = ctx->dss_sp_swap_byte;
200 memcpy(dst, src, DSS_FRAME_SIZE);
201 ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
204 /* make sure byte 40 is always 0 */
205 dst[DSS_FRAME_SIZE - 2] = 0;
209 static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
211 DSSDemuxContext *ctx = s->priv_data;
212 int read_size, ret, offset = 0, buff_offset = 0;
214 if (ctx->counter == 0)
215 dss_skip_audio_header(s, pkt);
217 pkt->pos = avio_tell(s->pb);
220 read_size = DSS_FRAME_SIZE - 2;
223 read_size = DSS_FRAME_SIZE;
225 ctx->counter -= read_size;
227 ret = av_new_packet(pkt, DSS_FRAME_SIZE);
232 pkt->stream_index = 0;
234 if (ctx->counter < 0) {
235 int size2 = ctx->counter + read_size;
237 ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
239 if (ret < size2 - offset)
242 dss_skip_audio_header(s, pkt);
246 ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
248 if (ret < read_size - offset)
251 dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
253 if (pkt->data[0] == 0xff)
254 return AVERROR_INVALIDDATA;
259 av_packet_unref(pkt);
260 return ret < 0 ? ret : AVERROR_EOF;
263 static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
265 DSSDemuxContext *ctx = s->priv_data;
266 int size, byte, ret, offset;
268 if (ctx->counter == 0)
269 dss_skip_audio_header(s, pkt);
271 pkt->pos = avio_tell(s->pb);
272 /* We make one byte-step here. Don't forget to add offset. */
273 byte = avio_r8(s->pb);
275 return AVERROR_INVALIDDATA;
277 size = frame_size[byte & 3];
279 ctx->counter -= size;
281 ret = av_new_packet(pkt, size);
289 pkt->stream_index = 0;
291 if (ctx->counter < 0) {
292 int size2 = ctx->counter + size;
294 ret = avio_read(s->pb, pkt->data + offset,
296 if (ret < size2 - offset) {
297 av_packet_unref(pkt);
298 return ret < 0 ? ret : AVERROR_EOF;
301 dss_skip_audio_header(s, pkt);
305 ret = avio_read(s->pb, pkt->data + offset, size - offset);
306 if (ret < size - offset) {
307 av_packet_unref(pkt);
308 return ret < 0 ? ret : AVERROR_EOF;
314 static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
316 DSSDemuxContext *ctx = s->priv_data;
318 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
319 return dss_sp_read_packet(s, pkt);
321 return dss_723_1_read_packet(s, pkt);
324 static int dss_read_close(AVFormatContext *s)
326 DSSDemuxContext *ctx = s->priv_data;
328 av_free(ctx->dss_sp_buf);
333 AVInputFormat ff_dss_demuxer = {
335 .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
336 .priv_data_size = sizeof(DSSDemuxContext),
337 .read_probe = dss_probe,
338 .read_header = dss_read_header,
339 .read_packet = dss_read_packet,
340 .read_close = dss_read_close,