FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ralf.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 239 287 83.3%
Branches: 123 168 73.2%

Line Branch Exec Source
1 /*
2 * RealAudio Lossless decoder
3 *
4 * Copyright (c) 2012 Konstantin Shishkov
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * This is a decoder for Real Audio Lossless format.
26 * Dedicated to the mastermind behind it, Ralph Wiggum.
27 */
28
29 #include "libavutil/attributes.h"
30 #include "libavutil/channel_layout.h"
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "get_bits.h"
34 #include "golomb.h"
35 #include "internal.h"
36 #include "unary.h"
37 #include "ralfdata.h"
38
39 #define FILTER_NONE 0
40 #define FILTER_RAW 642
41
42 typedef struct VLCSet {
43 VLC filter_params;
44 VLC bias;
45 VLC coding_mode;
46 VLC filter_coeffs[10][11];
47 VLC short_codes[15];
48 VLC long_codes[125];
49 } VLCSet;
50
51 #define RALF_MAX_PKT_SIZE 8192
52
53 typedef struct RALFContext {
54 int version;
55 int max_frame_size;
56 VLCSet sets[3];
57 int32_t channel_data[2][4096];
58
59 int filter_params; ///< combined filter parameters for the current channel data
60 int filter_length; ///< length of the filter for the current channel data
61 int filter_bits; ///< filter precision for the current channel data
62 int32_t filter[64];
63
64 unsigned bias[2]; ///< a constant value added to channel data after filtering
65
66 int num_blocks; ///< number of blocks inside the frame
67 int sample_offset;
68 int block_size[1 << 12]; ///< size of the blocks
69 int block_pts[1 << 12]; ///< block start time (in milliseconds)
70
71 uint8_t pkt[16384];
72 int has_pkt;
73 } RALFContext;
74
75 #define MAX_ELEMS 644 // no RALF table uses more than that
76
77 1518 static av_cold int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
78 {
79 uint8_t lens[MAX_ELEMS];
80 uint16_t codes[MAX_ELEMS];
81 int counts[17], prefixes[18];
82 int i, cur_len;
83 1518 int max_bits = 0;
84 1518 int nb = 0;
85
86
2/2
✓ Branch 0 taken 25806 times.
✓ Branch 1 taken 1518 times.
27324 for (i = 0; i <= 16; i++)
87 25806 counts[i] = 0;
88
2/2
✓ Branch 0 taken 380568 times.
✓ Branch 1 taken 1518 times.
382086 for (i = 0; i < elems; i++) {
89
2/2
✓ Branch 0 taken 189528 times.
✓ Branch 1 taken 191040 times.
380568 cur_len = (nb ? *data & 0xF : *data >> 4) + 1;
90 380568 counts[cur_len]++;
91 380568 max_bits = FFMAX(max_bits, cur_len);
92 380568 lens[i] = cur_len;
93 380568 data += nb;
94 380568 nb ^= 1;
95 }
96 1518 prefixes[1] = 0;
97
2/2
✓ Branch 0 taken 24288 times.
✓ Branch 1 taken 1518 times.
25806 for (i = 1; i <= 16; i++)
98 24288 prefixes[i + 1] = (prefixes[i] + counts[i]) << 1;
99
100
2/2
✓ Branch 0 taken 380568 times.
✓ Branch 1 taken 1518 times.
382086 for (i = 0; i < elems; i++)
101 380568 codes[i] = prefixes[lens[i]]++;
102
103 1518 return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), elems,
104 lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0);
105 }
106
107 2 static av_cold int decode_close(AVCodecContext *avctx)
108 {
109 2 RALFContext *ctx = avctx->priv_data;
110 int i, j, k;
111
112
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < 3; i++) {
113 6 ff_free_vlc(&ctx->sets[i].filter_params);
114 6 ff_free_vlc(&ctx->sets[i].bias);
115 6 ff_free_vlc(&ctx->sets[i].coding_mode);
116
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 6 times.
66 for (j = 0; j < 10; j++)
117
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 60 times.
720 for (k = 0; k < 11; k++)
118 660 ff_free_vlc(&ctx->sets[i].filter_coeffs[j][k]);
119
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 6 times.
96 for (j = 0; j < 15; j++)
120 90 ff_free_vlc(&ctx->sets[i].short_codes[j]);
121
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 6 times.
756 for (j = 0; j < 125; j++)
122 750 ff_free_vlc(&ctx->sets[i].long_codes[j]);
123 }
124
125 2 return 0;
126 }
127
128 2 static av_cold int decode_init(AVCodecContext *avctx)
129 {
130 2 RALFContext *ctx = avctx->priv_data;
131 int i, j, k;
132 int ret, channels;
133
134
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (avctx->extradata_size < 24 || memcmp(avctx->extradata, "LSD:", 4)) {
135 av_log(avctx, AV_LOG_ERROR, "Extradata is not groovy, dude\n");
136 return AVERROR_INVALIDDATA;
137 }
138
139 2 ctx->version = AV_RB16(avctx->extradata + 4);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ctx->version != 0x103) {
141 avpriv_request_sample(avctx, "Unknown version %X", ctx->version);
142 return AVERROR_PATCHWELCOME;
143 }
144
145 2 channels = AV_RB16(avctx->extradata + 8);
146 2 avctx->sample_rate = AV_RB32(avctx->extradata + 12);
147
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (channels < 1 || channels > 2
148
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 || avctx->sample_rate < 8000 || avctx->sample_rate > 96000) {
149 av_log(avctx, AV_LOG_ERROR, "Invalid coding parameters %d Hz %d ch\n",
150 avctx->sample_rate, channels);
151 return AVERROR_INVALIDDATA;
152 }
153 2 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
154 2 av_channel_layout_uninit(&avctx->ch_layout);
155 2 av_channel_layout_default(&avctx->ch_layout, channels);
156
157 2 ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
158
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
159 av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
160 ctx->max_frame_size);
161 }
162 2 ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
163
164
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < 3; i++) {
165 6 ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i],
166 FILTERPARAM_ELEMENTS);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
168 return ret;
169 6 ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
171 return ret;
172 6 ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
173 CODING_MODE_ELEMENTS);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
175 return ret;
176
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 6 times.
66 for (j = 0; j < 10; j++) {
177
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 60 times.
720 for (k = 0; k < 11; k++) {
178 660 ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k],
179 660 filter_coeffs_def[i][j][k],
180 FILTER_COEFFS_ELEMENTS);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660 times.
660 if (ret < 0)
182 return ret;
183 }
184 }
185
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 6 times.
96 for (j = 0; j < 15; j++) {
186 90 ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
187 90 short_codes_def[i][j], SHORT_CODES_ELEMENTS);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (ret < 0)
189 return ret;
190 }
191
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 6 times.
756 for (j = 0; j < 125; j++) {
192 750 ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
193 750 long_codes_def[i][j], LONG_CODES_ELEMENTS);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 750 times.
750 if (ret < 0)
195 return ret;
196 }
197 }
198
199 2 return 0;
200 }
201
202 869132 static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
203 {
204
2/2
✓ Branch 0 taken 7368 times.
✓ Branch 1 taken 861764 times.
869132 if (val == 0) {
205 7368 val = -range - get_ue_golomb(gb);
206
2/2
✓ Branch 0 taken 4328 times.
✓ Branch 1 taken 857436 times.
861764 } else if (val == range * 2) {
207 4328 val = range + get_ue_golomb(gb);
208 } else {
209 857436 val -= range;
210 }
211
2/2
✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 865280 times.
869132 if (bits)
212 3852 val = ((unsigned)val << bits) | get_bits(gb, bits);
213 869132 return val;
214 }
215
216 430 static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
217 int length, int mode, int bits)
218 {
219 int i, t;
220 int code_params;
221 430 VLCSet *set = ctx->sets + mode;
222 VLC *code_vlc; int range, range2, add_bits;
223 430 int *dst = ctx->channel_data[ch];
224
225 430 ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
226
1/2
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
430 if (ctx->filter_params > 1) {
227 430 ctx->filter_bits = (ctx->filter_params - 2) >> 6;
228 430 ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
229 }
230
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
430 if (ctx->filter_params == FILTER_RAW) {
232 for (i = 0; i < length; i++)
233 dst[i] = get_bits(gb, bits);
234 ctx->bias[ch] = 0;
235 return 0;
236 }
237
238 430 ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
239 430 ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
240
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
430 if (ctx->filter_params == FILTER_NONE) {
242 memset(dst, 0, sizeof(*dst) * length);
243 return 0;
244 }
245
246
1/2
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
430 if (ctx->filter_params > 1) {
247 430 int cmode = 0, coeff = 0;
248 430 VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
249
250 430 add_bits = ctx->filter_bits;
251
252
2/2
✓ Branch 0 taken 3422 times.
✓ Branch 1 taken 430 times.
3852 for (i = 0; i < ctx->filter_length; i++) {
253 3422 t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
254 3422 t = extend_code(gb, t, 21, add_bits);
255
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 2651 times.
3422 if (!cmode)
256 771 coeff -= 12U << add_bits;
257 3422 coeff = (unsigned)t - coeff;
258 3422 ctx->filter[i] = coeff;
259
260 3422 cmode = coeff >> add_bits;
261
2/2
✓ Branch 0 taken 1485 times.
✓ Branch 1 taken 1937 times.
3422 if (cmode < 0) {
262 1485 cmode = -1 - av_log2(-cmode);
263
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1481 times.
1485 if (cmode < -5)
264 4 cmode = -5;
265
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 425 times.
1937 } else if (cmode > 0) {
266 1512 cmode = 1 + av_log2(cmode);
267
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1510 times.
1512 if (cmode > 5)
268 2 cmode = 5;
269 }
270 }
271 }
272
273 430 code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
274
1/2
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
430 if (code_params >= 15) {
275 430 add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
276
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
430 if (add_bits > 9 && (code_params % 5) != 2)
277 add_bits--;
278 430 range = 10;
279 430 range2 = 21;
280 430 code_vlc = set->long_codes + (code_params - 15);
281 } else {
282 add_bits = 0;
283 range = 6;
284 range2 = 13;
285 code_vlc = set->short_codes + code_params;
286 }
287
288
2/2
✓ Branch 0 taken 432640 times.
✓ Branch 1 taken 430 times.
433070 for (i = 0; i < length; i += 2) {
289 int code1, code2;
290
291 432640 t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
292 432640 code1 = t / range2;
293 432640 code2 = t % range2;
294 432640 dst[i] = extend_code(gb, code1, range, 0) * (1U << add_bits);
295 432640 dst[i + 1] = extend_code(gb, code2, range, 0) * (1U << add_bits);
296
1/2
✓ Branch 0 taken 432640 times.
✗ Branch 1 not taken.
432640 if (add_bits) {
297 432640 dst[i] |= get_bits(gb, add_bits);
298 432640 dst[i + 1] |= get_bits(gb, add_bits);
299 }
300 }
301
302 430 return 0;
303 }
304
305 430 static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
306 {
307 int i, j, acc;
308 430 int *audio = ctx->channel_data[ch];
309 430 int bias = 1 << (ctx->filter_bits - 1);
310 430 int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1;
311
312
2/2
✓ Branch 0 taken 864850 times.
✓ Branch 1 taken 430 times.
865280 for (i = 1; i < length; i++) {
313 864850 int flen = FFMIN(ctx->filter_length, i);
314
315 864850 acc = 0;
316
2/2
✓ Branch 0 taken 7772933 times.
✓ Branch 1 taken 864850 times.
8637783 for (j = 0; j < flen; j++)
317 7772933 acc += (unsigned)ctx->filter[j] * audio[i - j - 1];
318
2/2
✓ Branch 0 taken 430200 times.
✓ Branch 1 taken 434650 times.
864850 if (acc < 0) {
319 430200 acc = (acc + bias - 1) >> ctx->filter_bits;
320 430200 acc = FFMAX(acc, min_clip);
321 } else {
322 434650 acc = ((unsigned)acc + bias) >> ctx->filter_bits;
323 434650 acc = FFMIN(acc, max_clip);
324 }
325 864850 audio[i] += acc;
326 }
327 430 }
328
329 215 static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
330 int16_t *dst0, int16_t *dst1)
331 {
332 215 RALFContext *ctx = avctx->priv_data;
333 int len, ch, ret;
334 int dmode, mode[2], bits[2];
335 int *ch0, *ch1;
336 int i;
337 unsigned int t, t2;
338
339 215 len = 12 - get_unary(gb, 0, 6);
340
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
342 215 len = 1 << len;
343
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (ctx->sample_offset + len > ctx->max_frame_size) {
345 av_log(avctx, AV_LOG_ERROR,
346 "Decoder's stomach is crying, it ate too many samples\n");
347 return AVERROR_INVALIDDATA;
348 }
349
350
1/2
✓ Branch 0 taken 215 times.
✗ Branch 1 not taken.
215 if (avctx->ch_layout.nb_channels > 1)
351 215 dmode = get_bits(gb, 2) + 1;
352 else
353 dmode = 0;
354
355 215 mode[0] = (dmode == 4) ? 1 : 0;
356
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 2 times.
215 mode[1] = (dmode >= 2) ? 2 : 0;
357 215 bits[0] = 16;
358
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 2 times.
215 bits[1] = (mode[1] == 2) ? 17 : 16;
359
360
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 215 times.
645 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
361
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 430 times.
430 if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
362 return ret;
363
2/4
✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 430 times.
✗ Branch 3 not taken.
430 if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
364 430 ctx->filter_bits += 3;
365 430 apply_lpc(ctx, ch, len, bits[ch]);
366 }
367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 430 times.
430 if (get_bits_left(gb) < 0)
368 return AVERROR_INVALIDDATA;
369 }
370 215 ch0 = ctx->channel_data[0];
371 215 ch1 = ctx->channel_data[1];
372
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 91 times.
✗ Branch 5 not taken.
215 switch (dmode) {
373 case 0:
374 for (i = 0; i < len; i++)
375 dst0[i] = ch0[i] + ctx->bias[0];
376 break;
377 2 case 1:
378
2/2
✓ Branch 0 taken 8192 times.
✓ Branch 1 taken 2 times.
8194 for (i = 0; i < len; i++) {
379 8192 dst0[i] = ch0[i] + ctx->bias[0];
380 8192 dst1[i] = ch1[i] + ctx->bias[1];
381 }
382 2 break;
383 79 case 2:
384
2/2
✓ Branch 0 taken 179456 times.
✓ Branch 1 taken 79 times.
179535 for (i = 0; i < len; i++) {
385 179456 ch0[i] += ctx->bias[0];
386 179456 dst0[i] = ch0[i];
387 179456 dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
388 }
389 79 break;
390 43 case 3:
391
2/2
✓ Branch 0 taken 60672 times.
✓ Branch 1 taken 43 times.
60715 for (i = 0; i < len; i++) {
392 60672 t = ch0[i] + ctx->bias[0];
393 60672 t2 = ch1[i] + ctx->bias[1];
394 60672 dst0[i] = t + t2;
395 60672 dst1[i] = t;
396 }
397 43 break;
398 91 case 4:
399
2/2
✓ Branch 0 taken 184320 times.
✓ Branch 1 taken 91 times.
184411 for (i = 0; i < len; i++) {
400 184320 t = ch1[i] + ctx->bias[1];
401 184320 t2 = ((ch0[i] + ctx->bias[0]) * 2) | (t & 1);
402 184320 dst0[i] = (int)(t2 + t) / 2;
403 184320 dst1[i] = (int)(t2 - t) / 2;
404 }
405 91 break;
406 }
407
408 215 ctx->sample_offset += len;
409
410 215 return 0;
411 }
412
413 191 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
414 int *got_frame_ptr, AVPacket *avpkt)
415 {
416 191 RALFContext *ctx = avctx->priv_data;
417 int16_t *samples0;
418 int16_t *samples1;
419 int ret;
420 GetBitContext gb;
421 int table_size, table_bytes, i;
422 const uint8_t *src, *block_pointer;
423 int src_size;
424 int bytes_left;
425
426
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 152 times.
191 if (ctx->has_pkt) {
427 39 ctx->has_pkt = 0;
428 39 table_bytes = (AV_RB16(avpkt->data) + 7) >> 3;
429
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
39 if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) {
430 av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n");
431 return AVERROR_INVALIDDATA;
432 }
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) {
434 av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n");
435 return AVERROR_INVALIDDATA;
436 }
437
438 39 src = ctx->pkt;
439 39 src_size = RALF_MAX_PKT_SIZE + avpkt->size;
440 39 memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes,
441 39 avpkt->size - 2 - table_bytes);
442 } else {
443
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 113 times.
152 if (avpkt->size == RALF_MAX_PKT_SIZE) {
444 39 memcpy(ctx->pkt, avpkt->data, avpkt->size);
445 39 ctx->has_pkt = 1;
446 39 *got_frame_ptr = 0;
447
448 39 return avpkt->size;
449 }
450 113 src = avpkt->data;
451 113 src_size = avpkt->size;
452 }
453
454 152 frame->nb_samples = ctx->max_frame_size;
455
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
152 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
456 return ret;
457 152 samples0 = (int16_t *)frame->data[0];
458 152 samples1 = (int16_t *)frame->data[1];
459
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
152 if (src_size < 5) {
461 av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
462 return AVERROR_INVALIDDATA;
463 }
464 152 table_size = AV_RB16(src);
465 152 table_bytes = (table_size + 7) >> 3;
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
152 if (src_size < table_bytes + 3) {
467 av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
468 return AVERROR_INVALIDDATA;
469 }
470 152 init_get_bits(&gb, src + 2, table_size);
471 152 ctx->num_blocks = 0;
472
2/2
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 152 times.
368 while (get_bits_left(&gb) > 0) {
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ctx->num_blocks >= FF_ARRAY_ELEMS(ctx->block_size))
474 return AVERROR_INVALIDDATA;
475 216 ctx->block_size[ctx->num_blocks] = get_bits(&gb, 13 + avctx->ch_layout.nb_channels);
476
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 152 times.
216 if (get_bits1(&gb)) {
477 64 ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
478 } else {
479 152 ctx->block_pts[ctx->num_blocks] = 0;
480 }
481 216 ctx->num_blocks++;
482 }
483
484 152 block_pointer = src + table_bytes + 2;
485 152 bytes_left = src_size - table_bytes - 2;
486 152 ctx->sample_offset = 0;
487
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 151 times.
367 for (i = 0; i < ctx->num_blocks; i++) {
488
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 215 times.
216 if (bytes_left < ctx->block_size[i]) {
489 1 av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
490 1 break;
491 }
492 215 init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (decode_block(avctx, &gb, samples0 + ctx->sample_offset,
494 215 samples1 + ctx->sample_offset) < 0) {
495 av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
496 break;
497 }
498 215 block_pointer += ctx->block_size[i];
499 215 bytes_left -= ctx->block_size[i];
500 }
501
502 152 frame->nb_samples = ctx->sample_offset;
503 152 *got_frame_ptr = ctx->sample_offset > 0;
504
505 152 return avpkt->size;
506 }
507
508 static void decode_flush(AVCodecContext *avctx)
509 {
510 RALFContext *ctx = avctx->priv_data;
511
512 ctx->has_pkt = 0;
513 }
514
515
516 const FFCodec ff_ralf_decoder = {
517 .p.name = "ralf",
518 .p.long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
519 .p.type = AVMEDIA_TYPE_AUDIO,
520 .p.id = AV_CODEC_ID_RALF,
521 .priv_data_size = sizeof(RALFContext),
522 .init = decode_init,
523 .close = decode_close,
524 FF_CODEC_DECODE_CB(decode_frame),
525 .flush = decode_flush,
526 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
527 AV_CODEC_CAP_DR1,
528 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
529 AV_SAMPLE_FMT_NONE },
530 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
531 };
532