00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include "avcodec.h"
00030 #include "get_bits.h"
00031 #include "golomb.h"
00032 #include "unary.h"
00033 #include "libavutil/audioconvert.h"
00034 #include "ralfdata.h"
00035
00036 #define FILTER_NONE 0
00037 #define FILTER_RAW 642
00038
00039 typedef struct VLCSet {
00040 VLC filter_params;
00041 VLC bias;
00042 VLC coding_mode;
00043 VLC filter_coeffs[10][11];
00044 VLC short_codes[15];
00045 VLC long_codes[125];
00046 } VLCSet;
00047
00048 #define RALF_MAX_PKT_SIZE 8192
00049
00050 typedef struct RALFContext {
00051 AVFrame frame;
00052
00053 int version;
00054 int max_frame_size;
00055 VLCSet sets[3];
00056 int32_t channel_data[2][4096];
00057
00058 int filter_params;
00059 int filter_length;
00060 int filter_bits;
00061 int32_t filter[64];
00062
00063 int bias[2];
00064
00065 int num_blocks;
00066 int sample_offset;
00067 int block_size[1 << 12];
00068 int block_pts[1 << 12];
00069
00070 uint8_t pkt[16384];
00071 int has_pkt;
00072 } RALFContext;
00073
00074 #define MAX_ELEMS 644 // no RALF table uses more than that
00075
00076 static int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
00077 {
00078 uint8_t lens[MAX_ELEMS];
00079 uint16_t codes[MAX_ELEMS];
00080 int counts[17], prefixes[18];
00081 int i, cur_len;
00082 int max_bits = 0;
00083 int nb = 0;
00084
00085 for (i = 0; i <= 16; i++)
00086 counts[i] = 0;
00087 for (i = 0; i < elems; i++) {
00088 cur_len = (nb ? *data & 0xF : *data >> 4) + 1;
00089 counts[cur_len]++;
00090 max_bits = FFMAX(max_bits, cur_len);
00091 lens[i] = cur_len;
00092 data += nb;
00093 nb ^= 1;
00094 }
00095 prefixes[1] = 0;
00096 for (i = 1; i <= 16; i++)
00097 prefixes[i + 1] = (prefixes[i] + counts[i]) << 1;
00098
00099 for (i = 0; i < elems; i++)
00100 codes[i] = prefixes[lens[i]]++;
00101
00102 return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), elems,
00103 lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0);
00104 }
00105
00106 static av_cold int decode_close(AVCodecContext *avctx)
00107 {
00108 RALFContext *ctx = avctx->priv_data;
00109 int i, j, k;
00110
00111 for (i = 0; i < 3; i++) {
00112 ff_free_vlc(&ctx->sets[i].filter_params);
00113 ff_free_vlc(&ctx->sets[i].bias);
00114 ff_free_vlc(&ctx->sets[i].coding_mode);
00115 for (j = 0; j < 10; j++)
00116 for (k = 0; k < 11; k++)
00117 ff_free_vlc(&ctx->sets[i].filter_coeffs[j][k]);
00118 for (j = 0; j < 15; j++)
00119 ff_free_vlc(&ctx->sets[i].short_codes[j]);
00120 for (j = 0; j < 125; j++)
00121 ff_free_vlc(&ctx->sets[i].long_codes[j]);
00122 }
00123
00124 return 0;
00125 }
00126
00127 static av_cold int decode_init(AVCodecContext *avctx)
00128 {
00129 RALFContext *ctx = avctx->priv_data;
00130 int i, j, k;
00131 int ret;
00132
00133 if (avctx->extradata_size < 24 || memcmp(avctx->extradata, "LSD:", 4)) {
00134 av_log(avctx, AV_LOG_ERROR, "Extradata is not groovy, dude\n");
00135 return AVERROR_INVALIDDATA;
00136 }
00137
00138 ctx->version = AV_RB16(avctx->extradata + 4);
00139 if (ctx->version != 0x103) {
00140 av_log_ask_for_sample(avctx, "unknown version %X\n", ctx->version);
00141 return AVERROR_PATCHWELCOME;
00142 }
00143
00144 avctx->channels = AV_RB16(avctx->extradata + 8);
00145 avctx->sample_rate = AV_RB32(avctx->extradata + 12);
00146 if (avctx->channels < 1 || avctx->channels > 2
00147 || avctx->sample_rate < 8000 || avctx->sample_rate > 96000) {
00148 av_log(avctx, AV_LOG_ERROR, "Invalid coding parameters %d Hz %d ch\n",
00149 avctx->sample_rate, avctx->channels);
00150 return AVERROR_INVALIDDATA;
00151 }
00152 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00153 avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
00154 : AV_CH_LAYOUT_MONO;
00155
00156 avcodec_get_frame_defaults(&ctx->frame);
00157 avctx->coded_frame = &ctx->frame;
00158
00159 ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
00160 if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
00161 av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
00162 ctx->max_frame_size);
00163 }
00164 ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
00165
00166 for (i = 0; i < 3; i++) {
00167 ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i],
00168 FILTERPARAM_ELEMENTS);
00169 if (ret < 0) {
00170 decode_close(avctx);
00171 return ret;
00172 }
00173 ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
00174 if (ret < 0) {
00175 decode_close(avctx);
00176 return ret;
00177 }
00178 ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
00179 CODING_MODE_ELEMENTS);
00180 if (ret < 0) {
00181 decode_close(avctx);
00182 return ret;
00183 }
00184 for (j = 0; j < 10; j++) {
00185 for (k = 0; k < 11; k++) {
00186 ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k],
00187 filter_coeffs_def[i][j][k],
00188 FILTER_COEFFS_ELEMENTS);
00189 if (ret < 0) {
00190 decode_close(avctx);
00191 return ret;
00192 }
00193 }
00194 }
00195 for (j = 0; j < 15; j++) {
00196 ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
00197 short_codes_def[i][j], SHORT_CODES_ELEMENTS);
00198 if (ret < 0) {
00199 decode_close(avctx);
00200 return ret;
00201 }
00202 }
00203 for (j = 0; j < 125; j++) {
00204 ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
00205 long_codes_def[i][j], LONG_CODES_ELEMENTS);
00206 if (ret < 0) {
00207 decode_close(avctx);
00208 return ret;
00209 }
00210 }
00211 }
00212
00213 return 0;
00214 }
00215
00216 static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
00217 {
00218 if (val == 0) {
00219 val = -range - get_ue_golomb(gb);
00220 } else if (val == range * 2) {
00221 val = range + get_ue_golomb(gb);
00222 } else {
00223 val -= range;
00224 }
00225 if (bits)
00226 val = (val << bits) | get_bits(gb, bits);
00227 return val;
00228 }
00229
00230 static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
00231 int length, int mode, int bits)
00232 {
00233 int i, t;
00234 int code_params;
00235 VLCSet *set = ctx->sets + mode;
00236 VLC *code_vlc; int range, range2, add_bits;
00237 int *dst = ctx->channel_data[ch];
00238
00239 ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
00240 ctx->filter_bits = (ctx->filter_params - 2) >> 6;
00241 ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
00242
00243 if (ctx->filter_params == FILTER_RAW) {
00244 for (i = 0; i < length; i++)
00245 dst[i] = get_bits(gb, bits);
00246 ctx->bias[ch] = 0;
00247 return 0;
00248 }
00249
00250 ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
00251 ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
00252
00253 if (ctx->filter_params == FILTER_NONE) {
00254 memset(dst, 0, sizeof(*dst) * length);
00255 return 0;
00256 }
00257
00258 if (ctx->filter_params > 1) {
00259 int cmode = 0, coeff = 0;
00260 VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
00261
00262 add_bits = ctx->filter_bits;
00263
00264 for (i = 0; i < ctx->filter_length; i++) {
00265 t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
00266 t = extend_code(gb, t, 21, add_bits);
00267 if (!cmode)
00268 coeff -= 12 << add_bits;
00269 coeff = t - coeff;
00270 ctx->filter[i] = coeff;
00271
00272 cmode = coeff >> add_bits;
00273 if (cmode < 0) {
00274 cmode = -1 - av_log2(-cmode);
00275 if (cmode < -5)
00276 cmode = -5;
00277 } else if (cmode > 0) {
00278 cmode = 1 + av_log2(cmode);
00279 if (cmode > 5)
00280 cmode = 5;
00281 }
00282 }
00283 }
00284
00285 code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
00286 if (code_params >= 15) {
00287 add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
00288 if (add_bits > 9 && (code_params % 5) != 2)
00289 add_bits--;
00290 range = 10;
00291 range2 = 21;
00292 code_vlc = set->long_codes + code_params - 15;
00293 } else {
00294 add_bits = 0;
00295 range = 6;
00296 range2 = 13;
00297 code_vlc = set->short_codes + code_params;
00298 }
00299
00300 for (i = 0; i < length; i += 2) {
00301 int code1, code2;
00302
00303 t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
00304 code1 = t / range2;
00305 code2 = t % range2;
00306 dst[i] = extend_code(gb, code1, range, 0) << add_bits;
00307 dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits;
00308 if (add_bits) {
00309 dst[i] |= get_bits(gb, add_bits);
00310 dst[i + 1] |= get_bits(gb, add_bits);
00311 }
00312 }
00313
00314 return 0;
00315 }
00316
00317 static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
00318 {
00319 int i, j, acc;
00320 int *audio = ctx->channel_data[ch];
00321 int bias = 1 << (ctx->filter_bits - 1);
00322 int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1;
00323
00324 for (i = 1; i < length; i++) {
00325 int flen = FFMIN(ctx->filter_length, i);
00326
00327 acc = 0;
00328 for (j = 0; j < flen; j++)
00329 acc += ctx->filter[j] * audio[i - j - 1];
00330 if (acc < 0) {
00331 acc = (acc + bias - 1) >> ctx->filter_bits;
00332 acc = FFMAX(acc, min_clip);
00333 } else {
00334 acc = (acc + bias) >> ctx->filter_bits;
00335 acc = FFMIN(acc, max_clip);
00336 }
00337 audio[i] += acc;
00338 }
00339 }
00340
00341 static int decode_block(AVCodecContext *avctx, GetBitContext *gb, int16_t *dst)
00342 {
00343 RALFContext *ctx = avctx->priv_data;
00344 int len, ch, ret;
00345 int dmode, mode[2], bits[2];
00346 int *ch0, *ch1;
00347 int i, t, t2;
00348
00349 len = 12 - get_unary(gb, 0, 6);
00350
00351 if (len <= 7) len ^= 1;
00352 len = 1 << len;
00353
00354 if (ctx->sample_offset + len > ctx->max_frame_size) {
00355 av_log(avctx, AV_LOG_ERROR,
00356 "Decoder's stomach is crying, it ate too many samples\n");
00357 return AVERROR_INVALIDDATA;
00358 }
00359
00360 if (avctx->channels > 1)
00361 dmode = get_bits(gb, 2) + 1;
00362 else
00363 dmode = 0;
00364
00365 mode[0] = (dmode == 4) ? 1 : 0;
00366 mode[1] = (dmode >= 2) ? 2 : 0;
00367 bits[0] = 16;
00368 bits[1] = (mode[1] == 2) ? 17 : 16;
00369
00370 for (ch = 0; ch < avctx->channels; ch++) {
00371 if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
00372 return ret;
00373 if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
00374 ctx->filter_bits += 3;
00375 apply_lpc(ctx, ch, len, bits[ch]);
00376 }
00377 if (get_bits_left(gb) < 0)
00378 return AVERROR_INVALIDDATA;
00379 }
00380 ch0 = ctx->channel_data[0];
00381 ch1 = ctx->channel_data[1];
00382 switch (dmode) {
00383 case 0:
00384 for (i = 0; i < len; i++)
00385 *dst++ = ch0[i] + ctx->bias[0];
00386 break;
00387 case 1:
00388 for (i = 0; i < len; i++) {
00389 *dst++ = ch0[i] + ctx->bias[0];
00390 *dst++ = ch1[i] + ctx->bias[1];
00391 }
00392 break;
00393 case 2:
00394 for (i = 0; i < len; i++) {
00395 ch0[i] += ctx->bias[0];
00396 *dst++ = ch0[i];
00397 *dst++ = ch0[i] - (ch1[i] + ctx->bias[1]);
00398 }
00399 break;
00400 case 3:
00401 for (i = 0; i < len; i++) {
00402 t = ch0[i] + ctx->bias[0];
00403 t2 = ch1[i] + ctx->bias[1];
00404 *dst++ = t + t2;
00405 *dst++ = t;
00406 }
00407 break;
00408 case 4:
00409 for (i = 0; i < len; i++) {
00410 t = ch1[i] + ctx->bias[1];
00411 t2 = ((ch0[i] + ctx->bias[0]) << 1) | (t & 1);
00412 *dst++ = (t2 + t) / 2;
00413 *dst++ = (t2 - t) / 2;
00414 }
00415 break;
00416 }
00417
00418 ctx->sample_offset += len;
00419
00420 return 0;
00421 }
00422
00423 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
00424 AVPacket *avpkt)
00425 {
00426 RALFContext *ctx = avctx->priv_data;
00427 int16_t *samples;
00428 int ret;
00429 GetBitContext gb;
00430 int table_size, table_bytes, i;
00431 const uint8_t *src, *block_pointer;
00432 int src_size;
00433 int bytes_left;
00434
00435 if (ctx->has_pkt) {
00436 ctx->has_pkt = 0;
00437 table_bytes = (AV_RB16(avpkt->data) + 7) >> 3;
00438 if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) {
00439 av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n");
00440 return AVERROR_INVALIDDATA;
00441 }
00442 if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) {
00443 av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n");
00444 return AVERROR_INVALIDDATA;
00445 }
00446
00447 src = ctx->pkt;
00448 src_size = RALF_MAX_PKT_SIZE + avpkt->size;
00449 memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes,
00450 avpkt->size - 2 - table_bytes);
00451 } else {
00452 if (avpkt->size == RALF_MAX_PKT_SIZE) {
00453 memcpy(ctx->pkt, avpkt->data, avpkt->size);
00454 ctx->has_pkt = 1;
00455 *got_frame_ptr = 0;
00456
00457 return avpkt->size;
00458 }
00459 src = avpkt->data;
00460 src_size = avpkt->size;
00461 }
00462
00463 ctx->frame.nb_samples = ctx->max_frame_size;
00464 if ((ret = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
00465 av_log(avctx, AV_LOG_ERROR, "Me fail get_buffer()? That's unpossible!\n");
00466 return ret;
00467 }
00468 samples = (int16_t*)ctx->frame.data[0];
00469
00470 if (src_size < 5) {
00471 av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
00472 return AVERROR_INVALIDDATA;
00473 }
00474 table_size = AV_RB16(src);
00475 table_bytes = (table_size + 7) >> 3;
00476 if (src_size < table_bytes + 3) {
00477 av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
00478 return AVERROR_INVALIDDATA;
00479 }
00480 init_get_bits(&gb, src + 2, table_size);
00481 ctx->num_blocks = 0;
00482 while (get_bits_left(&gb) > 0) {
00483 ctx->block_size[ctx->num_blocks] = get_bits(&gb, 15);
00484 if (get_bits1(&gb)) {
00485 ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
00486 } else {
00487 ctx->block_pts[ctx->num_blocks] = 0;
00488 }
00489 ctx->num_blocks++;
00490 }
00491
00492 block_pointer = src + table_bytes + 2;
00493 bytes_left = src_size - table_bytes - 2;
00494 ctx->sample_offset = 0;
00495 for (i = 0; i < ctx->num_blocks; i++) {
00496 if (bytes_left < ctx->block_size[i]) {
00497 av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
00498 break;
00499 }
00500 init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
00501 if (decode_block(avctx, &gb, samples + ctx->sample_offset
00502 * avctx->channels) < 0) {
00503 av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
00504 break;
00505 }
00506 block_pointer += ctx->block_size[i];
00507 bytes_left -= ctx->block_size[i];
00508 }
00509
00510 ctx->frame.nb_samples = ctx->sample_offset;
00511 *got_frame_ptr = ctx->sample_offset > 0;
00512 *(AVFrame*)data = ctx->frame;
00513
00514 return avpkt->size;
00515 }
00516
00517 static void decode_flush(AVCodecContext *avctx)
00518 {
00519 RALFContext *ctx = avctx->priv_data;
00520
00521 ctx->has_pkt = 0;
00522 }
00523
00524
00525 AVCodec ff_ralf_decoder = {
00526 .name = "ralf",
00527 .type = AVMEDIA_TYPE_AUDIO,
00528 .id = AV_CODEC_ID_RALF,
00529 .priv_data_size = sizeof(RALFContext),
00530 .init = decode_init,
00531 .close = decode_close,
00532 .decode = decode_frame,
00533 .flush = decode_flush,
00534 .capabilities = CODEC_CAP_DR1,
00535 .long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
00536 };