OLD | NEW |
---|---|
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 <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "media/base/audio_bus.h" | 10 #include "media/base/audio_bus.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 : sample_format_(sample_format), | 30 : sample_format_(sample_format), |
31 channel_layout_(channel_layout), | 31 channel_layout_(channel_layout), |
32 channel_count_(channel_count), | 32 channel_count_(channel_count), |
33 sample_rate_(sample_rate), | 33 sample_rate_(sample_rate), |
34 adjusted_frame_count_(frame_count), | 34 adjusted_frame_count_(frame_count), |
35 trim_start_(0), | 35 trim_start_(0), |
36 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), | 36 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), |
37 timestamp_(timestamp), | 37 timestamp_(timestamp), |
38 duration_(end_of_stream_ | 38 duration_(end_of_stream_ |
39 ? base::TimeDelta() | 39 ? base::TimeDelta() |
40 : CalculateDuration(adjusted_frame_count_, sample_rate_)) { | 40 : CalculateDuration(adjusted_frame_count_, sample_rate_)), |
41 data_size_(0) { | |
41 CHECK_GE(channel_count_, 0); | 42 CHECK_GE(channel_count_, 0); |
42 CHECK_LE(channel_count_, limits::kMaxChannels); | 43 CHECK_LE(channel_count_, limits::kMaxChannels); |
43 CHECK_GE(frame_count, 0); | 44 CHECK_GE(frame_count, 0); |
44 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || | 45 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || |
45 ChannelLayoutToChannelCount(channel_layout) == channel_count); | 46 ChannelLayoutToChannelCount(channel_layout) == channel_count); |
46 | 47 |
47 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); | 48 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); |
48 DCHECK_LE(bytes_per_channel, kChannelAlignment); | 49 DCHECK_LE(bytes_per_channel, kChannelAlignment); |
49 int data_size = frame_count * bytes_per_channel; | 50 int data_size = frame_count * bytes_per_channel; |
xhwang
2015/11/23 22:22:18
this should be something like data_size_per_channe
jrummell
2015/11/24 02:37:52
Done.
| |
50 | 51 |
51 // Empty buffer? | 52 // Empty buffer? |
52 if (!create_buffer) | 53 if (!create_buffer) |
53 return; | 54 return; |
xhwang
2015/11/23 22:22:18
Move this up to l.47?
jrummell
2015/11/24 02:37:53
But it would then miss the DCHECK on line 49. Move
| |
54 | 55 |
55 if (sample_format == kSampleFormatPlanarF32 || | 56 if (sample_format == kSampleFormatPlanarF32 || |
56 sample_format == kSampleFormatPlanarS16 || | 57 sample_format == kSampleFormatPlanarS16 || |
57 sample_format == kSampleFormatPlanarS32) { | 58 sample_format == kSampleFormatPlanarS32) { |
58 // Planar data, so need to allocate buffer for each channel. | 59 // Planar data, so need to allocate buffer for each channel. |
59 // Determine per channel data size, taking into account alignment. | 60 // Determine per channel data size, taking into account alignment. |
60 int block_size_per_channel = | 61 int block_size_per_channel = |
61 (data_size + kChannelAlignment - 1) & ~(kChannelAlignment - 1); | 62 (data_size + kChannelAlignment - 1) & ~(kChannelAlignment - 1); |
62 DCHECK_GE(block_size_per_channel, data_size); | 63 DCHECK_GE(block_size_per_channel, data_size); |
63 | 64 |
64 // Allocate a contiguous buffer for all the channel data. | 65 // Allocate a contiguous buffer for all the channel data. |
65 data_.reset(static_cast<uint8*>(base::AlignedAlloc( | 66 data_size_ = channel_count_ * block_size_per_channel; |
66 channel_count_ * block_size_per_channel, kChannelAlignment))); | 67 data_.reset( |
68 static_cast<uint8*>(base::AlignedAlloc(data_size_, kChannelAlignment))); | |
67 channel_data_.reserve(channel_count_); | 69 channel_data_.reserve(channel_count_); |
68 | 70 |
69 // Copy each channel's data into the appropriate spot. | 71 // Copy each channel's data into the appropriate spot. |
70 for (int i = 0; i < channel_count_; ++i) { | 72 for (int i = 0; i < channel_count_; ++i) { |
71 channel_data_.push_back(data_.get() + i * block_size_per_channel); | 73 channel_data_.push_back(data_.get() + i * block_size_per_channel); |
72 if (data) | 74 if (data) |
73 memcpy(channel_data_[i], data[i], data_size); | 75 memcpy(channel_data_[i], data[i], data_size); |
74 } | 76 } |
75 return; | 77 return; |
76 } | 78 } |
77 | 79 |
78 // Remaining formats are interleaved data. | 80 // Remaining formats are interleaved data. |
79 DCHECK(sample_format_ == kSampleFormatU8 || | 81 DCHECK(sample_format_ == kSampleFormatU8 || |
80 sample_format_ == kSampleFormatS16 || | 82 sample_format_ == kSampleFormatS16 || |
81 sample_format_ == kSampleFormatS32 || | 83 sample_format_ == kSampleFormatS32 || |
82 sample_format_ == kSampleFormatF32) << sample_format_; | 84 sample_format_ == kSampleFormatF32) << sample_format_; |
83 // Allocate our own buffer and copy the supplied data into it. Buffer must | 85 // Allocate our own buffer and copy the supplied data into it. Buffer must |
84 // contain the data for all channels. | 86 // contain the data for all channels. |
85 data_size *= channel_count_; | 87 data_size_ = data_size * channel_count_; |
86 data_.reset( | 88 data_.reset( |
87 static_cast<uint8*>(base::AlignedAlloc(data_size, kChannelAlignment))); | 89 static_cast<uint8*>(base::AlignedAlloc(data_size_, kChannelAlignment))); |
88 channel_data_.reserve(1); | 90 channel_data_.reserve(1); |
89 channel_data_.push_back(data_.get()); | 91 channel_data_.push_back(data_.get()); |
90 if (data) | 92 if (data) |
91 memcpy(data_.get(), data[0], data_size); | 93 memcpy(data_.get(), data[0], data_size); |
92 } | 94 } |
93 | 95 |
94 AudioBuffer::~AudioBuffer() {} | 96 AudioBuffer::~AudioBuffer() {} |
95 | 97 |
96 // static | 98 // static |
97 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( | 99 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 } | 420 } |
419 } else { | 421 } else { |
420 CHECK_EQ(frames_to_copy, 0); | 422 CHECK_EQ(frames_to_copy, 0); |
421 } | 423 } |
422 | 424 |
423 // Trim the leftover data off the end of the buffer and update duration. | 425 // Trim the leftover data off the end of the buffer and update duration. |
424 TrimEnd(frames_to_trim); | 426 TrimEnd(frames_to_trim); |
425 } | 427 } |
426 | 428 |
427 } // namespace media | 429 } // namespace media |
OLD | NEW |