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.

388 lines
15KB

  1. /*
  2. * ATRAC3+ compatible decoder
  3. *
  4. * Copyright (c) 2010-2013 Maxim Poliakovski
  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. * 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. av_free(((ATRAC3PContext *)(avctx->priv_data))->ch_units);
  62. return 0;
  63. }
  64. static av_cold int set_channel_params(ATRAC3PContext *ctx,
  65. AVCodecContext *avctx)
  66. {
  67. memset(ctx->channel_blocks, 0, sizeof(ctx->channel_blocks));
  68. switch (avctx->channel_layout) {
  69. case AV_CH_FRONT_LEFT:
  70. case AV_CH_LAYOUT_MONO:
  71. ctx->num_channel_blocks = 1;
  72. ctx->channel_blocks[0] = CH_UNIT_MONO;
  73. break;
  74. case AV_CH_LAYOUT_STEREO:
  75. ctx->num_channel_blocks = 1;
  76. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  77. break;
  78. case AV_CH_LAYOUT_SURROUND:
  79. ctx->num_channel_blocks = 2;
  80. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  81. ctx->channel_blocks[1] = CH_UNIT_MONO;
  82. break;
  83. case AV_CH_LAYOUT_4POINT0:
  84. ctx->num_channel_blocks = 3;
  85. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  86. ctx->channel_blocks[1] = CH_UNIT_MONO;
  87. ctx->channel_blocks[2] = CH_UNIT_MONO;
  88. break;
  89. case AV_CH_LAYOUT_5POINT1_BACK:
  90. ctx->num_channel_blocks = 4;
  91. ctx->channel_blocks[0] = CH_UNIT_STEREO;
  92. ctx->channel_blocks[1] = CH_UNIT_MONO;
  93. ctx->channel_blocks[2] = CH_UNIT_STEREO;
  94. ctx->channel_blocks[3] = CH_UNIT_MONO;
  95. break;
  96. case AV_CH_LAYOUT_6POINT1_BACK:
  97. ctx->num_channel_blocks = 5;
  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. ctx->channel_blocks[4] = CH_UNIT_MONO;
  103. break;
  104. case AV_CH_LAYOUT_7POINT1:
  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_STEREO;
  110. ctx->channel_blocks[4] = CH_UNIT_MONO;
  111. break;
  112. default:
  113. av_log(avctx, AV_LOG_ERROR,
  114. "Unsupported channel layout: %"PRIx64"!\n", avctx->channel_layout);
  115. return AVERROR_INVALIDDATA;
  116. }
  117. return 0;
  118. }
  119. static av_cold int atrac3p_decode_init(AVCodecContext *avctx)
  120. {
  121. ATRAC3PContext *ctx = avctx->priv_data;
  122. int i, ch, ret;
  123. if (!avctx->block_align) {
  124. av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
  125. return AVERROR(EINVAL);
  126. }
  127. avpriv_float_dsp_init(&ctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  128. /* initialize IPQF */
  129. ff_mdct_init(&ctx->ipqf_dct_ctx, 5, 1, 32.0 / 32768.0);
  130. ff_atrac3p_init_imdct(avctx, &ctx->mdct_ctx);
  131. ff_atrac_init_gain_compensation(&ctx->gainc_ctx, 6, 2);
  132. ff_atrac3p_init_wave_synth();
  133. if ((ret = set_channel_params(ctx, avctx)) < 0)
  134. return ret;
  135. ctx->my_channel_layout = avctx->channel_layout;
  136. ctx->ch_units = av_mallocz(sizeof(*ctx->ch_units) *
  137. ctx->num_channel_blocks);
  138. if (!ctx->ch_units) {
  139. atrac3p_decode_close(avctx);
  140. return AVERROR(ENOMEM);
  141. }
  142. for (i = 0; i < ctx->num_channel_blocks; i++) {
  143. for (ch = 0; ch < 2; ch++) {
  144. ctx->ch_units[i].channels[ch].ch_num = ch;
  145. ctx->ch_units[i].channels[ch].wnd_shape = &ctx->ch_units[i].channels[ch].wnd_shape_hist[0][0];
  146. ctx->ch_units[i].channels[ch].wnd_shape_prev = &ctx->ch_units[i].channels[ch].wnd_shape_hist[1][0];
  147. ctx->ch_units[i].channels[ch].gain_data = &ctx->ch_units[i].channels[ch].gain_data_hist[0][0];
  148. ctx->ch_units[i].channels[ch].gain_data_prev = &ctx->ch_units[i].channels[ch].gain_data_hist[1][0];
  149. ctx->ch_units[i].channels[ch].tones_info = &ctx->ch_units[i].channels[ch].tones_info_hist[0][0];
  150. ctx->ch_units[i].channels[ch].tones_info_prev = &ctx->ch_units[i].channels[ch].tones_info_hist[1][0];
  151. }
  152. ctx->ch_units[i].waves_info = &ctx->ch_units[i].wave_synth_hist[0];
  153. ctx->ch_units[i].waves_info_prev = &ctx->ch_units[i].wave_synth_hist[1];
  154. }
  155. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  156. return 0;
  157. }
  158. static void decode_residual_spectrum(Atrac3pChanUnitCtx *ctx,
  159. float out[2][ATRAC3P_FRAME_SAMPLES],
  160. int num_channels,
  161. AVCodecContext *avctx)
  162. {
  163. int i, sb, ch, qu, nspeclines, RNG_index;
  164. float *dst, q;
  165. int16_t *src;
  166. /* calculate RNG table index for each subband */
  167. int sb_RNG_index[ATRAC3P_SUBBANDS] = { 0 };
  168. if (ctx->mute_flag) {
  169. for (ch = 0; ch < num_channels; ch++)
  170. memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
  171. return;
  172. }
  173. for (qu = 0, RNG_index = 0; qu < ctx->used_quant_units; qu++)
  174. RNG_index += ctx->channels[0].qu_sf_idx[qu] +
  175. ctx->channels[1].qu_sf_idx[qu];
  176. for (sb = 0; sb < ctx->num_coded_subbands; sb++, RNG_index += 128)
  177. sb_RNG_index[sb] = RNG_index & 0x3FC;
  178. /* inverse quant and power compensation */
  179. for (ch = 0; ch < num_channels; ch++) {
  180. /* clear channel's residual spectrum */
  181. memset(out[ch], 0, ATRAC3P_FRAME_SAMPLES * sizeof(*out[ch]));
  182. for (qu = 0; qu < ctx->used_quant_units; qu++) {
  183. src = &ctx->channels[ch].spectrum[ff_atrac3p_qu_to_spec_pos[qu]];
  184. dst = &out[ch][ff_atrac3p_qu_to_spec_pos[qu]];
  185. nspeclines = ff_atrac3p_qu_to_spec_pos[qu + 1] -
  186. ff_atrac3p_qu_to_spec_pos[qu];
  187. if (ctx->channels[ch].qu_wordlen[qu] > 0) {
  188. q = ff_atrac3p_sf_tab[ctx->channels[ch].qu_sf_idx[qu]] *
  189. ff_atrac3p_mant_tab[ctx->channels[ch].qu_wordlen[qu]];
  190. for (i = 0; i < nspeclines; i++)
  191. dst[i] = src[i] * q;
  192. }
  193. }
  194. for (sb = 0; sb < ctx->num_coded_subbands; sb++)
  195. ff_atrac3p_power_compensation(ctx, ch, &out[ch][0],
  196. sb_RNG_index[sb], sb);
  197. }
  198. if (ctx->unit_type == CH_UNIT_STEREO) {
  199. for (sb = 0; sb < ctx->num_coded_subbands; sb++) {
  200. if (ctx->swap_channels[sb]) {
  201. for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
  202. FFSWAP(float, out[0][sb * ATRAC3P_SUBBAND_SAMPLES + i],
  203. out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
  204. }
  205. /* flip coefficients' sign if requested */
  206. if (ctx->negate_coeffs[sb])
  207. for (i = 0; i < ATRAC3P_SUBBAND_SAMPLES; i++)
  208. out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i] = -(out[1][sb * ATRAC3P_SUBBAND_SAMPLES + i]);
  209. }
  210. }
  211. }
  212. static void reconstruct_frame(ATRAC3PContext *ctx, Atrac3pChanUnitCtx *ch_unit,
  213. int num_channels, AVCodecContext *avctx)
  214. {
  215. int ch, sb;
  216. for (ch = 0; ch < num_channels; ch++) {
  217. for (sb = 0; sb < ch_unit->num_subbands; sb++) {
  218. /* inverse transform and windowing */
  219. ff_atrac3p_imdct(&ctx->fdsp, &ctx->mdct_ctx,
  220. &ctx->samples[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  221. &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  222. (ch_unit->channels[ch].wnd_shape_prev[sb] << 1) +
  223. ch_unit->channels[ch].wnd_shape[sb], sb);
  224. /* gain compensation and overlapping */
  225. ff_atrac_gain_compensation(&ctx->gainc_ctx,
  226. &ctx->mdct_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  227. &ch_unit->prev_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES],
  228. &ch_unit->channels[ch].gain_data_prev[sb],
  229. &ch_unit->channels[ch].gain_data[sb],
  230. ATRAC3P_SUBBAND_SAMPLES,
  231. &ctx->time_buf[ch][sb * ATRAC3P_SUBBAND_SAMPLES]);
  232. }
  233. /* zero unused subbands in both output and overlapping buffers */
  234. memset(&ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
  235. 0,
  236. (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
  237. ATRAC3P_SUBBAND_SAMPLES *
  238. sizeof(ch_unit->prev_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
  239. memset(&ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES],
  240. 0,
  241. (ATRAC3P_SUBBANDS - ch_unit->num_subbands) *
  242. ATRAC3P_SUBBAND_SAMPLES *
  243. sizeof(ctx->time_buf[ch][ch_unit->num_subbands * ATRAC3P_SUBBAND_SAMPLES]));
  244. /* resynthesize and add tonal signal */
  245. if (ch_unit->waves_info->tones_present ||
  246. ch_unit->waves_info_prev->tones_present) {
  247. for (sb = 0; sb < ch_unit->num_subbands; sb++)
  248. if (ch_unit->channels[ch].tones_info[sb].num_wavs ||
  249. ch_unit->channels[ch].tones_info_prev[sb].num_wavs) {
  250. ff_atrac3p_generate_tones(ch_unit, &ctx->fdsp, ch, sb,
  251. &ctx->time_buf[ch][sb * 128]);
  252. }
  253. }
  254. /* subband synthesis and acoustic signal output */
  255. ff_atrac3p_ipqf(&ctx->ipqf_dct_ctx, &ch_unit->ipqf_ctx[ch],
  256. &ctx->time_buf[ch][0], &ctx->outp_buf[ch][0]);
  257. }
  258. /* swap window shape and gain control buffers. */
  259. for (ch = 0; ch < num_channels; ch++) {
  260. FFSWAP(uint8_t *, ch_unit->channels[ch].wnd_shape,
  261. ch_unit->channels[ch].wnd_shape_prev);
  262. FFSWAP(AtracGainInfo *, ch_unit->channels[ch].gain_data,
  263. ch_unit->channels[ch].gain_data_prev);
  264. FFSWAP(Atrac3pWavesData *, ch_unit->channels[ch].tones_info,
  265. ch_unit->channels[ch].tones_info_prev);
  266. }
  267. FFSWAP(Atrac3pWaveSynthParams *, ch_unit->waves_info, ch_unit->waves_info_prev);
  268. }
  269. static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
  270. int *got_frame_ptr, AVPacket *avpkt)
  271. {
  272. ATRAC3PContext *ctx = avctx->priv_data;
  273. AVFrame *frame = data;
  274. int i, ret, ch_unit_id, ch_block = 0, out_ch_index = 0, channels_to_process;
  275. float **samples_p = (float **)frame->extended_data;
  276. frame->nb_samples = ATRAC3P_FRAME_SAMPLES;
  277. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  278. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  279. return ret;
  280. }
  281. if ((ret = init_get_bits8(&ctx->gb, avpkt->data, avpkt->size)) < 0)
  282. return ret;
  283. if (get_bits1(&ctx->gb)) {
  284. av_log(avctx, AV_LOG_ERROR, "Invalid start bit!\n");
  285. return AVERROR_INVALIDDATA;
  286. }
  287. while (get_bits_left(&ctx->gb) >= 2 &&
  288. (ch_unit_id = get_bits(&ctx->gb, 2)) != CH_UNIT_TERMINATOR) {
  289. if (ch_unit_id == CH_UNIT_EXTENSION) {
  290. avpriv_report_missing_feature(avctx, "Channel unit extension");
  291. return AVERROR_PATCHWELCOME;
  292. }
  293. if (ch_block >= ctx->num_channel_blocks ||
  294. ctx->channel_blocks[ch_block] != ch_unit_id) {
  295. av_log(avctx, AV_LOG_ERROR,
  296. "Frame data doesn't match channel configuration!\n");
  297. return AVERROR_INVALIDDATA;
  298. }
  299. ctx->ch_units[ch_block].unit_type = ch_unit_id;
  300. channels_to_process = ch_unit_id + 1;
  301. if ((ret = ff_atrac3p_decode_channel_unit(&ctx->gb,
  302. &ctx->ch_units[ch_block],
  303. channels_to_process,
  304. avctx)) < 0)
  305. return ret;
  306. decode_residual_spectrum(&ctx->ch_units[ch_block], ctx->samples,
  307. channels_to_process, avctx);
  308. reconstruct_frame(ctx, &ctx->ch_units[ch_block],
  309. channels_to_process, avctx);
  310. for (i = 0; i < channels_to_process; i++)
  311. memcpy(samples_p[out_ch_index + i], ctx->outp_buf[i],
  312. ATRAC3P_FRAME_SAMPLES * sizeof(**samples_p));
  313. ch_block++;
  314. out_ch_index += channels_to_process;
  315. }
  316. *got_frame_ptr = 1;
  317. return avctx->block_align;
  318. }
  319. AVCodec ff_atrac3p_decoder = {
  320. .name = "atrac3plus",
  321. .long_name = NULL_IF_CONFIG_SMALL("ATRAC3+ (Adaptive TRansform Acoustic Coding 3+)"),
  322. .type = AVMEDIA_TYPE_AUDIO,
  323. .id = AV_CODEC_ID_ATRAC3P,
  324. .priv_data_size = sizeof(ATRAC3PContext),
  325. .init = atrac3p_decode_init,
  326. .init_static_data = ff_atrac3p_init_vlcs,
  327. .close = atrac3p_decode_close,
  328. .decode = atrac3p_decode_frame,
  329. };