blob: 580ddbf74ffe43807fdab2204d7cb1b92f345283 [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 Kjellander74640892015-10-29 11:31:02 +010011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_INCLUDE_AUDIO_DECODER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_INCLUDE_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"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021// This is the interface class for decoders in NetEQ. Each codec type will have
22// and implementation of this class.
23class AudioDecoder {
24 public:
25 enum SpeechType {
26 kSpeech = 1,
27 kComfortNoise = 2
28 };
29
30 // Used by PacketDuration below. Save the value -1 for errors.
31 enum { kNotImplemented = -2 };
32
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +000033 AudioDecoder() = default;
34 virtual ~AudioDecoder() = default;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
36 // Decodes |encode_len| bytes from |encoded| and writes the result in
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000037 // |decoded|. The maximum bytes allowed to be written into |decoded| is
Minyue323b1322015-05-25 13:49:37 +020038 // |max_decoded_bytes|. Returns the total number of samples across all
39 // channels. If the decoder produced comfort noise, |speech_type|
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +000040 // is set to kComfortNoise, otherwise it is kSpeech. The desired output
41 // sample rate is provided in |sample_rate_hz|, which must be valid for the
42 // codec at hand.
Peter Boströmd7b7ae82015-12-08 13:41:35 +010043 int Decode(const uint8_t* encoded,
44 size_t encoded_len,
45 int sample_rate_hz,
46 size_t max_decoded_bytes,
47 int16_t* decoded,
48 SpeechType* speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
50 // Same as Decode(), but interfaces to the decoders redundant decode function.
51 // The default implementation simply calls the regular Decode() method.
Peter Boströmd7b7ae82015-12-08 13:41:35 +010052 int DecodeRedundant(const uint8_t* encoded,
53 size_t encoded_len,
54 int sample_rate_hz,
55 size_t max_decoded_bytes,
56 int16_t* decoded,
57 SpeechType* speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
59 // Indicates if the decoder implements the DecodePlc method.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000060 virtual bool HasDecodePlc() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061
62 // Calls the packet-loss concealment of the decoder to update the state after
minyuel6d92bf52015-09-23 15:20:39 +020063 // one or several lost packets. The caller has to make sure that the
64 // memory allocated in |decoded| should accommodate |num_frames| frames.
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 virtual size_t DecodePlc(size_t num_frames, int16_t* decoded);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066
Karl Wiberg43766482015-08-27 15:22:11 +020067 // Resets the decoder state (empty buffers etc.).
68 virtual void Reset() = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069
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
Minyue323b1322015-05-25 13:49:37 +020080 // Returns the duration in samples-per-channel of the payload in |encoded|
81 // which is |encoded_len| bytes long. Returns kNotImplemented if no duration
82 // estimate 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
Minyue323b1322015-05-25 13:49:37 +020085 // Returns the duration in samples-per-channel of the redandant payload in
86 // |encoded| which is |encoded_len| bytes long. Returns kNotImplemented if no
87 // duration estimate is available, or -1 in case of an error.
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +000088 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
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +000096 virtual size_t Channels() const = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097
98 protected:
99 static SpeechType ConvertSpeechType(int16_t type);
100
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000101 virtual int DecodeInternal(const uint8_t* encoded,
102 size_t encoded_len,
103 int sample_rate_hz,
104 int16_t* decoded,
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100105 SpeechType* speech_type) = 0;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000106
107 virtual int DecodeRedundantInternal(const uint8_t* encoded,
108 size_t encoded_len,
109 int sample_rate_hz,
110 int16_t* decoded,
111 SpeechType* speech_type);
112
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 private:
henrikg3c089d72015-09-16 05:37:44 -0700114 RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115};
116
117} // namespace webrtc
Henrik Kjellander74640892015-10-29 11:31:02 +0100118#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_INCLUDE_AUDIO_DECODER_H_