From patchwork Fri Oct 23 13:44:35 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: 23174 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 3675E44AC44 for ; Fri, 23 Oct 2020 16:44:45 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1E33A68A922; Fri, 23 Oct 2020 16:44:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail2.protonmail.ch (mail2.protonmail.ch [185.70.40.22]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C64AF687F29 for ; Fri, 23 Oct 2020 16:44:38 +0300 (EEST) Date: Fri, 23 Oct 2020 13:44:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail2; t=1603460677; bh=smEL4X5gfn1C6PezgLpiiaVYGiToeKB3qeXcHA/NP+c=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=j37PShUm4hQ6OEZepL9gW/gQxskPDGn3AcgoAhd3JemX4nCzbX7St/ZP2EP7TnwDR eMoQX2UTLmzYSpkAwB3EnexhnU3D987psumbXykMzuCPoa4Z/aL6vR4TMrRlIfIg2R +noLICITcDRg7uLjUvf1kW+navjbnnKrEXOXAP3eNgKpUMAwuYQy9sZMHMnrlFul6a 6/z9EGUnB8JH+u+MzhQ6Ju+tFKAwcriqHni8zeD0NGIXBAnFQcL11AwxvvWt3Q4B44 gxxISoLV3xKURlenv0+c4FLQbloZApZEoarQh0JJ79McLZY96ndS3B7YOGAfpxiEFt gdtD+LMBC3sSw== To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20201023134416.26577-3-zane@zanevaniperen.com> In-Reply-To: <20201023134416.26577-1-zane@zanevaniperen.com> References: <20201023134416.26577-1-zane@zanevaniperen.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=10.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 mailout.protonmail.ch Subject: [FFmpeg-devel] [PATCH 3/4] avformat: add alp muxer 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" Signed-off-by: Zane van Iperen --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/alp.c | 148 ++++++++++++++++++++++++++++++++++++++- libavformat/version.h | 2 +- 5 files changed, 151 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index 4bbb0fa6ff..27135f38fa 100644 --- a/Changelog +++ b/Changelog @@ -38,6 +38,7 @@ version : - VDPAU accelerated VP9 10/12bit decoding - afreqshift and aphaseshift filters - High Voltage Software ADPCM encoder +- LEGO Racers ALP (.tun & .pcm) muxer version 4.3: diff --git a/libavformat/Makefile b/libavformat/Makefile index a5e8bddb87..25bb8b2c69 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -88,6 +88,7 @@ OBJS-$(CONFIG_AIFF_DEMUXER) += aiffdec.o pcm.o isom.o \ OBJS-$(CONFIG_AIFF_MUXER) += aiffenc.o id3v2enc.o OBJS-$(CONFIG_AIX_DEMUXER) += aixdec.o OBJS-$(CONFIG_ALP_DEMUXER) += alp.o +OBJS-$(CONFIG_ALP_MUXER) += alp.o OBJS-$(CONFIG_AMR_DEMUXER) += amr.o OBJS-$(CONFIG_AMR_MUXER) += amr.o rawenc.o OBJS-$(CONFIG_AMRNB_DEMUXER) += amr.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 96b9bc2a0c..9b1b611a49 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -49,6 +49,7 @@ extern AVInputFormat ff_aiff_demuxer; extern AVOutputFormat ff_aiff_muxer; extern AVInputFormat ff_aix_demuxer; extern AVInputFormat ff_alp_demuxer; +extern AVOutputFormat ff_alp_muxer; extern AVInputFormat ff_amr_demuxer; extern AVOutputFormat ff_amr_muxer; extern AVInputFormat ff_amrnb_demuxer; diff --git a/libavformat/alp.c b/libavformat/alp.c index e1da312b8c..f2b0c37730 100644 --- a/libavformat/alp.c +++ b/libavformat/alp.c @@ -1,5 +1,5 @@ /* - * LEGO Racers ALP (.tun & .pcm) demuxer + * LEGO Racers ALP (.tun & .pcm) (de)muxer * * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com) * @@ -21,8 +21,10 @@ */ #include "avformat.h" #include "internal.h" +#include "rawenc.h" #include "libavutil/intreadwrite.h" #include "libavutil/internal.h" +#include "libavutil/opt.h" #define ALP_TAG MKTAG('A', 'L', 'P', ' ') #define ALP_MAX_READ_SIZE 4096 @@ -36,6 +38,18 @@ typedef struct ALPHeader { uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */ } ALPHeader; +typedef enum ALPType { + ALP_TYPE_AUTO = 0, /*< Autodetect based on file extension. */ + ALP_TYPE_TUN = 1, /*< Force a .TUN file. */ + ALP_TYPE_PCM = 2, /*< Force a .PCM file. */ +} ALPType; + +typedef struct ALPMuxContext { + const AVClass *class; + ALPType type; +} ALPMuxContext; + +#if CONFIG_ALP_DEMUXER static int alp_probe(const AVProbeData *p) { uint32_t i; @@ -144,3 +158,135 @@ AVInputFormat ff_alp_demuxer = { .read_header = alp_read_header, .read_packet = alp_read_packet }; +#endif + +#if CONFIG_ALP_MUXER + +static int alp_write_init(AVFormatContext *s) +{ + ALPMuxContext *alp = s->priv_data; + AVCodecParameters *par; + + if (alp->type == ALP_TYPE_AUTO) { + if (av_match_ext(s->url, "pcm")) + alp->type = ALP_TYPE_PCM; + else + alp->type = ALP_TYPE_TUN; + } + + if (s->nb_streams != 1) { + av_log(s, AV_LOG_ERROR, "Too many streams\n"); + return AVERROR(EINVAL); + } + + par = s->streams[0]->codecpar; + + if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_ALP) { + av_log(s, AV_LOG_ERROR, "%s codec not supported\n", + avcodec_get_name(par->codec_id)); + return AVERROR(EINVAL); + } + + if (par->channels > 2) { + av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n"); + return AVERROR(EINVAL); + } + + if (par->sample_rate > 44100) { + av_log(s, AV_LOG_ERROR, "Sample rate too large\n"); + return AVERROR(EINVAL); + } + + if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) { + av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n"); + return AVERROR(EINVAL); + } + return 0; +} + +static int alp_write_header(AVFormatContext *s) +{ + ALPMuxContext *alp = s->priv_data; + AVCodecParameters *par = s->streams[0]->codecpar; + + avio_wl32(s->pb, ALP_TAG); + avio_wl32(s->pb, alp->type == ALP_TYPE_PCM ? 12 : 8); + avio_write(s->pb, "ADPCM", 6); + avio_w8(s->pb, 0); + avio_w8(s->pb, par->channels); + if (alp->type == ALP_TYPE_PCM) + avio_wl32(s->pb, par->sample_rate); + + return 0; +} + +enum { AE = AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM }; + +static const AVOption alp_options[] = { + { + .name = "type", + .help = "set file type", + .offset = offsetof(ALPMuxContext, type), + .type = AV_OPT_TYPE_INT, + .default_val = {.i64 = ALP_TYPE_AUTO}, + .min = ALP_TYPE_AUTO, + .max = ALP_TYPE_PCM, + .flags = AE, + .unit = "type", + }, + { + .name = "auto", + .help = "autodetect based on file extension", + .offset = 0, + .type = AV_OPT_TYPE_CONST, + .default_val = {.i64 = ALP_TYPE_AUTO}, + .min = 0, + .max = 0, + .flags = AE, + .unit = "type" + }, + { + .name = "tun", + .help = "force .tun, used for music", + .offset = 0, + .type = AV_OPT_TYPE_CONST, + .default_val = {.i64 = ALP_TYPE_TUN}, + .min = 0, + .max = 0, + .flags = AE, + .unit = "type" + }, + { + .name = "pcm", + .help = "force .pcm, used for sfx", + .offset = 0, + .type = AV_OPT_TYPE_CONST, + .default_val = {.i64 = ALP_TYPE_PCM}, + .min = 0, + .max = 0, + .flags = AE, + .unit = "type" + }, + { NULL } +}; + +static const AVClass alp_muxer_class = { + .class_name = "alp", + .item_name = av_default_item_name, + .option = alp_options, + .version = LIBAVUTIL_VERSION_INT +}; + +AVOutputFormat ff_alp_muxer = { + .name = "alp", + .long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"), + .extensions = "tun,pcm", + .audio_codec = AV_CODEC_ID_ADPCM_IMA_ALP, + .video_codec = AV_CODEC_ID_NONE, + .init = alp_write_init, + .write_header = alp_write_header, + .write_packet = ff_raw_write_packet, + .priv_class = &alp_muxer_class, + .priv_data_size = sizeof(ALPMuxContext) +}; +#endif diff --git a/libavformat/version.h b/libavformat/version.h index 86e0a232ee..7853068649 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // 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 62 +#define LIBAVFORMAT_VERSION_MINOR 63 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \