From patchwork Wed Sep 2 23:16:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liushuyu@aosc.io X-Patchwork-Id: 22068 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 57D5C44BBA0 for ; Thu, 3 Sep 2020 02:17:15 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 38DAA68AB6F; Thu, 3 Sep 2020 02:17:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay2.mymailcheap.com (relay2.mymailcheap.com [217.182.66.162]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6EB5468A646 for ; Thu, 3 Sep 2020 02:17:09 +0300 (EEST) Received: from filter2.mymailcheap.com (filter2.mymailcheap.com [91.134.140.82]) by relay2.mymailcheap.com (Postfix) with ESMTPS id 6B32E3ECDA for ; Thu, 3 Sep 2020 01:17:08 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by filter2.mymailcheap.com (Postfix) with ESMTP id 492952A526 for ; Thu, 3 Sep 2020 01:17:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mymailcheap.com; s=default; t=1599088628; bh=EpkOZgitRiBocj4iJPqLloXEOAjOFhJkdiVuuccNilA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ga2pbASneW/cL7cEPc8AfGI3eMtGQUlFmtNtCPEPxpw7YlZHXCOwQuAvgLlx5MCOm ugODmfngmVqWz3t2WZlxQ5ETtDXedmBu8TrV49LOThtZmOWPf8NfO89axTCk8MVfGz Kx1muk0OyBdaJ6PKNzTmZ0c3u37TAWnJk29LOHes= X-Virus-Scanned: Debian amavisd-new at filter2.mymailcheap.com Received: from filter2.mymailcheap.com ([127.0.0.1]) by localhost (filter2.mymailcheap.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vL8KvFv8Frps for ; Thu, 3 Sep 2020 01:17:06 +0200 (CEST) Received: from mail20.mymailcheap.com (mail20.mymailcheap.com [51.83.111.147]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by filter2.mymailcheap.com (Postfix) with ESMTPS for ; Thu, 3 Sep 2020 01:17:06 +0200 (CEST) Received: from [213.133.102.83] (ml.mymailcheap.com [213.133.102.83]) by mail20.mymailcheap.com (Postfix) with ESMTP id 0CBAD40FE4; Wed, 2 Sep 2020 23:17:05 +0000 (UTC) Authentication-Results: mail20.mymailcheap.com; dkim=pass (1024-bit key; unprotected) header.d=aosc.io header.i=@aosc.io header.b="UzjFGipv"; dkim-atps=neutral AI-Spam-Status: Not processed Received: from liushuyu.lan (d50-99-10-89.abhsia.telus.net [50.99.10.89]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail20.mymailcheap.com (Postfix) with ESMTPSA id 44DB240FE4; Wed, 2 Sep 2020 23:16:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=aosc.io; s=default; t=1599088579; bh=EpkOZgitRiBocj4iJPqLloXEOAjOFhJkdiVuuccNilA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UzjFGipv5SEacI8KAE308KVP9SpmsTwscsqDVPBI2FBnybvCklATuzy2F3MM92A7m 4YvKTXqm0j9Ld/2ST7loMvF84pxBHxh89KHIGUbDLzZVBzSEUsMCPQSAJhQiFnQi/4 m37+tJMlzwWWArjtxNEfNSCHzyYF4JRD+EHPf9iQ= From: liushuyu@aosc.io To: ffmpeg-devel@ffmpeg.org Date: Wed, 2 Sep 2020 17:16:05 -0600 Message-Id: <20200902231605.694523-2-liushuyu@aosc.io> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200902231605.694523-1-liushuyu@aosc.io> References: <20200902231605.694523-1-liushuyu@aosc.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0CBAD40FE4 X-Spamd-Result: default: False [4.90 / 20.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(0.00)[aosc.io:s=default]; TO_DN_SOME(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[aosc.io]; BROKEN_CONTENT_TYPE(1.50)[]; R_SPF_SOFTFAIL(0.00)[~all:c]; ML_SERVERS(-3.10)[213.133.102.83]; DKIM_TRACE(0.00)[aosc.io:+]; RCPT_COUNT_TWO(0.00)[2]; FROM_NO_DN(0.00)[]; MID_CONTAINS_FROM(1.00)[]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:213.133.96.0/19, country:DE]; RCVD_COUNT_TWO(0.00)[2]; HFILTER_HELO_BAREIP(3.00)[213.133.102.83,1] X-Rspamd-Server: mail20.mymailcheap.com Subject: [FFmpeg-devel] [PATCH v3] avformat: add mca demuxer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zixing Liu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Zixing Liu Signed-off-by: liushuyu --- 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 : - 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; + 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, \