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