00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "libavutil/common.h"
00025 #include "libavutil/avassert.h"
00026 #include "libavutil/audioconvert.h"
00027 #include "avcodec.h"
00028 #include "get_bits.h"
00029 #include "put_bits.h"
00030 #include "dcaenc.h"
00031 #include "dcadata.h"
00032
00033 #undef NDEBUG
00034
00035 #define MAX_CHANNELS 6
00036 #define DCA_SUBBANDS_32 32
00037 #define DCA_MAX_FRAME_SIZE 16383
00038 #define DCA_HEADER_SIZE 13
00039
00040 #define DCA_SUBBANDS 32
00041 #define QUANTIZER_BITS 16
00042 #define SUBFRAMES 1
00043 #define SUBSUBFRAMES 4
00044 #define PCM_SAMPLES (SUBFRAMES*SUBSUBFRAMES*8)
00045 #define LFE_BITS 8
00046 #define LFE_INTERPOLATION 64
00047 #define LFE_PRESENT 2
00048 #define LFE_MISSING 0
00049
00050 static const int8_t dca_lfe_index[] = {
00051 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
00052 };
00053
00054 static const int8_t dca_channel_reorder_lfe[][9] = {
00055 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
00056 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00057 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00058 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00059 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00060 { 1, 2, 0, -1, -1, -1, -1, -1, -1 },
00061 { 0, 1, -1, 2, -1, -1, -1, -1, -1 },
00062 { 1, 2, 0, -1, 3, -1, -1, -1, -1 },
00063 { 0, 1, -1, 2, 3, -1, -1, -1, -1 },
00064 { 1, 2, 0, -1, 3, 4, -1, -1, -1 },
00065 { 2, 3, -1, 0, 1, 4, 5, -1, -1 },
00066 { 1, 2, 0, -1, 3, 4, 5, -1, -1 },
00067 { 0, -1, 4, 5, 2, 3, 1, -1, -1 },
00068 { 3, 4, 1, -1, 0, 2, 5, 6, -1 },
00069 { 2, 3, -1, 5, 7, 0, 1, 4, 6 },
00070 { 3, 4, 1, -1, 0, 2, 5, 7, 6 },
00071 };
00072
00073 static const int8_t dca_channel_reorder_nolfe[][9] = {
00074 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
00075 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00076 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00077 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00078 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
00079 { 1, 2, 0, -1, -1, -1, -1, -1, -1 },
00080 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
00081 { 1, 2, 0, 3, -1, -1, -1, -1, -1 },
00082 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
00083 { 1, 2, 0, 3, 4, -1, -1, -1, -1 },
00084 { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
00085 { 1, 2, 0, 3, 4, 5, -1, -1, -1 },
00086 { 0, 4, 5, 2, 3, 1, -1, -1, -1 },
00087 { 3, 4, 1, 0, 2, 5, 6, -1, -1 },
00088 { 2, 3, 5, 7, 0, 1, 4, 6, -1 },
00089 { 3, 4, 1, 0, 2, 5, 7, 6, -1 },
00090 };
00091
00092 typedef struct {
00093 PutBitContext pb;
00094 int32_t history[MAX_CHANNELS][512];
00095 int start[MAX_CHANNELS];
00096 int frame_size;
00097 int prim_channels;
00098 int lfe_channel;
00099 int sample_rate_code;
00100 int scale_factor[MAX_CHANNELS][DCA_SUBBANDS_32];
00101 int lfe_scale_factor;
00102 int lfe_data[SUBFRAMES*SUBSUBFRAMES*4];
00103
00104 int a_mode;
00105 int num_channel;
00106 int lfe_state;
00107 int lfe_offset;
00108 const int8_t *channel_order_tab;
00109
00110 int32_t pcm[FFMAX(LFE_INTERPOLATION, DCA_SUBBANDS_32)];
00111 int32_t subband[PCM_SAMPLES][MAX_CHANNELS][DCA_SUBBANDS_32];
00112 } DCAContext;
00113
00114 static int32_t cos_table[128];
00115
00116 static inline int32_t mul32(int32_t a, int32_t b)
00117 {
00118 int64_t r = (int64_t) a * b;
00119
00120 return (r + 0x80000000) >> 32;
00121 }
00122
00123
00124
00125 static void qmf_init(void)
00126 {
00127 int i;
00128 int32_t c[17], s[17];
00129 s[0] = 0;
00130 c[0] = 0x7fffffff;
00131
00132 for (i = 1; i <= 16; i++) {
00133 s[i] = 2 * (mul32(c[i - 1], 105372028) + mul32(s[i - 1], 2144896908));
00134 c[i] = 2 * (mul32(c[i - 1], 2144896908) - mul32(s[i - 1], 105372028));
00135 }
00136
00137 for (i = 0; i < 16; i++) {
00138 cos_table[i ] = c[i] >> 3;
00139 cos_table[i + 16] = s[16 - i] >> 3;
00140 cos_table[i + 32] = -s[i] >> 3;
00141 cos_table[i + 48] = -c[16 - i] >> 3;
00142 cos_table[i + 64] = -c[i] >> 3;
00143 cos_table[i + 80] = -s[16 - i] >> 3;
00144 cos_table[i + 96] = s[i] >> 3;
00145 cos_table[i + 112] = c[16 - i] >> 3;
00146 }
00147 }
00148
00149 static int32_t band_delta_factor(int band, int sample_num)
00150 {
00151 int index = band * (2 * sample_num + 1);
00152 if (band == 0)
00153 return 0x07ffffff;
00154 else
00155 return cos_table[index & 127];
00156 }
00157
00158 static void add_new_samples(DCAContext *c, const int32_t *in,
00159 int count, int channel)
00160 {
00161 int i;
00162
00163
00164 for (i = 0; i < count; i++) {
00165 c->history[channel][c->start[channel] + i] = in[i];
00166 av_assert0(c->start[channel] + i < 512);
00167 }
00168 c->start[channel] += count;
00169 if (c->start[channel] == 512)
00170 c->start[channel] = 0;
00171 av_assert0(c->start[channel] < 512);
00172 }
00173
00174 static void qmf_decompose(DCAContext *c, int32_t in[32], int32_t out[32],
00175 int channel)
00176 {
00177 int band, i, j, k;
00178 int32_t resp;
00179 int32_t accum[DCA_SUBBANDS_32] = {0};
00180
00181 add_new_samples(c, in, DCA_SUBBANDS_32, channel);
00182
00183
00184
00185
00186
00187
00188 for (k = 48, j = 0, i = c->start[channel]; i < 512; k++, j++, i++)
00189 accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
00190 for (i = 0; i < c->start[channel]; k++, j++, i++)
00191 accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
00192
00193 resp = 0;
00194
00195 for (band = 0; band < DCA_SUBBANDS_32; band++) {
00196 for (j = 0; j < 32; j++)
00197 resp += mul32(accum[j], band_delta_factor(band, j));
00198
00199 out[band] = (band & 2) ? (-resp) : resp;
00200 }
00201 }
00202
00203 static int32_t lfe_fir_64i[512];
00204 static int lfe_downsample(DCAContext *c, int32_t in[LFE_INTERPOLATION])
00205 {
00206 int i, j;
00207 int channel = c->prim_channels;
00208 int32_t accum = 0;
00209
00210 add_new_samples(c, in, LFE_INTERPOLATION, channel);
00211 for (i = c->start[channel], j = 0; i < 512; i++, j++)
00212 accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
00213 for (i = 0; i < c->start[channel]; i++, j++)
00214 accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
00215 return accum;
00216 }
00217
00218 static void init_lfe_fir(void)
00219 {
00220 static int initialized = 0;
00221 int i;
00222 if (initialized)
00223 return;
00224
00225 for (i = 0; i < 512; i++)
00226 lfe_fir_64i[i] = lfe_fir_64[i] * (1 << 25);
00227 initialized = 1;
00228 }
00229
00230 static void put_frame_header(DCAContext *c)
00231 {
00232
00233 put_bits(&c->pb, 16, 0x7ffe);
00234 put_bits(&c->pb, 16, 0x8001);
00235
00236
00237 put_bits(&c->pb, 1, 1);
00238
00239
00240 put_bits(&c->pb, 5, 31);
00241
00242
00243 put_bits(&c->pb, 1, 0);
00244
00245
00246 put_bits(&c->pb, 7, PCM_SAMPLES-1);
00247
00248
00249 put_bits(&c->pb, 14, c->frame_size-1);
00250
00251
00252 put_bits(&c->pb, 6, c->num_channel);
00253
00254
00255 put_bits(&c->pb, 4, c->sample_rate_code);
00256
00257
00258 put_bits(&c->pb, 5, 0x16);
00259
00260
00261 put_bits(&c->pb, 1, 0);
00262
00263
00264 put_bits(&c->pb, 1, 0);
00265
00266
00267 put_bits(&c->pb, 1, 0);
00268
00269
00270 put_bits(&c->pb, 1, 0);
00271
00272
00273 put_bits(&c->pb, 1, 0);
00274
00275
00276 put_bits(&c->pb, 3, 0);
00277
00278
00279 put_bits(&c->pb, 1, 0);
00280
00281
00282 put_bits(&c->pb, 1, 0);
00283
00284
00285 put_bits(&c->pb, 2, c->lfe_state);
00286
00287
00288 put_bits(&c->pb, 1, 1);
00289
00290
00291
00292 put_bits(&c->pb, 1, 0);
00293
00294
00295 put_bits(&c->pb, 4, 7);
00296
00297
00298 put_bits(&c->pb, 2, 0);
00299
00300
00301 put_bits(&c->pb, 3, 0);
00302
00303
00304 put_bits(&c->pb, 1, 0);
00305
00306
00307 put_bits(&c->pb, 1, 0);
00308
00309
00310 put_bits(&c->pb, 4, 0);
00311 }
00312
00313 static void put_primary_audio_header(DCAContext *c)
00314 {
00315 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
00316 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
00317
00318 int ch, i;
00319
00320 put_bits(&c->pb, 4, SUBFRAMES - 1);
00321
00322
00323 put_bits(&c->pb, 3, c->prim_channels - 1);
00324
00325
00326 for (ch = 0; ch < c->prim_channels; ch++)
00327 put_bits(&c->pb, 5, DCA_SUBBANDS - 2);
00328
00329
00330 for (ch = 0; ch < c->prim_channels; ch++)
00331 put_bits(&c->pb, 5, DCA_SUBBANDS - 1);
00332
00333
00334 for (ch = 0; ch < c->prim_channels; ch++)
00335 put_bits(&c->pb, 3, 0);
00336
00337
00338 for (ch = 0; ch < c->prim_channels; ch++)
00339 put_bits(&c->pb, 2, 0);
00340
00341
00342 for (ch = 0; ch < c->prim_channels; ch++)
00343 put_bits(&c->pb, 3, 6);
00344
00345
00346 for (ch = 0; ch < c->prim_channels; ch++)
00347 put_bits(&c->pb, 3, 6);
00348
00349
00350
00351
00352 for (i = 1; i < 11; i++)
00353 for (ch = 0; ch < c->prim_channels; ch++)
00354 put_bits(&c->pb, bitlen[i], thr[i]);
00355
00356
00357 }
00358
00364 static inline uint32_t quantize(int32_t sample, int bits)
00365 {
00366 av_assert0(sample < 1 << (bits - 1));
00367 av_assert0(sample >= -(1 << (bits - 1)));
00368 return sample & ((1 << bits) - 1);
00369 }
00370
00371 static inline int find_scale_factor7(int64_t max_value, int bits)
00372 {
00373 int i = 0, j = 128, q;
00374 max_value = ((max_value << 15) / lossy_quant[bits + 3]) >> (bits - 1);
00375 while (i < j) {
00376 q = (i + j) >> 1;
00377 if (max_value < scale_factor_quant7[q])
00378 j = q;
00379 else
00380 i = q + 1;
00381 }
00382 av_assert1(i < 128);
00383 return i;
00384 }
00385
00386 static inline void put_sample7(DCAContext *c, int64_t sample, int bits,
00387 int scale_factor)
00388 {
00389 sample = (sample << 15) / ((int64_t) lossy_quant[bits + 3] * scale_factor_quant7[scale_factor]);
00390 put_bits(&c->pb, bits, quantize((int) sample, bits));
00391 }
00392
00393 static void put_subframe(DCAContext *c,
00394 int32_t subband_data[8 * SUBSUBFRAMES][MAX_CHANNELS][32],
00395 int subframe)
00396 {
00397 int i, sub, ss, ch, max_value;
00398 int32_t *lfe_data = c->lfe_data + 4 * SUBSUBFRAMES * subframe;
00399
00400
00401 put_bits(&c->pb, 2, SUBSUBFRAMES -1);
00402
00403
00404 put_bits(&c->pb, 3, 0);
00405
00406
00407 for (ch = 0; ch < c->prim_channels; ch++)
00408 for (sub = 0; sub < DCA_SUBBANDS; sub++)
00409 put_bits(&c->pb, 1, 0);
00410
00411
00412
00413 for (ch = 0; ch < c->prim_channels; ch++)
00414 for (sub = 0; sub < DCA_SUBBANDS; sub++)
00415 put_bits(&c->pb, 5, QUANTIZER_BITS+3);
00416
00417 if (SUBSUBFRAMES > 1) {
00418
00419 for (ch = 0; ch < c->prim_channels; ch++)
00420 for (sub = 0; sub < DCA_SUBBANDS; sub++)
00421 put_bits(&c->pb, 1, 0);
00422 }
00423
00424
00425 for (ch = 0; ch < c->prim_channels; ch++)
00426 for (sub = 0; sub < DCA_SUBBANDS; sub++) {
00427 max_value = 0;
00428 for (i = 0; i < 8 * SUBSUBFRAMES; i++)
00429 max_value = FFMAX(max_value, FFABS(subband_data[i][ch][sub]));
00430 c->scale_factor[ch][sub] = find_scale_factor7(max_value, QUANTIZER_BITS);
00431 }
00432
00433 if (c->lfe_channel) {
00434 max_value = 0;
00435 for (i = 0; i < 4 * SUBSUBFRAMES; i++)
00436 max_value = FFMAX(max_value, FFABS(lfe_data[i]));
00437 c->lfe_scale_factor = find_scale_factor7(max_value, LFE_BITS);
00438 }
00439
00440
00441
00442 for (ch = 0; ch < c->prim_channels; ch++)
00443 for (sub = 0; sub < DCA_SUBBANDS; sub++)
00444 put_bits(&c->pb, 7, c->scale_factor[ch][sub]);
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 if (c->lfe_channel) {
00455 for (i = 0; i < 4 * SUBSUBFRAMES; i++)
00456 put_sample7(c, lfe_data[i], LFE_BITS, c->lfe_scale_factor);
00457 put_bits(&c->pb, 8, c->lfe_scale_factor);
00458 }
00459
00460
00461
00462 for (ss = 0; ss < SUBSUBFRAMES ; ss++)
00463 for (ch = 0; ch < c->prim_channels; ch++)
00464 for (sub = 0; sub < DCA_SUBBANDS; sub++)
00465 for (i = 0; i < 8; i++)
00466 put_sample7(c, subband_data[ss * 8 + i][ch][sub], QUANTIZER_BITS, c->scale_factor[ch][sub]);
00467
00468
00469 put_bits(&c->pb, 16, 0xffff);
00470 }
00471
00472 static void put_frame(DCAContext *c,
00473 int32_t subband_data[PCM_SAMPLES][MAX_CHANNELS][32],
00474 uint8_t *frame)
00475 {
00476 int i;
00477 init_put_bits(&c->pb, frame + DCA_HEADER_SIZE, DCA_MAX_FRAME_SIZE-DCA_HEADER_SIZE);
00478
00479 put_primary_audio_header(c);
00480 for (i = 0; i < SUBFRAMES; i++)
00481 put_subframe(c, &subband_data[SUBSUBFRAMES * 8 * i], i);
00482
00483 flush_put_bits(&c->pb);
00484 c->frame_size = (put_bits_count(&c->pb) >> 3) + DCA_HEADER_SIZE;
00485
00486 init_put_bits(&c->pb, frame, DCA_HEADER_SIZE);
00487 put_frame_header(c);
00488 flush_put_bits(&c->pb);
00489 }
00490
00491 static int encode_frame(AVCodecContext *avctx, uint8_t *frame,
00492 int buf_size, void *data)
00493 {
00494 int i, k, channel;
00495 DCAContext *c = avctx->priv_data;
00496 int16_t *samples = data;
00497 int real_channel = 0;
00498
00499 for (i = 0; i < PCM_SAMPLES; i ++) {
00500 for (channel = 0; channel < c->prim_channels + 1; channel++) {
00501
00502 for (k = 0; k < 32; k++) {
00503 c->pcm[k] = samples[avctx->channels * (32 * i + k) + channel] << 16;
00504 }
00505
00506 real_channel = c->channel_order_tab[channel];
00507 if (real_channel >= 0) {
00508 qmf_decompose(c, c->pcm, &c->subband[i][real_channel][0], real_channel);
00509 }
00510 }
00511 }
00512
00513 if (c->lfe_channel) {
00514 for (i = 0; i < PCM_SAMPLES / 2; i++) {
00515 for (k = 0; k < LFE_INTERPOLATION; k++)
00516 c->pcm[k] = samples[avctx->channels * (LFE_INTERPOLATION*i+k) + c->lfe_offset] << 16;
00517 c->lfe_data[i] = lfe_downsample(c, c->pcm);
00518 }
00519 }
00520
00521 put_frame(c, c->subband, frame);
00522
00523 return c->frame_size;
00524 }
00525
00526 static int encode_init(AVCodecContext *avctx)
00527 {
00528 DCAContext *c = avctx->priv_data;
00529 int i;
00530
00531 c->prim_channels = avctx->channels;
00532 c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
00533
00534 switch (avctx->channel_layout) {
00535 case AV_CH_LAYOUT_STEREO: c->a_mode = 2; c->num_channel = 2; break;
00536 case AV_CH_LAYOUT_5POINT0: c->a_mode = 9; c->num_channel = 9; break;
00537 case AV_CH_LAYOUT_5POINT1: c->a_mode = 9; c->num_channel = 9; break;
00538 case AV_CH_LAYOUT_5POINT0_BACK: c->a_mode = 9; c->num_channel = 9; break;
00539 case AV_CH_LAYOUT_5POINT1_BACK: c->a_mode = 9; c->num_channel = 9; break;
00540 default:
00541 av_log(avctx, AV_LOG_ERROR,
00542 "Only stereo, 5.0, 5.1 channel layouts supported at the moment!\n");
00543 return AVERROR_PATCHWELCOME;
00544 }
00545
00546 if (c->lfe_channel) {
00547 init_lfe_fir();
00548 c->prim_channels--;
00549 c->channel_order_tab = dca_channel_reorder_lfe[c->a_mode];
00550 c->lfe_state = LFE_PRESENT;
00551 c->lfe_offset = dca_lfe_index[c->a_mode];
00552 } else {
00553 c->channel_order_tab = dca_channel_reorder_nolfe[c->a_mode];
00554 c->lfe_state = LFE_MISSING;
00555 }
00556
00557 for (i = 0; i < 16; i++) {
00558 if (dca_sample_rates[i] && (dca_sample_rates[i] == avctx->sample_rate))
00559 break;
00560 }
00561 if (i == 16) {
00562 av_log(avctx, AV_LOG_ERROR, "Sample rate %iHz not supported, only ", avctx->sample_rate);
00563 for (i = 0; i < 16; i++)
00564 av_log(avctx, AV_LOG_ERROR, "%d, ", dca_sample_rates[i]);
00565 av_log(avctx, AV_LOG_ERROR, "supported.\n");
00566 return -1;
00567 }
00568 c->sample_rate_code = i;
00569
00570 avctx->frame_size = 32 * PCM_SAMPLES;
00571
00572 if (!cos_table[127])
00573 qmf_init();
00574 return 0;
00575 }
00576
00577 AVCodec ff_dca_encoder = {
00578 .name = "dca",
00579 .type = AVMEDIA_TYPE_AUDIO,
00580 .id = CODEC_ID_DTS,
00581 .priv_data_size = sizeof(DCAContext),
00582 .init = encode_init,
00583 .encode = encode_frame,
00584 .capabilities = CODEC_CAP_EXPERIMENTAL,
00585 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00586 };