blob: 8468da20f2b74669fabcd3607cbf8f47ef35c715 [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
ossua70695a2016-09-22 02:06:28 -070011#ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_DECODER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_DECODER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
ossu61a208b2016-09-20 01:38:00 -070014#include <memory>
15#include <vector>
16
17#include "webrtc/base/array_view.h"
18#include "webrtc/base/buffer.h"
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000019#include "webrtc/base/constructormagic.h"
ossu61a208b2016-09-20 01:38:00 -070020#include "webrtc/base/optional.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025// This is the interface class for decoders in NetEQ. Each codec type will have
26// and implementation of this class.
27class AudioDecoder {
28 public:
29 enum SpeechType {
30 kSpeech = 1,
31 kComfortNoise = 2
32 };
33
34 // Used by PacketDuration below. Save the value -1 for errors.
35 enum { kNotImplemented = -2 };
36
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +000037 AudioDecoder() = default;
38 virtual ~AudioDecoder() = default;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039
ossu61a208b2016-09-20 01:38:00 -070040 class EncodedAudioFrame {
41 public:
42 struct DecodeResult {
43 size_t num_decoded_samples;
44 SpeechType speech_type;
45 };
46
47 virtual ~EncodedAudioFrame() = default;
48
49 // Returns the duration in samples-per-channel of this audio frame.
50 // If no duration can be ascertained, returns zero.
51 virtual size_t Duration() const = 0;
52
53 // Decodes this frame of audio and writes the result in |decoded|.
54 // |decoded| must be large enough to store as many samples as indicated by a
55 // call to Duration() . On success, returns an rtc::Optional containing the
56 // total number of samples across all channels, as well as whether the
57 // decoder produced comfort noise or speech. On failure, returns an empty
58 // rtc::Optional. Decode may be called at most once per frame object.
59 virtual rtc::Optional<DecodeResult> Decode(
60 rtc::ArrayView<int16_t> decoded) const = 0;
61 };
62
63 struct ParseResult {
64 ParseResult();
65 ParseResult(uint32_t timestamp,
ossua70695a2016-09-22 02:06:28 -070066 int priority,
ossu61a208b2016-09-20 01:38:00 -070067 std::unique_ptr<EncodedAudioFrame> frame);
68 ParseResult(ParseResult&& b);
69 ~ParseResult();
70
71 ParseResult& operator=(ParseResult&& b);
72
73 // The timestamp of the frame is in samples per channel.
74 uint32_t timestamp;
ossua70695a2016-09-22 02:06:28 -070075 // The relative priority of the frame compared to other frames of the same
76 // payload and the same timeframe. A higher value means a lower priority.
77 // The highest priority is zero - negative values are not allowed.
78 int priority;
ossu61a208b2016-09-20 01:38:00 -070079 std::unique_ptr<EncodedAudioFrame> frame;
80 };
81
82 // Let the decoder parse this payload and prepare zero or more decodable
83 // frames. Each frame must be between 10 ms and 120 ms long. The caller must
84 // ensure that the AudioDecoder object outlives any frame objects returned by
85 // this call. The decoder is free to swap or move the data from the |payload|
86 // buffer. |timestamp| is the input timestamp, in samples, corresponding to
87 // the start of the payload.
88 virtual std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
ossua70695a2016-09-22 02:06:28 -070089 uint32_t timestamp);
ossu61a208b2016-09-20 01:38:00 -070090
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 // Decodes |encode_len| bytes from |encoded| and writes the result in
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000092 // |decoded|. The maximum bytes allowed to be written into |decoded| is
Minyue323b1322015-05-25 13:49:37 +020093 // |max_decoded_bytes|. Returns the total number of samples across all
94 // channels. If the decoder produced comfort noise, |speech_type|
henrik.lundin@webrtc.org1eda4e32015-02-25 10:02:29 +000095 // is set to kComfortNoise, otherwise it is kSpeech. The desired output
96 // sample rate is provided in |sample_rate_hz|, which must be valid for the
97 // codec at hand.
Peter Boströmd7b7ae82015-12-08 13:41:35 +010098 int Decode(const uint8_t* encoded,
99 size_t encoded_len,
100 int sample_rate_hz,
101 size_t max_decoded_bytes,
102 int16_t* decoded,
103 SpeechType* speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104
105 // Same as Decode(), but interfaces to the decoders redundant decode function.
106 // The default implementation simply calls the regular Decode() method.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100107 int DecodeRedundant(const uint8_t* encoded,
108 size_t encoded_len,
109 int sample_rate_hz,
110 size_t max_decoded_bytes,
111 int16_t* decoded,
112 SpeechType* speech_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
114 // Indicates if the decoder implements the DecodePlc method.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000115 virtual bool HasDecodePlc() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
117 // Calls the packet-loss concealment of the decoder to update the state after
minyuel6d92bf52015-09-23 15:20:39 +0200118 // one or several lost packets. The caller has to make sure that the
119 // memory allocated in |decoded| should accommodate |num_frames| frames.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700120 virtual size_t DecodePlc(size_t num_frames, int16_t* decoded);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121
Karl Wiberg43766482015-08-27 15:22:11 +0200122 // Resets the decoder state (empty buffers etc.).
123 virtual void Reset() = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124
125 // Notifies the decoder of an incoming packet to NetEQ.
126 virtual int IncomingPacket(const uint8_t* payload,
127 size_t payload_len,
128 uint16_t rtp_sequence_number,
129 uint32_t rtp_timestamp,
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000130 uint32_t arrival_timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000131
132 // Returns the last error code from the decoder.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000133 virtual int ErrorCode();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134
Minyue323b1322015-05-25 13:49:37 +0200135 // Returns the duration in samples-per-channel of the payload in |encoded|
136 // which is |encoded_len| bytes long. Returns kNotImplemented if no duration
137 // estimate is available, or -1 in case of an error.
minyue@webrtc.orga8cc3442015-02-13 14:01:54 +0000138 virtual int PacketDuration(const uint8_t* encoded, size_t encoded_len) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000139
Minyue323b1322015-05-25 13:49:37 +0200140 // Returns the duration in samples-per-channel of the redandant payload in
141 // |encoded| which is |encoded_len| bytes long. Returns kNotImplemented if no
142 // duration estimate is available, or -1 in case of an error.
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000143 virtual int PacketDurationRedundant(const uint8_t* encoded,
144 size_t encoded_len) const;
145
146 // Detects whether a packet has forward error correction. The packet is
147 // comprised of the samples in |encoded| which is |encoded_len| bytes long.
148 // Returns true if the packet has FEC and false otherwise.
149 virtual bool PacketHasFec(const uint8_t* encoded, size_t encoded_len) const;
150
kwibergf8828802016-06-02 03:19:23 -0700151 // Returns the actual sample rate of the decoder's output. This value may not
152 // change during the lifetime of the decoder.
kwiberg347d3512016-06-16 01:59:09 -0700153 virtual int SampleRateHz() const = 0;
kwiberg6c2eab32016-05-31 02:46:20 -0700154
kwibergf8828802016-06-02 03:19:23 -0700155 // The number of channels in the decoder's output. This value may not change
156 // during the lifetime of the decoder.
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000157 virtual size_t Channels() const = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158
159 protected:
160 static SpeechType ConvertSpeechType(int16_t type);
161
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000162 virtual int DecodeInternal(const uint8_t* encoded,
163 size_t encoded_len,
164 int sample_rate_hz,
165 int16_t* decoded,
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100166 SpeechType* speech_type) = 0;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000167
168 virtual int DecodeRedundantInternal(const uint8_t* encoded,
169 size_t encoded_len,
170 int sample_rate_hz,
171 int16_t* decoded,
172 SpeechType* speech_type);
173
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 private:
henrikg3c089d72015-09-16 05:37:44 -0700175 RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176};
177
178} // namespace webrtc
ossua70695a2016-09-22 02:06:28 -0700179#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_DECODER_H_