2 * RealAudio Lossless decoder
4 * Copyright (c) 2012 Konstantin Shishkov
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * This is a decoder for Real Audio Lossless format.
26 * Dedicated to the mastermind behind it, Ralph Wiggum.
29 #include "libavutil/channel_layout.h"
38 #define FILTER_RAW 642
40 typedef struct VLCSet
{
44 VLC filter_coeffs
[10][11];
49 #define RALF_MAX_PKT_SIZE 8192
51 typedef struct RALFContext
{
55 int32_t channel_data
[2][4096];
57 int filter_params
; ///< combined filter parameters for the current channel data
58 int filter_length
; ///< length of the filter for the current channel data
59 int filter_bits
; ///< filter precision for the current channel data
62 int bias
[2]; ///< a constant value added to channel data after filtering
64 int num_blocks
; ///< number of blocks inside the frame
66 int block_size
[1 << 12]; ///< size of the blocks
67 int block_pts
[1 << 12]; ///< block start time (in milliseconds)
73 #define MAX_ELEMS 644 // no RALF table uses more than that
75 static int init_ralf_vlc(VLC
*vlc
, const uint8_t *data
, int elems
)
77 uint8_t lens
[MAX_ELEMS
];
78 uint16_t codes
[MAX_ELEMS
];
79 int counts
[17], prefixes
[18];
84 for (i
= 0; i
<= 16; i
++)
86 for (i
= 0; i
< elems
; i
++) {
87 cur_len
= (nb
? *data
& 0xF : *data
>> 4) + 1;
89 max_bits
= FFMAX(max_bits
, cur_len
);
95 for (i
= 1; i
<= 16; i
++)
96 prefixes
[i
+ 1] = (prefixes
[i
] + counts
[i
]) << 1;
98 for (i
= 0; i
< elems
; i
++)
99 codes
[i
] = prefixes
[lens
[i
]]++;
101 return ff_init_vlc_sparse(vlc
, FFMIN(max_bits
, 9), elems
,
102 lens
, 1, 1, codes
, 2, 2, NULL
, 0, 0, 0);
105 static av_cold
int decode_close(AVCodecContext
*avctx
)
107 RALFContext
*ctx
= avctx
->priv_data
;
110 for (i
= 0; i
< 3; i
++) {
111 ff_free_vlc(&ctx
->sets
[i
].filter_params
);
112 ff_free_vlc(&ctx
->sets
[i
].bias
);
113 ff_free_vlc(&ctx
->sets
[i
].coding_mode
);
114 for (j
= 0; j
< 10; j
++)
115 for (k
= 0; k
< 11; k
++)
116 ff_free_vlc(&ctx
->sets
[i
].filter_coeffs
[j
][k
]);
117 for (j
= 0; j
< 15; j
++)
118 ff_free_vlc(&ctx
->sets
[i
].short_codes
[j
]);
119 for (j
= 0; j
< 125; j
++)
120 ff_free_vlc(&ctx
->sets
[i
].long_codes
[j
]);
126 static av_cold
int decode_init(AVCodecContext
*avctx
)
128 RALFContext
*ctx
= avctx
->priv_data
;
132 if (avctx
->extradata_size
< 24 || memcmp(avctx
->extradata
, "LSD:", 4)) {
133 av_log(avctx
, AV_LOG_ERROR
, "Extradata is not groovy, dude\n");
134 return AVERROR_INVALIDDATA
;
137 ctx
->version
= AV_RB16(avctx
->extradata
+ 4);
138 if (ctx
->version
!= 0x103) {
139 av_log_ask_for_sample(avctx
, "unknown version %X\n", ctx
->version
);
140 return AVERROR_PATCHWELCOME
;
143 avctx
->channels
= AV_RB16(avctx
->extradata
+ 8);
144 avctx
->sample_rate
= AV_RB32(avctx
->extradata
+ 12);
145 if (avctx
->channels
< 1 || avctx
->channels
> 2
146 || avctx
->sample_rate
< 8000 || avctx
->sample_rate
> 96000) {
147 av_log(avctx
, AV_LOG_ERROR
, "Invalid coding parameters %d Hz %d ch\n",
148 avctx
->sample_rate
, avctx
->channels
);
149 return AVERROR_INVALIDDATA
;
151 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16P
;
152 avctx
->channel_layout
= (avctx
->channels
== 2) ? AV_CH_LAYOUT_STEREO
155 ctx
->max_frame_size
= AV_RB32(avctx
->extradata
+ 16);
156 if (ctx
->max_frame_size
> (1 << 20) || !ctx
->max_frame_size
) {
157 av_log(avctx
, AV_LOG_ERROR
, "invalid frame size %d\n",
158 ctx
->max_frame_size
);
160 ctx
->max_frame_size
= FFMAX(ctx
->max_frame_size
, avctx
->sample_rate
);
162 for (i
= 0; i
< 3; i
++) {
163 ret
= init_ralf_vlc(&ctx
->sets
[i
].filter_params
, filter_param_def
[i
],
164 FILTERPARAM_ELEMENTS
);
169 ret
= init_ralf_vlc(&ctx
->sets
[i
].bias
, bias_def
[i
], BIAS_ELEMENTS
);
174 ret
= init_ralf_vlc(&ctx
->sets
[i
].coding_mode
, coding_mode_def
[i
],
175 CODING_MODE_ELEMENTS
);
180 for (j
= 0; j
< 10; j
++) {
181 for (k
= 0; k
< 11; k
++) {
182 ret
= init_ralf_vlc(&ctx
->sets
[i
].filter_coeffs
[j
][k
],
183 filter_coeffs_def
[i
][j
][k
],
184 FILTER_COEFFS_ELEMENTS
);
191 for (j
= 0; j
< 15; j
++) {
192 ret
= init_ralf_vlc(&ctx
->sets
[i
].short_codes
[j
],
193 short_codes_def
[i
][j
], SHORT_CODES_ELEMENTS
);
199 for (j
= 0; j
< 125; j
++) {
200 ret
= init_ralf_vlc(&ctx
->sets
[i
].long_codes
[j
],
201 long_codes_def
[i
][j
], LONG_CODES_ELEMENTS
);
212 static inline int extend_code(GetBitContext
*gb
, int val
, int range
, int bits
)
215 val
= -range
- get_ue_golomb(gb
);
216 } else if (val
== range
* 2) {
217 val
= range
+ get_ue_golomb(gb
);
222 val
= (val
<< bits
) | get_bits(gb
, bits
);
226 static int decode_channel(RALFContext
*ctx
, GetBitContext
*gb
, int ch
,
227 int length
, int mode
, int bits
)
231 VLCSet
*set
= ctx
->sets
+ mode
;
232 VLC
*code_vlc
; int range
, range2
, add_bits
;
233 int *dst
= ctx
->channel_data
[ch
];
235 ctx
->filter_params
= get_vlc2(gb
, set
->filter_params
.table
, 9, 2);
236 ctx
->filter_bits
= (ctx
->filter_params
- 2) >> 6;
237 ctx
->filter_length
= ctx
->filter_params
- (ctx
->filter_bits
<< 6) - 1;
239 if (ctx
->filter_params
== FILTER_RAW
) {
240 for (i
= 0; i
< length
; i
++)
241 dst
[i
] = get_bits(gb
, bits
);
246 ctx
->bias
[ch
] = get_vlc2(gb
, set
->bias
.table
, 9, 2);
247 ctx
->bias
[ch
] = extend_code(gb
, ctx
->bias
[ch
], 127, 4);
249 if (ctx
->filter_params
== FILTER_NONE
) {
250 memset(dst
, 0, sizeof(*dst
) * length
);
254 if (ctx
->filter_params
> 1) {
255 int cmode
= 0, coeff
= 0;
256 VLC
*vlc
= set
->filter_coeffs
[ctx
->filter_bits
] + 5;
258 add_bits
= ctx
->filter_bits
;
260 for (i
= 0; i
< ctx
->filter_length
; i
++) {
261 t
= get_vlc2(gb
, vlc
[cmode
].table
, vlc
[cmode
].bits
, 2);
262 t
= extend_code(gb
, t
, 21, add_bits
);
264 coeff
-= 12 << add_bits
;
266 ctx
->filter
[i
] = coeff
;
268 cmode
= coeff
>> add_bits
;
270 cmode
= -1 - av_log2(-cmode
);
273 } else if (cmode
> 0) {
274 cmode
= 1 + av_log2(cmode
);
281 code_params
= get_vlc2(gb
, set
->coding_mode
.table
, set
->coding_mode
.bits
, 2);
282 if (code_params
>= 15) {
283 add_bits
= av_clip((code_params
/ 5 - 3) / 2, 0, 10);
284 if (add_bits
> 9 && (code_params
% 5) != 2)
288 code_vlc
= set
->long_codes
+ code_params
- 15;
293 code_vlc
= set
->short_codes
+ code_params
;
296 for (i
= 0; i
< length
; i
+= 2) {
299 t
= get_vlc2(gb
, code_vlc
->table
, code_vlc
->bits
, 2);
302 dst
[i
] = extend_code(gb
, code1
, range
, 0) << add_bits
;
303 dst
[i
+ 1] = extend_code(gb
, code2
, range
, 0) << add_bits
;
305 dst
[i
] |= get_bits(gb
, add_bits
);
306 dst
[i
+ 1] |= get_bits(gb
, add_bits
);
313 static void apply_lpc(RALFContext
*ctx
, int ch
, int length
, int bits
)
316 int *audio
= ctx
->channel_data
[ch
];
317 int bias
= 1 << (ctx
->filter_bits
- 1);
318 int max_clip
= (1 << bits
) - 1, min_clip
= -max_clip
- 1;
320 for (i
= 1; i
< length
; i
++) {
321 int flen
= FFMIN(ctx
->filter_length
, i
);
324 for (j
= 0; j
< flen
; j
++)
325 acc
+= ctx
->filter
[j
] * audio
[i
- j
- 1];
327 acc
= (acc
+ bias
- 1) >> ctx
->filter_bits
;
328 acc
= FFMAX(acc
, min_clip
);
330 acc
= (acc
+ bias
) >> ctx
->filter_bits
;
331 acc
= FFMIN(acc
, max_clip
);
337 static int decode_block(AVCodecContext
*avctx
, GetBitContext
*gb
,
338 int16_t *dst0
, int16_t *dst1
)
340 RALFContext
*ctx
= avctx
->priv_data
;
342 int dmode
, mode
[2], bits
[2];
346 len
= 12 - get_unary(gb
, 0, 6);
348 if (len
<= 7) len
^= 1; // codes for length = 6 and 7 are swapped
351 if (ctx
->sample_offset
+ len
> ctx
->max_frame_size
) {
352 av_log(avctx
, AV_LOG_ERROR
,
353 "Decoder's stomach is crying, it ate too many samples\n");
354 return AVERROR_INVALIDDATA
;
357 if (avctx
->channels
> 1)
358 dmode
= get_bits(gb
, 2) + 1;
362 mode
[0] = (dmode
== 4) ? 1 : 0;
363 mode
[1] = (dmode
>= 2) ? 2 : 0;
365 bits
[1] = (mode
[1] == 2) ? 17 : 16;
367 for (ch
= 0; ch
< avctx
->channels
; ch
++) {
368 if ((ret
= decode_channel(ctx
, gb
, ch
, len
, mode
[ch
], bits
[ch
])) < 0)
370 if (ctx
->filter_params
> 1 && ctx
->filter_params
!= FILTER_RAW
) {
371 ctx
->filter_bits
+= 3;
372 apply_lpc(ctx
, ch
, len
, bits
[ch
]);
374 if (get_bits_left(gb
) < 0)
375 return AVERROR_INVALIDDATA
;
377 ch0
= ctx
->channel_data
[0];
378 ch1
= ctx
->channel_data
[1];
381 for (i
= 0; i
< len
; i
++)
382 dst0
[i
] = ch0
[i
] + ctx
->bias
[0];
385 for (i
= 0; i
< len
; i
++) {
386 dst0
[i
] = ch0
[i
] + ctx
->bias
[0];
387 dst1
[i
] = ch1
[i
] + ctx
->bias
[1];
391 for (i
= 0; i
< len
; i
++) {
392 ch0
[i
] += ctx
->bias
[0];
394 dst1
[i
] = ch0
[i
] - (ch1
[i
] + ctx
->bias
[1]);
398 for (i
= 0; i
< len
; i
++) {
399 t
= ch0
[i
] + ctx
->bias
[0];
400 t2
= ch1
[i
] + ctx
->bias
[1];
406 for (i
= 0; i
< len
; i
++) {
407 t
= ch1
[i
] + ctx
->bias
[1];
408 t2
= ((ch0
[i
] + ctx
->bias
[0]) << 1) | (t
& 1);
409 dst0
[i
] = (t2
+ t
) / 2;
410 dst1
[i
] = (t2
- t
) / 2;
415 ctx
->sample_offset
+= len
;
420 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *got_frame_ptr
,
423 RALFContext
*ctx
= avctx
->priv_data
;
424 AVFrame
*frame
= data
;
429 int table_size
, table_bytes
, i
;
430 const uint8_t *src
, *block_pointer
;
436 table_bytes
= (AV_RB16(avpkt
->data
) + 7) >> 3;
437 if (table_bytes
+ 3 > avpkt
->size
|| avpkt
->size
> RALF_MAX_PKT_SIZE
) {
438 av_log(avctx
, AV_LOG_ERROR
, "Wrong packet's breath smells of wrong data!\n");
439 return AVERROR_INVALIDDATA
;
441 if (memcmp(ctx
->pkt
, avpkt
->data
, 2 + table_bytes
)) {
442 av_log(avctx
, AV_LOG_ERROR
, "Wrong packet tails are wrong!\n");
443 return AVERROR_INVALIDDATA
;
447 src_size
= RALF_MAX_PKT_SIZE
+ avpkt
->size
;
448 memcpy(ctx
->pkt
+ RALF_MAX_PKT_SIZE
, avpkt
->data
+ 2 + table_bytes
,
449 avpkt
->size
- 2 - table_bytes
);
451 if (avpkt
->size
== RALF_MAX_PKT_SIZE
) {
452 memcpy(ctx
->pkt
, avpkt
->data
, avpkt
->size
);
459 src_size
= avpkt
->size
;
462 frame
->nb_samples
= ctx
->max_frame_size
;
463 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0) {
464 av_log(avctx
, AV_LOG_ERROR
, "Me fail get_buffer()? That's unpossible!\n");
467 samples0
= (int16_t *)frame
->data
[0];
468 samples1
= (int16_t *)frame
->data
[1];
471 av_log(avctx
, AV_LOG_ERROR
, "too short packets are too short!\n");
472 return AVERROR_INVALIDDATA
;
474 table_size
= AV_RB16(src
);
475 table_bytes
= (table_size
+ 7) >> 3;
476 if (src_size
< table_bytes
+ 3) {
477 av_log(avctx
, AV_LOG_ERROR
, "short packets are short!\n");
478 return AVERROR_INVALIDDATA
;
480 init_get_bits(&gb
, src
+ 2, table_size
);
482 while (get_bits_left(&gb
) > 0) {
483 ctx
->block_size
[ctx
->num_blocks
] = get_bits(&gb
, 15);
484 if (get_bits1(&gb
)) {
485 ctx
->block_pts
[ctx
->num_blocks
] = get_bits(&gb
, 9);
487 ctx
->block_pts
[ctx
->num_blocks
] = 0;
492 block_pointer
= src
+ table_bytes
+ 2;
493 bytes_left
= src_size
- table_bytes
- 2;
494 ctx
->sample_offset
= 0;
495 for (i
= 0; i
< ctx
->num_blocks
; i
++) {
496 if (bytes_left
< ctx
->block_size
[i
]) {
497 av_log(avctx
, AV_LOG_ERROR
, "I'm pedaling backwards\n");
500 init_get_bits(&gb
, block_pointer
, ctx
->block_size
[i
] * 8);
501 if (decode_block(avctx
, &gb
, samples0
+ ctx
->sample_offset
,
502 samples1
+ ctx
->sample_offset
) < 0) {
503 av_log(avctx
, AV_LOG_ERROR
, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
506 block_pointer
+= ctx
->block_size
[i
];
507 bytes_left
-= ctx
->block_size
[i
];
510 frame
->nb_samples
= ctx
->sample_offset
;
511 *got_frame_ptr
= ctx
->sample_offset
> 0;
516 static void decode_flush(AVCodecContext
*avctx
)
518 RALFContext
*ctx
= avctx
->priv_data
;
524 AVCodec ff_ralf_decoder
= {
526 .type
= AVMEDIA_TYPE_AUDIO
,
527 .id
= AV_CODEC_ID_RALF
,
528 .priv_data_size
= sizeof(RALFContext
),
530 .close
= decode_close
,
531 .decode
= decode_frame
,
532 .flush
= decode_flush
,
533 .capabilities
= CODEC_CAP_DR1
,
534 .long_name
= NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
535 .sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_S16P
,
536 AV_SAMPLE_FMT_NONE
},