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

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

Issue 600143002: Adding new function ReadFrames() that returns the audio frame in planar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Functioning unittests Created 6 years, 2 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
« no previous file with comments | « media/base/audio_buffer.h ('k') | media/base/audio_buffer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/audio_buffer.h" 5 #include "media/base/audio_buffer.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "media/base/audio_bus.h" 8 #include "media/base/audio_bus.h"
9 #include "media/base/buffers.h" 9 #include "media/base/buffers.h"
10 #include "media/base/limits.h" 10 #include "media/base/limits.h"
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 DCHECK(sample_format_ == kSampleFormatU8 || 236 DCHECK(sample_format_ == kSampleFormatU8 ||
237 sample_format_ == kSampleFormatS16 || 237 sample_format_ == kSampleFormatS16 ||
238 sample_format_ == kSampleFormatS32); 238 sample_format_ == kSampleFormatS32);
239 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_); 239 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_);
240 int frame_size = channel_count_ * bytes_per_channel; 240 int frame_size = channel_count_ * bytes_per_channel;
241 const uint8* source_data = data_.get() + source_frame_offset * frame_size; 241 const uint8* source_data = data_.get() + source_frame_offset * frame_size;
242 dest->FromInterleavedPartial( 242 dest->FromInterleavedPartial(
243 source_data, dest_frame_offset, frames_to_copy, bytes_per_channel); 243 source_data, dest_frame_offset, frames_to_copy, bytes_per_channel);
244 } 244 }
245 245
246 static inline int32 ConvertS16ToS32(int16 value) {
247 return static_cast<int32>(value) << 16;
248 }
249
250 static inline int32 ConvertF32ToS32(float value) {
251 return static_cast<int32>(value < 0
252 ? (-value) * std::numeric_limits<int32>::min()
253 : value * std::numeric_limits<int32>::max());
254 }
255
256 void AudioBuffer::ReadFramesInterleavedS32(int frames_to_copy,
257 int32* dest_data) {
258 DCHECK_LE(frames_to_copy, channel_count_ * adjusted_frame_count_);
DaleCurtis 2014/09/26 23:02:41 frames_to_copy must be per channel.
pwestin(chromium) 2014/09/29 22:54:47 Done.
259 DCHECK_EQ(frames_to_copy % channel_count_, 0);
260
DaleCurtis 2014/09/26 23:02:41 You'll need to update your frames to copy in the i
pwestin(chromium) 2014/09/29 22:54:47 Done.
261 switch (sample_format_) {
262 case kSampleFormatU8:
263 NOTIMPLEMENTED();
264 break;
265 case kSampleFormatS16: {
266 // Format is interleaved signed16. Convert each value into int32 and
267 // insert into output channel data.
268 const int16* source_data =
269 reinterpret_cast<const int16*>(channel_data_[0]) + trim_start_;
270
271 for (int i = 0; i < frames_to_copy; ++i) {
DaleCurtis 2014/09/26 23:02:41 Typically drop {} for one line for loops. Ditto f
pwestin(chromium) 2014/09/29 22:54:47 Done.
272 dest_data[i] = ConvertS16ToS32(source_data[i]);
273 }
274 } break;
DaleCurtis 2014/09/26 23:02:41 break; should be inside {}. What does clang-format
pwestin(chromium) 2014/09/29 22:54:47 Done.
275 case kSampleFormatS32: {
276 // Format is interleaved signed32; just copy the data.
277 const int32* source_data =
278 reinterpret_cast<const int32*>(channel_data_[0]) + trim_start_;
279 memcpy(dest_data, source_data, frames_to_copy * sizeof(int32));
280 } break;
281 case kSampleFormatF32: {
282 // Format is interleaved float. Convert each value into int32 and insert
283 // into output channel data.
284 const float* source_data =
285 reinterpret_cast<const float*>(channel_data_[0]) + trim_start_;
286 for (int i = 0; i < frames_to_copy; ++i) {
287 dest_data[i] = ConvertF32ToS32(source_data[i]);
288 }
289 } break;
290 case kSampleFormatPlanarS16: {
291 // Format is planar signed 16 bit. Convert each value into int32 and
292 // insert into output channel data.
293 for (int ch = 0; ch < channel_count_; ++ch) {
294 const int16* source_data =
295 reinterpret_cast<const int16*>(channel_data_[ch]) + trim_start_;
296
297 for (int i = 0, offset = ch; i < frames_to_copy / channel_count_;
298 ++i, offset += channel_count_) {
299 dest_data[offset] = ConvertS16ToS32(source_data[i]);
300 }
301 }
302 } break;
303 case kSampleFormatPlanarF32: {
304 // Format is planar float. Convert each value into int32 and insert into
305 // output channel data.
306 for (int ch = 0; ch < channel_count_; ++ch) {
307 const float* source_data =
308 reinterpret_cast<const float*>(channel_data_[ch]) + trim_start_;
309
310 for (int i = 0, offset = ch; i < frames_to_copy / channel_count_;
311 ++i, offset += channel_count_) {
312 dest_data[offset] = ConvertF32ToS32(source_data[i]);
313 }
314 }
315 } break;
316 case kUnknownSampleFormat:
317 NOTREACHED();
318 break;
319 }
320 }
321
246 void AudioBuffer::TrimStart(int frames_to_trim) { 322 void AudioBuffer::TrimStart(int frames_to_trim) {
247 CHECK_GE(frames_to_trim, 0); 323 CHECK_GE(frames_to_trim, 0);
248 CHECK_LE(frames_to_trim, adjusted_frame_count_); 324 CHECK_LE(frames_to_trim, adjusted_frame_count_);
249 325
250 // Adjust the number of frames in this buffer and where the start really is. 326 // Adjust the number of frames in this buffer and where the start really is.
251 adjusted_frame_count_ -= frames_to_trim; 327 adjusted_frame_count_ -= frames_to_trim;
252 trim_start_ += frames_to_trim; 328 trim_start_ += frames_to_trim;
253 329
254 // Adjust timestamp_ and duration_ to reflect the smaller number of frames. 330 // Adjust timestamp_ and duration_ to reflect the smaller number of frames.
255 const base::TimeDelta old_duration = duration_; 331 const base::TimeDelta old_duration = duration_;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } 379 }
304 } else { 380 } else {
305 CHECK_EQ(frames_to_copy, 0); 381 CHECK_EQ(frames_to_copy, 0);
306 } 382 }
307 383
308 // Trim the leftover data off the end of the buffer and update duration. 384 // Trim the leftover data off the end of the buffer and update duration.
309 TrimEnd(frames_to_trim); 385 TrimEnd(frames_to_trim);
310 } 386 }
311 387
312 } // namespace media 388 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_buffer.h ('k') | media/base/audio_buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698