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