FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ralf.c
Go to the documentation of this file.
1 /*
2  * RealAudio Lossless decoder
3  *
4  * Copyright (c) 2012 Konstantin Shishkov
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
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 
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "golomb.h"
33 #include "internal.h"
34 #include "unary.h"
35 #include "ralfdata.h"
36 
37 #define FILTER_NONE 0
38 #define FILTER_RAW 642
39 
40 typedef struct VLCSet {
44  VLC filter_coeffs[10][11];
47 } VLCSet;
48 
49 #define RALF_MAX_PKT_SIZE 8192
50 
51 typedef struct RALFContext {
53 
54  int version;
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
63 
64  int bias[2]; ///< a constant value added to channel data after filtering
65 
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)
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 static 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  int max_bits = 0;
84  int nb = 0;
85 
86  for (i = 0; i <= 16; i++)
87  counts[i] = 0;
88  for (i = 0; i < elems; i++) {
89  cur_len = (nb ? *data & 0xF : *data >> 4) + 1;
90  counts[cur_len]++;
91  max_bits = FFMAX(max_bits, cur_len);
92  lens[i] = cur_len;
93  data += nb;
94  nb ^= 1;
95  }
96  prefixes[1] = 0;
97  for (i = 1; i <= 16; i++)
98  prefixes[i + 1] = (prefixes[i] + counts[i]) << 1;
99 
100  for (i = 0; i < elems; i++)
101  codes[i] = prefixes[lens[i]]++;
102 
103  return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), elems,
104  lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0);
105 }
106 
108 {
109  RALFContext *ctx = avctx->priv_data;
110  int i, j, k;
111 
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]);
123  }
124 
125  return 0;
126 }
127 
129 {
130  RALFContext *ctx = avctx->priv_data;
131  int i, j, k;
132  int ret;
133 
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;
137  }
138 
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;
143  }
144 
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;
152  }
154  avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
156 
158  avctx->coded_frame = &ctx->frame;
159 
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);
164  }
165  ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
166 
167  for (i = 0; i < 3; i++) {
170  if (ret < 0) {
171  decode_close(avctx);
172  return ret;
173  }
174  ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
175  if (ret < 0) {
176  decode_close(avctx);
177  return ret;
178  }
179  ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
181  if (ret < 0) {
182  decode_close(avctx);
183  return ret;
184  }
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],
190  if (ret < 0) {
191  decode_close(avctx);
192  return ret;
193  }
194  }
195  }
196  for (j = 0; j < 15; j++) {
197  ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
199  if (ret < 0) {
200  decode_close(avctx);
201  return ret;
202  }
203  }
204  for (j = 0; j < 125; j++) {
205  ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
207  if (ret < 0) {
208  decode_close(avctx);
209  return ret;
210  }
211  }
212  }
213 
214  return 0;
215 }
216 
217 static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
218 {
219  if (val == 0) {
220  val = -range - get_ue_golomb(gb);
221  } else if (val == range * 2) {
222  val = range + get_ue_golomb(gb);
223  } else {
224  val -= range;
225  }
226  if (bits)
227  val = (val << bits) | get_bits(gb, bits);
228  return val;
229 }
230 
231 static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
232  int length, int mode, int bits)
233 {
234  int i, t;
235  int code_params;
236  VLCSet *set = ctx->sets + mode;
237  VLC *code_vlc; int range, range2, add_bits;
238  int *dst = ctx->channel_data[ch];
239 
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;
243 
244  if (ctx->filter_params == FILTER_RAW) {
245  for (i = 0; i < length; i++)
246  dst[i] = get_bits(gb, bits);
247  ctx->bias[ch] = 0;
248  return 0;
249  }
250 
251  ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
252  ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
253 
254  if (ctx->filter_params == FILTER_NONE) {
255  memset(dst, 0, sizeof(*dst) * length);
256  return 0;
257  }
258 
259  if (ctx->filter_params > 1) {
260  int cmode = 0, coeff = 0;
261  VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
262 
263  add_bits = ctx->filter_bits;
264 
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);
268  if (!cmode)
269  coeff -= 12 << add_bits;
270  coeff = t - coeff;
271  ctx->filter[i] = coeff;
272 
273  cmode = coeff >> add_bits;
274  if (cmode < 0) {
275  cmode = -1 - av_log2(-cmode);
276  if (cmode < -5)
277  cmode = -5;
278  } else if (cmode > 0) {
279  cmode = 1 + av_log2(cmode);
280  if (cmode > 5)
281  cmode = 5;
282  }
283  }
284  }
285 
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)
290  add_bits--;
291  range = 10;
292  range2 = 21;
293  code_vlc = set->long_codes + code_params - 15;
294  } else {
295  add_bits = 0;
296  range = 6;
297  range2 = 13;
298  code_vlc = set->short_codes + code_params;
299  }
300 
301  for (i = 0; i < length; i += 2) {
302  int code1, code2;
303 
304  t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
305  code1 = t / range2;
306  code2 = t % range2;
307  dst[i] = extend_code(gb, code1, range, 0) << add_bits;
308  dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits;
309  if (add_bits) {
310  dst[i] |= get_bits(gb, add_bits);
311  dst[i + 1] |= get_bits(gb, add_bits);
312  }
313  }
314 
315  return 0;
316 }
317 
318 static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
319 {
320  int i, j, acc;
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;
324 
325  for (i = 1; i < length; i++) {
326  int flen = FFMIN(ctx->filter_length, i);
327 
328  acc = 0;
329  for (j = 0; j < flen; j++)
330  acc += ctx->filter[j] * audio[i - j - 1];
331  if (acc < 0) {
332  acc = (acc + bias - 1) >> ctx->filter_bits;
333  acc = FFMAX(acc, min_clip);
334  } else {
335  acc = (acc + bias) >> ctx->filter_bits;
336  acc = FFMIN(acc, max_clip);
337  }
338  audio[i] += acc;
339  }
340 }
341 
343  int16_t *dst0, int16_t *dst1)
344 {
345  RALFContext *ctx = avctx->priv_data;
346  int len, ch, ret;
347  int dmode, mode[2], bits[2];
348  int *ch0, *ch1;
349  int i, t, t2;
350 
351  len = 12 - get_unary(gb, 0, 6);
352 
353  if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
354  len = 1 << len;
355 
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;
360  }
361 
362  if (avctx->channels > 1)
363  dmode = get_bits(gb, 2) + 1;
364  else
365  dmode = 0;
366 
367  mode[0] = (dmode == 4) ? 1 : 0;
368  mode[1] = (dmode >= 2) ? 2 : 0;
369  bits[0] = 16;
370  bits[1] = (mode[1] == 2) ? 17 : 16;
371 
372  for (ch = 0; ch < avctx->channels; ch++) {
373  if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
374  return ret;
375  if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
376  ctx->filter_bits += 3;
377  apply_lpc(ctx, ch, len, bits[ch]);
378  }
379  if (get_bits_left(gb) < 0)
380  return AVERROR_INVALIDDATA;
381  }
382  ch0 = ctx->channel_data[0];
383  ch1 = ctx->channel_data[1];
384  switch (dmode) {
385  case 0:
386  for (i = 0; i < len; i++)
387  dst0[i] = ch0[i] + ctx->bias[0];
388  break;
389  case 1:
390  for (i = 0; i < len; i++) {
391  dst0[i] = ch0[i] + ctx->bias[0];
392  dst1[i] = ch1[i] + ctx->bias[1];
393  }
394  break;
395  case 2:
396  for (i = 0; i < len; i++) {
397  ch0[i] += ctx->bias[0];
398  dst0[i] = ch0[i];
399  dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
400  }
401  break;
402  case 3:
403  for (i = 0; i < len; i++) {
404  t = ch0[i] + ctx->bias[0];
405  t2 = ch1[i] + ctx->bias[1];
406  dst0[i] = t + t2;
407  dst1[i] = t;
408  }
409  break;
410  case 4:
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;
416  }
417  break;
418  }
419 
420  ctx->sample_offset += len;
421 
422  return 0;
423 }
424 
425 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
426  AVPacket *avpkt)
427 {
428  RALFContext *ctx = avctx->priv_data;
429  int16_t *samples0;
430  int16_t *samples1;
431  int ret;
432  GetBitContext gb;
433  int table_size, table_bytes, i;
434  const uint8_t *src, *block_pointer;
435  int src_size;
436  int bytes_left;
437 
438  if (ctx->has_pkt) {
439  ctx->has_pkt = 0;
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;
444  }
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;
448  }
449 
450  src = ctx->pkt;
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);
454  } else {
455  if (avpkt->size == RALF_MAX_PKT_SIZE) {
456  memcpy(ctx->pkt, avpkt->data, avpkt->size);
457  ctx->has_pkt = 1;
458  *got_frame_ptr = 0;
459 
460  return avpkt->size;
461  }
462  src = avpkt->data;
463  src_size = avpkt->size;
464  }
465 
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");
469  return ret;
470  }
471  samples0 = (int16_t *)ctx->frame.data[0];
472  samples1 = (int16_t *)ctx->frame.data[1];
473 
474  if (src_size < 5) {
475  av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
476  return AVERROR_INVALIDDATA;
477  }
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;
483  }
484  init_get_bits(&gb, src + 2, table_size);
485  ctx->num_blocks = 0;
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);
490  } else {
491  ctx->block_pts[ctx->num_blocks] = 0;
492  }
493  ctx->num_blocks++;
494  }
495 
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");
502  break;
503  }
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");
508  break;
509  }
510  block_pointer += ctx->block_size[i];
511  bytes_left -= ctx->block_size[i];
512  }
513 
514  ctx->frame.nb_samples = ctx->sample_offset;
515  *got_frame_ptr = ctx->sample_offset > 0;
516  *(AVFrame*)data = ctx->frame;
517 
518  return avpkt->size;
519 }
520 
521 static void decode_flush(AVCodecContext *avctx)
522 {
523  RALFContext *ctx = avctx->priv_data;
524 
525  ctx->has_pkt = 0;
526 }
527 
528 
530  .name = "ralf",
531  .type = AVMEDIA_TYPE_AUDIO,
532  .id = AV_CODEC_ID_RALF,
533  .priv_data_size = sizeof(RALFContext),
534  .init = decode_init,
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,
542 };