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