00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <math.h>
00026 #include <stddef.h>
00027 #include <stdio.h>
00028
00029 #include "libavutil/common.h"
00030 #include "libavutil/intmath.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/mathematics.h"
00033 #include "libavutil/audioconvert.h"
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "fft.h"
00037 #include "get_bits.h"
00038 #include "put_bits.h"
00039 #include "dcadata.h"
00040 #include "dcahuff.h"
00041 #include "dca.h"
00042 #include "synth_filter.h"
00043 #include "dcadsp.h"
00044 #include "fmtconvert.h"
00045
00046
00047
00048 #define DCA_PRIM_CHANNELS_MAX (7)
00049 #define DCA_SUBBANDS (32)
00050 #define DCA_ABITS_MAX (32)
00051 #define DCA_SUBSUBFRAMES_MAX (4)
00052 #define DCA_SUBFRAMES_MAX (16)
00053 #define DCA_BLOCKS_MAX (16)
00054 #define DCA_LFE_MAX (3)
00055
00056 enum DCAMode {
00057 DCA_MONO = 0,
00058 DCA_CHANNEL,
00059 DCA_STEREO,
00060 DCA_STEREO_SUMDIFF,
00061 DCA_STEREO_TOTAL,
00062 DCA_3F,
00063 DCA_2F1R,
00064 DCA_3F1R,
00065 DCA_2F2R,
00066 DCA_3F2R,
00067 DCA_4F2R
00068 };
00069
00070
00071 enum DCAExSSSpeakerMask {
00072 DCA_EXSS_FRONT_CENTER = 0x0001,
00073 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
00074 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
00075 DCA_EXSS_LFE = 0x0008,
00076 DCA_EXSS_REAR_CENTER = 0x0010,
00077 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
00078 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
00079 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
00080 DCA_EXSS_OVERHEAD = 0x0100,
00081 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
00082 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
00083 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
00084 DCA_EXSS_LFE2 = 0x1000,
00085 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
00086 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
00087 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
00088 };
00089
00090 enum DCAExtensionMask {
00091 DCA_EXT_CORE = 0x001,
00092 DCA_EXT_XXCH = 0x002,
00093 DCA_EXT_X96 = 0x004,
00094 DCA_EXT_XCH = 0x008,
00095 DCA_EXT_EXSS_CORE = 0x010,
00096 DCA_EXT_EXSS_XBR = 0x020,
00097 DCA_EXT_EXSS_XXCH = 0x040,
00098 DCA_EXT_EXSS_X96 = 0x080,
00099 DCA_EXT_EXSS_LBR = 0x100,
00100 DCA_EXT_EXSS_XLL = 0x200,
00101 };
00102
00103
00104 static const int dca_ext_audio_descr_mask[] = {
00105 DCA_EXT_XCH,
00106 -1,
00107 DCA_EXT_X96,
00108 DCA_EXT_XCH | DCA_EXT_X96,
00109 -1,
00110 -1,
00111 DCA_EXT_XXCH,
00112 -1,
00113 };
00114
00115
00116 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 static const int64_t dca_core_channel_layout[] = {
00129 AV_CH_FRONT_CENTER,
00130 AV_CH_LAYOUT_STEREO,
00131 AV_CH_LAYOUT_STEREO,
00132 AV_CH_LAYOUT_STEREO,
00133 AV_CH_LAYOUT_STEREO,
00134 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER,
00135 AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER,
00136 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER,
00137 AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,
00138 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,
00139 AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER,
00140 AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER,
00141 AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_BACK_CENTER|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT,
00142 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,
00143 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT,
00144 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_BACK_CENTER|AV_CH_SIDE_RIGHT,
00145 };
00146
00147 static const int8_t dca_lfe_index[] = {
00148 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
00149 };
00150
00151 static const int8_t dca_channel_reorder_lfe[][9] = {
00152 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
00153 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00154 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00155 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00156 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00157 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
00158 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00159 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
00160 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
00161 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
00162 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
00163 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
00164 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
00165 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
00166 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
00167 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
00168 };
00169
00170 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
00171 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
00172 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00173 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00174 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00175 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00176 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
00177 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
00178 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
00179 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
00180 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
00181 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
00182 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
00183 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
00184 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
00185 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
00186 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
00187 };
00188
00189 static const int8_t dca_channel_reorder_nolfe[][9] = {
00190 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
00191 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00192 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00193 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00194 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00195 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
00196 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00197 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
00198 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
00199 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
00200 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
00201 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
00202 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
00203 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
00204 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
00205 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
00206 };
00207
00208 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
00209 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00210 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00211 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00212 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00213 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00214 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
00215 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
00216 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
00217 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
00218 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
00219 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
00220 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
00221 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
00222 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
00223 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
00224 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
00225 };
00226
00227 #define DCA_DOLBY 101
00228
00229 #define DCA_CHANNEL_BITS 6
00230 #define DCA_CHANNEL_MASK 0x3F
00231
00232 #define DCA_LFE 0x80
00233
00234 #define HEADER_SIZE 14
00235
00236 #define DCA_MAX_FRAME_SIZE 16384
00237 #define DCA_MAX_EXSS_HEADER_SIZE 4096
00238
00239 #define DCA_BUFFER_PADDING_SIZE 1024
00240
00242 typedef struct {
00243 int offset;
00244 int maxbits[8];
00245 int wrap;
00246 VLC vlc[8];
00247 } BitAlloc;
00248
00249 static BitAlloc dca_bitalloc_index;
00250 static BitAlloc dca_tmode;
00251 static BitAlloc dca_scalefactor;
00252 static BitAlloc dca_smpl_bitalloc[11];
00253
00254 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
00255 {
00256 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
00257 }
00258
00259 typedef struct {
00260 AVCodecContext *avctx;
00261
00262 int frame_type;
00263 int samples_deficit;
00264 int crc_present;
00265 int sample_blocks;
00266 int frame_size;
00267 int amode;
00268 int sample_rate;
00269 int bit_rate;
00270 int bit_rate_index;
00271
00272 int downmix;
00273 int dynrange;
00274 int timestamp;
00275 int aux_data;
00276 int hdcd;
00277 int ext_descr;
00278 int ext_coding;
00279 int aspf;
00280 int lfe;
00281 int predictor_history;
00282 int header_crc;
00283 int multirate_inter;
00284 int version;
00285 int copy_history;
00286 int source_pcm_res;
00287 int front_sum;
00288 int surround_sum;
00289 int dialog_norm;
00290
00291
00292 int subframes;
00293 int is_channels_set;
00294 int total_channels;
00295 int prim_channels;
00296 int subband_activity[DCA_PRIM_CHANNELS_MAX];
00297 int vq_start_subband[DCA_PRIM_CHANNELS_MAX];
00298 int joint_intensity[DCA_PRIM_CHANNELS_MAX];
00299 int transient_huffman[DCA_PRIM_CHANNELS_MAX];
00300 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX];
00301 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];
00302 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
00303 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
00304
00305
00306 int subsubframes[DCA_SUBFRAMES_MAX];
00307 int partial_samples[DCA_SUBFRAMES_MAX];
00308 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00309 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00310 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00311 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00312 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];
00313 int joint_huff[DCA_PRIM_CHANNELS_MAX];
00314 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00315 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];
00316 int dynrange_coef;
00317
00318 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00319
00320 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];
00321 int lfe_scale_factor;
00322
00323
00324 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
00325 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
00326 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
00327 int hist_index[DCA_PRIM_CHANNELS_MAX];
00328 DECLARE_ALIGNED(32, float, raXin)[32];
00329
00330 int output;
00331 float scale_bias;
00332
00333 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
00334 DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256];
00335 const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
00336
00337 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
00338 int dca_buffer_size;
00339
00340 const int8_t* channel_order_tab;
00341 GetBitContext gb;
00342
00343 int current_subframe;
00344 int current_subsubframe;
00345
00346 int core_ext_mask;
00347
00348
00349 int xch_present;
00350 int xch_base_channel;
00351
00352
00353 int static_fields;
00354 int mix_metadata;
00355 int num_mix_configs;
00356 int mix_config_num_ch[4];
00357
00358 int profile;
00359
00360 int debug_flag;
00361 DSPContext dsp;
00362 FFTContext imdct;
00363 SynthFilterContext synth;
00364 DCADSPContext dcadsp;
00365 FmtConvertContext fmt_conv;
00366 } DCAContext;
00367
00368 static const uint16_t dca_vlc_offs[] = {
00369 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
00370 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
00371 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
00372 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
00373 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
00374 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
00375 };
00376
00377 static av_cold void dca_init_vlcs(void)
00378 {
00379 static int vlcs_initialized = 0;
00380 int i, j, c = 14;
00381 static VLC_TYPE dca_table[23622][2];
00382
00383 if (vlcs_initialized)
00384 return;
00385
00386 dca_bitalloc_index.offset = 1;
00387 dca_bitalloc_index.wrap = 2;
00388 for (i = 0; i < 5; i++) {
00389 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
00390 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
00391 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
00392 bitalloc_12_bits[i], 1, 1,
00393 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
00394 }
00395 dca_scalefactor.offset = -64;
00396 dca_scalefactor.wrap = 2;
00397 for (i = 0; i < 5; i++) {
00398 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
00399 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
00400 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
00401 scales_bits[i], 1, 1,
00402 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
00403 }
00404 dca_tmode.offset = 0;
00405 dca_tmode.wrap = 1;
00406 for (i = 0; i < 4; i++) {
00407 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
00408 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
00409 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
00410 tmode_bits[i], 1, 1,
00411 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
00412 }
00413
00414 for (i = 0; i < 10; i++)
00415 for (j = 0; j < 7; j++){
00416 if (!bitalloc_codes[i][j]) break;
00417 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
00418 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
00419 dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
00420 dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
00421 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
00422 bitalloc_sizes[i],
00423 bitalloc_bits[i][j], 1, 1,
00424 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
00425 c++;
00426 }
00427 vlcs_initialized = 1;
00428 }
00429
00430 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
00431 {
00432 while(len--)
00433 *dst++ = get_bits(gb, bits);
00434 }
00435
00436 static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
00437 {
00438 int i, j;
00439 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
00440 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
00441 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
00442
00443 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
00444 s->prim_channels = s->total_channels;
00445
00446 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
00447 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
00448
00449
00450 for (i = base_channel; i < s->prim_channels; i++) {
00451 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
00452 if (s->subband_activity[i] > DCA_SUBBANDS)
00453 s->subband_activity[i] = DCA_SUBBANDS;
00454 }
00455 for (i = base_channel; i < s->prim_channels; i++) {
00456 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
00457 if (s->vq_start_subband[i] > DCA_SUBBANDS)
00458 s->vq_start_subband[i] = DCA_SUBBANDS;
00459 }
00460 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
00461 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
00462 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
00463 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
00464
00465
00466 if (!base_channel)
00467 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
00468 for (j = 1; j < 11; j++)
00469 for (i = base_channel; i < s->prim_channels; i++)
00470 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
00471
00472
00473 for (j = 0; j < 11; j++)
00474 for (i = base_channel; i < s->prim_channels; i++)
00475 s->scalefactor_adj[i][j] = 1;
00476
00477 for (j = 1; j < 11; j++)
00478 for (i = base_channel; i < s->prim_channels; i++)
00479 if (s->quant_index_huffman[i][j] < thr[j])
00480 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
00481
00482 if (s->crc_present) {
00483
00484 get_bits(&s->gb, 16);
00485 }
00486
00487 s->current_subframe = 0;
00488 s->current_subsubframe = 0;
00489
00490 #ifdef TRACE
00491 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
00492 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
00493 for (i = base_channel; i < s->prim_channels; i++){
00494 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
00495 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
00496 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
00497 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
00498 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
00499 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
00500 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
00501 for (j = 0; j < 11; j++)
00502 av_log(s->avctx, AV_LOG_DEBUG, " %i",
00503 s->quant_index_huffman[i][j]);
00504 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00505 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
00506 for (j = 0; j < 11; j++)
00507 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
00508 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00509 }
00510 #endif
00511
00512 return 0;
00513 }
00514
00515 static int dca_parse_frame_header(DCAContext * s)
00516 {
00517 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
00518
00519
00520 get_bits(&s->gb, 32);
00521
00522
00523 s->frame_type = get_bits(&s->gb, 1);
00524 s->samples_deficit = get_bits(&s->gb, 5) + 1;
00525 s->crc_present = get_bits(&s->gb, 1);
00526 s->sample_blocks = get_bits(&s->gb, 7) + 1;
00527 s->frame_size = get_bits(&s->gb, 14) + 1;
00528 if (s->frame_size < 95)
00529 return -1;
00530 s->amode = get_bits(&s->gb, 6);
00531 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
00532 if (!s->sample_rate)
00533 return -1;
00534 s->bit_rate_index = get_bits(&s->gb, 5);
00535 s->bit_rate = dca_bit_rates[s->bit_rate_index];
00536 if (!s->bit_rate)
00537 return -1;
00538
00539 s->downmix = get_bits(&s->gb, 1);
00540 s->dynrange = get_bits(&s->gb, 1);
00541 s->timestamp = get_bits(&s->gb, 1);
00542 s->aux_data = get_bits(&s->gb, 1);
00543 s->hdcd = get_bits(&s->gb, 1);
00544 s->ext_descr = get_bits(&s->gb, 3);
00545 s->ext_coding = get_bits(&s->gb, 1);
00546 s->aspf = get_bits(&s->gb, 1);
00547 s->lfe = get_bits(&s->gb, 2);
00548 s->predictor_history = get_bits(&s->gb, 1);
00549
00550
00551 if (s->crc_present)
00552 s->header_crc = get_bits(&s->gb, 16);
00553
00554 s->multirate_inter = get_bits(&s->gb, 1);
00555 s->version = get_bits(&s->gb, 4);
00556 s->copy_history = get_bits(&s->gb, 2);
00557 s->source_pcm_res = get_bits(&s->gb, 3);
00558 s->front_sum = get_bits(&s->gb, 1);
00559 s->surround_sum = get_bits(&s->gb, 1);
00560 s->dialog_norm = get_bits(&s->gb, 4);
00561
00562
00563 s->output = s->amode;
00564 if (s->lfe) s->output |= DCA_LFE;
00565
00566 #ifdef TRACE
00567 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
00568 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
00569 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
00570 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
00571 s->sample_blocks, s->sample_blocks * 32);
00572 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
00573 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
00574 s->amode, dca_channels[s->amode]);
00575 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
00576 s->sample_rate);
00577 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
00578 s->bit_rate);
00579 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
00580 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
00581 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
00582 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
00583 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
00584 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
00585 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
00586 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
00587 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
00588 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
00589 s->predictor_history);
00590 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
00591 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
00592 s->multirate_inter);
00593 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
00594 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
00595 av_log(s->avctx, AV_LOG_DEBUG,
00596 "source pcm resolution: %i (%i bits/sample)\n",
00597 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
00598 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
00599 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
00600 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
00601 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00602 #endif
00603
00604
00605 s->subframes = get_bits(&s->gb, 4) + 1;
00606
00607 return dca_parse_audio_coding_header(s, 0);
00608 }
00609
00610
00611 static inline int get_scale(GetBitContext *gb, int level, int value)
00612 {
00613 if (level < 5) {
00614
00615 value += get_bitalloc(gb, &dca_scalefactor, level);
00616 } else if (level < 8)
00617 value = get_bits(gb, level + 1);
00618 return value;
00619 }
00620
00621 static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
00622 {
00623
00624 int j, k;
00625
00626 if (get_bits_left(&s->gb) < 0)
00627 return -1;
00628
00629 if (!base_channel) {
00630 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
00631 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
00632 }
00633
00634 for (j = base_channel; j < s->prim_channels; j++) {
00635 for (k = 0; k < s->subband_activity[j]; k++)
00636 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
00637 }
00638
00639
00640 for (j = base_channel; j < s->prim_channels; j++) {
00641 for (k = 0; k < s->subband_activity[j]; k++) {
00642 if (s->prediction_mode[j][k] > 0) {
00643
00644 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
00645 }
00646 }
00647 }
00648
00649
00650 for (j = base_channel; j < s->prim_channels; j++) {
00651 for (k = 0; k < s->vq_start_subband[j]; k++) {
00652 if (s->bitalloc_huffman[j] == 6)
00653 s->bitalloc[j][k] = get_bits(&s->gb, 5);
00654 else if (s->bitalloc_huffman[j] == 5)
00655 s->bitalloc[j][k] = get_bits(&s->gb, 4);
00656 else if (s->bitalloc_huffman[j] == 7) {
00657 av_log(s->avctx, AV_LOG_ERROR,
00658 "Invalid bit allocation index\n");
00659 return -1;
00660 } else {
00661 s->bitalloc[j][k] =
00662 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
00663 }
00664
00665 if (s->bitalloc[j][k] > 26) {
00666
00667
00668 return -1;
00669 }
00670 }
00671 }
00672
00673
00674 for (j = base_channel; j < s->prim_channels; j++) {
00675 for (k = 0; k < s->subband_activity[j]; k++) {
00676 s->transition_mode[j][k] = 0;
00677 if (s->subsubframes[s->current_subframe] > 1 &&
00678 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
00679 s->transition_mode[j][k] =
00680 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
00681 }
00682 }
00683 }
00684
00685 if (get_bits_left(&s->gb) < 0)
00686 return -1;
00687
00688 for (j = base_channel; j < s->prim_channels; j++) {
00689 const uint32_t *scale_table;
00690 int scale_sum;
00691
00692 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
00693
00694 if (s->scalefactor_huffman[j] == 6)
00695 scale_table = scale_factor_quant7;
00696 else
00697 scale_table = scale_factor_quant6;
00698
00699
00700 scale_sum = 0;
00701
00702 for (k = 0; k < s->subband_activity[j]; k++) {
00703 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
00704 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
00705 s->scale_factor[j][k][0] = scale_table[scale_sum];
00706 }
00707
00708 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
00709
00710 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
00711 s->scale_factor[j][k][1] = scale_table[scale_sum];
00712 }
00713 }
00714 }
00715
00716
00717 for (j = base_channel; j < s->prim_channels; j++) {
00718
00719 if (s->joint_intensity[j] > 0)
00720 s->joint_huff[j] = get_bits(&s->gb, 3);
00721 }
00722
00723 if (get_bits_left(&s->gb) < 0)
00724 return -1;
00725
00726
00727 for (j = base_channel; j < s->prim_channels; j++) {
00728 int source_channel;
00729
00730
00731 if (s->joint_intensity[j] > 0) {
00732 int scale = 0;
00733 source_channel = s->joint_intensity[j] - 1;
00734
00735
00736
00737
00738 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
00739 scale = get_scale(&s->gb, s->joint_huff[j], 0);
00740 scale += 64;
00741 s->joint_scale_factor[j][k] = scale;
00742 }
00743
00744 if (!(s->debug_flag & 0x02)) {
00745 av_log(s->avctx, AV_LOG_DEBUG,
00746 "Joint stereo coding not supported\n");
00747 s->debug_flag |= 0x02;
00748 }
00749 }
00750 }
00751
00752
00753 if (!base_channel && s->prim_channels > 2) {
00754 if (s->downmix) {
00755 for (j = base_channel; j < s->prim_channels; j++) {
00756 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
00757 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
00758 }
00759 } else {
00760 int am = s->amode & DCA_CHANNEL_MASK;
00761 for (j = base_channel; j < s->prim_channels; j++) {
00762 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
00763 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
00764 }
00765 }
00766 }
00767
00768
00769 if (!base_channel && s->dynrange)
00770 s->dynrange_coef = get_bits(&s->gb, 8);
00771
00772
00773 if (s->crc_present) {
00774 get_bits(&s->gb, 16);
00775 }
00776
00777
00778
00779
00780
00781
00782 for (j = base_channel; j < s->prim_channels; j++)
00783 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00784
00785 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
00786
00787
00788 if (!base_channel && s->lfe) {
00789
00790 int lfe_samples = 2 * s->lfe * (4 + block_index);
00791 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
00792 float lfe_scale;
00793
00794 for (j = lfe_samples; j < lfe_end_sample; j++) {
00795
00796 s->lfe_data[j] = get_sbits(&s->gb, 8);
00797 }
00798
00799
00800 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
00801
00802
00803 lfe_scale = 0.035 * s->lfe_scale_factor;
00804
00805 for (j = lfe_samples; j < lfe_end_sample; j++)
00806 s->lfe_data[j] *= lfe_scale;
00807 }
00808
00809 #ifdef TRACE
00810 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
00811 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
00812 s->partial_samples[s->current_subframe]);
00813 for (j = base_channel; j < s->prim_channels; j++) {
00814 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
00815 for (k = 0; k < s->subband_activity[j]; k++)
00816 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
00817 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00818 }
00819 for (j = base_channel; j < s->prim_channels; j++) {
00820 for (k = 0; k < s->subband_activity[j]; k++)
00821 av_log(s->avctx, AV_LOG_DEBUG,
00822 "prediction coefs: %f, %f, %f, %f\n",
00823 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
00824 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
00825 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
00826 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
00827 }
00828 for (j = base_channel; j < s->prim_channels; j++) {
00829 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
00830 for (k = 0; k < s->vq_start_subband[j]; k++)
00831 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
00832 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00833 }
00834 for (j = base_channel; j < s->prim_channels; j++) {
00835 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
00836 for (k = 0; k < s->subband_activity[j]; k++)
00837 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
00838 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00839 }
00840 for (j = base_channel; j < s->prim_channels; j++) {
00841 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
00842 for (k = 0; k < s->subband_activity[j]; k++) {
00843 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
00844 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
00845 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
00846 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
00847 }
00848 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00849 }
00850 for (j = base_channel; j < s->prim_channels; j++) {
00851 if (s->joint_intensity[j] > 0) {
00852 int source_channel = s->joint_intensity[j] - 1;
00853 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
00854 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
00855 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
00856 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00857 }
00858 }
00859 if (!base_channel && s->prim_channels > 2 && s->downmix) {
00860 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
00861 for (j = 0; j < s->prim_channels; j++) {
00862 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
00863 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
00864 }
00865 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00866 }
00867 for (j = base_channel; j < s->prim_channels; j++)
00868 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00869 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
00870 if (!base_channel && s->lfe) {
00871 int lfe_samples = 2 * s->lfe * (4 + block_index);
00872 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
00873
00874 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
00875 for (j = lfe_samples; j < lfe_end_sample; j++)
00876 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
00877 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00878 }
00879 #endif
00880
00881 return 0;
00882 }
00883
00884 static void qmf_32_subbands(DCAContext * s, int chans,
00885 float samples_in[32][8], float *samples_out,
00886 float scale)
00887 {
00888 const float *prCoeff;
00889 int i;
00890
00891 int sb_act = s->subband_activity[chans];
00892 int subindex;
00893
00894 scale *= sqrt(1/8.0);
00895
00896
00897 if (!s->multirate_inter)
00898 prCoeff = fir_32bands_nonperfect;
00899 else
00900 prCoeff = fir_32bands_perfect;
00901
00902 for (i = sb_act; i < 32; i++)
00903 s->raXin[i] = 0.0;
00904
00905
00906 for (subindex = 0; subindex < 8; subindex++) {
00907
00908 for (i = 0; i < sb_act; i++){
00909 unsigned sign = (i - 1) & 2;
00910 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
00911 AV_WN32A(&s->raXin[i], v);
00912 }
00913
00914 s->synth.synth_filter_float(&s->imdct,
00915 s->subband_fir_hist[chans], &s->hist_index[chans],
00916 s->subband_fir_noidea[chans], prCoeff,
00917 samples_out, s->raXin, scale);
00918 samples_out+= 32;
00919
00920 }
00921 }
00922
00923 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
00924 int num_deci_sample, float *samples_in,
00925 float *samples_out, float scale)
00926 {
00927
00928
00929
00930
00931
00932
00933
00934
00935 int decifactor;
00936 const float *prCoeff;
00937 int deciindex;
00938
00939
00940 if (decimation_select == 1) {
00941 decifactor = 64;
00942 prCoeff = lfe_fir_128;
00943 } else {
00944 decifactor = 32;
00945 prCoeff = lfe_fir_64;
00946 }
00947
00948 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
00949 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
00950 scale);
00951 samples_in++;
00952 samples_out += 2 * decifactor;
00953 }
00954 }
00955
00956
00957 #define MIX_REAR1(samples, si1, rs, coef) \
00958 samples[i] += samples[si1] * coef[rs][0]; \
00959 samples[i+256] += samples[si1] * coef[rs][1];
00960
00961 #define MIX_REAR2(samples, si1, si2, rs, coef) \
00962 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
00963 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
00964
00965 #define MIX_FRONT3(samples, coef) \
00966 t = samples[i+c]; \
00967 u = samples[i+l]; \
00968 v = samples[i+r]; \
00969 samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
00970 samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
00971
00972 #define DOWNMIX_TO_STEREO(op1, op2) \
00973 for (i = 0; i < 256; i++){ \
00974 op1 \
00975 op2 \
00976 }
00977
00978 static void dca_downmix(float *samples, int srcfmt,
00979 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
00980 const int8_t *channel_mapping)
00981 {
00982 int c,l,r,sl,sr,s;
00983 int i;
00984 float t, u, v;
00985 float coef[DCA_PRIM_CHANNELS_MAX][2];
00986
00987 for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
00988 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
00989 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
00990 }
00991
00992 switch (srcfmt) {
00993 case DCA_MONO:
00994 case DCA_CHANNEL:
00995 case DCA_STEREO_TOTAL:
00996 case DCA_STEREO_SUMDIFF:
00997 case DCA_4F2R:
00998 av_log(NULL, 0, "Not implemented!\n");
00999 break;
01000 case DCA_STEREO:
01001 break;
01002 case DCA_3F:
01003 c = channel_mapping[0] * 256;
01004 l = channel_mapping[1] * 256;
01005 r = channel_mapping[2] * 256;
01006 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
01007 break;
01008 case DCA_2F1R:
01009 s = channel_mapping[2] * 256;
01010 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef),);
01011 break;
01012 case DCA_3F1R:
01013 c = channel_mapping[0] * 256;
01014 l = channel_mapping[1] * 256;
01015 r = channel_mapping[2] * 256;
01016 s = channel_mapping[3] * 256;
01017 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
01018 MIX_REAR1(samples, i + s, 3, coef));
01019 break;
01020 case DCA_2F2R:
01021 sl = channel_mapping[2] * 256;
01022 sr = channel_mapping[3] * 256;
01023 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef),);
01024 break;
01025 case DCA_3F2R:
01026 c = channel_mapping[0] * 256;
01027 l = channel_mapping[1] * 256;
01028 r = channel_mapping[2] * 256;
01029 sl = channel_mapping[3] * 256;
01030 sr = channel_mapping[4] * 256;
01031 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
01032 MIX_REAR2(samples, i + sl, i + sr, 3, coef));
01033 break;
01034 }
01035 }
01036
01037
01038
01039
01040 static int decode_blockcode(int code, int levels, int *values)
01041 {
01042 int i;
01043 int offset = (levels - 1) >> 1;
01044
01045 for (i = 0; i < 4; i++) {
01046 int div = FASTDIV(code, levels);
01047 values[i] = code - offset - div*levels;
01048 code = div;
01049 }
01050
01051 if (code == 0)
01052 return 0;
01053 else {
01054 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
01055 return -1;
01056 }
01057 }
01058
01059 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
01060 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
01061
01062 static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
01063 {
01064 int k, l;
01065 int subsubframe = s->current_subsubframe;
01066
01067 const float *quant_step_table;
01068
01069
01070 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
01071 LOCAL_ALIGNED_16(int, block, [8]);
01072
01073
01074
01075
01076
01077
01078 if (s->bit_rate_index == 0x1f)
01079 quant_step_table = lossless_quant_d;
01080 else
01081 quant_step_table = lossy_quant_d;
01082
01083 for (k = base_channel; k < s->prim_channels; k++) {
01084 if (get_bits_left(&s->gb) < 0)
01085 return -1;
01086
01087 for (l = 0; l < s->vq_start_subband[k]; l++) {
01088 int m;
01089
01090
01091 int abits = s->bitalloc[k][l];
01092
01093 float quant_step_size = quant_step_table[abits];
01094
01095
01096
01097
01098
01099
01100 int sel = s->quant_index_huffman[k][abits];
01101
01102
01103
01104
01105 if (!abits){
01106 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
01107 } else {
01108
01109 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
01110 float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
01111
01112 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
01113 if (abits <= 7){
01114
01115 int block_code1, block_code2, size, levels;
01116
01117 size = abits_sizes[abits-1];
01118 levels = abits_levels[abits-1];
01119
01120 block_code1 = get_bits(&s->gb, size);
01121
01122 decode_blockcode(block_code1, levels, block);
01123 block_code2 = get_bits(&s->gb, size);
01124 decode_blockcode(block_code2, levels, &block[4]);
01125 }else{
01126
01127 for (m = 0; m < 8; m++)
01128 block[m] = get_sbits(&s->gb, abits - 3);
01129 }
01130 }else{
01131
01132 for (m = 0; m < 8; m++)
01133 block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
01134 }
01135
01136 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
01137 block, rscale, 8);
01138 }
01139
01140
01141
01142
01143 if (s->prediction_mode[k][l]) {
01144 int n;
01145 for (m = 0; m < 8; m++) {
01146 for (n = 1; n <= 4; n++)
01147 if (m >= n)
01148 subband_samples[k][l][m] +=
01149 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
01150 subband_samples[k][l][m - n] / 8192);
01151 else if (s->predictor_history)
01152 subband_samples[k][l][m] +=
01153 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
01154 s->subband_samples_hist[k][l][m - n +
01155 4] / 8192);
01156 }
01157 }
01158 }
01159
01160
01161
01162
01163 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
01164
01165
01166 int m;
01167
01168 if (!s->debug_flag & 0x01) {
01169 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
01170 s->debug_flag |= 0x01;
01171 }
01172
01173 for (m = 0; m < 8; m++) {
01174 subband_samples[k][l][m] =
01175 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
01176 m]
01177 * (float) s->scale_factor[k][l][0] / 16.0;
01178 }
01179 }
01180 }
01181
01182
01183 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
01184 if (0xFFFF == get_bits(&s->gb, 16)) {
01185 #ifdef TRACE
01186 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
01187 #endif
01188 } else {
01189 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
01190 }
01191 }
01192
01193
01194 for (k = base_channel; k < s->prim_channels; k++)
01195 for (l = 0; l < s->vq_start_subband[k]; l++)
01196 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
01197 4 * sizeof(subband_samples[0][0][0]));
01198
01199 return 0;
01200 }
01201
01202 static int dca_filter_channels(DCAContext * s, int block_index)
01203 {
01204 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
01205 int k;
01206
01207
01208 for (k = 0; k < s->prim_channels; k++) {
01209
01210
01211 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
01212 M_SQRT1_2*s->scale_bias );
01213 }
01214
01215
01216 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
01217 dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
01218 }
01219
01220
01221 if (s->output & DCA_LFE) {
01222 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
01223 s->lfe_data + 2 * s->lfe * (block_index + 4),
01224 &s->samples[256 * dca_lfe_index[s->amode]],
01225 (1.0/256.0)*s->scale_bias);
01226
01227 }
01228
01229 return 0;
01230 }
01231
01232
01233 static int dca_subframe_footer(DCAContext * s, int base_channel)
01234 {
01235 int aux_data_count = 0, i;
01236
01237
01238
01239
01240
01241
01242 if (!base_channel) {
01243 if (s->timestamp)
01244 get_bits(&s->gb, 32);
01245
01246 if (s->aux_data)
01247 aux_data_count = get_bits(&s->gb, 6);
01248
01249 for (i = 0; i < aux_data_count; i++)
01250 get_bits(&s->gb, 8);
01251
01252 if (s->crc_present && (s->downmix || s->dynrange))
01253 get_bits(&s->gb, 16);
01254 }
01255
01256 return 0;
01257 }
01258
01265 static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
01266 {
01267
01268
01269 if (s->current_subframe >= s->subframes) {
01270 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
01271 s->current_subframe, s->subframes);
01272 return -1;
01273 }
01274
01275 if (!s->current_subsubframe) {
01276 #ifdef TRACE
01277 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
01278 #endif
01279
01280 if (dca_subframe_header(s, base_channel, block_index))
01281 return -1;
01282 }
01283
01284
01285 #ifdef TRACE
01286 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
01287 #endif
01288 if (dca_subsubframe(s, base_channel, block_index))
01289 return -1;
01290
01291
01292 s->current_subsubframe++;
01293 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
01294 s->current_subsubframe = 0;
01295 s->current_subframe++;
01296 }
01297 if (s->current_subframe >= s->subframes) {
01298 #ifdef TRACE
01299 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
01300 #endif
01301
01302 if (dca_subframe_footer(s, base_channel))
01303 return -1;
01304 }
01305
01306 return 0;
01307 }
01308
01312 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
01313 int max_size)
01314 {
01315 uint32_t mrk;
01316 int i, tmp;
01317 const uint16_t *ssrc = (const uint16_t *) src;
01318 uint16_t *sdst = (uint16_t *) dst;
01319 PutBitContext pb;
01320
01321 if ((unsigned)src_size > (unsigned)max_size) {
01322
01323
01324 src_size = max_size;
01325 }
01326
01327 mrk = AV_RB32(src);
01328 switch (mrk) {
01329 case DCA_MARKER_RAW_BE:
01330 memcpy(dst, src, src_size);
01331 return src_size;
01332 case DCA_MARKER_RAW_LE:
01333 for (i = 0; i < (src_size + 1) >> 1; i++)
01334 *sdst++ = av_bswap16(*ssrc++);
01335 return src_size;
01336 case DCA_MARKER_14B_BE:
01337 case DCA_MARKER_14B_LE:
01338 init_put_bits(&pb, dst, max_size);
01339 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
01340 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
01341 put_bits(&pb, 14, tmp);
01342 }
01343 flush_put_bits(&pb);
01344 return (put_bits_count(&pb) + 7) >> 3;
01345 default:
01346 return -1;
01347 }
01348 }
01349
01353 static int dca_exss_mask2count(int mask)
01354 {
01355
01356 return av_popcount(mask)
01357 + av_popcount(mask & (
01358 DCA_EXSS_CENTER_LEFT_RIGHT
01359 | DCA_EXSS_FRONT_LEFT_RIGHT
01360 | DCA_EXSS_FRONT_HIGH_LEFT_RIGHT
01361 | DCA_EXSS_WIDE_LEFT_RIGHT
01362 | DCA_EXSS_SIDE_LEFT_RIGHT
01363 | DCA_EXSS_SIDE_HIGH_LEFT_RIGHT
01364 | DCA_EXSS_SIDE_REAR_LEFT_RIGHT
01365 | DCA_EXSS_REAR_LEFT_RIGHT
01366 | DCA_EXSS_REAR_HIGH_LEFT_RIGHT
01367 ));
01368 }
01369
01373 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
01374 {
01375 int i;
01376
01377 for (i = 0; i < channels; i++) {
01378 int mix_map_mask = get_bits(gb, out_ch);
01379 int num_coeffs = av_popcount(mix_map_mask);
01380 skip_bits_long(gb, num_coeffs * 6);
01381 }
01382 }
01383
01387 static int dca_exss_parse_asset_header(DCAContext *s)
01388 {
01389 int header_pos = get_bits_count(&s->gb);
01390 int header_size;
01391 int channels;
01392 int embedded_stereo = 0;
01393 int embedded_6ch = 0;
01394 int drc_code_present;
01395 int extensions_mask;
01396 int i, j;
01397
01398 if (get_bits_left(&s->gb) < 16)
01399 return -1;
01400
01401
01402
01403
01404 header_size = get_bits(&s->gb, 9) + 1;
01405 skip_bits(&s->gb, 3);
01406
01407 if (s->static_fields) {
01408 if (get_bits1(&s->gb))
01409 skip_bits(&s->gb, 4);
01410 if (get_bits1(&s->gb))
01411 skip_bits_long(&s->gb, 24);
01412
01413 if (get_bits1(&s->gb)) {
01414
01415
01416 int text_length = get_bits(&s->gb, 10) + 1;
01417 if (get_bits_left(&s->gb) < text_length * 8)
01418 return -1;
01419 skip_bits_long(&s->gb, text_length * 8);
01420 }
01421
01422 skip_bits(&s->gb, 5);
01423 skip_bits(&s->gb, 4);
01424 channels = get_bits(&s->gb, 8) + 1;
01425
01426 if (get_bits1(&s->gb)) {
01427 int spkr_remap_sets;
01428 int spkr_mask_size = 16;
01429 int num_spkrs[7];
01430
01431 if (channels > 2)
01432 embedded_stereo = get_bits1(&s->gb);
01433 if (channels > 6)
01434 embedded_6ch = get_bits1(&s->gb);
01435
01436 if (get_bits1(&s->gb)) {
01437 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
01438 skip_bits(&s->gb, spkr_mask_size);
01439 }
01440
01441 spkr_remap_sets = get_bits(&s->gb, 3);
01442
01443 for (i = 0; i < spkr_remap_sets; i++) {
01444
01445 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
01446 }
01447
01448 for (i = 0; i < spkr_remap_sets; i++) {
01449 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
01450 if (get_bits_left(&s->gb) < 0)
01451 return -1;
01452
01453 for (j = 0; j < num_spkrs[i]; j++) {
01454 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
01455 int num_dec_ch = av_popcount(remap_dec_ch_mask);
01456 skip_bits_long(&s->gb, num_dec_ch * 5);
01457 }
01458 }
01459
01460 } else {
01461 skip_bits(&s->gb, 3);
01462 }
01463 }
01464
01465 drc_code_present = get_bits1(&s->gb);
01466 if (drc_code_present)
01467 get_bits(&s->gb, 8);
01468
01469 if (get_bits1(&s->gb))
01470 skip_bits(&s->gb, 5);
01471
01472 if (drc_code_present && embedded_stereo)
01473 get_bits(&s->gb, 8);
01474
01475 if (s->mix_metadata && get_bits1(&s->gb)) {
01476 skip_bits(&s->gb, 1);
01477 skip_bits(&s->gb, 6);
01478
01479 if (get_bits(&s->gb, 2) != 3)
01480 skip_bits(&s->gb, 3);
01481 else
01482 skip_bits(&s->gb, 8);
01483
01484 if (get_bits1(&s->gb))
01485 for (i = 0; i < s->num_mix_configs; i++)
01486 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6);
01487 else
01488 skip_bits_long(&s->gb, s->num_mix_configs * 6);
01489
01490 for (i = 0; i < s->num_mix_configs; i++) {
01491 if (get_bits_left(&s->gb) < 0)
01492 return -1;
01493 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
01494 if (embedded_6ch)
01495 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
01496 if (embedded_stereo)
01497 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
01498 }
01499 }
01500
01501 switch (get_bits(&s->gb, 2)) {
01502 case 0: extensions_mask = get_bits(&s->gb, 12); break;
01503 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
01504 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
01505 case 3: extensions_mask = 0; break;
01506 }
01507
01508
01509
01510 if (get_bits_left(&s->gb) < 0)
01511 return -1;
01512
01513 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
01514 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
01515 return -1;
01516 }
01517 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
01518
01519 if (extensions_mask & DCA_EXT_EXSS_XLL)
01520 s->profile = FF_PROFILE_DTS_HD_MA;
01521 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
01522 DCA_EXT_EXSS_XXCH))
01523 s->profile = FF_PROFILE_DTS_HD_HRA;
01524
01525 if (!(extensions_mask & DCA_EXT_CORE))
01526 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
01527 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
01528 av_log(s->avctx, AV_LOG_WARNING, "DTS extensions detection mismatch (%d, %d)\n",
01529 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
01530
01531 return 0;
01532 }
01533
01537 static void dca_exss_parse_header(DCAContext *s)
01538 {
01539 int ss_index;
01540 int blownup;
01541 int num_audiop = 1;
01542 int num_assets = 1;
01543 int active_ss_mask[8];
01544 int i, j;
01545
01546 if (get_bits_left(&s->gb) < 52)
01547 return;
01548
01549 skip_bits(&s->gb, 8);
01550 ss_index = get_bits(&s->gb, 2);
01551
01552 blownup = get_bits1(&s->gb);
01553 skip_bits(&s->gb, 8 + 4 * blownup);
01554 skip_bits(&s->gb, 16 + 4 * blownup);
01555
01556 s->static_fields = get_bits1(&s->gb);
01557 if (s->static_fields) {
01558 skip_bits(&s->gb, 2);
01559 skip_bits(&s->gb, 3);
01560
01561 if (get_bits1(&s->gb))
01562 skip_bits_long(&s->gb, 36);
01563
01564
01565
01566
01567 num_audiop = get_bits(&s->gb, 3) + 1;
01568 if (num_audiop > 1) {
01569 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
01570
01571 return;
01572 }
01573
01574 num_assets = get_bits(&s->gb, 3) + 1;
01575 if (num_assets > 1) {
01576 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
01577
01578 return;
01579 }
01580
01581 for (i = 0; i < num_audiop; i++)
01582 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
01583
01584 for (i = 0; i < num_audiop; i++)
01585 for (j = 0; j <= ss_index; j++)
01586 if (active_ss_mask[i] & (1 << j))
01587 skip_bits(&s->gb, 8);
01588
01589 s->mix_metadata = get_bits1(&s->gb);
01590 if (s->mix_metadata) {
01591 int mix_out_mask_size;
01592
01593 skip_bits(&s->gb, 2);
01594 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
01595 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
01596
01597 for (i = 0; i < s->num_mix_configs; i++) {
01598 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
01599 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
01600 }
01601 }
01602 }
01603
01604 for (i = 0; i < num_assets; i++)
01605 skip_bits_long(&s->gb, 16 + 4 * blownup);
01606
01607 for (i = 0; i < num_assets; i++) {
01608 if (dca_exss_parse_asset_header(s))
01609 return;
01610 }
01611
01612
01613
01614 }
01615
01620 static int dca_decode_frame(AVCodecContext * avctx,
01621 void *data, int *data_size,
01622 AVPacket *avpkt)
01623 {
01624 const uint8_t *buf = avpkt->data;
01625 int buf_size = avpkt->size;
01626
01627 int lfe_samples;
01628 int num_core_channels = 0;
01629 int i;
01630 float *samples_flt = data;
01631 int16_t *samples_s16 = data;
01632 int out_size;
01633 DCAContext *s = avctx->priv_data;
01634 int channels;
01635 int core_ss_end;
01636
01637
01638 s->xch_present = 0;
01639
01640 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer,
01641 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
01642 if (s->dca_buffer_size == -1) {
01643 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
01644 return -1;
01645 }
01646
01647 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
01648 if (dca_parse_frame_header(s) < 0) {
01649
01650 *data_size=0;
01651 return buf_size;
01652 }
01653
01654 avctx->sample_rate = s->sample_rate;
01655 avctx->bit_rate = s->bit_rate;
01656 avctx->frame_size = s->sample_blocks * 32;
01657
01658 s->profile = FF_PROFILE_DTS;
01659
01660 for (i = 0; i < (s->sample_blocks / 8); i++) {
01661 dca_decode_block(s, 0, i);
01662 }
01663
01664
01665 num_core_channels = s->prim_channels;
01666
01667 if (s->ext_coding)
01668 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
01669 else
01670 s->core_ext_mask = 0;
01671
01672 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
01673
01674
01675
01676 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
01677
01678
01679
01680 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
01681
01682
01683 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
01684
01685 while(core_ss_end - get_bits_count(&s->gb) >= 32) {
01686 uint32_t bits = get_bits_long(&s->gb, 32);
01687
01688 switch(bits) {
01689 case 0x5a5a5a5a: {
01690 int ext_amode, xch_fsize;
01691
01692 s->xch_base_channel = s->prim_channels;
01693
01694
01695 xch_fsize = show_bits(&s->gb, 10);
01696 if((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
01697 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
01698 continue;
01699
01700
01701 skip_bits(&s->gb, 10);
01702
01703 s->core_ext_mask |= DCA_EXT_XCH;
01704
01705
01706
01707 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
01708 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
01709 " supported!\n",ext_amode);
01710 continue;
01711 }
01712
01713
01714 dca_parse_audio_coding_header(s, s->xch_base_channel);
01715
01716 for (i = 0; i < (s->sample_blocks / 8); i++) {
01717 dca_decode_block(s, s->xch_base_channel, i);
01718 }
01719
01720 s->xch_present = 1;
01721 break;
01722 }
01723 case 0x47004a03:
01724
01725
01726
01727 s->core_ext_mask |= DCA_EXT_XXCH;
01728 break;
01729
01730 case 0x1d95f262: {
01731 int fsize96 = show_bits(&s->gb, 12) + 1;
01732 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
01733 continue;
01734
01735 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n", get_bits_count(&s->gb));
01736 skip_bits(&s->gb, 12);
01737 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
01738 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
01739
01740 s->core_ext_mask |= DCA_EXT_X96;
01741 break;
01742 }
01743 }
01744
01745 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
01746 }
01747
01748 } else {
01749
01750 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
01751 }
01752
01753 if (s->core_ext_mask & DCA_EXT_X96)
01754 s->profile = FF_PROFILE_DTS_96_24;
01755 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
01756 s->profile = FF_PROFILE_DTS_ES;
01757
01758
01759 if (s->dca_buffer_size - s->frame_size > 32
01760 && get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
01761 dca_exss_parse_header(s);
01762
01763 avctx->profile = s->profile;
01764
01765 channels = s->prim_channels + !!s->lfe;
01766
01767 if (s->amode<16) {
01768 avctx->channel_layout = dca_core_channel_layout[s->amode];
01769
01770 if (s->xch_present && (!avctx->request_channels ||
01771 avctx->request_channels > num_core_channels + !!s->lfe)) {
01772 avctx->channel_layout |= AV_CH_BACK_CENTER;
01773 if (s->lfe) {
01774 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
01775 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
01776 } else {
01777 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
01778 }
01779 } else {
01780 channels = num_core_channels + !!s->lfe;
01781 s->xch_present = 0;
01782 if (s->lfe) {
01783 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
01784 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
01785 } else
01786 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
01787 }
01788
01789 if (channels > !!s->lfe &&
01790 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
01791 return -1;
01792
01793 if (avctx->request_channels == 2 && s->prim_channels > 2) {
01794 channels = 2;
01795 s->output = DCA_STEREO;
01796 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
01797 }
01798 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
01799 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
01800 s->channel_order_tab = dca_channel_order_native;
01801 }
01802 } else {
01803 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
01804 return -1;
01805 }
01806
01807
01808
01809
01810
01811
01812
01813 if (s->is_channels_set == 0) {
01814 s->is_channels_set = 1;
01815 avctx->channels = channels;
01816 }
01817 if (avctx->channels != channels) {
01818 av_log(avctx, AV_LOG_ERROR, "DCA decoder does not support number of "
01819 "channels changing in stream. Skipping frame.\n");
01820 return -1;
01821 }
01822
01823 out_size = 256 / 8 * s->sample_blocks * channels *
01824 av_get_bytes_per_sample(avctx->sample_fmt);
01825 if (*data_size < out_size)
01826 return -1;
01827 *data_size = out_size;
01828
01829
01830 for (i = 0; i < (s->sample_blocks / 8); i++) {
01831 dca_filter_channels(s, i);
01832
01833
01834
01835 if((s->source_pcm_res & 1) && s->xch_present) {
01836 float* back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
01837 float* lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
01838 float* rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
01839 int j;
01840 for(j = 0; j < 256; ++j) {
01841 lt_chan[j] -= back_chan[j] * M_SQRT1_2;
01842 rt_chan[j] -= back_chan[j] * M_SQRT1_2;
01843 }
01844 }
01845
01846 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
01847 s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
01848 channels);
01849 samples_flt += 256 * channels;
01850 } else {
01851 s->fmt_conv.float_to_int16_interleave(samples_s16,
01852 s->samples_chanptr, 256,
01853 channels);
01854 samples_s16 += 256 * channels;
01855 }
01856 }
01857
01858
01859 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
01860 for (i = 0; i < 2 * s->lfe * 4; i++) {
01861 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
01862 }
01863
01864 return buf_size;
01865 }
01866
01867
01868
01875 static av_cold int dca_decode_init(AVCodecContext * avctx)
01876 {
01877 DCAContext *s = avctx->priv_data;
01878 int i;
01879
01880 s->avctx = avctx;
01881 dca_init_vlcs();
01882
01883 dsputil_init(&s->dsp, avctx);
01884 ff_mdct_init(&s->imdct, 6, 1, 1.0);
01885 ff_synth_filter_init(&s->synth);
01886 ff_dcadsp_init(&s->dcadsp);
01887 ff_fmt_convert_init(&s->fmt_conv, avctx);
01888
01889 for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
01890 s->samples_chanptr[i] = s->samples + i * 256;
01891
01892 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
01893 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01894 s->scale_bias = 1.0 / 32768.0;
01895 } else {
01896 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01897 s->scale_bias = 1.0;
01898 }
01899
01900
01901 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
01902 avctx->request_channels == 2) {
01903 avctx->channels = avctx->request_channels;
01904 }
01905
01906 return 0;
01907 }
01908
01909 static av_cold int dca_decode_end(AVCodecContext * avctx)
01910 {
01911 DCAContext *s = avctx->priv_data;
01912 ff_mdct_end(&s->imdct);
01913 return 0;
01914 }
01915
01916 static const AVProfile profiles[] = {
01917 { FF_PROFILE_DTS, "DTS" },
01918 { FF_PROFILE_DTS_ES, "DTS-ES" },
01919 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
01920 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
01921 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
01922 { FF_PROFILE_UNKNOWN },
01923 };
01924
01925 AVCodec ff_dca_decoder = {
01926 .name = "dca",
01927 .type = AVMEDIA_TYPE_AUDIO,
01928 .id = CODEC_ID_DTS,
01929 .priv_data_size = sizeof(DCAContext),
01930 .init = dca_decode_init,
01931 .decode = dca_decode_frame,
01932 .close = dca_decode_end,
01933 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
01934 .capabilities = CODEC_CAP_CHANNEL_CONF,
01935 .sample_fmts = (const enum AVSampleFormat[]) {
01936 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
01937 },
01938 .profiles = NULL_IF_CONFIG_SMALL(profiles),
01939 };