[FFmpeg-devel] [PATCH v2 1/1] avformat: add mca demuxer
liushuyu at aosc.io
liushuyu at aosc.io
Thu Sep 3 01:12:19 EEST 2020
On 2020-09-02 00:37, Andreas Rheinhardt wrote:
> liushuyu at aosc.io:
>> From: Zixing Liu <liushuyu at aosc.io>
>>
>> Signed-off-by: liushuyu <liushuyu at aosc.io>
>> ---
>> Changelog | 1 +
>> doc/general.texi | 2 +
>> libavformat/Makefile | 1 +
>> libavformat/allformats.c | 1 +
>> libavformat/mca.c | 240
>> +++++++++++++++++++++++++++++++++++++++
>> libavformat/version.h | 4 +-
>> 6 files changed, 247 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.
>> + at 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..dbbb374
>> --- /dev/null
>> +++ b/libavformat/mca.c
>> @@ -0,0 +1,240 @@
>> +/*
>> + * 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 "libavcodec/bytestream.h"
>
> You don't seem to be using anything from this header.
>
>> +#include "avformat.h"
>> +#include "internal.h"
>> +
>> +typedef struct MCADemuxContext {
>> + uint32_t block_count;
>> + uint16_t block_size;
>> + uint32_t coef_offset;
>> + 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;
>> + int64_t file_size = 0;
>> + uint16_t version = 0;
>> + uint32_t header_size, data_size, data_offset, loop_start,
>> loop_end,
>> + nb_samples, nb_metadata = 0;
>> + int ch;
>> + int ret = AVERROR_EOF;
>
> This value is never used.
>
>> +
>> + st = avformat_new_stream(s, NULL);
>> + if (!st)
>> + return AVERROR(ENOMEM);
>> + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
>
> If you used a dedicated variable (common name would be par) to access
> st->codecpar, the lengths of the lines below would not have outliers.
>
>> +
>> + // parse file headers
>> + avio_skip(s->pb, 0x4); // skip the file magic
>> + version = avio_rl16(s->pb); // offset 0x4
>> + avio_skip(s->pb, 0x2); // padding
>> + st->codecpar->channels = avio_r8(s->pb); // offset 0x8
>> + avio_skip(s->pb, 0x1); // padding
>> + m->block_size = avio_rl16(s->pb); // offset 0xa
>> + nb_samples = avio_rl32(s->pb); // offset 0xc
>> + st->codecpar->sample_rate = avio_rl32(s->pb); // offset
>> 0x10
>> + loop_start = avio_rl32(s->pb); // offset 0x14
>> + loop_end = avio_rl32(s->pb); // offset 0x18
>> + header_size = avio_rl32(s->pb); // offset 0x1c
>> + data_size = avio_rl32(s->pb); // offset 0x20
>> + avio_skip(s->pb, 0x4); // offset 0x24 (duration,
>> float)
>> + nb_metadata = avio_rl16(s->pb); // offset 0x28
>> + avio_skip(s->pb, 0x2); // unknown u16 field
>
> You can align these lines on '=' (well, the lines that have a '='). And
> I don't think that the offset comments are helpful.
>
>> +
>> + file_size = avio_size(s->pb);
>> +
> You could directly initialize file_size to this value.
>
>> + // samples per frame = 14; frame size = 8 (2^3)
>> + m->samples_per_block = (m->block_size * 14) >> 3;
>> + m->block_count = nb_samples / m->samples_per_block;
>
> You are dividing by zero here if m->samples_per_block is zero. The
> check
> to rule this out is a few lines below, but that's too late.
>
>> + st->duration = nb_samples;
>
> Is there a reason you prefer this over the duration field you skipped
> earlier?
>
The duration field I skipped earlier is the duration in seconds (32-bit
float). This field is missing in some of the files I could find so I
think it's not very reliable. Also, when seeking the file, I need an
accurate duration (in the number of samples) to calculate the data
offset.
>> +
>> + // sanity checks
>> + if (!st->codecpar->channels || st->codecpar->sample_rate <= 0
>> + || m->samples_per_block < 1 || loop_start > loop_end
>> + || m->block_count < 1)
>> + return AVERROR_INVALIDDATA;
>> + if (av_dict_set_int(&s->metadata, "loop_start",
>> + av_rescale(loop_start, AV_TIME_BASE,
>> + st->codecpar->sample_rate), 0) <
>> 0)
>> + return AVERROR(ENOMEM);
>
> Just forward the error.
>
>> + if (av_dict_set_int(&s->metadata, "loop_end",
>> + av_rescale(loop_end, AV_TIME_BASE,
>> + st->codecpar->sample_rate), 0) <
>> 0)
>> + return AVERROR(ENOMEM);
>> + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
>> +
>> + if (version <= 4) {
>> + // version <= 4 needs to use the file size to calculate the
>> offsets
>> + if (file_size < 0) {
>> + return AVERROR(EIO);
>> + }
>> + m->data_start = file_size - data_size;
>
> There is no guarantee here that the right hand side is in the range of
> uint32_t; same goes for data_offset below.
>
>> + 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
>> + data_offset = header_size - 0x30 * st->codecpar->channels -
>> 0x4;
>> + ret = avio_seek(s->pb, data_offset, SEEK_SET);
>
> ret needs to be an int64_t for it to hold the return value of
> avio_seek().
>
>> + if (ret < 0)
>> + return ret;
>> + m->data_start = avio_rl32(s->pb);
>> + // check if the metadata is reasonable
>> + if (file_size > 0 && m->data_start + data_size > file_size) {
>
> The addition will be performed as uint32_t (i.e. with wraparound); same
> for header_size + data_size below.
>
>> + // the header is broken beyond repair
>> + if (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");
>
> Split the string in two lines.
>
>> + m->data_start = file_size - data_size;
>> + }
>> + } else {
>> + avpriv_request_sample(s, "version %d", version);
>> + return AVERROR_PATCHWELCOME;
>> + }
>> +
>> + // coefficient alignment = 0x30; metadata size = 0x14
>> + m->coef_offset =
>> + header_size - 0x30 * st->codecpar->channels + nb_metadata *
>> 0x14;
>
> This is a completely local variable; it should not be in the context.
>
>> + m->current_block = 0;
>
> Your context is initially zeroed before read_header.
>
>> +
>> + st->start_time = 0;
>> + st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP_LE;
>> +
>> + ret = ff_alloc_extradata(st->codecpar, 32 *
>> st->codecpar->channels);
>> + if (ret < 0)
>> + return ret;
>> +
>> + ret = avio_seek(s->pb, m->coef_offset, SEEK_SET);
>> + if (ret < 0)
>> + return ret;
>> + for (ch = 0; ch < st->codecpar->channels; ch++) {
>> + if (avio_read(s->pb, st->codecpar->extradata + ch * 32, 32)
>> != 32) {
>
> ffio_read_size().
>
>> + return AVERROR_INVALIDDATA;
>> + }
>> + // 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 that the adpcm audio data is not
>> immediately after the header
>
> where the adpcm audio data
>
>> + ret = avio_seek(s->pb, m->data_start, SEEK_SET);
>> + if (ret < 0)
>> + return ret;
>> +
>> + return 0;
>> +}
>> +
>> +static int read_packet(AVFormatContext *s, AVPacket *pkt)
>> +{
>> + AVCodecParameters *par = s->streams[0]->codecpar;
>> + MCADemuxContext *m = s->priv_data;
>> + uint32_t samples, size = 0;
>> + int ret, i = 0;
>> + uint8_t *dst;
>> +
>> + if (avio_feof(s->pb))
>> + return AVERROR_EOF;
>> + m->current_block++;
>> + size = m->block_size;
>> + samples = m->samples_per_block;
>> + // adapted from brstm.c
>> + if (m->current_block == m->block_count) {
>> + if (samples < size * 14 / 8) {
>> + uint32_t adjusted_size = samples / 14 * 8;
>> + if (samples % 14)
>> + adjusted_size += (samples % 14 + 1) / 2 + 1;
>> +
>> + size = adjusted_size;
>> + }
>> + } else if (m->current_block > m->block_count)
>> + return AVERROR_EOF;
>> +
>> + if (size > (INT_MAX - 32 - 4) ||
>> + (32 + 4 + size) > (INT_MAX / par->channels) ||
>> + (32 + 4 + size) * par->channels > INT_MAX - 8)
>> + return AVERROR_INVALIDDATA;
>
> You should check the block_size when reading the header to rule this
> out.
>
>> + if ((ret = av_new_packet(pkt, size * par->channels)) < 0)
>> + return ret;
>> + dst = pkt->data;
>> + for (i = 0; i < par->channels; i++) {
>> + ret = avio_read(s->pb, dst, size);
>> + dst += size;
>> + if (ret != size) {
>> + return AVERROR(EIO);
>> + }
>> + }
>
> There is really no need to read the data for each channel individually;
> the whole thing above can be replaced with av_get_packet().
>
>> + pkt->duration = samples;
>> + pkt->stream_index = 0;
>> +
>> + return ret;
>
> return 0 on success (ret currently contains size).
>
>> +}
>> +
>> +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, \
>>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
I will fix all the issues you have proposed so far. And thanks for such
a detailed review!
Thanks,
Zixing
More information about the ffmpeg-devel
mailing list