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

Side by Side Diff: remoting/codec/audio_encoder_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_encoder_speex.h"
6
7 #include <string>
8 #include <sstream>
9
10 #include "base/basictypes.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_stereo.h"
16
17 namespace {
18 const int kSpeexQuality = 8;
Wez 2012/08/21 01:14:13 nit: Name this constant to indicate what quality w
kxing 2012/08/21 16:56:11 Done.
19 const int kEncodedDataBufferSize = 0xFF;
20 } // namespace
21
22 namespace remoting {
23
24 AudioEncoderSpeex::AudioEncoderSpeex()
25 : leftover_samples_(0) {
26 // Create and initialize the Speex structures.
27 speex_bits_.reset(new SpeexBits());
28 speex_bits_init(speex_bits_.get());
29 speex_state_ = speex_encoder_init(&speex_wb_mode);
30
31 // Set the encoding quality.
32 int quality = kSpeexQuality;
33 speex_encoder_ctl(speex_state_, SPEEX_SET_QUALITY, &quality);
34
35 // Get the frame size and construct the input buffer accordingly.
36 speex_encoder_ctl(speex_state_, SPEEX_GET_FRAME_SIZE, &speex_frame_size_);
Sergey Ulanov 2012/08/20 22:50:39 Check that this operation succeeds? also see my co
kxing 2012/08/21 16:56:11 Done.
37 raw_data_buffer_.reset(
38 new int16[speex_frame_size_ * AudioPacket::CHANNELS_STEREO]);
39 }
40
41 AudioEncoderSpeex::~AudioEncoderSpeex() {
42 speex_encoder_destroy(speex_state_);
43 speex_bits_destroy(speex_bits_.get());
44 }
45
46 scoped_ptr<AudioPacket> AudioEncoderSpeex::Encode(
47 scoped_ptr<AudioPacket> packet) {
48 DCHECK_EQ(AudioPacket::ENCODING_RAW, packet->encoding());
49 DCHECK_EQ(1, packet->data_size());
50 DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2, packet->bytes_per_sample());
51 DCHECK_NE(AudioPacket::SAMPLING_RATE_INVALID, packet->sampling_rate());
52 DCHECK_EQ(AudioPacket::CHANNELS_STEREO, packet->channels());
53
54 // Put the samples onto the queue.
55 int samples_left = packet->data(0).size() / packet->bytes_per_sample();
Sergey Ulanov 2012/08/20 22:50:39 It's better to divide this by number of channels()
kxing 2012/08/21 16:56:11 Done.
56 const int16* next_sample =
57 reinterpret_cast<const int16*>(packet->data(0).data());
58
59 // Create a new packet of encoded data.
60 scoped_ptr<AudioPacket> encoded_packet(new AudioPacket());
61 encoded_packet->set_encoding(AudioPacket::ENCODING_SPEEX);
62 encoded_packet->set_sampling_rate(packet->sampling_rate());
63 encoded_packet->set_bytes_per_sample(packet->bytes_per_sample());
64 encoded_packet->set_channels(packet->channels());
65
66 while (leftover_samples_ + samples_left >=
67 speex_frame_size_ * packet->channels()) {
Sergey Ulanov 2012/08/20 22:50:39 nit: indent one more space to align with (
kxing 2012/08/21 16:56:11 Done.
68 if (leftover_samples_ > 0) {
Wez 2012/08/21 01:14:13 Can you restructure this if...else... so that the
kxing 2012/08/21 16:56:11 Done.
69 int samples_needed =
70 speex_frame_size_ * packet->channels() - leftover_samples_;
71
72 memcpy(raw_data_buffer_.get() + leftover_samples_,
73 next_sample,
74 packet->bytes_per_sample() * samples_needed);
75
76 // Transform stereo to mono.
77 speex_encode_stereo_int(raw_data_buffer_.get(),
78 speex_frame_size_,
79 speex_bits_.get());
Wez 2012/08/21 01:14:13 nit: Missing blank line after this?
kxing 2012/08/21 16:56:11 Done.
80 // Encode the frame, treating all samples as integers.
81 speex_encode_int(speex_state_, raw_data_buffer_.get(), speex_bits_.get());
82
83 leftover_samples_ = 0;
84 next_sample += samples_needed;
85 samples_left -= samples_needed;
86 } else {
87 // Transform stereo to mono.
88 speex_encode_stereo_int(const_cast<int16*>(next_sample),
89 speex_frame_size_,
90 speex_bits_.get());
Wez 2012/08/21 01:14:13 nit: blank line
kxing 2012/08/21 16:56:11 Done.
91 // Encode the frame, treating all samples as integers.
92 speex_encode_int(speex_state_,
93 const_cast<int16*>(next_sample),
94 speex_bits_.get());
Wez 2012/08/21 01:14:13 nit: and here
kxing 2012/08/21 16:56:11 Done.
95 next_sample += speex_frame_size_ * packet->channels();
96 samples_left -= speex_frame_size_ * packet->channels();
97 }
98
99 std::string* new_data = encoded_packet->add_data();
100 new_data->resize(speex_bits_nbytes(speex_bits_.get()));
101
102 // Copy the encoded data from the bits structure into the buffer.
103 int bytes_written = speex_bits_write(speex_bits_.get(),
104 string_as_array(new_data),
105 new_data->size());
106
107 // Expect that the bytes are all written.
108 DCHECK_EQ(bytes_written, static_cast<int>(new_data->size()));
109
110 // Reset the bits structure for this frame.
111 speex_bits_reset(speex_bits_.get());
112 }
113
114 // Store the leftover samples.
115 if (samples_left > 0) {
116 CHECK_LE(leftover_samples_ + samples_left,
117 speex_frame_size_ * packet->channels());
118 memcpy(raw_data_buffer_.get() + leftover_samples_,
119 next_sample,
120 packet->bytes_per_sample() * samples_left);
121 leftover_samples_ += samples_left;
122 }
123
124 return encoded_packet.Pass();
Sergey Ulanov 2012/08/20 22:50:39 Return null if there is no data in the packet (e.g
kxing 2012/08/21 16:56:11 Done.
125 }
126
127 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698