]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mp3dec.c
http: Reduce scope of a variable in parse_content_encoding()
[ffmpeg] / libavformat / mp3dec.c
index 9da9aa8b1628876bc0b53400f2d8d50bb82e8303..14d8254f4c15463c4e63b557c0f68928f3b88593 100644 (file)
 
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/crc.h"
 #include "libavutil/dict.h"
 #include "libavutil/mathematics.h"
 #include "avformat.h"
 #include "internal.h"
+#include "avio_internal.h"
 #include "id3v2.h"
 #include "id3v1.h"
+#include "replaygain.h"
+
 #include "libavcodec/mpegaudiodecheader.h"
 
 #define XING_FLAG_FRAMES 0x01
@@ -37,6 +41,9 @@
 
 typedef struct MP3DecContext {
     int xing_toc;
+    unsigned frames; /* Total number of frames in file */
+    unsigned size;   /* Total number of bytes in the stream */
+    int is_cbr;
 } MP3DecContext;
 
 /* mp3 read */
@@ -73,7 +80,7 @@ static int mp3_read_probe(AVProbeData *p)
     }
     // keep this in sync with ac3 probe, both need to avoid
     // issues with MPEG-files!
-    if (first_frames >= 4) return AVPROBE_SCORE_MAX / 2 + 1;
+    if (first_frames >= 4) return AVPROBE_SCORE_EXTENSION + 1;
 
     if (max_frames) {
         int pes = 0, i;
@@ -94,8 +101,8 @@ static int mp3_read_probe(AVProbeData *p)
         if (pes)
             max_frames = (max_frames + pes - 1) / pes;
     }
-    if      (max_frames >  500) return AVPROBE_SCORE_MAX / 2;
-    else if (max_frames >= 4)   return AVPROBE_SCORE_MAX / 4;
+    if      (max_frames >  500) return AVPROBE_SCORE_EXTENSION;
+    else if (max_frames >= 4)   return AVPROBE_SCORE_EXTENSION / 2;
     else if (max_frames >= 1)   return 1;
     else                        return 0;
 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
@@ -123,18 +130,141 @@ static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration
     mp3->xing_toc = 1;
 }
 
