FFmpeg coverage


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