Line | Branch | Exec | Source |
---|---|---|---|
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 | 7186 | static int sds_probe(const AVProbeData *p) | |
35 | { | ||
36 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7186 | if (AV_RB32(p->buf) == 0xF07E0001 && p->buf[20] == 0xF7 && |
37 | ✗ | p->buf[6] >= 8 && p->buf[6] <= 28) | |
38 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
39 | 7186 | 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 | |||
79 | ✗ | static int sds_read_header(AVFormatContext *ctx) | |
80 | { | ||
81 | ✗ | SDSContext *s = ctx->priv_data; | |
82 | unsigned sample_period; | ||
83 | ✗ | AVIOContext *pb = ctx->pb; | |
84 | AVStream *st; | ||
85 | |||
86 | ✗ | st = avformat_new_stream(ctx, NULL); | |
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 | } | ||
107 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_PCM_U32LE; | |
108 | |||
109 | ✗ | sample_period = avio_rl24(pb); | |
110 | ✗ | sample_period = SDS_3BYTE_TO_INT_DECODE(sample_period); | |
111 | ✗ | avio_skip(pb, 11); | |
112 | |||
113 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
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 | |||
123 | ✗ | static int sds_read_packet(AVFormatContext *ctx, AVPacket *pkt) | |
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 | |||
157 | const FFInputFormat ff_sds_demuxer = { | ||
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), | ||
163 | .read_probe = sds_probe, | ||
164 | .read_header = sds_read_header, | ||
165 | .read_packet = sds_read_packet, | ||
166 | }; | ||
167 |