Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: media/base/multi_channel_resampler.cc

Issue 14969017: Cleanup and reduce memory usage for MultiChannelResampler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/base/multi_channel_resampler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/base/multi_channel_resampler.h" 5 #include "media/base/multi_channel_resampler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "media/base/audio_bus.h" 10 #include "media/base/audio_bus.h"
11 11
12 namespace media { 12 namespace media {
13 13
14 MultiChannelResampler::MultiChannelResampler(int channels, 14 MultiChannelResampler::MultiChannelResampler(int channels,
15 double io_sample_rate_ratio, 15 double io_sample_rate_ratio,
16 size_t request_size, 16 size_t request_size,
17 const ReadCB& read_cb) 17 const ReadCB& read_cb)
18 : last_frame_count_(0), 18 : read_cb_(read_cb),
19 read_cb_(read_cb), 19 wrapped_resampler_audio_bus_(AudioBus::CreateWrapper(channels)),
20 output_frames_ready_(0) { 20 output_frames_ready_(0) {
21 // Allocate each channel's resampler. 21 // Allocate each channel's resampler.
22 resamplers_.reserve(channels); 22 resamplers_.reserve(channels);
23 for (int i = 0; i < channels; ++i) { 23 for (int i = 0; i < channels; ++i) {
24 resamplers_.push_back(new SincResampler( 24 resamplers_.push_back(new SincResampler(
25 io_sample_rate_ratio, request_size, base::Bind( 25 io_sample_rate_ratio, request_size, base::Bind(
26 &MultiChannelResampler::ProvideInput, base::Unretained(this), i))); 26 &MultiChannelResampler::ProvideInput, base::Unretained(this), i)));
27 } 27 }
28
29 // Setup the wrapped AudioBus for channel data.
30 wrapped_resampler_audio_bus_->set_frames(request_size);
31
32 // Allocate storage for all channels except the first, which will use the
33 // |destination| provided to ProvideInput() directly.
34 if (channels > 1) {
35 resampler_audio_bus_ = AudioBus::Create(channels - 1, request_size);
36 for (int i = 0; i < resampler_audio_bus_->channels(); ++i) {
37 wrapped_resampler_audio_bus_->SetChannelData(
38 i + 1, resampler_audio_bus_->channel(i));
39 }
40 }
28 } 41 }
29 42
30 MultiChannelResampler::~MultiChannelResampler() {} 43 MultiChannelResampler::~MultiChannelResampler() {}
31 44
32 void MultiChannelResampler::Resample(int frames, AudioBus* audio_bus) { 45 void MultiChannelResampler::Resample(int frames, AudioBus* audio_bus) {
33 DCHECK_EQ(static_cast<size_t>(audio_bus->channels()), resamplers_.size()); 46 DCHECK_EQ(static_cast<size_t>(audio_bus->channels()), resamplers_.size());
34 47
35 // Optimize the single channel case to avoid the chunking process below. 48 // Optimize the single channel case to avoid the chunking process below.
36 if (audio_bus->channels() == 1) { 49 if (audio_bus->channels() == 1) {
37 resamplers_[0]->Resample(frames, audio_bus->channel(0)); 50 resamplers_[0]->Resample(frames, audio_bus->channel(0));
(...skipping 27 matching lines...) Expand all
65 } 78 }
66 } 79 }
67 80
68 void MultiChannelResampler::ProvideInput(int channel, 81 void MultiChannelResampler::ProvideInput(int channel,
69 int frames, 82 int frames,
70 float* destination) { 83 float* destination) {
71 // Get the data from the multi-channel provider when the first channel asks 84 // Get the data from the multi-channel provider when the first channel asks
72 // for it. For subsequent channels, we can just dish out the channel data 85 // for it. For subsequent channels, we can just dish out the channel data
73 // from that (stored in |resampler_audio_bus_|). 86 // from that (stored in |resampler_audio_bus_|).
74 if (channel == 0) { 87 if (channel == 0) {
75 // Allocate staging arrays on the first request and if the frame size or 88 wrapped_resampler_audio_bus_->SetChannelData(0, destination);
76 // |destination| changes (should only happen once).
77 if (!resampler_audio_bus_.get() ||
78 resampler_audio_bus_->frames() != frames ||
79 wrapped_resampler_audio_bus_->channel(0) != destination) {
80 resampler_audio_bus_ = AudioBus::Create(resamplers_.size(), frames);
81
82 // Create a channel vector based on |resampler_audio_bus_| but using
83 // |destination| directly for the first channel and then wrap it in a new
84 // AudioBus so we can avoid an extra memcpy later.
85 resampler_audio_data_.clear();
86 resampler_audio_data_.reserve(resampler_audio_bus_->channels());
87 resampler_audio_data_.push_back(destination);
88 for (int i = 1; i < resampler_audio_bus_->channels(); ++i)
89 resampler_audio_data_.push_back(resampler_audio_bus_->channel(i));
90 wrapped_resampler_audio_bus_ = AudioBus::WrapVector(
91 frames, resampler_audio_data_);
92 }
93
94 last_frame_count_ = frames;
95 read_cb_.Run(output_frames_ready_, wrapped_resampler_audio_bus_.get()); 89 read_cb_.Run(output_frames_ready_, wrapped_resampler_audio_bus_.get());
96 } else { 90 } else {
97 // All channels must ask for the same amount. This should always be the 91 // All channels must ask for the same amount. This should always be the
98 // case, but let's just make sure. 92 // case, but let's just make sure.
99 DCHECK_EQ(frames, last_frame_count_); 93 DCHECK_EQ(frames, wrapped_resampler_audio_bus_->frames());
100 94
101 // Copy the channel data from what we received from |read_cb_|. 95 // Copy the channel data from what we received from |read_cb_|.
102 memcpy(destination, resampler_audio_bus_->channel(channel), 96 memcpy(destination, wrapped_resampler_audio_bus_->channel(channel),
103 sizeof(*resampler_audio_bus_->channel(channel)) * frames); 97 sizeof(*wrapped_resampler_audio_bus_->channel(channel)) * frames);
104 } 98 }
105 } 99 }
106 100
107 void MultiChannelResampler::Flush() { 101 void MultiChannelResampler::Flush() {
108 last_frame_count_ = 0;
109 for (size_t i = 0; i < resamplers_.size(); ++i) 102 for (size_t i = 0; i < resamplers_.size(); ++i)
110 resamplers_[i]->Flush(); 103 resamplers_[i]->Flush();
111 } 104 }
112 105
113 void MultiChannelResampler::SetRatio(double io_sample_rate_ratio) { 106 void MultiChannelResampler::SetRatio(double io_sample_rate_ratio) {
114 for (size_t i = 0; i < resamplers_.size(); ++i) 107 for (size_t i = 0; i < resamplers_.size(); ++i)
115 resamplers_[i]->SetRatio(io_sample_rate_ratio); 108 resamplers_[i]->SetRatio(io_sample_rate_ratio);
116 } 109 }
117 110
118 } // namespace media 111 } // namespace media
OLDNEW
« no previous file with comments | « media/base/multi_channel_resampler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698