audio_buffer.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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
3// found in the LICENSE file.
4
5#include "media/base/audio_buffer.h"
6
7#include "base/logging.h"
8#include "media/base/audio_bus.h"
9#include "media/base/buffers.h"
10#include "media/base/limits.h"
11
12namespace media {
13
14// Alignment of each channel's data; use 8-byte alignment as that is bigger
15// than maximum size of a sample, and the minimum alignment.
16enum { kChannelAlignment = 8 };
17
18AudioBuffer::AudioBuffer(SampleFormat sample_format,
19                         int channel_count,
20                         int frame_count,
21                         const uint8* const* data,
22                         const base::TimeDelta timestamp,
23                         const base::TimeDelta duration)
24    : sample_format_(sample_format),
25      channel_count_(channel_count),
26      frame_count_(frame_count),
27      timestamp_(timestamp),
28      duration_(duration) {
29  CHECK_GE(channel_count, 0);
30  CHECK_LE(channel_count, limits::kMaxChannels);
31  CHECK_GE(frame_count, 0);
32  int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format);
33  DCHECK_LE(bytes_per_channel, kChannelAlignment);
34  int data_size = frame_count * bytes_per_channel;
35
36  // Empty buffer?
37  if (!data) {
38    CHECK_EQ(frame_count, 0);
39    return;
40  }
41
42  if (sample_format == kSampleFormatPlanarF32 ||
43      sample_format == kSampleFormatPlanarS16) {
44    // Planar data, so need to allocate buffer for each channel.
45    // Determine per channel data size, taking into account alignment.
46    int block_size_per_channel =
47        (data_size + kChannelAlignment - 1) & ~(kChannelAlignment - 1);
48    DCHECK_GE(block_size_per_channel, data_size);
49
50    // Allocate a contiguous buffer for all the channel data.
51    data_.reset(static_cast<uint8*>(base::AlignedAlloc(
52        channel_count * block_size_per_channel, kChannelAlignment)));
53    channel_data_.reserve(channel_count);
54
55    // Copy each channel's data into the appropriate spot.
56    for (int i = 0; i < channel_count; ++i) {
57      channel_data_.push_back(data_.get() + i * block_size_per_channel);
58      memcpy(channel_data_[i], data[i], data_size);
59    }
60    return;
61  }
62
63  // Remaining formats are interleaved data.
64  DCHECK(sample_format_ == kSampleFormatU8 ||
65         sample_format_ == kSampleFormatS16 ||
66         sample_format_ == kSampleFormatS32 ||
67         sample_format_ == kSampleFormatF32) << sample_format_;
68  // Allocate our own buffer and copy the supplied data into it. Buffer must
69  // contain the data for all channels.
70  data_size *= channel_count;
71  data_.reset(
72      static_cast<uint8*>(base::AlignedAlloc(data_size, kChannelAlignment)));
73  memcpy(data_.get(), data[0], data_size);
74}
75
76AudioBuffer::~AudioBuffer() {}
77
78// static
79scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom(
80    SampleFormat sample_format,
81    int channel_count,
82    int frame_count,
83    const uint8* const* data,
84    const base::TimeDelta timestamp,
85    const base::TimeDelta duration) {
86  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
87  CHECK(data[0]);
88  return make_scoped_refptr(new AudioBuffer(
89      sample_format, channel_count, frame_count, data, timestamp, duration));
90}
91
92// static
93scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() {
94  return make_scoped_refptr(new AudioBuffer(
95      kUnknownSampleFormat, 1, 0, NULL, kNoTimestamp(), kNoTimestamp()));
96}
97
98// Convert int16 values in the range [kint16min, kint16max] to [-1.0, 1.0].
99static inline float ConvertS16ToFloat(int16 value) {
100  return value * (value < 0 ? -1.0f / kint16min : 1.0f / kint16max);
101}
102
103void AudioBuffer::ReadFrames(int frames_to_copy,
104                             int source_frame_offset,
105                             int dest_frame_offset,
106                             AudioBus* dest) {
107  // Deinterleave each channel (if necessary) and convert to 32bit
108  // floating-point with nominal range -1.0 -> +1.0 (if necessary).
109
110  // |dest| must have the same number of channels, and the number of frames
111  // specified must be in range.
112  DCHECK(!end_of_stream());
113  DCHECK_EQ(dest->channels(), channel_count_);
114  DCHECK_LE(source_frame_offset + frames_to_copy, frame_count_);
115  DCHECK_LE(dest_frame_offset + frames_to_copy, dest->frames());
116
117  if (sample_format_ == kSampleFormatPlanarF32) {
118    // Format is planar float32. Copy the data from each channel as a block.
119    for (int ch = 0; ch < channel_count_; ++ch) {
120      const float* source_data =
121          reinterpret_cast<const float*>(channel_data_[ch]) +
122          source_frame_offset;
123      memcpy(dest->channel(ch) + dest_frame_offset,
124             source_data,
125             sizeof(float) * frames_to_copy);
126    }
127    return;
128  }
129
130  if (sample_format_ == kSampleFormatPlanarS16) {
131    // Format is planar signed16. Convert each value into float and insert into
132    // output channel data.
133    for (int ch = 0; ch < channel_count_; ++ch) {
134      const int16* source_data =
135          reinterpret_cast<const int16*>(channel_data_[ch]) +
136          source_frame_offset;
137      float* dest_data = dest->channel(ch) + dest_frame_offset;
138      for (int i = 0; i < frames_to_copy; ++i) {
139        dest_data[i] = ConvertS16ToFloat(source_data[i]);
140      }
141    }
142    return;
143  }
144
145  if (sample_format_ == kSampleFormatF32) {
146    // Format is interleaved float32. Copy the data into each channel.
147    const float* source_data = reinterpret_cast<const float*>(data_.get()) +
148                               source_frame_offset * channel_count_;
149    for (int ch = 0; ch < channel_count_; ++ch) {
150      float* dest_data = dest->channel(ch) + dest_frame_offset;
151      for (int i = 0, offset = ch; i < frames_to_copy;
152           ++i, offset += channel_count_) {
153        dest_data[i] = source_data[offset];
154      }
155    }
156    return;
157  }
158
159  // Remaining formats are integer interleaved data. Use the deinterleaving code
160  // in AudioBus to copy the data.
161  DCHECK(sample_format_ == kSampleFormatU8 ||
162         sample_format_ == kSampleFormatS16 ||
163         sample_format_ == kSampleFormatS32);
164  int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_);
165  int frame_size = channel_count_ * bytes_per_channel;
166  const uint8* source_data = data_.get() + source_frame_offset * frame_size;
167  dest->FromInterleavedPartial(
168      source_data, dest_frame_offset, frames_to_copy, bytes_per_channel);
169}
170
171}  // namespace media
172