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