Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / media_file / source / media_file_utility.cc
index e8883c9..71d62a1 100644 (file)
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+#include "webrtc/common_audio/wav_header.h"
 #include "webrtc/common_types.h"
 #include "webrtc/engine_configurations.h"
 #include "webrtc/modules/interface/module_common_types.h"
 #endif
 
 namespace {
-enum WaveFormats
-{
-    kWaveFormatPcm   = 0x0001,
-    kWaveFormatALaw  = 0x0006,
-    kWaveFormatMuLaw = 0x0007
-};
 
 // First 16 bytes the WAVE header. ckID should be "RIFF", wave_ckID should be
 // "WAVE" and ckSize is the chunk size (4 + n)
@@ -183,7 +178,7 @@ int32_t ModuleFileUtility::InitAviWriting(
             waveFormatHeader.nSamplesPerSec  = 8000;
             waveFormatHeader.wBitsPerSample  = 8;
             waveFormatHeader.nBlockAlign     = 1;
-            waveFormatHeader.wFormatTag      = kWaveFormatMuLaw;
+            waveFormatHeader.wFormatTag      = kWavFormatMuLaw;
 
         } else if (strncmp(audioCodecInst.plname, "PCMA", 4) == 0)
         {
@@ -196,7 +191,7 @@ int32_t ModuleFileUtility::InitAviWriting(
             waveFormatHeader.nSamplesPerSec  = 8000;
             waveFormatHeader.wBitsPerSample  = 8;
             waveFormatHeader.nBlockAlign     = 1;
-            waveFormatHeader.wFormatTag      = kWaveFormatALaw;
+            waveFormatHeader.wFormatTag      = kWavFormatALaw;
 
         } else if (strncmp(audioCodecInst.plname, "L16", 3) == 0)
         {
@@ -210,7 +205,7 @@ int32_t ModuleFileUtility::InitAviWriting(
             waveFormatHeader.nSamplesPerSec  = audioCodecInst.plfreq;
             waveFormatHeader.wBitsPerSample  = 16;
             waveFormatHeader.nBlockAlign     = 2;
-            waveFormatHeader.wFormatTag      = kWaveFormatPcm;
+            waveFormatHeader.wFormatTag      = kWavFormatPcm;
         } else
         {
             return -1;
@@ -499,8 +494,7 @@ int32_t ModuleFileUtility::ReadWavHeader(InStream& wav)
 
             memcpy(tmpStr2, &_wavFormatObj.formatTag, 2);
             _wavFormatObj.formatTag =
-                (WaveFormats) ((uint32_t)tmpStr2[0] +
-                               (((uint32_t)tmpStr2[1])<<8));
+                (uint32_t)tmpStr2[0] + (((uint32_t)tmpStr2[1])<<8);
             memcpy(tmpStr2, &_wavFormatObj.nChannels, 2);
             _wavFormatObj.nChannels =
                 (int16_t) ((uint32_t)tmpStr2[0] +
@@ -575,9 +569,9 @@ int32_t ModuleFileUtility::ReadWavHeader(InStream& wav)
 
     // Either a proper format chunk has been read or a data chunk was come
     // across.
-    if( (_wavFormatObj.formatTag != kWaveFormatPcm) &&
-        (_wavFormatObj.formatTag != kWaveFormatALaw) &&
-        (_wavFormatObj.formatTag != kWaveFormatMuLaw))
+    if( (_wavFormatObj.formatTag != kWavFormatPcm) &&
+        (_wavFormatObj.formatTag != kWavFormatALaw) &&
+        (_wavFormatObj.formatTag != kWavFormatMuLaw))
     {
         WEBRTC_TRACE(kTraceError, kTraceFile, _id,
                      "Coding formatTag value=%d not supported!",
@@ -603,7 +597,7 @@ int32_t ModuleFileUtility::ReadWavHeader(InStream& wav)
     }
 
     // Calculate the number of bytes that 10 ms of audio data correspond to.
-    if(_wavFormatObj.formatTag == kWaveFormatPcm)
+    if(_wavFormatObj.formatTag == kWavFormatPcm)
     {
         // TODO (hellner): integer division for 22050 and 11025 would yield
         //                 the same result as the else statement. Remove those
@@ -643,19 +637,19 @@ int32_t ModuleFileUtility::InitWavCodec(uint32_t samplesPerSec,
     // Calculate the packet size for 10ms frames
     switch(formatTag)
     {
-    case kWaveFormatALaw:
+    case kWavFormatALaw:
         strcpy(codec_info_.plname, "PCMA");
         _codecId = kCodecPcma;
         codec_info_.pltype = 8;
         codec_info_.pacsize  = codec_info_.plfreq / 100;
         break;
-    case kWaveFormatMuLaw:
+    case kWavFormatMuLaw:
         strcpy(codec_info_.plname, "PCMU");
         _codecId = kCodecPcmu;
         codec_info_.pltype = 0;
         codec_info_.pacsize  = codec_info_.plfreq / 100;
          break;
-    case kWaveFormatPcm:
+    case kWavFormatPcm:
         codec_info_.pacsize  = (bitsPerSample * (codec_info_.plfreq / 100)) / 8;
         if(samplesPerSec == 8000)
         {
@@ -1054,14 +1048,14 @@ int32_t ModuleFileUtility::InitWavWriting(OutStream& wav,
     {
         _bytesPerSample = 1;
         if(WriteWavHeader(wav, 8000, _bytesPerSample, channels,
-                          kWaveFormatMuLaw, 0) == -1)
+                          kWavFormatMuLaw, 0) == -1)
         {
             return -1;
         }
     }else if(STR_CASE_CMP(codecInst.plname, "PCMA") == 0)
     {
         _bytesPerSample = 1;
-        if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWaveFormatALaw,
+        if(WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWavFormatALaw,
                           0) == -1)
         {
             return -1;
@@ -1071,7 +1065,7 @@ int32_t ModuleFileUtility::InitWavWriting(OutStream& wav,
     {
         _bytesPerSample = 2;
         if(WriteWavHeader(wav, codecInst.plfreq, _bytesPerSample, channels,
-                          kWaveFormatPcm, 0) == -1)
+                          kWavFormatPcm, 0) == -1)
         {
             return -1;
         }
@@ -1124,103 +1118,18 @@ int32_t ModuleFileUtility::WriteWavHeader(
     const uint32_t format,
     const uint32_t lengthInBytes)
 {
-
     // Frame size in bytes for 10 ms of audio.
     // TODO (hellner): 44.1 kHz has 440 samples frame size. Doesn't seem to
     //                 be taken into consideration here!
-    int32_t frameSize = (freq / 100) * bytesPerSample * channels;
+    const int32_t frameSize = (freq / 100) * channels;
 
     // Calculate the number of full frames that the wave file contain.
-    const int32_t dataLengthInBytes = frameSize *
-        (lengthInBytes / frameSize);
-
-    int8_t tmpStr[4];
-    int8_t tmpChar;
-    uint32_t tmpLong;
-
-    memcpy(tmpStr, "RIFF", 4);
-    wav.Write(tmpStr, 4);
-
-    tmpLong = dataLengthInBytes + 36;
-    tmpChar = (int8_t)(tmpLong);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 8);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 16);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 24);
-    wav.Write(&tmpChar, 1);
-
-    memcpy(tmpStr, "WAVE", 4);
-    wav.Write(tmpStr, 4);
-
-    memcpy(tmpStr, "fmt ", 4);
-    wav.Write(tmpStr, 4);
-
-    tmpChar = 16;
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-
-    tmpChar = (int8_t)(format);
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-
-    tmpChar = (int8_t)(channels);
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-
-    tmpLong = freq;
-    tmpChar = (int8_t)(tmpLong);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 8);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 16);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 24);
-    wav.Write(&tmpChar, 1);
-
-    // nAverageBytesPerSec = Sample rate * Bytes per sample * Channels
-    tmpLong = bytesPerSample * freq * channels;
-    tmpChar = (int8_t)(tmpLong);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 8);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 16);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 24);
-    wav.Write(&tmpChar, 1);
-
-    // nBlockAlign = Bytes per sample * Channels
-    tmpChar = (int8_t)(bytesPerSample * channels);
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-
-    tmpChar = (int8_t)(bytesPerSample*8);
-    wav.Write(&tmpChar, 1);
-    tmpChar = 0;
-    wav.Write(&tmpChar, 1);
-
-    memcpy(tmpStr, "data", 4);
-    wav.Write(tmpStr, 4);
-
-    tmpLong = dataLengthInBytes;
-    tmpChar = (int8_t)(tmpLong);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 8);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 16);
-    wav.Write(&tmpChar, 1);
-    tmpChar = (int8_t)(tmpLong >> 24);
-    wav.Write(&tmpChar, 1);
+    const int32_t dataLengthInBytes = frameSize * (lengthInBytes / frameSize);
 
