FFmpeg  4.4.7
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 
25 #include "avformat.h"
26 #include "internal.h"
27 #include "avio_internal.h"
28 
29 #define DSS_HEAD_OFFSET_AUTHOR 0xc
30 #define DSS_AUTHOR_SIZE 16
31 
32 #define DSS_HEAD_OFFSET_START_TIME 0x26
33 #define DSS_HEAD_OFFSET_END_TIME 0x32
34 #define DSS_TIME_SIZE 12
35 
36 #define DSS_HEAD_OFFSET_ACODEC 0x2a4
37 #define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
38 #define DSS_ACODEC_G723_1 0x2 /* LP mode */
39 
40 #define DSS_HEAD_OFFSET_COMMENT 0x31e
41 #define DSS_COMMENT_SIZE 64
42 
43 #define DSS_BLOCK_SIZE 512
44 #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
45 #define DSS_FRAME_SIZE 42
46 
47 static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
48 
49 typedef struct DSSDemuxContext {
50  unsigned int audio_codec;
51  int counter;
52  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  } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
150  st->codecpar->sample_rate = 8000;
151  } else {
152  avpriv_request_sample(s, "Support for codec %x in DSS",
153  ctx->audio_codec);
154  return AVERROR_PATCHWELCOME;
155  }
156 
159  st->codecpar->channels = 1;
160 
161  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
162  st->start_time = 0;
163 
164  /* Jump over header */
165 
166  if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
167  return AVERROR(EIO);
168 
169  ctx->counter = 0;
170  ctx->swap = 0;
171 
172  return 0;
173 }
174 
176 {
177  DSSDemuxContext *ctx = s->priv_data;
178  AVIOContext *pb = s->pb;
179 
182 }
183 
185  uint8_t *dst, const uint8_t *src)
186 {
187  int i;
188 
189  if (ctx->swap) {
190  for (i = 3; i < DSS_FRAME_SIZE; i += 2)
191  dst[i] = src[i];
192 
193  for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
194  dst[i] = src[i + 4];
195 
196  dst[1] = ctx->dss_sp_swap_byte;
197  } else {
198  memcpy(dst, src, DSS_FRAME_SIZE);
199  ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
200  }
201 
202  /* make sure byte 40 is always 0 */
203  dst[DSS_FRAME_SIZE - 2] = 0;
204  ctx->swap ^= 1;
205 }
206 
208 {
209  DSSDemuxContext *ctx = s->priv_data;
210  AVStream *st = s->streams[0];
211  int read_size, ret, offset = 0, buff_offset = 0;
212  int64_t pos = avio_tell(s->pb);
213 
214  if (ctx->counter == 0)
216 
217  if (ctx->swap) {
218  read_size = DSS_FRAME_SIZE - 2;
219  buff_offset = 3;
220  } else
221  read_size = DSS_FRAME_SIZE;
222 
224 
226  if (ret < 0)
227  return ret;
228 
229  pkt->duration = 264;
230  pkt->pos = pos;
231  pkt->stream_index = 0;
232  s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
233 
234  if (ctx->counter < read_size) {
235  ret = avio_read(s->pb, ctx->dss_sp_buf + buff_offset,
236  ctx->counter);
237  if (ret < ctx->counter)
238  goto error_eof;
239 
240  offset = ctx->counter;
242  }
243  ctx->counter -= read_size;
244 
245  ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
246  read_size - offset);
247  if (ret < read_size - offset)
248  goto error_eof;
249 
250  dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
251 
252  if (ctx->dss_sp_swap_byte < 0) {
253  return AVERROR(EAGAIN);
254  }
255 
256  return pkt->size;
257 
258 error_eof:
259  return ret < 0 ? ret : AVERROR_EOF;
260 }
261 
263 {
264  DSSDemuxContext *ctx = s->priv_data;
265  AVStream *st = s->streams[0];
266  int size, byte, ret, offset;
267  int64_t pos = avio_tell(s->pb);
268 
269  if (ctx->counter == 0)
271 
272  /* We make one byte-step here. Don't forget to add offset. */
273  byte = avio_r8(s->pb);
274  if (byte == 0xff)
275  return AVERROR_INVALIDDATA;
276 
277  size = frame_size[byte & 3];
278 
279  ctx->packet_size = size;
280  ctx->counter--;
281 
282  ret = av_new_packet(pkt, size);
283  if (ret < 0)
284  return ret;
285  pkt->pos = pos;
286 
287  pkt->data[0] = byte;
288  offset = 1;
289  pkt->duration = 240;
290  s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
291 
292  pkt->stream_index = 0;
293 
294  if (ctx->counter < size) {
295  ret = avio_read(s->pb, pkt->data + offset,
296  ctx->counter);
297  if (ret < ctx->counter)
298  return ret < 0 ? ret : AVERROR_EOF;
299 
300  offset += ctx->counter;
301  size -= ctx->counter;
302  ctx->counter = 0;
304  }
305  ctx->counter -= size;
306 
307  ret = avio_read(s->pb, pkt->data + offset, size);
308  if (ret < size)
309  return ret < 0 ? ret : AVERROR_EOF;
310 
311  return pkt->size;
312 }
313 
315 {
316  DSSDemuxContext *ctx = s->priv_data;
317 
319  return dss_sp_read_packet(s, pkt);
320  else
321  return dss_723_1_read_packet(s, pkt);
322 }
323 
324 static int dss_read_seek(AVFormatContext *s, int stream_index,
325  int64_t timestamp, int flags)
326 {
327  DSSDemuxContext *ctx = s->priv_data;
328  int64_t ret, seekto;
330  int offset;
331 
333  seekto = timestamp / 264 * 41 / 506 * 512;
334  else
335  seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
336 
337  if (seekto < 0)
338  seekto = 0;
339 
340  seekto += ctx->dss_header_size;
341 
342  ret = avio_seek(s->pb, seekto, SEEK_SET);
343  if (ret < 0)
344  return ret;
345 
347  if (ret < 0)
348  return ret;
349  ctx->swap = !!(header[0] & 0x80);
350  offset = 2*header[1] + 2*ctx->swap;
352  return AVERROR_INVALIDDATA;
354  ctx->counter = 0;
356  } else {
357  ctx->counter = DSS_BLOCK_SIZE - offset;
359  }
360  ctx->dss_sp_swap_byte = -1;
361  return 0;
362 }
363 
364 
366  .name = "dss",
367  .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
368  .priv_data_size = sizeof(DSSDemuxContext),
373  .extensions = "dss"
374 };
uint8_t
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:682
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RL32
Definition: intreadwrite.h:146
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
audio channel layout utility functions
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
AVInputFormat ff_dss_demuxer
Definition: dss.c:365
#define DSS_ACODEC_G723_1
Definition: dss.c:38
#define DSS_AUTHOR_SIZE
Definition: dss.c:30
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:92
#define DSS_HEAD_OFFSET_ACODEC
Definition: dss.c:36
static int dss_probe(const AVProbeData *p)
Definition: dss.c:60
static void dss_sp_byte_swap(DSSDemuxContext *ctx, uint8_t *dst, const uint8_t *src)
Definition: dss.c:184
#define DSS_HEAD_OFFSET_END_TIME
Definition: dss.c:33
static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:175
static int dss_read_header(AVFormatContext *s)
Definition: dss.c:114
#define DSS_HEAD_OFFSET_COMMENT
Definition: dss.c:40
static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:314
static const uint8_t frame_size[4]
Definition: dss.c:47
#define DSS_HEAD_OFFSET_AUTHOR
Definition: dss.c:29
#define DSS_FRAME_SIZE
Definition: dss.c:45
static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dss.c:262
#define DSS_AUDIO_BLOCK_HEADER_SIZE
Definition: dss.c:44
#define DSS_COMMENT_SIZE
Definition: dss.c:41
#define DSS_ACODEC_DSS_SP
Definition: dss.c:37
#define DSS_TIME_SIZE
Definition: dss.c:34
static int dss_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dss.c:324
static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset, const char *key)
Definition: dss.c:69
#define DSS_BLOCK_SIZE
Definition: dss.c:43
double value
Definition: eval.c:100
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
#define AV_CH_LAYOUT_MONO
@ AV_CODEC_ID_G723_1
Definition: codec_id.h:476
@ AV_CODEC_ID_DSS_SP
Definition: codec_id.h:490
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#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:74
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
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
const char * key
int i
Definition: input.c:407
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:4945
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
version
Definition: libkvazaar.c:326
static const uint8_t header[24]
Definition: sdr2.c:67
#define snprintf
Definition: snprintf.h:34
unsigned int pos
Definition: spdifenc.c:412
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int channels
Audio only.
Definition: codec_par.h:166
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
unsigned int packet_size
Definition: avformat.h:1356
AVCodec * audio_codec
Forced audio codec.
Definition: avformat.h:1723
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
int packet_size
Definition: dss.c:56
int dss_header_size
Definition: dss.c:57
int dss_sp_swap_byte
Definition: dss.c:53
int8_t dss_sp_buf[DSS_FRAME_SIZE+1]
Definition: dss.c:54
int counter
Definition: dss.c:51
unsigned int audio_codec
Definition: dss.c:50
int swap
Definition: dss.c:52
#define av_free(p)
#define avpriv_request_sample(...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
int size
static const uint8_t offset[127][2]
Definition: vf_spp.c:107