On 9/3/20, liush...@aosc.io <liush...@aosc.io> wrote: > From: Zixing Liu <liush...@aosc.io> > > Signed-off-by: liushuyu <liush...@aosc.io> > --- > Changelog | 1 + > doc/general.texi | 2 + > libavformat/Makefile | 1 + > libavformat/allformats.c | 1 + > libavformat/mca.c | 228 +++++++++++++++++++++++++++++++++++++++ > libavformat/version.h | 4 +- > 6 files changed, 235 insertions(+), 2 deletions(-) > create mode 100644 libavformat/mca.c > > diff --git a/Changelog b/Changelog > index 7467e73..ae4219f 100644 > --- a/Changelog > +++ b/Changelog > @@ -15,6 +15,7 @@ version <next>: > - Argonaut Games ASF muxer > - AV1 Low overhead bitstream format demuxer > - RPZA video encoder > +- MCA demuxer > > > version 4.3: > diff --git a/doc/general.texi b/doc/general.texi > index d618565..fa76ed4 100644 > --- a/doc/general.texi > +++ b/doc/general.texi > @@ -524,6 +524,8 @@ library: > @tab Metadata in text format. > @item MAXIS XA @tab @tab X > @tab Used in Sim City 3000; file extension .xa. > +@item MCA @tab @tab X > + @tab Used in some games from Capcom; file extension .mca. > @item MD Studio @tab @tab X > @item Metal Gear Solid: The Twin Snakes @tab @tab X > @item Megalux Frame @tab @tab X > diff --git a/libavformat/Makefile b/libavformat/Makefile > index cbb33fe..7f5ab21 100644 > --- a/libavformat/Makefile > +++ b/libavformat/Makefile > @@ -305,6 +305,7 @@ OBJS-$(CONFIG_MATROSKA_MUXER) += > matroskaenc.o matroska.o \ > av1.o avc.o hevc.o \ > flacenc_header.o avlanguage.o \ > vorbiscomment.o wv.o > +OBJS-$(CONFIG_MCA_DEMUXER) += mca.o > OBJS-$(CONFIG_MCC_DEMUXER) += mccdec.o subtitles.o > OBJS-$(CONFIG_MD5_MUXER) += hashenc.o > OBJS-$(CONFIG_MGSTS_DEMUXER) += mgsts.o > diff --git a/libavformat/allformats.c b/libavformat/allformats.c > index 0aa9dd7..8a71de6 100644 > --- a/libavformat/allformats.c > +++ b/libavformat/allformats.c > @@ -232,6 +232,7 @@ extern AVInputFormat ff_lvf_demuxer; > extern AVInputFormat ff_lxf_demuxer; > extern AVInputFormat ff_m4v_demuxer; > extern AVOutputFormat ff_m4v_muxer; > +extern AVInputFormat ff_mca_demuxer; > extern AVInputFormat ff_mcc_demuxer; > extern AVOutputFormat ff_md5_muxer; > extern AVInputFormat ff_matroska_demuxer; > diff --git a/libavformat/mca.c b/libavformat/mca.c > new file mode 100644 > index 0000000..ad3fe48 > --- /dev/null > +++ b/libavformat/mca.c > @@ -0,0 +1,228 @@ > +/* > + * MCA demuxer > + * Copyright (c) 2020 Zixing Liu > + * > + * This file is part of FFmpeg. > + * > + * FFmpeg is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * FFmpeg is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with FFmpeg; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 > USA > + */ > + > +#include "libavutil/intreadwrite.h" > +#include "avformat.h" > +#include "avio_internal.h" > +#include "internal.h" > + > +typedef struct MCADemuxContext { > + uint32_t block_count; > + uint16_t block_size; > + uint32_t current_block; > + uint32_t data_start; > + uint32_t samples_per_block; > +} MCADemuxContext; > + > +static int probe(const AVProbeData *p) > +{ > + if (AV_RL32(p->buf) == MKTAG('M', 'A', 'D', 'P') && > + AV_RL16(p->buf + 4) <= 0xff) > + return AVPROBE_SCORE_MAX / 3 * 2; > + return 0; > +} > + > +static int read_header(AVFormatContext *s) > +{ > + AVStream *st; > + MCADemuxContext *m = s->priv_data; > + AVCodecParameters *par; > + int64_t file_size = avio_size(s->pb); > + uint16_t version = 0; > + uint32_t header_size, data_size, data_offset, loop_start, loop_end, > + nb_samples, nb_metadata, coef_offset = 0; > + int ch, ret; > + int64_t ret_size; > + > + st = avformat_new_stream(s, NULL); > + if (!st) > + return AVERROR(ENOMEM); > + par = st->codecpar; > + par->codec_type = AVMEDIA_TYPE_AUDIO; > + > + // parse file headers > + avio_skip(s->pb, 0x4); // skip the file magic > + version = avio_rl16(s->pb); > + avio_skip(s->pb, 0x2); // padding > + par->channels = avio_r8(s->pb); > + avio_skip(s->pb, 0x1); // padding > + m->block_size = avio_rl16(s->pb); > + nb_samples = avio_rl32(s->pb); > + par->sample_rate = avio_rl32(s->pb); > + loop_start = avio_rl32(s->pb); > + loop_end = avio_rl32(s->pb); > + header_size = avio_rl32(s->pb); > + data_size = avio_rl32(s->pb); > + avio_skip(s->pb, 0x4); > + nb_metadata = avio_rl16(s->pb); > + avio_skip(s->pb, 0x2); // unknown u16 field > + > + // samples per frame = 14; frame size = 8 (2^3) > + m->samples_per_block = (m->block_size * 14) >> 3; > + > + if (m->samples_per_block < 1) > + return AVERROR_INVALIDDATA; > + > + m->block_count = nb_samples / m->samples_per_block; > + st->duration = nb_samples; > + > + // sanity checks > + if (!par->channels || par->sample_rate <= 0 > + || loop_start > loop_end || m->block_count < 1) > + return AVERROR_INVALIDDATA; > + if ((ret = av_dict_set_int(&s->metadata, "loop_start", > + av_rescale(loop_start, AV_TIME_BASE, > + par->sample_rate), 0)) < 0) > + return ret; > + if ((ret = av_dict_set_int(&s->metadata, "loop_end", > + av_rescale(loop_end, AV_TIME_BASE, > + par->sample_rate), 0)) < 0) > + return ret; > + if ((32 + 4 + m->block_size) > (INT_MAX / par->channels) || > + (32 + 4 + m->block_size) * par->channels > INT_MAX - 8) > + return AVERROR_INVALIDDATA; > + avpriv_set_pts_info(st, 64, 1, par->sample_rate); > + > + if (version <= 4) { > + // version <= 4 needs to use the file size to calculate the offsets > + if (file_size < 0) { > + return AVERROR(EIO); > + } > + if (file_size - data_size > UINT32_MAX) > + return AVERROR_INVALIDDATA; > + m->data_start = file_size - data_size; > + if (version <= 3) { > + nb_metadata = 0; > + // header_size is not available or incorrect in older versions > + header_size = m->data_start; > + } > + } else if (version == 5) { > + // read data_start location from the header > + if (0x30 * par->channels + 0x4 > header_size) > + return AVERROR_INVALIDDATA; > + data_offset = header_size - 0x30 * par->channels - 0x4; > + if ((ret_size = avio_seek(s->pb, data_offset, SEEK_SET)) < 0) > + return ret_size; > + m->data_start = avio_rl32(s->pb); > + // check if the metadata is reasonable > + if (file_size > 0 && (int64_t)m->data_start + data_size > > file_size) { > + // the header is broken beyond repair > + if ((int64_t)header_size + data_size > file_size) { > + av_log(s, AV_LOG_ERROR, > + "MCA metadata corrupted, unable to determine the > data offset.\n"); > + return AVERROR_INVALIDDATA; > + } > + // recover the data_start information from the data size > + av_log(s, AV_LOG_WARNING, > + "Incorrect header size found in metadata, " > + "header size approximated from the data size\n"); > + if (file_size - data_offset > UINT32_MAX) > + return AVERROR_INVALIDDATA; > + m->data_start = file_size - data_size; > + } > + } else { > + avpriv_request_sample(s, "version %d", version); > + return AVERROR_PATCHWELCOME; > + } > + > + // coefficient alignment = 0x30; metadata size = 0x14 > + if (0x30 * par->channels + nb_metadata * 0x14 > header_size) > + return AVERROR_INVALIDDATA; > + coef_offset = > + header_size - 0x30 * par->channels + nb_metadata * 0x14; > + > + st->start_time = 0; > + par->codec_id = AV_CODEC_ID_ADPCM_THP_LE; > + > + ret = ff_alloc_extradata(st->codecpar, 32 * par->channels); > + if (ret < 0) > + return ret; > + > + if ((ret_size = avio_seek(s->pb, coef_offset, SEEK_SET)) < 0) > + return ret_size; > + for (ch = 0; ch < par->channels; ch++) { > + if ((ret = ffio_read_size(s->pb, par->extradata + ch * 32, 32)) < > 0) > + return ret; > + // 0x30 (alignment) - 0x20 (actual size, 32) = 0x10 (padding) > + avio_skip(s->pb, 0x10); > + } > + > + // seek to the beginning of the adpcm data > + // there are some files where the adpcm audio data is not immediately > after the header > + if ((ret_size = avio_seek(s->pb, m->data_start, SEEK_SET)) < 0) > + return ret_size; > + > + return 0; > +} > + > +static int read_packet(AVFormatContext *s, AVPacket *pkt) > +{ > + AVCodecParameters *par = s->streams[0]->codecpar; > + MCADemuxContext *m = s->priv_data; > + uint16_t size = m->block_size; > + uint32_t samples = m->samples_per_block; > + int ret = 0; > + > + if (avio_feof(s->pb)) > + return AVERROR_EOF; > + m->current_block++; > + if (m->current_block > m->block_count) > + return AVERROR_EOF; > + > + if ((ret = av_new_packet(pkt, size * par->channels)) < 0) > + return ret;
This looks wrong and unneeded. > + if ((ret = av_get_packet(s->pb, pkt, size * par->channels)) < 0) > + return ret; > + pkt->duration = samples; > + pkt->stream_index = 0; > + > + return 0; > +} > + > +static int read_seek(AVFormatContext *s, int stream_index, > + int64_t timestamp, int flags) > +{ > + AVStream *st = s->streams[stream_index]; > + MCADemuxContext *m = s->priv_data; > + int64_t ret = 0; > + > + timestamp /= m->samples_per_block; > + ret = avio_seek(s->pb, m->data_start + timestamp * m->block_size * > + st->codecpar->channels, SEEK_SET); > + > + if (ret < 0) > + return ret; > + > + m->current_block = timestamp; > + ff_update_cur_dts(s, st, timestamp * m->samples_per_block); > + return 0; > +} > + > +AVInputFormat ff_mca_demuxer = { > + .name = "mca", > + .long_name = NULL_IF_CONFIG_SMALL("MCA Audio Format"), > + .priv_data_size = sizeof(MCADemuxContext), > + .read_probe = probe, > + .read_header = read_header, > + .read_packet = read_packet, > + .read_seek = read_seek, > + .extensions = "mca", > +}; > diff --git a/libavformat/version.h b/libavformat/version.h > index 88876ae..146db09 100644 > --- a/libavformat/version.h > +++ b/libavformat/version.h > @@ -32,8 +32,8 @@ > // Major bumping may affect Ticket5467, 5421, 5451(compatibility with > Chromium) > // Also please add any ticket numbers that you believe might be affected > here > #define LIBAVFORMAT_VERSION_MAJOR 58 > -#define LIBAVFORMAT_VERSION_MINOR 51 > -#define LIBAVFORMAT_VERSION_MICRO 101 > +#define LIBAVFORMAT_VERSION_MINOR 52 > +#define LIBAVFORMAT_VERSION_MICRO 100 > > #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ > LIBAVFORMAT_VERSION_MINOR, \ > -- > 2.28.0 > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe". _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".