From patchwork Wed Jan 22 00:57:25 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: 17465 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 C990944A7F4 for ; Wed, 22 Jan 2020 02:57:35 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B534568B089; Wed, 22 Jan 2020 02:57:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 08B80687F85 for ; Wed, 22 Jan 2020 02:57:30 +0200 (EET) Date: Wed, 22 Jan 2020 00:57:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail; t=1579654648; bh=AJSEy3IhMhjKwnpFIqGmbzfWOITZoVpTYnNE33VcdDk=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=jwUBntMSs825V1w9jfSRzS4+Yxo6vH28mr84LV6xyEZ5F/qOTlBBmd3kSTgX+MjiK wr2Tb6UNvndl2QxvFIRPfVVscyBr62ZRTRY2zXe4DN2nzME4siRFqj7geyxHRNvV1g 1/v90GYGg8ygVqWowf+njmn3rs6fRhXOAOJVTqlQ= To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20200122005648.16482-2-zane@zanevaniperen.com> In-Reply-To: <20200122005648.16482-1-zane@zanevaniperen.com> References: <20200122005648.16482-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=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.protonmail.ch Subject: [FFmpeg-devel] [PATCH v4 1/2] avcodec: add decoder for argonaut games' adpcm codec 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 ADPCM variant used by some Argonaut Games' games, such as 'Croc! Legend of the Gobbos', and 'Croc 2'. Signed-off-by: Zane van Iperen --- Changelog | 1 + doc/general.texi | 1 + libavcodec/Makefile | 1 + libavcodec/adpcm.c | 152 ++++++++++++++++++++++++++++++++++++++++ libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/version.h | 2 +- 8 files changed, 165 insertions(+), 1 deletion(-) diff --git a/Changelog b/Changelog index 2ccd2645fc..c029d73c72 100644 --- a/Changelog +++ b/Changelog @@ -30,6 +30,7 @@ version : - MPEG-H 3D Audio support in mp4 - thistogram filter - freezeframes filter +- Argonaut Games ADPCM decoder version 4.2: diff --git a/doc/general.texi b/doc/general.texi index 4bd4b4f6b9..85db50462c 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -1079,6 +1079,7 @@ following image formats are supported: @item ACELP.KELVIN @tab @tab X @item ADPCM 4X Movie @tab @tab X @item APDCM Yamaha AICA @tab @tab X +@item ADPCM Argonaut Games @tab @tab X @item ADPCM CDROM XA @tab @tab X @item ADPCM Creative Technology @tab @tab X @tab 16 -> 4, 8 -> 4, 8 -> 3, 8 -> 2 diff --git a/libavcodec/Makefile b/libavcodec/Makefile index c1f35b40d8..a2fbb910a0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -817,6 +817,7 @@ OBJS-$(CONFIG_ADPCM_ADX_ENCODER) += adxenc.o adx.o OBJS-$(CONFIG_ADPCM_AFC_DECODER) += adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_AGM_DECODER) += adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_AICA_DECODER) += adpcm.o adpcm_data.o +OBJS-$(CONFIG_ADPCM_ARGO_DECODER) += adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_CT_DECODER) += adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_DTK_DECODER) += adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_EA_DECODER) += adpcm.o adpcm_data.o diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 7b5b3d9698..af9830d9ff 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -12,6 +12,7 @@ * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org) * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com) * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl) + * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com) * * This file is part of FFmpeg. * @@ -148,6 +149,10 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx) if (avctx->extradata && avctx->extradata_size >= 2) c->vqa_version = AV_RL16(avctx->extradata); break; + case AV_CODEC_ID_ADPCM_ARGO: + if (avctx->bits_per_coded_sample != 4) + return AVERROR_INVALIDDATA; + break; default: break; } @@ -546,6 +551,118 @@ static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_ } } +/* + * Argonaut Decoder 1: (prev0 + (s << (c + 2))) + */ +static int16_t *adpcm_argo_decoder_1(uint8_t c, int16_t *dst, const uint8_t *src, + const int16_t *prev_, int nsamples, int stride) +{ + int16_t prev; + int8_t s; + + av_assert0(stride == 0 || stride == 1); + ++stride; + + prev = prev_[stride]; + + c += 2; + for (int i = 0; i < nsamples / 2; i++, src++) { + s = (int8_t)((*src & 0xF0u) << 0u); + *dst = prev = ((prev * (1u << 6u)) + (s * (1u << c))) >> 6; + dst += stride; + + s = (int8_t)((*src & 0x0Fu) << 4u); + *dst = prev = ((prev * (1u << 6u)) + (s * (1u << c))) >> 6; + dst += stride; + } + + return dst; +} + +/* + * Argonaut Decoder 2: (2 * prev0) - (1 * prev1) + (s << (c + 2)) + */ +static int16_t *adpcm_argo_decoder_2(uint8_t c, int16_t *dst, const uint8_t *src, + const int16_t *prev_, int nsamples, int stride) +{ + int16_t cprev[2]; + int8_t s; + + av_assert0(stride == 0 || stride == 1); + ++stride; + + /* [t-1, t-2] */ + cprev[0] = prev_[stride]; + cprev[1] = prev_[0]; + + c += 2; + for (int i = 0; i < nsamples / 2; i++, src++) { + s = (int8_t)((*src & 0xF0u) << 0u); + *dst = ((2 * cprev[0] * (1u << 6u)) - (cprev[1] * (1u << 6u)) + (s * (1u << c))) >> 6; + cprev[1] = cprev[0]; + cprev[0] = *dst; + dst += stride; + + s = (int8_t)((*src & 0x0Fu) << 4u); + *dst = ((2 * cprev[0] * (1u << 6u)) - (cprev[1] * (1u << 6u)) + (s * (1u << c))) >> 6; + cprev[1] = cprev[0]; + cprev[0] = *dst; + dst += stride; + } + + return dst; +} + +/** + * Decode a block of Argonaut ADPCM samples. + * + * The format of each block: + * uint8_t left_control; + * uint4_t left_samples[]; + * ---- and if stereo ---- + * uint8_t right_control; + * uint4_t right_samples[]; + * + * Format of the control byte: + * MSB [SSSSDRRR] LSB + * S = (Shift Amount - 2) + * D = Decoder flag. If set, use decoder 2, otherwise use decoder 1 + * R = Reserved + * + * @param dst A pointer to the buffer to write the samples. + * This must be at least `nsamples * nchannels` + * @param src A pointer to the current block. + * @param prev A pointer to the previous two decoded samples, one per channel. + * For mono this is: [Lt-2, Lt-1] + * For stereo this is: [Lt-2, Rt-2, Lt-1, Rt-1] + * @param nsamples The number of samples per channel in the block. + * Must be divisible by and greater than 2. + * @param nchannels The number of channels. Must be 1 or 2. + * + */ +static int16_t *adpcm_argo_decode_block(int16_t *dst, const uint8_t *src, + const int16_t *prev, int nsamples, + int nchannels) +{ + uint8_t c; + + av_assert0(nsamples > 2 && (nsamples & 0x1) == 0); + av_assert0(nchannels == 1 || nchannels == 2); + + /* NB: nsamples/2 because samples are 4 bits, not 8. */ + for (int i = 0; i < nchannels; i++, src += nsamples / 2) { + /* Get the control byte and run the samples through the decoder. */ + c = *src++; + + if (c & 0x04) + adpcm_argo_decoder_2(c >> 4, dst + i, src, prev + i, nsamples, nchannels - 1); + else + adpcm_argo_decoder_1(c >> 4, dst + i, src, prev + i, nsamples, nchannels - 1); + } + + return dst + (nsamples * nchannels); +} + /** * Get the number of samples that will be decoded from the packet. * In one case, this is actually the maximum number of samples possible to @@ -584,6 +701,11 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb, return 0; nb_samples = 64; break; + case AV_CODEC_ID_ADPCM_ARGO: + if (buf_size < 17 * ch) + return 0; + nb_samples = 32; + break; /* simple 4-bit adpcm */ case AV_CODEC_ID_ADPCM_CT: case AV_CODEC_ID_ADPCM_IMA_APC: @@ -1770,6 +1892,35 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data, } } break; + case AV_CODEC_ID_ADPCM_ARGO: + { + int16_t prev[4]; + + if (avctx->channels == 1) { + prev[0] = c->status[0].sample1; + prev[1] = c->status[0].sample2; + } else { + prev[0] = c->status[0].sample1; + prev[1] = c->status[1].sample1; + prev[2] = c->status[0].sample2; + prev[3] = c->status[1].sample2; + } + + samples = adpcm_argo_decode_block(samples, buf, prev, nb_samples, avctx->channels); + + if (avctx->channels == 1) { + c->status[0].sample1 = *(samples - 2); + c->status[0].sample2 = *(samples - 1); + } else { + c->status[0].sample1 = *(samples - 4); + c->status[1].sample1 = *(samples - 3); + c->status[0].sample2 = *(samples - 2); + c->status[1].sample2 = *(samples - 1); + } + + bytestream2_seek(&gb, 0, SEEK_END); + break; + } default: av_assert0(0); // unsupported codec_id should not happen @@ -1824,6 +1975,7 @@ ADPCM_DECODER(AV_CODEC_ID_ADPCM_4XM, sample_fmts_s16p, adpcm_4xm, ADPCM_DECODER(AV_CODEC_ID_ADPCM_AFC, sample_fmts_s16p, adpcm_afc, "ADPCM Nintendo Gamecube AFC"); ADPCM_DECODER(AV_CODEC_ID_ADPCM_AGM, sample_fmts_s16, adpcm_agm, "ADPCM AmuseGraphics Movie"); ADPCM_DECODER(AV_CODEC_ID_ADPCM_AICA, sample_fmts_s16p, adpcm_aica, "ADPCM Yamaha AICA"); +ADPCM_DECODER(AV_CODEC_ID_ADPCM_ARGO, sample_fmts_s16, adpcm_argo, "ADPCM Argonaut Games"); ADPCM_DECODER(AV_CODEC_ID_ADPCM_CT, sample_fmts_s16, adpcm_ct, "ADPCM Creative Technology"); ADPCM_DECODER(AV_CODEC_ID_ADPCM_DTK, sample_fmts_s16p, adpcm_dtk, "ADPCM Nintendo Gamecube DTK"); ADPCM_DECODER(AV_CODEC_ID_ADPCM_EA, sample_fmts_s16, adpcm_ea, "ADPCM Electronic Arts"); diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index ec7366144f..01a083d06b 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -582,6 +582,7 @@ extern AVCodec ff_adpcm_adx_decoder; extern AVCodec ff_adpcm_afc_decoder; extern AVCodec ff_adpcm_agm_decoder; extern AVCodec ff_adpcm_aica_decoder; +extern AVCodec ff_adpcm_argo_decoder; extern AVCodec ff_adpcm_ct_decoder; extern AVCodec ff_adpcm_dtk_decoder; extern AVCodec ff_adpcm_ea_decoder; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 4b0e7c0853..ce126353b3 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -545,6 +545,7 @@ enum AVCodecID { AV_CODEC_ID_ADPCM_IMA_DAT4, AV_CODEC_ID_ADPCM_MTAF, AV_CODEC_ID_ADPCM_AGM, + AV_CODEC_ID_ADPCM_ARGO, /* AMR */ AV_CODEC_ID_AMR_NB = 0x12000, diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 529b838e5b..32f573d58c 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -2297,6 +2297,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("ADPCM AmuseGraphics Movie AGM"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_ADPCM_ARGO, + .type = AVMEDIA_TYPE_AUDIO, + .name = "adpcm_argo", + .long_name = NULL_IF_CONFIG_SMALL("ADPCM Argonaut Games"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, + }, /* AMR */ { diff --git a/libavcodec/version.h b/libavcodec/version.h index 6cf333eeb6..2fba26e8d0 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,7 +28,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 58 -#define LIBAVCODEC_VERSION_MINOR 66 +#define LIBAVCODEC_VERSION_MINOR 67 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ From patchwork Wed Jan 22 00:57:30 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: 17466 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 A260144A7F4 for ; Wed, 22 Jan 2020 02:57:43 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8A34968B0E1; Wed, 22 Jan 2020 02:57:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EE912687F19 for ; Wed, 22 Jan 2020 02:57:36 +0200 (EET) Date: Wed, 22 Jan 2020 00:57:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail; t=1579654656; bh=hRnKW9VpGL5sFB0N8zE+vy1PsuT3fVHHXmrcLr89J4Y=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=iRMVCUTjo2RqVox+SnNtpF3YinYTCMeD9eRqblJK2fd5bNwaJ1rys10G0DXBh3mA8 LvGZ3jEEsfBwp7slwtYySrmm7AP93B/szWIzJyyzk4fYak+kSvFBZGGqcFO4yN5s/6 HkBh0Oy77peyoULsbpDURWC/PP/9hU6vZbdbh+m8= To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20200122005648.16482-3-zane@zanevaniperen.com> In-Reply-To: <20200122005648.16482-1-zane@zanevaniperen.com> References: <20200122005648.16482-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=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.protonmail.ch Subject: [FFmpeg-devel] [PATCH v4 2/2] avformat: add demuxer for argonaut games' ASF 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 custom ASF container used by some Argonaut Games' games, such as 'Croc! Legend of the Gobbos', and 'Croc 2'. Can also handle the sample files in: https://samples.ffmpeg.org/game-formats/brender/part2.zip Signed-off-by: Zane van Iperen --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/argo_asf.c | 248 +++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 4 +- 5 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 libavformat/argo_asf.c diff --git a/Changelog b/Changelog index c029d73c72..65ef01c77d 100644 --- a/Changelog +++ b/Changelog @@ -31,6 +31,7 @@ version : - thistogram filter - freezeframes filter - Argonaut Games ADPCM decoder +- Argonaut Games ASF demuxer version 4.2: diff --git a/libavformat/Makefile b/libavformat/Makefile index 52f29b1a6d..ba6ea8c4a6 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -99,6 +99,7 @@ OBJS-$(CONFIG_APTX_MUXER) += rawenc.o OBJS-$(CONFIG_APTX_HD_DEMUXER) += aptxdec.o rawdec.o OBJS-$(CONFIG_APTX_HD_MUXER) += rawenc.o OBJS-$(CONFIG_AQTITLE_DEMUXER) += aqtitledec.o subtitles.o +OBJS-$(CONFIG_ARGO_ASF_DEMUXER) += argo_asf.o OBJS-$(CONFIG_ASF_DEMUXER) += asfdec_f.o asf.o asfcrypt.o \ avlanguage.o OBJS-$(CONFIG_ASF_O_DEMUXER) += asfdec_o.o asf.o asfcrypt.o \ diff --git a/libavformat/allformats.c b/libavformat/allformats.c index ff9bdb906f..fe74a32e47 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -60,6 +60,7 @@ extern AVOutputFormat ff_aptx_muxer; extern AVInputFormat ff_aptx_hd_demuxer; extern AVOutputFormat ff_aptx_hd_muxer; extern AVInputFormat ff_aqtitle_demuxer; +extern AVInputFormat ff_argo_asf_demuxer; extern AVInputFormat ff_asf_demuxer; extern AVOutputFormat ff_asf_muxer; extern AVInputFormat ff_asf_o_demuxer; diff --git a/libavformat/argo_asf.c b/libavformat/argo_asf.c new file mode 100644 index 0000000000..ac6726ee32 --- /dev/null +++ b/libavformat/argo_asf.c @@ -0,0 +1,248 @@ +/* + * Argonaut Games ASF 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 "libavutil/intreadwrite.h" +#include "libavutil/avassert.h" + +#define ASF_TAG MKTAG('A', 'S', 'F', '\0') +#define ASF_FILE_HEADER_SIZE 24 +#define ASF_CHUNK_HEADER_SIZE 20 + +typedef struct ArgoASFFileHeader { + uint32_t magic; /*< Magic Number, {'A', 'S', 'F', '\0'} */ + uint16_t version_major; /*< File Major Version. */ + uint16_t version_minor; /*< File Minor Version. */ + uint32_t num_chunks; /*< No. chunks in the file. */ + uint32_t chunk_offset; /*< Offset to the first chunk from the start of the file. */ + int8_t name[8]; /*< Name. */ +} ArgoASFFileHeader; + +typedef struct ArgoASFChunkHeader { + uint32_t num_blocks; /*< No. blocks in the chunk. */ + uint32_t num_samples; /*< No. samples per channel in a block. */ + uint32_t unk1; /*< Unknown */ + uint16_t sample_rate; /*< Sample rate. */ + uint16_t unk2; /*< Unknown. */ + uint32_t flags; /*< Stream flags. */ +} ArgoASFChunkHeader; + +enum { + ASF_CF_BITS_PER_SAMPLE = (1 << 0), /*< 16-bit if set, 8 otherwise. */ + ASF_CF_STEREO = (1 << 1), /*< Stereo if set, mono otherwise. */ + ASF_CF_ALWAYS1_1 = (1 << 2), /*< Unknown, always seems to be set. */ + ASF_CF_ALWAYS1_2 = (1 << 3), /*< Unknown, always seems to be set. */ + + ASF_CF_ALWAYS1 = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2, + ASF_CF_ALWAYS0 = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1) +}; + +typedef struct ArgoASFDemuxContext +{ + ArgoASFFileHeader fhdr; + ArgoASFChunkHeader ckhdr; + uint32_t blocks_read; +} ArgoASFDemuxContext; + +static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf) +{ + hdr->magic = AV_RL32(buf + 0); + hdr->version_major = AV_RL16(buf + 4); + hdr->version_minor = AV_RL16(buf + 6); + hdr->num_chunks = AV_RL32(buf + 8); + hdr->chunk_offset = AV_RL32(buf + 12); + for (int i = 0; i < 8; i++) + hdr->name[i] = AV_RL8(buf + 16 + i); +} + +static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf) +{ + hdr->num_blocks = AV_RL32(buf + 0); + hdr->num_samples = AV_RL32(buf + 4); + hdr->unk1 = AV_RL32(buf + 8); + hdr->sample_rate = AV_RL16(buf + 12); + hdr->unk2 = AV_RL16(buf + 14); + hdr->flags = AV_RL32(buf + 16); +} + +/* + * Known versions: + * 1.1: The sample files in /game-formats/brender/part2.zip + * 1.2: Croc! Legend of the Gobbos + * 2.1: Croc 2 + */ +static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr) +{ + return (hdr->version_major == 1 && hdr->version_minor == 1) || + (hdr->version_major == 1 && hdr->version_minor == 2) || + (hdr->version_major == 2 && hdr->version_minor == 1); +} + +static int argo_asf_probe(const AVProbeData *p) +{ + ArgoASFFileHeader hdr; + + av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE); + + argo_asf_parse_file_header(&hdr, p->buf); + + if (hdr.magic != ASF_TAG) + return 0; + + if (!argo_asf_is_known_version(&hdr)) + return AVPROBE_SCORE_EXTENSION / 2; + + return AVPROBE_SCORE_EXTENSION + 1; +} + +static int argo_asf_read_header(AVFormatContext *s) +{ + int64_t ret; + AVIOContext *pb = s->pb; + AVStream *st; + ArgoASFDemuxContext *asf = s->priv_data; + uint8_t buf[FFMAX(ASF_FILE_HEADER_SIZE, ASF_CHUNK_HEADER_SIZE)]; + + if (!(st = avformat_new_stream(s, NULL))) + return AVERROR(ENOMEM); + + if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0) + return ret; + else if (ret != ASF_FILE_HEADER_SIZE) + return AVERROR(EIO); + + argo_asf_parse_file_header(&asf->fhdr, buf); + + if (!argo_asf_is_known_version(&asf->fhdr)) { + avpriv_request_sample(s, "Version %hu.%hu", + asf->fhdr.version_major, asf->fhdr.version_minor + ); + return AVERROR_PATCHWELCOME; + } + + if (asf->fhdr.num_chunks > 1) { + avpriv_request_sample(s, ">1 chunk"); + return AVERROR_PATCHWELCOME; + } + + if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE) + return AVERROR_INVALIDDATA; + + if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0) + return ret; + + if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0) + return ret; + else if (ret != ASF_CHUNK_HEADER_SIZE) + return AVERROR(EIO); + + argo_asf_parse_chunk_header(&asf->ckhdr, buf); + + if ((asf->ckhdr.flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (asf->ckhdr.flags & ASF_CF_ALWAYS0) != 0) { + avpriv_request_sample(s, "Nonstandard flags (0x%08X)", asf->ckhdr.flags); + return AVERROR_PATCHWELCOME; + } + + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO; + st->codecpar->format = AV_SAMPLE_FMT_S16; + + if (asf->ckhdr.flags & ASF_CF_STEREO) { + st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; + st->codecpar->channels = 2; + } else { + st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + st->codecpar->channels = 1; + } + + st->codecpar->sample_rate = asf->ckhdr.sample_rate; + + st->codecpar->bits_per_coded_sample = 4; + + if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE) + st->codecpar->bits_per_raw_sample = 16; + else + st->codecpar->bits_per_raw_sample = 8; + + if (st->codecpar->bits_per_raw_sample != 16) { + /* The header allows for these, but I've never seen any files with them. */ + avpriv_request_sample(s, "Non 16-bit samples"); + return AVERROR_PATCHWELCOME; + } + + /* + * (nchannel control bytes) + ((bytes_per_channel) * nchannel) + * For mono, this is 17. For stereo, this is 34. + */ + st->codecpar->frame_size = st->codecpar->channels + + (asf->ckhdr.num_samples / 2) * + st->codecpar->channels; + + st->codecpar->block_align = st->codecpar->frame_size; + + st->codecpar->bit_rate = st->codecpar->channels * + st->codecpar->sample_rate * + st->codecpar->bits_per_coded_sample; + + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + st->start_time = 0; + st->duration = asf->ckhdr.num_blocks * asf->ckhdr.num_samples; + st->nb_frames = asf->ckhdr.num_blocks; + return 0; +} + +static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + ArgoASFDemuxContext *asf = s->priv_data; + + AVStream *st = s->streams[0]; + AVIOContext *pb = s->pb; + int ret; + + if (asf->blocks_read >= asf->ckhdr.num_blocks) + return AVERROR_EOF; + + if ((ret = av_get_packet(pb, pkt, st->codecpar->frame_size)) < 0) + return ret; + else if (ret != st->codecpar->frame_size) + return AVERROR_INVALIDDATA; + + pkt->stream_index = st->index; + pkt->duration = asf->ckhdr.num_samples; + + ++asf->blocks_read; + return 0; +} + +/* + * Not actually sure what ASF stands for. + * - Argonaut Sound File? + * - Audio Stream File? + */ +AVInputFormat ff_argo_asf_demuxer = { + .name = "argo_asf", + .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"), + .priv_data_size = sizeof(ArgoASFDemuxContext), + .read_probe = argo_asf_probe, + .read_header = argo_asf_read_header, + .read_packet = argo_asf_read_packet +}; diff --git a/libavformat/version.h b/libavformat/version.h index 8be0c41411..f72fb9478a 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 35 -#define LIBAVFORMAT_VERSION_MICRO 104 +#define LIBAVFORMAT_VERSION_MINOR 36 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \