blob: 42a46b25ce5852eebabfc86a9f6aeac393437da8 [file] [log] [blame]
Paul B Mahol7ef9d312023-06-27 17:54:251/*
2 * OSQ audio decoder
3 * Copyright (c) 2023 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavutil/internal.h"
23#include "libavutil/intreadwrite.h"
Andreas Rheinhardt790f7932024-03-25 00:30:3724#include "libavutil/mem.h"
Paul B Mahol7ef9d312023-06-27 17:54:2525#include "avcodec.h"
26#include "codec_internal.h"
27#include "decode.h"
28#include "internal.h"
29#define BITSTREAM_READER_LE
30#include "get_bits.h"
31#include "unary.h"
32
33#define OFFSET 5
34
35typedef struct OSQChannel {
36 unsigned prediction;
37 unsigned coding_mode;
38 unsigned residue_parameter;
39 unsigned residue_bits;
40 unsigned history[3];
41 unsigned pos, count;
42 double sum;
43 int32_t prev;
44} OSQChannel;
45
46typedef struct OSQContext {
47 GetBitContext gb;
48 OSQChannel ch[2];
49
50 uint8_t *bitstream;
51 size_t max_framesize;
52 size_t bitstream_size;
53
Paul B Maholc4ab17a2023-09-04 12:13:4554 int factor;
Paul B Mahol7ef9d312023-06-27 17:54:2555 int decorrelate;
56 int frame_samples;
Paul B Mahol87b8c102023-09-04 12:05:4457 uint64_t nb_samples;
Paul B Mahol7ef9d312023-06-27 17:54:2558
59 int32_t *decode_buffer[2];
60
61 AVPacket *pkt;
62 int pkt_offset;
63} OSQContext;
64
Michael Niedermayerc75fccd2023-09-20 21:53:2165static void osq_flush(AVCodecContext *avctx)
66{
67 OSQContext *s = avctx->priv_data;
68
69 s->bitstream_size = 0;
70 s->pkt_offset = 0;
71}
72
Paul B Mahol7ef9d312023-06-27 17:54:2573static av_cold int osq_close(AVCodecContext *avctx)
74{
75 OSQContext *s = avctx->priv_data;
76
77 av_freep(&s->bitstream);
78 s->bitstream_size = 0;
79
80 for (int ch = 0; ch < FF_ARRAY_ELEMS(s->decode_buffer); ch++)
81 av_freep(&s->decode_buffer[ch]);
82
83 return 0;
84}
85
86static av_cold int osq_init(AVCodecContext *avctx)
87{
88 OSQContext *s = avctx->priv_data;
89
90 if (avctx->extradata_size < 48)
91 return AVERROR(EINVAL);
92
93 if (avctx->extradata[0] != 1) {
94 av_log(avctx, AV_LOG_ERROR, "Unsupported version.\n");
95 return AVERROR_INVALIDDATA;
96 }
97
98 avctx->sample_rate = AV_RL32(avctx->extradata + 4);
99 if (avctx->sample_rate < 1)
100 return AVERROR_INVALIDDATA;
101
102 av_channel_layout_uninit(&avctx->ch_layout);
103 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
104 avctx->ch_layout.nb_channels = avctx->extradata[3];
105 if (avctx->ch_layout.nb_channels < 1)
106 return AVERROR_INVALIDDATA;
107 if (avctx->ch_layout.nb_channels > FF_ARRAY_ELEMS(s->decode_buffer))
108 return AVERROR_INVALIDDATA;
109
Paul B Maholc4ab17a2023-09-04 12:13:45110 s->factor = 1;
Paul B Mahol7ef9d312023-06-27 17:54:25111 switch (avctx->extradata[2]) {
112 case 8: avctx->sample_fmt = AV_SAMPLE_FMT_U8P; break;
113 case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16P; break;
114 case 20:
Paul B Mahol8cb2c6a2023-09-04 22:03:18115 case 24: s->factor = 256;
Paul B Maholc4ab17a2023-09-04 12:13:45116 avctx->sample_fmt = AV_SAMPLE_FMT_S32P; break;
Paul B Mahol7ef9d312023-06-27 17:54:25117 default: return AVERROR_INVALIDDATA;
118 }
119
Paul B Maholc4ab17a2023-09-04 12:13:45120 avctx->bits_per_raw_sample = avctx->extradata[2];
Paul B Mahol7ef9d312023-06-27 17:54:25121 s->nb_samples = AV_RL64(avctx->extradata + 16);
122 s->frame_samples = AV_RL16(avctx->extradata + 8);
123 s->max_framesize = (s->frame_samples * 16 + 1024) * avctx->ch_layout.nb_channels;
124
125 s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
126 if (!s->bitstream)
127 return AVERROR(ENOMEM);
128
129 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
130 s->decode_buffer[ch] = av_calloc(s->frame_samples + OFFSET,
131 sizeof(*s->decode_buffer[ch]));
132 if (!s->decode_buffer[ch])
133 return AVERROR(ENOMEM);
134 }
135
136 s->pkt = avctx->internal->in_pkt;
137
138 return 0;
139}
140
141static void reset_stats(OSQChannel *cb)
142{
143 memset(cb->history, 0, sizeof(cb->history));
144 cb->pos = cb->count = cb->sum = 0;
145}
146
147static void update_stats(OSQChannel *cb, int val)
148{
149 cb->sum += FFABS(val) - cb->history[cb->pos];
150 cb->history[cb->pos] = FFABS(val);
151 cb->pos++;
152 cb->count++;
153 if (cb->pos >= FF_ARRAY_ELEMS(cb->history))
154 cb->pos = 0;
155}
156
157static int update_residue_parameter(OSQChannel *cb)
158{
159 double sum, x;
160 int rice_k;
161
162 sum = cb->sum;
163 x = sum / cb->count;
Michael Niedermayer56c334d2023-09-14 22:49:41164 rice_k = ceil(log2(x));
Paul B Mahol7ef9d312023-06-27 17:54:25165 if (rice_k >= 30) {
Michael Niedermayer56c334d2023-09-14 22:49:41166 double f = floor(sum / 1.4426952 + 0.5);
167 if (f <= 1) {
Paul B Mahol7ef9d312023-06-27 17:54:25168 rice_k = 1;
Michael Niedermayer56c334d2023-09-14 22:49:41169 } else if (f >= 31) {
170 rice_k = 31;
171 } else
172 rice_k = f;
Paul B Mahol7ef9d312023-06-27 17:54:25173 }
174
175 return rice_k;
176}
177
178static uint32_t get_urice(GetBitContext *gb, int k)
179{
180 uint32_t z, x, b;
181
182 x = get_unary(gb, 1, 512);
183 b = get_bits_long(gb, k);
184 z = b | x << k;
185
186 return z;
187}
188
189static int32_t get_srice(GetBitContext *gb, int x)
190{
191 int32_t y = get_urice(gb, x);
192 return get_bits1(gb) ? -y : y;
193}
194
195static int osq_channel_parameters(AVCodecContext *avctx, int ch)
196{
197 OSQContext *s = avctx->priv_data;
198 OSQChannel *cb = &s->ch[ch];
199 GetBitContext *gb = &s->gb;
200
201 cb->prev = 0;
202 cb->prediction = get_urice(gb, 5);
203 cb->coding_mode = get_urice(gb, 3);
204 if (cb->prediction >= 15)
205 return AVERROR_INVALIDDATA;
206 if (cb->coding_mode > 0 && cb->coding_mode < 3) {
207 cb->residue_parameter = get_urice(gb, 4);
208 if (!cb->residue_parameter || cb->residue_parameter >= 31)
209 return AVERROR_INVALIDDATA;
210 } else if (cb->coding_mode == 3) {
211 cb->residue_bits = get_urice(gb, 4);
212 if (!cb->residue_bits || cb->residue_bits >= 31)
213 return AVERROR_INVALIDDATA;
214 } else if (cb->coding_mode) {
215 return AVERROR_INVALIDDATA;
216 }
217
218 if (cb->coding_mode == 2)
219 reset_stats(cb);
220
221 return 0;
222}
223
224#define A (-1)
225#define B (-2)
226#define C (-3)
227#define D (-4)
228#define E (-5)
Michael Niedermayerb54c9a92023-12-25 23:33:02229#define P2 (((unsigned)dst[A] + dst[A]) - dst[B])
230#define P3 (((unsigned)dst[A] - dst[B]) * 3 + dst[C])
Paul B Mahol7ef9d312023-06-27 17:54:25231
232static int do_decode(AVCodecContext *avctx, AVFrame *frame, int decorrelate, int downsample)
233{
234 OSQContext *s = avctx->priv_data;
235 const int nb_channels = avctx->ch_layout.nb_channels;
236 const int nb_samples = frame->nb_samples;
237 GetBitContext *gb = &s->gb;
238
239 for (int n = 0; n < nb_samples; n++) {
240 for (int ch = 0; ch < nb_channels; ch++) {
241 OSQChannel *cb = &s->ch[ch];
242 int32_t *dst = s->decode_buffer[ch] + OFFSET;
243 int32_t p, prev = cb->prev;
244
245 if (nb_channels == 2 && ch == 1 && decorrelate != s->decorrelate) {
246 if (!decorrelate) {
247 s->decode_buffer[1][OFFSET+A] += s->decode_buffer[0][OFFSET+B];
248 s->decode_buffer[1][OFFSET+B] += s->decode_buffer[0][OFFSET+C];
249 s->decode_buffer[1][OFFSET+C] += s->decode_buffer[0][OFFSET+D];
250 s->decode_buffer[1][OFFSET+D] += s->decode_buffer[0][OFFSET+E];
251 } else {
252 s->decode_buffer[1][OFFSET+A] -= s->decode_buffer[0][OFFSET+B];
253 s->decode_buffer[1][OFFSET+B] -= s->decode_buffer[0][OFFSET+C];
254 s->decode_buffer[1][OFFSET+C] -= s->decode_buffer[0][OFFSET+D];
255 s->decode_buffer[1][OFFSET+D] -= s->decode_buffer[0][OFFSET+E];
256 }
257 s->decorrelate = decorrelate;
258 }
259
260 if (!cb->coding_mode) {
261 dst[n] = 0;
262 } else if (cb->coding_mode == 3) {
263 dst[n] = get_sbits_long(gb, cb->residue_bits);
264 } else {
265 dst[n] = get_srice(gb, cb->residue_parameter);
266 }
267
268 if (get_bits_left(gb) < 0) {
269 av_log(avctx, AV_LOG_ERROR, "overread!\n");
270 return AVERROR_INVALIDDATA;
271 }
272
273 p = prev / 2;
274 prev = dst[n];
275
276 switch (cb->prediction) {
277 case 0:
278 break;
279 case 1:
Michael Niedermayerb54c9a92023-12-25 23:33:02280 dst[n] += (unsigned)dst[A];
Paul B Mahol7ef9d312023-06-27 17:54:25281 break;
282 case 2:
Michael Niedermayerb54c9a92023-12-25 23:33:02283 dst[n] += (unsigned)dst[A] + p;
Paul B Mahol7ef9d312023-06-27 17:54:25284 break;
285 case 3:
286 dst[n] += P2;
287 break;
288 case 4:
289 dst[n] += P2 + p;
290 break;
291 case 5:
292 dst[n] += P3;
293 break;
294 case 6:
295 dst[n] += P3 + p;
296 break;
297 case 7:
Michael Niedermayerb54c9a92023-12-25 23:33:02298 dst[n] += (int)(P2 + P3) / 2 + (unsigned)p;
Paul B Mahol7ef9d312023-06-27 17:54:25299 break;
300 case 8:
Michael Niedermayerb54c9a92023-12-25 23:33:02301 dst[n] += (int)(P2 + P3) / 2;
Paul B Mahol7ef9d312023-06-27 17:54:25302 break;
303 case 9:
Michael Niedermayerb54c9a92023-12-25 23:33:02304 dst[n] += (int)(P2 * 2 + P3) / 3 + (unsigned)p;
Paul B Mahol7ef9d312023-06-27 17:54:25305 break;
306 case 10:
Michael Niedermayerb54c9a92023-12-25 23:33:02307 dst[n] += (int)(P2 + P3 * 2) / 3 + (unsigned)p;
Paul B Mahol7ef9d312023-06-27 17:54:25308 break;
309 case 11:
Michael Niedermayerb54c9a92023-12-25 23:33:02310 dst[n] += (int)((unsigned)dst[A] + dst[B]) / 2;
Paul B Mahol7ef9d312023-06-27 17:54:25311 break;
312 case 12:
Michael Niedermayerb54c9a92023-12-25 23:33:02313 dst[n] += (unsigned)dst[B];
Paul B Mahol7ef9d312023-06-27 17:54:25314 break;
315 case 13:
Michael Niedermayerb54c9a92023-12-25 23:33:02316 dst[n] += (int)(unsigned)(dst[D] + dst[B]) / 2;
Paul B Mahol7ef9d312023-06-27 17:54:25317 break;
318 case 14:
Michael Niedermayerb54c9a92023-12-25 23:33:02319 dst[n] += (int)((unsigned)P2 + dst[A]) / 2 + (unsigned)p;
Paul B Mahol7ef9d312023-06-27 17:54:25320 break;
321 default:
322 return AVERROR_INVALIDDATA;
323 }
324
325 cb->prev = prev;
326
327 if (downsample)
Michael Niedermayered34b0c2024-06-21 19:35:48328 dst[n] *= 256U;
Paul B Mahol7ef9d312023-06-27 17:54:25329
330 dst[E] = dst[D];
331 dst[D] = dst[C];
332 dst[C] = dst[B];
333 dst[B] = dst[A];
334 dst[A] = dst[n];
335
336 if (cb->coding_mode == 2) {
337 update_stats(cb, dst[n]);
338 cb->residue_parameter = update_residue_parameter(cb);
339 }
340
341 if (nb_channels == 2 && ch == 1) {
342 if (decorrelate)
343 dst[n] += s->decode_buffer[0][OFFSET+n];
344 }
345
346 if (downsample)
347 dst[A] /= 256;
348 }
349 }
350
351 return 0;
352}
353
354static int osq_decode_block(AVCodecContext *avctx, AVFrame *frame)
355{
356 const int nb_channels = avctx->ch_layout.nb_channels;
Paul B Maholc4ab17a2023-09-04 12:13:45357 const int nb_samples = frame->nb_samples;
Paul B Mahol7ef9d312023-06-27 17:54:25358 OSQContext *s = avctx->priv_data;
Paul B Maholc4ab17a2023-09-04 12:13:45359 const int factor = s->factor;
Paul B Mahol7ef9d312023-06-27 17:54:25360 int ret, decorrelate, downsample;
361 GetBitContext *gb = &s->gb;
362
363 skip_bits1(gb);
364 decorrelate = get_bits1(gb);
365 downsample = get_bits1(gb);
366
367 for (int ch = 0; ch < nb_channels; ch++) {
368 if ((ret = osq_channel_parameters(avctx, ch)) < 0) {
369 av_log(avctx, AV_LOG_ERROR, "invalid channel parameters\n");
370 return ret;
371 }
372 }
373
374 if ((ret = do_decode(avctx, frame, decorrelate, downsample)) < 0)
375 return ret;
376
377 align_get_bits(gb);
378
379 switch (avctx->sample_fmt) {
380 case AV_SAMPLE_FMT_U8P:
381 for (int ch = 0; ch < nb_channels; ch++) {
382 uint8_t *dst = (uint8_t *)frame->extended_data[ch];
383 int32_t *src = s->decode_buffer[ch] + OFFSET;
384
Paul B Maholc4ab17a2023-09-04 12:13:45385 for (int n = 0; n < nb_samples; n++)
Paul B Mahol7ef9d312023-06-27 17:54:25386 dst[n] = av_clip_uint8(src[n] + 0x80);
387 }
388 break;
389 case AV_SAMPLE_FMT_S16P:
390 for (int ch = 0; ch < nb_channels; ch++) {
391 int16_t *dst = (int16_t *)frame->extended_data[ch];
392 int32_t *src = s->decode_buffer[ch] + OFFSET;
393
Paul B Maholc4ab17a2023-09-04 12:13:45394 for (int n = 0; n < nb_samples; n++)
Paul B Mahol7ef9d312023-06-27 17:54:25395 dst[n] = (int16_t)src[n];
396 }
397 break;
398 case AV_SAMPLE_FMT_S32P:
399 for (int ch = 0; ch < nb_channels; ch++) {
400 int32_t *dst = (int32_t *)frame->extended_data[ch];
401 int32_t *src = s->decode_buffer[ch] + OFFSET;
402
Paul B Maholc4ab17a2023-09-04 12:13:45403 for (int n = 0; n < nb_samples; n++)
404 dst[n] = src[n] * factor;
Paul B Mahol7ef9d312023-06-27 17:54:25405 }
406 break;
407 default:
408 return AVERROR_BUG;
409 }
410
411 return 0;
412}
413
414static int osq_receive_frame(AVCodecContext *avctx, AVFrame *frame)
415{
416 OSQContext *s = avctx->priv_data;
417 GetBitContext *gb = &s->gb;
418 int ret, n;
419
420 while (s->bitstream_size < s->max_framesize) {
421 int size;
422
423 if (!s->pkt->data) {
424 ret = ff_decode_get_packet(avctx, s->pkt);
425 if (ret == AVERROR_EOF && s->bitstream_size > 0)
426 break;
Paul B Maholea063172023-09-21 18:25:37427 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
Paul B Mahol7ef9d312023-06-27 17:54:25428 return ret;
Paul B Maholea063172023-09-21 18:25:37429 if (ret < 0)
430 goto fail;
Paul B Mahol7ef9d312023-06-27 17:54:25431 }
432
433 size = FFMIN(s->pkt->size - s->pkt_offset, s->max_framesize - s->bitstream_size);
434 memcpy(s->bitstream + s->bitstream_size, s->pkt->data + s->pkt_offset, size);
435 s->bitstream_size += size;
436 s->pkt_offset += size;
437
438 if (s->pkt_offset == s->pkt->size) {
439 av_packet_unref(s->pkt);
440 s->pkt_offset = 0;
441 }
442 }
443
444 frame->nb_samples = FFMIN(s->frame_samples, s->nb_samples);
445 if (frame->nb_samples <= 0)
446 return AVERROR_EOF;
447
448 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
449 goto fail;
450
451 if ((ret = init_get_bits8(gb, s->bitstream, s->bitstream_size)) < 0)
452 goto fail;
453
454 if ((ret = osq_decode_block(avctx, frame)) < 0)
455 goto fail;
456
457 s->nb_samples -= frame->nb_samples;
458
459 n = get_bits_count(gb) / 8;
460 if (n > s->bitstream_size) {
461 ret = AVERROR_INVALIDDATA;
462 goto fail;
463 }
464
465 memmove(s->bitstream, &s->bitstream[n], s->bitstream_size - n);
466 s->bitstream_size -= n;
467
468 return 0;
469
470fail:
471 s->bitstream_size = 0;
472 s->pkt_offset = 0;
473 av_packet_unref(s->pkt);
474
475 return ret;
476}
477
478const FFCodec ff_osq_decoder = {
479 .p.name = "osq",
480 CODEC_LONG_NAME("OSQ (Original Sound Quality)"),
481 .p.type = AVMEDIA_TYPE_AUDIO,
482 .p.id = AV_CODEC_ID_OSQ,
483 .priv_data_size = sizeof(OSQContext),
484 .init = osq_init,
485 FF_CODEC_RECEIVE_FRAME_CB(osq_receive_frame),
486 .close = osq_close,
487 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
488 AV_CODEC_CAP_DR1,
489 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
490 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
491 AV_SAMPLE_FMT_S16P,
492 AV_SAMPLE_FMT_S32P,
493 AV_SAMPLE_FMT_NONE },
Michael Niedermayerc75fccd2023-09-20 21:53:21494 .flush = osq_flush,
Paul B Mahol7ef9d312023-06-27 17:54:25495};