summaryrefslogtreecommitdiff
path: root/apps/codecs/libatrac/atrac3.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libatrac/atrac3.c')
-rw-r--r--apps/codecs/libatrac/atrac3.c1249
1 files changed, 1249 insertions, 0 deletions
diff --git a/apps/codecs/libatrac/atrac3.c b/apps/codecs/libatrac/atrac3.c
new file mode 100644
index 0000000000..a800511397
--- /dev/null
+++ b/apps/codecs/libatrac/atrac3.c
@@ -0,0 +1,1249 @@
1/*
2 * Atrac 3 compatible decoder
3 * Copyright (c) 2006-2008 Maxim Poliakovski
4 * Copyright (c) 2006-2008 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/**
24 * @file libavcodec/atrac3.c
25 * Atrac 3 compatible decoder.
26 * This decoder handles Sony's ATRAC3 data.
27 *
28 * Container formats used to store atrac 3 data:
29 * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
30 *
31 * To use this decoder, a calling application must supply the extradata
32 * bytes provided in the containers above.
33 */
34
35#include <math.h>
36#include <stddef.h>
37#include <stdio.h>
38
39#include "avcodec.h"
40#include "bitstream.h"
41#include "dsputil.h"
42#include "bytestream.h"
43
44#include <stdint.h>
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <fcntl.h>
48#include <unistd.h>
49#include <string.h>
50
51#include "../librm/rm.h"
52#include "atrac3data.h"
53
54#define JOINT_STEREO 0x12
55#define STEREO 0x2
56
57#define AVERROR(...) -1
58
59/* These structures are needed to store the parsed gain control data. */
60typedef struct {
61 int num_gain_data;
62 int levcode[8];
63 int loccode[8];
64} gain_info;
65
66typedef struct {
67 gain_info gBlock[4];
68} gain_block;
69
70typedef struct {
71 int pos;
72 int numCoefs;
73 float coef[8];
74} tonal_component;
75
76typedef struct {
77 int bandsCoded;
78 int numComponents;
79 tonal_component components[64];
80 float prevFrame[1024];
81 int gcBlkSwitch;
82 gain_block gainBlock[2];
83
84 DECLARE_ALIGNED_16(float, spectrum[1024]);
85 DECLARE_ALIGNED_16(float, IMDCT_buf[1024]);
86
87 float delayBuf1[46]; ///<qmf delay buffers
88 float delayBuf2[46];
89 float delayBuf3[46];
90} channel_unit;
91
92typedef struct {
93 GetBitContext gb;
94 //@{
95 /** stream data */
96 int channels;
97 int codingMode;
98 int bit_rate;
99 int sample_rate;
100 int samples_per_channel;
101 int samples_per_frame;
102
103 int bits_per_frame;
104 int bytes_per_frame;
105 int pBs;
106 channel_unit* pUnits;
107 //@}
108 //@{
109 /** joint-stereo related variables */
110 int matrix_coeff_index_prev[4];
111 int matrix_coeff_index_now[4];
112 int matrix_coeff_index_next[4];
113 int weighting_delay[6];
114 //@}
115 //@{
116 /** data buffers */
117 float outSamples[2048];
118 uint8_t* decoded_bytes_buffer;
119 float tempBuf[1070];
120 //@}
121 //@{
122 /** extradata */
123 int atrac3version;
124 int delay;
125 int scrambled_stream;
126 int frame_factor;
127 //@}
128} ATRAC3Context;
129
130static DECLARE_ALIGNED_16(float,mdct_window[512]);
131static float qmf_window[48];
132static VLC spectral_coeff_tab[7];
133static float SFTable[64];
134static float gain_tab1[16];
135static float gain_tab2[31];
136static MDCTContext mdct_ctx;
137static DSPContext dsp;
138
139
140/* quadrature mirror synthesis filter */
141
142/**
143 * Quadrature mirror synthesis filter.
144 *
145 * @param inlo lower part of spectrum
146 * @param inhi higher part of spectrum
147 * @param nIn size of spectrum buffer
148 * @param pOut out buffer
149 * @param delayBuf delayBuf buffer
150 * @param temp temp buffer
151 */
152
153
154static void iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
155{
156 int i, j;
157 float *p1, *p3;
158
159 memcpy(temp, delayBuf, 46*sizeof(float));
160
161 p3 = temp + 46;
162
163 /* loop1 */
164 for(i=0; i<nIn; i+=2){
165 p3[2*i+0] = inlo[i ] + inhi[i ];
166 p3[2*i+1] = inlo[i ] - inhi[i ];
167 p3[2*i+2] = inlo[i+1] + inhi[i+1];
168 p3[2*i+3] = inlo[i+1] - inhi[i+1];
169 }
170
171 /* loop2 */
172 p1 = temp;
173 for (j = nIn; j != 0; j--) {
174 float s1 = 0.0;
175 float s2 = 0.0;
176
177 for (i = 0; i < 48; i += 2) {
178 s1 += p1[i] * qmf_window[i];
179 s2 += p1[i+1] * qmf_window[i+1];
180 }
181
182 pOut[0] = s2;
183 pOut[1] = s1;
184
185 p1 += 2;
186 pOut += 2;
187 }
188
189 /* Update the delay buffer. */
190 memcpy(delayBuf, temp + nIn*2, 46*sizeof(float));
191}
192
193/**
194 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
195 * caused by the reverse spectra of the QMF.
196 *
197 * @param pInput float input
198 * @param pOutput float output
199 * @param odd_band 1 if the band is an odd band
200 */
201
202static void IMLT(float *pInput, float *pOutput, int odd_band)
203{
204 int i;
205
206 if (odd_band) {
207 /**
208 * Reverse the odd bands before IMDCT, this is an effect of the QMF transform
209 * or it gives better compression to do it this way.
210 * FIXME: It should be possible to handle this in ff_imdct_calc
211 * for that to happen a modification of the prerotation step of
212 * all SIMD code and C code is needed.
213 * Or fix the functions before so they generate a pre reversed spectrum.
214 */
215
216 for (i=0; i<128; i++)
217 FFSWAP(float, pInput[i], pInput[255-i]);
218 }
219
220 ff_imdct_calc(&mdct_ctx,pOutput,pInput);
221
222 /* Perform windowing on the output. */
223 dsp.vector_fmul(pOutput,mdct_window,512);
224
225}
226
227
228/**
229 * Atrac 3 indata descrambling, only used for data coming from the rm container
230 *
231 * @param in pointer to 8 bit array of indata
232 * @param bits amount of bits
233 * @param out pointer to 8 bit array of outdata
234 */
235
236static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
237 int i, off;
238 uint32_t c;
239 const uint32_t* buf;
240 uint32_t* obuf = (uint32_t*) out;
241
242#ifdef TEST
243 off = 0; //no check for memory alignment of inbuffer
244#else
245 off = (intptr_t)inbuffer & 3;
246#endif /* TEST */
247 buf = (const uint32_t*) (inbuffer - off);
248
249 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
250 bytes += 3 + off;
251 for (i = 0; i < bytes/4; i++)
252 obuf[i] = c ^ buf[i];
253
254 if (off)
255 av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off);
256
257 return off;
258}
259
260
261static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
262 float enc_window[256];
263 float s;
264 int i;
265
266 /* Generate the mdct window, for details see
267 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
268 for (i=0 ; i<256; i++)
269 enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
270
271 if (!mdct_window[0])
272 for (i=0 ; i<256; i++) {
273 mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
274 mdct_window[511-i] = mdct_window[i];
275 }
276
277 /* Generate the QMF window. */
278 for (i=0 ; i<24; i++) {
279 s = qmf_48tap_half[i] * 2.0;
280 qmf_window[i] = s;
281 qmf_window[47 - i] = s;
282 }
283
284 /* Initialize the MDCT transform. */
285 ff_mdct_init(&mdct_ctx, 9, 1);
286}
287
288/**
289 * Atrac3 uninit, free all allocated memory
290 */
291
292static av_cold int atrac3_decode_close(ATRAC3Context *q)
293{
294 //ATRAC3Context *q = rmctx->priv_data;
295
296 av_free(q->pUnits);
297 av_free(q->decoded_bytes_buffer);
298
299 return 0;
300}
301
302/**
303/ * Mantissa decoding
304 *
305 * @param gb the GetBit context
306 * @param selector what table is the output values coded with
307 * @param codingFlag constant length coding or variable length coding
308 * @param mantissas mantissa output table
309 * @param numCodes amount of values to get
310 */
311
312static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
313{
314 int numBits, cnt, code, huffSymb;
315
316 if (selector == 1)
317 numCodes /= 2;
318
319 if (codingFlag != 0) {
320 /* constant length coding (CLC) */
321 numBits = CLCLengthTab[selector];
322
323 if (selector > 1) {
324 for (cnt = 0; cnt < numCodes; cnt++) {
325 if (numBits)
326 code = get_sbits(gb, numBits);
327 else
328 code = 0;
329 mantissas[cnt] = code;
330 }
331 } else {
332 for (cnt = 0; cnt < numCodes; cnt++) {
333 if (numBits)
334 code = get_bits(gb, numBits); //numBits is always 4 in this case
335 else
336 code = 0;
337 mantissas[cnt*2] = seTab_0[code >> 2];
338 mantissas[cnt*2+1] = seTab_0[code & 3];
339 }
340 }
341 } else {
342 /* variable length coding (VLC) */
343 if (selector != 1) {
344 for (cnt = 0; cnt < numCodes; cnt++) {
345 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
346 huffSymb += 1;
347 code = huffSymb >> 1;
348 if (huffSymb & 1)
349 code = -code;
350 mantissas[cnt] = code;
351 }
352 } else {
353 for (cnt = 0; cnt < numCodes; cnt++) {
354 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
355 mantissas[cnt*2] = decTable1[huffSymb*2];
356 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
357 }
358 }
359 }
360}
361
362/**
363 * Restore the quantized band spectrum coefficients
364 *
365 * @param gb the GetBit context
366 * @param pOut decoded band spectrum
367 * @return outSubbands subband counter, fix for broken specification/files
368 */
369
370static int decodeSpectrum (GetBitContext *gb, float *pOut)
371{
372 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
373 int subband_vlc_index[32], SF_idxs[32];
374 int mantissas[128];
375 float SF;
376
377 numSubbands = get_bits(gb, 5); // number of coded subbands
378 codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
379
380 /* Get the VLC selector table for the subbands, 0 means not coded. */
381 for (cnt = 0; cnt <= numSubbands; cnt++)
382 subband_vlc_index[cnt] = get_bits(gb, 3);
383
384 /* Read the scale factor indexes from the stream. */
385 for (cnt = 0; cnt <= numSubbands; cnt++) {
386 if (subband_vlc_index[cnt] != 0)
387 SF_idxs[cnt] = get_bits(gb, 6);
388 }
389
390 for (cnt = 0; cnt <= numSubbands; cnt++) {
391 first = subbandTab[cnt];
392 last = subbandTab[cnt+1];
393
394 subbWidth = last - first;
395
396 if (subband_vlc_index[cnt] != 0) {
397 /* Decode spectral coefficients for this subband. */
398 /* TODO: This can be done faster is several blocks share the
399 * same VLC selector (subband_vlc_index) */
400 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
401
402 /* Decode the scale factor for this subband. */
403 SF = SFTable[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
404
405 /* Inverse quantize the coefficients. */
406 for (pIn=mantissas ; first<last; first++, pIn++)
407 pOut[first] = *pIn * SF;
408 } else {
409 /* This subband was not coded, so zero the entire subband. */
410 memset(pOut+first, 0, subbWidth*sizeof(float));
411 }
412 }
413
414 /* Clear the subbands that were not coded. */
415 first = subbandTab[cnt];
416 memset(pOut+first, 0, (1024 - first) * sizeof(float));
417 return numSubbands;
418}
419
420/**
421 * Restore the quantized tonal components
422 *
423 * @param gb the GetBit context
424 * @param pComponent tone component
425 * @param numBands amount of coded bands
426 */
427
428static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
429{
430 int i,j,k,cnt;
431 int components, coding_mode_selector, coding_mode, coded_values_per_component;
432 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
433 int band_flags[4], mantissa[8];
434 float *pCoef;
435 float scalefactor;
436 int component_count = 0;
437
438 components = get_bits(gb,5);
439
440 /* no tonal components */
441 if (components == 0)
442 return 0;
443
444 coding_mode_selector = get_bits(gb,2);
445 if (coding_mode_selector == 2)
446 return -1;
447
448 coding_mode = coding_mode_selector & 1;
449
450 for (i = 0; i < components; i++) {
451 for (cnt = 0; cnt <= numBands; cnt++)
452 band_flags[cnt] = get_bits1(gb);
453
454 coded_values_per_component = get_bits(gb,3);
455
456 quant_step_index = get_bits(gb,3);
457 if (quant_step_index <= 1)
458 return -1;
459
460 if (coding_mode_selector == 3)
461 coding_mode = get_bits1(gb);
462
463 for (j = 0; j < (numBands + 1) * 4; j++) {
464 if (band_flags[j >> 2] == 0)
465 continue;
466
467 coded_components = get_bits(gb,3);
468
469 for (k=0; k<coded_components; k++) {
470 sfIndx = get_bits(gb,6);
471 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
472 max_coded_values = 1024 - pComponent[component_count].pos;
473 coded_values = coded_values_per_component + 1;
474 coded_values = FFMIN(max_coded_values,coded_values);
475
476 scalefactor = SFTable[sfIndx] * iMaxQuant[quant_step_index];
477
478 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
479
480 pComponent[component_count].numCoefs = coded_values;
481
482 /* inverse quant */
483 pCoef = pComponent[component_count].coef;
484 for (cnt = 0; cnt < coded_values; cnt++)
485 pCoef[cnt] = mantissa[cnt] * scalefactor;
486
487 component_count++;
488 }
489 }
490 }
491
492 return component_count;
493}
494
495/**
496 * Decode gain parameters for the coded bands
497 *
498 * @param gb the GetBit context
499 * @param pGb the gainblock for the current band
500 * @param numBands amount of coded bands
501 */
502
503static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
504{
505 int i, cf, numData;
506 int *pLevel, *pLoc;
507
508 gain_info *pGain = pGb->gBlock;
509
510 for (i=0 ; i<=numBands; i++)
511 {
512 numData = get_bits(gb,3);
513 pGain[i].num_gain_data = numData;
514 pLevel = pGain[i].levcode;
515 pLoc = pGain[i].loccode;
516
517 for (cf = 0; cf < numData; cf++){
518 pLevel[cf]= get_bits(gb,4);
519 pLoc [cf]= get_bits(gb,5);
520 if(cf && pLoc[cf] <= pLoc[cf-1])
521 return -1;
522 }
523 }
524
525 /* Clear the unused blocks. */
526 for (; i<4 ; i++)
527 pGain[i].num_gain_data = 0;
528
529 return 0;
530}
531
532/**
533 * Apply gain parameters and perform the MDCT overlapping part
534 *
535 * @param pIn input float buffer
536 * @param pPrev previous float buffer to perform overlap against
537 * @param pOut output float buffer
538 * @param pGain1 current band gain info
539 * @param pGain2 next band gain info
540 */
541
542static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
543{
544 /* gain compensation function */
545 float gain1, gain2, gain_inc;
546 int cnt, numdata, nsample, startLoc, endLoc;
547
548
549 if (pGain2->num_gain_data == 0)
550 gain1 = 1.0;
551 else
552 gain1 = gain_tab1[pGain2->levcode[0]];
553
554 if (pGain1->num_gain_data == 0) {
555 for (cnt = 0; cnt < 256; cnt++)
556 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
557 } else {
558 numdata = pGain1->num_gain_data;
559 pGain1->loccode[numdata] = 32;
560 pGain1->levcode[numdata] = 4;
561
562 nsample = 0; // current sample = 0
563
564 for (cnt = 0; cnt < numdata; cnt++) {
565 startLoc = pGain1->loccode[cnt] * 8;
566 endLoc = startLoc + 8;
567
568 gain2 = gain_tab1[pGain1->levcode[cnt]];
569 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
570
571 /* interpolate */
572 for (; nsample < startLoc; nsample++)
573 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
574
575 /* interpolation is done over eight samples */
576 for (; nsample < endLoc; nsample++) {
577 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
578 gain2 *= gain_inc;
579 }
580 }
581
582 for (; nsample < 256; nsample++)
583 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
584 }
585
586 /* Delay for the overlapping part. */
587 memcpy(pPrev, &pIn[256], 256*sizeof(float));
588}
589
590/**
591 * Combine the tonal band spectrum and regular band spectrum
592 * Return position of the last tonal coefficient
593 *
594 * @param pSpectrum output spectrum buffer
595 * @param numComponents amount of tonal components
596 * @param pComponent tonal components for this band
597 */
598
599static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
600{
601 int cnt, i, lastPos = -1;
602 float *pIn, *pOut;
603
604 for (cnt = 0; cnt < numComponents; cnt++){
605 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
606 pIn = pComponent[cnt].coef;
607 pOut = &(pSpectrum[pComponent[cnt].pos]);
608
609 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
610 pOut[i] += pIn[i];
611 }
612
613 return lastPos;
614}
615
616
617#define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
618
619static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
620{
621 int i, band, nsample, s1, s2;
622 float c1, c2;
623 float mc1_l, mc1_r, mc2_l, mc2_r;
624
625 for (i=0,band = 0; band < 4*256; band+=256,i++) {
626 s1 = pPrevCode[i];
627 s2 = pCurrCode[i];
628 nsample = 0;
629
630 if (s1 != s2) {
631 /* Selector value changed, interpolation needed. */
632 mc1_l = matrixCoeffs[s1*2];
633 mc1_r = matrixCoeffs[s1*2+1];
634 mc2_l = matrixCoeffs[s2*2];
635 mc2_r = matrixCoeffs[s2*2+1];
636
637 /* Interpolation is done over the first eight samples. */
638 for(; nsample < 8; nsample++) {
639 c1 = su1[band+nsample];
640 c2 = su2[band+nsample];
641 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
642 su1[band+nsample] = c2;
643 su2[band+nsample] = c1 * 2.0 - c2;
644 }
645 }
646
647 /* Apply the matrix without interpolation. */
648 switch (s2) {
649 case 0: /* M/S decoding */
650 for (; nsample < 256; nsample++) {
651 c1 = su1[band+nsample];
652 c2 = su2[band+nsample];
653 su1[band+nsample] = c2 * 2.0;
654 su2[band+nsample] = (c1 - c2) * 2.0;
655 }
656 break;
657
658 case 1:
659 for (; nsample < 256; nsample++) {
660 c1 = su1[band+nsample];
661 c2 = su2[band+nsample];
662 su1[band+nsample] = (c1 + c2) * 2.0;
663 su2[band+nsample] = c2 * -2.0;
664 }
665 break;
666 case 2:
667 case 3:
668 for (; nsample < 256; nsample++) {
669 c1 = su1[band+nsample];
670 c2 = su2[band+nsample];
671 su1[band+nsample] = c1 + c2;
672 su2[band+nsample] = c1 - c2;
673 }
674 break;
675 default:
676 assert(0);
677 }
678 }
679}
680
681static void getChannelWeights (int indx, int flag, float ch[2]){
682
683 if (indx == 7) {
684 ch[0] = 1.0;
685 ch[1] = 1.0;
686 } else {
687 ch[0] = (float)(indx & 7) / 7.0;
688 ch[1] = sqrt(2 - ch[0]*ch[0]);
689 if(flag)
690 FFSWAP(float, ch[0], ch[1]);
691 }
692}
693
694static void channelWeighting (float *su1, float *su2, int *p3)
695{
696 int band, nsample;
697 /* w[x][y] y=0 is left y=1 is right */
698 float w[2][2];
699
700 if (p3[1] != 7 || p3[3] != 7){
701 getChannelWeights(p3[1], p3[0], w[0]);
702 getChannelWeights(p3[3], p3[2], w[1]);
703
704 for(band = 1; band < 4; band++) {
705 /* scale the channels by the weights */
706 for(nsample = 0; nsample < 8; nsample++) {
707 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
708 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
709 }
710
711 for(; nsample < 256; nsample++) {
712 su1[band*256+nsample] *= w[1][0];
713 su2[band*256+nsample] *= w[1][1];
714 }
715 }
716 }
717}
718
719
720/**
721 * Decode a Sound Unit
722 *
723 * @param gb the GetBit context
724 * @param pSnd the channel unit to be used
725 * @param pOut the decoded samples before IQMF in float representation
726 * @param channelNum channel number
727 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
728 */
729
730
731static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
732{
733 int band, result=0, numSubbands, lastTonal, numBands;
734
735 if (codingMode == JOINT_STEREO && channelNum == 1) {
736 if (get_bits(gb,2) != 3) {
737 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
738 return -1;
739 }
740 } else {
741 if (get_bits(gb,6) != 0x28) {
742 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
743 return -1;
744 }
745 }
746
747 /* number of coded QMF bands */
748 pSnd->bandsCoded = get_bits(gb,2);
749
750 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
751 if (result) return result;
752
753 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
754 if (pSnd->numComponents == -1) return -1;
755
756 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
757
758 /* Merge the decoded spectrum and tonal components. */
759 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
760
761
762 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
763 numBands = (subbandTab[numSubbands] - 1) >> 8;
764 if (lastTonal >= 0)
765 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
766
767
768 /* Reconstruct time domain samples. */
769 for (band=0; band<4; band++) {
770 /* Perform the IMDCT step without overlapping. */
771 if (band <= numBands) {
772 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
773 } else
774 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
775
776 /* gain compensation and overlapping */
777 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
778 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
779 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
780 }
781
782 /* Swap the gain control buffers for the next frame. */
783 pSnd->gcBlkSwitch ^= 1;
784
785 return 0;
786}
787
788/**
789 * Frame handling
790 *
791 * @param q Atrac3 private context
792 * @param databuf the input data
793 */
794
795static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
796{
797 int result, i;
798 float *p1, *p2, *p3, *p4;
799 uint8_t *ptr1;
800
801 if (q->codingMode == JOINT_STEREO) {
802
803 /* channel coupling mode */
804 /* decode Sound Unit 1 */
805 init_get_bits(&q->gb,databuf,q->bits_per_frame);
806
807 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
808 if (result != 0)
809 return (result);
810
811 /* Framedata of the su2 in the joint-stereo mode is encoded in
812 * reverse byte order so we need to swap it first. */
813 if (databuf == q->decoded_bytes_buffer) {
814 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
815 ptr1 = q->decoded_bytes_buffer;
816 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
817 FFSWAP(uint8_t,*ptr1,*ptr2);
818 }
819 } else {
820 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
821 for (i = 0; i < q->bytes_per_frame; i++)
822 q->decoded_bytes_buffer[i] = *ptr2--;
823 }
824
825 /* Skip the sync codes (0xF8). */
826 ptr1 = q->decoded_bytes_buffer;
827 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
828 if (i >= q->bytes_per_frame)
829 return -1;
830 }
831
832
833 /* set the bitstream reader at the start of the second Sound Unit*/
834 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
835
836 /* Fill the Weighting coeffs delay buffer */
837 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
838 q->weighting_delay[4] = get_bits1(&q->gb);
839 q->weighting_delay[5] = get_bits(&q->gb,3);
840
841 for (i = 0; i < 4; i++) {
842 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
843 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
844 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
845 }
846
847 /* Decode Sound Unit 2. */
848 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
849 if (result != 0)
850 return (result);
851
852 /* Reconstruct the channel coefficients. */
853 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
854
855 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
856
857 } else {
858 /* normal stereo mode or mono */
859 /* Decode the channel sound units. */
860 for (i=0 ; i<q->channels ; i++) {
861
862 /* Set the bitstream reader at the start of a channel sound unit. */
863 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
864
865 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
866 if (result != 0)
867 return (result);
868 }
869 }
870
871 /* Apply the iQMF synthesis filter. */
872 p1= q->outSamples;
873 for (i=0 ; i<q->channels ; i++) {
874 p2= p1+256;
875 p3= p2+256;
876 p4= p3+256;
877 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
878 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
879 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
880 p1 +=1024;
881 }
882
883 return 0;
884}
885
886
887/**
888 * Atrac frame decoding
889 *
890 * @param rmctx pointer to the AVCodecContext
891 */
892
893static int atrac3_decode_frame(RMContext *rmctx, ATRAC3Context *q,
894 void *data, int *data_size,
895 const uint8_t *buf, int buf_size) {
896 //ATRAC3Context *q = rmctx->priv_data;
897 int result = 0, i;
898 const uint8_t* databuf;
899 int16_t* samples = data;
900
901 if (buf_size < rmctx->block_align)
902 return buf_size;
903
904 /* Check if we need to descramble and what buffer to pass on. */
905 if (q->scrambled_stream) {
906 decode_bytes(buf, q->decoded_bytes_buffer, rmctx->block_align);
907 databuf = q->decoded_bytes_buffer;
908 } else {
909 databuf = buf;
910 }
911
912 result = decodeFrame(q, databuf);
913
914 if (result != 0) {
915 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
916 return -1;
917 }
918
919 if (q->channels == 1) {
920 /* mono */
921 for (i = 0; i<1024; i++)
922 samples[i] = av_clip_int16(round(q->outSamples[i]));
923 *data_size = 1024 * sizeof(int16_t);
924 } else {
925 /* stereo */
926 for (i = 0; i < 1024; i++) {
927 samples[i*2] = av_clip_int16(round(q->outSamples[i]));
928 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
929 }
930 *data_size = 2048 * sizeof(int16_t);
931 }
932
933 return rmctx->block_align;
934}
935
936
937/**
938 * Atrac3 initialization
939 *
940 * @param rmctx pointer to the RMContext
941 */
942
943static av_cold int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx)
944{
945 int i;
946 const uint8_t *edata_ptr = rmctx->codec_extradata;
947 //ATRAC3Context *q = rmctx->priv_data;
948 static VLC_TYPE atrac3_vlc_table[4096][2];
949 static int vlcs_initialized = 0;
950
951 /* Take data from the AVCodecContext (RM container). */
952 q->sample_rate = rmctx->sample_rate;
953 q->channels = rmctx->nb_channels;
954 q->bit_rate = rmctx->bit_rate;
955 q->bits_per_frame = rmctx->block_align * 8;
956 q->bytes_per_frame = rmctx->block_align;
957
958 /* Take care of the codec-specific extradata. */
959 if (rmctx->extradata_size == 14) {
960 /* Parse the extradata, WAV format */
961 av_log(rmctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown value always 1
962 q->samples_per_channel = bytestream_get_le32(&edata_ptr);
963 q->codingMode = bytestream_get_le16(&edata_ptr);
964 av_log(rmctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
965 q->frame_factor = bytestream_get_le16(&edata_ptr); //Unknown always 1
966 av_log(rmctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown always 0
967
968 /* setup */
969 q->samples_per_frame = 1024 * q->channels;
970 q->atrac3version = 4;
971 q->delay = 0x88E;
972 if (q->codingMode)
973 q->codingMode = JOINT_STEREO;
974 else
975 q->codingMode = STEREO;
976 q->scrambled_stream = 0;
977
978 if ((q->bytes_per_frame == 96*q->channels*q->frame_factor) || (q->bytes_per_frame == 152*q->channels*q->frame_factor) || (q->bytes_per_frame == 192*q->channels*q->frame_factor)) {
979 } else {
980 av_log(rmctx,AV_LOG_ERROR,"Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor);
981 return -1;
982 }
983
984 } else if (rmctx->extradata_size == 10) {
985 /* Parse the extradata, RM format. */
986 q->atrac3version = bytestream_get_be32(&edata_ptr);
987 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
988 q->delay = bytestream_get_be16(&edata_ptr);
989 q->codingMode = bytestream_get_be16(&edata_ptr);
990
991 q->samples_per_channel = q->samples_per_frame / q->channels;
992 q->scrambled_stream = 1;
993
994 } else {
995 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",rmctx->extradata_size);
996 }
997 /* Check the extradata. */
998
999 if (q->atrac3version != 4) {
1000 av_log(rmctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
1001 return -1;
1002 }
1003
1004 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
1005 av_log(rmctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
1006 return -1;
1007 }
1008
1009 if (q->delay != 0x88E) {
1010 av_log(rmctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
1011 return -1;
1012 }
1013
1014 if (q->codingMode == STEREO) {
1015 av_log(rmctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
1016 } else if (q->codingMode == JOINT_STEREO) {
1017 av_log(rmctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
1018 } else {
1019 av_log(rmctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
1020 return -1;
1021 }
1022
1023 if (rmctx->nb_channels <= 0 || rmctx->nb_channels > 2 /*|| ((rmctx->channels * 1024) != q->samples_per_frame)*/) {
1024 av_log(rmctx,AV_LOG_ERROR,"Channel configuration error!\n");
1025 return -1;
1026 }
1027
1028
1029 if(rmctx->block_align >= UINT16_MAX/2)
1030 return -1;
1031
1032 /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
1033 * this is for the bitstream reader. */
1034 if ((q->decoded_bytes_buffer = av_mallocz((rmctx->block_align+(4-rmctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
1035 return AVERROR(ENOMEM);
1036
1037
1038 /* Initialize the VLC tables. */
1039 if (!vlcs_initialized) {
1040 for (i=0 ; i<7 ; i++) {
1041 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
1042 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
1043 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
1044 huff_bits[i], 1, 1,
1045 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
1046 }
1047
1048 vlcs_initialized = 1;
1049
1050 }
1051
1052 init_atrac3_transforms(q);
1053
1054 /* Generate the scale factors. */
1055 for (i=0 ; i<64 ; i++)
1056 SFTable[i] = pow(2.0, (i - 15) / 3.0);
1057
1058 /* Generate gain tables. */
1059 for (i=0 ; i<16 ; i++)
1060 gain_tab1[i] = powf (2.0, (4 - i));
1061
1062 for (i=-15 ; i<16 ; i++)
1063 gain_tab2[i+15] = powf (2.0, i * -0.125);
1064
1065 /* init the joint-stereo decoding data */
1066 q->weighting_delay[0] = 0;
1067 q->weighting_delay[1] = 7;
1068 q->weighting_delay[2] = 0;
1069 q->weighting_delay[3] = 7;
1070 q->weighting_delay[4] = 0;
1071 q->weighting_delay[5] = 7;
1072
1073 for (i=0; i<4; i++) {
1074 q->matrix_coeff_index_prev[i] = 3;
1075 q->matrix_coeff_index_now[i] = 3;
1076 q->matrix_coeff_index_next[i] = 3;
1077 }
1078
1079 dsputil_init(&dsp);
1080
1081 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
1082 if (!q->pUnits) {
1083 av_free(q->decoded_bytes_buffer);
1084 return AVERROR(ENOMEM);
1085 }
1086
1087 return 0;
1088}
1089
1090/***************************************************************
1091 * Following is a test program to convert from atrac/rm to wav *
1092 ***************************************************************/
1093static unsigned char wav_header[44]={
1094 'R','I','F','F',// 0 - ChunkID
1095 0,0,0,0, // 4 - ChunkSize (filesize-8)
1096 'W','A','V','E',// 8 - Format
1097 'f','m','t',' ',// 12 - SubChunkID
1098 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
1099 1,0, // 20 - AudioFormat (1=Uncompressed)
1100 2,0, // 22 - NumChannels
1101 0,0,0,0, // 24 - SampleRate in Hz
1102 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
1103 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
1104 16,0, // 34 - BitsPerSample
1105 'd','a','t','a',// 36 - Subchunk2ID
1106 0,0,0,0 // 40 - Subchunk2Size
1107};
1108
1109int open_wav(char* filename) {
1110 int fd,res;
1111
1112 fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
1113 if (fd >= 0) {
1114 res = write(fd,wav_header,sizeof(wav_header));
1115 }
1116
1117 return(fd);
1118}
1119
1120void close_wav(int fd, RMContext *rmctx, ATRAC3Context *q) {
1121 int x,res;
1122 int filesize;
1123 int bytes_per_sample = 2;
1124 int samples_per_frame = q->samples_per_frame;
1125 int nb_channels = rmctx->nb_channels;
1126 int sample_rate = rmctx->sample_rate;
1127 int nb_frames = rmctx->audio_framesize/rmctx->block_align * rmctx->nb_packets - 2; // first 2 frames have no valid audio; skipped in output
1128
1129 filesize= samples_per_frame*bytes_per_sample*nb_frames +44;
1130 printf("Filesize = %d\n",filesize);
1131
1132 // ChunkSize
1133 x=filesize-8;
1134 wav_header[4]=(x&0xff);
1135 wav_header[5]=(x&0xff00)>>8;
1136 wav_header[6]=(x&0xff0000)>>16;
1137 wav_header[7]=(x&0xff000000)>>24;
1138
1139 // Number of channels
1140 wav_header[22]=nb_channels;
1141
1142 // Samplerate
1143 wav_header[24]=sample_rate&0xff;
1144 wav_header[25]=(sample_rate&0xff00)>>8;
1145 wav_header[26]=(sample_rate&0xff0000)>>16;
1146 wav_header[27]=(sample_rate&0xff000000)>>24;
1147
1148 // ByteRate
1149 x=sample_rate*bytes_per_sample*nb_channels;
1150 wav_header[28]=(x&0xff);
1151 wav_header[29]=(x&0xff00)>>8;
1152 wav_header[30]=(x&0xff0000)>>16;
1153 wav_header[31]=(x&0xff000000)>>24;
1154
1155 // BlockAlign
1156 wav_header[32]=rmctx->block_align;//2*rmctx->nb_channels;
1157
1158 // Bits per sample
1159 wav_header[34]=16;
1160
1161 // Subchunk2Size
1162 x=filesize-44;
1163 wav_header[40]=(x&0xff);
1164 wav_header[41]=(x&0xff00)>>8;
1165 wav_header[42]=(x&0xff0000)>>16;
1166 wav_header[43]=(x&0xff000000)>>24;
1167
1168 lseek(fd,0,SEEK_SET);
1169 res = write(fd,wav_header,sizeof(wav_header));
1170 close(fd);
1171}
1172
1173int main(int argc, char *argv[])
1174{
1175 int fd, fd_dec;
1176 int res, i, datasize = 0;
1177
1178#ifdef DUMP_RAW_FRAMES
1179 char filename[15];
1180 int fd_out;
1181#endif
1182 int16_t outbuf[2048];
1183 uint16_t fs,sps,h;
1184 uint32_t packet_count;
1185 ATRAC3Context q;
1186 RMContext rmctx;
1187 RMPacket pkt;
1188
1189 memset(&q,0,sizeof(ATRAC3Context));
1190 memset(&rmctx,0,sizeof(RMContext));
1191 memset(&pkt,0,sizeof(RMPacket));
1192
1193 if (argc != 2) {
1194 DEBUGF("Incorrect number of arguments\n");
1195 return -1;
1196 }
1197
1198 fd = open(argv[1],O_RDONLY);
1199 if (fd < 0) {
1200 DEBUGF("Error opening file %s\n", argv[1]);
1201 return -1;
1202 }
1203
1204 /* copy the input rm file to a memory buffer */
1205 uint8_t * filebuf = (uint8_t *)calloc((int)filesize(fd),sizeof(uint8_t));
1206 res = read(fd,filebuf,filesize(fd));
1207
1208 fd_dec = open_wav("output.wav");
1209 if (fd_dec < 0) {
1210 DEBUGF("Error creating output file\n");
1211 return -1;
1212 }
1213 res = real_parse_header(fd, &rmctx);
1214 packet_count = rmctx.nb_packets;
1215 rmctx.audio_framesize = rmctx.block_align;
1216 rmctx.block_align = rmctx.sub_packet_size;
1217 fs = rmctx.audio_framesize;
1218 sps= rmctx.block_align;
1219 h = rmctx.sub_packet_h;
1220 atrac3_decode_init(&q,&rmctx);
1221
1222 /* change the buffer pointer to point at the first audio frame */
1223 advance_buffer(&filebuf, rmctx.data_offset + DATA_HEADER_SIZE);
1224 while(packet_count)
1225 {
1226 rm_get_packet(&filebuf, &rmctx, &pkt);
1227 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
1228 {
1229 /* output raw audio frames that are sent to the decoder into separate files */
1230#ifdef DUMP_RAW_FRAMES
1231 snprintf(filename,sizeof(filename),"dump%d.raw",++x);
1232 fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
1233 write(fd_out,pkt.frames[i],sps);
1234 close(fd_out);
1235#endif
1236 if(pkt.length > 0)
1237 res = atrac3_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i] , rmctx.block_align);
1238 rmctx.frame_number++;
1239 res = write(fd_dec,outbuf,datasize);
1240 }
1241 packet_count -= rmctx.audio_pkt_cnt;
1242 rmctx.audio_pkt_cnt = 0;
1243 }
1244 atrac3_decode_close(&q);
1245 close_wav(fd_dec, &rmctx, &q);
1246 close(fd);
1247
1248 return 0;
1249}