Line | Branch | Exec | Source |
---|---|---|---|
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 "decode.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 | 11 | static void au_decompress(DFPWMState *state, int fs, int len, | |
42 | uint8_t *outbuf, const uint8_t *inbuf) | ||
43 | { | ||
44 | unsigned d; | ||
45 |
2/2✓ Branch 0 taken 5513 times.
✓ Branch 1 taken 11 times.
|
5524 | for (int i = 0; i < len; i++) { |
46 | // get bits | ||
47 | 5513 | d = *(inbuf++); | |
48 |
2/2✓ Branch 0 taken 44104 times.
✓ Branch 1 taken 5513 times.
|
49617 | for (int j = 0; j < 8; j++) { |
49 | int nq, lq, st, ns, ov; | ||
50 | // set target | ||
51 |
2/2✓ Branch 0 taken 21927 times.
✓ Branch 1 taken 22177 times.
|
44104 | int t = ((d&1) ? 127 : -128); |
52 | 44104 | d >>= 1; | |
53 | |||
54 | // adjust charge | ||
55 | 44104 | nq = state->q + ((state->s * (t-state->q) + 512)>>10); | |
56 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44103 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
44104 | if(nq == state->q && nq != t) |
57 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | nq += (t == 127 ? 1 : -1); |
58 | 44104 | lq = state->q; | |
59 | 44104 | state->q = nq; | |
60 | |||
61 | // adjust strength | ||
62 |
2/2✓ Branch 0 taken 22039 times.
✓ Branch 1 taken 22065 times.
|
44104 | st = (t != state->lt ? 0 : 1023); |
63 | 44104 | ns = state->s; | |
64 |
2/2✓ Branch 0 taken 44103 times.
✓ Branch 1 taken 1 times.
|
44104 | if(ns != st) |
65 |
2/2✓ Branch 0 taken 22065 times.
✓ Branch 1 taken 22038 times.
|
44103 | ns += (st != 0 ? 1 : -1); |
66 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 44103 times.
|
44104 | if(ns < 8) ns = 8; |
67 | 44104 | state->s = ns; | |
68 | |||
69 | // FILTER: perform antijerk | ||
70 |
2/2✓ Branch 0 taken 22039 times.
✓ Branch 1 taken 22065 times.
|
44104 | ov = (t != state->lt ? (nq+lq+1)>>1 : nq); |
71 | |||
72 | // FILTER: perform LPF | ||
73 | 44104 | state->fq += ((fs*(ov-state->fq) + 0x80)>>8); | |
74 | 44104 | ov = state->fq; | |
75 | |||
76 | // output sample | ||
77 | 44104 | *(outbuf++) = ov + 128; | |
78 | |||
79 | 44104 | state->lt = t; | |
80 | } | ||
81 | } | ||
82 | 11 | } | |
83 | |||
84 | 2 | static av_cold int dfpwm_dec_init(struct AVCodecContext *ctx) | |
85 | { | ||
86 | 2 | DFPWMState *state = ctx->priv_data; | |
87 | |||
88 | 2 | state->fq = 0; | |
89 | 2 | state->q = 0; | |
90 | 2 | state->s = 0; | |
91 | 2 | state->lt = -128; | |
92 | |||
93 | 2 | ctx->sample_fmt = AV_SAMPLE_FMT_U8; | |
94 | 2 | ctx->bits_per_raw_sample = 8; | |
95 | |||
96 | 2 | return 0; | |
97 | } | ||
98 | |||
99 | 11 | static int dfpwm_dec_frame(struct AVCodecContext *ctx, AVFrame *frame, | |
100 | int *got_frame, struct AVPacket *packet) | ||
101 | { | ||
102 | 11 | DFPWMState *state = ctx->priv_data; | |
103 | int ret; | ||
104 | |||
105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (packet->size * 8LL % ctx->ch_layout.nb_channels) |
106 | ✗ | return AVERROR_PATCHWELCOME; | |
107 | |||
108 | 11 | frame->nb_samples = packet->size * 8LL / ctx->ch_layout.nb_channels; | |
109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (frame->nb_samples <= 0) { |
110 | ✗ | av_log(ctx, AV_LOG_ERROR, "invalid number of samples in packet\n"); | |
111 | ✗ | return AVERROR_INVALIDDATA; | |
112 | } | ||
113 | |||
114 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if ((ret = ff_get_buffer(ctx, frame, 0)) < 0) |
115 | ✗ | return ret; | |
116 | |||
117 | 11 | au_decompress(state, 140, packet->size, frame->data[0], packet->data); | |
118 | |||
119 | 11 | *got_frame = 1; | |
120 | 11 | return packet->size; | |
121 | } | ||
122 | |||
123 | const FFCodec ff_dfpwm_decoder = { | ||
124 | .p.name = "dfpwm", | ||
125 | CODEC_LONG_NAME("DFPWM1a audio"), | ||
126 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
127 | .p.id = AV_CODEC_ID_DFPWM, | ||
128 | .priv_data_size = sizeof(DFPWMState), | ||
129 | .init = dfpwm_dec_init, | ||
130 | FF_CODEC_DECODE_CB(dfpwm_dec_frame), | ||
131 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
132 | }; | ||
133 |