audio_decoder.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  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. #ifndef API_AUDIO_CODECS_AUDIO_DECODER_H_
  11. #define API_AUDIO_CODECS_AUDIO_DECODER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/array_view.h"
  18. #include "rtc_base/buffer.h"
  19. #include "rtc_base/constructor_magic.h"
  20. namespace webrtc {
  21. class AudioDecoder {
  22. public:
  23. enum SpeechType {
  24. kSpeech = 1,
  25. kComfortNoise = 2,
  26. };
  27. // Used by PacketDuration below. Save the value -1 for errors.
  28. enum { kNotImplemented = -2 };
  29. AudioDecoder() = default;
  30. virtual ~AudioDecoder() = default;
  31. class EncodedAudioFrame {
  32. public:
  33. struct DecodeResult {
  34. size_t num_decoded_samples;
  35. SpeechType speech_type;
  36. };
  37. virtual ~EncodedAudioFrame() = default;
  38. // Returns the duration in samples-per-channel of this audio frame.
  39. // If no duration can be ascertained, returns zero.
  40. virtual size_t Duration() const = 0;
  41. // Returns true if this packet contains DTX.
  42. virtual bool IsDtxPacket() const;
  43. // Decodes this frame of audio and writes the result in |decoded|.
  44. // |decoded| must be large enough to store as many samples as indicated by a
  45. // call to Duration() . On success, returns an absl::optional containing the
  46. // total number of samples across all channels, as well as whether the
  47. // decoder produced comfort noise or speech. On failure, returns an empty
  48. // absl::optional. Decode may be called at most once per frame object.
  49. virtual absl::optional<DecodeResult> Decode(
  50. rtc::ArrayView<int16_t> decoded) const = 0;
  51. };
  52. struct ParseResult {
  53. ParseResult();
  54. ParseResult(uint32_t timestamp,
  55. int priority,
  56. std::unique_ptr<EncodedAudioFrame> frame);
  57. ParseResult(ParseResult&& b);
  58. ~ParseResult();
  59. ParseResult& operator=(ParseResult&& b);
  60. // The timestamp of the frame is in samples per channel.
  61. uint32_t timestamp;
  62. // The relative priority of the frame compared to other frames of the same
  63. // payload and the same timeframe. A higher value means a lower priority.
  64. // The highest priority is zero - negative values are not allowed.
  65. int priority;
  66. std::unique_ptr<EncodedAudioFrame> frame;
  67. };
  68. // Let the decoder parse this payload and prepare zero or more decodable
  69. // frames. Each frame must be between 10 ms and 120 ms long. The caller must
  70. // ensure that the AudioDecoder object outlives any frame objects returned by
  71. // this call. The decoder is free to swap or move the data from the |payload|
  72. // buffer. |timestamp| is the input timestamp, in samples, corresponding to
  73. // the start of the payload.
  74. virtual std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
  75. uint32_t timestamp);
  76. // TODO(bugs.webrtc.org/10098): The Decode and DecodeRedundant methods are
  77. // obsolete; callers should call ParsePayload instead. For now, subclasses
  78. // must still implement DecodeInternal.
  79. // Decodes |encode_len| bytes from |encoded| and writes the result in
  80. // |decoded|. The maximum bytes allowed to be written into |decoded| is
  81. // |max_decoded_bytes|. Returns the total number of samples across all
  82. // channels. If the decoder produced comfort noise, |speech_type|
  83. // is set to kComfortNoise, otherwise it is kSpeech. The desired output
  84. // sample rate is provided in |sample_rate_hz|, which must be valid for the
  85. // codec at hand.
  86. int Decode(const uint8_t* encoded,
  87. size_t encoded_len,
  88. int sample_rate_hz,
  89. size_t max_decoded_bytes,
  90. int16_t* decoded,
  91. SpeechType* speech_type);
  92. // Same as Decode(), but interfaces to the decoders redundant decode function.
  93. // The default implementation simply calls the regular Decode() method.
  94. int DecodeRedundant(const uint8_t* encoded,
  95. size_t encoded_len,
  96. int sample_rate_hz,
  97. size_t max_decoded_bytes,
  98. int16_t* decoded,
  99. SpeechType* speech_type);
  100. // Indicates if the decoder implements the DecodePlc method.
  101. virtual bool HasDecodePlc() const;
  102. // Calls the packet-loss concealment of the decoder to update the state after
  103. // one or several lost packets. The caller has to make sure that the
  104. // memory allocated in |decoded| should accommodate |num_frames| frames.
  105. virtual size_t DecodePlc(size_t num_frames, int16_t* decoded);
  106. // Asks the decoder to generate packet-loss concealment and append it to the
  107. // end of |concealment_audio|. The concealment audio should be in
  108. // channel-interleaved format, with as many channels as the last decoded
  109. // packet produced. The implementation must produce at least
  110. // requested_samples_per_channel, or nothing at all. This is a signal to the
  111. // caller to conceal the loss with other means. If the implementation provides
  112. // concealment samples, it is also responsible for "stitching" it together
  113. // with the decoded audio on either side of the concealment.
  114. // Note: The default implementation of GeneratePlc will be deleted soon. All
  115. // implementations must provide their own, which can be a simple as a no-op.
  116. // TODO(bugs.webrtc.org/9676): Remove default impementation.
  117. virtual void GeneratePlc(size_t requested_samples_per_channel,
  118. rtc::BufferT<int16_t>* concealment_audio);
  119. // Resets the decoder state (empty buffers etc.).
  120. virtual void Reset() = 0;
  121. // Returns the last error code from the decoder.
  122. virtual int ErrorCode();
  123. // Returns the duration in samples-per-channel of the payload in |encoded|
  124. // which is |encoded_len| bytes long. Returns kNotImplemented if no duration
  125. // estimate is available, or -1 in case of an error.
  126. virtual int PacketDuration(const uint8_t* encoded, size_t encoded_len) const;
  127. // Returns the duration in samples-per-channel of the redandant payload in
  128. // |encoded| which is |encoded_len| bytes long. Returns kNotImplemented if no
  129. // duration estimate is available, or -1 in case of an error.
  130. virtual int PacketDurationRedundant(const uint8_t* encoded,
  131. size_t encoded_len) const;
  132. // Detects whether a packet has forward error correction. The packet is
  133. // comprised of the samples in |encoded| which is |encoded_len| bytes long.
  134. // Returns true if the packet has FEC and false otherwise.
  135. virtual bool PacketHasFec(const uint8_t* encoded, size_t encoded_len) const;
  136. // Returns the actual sample rate of the decoder's output. This value may not
  137. // change during the lifetime of the decoder.
  138. virtual int SampleRateHz() const = 0;
  139. // The number of channels in the decoder's output. This value may not change
  140. // during the lifetime of the decoder.
  141. virtual size_t Channels() const = 0;
  142. protected:
  143. static SpeechType ConvertSpeechType(int16_t type);
  144. virtual int DecodeInternal(const uint8_t* encoded,
  145. size_t encoded_len,
  146. int sample_rate_hz,
  147. int16_t* decoded,
  148. SpeechType* speech_type) = 0;
  149. virtual int DecodeRedundantInternal(const uint8_t* encoded,
  150. size_t encoded_len,
  151. int sample_rate_hz,
  152. int16_t* decoded,
  153. SpeechType* speech_type);
  154. private:
  155. RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
  156. };
  157. } // namespace webrtc
  158. #endif // API_AUDIO_CODECS_AUDIO_DECODER_H_