]> git.tdb.fi Git - libs/al.git/commitdiff
Refactor Mp3Decoder a bit
authorMikko Rasa <tdb@tdb.fi>
Sun, 24 Jun 2018 14:45:13 +0000 (17:45 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 24 Jun 2018 14:45:13 +0000 (17:45 +0300)
It's now possible to decode only the header for a frame

source/mp3decoder.cpp
source/mp3decoder.h

index 00fc9181bd69a2a2d6318d7d13153c3a71fce363..ec08be7f9a5969bfe2ee67ad81f4a1eb5adf6bbd 100644 (file)
@@ -54,7 +54,9 @@ Mp3Decoder::Mp3Decoder(IO::Seekable &io):
 
        try
        {
-               decode_frame();
+               if(!fill_input() || !decode(false))
+                       throw runtime_error("no mp3 data");
+
                format = create_format(2, MAD_NCHANNELS(&priv->frame.header));
                freq = priv->frame.header.samplerate;
        }
@@ -98,11 +100,8 @@ unsigned Mp3Decoder::read(char *buf, unsigned len)
        unsigned pos = 0;
        while(pos+2*nchan<=len)
        {
-               if(read_pos>=pcm.length)
-               {
-                       if(!decode_frame())
-                               break;
-               }
+               if(read_pos>=pcm.length && !decode(true))
+                       break;
 
                for(unsigned i=0; i<nchan; ++i)
                {
@@ -135,26 +134,34 @@ bool Mp3Decoder::fill_input()
        return len;
 }
 
-bool Mp3Decoder::decode_frame()
+bool Mp3Decoder::try_decode(bool full)
 {
-       while(1)
+       if(full)
+               return !mad_frame_decode(&priv->frame, &priv->stream);
+       else
+               return !mad_header_decode(&priv->frame.header, &priv->stream);
+}
+
+bool Mp3Decoder::decode(bool full)
+{
+       while(!try_decode(full))
        {
-               if(!priv->stream.buffer || priv->stream.error==MAD_ERROR_BUFLEN)
+               if(priv->stream.error==MAD_ERROR_BUFLEN)
+               {
                        if(!fill_input())
                                return false;
-
-               if(!mad_frame_decode(&priv->frame, &priv->stream))
-               {
-                       mad_synth_frame(&priv->synth, &priv->frame);
-                       read_pos = 0;
-                       return true;
                }
-
-               if(priv->stream.error==MAD_ERROR_BUFLEN)
-                       continue;
                else if(!MAD_RECOVERABLE(priv->stream.error))
                        throw mp3_error("mad_frame_decode", priv->stream.error);
        }
+
+       if(full)
+       {
+               read_pos = 0;
+               mad_synth_frame(&priv->synth, &priv->frame);
+       }
+
+       return true;
 }
 
 } // namespace AL
index 06060de0671b0fbf872df4a8d2cda1f339ac0e90..c026081126b3f76c259d9dea70e70948bd54b0dc 100644 (file)
@@ -40,7 +40,8 @@ public:
 
 private:
        bool fill_input();
-       bool decode_frame();
+       bool try_decode(bool);
+       bool decode(bool);
 };
 
 } // namespace AL