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) {
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  avpriv_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 };
ff_mca_demuxer
const AVInputFormat ff_mca_demuxer
Definition: mca.c:220
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:352
AV_CODEC_ID_ADPCM_THP_LE
@ AV_CODEC_ID_ADPCM_THP_LE
Definition: codec_id.h:389
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:985
MCADemuxContext::data_start
uint32_t data_start
Definition: mca.c:31
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
probe
static int probe(const AVProbeData *p)
Definition: mca.c:35
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
read_header
static int read_header(AVFormatContext *s)
Definition: mca.c:43
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
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:1200
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:32
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
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:117
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mca.c:176
size
int size
Definition: twinvq_data.h:10344
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
version
version
Definition: libkvazaar.c:313
MCADemuxContext::current_block
uint32_t current_block
Definition: mca.c:30
avio_internal.h
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
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:128
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:197
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
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
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: utils.c:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
MCADemuxContext::samples_per_block
uint32_t samples_per_block
Definition: mca.c:32
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:147
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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:691
MCADemuxContext::block_count
uint32_t block_count
Definition: mca.c:28
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:975
MCADemuxContext::block_size
uint16_t block_size
Definition: mca.c:29
read_seek
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: mca.c:198
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:451
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:375
MCADemuxContext
Definition: mca.c:27