/src/ffmpeg/libavcodec/qoadec.c
Line | Count | Source |
1 | | /* |
2 | | * QOA decoder |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include "avcodec.h" |
22 | | #include "codec_internal.h" |
23 | | #include "decode.h" |
24 | | #include "get_bits.h" |
25 | | #include "bytestream.h" |
26 | | #include "mathops.h" |
27 | | |
28 | 1.86M | #define QOA_SLICE_LEN 20 |
29 | 321M | #define QOA_LMS_LEN 4 |
30 | | |
31 | | typedef struct QOAChannel { |
32 | | int history[QOA_LMS_LEN]; |
33 | | int weights[QOA_LMS_LEN]; |
34 | | } QOAChannel; |
35 | | |
36 | | typedef struct QOAContext { |
37 | | QOAChannel ch[256]; |
38 | | } QOAContext; |
39 | | |
40 | | static const int16_t qoa_dequant_tab[16][8] = { |
41 | | { 1, -1, 3, -3, 5, -5, 7, -7}, |
42 | | { 5, -5, 18, -18, 32, -32, 49, -49}, |
43 | | { 16, -16, 53, -53, 95, -95, 147, -147}, |
44 | | { 34, -34, 113, -113, 203, -203, 315, -315}, |
45 | | { 63, -63, 210, -210, 378, -378, 588, -588}, |
46 | | { 104, -104, 345, -345, 621, -621, 966, -966}, |
47 | | { 158, -158, 528, -528, 950, -950, 1477, -1477}, |
48 | | { 228, -228, 760, -760, 1368, -1368, 2128, -2128}, |
49 | | { 316, -316, 1053, -1053, 1895, -1895, 2947, -2947}, |
50 | | { 422, -422, 1405, -1405, 2529, -2529, 3934, -3934}, |
51 | | { 548, -548, 1828, -1828, 3290, -3290, 5117, -5117}, |
52 | | { 696, -696, 2320, -2320, 4176, -4176, 6496, -6496}, |
53 | | { 868, -868, 2893, -2893, 5207, -5207, 8099, -8099}, |
54 | | {1064, -1064, 3548, -3548, 6386, -6386, 9933, -9933}, |
55 | | {1286, -1286, 4288, -4288, 7718, -7718, 12005, -12005}, |
56 | | {1536, -1536, 5120, -5120, 9216, -9216, 14336, -14336}, |
57 | | }; |
58 | | |
59 | | static av_cold int qoa_decode_init(AVCodecContext *avctx) |
60 | 1.15k | { |
61 | 1.15k | avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
62 | | |
63 | 1.15k | return 0; |
64 | 1.15k | } |
65 | | |
66 | | static int qoa_lms_predict(QOAChannel *lms) |
67 | 21.3M | { |
68 | 21.3M | int prediction = 0; |
69 | 106M | for (int i = 0; i < QOA_LMS_LEN; i++) |
70 | 85.4M | prediction += (unsigned)lms->weights[i] * lms->history[i]; |
71 | 21.3M | return prediction >> 13; |
72 | 21.3M | } |
73 | | |
74 | | static void qoa_lms_update(QOAChannel *lms, int sample, int residual) |
75 | 21.3M | { |
76 | 21.3M | int delta = residual >> 4; |
77 | 106M | for (int i = 0; i < QOA_LMS_LEN; i++) |
78 | 85.4M | lms->weights[i] += lms->history[i] < 0 ? -delta : delta; |
79 | 85.4M | for (int i = 0; i < QOA_LMS_LEN-1; i++) |
80 | 64.0M | lms->history[i] = lms->history[i+1]; |
81 | 21.3M | lms->history[QOA_LMS_LEN-1] = sample; |
82 | 21.3M | } |
83 | | |
84 | | static int qoa_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
85 | | int *got_frame_ptr, AVPacket *avpkt) |
86 | 297k | { |
87 | 297k | QOAContext *s = avctx->priv_data; |
88 | 297k | int ret, frame_size, nb_channels, sample_rate; |
89 | 297k | GetByteContext gb; |
90 | 297k | int16_t *samples; |
91 | | |
92 | 297k | bytestream2_init(&gb, avpkt->data, avpkt->size); |
93 | | |
94 | 297k | nb_channels = bytestream2_get_byte(&gb); |
95 | 297k | sample_rate = bytestream2_get_be24(&gb); |
96 | 297k | if (!sample_rate || !nb_channels) |
97 | 148k | return AVERROR_INVALIDDATA; |
98 | | |
99 | 149k | if (nb_channels != avctx->ch_layout.nb_channels) { |
100 | 7.50k | av_channel_layout_uninit(&avctx->ch_layout); |
101 | 7.50k | av_channel_layout_default(&avctx->ch_layout, nb_channels); |
102 | 7.50k | if ((ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout)) < 0) |
103 | 0 | return ret; |
104 | 7.50k | } |
105 | | |
106 | 149k | frame->sample_rate = avctx->sample_rate = sample_rate; |
107 | | |
108 | 149k | frame->nb_samples = bytestream2_get_be16(&gb); |
109 | 149k | frame_size = bytestream2_get_be16(&gb); |
110 | 149k | if (frame_size > avpkt->size) |
111 | 5.99k | return AVERROR_INVALIDDATA; |
112 | | |
113 | 143k | if (avpkt->size < 8 + QOA_LMS_LEN * 4 * nb_channels + |
114 | 143k | 8LL * ((frame->nb_samples + QOA_SLICE_LEN - 1) / QOA_SLICE_LEN) * nb_channels) |
115 | 4.94k | return AVERROR_INVALIDDATA; |
116 | | |
117 | 138k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
118 | 2.60k | return ret; |
119 | 136k | samples = (int16_t *)frame->data[0]; |
120 | | |
121 | 278k | for (int ch = 0; ch < nb_channels; ch++) { |
122 | 142k | QOAChannel *qch = &s->ch[ch]; |
123 | | |
124 | 712k | for (int n = 0; n < QOA_LMS_LEN; n++) |
125 | 570k | qch->history[n] = sign_extend(bytestream2_get_be16u(&gb), 16); |
126 | 712k | for (int n = 0; n < QOA_LMS_LEN; n++) |
127 | 570k | qch->weights[n] = sign_extend(bytestream2_get_be16u(&gb), 16); |
128 | 142k | } |
129 | | |
130 | 518k | for (int sample_index = 0; sample_index < frame->nb_samples; |
131 | 382k | sample_index += QOA_SLICE_LEN) { |
132 | 1.57M | for (int ch = 0; ch < nb_channels; ch++) { |
133 | 1.19M | QOAChannel *lms = &s->ch[ch]; |
134 | 1.19M | uint64_t slice = bytestream2_get_be64u(&gb); |
135 | 1.19M | int scalefactor = (slice >> 60) & 0xf; |
136 | 1.19M | int slice_start = sample_index * nb_channels + ch; |
137 | 1.19M | int slice_end = av_clip(sample_index + QOA_SLICE_LEN, 0, frame->nb_samples) * nb_channels + ch; |
138 | | |
139 | 22.5M | for (int si = slice_start; si < slice_end; si += nb_channels) { |
140 | 21.3M | int predicted = qoa_lms_predict(lms); |
141 | 21.3M | int quantized = (slice >> 57) & 0x7; |
142 | 21.3M | int dequantized = qoa_dequant_tab[scalefactor][quantized]; |
143 | 21.3M | int reconstructed = av_clip_int16(predicted + dequantized); |
144 | | |
145 | 21.3M | samples[si] = reconstructed; |
146 | 21.3M | slice <<= 3; |
147 | | |
148 | 21.3M | qoa_lms_update(lms, reconstructed, dequantized); |
149 | 21.3M | } |
150 | 1.19M | } |
151 | 382k | } |
152 | | |
153 | 136k | *got_frame_ptr = 1; |
154 | | |
155 | 136k | return avpkt->size; |
156 | 138k | } |
157 | | |
158 | | const FFCodec ff_qoa_decoder = { |
159 | | .p.name = "qoa", |
160 | | CODEC_LONG_NAME("QOA (Quite OK Audio)"), |
161 | | .p.type = AVMEDIA_TYPE_AUDIO, |
162 | | .p.id = AV_CODEC_ID_QOA, |
163 | | .priv_data_size = sizeof(QOAContext), |
164 | | .init = qoa_decode_init, |
165 | | FF_CODEC_DECODE_CB(qoa_decode_frame), |
166 | | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | |
167 | | AV_CODEC_CAP_DR1, |
168 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16), |
169 | | }; |