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 "internal.h"
25 
26 typedef struct SDSContext {
27  uint8_t data[120];
28  int bit_depth;
29  int size;
30  void (*read_block)(const uint8_t *src, uint32_t *dst);
31 } SDSContext;
32 
33 static int sds_probe(const AVProbeData *p)
34 {
35  if (AV_RB32(p->buf) == 0xF07E0001 && p->buf[20] == 0xF7 &&
36  p->buf[6] >= 8 && p->buf[6] <= 28)
38  return 0;
39 }
40 
41 static void byte2_read(const uint8_t *src, uint32_t *dst)
42 {
43  int i;
44 
45  for (i = 0; i < 120; i += 2) {
46  unsigned sample = ((unsigned)src[i + 0] << 25) + ((unsigned)src[i + 1] << 18);
47 
48  dst[i / 2] = sample;
49  }
50 }
51 
52 static void byte3_read(const uint8_t *src, uint32_t *dst)
53 {
54  int i;
55 
56  for (i = 0; i < 120; i += 3) {
57  unsigned sample;
58 
59  sample = ((unsigned)src[i + 0] << 25) | ((unsigned)src[i + 1] << 18) | ((unsigned)src[i + 2] << 11);
60  dst[i / 3] = sample;
61  }
62 }
63 
64 static void byte4_read(const uint8_t *src, uint32_t *dst)
65 {
66  int i;
67 
68  for (i = 0; i < 120; i += 4) {
69  unsigned sample;
70 
71  sample = ((unsigned)src[i + 0] << 25) | ((unsigned)src[i + 1] << 18) | ((unsigned)src[i + 2] << 11) | ((unsigned)src[i + 3] << 4);
72  dst[i / 4] = sample;
73  }
74 }
75 
76 #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
77 
79 {
80  SDSContext *s = ctx->priv_data;
81  unsigned sample_period;
82  AVIOContext *pb = ctx->pb;
83  AVStream *st;
84 
85  st = avformat_new_stream(ctx, NULL);
86  if (!st)
87  return AVERROR(ENOMEM);
88 
89  avio_skip(pb, 4);
90  avio_skip(pb, 2);
91 
92  s->bit_depth = avio_r8(pb);
93  if (s->bit_depth < 8 || s->bit_depth > 28)
94  return AVERROR_INVALIDDATA;
95 
96  if (s->bit_depth < 14) {
98  s->size = 60 * 4;
99  } else if (s->bit_depth < 21) {
100  s->read_block = byte3_read;
101  s->size = 40 * 4;
102  } else {
103  s->read_block = byte4_read;
104  s->size = 30 * 4;
105  }
107 
108  sample_period = avio_rl24(pb);
109  sample_period = SDS_3BYTE_TO_INT_DECODE(sample_period);
110  avio_skip(pb, 11);
111 
113  st->codecpar->channels = 1;
114  st->codecpar->sample_rate = sample_period ? 1000000000 / sample_period : 16000;
115  st->duration = (avio_size(pb) - 21) / (127) * s->size / 4;
116 
117  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
118 
119  return 0;
120 }
121 
123 {
124  SDSContext *s = ctx->priv_data;
125  AVIOContext *pb = ctx->pb;
126  int64_t pos;
127  int ret;
128 
129  if (avio_feof(pb))
130  return AVERROR_EOF;
131 
132  pos = avio_tell(pb);
133  if (avio_rb16(pb) != 0xF07E)
134  return AVERROR_INVALIDDATA;
135  avio_skip(pb, 3);
136 
137  ret = av_new_packet(pkt, s->size);
138  if (ret < 0)
139  return ret;
140 
141  ret = avio_read(pb, s->data, 120);
142 
143  s->read_block(s->data, (uint32_t *)pkt->data);
144 
145  avio_skip(pb, 1); // checksum
146  if (avio_r8(pb) != 0xF7)
147  return AVERROR_INVALIDDATA;
148 
149  pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
150  pkt->stream_index = 0;
151  pkt->pos = pos;
152 
153  return ret;
154 }
155 
157  .name = "sds",
158  .long_name = NULL_IF_CONFIG_SMALL("MIDI Sample Dump Standard"),
159  .priv_data_size = sizeof(SDSContext),
163  .extensions = "sds",
165 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:339
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1500
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:4926
#define SDS_3BYTE_TO_INT_DECODE(x)
Definition: sdsdec.c:76
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:334
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:788
#define src
Definition: vp8dsp.c:254
#define sample
Format I/O context.
Definition: avformat.h:1358
uint8_t data[120]
Definition: sdsdec.c:27
uint8_t
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4499
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:87
uint8_t * data
Definition: avcodec.h:1480
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:650
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
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int bit_depth
Definition: sdsdec.c:28
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
static int sds_probe(const AVProbeData *p)
Definition: sdsdec.c:33
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1486
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
static int sds_read_header(AVFormatContext *ctx)
Definition: sdsdec.c:78
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
void(* read_block)(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:30
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:881
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static void byte2_read(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:41
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int sds_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: sdsdec.c:122
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:468
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:456
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
#define flags(name, subs,...)
Definition: cbs_av1.c:561
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:930
int sample_rate
Audio only.
Definition: avcodec.h:4078
Main libavformat public API header.
static void byte4_read(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:64
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1513
int size
Definition: sdsdec.c:29
void * priv_data
Format private data.
Definition: avformat.h:1386
static void byte3_read(const uint8_t *src, uint32_t *dst)
Definition: sdsdec.c:52
int channels
Audio only.
Definition: avcodec.h:4074
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:361
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:764
int stream_index
Definition: avcodec.h:1482
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
AVInputFormat ff_sds_demuxer
Definition: sdsdec.c:156
This structure stores compressed data.
Definition: avcodec.h:1457