+    uint8_t buf[kWavHeaderSize];
+    webrtc::WriteWavHeader(buf, channels, freq, static_cast<WavFormat>(format),
+                           bytesPerSample, dataLengthInBytes / bytesPerSample);
+    wav.Write(buf, kWavHeaderSize);
     return 0;
 }
 
@@ -1237,12 +1146,12 @@ int32_t ModuleFileUtility::UpdateWavHeader(OutStream& wav)
     if(STR_CASE_CMP(codec_info_.plname, "L16") == 0)
     {
         res = WriteWavHeader(wav, codec_info_.plfreq, 2, channels,
-                             kWaveFormatPcm, _bytesWritten);
+                             kWavFormatPcm, _bytesWritten);
     } else if(STR_CASE_CMP(codec_info_.plname, "PCMU") == 0) {
-            res = WriteWavHeader(wav, 8000, 1, channels, kWaveFormatMuLaw,
+            res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatMuLaw,
                                  _bytesWritten);
     } else if(STR_CASE_CMP(codec_info_.plname, "PCMA") == 0) {
-            res = WriteWavHeader(wav, 8000, 1, channels, kWaveFormatALaw,
+            res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatALaw,
                                  _bytesWritten);
     } else {
         // Allow calling this API even if not writing to a WAVE file.