]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegaudiodec_template.c
avformat/wavdec: Check for EOF in cues reading
[ffmpeg] / libavcodec / mpegaudiodec_template.c
index 9cce88e263b730981feb49506b94ba32a121b732..0634f70b05e8c55fb18608372973917795338fdf 100644 (file)
@@ -27,6 +27,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/crc.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/libm.h"
 #include "avcodec.h"
@@ -86,8 +87,9 @@ typedef struct MPADecodeContext {
     int err_recognition;
     AVCodecContext* avctx;
     MPADSPContext mpadsp;
-    AVFloatDSPContext *fdsp;
+    void (*butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len);
     AVFrame *frame;
+    uint32_t crc;
 } MPADecodeContext;
 
 #define HEADER_SIZE 4
@@ -106,8 +108,8 @@ static const int huff_vlc_tables_sizes[16] = {
   142,  204,  190,  170,  542,  460,  662,  414
 };
 static VLC huff_quad_vlc[2];
-static VLC_TYPE  huff_quad_vlc_tables[128+16][2];
-static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
+static VLC_TYPE  huff_quad_vlc_tables[64+16][2];
+static const int huff_quad_vlc_tables_sizes[2] = { 64, 16 };
 /* computed from band_size_long */
 static uint16_t band_index_long[9][23];
 #include "mpegaudio_tablegen.h"
@@ -116,9 +118,9 @@ static INTFLOAT is_table[2][16];
 static INTFLOAT is_table_lsf[2][2][16];
 static INTFLOAT csa_table[8][4];
 
-static int16_t division_tab3[1<<6 ];
-static int16_t division_tab5[1<<8 ];
-static int16_t division_tab9[1<<11];
+static int16_t division_tab3[1 << 6 ];
+static int16_t division_tab5[1 << 8 ];
+static int16_t division_tab9[1 << 11];
 
 static int16_t * const division_tabs[4] = {
     division_tab3, division_tab5, NULL, division_tab9
@@ -253,7 +255,7 @@ static inline int l3_unscale(int value, int exponent)
 #endif
     if (e > (SUINT)31)
         return 0;
-    m = (m + ((1U << e)>>1)) >> e;
+    m = (m + ((1U << e) >> 1)) >> e;
 
     return m;
 }
@@ -287,8 +289,6 @@ static av_cold void decode_init_static(void)
                 scale_factor_mult[i][2]);
     }
 
