FFmpeg
dfpwmdec.c
Go to the documentation of this file.
1 /*
2  * DFPWM decoder
3  * Copyright (c) 2022 Jack Bruienne
4  * Copyright (c) 2012, 2016 Ben "GreaseMonkey" Russell
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * DFPWM1a decoder
26  */
27 
28 #include "libavutil/internal.h"
29 #include "avcodec.h"
30 #include "codec_id.h"
31 #include "codec_internal.h"
32 #include "internal.h"
33 
34 typedef struct {
35  int fq, q, s, lt;
36 } DFPWMState;
37 
38 // DFPWM codec from https://github.com/ChenThread/dfpwm/blob/master/1a/
39 // Licensed in the public domain
40 
41 static void au_decompress(DFPWMState *state, int fs, int len,
42  uint8_t *outbuf, const uint8_t *inbuf)
43 {
44  unsigned d;
45  for (int i = 0; i < len; i++) {
46  // get bits
47  d = *(inbuf++);
48  for (int j = 0; j < 8; j++) {
49  int nq, lq, st, ns, ov;
50  // set target
51  int t = ((d&1) ? 127 : -128);
52  d >>= 1;
53 
54  // adjust charge
55  nq = state->q + ((state->s * (t-state->q) + 512)>>10);
56  if(nq == state->q && nq != t)
57  nq += (t == 127 ? 1 : -1);
58  lq = state->q;
59  state->q = nq;
60 
61  // adjust strength
62  st = (t != state->lt ? 0 : 1023);
63  ns = state->s;
64  if(ns != st)
65  ns += (st != 0 ? 1 : -1);
66  if(ns < 8) ns = 8;
67  state->s = ns;
68 
69  // FILTER: perform antijerk
70  ov = (t != state->lt ? (nq+lq+1)>>1 : nq);
71 
72  // FILTER: perform LPF
73  state->fq += ((fs*(ov-state->fq) + 0x80)>>8);
74  ov = state->fq;
75 
76  // output sample
77  *(outbuf++) = ov + 128;
78 
79  state->lt = t;
80  }
81  }
82 }
83 
85 {
87 
88  if (ctx->ch_layout.nb_channels <= 0) {
89  av_log(ctx, AV_LOG_ERROR, "Invalid number of channels\n");
90  return AVERROR(EINVAL);
91  }
92 
93  state->fq = 0;
94  state->q = 0;
95  state->s = 0;
96  state->lt = -128;
97 
98  ctx->sample_fmt = AV_SAMPLE_FMT_U8;
99  ctx->bits_per_raw_sample = 8;
100 
101  return 0;
102 }
103 
105  int *got_frame, struct AVPacket *packet)
106 {
108  int ret;
109 
110  if (packet->size * 8LL % ctx->ch_layout.nb_channels)
111  return AVERROR_PATCHWELCOME;
112 
113  frame->nb_samples = packet->size * 8LL / ctx->ch_layout.nb_channels;
114  if (frame->nb_samples <= 0) {
115  av_log(ctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
116  return AVERROR_INVALIDDATA;
117  }
118 
119  if ((ret = ff_get_buffer(ctx, frame, 0)) < 0)
120  return ret;
121 
122  au_decompress(state, 140, packet->size, frame->data[0], packet->data);
123 
124  *got_frame = 1;
125  return packet->size;
126 }
127 
129  .p.name = "dfpwm",
130  .p.long_name = NULL_IF_CONFIG_SMALL("DFPWM1a audio"),
131  .p.type = AVMEDIA_TYPE_AUDIO,
132  .p.id = AV_CODEC_ID_DFPWM,
133  .priv_data_size = sizeof(DFPWMState),
134  .init = dfpwm_dec_init,
136  .p.capabilities = AV_CODEC_CAP_DR1,
137 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:118
ff_dfpwm_decoder
const FFCodec ff_dfpwm_decoder
Definition: dfpwmdec.c:128
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:122
DFPWMState::s
int s
Definition: dfpwmdec.c:35
state
static struct @333 state
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
codec_id.h
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:260
au_decompress
static void au_decompress(DFPWMState *state, int fs, int len, uint8_t *outbuf, const uint8_t *inbuf)
Definition: dfpwmdec.c:41
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:258
dfpwm_dec_frame
static int dfpwm_dec_frame(struct AVCodecContext *ctx, AVFrame *frame, int *got_frame, struct AVPacket *packet)
Definition: dfpwmdec.c:104
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1462
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
codec_internal.h
ns
#define ns(max_value, name, subs,...)
Definition: cbs_av1.c:682
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
len
int len
Definition: vorbis_enc_data.h:426
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
dfpwm_dec_init
static av_cold int dfpwm_dec_init(struct AVCodecContext *ctx)
Definition: dfpwmdec.c:84
AVCodecContext
main external API structure.
Definition: avcodec.h:398
DFPWMState
Definition: dfpwmdec.c:34
AVPacket
This structure stores compressed data.
Definition: packet.h:351
d
d
Definition: ffmpeg_filter.c:155
AV_CODEC_ID_DFPWM
@ AV_CODEC_ID_DFPWM
Definition: codec_id.h:525
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1241