You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

543 lines
16KB

  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. * @file
  24. * This is a decoder for Real Audio Lossless format.
  25. * Dedicated to the mastermind behind it, Ralph Wiggum.
  26. */
  27. #include "libavutil/channel_layout.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "golomb.h"
  31. #include "internal.h"
  32. #include "unary.h"
  33. #include "ralfdata.h"
  34. #define FILTER_NONE 0
  35. #define FILTER_RAW 642
  36. typedef struct VLCSet {
  37. VLC filter_params;
  38. VLC bias;
  39. VLC coding_mode;
  40. VLC filter_coeffs[10][11];
  41. VLC short_codes[15];
  42. VLC long_codes[125];
  43. } VLCSet;
  44. #define RALF_MAX_PKT_SIZE 8192
  45. typedef struct RALFContext {
  46. AVFrame frame;
  47. int version;
  48. int max_frame_size;
  49. VLCSet sets[3];
  50. int32_t channel_data[2][4096];
  51. int filter_params; ///< combined filter parameters for the current channel data
  52. int filter_length; ///< length of the filter for the current channel data
  53. int filter_bits; ///< filter precision for the current channel data
  54. int32_t filter[64];
  55. int bias[2]; ///< a constant value added to channel data after filtering
  56. int num_blocks; ///< number of blocks inside the frame
  57. int sample_offset;
  58. int block_size[1 << 12]; ///< size of the blocks
  59. int block_pts[1 << 12]; ///< block start time (in milliseconds)
  60. uint8_t pkt[16384];
  61. int has_pkt;
  62. } RALFContext;
  63. #define MAX_ELEMS 644 // no RALF table uses more than that
  64. static int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
  65. {
  66. uint8_t lens[MAX_ELEMS];
  67. uint16_t codes[MAX_ELEMS];
  68. int counts[17], prefixes[18];
  69. int i, cur_len;
  70. int max_bits = 0;
  71. int nb = 0;
  72. for (i = 0; i <= 16; i++)
  73. counts[i] = 0;
  74. for (i = 0; i < elems; i++) {
  75. cur_len = (nb ? *data & 0xF : *data >> 4) + 1;
  76. counts[cur_len]++;
  77. max_bits = FFMAX(max_bits, cur_len);
  78. lens[i] = cur_len;
  79. data += nb;
  80. nb ^= 1;
  81. }
  82. prefixes[1] = 0;
  83. for (i = 1; i <= 16; i++)
  84. prefixes[i + 1] = (prefixes[i] + counts[i]) << 1;
  85. for (i = 0; i < elems; i++)
  86. codes[i] = prefixes[lens[i]]++;
  87. return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), elems,
  88. lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0);
  89. }
  90. static av_cold int decode_close(AVCodecContext *avctx)
  91. {
  92. RALFContext *ctx = avctx->priv_data;
  93. int i, j, k;
  94. for (i = 0; i < 3; i++) {
  95. ff_free_vlc(&ctx->sets[i].filter_params);
  96. ff_free_vlc(&ctx->sets[i].bias);
  97. ff_free_vlc(&ctx->sets[i].coding_mode);
  98. for (j = 0; j < 10; j++)
  99. for (k = 0; k < 11; k++)
  100. ff_free_vlc(&ctx->sets[i].filter_coeffs[j][k]);
  101. for (j = 0; j < 15; j++)
  102. ff_free_vlc(&ctx->sets[i].short_codes[j]);
  103. for (j = 0; j < 125; j++)
  104. ff_free_vlc(&ctx->sets[i].long_codes[j]);
  105. }
  106. return 0;
  107. }
  108. static av_cold int decode_init(AVCodecContext *avctx)
  109. {
  110. RALFContext *ctx = avctx->priv_data;
  111. int i, j, k;
  112. int ret;
  113. if (avctx->extradata_size < 24 || memcmp(avctx->extradata, "LSD:", 4)) {
  114. av_log(avctx, AV_LOG_ERROR, "Extradata is not groovy, dude\n");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. ctx->version = AV_RB16(avctx->extradata + 4);
  118. if (ctx->version != 0x103) {
  119. av_log_ask_for_sample(avctx, "unknown version %X\n", ctx->version);
  120. return AVERROR_PATCHWELCOME;
  121. }
  122. avctx->channels = AV_RB16(avctx->extradata + 8);
  123. avctx->sample_rate = AV_RB32(avctx->extradata + 12);
  124. if (avctx->channels < 1 || avctx->channels > 2
  125. || avctx->sample_rate < 8000 || avctx->sample_rate > 96000) {
  126. av_log(avctx, AV_LOG_ERROR, "Invalid coding parameters %d Hz %d ch\n",
  127. avctx->sample_rate, avctx->channels);
  128. return AVERROR_INVALIDDATA;
  129. }
  130. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  131. avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
  132. : AV_CH_LAYOUT_MONO;
  133. avcodec_get_frame_defaults(&ctx->frame);
  134. avctx->coded_frame = &ctx->frame;
  135. ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
  136. if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
  137. av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
  138. ctx->max_frame_size);
  139. }
  140. ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
  141. for (i = 0; i < 3; i++) {
  142. ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i],
  143. FILTERPARAM_ELEMENTS);
  144. if (ret < 0) {
  145. decode_close(avctx);
  146. return ret;
  147. }
  148. ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
  149. if (ret < 0) {
  150. decode_close(avctx);
  151. return ret;
  152. }
  153. ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
  154. CODING_MODE_ELEMENTS);
  155. if (ret < 0) {
  156. decode_close(avctx);
  157. return ret;
  158. }
  159. for (j = 0; j < 10; j++) {
  160. for (k = 0; k < 11; k++) {
  161. ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k],
  162. filter_coeffs_def[i][j][k],
  163. FILTER_COEFFS_ELEMENTS);
  164. if (ret < 0) {
  165. decode_close(avctx);
  166. return ret;
  167. }
  168. }
  169. }
  170. for (j = 0; j < 15; j++) {
  171. ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
  172. short_codes_def[i][j], SHORT_CODES_ELEMENTS);
  173. if (ret < 0) {
  174. decode_close(avctx);
  175. return ret;
  176. }
  177. }
  178. for (j = 0; j < 125; j++) {
  179. ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
  180. long_codes_def[i][j], LONG_CODES_ELEMENTS);
  181. if (ret < 0) {
  182. decode_close(avctx);
  183. return ret;
  184. }
  185. }
  186. }
  187. return 0;
  188. }
  189. static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
  190. {
  191. if (val == 0) {
  192. val = -range - get_ue_golomb(gb);
  193. } else if (val == range * 2) {
  194. val = range + get_ue_golomb(gb);
  195. } else {
  196. val -= range;
  197. }
  198. if (bits)
  199. val = (val << bits) | get_bits(gb, bits);
  200. return val;
  201. }
  202. static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
  203. int length, int mode, int bits)
  204. {
  205. int i, t;
  206. int code_params;
  207. VLCSet *set = ctx->sets + mode;
  208. VLC *code_vlc; int range, range2, add_bits;
  209. int *dst = ctx->channel_data[ch];
  210. ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
  211. ctx->filter_bits = (ctx->filter_params - 2) >> 6;
  212. ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
  213. if (ctx->filter_params == FILTER_RAW) {
  214. for (i = 0; i < length; i++)
  215. dst[i] = get_bits(gb, bits);
  216. ctx->bias[ch] = 0;
  217. return 0;
  218. }
  219. ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
  220. ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
  221. if (ctx->filter_params == FILTER_NONE) {
  222. memset(dst, 0, sizeof(*dst) * length);
  223. return 0;
  224. }
  225. if (ctx->filter_params > 1) {
  226. int cmode = 0, coeff = 0;
  227. VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
  228. add_bits = ctx->filter_bits;
  229. for (i = 0; i < ctx->filter_length; i++) {
  230. t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
  231. t = extend_code(gb, t, 21, add_bits);
  232. if (!cmode)
  233. coeff -= 12 << add_bits;
  234. coeff = t - coeff;
  235. ctx->filter[i] = coeff;
  236. cmode = coeff >> add_bits;
  237. if (cmode < 0) {
  238. cmode = -1 - av_log2(-cmode);
  239. if (cmode < -5)
  240. cmode = -5;
  241. } else if (cmode > 0) {
  242. cmode = 1 + av_log2(cmode);
  243. if (cmode > 5)
  244. cmode = 5;
  245. }
  246. }
  247. }
  248. code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
  249. if (code_params >= 15) {
  250. add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
  251. if (add_bits > 9 && (code_params % 5) != 2)
  252. add_bits--;
  253. range = 10;
  254. range2 = 21;
  255. code_vlc = set->long_codes + code_params - 15;
  256. } else {
  257. add_bits = 0;
  258. range = 6;
  259. range2 = 13;
  260. code_vlc = set->short_codes + code_params;
  261. }
  262. for (i = 0; i < length; i += 2) {
  263. int code1, code2;
  264. t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
  265. code1 = t / range2;
  266. code2 = t % range2;
  267. dst[i] = extend_code(gb, code1, range, 0) << add_bits;
  268. dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits;
  269. if (add_bits) {
  270. dst[i] |= get_bits(gb, add_bits);
  271. dst[i + 1] |= get_bits(gb, add_bits);
  272. }
  273. }
  274. return 0;
  275. }
  276. static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
  277. {
  278. int i, j, acc;
  279. int *audio = ctx->channel_data[ch];
  280. int bias = 1 << (ctx->filter_bits - 1);
  281. int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1;
  282. for (i = 1; i < length; i++) {
  283. int flen = FFMIN(ctx->filter_length, i);
  284. acc = 0;
  285. for (j = 0; j < flen; j++)
  286. acc += ctx->filter[j] * audio[i - j - 1];
  287. if (acc < 0) {
  288. acc = (acc + bias - 1) >> ctx->filter_bits;
  289. acc = FFMAX(acc, min_clip);
  290. } else {
  291. acc = (acc + bias) >> ctx->filter_bits;
  292. acc = FFMIN(acc, max_clip);
  293. }
  294. audio[i] += acc;
  295. }
  296. }
  297. static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
  298. int16_t *dst0, int16_t *dst1)
  299. {
  300. RALFContext *ctx = avctx->priv_data;
  301. int len, ch, ret;
  302. int dmode, mode[2], bits[2];
  303. int *ch0, *ch1;
  304. int i, t, t2;
  305. len = 12 - get_unary(gb, 0, 6);
  306. if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
  307. len = 1 << len;
  308. if (ctx->sample_offset + len > ctx->max_frame_size) {
  309. av_log(avctx, AV_LOG_ERROR,
  310. "Decoder's stomach is crying, it ate too many samples\n");
  311. return AVERROR_INVALIDDATA;
  312. }
  313. if (avctx->channels > 1)
  314. dmode = get_bits(gb, 2) + 1;
  315. else
  316. dmode = 0;
  317. mode[0] = (dmode == 4) ? 1 : 0;
  318. mode[1] = (dmode >= 2) ? 2 : 0;
  319. bits[0] = 16;
  320. bits[1] = (mode[1] == 2) ? 17 : 16;
  321. for (ch = 0; ch < avctx->channels; ch++) {
  322. if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
  323. return ret;
  324. if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
  325. ctx->filter_bits += 3;
  326. apply_lpc(ctx, ch, len, bits[ch]);
  327. }
  328. if (get_bits_left(gb) < 0)
  329. return AVERROR_INVALIDDATA;
  330. }
  331. ch0 = ctx->channel_data[0];
  332. ch1 = ctx->channel_data[1];
  333. switch (dmode) {
  334. case 0:
  335. for (i = 0; i < len; i++)
  336. dst0[i] = ch0[i] + ctx->bias[0];
  337. break;
  338. case 1:
  339. for (i = 0; i < len; i++) {
  340. dst0[i] = ch0[i] + ctx->bias[0];
  341. dst1[i] = ch1[i] + ctx->bias[1];
  342. }
  343. break;
  344. case 2:
  345. for (i = 0; i < len; i++) {
  346. ch0[i] += ctx->bias[0];
  347. dst0[i] = ch0[i];
  348. dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
  349. }
  350. break;
  351. case 3:
  352. for (i = 0; i < len; i++) {
  353. t = ch0[i] + ctx->bias[0];
  354. t2 = ch1[i] + ctx->bias[1];
  355. dst0[i] = t + t2;
  356. dst1[i] = t;
  357. }
  358. break;
  359. case 4:
  360. for (i = 0; i < len; i++) {
  361. t = ch1[i] + ctx->bias[1];
  362. t2 = ((ch0[i] + ctx->bias[0]) << 1) | (t & 1);
  363. dst0[i] = (t2 + t) / 2;
  364. dst1[i] = (t2 - t) / 2;
  365. }
  366. break;
  367. }
  368. ctx->sample_offset += len;
  369. return 0;
  370. }
  371. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
  372. AVPacket *avpkt)
  373. {
  374. RALFContext *ctx = avctx->priv_data;
  375. int16_t *samples0;
  376. int16_t *samples1;
  377. int ret;
  378. GetBitContext gb;
  379. int table_size, table_bytes, i;
  380. const uint8_t *src, *block_pointer;
  381. int src_size;
  382. int bytes_left;
  383. if (ctx->has_pkt) {
  384. ctx->has_pkt = 0;
  385. table_bytes = (AV_RB16(avpkt->data) + 7) >> 3;
  386. if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) {
  387. av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n");
  388. return AVERROR_INVALIDDATA;
  389. }
  390. if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) {
  391. av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n");
  392. return AVERROR_INVALIDDATA;
  393. }
  394. src = ctx->pkt;
  395. src_size = RALF_MAX_PKT_SIZE + avpkt->size;
  396. memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes,
  397. avpkt->size - 2 - table_bytes);
  398. } else {
  399. if (avpkt->size == RALF_MAX_PKT_SIZE) {
  400. memcpy(ctx->pkt, avpkt->data, avpkt->size);
  401. ctx->has_pkt = 1;
  402. *got_frame_ptr = 0;
  403. return avpkt->size;
  404. }
  405. src = avpkt->data;
  406. src_size = avpkt->size;
  407. }
  408. ctx->frame.nb_samples = ctx->max_frame_size;
  409. if ((ret = ff_get_buffer(avctx, &ctx->frame)) < 0) {
  410. av_log(avctx, AV_LOG_ERROR, "Me fail get_buffer()? That's unpossible!\n");
  411. return ret;
  412. }
  413. samples0 = (int16_t *)ctx->frame.data[0];
  414. samples1 = (int16_t *)ctx->frame.data[1];
  415. if (src_size < 5) {
  416. av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
  417. return AVERROR_INVALIDDATA;
  418. }
  419. table_size = AV_RB16(src);
  420. table_bytes = (table_size + 7) >> 3;
  421. if (src_size < table_bytes + 3) {
  422. av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
  423. return AVERROR_INVALIDDATA;
  424. }
  425. init_get_bits(&gb, src + 2, table_size);
  426. ctx->num_blocks = 0;
  427. while (get_bits_left(&gb) > 0) {
  428. ctx->block_size[ctx->num_blocks] = get_bits(&gb, 15);
  429. if (get_bits1(&gb)) {
  430. ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
  431. } else {
  432. ctx->block_pts[ctx->num_blocks] = 0;
  433. }
  434. ctx->num_blocks++;
  435. }
  436. block_pointer = src + table_bytes + 2;
  437. bytes_left = src_size - table_bytes - 2;
  438. ctx->sample_offset = 0;
  439. for (i = 0; i < ctx->num_blocks; i++) {
  440. if (bytes_left < ctx->block_size[i]) {
  441. av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
  442. break;
  443. }
  444. init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
  445. if (decode_block(avctx, &gb, samples0 + ctx->sample_offset,
  446. samples1 + ctx->sample_offset) < 0) {
  447. av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
  448. break;
  449. }
  450. block_pointer += ctx->block_size[i];
  451. bytes_left -= ctx->block_size[i];
  452. }
  453. ctx->frame.nb_samples = ctx->sample_offset;
  454. *got_frame_ptr = ctx->sample_offset > 0;
  455. *(AVFrame*)data = ctx->frame;
  456. return avpkt->size;
  457. }
  458. static void decode_flush(AVCodecContext *avctx)
  459. {
  460. RALFContext *ctx = avctx->priv_data;
  461. ctx->has_pkt = 0;
  462. }
  463. AVCodec ff_ralf_decoder = {
  464. .name = "ralf",
  465. .type = AVMEDIA_TYPE_AUDIO,
  466. .id = AV_CODEC_ID_RALF,
  467. .priv_data_size = sizeof(RALFContext),
  468. .init = decode_init,
  469. .close = decode_close,
  470. .decode = decode_frame,
  471. .flush = decode_flush,
  472. .capabilities = CODEC_CAP_DR1,
  473. .long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
  474. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  475. AV_SAMPLE_FMT_NONE },
  476. };