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

Side by Side Diff: media/cast/receiver/audio_decoder.cc

Issue 1534273002: Switch to standard integer types in media/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more Created 5 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/cast/receiver/audio_decoder.h" 5 #include "media/cast/receiver/audio_decoder.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/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 39
40 void DecodeFrame(scoped_ptr<EncodedFrame> encoded_frame, 40 void DecodeFrame(scoped_ptr<EncodedFrame> encoded_frame,
41 const DecodeFrameCallback& callback) { 41 const DecodeFrameCallback& callback) {
42 DCHECK_EQ(operational_status_, STATUS_INITIALIZED); 42 DCHECK_EQ(operational_status_, STATUS_INITIALIZED);
43 43
44 static_assert(sizeof(encoded_frame->frame_id) == sizeof(last_frame_id_), 44 static_assert(sizeof(encoded_frame->frame_id) == sizeof(last_frame_id_),
45 "size of frame_id types do not match"); 45 "size of frame_id types do not match");
46 bool is_continuous = true; 46 bool is_continuous = true;
47 if (seen_first_frame_) { 47 if (seen_first_frame_) {
48 const uint32 frames_ahead = encoded_frame->frame_id - last_frame_id_; 48 const uint32_t frames_ahead = encoded_frame->frame_id - last_frame_id_;
49 if (frames_ahead > 1) { 49 if (frames_ahead > 1) {
50 RecoverBecauseFramesWereDropped(); 50 RecoverBecauseFramesWereDropped();
51 is_continuous = false; 51 is_continuous = false;
52 } 52 }
53 } else { 53 } else {
54 seen_first_frame_ = true; 54 seen_first_frame_ = true;
55 } 55 }
56 last_frame_id_ = encoded_frame->frame_id; 56 last_frame_id_ = encoded_frame->frame_id;
57 57
58 scoped_ptr<AudioBus> decoded_audio = Decode( 58 scoped_ptr<AudioBus> decoded_audio = Decode(
(...skipping 15 matching lines...) Expand all
74 is_continuous)); 74 is_continuous));
75 } 75 }
76 76
77 protected: 77 protected:
78 friend class base::RefCountedThreadSafe<ImplBase>; 78 friend class base::RefCountedThreadSafe<ImplBase>;
79 virtual ~ImplBase() {} 79 virtual ~ImplBase() {}
80 80
81 virtual void RecoverBecauseFramesWereDropped() {} 81 virtual void RecoverBecauseFramesWereDropped() {}
82 82
83 // Note: Implementation of Decode() is allowed to mutate |data|. 83 // Note: Implementation of Decode() is allowed to mutate |data|.
84 virtual scoped_ptr<AudioBus> Decode(uint8* data, int len) = 0; 84 virtual scoped_ptr<AudioBus> Decode(uint8_t* data, int len) = 0;
85 85
86 const scoped_refptr<CastEnvironment> cast_environment_; 86 const scoped_refptr<CastEnvironment> cast_environment_;
87 const Codec codec_; 87 const Codec codec_;
88 const int num_channels_; 88 const int num_channels_;
89 89
90 // Subclass' ctor is expected to set this to STATUS_INITIALIZED. 90 // Subclass' ctor is expected to set this to STATUS_INITIALIZED.
91 OperationalStatus operational_status_; 91 OperationalStatus operational_status_;
92 92
93 private: 93 private:
94 bool seen_first_frame_; 94 bool seen_first_frame_;
95 uint32 last_frame_id_; 95 uint32_t last_frame_id_;
96 96
97 DISALLOW_COPY_AND_ASSIGN(ImplBase); 97 DISALLOW_COPY_AND_ASSIGN(ImplBase);
98 }; 98 };
99 99
100 class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase { 100 class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase {
101 public: 101 public:
102 OpusImpl(const scoped_refptr<CastEnvironment>& cast_environment, 102 OpusImpl(const scoped_refptr<CastEnvironment>& cast_environment,
103 int num_channels, 103 int num_channels,
104 int sampling_rate) 104 int sampling_rate)
105 : ImplBase(cast_environment, 105 : ImplBase(cast_environment,
106 CODEC_AUDIO_OPUS, 106 CODEC_AUDIO_OPUS,
107 num_channels, 107 num_channels,
108 sampling_rate), 108 sampling_rate),
109 decoder_memory_(new uint8[opus_decoder_get_size(num_channels)]), 109 decoder_memory_(new uint8_t[opus_decoder_get_size(num_channels)]),
110 opus_decoder_(reinterpret_cast<OpusDecoder*>(decoder_memory_.get())), 110 opus_decoder_(reinterpret_cast<OpusDecoder*>(decoder_memory_.get())),
111 max_samples_per_frame_( 111 max_samples_per_frame_(kOpusMaxFrameDurationMillis * sampling_rate /
112 kOpusMaxFrameDurationMillis * sampling_rate / 1000), 112 1000),
113 buffer_(new float[max_samples_per_frame_ * num_channels]) { 113 buffer_(new float[max_samples_per_frame_ * num_channels]) {
114 if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) 114 if (ImplBase::operational_status_ != STATUS_UNINITIALIZED)
115 return; 115 return;
116 if (opus_decoder_init(opus_decoder_, sampling_rate, num_channels) != 116 if (opus_decoder_init(opus_decoder_, sampling_rate, num_channels) !=
117 OPUS_OK) { 117 OPUS_OK) {
118 ImplBase::operational_status_ = STATUS_INVALID_CONFIGURATION; 118 ImplBase::operational_status_ = STATUS_INVALID_CONFIGURATION;
119 return; 119 return;
120 } 120 }
121 ImplBase::operational_status_ = STATUS_INITIALIZED; 121 ImplBase::operational_status_ = STATUS_INITIALIZED;
122 } 122 }
123 123
124 private: 124 private:
125 ~OpusImpl() final {} 125 ~OpusImpl() final {}
126 126
127 void RecoverBecauseFramesWereDropped() final { 127 void RecoverBecauseFramesWereDropped() final {
128 // Passing NULL for the input data notifies the decoder of frame loss. 128 // Passing NULL for the input data notifies the decoder of frame loss.
129 const opus_int32 result = 129 const opus_int32 result =
130 opus_decode_float( 130 opus_decode_float(
131 opus_decoder_, NULL, 0, buffer_.get(), max_samples_per_frame_, 0); 131 opus_decoder_, NULL, 0, buffer_.get(), max_samples_per_frame_, 0);
132 DCHECK_GE(result, 0); 132 DCHECK_GE(result, 0);
133 } 133 }
134 134
135 scoped_ptr<AudioBus> Decode(uint8* data, int len) final { 135 scoped_ptr<AudioBus> Decode(uint8_t* data, int len) final {
136 scoped_ptr<AudioBus> audio_bus; 136 scoped_ptr<AudioBus> audio_bus;
137 const opus_int32 num_samples_decoded = opus_decode_float( 137 const opus_int32 num_samples_decoded = opus_decode_float(
138 opus_decoder_, data, len, buffer_.get(), max_samples_per_frame_, 0); 138 opus_decoder_, data, len, buffer_.get(), max_samples_per_frame_, 0);
139 if (num_samples_decoded <= 0) 139 if (num_samples_decoded <= 0)
140 return audio_bus.Pass(); // Decode error. 140 return audio_bus.Pass(); // Decode error.
141 141
142 // Copy interleaved samples from |buffer_| into a new AudioBus (where 142 // Copy interleaved samples from |buffer_| into a new AudioBus (where
143 // samples are stored in planar format, for each channel). 143 // samples are stored in planar format, for each channel).
144 audio_bus = AudioBus::Create(num_channels_, num_samples_decoded).Pass(); 144 audio_bus = AudioBus::Create(num_channels_, num_samples_decoded).Pass();
145 // TODO(miu): This should be moved into AudioBus::FromInterleaved(). 145 // TODO(miu): This should be moved into AudioBus::FromInterleaved().
146 for (int ch = 0; ch < num_channels_; ++ch) { 146 for (int ch = 0; ch < num_channels_; ++ch) {
147 const float* src = buffer_.get() + ch; 147 const float* src = buffer_.get() + ch;
148 const float* const src_end = src + num_samples_decoded * num_channels_; 148 const float* const src_end = src + num_samples_decoded * num_channels_;
149 float* dest = audio_bus->channel(ch); 149 float* dest = audio_bus->channel(ch);
150 for (; src < src_end; src += num_channels_, ++dest) 150 for (; src < src_end; src += num_channels_, ++dest)
151 *dest = *src; 151 *dest = *src;
152 } 152 }
153 return audio_bus.Pass(); 153 return audio_bus.Pass();
154 } 154 }
155 155
156 const scoped_ptr<uint8[]> decoder_memory_; 156 const scoped_ptr<uint8_t[]> decoder_memory_;
157 OpusDecoder* const opus_decoder_; 157 OpusDecoder* const opus_decoder_;
158 const int max_samples_per_frame_; 158 const int max_samples_per_frame_;
159 const scoped_ptr<float[]> buffer_; 159 const scoped_ptr<float[]> buffer_;
160 160
161 // According to documentation in third_party/opus/src/include/opus.h, we must 161 // According to documentation in third_party/opus/src/include/opus.h, we must
162 // provide enough space in |buffer_| to contain 120ms of samples. At 48 kHz, 162 // provide enough space in |buffer_| to contain 120ms of samples. At 48 kHz,
163 // then, that means 5760 samples times the number of channels. 163 // then, that means 5760 samples times the number of channels.
164 static const int kOpusMaxFrameDurationMillis = 120; 164 static const int kOpusMaxFrameDurationMillis = 120;
165 165
166 DISALLOW_COPY_AND_ASSIGN(OpusImpl); 166 DISALLOW_COPY_AND_ASSIGN(OpusImpl);
167 }; 167 };
168 168
169 class AudioDecoder::Pcm16Impl : public AudioDecoder::ImplBase { 169 class AudioDecoder::Pcm16Impl : public AudioDecoder::ImplBase {
170 public: 170 public:
171 Pcm16Impl(const scoped_refptr<CastEnvironment>& cast_environment, 171 Pcm16Impl(const scoped_refptr<CastEnvironment>& cast_environment,
172 int num_channels, 172 int num_channels,
173 int sampling_rate) 173 int sampling_rate)
174 : ImplBase(cast_environment, 174 : ImplBase(cast_environment,
175 CODEC_AUDIO_PCM16, 175 CODEC_AUDIO_PCM16,
176 num_channels, 176 num_channels,
177 sampling_rate) { 177 sampling_rate) {
178 if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) 178 if (ImplBase::operational_status_ != STATUS_UNINITIALIZED)
179 return; 179 return;
180 ImplBase::operational_status_ = STATUS_INITIALIZED; 180 ImplBase::operational_status_ = STATUS_INITIALIZED;
181 } 181 }
182 182
183 private: 183 private:
184 ~Pcm16Impl() final {} 184 ~Pcm16Impl() final {}
185 185
186 scoped_ptr<AudioBus> Decode(uint8* data, int len) final { 186 scoped_ptr<AudioBus> Decode(uint8_t* data, int len) final {
187 scoped_ptr<AudioBus> audio_bus; 187 scoped_ptr<AudioBus> audio_bus;
188 const int num_samples = len / sizeof(int16) / num_channels_; 188 const int num_samples = len / sizeof(int16_t) / num_channels_;
189 if (num_samples <= 0) 189 if (num_samples <= 0)
190 return audio_bus.Pass(); 190 return audio_bus.Pass();
191 191
192 int16* const pcm_data = reinterpret_cast<int16*>(data); 192 int16_t* const pcm_data = reinterpret_cast<int16_t*>(data);
193 #if defined(ARCH_CPU_LITTLE_ENDIAN) 193 #if defined(ARCH_CPU_LITTLE_ENDIAN)
194 // Convert endianness. 194 // Convert endianness.
195 const int num_elements = num_samples * num_channels_; 195 const int num_elements = num_samples * num_channels_;
196 for (int i = 0; i < num_elements; ++i) 196 for (int i = 0; i < num_elements; ++i)
197 pcm_data[i] = static_cast<int16>(base::NetToHost16(pcm_data[i])); 197 pcm_data[i] = static_cast<int16_t>(base::NetToHost16(pcm_data[i]));
198 #endif 198 #endif
199 audio_bus = AudioBus::Create(num_channels_, num_samples).Pass(); 199 audio_bus = AudioBus::Create(num_channels_, num_samples).Pass();
200 audio_bus->FromInterleaved(pcm_data, num_samples, sizeof(int16)); 200 audio_bus->FromInterleaved(pcm_data, num_samples, sizeof(int16_t));
201 return audio_bus.Pass(); 201 return audio_bus.Pass();
202 } 202 }
203 203
204 DISALLOW_COPY_AND_ASSIGN(Pcm16Impl); 204 DISALLOW_COPY_AND_ASSIGN(Pcm16Impl);
205 }; 205 };
206 206
207 AudioDecoder::AudioDecoder( 207 AudioDecoder::AudioDecoder(
208 const scoped_refptr<CastEnvironment>& cast_environment, 208 const scoped_refptr<CastEnvironment>& cast_environment,
209 int channels, 209 int channels,
210 int sampling_rate, 210 int sampling_rate,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 cast_environment_->PostTask(CastEnvironment::AUDIO, 243 cast_environment_->PostTask(CastEnvironment::AUDIO,
244 FROM_HERE, 244 FROM_HERE,
245 base::Bind(&AudioDecoder::ImplBase::DecodeFrame, 245 base::Bind(&AudioDecoder::ImplBase::DecodeFrame,
246 impl_, 246 impl_,
247 base::Passed(&encoded_frame), 247 base::Passed(&encoded_frame),
248 callback)); 248 callback));
249 } 249 }
250 250
251 } // namespace cast 251 } // namespace cast
252 } // namespace media 252 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698