From patchwork Tue Feb 18 12:15:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zane van Iperen X-Patchwork-Id: 17829 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 6D90744BB26 for ; Tue, 18 Feb 2020 14:15:42 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 57396689DA5; Tue, 18 Feb 2020 14:15:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 47B22689C9E for ; Tue, 18 Feb 2020 14:15:36 +0200 (EET) Date: Tue, 18 Feb 2020 12:15:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail; t=1582028135; bh=0wZ6futJc3CMMRGUApX3ScXQLMP6syVZaiblFhSo444=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=Y4niENgpd0754SAPzR3p70BS+irHhpsLCVExPHYz68+SIuWf1cKetJo2FxfcSCphM izte0tD2+NlMNCnkW48QnwLfR4PXMF6SiMgdSIo81LHjOmXtkO8Hi8mOhx8/U0H5PR Jal8giHzDsRUcTFll5lvThuq0Fqw9EaRspYXqMDk= To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20200218121512.28323-3-zane@zanevaniperen.com> In-Reply-To: <20200218121512.28323-1-zane@zanevaniperen.com> References: <20200218121512.28323-1-zane@zanevaniperen.com> Feedback-ID: xylLYHwBzJW7F28tHN-oL9EBm6h5yqtCqG6YwPpJ2oMwBYJT6-HxTnFTF6p18axLiSywX61iUfj4CElBGg8-GA==:Ext:ProtonMail MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mail.protonmail.ch Subject: [FFmpeg-devel] [PATCH v2 2/2] avformat: add demuxer for Rayman 2's APM format 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: Zane van Iperen Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Adds support for the APM file format used by Ubisoft's Rayman 2. Signed-off-by: Zane van Iperen --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/apm.c | 188 +++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 4 +- 4 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 libavformat/apm.c diff --git a/libavformat/Makefile b/libavformat/Makefile index a9972fd99a..e0681058a2 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -92,6 +92,7 @@ OBJS-$(CONFIG_AMRWB_DEMUXER) += amr.o OBJS-$(CONFIG_ANM_DEMUXER) += anm.o OBJS-$(CONFIG_APC_DEMUXER) += apc.o OBJS-$(CONFIG_APE_DEMUXER) += ape.o apetag.o img2.o +OBJS-$(CONFIG_APM_DEMUXER) += apm.o OBJS-$(CONFIG_APNG_DEMUXER) += apngdec.o OBJS-$(CONFIG_APNG_MUXER) += apngenc.o OBJS-$(CONFIG_APTX_DEMUXER) += aptxdec.o rawdec.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 3ea4100e85..0209bf0e30 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -53,6 +53,7 @@ extern AVInputFormat ff_amrwb_demuxer; extern AVInputFormat ff_anm_demuxer; extern AVInputFormat ff_apc_demuxer; extern AVInputFormat ff_ape_demuxer; +extern AVInputFormat ff_apm_demuxer; extern AVInputFormat ff_apng_demuxer; extern AVOutputFormat ff_apng_muxer; extern AVInputFormat ff_aptx_demuxer; diff --git a/libavformat/apm.c b/libavformat/apm.c new file mode 100644 index 0000000000..3fbf6107c8 --- /dev/null +++ b/libavformat/apm.c @@ -0,0 +1,188 @@ +/* + * Rayman 2 APM Demuxer + * + * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com) + * + * 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 "avformat.h" +#include "internal.h" +#include "riff.h" +#include "libavutil/internal.h" +#include "libavutil/intreadwrite.h" + +#define APM_FILE_HEADER_SIZE 20 +#define APM_VS12_CHUNK_SIZE 76 +#define APM_MAX_READ_SIZE 4096 + +#define APM_TAG_VS12 MKTAG('v', 's', '1', '2') +#define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A') + +typedef struct APMState { + int32_t has_saved; + int32_t predictor_r; + int32_t step_index_r; + int32_t saved_r; + int32_t predictor_l; + int32_t step_index_l; + int32_t saved_l; +} APMState; + +typedef struct APMVS12Chunk { + uint32_t magic; + uint32_t file_size; + uint32_t data_size; + uint32_t unk1; + uint32_t unk2; + APMState state; + uint32_t pad[7]; +} APMVS12Chunk; + +static void apm_parse_vs12(APMVS12Chunk *vs12, const uint8_t *buf) +{ + vs12->magic = AV_RL32(buf + 0); + vs12->file_size = AV_RL32(buf + 4); + vs12->data_size = AV_RL32(buf + 8); + vs12->unk1 = AV_RL32(buf + 12); + vs12->unk2 = AV_RL32(buf + 16); + + vs12->state.has_saved = AV_RL32(buf + 20); + vs12->state.predictor_r = AV_RL32(buf + 24); + vs12->state.step_index_r = AV_RL32(buf + 28); + vs12->state.saved_r = AV_RL32(buf + 32); + vs12->state.predictor_l = AV_RL32(buf + 36); + vs12->state.step_index_l = AV_RL32(buf + 40); + vs12->state.saved_l = AV_RL32(buf + 44); + + for (int i = 0; i < FF_ARRAY_ELEMS(vs12->pad); i++) + vs12->pad[i] = AV_RL32(buf + 48 + (i * 4)); +} + +static int apm_probe(const AVProbeData *p) +{ + if (p->buf_size < 100) + return 0; + + if (AV_RL32(p->buf + 20) != APM_TAG_VS12) + return 0; + + if (AV_RL32(p->buf + 96) != APM_TAG_DATA) + return 0; + + return AVPROBE_SCORE_MAX - 1; +} + +static int apm_read_header(AVFormatContext *s) +{ + int64_t ret; + AVStream *st; + APMVS12Chunk vs12; + uint8_t buf[APM_VS12_CHUNK_SIZE]; + + if (!(st = avformat_new_stream(s, NULL))) + return AVERROR(ENOMEM); + + /* The header starts with a WAVEFORMATEX */ + if ((ret = ff_get_wav_header(s, s->pb, st->codecpar, APM_FILE_HEADER_SIZE, 0)) < 0) + return ret; + + if (st->codecpar->bits_per_coded_sample != 4) + return AVERROR_INVALIDDATA; + + if (st->codecpar->codec_tag != 0x2000) + return AVERROR_INVALIDDATA; + + /* ff_get_wav_header() does most of the work, but we need to fix a few things. */ + st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_APM; + st->codecpar->codec_tag = 0; + + if (st->codecpar->channels == 2) + st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; + else if (st->codecpar->channels == 1) + st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + else + return AVERROR_INVALIDDATA; + + st->codecpar->format = AV_SAMPLE_FMT_S16P; + st->codecpar->bits_per_raw_sample = 16; + st->codecpar->bit_rate = st->codecpar->channels * + st->codecpar->sample_rate * + st->codecpar->bits_per_coded_sample; + + if ((ret = avio_read(s->pb, buf, APM_VS12_CHUNK_SIZE)) < 0) + return ret; + else if (ret != APM_VS12_CHUNK_SIZE) + return AVERROR(EIO); + + apm_parse_vs12(&vs12, buf); + + if (vs12.magic != APM_TAG_VS12) { + return AVERROR_INVALIDDATA; + } + + if (vs12.state.has_saved) { + avpriv_request_sample(s, "Saved Samples"); + return AVERROR_PATCHWELCOME; + } + + if (avio_rl32(s->pb) != APM_TAG_DATA) + return AVERROR_INVALIDDATA; + + if ((ret = ff_alloc_extradata(st->codecpar, 16)) < 0) + return ret; + + AV_WL32(st->codecpar->extradata + 0, vs12.state.predictor_l); + AV_WL32(st->codecpar->extradata + 4, vs12.state.step_index_l); + AV_WL32(st->codecpar->extradata + 8, vs12.state.predictor_r); + AV_WL32(st->codecpar->extradata + 12, vs12.state.step_index_r); + + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + st->start_time = 0; + st->duration = vs12.data_size * + (8 / st->codecpar->bits_per_coded_sample) / + st->codecpar->channels; + return 0; +} + +static int apm_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + int ret; + AVCodecParameters *par = s->streams[0]->codecpar; + + /* + * For future reference: if files with the `has_saved` field set ever + * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples + * that should be sent to the decoder before the actual data. + */ + + if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0) + return ret; + + pkt->flags &= ~AV_PKT_FLAG_CORRUPT; + pkt->stream_index = 0; + pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->channels; + + return 0; +} + +AVInputFormat ff_apm_demuxer = { + .name = "apm", + .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"), + .read_probe = apm_probe, + .read_header = apm_read_header, + .read_packet = apm_read_packet +}; diff --git a/libavformat/version.h b/libavformat/version.h index 94fa57614d..741f2e30c5 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 38 -#define LIBAVFORMAT_VERSION_MICRO 101 +#define LIBAVFORMAT_VERSION_MINOR 39 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \