audio_decoder_config.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_AUDIO_DECODER_CONFIG_H_
  5. #define MEDIA_BASE_AUDIO_DECODER_CONFIG_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/time/time.h"
  10. #include "media/base/audio_codecs.h"
  11. #include "media/base/channel_layout.h"
  12. #include "media/base/encryption_scheme.h"
  13. #include "media/base/media_export.h"
  14. #include "media/base/sample_format.h"
  15. namespace media {
  16. // TODO(dalecurtis): FFmpeg API uses |bytes_per_channel| instead of
  17. // |bits_per_channel|, we should switch over since bits are generally confusing
  18. // to work with.
  19. class MEDIA_EXPORT AudioDecoderConfig {
  20. public:
  21. // Constructs an uninitialized object. Clients should call Initialize() with
  22. // appropriate values before using.
  23. AudioDecoderConfig();
  24. // Constructs an initialized object.
  25. AudioDecoderConfig(AudioCodec codec,
  26. SampleFormat sample_format,
  27. ChannelLayout channel_layout,
  28. int samples_per_second,
  29. const std::vector<uint8_t>& extra_data,
  30. EncryptionScheme encryption_scheme);
  31. AudioDecoderConfig(const AudioDecoderConfig& other);
  32. ~AudioDecoderConfig();
  33. // Resets the internal state of this object. |codec_delay| is in frames.
  34. void Initialize(AudioCodec codec,
  35. SampleFormat sample_format,
  36. ChannelLayout channel_layout,
  37. int samples_per_second,
  38. const std::vector<uint8_t>& extra_data,
  39. EncryptionScheme encryption_scheme,
  40. base::TimeDelta seek_preroll,
  41. int codec_delay);
  42. // Returns true if this object has appropriate configuration values, false
  43. // otherwise.
  44. bool IsValidConfig() const;
  45. // Returns true if all fields in |config| match this config.
  46. // Note: The contents of |extra_data_| are compared not the raw pointers.
  47. bool Matches(const AudioDecoderConfig& config) const;
  48. // Returns a human-readable string describing |*this|.
  49. std::string AsHumanReadableString() const;
  50. // Sets the number of channels if |channel_layout_| is CHANNEL_LAYOUT_DISCRETE
  51. void SetChannelsForDiscrete(int channels);
  52. AudioCodec codec() const { return codec_; }
  53. int bits_per_channel() const { return bytes_per_channel_ * 8; }
  54. int bytes_per_channel() const { return bytes_per_channel_; }
  55. ChannelLayout channel_layout() const { return channel_layout_; }
  56. int channels() const { return channels_; }
  57. int samples_per_second() const { return samples_per_second_; }
  58. SampleFormat sample_format() const { return sample_format_; }
  59. int bytes_per_frame() const { return bytes_per_frame_; }
  60. base::TimeDelta seek_preroll() const { return seek_preroll_; }
  61. int codec_delay() const { return codec_delay_; }
  62. // Optional byte data required to initialize audio decoders such as Vorbis
  63. // codebooks or AAC AudioSpecificConfig.
  64. const std::vector<uint8_t>& extra_data() const { return extra_data_; }
  65. // Whether the audio stream is potentially encrypted.
  66. // Note that in a potentially encrypted audio stream, individual buffers
  67. // can be encrypted or not encrypted.
  68. bool is_encrypted() const {
  69. return encryption_scheme_ != EncryptionScheme::kUnencrypted;
  70. }
  71. // Encryption scheme used for encrypted buffers.
  72. EncryptionScheme encryption_scheme() const { return encryption_scheme_; }
  73. // Sets the config to be encrypted or not encrypted manually. This can be
  74. // useful for decryptors that decrypts an encrypted stream to a clear stream.
  75. void SetIsEncrypted(bool is_encrypted);
  76. // Optionally set if the AudioCodec has a profile which may preclude certain
  77. // decoders from having support.
  78. void set_profile(AudioCodecProfile profile) { profile_ = profile; }
  79. AudioCodecProfile profile() const { return profile_; }
  80. bool should_discard_decoder_delay() const {
  81. return should_discard_decoder_delay_;
  82. }
  83. void disable_discard_decoder_delay() {
  84. should_discard_decoder_delay_ = false;
  85. }
  86. // Optionally set by renderer to provide hardware layout when playback
  87. // starts. Intentionally not part of IsValid(). Layout is not updated for
  88. // device changes - use with care!
  89. void set_target_output_channel_layout(ChannelLayout output_layout) {
  90. target_output_channel_layout_ = output_layout;
  91. }
  92. ChannelLayout target_output_channel_layout() const {
  93. return target_output_channel_layout_;
  94. }
  95. // Optionally set by renderer to signal desired bitstream-passthru format.
  96. void set_target_output_sample_format(SampleFormat sample_format) {
  97. target_output_sample_format_ = sample_format;
  98. }
  99. SampleFormat target_output_sample_format() const {
  100. return target_output_sample_format_;
  101. }
  102. void set_aac_extra_data(std::vector<uint8_t> aac_extra_data) {
  103. aac_extra_data_ = std::move(aac_extra_data);
  104. }
  105. const std::vector<uint8_t>& aac_extra_data() const { return aac_extra_data_; }
  106. private:
  107. // WARNING: When modifying or adding any parameters, update the following:
  108. // - AudioDecoderConfig::AsHumanReadableString()
  109. // - AudioDecoderConfig::Matches()
  110. // - media::mojom::AudioDecoderConfig
  111. // - audio_decoder_config_mojom_traits.{h|cc}
  112. // - audio_decoder_config_mojom_traits_unittest.cc
  113. // Mandatory parameters passed in constructor:
  114. AudioCodec codec_ = AudioCodec::kUnknown;
  115. SampleFormat sample_format_ = kUnknownSampleFormat;
  116. ChannelLayout channel_layout_ = CHANNEL_LAYOUT_UNSUPPORTED;
  117. int samples_per_second_ = 0;
  118. std::vector<uint8_t> extra_data_;
  119. EncryptionScheme encryption_scheme_ = EncryptionScheme::kUnencrypted;
  120. // The duration of data that the decoder must decode before the decoded data
  121. // is valid.
  122. base::TimeDelta seek_preroll_;
  123. // The number of frames the decoder should discard before returning decoded
  124. // data. Can include both decoder delay and padding added during encoding.
  125. int codec_delay_ = 0;
  126. // Optional parameters that can be set later:
  127. AudioCodecProfile profile_ = AudioCodecProfile::kUnknown;
  128. // Layout of the output hardware. Optionally set. See setter comments.
  129. ChannelLayout target_output_channel_layout_ = CHANNEL_LAYOUT_NONE;
  130. // Desired output format of bitstream. Optionally set. See setter comments.
  131. SampleFormat target_output_sample_format_ = kUnknownSampleFormat;
  132. // This is a hack for backward compatibility. For AAC, to preserve existing
  133. // behavior, we set `aac_extra_data_` on all platforms but only set
  134. // `extra_data` on Android.
  135. // TODO(crbug.com/1250841): Remove this after we land a long term fix.
  136. std::vector<uint8_t> aac_extra_data_;
  137. // Indicates if a decoder should implicitly discard decoder delay without it
  138. // being explicitly marked in discard padding.
  139. bool should_discard_decoder_delay_ = true;
  140. // Derived values from mandatory and optional parameters above:
  141. int bytes_per_channel_ = 0;
  142. int bytes_per_frame_ = 0;
  143. // Count of channels. By default derived from `channel_layout_`, but can also
  144. // be manually set in `SetChannelsForDiscrete()`;
  145. int channels_ = 0;
  146. // Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
  147. // generated copy constructor and assignment operator. Since the extra data is
  148. // typically small, the performance impact is minimal.
  149. };
  150. } // namespace media
  151. #endif // MEDIA_BASE_AUDIO_DECODER_CONFIG_H_