]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpc8.c
avformat: Constify all muxer/demuxers
[ffmpeg] / libavcodec / mpc8.c
index c599d34326db1deb2e16d331e0c7117183b030c8..ae07093338384710f617eeedbaff4eb6957de0b0 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/lfg.h"
+#include "libavutil/thread.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "internal.h"
@@ -86,33 +87,62 @@ static int mpc8_get_mask(GetBitContext *gb, int size, int t)
     return mask;
 }
 
-static const uint16_t vlc_offsets[13] = {
-    0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
-};
-
-static av_cold void build_vlc(VLC *vlc, int nb_bits,
+static av_cold void build_vlc(VLC *vlc, unsigned *buf_offset,
                               const uint8_t codes_counts[16],
-                              const uint8_t syms[], int offset)
+                              const uint8_t **syms, int offset)
 {
+    static VLC_TYPE vlc_buf[9296][2];
     uint8_t len[MPC8_MAX_VLC_SIZE];
     unsigned num = 0;
 
+    vlc->table           = &vlc_buf[*buf_offset];
+    vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
+
     for (int i = 16; i > 0; i--)
         for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
             len[num] = i;
 
-    ff_init_vlc_from_lengths(vlc, nb_bits, num, len, 1,
-                             syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC, NULL);
+    ff_init_vlc_from_lengths(vlc, FFMIN(len[0], 9), num, len, 1,
+                             *syms, 1, 1, offset, INIT_VLC_STATIC_OVERLONG, NULL);
+    *buf_offset += vlc->table_size;
+    *syms       += num;
+}
+
+static av_cold void mpc8_init_static(void)
+{
+    const uint8_t *q_syms   = mpc8_q_syms,  *bands_syms = mpc8_bands_syms;
+    const uint8_t *res_syms = mpc8_res_syms, *scfi_syms = mpc8_scfi_syms;
+    const uint8_t *dscf_syms = mpc8_dscf_syms;
+    unsigned offset = 0;
+
+    build_vlc(&band_vlc, &offset, mpc8_bands_len_counts, &bands_syms, 0);
+
+    build_vlc(&q1_vlc,   &offset, mpc8_q1_len_counts,   &q_syms, 0);
+    build_vlc(&q9up_vlc, &offset, mpc8_q9up_len_counts, &q_syms, 0);
+
+    for (int i = 0; i < 2; i++){
+        build_vlc(&scfi_vlc[i], &offset, mpc8_scfi_len_counts[i], &scfi_syms, 0);
+
+        build_vlc(&dscf_vlc[i], &offset, mpc8_dscf_len_counts[i], &dscf_syms, 0);
+
+        build_vlc(&res_vlc[i],  &offset, mpc8_res_len_counts[i],  &res_syms,  0);
+
+        build_vlc(&q2_vlc[i], &offset, mpc8_q2_len_counts[i], &q_syms, 0);
+        build_vlc(&q3_vlc[i], &offset, mpc8_q34_len_counts[i],
+                  &q_syms, -48 - 16 * i);
+        for (int j = 0; j < 4; j++)
+            build_vlc(&quant_vlc[j][i], &offset, mpc8_q5_8_len_counts[i][j],
+                      &q_syms, -((8 << j) - 1));
+    }
+    ff_mpa_synth_init_fixed();
 }
 
 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
 {
-    int i;
+    static AVOnce init_static_once = AV_ONCE_INIT;
     MPCContext *c = avctx->priv_data;
     GetBitContext gb;
-    static int vlc_initialized = 0;
     int channels;
-    static VLC_TYPE codes_table[5708][2];
 
     if(avctx->extradata_size < 2){
         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
@@ -142,71 +172,7 @@ static av_cold int mpc8_decode_init(AVCodecContext * avctx)
     avctx->channel_layout = (channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
     avctx->channels = channels;
 
-    if(vlc_initialized) return 0;
-    av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
-
-#define INIT_VLC(vlc, bits, len_counts, symbols, offset, static_size) \
-    do {                                                                   \
-        static VLC_TYPE table[static_size][2];                             \
-        (vlc)->table           = table;                                    \
-        (vlc)->table_allocated = static_size;                              \
-        build_vlc(vlc, bits, len_counts, symbols, offset);                 \
-    } while (0)
-
-
-    INIT_VLC(&band_vlc, MPC8_BANDS_BITS,
-             mpc8_bands_len_counts, mpc8_bands_syms, 0, 542);
-
-    INIT_VLC(&q1_vlc, MPC8_Q1_BITS,
-             mpc8_q1_len_counts, mpc8_q1_syms, 0, 520);
-    INIT_VLC(&q9up_vlc, MPC8_Q9UP_BITS,
-             mpc8_q9up_len_counts, mpc8_q9up_syms, 0, 524);
-
-    INIT_VLC(&scfi_vlc[0], MPC8_SCFI0_BITS,
-             mpc8_scfi_len_counts[0], mpc8_scfi0_syms, 0, 1 << MPC8_SCFI0_BITS);
-    INIT_VLC(&scfi_vlc[1], MPC8_SCFI1_BITS,
-             mpc8_scfi_len_counts[1], mpc8_scfi1_syms, 0, 1 << MPC8_SCFI1_BITS);
-
-    INIT_VLC(&dscf_vlc[0], MPC8_DSCF0_BITS,
-             mpc8_dscf_len_counts[0], mpc8_dscf0_syms, 0, 560);
-    INIT_VLC(&dscf_vlc[1], MPC8_DSCF1_BITS,
-             mpc8_dscf_len_counts[1], mpc8_dscf1_syms, 0, 598);
-
-    INIT_VLC(&q3_vlc[0], MPC8_Q3_BITS,
-             mpc8_q3_len_counts, mpc8_q3_syms, MPC8_Q3_OFFSET, 512);
-    INIT_VLC(&q3_vlc[1], MPC8_Q4_BITS,
-             mpc8_q4_len_counts, mpc8_q4_syms, MPC8_Q4_OFFSET, 516);
-
-    for(i = 0; i < 2; i++){
-        res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
-        res_vlc[i].table_allocated = vlc_offsets[1+i] - vlc_offsets[0+i];
-        build_vlc(&res_vlc[i], MPC8_RES_BITS,
-                  mpc8_res_len_counts[i], mpc8_res_syms[i], 0);
-
-        q2_vlc[i].table = &codes_table[vlc_offsets[2+i]];
-        q2_vlc[i].table_allocated = vlc_offsets[3+i] - vlc_offsets[2+i];
-        build_vlc(&q2_vlc[i], MPC8_Q2_BITS,
-                  mpc8_q2_len_counts[i], mpc8_q2_syms[i], 0);
-
-        quant_vlc[0][i].table = &codes_table[vlc_offsets[4+i]];
-        quant_vlc[0][i].table_allocated = vlc_offsets[5+i] - vlc_offsets[4+i];
-        build_vlc(&quant_vlc[0][i], MPC8_Q5_BITS,
-                  mpc8_q5_len_counts[i], mpc8_q5_syms[i], MPC8_Q5_OFFSET);
-        quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
-        quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
-        build_vlc(&quant_vlc[1][i], MPC8_Q6_BITS,
-                  mpc8_q6_len_counts[i], mpc8_q6_syms[i], MPC8_Q6_OFFSET);
-        quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
-        quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
-        build_vlc(&quant_vlc[2][i], MPC8_Q7_BITS,
-                  mpc8_q7_len_counts[i], mpc8_q7_syms[i], MPC8_Q7_OFFSET);
-        quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
-        quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - vlc_offsets[10+i];
-        build_vlc(&quant_vlc[3][i], MPC8_Q8_BITS,
-                  mpc8_q8_len_counts[i], mpc8_q8_syms[i], MPC8_Q8_OFFSET);
-    }
-    vlc_initialized = 1;
-    ff_mpa_synth_init_fixed();
+    ff_thread_once(&init_static_once, mpc8_init_static);
 
     return 0;
 }
@@ -417,7 +383,7 @@ static av_cold void mpc8_decode_flush(AVCodecContext *avctx)
     c->cur_frame = 0;
 }
 
-AVCodec ff_mpc8_decoder = {
+const AVCodec ff_mpc8_decoder = {
     .name           = "mpc8",
     .long_name      = NULL_IF_CONFIG_SMALL("Musepack SV8"),
     .type           = AVMEDIA_TYPE_AUDIO,
@@ -426,7 +392,8 @@ AVCodec ff_mpc8_decoder = {
     .init           = mpc8_decode_init,
     .decode         = mpc8_decode_frame,
     .flush          = mpc8_decode_flush,
-    .capabilities   = AV_CODEC_CAP_DR1,
+    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
                                                       AV_SAMPLE_FMT_NONE },
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
 };