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.

537 lines
16KB

  1. /*
  2. * RealAudio Lossless decoder
  3. *
  4. * Copyright (c) 2012 Konstantin Shishkov
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "get_bits.h"
  31. #include "golomb.h"
  32. #include "internal.h"
  33. #include "unary.h"
  34. #include "ralfdata.h"
  35. #define FILTER_NONE 0
  36. #define FILTER_RAW 642
  37. typedef struct VLCSet {
  38. VLC filter_params;
  39. VLC bias;
  40. VLC coding_mode;
  41. VLC filter_coeffs[10][11];
  42. VLC short_codes[15];
  43. VLC long_codes[125];
  44. } VLCSet;
  45. #define RALF_MAX_PKT_SIZE 8192
  46. typedef struct RALFContext {
  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 av_cold 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. avpriv_request_sample(avctx, "Unknown version %X", 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. ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
  134. if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
  135. av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
  136. ctx->max_frame_size);
  137. }
  138. ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
  139. for (i = 0; i < 3; i++) {
  140. ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i],
  141. FILTERPARAM_ELEMENTS);
  142. if (ret < 0) {
  143. decode_close(avctx);
  144. return ret;
  145. }
  146. ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
  147. if (ret < 0) {
  148. decode_close(avctx);
  149. return ret;
  150. }
  151. ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
  152. CODING_MODE_ELEMENTS);
  153. if (ret < 0) {
  154. decode_close(avctx);
  155. return ret;
  156. }
  157. for (j = 0; j < 10; j++) {
  158. for (k = 0; k < 11; k++) {
  159. ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k],
  160. filter_coeffs_def[i][j][k],
  161. FILTER_COEFFS_ELEMENTS);
  162. if (ret < 0) {
  163. decode_close(avctx);
  164. return ret;
  165. }
  166. }
  167. }
  168. for (j = 0; j < 15; j++) {
  169. ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
  170. short_codes_def[i][j], SHORT_CODES_ELEMENTS);
  171. if (ret < 0) {
  172. decode_close(avctx);
  173. return ret;
  174. }
  175. }
  176. for (j = 0; j < 125; j++) {
  177. ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
  178. long_codes_def[i][j], LONG_CODES_ELEMENTS);
  179. if (ret < 0) {
  180. decode_close(avctx);
  181. return ret;
  182. }
  183. }
  184. }
  185. return 0;
  186. }
  187. static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
  188. {
  189. if (val == 0) {
  190. val = -range - get_ue_golomb(gb);
  191. } else if (val == range * 2) {
  192. val = range + get_ue_golomb(gb);
  193. } else {
  194. val -= range;
  195. }
  196. if (bits)
  197. val = (val << bits) | get_bits(gb, bits);
  198. return val;
  199. }
  200. static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
  201. int length, int mode, int bits)
  202. {
  203. int i, t;
  204. int code_params;
  205. VLCSet *set = ctx->sets + mode;
  206. VLC *code_vlc; int range, range2, add_bits;
  207. int *dst = ctx->channel_data[ch];
  208. ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
  209. ctx->filter_bits = (ctx->filter_params - 2) >> 6;
  210. ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
  211. if (ctx->filter_params == FILTER_RAW) {
  212. for (i = 0; i < length; i++)
  213. dst[i] = get_bits(gb, bits);
  214. ctx->bias[ch] = 0;
  215. return 0;
  216. }
  217. ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
  218. ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
  219. if (ctx->filter_params == FILTER_NONE) {
  220. memset(dst, 0, sizeof(*dst) * length);
  221. return 0;
  222. }
  223. if (ctx->filter_params > 1) {
  224. int cmode = 0, coeff = 0;
  225. VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
  226. add_bits = ctx->filter_bits;
  227. for (i = 0; i < ctx->filter_length; i++) {
  228. t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
  229. t = extend_code(gb, t, 21, add_bits);
  230. if (!cmode)
  231. coeff -= 12 << add_bits;
  232. coeff = t - coeff;
  233. ctx->filter[i] = coeff;
  234. cmode = coeff >> add_bits;
  235. if (cmode < 0) {
  236. cmode = -1 - av_log2(-cmode);
  237. if (cmode < -5)
  238. cmode = -5;
  239. } else if (cmode > 0) {
  240. cmode = 1 + av_log2(cmode);
  241. if (cmode > 5)
  242. cmode = 5;
  243. }
  244. }
  245. }
  246. code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
  247. if (code_params >= 15) {
  248. add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
  249. if (add_bits > 9 && (code_params % 5) != 2)
  250. add_bits--;
  251. range = 10;
  252. range2 = 21;
  253. code_vlc = set->long_codes + code_params - 15;
  254. } else {
  255. add_bits = 0;
  256. range = 6;
  257. range2 = 13;
  258. code_vlc = set->short_codes + code_params;
  259. }
  260. for (i = 0; i < length; i += 2) {
  261. int code1, code2;
  262. t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
  263. code1 = t / range2;
  264. code2 = t % range2;
  265. dst[i] = extend_code(gb, code1, range, 0) << add_bits;
  266. dst[i + 1] = extend_code(gb, code2, range, 0) << add_bits;
  267. if (add_bits) {
  268. dst[i] |= get_bits(gb, add_bits);
  269. dst[i + 1] |= get_bits(gb, add_bits);
  270. }
  271. }
  272. return 0;
  273. }
  274. static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
  275. {
  276. int i, j, acc;
  277. int *audio = ctx->channel_data[ch];
  278. int bias = 1 << (ctx->filter_bits - 1);
  279. int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1;
  280. for (i = 1; i < length; i++) {
  281. int flen = FFMIN(ctx->filter_length, i);
  282. acc = 0;
  283. for (j = 0; j < flen; j++)
  284. acc += ctx->filter[j] * audio[i - j - 1];
  285. if (acc < 0) {
  286. acc = (acc + bias - 1) >> ctx->filter_bits;
  287. acc = FFMAX(acc, min_clip);
  288. } else {
  289. acc = (acc + bias) >> ctx->filter_bits;
  290. acc = FFMIN(acc, max_clip);
  291. }
  292. audio[i] += acc;
  293. }
  294. }
  295. static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
  296. int16_t *dst0, int16_t *dst1)
  297. {
  298. RALFContext *ctx = avctx->priv_data;
  299. int len, ch, ret;
  300. int dmode, mode[2], bits[2];
  301. int *ch0, *ch1;
  302. int i, t, t2;
  303. len = 12 - get_unary(gb, 0, 6);
  304. if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
  305. len = 1 << len;
  306. if (ctx->sample_offset + len > ctx->max_frame_size) {
  307. av_log(avctx, AV_LOG_ERROR,
  308. "Decoder's stomach is crying, it ate too many samples\n");
  309. return AVERROR_INVALIDDATA;
  310. }
  311. if (avctx->channels > 1)
  312. dmode = get_bits(gb, 2) + 1;
  313. else
  314. dmode = 0;
  315. mode[0] = (dmode == 4) ? 1 : 0;
  316. mode[1] = (dmode >= 2) ? 2 : 0;
  317. bits[0] = 16;
  318. bits[1] = (mode[1] == 2) ? 17 : 16;
  319. for (ch = 0; ch < avctx->channels; ch++) {
  320. if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
  321. return ret;
  322. if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
  323. ctx->filter_bits += 3;
  324. apply_lpc(ctx, ch, len, bits[ch]);
  325. }
  326. if (get_bits_left(gb) < 0)
  327. return AVERROR_INVALIDDATA;
  328. }
  329. ch0 = ctx->channel_data[0];
  330. ch1 = ctx->channel_data[1];
  331. switch (dmode) {
  332. case 0:
  333. for (i = 0; i < len; i++)
  334. dst0[i] = ch0[i] + ctx->bias[0];
  335. break;
  336. case 1:
  337. for (i = 0; i < len; i++) {
  338. dst0[i] = ch0[i] + ctx->bias[0];
  339. dst1[i] = ch1[i] + ctx->bias[1];
  340. }
  341. break;
  342. case 2:
  343. for (i = 0; i < len; i++) {
  344. ch0[i] += ctx->bias[0];
  345. dst0[i] = ch0[i];
  346. dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
  347. }
  348. break;
  349. case 3:
  350. for (i = 0; i < len; i++) {
  351. t = ch0[i] + ctx->bias[0];
  352. t2 = ch1[i] + ctx->bias[1];
  353. dst0[i] = t + t2;
  354. dst1[i] = t;
  355. }
  356. break;
  357. case 4:
  358. for (i = 0; i < len; i++) {
  359. t = ch1[i] + ctx->bias[1];
  360. t2 = ((ch0[i] + ctx->bias[0]) << 1) | (t & 1);
  361. dst0[i] = (t2 + t) / 2;
  362. dst1[i] = (t2 - t) / 2;
  363. }
  364. break;
  365. }
  366. ctx->sample_offset += len;
  367. return 0;
  368. }
  369. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
  370. AVPacket *avpkt)
  371. {
  372. RALFContext *ctx = avctx->priv_data;
  373. AVFrame *frame = data;
  374. int16_t *samples0;
  375. int16_t *samples1;
  376. int ret;
  377. GetBitContext gb;
  378. int table_size, table_bytes, i;
  379. const uint8_t *src, *block_pointer;
  380. int src_size;
  381. int bytes_left;
  382. if (ctx->has_pkt) {
  383. ctx->has_pkt = 0;
  384. table_bytes = (AV_RB16(avpkt->data) + 7) >> 3;
  385. if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) {
  386. av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n");
  387. return AVERROR_INVALIDDATA;
  388. }
  389. if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) {
  390. av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n");
  391. return AVERROR_INVALIDDATA;
  392. }
  393. src = ctx->pkt;
  394. src_size = RALF_MAX_PKT_SIZE + avpkt->size;
  395. memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes,
  396. avpkt->size - 2 - table_bytes);
  397. } else {
  398. if (avpkt->size == RALF_MAX_PKT_SIZE) {
  399. memcpy(ctx->pkt, avpkt->data, avpkt->size);
  400. ctx->has_pkt = 1;
  401. *got_frame_ptr = 0;
  402. return avpkt->size;
  403. }
  404. src = avpkt->data;
  405. src_size = avpkt->size;
  406. }
  407. frame->nb_samples = ctx->max_frame_size;
  408. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  409. return ret;
  410. samples0 = (int16_t *)frame->data[0];
  411. samples1 = (int16_t *)frame->data[1];
  412. if (src_size < 5) {
  413. av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
  414. return AVERROR_INVALIDDATA;
  415. }
  416. table_size = AV_RB16(src);
  417. table_bytes = (table_size + 7) >> 3;
  418. if (src_size < table_bytes + 3) {
  419. av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
  420. return AVERROR_INVALIDDATA;
  421. }
  422. init_get_bits(&gb, src + 2, table_size);
  423. ctx->num_blocks = 0;
  424. while (get_bits_left(&gb) > 0) {
  425. ctx->block_size[ctx->num_blocks] = get_bits(&gb, 15);
  426. if (get_bits1(&gb)) {
  427. ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
  428. } else {
  429. ctx->block_pts[ctx->num_blocks] = 0;
  430. }
  431. ctx->num_blocks++;
  432. }
  433. block_pointer = src + table_bytes + 2;
  434. bytes_left = src_size - table_bytes - 2;
  435. ctx->sample_offset = 0;
  436. for (i = 0; i < ctx->num_blocks; i++) {
  437. if (bytes_left < ctx->block_size[i]) {
  438. av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
  439. break;
  440. }
  441. init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
  442. if (decode_block(avctx, &gb, samples0 + ctx->sample_offset,
  443. samples1 + ctx->sample_offset) < 0) {
  444. av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
  445. break;
  446. }
  447. block_pointer += ctx->block_size[i];
  448. bytes_left -= ctx->block_size[i];
  449. }
  450. frame->nb_samples = ctx->sample_offset;
  451. *got_frame_ptr = ctx->sample_offset > 0;
  452. return avpkt->size;
  453. }
  454. static void decode_flush(AVCodecContext *avctx)
  455. {
  456. RALFContext *ctx = avctx->priv_data;
  457. ctx->has_pkt = 0;
  458. }
  459. AVCodec ff_ralf_decoder = {
  460. .name = "ralf",
  461. .type = AVMEDIA_TYPE_AUDIO,
  462. .id = AV_CODEC_ID_RALF,
  463. .priv_data_size = sizeof(RALFContext),
  464. .init = decode_init,
  465. .close = decode_close,
  466. .decode = decode_frame,
  467. .flush = decode_flush,
  468. .capabilities = CODEC_CAP_DR1,
  469. .long_name = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
  470. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  471. AV_SAMPLE_FMT_NONE },
  472. };