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

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: Adding trim_start_. 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') | 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 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 const int32 kUint8Bias = 128;
247
248 static inline int32 ConvertU8ToS32(uint8 value) {
249 return (static_cast<int32>(value) - kUint8Bias) << 24;
250 }
251
252 static inline int32 ConvertS16ToS32(int16 value) {
253 return static_cast<int32>(value) << 16;
254 }
255
256 static inline int32 ConvertF32ToS32(float value) {
257 return static_cast<int32>(value < 0
258 ? value * ~std::numeric_limits<int32>::min()
DaleCurtis 2014/09/25 22:30:44 I don't think this does what you want; e.g., ~(-5)
259 : value * std::numeric_limits<int32>::max());
260 }
261
262 void AudioBuffer::ReadFramesInterleavedS32(int frames_to_copy,
263 int32* dest_data) {
264 DCHECK_EQ(frames_to_copy, channel_count_ * adjusted_frame_count_);
265
266 if (sample_format_ == kSampleFormatU8) {
267 // Unsigned 8-bit w/ bias of 128.
268 const uint8* source_data =
269 reinterpret_cast<const uint8*>(channel_data_[0]) + trim_start_;
270
271 for (int i = 0; i < adjusted_frame_count_ * channel_count_; ++i) {
272 dest_data[i] = ConvertU8ToS32(source_data[i]);
273 }
274 } else if (sample_format_ == kSampleFormatS16) {
275 // Format is interleaved signed16. Convert each value into int32 and insert
276 // into output channel data.
277 const int16* source_data =
278 reinterpret_cast<const int16*>(channel_data_[0]) + trim_start_;
279
280 for (int i = 0; i < adjusted_frame_count_ * channel_count_; ++i) {
281 dest_data[i] = ConvertS16ToS32(source_data[i]);
282 }
283 } else if (sample_format_ == kSampleFormatS32) {
284 // Format is interleaved signed32; just copy the data.
285 const int32* source_data =
286 reinterpret_cast<const int32*>(channel_data_[0]) + trim_start_;
287 memcpy(dest_data,
288 source_data,
289 adjusted_frame_count_ * channel_count_ * sizeof(int32));
290 } else if (sample_format_ == kSampleFormatF32) {
291 // Format is interleaved float. Convert each value into int32 and insert
292 // into output channel data.
293 const float* source_data =
294 reinterpret_cast<const float*>(channel_data_[0]) + trim_start_;
295 for (int i = 0; i < adjusted_frame_count_ * channel_count_; ++i) {
296 dest_data[i] = ConvertF32ToS32(source_data[i]);
297 }
298 } else if (sample_format_ == kSampleFormatPlanarS16) {
299 // Format is planar signed 16 bit. Convert each value into int32 and insert
300 // into output channel data.
301 int dest_data_frame_size = channel_count_;
302 for (int ch = 0; ch < channel_count_; ++ch) {
303 const int16* source_data =
304 reinterpret_cast<const int16*>(channel_data_[ch]) + trim_start_;
305
306 for (int i = 0; i < adjusted_frame_count_; ++i) {
307 dest_data[ch + (i * dest_data_frame_size)] =
DaleCurtis 2014/09/25 22:30:44 A paired incrementing offset is more efficient tha
308 ConvertS16ToS32(source_data[i]);
309 }
310 }
311 } else if (sample_format_ == kSampleFormatPlanarF32) {
312 // Format is planar float. Convert each value into int32 and insert into
313 // output channel data.
314 int dest_data_frame_size = channel_count_;
315 for (int ch = 0; ch < channel_count_; ++ch) {
316 const float* source_data =
317 reinterpret_cast<const float*>(channel_data_[ch]) + trim_start_;
318
319 for (int i = 0; i < adjusted_frame_count_; ++i) {
320 dest_data[ch + (i * dest_data_frame_size)] =
DaleCurtis 2014/09/25 22:30:44 Ditto.
321 ConvertF32ToS32(source_data[i]);
322 }
323 }
324 } else {
325 NOTREACHED();
DaleCurtis 2014/09/25 22:30:44 If you prefer you could use case statement since i
326 }
327 }
328
246 void AudioBuffer::TrimStart(int frames_to_trim) { 329 void AudioBuffer::TrimStart(int frames_to_trim) {
247 CHECK_GE(frames_to_trim, 0); 330 CHECK_GE(frames_to_trim, 0);
248 CHECK_LE(frames_to_trim, adjusted_frame_count_); 331 CHECK_LE(frames_to_trim, adjusted_frame_count_);
249 332
250 // Adjust the number of frames in this buffer and where the start really is. 333 // Adjust the number of frames in this buffer and where the start really is.
251 adjusted_frame_count_ -= frames_to_trim; 334 adjusted_frame_count_ -= frames_to_trim;
252 trim_start_ += frames_to_trim; 335 trim_start_ += frames_to_trim;
253 336
254 // Adjust timestamp_ and duration_ to reflect the smaller number of frames. 337 // Adjust timestamp_ and duration_ to reflect the smaller number of frames.
255 const base::TimeDelta old_duration = duration_; 338 const base::TimeDelta old_duration = duration_;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } 386 }
304 } else { 387 } else {
305 CHECK_EQ(frames_to_copy, 0); 388 CHECK_EQ(frames_to_copy, 0);
306 } 389 }
307 390
308 // Trim the leftover data off the end of the buffer and update duration. 391 // Trim the leftover data off the end of the buffer and update duration.
309 TrimEnd(frames_to_trim); 392 TrimEnd(frames_to_trim);
310 } 393 }
311 394
312 } // namespace media 395 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_buffer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698