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.

541 lines
17KB

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