summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_util.cc
diff options
context:
space:
mode:
authorfbarchard@chromium.org <fbarchard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-25 03:16:21 +0000
committerfbarchard@chromium.org <fbarchard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-25 03:16:21 +0000
commit9787fec1d6a14f3ca72288d277d3049c2c65b85e (patch)
tree07c5beebcd00439d414831d179cec77d7dfe0c38 /media/audio/audio_util.cc
parente435d6b7103f0d0c57133121454458bda6ccb69f (diff)
downloadchromium_src-9787fec1d6a14f3ca72288d277d3049c2c65b85e.zip
chromium_src-9787fec1d6a14f3ca72288d277d3049c2c65b85e.tar.gz
chromium_src-9787fec1d6a14f3ca72288d277d3049c2c65b85e.tar.bz2
Surround Sound handling by folding 5 channels down to stereo.
BUG=16026 TEST=play a movie trailer with 5.1 audio. It should sound roughly the same as quicktime. Review URL: http://codereview.chromium.org/155894 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21613 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/audio_util.cc')
-rw-r--r--media/audio/audio_util.cc89
1 files changed, 89 insertions, 0 deletions
diff --git a/media/audio/audio_util.cc b/media/audio/audio_util.cc
index dabe448..c1c1bc8 100644
--- a/media/audio/audio_util.cc
+++ b/media/audio/audio_util.cc
@@ -59,4 +59,93 @@ bool AdjustVolume(void* buf,
return false;
}
+// Channel order for AAC
+// From http://www.hydrogenaudio.org/forums/lofiversion/index.php/t40046.html
+// And Quicktime Pro, Movie Inspector
+const int kChannel_C = 0;
+const int kChannel_L = 1;
+const int kChannel_R = 2;
+const int kChannel_SL = 3;
+const int kChannel_SR = 4;
+const int kChannel_LFE = 5;
+
+template<class Format>
+Format ChannelsClampInternal(float val,
+ const Format min_value,
+ const Format max_value) {
+ if (val > static_cast<float>(max_value)) {
+ return max_value;
+ }
+ if (val < static_cast<float>(min_value)) {
+ return min_value;
+ }
+ return static_cast<Format>(val);
+}
+
+template<class Format>
+void FoldChannelsInternal(Format* buf_out,
+ int sample_count,
+ float volume,
+ int channels,
+ const int min_value,
+ const int max_value) {
+ Format* buf_in = buf_out;
+ // mid_value is to adjust excess 128 notation in unsigned 8 bit samples
+ // to signed before adding channels.
+ const int mid_value = (max_value + min_value + 1) / 2;
+ const float kHalfPerceived = 0.707f; // 1/sqrt(2)
+ for (int i = 0; i < sample_count; ++i) {
+ float center_half = static_cast<float>(buf_in[kChannel_C] - mid_value) * kHalfPerceived;
+ float left = static_cast<float>(buf_in[kChannel_L] - mid_value);
+ float right = static_cast<float>(buf_in[kChannel_R] - mid_value);
+ float surround_left = static_cast<float>(buf_in[kChannel_SL] - mid_value);
+ float surround_right = static_cast<float>(buf_in[kChannel_SR] - mid_value);
+ buf_out[0] = ChannelsClampInternal(
+ (left + surround_left + center_half) * volume + mid_value,
+ static_cast<Format>(min_value), static_cast<Format>(max_value));
+ buf_out[1] = ChannelsClampInternal(
+ (right + surround_right + center_half) * volume + mid_value,
+ static_cast<Format>(min_value), static_cast<Format>(max_value));
+ buf_out += 2;
+ buf_in += channels;
+ }
+}
+
+bool FoldChannels(void* buf,
+ size_t buflen,
+ int channels,
+ int bytes_per_sample,
+ float volume) {
+ DCHECK(buf);
+ DCHECK(volume >= 0.0f && volume <= 1.0f);
+ if (channels >= 5 && channels <= 6 && bytes_per_sample > 0) {
+ int sample_count = buflen / (channels * bytes_per_sample);
+ if (bytes_per_sample == 1) {
+ FoldChannelsInternal(reinterpret_cast<uint8*>(buf),
+ sample_count,
+ volume,
+ channels,
+ 0, 255);
+ return true;
+ } else if (bytes_per_sample == 2) {
+ FoldChannelsInternal(reinterpret_cast<int16*>(buf),
+ sample_count,
+ volume,
+ channels,
+ -32768,
+ 32767);
+ return true;
+ } else if (bytes_per_sample == 4) {
+ FoldChannelsInternal(reinterpret_cast<int32*>(buf),
+ sample_count,
+ volume,
+ channels,
+ 0x80000000,
+ 0x7fffffff);
+ return true;
+ }
+ }
+ return false;
+}
+
} // namespace media