FFmpeg
Loading...
Searching...
No Matches
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
23#include "avformat.h"
24#include "demux.h"
25#include "internal.h"
26
27typedef struct SDSContext {
28 uint8_t data[120];
30 int size;
31 void (*read_block)(const uint8_t *src, uint32_t *dst);
33
34static 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
42static 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
53static 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
65static 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{
81 SDSContext *s = ctx->priv_data;
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)
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
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
119
120 return 0;
121}
122
124{
125 SDSContext *s = ctx->priv_data;
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
150 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
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};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFInputFormat ff_sds_demuxer
Definition sdsdec.c:157
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl24(AVIOContext *s)
Definition aviobuf.c:725
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define sample
@ AV_CODEC_ID_PCM_U32LE
Definition codec_id.h:340
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
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
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RB32(p)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static void byte3_read(const uint8_t *src, uint32_t *dst)
Definition sdsdec.c:53
static int sds_probe(const AVProbeData *p)
Definition sdsdec.c:34
static int sds_read_header(AVFormatContext *ctx)
Definition sdsdec.c:79
static void byte2_read(const uint8_t *src, uint32_t *dst)
Definition sdsdec.c:42
#define SDS_3BYTE_TO_INT_DECODE(x)
Definition sdsdec.c:77
static int sds_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition sdsdec.c:123
static void byte4_read(const uint8_t *src, uint32_t *dst)
Definition sdsdec.c:65
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int size
Definition sdsdec.c:30
uint8_t data[120]
Definition sdsdec.c:28
void(* read_block)(const uint8_t *src, uint32_t *dst)
Definition sdsdec.c:31
int bit_depth
Definition sdsdec.c:29
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49