1 // Copyright (c) 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.
5 #include "remoting/codec/audio_decoder_speex.h"
9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "remoting/proto/audio.pb.h"
14 #include "third_party/speex/include/speex/speex_callbacks.h"
15 #include "third_party/speex/include/speex/speex_stereo.h"
21 // Hosts will never generate more than 100 frames in a single packet.
22 const int kMaxFramesPerPacket
= 100;
26 AudioDecoderSpeex::AudioDecoderSpeex() {
27 // Create and initialize the Speex structures.
28 speex_bits_
.reset(new SpeexBits());
29 speex_bits_init(speex_bits_
.get());
30 speex_state_
= speex_decoder_init(&speex_wb_mode
);
32 // Create and initialize the Speex stereo state.
33 speex_stereo_state_
= speex_stereo_state_init();
35 // Create and initialize the stereo callback.
36 speex_callback_
.reset(new SpeexCallback());
37 speex_callback_
->callback_id
= SPEEX_INBAND_STEREO
;
38 speex_callback_
->func
= speex_std_stereo_request_handler
;
39 speex_callback_
->data
= speex_stereo_state_
;
43 // Turn on perceptual enhancer, which will make the audio sound better,
44 // at the price of further distorting the decoded samples.
46 result
= speex_decoder_ctl(speex_state_
, SPEEX_SET_ENH
, &enhancer
);
49 // Get the frame size, so that we know the size of output when we decode
51 result
= speex_decoder_ctl(speex_state_
,
56 // Set the stereo callback, so that the Speex decoder can get the intensity
57 // stereo information.
58 result
= speex_decoder_ctl(speex_state_
,
60 speex_callback_
.get());
64 AudioDecoderSpeex::~AudioDecoderSpeex() {
65 speex_stereo_state_destroy(speex_stereo_state_
);
66 speex_decoder_destroy(speex_state_
);
67 speex_bits_destroy(speex_bits_
.get());
70 scoped_ptr
<AudioPacket
> AudioDecoderSpeex::Decode(
71 scoped_ptr
<AudioPacket
> packet
) {
72 if ((packet
->encoding() != AudioPacket::ENCODING_SPEEX
) ||
73 (packet
->bytes_per_sample() != AudioPacket::BYTES_PER_SAMPLE_2
) ||
74 (packet
->sampling_rate() == AudioPacket::SAMPLING_RATE_INVALID
) ||
75 (packet
->channels() != AudioPacket::CHANNELS_STEREO
)) {
76 LOG(WARNING
) << "Received an unsupported packet.";
77 return scoped_ptr
<AudioPacket
>();
79 if (packet
->data_size() > kMaxFramesPerPacket
) {
80 LOG(WARNING
) << "Received an packet with too many frames.";
81 return scoped_ptr
<AudioPacket
>();
84 // Create a new packet of decoded data.
85 scoped_ptr
<AudioPacket
> decoded_packet(new AudioPacket());
86 decoded_packet
->set_encoding(AudioPacket::ENCODING_RAW
);
87 decoded_packet
->set_sampling_rate(packet
->sampling_rate());
88 decoded_packet
->set_bytes_per_sample(packet
->bytes_per_sample());
89 decoded_packet
->set_channels(packet
->channels());
91 std::string
* decoded_data
= decoded_packet
->add_data();
92 decoded_data
->resize(packet
->data_size() *
94 packet
->bytes_per_sample() *
96 int16
* samples
= reinterpret_cast<int16
*>(string_as_array(decoded_data
));
98 for (int i
= 0; i
< packet
->data_size(); ++i
) {
99 // Read the bytes into the bits structure.
100 speex_bits_read_from(speex_bits_
.get(),
101 string_as_array(packet
->mutable_data(i
)),
102 packet
->data(i
).size());
104 // Decode the frame and store it in the buffer.
105 int status
= speex_decode_int(speex_state_
, speex_bits_
.get(), samples
);
107 LOG(ERROR
) << "Error in decoding Speex data.";
108 return scoped_ptr
<AudioPacket
>();
110 // Transform mono to stereo.
111 speex_decode_stereo_int(samples
, speex_frame_size_
, speex_stereo_state_
);
113 samples
+= (speex_frame_size_
* packet
->channels());
116 return decoded_packet
.Pass();
119 } // namespace remoting