OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "remoting/codec/audio_decoder_speex.h" | |
6 | |
7 #include <string> | |
8 | |
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/speex.h" | |
15 #include "third_party/speex/include/speex/speex_callbacks.h" | |
Paweł Hajdan Jr.
2012/08/29 08:51:33
This breaks build with system speex, see https://b
| |
16 #include "third_party/speex/include/speex/speex_stereo.h" | |
17 | |
18 namespace remoting { | |
19 | |
20 AudioDecoderSpeex::AudioDecoderSpeex() { | |
21 // Create and initialize the Speex structures. | |
22 speex_bits_.reset(new SpeexBits()); | |
23 speex_bits_init(speex_bits_.get()); | |
24 speex_state_ = speex_decoder_init(&speex_wb_mode); | |
25 | |
26 // Create and initialize the Speex stereo state. | |
27 speex_stereo_state_ = speex_stereo_state_init(); | |
28 | |
29 // Create and initialize the stereo callback. | |
30 speex_callback_.reset(new SpeexCallback()); | |
31 speex_callback_->callback_id = SPEEX_INBAND_STEREO; | |
32 speex_callback_->func = speex_std_stereo_request_handler; | |
33 speex_callback_->data = speex_stereo_state_; | |
34 | |
35 int result; | |
36 | |
37 // Turn on perceptual enhancer, which will make the audio sound better, | |
38 // at the price of further distorting the decoded samples. | |
39 int enhancer = 1; | |
40 result = speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer); | |
41 CHECK_EQ(result, 0); | |
42 | |
43 // Get the frame size, so that we know the size of output when we decode | |
44 // frame by frame. | |
45 result = speex_decoder_ctl(speex_state_, | |
46 SPEEX_GET_FRAME_SIZE, | |
47 &speex_frame_size_); | |
48 CHECK_EQ(result, 0); | |
49 | |
50 // Set the stereo callback, so that the Speex decoder can get the intensity | |
51 // stereo information. | |
52 result = speex_decoder_ctl(speex_state_, | |
53 SPEEX_SET_HANDLER, | |
54 speex_callback_.get()); | |
55 CHECK_EQ(result, 0); | |
56 } | |
57 | |
58 AudioDecoderSpeex::~AudioDecoderSpeex() { | |
59 speex_stereo_state_destroy(speex_stereo_state_); | |
60 speex_decoder_destroy(speex_state_); | |
61 speex_bits_destroy(speex_bits_.get()); | |
62 } | |
63 | |
64 scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode( | |
65 scoped_ptr<AudioPacket> packet) { | |
66 if ((packet->encoding() != AudioPacket::ENCODING_SPEEX) || | |
67 (packet->bytes_per_sample() != AudioPacket::BYTES_PER_SAMPLE_2) || | |
68 (packet->sampling_rate() == AudioPacket::SAMPLING_RATE_INVALID) || | |
69 (packet->channels() != AudioPacket::CHANNELS_STEREO)) { | |
70 LOG(WARNING) << "Received an unsupported packet."; | |
71 return scoped_ptr<AudioPacket>(NULL); | |
72 } | |
73 | |
74 // Create a new packet of decoded data. | |
75 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket()); | |
76 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW); | |
77 decoded_packet->set_sampling_rate(packet->sampling_rate()); | |
78 decoded_packet->set_bytes_per_sample(packet->bytes_per_sample()); | |
79 decoded_packet->set_channels(packet->channels()); | |
80 | |
81 std::string* decoded_data = decoded_packet->add_data(); | |
82 decoded_data->resize(packet->data_size() * | |
83 speex_frame_size_ * | |
84 packet->bytes_per_sample() * | |
85 packet->channels()); | |
86 int16* samples = reinterpret_cast<int16*>(string_as_array(decoded_data)); | |
87 | |
88 for (int i = 0; i < packet->data_size(); ++i) { | |
89 // Read the bytes into the bits structure. | |
90 speex_bits_read_from(speex_bits_.get(), | |
91 string_as_array(packet->mutable_data(i)), | |
92 packet->data(i).size()); | |
93 | |
94 // Decode the frame and store it in the buffer. | |
95 int status = speex_decode_int(speex_state_, speex_bits_.get(), samples); | |
96 if (status < 0) { | |
97 LOG(ERROR) << "Error in decoding Speex data."; | |
98 return scoped_ptr<AudioPacket>(NULL); | |
99 } | |
100 // Transform mono to stereo. | |
101 speex_decode_stereo_int(samples, speex_frame_size_, speex_stereo_state_); | |
102 | |
103 samples += (speex_frame_size_ * packet->channels()); | |
104 } | |
105 | |
106 return decoded_packet.Pass(); | |
107 } | |
108 | |
109 } // namespace remoting | |
OLD | NEW |