+static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st,
+                               MPADecodeHeader *c, uint32_t spf)
+{
+#define LAST_BITS(k, n) ((k) & ((1 << (n)) - 1))
+#define MIDDLE_BITS(k, m, n) LAST_BITS((k) >> (m), ((n) - (m)))
+
+    uint16_t crc;
+    uint32_t v;
+
+    char version[10];
+
+    uint32_t peak   = 0;
+    int32_t  r_gain = INT32_MIN, a_gain = INT32_MIN;
+
+    MP3DecContext *mp3 = s->priv_data;
+    const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
+
+    /* Check for Xing / Info tag */
+    avio_skip(s->pb, xing_offtbl[c->lsf == 1][c->nb_channels == 1]);
+    v = avio_rb32(s->pb);
+    mp3->is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
+    if (v != MKBETAG('X', 'i', 'n', 'g') && !mp3->is_cbr)
+        return;
+
+    v = avio_rb32(s->pb);
+    if (v & XING_FLAG_FRAMES)
+        mp3->frames = avio_rb32(s->pb);
+    if (v & XING_FLAG_SIZE)
+        mp3->size = avio_rb32(s->pb);
+    if (v & XING_FLAG_TOC && mp3->frames)
+        read_xing_toc(s, mp3->size, av_rescale_q(mp3->frames,
+                                       (AVRational){spf, c->sample_rate},
+                                       st->time_base));
+
+    /* VBR quality */
+    avio_rb32(s->pb);
+
+    /* Encoder short version string */
+    memset(version, 0, sizeof(version));
+    avio_read(s->pb, version, 9);
+
+    /* Info Tag revision + VBR method */
+    avio_r8(s->pb);
+
+    /* Lowpass filter value */
+    avio_r8(s->pb);
+
+    /* ReplayGain peak */
+    v    = avio_rb32(s->pb);
+    peak = av_rescale(v, 100000, 1 << 23);
+
+    /* Radio ReplayGain */
+    v = avio_rb16(s->pb);
+
+    if (MIDDLE_BITS(v, 13, 15) == 1) {
+        r_gain = MIDDLE_BITS(v, 0, 8) * 10000;
+
+        if (v & (1 << 9))
+            r_gain *= -1;
+    }
+
+    /* Audiophile ReplayGain */
+    v = avio_rb16(s->pb);
+
+    if (MIDDLE_BITS(v, 13, 15) == 2) {
+        a_gain = MIDDLE_BITS(v, 0, 8) * 10000;
+
+        if (v & (1 << 9))
+            a_gain *= -1;
+    }
+
+    /* Encoding flags + ATH Type */
+    avio_r8(s->pb);
+
+    /* if ABR {specified bitrate} else {minimal bitrate} */
+    avio_r8(s->pb);
+
+    /* Encoder delays */
+    avio_rb24(s->pb);
+
+    /* Misc */
+    avio_r8(s->pb);
+
+    /* MP3 gain */
+    avio_r8(s->pb);
+
+    /* Preset and surround info */
+    avio_rb16(s->pb);
+
+    /* Music length */
+    avio_rb32(s->pb);
+
+    /* Music CRC */
+    avio_rb16(s->pb);
+
+    /* Info Tag CRC */
+    crc = ffio_get_checksum(s->pb);
+    v   = avio_rb16(s->pb);
+
+    if (v == crc) {
+        ff_replaygain_export_raw(st, r_gain, peak, a_gain, 0);
+        av_dict_set(&st->metadata, "encoder", version, 0);
+    }
+}
+
+static void mp3_parse_vbri_tag(AVFormatContext *s, AVStream *st, int64_t base)
+{
+    uint32_t v;
+    MP3DecContext *mp3 = s->priv_data;
+
+    /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
+    avio_seek(s->pb, base + 4 + 32, SEEK_SET);
+    v = avio_rb32(s->pb);
+    if (v == MKBETAG('V', 'B', 'R', 'I')) {
+        /* Check tag version */
+        if (avio_rb16(s->pb) == 1) {
+            /* skip delay and quality */
+            avio_skip(s->pb, 4);
+            mp3->size = avio_rb32(s->pb);
+            mp3->frames = avio_rb32(s->pb);
+        }
+    }
+}
+
 /**
  * Try to find Xing/Info/VBRI tags and compute duration from info therein
  */
 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
 {
     uint32_t v, spf;
-    unsigned frames = 0; /* Total number of frames in file */
-    unsigned size = 0; /* Total number of bytes in the stream */
-    const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
     MPADecodeHeader c;
     int vbrtag_size = 0;
-    int is_cbr;
+    MP3DecContext *mp3 = s->priv_data;
+
+    ffio_init_checksum(s->pb, ff_crcA001_update, 0);
 
     v = avio_rb32(s->pb);
     if(ff_mpa_check_header(v) < 0)
@@ -147,45 +277,23 @@ static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
 
     spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
 
-    /* Check for Xing / Info tag */
-    avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
-    v = avio_rb32(s->pb);
-    is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
-    if (v == MKBETAG('X', 'i', 'n', 'g') || is_cbr) {
-        v = avio_rb32(s->pb);
-        if(v & XING_FLAG_FRAMES)
-            frames = avio_rb32(s->pb);
-        if(v & XING_FLAG_SIZE)
-            size = avio_rb32(s->pb);
-        if (v & XING_FLAG_TOC && frames)
-            read_xing_toc(s, size, av_rescale_q(frames, (AVRational){spf, c.sample_rate},
-                                    st->time_base));
-    }
+    mp3->frames = 0;
+    mp3->size   = 0;
 
-    /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
-    avio_seek(s->pb, base + 4 + 32, SEEK_SET);
-    v = avio_rb32(s->pb);
-    if(v == MKBETAG('V', 'B', 'R', 'I')) {
-        /* Check tag version */
-        if(avio_rb16(s->pb) == 1) {
-            /* skip delay and quality */
-            avio_skip(s->pb, 4);
-            size = avio_rb32(s->pb);
-            frames = avio_rb32(s->pb);
-        }
-    }
+    mp3_parse_info_tag(s, st, &c, spf);
+    mp3_parse_vbri_tag(s, st, base);
 
-    if(!frames && !size)
+    if (!mp3->frames && !mp3->size)
         return -1;
 
     /* Skip the vbr tag frame */
     avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
 
-    if(frames)
-        st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
+    if (mp3->frames)
+        st->duration = av_rescale_q(mp3->frames, (AVRational){spf, c.sample_rate},
                                     st->time_base);
-    if (size && frames && !is_cbr)
-        st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
+    if (mp3->size && mp3->frames && !mp3->is_cbr)
+        st->codec->bit_rate = av_rescale(mp3->size, 8 * c.sample_rate, mp3->frames * (int64_t)spf);
 
     return 0;
 }
@@ -194,6 +302,7 @@ static int mp3_read_header(AVFormatContext *s)
 {
     AVStream *st;
     int64_t off;
+    int ret;
 
     st = avformat_new_stream(s, NULL);
     if (!st)
@@ -215,6 +324,10 @@ static int mp3_read_header(AVFormatContext *s)
     if (mp3_parse_vbr_tags(s, st, off) < 0)
         avio_seek(s->pb, off, SEEK_SET);
 
+    ret = ff_replaygain_export(st, s->metadata);
+    if (ret < 0)
+        return ret;
+
     /* the parameters will be extracted from the compressed bitstream */
     return 0;
 }
@@ -282,5 +395,5 @@ AVInputFormat ff_mp3_demuxer = {
     .read_seek      = mp3_seek,
     .priv_data_size = sizeof(MP3DecContext),
     .flags          = AVFMT_GENERIC_INDEX,
-    .extensions     = "mp2,mp3,m2a", /* XXX: use probe */
+    .extensions     = "mp2,mp3,m2a,mpa", /* XXX: use probe */
 };