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 {
57
int32_t channel_data[2][4096];
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
64
int bias[2]; ///< a constant value added to channel data after filtering
66
int num_blocks; ///< number of blocks inside the frame
68
int block_size[1 << 12]; ///< size of the blocks
69
int block_pts[1 << 12]; ///< block start time (in milliseconds)
75
#define MAX_ELEMS 644 // no RALF table uses more than that
77
static int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
79
uint8_t lens[MAX_ELEMS];
80
uint16_t codes[MAX_ELEMS];
81
int counts[17], prefixes[18];
86
for (i = 0; i <= 16; i++)
88
for (i = 0; i < elems; i++) {
89
cur_len = (nb ? *data & 0xF : *data >> 4) + 1;
91
max_bits = FFMAX(max_bits, cur_len);
97
for (i = 1; i <= 16; i++)
98
prefixes[i + 1] = (prefixes[i] + counts[i]) << 1;
100
for (i = 0; i < elems; i++)
101
codes[i] = prefixes[lens[i]]++;
103
return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), elems,
104
lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0);
107
static av_cold int decode_close(AVCodecContext *avctx)
109
RALFContext *ctx = avctx->priv_data;
112
for (i = 0; i < 3; i++) {
113
ff_free_vlc(&ctx->sets[i].filter_params);
114
ff_free_vlc(&ctx->sets[i].bias);
115
ff_free_vlc(&ctx->sets[i].coding_mode);
116
for (j = 0; j < 10; j++)
117
for (k = 0; k < 11; k++)
118
ff_free_vlc(&ctx->sets[i].filter_coeffs[j][k]);
119
for (j = 0; j < 15; j++)
120
ff_free_vlc(&ctx->sets[i].short_codes[j]);
121
for (j = 0; j < 125; j++)
122
ff_free_vlc(&ctx->sets[i].long_codes[j]);
128
static av_cold int decode_init(AVCodecContext *avctx)
130
RALFContext *ctx = avctx->priv_data;
134
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;
139
ctx->version = AV_RB16(avctx->extradata + 4);
140
if (ctx->version != 0x103) {
141
av_log_ask_for_sample(avctx, "unknown version %X\n", ctx->version);
142
return AVERROR_PATCHWELCOME;
145
avctx->channels = AV_RB16(avctx->extradata + 8);
146
avctx->sample_rate = AV_RB32(avctx->extradata + 12);
147
if (avctx->channels < 1 || avctx->channels > 2
148
|| 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, avctx->channels);
151
return AVERROR_INVALIDDATA;
153
avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
154
avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
157
avcodec_get_frame_defaults(&ctx->frame);
158
avctx->coded_frame = &ctx->frame;
160
ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
161
if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
162
av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
163
ctx->max_frame_size);
165
ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
167
for (i = 0; i < 3; i++) {
168
ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i],
169
FILTERPARAM_ELEMENTS);
174
ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
179
ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
180
CODING_MODE_ELEMENTS);
185
for (j = 0; j < 10; j++) {
186
for (k = 0; k < 11; k++) {
187
ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k],
188
filter_coeffs_def[i][j][k],
189
FILTER_COEFFS_ELEMENTS);
196
for (j = 0; j < 15; j++) {
197
ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
198
short_codes_def[i][j], SHORT_CODES_ELEMENTS);
204
for (j = 0; j < 125; j++) {
205
ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
206
long_codes_def[i][j], LONG_CODES_ELEMENTS);
217
static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
220
val = -range - get_ue_golomb(gb);
221
} else if (val == range * 2) {
222
val = range + get_ue_golomb(gb);
227
val = (val << bits) | get_bits(gb, bits);
231
static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
232
int length, int mode, int bits)
236
VLCSet *set = ctx->sets + mode;
237
VLC *code_vlc; int range, range2, add_bits;
238
int *dst = ctx->channel_data[ch];
240
ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
241
ctx->filter_bits = (ctx->filter_params - 2) >> 6;
242
ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
244
if (ctx->filter_params == FILTER_RAW) {
245
for (i = 0; i < length; i++)
246
dst[i] = get_bits(gb, bits);
251
ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
252
ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
254
if (ctx->filter_params == FILTER_NONE) {
255
memset(dst, 0, sizeof(*dst) * length);
259
if (ctx->filter_params > 1) {
260
int cmode = 0, coeff = 0;
261
VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
263
add_bits = ctx->filter_bits;
265
for (i = 0; i < ctx->filter_length; i++) {
266
t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
267
t = extend_code(gb, t, 21, add_bits);
269
coeff -= 12 << add_bits;
271
ctx->filter[i] = coeff;
273
cmode = coeff >> add_bits;
275
cmode = -1 - av_log2(-cmode);
278
} else if (cmode > 0) {
279
cmode = 1 + av_log2(cmode);
286
code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
287
if (code_params >= 15) {
288
add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
289
if (add_bits > 9 && (code_params % 5) != 2)
293
code_vlc = set->long_codes + code_params - 15;
298
code_vlc = set->short_codes + code_params;
301
for (i = 0; i < length; i += 2) {
304
t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
307
dst[i] = extend_code(gb, code1, range, 0) << add_bits;
308
dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits;
310
dst[i] |= get_bits(gb, add_bits);
311
dst[i + 1] |= get_bits(gb, add_bits);
318
static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
321
int *audio = ctx->channel_data[ch];
322
int bias = 1 << (ctx->filter_bits - 1);
323
int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1;
325
for (i = 1; i < length; i++) {
326
int flen = FFMIN(ctx->filter_length, i);
329
for (j = 0; j < flen; j++)
330
acc += ctx->filter[j] * audio[i - j - 1];
332
acc = (acc + bias - 1) >> ctx->filter_bits;
333
acc = FFMAX(acc, min_clip);
335
acc = (acc + bias) >> ctx->filter_bits;
336
acc = FFMIN(acc, max_clip);
342
static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
343
int16_t *dst0, int16_t *dst1)
345
RALFContext *ctx = avctx->priv_data;
347
int dmode, mode[2], bits[2];
351
len = 12 - get_unary(gb, 0, 6);
353
if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
356
if (ctx->sample_offset + len > ctx->max_frame_size) {
357
av_log(avctx, AV_LOG_ERROR,
358
"Decoder's stomach is crying, it ate too many samples\n");
359
return AVERROR_INVALIDDATA;
362
if (avctx->channels > 1)
363
dmode = get_bits(gb, 2) + 1;
367
mode[0] = (dmode == 4) ? 1 : 0;
368
mode[1] = (dmode >= 2) ? 2 : 0;
370
bits[1] = (mode[1] == 2) ? 17 : 16;
372
for (ch = 0; ch < avctx->channels; ch++) {
373
if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
375
if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
376
ctx->filter_bits += 3;
377
apply_lpc(ctx, ch, len, bits[ch]);
379
if (get_bits_left(gb) < 0)
380
return AVERROR_INVALIDDATA;
382
ch0 = ctx->channel_data[0];
383
ch1 = ctx->channel_data[1];
386
for (i = 0; i < len; i++)
387
dst0[i] = ch0[i] + ctx->bias[0];
390
for (i = 0; i < len; i++) {
391
dst0[i] = ch0[i] + ctx->bias[0];
392
dst1[i] = ch1[i] + ctx->bias[1];
396
for (i = 0; i < len; i++) {
397
ch0[i] += ctx->bias[0];
399
dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
403
for (i = 0; i < len; i++) {
404
t = ch0[i] + ctx->bias[0];
405
t2 = ch1[i] + ctx->bias[1];
411
for (i = 0; i < len; i++) {
412
t = ch1[i] + ctx->bias[1];
413
t2 = ((ch0[i] + ctx->bias[0]) << 1) | (t & 1);
414
dst0[i] = (t2 + t) / 2;
415
dst1[i] = (t2 - t) / 2;
420
ctx->sample_offset += len;
425
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
428
RALFContext *ctx = avctx->priv_data;
433
int table_size, table_bytes, i;
434
const uint8_t *src, *block_pointer;
440
table_bytes = (AV_RB16(avpkt->data) + 7) >> 3;
441
if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) {
442
av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n");
443
return AVERROR_INVALIDDATA;
445
if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) {
446
av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n");
447
return AVERROR_INVALIDDATA;
451
src_size = RALF_MAX_PKT_SIZE + avpkt->size;
452
memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes,
453
avpkt->size - 2 - table_bytes);
455
if (avpkt->size == RALF_MAX_PKT_SIZE) {
456
memcpy(ctx->pkt, avpkt->data, avpkt->size);
463
src_size = avpkt->size;
466
ctx->frame.nb_samples = ctx->max_frame_size;
467
if ((ret = ff_get_buffer(avctx, &ctx->frame)) < 0) {
468
av_log(avctx, AV_LOG_ERROR, "Me fail get_buffer()? That's unpossible!\n");
471
samples0 = (int16_t *)ctx->frame.data[0];
472
samples1 = (int16_t *)ctx->frame.data[1];
475
av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
476
return AVERROR_INVALIDDATA;
478
table_size = AV_RB16(src);
479
table_bytes = (table_size + 7) >> 3;
480
if (src_size < table_bytes + 3) {
481
av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
482
return AVERROR_INVALIDDATA;
484
init_get_bits(&gb, src + 2, table_size);
486
while (get_bits_left(&gb) > 0) {
487
ctx->block_size[ctx->num_blocks] = get_bits(&gb, 15);
488
if (get_bits1(&gb)) {
489
ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
491
ctx->block_pts[ctx->num_blocks] = 0;
496
block_pointer = src + table_bytes + 2;
497
bytes_left = src_size - table_bytes - 2;
498
ctx->sample_offset = 0;
499
for (i = 0; i < ctx->num_blocks; i++) {
500
if (bytes_left < ctx->block_size[i]) {
501
av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
504
init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
505
if (decode_block(avctx, &gb, samples0 + ctx->sample_offset,
506
samples1 + ctx->sample_offset) < 0) {
507
av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
510
block_pointer += ctx->block_size[i];
511
bytes_left -= ctx->block_size[i];
514
ctx->frame.nb_samples = ctx->sample_offset;
515
*got_frame_ptr = ctx->sample_offset > 0;
516
*(AVFrame*)data = ctx->frame;
521
static void decode_flush(AVCodecContext *avctx)
523
RALFContext *ctx = avctx->priv_data;
529
AVCodec ff_ralf_decoder = {
531
.type = AVMEDIA_TYPE_AUDIO,
532
.id = AV_CODEC_ID_RALF,
533
.priv_data_size = sizeof(RALFContext),
535
.close = decode_close,
536
.decode = decode_frame,
537
.flush = decode_flush,
538
.capabilities = CODEC_CAP_DR1,
539
.long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
540
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
541
AV_SAMPLE_FMT_NONE },