FFmpeg
Loading...
Searching...
No Matches
dss.c
Go to the documentation of this file.
1/*
2 * Digital Speech Standard (DSS) demuxer
3 * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
24#include "libavutil/mem.h"
25
26#include "avformat.h"
27#include "demux.h"
28#include "internal.h"
29#include "avio_internal.h"
30
31#define DSS_HEAD_OFFSET_AUTHOR 0xc
32#define DSS_AUTHOR_SIZE 16
33
34#define DSS_HEAD_OFFSET_START_TIME 0x26
35#define DSS_HEAD_OFFSET_END_TIME 0x32
36#define DSS_TIME_SIZE 12
37
38#define DSS_HEAD_OFFSET_ACODEC 0x2a4
39#define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
40#define DSS_ACODEC_G723_1 0x2 /* LP mode */
41
42#define DSS_HEAD_OFFSET_COMMENT 0x31e
43#define DSS_COMMENT_SIZE 64
44
45#define DSS_BLOCK_SIZE 512
46#define DSS_AUDIO_BLOCK_HEADER_SIZE 6
47#define DSS_FRAME_SIZE 42
48
49static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
50
60
61static int dss_probe(const AVProbeData *p)
62{
63 if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
64 && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
65 return 0;
66
67 return AVPROBE_SCORE_MAX;
68}
69
71 const char *key)
72{
73 AVIOContext *pb = s->pb;
74 char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
75 int y, month, d, h, minute, sec;
76 int ret;
77
78 avio_seek(pb, offset, SEEK_SET);
79
80 ret = avio_read(s->pb, string, DSS_TIME_SIZE);
81 if (ret < DSS_TIME_SIZE)
82 return ret < 0 ? ret : AVERROR_EOF;
83
84 if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
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);
91}
92
94 unsigned int size, const char *key)
95{
96 AVIOContext *pb = s->pb;
97 char *value;
98 int ret;
99
100 avio_seek(pb, offset, SEEK_SET);
101
102 value = av_mallocz(size + 1);
103 if (!value)
104 return AVERROR(ENOMEM);
105
106 ret = avio_read(s->pb, value, size);
107 if (ret < size) {
108 av_free(value);
109 return ret < 0 ? ret : AVERROR_EOF;
110 }
111
112 return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
113}
114
116{
117 DSSDemuxContext *ctx = s->priv_data;
118 AVIOContext *pb = s->pb;
119 AVStream *st;
120 int64_t ret64;
121 int ret, version;
122
124 if (!st)
125 return AVERROR(ENOMEM);
126
127 version = avio_r8(pb);
128 ctx->dss_header_size = version * DSS_BLOCK_SIZE;
129
131 DSS_AUTHOR_SIZE, "author");
132 if (ret)
133 return ret;
134
136 if (ret)
137 return ret;
138
140 DSS_COMMENT_SIZE, "comment");
141 if (ret)
142 return ret;
143
144 avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
145 ctx->audio_codec = avio_r8(pb);
146
147 if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
149 st->codecpar->sample_rate = 11025;
150 s->bit_rate = 8 * (DSS_FRAME_SIZE - 1) * st->codecpar->sample_rate
151 * 512 / (506 * 264);
152 } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
154 st->codecpar->sample_rate = 8000;
155 } else {
156 avpriv_request_sample(s, "Support for codec %x in DSS",
157 ctx->audio_codec);
159 }
160
163
165 st->start_time = 0;
166
167 /* Jump over header */
168
169 if ((ret64 = avio_seek(pb, ctx->dss_header_size, SEEK_SET)) < 0)
170 return (int)ret64;
171
172 ctx->counter = 0;
173 ctx->swap = 0;
174
175 return 0;
176}
177
186
188{
189 int i;
190
191 if (ctx->swap) {
192 for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
193 data[i] = data[i + 4];
194
195 /* Zero the padding. */
196 data[DSS_FRAME_SIZE] = 0;
197 data[1] = ctx->dss_sp_swap_byte;
198 } else {
199 ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
200 }
201
202 /* make sure byte 40 is always 0 */
203 data[DSS_FRAME_SIZE - 2] = 0;
204 ctx->swap ^= 1;
205}
206
208{
209 DSSDemuxContext *ctx = s->priv_data;
210 int read_size, ret, offset = 0, buff_offset = 0;
211 int64_t pos = avio_tell(s->pb);
212
213 if (ctx->counter == 0)
215
216 if (ctx->swap) {
217 read_size = DSS_FRAME_SIZE - 2;
218 buff_offset = 3;
219 } else
220 read_size = DSS_FRAME_SIZE;
221
223 if (ret < 0)
224 return ret;
225
226 pkt->duration = 264;
227 pkt->pos = pos;
228 pkt->stream_index = 0;
229
230 if (ctx->counter < read_size) {
231 ret = avio_read(s->pb, pkt->data + buff_offset,
232 ctx->counter);
233 if (ret < ctx->counter)
234 goto error_eof;
235
236 offset = ctx->counter;
238 }
239 ctx->counter -= read_size;
240
241 /* This will write one byte into pkt's padding if buff_offset == 3 */
242 ret = avio_read(s->pb, pkt->data + offset + buff_offset,
243 read_size - offset);
244 if (ret < read_size - offset)
245 goto error_eof;
246
247 dss_sp_byte_swap(ctx, pkt->data);
248
249 if (ctx->dss_sp_swap_byte < 0) {
250 return AVERROR(EAGAIN);
251 }
252
253 return 0;
254
255error_eof:
256 return ret < 0 ? ret : AVERROR_EOF;
257}
258
260{
261 DSSDemuxContext *ctx = s->priv_data;
262 AVStream *st = s->streams[0];
263 int size, byte, ret, offset;
264 int64_t pos = avio_tell(s->pb);
265
266 if (ctx->counter == 0)
268
269 /* We make one byte-step here. Don't forget to add offset. */
270 byte = avio_r8(s->pb);
271 if (byte == 0xff)
272 return AVERROR_INVALIDDATA;
273
274 size = frame_size[byte & 3];
275
276 ctx->packet_size = size;
277 ctx->counter--;
278
279 ret = av_new_packet(pkt, size);
280 if (ret < 0)
281 return ret;
282 pkt->pos = pos;
283
284 pkt->data[0] = byte;
285 offset = 1;
286 pkt->duration = 240;
287 s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
288
289 pkt->stream_index = 0;
290
291 if (ctx->counter < size) {
292 ret = avio_read(s->pb, pkt->data + offset,
293 ctx->counter);
294 if (ret < ctx->counter)
295 return ret < 0 ? ret : AVERROR_EOF;
296
297 offset += ctx->counter;
298 size -= ctx->counter;
299 ctx->counter = 0;
301 }
302 ctx->counter -= size;
303
304 ret = avio_read(s->pb, pkt->data + offset, size);
305 if (ret < size)
306 return ret < 0 ? ret : AVERROR_EOF;
307
308 return 0;
309}
310
312{
313 DSSDemuxContext *ctx = s->priv_data;
314
315 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
316 return dss_sp_read_packet(s, pkt);
317 else
318 return dss_723_1_read_packet(s, pkt);
319}
320
321static int dss_read_seek(AVFormatContext *s, int stream_index,
322 int64_t timestamp, int flags)
323{
324 DSSDemuxContext *ctx = s->priv_data;
325 int64_t ret, seekto;
327 int offset;
328
329 if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
330 seekto = timestamp / 264 * 41 / 506 * 512;
331 else
332 seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
333
334 if (seekto < 0)
335 seekto = 0;
336
337 seekto += ctx->dss_header_size;
338
339 ret = avio_seek(s->pb, seekto, SEEK_SET);
340 if (ret < 0)
341 return ret;
342
344 if (ret < 0)
345 return ret;
346 ctx->swap = !!(header[0] & 0x80);
347 offset = 2*header[1] + 2*ctx->swap;
349 return AVERROR_INVALIDDATA;
351 ctx->counter = 0;
353 } else {
354 ctx->counter = DSS_BLOCK_SIZE - offset;
356 }
357 ctx->dss_sp_swap_byte = -1;
358 return 0;
359}
360
361
363 .p.name = "dss",
364 .p.long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
365 .p.extensions = "dss",
366 .priv_data_size = sizeof(DSSDemuxContext),
371};
const FFInputFormat ff_dss_demuxer
Definition dss.c:362
static AVFormatContext * ctx
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition bytestream.h:99
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
#define DSS_ACODEC_G723_1
Definition dss.c:40
#define DSS_AUTHOR_SIZE
Definition dss.c:32
static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dss.c:207
static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset, unsigned int size, const char *key)
Definition dss.c:93
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
Definition dss.c:187
#define DSS_HEAD_OFFSET_ACODEC
Definition dss.c:38
static int dss_probe(const AVProbeData *p)
Definition dss.c:61
#define DSS_HEAD_OFFSET_END_TIME
Definition dss.c:35
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition dss.c:178
static int dss_read_header(AVFormatContext *s)
Definition dss.c:115
#define DSS_HEAD_OFFSET_COMMENT
Definition dss.c:42
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dss.c:311
#define DSS_HEAD_OFFSET_AUTHOR
Definition dss.c:31
#define DSS_FRAME_SIZE
Definition dss.c:47
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dss.c:259
#define DSS_AUDIO_BLOCK_HEADER_SIZE
Definition dss.c:46
#define DSS_COMMENT_SIZE
Definition dss.c:43
#define DSS_ACODEC_DSS_SP
Definition dss.c:39
#define DSS_TIME_SIZE
Definition dss.c:36
static int dss_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition dss.c:321
static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset, const char *key)
Definition dss.c:70
#define DSS_BLOCK_SIZE
Definition dss.c:45
double value
Definition eval.c:102
const char * key
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static const uint8_t frame_size[4]
Definition g723_1.h:222
@ AV_CODEC_ID_G723_1
Definition codec_id.h:505
@ AV_CODEC_ID_DSS_SP
Definition codec_id.h:519
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
version
Definition libkvazaar.c:313
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
const char data[16]
Definition mxf.c:149
static const uint8_t header[24]
Definition sdr2.c:68
#define snprintf
Definition snprintf.h:34
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int packet_size
Definition dss.c:57
int dss_header_size
Definition dss.c:58
int dss_sp_swap_byte
Definition dss.c:55
int counter
Definition dss.c:53
unsigned int audio_codec
Definition dss.c:52
#define av_free(p)
#define av_mallocz(s)
#define avpriv_request_sample(...)
int size