blob: b01a66a064a44ebf89e5e87891c9bd13bdda9894 [file] [log] [blame]
kwiberg087bd342017-02-10 08:15:44 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_AUDIO_CODECS_AUDIO_DECODER_H_
12#define API_AUDIO_CODECS_AUDIO_DECODER_H_
kwiberg087bd342017-02-10 08:15:44 -080013
14#include <memory>
15#include <vector>
16
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020017#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/buffer.h"
20#include "rtc_base/constructormagic.h"
kwiberg087bd342017-02-10 08:15:44 -080021
22namespace webrtc {
23
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
34 AudioDecoder() = default;
35 virtual ~AudioDecoder() = default;
36
37 class EncodedAudioFrame {
38 public:
39 struct DecodeResult {
40 size_t num_decoded_samples;
41 SpeechType speech_type;
42 };
43
44 virtual ~EncodedAudioFrame() = default;
45
46 // Returns the duration in samples-per-channel of this audio frame.
47 // If no duration can be ascertained, returns zero.
48 virtual size_t Duration() const = 0;
49
Ivo Creusenc7f09ad2018-05-22 13:21:01 +020050 // Returns true if this packet contains DTX.
51 virtual bool IsDtxPacket() const;
52
kwiberg087bd342017-02-10 08:15:44 -080053 // 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
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020055 // call to Duration() . On success, returns an absl::optional containing the
kwiberg087bd342017-02-10 08:15:44 -080056 // total number of samples across all channels, as well as whether the
57 // decoder produced comfort noise or speech. On failure, returns an empty
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020058 // absl::optional. Decode may be called at most once per frame object.
59 virtual absl::optional<DecodeResult> Decode(
kwiberg087bd342017-02-10 08:15:44 -080060 rtc::ArrayView<int16_t> decoded) const = 0;
61 };
62
63 struct ParseResult {
64 ParseResult();
65 ParseResult(uint32_t timestamp,
66 int priority,
67 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;
75 // 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;
79 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,
89 uint32_t timestamp);
90
91 // Decodes |encode_len| bytes from |encoded| and writes the result in
92 // |decoded|. The maximum bytes allowed to be written into |decoded| is
93 // |max_decoded_bytes|. Returns the total number of samples across all
94 // channels. If the decoder produced comfort noise, |speech_type|
95 // 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.
98 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);
104
105 // Same as Decode(), but interfaces to the decoders redundant decode function.
106 // The default implementation simply calls the regular Decode() method.
107 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);
113
114 // Indicates if the decoder implements the DecodePlc method.
115 virtual bool HasDecodePlc() const;
116
117 // Calls the packet-loss concealment of the decoder to update the state after
118 // one or several lost packets. The caller has to make sure that the
119 // memory allocated in |decoded| should accommodate |num_frames| frames.
120 virtual size_t DecodePlc(size_t num_frames, int16_t* decoded);
121
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200122 // Asks the decoder to generate packet-loss concealment and append it to the
123 // end of |concealment_audio|. The concealment audio should be in
124 // channel-interleaved format, with as many channels as the last decoded
125 // packet produced. The implementation must produce at least
126 // requested_samples_per_channel, or nothing at all. This is a signal to the
127 // caller to conceal the loss with other means. If the implementation provides
128 // concealment samples, it is also responsible for "stitching" it together
129 // with the decoded audio on either side of the concealment.
130 // Note: The default implementation of GeneratePlc will be deleted soon. All
131 // implementations must provide their own, which can be a simple as a no-op.
132 // TODO(bugs.webrtc.org/9676): Remove default impementation.
133 virtual void GeneratePlc(size_t requested_samples_per_channel,
134 rtc::BufferT<int16_t>* concealment_audio);
135
kwiberg087bd342017-02-10 08:15:44 -0800136 // Resets the decoder state (empty buffers etc.).
137 virtual void Reset() = 0;
138
139 // Notifies the decoder of an incoming packet to NetEQ.
140 virtual int IncomingPacket(const uint8_t* payload,
141 size_t payload_len,
142 uint16_t rtp_sequence_number,
143 uint32_t rtp_timestamp,
144 uint32_t arrival_timestamp);
145
146 // Returns the last error code from the decoder.
147 virtual int ErrorCode();
148
149 // Returns the duration in samples-per-channel of the payload in |encoded|
150 // which is |encoded_len| bytes long. Returns kNotImplemented if no duration
151 // estimate is available, or -1 in case of an error.
152 virtual int PacketDuration(const uint8_t* encoded, size_t encoded_len) const;
153
154 // Returns the duration in samples-per-channel of the redandant payload in
155 // |encoded| which is |encoded_len| bytes long. Returns kNotImplemented if no
156 // duration estimate is available, or -1 in case of an error.
157 virtual int PacketDurationRedundant(const uint8_t* encoded,
158 size_t encoded_len) const;
159
160 // Detects whether a packet has forward error correction. The packet is
161 // comprised of the samples in |encoded| which is |encoded_len| bytes long.
162 // Returns true if the packet has FEC and false otherwise.
163 virtual bool PacketHasFec(const uint8_t* encoded, size_t encoded_len) const;
164
165 // Returns the actual sample rate of the decoder's output. This value may not
166 // change during the lifetime of the decoder.
167 virtual int SampleRateHz() const = 0;
168
169 // The number of channels in the decoder's output. This value may not change
170 // during the lifetime of the decoder.
171 virtual size_t Channels() const = 0;
172
173 protected:
174 static SpeechType ConvertSpeechType(int16_t type);
175
176 virtual int DecodeInternal(const uint8_t* encoded,
177 size_t encoded_len,
178 int sample_rate_hz,
179 int16_t* decoded,
180 SpeechType* speech_type) = 0;
181
182 virtual int DecodeRedundantInternal(const uint8_t* encoded,
183 size_t encoded_len,
184 int sample_rate_hz,
185 int16_t* decoded,
186 SpeechType* speech_type);
187
188 private:
189 RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
190};
191
192} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200193#endif // API_AUDIO_CODECS_AUDIO_DECODER_H_