]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/dca_core.c
opus_pvq_search: only use rsqrtps approximation on CPUs with avx
[ffmpeg] / libavcodec / dca_core.c
index 36040f6f9d2b90b2975edcec18f4ea327190fffe..6cb1f30a3c161da7e73745d3e46f87f872048f63 100644 (file)
@@ -35,35 +35,7 @@ enum HeaderType {
     HEADER_XXCH
 };
 
-enum AudioMode {
-    AMODE_MONO,             // Mode 0: A (mono)
-    AMODE_MONO_DUAL,        // Mode 1: A + B (dual mono)
-    AMODE_STEREO,           // Mode 2: L + R (stereo)
-    AMODE_STEREO_SUMDIFF,   // Mode 3: (L+R) + (L-R) (sum-diff)
-    AMODE_STEREO_TOTAL,     // Mode 4: LT + RT (left and right total)
-    AMODE_3F,               // Mode 5: C + L + R
-    AMODE_2F1R,             // Mode 6: L + R + S
-    AMODE_3F1R,             // Mode 7: C + L + R + S
-    AMODE_2F2R,             // Mode 8: L + R + SL + SR
-    AMODE_3F2R,             // Mode 9: C + L + R + SL + SR
-
-    AMODE_COUNT
-};
-
-enum ExtAudioType {
-    EXT_AUDIO_XCH   = 0,
-    EXT_AUDIO_X96   = 2,
-    EXT_AUDIO_XXCH  = 6
-};
-
-enum LFEFlag {
-    LFE_FLAG_NONE,
-    LFE_FLAG_128,
-    LFE_FLAG_64,
-    LFE_FLAG_INVALID
-};
-
-static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
+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 },
@@ -76,7 +48,7 @@ static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
     { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R,  DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
 };
 
-static const uint8_t audio_mode_ch_mask[AMODE_COUNT] = {
+static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = {
     DCA_SPEAKER_LAYOUT_MONO,
     DCA_SPEAKER_LAYOUT_STEREO,
     DCA_SPEAKER_LAYOUT_STEREO,
@@ -109,114 +81,68 @@ static void get_array(GetBitContext *s, int32_t *array, int size, int n)
 // 5.3.1 - Bit stream header
 static int parse_frame_header(DCACoreDecoder *s)
 {
-    int normal_frame, pcmr_index;
-
-    // Frame type
-    normal_frame = get_bits1(&s->gb);
-
-    // 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 >= 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)];
+    DCACoreFrameHeader h = { 0 };
+    int err = ff_dca_parse_core_frame_header(&h, &s->gb);
 
-    // Reserved field
-    skip_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;
 
-    // Embedded dynamic range flag
-    s->drc_present = get_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;
 
-    // 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);
-
-    // 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 == 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;
 }
@@ -1778,12 +1704,13 @@ static int parse_optional_info(DCACoreDecoder *s)
         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
         // sync word aliasing issues.
         switch (s->ext_audio_type) {
-        case EXT_AUDIO_XCH:
+        case DCA_EXT_AUDIO_XCH:
             if (dca->request_channel_layout)
                 break;
 
@@ -1792,15 +1719,15 @@ static int parse_optional_info(DCACoreDecoder *s)
             // 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;
                     }
                 }
@@ -1813,17 +1740,17 @@ static int parse_optional_info(DCACoreDecoder *s)
             }
             break;
 
-        case EXT_AUDIO_X96:
+        case DCA_EXT_AUDIO_X96:
             // 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;
                     }
                 }
@@ -1836,16 +1763,16 @@ static int parse_optional_info(DCACoreDecoder *s)
             }
             break;
 
-        case EXT_AUDIO_XXCH:
+        case DCA_EXT_AUDIO_XXCH:
             if (dca->request_channel_layout)
                 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 +
@@ -1877,8 +1804,8 @@ int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
 
     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)
@@ -1905,7 +1832,6 @@ int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset
 {
     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;
 
@@ -1917,11 +1843,13 @@ int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset
             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;
         }
@@ -1963,8 +1891,8 @@ int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset
                 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;
@@ -2102,7 +2030,7 @@ int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
         int nlfesamples = s->npcmblocks >> 1;
 
         // Check LFF
-        if (s->lfe_present == LFE_FLAG_128) {
+        if (s->lfe_present == DCA_LFE_FLAG_128) {
             av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
             return AVERROR(EINVAL);
         }
@@ -2152,7 +2080,7 @@ static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
 
     // Undo embedded XCH downmix
     if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
-        && s->audio_mode >= AMODE_2F2R) {
+        && s->audio_mode >= DCA_AMODE_2F2R) {
         s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
                                 s->output_samples[DCA_SPEAKER_Rs],
                                 s->output_samples[DCA_SPEAKER_Cs],
@@ -2196,15 +2124,15 @@ static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
 
     if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
         // Front sum/difference decoding
-        if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
-            || s->audio_mode == AMODE_STEREO_SUMDIFF) {
+        if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
+            || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
             s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
                                             s->output_samples[DCA_SPEAKER_R],
                                             nsamples);
         }
 
         // Surround sum/difference decoding
-        if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
+        if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
             s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
                                             s->output_samples[DCA_SPEAKER_Rs],
                                             nsamples);
@@ -2308,7 +2236,7 @@ static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
 
     // Filter LFE channel
     if (s->lfe_present) {
-        int dec_select = (s->lfe_present == LFE_FLAG_128);
+        int dec_select = (s->lfe_present == DCA_LFE_FLAG_128);
         float *samples = output_samples[DCA_SPEAKER_LFE1];
         int nlfesamples = s->npcmblocks >> (dec_select + 1);
 
@@ -2342,7 +2270,7 @@ static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
 
     // Undo embedded XCH downmix
     if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
-        && s->audio_mode >= AMODE_2F2R) {
+        && s->audio_mode >= DCA_AMODE_2F2R) {
         s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
                                          output_samples[DCA_SPEAKER_Cs],
                                          -M_SQRT1_2, nsamples);
@@ -2389,15 +2317,15 @@ static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
 
     if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
         // Front sum/difference decoding
-        if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
-            || s->audio_mode == AMODE_STEREO_SUMDIFF) {
+        if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
+            || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
             s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
                                             output_samples[DCA_SPEAKER_R],
                                             nsamples);
         }
 
         // Surround sum/difference decoding
-        if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
+        if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
             s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
                                             output_samples[DCA_SPEAKER_Rs],
                                             nsamples);
@@ -2424,7 +2352,7 @@ int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
 
     // Handle downmixing to stereo request
     if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
-        && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
+        && s->audio_mode > DCA_AMODE_MONO && s->prim_dmix_embedded
         && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
             s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
         s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
@@ -2457,8 +2385,8 @@ int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
     else
         avctx->bit_rate = 0;
 
-    if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
-                                                s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
+    if (s->audio_mode == DCA_AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
+                                                    s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
         matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
     else
         matrix_encoding = AV_MATRIX_ENCODING_NONE;