📄 dca.c
字号:
if (s->bitalloc_huffman[j] == 6) s->bitalloc[j][k] = get_bits(&s->gb, 5); else if (s->bitalloc_huffman[j] == 5) s->bitalloc[j][k] = get_bits(&s->gb, 4); else if (s->bitalloc_huffman[j] == 7) { av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n"); return -1; } else { s->bitalloc[j][k] = get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]); } if (s->bitalloc[j][k] > 26) {// av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",// j, k, s->bitalloc[j][k]); return -1; } } } /* Transition mode */ for (j = 0; j < s->prim_channels; j++) { for (k = 0; k < s->subband_activity[j]; k++) { s->transition_mode[j][k] = 0; if (s->subsubframes > 1 && k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) { s->transition_mode[j][k] = get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]); } } } for (j = 0; j < s->prim_channels; j++) { const uint32_t *scale_table; int scale_sum; memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2); if (s->scalefactor_huffman[j] == 6) scale_table = scale_factor_quant7; else scale_table = scale_factor_quant6; /* When huffman coded, only the difference is encoded */ scale_sum = 0; for (k = 0; k < s->subband_activity[j]; k++) { if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) { scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); s->scale_factor[j][k][0] = scale_table[scale_sum]; } if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) { /* Get second scale factor */ scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); s->scale_factor[j][k][1] = scale_table[scale_sum]; } } } /* Joint subband scale factor codebook select */ for (j = 0; j < s->prim_channels; j++) { /* Transmitted only if joint subband coding enabled */ if (s->joint_intensity[j] > 0) s->joint_huff[j] = get_bits(&s->gb, 3); } /* Scale factors for joint subband coding */ for (j = 0; j < s->prim_channels; j++) { int source_channel; /* Transmitted only if joint subband coding enabled */ if (s->joint_intensity[j] > 0) { int scale = 0; source_channel = s->joint_intensity[j] - 1; /* When huffman coded, only the difference is encoded * (is this valid as well for joint scales ???) */ for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) { scale = get_scale(&s->gb, s->joint_huff[j], 0); scale += 64; /* bias */ s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */ } if (!s->debug_flag & 0x02) { av_log(s->avctx, AV_LOG_DEBUG, "Joint stereo coding not supported\n"); s->debug_flag |= 0x02; } } } /* Stereo downmix coefficients */ if (s->prim_channels > 2) { if(s->downmix) { for (j = 0; j < s->prim_channels; j++) { s->downmix_coef[j][0] = get_bits(&s->gb, 7); s->downmix_coef[j][1] = get_bits(&s->gb, 7); } } else { int am = s->amode & DCA_CHANNEL_MASK; for (j = 0; j < s->prim_channels; j++) { s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; } } } /* Dynamic range coefficient */ if (s->dynrange) s->dynrange_coef = get_bits(&s->gb, 8); /* Side information CRC check word */ if (s->crc_present) { get_bits(&s->gb, 16); } /* * Primary audio data arrays */ /* VQ encoded high frequency subbands */ for (j = 0; j < s->prim_channels; j++) for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) /* 1 vector -> 32 samples */ s->high_freq_vq[j][k] = get_bits(&s->gb, 10); /* Low frequency effect data */ if (s->lfe) { /* LFE samples */ int lfe_samples = 2 * s->lfe * s->subsubframes; float lfe_scale; for (j = lfe_samples; j < lfe_samples * 2; j++) { /* Signed 8 bits int */ s->lfe_data[j] = get_sbits(&s->gb, 8); } /* Scale factor index */ s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)]; /* Quantization step size * scale factor */ lfe_scale = 0.035 * s->lfe_scale_factor; for (j = lfe_samples; j < lfe_samples * 2; j++) s->lfe_data[j] *= lfe_scale; }#ifdef TRACE av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes); av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n", s->partial_samples); for (j = 0; j < s->prim_channels; j++) { av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:"); for (k = 0; k < s->subband_activity[j]; k++) av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]); av_log(s->avctx, AV_LOG_DEBUG, "\n"); } for (j = 0; j < s->prim_channels; j++) { for (k = 0; k < s->subband_activity[j]; k++) av_log(s->avctx, AV_LOG_DEBUG, "prediction coefs: %f, %f, %f, %f\n", (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192, (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192, (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192, (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192); } for (j = 0; j < s->prim_channels; j++) { av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: "); for (k = 0; k < s->vq_start_subband[j]; k++) av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]); av_log(s->avctx, AV_LOG_DEBUG, "\n"); } for (j = 0; j < s->prim_channels; j++) { av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:"); for (k = 0; k < s->subband_activity[j]; k++) av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]); av_log(s->avctx, AV_LOG_DEBUG, "\n"); } for (j = 0; j < s->prim_channels; j++) { av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:"); for (k = 0; k < s->subband_activity[j]; k++) { if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]); if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]); } av_log(s->avctx, AV_LOG_DEBUG, "\n"); } for (j = 0; j < s->prim_channels; j++) { if (s->joint_intensity[j] > 0) { int source_channel = s->joint_intensity[j] - 1; av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n"); for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]); av_log(s->avctx, AV_LOG_DEBUG, "\n"); } } if (s->prim_channels > 2 && s->downmix) { av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n"); for (j = 0; j < s->prim_channels; j++) { av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]); av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]); } av_log(s->avctx, AV_LOG_DEBUG, "\n"); } for (j = 0; j < s->prim_channels; j++) for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]); if(s->lfe){ int lfe_samples = 2 * s->lfe * s->subsubframes; av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n"); for (j = lfe_samples; j < lfe_samples * 2; j++) av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]); av_log(s->avctx, AV_LOG_DEBUG, "\n"); }#endif return 0;}static void qmf_32_subbands(DCAContext * s, int chans, float samples_in[32][8], float *samples_out, float scale, float bias){ const float *prCoeff; int i, j, k; float praXin[33], *raXin = &praXin[1]; float *subband_fir_hist = s->subband_fir_hist[chans]; float *subband_fir_hist2 = s->subband_fir_noidea[chans]; int chindex = 0, subindex; praXin[0] = 0.0; /* Select filter */ if (!s->multirate_inter) /* Non-perfect reconstruction */ prCoeff = fir_32bands_nonperfect; else /* Perfect reconstruction */ prCoeff = fir_32bands_perfect; /* Reconstructed channel sample index */ for (subindex = 0; subindex < 8; subindex++) { float t1, t2, sum[16], diff[16]; /* Load in one sample from each subband and clear inactive subbands */ for (i = 0; i < s->subband_activity[chans]; i++) raXin[i] = samples_in[i][subindex]; for (; i < 32; i++) raXin[i] = 0.0; /* Multiply by cosine modulation coefficients and * create temporary arrays SUM and DIFF */ for (j = 0, k = 0; k < 16; k++) { t1 = 0.0; t2 = 0.0; for (i = 0; i < 16; i++, j++){ t1 += (raXin[2 * i] + raXin[2 * i + 1]) * cos_mod[j]; t2 += (raXin[2 * i] + raXin[2 * i - 1]) * cos_mod[j + 256]; } sum[k] = t1 + t2; diff[k] = t1 - t2; } j = 512; /* Store history */ for (k = 0; k < 16; k++) subband_fir_hist[k] = cos_mod[j++] * sum[k]; for (k = 0; k < 16; k++) subband_fir_hist[32-k-1] = cos_mod[j++] * diff[k]; /* Multiply by filter coefficients */ for (k = 31, i = 0; i < 32; i++, k--) for (j = 0; j < 512; j += 64){ subband_fir_hist2[i] += prCoeff[i+j] * ( subband_fir_hist[i+j] - subband_fir_hist[j+k]); subband_fir_hist2[i+32] += prCoeff[i+j+32]*(-subband_fir_hist[i+j] - subband_fir_hist[j+k]); } /* Create 32 PCM output samples */ for (i = 0; i < 32; i++) samples_out[chindex++] = subband_fir_hist2[i] * scale + bias; /* Update working arrays */ memmove(&subband_fir_hist[32], &subband_fir_hist[0], (512 - 32) * sizeof(float)); memmove(&subband_fir_hist2[0], &subband_fir_hist2[32], 32 * sizeof(float)); memset(&subband_fir_hist2[32], 0, 32 * sizeof(float)); }}static void lfe_interpolation_fir(int decimation_select, int num_deci_sample, float *samples_in, float *samples_out, float scale, float bias){ /* samples_in: An array holding decimated samples. * Samples in current subframe starts from samples_in[0], * while samples_in[-1], samples_in[-2], ..., stores samples * from last subframe as history. * * samples_out: An array holding interpolated samples */ int decifactor, k, j; const float *prCoeff; int interp_index = 0; /* Index to the interpolated samples */ int deciindex; /* Select decimation filter */ if (decimation_select == 1) { decifactor = 128; prCoeff = lfe_fir_128; } else { decifactor = 64; prCoeff = lfe_fir_64; } /* Interpolation */ for (deciindex = 0; deciindex < num_deci_sample; deciindex++) { /* One decimated sample generates decifactor interpolated ones */ for (k = 0; k < decifactor; k++) { float rTmp = 0.0; //FIXME the coeffs are symetric, fix that for (j = 0; j < 512 / decifactor; j++) rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor]; samples_out[interp_index++] = rTmp / scale + bias; } }}/* downmixing routines */#define MIX_REAR1(samples, si1, rs, coef) \ samples[i] += samples[si1] * coef[rs][0]; \ samples[i+256] += samples[si1] * coef[rs][1];#define MIX_REAR2(samples, si1, si2, rs, coef) \ samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \ samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];#define MIX_FRONT3(samples, coef) \ t = samples[i]; \ samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \ samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];#define DOWNMIX_TO_STEREO(op1, op2) \ for(i = 0; i < 256; i++){ \ op1 \ op2 \ }static void dca_downmix(float *samples, int srcfmt, int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]){ int i; float t; float coef[DCA_PRIM_CHANNELS_MAX][2]; for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) { coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]]; coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]]; } switch (srcfmt) { case DCA_MONO: case DCA_CHANNEL: case DCA_STEREO_TOTAL: case DCA_STEREO_SUMDIFF: case DCA_4F2R: av_log(NULL, 0, "Not implemented!\n"); break; case DCA_STEREO: break; case DCA_3F: DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),); break; case DCA_2F1R: DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),); break; case DCA_3F1R: DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), MIX_REAR1(samples, i + 768, 3, coef)); break; case DCA_2F2R: DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),); break; case DCA_3F2R: DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), MIX_REAR2(samples, i + 768, i + 1024, 3, coef)); break; }}/* Very compact version of the block code decoder that does not use table * look-up but is slightly slower */static int decode_blockcode(int code, int levels, int *values){ int i; int offset = (levels - 1) >> 1; for (i = 0; i < 4; i++) { values[i] = (code % levels) - offset; code /= levels; } if (code == 0) return 0; else { av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n"); return -1; }}static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };static int dca_subsubframe(DCAContext * s){ int k, l; int subsubframe = s->current_subsubframe; const float *quant_step_table; /* FIXME */ float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -