/src/ffmpeg/libavcodec/ralf.c
Line | Count | Source (jump to first uncovered line) |
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 "decode.h" |
34 | | #include "get_bits.h" |
35 | | #include "golomb.h" |
36 | | #include "unary.h" |
37 | | #include "ralfdata.h" |
38 | | |
39 | 182k | #define FILTER_NONE 0 |
40 | 288k | #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 | 196k | #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 sample_offset; |
67 | | int block_size[1 << 12]; ///< size of the blocks |
68 | | int block_pts[1 << 12]; ///< block start time (in milliseconds) |
69 | | |
70 | | uint8_t pkt[16384]; |
71 | | int has_pkt; |
72 | | } RALFContext; |
73 | | |
74 | | #define MAX_ELEMS 644 // no RALF table uses more than that |
75 | | |
76 | | static av_cold int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems) |
77 | 763k | { |
78 | 763k | uint8_t lens[MAX_ELEMS]; |
79 | 763k | uint16_t codes[MAX_ELEMS]; |
80 | 763k | int counts[17], prefixes[18]; |
81 | 763k | int i, cur_len; |
82 | 763k | int max_bits = 0; |
83 | 763k | int nb = 0; |
84 | | |
85 | 13.7M | for (i = 0; i <= 16; i++) |
86 | 12.9M | counts[i] = 0; |
87 | 192M | for (i = 0; i < elems; i++) { |
88 | 191M | cur_len = (nb ? *data & 0xF : *data >> 4) + 1; |
89 | 191M | counts[cur_len]++; |
90 | 191M | max_bits = FFMAX(max_bits, cur_len); |
91 | 191M | lens[i] = cur_len; |
92 | 191M | data += nb; |
93 | 191M | nb ^= 1; |
94 | 191M | } |
95 | 763k | prefixes[1] = 0; |
96 | 12.9M | for (i = 1; i <= 16; i++) |
97 | 12.2M | prefixes[i + 1] = (prefixes[i] + counts[i]) << 1; |
98 | | |
99 | 192M | for (i = 0; i < elems; i++) |
100 | 191M | codes[i] = prefixes[lens[i]]++; |
101 | | |
102 | 763k | return ff_vlc_init_sparse(vlc, FFMIN(max_bits, 9), elems, |
103 | 763k | lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0); |
104 | 763k | } |
105 | | |
106 | | static av_cold int decode_close(AVCodecContext *avctx) |
107 | 1.30k | { |
108 | 1.30k | RALFContext *ctx = avctx->priv_data; |
109 | 1.30k | int i, j, k; |
110 | | |
111 | 5.20k | for (i = 0; i < 3; i++) { |
112 | 3.90k | ff_vlc_free(&ctx->sets[i].filter_params); |
113 | 3.90k | ff_vlc_free(&ctx->sets[i].bias); |
114 | 3.90k | ff_vlc_free(&ctx->sets[i].coding_mode); |
115 | 42.9k | for (j = 0; j < 10; j++) |
116 | 468k | for (k = 0; k < 11; k++) |
117 | 429k | ff_vlc_free(&ctx->sets[i].filter_coeffs[j][k]); |
118 | 62.4k | for (j = 0; j < 15; j++) |
119 | 58.5k | ff_vlc_free(&ctx->sets[i].short_codes[j]); |
120 | 491k | for (j = 0; j < 125; j++) |
121 | 487k | ff_vlc_free(&ctx->sets[i].long_codes[j]); |
122 | 3.90k | } |
123 | | |
124 | 1.30k | return 0; |
125 | 1.30k | } |
126 | | |
127 | | static av_cold int decode_init(AVCodecContext *avctx) |
128 | 1.30k | { |
129 | 1.30k | RALFContext *ctx = avctx->priv_data; |
130 | 1.30k | int i, j, k; |
131 | 1.30k | int ret, channels; |
132 | | |
133 | 1.30k | if (avctx->extradata_size < 24 || memcmp(avctx->extradata, "LSD:", 4)) { |
134 | 239 | av_log(avctx, AV_LOG_ERROR, "Extradata is not groovy, dude\n"); |
135 | 239 | return AVERROR_INVALIDDATA; |
136 | 239 | } |
137 | | |
138 | 1.06k | ctx->version = AV_RB16(avctx->extradata + 4); |
139 | 1.06k | if (ctx->version != 0x103) { |
140 | 1 | avpriv_request_sample(avctx, "Unknown version %X", ctx->version); |
141 | 1 | return AVERROR_PATCHWELCOME; |
142 | 1 | } |
143 | | |
144 | 1.06k | channels = AV_RB16(avctx->extradata + 8); |
145 | 1.06k | avctx->sample_rate = AV_RB32(avctx->extradata + 12); |
146 | 1.06k | if (channels < 1 || channels > 2 |
147 | 1.06k | || avctx->sample_rate < 8000 || avctx->sample_rate > 96000) { |
148 | 55 | av_log(avctx, AV_LOG_ERROR, "Invalid coding parameters %d Hz %d ch\n", |
149 | 55 | avctx->sample_rate, channels); |
150 | 55 | return AVERROR_INVALIDDATA; |
151 | 55 | } |
152 | 1.00k | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; |
153 | 1.00k | av_channel_layout_uninit(&avctx->ch_layout); |
154 | 1.00k | av_channel_layout_default(&avctx->ch_layout, channels); |
155 | | |
156 | 1.00k | ctx->max_frame_size = AV_RB32(avctx->extradata + 16); |
157 | 1.00k | if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) { |
158 | 100 | av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n", |
159 | 100 | ctx->max_frame_size); |
160 | 100 | } |
161 | 1.00k | ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate); |
162 | | |
163 | 4.02k | for (i = 0; i < 3; i++) { |
164 | 3.01k | ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i], |
165 | 3.01k | FILTERPARAM_ELEMENTS); |
166 | 3.01k | if (ret < 0) |
167 | 0 | return ret; |
168 | 3.01k | ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS); |
169 | 3.01k | if (ret < 0) |
170 | 0 | return ret; |
171 | 3.01k | ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i], |
172 | 3.01k | CODING_MODE_ELEMENTS); |
173 | 3.01k | if (ret < 0) |
174 | 0 | return ret; |
175 | 33.1k | for (j = 0; j < 10; j++) { |
176 | 362k | for (k = 0; k < 11; k++) { |
177 | 331k | ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k], |
178 | 331k | filter_coeffs_def[i][j][k], |
179 | 331k | FILTER_COEFFS_ELEMENTS); |
180 | 331k | if (ret < 0) |
181 | 0 | return ret; |
182 | 331k | } |
183 | 30.1k | } |
184 | 48.2k | for (j = 0; j < 15; j++) { |
185 | 45.2k | ret = init_ralf_vlc(&ctx->sets[i].short_codes[j], |
186 | 45.2k | short_codes_def[i][j], SHORT_CODES_ELEMENTS); |
187 | 45.2k | if (ret < 0) |
188 | 0 | return ret; |
189 | 45.2k | } |
190 | 380k | for (j = 0; j < 125; j++) { |
191 | 377k | ret = init_ralf_vlc(&ctx->sets[i].long_codes[j], |
192 | 377k | long_codes_def[i][j], LONG_CODES_ELEMENTS); |
193 | 377k | if (ret < 0) |
194 | 0 | return ret; |
195 | 377k | } |
196 | 3.01k | } |
197 | | |
198 | 1.00k | return 0; |
199 | 1.00k | } |
200 | | |
201 | | static inline int extend_code(GetBitContext *gb, int val, int range, int bits) |
202 | 301M | { |
203 | 301M | if (val == 0) { |
204 | 254k | val = -range - get_ue_golomb(gb); |
205 | 301M | } else if (val == range * 2) { |
206 | 748k | val = range + get_ue_golomb(gb); |
207 | 300M | } else { |
208 | 300M | val -= range; |
209 | 300M | } |
210 | 301M | if (bits) |
211 | 759k | val = ((unsigned)val << bits) | get_bits(gb, bits); |
212 | 301M | return val; |
213 | 301M | } |
214 | | |
215 | | static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch, |
216 | | int length, int mode, int bits) |
217 | 183k | { |
218 | 183k | int i, t; |
219 | 183k | int code_params; |
220 | 183k | VLCSet *set = ctx->sets + mode; |
221 | 183k | VLC *code_vlc; int range, range2, add_bits; |
222 | 183k | int *dst = ctx->channel_data[ch]; |
223 | | |
224 | 183k | ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2); |
225 | 183k | if (ctx->filter_params > 1) { |
226 | 105k | ctx->filter_bits = (ctx->filter_params - 2) >> 6; |
227 | 105k | ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1; |
228 | 105k | } |
229 | | |
230 | 183k | if (ctx->filter_params == FILTER_RAW) { |
231 | 148k | for (i = 0; i < length; i++) |
232 | 148k | dst[i] = get_bits(gb, bits); |
233 | 738 | ctx->bias[ch] = 0; |
234 | 738 | return 0; |
235 | 738 | } |
236 | | |
237 | 182k | ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2); |
238 | 182k | ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4); |
239 | | |
240 | 182k | if (ctx->filter_params == FILTER_NONE) { |
241 | 76.8k | memset(dst, 0, sizeof(*dst) * length); |
242 | 76.8k | return 0; |
243 | 76.8k | } |
244 | | |
245 | 105k | if (ctx->filter_params > 1) { |
246 | 105k | int cmode = 0, coeff = 0; |
247 | 105k | VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5; |
248 | | |
249 | 105k | add_bits = ctx->filter_bits; |
250 | | |
251 | 685k | for (i = 0; i < ctx->filter_length; i++) { |
252 | 580k | t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2); |
253 | 580k | t = extend_code(gb, t, 21, add_bits); |
254 | 580k | if (!cmode) |
255 | 150k | coeff -= 12U << add_bits; |
256 | 580k | coeff = (unsigned)t - coeff; |
257 | 580k | ctx->filter[i] = coeff; |
258 | | |
259 | 580k | cmode = coeff >> add_bits; |
260 | 580k | if (cmode < 0) { |
261 | 274k | cmode = -1 - av_log2(-cmode); |
262 | 274k | if (cmode < -5) |
263 | 18.1k | cmode = -5; |
264 | 305k | } else if (cmode > 0) { |
265 | 257k | cmode = 1 + av_log2(cmode); |
266 | 257k | if (cmode > 5) |
267 | 24.9k | cmode = 5; |
268 | 257k | } |
269 | 580k | } |
270 | 105k | } |
271 | | |
272 | 105k | code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2); |
273 | 105k | if (code_params >= 15) { |
274 | 105k | add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10); |
275 | 105k | if (add_bits > 9 && (code_params % 5) != 2) |
276 | 737 | add_bits--; |
277 | 105k | range = 10; |
278 | 105k | range2 = 21; |
279 | 105k | code_vlc = set->long_codes + (code_params - 15); |
280 | 105k | } else { |
281 | 326 | add_bits = 0; |
282 | 326 | range = 6; |
283 | 326 | range2 = 13; |
284 | 326 | code_vlc = set->short_codes + code_params; |
285 | 326 | } |
286 | | |
287 | 150M | for (i = 0; i < length; i += 2) { |
288 | 150M | int code1, code2; |
289 | | |
290 | 150M | t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2); |
291 | 150M | code1 = t / range2; |
292 | 150M | code2 = t % range2; |
293 | 150M | dst[i] = extend_code(gb, code1, range, 0) * (1U << add_bits); |
294 | 150M | dst[i + 1] = extend_code(gb, code2, range, 0) * (1U << add_bits); |
295 | 150M | if (add_bits) { |
296 | 149M | dst[i] |= get_bits(gb, add_bits); |
297 | 149M | dst[i + 1] |= get_bits(gb, add_bits); |
298 | 149M | } |
299 | 150M | } |
300 | | |
301 | 105k | return 0; |
302 | 182k | } |
303 | | |
304 | | static void apply_lpc(RALFContext *ctx, int ch, int length, int bits) |
305 | 105k | { |
306 | 105k | int i, j, acc; |
307 | 105k | int *audio = ctx->channel_data[ch]; |
308 | 105k | int bias = 1 << (ctx->filter_bits - 1); |
309 | 105k | int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1; |
310 | | |
311 | 299M | for (i = 1; i < length; i++) { |
312 | 299M | int flen = FFMIN(ctx->filter_length, i); |
313 | | |
314 | 299M | acc = 0; |
315 | 1.41G | for (j = 0; j < flen; j++) |
316 | 1.11G | acc += (unsigned)ctx->filter[j] * audio[i - j - 1]; |
317 | 299M | if (acc < 0) { |
318 | 244M | acc = (acc + bias - 1) >> ctx->filter_bits; |
319 | 244M | acc = FFMAX(acc, min_clip); |
320 | 244M | } else { |
321 | 54.7M | acc = ((unsigned)acc + bias) >> ctx->filter_bits; |
322 | 54.7M | acc = FFMIN(acc, max_clip); |
323 | 54.7M | } |
324 | 299M | audio[i] += acc; |
325 | 299M | } |
326 | 105k | } |
327 | | |
328 | | static int decode_block(AVCodecContext *avctx, GetBitContext *gb, |
329 | | int16_t *dst0, int16_t *dst1) |
330 | 157k | { |
331 | 157k | RALFContext *ctx = avctx->priv_data; |
332 | 157k | int len, ch, ret; |
333 | 157k | int dmode, mode[2], bits[2]; |
334 | 157k | int *ch0, *ch1; |
335 | 157k | int i; |
336 | 157k | unsigned int t, t2; |
337 | | |
338 | 157k | len = 12 - get_unary(gb, 0, 6); |
339 | | |
340 | 157k | if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped |
341 | 157k | len = 1 << len; |
342 | | |
343 | 157k | if (ctx->sample_offset + len > ctx->max_frame_size) { |
344 | 233 | av_log(avctx, AV_LOG_ERROR, |
345 | 233 | "Decoder's stomach is crying, it ate too many samples\n"); |
346 | 233 | return AVERROR_INVALIDDATA; |
347 | 233 | } |
348 | | |
349 | 157k | if (avctx->ch_layout.nb_channels > 1) |
350 | 145k | dmode = get_bits(gb, 2) + 1; |
351 | 12.2k | else |
352 | 12.2k | dmode = 0; |
353 | | |
354 | 157k | mode[0] = (dmode == 4) ? 1 : 0; |
355 | 157k | mode[1] = (dmode >= 2) ? 2 : 0; |
356 | 157k | bits[0] = 16; |
357 | 157k | bits[1] = (mode[1] == 2) ? 17 : 16; |
358 | | |
359 | 212k | for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { |
360 | 183k | if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0) |
361 | 0 | return ret; |
362 | 183k | if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) { |
363 | 105k | ctx->filter_bits += 3; |
364 | 105k | apply_lpc(ctx, ch, len, bits[ch]); |
365 | 105k | } |
366 | 183k | if (get_bits_left(gb) < 0) |
367 | 128k | return AVERROR_INVALIDDATA; |
368 | 183k | } |
369 | 29.1k | ch0 = ctx->channel_data[0]; |
370 | 29.1k | ch1 = ctx->channel_data[1]; |
371 | 29.1k | switch (dmode) { |
372 | 5.00k | case 0: |
373 | 20.1M | for (i = 0; i < len; i++) |
374 | 20.1M | dst0[i] = ch0[i] + ctx->bias[0]; |
375 | 5.00k | break; |
376 | 21.1k | case 1: |
377 | 86.4M | for (i = 0; i < len; i++) { |
378 | 86.4M | dst0[i] = ch0[i] + ctx->bias[0]; |
379 | 86.4M | dst1[i] = ch1[i] + ctx->bias[1]; |
380 | 86.4M | } |
381 | 21.1k | break; |
382 | 1.36k | case 2: |
383 | 351k | for (i = 0; i < len; i++) { |
384 | 350k | ch0[i] += ctx->bias[0]; |
385 | 350k | dst0[i] = ch0[i]; |
386 | 350k | dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]); |
387 | 350k | } |
388 | 1.36k | break; |
389 | 841 | case 3: |
390 | 153k | for (i = 0; i < len; i++) { |
391 | 152k | t = ch0[i] + ctx->bias[0]; |
392 | 152k | t2 = ch1[i] + ctx->bias[1]; |
393 | 152k | dst0[i] = t + t2; |
394 | 152k | dst1[i] = t; |
395 | 152k | } |
396 | 841 | break; |
397 | 768 | case 4: |
398 | 131k | for (i = 0; i < len; i++) { |
399 | 130k | t = ch1[i] + ctx->bias[1]; |
400 | 130k | t2 = ((ch0[i] + ctx->bias[0]) * 2) | (t & 1); |
401 | 130k | dst0[i] = (int)(t2 + t) / 2; |
402 | 130k | dst1[i] = (int)(t2 - t) / 2; |
403 | 130k | } |
404 | 768 | break; |
405 | 29.1k | } |
406 | | |
407 | 29.1k | ctx->sample_offset += len; |
408 | | |
409 | 29.1k | return 0; |
410 | 29.1k | } |
411 | | |
412 | | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
413 | | int *got_frame_ptr, AVPacket *avpkt) |
414 | 196k | { |
415 | 196k | RALFContext *ctx = avctx->priv_data; |
416 | 196k | int16_t *samples0; |
417 | 196k | int16_t *samples1; |
418 | 196k | int ret; |
419 | 196k | GetBitContext gb; |
420 | 196k | int table_size, table_bytes, num_blocks; |
421 | 196k | const uint8_t *src, *block_pointer; |
422 | 196k | int src_size; |
423 | 196k | int bytes_left; |
424 | | |
425 | 196k | if (ctx->has_pkt) { |
426 | 417 | ctx->has_pkt = 0; |
427 | 417 | table_bytes = (AV_RB16(avpkt->data) + 7) >> 3; |
428 | 417 | if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) { |
429 | 183 | av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n"); |
430 | 183 | return AVERROR_INVALIDDATA; |
431 | 183 | } |
432 | 234 | if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) { |
433 | 167 | av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n"); |
434 | 167 | return AVERROR_INVALIDDATA; |
435 | 167 | } |
436 | | |
437 | 67 | src = ctx->pkt; |
438 | 67 | src_size = RALF_MAX_PKT_SIZE + avpkt->size; |
439 | 67 | memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes, |
440 | 67 | avpkt->size - 2 - table_bytes); |
441 | 195k | } else { |
442 | 195k | if (avpkt->size == RALF_MAX_PKT_SIZE) { |
443 | 428 | memcpy(ctx->pkt, avpkt->data, avpkt->size); |
444 | 428 | ctx->has_pkt = 1; |
445 | 428 | *got_frame_ptr = 0; |
446 | | |
447 | 428 | return avpkt->size; |
448 | 428 | } |
449 | 195k | src = avpkt->data; |
450 | 195k | src_size = avpkt->size; |
451 | 195k | } |
452 | | |
453 | 195k | if (src_size < 5) { |
454 | 42.4k | av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n"); |
455 | 42.4k | return AVERROR_INVALIDDATA; |
456 | 42.4k | } |
457 | 152k | table_size = AV_RB16(src); |
458 | 152k | table_bytes = (table_size + 7) >> 3; |
459 | 152k | if (src_size < table_bytes + 3) { |
460 | 10.3k | av_log(avctx, AV_LOG_ERROR, "short packets are short!\n"); |
461 | 10.3k | return AVERROR_INVALIDDATA; |
462 | 10.3k | } |
463 | 142k | init_get_bits(&gb, src + 2, table_size); |
464 | 142k | num_blocks = 0; |
465 | 918k | while (get_bits_left(&gb) > 0) { |
466 | 775k | if (num_blocks >= FF_ARRAY_ELEMS(ctx->block_size)) |
467 | 68 | return AVERROR_INVALIDDATA; |
468 | 775k | ctx->block_size[num_blocks] = get_bits(&gb, 13 + avctx->ch_layout.nb_channels); |
469 | 775k | if (get_bits1(&gb)) { |
470 | 119k | ctx->block_pts[num_blocks] = get_bits(&gb, 9); |
471 | 656k | } else { |
472 | 656k | ctx->block_pts[num_blocks] = 0; |
473 | 656k | } |
474 | 775k | num_blocks++; |
475 | 775k | } |
476 | | |
477 | 142k | frame->nb_samples = ctx->max_frame_size; |
478 | 142k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
479 | 610 | return ret; |
480 | 141k | samples0 = (int16_t *)frame->data[0]; |
481 | 141k | samples1 = (int16_t *)frame->data[1]; |
482 | 141k | block_pointer = src + table_bytes + 2; |
483 | 141k | bytes_left = src_size - table_bytes - 2; |
484 | 141k | ctx->sample_offset = 0; |
485 | 170k | for (int i = 0; i < num_blocks; i++) { |
486 | 170k | if (bytes_left < ctx->block_size[i]) { |
487 | 12.6k | av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n"); |
488 | 12.6k | break; |
489 | 12.6k | } |
490 | 157k | init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8); |
491 | 157k | if (decode_block(avctx, &gb, samples0 + ctx->sample_offset, |
492 | 157k | samples1 + ctx->sample_offset) < 0) { |
493 | 128k | av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n"); |
494 | 128k | break; |
495 | 128k | } |
496 | 29.1k | block_pointer += ctx->block_size[i]; |
497 | 29.1k | bytes_left -= ctx->block_size[i]; |
498 | 29.1k | } |
499 | | |
500 | 141k | frame->nb_samples = ctx->sample_offset; |
501 | 141k | *got_frame_ptr = ctx->sample_offset > 0; |
502 | | |
503 | 141k | return avpkt->size; |
504 | 142k | } |
505 | | |
506 | | static void decode_flush(AVCodecContext *avctx) |
507 | 73.4k | { |
508 | 73.4k | RALFContext *ctx = avctx->priv_data; |
509 | | |
510 | 73.4k | ctx->has_pkt = 0; |
511 | 73.4k | } |
512 | | |
513 | | |
514 | | const FFCodec ff_ralf_decoder = { |
515 | | .p.name = "ralf", |
516 | | CODEC_LONG_NAME("RealAudio Lossless"), |
517 | | .p.type = AVMEDIA_TYPE_AUDIO, |
518 | | .p.id = AV_CODEC_ID_RALF, |
519 | | .priv_data_size = sizeof(RALFContext), |
520 | | .init = decode_init, |
521 | | .close = decode_close, |
522 | | FF_CODEC_DECODE_CB(decode_frame), |
523 | | .flush = decode_flush, |
524 | | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | |
525 | | AV_CODEC_CAP_DR1, |
526 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16P), |
527 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
528 | | }; |