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.

411 lines
14KB

  1. /*
  2. * Atrac 1 compatible decoder
  3. * Copyright (c) 2009 Maxim Poliakovski
  4. * Copyright (c) 2009 Benjamin Larsson
  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 libavcodec/atrac1.c
  24. * Atrac 1 compatible decoder.
  25. * This decoder handles raw ATRAC1 data.
  26. */
  27. /* Many thanks to Tim Craig for all the help! */
  28. #include <math.h>
  29. #include <stddef.h>
  30. #include <stdio.h>
  31. #include "avcodec.h"
  32. #include "get_bits.h"
  33. #include "dsputil.h"
  34. #include "atrac.h"
  35. #include "atrac1data.h"
  36. #define AT1_MAX_BFU 52 ///< max number of block floating units in a sound unit
  37. #define AT1_SU_SIZE 212 ///< number of bytes in a sound unit
  38. #define AT1_SU_SAMPLES 512 ///< number of samples in a sound unit
  39. #define AT1_FRAME_SIZE AT1_SU_SIZE * 2
  40. #define AT1_SU_MAX_BITS AT1_SU_SIZE * 8
  41. #define AT1_MAX_CHANNELS 2
  42. #define AT1_QMF_BANDS 3
  43. #define IDX_LOW_BAND 0
  44. #define IDX_MID_BAND 1
  45. #define IDX_HIGH_BAND 2
  46. /**
  47. * Sound unit struct, one unit is used per channel
  48. */
  49. typedef struct {
  50. int log2_block_count[AT1_QMF_BANDS]; ///< log2 number of blocks in a band
  51. int num_bfus; ///< number of Block Floating Units
  52. int idwls[AT1_MAX_BFU]; ///< the word length indexes for each BFU
  53. int idsfs[AT1_MAX_BFU]; ///< the scalefactor indexes for each BFU
  54. float* spectrum[2];
  55. DECLARE_ALIGNED_16(float,spec1[AT1_SU_SAMPLES]); ///< mdct buffer
  56. DECLARE_ALIGNED_16(float,spec2[AT1_SU_SAMPLES]); ///< mdct buffer
  57. DECLARE_ALIGNED_16(float,fst_qmf_delay[46]); ///< delay line for the 1st stacked QMF filter
  58. DECLARE_ALIGNED_16(float,snd_qmf_delay[46]); ///< delay line for the 2nd stacked QMF filter
  59. DECLARE_ALIGNED_16(float,last_qmf_delay[256+23]); ///< delay line for the last stacked QMF filter
  60. } AT1SUCtx;
  61. /**
  62. * The atrac1 context, holds all needed parameters for decoding
  63. */
  64. typedef struct {
  65. AT1SUCtx SUs[AT1_MAX_CHANNELS]; ///< channel sound unit
  66. DECLARE_ALIGNED_16(float,spec[AT1_SU_SAMPLES]); ///< the mdct spectrum buffer
  67. DECLARE_ALIGNED_16(float,short_buf[64]); ///< buffer for the short mode
  68. DECLARE_ALIGNED_16(float, low[256]);
  69. DECLARE_ALIGNED_16(float, mid[256]);
  70. DECLARE_ALIGNED_16(float,high[512]);
  71. float* bands[3];
  72. float out_samples[AT1_MAX_CHANNELS][AT1_SU_SAMPLES];
  73. MDCTContext mdct_ctx[3];
  74. int channels;
  75. DSPContext dsp;
  76. } AT1Ctx;
  77. static float *short_window;
  78. static float *mid_window;
  79. DECLARE_ALIGNED_16(static float, long_window[256]);
  80. static float *window_per_band[3];
  81. /** size of the transform in samples in the long mode for each QMF band */
  82. static const uint16_t samples_per_band[3] = {128, 128, 256};
  83. static const uint8_t mdct_long_nbits[3] = {7, 7, 8};
  84. static void at1_imdct(AT1Ctx *q, float *spec, float *out, int nbits,
  85. int rev_spec)
  86. {
  87. MDCTContext* mdct_context;
  88. int transf_size = 1 << nbits;
  89. mdct_context = &q->mdct_ctx[nbits - 5 - (nbits>6)];
  90. if (rev_spec) {
  91. int i;
  92. for (i=0 ; i<transf_size/2 ; i++)
  93. FFSWAP(float, spec[i], spec[transf_size - 1 - i]);
  94. }
  95. ff_imdct_half(mdct_context, out, spec);
  96. }
  97. static int at1_imdct_block(AT1SUCtx* su, AT1Ctx *q)
  98. {
  99. int band_num, band_samples, log2_block_count, nbits, num_blocks, block_size;
  100. unsigned int start_pos, ref_pos=0, pos = 0;
  101. for (band_num=0 ; band_num<AT1_QMF_BANDS ; band_num++) {
  102. band_samples = samples_per_band[band_num];
  103. log2_block_count = su->log2_block_count[band_num];
  104. /* number of mdct blocks in the current QMF band: 1 - for long mode */
  105. /* 4 for short mode(low/middle bands) and 8 for short mode(high band)*/
  106. num_blocks = 1 << log2_block_count;
  107. /* mdct block size in samples: 128 (long mode, low & mid bands), */
  108. /* 256 (long mode, high band) and 32 (short mode, all bands) */
  109. block_size = band_samples >> log2_block_count;
  110. /* calc transform size in bits according to the block_size_mode */
  111. nbits = mdct_long_nbits[band_num] - log2_block_count;
  112. if (nbits!=5 && nbits!=7 && nbits!=8)
  113. return -1;
  114. if (num_blocks == 1) {
  115. at1_imdct(q, &q->spec[pos], &su->spectrum[0][ref_pos], nbits, band_num);
  116. pos += block_size; // move to the next mdct block in the spectrum
  117. } else {
  118. /* calc start position for the 1st short block: 96(128) or 112(256) */
  119. start_pos = (band_samples * (num_blocks - 1)) >> (log2_block_count + 1);
  120. memset(&su->spectrum[0][ref_pos], 0, sizeof(float) * (band_samples * 2));
  121. for (; num_blocks!=0 ; num_blocks--) {
  122. /* use hardcoded nbits for the short mode */
  123. at1_imdct(q, &q->spec[pos], q->short_buf, 5, band_num);
  124. /* overlap and window between short blocks */
  125. q->dsp.vector_fmul_window(&su->spectrum[0][ref_pos+start_pos],
  126. &su->spectrum[0][ref_pos+start_pos],
  127. q->short_buf,short_window, 0, 16);
  128. start_pos += 32; // use hardcoded block_size
  129. pos += 32;
  130. }
  131. }
  132. /* overlap and window with the previous frame and output the result */
  133. q->dsp.vector_fmul_window(q->bands[band_num], &su->spectrum[1][ref_pos+band_samples/2],
  134. &su->spectrum[0][ref_pos], window_per_band[band_num], 0, band_samples/2);
  135. ref_pos += band_samples;
  136. }
  137. /* Swap buffers so the mdct overlap works */
  138. FFSWAP(float*, su->spectrum[0], su->spectrum[1]);
  139. return 0;
  140. }
  141. /**
  142. * Parse the block size mode byte
  143. */
  144. static int at1_parse_bsm(GetBitContext* gb, int log2_block_cnt[AT1_QMF_BANDS])
  145. {
  146. int log2_block_count_tmp, i;
  147. for(i=0 ; i<2 ; i++) {
  148. /* low and mid band */
  149. log2_block_count_tmp = get_bits(gb, 2);
  150. if (log2_block_count_tmp & 1)
  151. return -1;
  152. log2_block_cnt[i] = 2 - log2_block_count_tmp;
  153. }
  154. /* high band */
  155. log2_block_count_tmp = get_bits(gb, 2);
  156. if (log2_block_count_tmp != 0 && log2_block_count_tmp != 3)
  157. return -1;
  158. log2_block_cnt[IDX_HIGH_BAND] = 3 - log2_block_count_tmp;
  159. skip_bits(gb, 2);
  160. return 0;
  161. }
  162. static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su,
  163. float spec[AT1_SU_SAMPLES])
  164. {
  165. int bits_used, band_num, bfu_num, i;
  166. /* parse the info byte (2nd byte) telling how much BFUs were coded */
  167. su->num_bfus = bfu_amount_tab1[get_bits(gb, 3)];
  168. /* calc number of consumed bits:
  169. num_BFUs * (idwl(4bits) + idsf(6bits)) + log2_block_count(8bits) + info_byte(8bits)
  170. + info_byte_copy(8bits) + log2_block_count_copy(8bits) */
  171. bits_used = su->num_bfus * 10 + 32 +
  172. bfu_amount_tab2[get_bits(gb, 2)] +
  173. (bfu_amount_tab3[get_bits(gb, 3)] << 1);
  174. /* get word length index (idwl) for each BFU */
  175. for (i=0 ; i<su->num_bfus ; i++)
  176. su->idwls[i] = get_bits(gb, 4);
  177. /* get scalefactor index (idsf) for each BFU */
  178. for (i=0 ; i<su->num_bfus ; i++)
  179. su->idsfs[i] = get_bits(gb, 6);
  180. /* zero idwl/idsf for empty BFUs */
  181. for (i = su->num_bfus; i < AT1_MAX_BFU; i++)
  182. su->idwls[i] = su->idsfs[i] = 0;
  183. /* read in the spectral data and reconstruct MDCT spectrum of this channel */
  184. for (band_num=0 ; band_num<AT1_QMF_BANDS ; band_num++) {
  185. for (bfu_num=bfu_bands_t[band_num] ; bfu_num<bfu_bands_t[band_num+1] ; bfu_num++) {
  186. int pos;
  187. int num_specs = specs_per_bfu[bfu_num];
  188. int word_len = !!su->idwls[bfu_num] + su->idwls[bfu_num];
  189. float scale_factor = sf_table[su->idsfs[bfu_num]];
  190. bits_used += word_len * num_specs; /* add number of bits consumed by current BFU */
  191. /* check for bitstream overflow */
  192. if (bits_used > AT1_SU_MAX_BITS)
  193. return -1;
  194. /* get the position of the 1st spec according to the block size mode */
  195. pos = su->log2_block_count[band_num] ? bfu_start_short[bfu_num] : bfu_start_long[bfu_num];
  196. if (word_len) {
  197. float max_quant = 1.0 / (float)((1 << (word_len - 1)) - 1);
  198. for (i=0 ; i<num_specs ; i++) {
  199. /* read in a quantized spec and convert it to
  200. * signed int and then inverse quantization
  201. */
  202. spec[pos+i] = get_sbits(gb, word_len) * scale_factor * max_quant;
  203. }
  204. } else { /* word_len = 0 -> empty BFU, zero all specs in the emty BFU */
  205. memset(&spec[pos], 0, num_specs*sizeof(float));
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. void at1_subband_synthesis(AT1Ctx *q, AT1SUCtx* su, float *pOut)
  212. {
  213. float temp[256];
  214. float iqmf_temp[512 + 46];
  215. /* combine low and middle bands */
  216. atrac_iqmf(q->bands[0], q->bands[1], 128, temp, su->fst_qmf_delay, iqmf_temp);
  217. /* delay the signal of the high band by 23 samples */
  218. memcpy( su->last_qmf_delay, &su->last_qmf_delay[256], sizeof(float)*23);
  219. memcpy(&su->last_qmf_delay[23], q->bands[2], sizeof(float)*256);
  220. /* combine (low + middle) and high bands */
  221. atrac_iqmf(temp, su->last_qmf_delay, 256, pOut, su->snd_qmf_delay, iqmf_temp);
  222. }
  223. static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
  224. int *data_size, AVPacket *avpkt)
  225. {
  226. const uint8_t *buf = avpkt->data;
  227. int buf_size = avpkt->size;
  228. AT1Ctx *q = avctx->priv_data;
  229. int ch, ret, i;
  230. GetBitContext gb;
  231. float* samples = data;
  232. if (buf_size < 212 * q->channels) {
  233. av_log(q,AV_LOG_ERROR,"Not enought data to decode!\n");
  234. return -1;
  235. }
  236. for (ch=0 ; ch<q->channels ; ch++) {
  237. AT1SUCtx* su = &q->SUs[ch];
  238. init_get_bits(&gb, &buf[212*ch], 212*8);
  239. /* parse block_size_mode, 1st byte */
  240. ret = at1_parse_bsm(&gb, su->log2_block_count);
  241. if (ret < 0)
  242. return ret;
  243. ret = at1_unpack_dequant(&gb, su, q->spec);
  244. if (ret < 0)
  245. return ret;
  246. ret = at1_imdct_block(su, q);
  247. if (ret < 0)
  248. return ret;
  249. at1_subband_synthesis(q, su, q->out_samples[ch]);
  250. }
  251. /* round, convert to 16bit and interleave */
  252. if (q->channels == 1) {
  253. /* mono */
  254. q->dsp.vector_clipf(samples, q->out_samples[0], -32700.0 / (1<<15),
  255. 32700.0 / (1<<15), AT1_SU_SAMPLES);
  256. } else {
  257. /* stereo */
  258. for (i = 0; i < AT1_SU_SAMPLES; i++) {
  259. samples[i*2] = av_clipf(q->out_samples[0][i], -32700.0 / (1<<15),
  260. 32700.0 / (1<<15));
  261. samples[i*2+1] = av_clipf(q->out_samples[1][i], -32700.0 / (1<<15),
  262. 32700.0 / (1<<15));
  263. }
  264. }
  265. *data_size = q->channels * AT1_SU_SAMPLES * sizeof(*samples);
  266. return avctx->block_align;
  267. }
  268. static av_cold void init_mdct_windows(void)
  269. {
  270. int i;
  271. /** The mid and long windows uses the same sine window splitted
  272. * in the middle and wrapped into zero/one regions as follows:
  273. *
  274. * region of "ones"
  275. * -------------
  276. * /
  277. * / 1st half
  278. * / of the sine
  279. * / window
  280. * ---------/
  281. * zero region
  282. *
  283. * The mid and short windows are subsets of the long window.
  284. */
  285. /* Build "zero" region */
  286. memset(long_window, 0, sizeof(long_window));
  287. /* Build sine window region */
  288. short_window = &long_window[112];
  289. ff_sine_window_init(short_window,32);
  290. /* Build "ones" region */
  291. for (i = 0; i < 112; i++)
  292. long_window[144 + i] = 1.0f;
  293. /* Save the mid window subset start */
  294. mid_window = &long_window[64];
  295. /* Prepare the window table */
  296. window_per_band[0] = mid_window;
  297. window_per_band[1] = mid_window;
  298. window_per_band[2] = long_window;
  299. }
  300. static av_cold int atrac1_decode_init(AVCodecContext *avctx)
  301. {
  302. AT1Ctx *q = avctx->priv_data;
  303. avctx->sample_fmt = SAMPLE_FMT_FLT;
  304. q->channels = avctx->channels;
  305. /* Init the mdct transforms */
  306. ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1<<15));
  307. ff_mdct_init(&q->mdct_ctx[1], 8, 1, -1.0/ (1<<15));
  308. ff_mdct_init(&q->mdct_ctx[2], 9, 1, -1.0/ (1<<15));
  309. init_mdct_windows();
  310. atrac_generate_tables();
  311. dsputil_init(&q->dsp, avctx);
  312. q->bands[0] = q->low;
  313. q->bands[1] = q->mid;
  314. q->bands[2] = q->high;
  315. /* Prepare the mdct overlap buffers */
  316. q->SUs[0].spectrum[0] = q->SUs[0].spec1;
  317. q->SUs[0].spectrum[1] = q->SUs[0].spec2;
  318. q->SUs[1].spectrum[0] = q->SUs[1].spec1;
  319. q->SUs[1].spectrum[1] = q->SUs[1].spec2;
  320. return 0;
  321. }
  322. AVCodec atrac1_decoder = {
  323. .name = "atrac1",
  324. .type = CODEC_TYPE_AUDIO,
  325. .id = CODEC_ID_ATRAC1,
  326. .priv_data_size = sizeof(AT1Ctx),
  327. .init = atrac1_decode_init,
  328. .close = NULL,
  329. .decode = atrac1_decode_frame,
  330. .long_name = NULL_IF_CONFIG_SMALL("Atrac 1 (Adaptive TRansform Acoustic Coding)"),
  331. };