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.

399 lines
15KB

  1. /*
  2. * ATRAC3+ compatible decoder
  3. *
  4. * Copyright (c) 2010-2013 Maxim Poliakovski
  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. * Sony ATRAC3+ compatible decoder.
  25. *
  26. * Container formats used to store its data:
  27. * RIFF WAV (.at3) and Sony OpenMG (.oma, .aa3).
  28. *
  29. * Technical description of this codec can be found here:
  30. * http://wiki.multimedia.cx/index.php?title=ATRAC3plus
  31. *
  32. * Kudos to Benjamin Larsson and Michael Karcher
  33. * for their precious technical help!
  34. */
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include "libavutil/channel_layout.h"
  38. #include "libavutil/float_dsp.h"
  39. #include "avcodec.h"
  40. #include "get_bits.h"
  41. #include "internal.h"
  42. #include "atrac.h"
  43. #include "atrac3plus.h"
  44. typedef struct ATRAC3PContext {
  45. GetBitContext gb;
  46. AVFloatDSPContext *fdsp;
  47. DECLARE_ALIGNED(32, float, samples)[2][ATRAC3P_FRAME_SAMPLES]; ///< quantized MDCT spectrum
  48. DECLARE_ALIGNED(32, float, mdct_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the IMDCT
  49. DECLARE_ALIGNED(32, float, time_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< output of the gain compensation
  50. DECLARE_ALIGNED(32, float, outp_buf)[2][ATRAC3P_FRAME_SAMPLES];
  51. AtracGCContext gainc_ctx; ///< gain compensation context
  52. FFTContext mdct_ctx;
  53. FFTContext ipqf_dct_ctx; ///< IDCT context used by IPQF
  54. Atrac3pChanUnitCtx *ch_units; ///< global channel units
  55. int num_channel_blocks; ///< number of channel blocks
  56. uint8_t channel_blocks[5]; ///< channel configuration descriptor
  57. uint64_t my_channel_layout; ///< current channel layout
  58. } ATRAC3PContext;
  59. static av_cold int atrac3p_decode_close(AVCodecContext *avctx)
  60. {
  61. ATRAC3PContext *ctx = avctx->priv_data;
  62. av_freep(&ctx->ch_units);
  63. av_freep(&ctx->fdsp);
  64. return 0;
  65. }
  66. static av_cold int set_channel_params(ATRAC3PContext *ctx,
  67. AVCodecContext *avctx)
  68. {
  69. memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
  70. switch (avctx->channels) {
  71. case 1:
  72. if (avctx->channel_layout != AV_CH_FRONT_LEFT)
  73. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  74. ctx->num_channel_blocks = 1;
  75. ctx->channel_blocks[0] = CH_UNIT_MONO;
  76. break;
  77. case 2:
  78. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  79. ctx->num_channel_blocks = 1;
  80. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  81. break;
  82. case 3:
  83. avctx->channel_layout = AV_CH_LAYOUT_SURROUND;
  84. ctx->num_channel_blocks = 2;
  85. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  86. ctx->channel_blocks[1] = CH_UNIT_MONO;
  87. break;
  88. case 4:
  89. avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
  90. ctx->num_channel_blocks = 3;
  91. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  92. ctx->channel_blocks[1] = CH_UNIT_MONO;
  93. ctx->channel_blocks[2] = CH_UNIT_MONO;
  94. break;
  95. case 6:
  96. avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
  97. ctx->num_channel_blocks = 4;
  98. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  99. ctx->channel_blocks[1] = CH_UNIT_MONO;
  100. ctx->channel_blocks[2] = CH_UNIT_STEREO;
  101. ctx->channel_blocks[3] = CH_UNIT_MONO;
  102. break;
  103. case 7:
  104. avctx->channel_layout = AV_CH_LAYOUT_6POINT1_BACK;
  105. ctx->num_channel_blocks = 5;
  106. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  107. ctx->channel_blocks[1] = CH_UNIT_MONO;
  108. ctx->channel_blocks[2] = CH_UNIT_STEREO;
  109. ctx->channel_blocks[3] = CH_UNIT_MONO;
  110. ctx->channel_blocks[4] = CH_UNIT_MONO;
  111. break;
  112. case 8:
  113. avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
  114. ctx->num_channel_blocks = 5;
  115. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  116. ctx->channel_blocks[1] = CH_UNIT_MONO;
  117. ctx->channel_blocks[2] = CH_UNIT_STEREO;
  118. ctx->channel_blocks[3] = CH_UNIT_STEREO;
  119. ctx->channel_blocks[4] = CH_UNIT_MONO;
  120. break;
  121. default:
  122. av_log(avctx, AV_LOG_ERROR,
  123. "Unsupported channel count: %d!\n", avctx->channels);
  124. return AVERROR_INVALIDDATA;
  125. }
  126. return 0;
  127. }
  128. static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
  129. {
  130. ATRAC3PContext *ctx = avctx->priv_data;
  131. int i, ch, ret;
  132. if (!avctx->block_align) {
  133. av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
  134. return AVERROR(EINVAL);
  135. }
  136. ff_atrac3p_init_vlcs();
  137. /* initialize IPQF */
  138. ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
  139. ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
  140. ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
  141. ff_atrac3p_init_wave_synth();
  142. if ((ret = set_channel_params(ctx, avctx)) < 0)
  143. return ret;
  144. ctx->my_channel_layout = avctx->channel_layout;
  145. ctx->ch_units = av_mallocz_array(ctx->num_channel_blocks, sizeof(*ctx->ch_units));
  146. ctx->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
  147. if (!ctx->ch_units || !ctx->fdsp) {
  148. atrac3p_decode_close(avctx);
  149. return AVERROR(ENOMEM);
  150. }
  151. for (i = 0; i < ctx->num_channel_blocks; i++) {
  152. for (ch = 0; ch < 2; ch++) {
  153. ctx->ch_units[i].channels[ch].ch_num = ch;
  154. ctx->ch_units[i].channels[ch].wnd_shape = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
  155. ctx->ch_units[i].channels[ch].wnd_shape_prev = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
  156. ctx->ch_units[i].channels[ch].gain_data = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
  157. ctx->ch_units[i].channels[ch].gain_data_prev = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
  158. ctx->ch_units[i].channels[ch].tones_info = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
  159. ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
  160. }
  161. ctx->ch_units[i].waves_info = &ctx->ch_units[i].wave_synth_hist[0];
  162. ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
  163. }
  164. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  165. return 0;
  166. }
  167. static void decode_residual_spectrum(Atrac3pChanUnitCtx *ctx,
  168. float out[2][ATRAC3P_FRAME_SAMPLES],
  169. int num_channels,
  170. AVCodecContext *avctx)
  171. {
  172. int i, sb, ch, qu, nspeclines, RNG_index;
  173. float *dst, q;
  174. int16_t *src;
  175. /* calculate RNG table index for each subband */
  176. int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
  177. if (ctx->mute_flag) {
  178. for (ch = 0; ch < num_channels; ch++)
  179. memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
  180. return;
  181. }
  182. for (qu = 0, RNG_index = 0; qu < ctx->used_quant_units; qu++)
  183. RNG_index += ctx->channels[0].qu_sf_idx[qu] +
  184. ctx->channels[1].qu_sf_idx[qu];
  185. for (sb = 0; sb < ctx->num_coded_subbands; sb++, RNG_index += 128)
  186. sb_RNG_index[sb] = RNG_index & 0x3FC;
  187. /* inverse quant and power compensation */
  188. for (ch = 0; ch < num_channels; ch++) {
  189. /* clear channel's residual spectrum */
  190. memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
  191. for (qu = 0; qu < ctx->used_quant_units; qu++) {
  192. src = &ctx->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
  193. dst = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
  194. nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
  195. ff_atrac3p_qu_to_spec_pos[qu];
  196. if (ctx->channels[ch].qu_wordlen[qu] > 0) {
  197. q = ff_atrac3p_sf_tab[ctx->channels[ch].qu_sf_idx[qu]] *
  198. ff_atrac3p_mant_tab[ctx->channels[ch].qu_wordlen[qu]];
  199. for (i = 0; i < nspeclines; i++)
  200. dst[i] = src[i] * q;
  201. }
  202. }
  203. for (sb = 0; sb < ctx->num_coded_subbands; sb++)
  204. ff_atrac3p_power_compensation(ctx, ch, &out[ch][0],
  205. sb_RNG_index[sb], sb);
  206. }
  207. if (ctx->unit_type == CH_UNIT_STEREO) {
  208. for (sb = 0; sb < ctx->num_coded_subbands; sb++) {
  209. if (ctx->swap_channels[sb]) {
  210. for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
  211. FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
  212. out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
  213. }
  214. /* flip coefficients' sign if requested */
  215. if (ctx->negate_coeffs[sb])
  216. for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
  217. out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
  218. }
  219. }
  220. }
  221. static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
  222. int num_channels, AVCodecContext *avctx)
  223. {
  224. int ch, sb;
  225. for (ch = 0; ch < num_channels; ch++) {
  226. for (sb = 0; sb < ch_unit->num_subbands; sb++) {
  227. /* inverse transform and windowing */
  228. ff_atrac3p_imdct(ctx->fdsp, &ctx->mdct_ctx,
  229. &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  230. &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  231. (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
  232. ch_unit->channels[ch].wnd_shape[sb], sb);
  233. /* gain compensation and overlapping */
  234. ff_atrac_gain_compensation(&ctx->gainc_ctx,
  235. &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  236. &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  237. &ch_unit->channels[ch].gain_data_prev[sb],
  238. &ch_unit->channels[ch].gain_data[sb],
  239. ATRAC3P_SUBBAND_SAMPLES,
  240. &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
  241. }
  242. /* zero unused subbands in both output and overlapping buffers */
  243. memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
  244. 0,
  245. (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
  246. ATRAC3P_SUBBAND_SAMPLES *
  247. sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
  248. memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
  249. 0,
  250. (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
  251. ATRAC3P_SUBBAND_SAMPLES *
  252. sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
  253. /* resynthesize and add tonal signal */
  254. if (ch_unit->waves_info->tones_present ||
  255. ch_unit->waves_info_prev->tones_present) {
  256. for (sb = 0; sb < ch_unit->num_subbands; sb++)
  257. if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
  258. ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
  259. ff_atrac3p_generate_tones(ch_unit, ctx->fdsp, ch, sb,
  260. &ctx->time_buf[ch][sb * 128]);
  261. }
  262. }
  263. /* subband synthesis and acoustic signal output */
  264. ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
  265. &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
  266. }
  267. /* swap window shape and gain control buffers. */
  268. for (ch = 0; ch < num_channels; ch++) {
  269. FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
  270. ch_unit->channels[ch].wnd_shape_prev);
  271. FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
  272. ch_unit->channels[ch].gain_data_prev);
  273. FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
  274. ch_unit->channels[ch].tones_info_prev);
  275. }
  276. FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
  277. }
  278. static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
  279. int *got_frame_ptr, AVPacket *avpkt)
  280. {
  281. ATRAC3PContext *ctx = avctx->priv_data;
  282. AVFrame *frame = data;
  283. int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
  284. float **samples_p = (float **)frame->extended_data;
  285. frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
  286. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  287. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  288. return ret;
  289. }
  290. if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0)
  291. return ret;
  292. if (get_bits1(&ctx->gb)) {
  293. av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
  294. return AVERROR_INVALIDDATA;
  295. }
  296. while (get_bits_left(&ctx->gb) >= 2 &&
  297. (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) {
  298. if (ch_unit_id == CH_UNIT_EXTENSION) {
  299. avpriv_report_missing_feature(avctx, "Channel unit extension");
  300. return AVERROR_PATCHWELCOME;
  301. }
  302. if (ch_block >= ctx->num_channel_blocks ||
  303. ctx->channel_blocks[ch_block] != ch_unit_id) {
  304. av_log(avctx, AV_LOG_ERROR,
  305. "Frame data doesn't match channel configuration!\n");
  306. return AVERROR_INVALIDDATA;
  307. }
  308. ctx->ch_units[ch_block].unit_type = ch_unit_id;
  309. channels_to_process = ch_unit_id + 1;
  310. if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb,
  311. &ctx->ch_units[ch_block],
  312. channels_to_process,
  313. avctx)) < 0)
  314. return ret;
  315. decode_residual_spectrum(&ctx->ch_units[ch_block], ctx->samples,
  316. channels_to_process, avctx);
  317. reconstruct_frame(ctx, &ctx->ch_units[ch_block],
  318. channels_to_process, avctx);
  319. for (i = 0; i < channels_to_process; i++)
  320. memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
  321. ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
  322. ch_block++;
  323. out_ch_index += channels_to_process;
  324. }
  325. *got_frame_ptr = 1;
  326. return avctx->block_align;
  327. }
  328. AVCodec ff_atrac3p_decoder = {
  329. .name = "atrac3plus",
  330. .long_name = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
  331. .type = AVMEDIA_TYPE_AUDIO,
  332. .id = AV_CODEC_ID_ATRAC3P,
  333. .priv_data_size = sizeof(ATRAC3PContext),
  334. .init = atrac3p_decode_init,
  335. .close = atrac3p_decode_close,
  336. .decode = atrac3p_decode_frame,
  337. };