From patchwork Sat Dec 30 21:13:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 45398 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6623:b0:194:e134:edd4 with SMTP id n35csp5483640pzh; Sat, 30 Dec 2023 13:14:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IFftAUtt02eEOvsmXAteJMRzCMTgkAqTe96cQ9ZJcBv5vwFzd2zoA9oxs523pVB9I5y8KwP X-Received: by 2002:a17:906:ae13:b0:a26:f957:b9c2 with SMTP id le19-20020a170906ae1300b00a26f957b9c2mr3656505ejb.0.1703970846096; Sat, 30 Dec 2023 13:14:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703970846; cv=none; d=google.com; s=arc-20160816; b=uo+zhnNegKiM459crcy1fwMklqSvb2g3UgM574+m3qEXfANrVFNnTFYY++Gk7c+Plh xYvxGtd/ytYwGJUXazQbVUwLepI6fTmrU1xdSvd2/rAWNMAV6aqNNwZNr2WNHWZPlzht psNggmcI20Y924jKSIBcxXsAeWxJ59hxzJgRcvsfUzntGD1l2MQ8iA6xfwkYkS4ZJmhQ 8ZVzPKL8k2LOO3WvntJLC0uuWwS814J6qwtypo11fz3wk5PhAwe/Ix4M7t8FOqGzNP+a V/IwPnablnN4gnYxNTB0zEfAYKhUJSBL/j6xQSQnC2V8uf3zSe892tZ/A5jO7VSyS//u CN/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:user-agent:references:in-reply-to:date:to:from :message-id:delivered-to; bh=8CcPXBH0tDWaSMbBN8MYKcSla9Nyhx9FSdJiEbABdC4=; fh=e5zN9xSzcxLA6bGo3lF+CqTbY/oLwzApV03EO/RBfgQ=; b=lI2meDEIE4vXpZQxc8fV826/c1ITrY67PpoXATlce1p4woaR+FZ+VwVORSJLpai3yz d88IPWC/mDZ4UxLTDWfh8FBBv1k20W9bfGOwzdYoQzMhjdVRe9RAGIiYQfk0QeWcAsrl /bH5Ki+LbGPGb2shsXoanOmtwUhf3zK6YU8dhTOkrJ4m+hcCRj8MyH1cEHbKLD5LFRPC EMEvedNPAhDi4hW61sG9mOAsCGjxPzB0rgSykSVTyOtpddcs9PGGNr8e20ut1SNqBuBR NI68hc9hXuSi6P3Wo39TDmGGdd3pAiqXTO8ZPl+oCT9pFcFNrZjl6Y028wRleLNYBOgR FR8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id se1-20020a170906ce4100b00a27d968a6c7si247658ejb.429.2023.12.30.13.14.05; Sat, 30 Dec 2023 13:14:06 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5BAC368B3B0; Sat, 30 Dec 2023 23:14:03 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from glom.nmugroup.com (glom.nmugroup.com [193.183.80.6]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4C1AA68B3B0 for ; Sat, 30 Dec 2023 23:13:56 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by glom.nmugroup.com (Postfix) with ESMTP id E9DBE5428493 for ; Sat, 30 Dec 2023 22:13:55 +0100 (CET) Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (Authenticated sender: git01) by glom.nmugroup.com (Postfix) with ESMTPSA id A46975428441 for ; Sat, 30 Dec 2023 22:13:55 +0100 (CET) Message-ID: <326e731192863601001f2c5904e0c378c08960f7.camel@haerdin.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Sat, 30 Dec 2023 22:13:54 +0100 In-Reply-To: References: User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 4/6] lavf: Add CRYO APC muxer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: a1GXq4r5ECe2 From 1fab30dea9528478ccbe78dc68a3933acb8115b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Tue, 26 Dec 2023 14:32:10 +0100 Subject: [PATCH 4/6] lavf: Add CRYO APC muxer --- Changelog | 1 + doc/general_contents.texi | 2 +- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/apcenc.c | 129 ++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 6 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 libavformat/apcenc.c diff --git a/Changelog b/Changelog index a638c03250..31c9e25be6 100644 --- a/Changelog +++ b/Changelog @@ -13,6 +13,7 @@ version : - IAMF raw demuxer and muxer - D3D12VA hardware accelerated H264, HEVC, VP9, AV1, MPEG-2 and VC1 decoding - tiltandshift filter +- CRYO APC muxer version 6.1: - libaribcaption decoder diff --git a/doc/general_contents.texi b/doc/general_contents.texi index ec7516dce8..df31edd060 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -464,7 +464,7 @@ library: @item CRC testing format @tab X @tab @item Creative Voice @tab X @tab X @tab Created for the Sound Blaster Pro. -@item CRYO APC @tab @tab X +@item CRYO APC @tab X @tab X @tab Audio format used in some games by CRYO Interactive Entertainment. @item D-Cinema audio @tab X @tab X @item Deluxe Paint Animation @tab @tab X diff --git a/libavformat/Makefile b/libavformat/Makefile index 581e378d95..f605f4ae69 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -105,6 +105,7 @@ OBJS-$(CONFIG_AMV_MUXER) += amvenc.o OBJS-$(CONFIG_ANM_DEMUXER) += anm.o OBJS-$(CONFIG_APAC_DEMUXER) += apac.o rawdec.o OBJS-$(CONFIG_APC_DEMUXER) += apc.o +OBJS-$(CONFIG_APC_MUXER) += apcenc.o OBJS-$(CONFIG_APE_DEMUXER) += ape.o apetag.o img2.o OBJS-$(CONFIG_APM_DEMUXER) += apm.o OBJS-$(CONFIG_APM_MUXER) += apm.o rawenc.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index ce6be5f04d..18a33d7448 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -60,6 +60,7 @@ extern const FFOutputFormat ff_amv_muxer; extern const AVInputFormat ff_anm_demuxer; extern const AVInputFormat ff_apac_demuxer; extern const AVInputFormat ff_apc_demuxer; +extern const FFOutputFormat ff_apc_muxer; extern const AVInputFormat ff_ape_demuxer; extern const AVInputFormat ff_apm_demuxer; extern const FFOutputFormat ff_apm_muxer; diff --git a/libavformat/apcenc.c b/libavformat/apcenc.c new file mode 100644 index 0000000000..7c7f3a1f26 --- /dev/null +++ b/libavformat/apcenc.c @@ -0,0 +1,129 @@ +/* + * CRYO APC audio format muxer + * Copyright (c) 2023 Tomas Härdin + * + * 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 "avio_internal.h" +#include "internal.h" +#include "mux.h" + +#define APC_HEADER_SIZE (8*4) + +static int apc_write_header(AVFormatContext *s) +{ + AVCodecParameters *par; + AVStream *st; + + if (s->nb_streams != 1) { + av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n"); + return AVERROR(EINVAL); + } + + st = s->streams[0]; + par = st->codecpar; + + if (par->ch_layout.nb_channels <= 0 || par->ch_layout.nb_channels > 2) { + av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n"); + return AVERROR(EINVAL); + } + + avpriv_set_pts_info(st, 64, 1, par->sample_rate); + // delay writing the actual header until we get extradata from the encoder + return 0; +} + +static int apc_write_header_delayed(AVFormatContext *s, AVPacket *pkt) +{ + AVIOContext *pb = s->pb; + AVCodecParameters *par = s->streams[0]->codecpar; + size_t new_extradata_size = 0, extradata_size; + const uint8_t *new_extradata = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &new_extradata_size), *extradata; + + // prefer updated extradata from the encoder + if (new_extradata) { + extradata_size = new_extradata_size; + extradata = new_extradata; + } else { + extradata_size = par->extradata_size; + extradata = par->extradata; + } + + if (extradata_size != 8) { + av_log(s, AV_LOG_ERROR, "Must have exactly 8 bytes of extradata, got %zu\n", extradata_size); + return AVERROR(EINVAL); + } + + ffio_wfourcc(pb, "CRYO"); + ffio_wfourcc(pb, "_APC"); + ffio_wfourcc(pb, "1.20"); + avio_wl32(pb, 0); // number or samples. updated in apc_write_trailer() + avio_wl32(pb, par->sample_rate); + avio_write(pb, extradata, extradata_size); + avio_wl32(pb, par->ch_layout.nb_channels - 1); + + return 0; +} + +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + if (avio_tell(s->pb) == 0) { + // first packet - write header + int ret; + if ((ret = apc_write_header_delayed(s, pkt)) < 0) + return ret; + } + + avio_write(s->pb, pkt->data, pkt->size); + return 0; +} + +static int apc_write_trailer(AVFormatContext *s) +{ + int64_t file_size = avio_tell(s->pb); + + if (file_size < APC_HEADER_SIZE) { + av_log(s, AV_LOG_ERROR, "Got no packets\n"); + return AVERROR(EINVAL); + } + + // write length, if we're able to seek back + if (avio_seek(s->pb, 12, SEEK_SET) >= 0) { + int64_t data_size = file_size -= APC_HEADER_SIZE; + int channels = s->streams[0]->codecpar->ch_layout.nb_channels; + + if (data_size / channels > UINT32_MAX / 2) { + av_log(s, AV_LOG_ERROR, "File too large\n"); + return AVERROR(EINVAL); + } + + avio_wl32(s->pb, data_size * 2 / channels); + } + return 0; +} + +const FFOutputFormat ff_apc_muxer = { + .p.name = "apc", + .p.long_name = NULL_IF_CONFIG_SMALL("CRYO APC"), + .p.extensions = "apc", + .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_APC, + .write_header = apc_write_header, + .write_packet = apc_write_packet, + .write_trailer = apc_write_trailer, +}; + diff --git a/libavformat/version.h b/libavformat/version.h index de9cc8e31d..683184d5da 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,7 +31,7 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 20 +#define LIBAVFORMAT_VERSION_MINOR 21 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ -- 2.39.2