From patchwork Thu Mar 5 00:40:36 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: 18034 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 5BBC944AB6D for ; Thu, 5 Mar 2020 02:40:47 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 44BE168B609; Thu, 5 Mar 2020 02:40:47 +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 80CFD68B56C for ; Thu, 5 Mar 2020 02:40:41 +0200 (EET) Date: Thu, 05 Mar 2020 00:40:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail; t=1583368840; bh=lFvut4yr47HT0VQiq76JSJgO5Mj6QRdmHfyorwJa7vM=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=b3KH3XnIhdAgmVuj0EyZZZZZmiGb3UDIdWaS2mZWqkC23bVpuSbTQL9MZb4DSLEy0 TSnib8AS/rAw9Kw7NkLr35nZ4odzm6zq0qgHR1nEKtdBEF6IaHqDSLUY2p8+YHgdKr o+aw+W45juCSumue0VdYNCtCd1l8yTvk0LKp6qwk= To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20200305004013.29385-3-zane@zanevaniperen.com> In-Reply-To: <20200305004013.29385-1-zane@zanevaniperen.com> References: <20200305004013.29385-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 2/4] avformat: add demuxer for LEGO Racers' ALP 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" Signed-off-by: Zane van Iperen --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/alp.c | 135 +++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 4 +- 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 libavformat/alp.c diff --git a/libavformat/Makefile b/libavformat/Makefile index e0681058a2..fbb29505ff 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -85,6 +85,7 @@ OBJS-$(CONFIG_AIFF_DEMUXER) += aiffdec.o pcm.o isom.o \ mov_chan.o replaygain.o OBJS-$(CONFIG_AIFF_MUXER) += aiffenc.o id3v2enc.o OBJS-$(CONFIG_AIX_DEMUXER) += aixdec.o +OBJS-$(CONFIG_ALP_DEMUXER) += alp.o OBJS-$(CONFIG_AMR_DEMUXER) += amr.o OBJS-$(CONFIG_AMR_MUXER) += amr.o OBJS-$(CONFIG_AMRNB_DEMUXER) += amr.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 0209bf0e30..08012ea208 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -46,6 +46,7 @@ extern AVInputFormat ff_afc_demuxer; extern AVInputFormat ff_aiff_demuxer; extern AVOutputFormat ff_aiff_muxer; extern AVInputFormat ff_aix_demuxer; +extern AVInputFormat ff_alp_demuxer; 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 new file mode 100644 index 0000000000..76703e7ad1 --- /dev/null +++ b/libavformat/alp.c @@ -0,0 +1,135 @@ +/* + * LEGO Racers ALP (.tun & .pcm) 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" + +#define ALP_TAG MKTAG('A', 'L', 'P', ' ') +#define ALP_MAX_READ_SIZE 4096 + +typedef struct ALPHeader { + uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */ + uint32_t header_size; /*< Header size (after this). */ + char adpcm[6]; /*< "ADPCM" */ + uint8_t unk1; /*< Unknown */ + uint8_t num_channels; /*< Channel Count. */ + uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */ +} ALPHeader; + +static int alp_probe(const AVProbeData *p) +{ + if (AV_RL32(p->buf) != ALP_TAG) + return 0; + + if (AV_RL32(p->buf) < 8) + return 0; + + return AVPROBE_SCORE_EXTENSION + 1; +} + +static int alp_read_header(AVFormatContext *s) +{ + int ret; + AVStream *st; + ALPHeader hdr; + AVCodecParameters *par; + + memset(&hdr, 0, sizeof(hdr)); + + if (!(st = avformat_new_stream(s, NULL))) + return AVERROR(ENOMEM); + + if ((hdr.magic = avio_rl32(s->pb)) != ALP_TAG) + return AVERROR_INVALIDDATA; + + hdr.header_size = avio_rl32(s->pb); + + if (hdr.header_size != 8 && hdr.header_size != 12) { + return AVERROR_INVALIDDATA; + } + + if ((ret = avio_read(s->pb, hdr.adpcm, sizeof(hdr.adpcm))) < 0) + return ret; + else if (ret != sizeof(hdr.adpcm)) + return AVERROR(EIO); + + if (strncmp("ADPCM", hdr.adpcm, sizeof(hdr.adpcm)) != 0) + return AVERROR_INVALIDDATA; + + hdr.unk1 = avio_r8(s->pb); + hdr.num_channels = avio_r8(s->pb); + + if (hdr.header_size == 8) { + /* .TUN music file */ + hdr.sample_rate = 11025 * hdr.num_channels; + } else { + /* .PCM sound file */ + hdr.sample_rate = avio_rl32(s->pb); + } + + par = st->codecpar; + par->codec_type = AVMEDIA_TYPE_AUDIO; + par->codec_id = AV_CODEC_ID_ADPCM_IMA_ALP; + par->format = AV_SAMPLE_FMT_S16; + par->sample_rate = hdr.sample_rate; + par->channels = hdr.num_channels; + + if (hdr.num_channels == 1) + par->channel_layout = AV_CH_LAYOUT_MONO; + else if (hdr.num_channels == 2) + par->channel_layout = AV_CH_LAYOUT_STEREO; + else + return AVERROR_INVALIDDATA; + + par->bits_per_coded_sample = 4; + par->bits_per_raw_sample = 16; + par->block_align = 1; + par->bit_rate = par->channels * + par->sample_rate * + par->bits_per_coded_sample; + + avpriv_set_pts_info(st, 64, 1, par->sample_rate); + return 0; +} + +static int alp_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + int ret; + AVCodecParameters *par = s->streams[0]->codecpar; + + if ((ret = av_get_packet(s->pb, pkt, ALP_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_alp_demuxer = { + .name = "alp", + .long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"), + .read_probe = alp_probe, + .read_header = alp_read_header, + .read_packet = alp_read_packet +}; diff --git a/libavformat/version.h b/libavformat/version.h index 4724269b3c..a233b67351 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 39 -#define LIBAVFORMAT_VERSION_MICRO 101 +#define LIBAVFORMAT_VERSION_MINOR 40 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \