Merge "Fix calculations for an obscure combo of MPEG audio options." into jb-mr1-dev
diff --git a/media/libstagefright/MP3Extractor.cpp b/media/libstagefright/MP3Extractor.cpp
index 6abaf23..d94054b 100644
--- a/media/libstagefright/MP3Extractor.cpp
+++ b/media/libstagefright/MP3Extractor.cpp
@@ -228,6 +228,7 @@
     virtual ~MP3Source();
 
 private:
+    static const size_t kMaxFrameSize;
     sp<MetaData> mMeta;
     sp<DataSource> mDataSource;
     off64_t mFirstFramePos;
@@ -405,6 +406,13 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
+// The theoretical maximum frame size for an MPEG audio stream should occur
+// while playing a Layer 2, MPEGv2.5 audio stream at 160kbps (with padding).
+// The size of this frame should be...
+// ((1152 samples/frame * 160000 bits/sec) /
+//  (8000 samples/sec * 8 bits/byte)) + 1 padding byte/frame = 2881 bytes/frame.
+// Set our max frame size to the nearest power of 2 above this size (aka, 4kB)
+const size_t MP3Source::kMaxFrameSize = (1 << 12); /* 4096 bytes */
 MP3Source::MP3Source(
         const sp<MetaData> &meta, const sp<DataSource> &source,
         off64_t first_frame_pos, uint32_t fixed_header,
@@ -433,7 +441,6 @@
 
     mGroup = new MediaBufferGroup;
 
-    const size_t kMaxFrameSize = 32768;
     mGroup->add_buffer(new MediaBuffer(kMaxFrameSize));
 
     mCurrentPos = mFirstFramePos;
diff --git a/media/libstagefright/avc_utils.cpp b/media/libstagefright/avc_utils.cpp
index 65c1848..a141752 100644
--- a/media/libstagefright/avc_utils.cpp
+++ b/media/libstagefright/avc_utils.cpp
@@ -600,7 +600,7 @@
 
             bitrate = kBitrateV2[bitrate_index - 1];
             if (out_num_samples) {
-                *out_num_samples = 576;
+                *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
             }
         }
 
@@ -612,7 +612,8 @@
             *frame_size = 144000 * bitrate / sampling_rate + padding;
         } else {
             // V2 or V2.5
-            *frame_size = 72000 * bitrate / sampling_rate + padding;
+            size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
+            *frame_size = tmp * bitrate / sampling_rate + padding;
         }
     }