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