blob: 22e44a44838c708924e91cd4307cdc2e8daa6de6 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_INTERFACE_AUDIO_DECODER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_INTERFACE_AUDIO_DECODER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <stdlib.h> // NULL
15
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000016#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org8b2058e2014-11-06 07:54:31 +000017#include "webrtc/modules/audio_coding/codecs/cng/include/webrtc_cng.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022// This is the interface class for decoders in NetEQ. Each codec type will have
23// and implementation of this class.
24class AudioDecoder {
25 public:
26 enum SpeechType {
27 kSpeech = 1,
28 kComfortNoise = 2
29 };
30
31 // Used by PacketDuration below. Save the value -1 for errors.
32 enum { kNotImplemented = -2 };
33
kwiberg@webrtc.org8b2058e2014-11-06 07:54:31 +000034 AudioDecoder() : channels_(1) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035 virtual ~AudioDecoder() {}
36
37 // Decodes |encode_len| bytes from |encoded| and writes the result in
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000038 // |decoded|. The maximum bytes allowed to be written into |decoded| is
39 // |max_decoded_bytes|. The number of samples from all channels produced is
40 // in the return value. If the decoder produced comfort noise, |speech_type|
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +000041 // is set to kComfortNoise, otherwise it is kSpeech. The desired output
42 // sample rate is provided in |sample_rate_hz|, which must be valid for the
43 // codec at hand.
44 virtual int Decode(const uint8_t* encoded,
45 size_t encoded_len,
46 int sample_rate_hz,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000047 size_t max_decoded_bytes,
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +000048 int16_t* decoded,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000049 SpeechType* speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
51 // Same as Decode(), but interfaces to the decoders redundant decode function.
52 // The default implementation simply calls the regular Decode() method.
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +000053 virtual int DecodeRedundant(const uint8_t* encoded,
54 size_t encoded_len,
55 int sample_rate_hz,
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000056 size_t max_decoded_bytes,
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +000057 int16_t* decoded,
58 SpeechType* speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
60 // Indicates if the decoder implements the DecodePlc method.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000061 virtual bool HasDecodePlc() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062
63 // Calls the packet-loss concealment of the decoder to update the state after
64 // one or several lost packets.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000065 virtual int DecodePlc(int num_frames, int16_t* decoded);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066
67 // Initializes the decoder.
68 virtual int Init() = 0;
69
70 // Notifies the decoder of an incoming packet to NetEQ.
71 virtual int IncomingPacket(const uint8_t* payload,
72 size_t payload_len,
73 uint16_t rtp_sequence_number,
74 uint32_t rtp_timestamp,
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000075 uint32_t arrival_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076
77 // Returns the last error code from the decoder.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000078 virtual int ErrorCode();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079
80 // Returns the duration in samples of the payload in |encoded| which is
81 // |encoded_len| bytes long. Returns kNotImplemented if no duration estimate
82 // is available, or -1 in case of an error.
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +000083 virtual int PacketDuration(const uint8_t* encoded, size_t encoded_len) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +000085 // Returns the duration in samples of the redandant payload in |encoded| which
86 // is |encoded_len| bytes long. Returns kNotImplemented if no duration
87 // estimate is available, or -1 in case of an error.
88 virtual int PacketDurationRedundant(const uint8_t* encoded,
89 size_t encoded_len) const;
90
91 // Detects whether a packet has forward error correction. The packet is
92 // comprised of the samples in |encoded| which is |encoded_len| bytes long.
93 // Returns true if the packet has FEC and false otherwise.
94 virtual bool PacketHasFec(const uint8_t* encoded, size_t encoded_len) const;
95
kwiberg@webrtc.org8b2058e2014-11-06 07:54:31 +000096 // If this is a CNG decoder, return the underlying CNG_dec_inst*. If this
97 // isn't a CNG decoder, don't call this method.
98 virtual CNG_dec_inst* CngDecoderInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000100 size_t channels() const { return channels_; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101
102 protected:
103 static SpeechType ConvertSpeechType(int16_t type);
104
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000105 virtual int DecodeInternal(const uint8_t* encoded,
106 size_t encoded_len,
107 int sample_rate_hz,
108 int16_t* decoded,
109 SpeechType* speech_type);
110
111 virtual int DecodeRedundantInternal(const uint8_t* encoded,
112 size_t encoded_len,
113 int sample_rate_hz,
114 int16_t* decoded,
115 SpeechType* speech_type);
116
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 size_t channels_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
121};
122
123} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000124#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_INTERFACE_AUDIO_DECODER_H_