mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2024-11-26 19:01:44 +02:00
39a33038ce
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 <file.dfpwm>" 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. Signed-off-by: Jack Bruienne <jackbruienne@gmail.com>
135 lines
3.7 KiB
C
135 lines
3.7 KiB
C
/*
|
|
* 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;
|
|
|
|
if (ctx->channels <= 0) {
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid number of channels\n");
|
|
return AVERROR(EINVAL);
|
|
}
|
|
|
|
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;
|
|
|
|
frame->nb_samples = packet->size * 8 / ctx->channels;
|
|
if (frame->nb_samples <= 0) {
|
|
av_log(ctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
|
|
return AVERROR_INVALIDDATA;
|
|
}
|
|
|
|
if ((ret = ff_get_buffer(ctx, frame, 0)) < 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,
|
|
};
|