Bug 805254. Part 4: Remove FORMAT_U8 from nsAudioStream::SampleFormat. r=kinetik
authorRobert O'Callahan <robert@ocallahan.org>
Thu, 25 Oct 2012 23:09:39 +1300
changeset 111488 4edf6b315c50fc626223e5a7e526a48a8b00fc18
parent 111487 afb0fa607ef2f12dc8f7d238535b4e91768af412
child 111489 870743d33a56ba6b6a25ddccd79d509f5cceebd3
push id23748
push userryanvm@gmail.com
push dateFri, 26 Oct 2012 11:29:12 +0000
treeherdermozilla-central@8586bd350875 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs805254
milestone19.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 805254. Part 4: Remove FORMAT_U8 from nsAudioStream::SampleFormat. r=kinetik We also give nsWaveReader its own separate format enum.
content/media/AudioSegment.cpp
content/media/AudioSegment.h
content/media/nsAudioStream.h
content/media/wave/nsWaveReader.cpp
content/media/wave/nsWaveReader.h
media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
--- a/content/media/AudioSegment.cpp
+++ b/content/media/AudioSegment.cpp
@@ -14,39 +14,27 @@ namespace mozilla {
  * http://blog.bjornroche.com/2009/12/linearity-and-dynamic-range-in-int.html
  */
 static float
 SampleToFloat(float aValue)
 {
   return aValue;
 }
 static float
-SampleToFloat(uint8_t aValue)
-{
-  return (aValue - 128)/128.0f;
-}
-static float
 SampleToFloat(int16_t aValue)
 {
   return aValue/32768.0f;
 }
 
 static void
 FloatToSample(float aValue, float* aOut)
 {
   *aOut = aValue;
 }
 static void
-FloatToSample(float aValue, uint8_t* aOut)
-{
-  float v = aValue*128 + 128;
-  float clamped = NS_MAX(0.0f, NS_MIN(255.0f, v));
-  *aOut = uint8_t(clamped);
-}
-static void
 FloatToSample(float aValue, int16_t* aOut)
 {
   float v = aValue*32768.0f;
   float clamped = NS_MAX(-32768.0f, NS_MIN(32767.0f, v));
   *aOut = int16_t(clamped);
 }
 
 template <class SrcT, class DestT>
@@ -98,20 +86,16 @@ InterleaveAndConvertBuffer(const SrcT* a
   case nsAudioStream::FORMAT_FLOAT32:
     InterleaveAndConvertBuffer(aSource, aSourceLength, aLength, aVolume,
                                aChannels, static_cast<float*>(aOutput));
     break;
   case nsAudioStream::FORMAT_S16:
     InterleaveAndConvertBuffer(aSource, aSourceLength, aLength, aVolume,
                                aChannels, static_cast<int16_t*>(aOutput));
     break;
-  case nsAudioStream::FORMAT_U8:
-    InterleaveAndConvertBuffer(aSource, aSourceLength, aLength, aVolume,
-                               aChannels, static_cast<uint8_t*>(aOutput));
-    break;
   }
 }
 
 static void
 InterleaveAndConvertBuffer(const void* aSource, nsAudioStream::SampleFormat aSourceFormat,
                            int32_t aSourceLength,
                            int32_t aOffset, int32_t aLength,
                            float aVolume,
@@ -128,23 +112,16 @@ InterleaveAndConvertBuffer(const void* a
     break;
   case nsAudioStream::FORMAT_S16:
     InterleaveAndConvertBuffer(static_cast<const int16_t*>(aSource) + aOffset, aSourceLength,
                                aLength,
                                aVolume,
                                aChannels,
                                aOutput, aOutputFormat);
     break;
-  case nsAudioStream::FORMAT_U8:
-    InterleaveAndConvertBuffer(static_cast<const uint8_t*>(aSource) + aOffset, aSourceLength,
-                               aLength,
-                               aVolume,
-                               aChannels,
-                               aOutput, aOutputFormat);
-    break;
   }
 }
 
 void
 AudioSegment::ApplyVolume(float aVolume)
 {
   for (ChunkIterator ci(*this); !ci.IsEnded(); ci.Next()) {
     ci->mVolume *= aVolume;
--- a/content/media/AudioSegment.h
+++ b/content/media/AudioSegment.h
@@ -62,17 +62,16 @@ struct AudioChunk {
  */
 class AudioSegment : public MediaSegmentBase<AudioSegment, AudioChunk> {
 public:
   typedef nsAudioStream::SampleFormat SampleFormat;
 
   static int GetSampleSize(SampleFormat aFormat)
   {
     switch (aFormat) {
-    case nsAudioStream::FORMAT_U8: return 1;
     case nsAudioStream::FORMAT_S16: return 2;
     case nsAudioStream::FORMAT_FLOAT32: return 4;
     }
     NS_ERROR("Bad format");
     return 0;
   }
 
   AudioSegment() : MediaSegmentBase<AudioSegment, AudioChunk>(AUDIO), mChannels(0) {}
--- a/content/media/nsAudioStream.h
+++ b/content/media/nsAudioStream.h
@@ -16,18 +16,19 @@
 // GetPosition, GetPositionInFrames, SetVolume, and Get{Rate,Channels}
 // is thread-safe without external synchronization.
 class nsAudioStream : public nsISupports
 {
 public:
 
   enum SampleFormat
   {
-    FORMAT_U8,
+    // Native-endian signed 16-bit audio samples
     FORMAT_S16,
+    // Signed 32-bit float samples
     FORMAT_FLOAT32
   };
 
   nsAudioStream()
     : mRate(0),
       mChannels(0)
   {}
 
--- a/content/media/wave/nsWaveReader.cpp
+++ b/content/media/wave/nsWaveReader.cpp
@@ -171,25 +171,25 @@ bool nsWaveReader::DecodeAudioData()
     return false;
   }
 
   // convert data to samples
   const char* d = dataBuffer.get();
   AudioDataValue* s = sampleBuffer.get();
   for (int i = 0; i < frames; ++i) {
     for (unsigned int j = 0; j < mChannels; ++j) {
-      if (mSampleFormat == nsAudioStream::FORMAT_U8) {
+      if (mSampleFormat == FORMAT_U8) {
         uint8_t v =  ReadUint8(&d);
 #if defined(MOZ_SAMPLE_TYPE_S16)
         *s++ = (v * (1.F/UINT8_MAX)) * UINT16_MAX + INT16_MIN;
 #elif defined(MOZ_SAMPLE_TYPE_FLOAT32)
         *s++ = (v * (1.F/UINT8_MAX)) * 2.F - 1.F;
 #endif
       }
-      else if (mSampleFormat == nsAudioStream::FORMAT_S16) {
+      else if (mSampleFormat == FORMAT_S16) {
         int16_t v =  ReadInt16LE(&d);
 #if defined(MOZ_SAMPLE_TYPE_S16)
         *s++ = v;
 #elif defined(MOZ_SAMPLE_TYPE_FLOAT32)
         *s++ = (int32_t(v) - INT16_MIN) / float(UINT16_MAX) * 2.F - 1.F;
 #endif
       }
     }
@@ -446,19 +446,19 @@ nsWaveReader::LoadFormatChunk()
     return false;
   }
 
   ReentrantMonitorAutoEnter monitor(mDecoder->GetReentrantMonitor());
   mSampleRate = rate;
   mChannels = channels;
   mFrameSize = frameSize;
   if (sampleFormat == 8) {
-    mSampleFormat = nsAudioStream::FORMAT_U8;
+    mSampleFormat = FORMAT_U8;
   } else {
-    mSampleFormat = nsAudioStream::FORMAT_S16;
+    mSampleFormat = FORMAT_S16;
   }
   return true;
 }
 
 bool
 nsWaveReader::FindDataOffset()
 {
   // RIFF chunks are always word (two byte) aligned.
--- a/content/media/wave/nsWaveReader.h
+++ b/content/media/wave/nsWaveReader.h
@@ -75,18 +75,22 @@ private:
 
   // Number of channels.  Limited to range [1, 2] in LoadFormatChunk.
   uint32_t mChannels;
 
   // Size of a single audio frame, which includes a sample for each channel
   // (interleaved).
   uint32_t mFrameSize;
 
-  // The sample format of the PCM data.
-  nsAudioStream::SampleFormat mSampleFormat;
+  // The sample format of the PCM data. nsAudioStream::SampleFormat doesn't
+  // support U8.
+  enum {
+    FORMAT_U8,
+    FORMAT_S16
+  } mSampleFormat;
 
   // Size of PCM data stored in the WAVE as reported by the data chunk in
   // the media.
   int64_t mWaveLength;
 
   // Start offset of the PCM data in the media stream.  Extends mWaveLength
   // bytes.
   int64_t mWavePCMOffset;
--- a/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
+++ b/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
@@ -547,17 +547,16 @@ NotifyQueuedTrackChanges(MediaStreamGrap
 void MediaPipelineTransmit::ProcessAudioChunk(AudioSessionConduit *conduit,
                                               TrackRate rate,
                                               AudioChunk& chunk) {
   // TODO(ekr@rtfm.com): Do more than one channel
   nsAutoArrayPtr<int16_t> samples(new int16_t[chunk.mDuration]);
 
   if (chunk.mBuffer) {
     switch(chunk.mBufferFormat) {
-      case nsAudioStream::FORMAT_U8:
       case nsAudioStream::FORMAT_FLOAT32:
         MOZ_MTLOG(PR_LOG_ERROR, "Can't process audio except in 16-bit PCM yet");
         MOZ_ASSERT(PR_FALSE);
         return;
         break;
       case nsAudioStream::FORMAT_S16:
         {
           // Code based on nsAudioStream