FFmpeg
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 
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;
34 
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 
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;
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
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;
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 
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 
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 
221  .name = "mca",
222  .long_name = NULL_IF_CONFIG_SMALL("MCA Audio Format"),
223  .priv_data_size = sizeof(MCADemuxContext),
224  .read_probe = probe,
227  .read_seek = read_seek,
228  .extensions = "mca",
229 };
#define NULL
Definition: coverity.c:32
#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:346
version
Definition: libkvazaar.c:320
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
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:4900
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:686
#define avpriv_request_sample(...)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
uint32_t current_block
Definition: mca.c:30
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:91
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
Format I/O context.
Definition: avformat.h:1239
void ff_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: utils.c:1904
static int probe(const AVProbeData *p)
Definition: mca.c:35
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:381
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4481
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1307
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: mca.c:198
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:310
ptrdiff_t size
Definition: opengl_enc.c:100
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1479
AVInputFormat ff_mca_demuxer
Definition: mca.c:220
uint32_t block_count
Definition: mca.c:28
uint32_t samples_per_block
Definition: mca.c:32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:754
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mca.c:176
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
static int read_header(AVFormatContext *s)
Definition: mca.c:43
uint16_t block_size
Definition: mca.c:29
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:3286
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define s(width, name)
Definition: cbs_vp9.c:257
Stream structure.
Definition: avformat.h:880
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVIOContext * pb
I/O context.
Definition: avformat.h:1281
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
#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:929
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:738
Main libavformat public API header.
uint32_t data_start
Definition: mca.c:31
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:919
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:147
void * priv_data
Format private data.
Definition: avformat.h:1267
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
int channels
Audio only.
Definition: codec_par.h:166
Filter the word “frame” indicates either a video frame or a group of audio samples
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1045
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
int stream_index
Definition: packet.h:365
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
#define MKTAG(a, b, c, d)
Definition: common.h:413
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
This structure stores compressed data.
Definition: packet.h:340