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

Side by Side Diff: remoting/codec/audio_decoder_speex.cc

Issue 10831246: Speex encoding/decoding. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 4 months 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 | Annotate | Revision Log
OLDNEW
(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"
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 if (result < 0) {
Sergey Ulanov 2012/08/21 17:32:23 nit: If you move result to a separate variable, th
kxing 2012/08/21 18:10:20 Done.
42 LOG(FATAL) << "Failed to turn on the perceptual enhancer";
43 return;
Sergey Ulanov 2012/08/21 17:32:23 nit: don't need this return because you have LOG(F
kxing 2012/08/21 18:10:20 Done.
44 }
45
46 // Get the frame size, so that we know the size of output when we decode
47 // frame by frame.
48 result = speex_decoder_ctl(speex_state_,
49 SPEEX_GET_FRAME_SIZE,
50 &speex_frame_size_);
51 if (result < 0) {
52 LOG(FATAL) << "Failed to get the frame size";
53 return;
54 }
55
56 // Set the stereo callback, so that the Speex decoder can get the intensity
57 // stereo information.
58 result = speex_decoder_ctl(speex_state_,
59 SPEEX_SET_HANDLER,
60 speex_callback_.get());
61 if (result < 0) {
62 LOG(FATAL) << "Failed to set the stereo callback.";
63 return;
64 }
65 }
66
67 AudioDecoderSpeex::~AudioDecoderSpeex() {
68 speex_stereo_state_destroy(speex_stereo_state_);
69 speex_decoder_destroy(speex_state_);
70 speex_bits_destroy(speex_bits_.get());
71 }
72
73 scoped_ptr<AudioPacket> AudioDecoderSpeex::Decode(
74 scoped_ptr<AudioPacket> packet) {
75 if ((packet->encoding() != AudioPacket::ENCODING_SPEEX) ||
76 (packet->bytes_per_sample() != AudioPacket::BYTES_PER_SAMPLE_2) ||
77 (packet->sampling_rate() == AudioPacket::SAMPLING_RATE_INVALID) ||
78 (packet->channels() != AudioPacket::CHANNELS_STEREO)) {
79 LOG(WARNING) << "Received an unsupported packet.";
80 return scoped_ptr<AudioPacket>(NULL);
81 }
82
83 // Create a new packet of decoded data.
84 scoped_ptr<AudioPacket> decoded_packet(new AudioPacket());
85 decoded_packet->set_encoding(AudioPacket::ENCODING_RAW);
86 decoded_packet->set_sampling_rate(packet->sampling_rate());
87 decoded_packet->set_bytes_per_sample(packet->bytes_per_sample());
88 decoded_packet->set_channels(packet->channels());
89
90 std::string* decoded_data = decoded_packet->add_data();
91 decoded_data->resize(packet->data_size() *
92 speex_frame_size_ *
93 packet->bytes_per_sample() *
94 packet->channels());
95 int16* samples = reinterpret_cast<int16*>(string_as_array(decoded_data));
96
97 for (int i = 0; i < packet->data_size(); ++i) {
98 // Read the bytes into the bits structure.
99 speex_bits_read_from(speex_bits_.get(),
100 string_as_array(packet->mutable_data(i)),
101 packet->data(i).size());
102
103 // Decode the frame and store it in the buffer.
104 int status = speex_decode_int(speex_state_, speex_bits_.get(), samples);
105 if (status < 0) {
106 LOG(ERROR) << "Error in decoding Speex data.";
107 return scoped_ptr<AudioPacket>(NULL);
108 }
109 // Transform mono to stereo.
110 speex_decode_stereo_int(samples, speex_frame_size_, speex_stereo_state_);
111
112 samples += (speex_frame_size_ * packet->channels());
113 }
114
115 return decoded_packet.Pass();
116 }
117
118 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698