FFmpeg
sdsdec.c
Go to the documentation of this file.
1 /*
2  * MIDI Sample Dump Standard format demuxer
3  * Copyright (c) 2017 Paul B Mahol
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/intreadwrite.h"
23 #include "avformat.h"
24 #include "demux.h"
25 #include "internal.h"
26 
27 typedef struct SDSContext {
28  uint8_t data[120];
29  int bit_depth;
30  int size;
31  void (*read_block)(const uint8_t *src, uint32_t *dst);
32 } SDSContext;
33 
34 static int sds_probe(const AVProbeData *p)
35 {
36  if (AV_RB32(p->buf) == 0xF07E0001 && p->buf[20] == 0xF7 &&
37  p->buf[6] >= 8 && p->buf[6] <= 28)
39  return 0;
40 }
41 
42 static void byte2_read(const uint8_t *src, uint32_t *dst)
43 {
44  int i;
45 
46  for (i = 0; i < 120; i += 2) {
47  unsigned sample = ((unsigned)src[i + 0] << 25) + ((unsigned)src[i + 1] << 18);
48 
49  dst[i / 2] = sample;
50  }
51 }
52 
53 static void byte3_read(const uint8_t *src, uint32_t *dst)
54 {
55  int i;
56 
57  for (i = 0; i < 120; i += 3) {
58  unsigned sample;
59 
60  sample = ((unsigned)src[i + 0] << 25) | ((unsigned)src[i + 1] << 18) | ((unsigned)src[i + 2] << 11);
61  dst[i / 3] = sample;
62  }
63 }
64 
65 static void byte4_read(const uint8_t *src, uint32_t *dst)
66 {
67  int i;
68 
69  for (i = 0; i < 120; i += 4) {
70  unsigned sample;
71 
72  sample = ((unsigned)src[i + 0] << 25) | ((unsigned)src[i + 1] << 18) | ((unsigned)src[i + 2] << 11) | ((unsigned)src[i + 3] << 4);
73  dst[i / 4] = sample;
74  }
75 }
76 
77 #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
78 
80 {
82  unsigned sample_period;
83  AVIOContext *pb = ctx->pb;
84  AVStream *st;
85 
87  if (!st)
88  return AVERROR(ENOMEM);
89 
90  avio_skip(pb, 4);
91  avio_skip(pb, 2);
92 
93  s->bit_depth = avio_r8(pb);
94  if (s->bit_depth < 8 || s->bit_depth > 28)
95  return AVERROR_INVALIDDATA;
96 
97  if (s->bit_depth < 14) {
98  s->read_block = byte2_read;
99  s->size = 60 * 4;
100  } else if (s->bit_depth < 21) {
101  s->read_block = byte3_read;
102  s->size = 40 * 4;
103  } else {
104  s->read_block = byte4_read;
105  s->size = 30 * 4;
106  }
108 
109  sample_period = avio_rl24(pb);
110  sample_period = SDS_3BYTE_TO_INT_DECODE(sample_period);
111  avio_skip(pb, 11);
112 
114  st->codecpar->ch_layout.nb_channels = 1;
115  st->codecpar->sample_rate = sample_period ? 1000000000 / sample_period : 16000;
116  st->duration = av_rescale((avio_size(pb) - 21) / 127, s->size, 4);
117 
118  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
119 
120  return 0;
121 }
122 
124 {
126  AVIOContext *pb = ctx->pb;
127  int64_t pos;
128  int ret;
129 
130  if (avio_feof(pb))
131  return AVERROR_EOF;
132 
133  pos = avio_tell(pb);
134  if (avio_rb16(pb) != 0xF07E)
135  return AVERROR_INVALIDDATA;
136  avio_skip(pb, 3);
137 
138  ret = av_new_packet(pkt, s->size);
139  if (ret < 0)
140  return ret;
141 
142  ret = avio_read(pb, s->data, 120);
143 
144  s->read_block(s->data, (uint32_t *)pkt->data);
145 
146  avio_skip(pb, 1); // checksum
147  if (avio_r8(pb) != 0xF7)
148  return AVERROR_INVALIDDATA;
149 
151  pkt->stream_index = 0;
152  pkt->pos = pos;
153 
154  return ret;
155 }
156 
158  .p.name = "sds",
159  .p.long_name = NULL_IF_CONFIG_SMALL("MIDI Sample Dump Standard"),
160  .p.extensions = "sds",
161  .p.flags = AVFMT_GENERIC_INDEX,
162  .priv_data_size = sizeof(SDSContext),
166 };
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
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
sds_probe
static int sds_probe(const AVProbeData *p)
Definition: sdsdec.c:34
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
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
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
SDSContext
Definition: sdsdec.c:27
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:595
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
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
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
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SDSContext::read_block
void(* read_block)(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:31
SDSContext::size
int size
Definition: sdsdec.c:30
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
byte3_read
static void byte3_read(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:53
byte2_read
static void byte2_read(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:42
NULL
#define NULL
Definition: coverity.c:32
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1342
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
SDSContext::data
uint8_t data[120]
Definition: sdsdec.c:28
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
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
byte4_read
static void byte4_read(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:65
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
sample
#define sample
Definition: flacdsp_template.c:44
AV_RB32
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_RB32
Definition: bytestream.h:96
SDSContext::bit_depth
int bit_depth
Definition: sdsdec.c:29
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
SDS_3BYTE_TO_INT_DECODE
#define SDS_3BYTE_TO_INT_DECODE(x)
Definition: sdsdec.c:77
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:722
demux.h
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:746
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AV_CODEC_ID_PCM_U32LE
@ AV_CODEC_ID_PCM_U32LE
Definition: codec_id.h:344
ff_sds_demuxer
const FFInputFormat ff_sds_demuxer
Definition: sdsdec.c:157
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
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
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
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
sds_read_packet
static int sds_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: sdsdec.c:123
sds_read_header
static int sds_read_header(AVFormatContext *ctx)
Definition: sdsdec.c:79
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
src
#define src
Definition: vp8dsp.c:248
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346