Home | History | Annotate | Download | only in opus
      1 /*
      2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/modules/audio_coding/codecs/opus/audio_decoder_opus.h"
     12 
     13 #include "webrtc/base/checks.h"
     14 
     15 namespace webrtc {
     16 
     17 AudioDecoderOpus::AudioDecoderOpus(size_t num_channels)
     18     : channels_(num_channels) {
     19   RTC_DCHECK(num_channels == 1 || num_channels == 2);
     20   WebRtcOpus_DecoderCreate(&dec_state_, channels_);
     21   WebRtcOpus_DecoderInit(dec_state_);
     22 }
     23 
     24 AudioDecoderOpus::~AudioDecoderOpus() {
     25   WebRtcOpus_DecoderFree(dec_state_);
     26 }
     27 
     28 int AudioDecoderOpus::DecodeInternal(const uint8_t* encoded,
     29                                      size_t encoded_len,
     30                                      int sample_rate_hz,
     31                                      int16_t* decoded,
     32                                      SpeechType* speech_type) {
     33   RTC_DCHECK_EQ(sample_rate_hz, 48000);
     34   int16_t temp_type = 1;  // Default is speech.
     35   int ret =
     36       WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type);
     37   if (ret > 0)
     38     ret *= static_cast<int>(channels_);  // Return total number of samples.
     39   *speech_type = ConvertSpeechType(temp_type);
     40   return ret;
     41 }
     42 
     43 int AudioDecoderOpus::DecodeRedundantInternal(const uint8_t* encoded,
     44                                               size_t encoded_len,
     45                                               int sample_rate_hz,
     46                                               int16_t* decoded,
     47                                               SpeechType* speech_type) {
     48   if (!PacketHasFec(encoded, encoded_len)) {
     49     // This packet is a RED packet.
     50     return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
     51                           speech_type);
     52   }
     53 
     54   RTC_DCHECK_EQ(sample_rate_hz, 48000);
     55   int16_t temp_type = 1;  // Default is speech.
     56   int ret = WebRtcOpus_DecodeFec(dec_state_, encoded, encoded_len, decoded,
     57                                  &temp_type);
     58   if (ret > 0)
     59     ret *= static_cast<int>(channels_);  // Return total number of samples.
     60   *speech_type = ConvertSpeechType(temp_type);
     61   return ret;
     62 }
     63 
     64 void AudioDecoderOpus::Reset() {
     65   WebRtcOpus_DecoderInit(dec_state_);
     66 }
     67 
     68 int AudioDecoderOpus::PacketDuration(const uint8_t* encoded,
     69                                      size_t encoded_len) const {
     70   return WebRtcOpus_DurationEst(dec_state_, encoded, encoded_len);
     71 }
     72 
     73 int AudioDecoderOpus::PacketDurationRedundant(const uint8_t* encoded,
     74                                               size_t encoded_len) const {
     75   if (!PacketHasFec(encoded, encoded_len)) {
     76     // This packet is a RED packet.
     77     return PacketDuration(encoded, encoded_len);
     78   }
     79 
     80   return WebRtcOpus_FecDurationEst(encoded, encoded_len);
     81 }
     82 
     83 bool AudioDecoderOpus::PacketHasFec(const uint8_t* encoded,
     84                                     size_t encoded_len) const {
     85   int fec;
     86   fec = WebRtcOpus_PacketHasFec(encoded, encoded_len);
     87   return (fec == 1);
     88 }
     89 
     90 size_t AudioDecoderOpus::Channels() const {
     91   return channels_;
     92 }
     93 
     94 }  // namespace webrtc
     95