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
33
34 typedef struct {
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).
43 {
44 unsigned d = 0;
45 for (
int i = 0;
i <
len;
i++) {
46 for (int j = 0; j < 8; j++) {
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
58 if(nq ==
state->q && nq != t)
59 nq += (t == 127 ? 1 : -1);
61
62 // adjust strength
63 st = (t !=
state->lt ? 0 : 1023);
66 ns += (st != 0 ? 1 : -1);
69
71 }
72
73 // output bits
74 *(outbuf++) = d;
75 }
76 }
77
79 {
81
86
87 ctx->bits_per_coded_sample = 1;
88
89 return 0;
90 }
91
94 {
96 int size =
frame->nb_samples *
frame->ch_layout.nb_channels / 8 + (
frame->nb_samples % 8 > 0 ? 1 : 0);
98
100 *got_packet = 0;
102 }
103
105
106 *got_packet = 1;
107 return 0;
108 }
109
121 };