Home | History | Annotate | Download | only in interface
      1 /*
      2  *  Copyright (c) 2012 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 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_INTERFACE_AUDIO_DECODER_H_
     12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_INTERFACE_AUDIO_DECODER_H_
     13 
     14 #include <stdlib.h>  // NULL
     15 
     16 #include "webrtc/base/constructormagic.h"
     17 #include "webrtc/typedefs.h"
     18 
     19 namespace webrtc {
     20 
     21 enum NetEqDecoder {
     22   kDecoderPCMu,
     23   kDecoderPCMa,
     24   kDecoderPCMu_2ch,
     25   kDecoderPCMa_2ch,
     26   kDecoderILBC,
     27   kDecoderISAC,
     28   kDecoderISACswb,
     29   kDecoderISACfb,
     30   kDecoderPCM16B,
     31   kDecoderPCM16Bwb,
     32   kDecoderPCM16Bswb32kHz,
     33   kDecoderPCM16Bswb48kHz,
     34   kDecoderPCM16B_2ch,
     35   kDecoderPCM16Bwb_2ch,
     36   kDecoderPCM16Bswb32kHz_2ch,
     37   kDecoderPCM16Bswb48kHz_2ch,
     38   kDecoderPCM16B_5ch,
     39   kDecoderG722,
     40   kDecoderG722_2ch,
     41   kDecoderRED,
     42   kDecoderAVT,
     43   kDecoderCNGnb,
     44   kDecoderCNGwb,
     45   kDecoderCNGswb32kHz,
     46   kDecoderCNGswb48kHz,
     47   kDecoderArbitrary,
     48   kDecoderOpus,
     49   kDecoderOpus_2ch,
     50   kDecoderCELT_32,
     51   kDecoderCELT_32_2ch,
     52 };
     53 
     54 // This is the interface class for decoders in NetEQ. Each codec type will have
     55 // and implementation of this class.
     56 class AudioDecoder {
     57  public:
     58   enum SpeechType {
     59     kSpeech = 1,
     60     kComfortNoise = 2
     61   };
     62 
     63   // Used by PacketDuration below. Save the value -1 for errors.
     64   enum { kNotImplemented = -2 };
     65 
     66   explicit AudioDecoder(enum NetEqDecoder type)
     67     : codec_type_(type),
     68       channels_(1),
     69       state_(NULL) {
     70   }
     71 
     72   virtual ~AudioDecoder() {}
     73 
     74   // Decodes |encode_len| bytes from |encoded| and writes the result in
     75   // |decoded|. The number of samples from all channels produced is in
     76   // the return value. If the decoder produced comfort noise, |speech_type|
     77   // is set to kComfortNoise, otherwise it is kSpeech.
     78   virtual int Decode(const uint8_t* encoded, size_t encoded_len,
     79                      int16_t* decoded, SpeechType* speech_type) = 0;
     80 
     81   // Same as Decode(), but interfaces to the decoders redundant decode function.
     82   // The default implementation simply calls the regular Decode() method.
     83   virtual int DecodeRedundant(const uint8_t* encoded, size_t encoded_len,
     84                               int16_t* decoded, SpeechType* speech_type);
     85 
     86   // Indicates if the decoder implements the DecodePlc method.
     87   virtual bool HasDecodePlc() const;
     88 
     89   // Calls the packet-loss concealment of the decoder to update the state after
     90   // one or several lost packets.
     91   virtual int DecodePlc(int num_frames, int16_t* decoded);
     92 
     93   // Initializes the decoder.
     94   virtual int Init() = 0;
     95 
     96   // Notifies the decoder of an incoming packet to NetEQ.
     97   virtual int IncomingPacket(const uint8_t* payload,
     98                              size_t payload_len,
     99                              uint16_t rtp_sequence_number,
    100                              uint32_t rtp_timestamp,
    101                              uint32_t arrival_timestamp);
    102 
    103   // Returns the last error code from the decoder.
    104   virtual int ErrorCode();
    105 
    106   // Returns the duration in samples of the payload in |encoded| which is
    107   // |encoded_len| bytes long. Returns kNotImplemented if no duration estimate
    108   // is available, or -1 in case of an error.
    109   virtual int PacketDuration(const uint8_t* encoded, size_t encoded_len);
    110 
    111   // Returns the duration in samples of the redandant payload in |encoded| which
    112   // is |encoded_len| bytes long. Returns kNotImplemented if no duration
    113   // estimate is available, or -1 in case of an error.
    114   virtual int PacketDurationRedundant(const uint8_t* encoded,
    115                                       size_t encoded_len) const;
    116 
    117   // Detects whether a packet has forward error correction. The packet is
    118   // comprised of the samples in |encoded| which is |encoded_len| bytes long.
    119   // Returns true if the packet has FEC and false otherwise.
    120   virtual bool PacketHasFec(const uint8_t* encoded, size_t encoded_len) const;
    121 
    122   virtual NetEqDecoder codec_type() const;
    123 
    124   // Returns the underlying decoder state.
    125   void* state() { return state_; }
    126 
    127   // Returns true if |codec_type| is supported.
    128   static bool CodecSupported(NetEqDecoder codec_type);
    129 
    130   // Returns the sample rate for |codec_type|.
    131   static int CodecSampleRateHz(NetEqDecoder codec_type);
    132 
    133   // Creates an AudioDecoder object of type |codec_type|. Returns NULL for
    134   // for unsupported codecs, and when creating an AudioDecoder is not
    135   // applicable (e.g., for RED and DTMF/AVT types).
    136   static AudioDecoder* CreateAudioDecoder(NetEqDecoder codec_type);
    137 
    138   size_t channels() const { return channels_; }
    139 
    140  protected:
    141   static SpeechType ConvertSpeechType(int16_t type);
    142 
    143   enum NetEqDecoder codec_type_;
    144   size_t channels_;
    145   void* state_;
    146 
    147  private:
    148   DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
    149 };
    150 
    151 }  // namespace webrtc
    152 #endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_INTERFACE_AUDIO_DECODER_H_
    153