Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/osq.c
Line
Count
Source
1
/*
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/attributes.h"
23
#include "libavutil/internal.h"
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/mem.h"
26
#include "avcodec.h"
27
#include "codec_internal.h"
28
#include "decode.h"
29
#include "internal.h"
30
#define BITSTREAM_READER_LE
31
#include "get_bits.h"
32
#include "unary.h"
33
34
7.70M
#define OFFSET 5
35
36
typedef struct OSQChannel {
37
    unsigned prediction;
38
    unsigned coding_mode;
39
    unsigned residue_parameter;
40
    unsigned residue_bits;
41
    unsigned history[3];
42
    unsigned pos, count;
43
    double sum;
44
    int32_t prev;
45
} OSQChannel;
46
47
typedef struct OSQContext {
48
    GetBitContext gb;
49
    OSQChannel ch[2];
50
51
    uint8_t *bitstream;
52
    size_t max_framesize;
53
    size_t bitstream_size;
54
55
    int factor;
56
    int decorrelate;
57
    int frame_samples;
58
    uint64_t nb_samples;
59
60
    int32_t *decode_buffer[2];
61
62
    AVPacket *pkt;
63
    int pkt_offset;
64
} OSQContext;
65
66
static av_cold void osq_flush(AVCodecContext *avctx)
67
36.4k
{
68
36.4k
    OSQContext *s = avctx->priv_data;
69
70
36.4k
    s->bitstream_size = 0;
71
36.4k
    s->pkt_offset = 0;
72
36.4k
}
73
74
static av_cold int osq_close(AVCodecContext *avctx)
75
1.12k
{
76
1.12k
    OSQContext *s = avctx->priv_data;
77
78
1.12k
    av_freep(&s->bitstream);
79
1.12k
    s->bitstream_size = 0;
80
81
3.36k
    for (int ch = 0; ch < FF_ARRAY_ELEMS(s->decode_buffer); ch++)
82
2.24k
        av_freep(&s->decode_buffer[ch]);
83
84
1.12k
    return 0;
85
1.12k
}
86
87
static av_cold int osq_init(AVCodecContext *avctx)
88
1.12k
{
89
1.12k
    OSQContext *s = avctx->priv_data;
90
91
1.12k
    if (avctx->extradata_size < 48)
92
182
        return AVERROR(EINVAL);
93
94
939
    if (avctx->extradata[0] != 1) {
95
24
        av_log(avctx, AV_LOG_ERROR, "Unsupported version.\n");
96
24
        return AVERROR_INVALIDDATA;
97
24
    }
98
99
915
    avctx->sample_rate = AV_RL32(avctx->extradata + 4);
100
915
    if (avctx->sample_rate < 1)
101
32
        return AVERROR_INVALIDDATA;
102
103
883
    av_channel_layout_uninit(&avctx->ch_layout);
104
883
    avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
105
883
    avctx->ch_layout.nb_channels = avctx->extradata[3];
106
883
    if (avctx->ch_layout.nb_channels < 1)
107
2
        return AVERROR_INVALIDDATA;
108
881
    if (avctx->ch_layout.nb_channels > FF_ARRAY_ELEMS(s->decode_buffer))
109
7
        return AVERROR_INVALIDDATA;
110
111
874
    s->factor = 1;
112
874
    switch (avctx->extradata[2]) {
113
526
    case  8: avctx->sample_fmt = AV_SAMPLE_FMT_U8P; break;
114
124
    case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16P; break;
115
45
    case 20:
116
223
    case 24: s->factor = 256;
117
223
             avctx->sample_fmt = AV_SAMPLE_FMT_S32P; break;
118
1
    default: return AVERROR_INVALIDDATA;
119
874
    }
120
121
873
    avctx->bits_per_raw_sample = avctx->extradata[2];
122
873
    s->nb_samples = AV_RL64(avctx->extradata + 16);
123
873
    s->frame_samples = AV_RL16(avctx->extradata + 8);
124
873
    s->max_framesize = (s->frame_samples * 16 + 1024) * avctx->ch_layout.nb_channels;
125
126
873
    s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
127
873
    if (!s->bitstream)
128
0
        return AVERROR(ENOMEM);
129
130
1.96k
    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
131
1.09k
        s->decode_buffer[ch] = av_calloc(s->frame_samples + OFFSET,
132
1.09k
                                         sizeof(*s->decode_buffer[ch]));
133
1.09k
        if (!s->decode_buffer[ch])
134
0
            return AVERROR(ENOMEM);
135
1.09k
    }
136
137
873
    s->pkt = avctx->internal->in_pkt;
138
139
873
    return 0;
140
873
}
141
142
static void reset_stats(OSQChannel *cb)
143
27.0k
{
144
27.0k
    memset(cb->history, 0, sizeof(cb->history));
145
27.0k
    cb->pos = cb->count = cb->sum = 0;
146
27.0k
}
147
148
static void update_stats(OSQChannel *cb, int val)
149
336k
{
150
336k
    cb->sum += FFABS((int64_t)val) - cb->history[cb->pos];
151
336k
    cb->history[cb->pos] = FFABS((int64_t)val);
152
336k
    cb->pos++;
153
336k
    cb->count++;
154
    //NOTE for this to make sense count would need to be limited to FF_ARRAY_ELEMS(cb->history)
155
    //Otherwise the average computation later makes no sense
156
336k
    if (cb->pos >= FF_ARRAY_ELEMS(cb->history))
157
103k
        cb->pos = 0;
158
336k
}
159
160
static int update_residue_parameter(OSQChannel *cb)
161
336k
{
162
336k
    double sum, x;
163
336k
    int rice_k;
164
165
336k
    sum = cb->sum;
166
336k
    if (!sum)
167
733
        return 0;
168
335k
    x = sum / cb->count;
169
335k
    av_assert2(x <= 0x80000000U);
170
335k
    rice_k = av_ceil_log2(x);
171
335k
    if (rice_k >= 30) {
172
2.69k
        double f = floor(sum / 1.4426952 + 0.5);
173
2.69k
        if (f <= 1) {
174
258
            rice_k = 1;
175
2.43k
        } else if (f >= 31) {
176
1.76k
            rice_k = 31;
177
1.76k
        } else
178
673
            rice_k = f;
179
2.69k
    }
180
181
335k
    return rice_k;
182
336k
}
183
184
static uint32_t get_urice(GetBitContext *gb, int k)
185
1.86M
{
186
1.86M
    uint32_t z, x, b;
187
188
1.86M
    x = get_unary(gb, 1, 512);
189
1.86M
    b = get_bits_long(gb, k);
190
1.86M
    z = b | x << k;
191
192
1.86M
    return z;
193
1.86M
}
194
195
static int32_t get_srice(GetBitContext *gb, int x)
196
1.72M
{
197
1.72M
    uint32_t y = get_urice(gb, x);
198
1.72M
    return get_bits1(gb) ? -y : y;
199
1.72M
}
200
201
static int osq_channel_parameters(AVCodecContext *avctx, int ch)
202
51.8k
{
203
51.8k
    OSQContext *s = avctx->priv_data;
204
51.8k
    OSQChannel *cb = &s->ch[ch];
205
51.8k
    GetBitContext *gb = &s->gb;
206
207
51.8k
    cb->prev = 0;
208
51.8k
    cb->prediction = get_urice(gb, 5);
209
51.8k
    cb->coding_mode = get_urice(gb, 3);
210
51.8k
    if (cb->prediction >= 15)
211
921
        return AVERROR_INVALIDDATA;
212
50.9k
    if (cb->coding_mode > 0 && cb->coding_mode < 3) {
213
28.4k
        cb->residue_parameter = get_urice(gb, 4);
214
28.4k
        if (!cb->residue_parameter || cb->residue_parameter >= 31)
215
672
            return AVERROR_INVALIDDATA;
216
27.7k
        if (cb->coding_mode == 2)
217
27.0k
            avpriv_request_sample(avctx, "coding mode 2");
218
27.7k
    } else if (cb->coding_mode == 3) {
219
8.29k
        cb->residue_bits = get_urice(gb, 4);
220
8.29k
        if (!cb->residue_bits || cb->residue_bits >= 31)
221
668
            return AVERROR_INVALIDDATA;
222
14.1k
    } else if (cb->coding_mode) {
223
331
        return AVERROR_INVALIDDATA;
224
331
    }
225
226
49.2k
    if (cb->coding_mode == 2)
227
27.0k
        reset_stats(cb);
228
229
49.2k
    return 0;
230
50.9k
}
231
232
28.5M
#define A (-1)
233
20.8M
#define B (-2)
234
16.7M
#define C (-3)
235
13.5M
#define D (-4)
236
6.79M
#define E (-5)
237
3.32M
#define P2 (((unsigned)dst[A] + dst[A]) - dst[B])
238
3.13M
#define P3 (((unsigned)dst[A] - dst[B]) * 3 + dst[C])
239
240
static int do_decode(AVCodecContext *avctx, AVFrame *frame, int decorrelate, int downsample)
241
47.4k
{
242
47.4k
    OSQContext *s = avctx->priv_data;
243
47.4k
    const int nb_channels = avctx->ch_layout.nb_channels;
244
47.4k
    const int nb_samples = frame->nb_samples;
245
47.4k
    GetBitContext *gb = &s->gb;
246
247
5.33M
    for (int n = 0; n < nb_samples; n++) {
248
12.0M
        for (int ch = 0; ch < nb_channels; ch++) {
249
6.79M
            OSQChannel *cb = &s->ch[ch];
250
6.79M
            int32_t *dst = s->decode_buffer[ch] + OFFSET;
251
6.79M
            int32_t p, prev = cb->prev;
252
253
6.79M
            if (nb_channels == 2 && ch == 1 && decorrelate != s->decorrelate) {
254
558
                if (!decorrelate) {
255
231
                    s->decode_buffer[1][OFFSET+A] += s->decode_buffer[0][OFFSET+B];
256
231
                    s->decode_buffer[1][OFFSET+B] += s->decode_buffer[0][OFFSET+C];
257
231
                    s->decode_buffer[1][OFFSET+C] += s->decode_buffer[0][OFFSET+D];
258
231
                    s->decode_buffer[1][OFFSET+D] += s->decode_buffer[0][OFFSET+E];
259
327
                } else {
260
327
                    s->decode_buffer[1][OFFSET+A] -= s->decode_buffer[0][OFFSET+B];
261
327
                    s->decode_buffer[1][OFFSET+B] -= s->decode_buffer[0][OFFSET+C];
262
327
                    s->decode_buffer[1][OFFSET+C] -= s->decode_buffer[0][OFFSET+D];
263
327
                    s->decode_buffer[1][OFFSET+D] -= s->decode_buffer[0][OFFSET+E];
264
327
                }
265
558
                s->decorrelate = decorrelate;
266
558
            }
267
268
6.79M
            if (!cb->coding_mode) {
269
4.31M
                dst[n] = 0;
270
4.31M
            } else if (cb->coding_mode == 3) {
271
753k
                dst[n] = get_sbits_long(gb, cb->residue_bits);
272
1.72M
            } else {
273
1.72M
                dst[n] = get_srice(gb, cb->residue_parameter);
274
1.72M
            }
275
276
6.79M
            if (get_bits_left(gb) < 0) {
277
361
                av_log(avctx, AV_LOG_ERROR, "overread!\n");
278
361
                return AVERROR_INVALIDDATA;
279
361
            }
280
281
6.79M
            p = prev / 2;
282
6.79M
            prev = dst[n];
283
284
6.79M
            switch (cb->prediction) {
285
596k
            case 0:
286
596k
                break;
287
662k
            case 1:
288
662k
                dst[n] += (unsigned)dst[A];
289
662k
                break;
290
367k
            case 2:
291
367k
                dst[n] += (unsigned)dst[A] + p;
292
367k
                break;
293
184k
            case 3:
294
184k
                dst[n] += P2;
295
184k
                break;
296
139k
            case 4:
297
139k
                dst[n] += P2 + p;
298
139k
                break;
299
659k
            case 5:
300
659k
                dst[n] += P3;
301
659k
                break;
302
344k
            case 6:
303
344k
                dst[n] += P3 + p;
304
344k
                break;
305
485k
            case 7:
306
485k
                dst[n] += (int)(P2 + P3) / 2 + (unsigned)p;
307
485k
                break;
308
931k
            case 8:
309
931k
                dst[n] += (int)(P2 + P3) / 2 + 0U;
310
931k
                break;
311
469k
            case 9:
312
469k
                dst[n] += (int)(P2 * 2 + P3) / 3 + (unsigned)p;
313
469k
                break;
314
246k
            case 10:
315
246k
                dst[n] += (int)(P2 + P3 * 2) / 3 + (unsigned)p;
316
246k
                break;
317
311k
            case 11:
318
311k
                dst[n] += (int)((unsigned)dst[A] + dst[B]) / 2 + 0U;
319
311k
                break;
320
521k
            case 12:
321
521k
                dst[n] += (unsigned)dst[B];
322
521k
                break;
323
4.06k
            case 13:
324
4.06k
                dst[n] += (int)((unsigned)dst[D] + dst[B]) / 2 + 0U;
325
4.06k
                break;
326
868k
            case 14:
327
868k
                dst[n] += (int)((unsigned)P2 + dst[A]) / 2 + (unsigned)p;
328
868k
                break;
329
0
            default:
330
0
                return AVERROR_INVALIDDATA;
331
6.79M
            }
332
333
6.79M
            cb->prev = prev;
334
335
6.79M
            if (downsample)
336
2.99M
                dst[n] *= 256U;
337
338
6.79M
            dst[E] = dst[D];
339
6.79M
            dst[D] = dst[C];
340
6.79M
            dst[C] = dst[B];
341
6.79M
            dst[B] = dst[A];
342
6.79M
            dst[A] = dst[n];
343
344
6.79M
            if (cb->coding_mode == 2) {
345
336k
                update_stats(cb, dst[n]);
346
336k
                cb->residue_parameter = update_residue_parameter(cb);
347
336k
            }
348
349
6.79M
            if (nb_channels == 2 && ch == 1) {
350
1.50M
                if (decorrelate)
351
853k
                    dst[n] += (unsigned)s->decode_buffer[0][OFFSET+n];
352
1.50M
            }
353
354
6.79M
            if (downsample)
355
2.99M
                dst[A] /= 256;
356
6.79M
        }
357
5.28M
    }
358
359
47.1k
    return 0;
360
47.4k
}
361
362
static int osq_decode_block(AVCodecContext *avctx, AVFrame *frame)
363
50.0k
{
364
50.0k
    const int nb_channels = avctx->ch_layout.nb_channels;
365
50.0k
    const int nb_samples = frame->nb_samples;
366
50.0k
    OSQContext *s = avctx->priv_data;
367
50.0k
    const unsigned factor = s->factor;
368
50.0k
    int ret, decorrelate, downsample;
369
50.0k
    GetBitContext *gb = &s->gb;
370
371
50.0k
    skip_bits1(gb);
372
50.0k
    decorrelate = get_bits1(gb);
373
50.0k
    downsample = get_bits1(gb);
374
375
99.2k
    for (int ch = 0; ch < nb_channels; ch++) {
376
51.8k
        if ((ret = osq_channel_parameters(avctx, ch)) < 0) {
377
2.59k
            av_log(avctx, AV_LOG_ERROR, "invalid channel parameters\n");
378
2.59k
            return ret;
379
2.59k
        }
380
51.8k
    }
381
382
47.4k
    if ((ret = do_decode(avctx, frame, decorrelate, downsample)) < 0)
383
361
        return ret;
384
385
47.1k
    align_get_bits(gb);
386
387
47.1k
    switch (avctx->sample_fmt) {
388
44.6k
    case AV_SAMPLE_FMT_U8P:
389
90.1k
        for (int ch = 0; ch < nb_channels; ch++) {
390
45.4k
            uint8_t *dst = (uint8_t *)frame->extended_data[ch];
391
45.4k
            int32_t *src = s->decode_buffer[ch] + OFFSET;
392
393
1.44M
            for (int n = 0; n < nb_samples; n++)
394
1.40M
                dst[n] = av_clip_uint8(src[n] + 0x80ll);
395
45.4k
        }
396
44.6k
        break;
397
1.12k
    case AV_SAMPLE_FMT_S16P:
398
2.57k
        for (int ch = 0; ch < nb_channels; ch++) {
399
1.44k
            int16_t *dst = (int16_t *)frame->extended_data[ch];
400
1.44k
            int32_t *src = s->decode_buffer[ch] + OFFSET;
401
402
1.65M
            for (int n = 0; n < nb_samples; n++)
403
1.65M
                dst[n] = (int16_t)src[n];
404
1.44k
        }
405
1.12k
        break;
406
1.33k
    case AV_SAMPLE_FMT_S32P:
407
3.12k
        for (int ch = 0; ch < nb_channels; ch++) {
408
1.79k
            int32_t *dst = (int32_t *)frame->extended_data[ch];
409
1.79k
            int32_t *src = s->decode_buffer[ch] + OFFSET;
410
411
2.37M
            for (int n = 0; n < nb_samples; n++)
412
2.37M
                dst[n] = src[n] * factor;
413
1.79k
        }
414
1.33k
        break;
415
0
    default:
416
0
        return AVERROR_BUG;
417
47.1k
    }
418
419
47.1k
    return 0;
420
47.1k
}
421
422
static int osq_receive_frame(AVCodecContext *avctx, AVFrame *frame)
423
595k
{
424
595k
    OSQContext *s = avctx->priv_data;
425
595k
    GetBitContext *gb = &s->gb;
426
595k
    int ret, n;
427
428
911k
    while (s->bitstream_size < s->max_framesize) {
429
867k
        int size;
430
431
867k
        if (!s->pkt->data) {
432
826k
            ret = ff_decode_get_packet(avctx, s->pkt);
433
826k
            if (ret == AVERROR_EOF && s->bitstream_size > 0)
434
6.87k
                break;
435
819k
            if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
436
544k
                return ret;
437
275k
            if (ret < 0)
438
0
                goto fail;
439
275k
        }
440
441
315k
        size = FFMIN(s->pkt->size - s->pkt_offset, s->max_framesize - s->bitstream_size);
442
315k
        memcpy(s->bitstream + s->bitstream_size, s->pkt->data + s->pkt_offset, size);
443
315k
        s->bitstream_size += size;
444
315k
        s->pkt_offset += size;
445
446
315k
        if (s->pkt_offset == s->pkt->size) {
447
272k
            av_packet_unref(s->pkt);
448
272k
            s->pkt_offset = 0;
449
272k
        }
450
315k
    }
451
452
50.9k
    frame->nb_samples = FFMIN(s->frame_samples, s->nb_samples);
453
50.9k
    if (frame->nb_samples <= 0)
454
880
        return AVERROR_EOF;
455
456
50.0k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
457
0
        goto fail;
458
459
50.0k
    if ((ret = init_get_bits8(gb, s->bitstream, s->bitstream_size)) < 0)
460
0
        goto fail;
461
462
50.0k
    if ((ret = osq_decode_block(avctx, frame)) < 0)
463
2.95k
        goto fail;
464
465
47.1k
    s->nb_samples -= frame->nb_samples;
466
467
47.1k
    n = get_bits_count(gb) / 8;
468
47.1k
    if (n > s->bitstream_size) {
469
0
        ret = AVERROR_INVALIDDATA;
470
0
        goto fail;
471
0
    }
472
473
47.1k
    memmove(s->bitstream, &s->bitstream[n], s->bitstream_size - n);
474
47.1k
    s->bitstream_size -= n;
475
476
47.1k
    return 0;
477
478
2.95k
fail:
479
2.95k
    s->bitstream_size = 0;
480
2.95k
    s->pkt_offset = 0;
481
2.95k
    av_packet_unref(s->pkt);
482
483
2.95k
    return ret;
484
47.1k
}
485
486
const FFCodec ff_osq_decoder = {
487
    .p.name           = "osq",
488
    CODEC_LONG_NAME("OSQ (Original Sound Quality)"),
489
    .p.type           = AVMEDIA_TYPE_AUDIO,
490
    .p.id             = AV_CODEC_ID_OSQ,
491
    .priv_data_size   = sizeof(OSQContext),
492
    .init             = osq_init,
493
    FF_CODEC_RECEIVE_FRAME_CB(osq_receive_frame),
494
    .close            = osq_close,
495
    .p.capabilities   = AV_CODEC_CAP_CHANNEL_CONF |
496
                        AV_CODEC_CAP_DR1,
497
    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
498
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P),
499
    .flush            = osq_flush,
500
};