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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 
26 #include "avformat.h"
27 #include "demux.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 
59 
60 static int dss_probe(const AVProbeData *p)
61 {
62  if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
63  && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
64  return 0;
65 
66  return AVPROBE_SCORE_MAX;
67 }
68 
69 static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
70  const char *key)
71 {
72  AVIOContext *pb = s->pb;
73  char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
74  int y, month, d, h, minute, sec;
75  int ret;
76 
77  avio_seek(pb, offset, SEEK_SET);
78 
79  ret = avio_read(s->pb, string, DSS_TIME_SIZE);
80  if (ret < DSS_TIME_SIZE)
81  return ret < 0 ? ret : AVERROR_EOF;
82 
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);
90 }
91 
93  unsigned int size, const char *key)
94 {
95  AVIOContext *pb = s->pb;
96  char *value;
97  int ret;
98 
99  avio_seek(pb, offset, SEEK_SET);
100 
101  value = av_mallocz(size + 1);
102  if (!value)
103  return AVERROR(ENOMEM);
104 
105  ret = avio_read(s->pb, value, size);
106  if (ret < size) {
107  av_free(value);
108  return ret < 0 ? ret : AVERROR_EOF;
109  }
110 
111  return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
112 }
113 
115 {
116  DSSDemuxContext *ctx = s->priv_data;
117  AVIOContext *pb = s->pb;
118  AVStream *st;
119  int ret, version;
120 
121  st = avformat_new_stream(s, NULL);
122  if (!st)
123  return AVERROR(ENOMEM);
124 
125  version = avio_r8(pb);
126  ctx->dss_header_size = version * DSS_BLOCK_SIZE;
127 
129  DSS_AUTHOR_SIZE, "author");
130  if (ret)
131  return ret;
132 
134  if (ret)
135  return ret;
136 
138  DSS_COMMENT_SIZE, "comment");
139  if (ret)
140  return ret;
141 
142  avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
143  ctx->audio_codec = avio_r8(pb);
144 
147  st->codecpar->sample_rate = 11025;
148  s->bit_rate = 8 * (DSS_FRAME_SIZE - 1) * st->codecpar->sample_rate
149  * 512 / (506 * 264);
150  } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
152  st->codecpar->sample_rate = 8000;
153  } else {
154  avpriv_request_sample(s, "Support for codec %x in DSS",
155  ctx->audio_codec);
156  return AVERROR_PATCHWELCOME;
157  }
158 
161 
162  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
163  st->start_time = 0;
164 
165  /* Jump over header */
166 
167  if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
168  return AVERROR(EIO);
169 
170  ctx->counter = 0;
171  ctx->swap = 0;
172 
173  return 0;
174 }
175 
177 {
178  DSSDemuxContext *ctx = s->priv_data;
179  AVIOContext *pb = s->pb;
180 
183 }
184 
185 static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
186 {
187  int i;
188 
189  if (ctx->swap) {
190  for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
191  data[i] = data[i + 4];
192 
193  /* Zero the padding. */
194  data[DSS_FRAME_SIZE] = 0;
195  data[1] = ctx->dss_sp_swap_byte;
196  } else {
197  ctx->dss_sp_swap_byte = data[DSS_FRAME_SIZE - 2];
198  }
199 
200  /* make sure byte 40 is always 0 */
201  data[DSS_FRAME_SIZE - 2] = 0;
202  ctx->swap ^= 1;
203 }
204 
206 {
207  DSSDemuxContext *ctx = s->priv_data;
208  int read_size, ret, offset = 0, buff_offset = 0;
209  int64_t pos = avio_tell(s->pb);
210 
211  if (ctx->counter == 0)
213 
214  if (ctx->swap) {
215  read_size = DSS_FRAME_SIZE - 2;
216  buff_offset = 3;
217  } else
218  read_size = DSS_FRAME_SIZE;
219 
221  if (ret < 0)
222  return ret;
223 
224  pkt->duration = 264;
225  pkt->pos = pos;
226  pkt->stream_index = 0;
227 
228  if (ctx->counter < read_size) {
229  ret = avio_read(s->pb, pkt->data + buff_offset,
230  ctx->counter);
231  if (ret < ctx->counter)
232  goto error_eof;
233 
234  offset = ctx->counter;
236  }
237  ctx->counter -= read_size;
238 
239  /* This will write one byte into pkt's padding if buff_offset == 3 */
240  ret = avio_read(s->pb, pkt->data + offset + buff_offset,
241  read_size - offset);
242  if (ret < read_size - offset)
243  goto error_eof;
244 
246 
247  if (ctx->dss_sp_swap_byte < 0) {
248  return AVERROR(EAGAIN);
249  }
250 
251  return 0;
252 
253 error_eof:
254  return ret < 0 ? ret : AVERROR_EOF;
255 }
256 
258 {
259  DSSDemuxContext *ctx = s->priv_data;
260  AVStream *st = s->streams[0];
261  int size, byte, ret, offset;
262  int64_t pos = avio_tell(s->pb);
263 
264  if (ctx->counter == 0)
266 
267  /* We make one byte-step here. Don't forget to add offset. */
268  byte = avio_r8(s->pb);
269  if (byte == 0xff)
270  return AVERROR_INVALIDDATA;
271 
272  size = frame_size[byte & 3];
273 
274  ctx->packet_size = size;
275  ctx->counter--;
276 
278  if (ret < 0)
279  return ret;
280  pkt->pos = pos;
281 
282  pkt->data[0] = byte;
283  offset = 1;
284  pkt->duration = 240;
285  s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
286 
287  pkt->stream_index = 0;
288 
289  if (ctx->counter < size) {
290  ret = avio_read(s->pb, pkt->data + offset,
291  ctx->counter);
292  if (ret < ctx->counter)
293  return ret < 0 ? ret : AVERROR_EOF;
294 
295  offset += ctx->counter;
296  size -= ctx->counter;
297  ctx->counter = 0;
299  }
300  ctx->counter -= size;
301 
302  ret = avio_read(s->pb, pkt->data + offset, size);
303  if (ret < size)
304  return ret < 0 ? ret : AVERROR_EOF;
305 
306  return 0;
307 }
308 
310 {
311  DSSDemuxContext *ctx = s->priv_data;
312 
314  return dss_sp_read_packet(s, pkt);
315  else
316  return dss_723_1_read_packet(s, pkt);
317 }
318 
319 static int dss_read_seek(AVFormatContext *s, int stream_index,
320  int64_t timestamp, int flags)
321 {
322  DSSDemuxContext *ctx = s->priv_data;
323  int64_t ret, seekto;
325  int offset;
326 
328  seekto = timestamp / 264 * 41 / 506 * 512;
329  else
330  seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
331 
332  if (seekto < 0)
333  seekto = 0;
334 
335  seekto += ctx->dss_header_size;
336 
337  ret = avio_seek(s->pb, seekto, SEEK_SET);
338  if (ret < 0)
339  return ret;
340 
342  ctx->swap = !!(header[0] & 0x80);
343  offset = 2*header[1] + 2*ctx->swap;
345  return AVERROR_INVALIDDATA;
347  ctx->counter = 0;
349  } else {
350  ctx->counter = DSS_BLOCK_SIZE - offset;
352  }
353  ctx->dss_sp_swap_byte = -1;
354  return 0;
355 }
356 
357 
359  .p.name = "dss",
360  .p.long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
361  .p.extensions = "dss",
362  .priv_data_size = sizeof(DSSDemuxContext),
367 };
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
DSS_HEAD_OFFSET_COMMENT
#define DSS_HEAD_OFFSET_COMMENT
Definition: dss.c:41
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:539
data
const char data[16]
Definition: mxf.c:149
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
dss_skip_audio_header
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:176
dss_723_1_read_packet
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:257
DSS_HEAD_OFFSET_ACODEC
#define DSS_HEAD_OFFSET_ACODEC
Definition: dss.c:37
dss_sp_byte_swap
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *data)
Definition: dss.c:185
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:867
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
DSS_ACODEC_DSS_SP
#define DSS_ACODEC_DSS_SP
Definition: dss.c:38
AV_DICT_DONT_STRDUP_VAL
#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
dss_read_seek
static int dss_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dss.c:319
pkt
AVPacket * pkt
Definition: movenc.c:60
DSS_FRAME_SIZE
#define DSS_FRAME_SIZE
Definition: dss.c:46
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
DSS_AUTHOR_SIZE
#define DSS_AUTHOR_SIZE
Definition: dss.c:31
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
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: packet.c:98
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:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
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:69
ctx
AVFormatContext * ctx
Definition: movenc.c:49
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:189
dss_sp_read_packet
static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:205
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
frame_size
static const uint8_t frame_size[4]
Definition: dss.c:48
AV_CODEC_ID_G723_1
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:498
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
DSSDemuxContext::counter
int counter
Definition: dss.c:52
DSSDemuxContext
Definition: dss.c:50
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVFormatContext::audio_codec
const struct AVCodec * audio_codec
Forced audio codec.
Definition: avformat.h:1833
DSS_ACODEC_G723_1
#define DSS_ACODEC_G723_1
Definition: dss.c:39
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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:94
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:99
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
DSSDemuxContext::packet_size
int packet_size
Definition: dss.c:56
size
int size
Definition: twinvq_data.h:10344
DSSDemuxContext::dss_sp_swap_byte
int dss_sp_swap_byte
Definition: dss.c:54
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
header
static const uint8_t header[24]
Definition: sdr2.c:68
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
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
DSS_HEAD_OFFSET_END_TIME
#define DSS_HEAD_OFFSET_END_TIME
Definition: dss.c:34
version
version
Definition: libkvazaar.c:321
DSS_TIME_SIZE
#define DSS_TIME_SIZE
Definition: dss.c:35
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
dss_read_metadata_string
static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset, unsigned int size, const char *key)
Definition: dss.c:92
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
AV_CODEC_ID_DSS_SP
@ AV_CODEC_ID_DSS_SP
Definition: codec_id.h:512
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:256
demux.h
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
dss_read_packet
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:309
pos
unsigned int pos
Definition: spdifenc.c:414
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:92
channel_layout.h
ff_dss_demuxer
const FFInputFormat ff_dss_demuxer
Definition: dss.c:358
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AVFormatContext::packet_size
unsigned int packet_size
Definition: avformat.h:1444
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:392
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
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:88
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
dss_read_header
static int dss_read_header(AVFormatContext *s)
Definition: dss.c:114
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
DSSDemuxContext::swap
int swap
Definition: dss.c:53
dss_probe
static int dss_probe(const AVProbeData *p)
Definition: dss.c:60
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2070
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:797
DSSDemuxContext::dss_header_size
int dss_header_size
Definition: dss.c:57
snprintf
#define snprintf
Definition: snprintf.h:34