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 "demux.h"
26 #include "internal.h"
27 
28 typedef struct MCADemuxContext {
29  uint32_t block_count;
30  uint16_t block_size;
31  uint32_t current_block;
32  uint32_t data_start;
35 
36 static 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;
48  AVCodecParameters *par;
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 
56  st = avformat_new_stream(s, NULL);
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)
83  return AVERROR_INVALIDDATA;
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)
91  return AVERROR_INVALIDDATA;
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(EIO);
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);
144  return AVERROR_PATCHWELCOME;
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 
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 
199 static 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,
229  .read_seek = read_seek,
230 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVERROR
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
ff_mca_demuxer
const FFInputFormat ff_mca_demuxer
Definition: mca.c:221
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
avpriv_update_cur_dts
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
AV_CODEC_ID_ADPCM_THP_LE
@ AV_CODEC_ID_ADPCM_THP_LE
Definition: codec_id.h:409
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:867
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
MCADemuxContext::data_start
uint32_t data_start
Definition: mca.c:32
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
probe
static int probe(const AVProbeData *p)
Definition: mca.c:36
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
read_header
static int read_header(AVFormatContext *s)
Definition: mca.c:44
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mca.c:177
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
version
version
Definition: libkvazaar.c:321
MCADemuxContext::current_block
uint32_t current_block
Definition: mca.c:31
avio_internal.h
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
demux.h
av_rescale
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
av_get_packet
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
MCADemuxContext::samples_per_block
uint32_t samples_per_block
Definition: mca.c:33
av_dict_set_int
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:167
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFInputFormat
Definition: demux.h:42
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:662
MCADemuxContext::block_count
uint32_t block_count
Definition: mca.c:29
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:797
MCADemuxContext::block_size
uint16_t block_size
Definition: mca.c:30
read_seek
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: mca.c:199
ff_alloc_extradata
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:227
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346
MCADemuxContext
Definition: mca.c:28