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_encoder_speex.h"
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/include/speex/speex_callbacks.h"
15 #include "third_party/speex/include/speex/speex_stereo.h"
18 // A quality of 8 in wide band mode corresponds to 27,800 bits per second.
19 const int kSpeexHighQuality
= 8;
20 const int kEncodedDataBufferSize
= 0xFF;
25 AudioEncoderSpeex::AudioEncoderSpeex()
26 : leftover_frames_(0) {
27 // Create and initialize the Speex structures.
28 speex_bits_
.reset(new SpeexBits());
29 speex_bits_init(speex_bits_
.get());
30 speex_state_
= speex_encoder_init(&speex_wb_mode
);
32 // Set the encoding quality.
33 int quality
= kSpeexHighQuality
;
34 speex_encoder_ctl(speex_state_
, SPEEX_SET_QUALITY
, &quality
);
36 // Get the frame size and construct the input buffer accordingly.
37 int result
= speex_encoder_ctl(speex_state_
,
42 leftover_buffer_
.reset(
43 new int16
[speex_frame_size_
* AudioPacket::CHANNELS_STEREO
]);
46 AudioEncoderSpeex::~AudioEncoderSpeex() {
47 speex_encoder_destroy(speex_state_
);
48 speex_bits_destroy(speex_bits_
.get());
51 scoped_ptr
<AudioPacket
> AudioEncoderSpeex::Encode(
52 scoped_ptr
<AudioPacket
> packet
) {
53 DCHECK_EQ(AudioPacket::ENCODING_RAW
, packet
->encoding());
54 DCHECK_EQ(1, packet
->data_size());
55 DCHECK_EQ(AudioPacket::BYTES_PER_SAMPLE_2
, packet
->bytes_per_sample());
56 DCHECK_NE(AudioPacket::SAMPLING_RATE_INVALID
, packet
->sampling_rate());
57 DCHECK_EQ(AudioPacket::CHANNELS_STEREO
, packet
->channels());
60 packet
->data(0).size() / packet
->bytes_per_sample() / packet
->channels();
61 const int16
* next_sample
=
62 reinterpret_cast<const int16
*>(packet
->data(0).data());
64 // Create a new packet of encoded data.
65 scoped_ptr
<AudioPacket
> encoded_packet(new AudioPacket());
66 encoded_packet
->set_encoding(AudioPacket::ENCODING_SPEEX
);
67 encoded_packet
->set_sampling_rate(packet
->sampling_rate());
68 encoded_packet
->set_bytes_per_sample(packet
->bytes_per_sample());
69 encoded_packet
->set_channels(packet
->channels());
71 while (leftover_frames_
+ frames_left
>= speex_frame_size_
) {
72 int16
* unencoded_buffer
= NULL
;
73 int frames_consumed
= 0;
75 if (leftover_frames_
> 0) {
76 unencoded_buffer
= leftover_buffer_
.get();
77 frames_consumed
= speex_frame_size_
- leftover_frames_
;
79 memcpy(leftover_buffer_
.get() + leftover_frames_
* packet
->channels(),
81 frames_consumed
* packet
->bytes_per_sample() * packet
->channels());
85 unencoded_buffer
= const_cast<int16
*>(next_sample
);
86 frames_consumed
= speex_frame_size_
;
89 // Transform stereo to mono.
90 speex_encode_stereo_int(unencoded_buffer
,
94 // Encode the frame, treating all samples as integers.
95 speex_encode_int(speex_state_
,
99 next_sample
+= frames_consumed
* packet
->channels();
100 frames_left
-= frames_consumed
;
102 std::string
* new_data
= encoded_packet
->add_data();
103 new_data
->resize(speex_bits_nbytes(speex_bits_
.get()));
105 // Copy the encoded data from the bits structure into the buffer.
106 int bytes_written
= speex_bits_write(speex_bits_
.get(),
107 string_as_array(new_data
),
110 // Expect that the bytes are all written.
111 DCHECK_EQ(bytes_written
, static_cast<int>(new_data
->size()));
113 // Reset the bits structure for this frame.
114 speex_bits_reset(speex_bits_
.get());
117 // Store the leftover samples.
118 if (frames_left
> 0) {
119 CHECK_LE(leftover_frames_
+ frames_left
, speex_frame_size_
);
120 memcpy(leftover_buffer_
.get() + leftover_frames_
* packet
->channels(),
122 frames_left
* packet
->bytes_per_sample() * packet
->channels());
123 leftover_frames_
+= frames_left
;
126 // Return NULL if there's nothing in the packet.
127 if (encoded_packet
->data_size() == 0)
128 return scoped_ptr
<AudioPacket
>();
130 return encoded_packet
.Pass();
133 } // namespace remoting