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

Side by Side Diff: media/filters/opus_audio_decoder.cc

Issue 2543633006: To M56: Use ffmpeg for opus decoding, no need to maintain our decoder. (Closed)
Patch Set: Created 4 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
« no previous file with comments | « media/filters/opus_audio_decoder.h ('k') | media/renderers/default_renderer_factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 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/filters/opus_audio_decoder.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <cmath>
11
12 #include "base/single_thread_task_runner.h"
13 #include "base/sys_byteorder.h"
14 #include "media/base/audio_buffer.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/audio_discard_helper.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/decoder_buffer.h"
19 #include "media/base/timestamp_constants.h"
20 #include "media/filters/opus_constants.h"
21 #include "third_party/opus/src/include/opus.h"
22 #include "third_party/opus/src/include/opus_multistream.h"
23
24 namespace media {
25
26 static uint16_t ReadLE16(const uint8_t* data,
27 size_t data_size,
28 int read_offset) {
29 uint16_t value = 0;
30 DCHECK_LE(read_offset + sizeof(value), data_size);
31 memcpy(&value, data + read_offset, sizeof(value));
32 return base::ByteSwapToLE16(value);
33 }
34
35 // The Opus specification is part of IETF RFC 6716:
36 // http://tools.ietf.org/html/rfc6716
37
38 // Maximum packet size used in Xiph's opusdec and FFmpeg's libopusdec.
39 static const int kMaxOpusOutputPacketSizeSamples = 960 * 6;
40
41 static void RemapOpusChannelLayout(const uint8_t* opus_mapping,
42 int num_channels,
43 uint8_t* channel_layout) {
44 DCHECK_LE(num_channels, OPUS_MAX_VORBIS_CHANNELS);
45
46 // Reorder the channels to produce the same ordering as FFmpeg, which is
47 // what the pipeline expects.
48 const uint8_t* vorbis_layout_offset =
49 kFFmpegChannelDecodingLayouts[num_channels - 1];
50 for (int channel = 0; channel < num_channels; ++channel)
51 channel_layout[channel] = opus_mapping[vorbis_layout_offset[channel]];
52 }
53
54 struct OpusExtraData {
55 OpusExtraData()
56 : channels(0),
57 skip_samples(0),
58 channel_mapping(0),
59 num_streams(0),
60 num_coupled(0),
61 gain_db(0),
62 stream_map() {
63 memcpy(stream_map, kDefaultOpusChannelLayout,
64 OPUS_MAX_CHANNELS_WITH_DEFAULT_LAYOUT);
65 }
66 int channels;
67 uint16_t skip_samples;
68 int channel_mapping;
69 int num_streams;
70 int num_coupled;
71 int16_t gain_db;
72 uint8_t stream_map[OPUS_MAX_VORBIS_CHANNELS];
73 };
74
75 // Returns true when able to successfully parse and store Opus extra data in
76 // |extra_data|. Based on opus header parsing code in libopusdec from FFmpeg,
77 // and opus_header from Xiph's opus-tools project.
78 static bool ParseOpusExtraData(const uint8_t* data,
79 int data_size,
80 const AudioDecoderConfig& config,
81 OpusExtraData* extra_data) {
82 if (data_size < OPUS_EXTRADATA_SIZE) {
83 DLOG(ERROR) << "Extra data size is too small:" << data_size;
84 return false;
85 }
86
87 extra_data->channels = *(data + OPUS_EXTRADATA_CHANNELS_OFFSET);
88
89 if (extra_data->channels <= 0 ||
90 extra_data->channels > OPUS_MAX_VORBIS_CHANNELS) {
91 DLOG(ERROR) << "invalid channel count in extra data: "
92 << extra_data->channels;
93 return false;
94 }
95
96 extra_data->skip_samples =
97 ReadLE16(data, data_size, OPUS_EXTRADATA_SKIP_SAMPLES_OFFSET);
98 extra_data->gain_db = static_cast<int16_t>(
99 ReadLE16(data, data_size, OPUS_EXTRADATA_GAIN_OFFSET));
100
101 extra_data->channel_mapping = *(data + OPUS_EXTRADATA_CHANNEL_MAPPING_OFFSET);
102
103 if (!extra_data->channel_mapping) {
104 if (extra_data->channels > OPUS_MAX_CHANNELS_WITH_DEFAULT_LAYOUT) {
105 DLOG(ERROR) << "Invalid extra data, missing stream map.";
106 return false;
107 }
108
109 extra_data->num_streams = 1;
110 extra_data->num_coupled =
111 (ChannelLayoutToChannelCount(config.channel_layout()) > 1) ? 1 : 0;
112 return true;
113 }
114
115 if (data_size < OPUS_EXTRADATA_STREAM_MAP_OFFSET + extra_data->channels) {
116 DLOG(ERROR) << "Invalid stream map; insufficient data for current channel "
117 << "count: " << extra_data->channels;
118 return false;
119 }
120
121 extra_data->num_streams = *(data + OPUS_EXTRADATA_NUM_STREAMS_OFFSET);
122 extra_data->num_coupled = *(data + OPUS_EXTRADATA_NUM_COUPLED_OFFSET);
123
124 if (extra_data->num_streams + extra_data->num_coupled != extra_data->channels)
125 DVLOG(1) << "Inconsistent channel mapping.";
126
127 for (int i = 0; i < extra_data->channels; ++i)
128 extra_data->stream_map[i] = *(data + OPUS_EXTRADATA_STREAM_MAP_OFFSET + i);
129 return true;
130 }
131
132 OpusAudioDecoder::OpusAudioDecoder(
133 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
134 : task_runner_(task_runner), opus_decoder_(nullptr) {}
135
136 std::string OpusAudioDecoder::GetDisplayName() const {
137 return "OpusAudioDecoder";
138 }
139
140 void OpusAudioDecoder::Initialize(const AudioDecoderConfig& config,
141 CdmContext* /* cdm_context */,
142 const InitCB& init_cb,
143 const OutputCB& output_cb) {
144 DCHECK(task_runner_->BelongsToCurrentThread());
145 InitCB bound_init_cb = BindToCurrentLoop(init_cb);
146
147 if (config.is_encrypted()) {
148 bound_init_cb.Run(false);
149 return;
150 }
151
152 config_ = config;
153 output_cb_ = BindToCurrentLoop(output_cb);
154
155 if (!ConfigureDecoder()) {
156 bound_init_cb.Run(false);
157 return;
158 }
159
160 bound_init_cb.Run(true);
161 }
162
163 void OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
164 const DecodeCB& decode_cb) {
165 DCHECK(task_runner_->BelongsToCurrentThread());
166 DCHECK(!decode_cb.is_null());
167
168 DecodeBuffer(buffer, BindToCurrentLoop(decode_cb));
169 }
170
171 void OpusAudioDecoder::Reset(const base::Closure& closure) {
172 DCHECK(task_runner_->BelongsToCurrentThread());
173
174 opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE);
175 ResetTimestampState();
176 task_runner_->PostTask(FROM_HERE, closure);
177 }
178
179 OpusAudioDecoder::~OpusAudioDecoder() {
180 DCHECK(task_runner_->BelongsToCurrentThread());
181
182 if (!opus_decoder_)
183 return;
184
185 opus_multistream_decoder_ctl(opus_decoder_, OPUS_RESET_STATE);
186 CloseDecoder();
187 }
188
189 void OpusAudioDecoder::DecodeBuffer(
190 const scoped_refptr<DecoderBuffer>& input,
191 const DecodeCB& decode_cb) {
192 DCHECK(task_runner_->BelongsToCurrentThread());
193 DCHECK(!decode_cb.is_null());
194 DCHECK(input.get());
195
196 // Libopus does not buffer output. Decoding is complete when an end of stream
197 // input buffer is received.
198 if (input->end_of_stream()) {
199 decode_cb.Run(DecodeStatus::OK);
200 return;
201 }
202
203 // Make sure we are notified if http://crbug.com/49709 returns. Issue also
204 // occurs with some damaged files.
205 if (input->timestamp() == kNoTimestamp) {
206 DLOG(ERROR) << "Received a buffer without timestamps!";
207 decode_cb.Run(DecodeStatus::DECODE_ERROR);
208 return;
209 }
210
211 scoped_refptr<AudioBuffer> output_buffer;
212
213 if (!Decode(input, &output_buffer)) {
214 decode_cb.Run(DecodeStatus::DECODE_ERROR);
215 return;
216 }
217
218 if (output_buffer.get()) {
219 output_cb_.Run(output_buffer);
220 }
221
222 decode_cb.Run(DecodeStatus::OK);
223 }
224
225 bool OpusAudioDecoder::ConfigureDecoder() {
226 if (config_.codec() != kCodecOpus) {
227 DVLOG(1) << "Codec must be kCodecOpus.";
228 return false;
229 }
230
231 const int channel_count =
232 ChannelLayoutToChannelCount(config_.channel_layout());
233 if (!config_.IsValidConfig() || channel_count > OPUS_MAX_VORBIS_CHANNELS) {
234 DLOG(ERROR) << "Invalid or unsupported audio stream -"
235 << " codec: " << config_.codec()
236 << " channel count: " << channel_count
237 << " channel layout: " << config_.channel_layout()
238 << " bits per channel: " << config_.bits_per_channel()
239 << " samples per second: " << config_.samples_per_second();
240 return false;
241 }
242
243 if (config_.is_encrypted()) {
244 DLOG(ERROR) << "Encrypted audio stream not supported.";
245 return false;
246 }
247
248 // Clean up existing decoder if necessary.
249 CloseDecoder();
250
251 // Parse the Opus Extra Data.
252 OpusExtraData opus_extra_data;
253 if (!ParseOpusExtraData(config_.extra_data().empty() ? nullptr :
254 &config_.extra_data()[0],
255 config_.extra_data().size(),
256 config_,
257 &opus_extra_data))
258 return false;
259
260 if (config_.codec_delay() < 0) {
261 DLOG(ERROR) << "Invalid file. Incorrect value for codec delay: "
262 << config_.codec_delay();
263 return false;
264 }
265
266 if (config_.codec_delay() != opus_extra_data.skip_samples) {
267 DLOG(WARNING) << "Invalid file. Codec Delay in container does not match "
268 << "the value in Opus Extra Data. " << config_.codec_delay()
269 << " vs " << opus_extra_data.skip_samples;
270 config_.Initialize(config_.codec(), config_.sample_format(),
271 config_.channel_layout(), config_.samples_per_second(),
272 config_.extra_data(), config_.encryption_scheme(),
273 config_.seek_preroll(), opus_extra_data.skip_samples);
274 }
275
276 uint8_t channel_mapping[OPUS_MAX_VORBIS_CHANNELS] = {0};
277 memcpy(&channel_mapping, kDefaultOpusChannelLayout,
278 OPUS_MAX_CHANNELS_WITH_DEFAULT_LAYOUT);
279
280 if (channel_count > OPUS_MAX_CHANNELS_WITH_DEFAULT_LAYOUT) {
281 RemapOpusChannelLayout(opus_extra_data.stream_map,
282 channel_count,
283 channel_mapping);
284 }
285
286 // Init Opus.
287 int status = OPUS_INVALID_STATE;
288 opus_decoder_ = opus_multistream_decoder_create(config_.samples_per_second(),
289 channel_count,
290 opus_extra_data.num_streams,
291 opus_extra_data.num_coupled,
292 channel_mapping,
293 &status);
294 if (!opus_decoder_ || status != OPUS_OK) {
295 DLOG(ERROR) << "opus_multistream_decoder_create failed status="
296 << opus_strerror(status);
297 return false;
298 }
299
300 status = opus_multistream_decoder_ctl(
301 opus_decoder_, OPUS_SET_GAIN(opus_extra_data.gain_db));
302 if (status != OPUS_OK) {
303 DLOG(ERROR) << "Failed to set OPUS header gain; status="
304 << opus_strerror(status);
305 return false;
306 }
307
308 ResetTimestampState();
309 return true;
310 }
311
312 void OpusAudioDecoder::CloseDecoder() {
313 if (opus_decoder_) {
314 opus_multistream_decoder_destroy(opus_decoder_);
315 opus_decoder_ = nullptr;
316 }
317 }
318
319 void OpusAudioDecoder::ResetTimestampState() {
320 discard_helper_.reset(
321 new AudioDiscardHelper(config_.samples_per_second(), 0));
322 discard_helper_->Reset(config_.codec_delay());
323 }
324
325 bool OpusAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& input,
326 scoped_refptr<AudioBuffer>* output_buffer) {
327 // Allocate a buffer for the output samples.
328 *output_buffer = AudioBuffer::CreateBuffer(
329 kSampleFormatF32, config_.channel_layout(),
330 ChannelLayoutToChannelCount(config_.channel_layout()),
331 config_.samples_per_second(), kMaxOpusOutputPacketSizeSamples);
332 const int buffer_size = output_buffer->get()->channel_count() *
333 output_buffer->get()->frame_count() *
334 SampleFormatToBytesPerChannel(kSampleFormatF32);
335
336 float* float_output_buffer = reinterpret_cast<float*>(
337 output_buffer->get()->channel_data()[0]);
338 const int frames_decoded =
339 opus_multistream_decode_float(opus_decoder_,
340 input->data(),
341 input->data_size(),
342 float_output_buffer,
343 buffer_size,
344 0);
345
346 if (frames_decoded < 0) {
347 DLOG(ERROR) << "opus_multistream_decode failed for"
348 << " timestamp: " << input->timestamp().InMicroseconds()
349 << " us, duration: " << input->duration().InMicroseconds()
350 << " us, packet size: " << input->data_size() << " bytes with"
351 << " status: " << opus_strerror(frames_decoded);
352 return false;
353 }
354
355 // Trim off any extraneous allocation.
356 DCHECK_LE(frames_decoded, output_buffer->get()->frame_count());
357 const int trim_frames = output_buffer->get()->frame_count() - frames_decoded;
358 if (trim_frames > 0)
359 output_buffer->get()->TrimEnd(trim_frames);
360
361 // Handles discards and timestamping. Discard the buffer if more data needed.
362 if (!discard_helper_->ProcessBuffers(input, *output_buffer))
363 *output_buffer = nullptr;
364
365 return true;
366 }
367
368 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/opus_audio_decoder.h ('k') | media/renderers/default_renderer_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698