FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/osq.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 180 278 64.7%
Functions: 8 12 66.7%
Branches: 83 144 57.6%

Line Branch Exec 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/internal.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avcodec.h"
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "internal.h"
28 #define BITSTREAM_READER_LE
29 #include "get_bits.h"
30 #include "unary.h"
31
32 #define OFFSET 5
33
34 typedef struct OSQChannel {
35 unsigned prediction;
36 unsigned coding_mode;
37 unsigned residue_parameter;
38 unsigned residue_bits;
39 unsigned history[3];
40 unsigned pos, count;
41 double sum;
42 int32_t prev;
43 } OSQChannel;
44
45 typedef struct OSQContext {
46 GetBitContext gb;
47 OSQChannel ch[2];
48
49 uint8_t *bitstream;
50 size_t max_framesize;
51 size_t bitstream_size;
52
53 int factor;
54 int decorrelate;
55 int frame_samples;
56 uint64_t nb_samples;
57
58 int32_t *decode_buffer[2];
59
60 AVPacket *pkt;
61 int pkt_offset;
62 } OSQContext;
63
64 static void osq_flush(AVCodecContext *avctx)
65 {
66 OSQContext *s = avctx->priv_data;
67
68 s->bitstream_size = 0;
69 s->pkt_offset = 0;
70 }
71
72 2 static av_cold int osq_close(AVCodecContext *avctx)
73 {
74 2 OSQContext *s = avctx->priv_data;
75
76 2 av_freep(&s->bitstream);
77 2 s->bitstream_size = 0;
78
79
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int ch = 0; ch < FF_ARRAY_ELEMS(s->decode_buffer); ch++)
80 4 av_freep(&s->decode_buffer[ch]);
81
82 2 return 0;
83 }
84
85 2 static av_cold int osq_init(AVCodecContext *avctx)
86 {
87 2 OSQContext *s = avctx->priv_data;
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->extradata_size < 48)
90 return AVERROR(EINVAL);
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->extradata[0] != 1) {
93 av_log(avctx, AV_LOG_ERROR, "Unsupported version.\n");
94 return AVERROR_INVALIDDATA;
95 }
96
97 2 avctx->sample_rate = AV_RL32(avctx->extradata + 4);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->sample_rate < 1)
99 return AVERROR_INVALIDDATA;
100
101 2 av_channel_layout_uninit(&avctx->ch_layout);
102 2 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
103 2 avctx->ch_layout.nb_channels = avctx->extradata[3];
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->ch_layout.nb_channels < 1)
105 return AVERROR_INVALIDDATA;
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->ch_layout.nb_channels > FF_ARRAY_ELEMS(s->decode_buffer))
107 return AVERROR_INVALIDDATA;
108
109 2 s->factor = 1;
110
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 switch (avctx->extradata[2]) {
111 case 8: avctx->sample_fmt = AV_SAMPLE_FMT_U8P; break;
112 2 case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16P; break;
113 case 20:
114 case 24: s->factor = 256;
115 avctx->sample_fmt = AV_SAMPLE_FMT_S32P; break;
116 default: return AVERROR_INVALIDDATA;
117 }
118
119 2 avctx->bits_per_raw_sample = avctx->extradata[2];
120 2 s->nb_samples = AV_RL64(avctx->extradata + 16);
121 2 s->frame_samples = AV_RL16(avctx->extradata + 8);
122 2 s->max_framesize = (s->frame_samples * 16 + 1024) * avctx->ch_layout.nb_channels;
123
124 2 s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->bitstream)
126 return AVERROR(ENOMEM);
127
128
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
129 4 s->decode_buffer[ch] = av_calloc(s->frame_samples + OFFSET,
130 sizeof(*s->decode_buffer[ch]));
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->decode_buffer[ch])
132 return AVERROR(ENOMEM);
133 }
134
135 2 s->pkt = avctx->internal->in_pkt;
136
137 2 return 0;
138 }
139
140 static void reset_stats(OSQChannel *cb)
141 {
142 memset(cb->history, 0, sizeof(cb->history));
143 cb->pos = cb->count = cb->sum = 0;
144 }
145
146 static void update_stats(OSQChannel *cb, int val)
147 {
148 cb->sum += FFABS(val) - cb->history[cb->pos];
149 cb->history[cb->pos] = FFABS(val);
150 cb->pos++;
151 cb->count++;
152 if (cb->pos >= FF_ARRAY_ELEMS(cb->history))
153 cb->pos = 0;
154 }
155
156 static int update_residue_parameter(OSQChannel *cb)
157 {
158 double sum, x;
159 int rice_k;
160
161 sum = cb->sum;
162 x = sum / cb->count;
163 rice_k = av_ceil_log2(x);
164 if (rice_k >= 30) {
165 rice_k = floor(sum / 1.4426952 + 0.5);
166 if (rice_k < 1)
167 rice_k = 1;
168 }
169
170 return rice_k;
171 }
172
173 847722 static uint32_t get_urice(GetBitContext *gb, int k)
174 {
175 uint32_t z, x, b;
176
177 847722 x = get_unary(gb, 1, 512);
178 847722 b = get_bits_long(gb, k);
179 847722 z = b | x << k;
180
181 847722 return z;
182 }
183
184 837900 static int32_t get_srice(GetBitContext *gb, int x)
185 {
186 837900 int32_t y = get_urice(gb, x);
187
2/2
✓ Branch 1 taken 416860 times.
✓ Branch 2 taken 421040 times.
837900 return get_bits1(gb) ? -y : y;
188 }
189
190 3274 static int osq_channel_parameters(AVCodecContext *avctx, int ch)
191 {
192 3274 OSQContext *s = avctx->priv_data;
193 3274 OSQChannel *cb = &s->ch[ch];
194 3274 GetBitContext *gb = &s->gb;
195
196 3274 cb->prev = 0;
197 3274 cb->prediction = get_urice(gb, 5);
198 3274 cb->coding_mode = get_urice(gb, 3);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3274 times.
3274 if (cb->prediction >= 15)
200 return AVERROR_INVALIDDATA;
201
2/4
✓ Branch 0 taken 3274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3274 times.
✗ Branch 3 not taken.
3274 if (cb->coding_mode > 0 && cb->coding_mode < 3) {
202 3274 cb->residue_parameter = get_urice(gb, 4);
203
2/4
✓ Branch 0 taken 3274 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3274 times.
3274 if (!cb->residue_parameter || cb->residue_parameter >= 31)
204 return AVERROR_INVALIDDATA;
205 } else if (cb->coding_mode == 3) {
206 cb->residue_bits = get_urice(gb, 4);
207 if (!cb->residue_bits || cb->residue_bits >= 31)
208 return AVERROR_INVALIDDATA;
209 } else if (cb->coding_mode) {
210 return AVERROR_INVALIDDATA;
211 }
212
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3274 times.
3274 if (cb->coding_mode == 2)
214 reset_stats(cb);
215
216 3274 return 0;
217 }
218
219 #define A (-1)
220 #define B (-2)
221 #define C (-3)
222 #define D (-4)
223 #define E (-5)
224 #define P2 ((dst[A] + dst[A]) - dst[B])
225 #define P3 ((dst[A] - dst[B]) * 3 + dst[C])
226
227 1637 static int do_decode(AVCodecContext *avctx, AVFrame *frame, int decorrelate, int downsample)
228 {
229 1637 OSQContext *s = avctx->priv_data;
230 1637 const int nb_channels = avctx->ch_layout.nb_channels;
231 1637 const int nb_samples = frame->nb_samples;
232 1637 GetBitContext *gb = &s->gb;
233
234
2/2
✓ Branch 0 taken 418950 times.
✓ Branch 1 taken 1637 times.
420587 for (int n = 0; n < nb_samples; n++) {
235
2/2
✓ Branch 0 taken 837900 times.
✓ Branch 1 taken 418950 times.
1256850 for (int ch = 0; ch < nb_channels; ch++) {
236 837900 OSQChannel *cb = &s->ch[ch];
237 837900 int32_t *dst = s->decode_buffer[ch] + OFFSET;
238 837900 int32_t p, prev = cb->prev;
239
240
5/6
✓ Branch 0 taken 837900 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 418950 times.
✓ Branch 3 taken 418950 times.
✓ Branch 4 taken 99 times.
✓ Branch 5 taken 418851 times.
837900 if (nb_channels == 2 && ch == 1 && decorrelate != s->decorrelate) {
241
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 50 times.
99 if (!decorrelate) {
242 49 s->decode_buffer[1][OFFSET+A] += s->decode_buffer[0][OFFSET+B];
243 49 s->decode_buffer[1][OFFSET+B] += s->decode_buffer[0][OFFSET+C];
244 49 s->decode_buffer[1][OFFSET+C] += s->decode_buffer[0][OFFSET+D];
245 49 s->decode_buffer[1][OFFSET+D] += s->decode_buffer[0][OFFSET+E];
246 } else {
247 50 s->decode_buffer[1][OFFSET+A] -= s->decode_buffer[0][OFFSET+B];
248 50 s->decode_buffer[1][OFFSET+B] -= s->decode_buffer[0][OFFSET+C];
249 50 s->decode_buffer[1][OFFSET+C] -= s->decode_buffer[0][OFFSET+D];
250 50 s->decode_buffer[1][OFFSET+D] -= s->decode_buffer[0][OFFSET+E];
251 }
252 99 s->decorrelate = decorrelate;
253 }
254
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 837900 times.
837900 if (!cb->coding_mode) {
256 dst[n] = 0;
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 837900 times.
837900 } else if (cb->coding_mode == 3) {
258 dst[n] = get_sbits_long(gb, cb->residue_bits);
259 } else {
260 837900 dst[n] = get_srice(gb, cb->residue_parameter);
261 }
262
263
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 837900 times.
837900 if (get_bits_left(gb) < 0) {
264 av_log(avctx, AV_LOG_ERROR, "overread!\n");
265 return AVERROR_INVALIDDATA;
266 }
267
268 837900 p = prev / 2;
269 837900 prev = dst[n];
270
271
12/16
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 49920 times.
✓ Branch 2 taken 159232 times.
✓ Branch 3 taken 35462 times.
✓ Branch 4 taken 29184 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 14592 times.
✓ Branch 8 taken 23296 times.
✓ Branch 9 taken 29440 times.
✓ Branch 10 taken 11264 times.
✓ Branch 11 taken 38656 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 13568 times.
✓ Branch 14 taken 420230 times.
✗ Branch 15 not taken.
837900 switch (cb->prediction) {
272 13056 case 0:
273 13056 break;
274 49920 case 1:
275 49920 dst[n] += dst[A];
276 49920 break;
277 159232 case 2:
278 159232 dst[n] += dst[A] + p;
279 159232 break;
280 35462 case 3:
281 35462 dst[n] += P2;
282 35462 break;
283 29184 case 4:
284 29184 dst[n] += P2 + p;
285 29184 break;
286 case 5:
287 dst[n] += P3;
288 break;
289 case 6:
290 dst[n] += P3 + p;
291 break;
292 14592 case 7:
293 14592 dst[n] += (P2 + P3) / 2 + p;
294 14592 break;
295 23296 case 8:
296 23296 dst[n] += (P2 + P3) / 2;
297 23296 break;
298 29440 case 9:
299 29440 dst[n] += (P2 * 2 + P3) / 3 + p;
300 29440 break;
301 11264 case 10:
302 11264 dst[n] += (P2 + P3 * 2) / 3 + p;
303 11264 break;
304 38656 case 11:
305 38656 dst[n] += (dst[A] + dst[B]) / 2;
306 38656 break;
307 case 12:
308 dst[n] += dst[B];
309 break;
310 13568 case 13:
311 13568 dst[n] += (dst[D] + dst[B]) / 2;
312 13568 break;
313 420230 case 14:
314 420230 dst[n] += (P2 + dst[A]) / 2 + p;
315 420230 break;
316 default:
317 return AVERROR_INVALIDDATA;
318 }
319
320 837900 cb->prev = prev;
321
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 837900 times.
837900 if (downsample)
323 dst[n] *= 256;
324
325 837900 dst[E] = dst[D];
326 837900 dst[D] = dst[C];
327 837900 dst[C] = dst[B];
328 837900 dst[B] = dst[A];
329 837900 dst[A] = dst[n];
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 837900 times.
837900 if (cb->coding_mode == 2) {
332 update_stats(cb, dst[n]);
333 cb->residue_parameter = update_residue_parameter(cb);
334 }
335
336
3/4
✓ Branch 0 taken 837900 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 418950 times.
✓ Branch 3 taken 418950 times.
837900 if (nb_channels == 2 && ch == 1) {
337
2/2
✓ Branch 0 taken 390022 times.
✓ Branch 1 taken 28928 times.
418950 if (decorrelate)
338 390022 dst[n] += s->decode_buffer[0][OFFSET+n];
339 }
340
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 837900 times.
837900 if (downsample)
342 dst[A] /= 256;
343 }
344 }
345
346 1637 return 0;
347 }
348
349 1637 static int osq_decode_block(AVCodecContext *avctx, AVFrame *frame)
350 {
351 1637 const int nb_channels = avctx->ch_layout.nb_channels;
352 1637 const int nb_samples = frame->nb_samples;
353 1637 OSQContext *s = avctx->priv_data;
354 1637 const int factor = s->factor;
355 int ret, decorrelate, downsample;
356 1637 GetBitContext *gb = &s->gb;
357
358 1637 skip_bits1(gb);
359 1637 decorrelate = get_bits1(gb);
360 1637 downsample = get_bits1(gb);
361
362
2/2
✓ Branch 0 taken 3274 times.
✓ Branch 1 taken 1637 times.
4911 for (int ch = 0; ch < nb_channels; ch++) {
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3274 times.
3274 if ((ret = osq_channel_parameters(avctx, ch)) < 0) {
364 av_log(avctx, AV_LOG_ERROR, "invalid channel parameters\n");
365 return ret;
366 }
367 }
368
369
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1637 times.
1637 if ((ret = do_decode(avctx, frame, decorrelate, downsample)) < 0)
370 return ret;
371
372 1637 align_get_bits(gb);
373
374
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1637 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1637 switch (avctx->sample_fmt) {
375 case AV_SAMPLE_FMT_U8P:
376 for (int ch = 0; ch < nb_channels; ch++) {
377 uint8_t *dst = (uint8_t *)frame->extended_data[ch];
378 int32_t *src = s->decode_buffer[ch] + OFFSET;
379
380 for (int n = 0; n < nb_samples; n++)
381 dst[n] = av_clip_uint8(src[n] + 0x80);
382 }
383 break;
384 1637 case AV_SAMPLE_FMT_S16P:
385
2/2
✓ Branch 0 taken 3274 times.
✓ Branch 1 taken 1637 times.
4911 for (int ch = 0; ch < nb_channels; ch++) {
386 3274 int16_t *dst = (int16_t *)frame->extended_data[ch];
387 3274 int32_t *src = s->decode_buffer[ch] + OFFSET;
388
389
2/2
✓ Branch 0 taken 837900 times.
✓ Branch 1 taken 3274 times.
841174 for (int n = 0; n < nb_samples; n++)
390 837900 dst[n] = (int16_t)src[n];
391 }
392 1637 break;
393 case AV_SAMPLE_FMT_S32P:
394 for (int ch = 0; ch < nb_channels; ch++) {
395 int32_t *dst = (int32_t *)frame->extended_data[ch];
396 int32_t *src = s->decode_buffer[ch] + OFFSET;
397
398 for (int n = 0; n < nb_samples; n++)
399 dst[n] = src[n] * factor;
400 }
401 break;
402 default:
403 return AVERROR_BUG;
404 }
405
406 1637 return 0;
407 }
408
409 2674 static int osq_receive_frame(AVCodecContext *avctx, AVFrame *frame)
410 {
411 2674 OSQContext *s = avctx->priv_data;
412 2674 GetBitContext *gb = &s->gb;
413 int ret, n;
414
415
2/2
✓ Branch 0 taken 3698 times.
✓ Branch 1 taken 1620 times.
7992 while (s->bitstream_size < s->max_framesize) {
416 int size;
417
418
2/2
✓ Branch 0 taken 2080 times.
✓ Branch 1 taken 1618 times.
3698 if (!s->pkt->data) {
419 2080 ret = ff_decode_get_packet(avctx, s->pkt);
420
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2062 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1 times.
2080 if (ret == AVERROR_EOF && s->bitstream_size > 0)
421 17 break;
422
4/4
✓ Branch 0 taken 2062 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1036 times.
✓ Branch 3 taken 1026 times.
2063 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
423 1037 return ret;
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1026 times.
1026 if (ret < 0)
425 goto fail;
426 }
427
428 2644 size = FFMIN(s->pkt->size - s->pkt_offset, s->max_framesize - s->bitstream_size);
429 2644 memcpy(s->bitstream + s->bitstream_size, s->pkt->data + s->pkt_offset, size);
430 2644 s->bitstream_size += size;
431 2644 s->pkt_offset += size;
432
433
2/2
✓ Branch 0 taken 1618 times.
✓ Branch 1 taken 1026 times.
2644 if (s->pkt_offset == s->pkt->size) {
434 1026 av_packet_unref(s->pkt);
435 1026 s->pkt_offset = 0;
436 }
437 }
438
439 1637 frame->nb_samples = FFMIN(s->frame_samples, s->nb_samples);
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1637 times.
1637 if (frame->nb_samples <= 0)
441 return AVERROR_EOF;
442
443
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1637 times.
1637 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
444 goto fail;
445
446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1637 times.
1637 if ((ret = init_get_bits8(gb, s->bitstream, s->bitstream_size)) < 0)
447 goto fail;
448
449
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1637 times.
1637 if ((ret = osq_decode_block(avctx, frame)) < 0)
450 goto fail;
451
452 1637 s->nb_samples -= frame->nb_samples;
453
454 1637 n = get_bits_count(gb) / 8;
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1637 times.
1637 if (n > s->bitstream_size) {
456 ret = AVERROR_INVALIDDATA;
457 goto fail;
458 }
459
460 1637 memmove(s->bitstream, &s->bitstream[n], s->bitstream_size - n);
461 1637 s->bitstream_size -= n;
462
463 1637 return 0;
464
465 fail:
466 s->bitstream_size = 0;
467 s->pkt_offset = 0;
468 av_packet_unref(s->pkt);
469
470 return ret;
471 }
472
473 const FFCodec ff_osq_decoder = {
474 .p.name = "osq",
475 CODEC_LONG_NAME("OSQ (Original Sound Quality)"),
476 .p.type = AVMEDIA_TYPE_AUDIO,
477 .p.id = AV_CODEC_ID_OSQ,
478 .priv_data_size = sizeof(OSQContext),
479 .init = osq_init,
480 FF_CODEC_RECEIVE_FRAME_CB(osq_receive_frame),
481 .close = osq_close,
482 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
483 AV_CODEC_CAP_DR1,
484 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
485 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
486 AV_SAMPLE_FMT_S16P,
487 AV_SAMPLE_FMT_S32P,
488 AV_SAMPLE_FMT_NONE },
489 .flush = osq_flush,
490 };
491