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/float_dsp.h"
00031 #include "libavutil/intmath.h"
00032 #include "libavutil/intreadwrite.h"
00033 #include "libavutil/mathematics.h"
00034 #include "libavutil/audioconvert.h"
00035 #include "avcodec.h"
00036 #include "dsputil.h"
00037 #include "fft.h"
00038 #include "get_bits.h"
00039 #include "put_bits.h"
00040 #include "dcadata.h"
00041 #include "dcahuff.h"
00042 #include "dca.h"
00043 #include "dca_parser.h"
00044 #include "synth_filter.h"
00045 #include "dcadsp.h"
00046 #include "fmtconvert.h"
00047
00048 #if ARCH_ARM
00049 # include "arm/dca.h"
00050 #endif
00051
00052
00053
00054 #define DCA_PRIM_CHANNELS_MAX (7)
00055 #define DCA_SUBBANDS (64)
00056 #define DCA_ABITS_MAX (32)
00057 #define DCA_SUBSUBFRAMES_MAX (4)
00058 #define DCA_SUBFRAMES_MAX (16)
00059 #define DCA_BLOCKS_MAX (16)
00060 #define DCA_LFE_MAX (3)
00061 #define DCA_CHSETS_MAX (4)
00062 #define DCA_CHSET_CHANS_MAX (8)
00063
00064 enum DCAMode {
00065 DCA_MONO = 0,
00066 DCA_CHANNEL,
00067 DCA_STEREO,
00068 DCA_STEREO_SUMDIFF,
00069 DCA_STEREO_TOTAL,
00070 DCA_3F,
00071 DCA_2F1R,
00072 DCA_3F1R,
00073 DCA_2F2R,
00074 DCA_3F2R,
00075 DCA_4F2R
00076 };
00077
00078
00079 enum DCAExSSSpeakerMask {
00080 DCA_EXSS_FRONT_CENTER = 0x0001,
00081 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
00082 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
00083 DCA_EXSS_LFE = 0x0008,
00084 DCA_EXSS_REAR_CENTER = 0x0010,
00085 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
00086 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
00087 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
00088 DCA_EXSS_OVERHEAD = 0x0100,
00089 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
00090 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
00091 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
00092 DCA_EXSS_LFE2 = 0x1000,
00093 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
00094 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
00095 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
00096 };
00097
00098 enum DCAXxchSpeakerMask {
00099 DCA_XXCH_FRONT_CENTER = 0x0000001,
00100 DCA_XXCH_FRONT_LEFT = 0x0000002,
00101 DCA_XXCH_FRONT_RIGHT = 0x0000004,
00102 DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
00103 DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
00104 DCA_XXCH_LFE1 = 0x0000020,
00105 DCA_XXCH_REAR_CENTER = 0x0000040,
00106 DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
00107 DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
00108 DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
00109 DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
00110 DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
00111 DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
00112 DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
00113 DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
00114 DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
00115 DCA_XXCH_LFE2 = 0x0010000,
00116 DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
00117 DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
00118 DCA_XXCH_OVERHEAD = 0x0080000,
00119 DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
00120 DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
00121 DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
00122 DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
00123 DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
00124 DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
00125 DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
00126 DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
00127 };
00128
00129 static const uint32_t map_xxch_to_native[28] = {
00130 AV_CH_FRONT_CENTER,
00131 AV_CH_FRONT_LEFT,
00132 AV_CH_FRONT_RIGHT,
00133 AV_CH_SIDE_LEFT,
00134 AV_CH_SIDE_RIGHT,
00135 AV_CH_LOW_FREQUENCY,
00136 AV_CH_BACK_CENTER,
00137 AV_CH_BACK_LEFT,
00138 AV_CH_BACK_RIGHT,
00139 AV_CH_SIDE_LEFT,
00140 AV_CH_SIDE_RIGHT,
00141 AV_CH_FRONT_LEFT_OF_CENTER,
00142 AV_CH_FRONT_RIGHT_OF_CENTER,
00143 AV_CH_TOP_FRONT_LEFT,
00144 AV_CH_TOP_FRONT_CENTER,
00145 AV_CH_TOP_FRONT_RIGHT,
00146 AV_CH_LOW_FREQUENCY,
00147 AV_CH_FRONT_LEFT_OF_CENTER,
00148 AV_CH_FRONT_RIGHT_OF_CENTER,
00149 AV_CH_TOP_CENTER,
00150 AV_CH_TOP_FRONT_LEFT,
00151 AV_CH_TOP_FRONT_RIGHT,
00152 AV_CH_TOP_BACK_CENTER,
00153 AV_CH_TOP_BACK_LEFT,
00154 AV_CH_TOP_BACK_RIGHT,
00155 AV_CH_BACK_CENTER,
00156 AV_CH_BACK_LEFT,
00157 AV_CH_BACK_RIGHT
00158 };
00159
00160 enum DCAExtensionMask {
00161 DCA_EXT_CORE = 0x001,
00162 DCA_EXT_XXCH = 0x002,
00163 DCA_EXT_X96 = 0x004,
00164 DCA_EXT_XCH = 0x008,
00165 DCA_EXT_EXSS_CORE = 0x010,
00166 DCA_EXT_EXSS_XBR = 0x020,
00167 DCA_EXT_EXSS_XXCH = 0x040,
00168 DCA_EXT_EXSS_X96 = 0x080,
00169 DCA_EXT_EXSS_LBR = 0x100,
00170 DCA_EXT_EXSS_XLL = 0x200,
00171 };
00172
00173
00174 static const int dca_ext_audio_descr_mask[] = {
00175 DCA_EXT_XCH,
00176 -1,
00177 DCA_EXT_X96,
00178 DCA_EXT_XCH | DCA_EXT_X96,
00179 -1,
00180 -1,
00181 DCA_EXT_XXCH,
00182 -1,
00183 };
00184
00185
00186 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 static const uint64_t dca_core_channel_layout[] = {
00198 AV_CH_FRONT_CENTER,
00199 AV_CH_LAYOUT_STEREO,
00200 AV_CH_LAYOUT_STEREO,
00201 AV_CH_LAYOUT_STEREO,
00202 AV_CH_LAYOUT_STEREO,
00203 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER,
00204 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER,
00205 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,
00206 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
00207
00208 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
00209 AV_CH_SIDE_RIGHT,
00210
00211 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
00212 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER,
00213
00214 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
00215 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,
00216
00217 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
00218 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
00219 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT,
00220
00221 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
00222 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
00223 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
00224
00225 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
00226 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
00227 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT,
00228
00229 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
00230 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
00231 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT,
00232 };
00233
00234 static const int8_t dca_lfe_index[] = {
00235 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
00236 };
00237
00238 static const int8_t dca_channel_reorder_lfe[][9] = {
00239 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
00240 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00241 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00242 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00243 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00244 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
00245 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00246 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
00247 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
00248 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
00249 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
00250 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
00251 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
00252 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
00253 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
00254 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
00255 };
00256
00257 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
00258 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
00259 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00260 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00261 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00262 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
00263 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
00264 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
00265 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
00266 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
00267 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
00268 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
00269 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
00270 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
00271 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
00272 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
00273 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
00274 };
00275
00276 static const int8_t dca_channel_reorder_nolfe[][9] = {
00277 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
00278 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00279 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00280 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00281 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00282 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
00283 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00284 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
00285 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
00286 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
00287 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
00288 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
00289 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
00290 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
00291 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
00292 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
00293 };
00294
00295 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
00296 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
00297 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00298 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00299 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00300 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
00301 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
00302 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
00303 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
00304 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
00305 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
00306 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
00307 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
00308 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
00309 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
00310 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
00311 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
00312 };
00313
00314 #define DCA_DOLBY 101
00315
00316 #define DCA_CHANNEL_BITS 6
00317 #define DCA_CHANNEL_MASK 0x3F
00318
00319 #define DCA_LFE 0x80
00320
00321 #define HEADER_SIZE 14
00322
00323 #define DCA_MAX_FRAME_SIZE 16384
00324 #define DCA_MAX_EXSS_HEADER_SIZE 4096
00325
00326 #define DCA_BUFFER_PADDING_SIZE 1024
00327
00329 typedef struct {
00330 int offset;
00331 int maxbits[8];
00332 int wrap;
00333 VLC vlc[8];
00334 } BitAlloc;
00335
00336 static BitAlloc dca_bitalloc_index;
00337 static BitAlloc dca_tmode;
00338 static BitAlloc dca_scalefactor;
00339 static BitAlloc dca_smpl_bitalloc[11];
00340
00341 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
00342 int idx)
00343 {
00344 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
00345 ba->offset;
00346 }
00347
00348 typedef struct {
00349 AVCodecContext *avctx;
00350 AVFrame frame;
00351
00352 int frame_type;
00353 int samples_deficit;
00354 int crc_present;
00355 int sample_blocks;
00356 int frame_size;
00357 int amode;
00358 int sample_rate;
00359 int bit_rate;
00360 int bit_rate_index;
00361
00362 int downmix;
00363 int dynrange;
00364 int timestamp;
00365 int aux_data;
00366 int hdcd;
00367 int ext_descr;
00368 int ext_coding;
00369 int aspf;
00370 int lfe;
00371 int predictor_history;
00372 int header_crc;
00373 int multirate_inter;
00374 int version;
00375 int copy_history;
00376 int source_pcm_res;
00377 int front_sum;
00378 int surround_sum;
00379 int dialog_norm;
00380
00381
00382 int subframes;
00383 int total_channels;
00384 int prim_channels;
00385 int subband_activity[DCA_PRIM_CHANNELS_MAX];
00386 int vq_start_subband[DCA_PRIM_CHANNELS_MAX];
00387 int joint_intensity[DCA_PRIM_CHANNELS_MAX];
00388 int transient_huffman[DCA_PRIM_CHANNELS_MAX];
00389 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX];
00390 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];
00391 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
00392 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
00393
00394
00395 int subsubframes[DCA_SUBFRAMES_MAX];
00396 int partial_samples[DCA_SUBFRAMES_MAX];
00397 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00398 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00399 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00400 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00401 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];
00402 int joint_huff[DCA_PRIM_CHANNELS_MAX];
00403 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00404 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];
00405 int dynrange_coef;
00406
00407 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
00408
00409 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];
00410 int lfe_scale_factor;
00411
00412
00413 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
00414 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
00415 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
00416 int hist_index[DCA_PRIM_CHANNELS_MAX];
00417 DECLARE_ALIGNED(32, float, raXin)[32];
00418
00419 int output;
00420 float scale_bias;
00421
00422 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
00423 DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX + 1) * 256];
00424 const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
00425
00426 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
00427 int dca_buffer_size;
00428
00429 const int8_t *channel_order_tab;
00430 GetBitContext gb;
00431
00432 int current_subframe;
00433 int current_subsubframe;
00434
00435 int core_ext_mask;
00436
00437
00438 int xch_present;
00439 int xch_base_channel;
00440
00441
00442 int xxch_chset;
00443 int xxch_nbits_spk_mask;
00444 uint32_t xxch_core_spkmask;
00445 uint32_t xxch_spk_masks[4];
00446 int xxch_chset_nch[4];
00447 float xxch_dmix_sf[DCA_CHSETS_MAX];
00448
00449 uint32_t xxch_downmix;
00450 uint32_t xxch_dmix_embedded;
00451 float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32];
00452
00453 int8_t xxch_order_tab[32];
00454 int8_t lfe_index;
00455
00456
00457 int static_fields;
00458 int mix_metadata;
00459 int num_mix_configs;
00460 int mix_config_num_ch[4];
00461
00462 int profile;
00463
00464 int debug_flag;
00465 AVFloatDSPContext fdsp;
00466 FFTContext imdct;
00467 SynthFilterContext synth;
00468 DCADSPContext dcadsp;
00469 FmtConvertContext fmt_conv;
00470 } DCAContext;
00471
00472 static const uint16_t dca_vlc_offs[] = {
00473 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
00474 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
00475 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
00476 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
00477 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
00478 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
00479 };
00480
00481 static av_cold void dca_init_vlcs(void)
00482 {
00483 static int vlcs_initialized = 0;
00484 int i, j, c = 14;
00485 static VLC_TYPE dca_table[23622][2];
00486
00487 if (vlcs_initialized)
00488 return;
00489
00490 dca_bitalloc_index.offset = 1;
00491 dca_bitalloc_index.wrap = 2;
00492 for (i = 0; i < 5; i++) {
00493 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
00494 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
00495 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
00496 bitalloc_12_bits[i], 1, 1,
00497 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
00498 }
00499 dca_scalefactor.offset = -64;
00500 dca_scalefactor.wrap = 2;
00501 for (i = 0; i < 5; i++) {
00502 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
00503 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
00504 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
00505 scales_bits[i], 1, 1,
00506 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
00507 }
00508 dca_tmode.offset = 0;
00509 dca_tmode.wrap = 1;
00510 for (i = 0; i < 4; i++) {
00511 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
00512 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
00513 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
00514 tmode_bits[i], 1, 1,
00515 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
00516 }
00517
00518 for (i = 0; i < 10; i++)
00519 for (j = 0; j < 7; j++) {
00520 if (!bitalloc_codes[i][j])
00521 break;
00522 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
00523 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
00524 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
00525 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
00526
00527 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
00528 bitalloc_sizes[i],
00529 bitalloc_bits[i][j], 1, 1,
00530 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
00531 c++;
00532 }
00533 vlcs_initialized = 1;
00534 }
00535
00536 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
00537 {
00538 while (len--)
00539 *dst++ = get_bits(gb, bits);
00540 }
00541
00542 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
00543 {
00544 int i, base, mask;
00545
00546
00547 for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
00548 i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
00549 base += av_popcount(mask);
00550
00551 return base + av_popcount(mask & (xxch_ch - 1));
00552 }
00553
00554 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
00555 int xxch)
00556 {
00557 int i, j;
00558 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
00559 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
00560 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
00561 int hdr_pos = 0, hdr_size = 0;
00562 float sign, mag, scale_factor;
00563 int this_chans, acc_mask;
00564 int embedded_downmix;
00565 int nchans, mask[8];
00566 int coeff, ichan;
00567
00568
00569 if (xxch) {
00570 hdr_pos = get_bits_count(&s->gb);
00571 hdr_size = get_bits(&s->gb, 7) + 1;
00572 }
00573
00574 nchans = get_bits(&s->gb, 3) + 1;
00575 s->total_channels = nchans + base_channel;
00576 s->prim_channels = s->total_channels;
00577
00578
00579 if (xxch) {
00580 acc_mask = s->xxch_core_spkmask;
00581
00582 this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
00583 s->xxch_spk_masks[s->xxch_chset] = this_chans;
00584 s->xxch_chset_nch[s->xxch_chset] = nchans;
00585
00586 for (i = 0; i <= s->xxch_chset; i++)
00587 acc_mask |= s->xxch_spk_masks[i];
00588
00589
00590 if (get_bits1(&s->gb)) {
00591 embedded_downmix = get_bits1(&s->gb);
00592 scale_factor =
00593 1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
00594
00595 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
00596
00597 for (i = base_channel; i < s->prim_channels; i++) {
00598 s->xxch_downmix |= (1 << i);
00599 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
00600 }
00601
00602 for (j = base_channel; j < s->prim_channels; j++) {
00603 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
00604 s->xxch_dmix_embedded |= (embedded_downmix << j);
00605 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
00606 if (mask[j] & (1 << i)) {
00607 if ((1 << i) == DCA_XXCH_LFE1) {
00608 av_log(s->avctx, AV_LOG_WARNING,
00609 "DCA-XXCH: dmix to LFE1 not supported.\n");
00610 continue;
00611 }
00612
00613 coeff = get_bits(&s->gb, 7);
00614 sign = (coeff & 64) ? 1.0 : -1.0;
00615 mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
00616 ichan = dca_xxch2index(s, 1 << i);
00617 s->xxch_dmix_coeff[j][ichan] = sign * mag;
00618 }
00619 }
00620 }
00621 }
00622 }
00623
00624 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
00625 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
00626
00627
00628 for (i = base_channel; i < s->prim_channels; i++) {
00629 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
00630 if (s->subband_activity[i] > DCA_SUBBANDS)
00631 s->subband_activity[i] = DCA_SUBBANDS;
00632 }
00633 for (i = base_channel; i < s->prim_channels; i++) {
00634 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
00635 if (s->vq_start_subband[i] > DCA_SUBBANDS)
00636 s->vq_start_subband[i] = DCA_SUBBANDS;
00637 }
00638 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
00639 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
00640 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
00641 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
00642
00643
00644 if (!base_channel)
00645 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
00646 for (j = 1; j < 11; j++)
00647 for (i = base_channel; i < s->prim_channels; i++)
00648 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
00649
00650
00651 for (j = 0; j < 11; j++)
00652 for (i = base_channel; i < s->prim_channels; i++)
00653 s->scalefactor_adj[i][j] = 1;
00654
00655 for (j = 1; j < 11; j++)
00656 for (i = base_channel; i < s->prim_channels; i++)
00657 if (s->quant_index_huffman[i][j] < thr[j])
00658 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
00659
00660 if (!xxch) {
00661 if (s->crc_present) {
00662
00663 get_bits(&s->gb, 16);
00664 }
00665 } else {
00666
00667 i = get_bits_count(&s->gb);
00668 if (hdr_pos + 8 * hdr_size > i)
00669 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
00670 }
00671
00672 s->current_subframe = 0;
00673 s->current_subsubframe = 0;
00674
00675 #ifdef TRACE
00676 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
00677 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
00678 for (i = base_channel; i < s->prim_channels; i++) {
00679 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
00680 s->subband_activity[i]);
00681 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
00682 s->vq_start_subband[i]);
00683 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
00684 s->joint_intensity[i]);
00685 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
00686 s->transient_huffman[i]);
00687 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
00688 s->scalefactor_huffman[i]);
00689 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
00690 s->bitalloc_huffman[i]);
00691 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
00692 for (j = 0; j < 11; j++)
00693 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
00694 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00695 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
00696 for (j = 0; j < 11; j++)
00697 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
00698 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00699 }
00700 #endif
00701
00702 return 0;
00703 }
00704
00705 static int dca_parse_frame_header(DCAContext *s)
00706 {
00707 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
00708
00709
00710 skip_bits_long(&s->gb, 32);
00711
00712
00713 s->frame_type = get_bits(&s->gb, 1);
00714 s->samples_deficit = get_bits(&s->gb, 5) + 1;
00715 s->crc_present = get_bits(&s->gb, 1);
00716 s->sample_blocks = get_bits(&s->gb, 7) + 1;
00717 s->frame_size = get_bits(&s->gb, 14) + 1;
00718 if (s->frame_size < 95)
00719 return AVERROR_INVALIDDATA;
00720 s->amode = get_bits(&s->gb, 6);
00721 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
00722 if (!s->sample_rate)
00723 return AVERROR_INVALIDDATA;
00724 s->bit_rate_index = get_bits(&s->gb, 5);
00725 s->bit_rate = dca_bit_rates[s->bit_rate_index];
00726 if (!s->bit_rate)
00727 return AVERROR_INVALIDDATA;
00728
00729 s->downmix = get_bits(&s->gb, 1);
00730 s->dynrange = get_bits(&s->gb, 1);
00731 s->timestamp = get_bits(&s->gb, 1);
00732 s->aux_data = get_bits(&s->gb, 1);
00733 s->hdcd = get_bits(&s->gb, 1);
00734 s->ext_descr = get_bits(&s->gb, 3);
00735 s->ext_coding = get_bits(&s->gb, 1);
00736 s->aspf = get_bits(&s->gb, 1);
00737 s->lfe = get_bits(&s->gb, 2);
00738 s->predictor_history = get_bits(&s->gb, 1);
00739
00740
00741 if (s->crc_present)
00742 s->header_crc = get_bits(&s->gb, 16);
00743
00744 s->multirate_inter = get_bits(&s->gb, 1);
00745 s->version = get_bits(&s->gb, 4);
00746 s->copy_history = get_bits(&s->gb, 2);
00747 s->source_pcm_res = get_bits(&s->gb, 3);
00748 s->front_sum = get_bits(&s->gb, 1);
00749 s->surround_sum = get_bits(&s->gb, 1);
00750 s->dialog_norm = get_bits(&s->gb, 4);
00751
00752
00753 s->output = s->amode;
00754 if (s->lfe)
00755 s->output |= DCA_LFE;
00756
00757 #ifdef TRACE
00758 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
00759 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
00760 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
00761 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
00762 s->sample_blocks, s->sample_blocks * 32);
00763 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
00764 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
00765 s->amode, dca_channels[s->amode]);
00766 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
00767 s->sample_rate);
00768 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
00769 s->bit_rate);
00770 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
00771 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
00772 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
00773 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
00774 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
00775 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
00776 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
00777 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
00778 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
00779 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
00780 s->predictor_history);
00781 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
00782 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
00783 s->multirate_inter);
00784 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
00785 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
00786 av_log(s->avctx, AV_LOG_DEBUG,
00787 "source pcm resolution: %i (%i bits/sample)\n",
00788 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
00789 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
00790 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
00791 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
00792 av_log(s->avctx, AV_LOG_DEBUG, "\n");
00793 #endif
00794
00795
00796 s->subframes = get_bits(&s->gb, 4) + 1;
00797
00798 return dca_parse_audio_coding_header(s, 0, 0);
00799 }
00800
00801
00802 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
00803 {
00804 if (level < 5) {
00805
00806 value += get_bitalloc(gb, &dca_scalefactor, level);
00807 value = av_clip(value, 0, (1 << log2range) - 1);
00808 } else if (level < 8) {
00809 if (level + 1 > log2range) {
00810 skip_bits(gb, level + 1 - log2range);
00811 value = get_bits(gb, log2range);
00812 } else {
00813 value = get_bits(gb, level + 1);
00814 }
00815 }
00816 return value;
00817 }
00818
00819 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
00820 {
00821
00822 int j, k;
00823
00824 if (get_bits_left(&s->gb) < 0)
00825 return AVERROR_INVALIDDATA;
00826
00827 if (!base_channel) {
00828 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
00829 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
00830 }
00831
00832 for (j = base_channel; j < s->prim_channels; j++) {
00833 for (k = 0; k < s->subband_activity[j]; k++)
00834 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
00835 }
00836
00837
00838 for (j = base_channel; j < s->prim_channels; j++) {
00839 for (k = 0; k < s->subband_activity[j]; k++) {
00840 if (s->prediction_mode[j][k] > 0) {
00841
00842 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
00843 }
00844 }
00845 }
00846
00847
00848 for (j = base_channel; j < s->prim_channels; j++) {
00849 for (k = 0; k < s->vq_start_subband[j]; k++) {
00850 if (s->bitalloc_huffman[j] == 6)
00851 s->bitalloc[j][k] = get_bits(&s->gb, 5);
00852 else if (s->bitalloc_huffman[j] == 5)
00853 s->bitalloc[j][k] = get_bits(&s->gb, 4);
00854 else if (s->bitalloc_huffman[j] == 7) {
00855 av_log(s->avctx, AV_LOG_ERROR,
00856 "Invalid bit allocation index\n");
00857 return AVERROR_INVALIDDATA;
00858 } else {
00859 s->bitalloc[j][k] =
00860 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
00861 }
00862
00863 if (s->bitalloc[j][k] > 26) {
00864
00865
00866 return AVERROR_INVALIDDATA;
00867 }
00868 }
00869 }
00870
00871
00872 for (j = base_channel; j < s->prim_channels; j++) {
00873 for (k = 0; k < s->subband_activity[j]; k++) {
00874 s->transition_mode[j][k] = 0;
00875 if (s->subsubframes[s->current_subframe] > 1 &&
00876 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
00877 s->transition_mode[j][k] =
00878 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
00879 }
00880 }
00881 }
00882
00883 if (get_bits_left(&s->gb) < 0)
00884 return AVERROR_INVALIDDATA;
00885
00886 for (j = base_channel; j < s->prim_channels; j++) {
00887 const uint32_t *scale_table;
00888 int scale_sum, log_size;
00889
00890 memset(s->scale_factor[j], 0,
00891 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
00892
00893 if (s->scalefactor_huffman[j] == 6) {
00894 scale_table = scale_factor_quant7;
00895 log_size = 7;
00896 } else {
00897 scale_table = scale_factor_quant6;
00898 log_size = 6;
00899 }
00900
00901
00902 scale_sum = 0;
00903
00904 for (k = 0; k < s->subband_activity[j]; k++) {
00905 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
00906 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
00907 s->scale_factor[j][k][0] = scale_table[scale_sum];
00908 }
00909
00910 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
00911
00912 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
00913 s->scale_factor[j][k][1] = scale_table[scale_sum];
00914 }
00915 }
00916 }
00917
00918
00919 for (j = base_channel; j < s->prim_channels; j++) {
00920
00921 if (s->joint_intensity[j] > 0)
00922 s->joint_huff[j] = get_bits(&s->gb, 3);
00923 }
00924
00925 if (get_bits_left(&s->gb) < 0)
00926 return AVERROR_INVALIDDATA;
00927
00928
00929 for (j = base_channel; j < s->prim_channels; j++) {
00930 int source_channel;
00931
00932
00933 if (s->joint_intensity[j] > 0) {
00934 int scale = 0;
00935 source_channel = s->joint_intensity[j] - 1;
00936
00937
00938
00939
00940 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
00941 scale = get_scale(&s->gb, s->joint_huff[j], 64 , 7);
00942 s->joint_scale_factor[j][k] = scale;
00943 }
00944
00945 if (!(s->debug_flag & 0x02)) {
00946 av_log(s->avctx, AV_LOG_DEBUG,
00947 "Joint stereo coding not supported\n");
00948 s->debug_flag |= 0x02;
00949 }
00950 }
00951 }
00952
00953
00954 if (!base_channel && s->prim_channels > 2) {
00955 if (s->downmix) {
00956 for (j = base_channel; j < s->prim_channels; j++) {
00957 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
00958 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
00959 }
00960 } else {
00961 int am = s->amode & DCA_CHANNEL_MASK;
00962 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
00963 av_log(s->avctx, AV_LOG_ERROR,
00964 "Invalid channel mode %d\n", am);
00965 return AVERROR_INVALIDDATA;
00966 }
00967 for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
00968 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
00969 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
00970 }
00971 }
00972 }
00973
00974
00975 if (!base_channel && s->dynrange)
00976 s->dynrange_coef = get_bits(&s->gb, 8);
00977
00978
00979 if (s->crc_present) {
00980 get_bits(&s->gb, 16);
00981 }
00982
00983
00984
00985
00986
00987
00988 for (j = base_channel; j < s->prim_channels; j++)
00989 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00990
00991 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
00992
00993
00994 if (!base_channel && s->lfe) {
00995 int quant7;
00996
00997 int lfe_samples = 2 * s->lfe * (4 + block_index);
00998 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
00999 float lfe_scale;
01000
01001 for (j = lfe_samples; j < lfe_end_sample; j++) {
01002
01003 s->lfe_data[j] = get_sbits(&s->gb, 8);
01004 }
01005
01006
01007 quant7 = get_bits(&s->gb, 8);
01008 if (quant7 > 127) {
01009 av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
01010 return AVERROR_INVALIDDATA;
01011 }
01012 s->lfe_scale_factor = scale_factor_quant7[quant7];
01013
01014
01015 lfe_scale = 0.035 * s->lfe_scale_factor;
01016
01017 for (j = lfe_samples; j < lfe_end_sample; j++)
01018 s->lfe_data[j] *= lfe_scale;
01019 }
01020
01021 #ifdef TRACE
01022 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
01023 s->subsubframes[s->current_subframe]);
01024 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
01025 s->partial_samples[s->current_subframe]);
01026
01027 for (j = base_channel; j < s->prim_channels; j++) {
01028 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
01029 for (k = 0; k < s->subband_activity[j]; k++)
01030 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
01031 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01032 }
01033 for (j = base_channel; j < s->prim_channels; j++) {
01034 for (k = 0; k < s->subband_activity[j]; k++)
01035 av_log(s->avctx, AV_LOG_DEBUG,
01036 "prediction coefs: %f, %f, %f, %f\n",
01037 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
01038 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
01039 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
01040 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
01041 }
01042 for (j = base_channel; j < s->prim_channels; j++) {
01043 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
01044 for (k = 0; k < s->vq_start_subband[j]; k++)
01045 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
01046 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01047 }
01048 for (j = base_channel; j < s->prim_channels; j++) {
01049 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
01050 for (k = 0; k < s->subband_activity[j]; k++)
01051 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
01052 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01053 }
01054 for (j = base_channel; j < s->prim_channels; j++) {
01055 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
01056 for (k = 0; k < s->subband_activity[j]; k++) {
01057 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
01058 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
01059 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
01060 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
01061 }
01062 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01063 }
01064 for (j = base_channel; j < s->prim_channels; j++) {
01065 if (s->joint_intensity[j] > 0) {
01066 int source_channel = s->joint_intensity[j] - 1;
01067 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
01068 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
01069 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
01070 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01071 }
01072 }
01073 if (!base_channel && s->prim_channels > 2 && s->downmix) {
01074 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
01075 for (j = 0; j < s->prim_channels; j++) {
01076 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
01077 dca_downmix_coeffs[s->downmix_coef[j][0]]);
01078 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
01079 dca_downmix_coeffs[s->downmix_coef[j][1]]);
01080 }
01081 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01082 }
01083 for (j = base_channel; j < s->prim_channels; j++)
01084 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
01085 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
01086 if (!base_channel && s->lfe) {
01087 int lfe_samples = 2 * s->lfe * (4 + block_index);
01088 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
01089
01090 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
01091 for (j = lfe_samples; j < lfe_end_sample; j++)
01092 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
01093 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01094 }
01095 #endif
01096
01097 return 0;
01098 }
01099
01100 static void qmf_32_subbands(DCAContext *s, int chans,
01101 float samples_in[32][8], float *samples_out,
01102 float scale)
01103 {
01104 const float *prCoeff;
01105 int i;
01106
01107 int sb_act = s->subband_activity[chans];
01108 int subindex;
01109
01110 scale *= sqrt(1 / 8.0);
01111
01112
01113 if (!s->multirate_inter)
01114 prCoeff = fir_32bands_nonperfect;
01115 else
01116 prCoeff = fir_32bands_perfect;
01117
01118 for (i = sb_act; i < 32; i++)
01119 s->raXin[i] = 0.0;
01120
01121
01122 for (subindex = 0; subindex < 8; subindex++) {
01123
01124 for (i = 0; i < sb_act; i++) {
01125 unsigned sign = (i - 1) & 2;
01126 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
01127 AV_WN32A(&s->raXin[i], v);
01128 }
01129
01130 s->synth.synth_filter_float(&s->imdct,
01131 s->subband_fir_hist[chans],
01132 &s->hist_index[chans],
01133 s->subband_fir_noidea[chans], prCoeff,
01134 samples_out, s->raXin, scale);
01135 samples_out += 32;
01136 }
01137 }
01138
01139 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
01140 int num_deci_sample, float *samples_in,
01141 float *samples_out, float scale)
01142 {
01143
01144
01145
01146
01147
01148
01149
01150
01151 int decifactor;
01152 const float *prCoeff;
01153 int deciindex;
01154
01155
01156 if (decimation_select == 1) {
01157 decifactor = 64;
01158 prCoeff = lfe_fir_128;
01159 } else {
01160 decifactor = 32;
01161 prCoeff = lfe_fir_64;
01162 }
01163
01164 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
01165 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
01166 samples_in++;
01167 samples_out += 2 * decifactor;
01168 }
01169 }
01170
01171
01172 #define MIX_REAR1(samples, si1, rs, coef) \
01173 samples[i] += samples[si1] * coef[rs][0]; \
01174 samples[i+256] += samples[si1] * coef[rs][1];
01175
01176 #define MIX_REAR2(samples, si1, si2, rs, coef) \
01177 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \
01178 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1];
01179
01180 #define MIX_FRONT3(samples, coef) \
01181 t = samples[i + c]; \
01182 u = samples[i + l]; \
01183 v = samples[i + r]; \
01184 samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
01185 samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
01186
01187 #define DOWNMIX_TO_STEREO(op1, op2) \
01188 for (i = 0; i < 256; i++) { \
01189 op1 \
01190 op2 \
01191 }
01192
01193 static void dca_downmix(float *samples, int srcfmt,
01194 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
01195 const int8_t *channel_mapping)
01196 {
01197 int c, l, r, sl, sr, s;
01198 int i;
01199 float t, u, v;
01200 float coef[DCA_PRIM_CHANNELS_MAX][2];
01201
01202 for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
01203 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
01204 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
01205 }
01206
01207 switch (srcfmt) {
01208 case DCA_MONO:
01209 case DCA_CHANNEL:
01210 case DCA_STEREO_TOTAL:
01211 case DCA_STEREO_SUMDIFF:
01212 case DCA_4F2R:
01213 av_log(NULL, 0, "Not implemented!\n");
01214 break;
01215 case DCA_STEREO:
01216 break;
01217 case DCA_3F:
01218 c = channel_mapping[0] * 256;
01219 l = channel_mapping[1] * 256;
01220 r = channel_mapping[2] * 256;
01221 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
01222 break;
01223 case DCA_2F1R:
01224 s = channel_mapping[2] * 256;
01225 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), );
01226 break;
01227 case DCA_3F1R:
01228 c = channel_mapping[0] * 256;
01229 l = channel_mapping[1] * 256;
01230 r = channel_mapping[2] * 256;
01231 s = channel_mapping[3] * 256;
01232 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
01233 MIX_REAR1(samples, i + s, 3, coef));
01234 break;
01235 case DCA_2F2R:
01236 sl = channel_mapping[2] * 256;
01237 sr = channel_mapping[3] * 256;
01238 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), );
01239 break;
01240 case DCA_3F2R:
01241 c = channel_mapping[0] * 256;
01242 l = channel_mapping[1] * 256;
01243 r = channel_mapping[2] * 256;
01244 sl = channel_mapping[3] * 256;
01245 sr = channel_mapping[4] * 256;
01246 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
01247 MIX_REAR2(samples, i + sl, i + sr, 3, coef));
01248 break;
01249 }
01250 }
01251
01252
01253 #ifndef decode_blockcodes
01254
01255
01256 static int decode_blockcode(int code, int levels, int *values)
01257 {
01258 int i;
01259 int offset = (levels - 1) >> 1;
01260
01261 for (i = 0; i < 4; i++) {
01262 int div = FASTDIV(code, levels);
01263 values[i] = code - offset - div * levels;
01264 code = div;
01265 }
01266
01267 return code;
01268 }
01269
01270 static int decode_blockcodes(int code1, int code2, int levels, int *values)
01271 {
01272 return decode_blockcode(code1, levels, values) |
01273 decode_blockcode(code2, levels, values + 4);
01274 }
01275 #endif
01276
01277 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
01278 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
01279
01280 #ifndef int8x8_fmul_int32
01281 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
01282 {
01283 float fscale = scale / 16.0;
01284 int i;
01285 for (i = 0; i < 8; i++)
01286 dst[i] = src[i] * fscale;
01287 }
01288 #endif
01289
01290 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
01291 {
01292 int k, l;
01293 int subsubframe = s->current_subsubframe;
01294
01295 const float *quant_step_table;
01296
01297
01298 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
01299 LOCAL_ALIGNED_16(int, block, [8]);
01300
01301
01302
01303
01304
01305
01306 if (s->bit_rate_index == 0x1f)
01307 quant_step_table = lossless_quant_d;
01308 else
01309 quant_step_table = lossy_quant_d;
01310
01311 for (k = base_channel; k < s->prim_channels; k++) {
01312 if (get_bits_left(&s->gb) < 0)
01313 return AVERROR_INVALIDDATA;
01314
01315 for (l = 0; l < s->vq_start_subband[k]; l++) {
01316 int m;
01317
01318
01319 int abits = s->bitalloc[k][l];
01320
01321 float quant_step_size = quant_step_table[abits];
01322
01323
01324
01325
01326
01327
01328 int sel = s->quant_index_huffman[k][abits];
01329
01330
01331
01332
01333 if (!abits) {
01334 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
01335 } else {
01336
01337 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
01338 float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
01339 s->scalefactor_adj[k][sel];
01340
01341 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
01342 if (abits <= 7) {
01343
01344 int block_code1, block_code2, size, levels, err;
01345
01346 size = abits_sizes[abits - 1];
01347 levels = abits_levels[abits - 1];
01348
01349 block_code1 = get_bits(&s->gb, size);
01350 block_code2 = get_bits(&s->gb, size);
01351 err = decode_blockcodes(block_code1, block_code2,
01352 levels, block);
01353 if (err) {
01354 av_log(s->avctx, AV_LOG_ERROR,
01355 "ERROR: block code look-up failed\n");
01356 return AVERROR_INVALIDDATA;
01357 }
01358 } else {
01359
01360 for (m = 0; m < 8; m++)
01361 block[m] = get_sbits(&s->gb, abits - 3);
01362 }
01363 } else {
01364
01365 for (m = 0; m < 8; m++)
01366 block[m] = get_bitalloc(&s->gb,
01367 &dca_smpl_bitalloc[abits], sel);
01368 }
01369
01370 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
01371 block, rscale, 8);
01372 }
01373
01374
01375
01376
01377 if (s->prediction_mode[k][l]) {
01378 int n;
01379 for (m = 0; m < 8; m++) {
01380 for (n = 1; n <= 4; n++)
01381 if (m >= n)
01382 subband_samples[k][l][m] +=
01383 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
01384 subband_samples[k][l][m - n] / 8192);
01385 else if (s->predictor_history)
01386 subband_samples[k][l][m] +=
01387 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
01388 s->subband_samples_hist[k][l][m - n + 4] / 8192);
01389 }
01390 }
01391 }
01392
01393
01394
01395
01396 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
01397
01398
01399 int hfvq = s->high_freq_vq[k][l];
01400
01401 if (!s->debug_flag & 0x01) {
01402 av_log(s->avctx, AV_LOG_DEBUG,
01403 "Stream with high frequencies VQ coding\n");
01404 s->debug_flag |= 0x01;
01405 }
01406
01407 int8x8_fmul_int32(subband_samples[k][l],
01408 &high_freq_vq[hfvq][subsubframe * 8],
01409 s->scale_factor[k][l][0]);
01410 }
01411 }
01412
01413
01414 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
01415 if (0xFFFF == get_bits(&s->gb, 16)) {
01416 #ifdef TRACE
01417 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
01418 #endif
01419 } else {
01420 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
01421 }
01422 }
01423
01424
01425 for (k = base_channel; k < s->prim_channels; k++)
01426 for (l = 0; l < s->vq_start_subband[k]; l++)
01427 memcpy(s->subband_samples_hist[k][l],
01428 &subband_samples[k][l][4],
01429 4 * sizeof(subband_samples[0][0][0]));
01430
01431 return 0;
01432 }
01433
01434 static int dca_filter_channels(DCAContext *s, int block_index)
01435 {
01436 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
01437 int k;
01438
01439
01440 for (k = 0; k < s->prim_channels; k++) {
01441
01442
01443 qmf_32_subbands(s, k, subband_samples[k],
01444 &s->samples[256 * s->channel_order_tab[k]],
01445 M_SQRT1_2 * s->scale_bias );
01446 }
01447
01448
01449 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
01450 dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
01451 }
01452
01453
01454 if (s->output & DCA_LFE) {
01455 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
01456 s->lfe_data + 2 * s->lfe * (block_index + 4),
01457 &s->samples[256 * s->lfe_index],
01458 (1.0 / 256.0) * s->scale_bias);
01459
01460 }
01461
01462 return 0;
01463 }
01464
01465
01466 static int dca_subframe_footer(DCAContext *s, int base_channel)
01467 {
01468 int aux_data_count = 0, i;
01469
01470
01471
01472
01473
01474
01475 if (!base_channel) {
01476 if (s->timestamp)
01477 skip_bits_long(&s->gb, 32);
01478
01479 if (s->aux_data)
01480 aux_data_count = get_bits(&s->gb, 6);
01481
01482 for (i = 0; i < aux_data_count; i++)
01483 get_bits(&s->gb, 8);
01484
01485 if (s->crc_present && (s->downmix || s->dynrange))
01486 get_bits(&s->gb, 16);
01487 }
01488
01489 return 0;
01490 }
01491
01498 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
01499 {
01500 int ret;
01501
01502
01503 if (s->current_subframe >= s->subframes) {
01504 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
01505 s->current_subframe, s->subframes);
01506 return AVERROR_INVALIDDATA;
01507 }
01508
01509 if (!s->current_subsubframe) {
01510 #ifdef TRACE
01511 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
01512 #endif
01513
01514 if ((ret = dca_subframe_header(s, base_channel, block_index)))
01515 return ret;
01516 }
01517
01518
01519 #ifdef TRACE
01520 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
01521 #endif
01522 if ((ret = dca_subsubframe(s, base_channel, block_index)))
01523 return ret;
01524
01525
01526 s->current_subsubframe++;
01527 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
01528 s->current_subsubframe = 0;
01529 s->current_subframe++;
01530 }
01531 if (s->current_subframe >= s->subframes) {
01532 #ifdef TRACE
01533 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
01534 #endif
01535
01536 if ((ret = dca_subframe_footer(s, base_channel)))
01537 return ret;
01538 }
01539
01540 return 0;
01541 }
01542
01546 static int dca_exss_mask2count(int mask)
01547 {
01548
01549 return av_popcount(mask) +
01550 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
01551 DCA_EXSS_FRONT_LEFT_RIGHT |
01552 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
01553 DCA_EXSS_WIDE_LEFT_RIGHT |
01554 DCA_EXSS_SIDE_LEFT_RIGHT |
01555 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
01556 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
01557 DCA_EXSS_REAR_LEFT_RIGHT |
01558 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
01559 }
01560
01564 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
01565 {
01566 int i;
01567
01568 for (i = 0; i < channels; i++) {
01569 int mix_map_mask = get_bits(gb, out_ch);
01570 int num_coeffs = av_popcount(mix_map_mask);
01571 skip_bits_long(gb, num_coeffs * 6);
01572 }
01573 }
01574
01578 static int dca_exss_parse_asset_header(DCAContext *s)
01579 {
01580 int header_pos = get_bits_count(&s->gb);
01581 int header_size;
01582 int channels = 0;
01583 int embedded_stereo = 0;
01584 int embedded_6ch = 0;
01585 int drc_code_present;
01586 int av_uninit(extensions_mask);
01587 int i, j;
01588
01589 if (get_bits_left(&s->gb) < 16)
01590 return -1;
01591
01592
01593
01594
01595 header_size = get_bits(&s->gb, 9) + 1;
01596 skip_bits(&s->gb, 3);
01597
01598 if (s->static_fields) {
01599 if (get_bits1(&s->gb))
01600 skip_bits(&s->gb, 4);
01601 if (get_bits1(&s->gb))
01602 skip_bits_long(&s->gb, 24);
01603
01604 if (get_bits1(&s->gb)) {
01605
01606
01607 int text_length = get_bits(&s->gb, 10) + 1;
01608 if (get_bits_left(&s->gb) < text_length * 8)
01609 return -1;
01610 skip_bits_long(&s->gb, text_length * 8);
01611 }
01612
01613 skip_bits(&s->gb, 5);
01614 skip_bits(&s->gb, 4);
01615 channels = get_bits(&s->gb, 8) + 1;
01616
01617 if (get_bits1(&s->gb)) {
01618 int spkr_remap_sets;
01619 int spkr_mask_size = 16;
01620 int num_spkrs[7];
01621
01622 if (channels > 2)
01623 embedded_stereo = get_bits1(&s->gb);
01624 if (channels > 6)
01625 embedded_6ch = get_bits1(&s->gb);
01626
01627 if (get_bits1(&s->gb)) {
01628 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
01629 skip_bits(&s->gb, spkr_mask_size);
01630 }
01631
01632 spkr_remap_sets = get_bits(&s->gb, 3);
01633
01634 for (i = 0; i < spkr_remap_sets; i++) {
01635
01636 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
01637 }
01638
01639 for (i = 0; i < spkr_remap_sets; i++) {
01640 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
01641 if (get_bits_left(&s->gb) < 0)
01642 return -1;
01643
01644 for (j = 0; j < num_spkrs[i]; j++) {
01645 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
01646 int num_dec_ch = av_popcount(remap_dec_ch_mask);
01647 skip_bits_long(&s->gb, num_dec_ch * 5);
01648 }
01649 }
01650
01651 } else {
01652 skip_bits(&s->gb, 3);
01653 }
01654 }
01655
01656 drc_code_present = get_bits1(&s->gb);
01657 if (drc_code_present)
01658 get_bits(&s->gb, 8);
01659
01660 if (get_bits1(&s->gb))
01661 skip_bits(&s->gb, 5);
01662
01663 if (drc_code_present && embedded_stereo)
01664 get_bits(&s->gb, 8);
01665
01666 if (s->mix_metadata && get_bits1(&s->gb)) {
01667 skip_bits(&s->gb, 1);
01668 skip_bits(&s->gb, 6);
01669
01670 if (get_bits(&s->gb, 2) != 3)
01671 skip_bits(&s->gb, 3);
01672 else
01673 skip_bits(&s->gb, 8);
01674
01675 if (get_bits1(&s->gb))
01676 for (i = 0; i < s->num_mix_configs; i++)
01677 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6);
01678 else
01679 skip_bits_long(&s->gb, s->num_mix_configs * 6);
01680
01681 for (i = 0; i < s->num_mix_configs; i++) {
01682 if (get_bits_left(&s->gb) < 0)
01683 return -1;
01684 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
01685 if (embedded_6ch)
01686 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
01687 if (embedded_stereo)
01688 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
01689 }
01690 }
01691
01692 switch (get_bits(&s->gb, 2)) {
01693 case 0: extensions_mask = get_bits(&s->gb, 12); break;
01694 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
01695 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
01696 case 3: extensions_mask = 0; break;
01697 }
01698
01699
01700
01701 if (get_bits_left(&s->gb) < 0)
01702 return -1;
01703
01704 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
01705 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
01706 return -1;
01707 }
01708 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
01709
01710 if (extensions_mask & DCA_EXT_EXSS_XLL)
01711 s->profile = FF_PROFILE_DTS_HD_MA;
01712 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
01713 DCA_EXT_EXSS_XXCH))
01714 s->profile = FF_PROFILE_DTS_HD_HRA;
01715
01716 if (!(extensions_mask & DCA_EXT_CORE))
01717 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
01718 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
01719 av_log(s->avctx, AV_LOG_WARNING,
01720 "DTS extensions detection mismatch (%d, %d)\n",
01721 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
01722
01723 return 0;
01724 }
01725
01726 static int dca_xbr_parse_frame(DCAContext *s)
01727 {
01728 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
01729 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
01730 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
01731 int anctemp[DCA_CHSET_CHANS_MAX];
01732 int chset_fsize[DCA_CHSETS_MAX];
01733 int n_xbr_ch[DCA_CHSETS_MAX];
01734 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
01735 int i, j, k, l, chset, chan_base;
01736
01737 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
01738
01739
01740 hdr_pos = get_bits_count(&s->gb) - 32;
01741
01742 hdr_size = get_bits(&s->gb, 6) + 1;
01743 num_chsets = get_bits(&s->gb, 2) + 1;
01744
01745 for(i = 0; i < num_chsets; i++)
01746 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
01747
01748 xbr_tmode = get_bits1(&s->gb);
01749
01750 for(i = 0; i < num_chsets; i++) {
01751 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
01752 k = get_bits(&s->gb, 2) + 5;
01753 for(j = 0; j < n_xbr_ch[i]; j++)
01754 active_bands[i][j] = get_bits(&s->gb, k) + 1;
01755 }
01756
01757
01758 i = get_bits_count(&s->gb);
01759 if(hdr_pos + hdr_size * 8 > i)
01760 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
01761
01762
01763
01764 for(chset = 0, chan_base = 0;
01765 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
01766 chan_base += n_xbr_ch[chset++]) {
01767 int start_posn = get_bits_count(&s->gb);
01768 int subsubframe = 0;
01769 int subframe = 0;
01770
01771
01772 for (k = 0; k < (s->sample_blocks / 8); k++) {
01773
01774 if(subsubframe == 0) {
01775
01776 for(i = 0; i < n_xbr_ch[chset]; i++) {
01777 anctemp[i] = get_bits(&s->gb, 2) + 2;
01778 }
01779
01780 for(i = 0; i < n_xbr_ch[chset]; i++) {
01781 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
01782 }
01783
01784 for(i = 0; i < n_xbr_ch[chset]; i++) {
01785 anctemp[i] = get_bits(&s->gb, 3);
01786 if(anctemp[i] < 1) {
01787 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
01788 return AVERROR_INVALIDDATA;
01789 }
01790 }
01791
01792
01793 for(i = 0; i < n_xbr_ch[chset]; i++) {
01794 const uint32_t *scale_table;
01795 int nbits;
01796
01797 if (s->scalefactor_huffman[chan_base+i] == 6) {
01798 scale_table = scale_factor_quant7;
01799 } else {
01800 scale_table = scale_factor_quant6;
01801 }
01802
01803 nbits = anctemp[i];
01804
01805 for(j = 0; j < active_bands[chset][i]; j++) {
01806 if(abits_high[i][j] > 0) {
01807 scale_table_high[i][j][0] =
01808 scale_table[get_bits(&s->gb, nbits)];
01809
01810 if(xbr_tmode && s->transition_mode[i][j]) {
01811 scale_table_high[i][j][1] =
01812 scale_table[get_bits(&s->gb, nbits)];
01813 }
01814 }
01815 }
01816 }
01817 }
01818
01819
01820 for(i = 0; i < n_xbr_ch[chset]; i++) {
01821 for(j = 0; j < active_bands[chset][i]; j++) {
01822 const int xbr_abits = abits_high[i][j];
01823 const float quant_step_size = lossless_quant_d[xbr_abits];
01824 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
01825 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
01826 float *subband_samples = s->subband_samples[k][chan_base+i][j];
01827 int block[8];
01828
01829 if(xbr_abits <= 0)
01830 continue;
01831
01832 if(xbr_abits > 7) {
01833 get_array(&s->gb, block, 8, xbr_abits - 3);
01834 } else {
01835 int block_code1, block_code2, size, levels, err;
01836
01837 size = abits_sizes[xbr_abits - 1];
01838 levels = abits_levels[xbr_abits - 1];
01839
01840 block_code1 = get_bits(&s->gb, size);
01841 block_code2 = get_bits(&s->gb, size);
01842 err = decode_blockcodes(block_code1, block_code2,
01843 levels, block);
01844 if (err) {
01845 av_log(s->avctx, AV_LOG_ERROR,
01846 "ERROR: DTS-XBR: block code look-up failed\n");
01847 return AVERROR_INVALIDDATA;
01848 }
01849 }
01850
01851
01852 for(l = 0; l < 8; l++)
01853 subband_samples[l] += (float)block[l] * rscale;
01854 }
01855 }
01856
01857
01858 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
01859 if(get_bits(&s->gb, 16) != 0xffff) {
01860 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
01861 return AVERROR_INVALIDDATA;
01862 }
01863 }
01864
01865
01866 if(++subsubframe >= s->subsubframes[subframe]) {
01867 subsubframe = 0;
01868 subframe++;
01869 }
01870 }
01871
01872
01873 i = get_bits_count(&s->gb);
01874 if(start_posn + chset_fsize[chset] * 8 != i) {
01875 j = start_posn + chset_fsize[chset] * 8 - i;
01876 if(j < 0 || j >= 8)
01877 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
01878 " skipping further than expected (%d bits)\n", j);
01879 skip_bits_long(&s->gb, j);
01880 }
01881 }
01882
01883 return 0;
01884 }
01885
01886
01887 static int dca_xxch_decode_frame(DCAContext *s)
01888 {
01889 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
01890 int i, chset, base_channel, chstart, fsize[8];
01891
01892
01893 hdr_pos = get_bits_count(&s->gb) - 32;
01894 hdr_size = get_bits(&s->gb, 6) + 1;
01895 skip_bits1(&s->gb);
01896 spkmsk_bits = get_bits(&s->gb, 5) + 1;
01897 num_chsets = get_bits(&s->gb, 2) + 1;
01898
01899 for (i = 0; i < num_chsets; i++)
01900 fsize[i] = get_bits(&s->gb, 14) + 1;
01901
01902 core_spk = get_bits(&s->gb, spkmsk_bits);
01903 s->xxch_core_spkmask = core_spk;
01904 s->xxch_nbits_spk_mask = spkmsk_bits;
01905 s->xxch_downmix = 0;
01906 s->xxch_dmix_embedded = 0;
01907
01908
01909 i = get_bits_count(&s->gb);
01910 if (hdr_pos + hdr_size * 8 > i)
01911 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
01912
01913 for (chset = 0; chset < num_chsets; chset++) {
01914 chstart = get_bits_count(&s->gb);
01915 base_channel = s->prim_channels;
01916 s->xxch_chset = chset;
01917
01918
01919
01920 dca_parse_audio_coding_header(s, base_channel, 1);
01921
01922
01923 for (i = 0; i < (s->sample_blocks / 8); i++) {
01924 if (dca_decode_block(s, base_channel, i)) {
01925 av_log(s->avctx, AV_LOG_ERROR,
01926 "Error decoding DTS-XXCH extension\n");
01927 continue;
01928 }
01929 }
01930
01931
01932 i = get_bits_count(&s->gb);
01933 if (chstart + fsize[chset] * 8 > i)
01934 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
01935 }
01936 s->xxch_chset = num_chsets;
01937
01938 return 0;
01939 }
01940
01944 static void dca_exss_parse_header(DCAContext *s)
01945 {
01946 int asset_size[8];
01947 int ss_index;
01948 int blownup;
01949 int num_audiop = 1;
01950 int num_assets = 1;
01951 int active_ss_mask[8];
01952 int i, j;
01953 int start_posn;
01954 int hdrsize;
01955 uint32_t mkr;
01956
01957 if (get_bits_left(&s->gb) < 52)
01958 return;
01959
01960 start_posn = get_bits_count(&s->gb) - 32;
01961
01962 skip_bits(&s->gb, 8);
01963 ss_index = get_bits(&s->gb, 2);
01964
01965 blownup = get_bits1(&s->gb);
01966 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1;
01967 skip_bits(&s->gb, 16 + 4 * blownup);
01968
01969 s->static_fields = get_bits1(&s->gb);
01970 if (s->static_fields) {
01971 skip_bits(&s->gb, 2);
01972 skip_bits(&s->gb, 3);
01973
01974 if (get_bits1(&s->gb))
01975 skip_bits_long(&s->gb, 36);
01976
01977
01978
01979
01980 num_audiop = get_bits(&s->gb, 3) + 1;
01981 if (num_audiop > 1) {
01982 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
01983
01984 return;
01985 }
01986
01987 num_assets = get_bits(&s->gb, 3) + 1;
01988 if (num_assets > 1) {
01989 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
01990
01991 return;
01992 }
01993
01994 for (i = 0; i < num_audiop; i++)
01995 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
01996
01997 for (i = 0; i < num_audiop; i++)
01998 for (j = 0; j <= ss_index; j++)
01999 if (active_ss_mask[i] & (1 << j))
02000 skip_bits(&s->gb, 8);
02001
02002 s->mix_metadata = get_bits1(&s->gb);
02003 if (s->mix_metadata) {
02004 int mix_out_mask_size;
02005
02006 skip_bits(&s->gb, 2);
02007 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
02008 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
02009
02010 for (i = 0; i < s->num_mix_configs; i++) {
02011 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
02012 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
02013 }
02014 }
02015 }
02016
02017 for (i = 0; i < num_assets; i++)
02018 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
02019
02020 for (i = 0; i < num_assets; i++) {
02021 if (dca_exss_parse_asset_header(s))
02022 return;
02023 }
02024
02025
02026
02027
02028 if (num_assets > 0) {
02029 j = get_bits_count(&s->gb);
02030 if (start_posn + hdrsize * 8 > j)
02031 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
02032
02033 for (i = 0; i < num_assets; i++) {
02034 start_posn = get_bits_count(&s->gb);
02035 mkr = get_bits_long(&s->gb, 32);
02036
02037
02038 if (mkr == 0x655e315e) {
02039 dca_xbr_parse_frame(s);
02040 } else if (mkr == 0x47004a03) {
02041 dca_xxch_decode_frame(s);
02042 s->core_ext_mask |= DCA_EXT_XXCH;
02043 } else {
02044 av_log(s->avctx, AV_LOG_DEBUG,
02045 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
02046 }
02047
02048
02049 j = get_bits_count(&s->gb);
02050 if (start_posn + asset_size[i] * 8 > j)
02051 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
02052 }
02053 }
02054 }
02055
02060 static int dca_decode_frame(AVCodecContext *avctx, void *data,
02061 int *got_frame_ptr, AVPacket *avpkt)
02062 {
02063 const uint8_t *buf = avpkt->data;
02064 int buf_size = avpkt->size;
02065 int channel_mask;
02066 int channel_layout;
02067 int lfe_samples;
02068 int num_core_channels = 0;
02069 int i, ret;
02070 float *samples_flt;
02071 float *src_chan;
02072 float *dst_chan;
02073 int16_t *samples_s16;
02074 DCAContext *s = avctx->priv_data;
02075 int core_ss_end;
02076 int channels;
02077 float scale;
02078 int achan;
02079 int chset;
02080 int mask;
02081 int lavc;
02082 int posn;
02083 int j, k;
02084 int ch;
02085 int endch;
02086
02087 s->xch_present = 0;
02088
02089 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
02090 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
02091 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
02092 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
02093 return AVERROR_INVALIDDATA;
02094 }
02095
02096 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
02097 if ((ret = dca_parse_frame_header(s)) < 0) {
02098
02099 return ret;
02100 }
02101
02102 avctx->sample_rate = s->sample_rate;
02103 avctx->bit_rate = s->bit_rate;
02104
02105 s->profile = FF_PROFILE_DTS;
02106
02107 for (i = 0; i < (s->sample_blocks / 8); i++) {
02108 if ((ret = dca_decode_block(s, 0, i))) {
02109 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
02110 return ret;
02111 }
02112 }
02113
02114
02115 num_core_channels = s->prim_channels;
02116
02117 if (s->ext_coding)
02118 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
02119 else
02120 s->core_ext_mask = 0;
02121
02122 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
02123
02124
02125
02126 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
02127
02128
02129
02130 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
02131
02132
02133 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
02134
02135 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
02136 uint32_t bits = get_bits_long(&s->gb, 32);
02137
02138 switch (bits) {
02139 case 0x5a5a5a5a: {
02140 int ext_amode, xch_fsize;
02141
02142 s->xch_base_channel = s->prim_channels;
02143
02144
02145 xch_fsize = show_bits(&s->gb, 10);
02146 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
02147 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
02148 continue;
02149
02150
02151 skip_bits(&s->gb, 10);
02152
02153 s->core_ext_mask |= DCA_EXT_XCH;
02154
02155
02156
02157 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
02158 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
02159 " supported!\n", ext_amode);
02160 continue;
02161 }
02162
02163
02164 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
02165
02166 for (i = 0; i < (s->sample_blocks / 8); i++)
02167 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
02168 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
02169 continue;
02170 }
02171
02172 s->xch_present = 1;
02173 break;
02174 }
02175 case 0x47004a03:
02176
02177
02178
02179 s->core_ext_mask |= DCA_EXT_XXCH;
02180 dca_xxch_decode_frame(s);
02181 break;
02182
02183 case 0x1d95f262: {
02184 int fsize96 = show_bits(&s->gb, 12) + 1;
02185 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
02186 continue;
02187
02188 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
02189 get_bits_count(&s->gb));
02190 skip_bits(&s->gb, 12);
02191 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
02192 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
02193
02194 s->core_ext_mask |= DCA_EXT_X96;
02195 break;
02196 }
02197 }
02198
02199 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
02200 }
02201 } else {
02202
02203 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
02204 }
02205
02206 if (s->core_ext_mask & DCA_EXT_X96)
02207 s->profile = FF_PROFILE_DTS_96_24;
02208 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
02209 s->profile = FF_PROFILE_DTS_ES;
02210
02211
02212 if (s->dca_buffer_size - s->frame_size > 32 &&
02213 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
02214 dca_exss_parse_header(s);
02215
02216 avctx->profile = s->profile;
02217
02218 channels = s->prim_channels + !!s->lfe;
02219
02220
02221
02222 if (!(s->core_ext_mask & DCA_EXT_XXCH)
02223 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
02224 && avctx->request_channels
02225 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
02226 {
02227 if (s->amode < 16) {
02228 avctx->channel_layout = dca_core_channel_layout[s->amode];
02229
02230 if (s->xch_present && (!avctx->request_channels ||
02231 avctx->request_channels
02232 > num_core_channels + !!s->lfe)) {
02233 avctx->channel_layout |= AV_CH_BACK_CENTER;
02234 if (s->lfe) {
02235 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
02236 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
02237 } else {
02238 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
02239 }
02240 } else {
02241 channels = num_core_channels + !!s->lfe;
02242 s->xch_present = 0;
02243 if (s->lfe) {
02244 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
02245 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
02246 } else
02247 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
02248 }
02249
02250 if (channels > !!s->lfe &&
02251 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
02252 return AVERROR_INVALIDDATA;
02253
02254 if (avctx->request_channels == 2 && s->prim_channels > 2) {
02255 channels = 2;
02256 s->output = DCA_STEREO;
02257 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
02258 }
02259 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
02260 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
02261 s->channel_order_tab = dca_channel_order_native;
02262 }
02263 s->lfe_index = dca_lfe_index[s->amode];
02264 } else {
02265 av_log(avctx, AV_LOG_ERROR,
02266 "Non standard configuration %d !\n", s->amode);
02267 return AVERROR_INVALIDDATA;
02268 }
02269
02270 s->xxch_downmix = 0;
02271 } else {
02272
02273 channel_mask = s->xxch_core_spkmask;
02274
02275 if (avctx->request_channels > 0
02276 && avctx->request_channels < s->prim_channels) {
02277 channels = num_core_channels + !!s->lfe;
02278 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
02279 <= avctx->request_channels; i++) {
02280 channels += s->xxch_chset_nch[i];
02281 channel_mask |= s->xxch_spk_masks[i];
02282 }
02283 } else {
02284 channels = s->prim_channels + !!s->lfe;
02285 for (i = 0; i < s->xxch_chset; i++) {
02286 channel_mask |= s->xxch_spk_masks[i];
02287 }
02288 }
02289
02290
02291 channel_layout = 0;
02292 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
02293 if (channel_mask & (1 << i)) {
02294 channel_layout |= map_xxch_to_native[i];
02295 }
02296 }
02297
02298
02299
02300 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
02301 av_log(avctx, AV_LOG_DEBUG,
02302 "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
02303 return AVERROR_INVALIDDATA;
02304 }
02305
02306 avctx->channel_layout = channel_layout;
02307
02308 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
02309
02310 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
02311 mask = chset >= 0 ? s->xxch_spk_masks[chset]
02312 : s->xxch_core_spkmask;
02313 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
02314 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
02315 lavc = map_xxch_to_native[i];
02316 posn = av_popcount(channel_layout & (lavc - 1));
02317 s->xxch_order_tab[j++] = posn;
02318 }
02319 }
02320 }
02321
02322 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
02323 } else {
02324 for (i = 0; i < channels; i++)
02325 s->xxch_order_tab[i] = i;
02326
02327 s->lfe_index = channels - 1;
02328 }
02329
02330 s->channel_order_tab = s->xxch_order_tab;
02331 }
02332
02333 if (avctx->channels != channels) {
02334 if (avctx->channels)
02335 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
02336 avctx->channels = channels;
02337 }
02338
02339
02340 s->frame.nb_samples = 256 * (s->sample_blocks / 8);
02341 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
02342 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
02343 return ret;
02344 }
02345 samples_flt = (float *) s->frame.data[0];
02346 samples_s16 = (int16_t *) s->frame.data[0];
02347
02348
02349 for (i = 0; i < (s->sample_blocks / 8); i++) {
02350 dca_filter_channels(s, i);
02351
02352
02353
02354 if ((s->source_pcm_res & 1) && s->xch_present) {
02355 float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
02356 float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
02357 float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
02358 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
02359 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
02360 }
02361
02362
02363 if (s->xxch_dmix_embedded) {
02364
02365 ch = num_core_channels;
02366 for (chset = 0; chset < s->xxch_chset; chset++) {
02367 endch = ch + s->xxch_chset_nch[chset];
02368 mask = s->xxch_dmix_embedded;
02369
02370
02371 for (j = ch; j < endch; j++) {
02372 if (mask & (1 << j)) {
02373 src_chan = s->samples + s->channel_order_tab[j] * 256;
02374 for (k = 0; k < endch; k++) {
02375 achan = s->channel_order_tab[k];
02376 scale = s->xxch_dmix_coeff[j][k];
02377 if (scale != 0.0) {
02378 dst_chan = s->samples + achan * 256;
02379 s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
02380 -scale, 256);
02381 }
02382 }
02383 }
02384 }
02385
02386
02387 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
02388 scale = s->xxch_dmix_sf[chset];
02389
02390 for (j = 0; j < ch; j++) {
02391 src_chan = s->samples + s->channel_order_tab[j] * 256;
02392 for (k = 0; k < 256; k++)
02393 src_chan[k] *= scale;
02394 }
02395
02396
02397 if (s->lfe) {
02398 src_chan = s->samples + s->lfe_index * 256;
02399 for (k = 0; k < 256; k++)
02400 src_chan[k] *= scale;
02401 }
02402 }
02403
02404 ch = endch;
02405 }
02406
02407 }
02408
02409 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
02410 s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
02411 channels);
02412 samples_flt += 256 * channels;
02413 } else {
02414 s->fmt_conv.float_to_int16_interleave(samples_s16,
02415 s->samples_chanptr, 256,
02416 channels);
02417 samples_s16 += 256 * channels;
02418 }
02419 }
02420
02421
02422 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
02423 for (i = 0; i < 2 * s->lfe * 4; i++)
02424 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
02425
02426 *got_frame_ptr = 1;
02427 *(AVFrame *) data = s->frame;
02428
02429 return buf_size;
02430 }
02431
02432
02433
02440 static av_cold int dca_decode_init(AVCodecContext *avctx)
02441 {
02442 DCAContext *s = avctx->priv_data;
02443 int i;
02444
02445 s->avctx = avctx;
02446 dca_init_vlcs();
02447
02448 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
02449 ff_mdct_init(&s->imdct, 6, 1, 1.0);
02450 ff_synth_filter_init(&s->synth);
02451 ff_dcadsp_init(&s->dcadsp);
02452 ff_fmt_convert_init(&s->fmt_conv, avctx);
02453
02454 for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++)
02455 s->samples_chanptr[i] = s->samples + i * 256;
02456
02457 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
02458 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
02459 s->scale_bias = 1.0 / 32768.0;
02460 } else {
02461 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
02462 s->scale_bias = 1.0;
02463 }
02464
02465
02466 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
02467 avctx->request_channels == 2) {
02468 avctx->channels = avctx->request_channels;
02469 }
02470
02471 avcodec_get_frame_defaults(&s->frame);
02472 avctx->coded_frame = &s->frame;
02473
02474 return 0;
02475 }
02476
02477 static av_cold int dca_decode_end(AVCodecContext *avctx)
02478 {
02479 DCAContext *s = avctx->priv_data;
02480 ff_mdct_end(&s->imdct);
02481 return 0;
02482 }
02483
02484 static const AVProfile profiles[] = {
02485 { FF_PROFILE_DTS, "DTS" },
02486 { FF_PROFILE_DTS_ES, "DTS-ES" },
02487 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
02488 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
02489 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
02490 { FF_PROFILE_UNKNOWN },
02491 };
02492
02493 AVCodec ff_dca_decoder = {
02494 .name = "dca",
02495 .type = AVMEDIA_TYPE_AUDIO,
02496 .id = AV_CODEC_ID_DTS,
02497 .priv_data_size = sizeof(DCAContext),
02498 .init = dca_decode_init,
02499 .decode = dca_decode_frame,
02500 .close = dca_decode_end,
02501 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
02502 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
02503 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
02504 AV_SAMPLE_FMT_S16,
02505 AV_SAMPLE_FMT_NONE },
02506 .profiles = NULL_IF_CONFIG_SMALL(profiles),
02507 };