blob: d619ea00d7242bea10a7c4c88408e3d50ba729da [file] [log] [blame]
Oleksij Rempel062cd5a2015-02-13 07:36:171/*
2 * Digital Speech Standard (DSS) demuxer
3 * Copyright (c) 2014 Oleksij Rempel <[email protected]>
4 *
Michael Niedermayere8219102015-02-19 20:44:585 * This file is part of FFmpeg.
Oleksij Rempel062cd5a2015-02-13 07:36:176 *
Michael Niedermayere8219102015-02-19 20:44:587 * FFmpeg is free software; you can redistribute it and/or
Oleksij Rempel062cd5a2015-02-13 07:36:178 * 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 *
Michael Niedermayere8219102015-02-19 20:44:5812 * FFmpeg is distributed in the hope that it will be useful,
Oleksij Rempel062cd5a2015-02-13 07:36:1713 * 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
Michael Niedermayere8219102015-02-19 20:44:5818 * License along with FFmpeg; if not, write to the Free Software
Oleksij Rempel062cd5a2015-02-13 07:36:1719 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Oleksij Rempel062cd5a2015-02-13 07:36:1722#include "libavutil/channel_layout.h"
23#include "libavutil/intreadwrite.h"
24
25#include "avformat.h"
26#include "internal.h"
27
28#define DSS_HEAD_OFFSET_AUTHOR 0xc
29#define DSS_AUTHOR_SIZE 16
30
31#define DSS_HEAD_OFFSET_START_TIME 0x26
32#define DSS_HEAD_OFFSET_END_TIME 0x32
33#define DSS_TIME_SIZE 12
34
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 */
38
39#define DSS_HEAD_OFFSET_COMMENT 0x31e
40#define DSS_COMMENT_SIZE 64
41
42#define DSS_BLOCK_SIZE 512
Oleksij Rempel062cd5a2015-02-13 07:36:1743#define DSS_AUDIO_BLOCK_HEADER_SIZE 6
44#define DSS_FRAME_SIZE 42
45
46static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
47
48typedef struct DSSDemuxContext {
49 unsigned int audio_codec;
50 int counter;
51 int swap;
52 int dss_sp_swap_byte;
Michael Niedermayer626904b2015-02-24 13:57:3953
54 int packet_size;
Carl Eugen Hoyosf55da222017-01-11 12:23:1355 int dss_header_size;
Oleksij Rempel062cd5a2015-02-13 07:36:1756} DSSDemuxContext;
57
Carl Eugen Hoyos4d8875e2019-03-21 00:18:3758static int dss_probe(const AVProbeData *p)
Oleksij Rempel062cd5a2015-02-13 07:36:1759{
Carl Eugen Hoyosf55da222017-01-11 12:23:1360 if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
61 && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
Oleksij Rempel062cd5a2015-02-13 07:36:1762 return 0;
63
64 return AVPROBE_SCORE_MAX;
65}
66
67static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
68 const char *key)
69{
70 AVIOContext *pb = s->pb;
71 char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
72 int y, month, d, h, minute, sec;
73 int ret;
74
75 avio_seek(pb, offset, SEEK_SET);
76
77 ret = avio_read(s->pb, string, DSS_TIME_SIZE);
78 if (ret < DSS_TIME_SIZE)
79 return ret < 0 ? ret : AVERROR_EOF;
80
Michael Niedermayerb2bbe822015-02-19 21:10:4781 if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
82 return AVERROR_INVALIDDATA;
Oleksij Rempel062cd5a2015-02-13 07:36:1783 /* We deal with a two-digit year here, so set the default date to 2000
84 * and hope it will never be used in the next century. */
85 snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
86 y + 2000, month, d, h, minute, sec);
87 return av_dict_set(&s->metadata, key, datetime, 0);
88}
89
90static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
91 unsigned int size, const char *key)
92{
93 AVIOContext *pb = s->pb;
94 char *value;
95 int ret;
96
97 avio_seek(pb, offset, SEEK_SET);
98
99 value = av_mallocz(size + 1);
100 if (!value)
101 return AVERROR(ENOMEM);
102
103 ret = avio_read(s->pb, value, size);
104 if (ret < size) {
Andreas Rheinhardt557668e2019-11-10 01:48:21105 av_free(value);
106 return ret < 0 ? ret : AVERROR_EOF;
Oleksij Rempel062cd5a2015-02-13 07:36:17107 }
108
Andreas Rheinhardt557668e2019-11-10 01:48:21109 return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
Oleksij Rempel062cd5a2015-02-13 07:36:17110}
111
112static int dss_read_header(AVFormatContext *s)
113{
114 DSSDemuxContext *ctx = s->priv_data;
115 AVIOContext *pb = s->pb;
116 AVStream *st;
Carl Eugen Hoyosf55da222017-01-11 12:23:13117 int ret, version;
Oleksij Rempel062cd5a2015-02-13 07:36:17118
119 st = avformat_new_stream(s, NULL);
120 if (!st)
121 return AVERROR(ENOMEM);
122
Carl Eugen Hoyosf55da222017-01-11 12:23:13123 version = avio_r8(pb);
124 ctx->dss_header_size = version * DSS_BLOCK_SIZE;
125
Oleksij Rempel062cd5a2015-02-13 07:36:17126 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
127 DSS_AUTHOR_SIZE, "author");
128 if (ret)
129 return ret;
130
131 ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
132 if (ret)
133 return ret;
134
135 ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
136 DSS_COMMENT_SIZE, "comment");
137 if (ret)
138 return ret;
139
140 avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
141 ctx->audio_codec = avio_r8(pb);
142
143 if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
Anton Khirnov92005142014-06-18 18:42:52144 st->codecpar->codec_id = AV_CODEC_ID_DSS_SP;
Derek Buitenhuis6f69f7a2016-04-10 19:58:15145 st->codecpar->sample_rate = 11025;
Andreas Rheinhardtef8c8b42021-04-01 20:31:13146 s->bit_rate = 8 * (DSS_FRAME_SIZE - 1) * st->codecpar->sample_rate
147 * 512 / (506 * 264);
Oleksij Rempel062cd5a2015-02-13 07:36:17148 } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
Anton Khirnov92005142014-06-18 18:42:52149 st->codecpar->codec_id = AV_CODEC_ID_G723_1;
150 st->codecpar->sample_rate = 8000;
Oleksij Rempel062cd5a2015-02-13 07:36:17151 } else {
152 avpriv_request_sample(s, "Support for codec %x in DSS",
153 ctx->audio_codec);
154 return AVERROR_PATCHWELCOME;
155 }
156
Anton Khirnov92005142014-06-18 18:42:52157 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
Vittorio Giovaraca467f22017-03-31 11:46:26158 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
Oleksij Rempel062cd5a2015-02-13 07:36:17159
Anton Khirnov92005142014-06-18 18:42:52160 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
Oleksij Rempel062cd5a2015-02-13 07:36:17161 st->start_time = 0;
162
163 /* Jump over header */
164
Carl Eugen Hoyosf55da222017-01-11 12:23:13165 if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
Oleksij Rempel062cd5a2015-02-13 07:36:17166 return AVERROR(EIO);
167
168 ctx->counter = 0;
169 ctx->swap = 0;
170
Oleksij Rempel062cd5a2015-02-13 07:36:17171 return 0;
172}
173
174static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
175{
176 DSSDemuxContext *ctx = s->priv_data;
177 AVIOContext *pb = s->pb;
178
179 avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
180 ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
181}
182
Andreas Rheinhardt48a594a2021-04-01 20:55:31183static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
Oleksij Rempel062cd5a2015-02-13 07:36:17184{
185 int i;
186
187 if (ctx->swap) {
Oleksij Rempel062cd5a2015-02-13 07:36:17188 for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
Andreas Rheinhardt48a594a2021-04-01 20:55:31189 data[i] = data[i + 4];
Oleksij Rempel062cd5a2015-02-13 07:36:17190
Andreas Rheinhardt48a594a2021-04-01 20:55:31191 /* Zero the padding. */
192 data[DSS_FRAME_SIZE] = 0;
193 data[1] = ctx->dss_sp_swap_byte;
Oleksij Rempel062cd5a2015-02-13 07:36:17194 } else {
Andreas Rheinhardt48a594a2021-04-01 20:55:31195 ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
Oleksij Rempel062cd5a2015-02-13 07:36:17196 }
197
198 /* make sure byte 40 is always 0 */
Andreas Rheinhardt48a594a2021-04-01 20:55:31199 data[DSS_FRAME_SIZE - 2] = 0;
Oleksij Rempel062cd5a2015-02-13 07:36:17200 ctx->swap ^= 1;
201}
202
203static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
204{
205 DSSDemuxContext *ctx = s->priv_data;
Oleksij Rempel062cd5a2015-02-13 07:36:17206 int read_size, ret, offset = 0, buff_offset = 0;
Michael Niedermayer847daac2015-02-24 13:54:20207 int64_t pos = avio_tell(s->pb);
Oleksij Rempel062cd5a2015-02-13 07:36:17208
209 if (ctx->counter == 0)
210 dss_skip_audio_header(s, pkt);
211
Oleksij Rempel062cd5a2015-02-13 07:36:17212 if (ctx->swap) {
213 read_size = DSS_FRAME_SIZE - 2;
214 buff_offset = 3;
215 } else
216 read_size = DSS_FRAME_SIZE;
217
Oleksij Rempel062cd5a2015-02-13 07:36:17218 ret = av_new_packet(pkt, DSS_FRAME_SIZE);
219 if (ret < 0)
220 return ret;
221
Michael Niedermayer5634ca62015-02-24 13:51:16222 pkt->duration = 264;
Michael Niedermayer847daac2015-02-24 13:54:20223 pkt->pos = pos;
Oleksij Rempel062cd5a2015-02-13 07:36:17224 pkt->stream_index = 0;
Oleksij Rempel062cd5a2015-02-13 07:36:17225
Andreas Rheinhardtafa511a2021-04-01 20:07:40226 if (ctx->counter < read_size) {
Andreas Rheinhardt48a594a2021-04-01 20:55:31227 ret = avio_read(s->pb, pkt->data + buff_offset,
Andreas Rheinhardtafa511a2021-04-01 20:07:40228 ctx->counter);
229 if (ret < ctx->counter)
Oleksij Rempel062cd5a2015-02-13 07:36:17230 goto error_eof;
231
Andreas Rheinhardtafa511a2021-04-01 20:07:40232 offset = ctx->counter;
Oleksij Rempel062cd5a2015-02-13 07:36:17233 dss_skip_audio_header(s, pkt);
Oleksij Rempel062cd5a2015-02-13 07:36:17234 }
Andreas Rheinhardtafa511a2021-04-01 20:07:40235 ctx->counter -= read_size;
Oleksij Rempel062cd5a2015-02-13 07:36:17236
Andreas Rheinhardt48a594a2021-04-01 20:55:31237 /* This will write one byte into pkt's padding if buff_offset == 3 */
238 ret = avio_read(s->pb, pkt->data + offset + buff_offset,
Oleksij Rempel062cd5a2015-02-13 07:36:17239 read_size - offset);
240 if (ret < read_size - offset)
241 goto error_eof;
242
Andreas Rheinhardt48a594a2021-04-01 20:55:31243 dss_sp_byte_swap(ctx, pkt->data);
Oleksij Rempel062cd5a2015-02-13 07:36:17244
Michael Niedermayer111d79a2015-02-24 13:58:17245 if (ctx->dss_sp_swap_byte < 0) {
Andreas Rheinhardt6a67d512020-01-07 13:55:40246 return AVERROR(EAGAIN);
Michael Niedermayer111d79a2015-02-24 13:58:17247 }
248
Andreas Rheinhardtb3652d92021-04-01 21:02:18249 return 0;
Oleksij Rempel062cd5a2015-02-13 07:36:17250
251error_eof:
Oleksij Rempel062cd5a2015-02-13 07:36:17252 return ret < 0 ? ret : AVERROR_EOF;
253}
254
255static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
256{
257 DSSDemuxContext *ctx = s->priv_data;
Michael Niedermayer626904b2015-02-24 13:57:39258 AVStream *st = s->streams[0];
Oleksij Rempel062cd5a2015-02-13 07:36:17259 int size, byte, ret, offset;
Michael Niedermayer847daac2015-02-24 13:54:20260 int64_t pos = avio_tell(s->pb);
Oleksij Rempel062cd5a2015-02-13 07:36:17261
262 if (ctx->counter == 0)
263 dss_skip_audio_header(s, pkt);
264
Oleksij Rempel062cd5a2015-02-13 07:36:17265 /* We make one byte-step here. Don't forget to add offset. */
266 byte = avio_r8(s->pb);
267 if (byte == 0xff)
268 return AVERROR_INVALIDDATA;
269
270 size = frame_size[byte & 3];
271
Michael Niedermayer626904b2015-02-24 13:57:39272 ctx->packet_size = size;
Andreas Rheinhardtafa511a2021-04-01 20:07:40273 ctx->counter--;
Oleksij Rempel062cd5a2015-02-13 07:36:17274
275 ret = av_new_packet(pkt, size);
276 if (ret < 0)
277 return ret;
Michael Niedermayer847daac2015-02-24 13:54:20278 pkt->pos = pos;
Oleksij Rempel062cd5a2015-02-13 07:36:17279
280 pkt->data[0] = byte;
281 offset = 1;
282 pkt->duration = 240;
Andreas Rheinhardtafa511a2021-04-01 20:07:40283 s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
Oleksij Rempel062cd5a2015-02-13 07:36:17284
285 pkt->stream_index = 0;
286
Andreas Rheinhardtafa511a2021-04-01 20:07:40287 if (ctx->counter < size) {
Oleksij Rempel062cd5a2015-02-13 07:36:17288 ret = avio_read(s->pb, pkt->data + offset,
Andreas Rheinhardtafa511a2021-04-01 20:07:40289 ctx->counter);
290 if (ret < ctx->counter)
Oleksij Rempel062cd5a2015-02-13 07:36:17291 return ret < 0 ? ret : AVERROR_EOF;
Oleksij Rempel062cd5a2015-02-13 07:36:17292
Andreas Rheinhardtafa511a2021-04-01 20:07:40293 offset += ctx->counter;
294 size -= ctx->counter;
295 ctx->counter = 0;
Oleksij Rempel062cd5a2015-02-13 07:36:17296 dss_skip_audio_header(s, pkt);
Oleksij Rempel062cd5a2015-02-13 07:36:17297 }
Andreas Rheinhardtafa511a2021-04-01 20:07:40298 ctx->counter -= size;
Oleksij Rempel062cd5a2015-02-13 07:36:17299
Andreas Rheinhardtafa511a2021-04-01 20:07:40300 ret = avio_read(s->pb, pkt->data + offset, size);
301 if (ret < size)
Oleksij Rempel062cd5a2015-02-13 07:36:17302 return ret < 0 ? ret : AVERROR_EOF;
Oleksij Rempel062cd5a2015-02-13 07:36:17303
Andreas Rheinhardtb3652d92021-04-01 21:02:18304 return 0;
Oleksij Rempel062cd5a2015-02-13 07:36:17305}
306
307static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
308{
309 DSSDemuxContext *ctx = s->priv_data;
310
311 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
312 return dss_sp_read_packet(s, pkt);
313 else
314 return dss_723_1_read_packet(s, pkt);
315}
316
Michael Niedermayer111d79a2015-02-24 13:58:17317static int dss_read_seek(AVFormatContext *s, int stream_index,
318 int64_t timestamp, int flags)
319{
320 DSSDemuxContext *ctx = s->priv_data;
321 int64_t ret, seekto;
322 uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
323 int offset;
324
325 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
326 seekto = timestamp / 264 * 41 / 506 * 512;
327 else
328 seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
329
330 if (seekto < 0)
331 seekto = 0;
332
Carl Eugen Hoyosf55da222017-01-11 12:23:13333 seekto += ctx->dss_header_size;
Michael Niedermayer111d79a2015-02-24 13:58:17334
335 ret = avio_seek(s->pb, seekto, SEEK_SET);
336 if (ret < 0)
337 return ret;
338
339 avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
340 ctx->swap = !!(header[0] & 0x80);
341 offset = 2*header[1] + 2*ctx->swap;
342 if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
343 return AVERROR_INVALIDDATA;
344 if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
345 ctx->counter = 0;
346 offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
347 } else {
348 ctx->counter = DSS_BLOCK_SIZE - offset;
349 offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
350 }
351 ctx->dss_sp_swap_byte = -1;
352 return 0;
353}
354
355
Andreas Rheinhardtbc706842021-04-19 17:45:24356const AVInputFormat ff_dss_demuxer = {
Oleksij Rempel062cd5a2015-02-13 07:36:17357 .name = "dss",
358 .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
359 .priv_data_size = sizeof(DSSDemuxContext),
360 .read_probe = dss_probe,
361 .read_header = dss_read_header,
362 .read_packet = dss_read_packet,
Michael Niedermayer111d79a2015-02-24 13:58:17363 .read_seek = dss_read_seek,
Oleksij Rempel062cd5a2015-02-13 07:36:17364 .extensions = "dss"
365};