/* * Copyright (C) 2016 foo86 * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/channel_layout.h"#include "libavutil/mem.h"#include "dcaadpcm.h"#include "dcadec.h"#include "dcadata.h"#include "dcahuff.h"#include "dcamath.h"#include "dca_syncwords.h"#include "decode.h" #if ARCH_ARM#include "arm/dca.h"#endif enum HeaderType { HEADER_CORE, HEADER_XCH, HEADER_XXCH}; static const int8_t prm_ch_to_spkr_map[DCA_AMODE_COUNT][5] = { { DCA_SPEAKER_C, -1, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 }, { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 }, { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }}; static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = { DCA_SPEAKER_LAYOUT_MONO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_3_0, DCA_SPEAKER_LAYOUT_2_1, DCA_SPEAKER_LAYOUT_3_1, DCA_SPEAKER_LAYOUT_2_2, DCA_SPEAKER_LAYOUT_5POINT0}; static const uint8_t block_code_nbits[7] = { 7, 10, 12, 13, 15, 17, 19}; static int dca_get_vlc(GetBitContext *s, const VLC *vlc){ return get_vlc2(s, vlc->table, vlc->bits, 2);} static void get_array(GetBitContext *s, int32_t *array, int size, int n){ int i; for (i = 0; i < size; i++) array[i] = get_sbits(s, n);} // 5.3.1 - Bit stream headerstatic int parse_frame_header(DCACoreDecoder *s){ DCACoreFrameHeader h = { 0 }; int err = ff_dca_parse_core_frame_header(&h, &s->gb); if (err < 0) { switch (err) { case DCA_PARSE_ERROR_DEFICIT_SAMPLES: av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n"); return h.normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME; case DCA_PARSE_ERROR_PCM_BLOCKS: av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", h.npcmblocks); return (h.npcmblocks < 6 || h.normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME; case DCA_PARSE_ERROR_FRAME_SIZE: av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", h.frame_size); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_AMODE: av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode); return AVERROR_PATCHWELCOME; case DCA_PARSE_ERROR_SAMPLE_RATE: av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n"); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_RESERVED_BIT: av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n"); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_LFE_FLAG: av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n"); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_PCM_RES: av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n"); return AVERROR_INVALIDDATA; default: av_log(s->avctx, AV_LOG_ERROR, "Unknown core frame header error\n"); return AVERROR_INVALIDDATA; } } s->crc_present = h.crc_present; s->npcmblocks = h.npcmblocks; s->frame_size = h.frame_size; s->audio_mode = h.audio_mode; s->sample_rate = ff_dca_sample_rates[h.sr_code]; s->bit_rate = ff_dca_bit_rates[h.br_code]; s->drc_present = h.drc_present; s->ts_present = h.ts_present; s->aux_present = h.aux_present; s->ext_audio_type = h.ext_audio_type; s->ext_audio_present = h.ext_audio_present; s->sync_ssf = h.sync_ssf; s->lfe_present = h.lfe_present; s->predictor_history = h.predictor_history; s->filter_perfect = h.filter_perfect; s->source_pcm_res = ff_dca_bits_per_sample[h.pcmr_code]; s->es_format = h.pcmr_code & 1; s->sumdiff_front = h.sumdiff_front; s->sumdiff_surround = h.sumdiff_surround; return 0;} // 5.3.2 - Primary audio coding headerstatic int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base){ int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb); unsigned int mask, index; if (get_bits_left(&s->gb) < 0) return AVERROR_INVALIDDATA; switch (header) { case HEADER_CORE: // Number of subframes s->nsubframes = get_bits(&s->gb, 4) + 1; // Number of primary audio channels s->nchannels = get_bits(&s->gb, 3) + 1; if (s->nchannels != ff_dca_channels[s->audio_mode]) { av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode); return AVERROR_INVALIDDATA; } av_assert1(s->nchannels <= DCA_CHANNELS - 2); s->ch_mask = audio_mode_ch_mask[s->audio_mode]; // Add LFE channel if present if (s->lfe_present) s->ch_mask |= DCA_SPEAKER_MASK_LFE1; break; case HEADER_XCH: s->nchannels = ff_dca_channels[s->audio_mode] + 1; av_assert1(s->nchannels <= DCA_CHANNELS - 1); s->ch_mask |= DCA_SPEAKER_MASK_Cs; break; case HEADER_XXCH: // Channel set header length header_size = get_bits(&s->gb, 7) + 1; // Check CRC if (s->xxch_crc_present && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n"); return AVERROR_INVALIDDATA; } // Number of channels in a channel set nchannels = get_bits(&s->gb, 3) + 1; if (nchannels > DCA_XXCH_CHANNELS_MAX) { avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels); return AVERROR_PATCHWELCOME; } s->nchannels = ff_dca_channels[s->audio_mode] + nchannels; av_assert1(s->nchannels <= DCA_CHANNELS); // Loudspeaker layout mask mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs); s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs; if (av_popcount(s->xxch_spkr_mask) != nchannels) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask); return AVERROR_INVALIDDATA; } if (s->xxch_core_mask & s->xxch_spkr_mask) { av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask); return AVERROR_INVALIDDATA; } // Combine core and XXCH masks together s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask; // Downmix coefficients present in stream if (get_bits1(&s->gb)) { int *coeff_ptr = s->xxch_dmix_coeff; // Downmix already performed by encoder s->xxch_dmix_embedded = get_bits1(&s->gb); // Downmix scale factor index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3; if (index >= FF_DCA_INV_DMIXTABLE_SIZE) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index); return AVERROR_INVALIDDATA; } s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index]; // Downmix channel mapping mask for (ch = 0; ch < nchannels; ch++) { mask = get_bits_long(&s->gb, s->xxch_mask_nbits); if ((mask & s->xxch_core_mask) != mask) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask); return AVERROR_INVALIDDATA; } s->xxch_dmix_mask[ch] = mask; } // Downmix coefficients for (ch = 0; ch < nchannels; ch++) { for (n = 0; n < s->xxch_mask_nbits; n++) { if (s->xxch_dmix_mask[ch] & (1U << n)) { int code = get_bits(&s->gb, 7); int sign = (code >> 6) - 1; if (code &= 63) { index = code * 4 - 3; if (index >= FF_DCA_DMIXTABLE_SIZE) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index); return AVERROR_INVALIDDATA; } *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign; } else { *coeff_ptr++ = 0; } } } } } else { s->xxch_dmix_embedded = 0; } break; } // Subband activity count for (ch = xch_base; ch < s->nchannels; ch++) { s->nsubbands[ch] = get_bits(&s->gb, 5) + 2; if (s->nsubbands[ch] > DCA_SUBBANDS) { av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n"); return AVERROR_INVALIDDATA; } } // High frequency VQ start subband for (ch = xch_base; ch < s->nchannels; ch++) s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1; // Joint intensity coding index for (ch = xch_base; ch < s->nchannels; ch++) { if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH) n += xch_base - 1; if (n > s->nchannels) { av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n"); return AVERROR_INVALIDDATA; } s->joint_intensity_index[ch] = n; } // Transient mode code book for (ch = xch_base; ch < s->nchannels; ch++) s->transition_mode_sel[ch] = get_bits(&s->gb, 2); // Scale factor code book for (ch = xch_base; ch < s->nchannels; ch++) { s->scale_factor_sel[ch] = get_bits(&s->gb, 3); if (s->scale_factor_sel[ch] == 7) { av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n"); return AVERROR_INVALIDDATA; } } // Bit allocation quantizer select for (ch = xch_base; ch < s->nchannels; ch++) { s->bit_allocation_sel[ch] = get_bits(&s->gb, 3); if (s->bit_allocation_sel[ch] == 7) { av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n"); return AVERROR_INVALIDDATA; } } // Quantization index codebook select for (n = 0; n < DCA_CODE_BOOKS; n++) for (ch = xch_base; ch < s->nchannels; ch++) s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]); // Scale factor adjustment index for (n = 0; n < DCA_CODE_BOOKS; n++) for (ch = xch_base; ch < s->nchannels; ch++) if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n]) s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)]; if (header == HEADER_XXCH) { // Reserved // Byte align // CRC16 of channel set header if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n"); return AVERROR_INVALIDDATA; } } else { // Audio header CRC check word if (s->crc_present) skip_bits(&s->gb, 16); } return 0;} static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel){ const uint32_t *scale_table; unsigned int scale_size; // Select the root square table if (sel > 5) { scale_table = ff_dca_scale_factor_quant7; scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7); } else { scale_table = ff_dca_scale_factor_quant6; scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6); } // If Huffman code was used, the difference of scales was encoded if (sel < 5) *scale_index += get_vlc2(&s->gb, ff_dca_vlc_scale_factor[sel].table, DCA_SCALES_VLC_BITS, 2); else *scale_index = get_bits(&s->gb, sel + 1); // Look up scale factor from the root square table if ((unsigned int)*scale_index >= scale_size) { av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n"); return AVERROR_INVALIDDATA; } return scale_table[*scale_index];} static inline int parse_joint_scale(DCACoreDecoder *s, int sel){ int scale_index; // Absolute value was encoded even when Huffman code was used if (sel < 5) scale_index = get_vlc2(&s->gb, ff_dca_vlc_scale_factor[sel].table, DCA_SCALES_VLC_BITS, 2); else scale_index = get_bits(&s->gb, sel + 1); // Bias by 64 scale_index += 64; // Look up joint scale factor if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) { av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n"); return AVERROR_INVALIDDATA; } return ff_dca_joint_scale_factors[scale_index];} // 5.4.1 - Primary audio coding side informationstatic int parse_subframe_header(DCACoreDecoder *s, int sf, enum HeaderType header, int xch_base){ int ch, band, ret; if (get_bits_left(&s->gb) < 0) return AVERROR_INVALIDDATA; if (header == HEADER_CORE) { // Subsubframe count s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1; // Partial subsubframe sample count skip_bits(&s->gb, 3); } // Prediction mode for (ch = xch_base; ch < s->nchannels; ch++) for (band = 0; band < s->nsubbands[ch]; band++) s->prediction_mode[ch][band] = get_bits1(&s->gb); // Prediction coefficients VQ address for (ch = xch_base; ch < s->nchannels; ch++) for (band = 0; band < s->nsubbands[ch]; band++) if (s->prediction_mode[ch][band]) s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12); // Bit allocation index for (ch = xch_base; ch < s->nchannels; ch++) { int sel = s->bit_allocation_sel[ch]; for (band = 0; band < s->subband_vq_start[ch]; band++) { int abits; if (sel < 5) abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation[sel]); else abits = get_bits(&s->gb, sel - 1); if (abits > DCA_ABITS_MAX) { av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n"); return AVERROR_INVALIDDATA; } s->bit_allocation[ch][band] = abits; } } // Transition mode for (ch = xch_base; ch < s->nchannels; ch++) { // Clear transition mode for all subbands memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0])); // Transient possible only if more than one subsubframe if (s->nsubsubframes[sf] > 1) { int sel = s->transition_mode_sel[ch]; for (band = 0; band < s->subband_vq_start[ch]; band++) if (s->bit_allocation[ch][band]) s->transition_mode[sf][ch][band] = get_vlc2(&s->gb, ff_dca_vlc_transition_mode[sel].table, DCA_TMODE_VLC_BITS, 1); } } // Scale factors for (ch = xch_base; ch < s->nchannels; ch++) { int sel = s->scale_factor_sel[ch]; int scale_index = 0; // Extract scales for subbands up to VQ for (band = 0; band < s->subband_vq_start[ch]; band++) { if (s->bit_allocation[ch][band]) { if ((ret = parse_scale(s, &scale_index, sel)) < 0) return ret; s->scale_factors[ch][band][0] = ret; if (s->transition_mode[sf][ch][band]) { if ((ret = parse_scale(s, &scale_index, sel)) < 0) return ret; s->scale_factors[ch][band][1] = ret; } } else { s->scale_factors[ch][band][0] = 0; } } // High frequency VQ subbands for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) { if ((ret = parse_scale(s, &scale_index, sel)) < 0) return ret; s->scale_factors[ch][band][0] = ret; } } // Joint subband codebook select for (ch = xch_base; ch < s->nchannels; ch++) { if (s->joint_intensity_index[ch]) { s->joint_scale_sel[ch] = get_bits(&s->gb, 3); if (s->joint_scale_sel[ch] == 7) {Lines 1–500 of 2453. Load more
/* * Copyright (C) 2016 foo86 * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/channel_layout.h" #include "libavutil/mem.h" #include "dcaadpcm.h" #include "dcadec.h" #include "dcadata.h" #include "dcahuff.h" #include "dcamath.h" #include "dca_syncwords.h" #include "decode.h" #if ARCH_ARM #include "arm/dca.h" #endif enum HeaderType { HEADER_CORE, HEADER_XCH, HEADER_XXCH }; static const int8_t prm_ch_to_spkr_map[DCA_AMODE_COUNT][5] = { { DCA_SPEAKER_C, -1, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 }, { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 }, { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 }, { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs } }; static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = { DCA_SPEAKER_LAYOUT_MONO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_STEREO, DCA_SPEAKER_LAYOUT_3_0, DCA_SPEAKER_LAYOUT_2_1, DCA_SPEAKER_LAYOUT_3_1, DCA_SPEAKER_LAYOUT_2_2, DCA_SPEAKER_LAYOUT_5POINT0 }; static const uint8_t block_code_nbits[7] = { 7, 10, 12, 13, 15, 17, 19 }; static int dca_get_vlc(GetBitContext *s, const VLC *vlc) { return get_vlc2(s, vlc->table, vlc->bits, 2); } static void get_array(GetBitContext *s, int32_t *array, int size, int n) { int i; for (i = 0; i < size; i++) array[i] = get_sbits(s, n); } // 5.3.1 - Bit stream header static int parse_frame_header(DCACoreDecoder *s) { DCACoreFrameHeader h = { 0 }; int err = ff_dca_parse_core_frame_header(&h, &s->gb); if (err < 0) { switch (err) { case DCA_PARSE_ERROR_DEFICIT_SAMPLES: av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n"); return h.normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME; case DCA_PARSE_ERROR_PCM_BLOCKS: av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", h.npcmblocks); return (h.npcmblocks < 6 || h.normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME; case DCA_PARSE_ERROR_FRAME_SIZE: av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", h.frame_size); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_AMODE: av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode); return AVERROR_PATCHWELCOME; case DCA_PARSE_ERROR_SAMPLE_RATE: av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n"); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_RESERVED_BIT: av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n"); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_LFE_FLAG: av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n"); return AVERROR_INVALIDDATA; case DCA_PARSE_ERROR_PCM_RES: av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n"); return AVERROR_INVALIDDATA; default: av_log(s->avctx, AV_LOG_ERROR, "Unknown core frame header error\n"); return AVERROR_INVALIDDATA; } } s->crc_present = h.crc_present; s->npcmblocks = h.npcmblocks; s->frame_size = h.frame_size; s->audio_mode = h.audio_mode; s->sample_rate = ff_dca_sample_rates[h.sr_code]; s->bit_rate = ff_dca_bit_rates[h.br_code]; s->drc_present = h.drc_present; s->ts_present = h.ts_present; s->aux_present = h.aux_present; s->ext_audio_type = h.ext_audio_type; s->ext_audio_present = h.ext_audio_present; s->sync_ssf = h.sync_ssf; s->lfe_present = h.lfe_present; s->predictor_history = h.predictor_history; s->filter_perfect = h.filter_perfect; s->source_pcm_res = ff_dca_bits_per_sample[h.pcmr_code]; s->es_format = h.pcmr_code & 1; s->sumdiff_front = h.sumdiff_front; s->sumdiff_surround = h.sumdiff_surround; return 0; } // 5.3.2 - Primary audio coding header static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base) { int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb); unsigned int mask, index; if (get_bits_left(&s->gb) < 0) return AVERROR_INVALIDDATA; switch (header) { case HEADER_CORE: // Number of subframes s->nsubframes = get_bits(&s->gb, 4) + 1; // Number of primary audio channels s->nchannels = get_bits(&s->gb, 3) + 1; if (s->nchannels != ff_dca_channels[s->audio_mode]) { av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode); return AVERROR_INVALIDDATA; } av_assert1(s->nchannels <= DCA_CHANNELS - 2); s->ch_mask = audio_mode_ch_mask[s->audio_mode]; // Add LFE channel if present if (s->lfe_present) s->ch_mask |= DCA_SPEAKER_MASK_LFE1; break; case HEADER_XCH: s->nchannels = ff_dca_channels[s->audio_mode] + 1; av_assert1(s->nchannels <= DCA_CHANNELS - 1); s->ch_mask |= DCA_SPEAKER_MASK_Cs; break; case HEADER_XXCH: // Channel set header length header_size = get_bits(&s->gb, 7) + 1; // Check CRC if (s->xxch_crc_present && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n"); return AVERROR_INVALIDDATA; } // Number of channels in a channel set nchannels = get_bits(&s->gb, 3) + 1; if (nchannels > DCA_XXCH_CHANNELS_MAX) { avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels); return AVERROR_PATCHWELCOME; } s->nchannels = ff_dca_channels[s->audio_mode] + nchannels; av_assert1(s->nchannels <= DCA_CHANNELS); // Loudspeaker layout mask mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs); s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs; if (av_popcount(s->xxch_spkr_mask) != nchannels) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask); return AVERROR_INVALIDDATA; } if (s->xxch_core_mask & s->xxch_spkr_mask) { av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask); return AVERROR_INVALIDDATA; } // Combine core and XXCH masks together s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask; // Downmix coefficients present in stream if (get_bits1(&s->gb)) { int *coeff_ptr = s->xxch_dmix_coeff; // Downmix already performed by encoder s->xxch_dmix_embedded = get_bits1(&s->gb); // Downmix scale factor index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3; if (index >= FF_DCA_INV_DMIXTABLE_SIZE) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index); return AVERROR_INVALIDDATA; } s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index]; // Downmix channel mapping mask for (ch = 0; ch < nchannels; ch++) { mask = get_bits_long(&s->gb, s->xxch_mask_nbits); if ((mask & s->xxch_core_mask) != mask) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask); return AVERROR_INVALIDDATA; } s->xxch_dmix_mask[ch] = mask; } // Downmix coefficients for (ch = 0; ch < nchannels; ch++) { for (n = 0; n < s->xxch_mask_nbits; n++) { if (s->xxch_dmix_mask[ch] & (1U << n)) { int code = get_bits(&s->gb, 7); int sign = (code >> 6) - 1; if (code &= 63) { index = code * 4 - 3; if (index >= FF_DCA_DMIXTABLE_SIZE) { av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index); return AVERROR_INVALIDDATA; } *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign; } else { *coeff_ptr++ = 0; } } } } } else { s->xxch_dmix_embedded = 0; } break; } // Subband activity count for (ch = xch_base; ch < s->nchannels; ch++) { s->nsubbands[ch] = get_bits(&s->gb, 5) + 2; if (s->nsubbands[ch] > DCA_SUBBANDS) { av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n"); return AVERROR_INVALIDDATA; } } // High frequency VQ start subband for (ch = xch_base; ch < s->nchannels; ch++) s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1; // Joint intensity coding index for (ch = xch_base; ch < s->nchannels; ch++) { if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH) n += xch_base - 1; if (n > s->nchannels) { av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n"); return AVERROR_INVALIDDATA; } s->joint_intensity_index[ch] = n; } // Transient mode code book for (ch = xch_base; ch < s->nchannels; ch++) s->transition_mode_sel[ch] = get_bits(&s->gb, 2); // Scale factor code book for (ch = xch_base; ch < s->nchannels; ch++) { s->scale_factor_sel[ch] = get_bits(&s->gb, 3); if (s->scale_factor_sel[ch] == 7) { av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n"); return AVERROR_INVALIDDATA; } } // Bit allocation quantizer select for (ch = xch_base; ch < s->nchannels; ch++) { s->bit_allocation_sel[ch] = get_bits(&s->gb, 3); if (s->bit_allocation_sel[ch] == 7) { av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n"); return AVERROR_INVALIDDATA; } } // Quantization index codebook select for (n = 0; n < DCA_CODE_BOOKS; n++) for (ch = xch_base; ch < s->nchannels; ch++) s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]); // Scale factor adjustment index for (n = 0; n < DCA_CODE_BOOKS; n++) for (ch = xch_base; ch < s->nchannels; ch++) if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n]) s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)]; if (header == HEADER_XXCH) { // Reserved // Byte align // CRC16 of channel set header if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n"); return AVERROR_INVALIDDATA; } } else { // Audio header CRC check word if (s->crc_present) skip_bits(&s->gb, 16); } return 0; } static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel) { const uint32_t *scale_table; unsigned int scale_size; // Select the root square table if (sel > 5) { scale_table = ff_dca_scale_factor_quant7; scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7); } else { scale_table = ff_dca_scale_factor_quant6; scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6); } // If Huffman code was used, the difference of scales was encoded if (sel < 5) *scale_index += get_vlc2(&s->gb, ff_dca_vlc_scale_factor[sel].table, DCA_SCALES_VLC_BITS, 2); else *scale_index = get_bits(&s->gb, sel + 1); // Look up scale factor from the root square table if ((unsigned int)*scale_index >= scale_size) { av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n"); return AVERROR_INVALIDDATA; } return scale_table[*scale_index]; } static inline int parse_joint_scale(DCACoreDecoder *s, int sel) { int scale_index; // Absolute value was encoded even when Huffman code was used if (sel < 5) scale_index = get_vlc2(&s->gb, ff_dca_vlc_scale_factor[sel].table, DCA_SCALES_VLC_BITS, 2); else scale_index = get_bits(&s->gb, sel + 1); // Bias by 64 scale_index += 64; // Look up joint scale factor if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) { av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n"); return AVERROR_INVALIDDATA; } return ff_dca_joint_scale_factors[scale_index]; } // 5.4.1 - Primary audio coding side information static int parse_subframe_header(DCACoreDecoder *s, int sf, enum HeaderType header, int xch_base) { int ch, band, ret; if (get_bits_left(&s->gb) < 0) return AVERROR_INVALIDDATA; if (header == HEADER_CORE) { // Subsubframe count s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1; // Partial subsubframe sample count skip_bits(&s->gb, 3); } // Prediction mode for (ch = xch_base; ch < s->nchannels; ch++) for (band = 0; band < s->nsubbands[ch]; band++) s->prediction_mode[ch][band] = get_bits1(&s->gb); // Prediction coefficients VQ address for (ch = xch_base; ch < s->nchannels; ch++) for (band = 0; band < s->nsubbands[ch]; band++) if (s->prediction_mode[ch][band]) s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12); // Bit allocation index for (ch = xch_base; ch < s->nchannels; ch++) { int sel = s->bit_allocation_sel[ch]; for (band = 0; band < s->subband_vq_start[ch]; band++) { int abits; if (sel < 5) abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation[sel]); else abits = get_bits(&s->gb, sel - 1); if (abits > DCA_ABITS_MAX) { av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n"); return AVERROR_INVALIDDATA; } s->bit_allocation[ch][band] = abits; } } // Transition mode for (ch = xch_base; ch < s->nchannels; ch++) { // Clear transition mode for all subbands memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0])); // Transient possible only if more than one subsubframe if (s->nsubsubframes[sf] > 1) { int sel = s->transition_mode_sel[ch]; for (band = 0; band < s->subband_vq_start[ch]; band++) if (s->bit_allocation[ch][band]) s->transition_mode[sf][ch][band] = get_vlc2(&s->gb, ff_dca_vlc_transition_mode[sel].table, DCA_TMODE_VLC_BITS, 1); } } // Scale factors for (ch = xch_base; ch < s->nchannels; ch++) { int sel = s->scale_factor_sel[ch]; int scale_index = 0; // Extract scales for subbands up to VQ for (band = 0; band < s->subband_vq_start[ch]; band++) { if (s->bit_allocation[ch][band]) { if ((ret = parse_scale(s, &scale_index, sel)) < 0) return ret; s->scale_factors[ch][band][0] = ret; if (s->transition_mode[sf][ch][band]) { if ((ret = parse_scale(s, &scale_index, sel)) < 0) return ret; s->scale_factors[ch][band][1] = ret; } } else { s->scale_factors[ch][band][0] = 0; } } // High frequency VQ subbands for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) { if ((ret = parse_scale(s, &scale_index, sel)) < 0) return ret; s->scale_factors[ch][band][0] = ret; } } // Joint subband codebook select for (ch = xch_base; ch < s->nchannels; ch++) { if (s->joint_intensity_index[ch]) { s->joint_scale_sel[ch] = get_bits(&s->gb, 3); if (s->joint_scale_sel[ch] == 7) {