]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegaudiodec.c
Fix infinite loop at EOF.
[ffmpeg] / libavcodec / mpegaudiodec.c
index 7c4fce5654e0908a65892b7b6924eaa2d562cf64..0064fa284231122c0529e541b5dfb7d7f1f7db16 100644 (file)
 
 #define HEADER_SIZE 4
 
-/**
- * Context for MP3On4 decoder
- */
-typedef struct MP3On4DecodeContext {
-    int frames;   ///< number of mp3 frames per block (number of mp3 decoder instances)
-    int chan_cfg; ///< channel config number
-    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
-} MP3On4DecodeContext;
-
 /* layer 3 "granule" */
 typedef struct GranuleDef {
     uint8_t scfsi;
@@ -775,6 +766,8 @@ static inline int round_sample(int *sum)
 /* signed 16x16 -> 32 multiply */
 #define MULS(ra, rb) MUL16(ra, rb)
 
+#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
+
 #else
 
 static inline int round_sample(int64_t *sum)
@@ -790,47 +783,49 @@ static inline int round_sample(int64_t *sum)
 }
 
 #   define MULS(ra, rb) MUL64(ra, rb)
+#   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
+#   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
 #endif
 
-#define SUM8(sum, op, w, p) \
-{                                               \
-    sum op MULS((w)[0 * 64], p[0 * 64]);\
-    sum op MULS((w)[1 * 64], p[1 * 64]);\
-    sum op MULS((w)[2 * 64], p[2 * 64]);\
-    sum op MULS((w)[3 * 64], p[3 * 64]);\
-    sum op MULS((w)[4 * 64], p[4 * 64]);\
-    sum op MULS((w)[5 * 64], p[5 * 64]);\
-    sum op MULS((w)[6 * 64], p[6 * 64]);\
-    sum op MULS((w)[7 * 64], p[7 * 64]);\
+#define SUM8(op, sum, w, p)               \
+{                                         \
+    op(sum, (w)[0 * 64], p[0 * 64]);      \
+    op(sum, (w)[1 * 64], p[1 * 64]);      \
+    op(sum, (w)[2 * 64], p[2 * 64]);      \
+    op(sum, (w)[3 * 64], p[3 * 64]);      \
+    op(sum, (w)[4 * 64], p[4 * 64]);      \
+    op(sum, (w)[5 * 64], p[5 * 64]);      \
+    op(sum, (w)[6 * 64], p[6 * 64]);      \
+    op(sum, (w)[7 * 64], p[7 * 64]);      \
 }
 
 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
 {                                               \
     int tmp;\
     tmp = p[0 * 64];\
-    sum1 op1 MULS((w1)[0 * 64], tmp);\
-    sum2 op2 MULS((w2)[0 * 64], tmp);\
+    op1(sum1, (w1)[0 * 64], tmp);\
+    op2(sum2, (w2)[0 * 64], tmp);\
     tmp = p[1 * 64];\
-    sum1 op1 MULS((w1)[1 * 64], tmp);\
-    sum2 op2 MULS((w2)[1 * 64], tmp);\
+    op1(sum1, (w1)[1 * 64], tmp);\
+    op2(sum2, (w2)[1 * 64], tmp);\
     tmp = p[2 * 64];\
-    sum1 op1 MULS((w1)[2 * 64], tmp);\
-    sum2 op2 MULS((w2)[2 * 64], tmp);\
+    op1(sum1, (w1)[2 * 64], tmp);\
+    op2(sum2, (w2)[2 * 64], tmp);\
     tmp = p[3 * 64];\
-    sum1 op1 MULS((w1)[3 * 64], tmp);\
-    sum2 op2 MULS((w2)[3 * 64], tmp);\
+    op1(sum1, (w1)[3 * 64], tmp);\
+    op2(sum2, (w2)[3 * 64], tmp);\
     tmp = p[4 * 64];\
-    sum1 op1 MULS((w1)[4 * 64], tmp);\
-    sum2 op2 MULS((w2)[4 * 64], tmp);\
+    op1(sum1, (w1)[4 * 64], tmp);\
+    op2(sum2, (w2)[4 * 64], tmp);\
     tmp = p[5 * 64];\
-    sum1 op1 MULS((w1)[5 * 64], tmp);\
-    sum2 op2 MULS((w2)[5 * 64], tmp);\
+    op1(sum1, (w1)[5 * 64], tmp);\
+    op2(sum2, (w2)[5 * 64], tmp);\
     tmp = p[6 * 64];\
-    sum1 op1 MULS((w1)[6 * 64], tmp);\
-    sum2 op2 MULS((w2)[6 * 64], tmp);\
+    op1(sum1, (w1)[6 * 64], tmp);\
+    op2(sum2, (w2)[6 * 64], tmp);\
     tmp = p[7 * 64];\
-    sum1 op1 MULS((w1)[7 * 64], tmp);\
-    sum2 op2 MULS((w2)[7 * 64], tmp);\
+    op1(sum1, (w1)[7 * 64], tmp);\
+    op2(sum2, (w2)[7 * 64], tmp);\
 }
 
 void ff_mpa_synth_init(MPA_INT *window)
@@ -894,9 +889,9 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
 
     sum = *dither_state;
     p = synth_buf + 16;
-    SUM8(sum, +=, w, p);
+    SUM8(MACS, sum, w, p);
     p = synth_buf + 48;
-    SUM8(sum, -=, w + 32, p);
+    SUM8(MLSS, sum, w + 32, p);
     *samples = round_sample(&sum);
     samples += incr;
     w++;
@@ -906,9 +901,9 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
     for(j=1;j<16;j++) {
         sum2 = 0;
         p = synth_buf + 16 + j;
-        SUM8P2(sum, +=, sum2, -=, w, w2, p);
+        SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
         p = synth_buf + 48 - j;
-        SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
+        SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
 
         *samples = round_sample(&sum);
         samples += incr;
@@ -920,7 +915,7 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
     }
 
     p = synth_buf + 32;
-    SUM8(sum, -=, w + 32, p);
+    SUM8(MLSS, sum, w + 32, p);
     *samples = round_sample(&sum);
     *dither_state= sum;
 
@@ -2476,12 +2471,22 @@ static int decode_frame_adu(AVCodecContext * avctx,
 
 #ifdef CONFIG_MP3ON4_DECODER
 
+/**
+ * Context for MP3On4 decoder
+ */
+typedef struct MP3On4DecodeContext {
+    int frames;   ///< number of mp3 frames per block (number of mp3 decoder instances)
+    int syncword; ///< syncword patch
+    const uint8_t *coff; ///< channels offsets in output buffer
+    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
+} MP3On4DecodeContext;
+
 #include "mpeg4audio.h"
 
 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
-static const uint8_t mp3Frames[16] = {0,1,1,2,3,3,4,5,2};   /* number of mp3 decoder instances */
+static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};   /* number of mp3 decoder instances */
 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
-static const uint8_t chan_offset[9][5] = {
+static const uint8_t chan_offset[8][5] = {
     {0},
     {0},            // C
     {0},            // FLR
@@ -2490,7 +2495,6 @@ static const uint8_t chan_offset[9][5] = {
     {4,0,2},        // C FLR BLRS
     {4,0,2,5},      // C FLR BLRS LFE
     {4,0,2,6,5},    // C FLR BLRS BLR LFE
-    {0,2}           // FLR BLRS
 };
 
 
@@ -2510,9 +2514,14 @@ static int decode_init_mp3on4(AVCodecContext * avctx)
         av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
         return -1;
     }
-    s->chan_cfg = cfg.chan_config;
-    s->frames = mp3Frames[s->chan_cfg];
-    avctx->channels = ff_mpeg4audio_channels[s->chan_cfg];
+    s->frames = mp3Frames[cfg.chan_config];
+    s->coff = chan_offset[cfg.chan_config];
+    avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
+
+    if (cfg.sample_rate < 16000)
+        s->syncword = 0xffe00000;
+    else
+        s->syncword = 0xfff00000;
 
     /* Init the first mp3 decoder in standard way, so that all tables get builded
      * We replace avctx->priv_data with the context of the first decoder so that
@@ -2561,17 +2570,12 @@ static int decode_frame_mp3on4(AVCodecContext * avctx,
 {
     MP3On4DecodeContext *s = avctx->priv_data;
     MPADecodeContext *m;
-    int len, out_size = 0;
+    int fsize, len = buf_size, out_size = 0;
     uint32_t header;
     OUT_INT *out_samples = data;
     OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
     OUT_INT *outptr, *bp;
-    int fsize;
-    int fr, i, j, n;
-    int off = avctx->channels;
-    const uint8_t *coff = chan_offset[s->chan_cfg];
-
-    len = buf_size;
+    int fr, j, n;
 
     *data_size = 0;
     // Discard too short frames
@@ -2581,49 +2585,46 @@ static int decode_frame_mp3on4(AVCodecContext * avctx,
     // If only one decoder interleave is not needed
     outptr = s->frames == 1 ? out_samples : decoded_buf;
 
+    avctx->bit_rate = 0;
+
     for (fr = 0; fr < s->frames; fr++) {
         fsize = AV_RB16(buf) >> 4;
         fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
         m = s->mp3decctx[fr];
         assert (m != NULL);
 
-        // Get header
-        header = AV_RB32(buf) | 0xfff00000;
+        header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
 
-        if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
-            *data_size = 0;
-            return buf_size;
-        }
+        if (ff_mpa_check_header(header) < 0) // Bad header, discard block
+            break;
 
         ff_mpegaudio_decode_header(m, header);
-        out_size += mp_decode_frame(m, decoded_buf, buf, fsize);
+        out_size += mp_decode_frame(m, outptr, buf, fsize);
         buf += fsize;
         len -= fsize;
 
         if(s->frames > 1) {
             n = m->avctx->frame_size*m->nb_channels;
             /* interleave output data */
-            bp = out_samples + coff[fr];
+            bp = out_samples + s->coff[fr];
             if(m->nb_channels == 1) {
                 for(j = 0; j < n; j++) {
                     *bp = decoded_buf[j];
-                    bp += off;
+                    bp += avctx->channels;
                 }
             } else {
                 for(j = 0; j < n; j++) {
                     bp[0] = decoded_buf[j++];
                     bp[1] = decoded_buf[j];
-                    bp += off;
+                    bp += avctx->channels;
                 }
             }
         }
+        avctx->bit_rate += m->bit_rate;
     }
 
     /* update codec info */
     avctx->sample_rate = s->mp3decctx[0]->sample_rate;
-    avctx->bit_rate = 0;
-    for (i = 0; i < s->frames; i++)
-        avctx->bit_rate += s->mp3decctx[i]->bit_rate;
 
     *data_size = out_size;
     return buf_size;
@@ -2643,6 +2644,7 @@ AVCodec mp2_decoder =
     decode_frame,
     CODEC_CAP_PARSE_ONLY,
     .flush= flush,
+    .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
 };
 #endif
 #ifdef CONFIG_MP3_DECODER
@@ -2658,6 +2660,7 @@ AVCodec mp3_decoder =
     decode_frame,
     CODEC_CAP_PARSE_ONLY,
     .flush= flush,
+    .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
 };
 #endif
 #ifdef CONFIG_MP3ADU_DECODER
@@ -2673,6 +2676,7 @@ AVCodec mp3adu_decoder =
     decode_frame_adu,
     CODEC_CAP_PARSE_ONLY,
     .flush= flush,
+    .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
 };
 #endif
 #ifdef CONFIG_MP3ON4_DECODER
@@ -2687,5 +2691,6 @@ AVCodec mp3on4_decoder =
     decode_close_mp3on4,
     decode_frame_mp3on4,
     .flush= flush,
+    .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
 };
 #endif