dca.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*
  2. * DCA compatible decoder
  3. * Copyright (C) 2004 Gildas Bazin
  4. * Copyright (C) 2004 Benjamin Zores
  5. * Copyright (C) 2006 Benjamin Larsson
  6. * Copyright (C) 2007 Konstantin Shishkov
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file libavcodec/dca.c
  26. */
  27. #include <math.h>
  28. #include <stddef.h>
  29. #include <stdio.h>
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "bitstream.h"
  33. #include "dcadata.h"
  34. #include "dcahuff.h"
  35. #include "dca.h"
  36. //#define TRACE
  37. #define DCA_PRIM_CHANNELS_MAX (5)
  38. #define DCA_SUBBANDS (32)
  39. #define DCA_ABITS_MAX (32) /* Should be 28 */
  40. #define DCA_SUBSUBFAMES_MAX (4)
  41. #define DCA_LFE_MAX (3)
  42. enum DCAMode {
  43. DCA_MONO = 0,
  44. DCA_CHANNEL,
  45. DCA_STEREO,
  46. DCA_STEREO_SUMDIFF,
  47. DCA_STEREO_TOTAL,
  48. DCA_3F,
  49. DCA_2F1R,
  50. DCA_3F1R,
  51. DCA_2F2R,
  52. DCA_3F2R,
  53. DCA_4F2R
  54. };
  55. /* Tables for mapping dts channel configurations to libavcodec multichannel api.
  56. * Some compromises have been made for special configurations. Most configurations
  57. * are never used so complete accuracy is not needed.
  58. *
  59. * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
  60. * S -> side, when both rear and back are configured move one of them to the side channel
  61. * OV -> center back
  62. * All 2 channel configurations -> CH_LAYOUT_STEREO
  63. */
  64. static const int64_t dca_core_channel_layout[] = {
  65. CH_FRONT_CENTER, ///< 1, A
  66. CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
  67. CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
  68. CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
  69. CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
  70. CH_LAYOUT_STEREO|CH_FRONT_CENTER, ///< 3, C+L+R
  71. CH_LAYOUT_STEREO|CH_BACK_CENTER, ///< 3, L+R+S
  72. CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 4, C + L + R+ S
  73. CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
  74. CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
  75. CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
  76. CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV
  77. CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_FRONT_LEFT_OF_CENTER|CH_BACK_CENTER|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 6, CF+ CR+LF+ RF+LR + RR
  78. CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
  79. CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2
  80. CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_BACK_CENTER|CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR
  81. };
  82. static const int8_t dca_lfe_index[] = {
  83. 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
  84. };
  85. static const int8_t dca_channel_reorder_lfe[][8] = {
  86. { 0, -1, -1, -1, -1, -1, -1, -1},
  87. { 0, 1, -1, -1, -1, -1, -1, -1},
  88. { 0, 1, -1, -1, -1, -1, -1, -1},
  89. { 0, 1, -1, -1, -1, -1, -1, -1},
  90. { 0, 1, -1, -1, -1, -1, -1, -1},
  91. { 2, 0, 1, -1, -1, -1, -1, -1},
  92. { 0, 1, 3, -1, -1, -1, -1, -1},
  93. { 2, 0, 1, 4, -1, -1, -1, -1},
  94. { 0, 1, 3, 4, -1, -1, -1, -1},
  95. { 2, 0, 1, 4, 5, -1, -1, -1},
  96. { 3, 4, 0, 1, 5, 6, -1, -1},
  97. { 2, 0, 1, 4, 5, 6, -1, -1},
  98. { 0, 6, 4, 5, 2, 3, -1, -1},
  99. { 4, 2, 5, 0, 1, 6, 7, -1},
  100. { 5, 6, 0, 1, 7, 3, 8, 4},
  101. { 4, 2, 5, 0, 1, 6, 8, 7},
  102. };
  103. static const int8_t dca_channel_reorder_nolfe[][8] = {
  104. { 0, -1, -1, -1, -1, -1, -1, -1},
  105. { 0, 1, -1, -1, -1, -1, -1, -1},
  106. { 0, 1, -1, -1, -1, -1, -1, -1},
  107. { 0, 1, -1, -1, -1, -1, -1, -1},
  108. { 0, 1, -1, -1, -1, -1, -1, -1},
  109. { 2, 0, 1, -1, -1, -1, -1, -1},
  110. { 0, 1, 2, -1, -1, -1, -1, -1},
  111. { 2, 0, 1, 3, -1, -1, -1, -1},
  112. { 0, 1, 2, 3, -1, -1, -1, -1},
  113. { 2, 0, 1, 3, 4, -1, -1, -1},
  114. { 2, 3, 0, 1, 4, 5, -1, -1},
  115. { 2, 0, 1, 3, 4, 5, -1, -1},
  116. { 0, 5, 3, 4, 1, 2, -1, -1},
  117. { 3, 2, 4, 0, 1, 5, 6, -1},
  118. { 4, 5, 0, 1, 6, 2, 7, 3},
  119. { 3, 2, 4, 0, 1, 5, 7, 6},
  120. };
  121. #define DCA_DOLBY 101 /* FIXME */
  122. #define DCA_CHANNEL_BITS 6
  123. #define DCA_CHANNEL_MASK 0x3F
  124. #define DCA_LFE 0x80
  125. #define HEADER_SIZE 14
  126. #define DCA_MAX_FRAME_SIZE 16384
  127. /** Bit allocation */
  128. typedef struct {
  129. int offset; ///< code values offset
  130. int maxbits[8]; ///< max bits in VLC
  131. int wrap; ///< wrap for get_vlc2()
  132. VLC vlc[8]; ///< actual codes
  133. } BitAlloc;
  134. static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
  135. static BitAlloc dca_tmode; ///< transition mode VLCs
  136. static BitAlloc dca_scalefactor; ///< scalefactor VLCs
  137. static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
  138. static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
  139. {
  140. return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
  141. }
  142. typedef struct {
  143. AVCodecContext *avctx;
  144. /* Frame header */
  145. int frame_type; ///< type of the current frame
  146. int samples_deficit; ///< deficit sample count
  147. int crc_present; ///< crc is present in the bitstream
  148. int sample_blocks; ///< number of PCM sample blocks
  149. int frame_size; ///< primary frame byte size
  150. int amode; ///< audio channels arrangement
  151. int sample_rate; ///< audio sampling rate
  152. int bit_rate; ///< transmission bit rate
  153. int bit_rate_index; ///< transmission bit rate index
  154. int downmix; ///< embedded downmix enabled
  155. int dynrange; ///< embedded dynamic range flag
  156. int timestamp; ///< embedded time stamp flag
  157. int aux_data; ///< auxiliary data flag
  158. int hdcd; ///< source material is mastered in HDCD
  159. int ext_descr; ///< extension audio descriptor flag
  160. int ext_coding; ///< extended coding flag
  161. int aspf; ///< audio sync word insertion flag
  162. int lfe; ///< low frequency effects flag
  163. int predictor_history; ///< predictor history flag
  164. int header_crc; ///< header crc check bytes
  165. int multirate_inter; ///< multirate interpolator switch
  166. int version; ///< encoder software revision
  167. int copy_history; ///< copy history
  168. int source_pcm_res; ///< source pcm resolution
  169. int front_sum; ///< front sum/difference flag
  170. int surround_sum; ///< surround sum/difference flag
  171. int dialog_norm; ///< dialog normalisation parameter
  172. /* Primary audio coding header */
  173. int subframes; ///< number of subframes
  174. int total_channels; ///< number of channels including extensions
  175. int prim_channels; ///< number of primary audio channels
  176. int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
  177. int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
  178. int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
  179. int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
  180. int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
  181. int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
  182. int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
  183. float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
  184. /* Primary audio coding side information */
  185. int subsubframes; ///< number of subsubframes
  186. int partial_samples; ///< partial subsubframe samples count
  187. int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
  188. int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
  189. int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
  190. int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
  191. int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
  192. int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
  193. int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
  194. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
  195. int dynrange_coef; ///< dynamic range coefficient
  196. int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
  197. float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
  198. 2 /*history */ ]; ///< Low frequency effect data
  199. int lfe_scale_factor;
  200. /* Subband samples history (for ADPCM) */
  201. float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
  202. DECLARE_ALIGNED_16(float, subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512]);
  203. float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][32];
  204. int hist_index[DCA_PRIM_CHANNELS_MAX];
  205. int output; ///< type of output
  206. float add_bias; ///< output bias
  207. float scale_bias; ///< output scale
  208. DECLARE_ALIGNED_16(float, samples[1536]); /* 6 * 256 = 1536, might only need 5 */
  209. const float *samples_chanptr[6];
  210. uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
  211. int dca_buffer_size; ///< how much data is in the dca_buffer
  212. const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
  213. GetBitContext gb;
  214. /* Current position in DCA frame */
  215. int current_subframe;
  216. int current_subsubframe;
  217. int debug_flag; ///< used for suppressing repeated error messages output
  218. DSPContext dsp;
  219. MDCTContext imdct;
  220. } DCAContext;
  221. static av_cold void dca_init_vlcs(void)
  222. {
  223. static int vlcs_initialized = 0;
  224. int i, j;
  225. if (vlcs_initialized)
  226. return;
  227. dca_bitalloc_index.offset = 1;
  228. dca_bitalloc_index.wrap = 2;
  229. for (i = 0; i < 5; i++)
  230. init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
  231. bitalloc_12_bits[i], 1, 1,
  232. bitalloc_12_codes[i], 2, 2, 1);
  233. dca_scalefactor.offset = -64;
  234. dca_scalefactor.wrap = 2;
  235. for (i = 0; i < 5; i++)
  236. init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
  237. scales_bits[i], 1, 1,
  238. scales_codes[i], 2, 2, 1);
  239. dca_tmode.offset = 0;
  240. dca_tmode.wrap = 1;
  241. for (i = 0; i < 4; i++)
  242. init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
  243. tmode_bits[i], 1, 1,
  244. tmode_codes[i], 2, 2, 1);
  245. for(i = 0; i < 10; i++)
  246. for(j = 0; j < 7; j++){
  247. if(!bitalloc_codes[i][j]) break;
  248. dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
  249. dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
  250. init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
  251. bitalloc_sizes[i],
  252. bitalloc_bits[i][j], 1, 1,
  253. bitalloc_codes[i][j], 2, 2, 1);
  254. }
  255. vlcs_initialized = 1;
  256. }
  257. static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
  258. {
  259. while(len--)
  260. *dst++ = get_bits(gb, bits);
  261. }
  262. static int dca_parse_frame_header(DCAContext * s)
  263. {
  264. int i, j;
  265. static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
  266. static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  267. static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  268. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  269. /* Sync code */
  270. get_bits(&s->gb, 32);
  271. /* Frame header */
  272. s->frame_type = get_bits(&s->gb, 1);
  273. s->samples_deficit = get_bits(&s->gb, 5) + 1;
  274. s->crc_present = get_bits(&s->gb, 1);
  275. s->sample_blocks = get_bits(&s->gb, 7) + 1;
  276. s->frame_size = get_bits(&s->gb, 14) + 1;
  277. if (s->frame_size < 95)
  278. return -1;
  279. s->amode = get_bits(&s->gb, 6);
  280. s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
  281. if (!s->sample_rate)
  282. return -1;
  283. s->bit_rate_index = get_bits(&s->gb, 5);
  284. s->bit_rate = dca_bit_rates[s->bit_rate_index];
  285. if (!s->bit_rate)
  286. return -1;
  287. s->downmix = get_bits(&s->gb, 1);
  288. s->dynrange = get_bits(&s->gb, 1);
  289. s->timestamp = get_bits(&s->gb, 1);
  290. s->aux_data = get_bits(&s->gb, 1);
  291. s->hdcd = get_bits(&s->gb, 1);
  292. s->ext_descr = get_bits(&s->gb, 3);
  293. s->ext_coding = get_bits(&s->gb, 1);
  294. s->aspf = get_bits(&s->gb, 1);
  295. s->lfe = get_bits(&s->gb, 2);
  296. s->predictor_history = get_bits(&s->gb, 1);
  297. /* TODO: check CRC */
  298. if (s->crc_present)
  299. s->header_crc = get_bits(&s->gb, 16);
  300. s->multirate_inter = get_bits(&s->gb, 1);
  301. s->version = get_bits(&s->gb, 4);
  302. s->copy_history = get_bits(&s->gb, 2);
  303. s->source_pcm_res = get_bits(&s->gb, 3);
  304. s->front_sum = get_bits(&s->gb, 1);
  305. s->surround_sum = get_bits(&s->gb, 1);
  306. s->dialog_norm = get_bits(&s->gb, 4);
  307. /* FIXME: channels mixing levels */
  308. s->output = s->amode;
  309. if(s->lfe) s->output |= DCA_LFE;
  310. #ifdef TRACE
  311. av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
  312. av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
  313. av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
  314. av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
  315. s->sample_blocks, s->sample_blocks * 32);
  316. av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
  317. av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
  318. s->amode, dca_channels[s->amode]);
  319. av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
  320. s->sample_rate);
  321. av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
  322. s->bit_rate);
  323. av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
  324. av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
  325. av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
  326. av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
  327. av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
  328. av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
  329. av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
  330. av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
  331. av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
  332. av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
  333. s->predictor_history);
  334. av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
  335. av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
  336. s->multirate_inter);
  337. av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
  338. av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
  339. av_log(s->avctx, AV_LOG_DEBUG,
  340. "source pcm resolution: %i (%i bits/sample)\n",
  341. s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
  342. av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
  343. av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
  344. av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
  345. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  346. #endif
  347. /* Primary audio coding header */
  348. s->subframes = get_bits(&s->gb, 4) + 1;
  349. s->total_channels = get_bits(&s->gb, 3) + 1;
  350. s->prim_channels = s->total_channels;
  351. if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
  352. s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */
  353. for (i = 0; i < s->prim_channels; i++) {
  354. s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
  355. if (s->subband_activity[i] > DCA_SUBBANDS)
  356. s->subband_activity[i] = DCA_SUBBANDS;
  357. }
  358. for (i = 0; i < s->prim_channels; i++) {
  359. s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
  360. if (s->vq_start_subband[i] > DCA_SUBBANDS)
  361. s->vq_start_subband[i] = DCA_SUBBANDS;
  362. }
  363. get_array(&s->gb, s->joint_intensity, s->prim_channels, 3);
  364. get_array(&s->gb, s->transient_huffman, s->prim_channels, 2);
  365. get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
  366. get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3);
  367. /* Get codebooks quantization indexes */
  368. memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
  369. for (j = 1; j < 11; j++)
  370. for (i = 0; i < s->prim_channels; i++)
  371. s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
  372. /* Get scale factor adjustment */
  373. for (j = 0; j < 11; j++)
  374. for (i = 0; i < s->prim_channels; i++)
  375. s->scalefactor_adj[i][j] = 1;
  376. for (j = 1; j < 11; j++)
  377. for (i = 0; i < s->prim_channels; i++)
  378. if (s->quant_index_huffman[i][j] < thr[j])
  379. s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
  380. if (s->crc_present) {
  381. /* Audio header CRC check */
  382. get_bits(&s->gb, 16);
  383. }
  384. s->current_subframe = 0;
  385. s->current_subsubframe = 0;
  386. #ifdef TRACE
  387. av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
  388. av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
  389. for(i = 0; i < s->prim_channels; i++){
  390. av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
  391. av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
  392. av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
  393. av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
  394. av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
  395. av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
  396. av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
  397. for (j = 0; j < 11; j++)
  398. av_log(s->avctx, AV_LOG_DEBUG, " %i",
  399. s->quant_index_huffman[i][j]);
  400. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  401. av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
  402. for (j = 0; j < 11; j++)
  403. av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
  404. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  405. }
  406. #endif
  407. return 0;
  408. }
  409. static inline int get_scale(GetBitContext *gb, int level, int value)
  410. {
  411. if (level < 5) {
  412. /* huffman encoded */
  413. value += get_bitalloc(gb, &dca_scalefactor, level);
  414. } else if(level < 8)
  415. value = get_bits(gb, level + 1);
  416. return value;
  417. }
  418. static int dca_subframe_header(DCAContext * s)
  419. {
  420. /* Primary audio coding side information */
  421. int j, k;
  422. s->subsubframes = get_bits(&s->gb, 2) + 1;
  423. s->partial_samples = get_bits(&s->gb, 3);
  424. for (j = 0; j < s->prim_channels; j++) {
  425. for (k = 0; k < s->subband_activity[j]; k++)
  426. s->prediction_mode[j][k] = get_bits(&s->gb, 1);
  427. }
  428. /* Get prediction codebook */
  429. for (j = 0; j < s->prim_channels; j++) {
  430. for (k = 0; k < s->subband_activity[j]; k++) {
  431. if (s->prediction_mode[j][k] > 0) {
  432. /* (Prediction coefficient VQ address) */
  433. s->prediction_vq[j][k] = get_bits(&s->gb, 12);
  434. }
  435. }
  436. }
  437. /* Bit allocation index */
  438. for (j = 0; j < s->prim_channels; j++) {
  439. for (k = 0; k < s->vq_start_subband[j]; k++) {
  440. if (s->bitalloc_huffman[j] == 6)
  441. s->bitalloc[j][k] = get_bits(&s->gb, 5);
  442. else if (s->bitalloc_huffman[j] == 5)
  443. s->bitalloc[j][k] = get_bits(&s->gb, 4);
  444. else if (s->bitalloc_huffman[j] == 7) {
  445. av_log(s->avctx, AV_LOG_ERROR,
  446. "Invalid bit allocation index\n");
  447. return -1;
  448. } else {
  449. s->bitalloc[j][k] =
  450. get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
  451. }
  452. if (s->bitalloc[j][k] > 26) {
  453. // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
  454. // j, k, s->bitalloc[j][k]);
  455. return -1;
  456. }
  457. }
  458. }
  459. /* Transition mode */
  460. for (j = 0; j < s->prim_channels; j++) {
  461. for (k = 0; k < s->subband_activity[j]; k++) {
  462. s->transition_mode[j][k] = 0;
  463. if (s->subsubframes > 1 &&
  464. k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
  465. s->transition_mode[j][k] =
  466. get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
  467. }
  468. }
  469. }
  470. for (j = 0; j < s->prim_channels; j++) {
  471. const uint32_t *scale_table;
  472. int scale_sum;
  473. memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
  474. if (s->scalefactor_huffman[j] == 6)
  475. scale_table = scale_factor_quant7;
  476. else
  477. scale_table = scale_factor_quant6;
  478. /* When huffman coded, only the difference is encoded */
  479. scale_sum = 0;
  480. for (k = 0; k < s->subband_activity[j]; k++) {
  481. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
  482. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
  483. s->scale_factor[j][k][0] = scale_table[scale_sum];
  484. }
  485. if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
  486. /* Get second scale factor */
  487. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
  488. s->scale_factor[j][k][1] = scale_table[scale_sum];
  489. }
  490. }
  491. }
  492. /* Joint subband scale factor codebook select */
  493. for (j = 0; j < s->prim_channels; j++) {
  494. /* Transmitted only if joint subband coding enabled */
  495. if (s->joint_intensity[j] > 0)
  496. s->joint_huff[j] = get_bits(&s->gb, 3);
  497. }
  498. /* Scale factors for joint subband coding */
  499. for (j = 0; j < s->prim_channels; j++) {
  500. int source_channel;
  501. /* Transmitted only if joint subband coding enabled */
  502. if (s->joint_intensity[j] > 0) {
  503. int scale = 0;
  504. source_channel = s->joint_intensity[j] - 1;
  505. /* When huffman coded, only the difference is encoded
  506. * (is this valid as well for joint scales ???) */
  507. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
  508. scale = get_scale(&s->gb, s->joint_huff[j], 0);
  509. scale += 64; /* bias */
  510. s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
  511. }
  512. if (!s->debug_flag & 0x02) {
  513. av_log(s->avctx, AV_LOG_DEBUG,
  514. "Joint stereo coding not supported\n");
  515. s->debug_flag |= 0x02;
  516. }
  517. }
  518. }
  519. /* Stereo downmix coefficients */
  520. if (s->prim_channels > 2) {
  521. if(s->downmix) {
  522. for (j = 0; j < s->prim_channels; j++) {
  523. s->downmix_coef[j][0] = get_bits(&s->gb, 7);
  524. s->downmix_coef[j][1] = get_bits(&s->gb, 7);
  525. }
  526. } else {
  527. int am = s->amode & DCA_CHANNEL_MASK;
  528. for (j = 0; j < s->prim_channels; j++) {
  529. s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
  530. s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
  531. }
  532. }
  533. }
  534. /* Dynamic range coefficient */
  535. if (s->dynrange)
  536. s->dynrange_coef = get_bits(&s->gb, 8);
  537. /* Side information CRC check word */
  538. if (s->crc_present) {
  539. get_bits(&s->gb, 16);
  540. }
  541. /*
  542. * Primary audio data arrays
  543. */
  544. /* VQ encoded high frequency subbands */
  545. for (j = 0; j < s->prim_channels; j++)
  546. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  547. /* 1 vector -> 32 samples */
  548. s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
  549. /* Low frequency effect data */
  550. if (s->lfe) {
  551. /* LFE samples */
  552. int lfe_samples = 2 * s->lfe * s->subsubframes;
  553. float lfe_scale;
  554. for (j = lfe_samples; j < lfe_samples * 2; j++) {
  555. /* Signed 8 bits int */
  556. s->lfe_data[j] = get_sbits(&s->gb, 8);
  557. }
  558. /* Scale factor index */
  559. s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
  560. /* Quantization step size * scale factor */
  561. lfe_scale = 0.035 * s->lfe_scale_factor;
  562. for (j = lfe_samples; j < lfe_samples * 2; j++)
  563. s->lfe_data[j] *= lfe_scale;
  564. }
  565. #ifdef TRACE
  566. av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
  567. av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
  568. s->partial_samples);
  569. for (j = 0; j < s->prim_channels; j++) {
  570. av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
  571. for (k = 0; k < s->subband_activity[j]; k++)
  572. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
  573. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  574. }
  575. for (j = 0; j < s->prim_channels; j++) {
  576. for (k = 0; k < s->subband_activity[j]; k++)
  577. av_log(s->avctx, AV_LOG_DEBUG,
  578. "prediction coefs: %f, %f, %f, %f\n",
  579. (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
  580. (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
  581. (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
  582. (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
  583. }
  584. for (j = 0; j < s->prim_channels; j++) {
  585. av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
  586. for (k = 0; k < s->vq_start_subband[j]; k++)
  587. av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
  588. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  589. }
  590. for (j = 0; j < s->prim_channels; j++) {
  591. av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
  592. for (k = 0; k < s->subband_activity[j]; k++)
  593. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
  594. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  595. }
  596. for (j = 0; j < s->prim_channels; j++) {
  597. av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
  598. for (k = 0; k < s->subband_activity[j]; k++) {
  599. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
  600. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
  601. if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
  602. av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
  603. }
  604. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  605. }
  606. for (j = 0; j < s->prim_channels; j++) {
  607. if (s->joint_intensity[j] > 0) {
  608. int source_channel = s->joint_intensity[j] - 1;
  609. av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
  610. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
  611. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
  612. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  613. }
  614. }
  615. if (s->prim_channels > 2 && s->downmix) {
  616. av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
  617. for (j = 0; j < s->prim_channels; j++) {
  618. av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
  619. av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
  620. }
  621. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  622. }
  623. for (j = 0; j < s->prim_channels; j++)
  624. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  625. av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
  626. if(s->lfe){
  627. int lfe_samples = 2 * s->lfe * s->subsubframes;
  628. av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
  629. for (j = lfe_samples; j < lfe_samples * 2; j++)
  630. av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
  631. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  632. }
  633. #endif
  634. return 0;
  635. }
  636. static void qmf_32_subbands(DCAContext * s, int chans,
  637. float samples_in[32][8], float *samples_out,
  638. float scale, float bias)
  639. {
  640. const float *prCoeff;
  641. int i, j;
  642. DECLARE_ALIGNED_16(float, raXin[32]);
  643. int hist_index= s->hist_index[chans];
  644. float *subband_fir_hist2 = s->subband_fir_noidea[chans];
  645. int subindex;
  646. scale *= sqrt(1/8.0);
  647. /* Select filter */
  648. if (!s->multirate_inter) /* Non-perfect reconstruction */
  649. prCoeff = fir_32bands_nonperfect;
  650. else /* Perfect reconstruction */
  651. prCoeff = fir_32bands_perfect;
  652. /* Reconstructed channel sample index */
  653. for (subindex = 0; subindex < 8; subindex++) {
  654. float *subband_fir_hist = s->subband_fir_hist[chans] + hist_index;
  655. /* Load in one sample from each subband and clear inactive subbands */
  656. for (i = 0; i < s->subband_activity[chans]; i++){
  657. if((i-1)&2) raXin[i] = -samples_in[i][subindex];
  658. else raXin[i] = samples_in[i][subindex];
  659. }
  660. for (; i < 32; i++)
  661. raXin[i] = 0.0;
  662. ff_imdct_half(&s->imdct, subband_fir_hist, raXin);
  663. /* Multiply by filter coefficients */
  664. for (i = 0; i < 16; i++){
  665. float a= subband_fir_hist2[i ];
  666. float b= subband_fir_hist2[i+16];
  667. float c= 0;
  668. float d= 0;
  669. for (j = 0; j < 512-hist_index; j += 64){
  670. a += prCoeff[i+j ]*(-subband_fir_hist[15-i+j]);
  671. b += prCoeff[i+j+16]*( subband_fir_hist[ i+j]);
  672. c += prCoeff[i+j+32]*( subband_fir_hist[16+i+j]);
  673. d += prCoeff[i+j+48]*( subband_fir_hist[31-i+j]);
  674. }
  675. for ( ; j < 512; j += 64){
  676. a += prCoeff[i+j ]*(-subband_fir_hist[15-i+j-512]);
  677. b += prCoeff[i+j+16]*( subband_fir_hist[ i+j-512]);
  678. c += prCoeff[i+j+32]*( subband_fir_hist[16+i+j-512]);
  679. d += prCoeff[i+j+48]*( subband_fir_hist[31-i+j-512]);
  680. }
  681. samples_out[i ] = a * scale + bias;
  682. samples_out[i+16] = b * scale + bias;
  683. subband_fir_hist2[i ] = c;
  684. subband_fir_hist2[i+16] = d;
  685. }
  686. samples_out+= 32;
  687. hist_index = (hist_index-32)&511;
  688. }
  689. s->hist_index[chans]= hist_index;
  690. }
  691. static void lfe_interpolation_fir(int decimation_select,
  692. int num_deci_sample, float *samples_in,
  693. float *samples_out, float scale,
  694. float bias)
  695. {
  696. /* samples_in: An array holding decimated samples.
  697. * Samples in current subframe starts from samples_in[0],
  698. * while samples_in[-1], samples_in[-2], ..., stores samples
  699. * from last subframe as history.
  700. *
  701. * samples_out: An array holding interpolated samples
  702. */
  703. int decifactor, k, j;
  704. const float *prCoeff;
  705. int interp_index = 0; /* Index to the interpolated samples */
  706. int deciindex;
  707. /* Select decimation filter */
  708. if (decimation_select == 1) {
  709. decifactor = 128;
  710. prCoeff = lfe_fir_128;
  711. } else {
  712. decifactor = 64;
  713. prCoeff = lfe_fir_64;
  714. }
  715. /* Interpolation */
  716. for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
  717. /* One decimated sample generates decifactor interpolated ones */
  718. for (k = 0; k < decifactor; k++) {
  719. float rTmp = 0.0;
  720. //FIXME the coeffs are symetric, fix that
  721. for (j = 0; j < 512 / decifactor; j++)
  722. rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
  723. samples_out[interp_index++] = (rTmp * scale) + bias;
  724. }
  725. }
  726. }
  727. /* downmixing routines */
  728. #define MIX_REAR1(samples, si1, rs, coef) \
  729. samples[i] += samples[si1] * coef[rs][0]; \
  730. samples[i+256] += samples[si1] * coef[rs][1];
  731. #define MIX_REAR2(samples, si1, si2, rs, coef) \
  732. samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
  733. samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
  734. #define MIX_FRONT3(samples, coef) \
  735. t = samples[i]; \
  736. samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
  737. samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
  738. #define DOWNMIX_TO_STEREO(op1, op2) \
  739. for(i = 0; i < 256; i++){ \
  740. op1 \
  741. op2 \
  742. }
  743. static void dca_downmix(float *samples, int srcfmt,
  744. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
  745. {
  746. int i;
  747. float t;
  748. float coef[DCA_PRIM_CHANNELS_MAX][2];
  749. for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
  750. coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
  751. coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
  752. }
  753. switch (srcfmt) {
  754. case DCA_MONO:
  755. case DCA_CHANNEL:
  756. case DCA_STEREO_TOTAL:
  757. case DCA_STEREO_SUMDIFF:
  758. case DCA_4F2R:
  759. av_log(NULL, 0, "Not implemented!\n");
  760. break;
  761. case DCA_STEREO:
  762. break;
  763. case DCA_3F:
  764. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
  765. break;
  766. case DCA_2F1R:
  767. DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
  768. break;
  769. case DCA_3F1R:
  770. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  771. MIX_REAR1(samples, i + 768, 3, coef));
  772. break;
  773. case DCA_2F2R:
  774. DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
  775. break;
  776. case DCA_3F2R:
  777. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  778. MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
  779. break;
  780. }
  781. }
  782. /* Very compact version of the block code decoder that does not use table
  783. * look-up but is slightly slower */
  784. static int decode_blockcode(int code, int levels, int *values)
  785. {
  786. int i;
  787. int offset = (levels - 1) >> 1;
  788. for (i = 0; i < 4; i++) {
  789. values[i] = (code % levels) - offset;
  790. code /= levels;
  791. }
  792. if (code == 0)
  793. return 0;
  794. else {
  795. av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
  796. return -1;
  797. }
  798. }
  799. static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
  800. static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
  801. static int dca_subsubframe(DCAContext * s)
  802. {
  803. int k, l;
  804. int subsubframe = s->current_subsubframe;
  805. const float *quant_step_table;
  806. /* FIXME */
  807. float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  808. /*
  809. * Audio data
  810. */
  811. /* Select quantization step size table */
  812. if (s->bit_rate_index == 0x1f)
  813. quant_step_table = lossless_quant_d;
  814. else
  815. quant_step_table = lossy_quant_d;
  816. for (k = 0; k < s->prim_channels; k++) {
  817. for (l = 0; l < s->vq_start_subband[k]; l++) {
  818. int m;
  819. /* Select the mid-tread linear quantizer */
  820. int abits = s->bitalloc[k][l];
  821. float quant_step_size = quant_step_table[abits];
  822. float rscale;
  823. /*
  824. * Determine quantization index code book and its type
  825. */
  826. /* Select quantization index code book */
  827. int sel = s->quant_index_huffman[k][abits];
  828. /*
  829. * Extract bits from the bit stream
  830. */
  831. if(!abits){
  832. memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
  833. }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
  834. if(abits <= 7){
  835. /* Block code */
  836. int block_code1, block_code2, size, levels;
  837. int block[8];
  838. size = abits_sizes[abits-1];
  839. levels = abits_levels[abits-1];
  840. block_code1 = get_bits(&s->gb, size);
  841. /* FIXME Should test return value */
  842. decode_blockcode(block_code1, levels, block);
  843. block_code2 = get_bits(&s->gb, size);
  844. decode_blockcode(block_code2, levels, &block[4]);
  845. for (m = 0; m < 8; m++)
  846. subband_samples[k][l][m] = block[m];
  847. }else{
  848. /* no coding */
  849. for (m = 0; m < 8; m++)
  850. subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3);
  851. }
  852. }else{
  853. /* Huffman coded */
  854. for (m = 0; m < 8; m++)
  855. subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
  856. }
  857. /* Deal with transients */
  858. if (s->transition_mode[k][l] &&
  859. subsubframe >= s->transition_mode[k][l])
  860. rscale = quant_step_size * s->scale_factor[k][l][1];
  861. else
  862. rscale = quant_step_size * s->scale_factor[k][l][0];
  863. rscale *= s->scalefactor_adj[k][sel];
  864. for (m = 0; m < 8; m++)
  865. subband_samples[k][l][m] *= rscale;
  866. /*
  867. * Inverse ADPCM if in prediction mode
  868. */
  869. if (s->prediction_mode[k][l]) {
  870. int n;
  871. for (m = 0; m < 8; m++) {
  872. for (n = 1; n <= 4; n++)
  873. if (m >= n)
  874. subband_samples[k][l][m] +=
  875. (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  876. subband_samples[k][l][m - n] / 8192);
  877. else if (s->predictor_history)
  878. subband_samples[k][l][m] +=
  879. (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  880. s->subband_samples_hist[k][l][m - n +
  881. 4] / 8192);
  882. }
  883. }
  884. }
  885. /*
  886. * Decode VQ encoded high frequencies
  887. */
  888. for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
  889. /* 1 vector -> 32 samples but we only need the 8 samples
  890. * for this subsubframe. */
  891. int m;
  892. if (!s->debug_flag & 0x01) {
  893. av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
  894. s->debug_flag |= 0x01;
  895. }
  896. for (m = 0; m < 8; m++) {
  897. subband_samples[k][l][m] =
  898. high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
  899. m]
  900. * (float) s->scale_factor[k][l][0] / 16.0;
  901. }
  902. }
  903. }
  904. /* Check for DSYNC after subsubframe */
  905. if (s->aspf || subsubframe == s->subsubframes - 1) {
  906. if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
  907. #ifdef TRACE
  908. av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
  909. #endif
  910. } else {
  911. av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
  912. }
  913. }
  914. /* Backup predictor history for adpcm */
  915. for (k = 0; k < s->prim_channels; k++)
  916. for (l = 0; l < s->vq_start_subband[k]; l++)
  917. memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
  918. 4 * sizeof(subband_samples[0][0][0]));
  919. /* 32 subbands QMF */
  920. for (k = 0; k < s->prim_channels; k++) {
  921. /* static float pcm_to_double[8] =
  922. {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
  923. qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
  924. M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
  925. s->add_bias );
  926. }
  927. /* Down mixing */
  928. if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
  929. dca_downmix(s->samples, s->amode, s->downmix_coef);
  930. }
  931. /* Generate LFE samples for this subsubframe FIXME!!! */
  932. if (s->output & DCA_LFE) {
  933. int lfe_samples = 2 * s->lfe * s->subsubframes;
  934. lfe_interpolation_fir(s->lfe, 2 * s->lfe,
  935. s->lfe_data + lfe_samples +
  936. 2 * s->lfe * subsubframe,
  937. &s->samples[256 * dca_lfe_index[s->amode]],
  938. (1.0/256.0)*s->scale_bias, s->add_bias);
  939. /* Outputs 20bits pcm samples */
  940. }
  941. return 0;
  942. }
  943. static int dca_subframe_footer(DCAContext * s)
  944. {
  945. int aux_data_count = 0, i;
  946. int lfe_samples;
  947. /*
  948. * Unpack optional information
  949. */
  950. if (s->timestamp)
  951. get_bits(&s->gb, 32);
  952. if (s->aux_data)
  953. aux_data_count = get_bits(&s->gb, 6);
  954. for (i = 0; i < aux_data_count; i++)
  955. get_bits(&s->gb, 8);
  956. if (s->crc_present && (s->downmix || s->dynrange))
  957. get_bits(&s->gb, 16);
  958. lfe_samples = 2 * s->lfe * s->subsubframes;
  959. for (i = 0; i < lfe_samples; i++) {
  960. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  961. }
  962. return 0;
  963. }
  964. /**
  965. * Decode a dca frame block
  966. *
  967. * @param s pointer to the DCAContext
  968. */
  969. static int dca_decode_block(DCAContext * s)
  970. {
  971. /* Sanity check */
  972. if (s->current_subframe >= s->subframes) {
  973. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  974. s->current_subframe, s->subframes);
  975. return -1;
  976. }
  977. if (!s->current_subsubframe) {
  978. #ifdef TRACE
  979. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
  980. #endif
  981. /* Read subframe header */
  982. if (dca_subframe_header(s))
  983. return -1;
  984. }
  985. /* Read subsubframe */
  986. #ifdef TRACE
  987. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
  988. #endif
  989. if (dca_subsubframe(s))
  990. return -1;
  991. /* Update state */
  992. s->current_subsubframe++;
  993. if (s->current_subsubframe >= s->subsubframes) {
  994. s->current_subsubframe = 0;
  995. s->current_subframe++;
  996. }
  997. if (s->current_subframe >= s->subframes) {
  998. #ifdef TRACE
  999. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
  1000. #endif
  1001. /* Read subframe footer */
  1002. if (dca_subframe_footer(s))
  1003. return -1;
  1004. }
  1005. return 0;
  1006. }
  1007. /**
  1008. * Convert bitstream to one representation based on sync marker
  1009. */
  1010. static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
  1011. int max_size)
  1012. {
  1013. uint32_t mrk;
  1014. int i, tmp;
  1015. const uint16_t *ssrc = (const uint16_t *) src;
  1016. uint16_t *sdst = (uint16_t *) dst;
  1017. PutBitContext pb;
  1018. if((unsigned)src_size > (unsigned)max_size) {
  1019. // av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
  1020. // return -1;
  1021. src_size = max_size;
  1022. }
  1023. mrk = AV_RB32(src);
  1024. switch (mrk) {
  1025. case DCA_MARKER_RAW_BE:
  1026. memcpy(dst, src, src_size);
  1027. return src_size;
  1028. case DCA_MARKER_RAW_LE:
  1029. for (i = 0; i < (src_size + 1) >> 1; i++)
  1030. *sdst++ = bswap_16(*ssrc++);
  1031. return src_size;
  1032. case DCA_MARKER_14B_BE:
  1033. case DCA_MARKER_14B_LE:
  1034. init_put_bits(&pb, dst, max_size);
  1035. for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
  1036. tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
  1037. put_bits(&pb, 14, tmp);
  1038. }
  1039. flush_put_bits(&pb);
  1040. return (put_bits_count(&pb) + 7) >> 3;
  1041. default:
  1042. return -1;
  1043. }
  1044. }
  1045. /**
  1046. * Main frame decoding function
  1047. * FIXME add arguments
  1048. */
  1049. static int dca_decode_frame(AVCodecContext * avctx,
  1050. void *data, int *data_size,
  1051. const uint8_t * buf, int buf_size)
  1052. {
  1053. int i;
  1054. int16_t *samples = data;
  1055. DCAContext *s = avctx->priv_data;
  1056. int channels;
  1057. s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
  1058. if (s->dca_buffer_size == -1) {
  1059. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1060. return -1;
  1061. }
  1062. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  1063. if (dca_parse_frame_header(s) < 0) {
  1064. //seems like the frame is corrupt, try with the next one
  1065. *data_size=0;
  1066. return buf_size;
  1067. }
  1068. //set AVCodec values with parsed data
  1069. avctx->sample_rate = s->sample_rate;
  1070. avctx->bit_rate = s->bit_rate;
  1071. channels = s->prim_channels + !!s->lfe;
  1072. if (s->amode<16) {
  1073. avctx->channel_layout = dca_core_channel_layout[s->amode];
  1074. if (s->lfe) {
  1075. avctx->channel_layout |= CH_LOW_FREQUENCY;
  1076. s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
  1077. } else
  1078. s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
  1079. if(avctx->request_channels == 2 && s->prim_channels > 2) {
  1080. channels = 2;
  1081. s->output = DCA_STEREO;
  1082. avctx->channel_layout = CH_LAYOUT_STEREO;
  1083. }
  1084. } else {
  1085. av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
  1086. return -1;
  1087. }
  1088. /* There is nothing that prevents a dts frame to change channel configuration
  1089. but FFmpeg doesn't support that so only set the channels if it is previously
  1090. unset. Ideally during the first probe for channels the crc should be checked
  1091. and only set avctx->channels when the crc is ok. Right now the decoder could
  1092. set the channels based on a broken first frame.*/
  1093. if (!avctx->channels)
  1094. avctx->channels = channels;
  1095. if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
  1096. return -1;
  1097. *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
  1098. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1099. dca_decode_block(s);
  1100. s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
  1101. samples += 256 * channels;
  1102. }
  1103. return buf_size;
  1104. }
  1105. /**
  1106. * DCA initialization
  1107. *
  1108. * @param avctx pointer to the AVCodecContext
  1109. */
  1110. static av_cold int dca_decode_init(AVCodecContext * avctx)
  1111. {
  1112. DCAContext *s = avctx->priv_data;
  1113. int i;
  1114. s->avctx = avctx;
  1115. dca_init_vlcs();
  1116. dsputil_init(&s->dsp, avctx);
  1117. ff_mdct_init(&s->imdct, 6, 1);
  1118. for(i = 0; i < 6; i++)
  1119. s->samples_chanptr[i] = s->samples + i * 256;
  1120. avctx->sample_fmt = SAMPLE_FMT_S16;
  1121. if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
  1122. s->add_bias = 385.0f;
  1123. s->scale_bias = 1.0 / 32768.0;
  1124. } else {
  1125. s->add_bias = 0.0f;
  1126. s->scale_bias = 1.0;
  1127. /* allow downmixing to stereo */
  1128. if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
  1129. avctx->request_channels == 2) {
  1130. avctx->channels = avctx->request_channels;
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. static av_cold int dca_decode_end(AVCodecContext * avctx)
  1136. {
  1137. DCAContext *s = avctx->priv_data;
  1138. ff_mdct_end(&s->imdct);
  1139. return 0;
  1140. }
  1141. AVCodec dca_decoder = {
  1142. .name = "dca",
  1143. .type = CODEC_TYPE_AUDIO,
  1144. .id = CODEC_ID_DTS,
  1145. .priv_data_size = sizeof(DCAContext),
  1146. .init = dca_decode_init,
  1147. .decode = dca_decode_frame,
  1148. .close = dca_decode_end,
  1149. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1150. };