-    RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
-
     /* huffman decode tables */
     offset = 0;
     for (i = 1; i < 16; i++) {
@@ -302,8 +302,8 @@ static av_cold void decode_init_static(void)
         j = 0;
         for (x = 0; x < xsize; x++) {
             for (y = 0; y < xsize; y++) {
-                tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
-                tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
+                tmp_bits [(x << 5) | y | ((x && y) << 4)]= h->bits [j  ];
+                tmp_codes[(x << 5) | y | ((x && y) << 4)]= h->codes[j++];
             }
         }
 
@@ -321,7 +321,7 @@ static av_cold void decode_init_static(void)
     for (i = 0; i < 2; i++) {
         huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
         huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
-        init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
+        init_vlc(&huff_quad_vlc[i], i == 0 ? 6 : 4, 16,
                  mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
                  INIT_VLC_USE_NEW_STATIC);
         offset += huff_quad_vlc_tables_sizes[i];
@@ -343,7 +343,7 @@ static av_cold void decode_init_static(void)
 
     for (i = 0; i < 4; i++) {
         if (ff_mpa_quant_bits[i] < 0) {
-            for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
+            for (j = 0; j < (1 << (-ff_mpa_quant_bits[i] + 1)); j++) {
                 int val1, val2, val3, steps;
                 int val = j;
                 steps   = ff_mpa_quant_steps[i];
@@ -406,18 +406,9 @@ static av_cold void decode_init_static(void)
         csa_table[i][3] = ca - cs;
 #endif
     }
+    RENAME(ff_mpa_synth_init)();
 }
 
-#if USE_FLOATS
-static av_cold int decode_close(AVCodecContext * avctx)
-{
-    MPADecodeContext *s = avctx->priv_data;
-    av_freep(&s->fdsp);
-
-    return 0;
-}
-#endif
-
 static av_cold int decode_init(AVCodecContext * avctx)
 {
     static int initialized_tables = 0;
@@ -431,9 +422,14 @@ static av_cold int decode_init(AVCodecContext * avctx)
     s->avctx = avctx;
 
 #if USE_FLOATS
-    s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
-    if (!s->fdsp)
-        return AVERROR(ENOMEM);
+    {
+        AVFloatDSPContext *fdsp;
+        fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
+        if (!fdsp)
+            return AVERROR(ENOMEM);
+        s->butterflies_float = fdsp->butterflies_float;
+        av_free(fdsp);
+    }
 #endif
 
     ff_mpadsp_init(&s->mpadsp);
@@ -499,12 +495,43 @@ static void imdct12(INTFLOAT *out, SUINTFLOAT *in)
     out[11] = in0 + in5;
 }
 
+static int handle_crc(MPADecodeContext *s, int sec_len)
+{
+    if (s->error_protection && (s->err_recognition & AV_EF_CRCCHECK)) {
+        const uint8_t *buf = s->gb.buffer - HEADER_SIZE;
+        int sec_byte_len  = sec_len >> 3;
+        int sec_rem_bits  = sec_len & 7;
+        const AVCRC *crc_tab = av_crc_get_table(AV_CRC_16_ANSI);
+        uint8_t tmp_buf[4];
+        uint32_t crc_val = av_crc(crc_tab, UINT16_MAX, &buf[2], 2);
+        crc_val = av_crc(crc_tab, crc_val, &buf[6], sec_byte_len);
+
+        AV_WB32(tmp_buf,
+                ((buf[6 + sec_byte_len] & (0xFF00 >> sec_rem_bits)) << 24) +
+                ((s->crc << 16) >> sec_rem_bits));
+
+        crc_val = av_crc(crc_tab, crc_val, tmp_buf, 3);
+
+        if (crc_val) {
+            av_log(s->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc_val);
+            if (s->err_recognition & AV_EF_EXPLODE)
+                return AVERROR_INVALIDDATA;
+        }
+    }
+    return 0;
+}
+
 /* return the number of decoded frames */
 static int mp_decode_layer1(MPADecodeContext *s)
 {
     int bound, i, v, n, ch, j, mant;
     uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
     uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
+    int ret;
+
+    ret = handle_crc(s, (s->nb_channels == 1) ? 8*16  : 8*32);
+    if (ret < 0)
+        return ret;
 
     if (s->mode == MPA_JSTEREO)
         bound = (s->mode_ext + 1) * 4;
@@ -574,6 +601,7 @@ static int mp_decode_layer2(MPADecodeContext *s)
     unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
     unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
     int scale, qindex, bits, steps, k, l, m, b;
+    int ret;
 
     /* select decoding table */
     table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
@@ -616,6 +644,10 @@ static int mp_decode_layer2(MPADecodeContext *s)
         }
     }
 
+    ret = handle_crc(s, get_bits_count(&s->gb) - 16);
+    if (ret < 0)
+        return ret;
+
     /* scale factors */
     for (i = 0; i < sblimit; i++) {
         for (ch = 0; ch < s->nb_channels; ch++) {
@@ -697,7 +729,7 @@ static int mp_decode_layer2(MPADecodeContext *s)
                     int mant, scale0, scale1;
                     scale0 = scale_factors[0][i][k];
                     scale1 = scale_factors[1][i][k];
-                    qindex = alloc_table[j+b];
+                    qindex = alloc_table[j + b];
                     bits = ff_mpa_quant_bits[qindex];
                     if (bits < 0) {
                         /* 3 values at the same time */
@@ -891,8 +923,8 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
             y = get_vlc2(&s->gb, vlc->table, 7, 3);
 
             if (!y) {
-                g->sb_hybrid[s_index  ] =
-                g->sb_hybrid[s_index+1] = 0;
+                g->sb_hybrid[s_index    ] =
+                g->sb_hybrid[s_index + 1] = 0;
                 s_index += 2;
                 continue;
             }
@@ -920,7 +952,7 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
                     v  = l3_unscale(y, exponent);
                     if (get_bits1(&s->gb))
                         v = -v;
-                    g->sb_hybrid[s_index+1] = v;
+                    g->sb_hybrid[s_index + 1] = v;
                 }
             } else {
                 x = y >> 5;
@@ -966,10 +998,10 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
 
         code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
         ff_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
-        g->sb_hybrid[s_index+0] =
-        g->sb_hybrid[s_index+1] =
-        g->sb_hybrid[s_index+2] =
-        g->sb_hybrid[s_index+3] = 0;
+        g->sb_hybrid[s_index + 0] =
+        g->sb_hybrid[s_index + 1] =
+        g->sb_hybrid[s_index + 2] =
+        g->sb_hybrid[s_index + 3] = 0;
         while (code) {
             static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
             int v;
@@ -1150,7 +1182,7 @@ found2:
         /* NOTE: the 1/sqrt(2) normalization factor is included in the
            global gain */
 #if USE_FLOATS
-       s->fdsp->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
+       s->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
 #else
         tab0 = g0->sb_hybrid;
         tab1 = g1->sb_hybrid;
@@ -1309,13 +1341,16 @@ static int mp_decode_layer3(MPADecodeContext *s)
     int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
     GranuleDef *g;
     int16_t exponents[576]; //FIXME try INTFLOAT
+    int ret;
 
     /* read side info */
     if (s->lsf) {
+        ret = handle_crc(s, ((s->nb_channels == 1) ? 8*9  : 8*17));
         main_data_begin = get_bits(&s->gb, 8);
         skip_bits(&s->gb, s->nb_channels);
         nb_granules = 1;
     } else {
+        ret = handle_crc(s, ((s->nb_channels == 1) ? 8*17 : 8*32));
         main_data_begin = get_bits(&s->gb, 9);
         if (s->nb_channels == 2)
             skip_bits(&s->gb, 3);
@@ -1327,6 +1362,8 @@ static int mp_decode_layer3(MPADecodeContext *s)
             s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
         }
     }
+    if (ret < 0)
+        return ret;
 
     for (gr = 0; gr < nb_granules; gr++) {
         for (ch = 0; ch < s->nb_channels; ch++) {
@@ -1390,7 +1427,7 @@ static int mp_decode_layer3(MPADecodeContext *s)
 
     if (!s->adu_mode) {
         int skip;
-        const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
+        const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb) >> 3);
         s->extrasize = av_clip((get_bits_left(&s->gb) >> 3) - s->extrasize, 0,
                                FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
         av_assert1((get_bits_count(&s->gb) & 7) == 0);
@@ -1564,10 +1601,8 @@ static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
     OUT_INT *samples_ptr;
 
     init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
-
-    /* skip error protection field */
     if (s->error_protection)
-        skip_bits(&s->gb, 16);
+        s->crc = get_bits(&s->gb, 16);
 
     switch(s->layer) {
     case 1:
@@ -1588,7 +1623,7 @@ static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
             align_get_bits(&s->gb);
             i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
             if (i >= 0 && i <= BACKSTEP_SIZE) {
-                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
+                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb) >> 3), i);
                 s->last_buf_size=i;
             } else
                 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
@@ -1665,7 +1700,7 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
         return AVERROR_INVALIDDATA;
 
     header = AV_RB32(buf);
-    if (header>>8 == AV_RB32("TAG")>>8) {
+    if (header >> 8 == AV_RB32("TAG") >> 8) {
         av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
         return buf_size + skipped;
     }
@@ -1830,9 +1865,6 @@ static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
     MP3On4DecodeContext *s = avctx->priv_data;
     int i;
 
-    if (s->mp3decctx[0])
-        av_freep(&s->mp3decctx[0]->fdsp);
-
     for (i = 0; i < s->frames; i++)
         av_freep(&s->mp3decctx[i]);
 
@@ -1844,15 +1876,15 @@ static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
 {
     MP3On4DecodeContext *s = avctx->priv_data;
     MPEG4AudioConfig cfg;
-    int i;
+    int i, ret;
 
     if ((avctx->extradata_size < 2) || !avctx->extradata) {
         av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
         return AVERROR_INVALIDDATA;
     }
 
-    avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
-                                 avctx->extradata_size * 8, 1);
+    avpriv_mpeg4audio_get_config2(&cfg, avctx->extradata,
+                                  avctx->extradata_size, 1, avctx);
     if (!cfg.chan_config || cfg.chan_config > 7) {
         av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
         return AVERROR_INVALIDDATA;
@@ -1875,12 +1907,14 @@ static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
     // Allocate zeroed memory for the first decoder context
     s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
     if (!s->mp3decctx[0])
-        goto alloc_fail;
+        return AVERROR(ENOMEM);
     // Put decoder context in place to make init_decode() happy
     avctx->priv_data = s->mp3decctx[0];
-    decode_init(avctx);
+    ret = decode_init(avctx);
     // Restore mp3on4 context pointer
     avctx->priv_data = s;
+    if (ret < 0)
+        return ret;
     s->mp3decctx[0]->adu_mode = 1; // Set adu mode
 
     /* Create a separate codec/context for each frame (first is already ok).
@@ -1889,17 +1923,14 @@ static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
     for (i = 1; i < s->frames; i++) {
         s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
         if (!s->mp3decctx[i])
-            goto alloc_fail;
+            return AVERROR(ENOMEM);
         s->mp3decctx[i]->adu_mode = 1;
         s->mp3decctx[i]->avctx = avctx;
         s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
-        s->mp3decctx[i]->fdsp = s->mp3decctx[0]->fdsp;
+        s->mp3decctx[i]->butterflies_float = s->mp3decctx[0]->butterflies_float;
     }
 
     return 0;
-alloc_fail:
-    decode_close_mp3on4(avctx);
-    return AVERROR(ENOMEM);
 }