[libav-commits] mp3dec: move XING/Info and VBRI parsing into their own functions

Alessandro Ghedini git at libav.org
Thu Apr 17 20:09:03 CEST 2014


Module: libav
Branch: master
Commit: fbd8e042107ec63e0ddf155588c59dcb76007641

Author:    Alessandro Ghedini <alessandro at ghedini.me>
Committer: Anton Khirnov <anton at khirnov.net>
Date:      Sat Apr 12 18:25:41 2014 +0200

mp3dec: move XING/Info and VBRI parsing into their own functions

Signed-off-by: Anton Khirnov <anton at khirnov.net>

---

 libavformat/mp3dec.c |   93 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 58 insertions(+), 35 deletions(-)

diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c
index 414c7e9..1ab5159 100644
--- a/libavformat/mp3dec.c
+++ b/libavformat/mp3dec.c
@@ -39,6 +39,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 */
@@ -125,18 +128,60 @@ 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)
+{
+    uint32_t v;
+    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) {
+        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));
+    }
+}
+
+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;
 
     v = avio_rb32(s->pb);
     if(ff_mpa_check_header(v) < 0)
@@ -149,45 +194,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;
 }



More information about the libav-commits mailing list