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"
24 #include "libavutil/mem.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
;
60 static int dss_probe(const AVProbeData
*p
)
62 if ( AV_RL32(p
->buf
) != MKTAG(0x2, 'd', 's', 's')
63 && AV_RL32(p
->buf
) != MKTAG(0x3, 'd', 's', 's'))
66 return AVPROBE_SCORE_MAX
;
69 static int dss_read_metadata_date(AVFormatContext
*s
, unsigned int offset
,
72 AVIOContext
*pb
= s
->pb
;
73 char datetime
[64], string
[DSS_TIME_SIZE
+ 1] = { 0 };
74 int y
, month
, d
, h
, minute
, sec
;
77 avio_seek(pb
, offset
, SEEK_SET
);
79 ret
= avio_read(s
->pb
, string
, DSS_TIME_SIZE
);
80 if (ret
< DSS_TIME_SIZE
)
81 return ret
< 0 ? ret
: AVERROR_EOF
;
83 if (sscanf(string
, "%2d%2d%2d%2d%2d%2d", &y
, &month
, &d
, &h
, &minute
, &sec
) != 6)
84 return AVERROR_INVALIDDATA
;
85 /* We deal with a two-digit year here, so set the default date to 2000
86 * and hope it will never be used in the next century. */
87 snprintf(datetime
, sizeof(datetime
), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
88 y
+ 2000, month
, d
, h
, minute
, sec
);
89 return av_dict_set(&s
->metadata
, key
, datetime
, 0);
92 static int dss_read_metadata_string(AVFormatContext
*s
, unsigned int offset
,
93 unsigned int size
, const char *key
)
95 AVIOContext
*pb
= s
->pb
;
99 avio_seek(pb
, offset
, SEEK_SET
);
101 value
= av_mallocz(size
+ 1);
103 return AVERROR(ENOMEM
);
105 ret
= avio_read(s
->pb
, value
, size
);
108 return ret
< 0 ? ret
: AVERROR_EOF
;
111 return av_dict_set(&s
->metadata
, key
, value
, AV_DICT_DONT_STRDUP_VAL
);
114 static int dss_read_header(AVFormatContext
*s
)
116 DSSDemuxContext
*ctx
= s
->priv_data
;
117 AVIOContext
*pb
= s
->pb
;
122 st
= avformat_new_stream(s
, NULL
);
124 return AVERROR(ENOMEM
);
126 version
= avio_r8(pb
);
127 ctx
->dss_header_size
= version
* DSS_BLOCK_SIZE
;
129 ret
= dss_read_metadata_string(s
, DSS_HEAD_OFFSET_AUTHOR
,
130 DSS_AUTHOR_SIZE
, "author");
134 ret
= dss_read_metadata_date(s
, DSS_HEAD_OFFSET_END_TIME
, "date");
138 ret
= dss_read_metadata_string(s
, DSS_HEAD_OFFSET_COMMENT
,
139 DSS_COMMENT_SIZE
, "comment");
143 avio_seek(pb
, DSS_HEAD_OFFSET_ACODEC
, SEEK_SET
);
144 ctx
->audio_codec
= avio_r8(pb
);
146 if (ctx
->audio_codec
== DSS_ACODEC_DSS_SP
) {
147 st
->codecpar
->codec_id
= AV_CODEC_ID_DSS_SP
;
148 st
->codecpar
->sample_rate
= 11025;
149 s
->bit_rate
= 8 * (DSS_FRAME_SIZE
- 1) * st
->codecpar
->sample_rate
151 } else if (ctx
->audio_codec
== DSS_ACODEC_G723_1
) {
152 st
->codecpar
->codec_id
= AV_CODEC_ID_G723_1
;
153 st
->codecpar
->sample_rate
= 8000;
155 avpriv_request_sample(s
, "Support for codec %x in DSS",
157 return AVERROR_PATCHWELCOME
;
160 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
161 st
->codecpar
->ch_layout
= (AVChannelLayout
)AV_CHANNEL_LAYOUT_MONO
;
163 avpriv_set_pts_info(st
, 64, 1, st
->codecpar
->sample_rate
);
166 /* Jump over header */
168 if ((ret64
= avio_seek(pb
, ctx
->dss_header_size
, SEEK_SET
)) < 0)
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
, uint8_t *data
)
191 for (i
= 0; i
< DSS_FRAME_SIZE
- 2; i
+= 2)
192 data
[i
] = data
[i
+ 4];
194 /* Zero the padding. */
195 data
[DSS_FRAME_SIZE
] = 0;
196 data
[1] = ctx
->dss_sp_swap_byte
;
198 ctx
->dss_sp_swap_byte
= data
[DSS_FRAME_SIZE
- 2];
201 /* make sure byte 40 is always 0 */
202 data
[DSS_FRAME_SIZE
- 2] = 0;
206 static int dss_sp_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
208 DSSDemuxContext
*ctx
= s
->priv_data
;
209 int read_size
, ret
, offset
= 0, buff_offset
= 0;
210 int64_t pos
= avio_tell(s
->pb
);
212 if (ctx
->counter
== 0)
213 dss_skip_audio_header(s
, pkt
);
216 read_size
= DSS_FRAME_SIZE
- 2;
219 read_size
= DSS_FRAME_SIZE
;
221 ret
= av_new_packet(pkt
, DSS_FRAME_SIZE
);
227 pkt
->stream_index
= 0;
229 if (ctx
->counter
< read_size
) {
230 ret
= avio_read(s
->pb
, pkt
->data
+ buff_offset
,
232 if (ret
< ctx
->counter
)
235 offset
= ctx
->counter
;
236 dss_skip_audio_header(s
, pkt
);
238 ctx
->counter
-= read_size
;
240 /* This will write one byte into pkt's padding if buff_offset == 3 */
241 ret
= avio_read(s
->pb
, pkt
->data
+ offset
+ buff_offset
,
243 if (ret
< read_size
- offset
)
246 dss_sp_byte_swap(ctx
, pkt
->data
);
248 if (ctx
->dss_sp_swap_byte
< 0) {
249 return AVERROR(EAGAIN
);
255 return ret
< 0 ? ret
: AVERROR_EOF
;
258 static int dss_723_1_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
260 DSSDemuxContext
*ctx
= s
->priv_data
;
261 AVStream
*st
= s
->streams
[0];
262 int size
, byte
, ret
, offset
;
263 int64_t pos
= avio_tell(s
->pb
);
265 if (ctx
->counter
== 0)
266 dss_skip_audio_header(s
, pkt
);
268 /* We make one byte-step here. Don't forget to add offset. */
269 byte
= avio_r8(s
->pb
);
271 return AVERROR_INVALIDDATA
;
273 size
= frame_size
[byte
& 3];
275 ctx
->packet_size
= size
;
278 ret
= av_new_packet(pkt
, size
);
286 s
->bit_rate
= 8LL * size
-- * st
->codecpar
->sample_rate
* 512 / (506 * pkt
->duration
);
288 pkt
->stream_index
= 0;
290 if (ctx
->counter
< size
) {
291 ret
= avio_read(s
->pb
, pkt
->data
+ offset
,
293 if (ret
< ctx
->counter
)
294 return ret
< 0 ? ret
: AVERROR_EOF
;
296 offset
+= ctx
->counter
;
297 size
-= ctx
->counter
;
299 dss_skip_audio_header(s
, pkt
);
301 ctx
->counter
-= size
;
303 ret
= avio_read(s
->pb
, pkt
->data
+ offset
, size
);
305 return ret
< 0 ? ret
: AVERROR_EOF
;
310 static int dss_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
312 DSSDemuxContext
*ctx
= s
->priv_data
;
314 if (ctx
->audio_codec
== DSS_ACODEC_DSS_SP
)
315 return dss_sp_read_packet(s
, pkt
);
317 return dss_723_1_read_packet(s
, pkt
);
320 static int dss_read_seek(AVFormatContext
*s
, int stream_index
,
321 int64_t timestamp
, int flags
)
323 DSSDemuxContext
*ctx
= s
->priv_data
;
325 uint8_t header
[DSS_AUDIO_BLOCK_HEADER_SIZE
];
328 if (ctx
->audio_codec
== DSS_ACODEC_DSS_SP
)
329 seekto
= timestamp
/ 264 * 41 / 506 * 512;
331 seekto
= timestamp
/ 240 * ctx
->packet_size
/ 506 * 512;
336 seekto
+= ctx
->dss_header_size
;
338 ret
= avio_seek(s
->pb
, seekto
, SEEK_SET
);
342 avio_read(s
->pb
, header
, DSS_AUDIO_BLOCK_HEADER_SIZE
);
343 ctx
->swap
= !!(header
[0] & 0x80);
344 offset
= 2*header
[1] + 2*ctx
->swap
;
345 if (offset
< DSS_AUDIO_BLOCK_HEADER_SIZE
)
346 return AVERROR_INVALIDDATA
;
347 if (offset
== DSS_AUDIO_BLOCK_HEADER_SIZE
) {
349 offset
= avio_skip(s
->pb
, -DSS_AUDIO_BLOCK_HEADER_SIZE
);
351 ctx
->counter
= DSS_BLOCK_SIZE
- offset
;
352 offset
= avio_skip(s
->pb
, offset
- DSS_AUDIO_BLOCK_HEADER_SIZE
);
354 ctx
->dss_sp_swap_byte
= -1;
359 const FFInputFormat ff_dss_demuxer
= {
361 .p
.long_name
= NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
362 .p
.extensions
= "dss",
363 .priv_data_size
= sizeof(DSSDemuxContext
),
364 .read_probe
= dss_probe
,
365 .read_header
= dss_read_header
,
366 .read_packet
= dss_read_packet
,
367 .read_seek
= dss_read_seek
,