dca.c 45 KB

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