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