From patchwork Mon May 3 11:31:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zane van Iperen X-Patchwork-Id: 27565 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a10:399:0:0:0:0 with SMTP id 25csp158996pxh; Mon, 3 May 2021 04:32:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0Wez9Wg3t8Qq7kwY/mffJVhprDBMMhNbLDbJf9+1an77nQyrEfgy9V6omRNUo+6OOE6uL X-Received: by 2002:a05:6402:4383:: with SMTP id o3mr11260475edc.333.1620041566761; Mon, 03 May 2021 04:32:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620041566; cv=none; d=google.com; s=arc-20160816; b=GuNIU/V7CYhN3Fctwh15GpN8PW7cZtN4/yOl+Miepqn2lY4gdMwmX6XHhPUmKmtGqY e0+CaPY7aOEuHeJVaM7TlQSvevr88QRQGmGUKtuAWH2m4b8x3RfTaUpOBvZfwcQME5N9 1ghPgR4jZWqqCFkVOl5CCn4C5J13/OJ8ofQrSM9OiSIWI05U54YX17F6jYlaj5LZwv5D tTp3fEh3xovjF7BZYR8oNUzPrmz5ND4JPQ/zUgJLVQVIJAYvN4CKG3+5yUJef+rOIbgk Pz8OD5B521KsEu2USxoZ89mAgLoiM48eNAXnH9eUHVpmOQ4xGsZj95HKfIm2FPoJM9+y pc5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=0PKy7MCs1ZxwsTU3dhjoGIX9M2ixl/p4rAN0C+R3bLo=; b=f0i71LGVWpYQEgzC82ZuBCOCYDzAaZUeiTSbbwHqbFjxoqr61ZvFKUMl0CFPfJ0HfJ 6ORUMBwYb4hdnaWNJqX7WG7+D9sw+F4K7AMkegEmccj7Cxh/vKAWQrh0uE14odXvo7MI 1EHUJqMF4BTv+Rh6qBxrQSAQCXdLJHqb2ysfFC786Q+hFuvtyfFp7K4gKixc7OdrNGJz Yw7cnRBs2/T0jckG4FpkIzxcFWwGXWFrC9mbSFYhihw9y3RO1tb4sA1DbgnMmE04j+CO RSIAUimoMFhoq01K2LxF60/1YRCvShjoXD77g+0Yp44bl3uIVYQYTDX59/3yx0c2ANMn VJMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@zanevaniperen.com header.s=key1 header.b=AWtF+c3+; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zanevaniperen.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id b13si9879979ede.27.2021.05.03.04.32.46; Mon, 03 May 2021 04:32:46 -0700 (PDT) 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; dkim=neutral (body hash did not verify) header.i=@zanevaniperen.com header.s=key1 header.b=AWtF+c3+; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zanevaniperen.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E53FA68814B; Mon, 3 May 2021 14:32:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1148C688081 for ; Mon, 3 May 2021 14:32:11 +0300 (EEST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=key1; t=1620041530; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=U3g1Sw5vQCQpoXFE9JdNm+gXZHvdKrqotgtkHEPv0Lc=; b=AWtF+c3+v4fnsYbvZMdYo6ukcPfBRkp8mgRBdS1IdGEcsl01KmDbN4IGjMR1agCMN481Tm n7nozJy4ZF9sYVWrZOZu4ju7rI9f+iI0yWpwvp0eOrOTebZK4VANFMnbNGKJZLdUcPAIOG RMbt/GoSawZ+vUvgctZIeJZyiQh7CH7LkqcCye2dej9GjAY3exhfCPSf4yA/kWlIyIirG6 BnZsyH8FozXnEBEsdR2y6wYbNViaVh7k5/Tv1frmYGI2BmkOSTSaYrf5i0Ix60BVQudRTI s2c4siocg7cQ1XmpuwqCgPexDfas9ASMiNqCx3y6rdpbTK16PjvHopGDLqu8Zw== From: Zane van Iperen To: ffmpeg-devel@ffmpeg.org Date: Mon, 3 May 2021 21:31:57 +1000 Message-Id: <20210503113157.2215-4-zane@zanevaniperen.com> In-Reply-To: <20210503113157.2215-1-zane@zanevaniperen.com> References: <20210503113157.2215-1-zane@zanevaniperen.com> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: git-morningstar@zanevaniperen.com Subject: [FFmpeg-devel] [PATCH 4/4] avformat: add Argonaut Games CVG 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: octhIcVJf/Cr Signed-off-by: Zane van Iperen --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/argo_cvg.c | 149 ++++++++++++++++++++++++++++++++++++++- libavformat/version.h | 2 +- 5 files changed, 152 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index 9567009d63..41b1690dfd 100644 --- a/Changelog +++ b/Changelog @@ -5,6 +5,7 @@ version : - ADPCM IMA Westwood encoder - Westwood AUD muxer - Argonaut Games CVG demuxer +- Argonaut Games CVG muxer version 4.4: diff --git a/libavformat/Makefile b/libavformat/Makefile index 0dca1ffd77..c9ef564523 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -108,6 +108,7 @@ OBJS-$(CONFIG_ARGO_ASF_DEMUXER) += argo_asf.o OBJS-$(CONFIG_ARGO_ASF_MUXER) += argo_asf.o OBJS-$(CONFIG_ARGO_BRP_DEMUXER) += argo_brp.o argo_asf.o OBJS-$(CONFIG_ARGO_CVG_DEMUXER) += argo_cvg.o +OBJS-$(CONFIG_ARGO_CVG_MUXER) += argo_cvg.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 923af3f649..111ca3cbf0 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -67,6 +67,7 @@ extern const AVInputFormat ff_argo_asf_demuxer; extern const AVOutputFormat ff_argo_asf_muxer; extern const AVInputFormat ff_argo_brp_demuxer; extern const AVInputFormat ff_argo_cvg_demuxer; +extern const AVOutputFormat ff_argo_cvg_muxer; extern const AVInputFormat ff_asf_demuxer; extern const AVOutputFormat ff_asf_muxer; extern const AVInputFormat ff_asf_o_demuxer; diff --git a/libavformat/argo_cvg.c b/libavformat/argo_cvg.c index 2851c1649f..f0d9f3e076 100644 --- a/libavformat/argo_cvg.c +++ b/libavformat/argo_cvg.c @@ -1,5 +1,5 @@ /* - * Argonaut Games CVG demuxer + * Argonaut Games CVG (de)muxer * * Copyright (C) 2021 Zane van Iperen (zane@zanevaniperen.com) * @@ -21,6 +21,7 @@ */ #include "avformat.h" #include "internal.h" +#include "libavutil/opt.h" #include "libavutil/intreadwrite.h" /* @@ -53,6 +54,14 @@ typedef struct ArgoCVGDemuxContext { uint32_t blocks_read; } ArgoCVGDemuxContext; +typedef struct ArgoCVGMuxContext { + const AVClass *class; + int skip_rate_check; + uint32_t checksum; + size_t size; +} ArgoCVGMuxContext; + +#if CONFIG_ARGO_CVG_DEMUXER /* "Special" files that are played at a different rate. */ static ArgoCVGOverride overrides[] = { { "CRYS.CVG", { 23592, 0, 1 }, 2495499, 88200 }, /* Beta */ @@ -243,3 +252,141 @@ const AVInputFormat ff_argo_cvg_demuxer = { .read_packet = argo_cvg_read_packet, .read_seek = argo_cvg_seek, }; +#endif + +#if CONFIG_ARGO_CVG_MUXER +static int argo_cvg_write_init(AVFormatContext *s) +{ + ArgoCVGMuxContext *ctx = s->priv_data; + const AVCodecParameters *par; + + if (s->nb_streams != 1) { + av_log(s, AV_LOG_ERROR, "CVG files have exactly one stream\n"); + return AVERROR(EINVAL); + } + + par = s->streams[0]->codecpar; + + if (par->codec_id != AV_CODEC_ID_ADPCM_PSX) { + av_log(s, AV_LOG_ERROR, "%s codec not supported\n", + avcodec_get_name(par->codec_id)); + return AVERROR(EINVAL); + } + + if (par->channels != 1) { + av_log(s, AV_LOG_ERROR, "CVG files only support 1 channel\n"); + return AVERROR(EINVAL); + } + + if (par->block_align != ARGO_CVG_BLOCK_ALIGN) + return AVERROR(EINVAL); + + if (!ctx->skip_rate_check && par->sample_rate != 22050) { + av_log(s, AV_LOG_ERROR, "Sample rate must be 22050\n"); + return AVERROR(EINVAL); + } + + if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) { + av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n"); + return AVERROR(EINVAL); + } + + return 0; +} + +static int argo_cvg_write_header(AVFormatContext *s) +{ + ArgoCVGMuxContext *ctx = s->priv_data; + + avio_wl32(s->pb, 0); /* Size, fixed later. */ + avio_wl32(s->pb, 0); + avio_wl32(s->pb, 1); + + ctx->checksum = 1; + ctx->size = 8; + return 0; +} + +static int argo_cvg_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + ArgoCVGMuxContext *ctx = s->priv_data; + AVCodecParameters *par = s->streams[0]->codecpar; + + if (pkt->size % par->block_align != 0) + return AVERROR_INVALIDDATA; + + avio_write(s->pb, pkt->data, pkt->size); + + ctx->size += pkt->size; + + if (ctx->size > UINT32_MAX) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < pkt->size; i++) + ctx->checksum += pkt->data[i]; + + return 0; +} + +static int argo_cvg_write_trailer(AVFormatContext *s) +{ + ArgoCVGMuxContext *ctx = s->priv_data; + int64_t ret; + + av_log(s, AV_LOG_TRACE, "size = %zu\n", ctx->size); + av_log(s, AV_LOG_TRACE, "checksum = %u\n", ctx->checksum); + + /* + * NB: This is wrong. We're always slightly under the original. + * Verified by remuxing. For reference (orig - remuxed): + * - TCLD.CVG: 4706074 - 4705696 = 378 + * - DANLOOP1.CVG: 5684641 - 5684212 = 429 + * - CRYS.CVG: 2495499 - 2495367 = 132 + * - PICKUP88.CVG: 1348091 - 1347937 = 154 + * - SELECT1.CVG: 549987 - 549752 = 235 + * Also NB: it doesn't matter, the game doesn't check them. + */ + avio_wl32(s->pb, ctx->checksum); + + if ((ret = avio_seek(s->pb, 0, SEEK_SET) < 0)) + return ret; + + avio_wl32(s->pb, (uint32_t)ctx->size); + return 0; +} + +static const AVOption argo_cvg_options[] = { + { + .name = "skip_rate_check", + .help = "skip sample rate check", + .offset = offsetof(ArgoCVGMuxContext, skip_rate_check), + .type = AV_OPT_TYPE_BOOL, + .default_val = {.i64 = 0}, + .min = 0, + .max = 1, + .flags = AV_OPT_FLAG_ENCODING_PARAM + }, + { NULL } +}; + +static const AVClass argo_cvg_muxer_class = { + .class_name = "argo_cvg_muxer", + .item_name = av_default_item_name, + .option = argo_cvg_options, + .version = LIBAVUTIL_VERSION_INT +}; + +const AVOutputFormat ff_argo_cvg_muxer = { + .name = "argo_cvg", + .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games CVG"), + .extensions = "cvg", + .audio_codec = AV_CODEC_ID_ADPCM_PSX, + .video_codec = AV_CODEC_ID_NONE, + .init = argo_cvg_write_init, + .write_header = argo_cvg_write_header, + .write_packet = argo_cvg_write_packet, + .write_trailer = argo_cvg_write_trailer, + .priv_class = &argo_cvg_muxer_class, + .priv_data_size = sizeof(ArgoCVGMuxContext) +}; +#endif diff --git a/libavformat/version.h b/libavformat/version.h index 53273334e2..cb9f5d33dc 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 59 -#define LIBAVFORMAT_VERSION_MINOR 1 +#define LIBAVFORMAT_VERSION_MINOR 2 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \