blob: 51d20c4982cbd0bec5703aea6a76e38c9f50072d [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
kwiberg087bd342017-02-10 08:15:44 -080017#include <memory>
18#include <vector>
19
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/constructor_magic.h"
kwiberg087bd342017-02-10 08:15:44 -080024
25namespace webrtc {
26
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
37 AudioDecoder() = default;
38 virtual ~AudioDecoder() = default;
39
40 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
Ivo Creusenc7f09ad2018-05-22 13:21:01 +020053 // Returns true if this packet contains DTX.
54 virtual bool IsDtxPacket() const;
55
Artem Titov0e61fdd2021-07-25 21:50:14 +020056 // Decodes this frame of audio and writes the result in `decoded`.
57 // `decoded` must be large enough to store as many samples as indicated by a
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020058 // call to Duration() . On success, returns an absl::optional containing the
kwiberg087bd342017-02-10 08:15:44 -080059 // total number of samples across all channels, as well as whether the
60 // decoder produced comfort noise or speech. On failure, returns an empty
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020061 // absl::optional. Decode may be called at most once per frame object.
62 virtual absl::optional<DecodeResult> Decode(
kwiberg087bd342017-02-10 08:15:44 -080063 rtc::ArrayView<int16_t> decoded) const = 0;
64 };
65
66 struct ParseResult {
67 ParseResult();
68 ParseResult(uint32_t timestamp,
69 int priority,
70 std::unique_ptr<EncodedAudioFrame> frame);
71 ParseResult(ParseResult&& b);
72 ~ParseResult();
73
74 ParseResult& operator=(ParseResult&& b);
75
76 // The timestamp of the frame is in samples per channel.
77 uint32_t timestamp;
78 // The relative priority of the frame compared to other frames of the same
79 // payload and the same timeframe. A higher value means a lower priority.
80 // The highest priority is zero - negative values are not allowed.
81 int priority;
82 std::unique_ptr<EncodedAudioFrame> frame;
83 };
84
85 // Let the decoder parse this payload and prepare zero or more decodable
86 // frames. Each frame must be between 10 ms and 120 ms long. The caller must
87 // ensure that the AudioDecoder object outlives any frame objects returned by
Artem Titov0e61fdd2021-07-25 21:50:14 +020088 // this call. The decoder is free to swap or move the data from the `payload`
89 // buffer. `timestamp` is the input timestamp, in samples, corresponding to
kwiberg087bd342017-02-10 08:15:44 -080090 // the start of the payload.
91 virtual std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
92 uint32_t timestamp);
93
Niels Möllerb7180c02018-12-06 13:07:11 +010094 // TODO(bugs.webrtc.org/10098): The Decode and DecodeRedundant methods are
95 // obsolete; callers should call ParsePayload instead. For now, subclasses
96 // must still implement DecodeInternal.
97
Artem Titov0e61fdd2021-07-25 21:50:14 +020098 // Decodes `encode_len` bytes from `encoded` and writes the result in
99 // `decoded`. The maximum bytes allowed to be written into `decoded` is
100 // `max_decoded_bytes`. Returns the total number of samples across all
101 // channels. If the decoder produced comfort noise, `speech_type`
kwiberg087bd342017-02-10 08:15:44 -0800102 // is set to kComfortNoise, otherwise it is kSpeech. The desired output
Artem Titov0e61fdd2021-07-25 21:50:14 +0200103 // sample rate is provided in `sample_rate_hz`, which must be valid for the
kwiberg087bd342017-02-10 08:15:44 -0800104 // codec at hand.
105 int Decode(const uint8_t* encoded,
106 size_t encoded_len,
107 int sample_rate_hz,
108 size_t max_decoded_bytes,
109 int16_t* decoded,
110 SpeechType* speech_type);
111
112 // Same as Decode(), but interfaces to the decoders redundant decode function.
113 // The default implementation simply calls the regular Decode() method.
114 int DecodeRedundant(const uint8_t* encoded,
115 size_t encoded_len,
116 int sample_rate_hz,
117 size_t max_decoded_bytes,
118 int16_t* decoded,
119 SpeechType* speech_type);
120
121 // Indicates if the decoder implements the DecodePlc method.
122 virtual bool HasDecodePlc() const;
123
124 // Calls the packet-loss concealment of the decoder to update the state after
125 // one or several lost packets. The caller has to make sure that the
Artem Titov0e61fdd2021-07-25 21:50:14 +0200126 // memory allocated in `decoded` should accommodate `num_frames` frames.
kwiberg087bd342017-02-10 08:15:44 -0800127 virtual size_t DecodePlc(size_t num_frames, int16_t* decoded);
128
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200129 // Asks the decoder to generate packet-loss concealment and append it to the
Artem Titov0e61fdd2021-07-25 21:50:14 +0200130 // end of `concealment_audio`. The concealment audio should be in
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200131 // channel-interleaved format, with as many channels as the last decoded
132 // packet produced. The implementation must produce at least
133 // requested_samples_per_channel, or nothing at all. This is a signal to the
134 // caller to conceal the loss with other means. If the implementation provides
135 // concealment samples, it is also responsible for "stitching" it together
136 // with the decoded audio on either side of the concealment.
137 // Note: The default implementation of GeneratePlc will be deleted soon. All
138 // implementations must provide their own, which can be a simple as a no-op.
Pablo Barrera Gonzálezff0e01f2021-02-10 10:38:50 +0100139 // TODO(bugs.webrtc.org/9676): Remove default implementation.
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200140 virtual void GeneratePlc(size_t requested_samples_per_channel,
141 rtc::BufferT<int16_t>* concealment_audio);
142
kwiberg087bd342017-02-10 08:15:44 -0800143 // Resets the decoder state (empty buffers etc.).
144 virtual void Reset() = 0;
145
kwiberg087bd342017-02-10 08:15:44 -0800146 // Returns the last error code from the decoder.
147 virtual int ErrorCode();
148
Artem Titov0e61fdd2021-07-25 21:50:14 +0200149 // Returns the duration in samples-per-channel of the payload in `encoded`
150 // which is `encoded_len` bytes long. Returns kNotImplemented if no duration
kwiberg087bd342017-02-10 08:15:44 -0800151 // 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
Artem Titov0e61fdd2021-07-25 21:50:14 +0200155 // `encoded` which is `encoded_len` bytes long. Returns kNotImplemented if no
kwiberg087bd342017-02-10 08:15:44 -0800156 // 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
Artem Titov0e61fdd2021-07-25 21:50:14 +0200161 // comprised of the samples in `encoded` which is `encoded_len` bytes long.
kwiberg087bd342017-02-10 08:15:44 -0800162 // 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_