Mercurial > libavcodec.hg
annotate atrac3.c @ 7459:283eeda62184 libavcodec
Modify av_audio_convert() to use AVAudioConvert context struct; add av_audio_convert_alloc() and av_audio_convert_free() support functions.
author | pross |
---|---|
date | Fri, 01 Aug 2008 13:53:18 +0000 |
parents | 85ab7655ad4d |
children | 97383e012cb9 |
rev | line source |
---|---|
4856 | 1 /* |
2 * Atrac 3 compatible decoder | |
6844 | 3 * Copyright (c) 2006-2008 Maxim Poliakovski |
4 * Copyright (c) 2006-2008 Benjamin Larsson | |
4856 | 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 atrac3.c | |
25 * Atrac 3 compatible decoder. | |
6844 | 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). | |
4856 | 30 * |
31 * To use this decoder, a calling application must supply the extradata | |
6844 | 32 * bytes provided in the containers above. |
4856 | 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 "atrac3data.h" | |
45 | |
46 #define JOINT_STEREO 0x12 | |
47 #define STEREO 0x2 | |
48 | |
49 | |
50 /* These structures are needed to store the parsed gain control data. */ | |
51 typedef struct { | |
52 int num_gain_data; | |
53 int levcode[8]; | |
54 int loccode[8]; | |
55 } gain_info; | |
56 | |
57 typedef struct { | |
58 gain_info gBlock[4]; | |
59 } gain_block; | |
60 | |
61 typedef struct { | |
62 int pos; | |
63 int numCoefs; | |
64 float coef[8]; | |
65 } tonal_component; | |
66 | |
67 typedef struct { | |
68 int bandsCoded; | |
69 int numComponents; | |
70 tonal_component components[64]; | |
71 float prevFrame[1024]; | |
72 int gcBlkSwitch; | |
73 gain_block gainBlock[2]; | |
74 | |
75 DECLARE_ALIGNED_16(float, spectrum[1024]); | |
76 DECLARE_ALIGNED_16(float, IMDCT_buf[1024]); | |
77 | |
78 float delayBuf1[46]; ///<qmf delay buffers | |
79 float delayBuf2[46]; | |
80 float delayBuf3[46]; | |
81 } channel_unit; | |
82 | |
83 typedef struct { | |
84 GetBitContext gb; | |
85 //@{ | |
86 /** stream data */ | |
87 int channels; | |
88 int codingMode; | |
89 int bit_rate; | |
90 int sample_rate; | |
91 int samples_per_channel; | |
92 int samples_per_frame; | |
93 | |
94 int bits_per_frame; | |
95 int bytes_per_frame; | |
96 int pBs; | |
97 channel_unit* pUnits; | |
98 //@} | |
99 //@{ | |
100 /** joint-stereo related variables */ | |
101 int matrix_coeff_index_prev[4]; | |
102 int matrix_coeff_index_now[4]; | |
103 int matrix_coeff_index_next[4]; | |
104 int weighting_delay[6]; | |
105 //@} | |
106 //@{ | |
107 /** data buffers */ | |
108 float outSamples[2048]; | |
109 uint8_t* decoded_bytes_buffer; | |
110 float tempBuf[1070]; | |
111 DECLARE_ALIGNED_16(float,mdct_tmp[512]); | |
112 //@} | |
113 //@{ | |
114 /** extradata */ | |
115 int atrac3version; | |
116 int delay; | |
117 int scrambled_stream; | |
118 int frame_factor; | |
119 //@} | |
120 } ATRAC3Context; | |
121 | |
122 static DECLARE_ALIGNED_16(float,mdct_window[512]); | |
123 static float qmf_window[48]; | |
124 static VLC spectral_coeff_tab[7]; | |
125 static float SFTable[64]; | |
126 static float gain_tab1[16]; | |
127 static float gain_tab2[31]; | |
128 static MDCTContext mdct_ctx; | |
129 static DSPContext dsp; | |
130 | |
131 | |
132 /* quadrature mirror synthesis filter */ | |
133 | |
134 /** | |
135 * Quadrature mirror synthesis filter. | |
136 * | |
137 * @param inlo lower part of spectrum | |
138 * @param inhi higher part of spectrum | |
139 * @param nIn size of spectrum buffer | |
140 * @param pOut out buffer | |
141 * @param delayBuf delayBuf buffer | |
142 * @param temp temp buffer | |
143 */ | |
144 | |
145 | |
146 static void iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp) | |
147 { | |
148 int i, j; | |
149 float *p1, *p3; | |
150 | |
151 memcpy(temp, delayBuf, 46*sizeof(float)); | |
152 | |
153 p3 = temp + 46; | |
154 | |
155 /* loop1 */ | |
156 for(i=0; i<nIn; i+=2){ | |
157 p3[2*i+0] = inlo[i ] + inhi[i ]; | |
158 p3[2*i+1] = inlo[i ] - inhi[i ]; | |
159 p3[2*i+2] = inlo[i+1] + inhi[i+1]; | |
160 p3[2*i+3] = inlo[i+1] - inhi[i+1]; | |
161 } | |
162 | |
163 /* loop2 */ | |
164 p1 = temp; | |
165 for (j = nIn; j != 0; j--) { | |
166 float s1 = 0.0; | |
167 float s2 = 0.0; | |
168 | |
169 for (i = 0; i < 48; i += 2) { | |
170 s1 += p1[i] * qmf_window[i]; | |
171 s2 += p1[i+1] * qmf_window[i+1]; | |
172 } | |
173 | |
174 pOut[0] = s2; | |
175 pOut[1] = s1; | |
176 | |
177 p1 += 2; | |
178 pOut += 2; | |
179 } | |
180 | |
181 /* Update the delay buffer. */ | |
182 memcpy(delayBuf, temp + nIn*2, 46*sizeof(float)); | |
183 } | |
184 | |
185 /** | |
186 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands | |
187 * caused by the reverse spectra of the QMF. | |
188 * | |
189 * @param pInput float input | |
190 * @param pOutput float output | |
191 * @param odd_band 1 if the band is an odd band | |
192 * @param mdct_tmp aligned temporary buffer for the mdct | |
193 */ | |
194 | |
195 static void IMLT(float *pInput, float *pOutput, int odd_band, float* mdct_tmp) | |
196 { | |
197 int i; | |
198 | |
199 if (odd_band) { | |
200 /** | |
201 * Reverse the odd bands before IMDCT, this is an effect of the QMF transform | |
202 * or it gives better compression to do it this way. | |
203 * FIXME: It should be possible to handle this in ff_imdct_calc | |
204 * for that to happen a modification of the prerotation step of | |
205 * all SIMD code and C code is needed. | |
206 * Or fix the functions before so they generate a pre reversed spectrum. | |
207 */ | |
208 | |
209 for (i=0; i<128; i++) | |
210 FFSWAP(float, pInput[i], pInput[255-i]); | |
211 } | |
212 | |
213 mdct_ctx.fft.imdct_calc(&mdct_ctx,pOutput,pInput,mdct_tmp); | |
214 | |
215 /* Perform windowing on the output. */ | |
216 dsp.vector_fmul(pOutput,mdct_window,512); | |
217 | |
218 } | |
219 | |
220 | |
221 /** | |
222 * Atrac 3 indata descrambling, only used for data coming from the rm container | |
223 * | |
224 * @param in pointer to 8 bit array of indata | |
225 * @param bits amount of bits | |
226 * @param out pointer to 8 bit array of outdata | |
227 */ | |
228 | |
6228 | 229 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ |
4856 | 230 int i, off; |
231 uint32_t c; | |
6228 | 232 const uint32_t* buf; |
4856 | 233 uint32_t* obuf = (uint32_t*) out; |
234 | |
235 off = (int)((long)inbuffer & 3); | |
6228 | 236 buf = (const uint32_t*) (inbuffer - off); |
4856 | 237 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8)))); |
238 bytes += 3 + off; | |
239 for (i = 0; i < bytes/4; i++) | |
240 obuf[i] = c ^ buf[i]; | |
241 | |
242 if (off) | |
243 av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off); | |
244 | |
245 return off; | |
246 } | |
247 | |
248 | |
249 static void init_atrac3_transforms(ATRAC3Context *q) { | |
250 float enc_window[256]; | |
251 float s; | |
252 int i; | |
253 | |
254 /* Generate the mdct window, for details see | |
255 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */ | |
256 for (i=0 ; i<256; i++) | |
257 enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5; | |
258 | |
259 if (!mdct_window[0]) | |
260 for (i=0 ; i<256; i++) { | |
261 mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]); | |
262 mdct_window[511-i] = mdct_window[i]; | |
263 } | |
264 | |
265 /* Generate the QMF window. */ | |
266 for (i=0 ; i<24; i++) { | |
267 s = qmf_48tap_half[i] * 2.0; | |
268 qmf_window[i] = s; | |
269 qmf_window[47 - i] = s; | |
270 } | |
271 | |
272 /* Initialize the MDCT transform. */ | |
273 ff_mdct_init(&mdct_ctx, 9, 1); | |
274 } | |
275 | |
276 /** | |
277 * Atrac3 uninit, free all allocated memory | |
278 */ | |
279 | |
280 static int atrac3_decode_close(AVCodecContext *avctx) | |
281 { | |
282 ATRAC3Context *q = avctx->priv_data; | |
283 | |
284 av_free(q->pUnits); | |
285 av_free(q->decoded_bytes_buffer); | |
286 | |
287 return 0; | |
288 } | |
289 | |
290 /** | |
291 / * Mantissa decoding | |
292 * | |
293 * @param gb the GetBit context | |
294 * @param selector what table is the output values coded with | |
295 * @param codingFlag constant length coding or variable length coding | |
296 * @param mantissas mantissa output table | |
297 * @param numCodes amount of values to get | |
298 */ | |
299 | |
300 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes) | |
301 { | |
302 int numBits, cnt, code, huffSymb; | |
303 | |
304 if (selector == 1) | |
305 numCodes /= 2; | |
306 | |
307 if (codingFlag != 0) { | |
308 /* constant length coding (CLC) */ | |
309 numBits = CLCLengthTab[selector]; | |
310 | |
311 if (selector > 1) { | |
312 for (cnt = 0; cnt < numCodes; cnt++) { | |
313 if (numBits) | |
314 code = get_sbits(gb, numBits); | |
315 else | |
316 code = 0; | |
317 mantissas[cnt] = code; | |
318 } | |
319 } else { | |
320 for (cnt = 0; cnt < numCodes; cnt++) { | |
321 if (numBits) | |
322 code = get_bits(gb, numBits); //numBits is always 4 in this case | |
323 else | |
324 code = 0; | |
325 mantissas[cnt*2] = seTab_0[code >> 2]; | |
326 mantissas[cnt*2+1] = seTab_0[code & 3]; | |
327 } | |
328 } | |
329 } else { | |
330 /* variable length coding (VLC) */ | |
331 if (selector != 1) { | |
332 for (cnt = 0; cnt < numCodes; cnt++) { | |
333 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3); | |
334 huffSymb += 1; | |
335 code = huffSymb >> 1; | |
336 if (huffSymb & 1) | |
337 code = -code; | |
338 mantissas[cnt] = code; | |
339 } | |
340 } else { | |
341 for (cnt = 0; cnt < numCodes; cnt++) { | |
342 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3); | |
343 mantissas[cnt*2] = decTable1[huffSymb*2]; | |
344 mantissas[cnt*2+1] = decTable1[huffSymb*2+1]; | |
345 } | |
346 } | |
347 } | |
348 } | |
349 | |
350 /** | |
351 * Restore the quantized band spectrum coefficients | |
352 * | |
353 * @param gb the GetBit context | |
354 * @param pOut decoded band spectrum | |
355 * @return outSubbands subband counter, fix for broken specification/files | |
356 */ | |
357 | |
358 static int decodeSpectrum (GetBitContext *gb, float *pOut) | |
359 { | |
360 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn; | |
361 int subband_vlc_index[32], SF_idxs[32]; | |
362 int mantissas[128]; | |
363 float SF; | |
364 | |
365 numSubbands = get_bits(gb, 5); // number of coded subbands | |
5513 | 366 codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC |
4856 | 367 |
368 /* Get the VLC selector table for the subbands, 0 means not coded. */ | |
369 for (cnt = 0; cnt <= numSubbands; cnt++) | |
370 subband_vlc_index[cnt] = get_bits(gb, 3); | |
371 | |
372 /* Read the scale factor indexes from the stream. */ | |
373 for (cnt = 0; cnt <= numSubbands; cnt++) { | |
374 if (subband_vlc_index[cnt] != 0) | |
375 SF_idxs[cnt] = get_bits(gb, 6); | |
376 } | |
377 | |
378 for (cnt = 0; cnt <= numSubbands; cnt++) { | |
379 first = subbandTab[cnt]; | |
380 last = subbandTab[cnt+1]; | |
381 | |
382 subbWidth = last - first; | |
383 | |
384 if (subband_vlc_index[cnt] != 0) { | |
385 /* Decode spectral coefficients for this subband. */ | |
386 /* TODO: This can be done faster is several blocks share the | |
387 * same VLC selector (subband_vlc_index) */ | |
388 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth); | |
389 | |
390 /* Decode the scale factor for this subband. */ | |
391 SF = SFTable[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]]; | |
392 | |
393 /* Inverse quantize the coefficients. */ | |
394 for (pIn=mantissas ; first<last; first++, pIn++) | |
395 pOut[first] = *pIn * SF; | |
396 } else { | |
397 /* This subband was not coded, so zero the entire subband. */ | |
398 memset(pOut+first, 0, subbWidth*sizeof(float)); | |
399 } | |
400 } | |
401 | |
402 /* Clear the subbands that were not coded. */ | |
403 first = subbandTab[cnt]; | |
404 memset(pOut+first, 0, (1024 - first) * sizeof(float)); | |
405 return numSubbands; | |
406 } | |
407 | |
408 /** | |
409 * Restore the quantized tonal components | |
410 * | |
411 * @param gb the GetBit context | |
412 * @param pComponent tone component | |
413 * @param numBands amount of coded bands | |
414 */ | |
415 | |
4865 | 416 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands) |
4856 | 417 { |
418 int i,j,k,cnt; | |
4865 | 419 int components, coding_mode_selector, coding_mode, coded_values_per_component; |
4856 | 420 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components; |
421 int band_flags[4], mantissa[8]; | |
422 float *pCoef; | |
423 float scalefactor; | |
4865 | 424 int component_count = 0; |
4856 | 425 |
426 components = get_bits(gb,5); | |
427 | |
428 /* no tonal components */ | |
429 if (components == 0) | |
430 return 0; | |
431 | |
432 coding_mode_selector = get_bits(gb,2); | |
433 if (coding_mode_selector == 2) | |
434 return -1; | |
435 | |
436 coding_mode = coding_mode_selector & 1; | |
437 | |
438 for (i = 0; i < components; i++) { | |
439 for (cnt = 0; cnt <= numBands; cnt++) | |
440 band_flags[cnt] = get_bits1(gb); | |
441 | |
442 coded_values_per_component = get_bits(gb,3); | |
443 | |
444 quant_step_index = get_bits(gb,3); | |
445 if (quant_step_index <= 1) | |
446 return -1; | |
447 | |
448 if (coding_mode_selector == 3) | |
449 coding_mode = get_bits1(gb); | |
450 | |
451 for (j = 0; j < (numBands + 1) * 4; j++) { | |
452 if (band_flags[j >> 2] == 0) | |
453 continue; | |
454 | |
455 coded_components = get_bits(gb,3); | |
456 | |
457 for (k=0; k<coded_components; k++) { | |
458 sfIndx = get_bits(gb,6); | |
459 pComponent[component_count].pos = j * 64 + (get_bits(gb,6)); | |
460 max_coded_values = 1024 - pComponent[component_count].pos; | |
461 coded_values = coded_values_per_component + 1; | |
462 coded_values = FFMIN(max_coded_values,coded_values); | |
463 | |
464 scalefactor = SFTable[sfIndx] * iMaxQuant[quant_step_index]; | |
465 | |
466 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values); | |
467 | |
468 pComponent[component_count].numCoefs = coded_values; | |
469 | |
470 /* inverse quant */ | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
471 pCoef = pComponent[component_count].coef; |
4856 | 472 for (cnt = 0; cnt < coded_values; cnt++) |
473 pCoef[cnt] = mantissa[cnt] * scalefactor; | |
474 | |
475 component_count++; | |
476 } | |
477 } | |
478 } | |
479 | |
4865 | 480 return component_count; |
4856 | 481 } |
482 | |
483 /** | |
484 * Decode gain parameters for the coded bands | |
485 * | |
486 * @param gb the GetBit context | |
487 * @param pGb the gainblock for the current band | |
488 * @param numBands amount of coded bands | |
489 */ | |
490 | |
491 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands) | |
492 { | |
493 int i, cf, numData; | |
494 int *pLevel, *pLoc; | |
495 | |
496 gain_info *pGain = pGb->gBlock; | |
497 | |
498 for (i=0 ; i<=numBands; i++) | |
499 { | |
500 numData = get_bits(gb,3); | |
501 pGain[i].num_gain_data = numData; | |
502 pLevel = pGain[i].levcode; | |
503 pLoc = pGain[i].loccode; | |
504 | |
505 for (cf = 0; cf < numData; cf++){ | |
506 pLevel[cf]= get_bits(gb,4); | |
507 pLoc [cf]= get_bits(gb,5); | |
508 if(cf && pLoc[cf] <= pLoc[cf-1]) | |
509 return -1; | |
510 } | |
511 } | |
512 | |
513 /* Clear the unused blocks. */ | |
514 for (; i<4 ; i++) | |
515 pGain[i].num_gain_data = 0; | |
516 | |
517 return 0; | |
518 } | |
519 | |
520 /** | |
521 * Apply gain parameters and perform the MDCT overlapping part | |
522 * | |
523 * @param pIn input float buffer | |
524 * @param pPrev previous float buffer to perform overlap against | |
525 * @param pOut output float buffer | |
526 * @param pGain1 current band gain info | |
527 * @param pGain2 next band gain info | |
528 */ | |
529 | |
530 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2) | |
531 { | |
532 /* gain compensation function */ | |
533 float gain1, gain2, gain_inc; | |
534 int cnt, numdata, nsample, startLoc, endLoc; | |
535 | |
536 | |
537 if (pGain2->num_gain_data == 0) | |
538 gain1 = 1.0; | |
539 else | |
540 gain1 = gain_tab1[pGain2->levcode[0]]; | |
541 | |
542 if (pGain1->num_gain_data == 0) { | |
543 for (cnt = 0; cnt < 256; cnt++) | |
544 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt]; | |
545 } else { | |
546 numdata = pGain1->num_gain_data; | |
547 pGain1->loccode[numdata] = 32; | |
548 pGain1->levcode[numdata] = 4; | |
549 | |
550 nsample = 0; // current sample = 0 | |
551 | |
552 for (cnt = 0; cnt < numdata; cnt++) { | |
553 startLoc = pGain1->loccode[cnt] * 8; | |
554 endLoc = startLoc + 8; | |
555 | |
556 gain2 = gain_tab1[pGain1->levcode[cnt]]; | |
557 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15]; | |
558 | |
559 /* interpolate */ | |
560 for (; nsample < startLoc; nsample++) | |
561 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2; | |
562 | |
563 /* interpolation is done over eight samples */ | |
564 for (; nsample < endLoc; nsample++) { | |
565 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2; | |
566 gain2 *= gain_inc; | |
567 } | |
568 } | |
569 | |
570 for (; nsample < 256; nsample++) | |
571 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample]; | |
572 } | |
573 | |
574 /* Delay for the overlapping part. */ | |
575 memcpy(pPrev, &pIn[256], 256*sizeof(float)); | |
576 } | |
577 | |
578 /** | |
579 * Combine the tonal band spectrum and regular band spectrum | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
580 * Return position of the last tonal coefficient |
4856 | 581 * |
582 * @param pSpectrum output spectrum buffer | |
583 * @param numComponents amount of tonal components | |
584 * @param pComponent tonal components for this band | |
585 */ | |
586 | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
587 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent) |
4856 | 588 { |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
589 int cnt, i, lastPos = -1; |
4856 | 590 float *pIn, *pOut; |
591 | |
592 for (cnt = 0; cnt < numComponents; cnt++){ | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
593 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos); |
4856 | 594 pIn = pComponent[cnt].coef; |
595 pOut = &(pSpectrum[pComponent[cnt].pos]); | |
596 | |
597 for (i=0 ; i<pComponent[cnt].numCoefs ; i++) | |
598 pOut[i] += pIn[i]; | |
599 } | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
600 |
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
601 return lastPos; |
4856 | 602 } |
603 | |
604 | |
605 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old))) | |
606 | |
607 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode) | |
608 { | |
609 int i, band, nsample, s1, s2; | |
610 float c1, c2; | |
611 float mc1_l, mc1_r, mc2_l, mc2_r; | |
612 | |
613 for (i=0,band = 0; band < 4*256; band+=256,i++) { | |
614 s1 = pPrevCode[i]; | |
615 s2 = pCurrCode[i]; | |
616 nsample = 0; | |
617 | |
618 if (s1 != s2) { | |
619 /* Selector value changed, interpolation needed. */ | |
620 mc1_l = matrixCoeffs[s1*2]; | |
621 mc1_r = matrixCoeffs[s1*2+1]; | |
622 mc2_l = matrixCoeffs[s2*2]; | |
623 mc2_r = matrixCoeffs[s2*2+1]; | |
624 | |
625 /* Interpolation is done over the first eight samples. */ | |
626 for(; nsample < 8; nsample++) { | |
627 c1 = su1[band+nsample]; | |
628 c2 = su2[band+nsample]; | |
629 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample); | |
630 su1[band+nsample] = c2; | |
631 su2[band+nsample] = c1 * 2.0 - c2; | |
632 } | |
633 } | |
634 | |
635 /* Apply the matrix without interpolation. */ | |
636 switch (s2) { | |
637 case 0: /* M/S decoding */ | |
638 for (; nsample < 256; nsample++) { | |
639 c1 = su1[band+nsample]; | |
640 c2 = su2[band+nsample]; | |
641 su1[band+nsample] = c2 * 2.0; | |
642 su2[band+nsample] = (c1 - c2) * 2.0; | |
643 } | |
644 break; | |
645 | |
646 case 1: | |
647 for (; nsample < 256; nsample++) { | |
648 c1 = su1[band+nsample]; | |
649 c2 = su2[band+nsample]; | |
650 su1[band+nsample] = (c1 + c2) * 2.0; | |
651 su2[band+nsample] = c2 * -2.0; | |
652 } | |
653 break; | |
654 case 2: | |
655 case 3: | |
656 for (; nsample < 256; nsample++) { | |
657 c1 = su1[band+nsample]; | |
658 c2 = su2[band+nsample]; | |
659 su1[band+nsample] = c1 + c2; | |
660 su2[band+nsample] = c1 - c2; | |
661 } | |
662 break; | |
663 default: | |
664 assert(0); | |
665 } | |
666 } | |
667 } | |
668 | |
669 static void getChannelWeights (int indx, int flag, float ch[2]){ | |
670 | |
671 if (indx == 7) { | |
672 ch[0] = 1.0; | |
673 ch[1] = 1.0; | |
674 } else { | |
675 ch[0] = (float)(indx & 7) / 7.0; | |
676 ch[1] = sqrt(2 - ch[0]*ch[0]); | |
677 if(flag) | |
678 FFSWAP(float, ch[0], ch[1]); | |
679 } | |
680 } | |
681 | |
682 static void channelWeighting (float *su1, float *su2, int *p3) | |
683 { | |
684 int band, nsample; | |
685 /* w[x][y] y=0 is left y=1 is right */ | |
686 float w[2][2]; | |
687 | |
688 if (p3[1] != 7 || p3[3] != 7){ | |
689 getChannelWeights(p3[1], p3[0], w[0]); | |
690 getChannelWeights(p3[3], p3[2], w[1]); | |
691 | |
692 for(band = 1; band < 4; band++) { | |
693 /* scale the channels by the weights */ | |
694 for(nsample = 0; nsample < 8; nsample++) { | |
695 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample); | |
696 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample); | |
697 } | |
698 | |
699 for(; nsample < 256; nsample++) { | |
700 su1[band*256+nsample] *= w[1][0]; | |
701 su2[band*256+nsample] *= w[1][1]; | |
702 } | |
703 } | |
704 } | |
705 } | |
706 | |
707 | |
708 /** | |
709 * Decode a Sound Unit | |
710 * | |
711 * @param gb the GetBit context | |
712 * @param pSnd the channel unit to be used | |
713 * @param pOut the decoded samples before IQMF in float representation | |
714 * @param channelNum channel number | |
715 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono) | |
716 */ | |
717 | |
718 | |
719 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode) | |
720 { | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
721 int band, result=0, numSubbands, lastTonal, numBands; |
4856 | 722 |
723 if (codingMode == JOINT_STEREO && channelNum == 1) { | |
724 if (get_bits(gb,2) != 3) { | |
725 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n"); | |
726 return -1; | |
727 } | |
728 } else { | |
729 if (get_bits(gb,6) != 0x28) { | |
730 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n"); | |
731 return -1; | |
732 } | |
733 } | |
734 | |
735 /* number of coded QMF bands */ | |
736 pSnd->bandsCoded = get_bits(gb,2); | |
737 | |
738 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded); | |
739 if (result) return result; | |
740 | |
4865 | 741 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded); |
742 if (pSnd->numComponents == -1) return -1; | |
4856 | 743 |
744 numSubbands = decodeSpectrum (gb, pSnd->spectrum); | |
745 | |
746 /* Merge the decoded spectrum and tonal components. */ | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
747 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components); |
4856 | 748 |
749 | |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
750 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */ |
4856 | 751 numBands = (subbandTab[numSubbands] - 1) >> 8; |
6843
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
752 if (lastTonal >= 0) |
bc8faf4f8b7d
Fix decoding of 01-Untitled(1).oma, patch by Maxim Poliakovski
banan
parents:
6716
diff
changeset
|
753 numBands = FFMAX((lastTonal + 256) >> 8, numBands); |
4856 | 754 |
755 | |
756 /* Reconstruct time domain samples. */ | |
757 for (band=0; band<4; band++) { | |
758 /* Perform the IMDCT step without overlapping. */ | |
759 if (band <= numBands) { | |
760 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1,q->mdct_tmp); | |
761 } else | |
762 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float)); | |
763 | |
764 /* gain compensation and overlapping */ | |
765 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]), | |
766 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]), | |
767 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band])); | |
768 } | |
769 | |
770 /* Swap the gain control buffers for the next frame. */ | |
771 pSnd->gcBlkSwitch ^= 1; | |
772 | |
773 return 0; | |
774 } | |
775 | |
776 /** | |
777 * Frame handling | |
778 * | |
779 * @param q Atrac3 private context | |
780 * @param databuf the input data | |
781 */ | |
782 | |
783 static int decodeFrame(ATRAC3Context *q, uint8_t* databuf) | |
784 { | |
785 int result, i; | |
786 float *p1, *p2, *p3, *p4; | |
787 uint8_t *ptr1, *ptr2; | |
788 | |
789 if (q->codingMode == JOINT_STEREO) { | |
790 | |
791 /* channel coupling mode */ | |
792 /* decode Sound Unit 1 */ | |
793 init_get_bits(&q->gb,databuf,q->bits_per_frame); | |
794 | |
795 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO); | |
796 if (result != 0) | |
797 return (result); | |
798 | |
799 /* Framedata of the su2 in the joint-stereo mode is encoded in | |
800 * reverse byte order so we need to swap it first. */ | |
801 ptr1 = databuf; | |
802 ptr2 = databuf+q->bytes_per_frame-1; | |
803 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) { | |
804 FFSWAP(uint8_t,*ptr1,*ptr2); | |
805 } | |
806 | |
807 /* Skip the sync codes (0xF8). */ | |
808 ptr1 = databuf; | |
809 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) { | |
810 if (i >= q->bytes_per_frame) | |
811 return -1; | |
812 } | |
813 | |
814 | |
815 /* set the bitstream reader at the start of the second Sound Unit*/ | |
816 init_get_bits(&q->gb,ptr1,q->bits_per_frame); | |
817 | |
818 /* Fill the Weighting coeffs delay buffer */ | |
819 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int)); | |
5513 | 820 q->weighting_delay[4] = get_bits1(&q->gb); |
4856 | 821 q->weighting_delay[5] = get_bits(&q->gb,3); |
822 | |
823 for (i = 0; i < 4; i++) { | |
824 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i]; | |
825 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i]; | |
826 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2); | |
827 } | |
828 | |
829 /* Decode Sound Unit 2. */ | |
830 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO); | |
831 if (result != 0) | |
832 return (result); | |
833 | |
834 /* Reconstruct the channel coefficients. */ | |
835 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now); | |
836 | |
837 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay); | |
838 | |
839 } else { | |
840 /* normal stereo mode or mono */ | |
841 /* Decode the channel sound units. */ | |
842 for (i=0 ; i<q->channels ; i++) { | |
843 | |
844 /* Set the bitstream reader at the start of a channel sound unit. */ | |
845 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels); | |
846 | |
847 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode); | |
848 if (result != 0) | |
849 return (result); | |
850 } | |
851 } | |
852 | |
853 /* Apply the iQMF synthesis filter. */ | |
854 p1= q->outSamples; | |
855 for (i=0 ; i<q->channels ; i++) { | |
856 p2= p1+256; | |
857 p3= p2+256; | |
858 p4= p3+256; | |
859 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf); | |
860 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf); | |
861 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf); | |
862 p1 +=1024; | |
863 } | |
864 | |
865 return 0; | |
866 } | |
867 | |
868 | |
869 /** | |
870 * Atrac frame decoding | |
871 * | |
872 * @param avctx pointer to the AVCodecContext | |
873 */ | |
874 | |
875 static int atrac3_decode_frame(AVCodecContext *avctx, | |
876 void *data, int *data_size, | |
6228 | 877 const uint8_t *buf, int buf_size) { |
4856 | 878 ATRAC3Context *q = avctx->priv_data; |
879 int result = 0, i; | |
880 uint8_t* databuf; | |
881 int16_t* samples = data; | |
882 | |
883 if (buf_size < avctx->block_align) | |
884 return buf_size; | |
885 | |
886 /* Check if we need to descramble and what buffer to pass on. */ | |
887 if (q->scrambled_stream) { | |
888 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align); | |
889 databuf = q->decoded_bytes_buffer; | |
890 } else { | |
891 databuf = buf; | |
892 } | |
893 | |
894 result = decodeFrame(q, databuf); | |
895 | |
896 if (result != 0) { | |
897 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n"); | |
898 return -1; | |
899 } | |
900 | |
901 if (q->channels == 1) { | |
902 /* mono */ | |
903 for (i = 0; i<1024; i++) | |
5523 | 904 samples[i] = av_clip_int16(round(q->outSamples[i])); |
4856 | 905 *data_size = 1024 * sizeof(int16_t); |
906 } else { | |
907 /* stereo */ | |
908 for (i = 0; i < 1024; i++) { | |
5523 | 909 samples[i*2] = av_clip_int16(round(q->outSamples[i])); |
910 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i])); | |
4856 | 911 } |
912 *data_size = 2048 * sizeof(int16_t); | |
913 } | |
914 | |
915 return avctx->block_align; | |
916 } | |
917 | |
918 | |
919 /** | |
920 * Atrac3 initialization | |
921 * | |
922 * @param avctx pointer to the AVCodecContext | |
923 */ | |
924 | |
925 static int atrac3_decode_init(AVCodecContext *avctx) | |
926 { | |
927 int i; | |
6228 | 928 const uint8_t *edata_ptr = avctx->extradata; |
4856 | 929 ATRAC3Context *q = avctx->priv_data; |
930 | |
931 /* Take data from the AVCodecContext (RM container). */ | |
932 q->sample_rate = avctx->sample_rate; | |
933 q->channels = avctx->channels; | |
934 q->bit_rate = avctx->bit_rate; | |
935 q->bits_per_frame = avctx->block_align * 8; | |
936 q->bytes_per_frame = avctx->block_align; | |
937 | |
938 /* Take care of the codec-specific extradata. */ | |
939 if (avctx->extradata_size == 14) { | |
940 /* Parse the extradata, WAV format */ | |
941 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown value always 1 | |
942 q->samples_per_channel = bytestream_get_le32(&edata_ptr); | |
943 q->codingMode = bytestream_get_le16(&edata_ptr); | |
944 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr)); //Dupe of coding mode | |
945 q->frame_factor = bytestream_get_le16(&edata_ptr); //Unknown always 1 | |
946 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown always 0 | |
947 | |
948 /* setup */ | |
949 q->samples_per_frame = 1024 * q->channels; | |
950 q->atrac3version = 4; | |
951 q->delay = 0x88E; | |
952 if (q->codingMode) | |
953 q->codingMode = JOINT_STEREO; | |
954 else | |
955 q->codingMode = STEREO; | |
956 | |
957 q->scrambled_stream = 0; | |
958 | |
959 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)) { | |
960 } else { | |
961 av_log(avctx,AV_LOG_ERROR,"Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor); | |
962 return -1; | |
963 } | |
964 | |
965 } else if (avctx->extradata_size == 10) { | |
966 /* Parse the extradata, RM format. */ | |
967 q->atrac3version = bytestream_get_be32(&edata_ptr); | |
968 q->samples_per_frame = bytestream_get_be16(&edata_ptr); | |
969 q->delay = bytestream_get_be16(&edata_ptr); | |
970 q->codingMode = bytestream_get_be16(&edata_ptr); | |
971 | |
972 q->samples_per_channel = q->samples_per_frame / q->channels; | |
973 q->scrambled_stream = 1; | |
974 | |
975 } else { | |
976 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size); | |
977 } | |
978 /* Check the extradata. */ | |
979 | |
980 if (q->atrac3version != 4) { | |
981 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version); | |
982 return -1; | |
983 } | |
984 | |
985 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) { | |
986 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame); | |
987 return -1; | |
988 } | |
989 | |
990 if (q->delay != 0x88E) { | |
991 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay); | |
992 return -1; | |
993 } | |
994 | |
995 if (q->codingMode == STEREO) { | |
996 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n"); | |
997 } else if (q->codingMode == JOINT_STEREO) { | |
998 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n"); | |
999 } else { | |
1000 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode); | |
1001 return -1; | |
1002 } | |
1003 | |
1004 if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) { | |
1005 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n"); | |
1006 return -1; | |
1007 } | |
1008 | |
1009 | |
1010 if(avctx->block_align >= UINT_MAX/2) | |
1011 return -1; | |
1012 | |
1013 /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE, | |
1014 * this is for the bitstream reader. */ | |
1015 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL) | |
5407 | 1016 return AVERROR(ENOMEM); |
4856 | 1017 |
1018 | |
1019 /* Initialize the VLC tables. */ | |
1020 for (i=0 ; i<7 ; i++) { | |
1021 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i], | |
1022 huff_bits[i], 1, 1, | |
1023 huff_codes[i], 1, 1, INIT_VLC_USE_STATIC); | |
1024 } | |
1025 | |
1026 init_atrac3_transforms(q); | |
1027 | |
1028 /* Generate the scale factors. */ | |
1029 for (i=0 ; i<64 ; i++) | |
1030 SFTable[i] = pow(2.0, (i - 15) / 3.0); | |
1031 | |
1032 /* Generate gain tables. */ | |
1033 for (i=0 ; i<16 ; i++) | |
1034 gain_tab1[i] = powf (2.0, (4 - i)); | |
1035 | |
1036 for (i=-15 ; i<16 ; i++) | |
1037 gain_tab2[i+15] = powf (2.0, i * -0.125); | |
1038 | |
1039 /* init the joint-stereo decoding data */ | |
1040 q->weighting_delay[0] = 0; | |
1041 q->weighting_delay[1] = 7; | |
1042 q->weighting_delay[2] = 0; | |
1043 q->weighting_delay[3] = 7; | |
1044 q->weighting_delay[4] = 0; | |
1045 q->weighting_delay[5] = 7; | |
1046 | |
1047 for (i=0; i<4; i++) { | |
1048 q->matrix_coeff_index_prev[i] = 3; | |
1049 q->matrix_coeff_index_now[i] = 3; | |
1050 q->matrix_coeff_index_next[i] = 3; | |
1051 } | |
1052 | |
1053 dsputil_init(&dsp, avctx); | |
1054 | |
1055 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels); | |
5423 | 1056 if (!q->pUnits) { |
1057 av_free(q->decoded_bytes_buffer); | |
1058 return AVERROR(ENOMEM); | |
1059 } | |
4856 | 1060 |
7451
85ab7655ad4d
Modify all codecs to report their supported input and output sample format(s).
pross
parents:
7040
diff
changeset
|
1061 avctx->sample_fmt = SAMPLE_FMT_S16; |
4856 | 1062 return 0; |
1063 } | |
1064 | |
1065 | |
1066 AVCodec atrac3_decoder = | |
1067 { | |
6716 | 1068 .name = "atrac3", |
4856 | 1069 .type = CODEC_TYPE_AUDIO, |
1070 .id = CODEC_ID_ATRAC3, | |
1071 .priv_data_size = sizeof(ATRAC3Context), | |
1072 .init = atrac3_decode_init, | |
1073 .close = atrac3_decode_close, | |
1074 .decode = atrac3_decode_frame, | |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6997
diff
changeset
|
1075 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"), |
4856 | 1076 }; |