1 /*
2 * MCA demuxer
3 * Copyright (c) 2020 Zixing Liu
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 "avio_internal.h"
25 #include "internal.h"
26
27 typedef struct MCADemuxContext {
28 uint32_t block_count;
29 uint16_t block_size;
30 uint32_t current_block;
31 uint32_t data_start;
32 uint32_t samples_per_block;
33 } MCADemuxContext;
34
probe(const AVProbeData * p)35 static int probe(const AVProbeData *p)
36 {
37 if (AV_RL32(p->buf) == MKTAG('M', 'A', 'D', 'P') &&
38 AV_RL16(p->buf + 4) <= 0x5)
39 return AVPROBE_SCORE_MAX / 3 * 2;
40 return 0;
41 }
42
read_header(AVFormatContext * s)43 static int read_header(AVFormatContext *s)
44 {
45 AVStream *st;
46 MCADemuxContext *m = s->priv_data;
47 AVCodecParameters *par;
48 int64_t file_size = avio_size(s->pb);
49 uint16_t version = 0;
50 uint32_t header_size, data_size, data_offset, loop_start, loop_end,
51 nb_samples, nb_metadata, coef_offset = 0;
52 int ch, ret;
53 int64_t ret_size;
54
55 st = avformat_new_stream(s, NULL);
56 if (!st)
57 return AVERROR(ENOMEM);
58 par = st->codecpar;
59 par->codec_type = AVMEDIA_TYPE_AUDIO;
60
61 // parse file headers
62 avio_skip(s->pb, 0x4); // skip the file magic
63 version = avio_rl16(s->pb);
64 avio_skip(s->pb, 0x2); // padding
65 par->channels = avio_r8(s->pb);
66 avio_skip(s->pb, 0x1); // padding
67 m->block_size = avio_rl16(s->pb);
68 nb_samples = avio_rl32(s->pb);
69 par->sample_rate = avio_rl32(s->pb);
70 loop_start = avio_rl32(s->pb);
71 loop_end = avio_rl32(s->pb);
72 header_size = avio_rl32(s->pb);
73 data_size = avio_rl32(s->pb);
74 avio_skip(s->pb, 0x4);
75 nb_metadata = avio_rl16(s->pb);
76 avio_skip(s->pb, 0x2); // unknown u16 field
77
78 // samples per frame = 14; frame size = 8 (2^3)
79 m->samples_per_block = (m->block_size * 14) >> 3;
80
81 if (m->samples_per_block < 1)
82 return AVERROR_INVALIDDATA;
83
84 m->block_count = nb_samples / m->samples_per_block;
85 st->duration = nb_samples;
86
87 // sanity checks
88 if (!par->channels || par->sample_rate <= 0
89 || loop_start > loop_end || m->block_count < 1)
90 return AVERROR_INVALIDDATA;
91 if ((ret = av_dict_set_int(&s->metadata, "loop_start",
92 av_rescale(loop_start, AV_TIME_BASE,
93 par->sample_rate), 0)) < 0)
94 return ret;
95 if ((ret = av_dict_set_int(&s->metadata, "loop_end",
96 av_rescale(loop_end, AV_TIME_BASE,
97 par->sample_rate), 0)) < 0)
98 return ret;
99 if ((32 + 4 + m->block_size) > (INT_MAX / par->channels) ||
100 (32 + 4 + m->block_size) * par->channels > INT_MAX - 8)
101 return AVERROR_INVALIDDATA;
102 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
103
104 if (version <= 4) {
105 // version <= 4 needs to use the file size to calculate the offsets
106 if (file_size < 0) {
107 return AVERROR(EIO);
108 }
109 if (file_size - data_size > UINT32_MAX)
110 return AVERROR_INVALIDDATA;
111 m->data_start = file_size - data_size;
112 if (version <= 3) {
113 nb_metadata = 0;
114 // header_size is not available or incorrect in older versions
115 header_size = m->data_start;
116 }
117 } else if (version == 5) {
118 // read data_start location from the header
119 if (0x30 * par->channels + 0x4 > header_size)
120 return AVERROR_INVALIDDATA;
121 data_offset = header_size - 0x30 * par->channels - 0x4;
122 if ((ret_size = avio_seek(s->pb, data_offset, SEEK_SET)) < 0)
123 return ret_size;
124 m->data_start = avio_rl32(s->pb);
125 // check if the metadata is reasonable
126 if (file_size > 0 && (int64_t)m->data_start + data_size > file_size) {
127 // the header is broken beyond repair
128 if ((int64_t)header_size + data_size > file_size) {
129 av_log(s, AV_LOG_ERROR,
130 "MCA metadata corrupted, unable to determine the data offset.\n");
131 return AVERROR_INVALIDDATA;
132 }
133 // recover the data_start information from the data size
134 av_log(s, AV_LOG_WARNING,
135 "Incorrect header size found in metadata, "
136 "header size approximated from the data size\n");
137 if (file_size - data_offset > UINT32_MAX)
138 return AVERROR_INVALIDDATA;
139 m->data_start = file_size - data_size;
140 }
141 } else {
142 avpriv_request_sample(s, "version %d", version);
143 return AVERROR_PATCHWELCOME;
144 }
145
146 // coefficient alignment = 0x30; metadata size = 0x14
147 if (0x30 * par->channels + nb_metadata * 0x14 > header_size)
148 return AVERROR_INVALIDDATA;
149 coef_offset =
150 header_size - 0x30 * par->channels + nb_metadata * 0x14;
151
152 st->start_time = 0;
153 par->codec_id = AV_CODEC_ID_ADPCM_THP_LE;
154
155 ret = ff_alloc_extradata(st->codecpar, 32 * par->channels);
156 if (ret < 0)
157 return ret;
158
159 if ((ret_size = avio_seek(s->pb, coef_offset, SEEK_SET)) < 0)
160 return ret_size;
161 for (ch = 0; ch < par->channels; ch++) {
162 if ((ret = ffio_read_size(s->pb, par->extradata + ch * 32, 32)) < 0)
163 return ret;
164 // 0x30 (alignment) - 0x20 (actual size, 32) = 0x10 (padding)
165 avio_skip(s->pb, 0x10);
166 }
167
168 // seek to the beginning of the adpcm data
169 // there are some files where the adpcm audio data is not immediately after the header
170 if ((ret_size = avio_seek(s->pb, m->data_start, SEEK_SET)) < 0)
171 return ret_size;
172
173 return 0;
174 }
175
read_packet(AVFormatContext * s,AVPacket * pkt)176 static int read_packet(AVFormatContext *s, AVPacket *pkt)
177 {
178 AVCodecParameters *par = s->streams[0]->codecpar;
179 MCADemuxContext *m = s->priv_data;
180 uint16_t size = m->block_size;
181 uint32_t samples = m->samples_per_block;
182 int ret = 0;
183
184 if (avio_feof(s->pb))
185 return AVERROR_EOF;
186 m->current_block++;
187 if (m->current_block > m->block_count)
188 return AVERROR_EOF;
189
190 if ((ret = av_get_packet(s->pb, pkt, size * par->channels)) < 0)
191 return ret;
192 pkt->duration = samples;
193 pkt->stream_index = 0;
194
195 return 0;
196 }
197
read_seek(AVFormatContext * s,int stream_index,int64_t timestamp,int flags)198 static int read_seek(AVFormatContext *s, int stream_index,
199 int64_t timestamp, int flags)
200 {
201 AVStream *st = s->streams[stream_index];
202 MCADemuxContext *m = s->priv_data;
203 int64_t ret = 0;
204
205 if (timestamp < 0)
206 timestamp = 0;
207 timestamp /= m->samples_per_block;
208 if (timestamp >= m->block_count)
209 timestamp = m->block_count - 1;
210 ret = avio_seek(s->pb, m->data_start + timestamp * m->block_size *
211 st->codecpar->channels, SEEK_SET);
212 if (ret < 0)
213 return ret;
214
215 m->current_block = timestamp;
216 ff_update_cur_dts(s, st, timestamp * m->samples_per_block);
217 return 0;
218 }
219
220 AVInputFormat ff_mca_demuxer = {
221 .name = "mca",
222 .long_name = NULL_IF_CONFIG_SMALL("MCA Audio Format"),
223 .priv_data_size = sizeof(MCADemuxContext),
224 .read_probe = probe,
225 .read_header = read_header,
226 .read_packet = read_packet,
227 .read_seek = read_seek,
228 .extensions = "mca",
229 };
230