From patchwork Sun Feb 27 21:37:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jack Bruienne X-Patchwork-Id: 34539 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp2152043nkx; Sun, 27 Feb 2022 13:37:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJxC1PRwA2PrHI/ebXfWS7wP9nMnO1LbAdx57yV1HDWTGkFqPg8VzTFSd4pQFNFDxr9G1wzP X-Received: by 2002:a05:6402:375:b0:404:c2be:3b8c with SMTP id s21-20020a056402037500b00404c2be3b8cmr16237775edw.247.1645997841122; Sun, 27 Feb 2022 13:37:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645997841; cv=none; d=google.com; s=arc-20160816; b=KjwFhWvsZXhOzUqB4SXm+fo/3OOsIppUVqk8qvDYKjqC3HTezKjXL0pbyo3hYKMEWy Zj7c3TtYLAzDEw0vQc1+L1kUYWORLcnpPs3p51f3bORB/i8I4a0rHuD8Fh5TtXGxj/DV OVcj77dkMxBebOhQBqwmlMGTWPVgAATxxou37J0tBtBUIxd6kKEoZXEwXt+2UhplJzwq For7jRCPAH6sb853+G5ijkTqOkWbO/JpFVGDdX2eZu14jAlPs22Y7KxEN3GdKhy813B0 kwtYzkl32qNyue+fUafjH8Dwzv0Hm93C/NfBlKVP/Pe3l/eBo+UmvkwEdnzdcTqUQleO shRA== 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 :content-language:to:from:user-agent:mime-version:date:message-id :dkim-signature:delivered-to; bh=8l9JqdHunybXW/6nsi6mczPdGP57LZtQ51PaHHThkwU=; b=WXGgEbZnLcQgrOqRmmetkHmujgtJOXHHHktVyhoTyGsvdKdg9/NJ/RHaSqAI7oDy0h mksLNXJvZy9J+BbPXAlZ7/WL+caY6591OJpIc7bmWG9zgkrYjst8vu3sA8tP7ufsaQZ6 mr6KR5QMp0itiLq6vJCdfgiU3gUVLjKx0xNEFheRM0uVNgZ2j7P95DMuQpZbrDtAym2l g941TdyAK7+B1B2cxMoME/3NWqV1OUzgO9A2k7Uy5LGedhjEyQ0YijQ+5fwFXjvfR8q5 sJu7HTqTA5MKBJBJdBzHeg4Y7AXvuRXeBNbQik0wQ2e9ImvfhyhrrjYb6elj7R0t8HoM Za2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=J9YIzJef; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q2-20020a056402032200b00412e15e0a42si5405840edw.632.2022.02.27.13.37.20; Sun, 27 Feb 2022 13:37:21 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=J9YIzJef; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 53D3E68B13C; Sun, 27 Feb 2022 23:37:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f44.google.com (mail-io1-f44.google.com [209.85.166.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 373B068B13C for ; Sun, 27 Feb 2022 23:37:13 +0200 (EET) Received: by mail-io1-f44.google.com with SMTP id d62so12746856iog.13 for ; Sun, 27 Feb 2022 13:37:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:from:subject:to :content-language; bh=758LjyVIFY4CcFqazACEjZYZ3ev0MVfsf13pYoLRSt4=; b=J9YIzJefWuAwkI74O8QJ2f63amqW1wDRP43ZcGX6CE4NuAbH8L6gYdTMx5s2axQPrr 0HfEQFUTNh6yS8VAOkaqH88vepghfUUl6gIG0CkeBs5/mSD1zS1rXQPm/Pgkwud9K+KM LCxKC1omcjr2SzJZuZFWzvOScJqELGoTmf6NY+ztt3KLmZfO7feUkkKkWjTzyfY/Gqwm veyC3lGUjCIEJnCqrEXubRq1Edn3J1KoIs3zxe9uYk+3QVQgh6zoHrQj7n4+ZCUqHPOV Gr1Mw29PxnZjNE+iGmtbkWQG1xgfOefe9XbiiByPcM1kuFcl3nqVdAqs/10Zt34NkroI AynQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:content-language; bh=758LjyVIFY4CcFqazACEjZYZ3ev0MVfsf13pYoLRSt4=; b=G5sk1wi7VFjSZ5lBo/javDbQ+f2y9a8OgxR6sKYAuwrO/axYjtijsA0ELlS7piILQI GfATS/OKjFzLjk9S1pAzZticRZoCEWSuSVkvOd6M8EMcrih6x+kVowg4pXOnm0t7VLLP ozcv720wJwyUmiTqA+4KjBoKWdVbwQcbRFZ9IdDwZnpCZCJgApzWAZGxn2Gx+x0WJrzT Il3aHqMchLEi4+3iOezUqnc5vnVZRnuZhv6FbSVTY+YHdMvNWwLnOyO2zR3uNYShCOoP kLSUMR2g32ojHzfXJ/QCvR3+YarB1C3R6z6BtMuGUu5bVdDwf/rEC6X+/+41v6iKJ/C/ ZarQ== X-Gm-Message-State: AOAM532unO+urlvMRpxcv6BWiLq1W5g8nfPI6EhoaHB3jRqYRf/LrCVg hRx0vGWb4PVk7tx8JIN/Gvpapksv8axLEQ== X-Received: by 2002:a6b:d10c:0:b0:641:63b2:9ef8 with SMTP id l12-20020a6bd10c000000b0064163b29ef8mr13302990iob.135.1645997830851; Sun, 27 Feb 2022 13:37:10 -0800 (PST) Received: from [192.168.7.86] (c-68-40-205-18.hsd1.mi.comcast.net. [68.40.205.18]) by smtp.gmail.com with ESMTPSA id i16-20020a056e02055000b002c2877213bdsm5270377ils.3.2022.02.27.13.37.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Feb 2022 13:37:10 -0800 (PST) Message-ID: Date: Sun, 27 Feb 2022 16:37:08 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 From: Jack Bruienne To: ffmpeg-devel@ffmpeg.org Content-Language: en-US Subject: [FFmpeg-devel] [PATCH v5 1/2] libavcodec: Added DFPWM1a codec 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: k68TmTtnL7Ru From the wiki page (https://wiki.vexatos.com/dfpwm): > DFPWM (Dynamic Filter Pulse Width Modulation) is an audio codec > created by Ben “GreaseMonkey” Russell in 2012, originally to be used > as a voice codec for asiekierka's pixmess, a C remake of 64pixels. > It is a 1-bit-per-sample codec which uses a dynamic-strength one-pole > low-pass filter as a predictor. Due to the fact that a raw DPFWM decoding > creates a high-pitched whine, it is often followed by some post-processing > filters to make the stream more listenable. It has recently gained popularity through the ComputerCraft mod for Minecraft, which added support for audio through this codec, as well as the Computronics expansion which preceeded the official support. These both implement the slightly adjusted 1a version of the codec, which is the version I have chosen for this patch. This patch adds a new codec (with encoding and decoding) for DFPWM1a. The codec sources are pretty simple: they use the reference codec with a basic wrapper to connect it to the FFmpeg AVCodec system. To clarify, the codec does not have a specific sample rate - it is provided by the container (or user), which is typically 48000, but has also been known to be 32768. The codec does not specify channel info either, and it's pretty much always used with one mono channel. However, since it appears that libavcodec expects both sample rate and channel count to be handled by either the codec or container, I have made the decision to allow multiple channels interleaved, which as far as I know has never been used, but it works fine here nevertheless. The accompanying raw format has a channels option to set this. (I expect most users of this will not use multiple channels, but it remains an option just in case.) This patch will be highly useful to ComputerCraft developers who are working with audio, as it is the standard format for audio, and there are few user-friendly encoders out there, and even fewer decoders. It will streamline the process for importing and listening to audio, replacing the need to write code or use tools that require very specific input formats. You may use the CraftOS-PC program (https://www.craftos-pc.cc) to test out DFPWM playback. To use it, run the program and type this command: "attach left speaker" Then run "speaker play " for each file. The app runs in a sandbox, so files have to be transferred in first; the easiest way to do this is to simply drag the file on the window. (Or copy files to the folder at https://www.craftos-pc.cc/docs/saves.) Sample DFPWM files can be generated with an online tool at https://music.madefor.cc. This is the current best way to encode DFPWM files. Simply drag an audio file onto the page, and it will encode it, giving a download link on the page. I've made sure to update all of the docs as per Developer§7, and I've tested it as per section 8. Test files encoded to DFPWM play correctly in ComputerCraft, and other files that work in CC are correctly decoded. I have also verified that corrupt files do not crash the decoder - this should theoretically not be an issue as the result size is constant with respect to the input size. Changes since v4: Fixed missing channel check in decoder. Changes since v3: Added support for multiple interleaved channels, and cleaned up the code a bunch. Changes since v2: I've found that the reference encoder has a few errors, and sounds worse than the Java-based implementation that is used most often. I got in contact with someone who knows DFPWM much better than I do, and I worked with them to make a few adjustments that should improve the audio quality. I also made sure that the output matches the Java codec exactly, so it should have the exact same quality as other codecs. Signed-off-by: Jack Bruienne --- Changelog | 1 + MAINTAINERS | 1 + doc/general_contents.texi | 1 + libavcodec/Makefile | 2 + libavcodec/allcodecs.c | 2 + libavcodec/codec_desc.c | 7 ++ libavcodec/codec_id.h | 1 + libavcodec/dfpwmdec.c | 133 ++++++++++++++++++++++++++++++++++++++ libavcodec/dfpwmenc.c | 121 ++++++++++++++++++++++++++++++++++ libavcodec/utils.c | 2 + libavcodec/version.h | 2 +- 11 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 libavcodec/dfpwmdec.c create mode 100644 libavcodec/dfpwmenc.c diff --git a/Changelog b/Changelog index 5ad2cef..5170a6a 100644 --- a/Changelog +++ b/Changelog @@ -4,6 +4,7 @@ releases are sorted from youngest to oldest. version 5.1: - dialogue enhance audio filter - dropped obsolete XvMC hwaccel +- DFPWM audio encoder/decoder version 5.0: diff --git a/MAINTAINERS b/MAINTAINERS index f33ccbd..57b6f33 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -161,6 +161,7 @@ Codecs: cscd.c Reimar Doeffinger cuviddec.c Timo Rothenpieler dca* foo86 + dfpwm* Jack Bruienne dirac* Rostislav Pehlivanov dnxhd* Baptiste Coudurier dolby_e* foo86 diff --git a/doc/general_contents.texi b/doc/general_contents.texi index df1692c..14aeaed 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -1194,6 +1194,7 @@ following image formats are supported: @item CRI HCA @tab @tab X @item Delphine Software International CIN audio @tab @tab X @tab Codec used in Delphine Software International games. +@item DFPWM @tab X @tab X @item Digital Speech Standard - Standard Play mode (DSS SP) @tab @tab X @item Discworld II BMV Audio @tab @tab X @item COOK @tab @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 6076b4a..7474220 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -289,6 +289,8 @@ OBJS-$(CONFIG_DERF_DPCM_DECODER) += dpcm.o OBJS-$(CONFIG_DIRAC_DECODER) += diracdec.o dirac.o diracdsp.o diractab.o \ dirac_arith.o dirac_dwt.o dirac_vlc.o OBJS-$(CONFIG_DFA_DECODER) += dfa.o +OBJS-$(CONFIG_DFPWM_DECODER) += dfpwmdec.o +OBJS-$(CONFIG_DFPWM_ENCODER) += dfpwmenc.o OBJS-$(CONFIG_DNXHD_DECODER) += dnxhddec.o dnxhddata.o OBJS-$(CONFIG_DNXHD_ENCODER) += dnxhdenc.o dnxhddata.o OBJS-$(CONFIG_DOLBY_E_DECODER) += dolby_e.o dolby_e_parse.o kbdwin.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index d1e1019..c3a0c26 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -437,6 +437,8 @@ extern const AVCodec ff_bmv_audio_decoder; extern const AVCodec ff_cook_decoder; extern const AVCodec ff_dca_encoder; extern const AVCodec ff_dca_decoder; +extern const AVCodec ff_dfpwm_encoder; +extern const AVCodec ff_dfpwm_decoder; extern const AVCodec ff_dolby_e_decoder; extern const AVCodec ff_dsd_lsbf_decoder; extern const AVCodec ff_dsd_msbf_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 725c687..81f3b3c 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -3237,6 +3237,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("MSN Siren"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_DFPWM, + .type = AVMEDIA_TYPE_AUDIO, + .name = "dfpwm", + .long_name = NULL_IF_CONFIG_SMALL("DFPWM (Dynamic Filter Pulse Width Modulation)"), + .props = AV_CODEC_PROP_LOSSY, + }, /* subtitle codecs */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index ab265ec..3ffb9bd 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -516,6 +516,7 @@ enum AVCodecID { AV_CODEC_ID_HCA, AV_CODEC_ID_FASTAUDIO, AV_CODEC_ID_MSNSIREN, + AV_CODEC_ID_DFPWM, /* subtitle codecs */ AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs. diff --git a/libavcodec/dfpwmdec.c b/libavcodec/dfpwmdec.c new file mode 100644 index 0000000..7946428 --- /dev/null +++ b/libavcodec/dfpwmdec.c @@ -0,0 +1,133 @@ +/* + * DFPWM decoder + * Copyright (c) 2022 Jack Bruienne + * Copyright (c) 2012, 2016 Ben "GreaseMonkey" Russell + * + * 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 + */ + +/** + * @file + * DFPWM1a decoder + */ + +#include "libavutil/internal.h" +#include "avcodec.h" +#include "codec_id.h" +#include "internal.h" + +typedef struct { + int fq, q, s, lt; +} DFPWMState; + +// DFPWM codec from https://github.com/ChenThread/dfpwm/blob/master/1a/ +// Licensed in the public domain + +static void au_decompress(DFPWMState *state, int fs, int len, uint8_t *outbuf, uint8_t *inbuf) +{ + unsigned d; + for (int i = 0; i < len; i++) { + // get bits + d = *(inbuf++); + for (int j = 0; j < 8; j++) { + int nq, lq, st, ns, ov; + // set target + int t = ((d&1) ? 127 : -128); + d >>= 1; + + // adjust charge + nq = state->q + ((state->s * (t-state->q) + 512)>>10); + if(nq == state->q && nq != t) + nq += (t == 127 ? 1 : -1); + lq = state->q; + state->q = nq; + + // adjust strength + st = (t != state->lt ? 0 : 1023); + ns = state->s; + if(ns != st) + ns += (st != 0 ? 1 : -1); + if(ns < 8) ns = 8; + state->s = ns; + + // FILTER: perform antijerk + ov = (t != state->lt ? (nq+lq+1)>>1 : nq); + + // FILTER: perform LPF + state->fq += ((fs*(ov-state->fq) + 0x80)>>8); + ov = state->fq; + + // output sample + *(outbuf++) = ov + 128; + + state->lt = t; + } + } +} + +static av_cold int dfpwm_dec_init(struct AVCodecContext *ctx) +{ + DFPWMState *state = ctx->priv_data; + + state->fq = 0; + state->q = 0; + state->s = 0; + state->lt = -128; + + ctx->sample_fmt = AV_SAMPLE_FMT_U8; + ctx->bits_per_raw_sample = 8; + + return 0; +} + +static int dfpwm_dec_frame(struct AVCodecContext *ctx, void *data, + int *got_frame, struct AVPacket *packet) +{ + DFPWMState *state = ctx->priv_data; + AVFrame *frame = data; + int ret; + + if (ctx->channels <= 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid number of channels\n"); + *got_frame = 0; + return AVERROR(EINVAL); + } + + frame->nb_samples = packet->size * 8 / ctx->channels; + ret = ff_get_buffer(ctx, frame, 0); + if (ret) { + *got_frame = 0; + return ret; + } + + au_decompress(state, 140, packet->size, frame->data[0], packet->data); + + *got_frame = 1; + return packet->size; +} + +const AVCodec ff_dfpwm_decoder = { + .name = "dfpwm", + .long_name = NULL_IF_CONFIG_SMALL("DFPWM1a audio"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_DFPWM, + .priv_data_size = sizeof(DFPWMState), + .init = dfpwm_dec_init, + .decode = dfpwm_dec_frame, + .capabilities = AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, +}; diff --git a/libavcodec/dfpwmenc.c b/libavcodec/dfpwmenc.c new file mode 100644 index 0000000..02f2e64 --- /dev/null +++ b/libavcodec/dfpwmenc.c @@ -0,0 +1,121 @@ +/* + * DFPWM encoder + * Copyright (c) 2022 Jack Bruienne + * Copyright (c) 2012, 2016 Ben "GreaseMonkey" Russell + * + * 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 + */ + +/** + * @file + * DFPWM1a encoder + */ + +#include "libavutil/internal.h" +#include "avcodec.h" +#include "codec_id.h" +#include "encode.h" +#include "internal.h" + +typedef struct { + int fq, q, s, lt; +} DFPWMState; + +// DFPWM codec from https://github.com/ChenThread/dfpwm/blob/master/1a/ +// Licensed in the public domain + +// note, len denotes how many compressed bytes there are (uncompressed bytes / 8). +static void au_compress(DFPWMState *state, int len, uint8_t *outbuf, uint8_t *inbuf) +{ + unsigned d = 0; + for (int i = 0; i < len; i++) { + for (int j = 0; j < 8; j++) { + int nq, st, ns; + // get sample + int v = *(inbuf++) - 128; + // set bit / target + int t = (v > state->q || (v == state->q && v == 127) ? 127 : -128); + d >>= 1; + if(t > 0) + d |= 0x80; + + // adjust charge + nq = state->q + ((state->s * (t-state->q) + 512)>>10); + if(nq == state->q && nq != t) + nq += (t == 127 ? 1 : -1); + state->q = nq; + + // adjust strength + st = (t != state->lt ? 0 : 1023); + ns = state->s; + if(ns != st) + ns += (st != 0 ? 1 : -1); + if(ns < 8) ns = 8; + state->s = ns; + + state->lt = t; + } + + // output bits + *(outbuf++) = d; + } +} + +static av_cold int dfpwm_enc_init(struct AVCodecContext *ctx) +{ + DFPWMState *state = ctx->priv_data; + + state->fq = 0; + state->q = 0; + state->s = 0; + state->lt = -128; + + ctx->bits_per_coded_sample = 1; + + return 0; +} + +static int dfpwm_enc_frame(struct AVCodecContext *ctx, struct AVPacket *packet, + const struct AVFrame *frame, int *got_packet) +{ + DFPWMState *state = ctx->priv_data; + int size = frame->nb_samples * frame->channels / 8 + (frame->nb_samples % 8 > 0 ? 1 : 0); + int ret = ff_get_encode_buffer(ctx, packet, size, 0); + + if (ret) { + *got_packet = 0; + return ret; + } + + au_compress(state, size, packet->data, frame->data[0]); + + *got_packet = 1; + return 0; +} + +const AVCodec ff_dfpwm_encoder = { + .name = "dfpwm", + .long_name = NULL_IF_CONFIG_SMALL("DFPWM1a audio"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_DFPWM, + .priv_data_size = sizeof(DFPWMState), + .init = dfpwm_enc_init, + .encode2 = dfpwm_enc_frame, + .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE}, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, +}; diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 6f9d90a..066da76 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -577,6 +577,8 @@ enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be) int av_get_bits_per_sample(enum AVCodecID codec_id) { switch (codec_id) { + case AV_CODEC_ID_DFPWM: + return 1; case AV_CODEC_ID_ADPCM_SBPRO_2: return 2; case AV_CODEC_ID_ADPCM_SBPRO_3: diff --git a/libavcodec/version.h b/libavcodec/version.h index d900503..84f3979 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,7 +28,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 59 -#define LIBAVCODEC_VERSION_MINOR 21 +#define LIBAVCODEC_VERSION_MINOR 22 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ From patchwork Sun Feb 27 21:37:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jack Bruienne X-Patchwork-Id: 34540 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp2152114nkx; Sun, 27 Feb 2022 13:37:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJw5iXn0z8N5Q/gy+tybRKWIVzwXNLINHOdKGtQP8mMT+3VKe9E7LmdwHl3S0DoOs+mr97Zd X-Received: by 2002:a05:6402:46:b0:413:2cf2:e7e2 with SMTP id f6-20020a056402004600b004132cf2e7e2mr16270605edu.258.1645997851703; Sun, 27 Feb 2022 13:37:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645997851; cv=none; d=google.com; s=arc-20160816; b=pBzn/WwNYXAz7wdgCcUAqNVVRcgCBCNYIMmSH9a18R24VC8tMIoGxG3IeHeQ22GmtE 16uHRvdTa8CBRoCsw+96f6PwliNbCPJVfyZEtdIAEEejVJuK0Sk/NNRbvtXIBd1BDoMi D0eZxPwWfFNvQoTLnZYoOqQijohI41qR8gwZs8QiRqfIByE757Ncjx/z0J9AAt4vqKPy FXz9Byb4Zl92YHhm38fBksV1W6EmeYmY4kE6CpPGHmYA+/n/A9bRJjqf9X0RoVbz1jQE 49UjgdxkxdSwstyXR+HA48YO3Oof3xMDPL4xBM3wSPf4+8msItwQn3Fael1SOLovuDhM Mtaw== 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 :content-language:to:from:user-agent:mime-version:date:message-id :dkim-signature:delivered-to; bh=3bWZJlHgYlHOI4RSmaYAXujAXurhN94BrLnoI1wh+1I=; b=W4ciqwmnJ8/IOW8Q08cxC4fMWLub87kuStevCwNvxfdEFmXKhOMQdFFqIWcXV23A2E LRnG3MgDlUaPmjKtaqTV55dL1Z1cs38GAavBmZoWHBPjQazqxTIVf77iFAl6kFGyp9IC iMcyXys+SOYFmlxZiqRXMYP5hemZiGH5pNTfWXgYDT57WslE6LfvLciFcRY6ZupX9bxd cjMXcwxDiZTkebyg8n/xizf6yVbdZjS3Fc5h44eujhLxUsy0oqAUPNG2JihZjTU02GFl UN0Rn+VTwua2/hPHIOsVNu2VDt7zWDfU3H/cZvnNMQ1zICBXLywsmHuZSqamkpAUX/Fp NEJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b="FK/Xast0"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w3-20020a50d983000000b003fed229a560si5771037edj.97.2022.02.27.13.37.31; Sun, 27 Feb 2022 13:37:31 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b="FK/Xast0"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5F32F68B195; Sun, 27 Feb 2022 23:37:18 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-il1-f176.google.com (mail-il1-f176.google.com [209.85.166.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F2EA568A2C4 for ; Sun, 27 Feb 2022 23:37:16 +0200 (EET) Received: by mail-il1-f176.google.com with SMTP id d3so8624938ilr.10 for ; Sun, 27 Feb 2022 13:37:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:from:subject:to :content-language; bh=oH5UKIRQBZTOqFqnHYJcxQP2jnvlEl9QNRb5v6Rx6pI=; b=FK/Xast0se7fPIcWLwh1jwUK3jgF/NIBhVwsdTKB2um3v9d0GO8cJSDnmHh9nfNRER qFXCbrkx+f6wFkPX7c0Pn2XKlEcmQyB46nZJzThxX5Za9g5UtXDy59NFKfShV34KPhqu sRTVif+kZ6waS4ZVY+ttPGF28ftbQQziYq8xBDhUXl8lBZr3GMQY/8CItw44b+NYi0ie QUHfcI8FabmzKpUOlI66InXaQtkUaWOn8oMbU4AZVpUSxQYOPJDIn51zYQXbwEm9xBMR ckD63P6uO5g9P0Sb5LdMmzTGwNZbEa9u2clzPD0PIaDXtN0s/BI4IybXxdbFRou9daM8 rtDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:content-language; bh=oH5UKIRQBZTOqFqnHYJcxQP2jnvlEl9QNRb5v6Rx6pI=; b=44acKJp5+PEQ3+mL1+rvA1Oi941BEUWAdWrc7LBngjsKBMGwT02LsMVK0vMUQJhYVV YOmMWENoPOajxEnkuLLH2bDZ0oY1aDArsrSdSnFyipppPKaqYiy92KfWyuRnktSN3QRK 1l73EA2Bu68lVawk/jrkVHk+obzlg/oEFmRACGcW1GXaqLe4xU6I0Gcb8lRGt2pt7WFM VFfICCt3ZXdptn0rmxT9BcwTxUZd4hvn/QWCFcFgLhTqYqg6RQRqL/HfA6uAU3d6+U5W FQtMAO54oY84XajnusXqHWctsK2FvFLgyBwvGtPWI/hdZ8OcrfTsDsNArZUBguRRf6rx 16RQ== X-Gm-Message-State: AOAM5335LwTE8qbT020c1lIrnIWD6Elmkt+nj+hr8/3A3QlLtpQkBpAf d3Jz4X0dKeZQES7czLwE0cmdgNThkZif3g== X-Received: by 2002:a05:6e02:1d0d:b0:2c2:1d2c:5b2d with SMTP id i13-20020a056e021d0d00b002c21d2c5b2dmr15777080ila.168.1645997834909; Sun, 27 Feb 2022 13:37:14 -0800 (PST) Received: from [192.168.7.86] (c-68-40-205-18.hsd1.mi.comcast.net. [68.40.205.18]) by smtp.gmail.com with ESMTPSA id c7-20020a92dc87000000b002c2ed8cc0d1sm598229iln.10.2022.02.27.13.37.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Feb 2022 13:37:14 -0800 (PST) Message-ID: Date: Sun, 27 Feb 2022 16:37:12 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 From: Jack Bruienne To: ffmpeg-devel@ffmpeg.org Content-Language: en-US Subject: [FFmpeg-devel] [PATCH v5 2/2] libavformat: Add DFPWM raw format 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: dCeRTgXnV5CK This patch builds on my previous DFPWM codec patch, adding a raw audio format to be able to read/write the raw files that are most commonly used (as no other container format supports it yet). The muxers are mostly copied from the PCM demuxer and the raw muxers, as DFPWM is typically stored as raw data. Note on the channels argument: The -ac argument can only be used as an alias if -f dfpwm is passed. If missing, -ac has no effect. The -channels argument will work regardless of the presence of -f. Please see the previous patch for more information on DFPWM. Changes since v4: Fixed descriptions of formats. Changes since v2/v3: Removed unused MIME parsing code, and added channels option. Signed-off-by: Jack Bruienne --- Changelog | 2 +- MAINTAINERS | 1 + doc/general_contents.texi | 1 + libavformat/Makefile | 2 + libavformat/allformats.c | 2 + libavformat/dfpwmdec.c | 82 +++++++++++++++++++++++++++++++++++++++ libavformat/rawenc.c | 13 +++++++ libavformat/version.h | 4 +- 8 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 libavformat/dfpwmdec.c diff --git a/Changelog b/Changelog index 5170a6a..ec688da 100644 --- a/Changelog +++ b/Changelog @@ -4,7 +4,7 @@ releases are sorted from youngest to oldest. version 5.1: - dialogue enhance audio filter - dropped obsolete XvMC hwaccel -- DFPWM audio encoder/decoder +- DFPWM audio encoder/decoder and raw muxer/demuxer version 5.0: diff --git a/MAINTAINERS b/MAINTAINERS index 57b6f33..931cf4b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -416,6 +416,7 @@ Muxers/Demuxers: dashdec.c Steven Liu dashenc.c Karthick Jeyapal daud.c Reimar Doeffinger + dfpwmdec.c Jack Bruienne dss.c Oleksij Rempel dtsdec.c foo86 dtshddec.c Paul B Mahol diff --git a/doc/general_contents.texi b/doc/general_contents.texi index 14aeaed..fcd9da1 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -578,6 +578,7 @@ library: @item raw aptX @tab X @tab X @item raw aptX HD @tab X @tab X @item raw Chinese AVS video @tab X @tab X +@item raw DFPWM @tab X @tab X @item raw Dirac @tab X @tab X @item raw DNxHD @tab X @tab X @item raw DTS @tab X @tab X diff --git a/libavformat/Makefile b/libavformat/Makefile index 6566e40..b89073a 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -165,6 +165,8 @@ OBJS-$(CONFIG_DAUD_MUXER) += daudenc.o OBJS-$(CONFIG_DCSTR_DEMUXER) += dcstr.o OBJS-$(CONFIG_DERF_DEMUXER) += derf.o pcm.o OBJS-$(CONFIG_DFA_DEMUXER) += dfa.o +OBJS-$(CONFIG_DFPWM_DEMUXER) += dfpwmdec.o pcm.o +OBJS-$(CONFIG_DFPWM_MUXER) += rawenc.o OBJS-$(CONFIG_DHAV_DEMUXER) += dhav.o OBJS-$(CONFIG_DIRAC_DEMUXER) += diracdec.o rawdec.o OBJS-$(CONFIG_DIRAC_MUXER) += rawenc.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index d066a77..587ad59 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -124,6 +124,8 @@ extern const AVOutputFormat ff_daud_muxer; extern const AVInputFormat ff_dcstr_demuxer; extern const AVInputFormat ff_derf_demuxer; extern const AVInputFormat ff_dfa_demuxer; +extern const AVInputFormat ff_dfpwm_demuxer; +extern const AVOutputFormat ff_dfpwm_muxer; extern const AVInputFormat ff_dhav_demuxer; extern const AVInputFormat ff_dirac_demuxer; extern const AVOutputFormat ff_dirac_muxer; diff --git a/libavformat/dfpwmdec.c b/libavformat/dfpwmdec.c new file mode 100644 index 0000000..85d9b61 --- /dev/null +++ b/libavformat/dfpwmdec.c @@ -0,0 +1,82 @@ +/* + * RAW PCM demuxers + * Copyright (c) 2002 Fabrice Bellard + * Copyright (c) 2022 Jack Bruienne + * + * 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 "libavutil/avstring.h" +#include "avformat.h" +#include "internal.h" +#include "pcm.h" +#include "libavutil/log.h" +#include "libavutil/opt.h" +#include "libavutil/avassert.h" + +typedef struct DFPWMAudioDemuxerContext { + AVClass *class; + int sample_rate; + int channels; +} DFPWMAudioDemuxerContext; + +static int dfpwm_read_header(AVFormatContext *s) +{ + DFPWMAudioDemuxerContext *s1 = s->priv_data; + AVCodecParameters *par; + AVStream *st; + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + par = st->codecpar; + + par->codec_type = AVMEDIA_TYPE_AUDIO; + par->codec_id = s->iformat->raw_codec_id; + par->sample_rate = s1->sample_rate; + par->channels = s1->channels; + par->bits_per_coded_sample = 1; + par->block_align = 1; + + avpriv_set_pts_info(st, 64, 1, par->sample_rate); + return 0; +} + +static const AVOption dfpwm_options[] = { + { "sample_rate", "", offsetof(DFPWMAudioDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, + { "channels", "", offsetof(DFPWMAudioDemuxerContext, channels), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, + { NULL }, +}; +static const AVClass dfpwm_demuxer_class = { + .class_name = "dfpwm demuxer", + .item_name = av_default_item_name, + .option = dfpwm_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +const AVInputFormat ff_dfpwm_demuxer = { + .name = "dfpwm", + .long_name = NULL_IF_CONFIG_SMALL("raw DFPWM1a"), + .priv_data_size = sizeof(DFPWMAudioDemuxerContext), + .read_header = dfpwm_read_header, + .read_packet = ff_pcm_read_packet, + .read_seek = ff_pcm_read_seek, + .flags = AVFMT_GENERIC_INDEX, + .extensions = "dfpwm", + .raw_codec_id = AV_CODEC_ID_DFPWM, + .priv_class = &dfpwm_demuxer_class, +}; \ No newline at end of file diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c index 4bbae77..f8c267c 100644 --- a/libavformat/rawenc.c +++ b/libavformat/rawenc.c @@ -192,6 +192,19 @@ const AVOutputFormat ff_data_muxer = { }; #endif +#if CONFIG_DFPWM_MUXER +const AVOutputFormat ff_dfpwm_muxer = { + .name = "dfpwm", + .long_name = NULL_IF_CONFIG_SMALL("raw DFPWM1a"), + .extensions = "dfpwm", + .audio_codec = AV_CODEC_ID_DFPWM, + .video_codec = AV_CODEC_ID_NONE, + .init = force_one_stream, + .write_packet = ff_raw_write_packet, + .flags = AVFMT_NOTIMESTAMPS, +}; +#endif + #if CONFIG_DIRAC_MUXER const AVOutputFormat ff_dirac_muxer = { .name = "dirac", diff --git a/libavformat/version.h b/libavformat/version.h index 2623457..0f89af4 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 59 -#define LIBAVFORMAT_VERSION_MINOR 17 -#define LIBAVFORMAT_VERSION_MICRO 102 +#define LIBAVFORMAT_VERSION_MINOR 18 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \