FFmpeg
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 
22 #include "libavutil/attributes.h"
23 #include "libavutil/bswap.h"
25 #include "libavutil/intreadwrite.h"
26 
27 #include "avformat.h"
28 #include "internal.h"
29 
30 #define DSS_HEAD_OFFSET_AUTHOR 0xc
31 #define DSS_AUTHOR_SIZE 16
32 
33 #define DSS_HEAD_OFFSET_START_TIME 0x26
34 #define DSS_HEAD_OFFSET_END_TIME 0x32
35 #define DSS_TIME_SIZE 12
36 
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 */
40 
41 #define DSS_HEAD_OFFSET_COMMENT 0x31e
42 #define DSS_COMMENT_SIZE 64
43 
44 #define DSS_BLOCK_SIZE 512
45 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
46 #define DSS_FRAME_SIZE 42
47 
48 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
49 
50 typedef struct DSSDemuxContext {
51  unsigned int audio_codec;
52  int counter;
53  int swap;
55  int8_t *dss_sp_buf;
56 
60 
61 static 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 
70 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
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)
85  return AVERROR_INVALIDDATA;
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  ret = ret < 0 ? ret : AVERROR_EOF;
109  goto exit;
110  }
111 
112  ret = av_dict_set(&s->metadata, key, value, 0);
113 
114 exit:
115  av_free(value);
116  return ret;
117 }
118 
120 {
121  DSSDemuxContext *ctx = s->priv_data;
122  AVIOContext *pb = s->pb;
123  AVStream *st;
124  int ret, version;
125 
126  st = avformat_new_stream(s, NULL);
127  if (!st)
128  return AVERROR(ENOMEM);
129 
130  version = avio_r8(pb);
131  ctx->dss_header_size = version * DSS_BLOCK_SIZE;
132 
134  DSS_AUTHOR_SIZE, "author");
135  if (ret)
136  return ret;
137 
139  if (ret)
140  return ret;
141 
143  DSS_COMMENT_SIZE, "comment");
144  if (ret)
145  return ret;
146 
147  avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
148  ctx->audio_codec = avio_r8(pb);
149 
152  st->codecpar->sample_rate = 11025;
153  } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
155  st->codecpar->sample_rate = 8000;
156  } else {
157  avpriv_request_sample(s, "Support for codec %x in DSS",
158  ctx->audio_codec);
159  return AVERROR_PATCHWELCOME;
160  }
161 
164  st->codecpar->channels = 1;
165 
166  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
167  st->start_time = 0;
168 
169  /* Jump over header */
170 
171  if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
172  return AVERROR(EIO);
173 
174  ctx->counter = 0;
175  ctx->swap = 0;
176 
177  ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
178  if (!ctx->dss_sp_buf)
179  return AVERROR(ENOMEM);
180 
181  return 0;
182 }
183 
185 {
186  DSSDemuxContext *ctx = s->priv_data;
187  AVIOContext *pb = s->pb;
188 
191 }
192 
194  uint8_t *dst, const uint8_t *src)
195 {
196  int i;
197 
198  if (ctx->swap) {
199  for (i = 3; i < DSS_FRAME_SIZE; i += 2)
200  dst[i] = src[i];
201 
202  for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
203  dst[i] = src[i + 4];
204 
205  dst[1] = ctx->dss_sp_swap_byte;
206  } else {
207  memcpy(dst, src, DSS_FRAME_SIZE);
208  ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
209  }
210 
211  /* make sure byte 40 is always 0 */
212  dst[DSS_FRAME_SIZE - 2] = 0;
213  ctx->swap ^= 1;
214 }
215 
217 {
218  DSSDemuxContext *ctx = s->priv_data;
219  AVStream *st = s->streams[0];
220  int read_size, ret, offset = 0, buff_offset = 0;
221  int64_t pos = avio_tell(s->pb);
222 
223  if (ctx->counter == 0)
225 
226  if (ctx->swap) {
227  read_size = DSS_FRAME_SIZE - 2;
228  buff_offset = 3;
229  } else
230  read_size = DSS_FRAME_SIZE;
231 
232  ctx->counter -= read_size;
234 
236  if (ret < 0)
237  return ret;
238 
239  pkt->duration = 264;
240  pkt->pos = pos;
241  pkt->stream_index = 0;
242  s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
243 
244  if (ctx->counter < 0) {
245  int size2 = ctx->counter + read_size;
246 
247  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
248  size2 - offset);
249  if (ret < size2 - offset)
250  goto error_eof;
251 
253  offset = size2;
254  }
255 
256  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
257  read_size - offset);
258  if (ret < read_size - offset)
259  goto error_eof;
260 
261  dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
262 
263  if (ctx->dss_sp_swap_byte < 0) {
264  ret = AVERROR(EAGAIN);
265  goto error_eof;
266  }
267 
268  return pkt->size;
269 
270 error_eof:
272  return ret < 0 ? ret : AVERROR_EOF;
273 }
274 
276 {
277  DSSDemuxContext *ctx = s->priv_data;
278  AVStream *st = s->streams[0];
279  int size, byte, ret, offset;
280  int64_t pos = avio_tell(s->pb);
281 
282  if (ctx->counter == 0)
284 
285  /* We make one byte-step here. Don't forget to add offset. */
286  byte = avio_r8(s->pb);
287  if (byte == 0xff)
288  return AVERROR_INVALIDDATA;
289 
290  size = frame_size[byte & 3];
291 
292  ctx->packet_size = size;
293  ctx->counter -= size;
294 
296  if (ret < 0)
297  return ret;
298  pkt->pos = pos;
299 
300  pkt->data[0] = byte;
301  offset = 1;
302  pkt->duration = 240;
303  s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
304 
305  pkt->stream_index = 0;
306 
307  if (ctx->counter < 0) {
308  int size2 = ctx->counter + size;
309 
310  ret = avio_read(s->pb, pkt->data + offset,
311  size2 - offset);
312  if (ret < size2 - offset) {
314  return ret < 0 ? ret : AVERROR_EOF;
315  }
316 
318  offset = size2;
319  }
320 
321  ret = avio_read(s->pb, pkt->data + offset, size - offset);
322  if (ret < size - offset) {
324  return ret < 0 ? ret : AVERROR_EOF;
325  }
326 
327  return pkt->size;
328 }
329 
331 {
332  DSSDemuxContext *ctx = s->priv_data;
333 
335  return dss_sp_read_packet(s, pkt);
336  else
337  return dss_723_1_read_packet(s, pkt);
338 }
339 
341 {
342  DSSDemuxContext *ctx = s->priv_data;
343 
344  av_freep(&ctx->dss_sp_buf);
345 
346  return 0;
347 }
348 
349 static int dss_read_seek(AVFormatContext *s, int stream_index,
350  int64_t timestamp, int flags)
351 {
352  DSSDemuxContext *ctx = s->priv_data;
353  int64_t ret, seekto;
355  int offset;
356 
358  seekto = timestamp / 264 * 41 / 506 * 512;
359  else
360  seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
361 
362  if (seekto < 0)
363  seekto = 0;
364 
365  seekto += ctx->dss_header_size;
366 
367  ret = avio_seek(s->pb, seekto, SEEK_SET);
368  if (ret < 0)
369  return ret;
370 
372  ctx->swap = !!(header[0] & 0x80);
373  offset = 2*header[1] + 2*ctx->swap;
375  return AVERROR_INVALIDDATA;
377  ctx->counter = 0;
379  } else {
380  ctx->counter = DSS_BLOCK_SIZE - offset;
382  }
383  ctx->dss_sp_swap_byte = -1;
384  return 0;
385 }
386 
387 
389  .name = "dss",
390  .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
391  .priv_data_size = sizeof(DSSDemuxContext),
397  .extensions = "dss"
398 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
DSSDemuxContext::audio_codec
unsigned int audio_codec
Definition: dss.c:51
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
DSS_HEAD_OFFSET_COMMENT
#define DSS_HEAD_OFFSET_COMMENT
Definition: dss.c:41
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
dss_skip_audio_header
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:184
dss_723_1_read_packet
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:275
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
DSS_HEAD_OFFSET_ACODEC
#define DSS_HEAD_OFFSET_ACODEC
Definition: dss.c:37
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
DSS_ACODEC_DSS_SP
#define DSS_ACODEC_DSS_SP
Definition: dss.c:38
src
#define src
Definition: vp8dsp.c:254
AVFormatContext::audio_codec
AVCodec * audio_codec
Forced audio codec.
Definition: avformat.h:1819
dss_read_seek
static int dss_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dss.c:349
AVInputFormat
Definition: avformat.h:640
DSS_FRAME_SIZE
#define DSS_FRAME_SIZE
Definition: dss.c:46
DSS_AUTHOR_SIZE
#define DSS_AUTHOR_SIZE
Definition: dss.c:31
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
DSS_HEAD_OFFSET_AUTHOR
#define DSS_HEAD_OFFSET_AUTHOR
Definition: dss.c:30
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
dss_read_metadata_date
static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset, const char *key)
Definition: dss.c:70
ctx
AVFormatContext * ctx
Definition: movenc.c:48
DSS_BLOCK_SIZE
#define DSS_BLOCK_SIZE
Definition: dss.c:44
DSS_COMMENT_SIZE
#define DSS_COMMENT_SIZE
Definition: dss.c:42
key
const char * key
Definition: hwcontext_opencl.c:168
version
int version
Definition: avisynth_c.h:858
dss_sp_read_packet
static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:216
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
frame_size
static const uint8_t frame_size[4]
Definition: dss.c:48
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: avcodec.h:616
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
DSSDemuxContext::dss_sp_buf
int8_t * dss_sp_buf
Definition: dss.c:55
DSSDemuxContext::counter
int counter
Definition: dss.c:52
DSSDemuxContext
Definition: dss.c:50
dss_read_close
static int dss_read_close(AVFormatContext *s)
Definition: dss.c:340
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
DSS_ACODEC_G723_1
#define DSS_ACODEC_G723_1
Definition: dss.c:39
ff_dss_demuxer
AVInputFormat ff_dss_demuxer
Definition: dss.c:388
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
byte
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:95
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4910
DSSDemuxContext::packet_size
int packet_size
Definition: dss.c:57
size
int size
Definition: twinvq_data.h:11134
DSSDemuxContext::dss_sp_swap_byte
int dss_sp_swap_byte
Definition: dss.c:54
header
static const uint8_t header[24]
Definition: sdr2.c:67
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
DSS_HEAD_OFFSET_END_TIME
#define DSS_HEAD_OFFSET_END_TIME
Definition: dss.c:34
DSS_TIME_SIZE
#define DSS_TIME_SIZE
Definition: dss.c:35
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
dss_read_metadata_string
static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset, unsigned int size, const char *key)
Definition: dss.c:93
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_CODEC_ID_DSS_SP
@ AV_CODEC_ID_DSS_SP
Definition: avcodec.h:630
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:870
bswap.h
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
dss_read_packet
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:330
avformat.h
DSS_AUDIO_BLOCK_HEADER_SIZE
#define DSS_AUDIO_BLOCK_HEADER_SIZE
Definition: dss.c:45
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
channel_layout.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVFormatContext::packet_size
unsigned int packet_size
Definition: avformat.h:1466
dss_sp_byte_swap
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *dst, const uint8_t *src)
Definition: dss.c:193
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
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:70
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1497
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4059
dss_read_header
static int dss_read_header(AVFormatContext *s)
Definition: dss.c:119
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
DSSDemuxContext::swap
int swap
Definition: dss.c:53
dss_probe
static int dss_probe(const AVProbeData *p)
Definition: dss.c:61
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
h
h
Definition: vp9dsp_template.c:2038
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:909
DSSDemuxContext::dss_header_size
int dss_header_size
Definition: dss.c:58
snprintf
#define snprintf
Definition: snprintf.h:34