FFmpeg
dfpwmenc.c
Go to the documentation of this file.
1 /*
2  * DFPWM encoder
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 encoder
26  */
27 
28 #include "libavutil/internal.h"
29 #include "avcodec.h"
30 #include "codec_id.h"
31 #include "codec_internal.h"
32 #include "encode.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 // note, len denotes how many compressed bytes there are (uncompressed bytes / 8).
42 static void au_compress(DFPWMState *state, int len, uint8_t *outbuf, const uint8_t *inbuf)
43 {
44  unsigned d = 0;
45  for (int i = 0; i < len; i++) {
46  for (int j = 0; j < 8; j++) {
47  int nq, st, ns;
48  // get sample
49  int v = *(inbuf++) - 128;
50  // set bit / target
51  int t = (v > state->q || (v == state->q && v == 127) ? 127 : -128);
52  d >>= 1;
53  if(t > 0)
54  d |= 0x80;
55 
56  // adjust charge
57  nq = state->q + ((state->s * (t-state->q) + 512)>>10);
58  if(nq == state->q && nq != t)
59  nq += (t == 127 ? 1 : -1);
60  state->q = nq;
61 
62  // adjust strength
63  st = (t != state->lt ? 0 : 1023);
64  ns = state->s;
65  if(ns != st)
66  ns += (st != 0 ? 1 : -1);
67  if(ns < 8) ns = 8;
68  state->s = ns;
69 
70  state->lt = t;
71  }
72 
73  // output bits
74  *(outbuf++) = d;
75  }
76 }
77 
79 {
81 
82  state->fq = 0;
83  state->q = 0;
84  state->s = 0;
85  state->lt = -128;
86 
87  ctx->bits_per_coded_sample = 1;
88 
89  return 0;
90 }
91 
92 static int dfpwm_enc_frame(struct AVCodecContext *ctx, struct AVPacket *packet,
93  const struct AVFrame *frame, int *got_packet)
94 {
96  int size = frame->nb_samples * frame->ch_layout.nb_channels / 8 + (frame->nb_samples % 8 > 0 ? 1 : 0);
97  int ret = ff_get_encode_buffer(ctx, packet, size, 0);
98 
99  if (ret) {
100  *got_packet = 0;
101  return ret;
102  }
103 
104  au_compress(state, size, packet->data, frame->data[0]);
105 
106  *got_packet = 1;
107  return 0;
108 }
109 
111  .p.name = "dfpwm",
112  CODEC_LONG_NAME("DFPWM1a audio"),
113  .p.type = AVMEDIA_TYPE_AUDIO,
114  .p.id = AV_CODEC_ID_DFPWM,
115  .priv_data_size = sizeof(DFPWMState),
116  .init = dfpwm_enc_init,
118  .p.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE},
121 };
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
encode.h
au_compress
static void au_compress(DFPWMState *state, int len, uint8_t *outbuf, const uint8_t *inbuf)
Definition: dfpwmenc.c:42
FFCodec
Definition: codec_internal.h:127
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
dfpwm_enc_frame
static int dfpwm_enc_frame(struct AVCodecContext *ctx, struct AVPacket *packet, const struct AVFrame *frame, int *got_packet)
Definition: dfpwmenc.c:92
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
av_cold
#define av_cold
Definition: attributes.h:90
codec_id.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
ctx
AVFormatContext * ctx
Definition: movenc.c:49
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
state
static struct @466 state
dfpwm_enc_init
static av_cold int dfpwm_enc_init(struct AVCodecContext *ctx)
Definition: dfpwmenc.c:78
AV_CODEC_CAP_VARIABLE_FRAME_SIZE
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:128
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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
codec_internal.h
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
size
int size
Definition: twinvq_data.h:10344
ns
#define ns(max_value, name, subs,...)
Definition: cbs_av1.c:616
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
internal.h
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
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
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
ff_dfpwm_encoder
const FFCodec ff_dfpwm_encoder
Definition: dfpwmenc.c:110
DFPWMState
Definition: dfpwmdec.c:34
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AV_CODEC_ID_DFPWM
@ AV_CODEC_ID_DFPWM
Definition: codec_id.h:542
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328