// 5.3.1 - Bit stream header
static int parse_frame_header(DCACoreDecoder *s)
{
- int normal_frame, pcmr_index;
+ DCACoreFrameHeader h = { 0 };
+ int err = ff_dca_parse_core_frame_header(&h, &s->gb);
- // Frame type
- normal_frame = get_bits1(&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;
- // Deficit sample count
- if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
- av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
- return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
- }
-
- // CRC present flag
- s->crc_present = get_bits1(&s->gb);
-
- // Number of PCM sample blocks
- s->npcmblocks = get_bits(&s->gb, 7) + 1;
- if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
- av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
- return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
- }
-
- // Primary frame byte size
- s->frame_size = get_bits(&s->gb, 14) + 1;
- if (s->frame_size < 96) {
- av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
- return AVERROR_INVALIDDATA;
- }
-
- // Audio channel arrangement
- s->audio_mode = get_bits(&s->gb, 6);
- if (s->audio_mode >= DCA_AMODE_COUNT) {
- av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
- return AVERROR_PATCHWELCOME;
- }
-
- // Core audio sampling frequency
- s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
- if (!s->sample_rate) {
- av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
- return AVERROR_INVALIDDATA;
- }
-
- // Transmission bit rate
- s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
-
- // Reserved field
- skip_bits1(&s->gb);
-
- // Embedded dynamic range flag
- s->drc_present = get_bits1(&s->gb);
-
- // Embedded time stamp flag
- s->ts_present = get_bits1(&s->gb);
-
- // Auxiliary data flag
- s->aux_present = get_bits1(&s->gb);
-
- // HDCD mastering flag
- skip_bits1(&s->gb);
+ 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;
- // Extension audio descriptor flag
- s->ext_audio_type = get_bits(&s->gb, 3);
-
- // Extended coding flag
- s->ext_audio_present = get_bits1(&s->gb);
-
- // Audio sync word insertion flag
- s->sync_ssf = get_bits1(&s->gb);
-
- // Low frequency effects flag
- s->lfe_present = get_bits(&s->gb, 2);
- if (s->lfe_present == DCA_LFE_FLAG_INVALID) {
- av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
- return AVERROR_INVALIDDATA;
- }
+ 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;
- // Predictor history flag switch
- s->predictor_history = get_bits1(&s->gb);
+ case DCA_PARSE_ERROR_AMODE:
+ av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode);
+ return AVERROR_PATCHWELCOME;
- // Header CRC check bytes
- if (s->crc_present)
- skip_bits(&s->gb, 16);
+ case DCA_PARSE_ERROR_SAMPLE_RATE:
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
+ return AVERROR_INVALIDDATA;
- // Multirate interpolator switch
- s->filter_perfect = get_bits1(&s->gb);
+ case DCA_PARSE_ERROR_RESERVED_BIT:
+ av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n");
+ return AVERROR_INVALIDDATA;
- // Encoder software revision
- skip_bits(&s->gb, 4);
+ case DCA_PARSE_ERROR_LFE_FLAG:
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
+ return AVERROR_INVALIDDATA;
- // Copy history
- skip_bits(&s->gb, 2);
+ case DCA_PARSE_ERROR_PCM_RES:
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
+ return AVERROR_INVALIDDATA;
- // Source PCM resolution
- s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
- if (!s->source_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->es_format = pcmr_index & 1;
-
- // Front sum/difference flag
- s->sumdiff_front = get_bits1(&s->gb);
-
- // Surround sum/difference flag
- s->sumdiff_surround = get_bits1(&s->gb);
- // Dialog normalization / unspecified
- skip_bits(&s->gb, 4);
+ 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 = avpriv_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;
}
int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
int last_pos = get_bits_count(&s->gb) / 32;
int size, dist;
+ uint32_t w1, w2 = 0;
// Search for extension sync words aligned on 4-byte boundary. Search
// must be done backwards from the end of core frame to work around
// compatibility with legacy bitstreams. Minimum XCH frame size is
// 96 bytes. AMODE and PCHS are further checked to reduce
// probability of alias sync detection.
- for (; sync_pos >= last_pos; sync_pos--) {
- if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
- s->gb.index = (sync_pos + 1) * 32;
- size = get_bits(&s->gb, 10) + 1;
+ for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
+ w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
+ if (w1 == DCA_SYNCWORD_XCH) {
+ size = (w2 >> 22) + 1;
dist = s->frame_size - sync_pos * 4;
if (size >= 96
&& (size == dist || size - 1 == dist)
- && get_bits(&s->gb, 7) == 0x08) {
- s->xch_pos = get_bits_count(&s->gb);
+ && (w2 >> 15 & 0x7f) == 0x08) {
+ s->xch_pos = sync_pos * 32 + 49;
break;
}
}
// The distance between X96 sync word and end of the core frame
// must be equal to X96 frame size. Minimum X96 frame size is 96
// bytes.
- for (; sync_pos >= last_pos; sync_pos--) {
- if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
- s->gb.index = (sync_pos + 1) * 32;
- size = get_bits(&s->gb, 12) + 1;
+ for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
+ w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
+ if (w1 == DCA_SYNCWORD_X96) {
+ size = (w2 >> 20) + 1;
dist = s->frame_size - sync_pos * 4;
if (size >= 96 && size == dist) {
- s->x96_pos = get_bits_count(&s->gb);
+ s->x96_pos = sync_pos * 32 + 44;
break;
}
}
// XXCH frame header CRC must be valid. Minimum XXCH frame header
// size is 11 bytes.
- for (; sync_pos >= last_pos; sync_pos--) {
- if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
- s->gb.index = (sync_pos + 1) * 32;
- size = get_bits(&s->gb, 6) + 1;
+ for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
+ w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
+ if (w1 == DCA_SYNCWORD_XXCH) {
+ size = (w2 >> 26) + 1;
dist = s->gb.size_in_bits / 8 - sync_pos * 4;
if (size >= 11 && size <= dist &&
!av_crc(dca->crctab, 0xffff, s->gb.buffer +
if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
return ret;
+ s->gb_in = s->gb;
- skip_bits_long(&s->gb, 32);
if ((ret = parse_frame_header(s)) < 0)
return ret;
if ((ret = alloc_sample_buffer(s)) < 0)
return ret;
// Workaround for DTS in WAV
- if (s->frame_size > size && s->frame_size < size + 4)
+ if (s->frame_size > size)
s->frame_size = size;
if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
{
AVCodecContext *avctx = s->avctx;
DCAContext *dca = avctx->priv_data;
- GetBitContext gb = s->gb;
int exss_mask = asset ? asset->extension_mask : 0;
int ret = 0, ext = 0;
ret = parse_xxch_frame(s);
ext = DCA_EXSS_XXCH;
} else if (s->xxch_pos) {
- s->gb.index = s->xxch_pos;
+ s->gb = s->gb_in;
+ skip_bits_long(&s->gb, s->xxch_pos);
ret = parse_xxch_frame(s);
ext = DCA_CSS_XXCH;
} else if (s->xch_pos) {
- s->gb.index = s->xch_pos;
+ s->gb = s->gb_in;
+ skip_bits_long(&s->gb, s->xch_pos);
ret = parse_xch_frame(s);
ext = DCA_CSS_XCH;
}
s->ext_audio_mask |= DCA_EXSS_X96;
}
} else if (s->x96_pos) {
- s->gb = gb;
- s->gb.index = s->x96_pos;
+ s->gb = s->gb_in;
+ skip_bits_long(&s->gb, s->x96_pos);
if ((ret = parse_x96_frame(s)) < 0) {
if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
return ret;