FFmpeg
Loading...
Searching...
No Matches
mca.c
Go to the documentation of this file.
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
23#include "avformat.h"
24#include "avio_internal.h"
25#include "demux.h"
26#include "internal.h"
27
28typedef struct MCADemuxContext {
29 uint32_t block_count;
30 uint16_t block_size;
31 uint32_t current_block;
32 uint32_t data_start;
35
36static int probe(const AVProbeData *p)
37{
38 if (AV_RL32(p->buf) == MKTAG('M', 'A', 'D', 'P') &&
39 AV_RL16(p->buf + 4) <= 0x5)
40 return AVPROBE_SCORE_MAX / 3 * 2;
41 return 0;
42}
43
45{
46 AVStream *st;
47 MCADemuxContext *m = s->priv_data;
49 int64_t file_size = avio_size(s->pb);
50 uint16_t version = 0;
51 uint32_t header_size, data_size, data_offset, loop_start, loop_end,
52 nb_samples, nb_metadata, coef_offset = 0;
53 int ch, ret;
54 int64_t ret_size;
55
57 if (!st)
58 return AVERROR(ENOMEM);
59 par = st->codecpar;
61
62 // parse file headers
63 avio_skip(s->pb, 0x4); // skip the file magic
64 version = avio_rl16(s->pb);
65 avio_skip(s->pb, 0x2); // padding
66 par->ch_layout.nb_channels = avio_r8(s->pb);
67 avio_skip(s->pb, 0x1); // padding
68 m->block_size = avio_rl16(s->pb);
69 nb_samples = avio_rl32(s->pb);
70 par->sample_rate = avio_rl32(s->pb);
71 loop_start = avio_rl32(s->pb);
72 loop_end = avio_rl32(s->pb);
73 header_size = avio_rl32(s->pb);
74 data_size = avio_rl32(s->pb);
75 avio_skip(s->pb, 0x4);
76 nb_metadata = avio_rl16(s->pb);
77 avio_skip(s->pb, 0x2); // unknown u16 field
78
79 // samples per frame = 14; frame size = 8 (2^3)
80 m->samples_per_block = (m->block_size * 14) >> 3;
81
82 if (m->samples_per_block < 1)
84
85 m->block_count = nb_samples / m->samples_per_block;
86 st->duration = nb_samples;
87
88 // sanity checks
89 if (!par->ch_layout.nb_channels || par->sample_rate <= 0
90 || loop_start > loop_end || m->block_count < 1)
92 if ((ret = av_dict_set_int(&s->metadata, "loop_start",
93 av_rescale(loop_start, AV_TIME_BASE,
94 par->sample_rate), 0)) < 0)
95 return ret;
96 if ((ret = av_dict_set_int(&s->metadata, "loop_end",
97 av_rescale(loop_end, AV_TIME_BASE,
98 par->sample_rate), 0)) < 0)
99 return ret;
100 if ((32 + 4 + m->block_size) > (INT_MAX / par->ch_layout.nb_channels) ||
101 (32 + 4 + m->block_size) * par->ch_layout.nb_channels > INT_MAX - 8)
102 return AVERROR_INVALIDDATA;
103 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
104
105 if (version <= 4) {
106 // version <= 4 needs to use the file size to calculate the offsets
107 if (file_size < 0) {
108 return AVERROR_INVALIDDATA;
109 }
110 if (file_size - data_size > UINT32_MAX)
111 return AVERROR_INVALIDDATA;
112 m->data_start = file_size - data_size;
113 if (version <= 3) {
114 nb_metadata = 0;
115 // header_size is not available or incorrect in older versions
116 header_size = m->data_start;
117 }
118 } else if (version == 5) {
119 // read data_start location from the header
120 if (0x30 * par->ch_layout.nb_channels + 0x4 > header_size)
121 return AVERROR_INVALIDDATA;
122 data_offset = header_size - 0x30 * par->ch_layout.nb_channels - 0x4;
123 if ((ret_size = avio_seek(s->pb, data_offset, SEEK_SET)) < 0)
124 return ret_size;
125 m->data_start = avio_rl32(s->pb);
126 // check if the metadata is reasonable
127 if (file_size > 0 && (int64_t)m->data_start + data_size > file_size) {
128 // the header is broken beyond repair
129 if ((int64_t)header_size + data_size > file_size) {
131 "MCA metadata corrupted, unable to determine the data offset.\n");
132 return AVERROR_INVALIDDATA;
133 }
134 // recover the data_start information from the data size
136 "Incorrect header size found in metadata, "
137 "header size approximated from the data size\n");
138 if (file_size - data_offset > UINT32_MAX)
139 return AVERROR_INVALIDDATA;
140 m->data_start = file_size - data_size;
141 }
142 } else {
143 avpriv_request_sample(s, "version %d", version);
145 }
146
147 // coefficient alignment = 0x30; metadata size = 0x14
148 if (0x30 * par->ch_layout.nb_channels + nb_metadata * 0x14 > header_size)
149 return AVERROR_INVALIDDATA;
150 coef_offset =
151 header_size - 0x30 * par->ch_layout.nb_channels + nb_metadata * 0x14;
152
153 st->start_time = 0;
155
156 ret = ff_alloc_extradata(st->codecpar, 32 * par->ch_layout.nb_channels);
157 if (ret < 0)
158 return ret;
159
160 if ((ret_size = avio_seek(s->pb, coef_offset, SEEK_SET)) < 0)
161 return ret_size;
162 for (ch = 0; ch < par->ch_layout.nb_channels; ch++) {
163 if ((ret = ffio_read_size(s->pb, par->extradata + ch * 32, 32)) < 0)
164 return ret;
165 // 0x30 (alignment) - 0x20 (actual size, 32) = 0x10 (padding)
166 avio_skip(s->pb, 0x10);
167 }
168
169 // seek to the beginning of the adpcm data
170 // there are some files where the adpcm audio data is not immediately after the header
171 if ((ret_size = avio_seek(s->pb, m->data_start, SEEK_SET)) < 0)
172 return ret_size;
173
174 return 0;
175}
176
178{
179 AVCodecParameters *par = s->streams[0]->codecpar;
180 MCADemuxContext *m = s->priv_data;
181 uint16_t size = m->block_size;
182 uint32_t samples = m->samples_per_block;
183 int ret = 0;
184
185 if (avio_feof(s->pb))
186 return AVERROR_EOF;
187 m->current_block++;
188 if (m->current_block > m->block_count)
189 return AVERROR_EOF;
190
191 if ((ret = av_get_packet(s->pb, pkt, size * par->ch_layout.nb_channels)) < 0)
192 return ret;
193 pkt->duration = samples;
194 pkt->stream_index = 0;
195
196 return 0;
197}
198
199static int read_seek(AVFormatContext *s, int stream_index,
200 int64_t timestamp, int flags)
201{
202 AVStream *st = s->streams[stream_index];
203 MCADemuxContext *m = s->priv_data;
204 int64_t ret = 0;
205
206 if (timestamp < 0)
207 timestamp = 0;
208 timestamp /= m->samples_per_block;
209 if (timestamp >= m->block_count)
210 timestamp = m->block_count - 1;
211 ret = avio_seek(s->pb, m->data_start + timestamp * m->block_size *
212 st->codecpar->ch_layout.nb_channels, SEEK_SET);
213 if (ret < 0)
214 return ret;
215
216 m->current_block = timestamp;
217 avpriv_update_cur_dts(s, st, timestamp * m->samples_per_block);
218 return 0;
219}
220
222 .p.name = "mca",
223 .p.long_name = NULL_IF_CONFIG_SMALL("MCA Audio Format"),
224 .p.extensions = "mca",
225 .priv_data_size = sizeof(MCADemuxContext),
226 .read_probe = probe,
230};
static int probe(const AVProbeData *p)
Definition act.c:39
const FFInputFormat ff_mca_demuxer
Definition mca.c:221
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_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#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
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition seek.c:37
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ADPCM_THP_LE
Definition codec_id.h:406
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:177
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#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
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_RL32(p)
#define AV_RL16(p)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
version
Definition libkvazaar.c:313
#define MKTAG(a, b, c, d)
Definition macros.h:55
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition mca.c:199
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mca.c:177
static int read_header(AVFormatContext *s)
Definition mca.c:44
static int probe(const AVProbeData *p)
Definition mca.c:36
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
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
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
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
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
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
uint32_t data_start
Definition mca.c:32
uint32_t samples_per_block
Definition mca.c:33
uint32_t block_count
Definition mca.c:29
uint32_t current_block
Definition mca.c:31
uint16_t block_size
Definition mca.c:30
#define avpriv_request_sample(...)
#define av_log(a,...)
int size