3 * Copyright (c) 2022 Jack Bruienne
4 * Copyright (c) 2012, 2016 Ben "GreaseMonkey" Russell
6 * This file is part of FFmpeg.
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.
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.
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
28 #include "libavutil/internal.h"
31 #include "codec_internal.h"
38 // DFPWM codec from https://github.com/ChenThread/dfpwm/blob/master/1a/
39 // Licensed in the public domain
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
)
45 for (int i
= 0; i
< len
; i
++) {
46 for (int j
= 0; j
< 8; j
++) {
49 int v
= *(inbuf
++) - 128;
51 int t
= (v
> state
->q
|| (v
== state
->q
&& v
== 127) ? 127 : -128);
57 nq
= state
->q
+ ((state
->s
* (t
-state
->q
) + 512)>>10);
58 if(nq
== state
->q
&& nq
!= t
)
59 nq
+= (t
== 127 ? 1 : -1);
63 st
= (t
!= state
->lt
? 0 : 1023);
66 ns
+= (st
!= 0 ? 1 : -1);
78 static av_cold
int dfpwm_enc_init(struct AVCodecContext
*ctx
)
80 DFPWMState
*state
= ctx
->priv_data
;
87 ctx
->bits_per_coded_sample
= 1;
92 static int dfpwm_enc_frame(struct AVCodecContext
*ctx
, struct AVPacket
*packet
,
93 const struct AVFrame
*frame
, int *got_packet
)
95 DFPWMState
*state
= ctx
->priv_data
;
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);
104 au_compress(state
, size
, packet
->data
, frame
->data
[0]);
110 const FFCodec ff_dfpwm_encoder
= {
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
,
117 FF_CODEC_ENCODE_CB(dfpwm_enc_frame
),
118 .p
.sample_fmts
= (const enum AVSampleFormat
[]){AV_SAMPLE_FMT_U8
, AV_SAMPLE_FMT_NONE
},
119 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_VARIABLE_FRAME_SIZE
|
120 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
,