Home | History | Annotate | Download | only in codec
      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/include/speex/speex_callbacks.h"
     15 #include "third_party/speex/include/speex/speex_stereo.h"
     16 
     17 namespace remoting {
     18 
     19 namespace {
     20 
     21 // Hosts will never generate more than 100 frames in a single packet.
     22 const int kMaxFramesPerPacket = 100;
     23 
     24 }  // namespace
     25 
     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);
     31 
     32   // Create and initialize the Speex stereo state.
     33   speex_stereo_state_ = speex_stereo_state_init();
     34 
     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_;
     40 
     41   int result;
     42 
     43   // Turn on perceptual enhancer, which will make the audio sound better,
     44   // at the price of further distorting the decoded samples.
     45   int enhancer = 1;
     46   result = speex_decoder_ctl(speex_state_, SPEEX_SET_ENH, &enhancer);
     47   CHECK_EQ(result, 0);
     48 
     49   // Get the frame size, so that we know the size of output when we decode
     50   // frame by frame.
     51   result = speex_decoder_ctl(speex_state_,
     52                              SPEEX_GET_FRAME_SIZE,
     53                              &speex_frame_size_);
     54   CHECK_EQ(result, 0);
     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   CHECK_EQ(result, 0);
     62 }
     63 
     64 AudioDecoderSpeex::~AudioDecoderSpeex() {
     65   speex_stereo_state_destroy(speex_stereo_state_);
     66   speex_decoder_destroy(speex_state_);
     67   speex_bits_destroy(speex_bits_.get());
     68 }
     69 
     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>();
     78   }
     79   if (packet->data_size() > kMaxFramesPerPacket) {
     80     LOG(WARNING) << "Received an packet with too many frames.";
     81     return scoped_ptr<AudioPacket>();
     82   }
     83 
     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());
     90 
     91   std::string* decoded_data = decoded_packet->add_data();
     92   decoded_data->resize(packet->data_size() *
     93                        speex_frame_size_ *
     94                        packet->bytes_per_sample() *
     95                        packet->channels());
     96   int16* samples = reinterpret_cast<int16*>(string_as_array(decoded_data));
     97 
     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());
    103 
    104     // Decode the frame and store it in the buffer.
    105     int status = speex_decode_int(speex_state_, speex_bits_.get(), samples);
    106     if (status < 0) {
    107       LOG(ERROR) << "Error in decoding Speex data.";
    108       return scoped_ptr<AudioPacket>();
    109     }
    110     // Transform mono to stereo.
    111     speex_decode_stereo_int(samples, speex_frame_size_, speex_stereo_state_);
    112 
    113     samples += (speex_frame_size_ * packet->channels());
    114   }
    115 
    116   return decoded_packet.Pass();
    117 }
    118 
    119 }  // namespace remoting
    120