decoder_buffer.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_DECODER_BUFFER_H_
  5. #define MEDIA_BASE_DECODER_BUFFER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include "base/check.h"
  12. #include "base/memory/read_only_shared_memory_region.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/shared_memory_mapping.h"
  15. #include "base/memory/unsafe_shared_memory_region.h"
  16. #include "base/time/time.h"
  17. #include "build/build_config.h"
  18. #include "media/base/decrypt_config.h"
  19. #include "media/base/media_export.h"
  20. #include "media/base/timestamp_constants.h"
  21. namespace media {
  22. // A specialized buffer for interfacing with audio / video decoders.
  23. //
  24. // Also includes decoder specific functionality for decryption.
  25. //
  26. // NOTE: It is illegal to call any method when end_of_stream() is true.
  27. class MEDIA_EXPORT DecoderBuffer
  28. : public base::RefCountedThreadSafe<DecoderBuffer> {
  29. public:
  30. enum {
  31. kPaddingSize = 64,
  32. #if defined(ARCH_CPU_ARM_FAMILY)
  33. kAlignmentSize = 16
  34. #else
  35. kAlignmentSize = 32
  36. #endif
  37. };
  38. using DiscardPadding = std::pair<base::TimeDelta, base::TimeDelta>;
  39. struct MEDIA_EXPORT TimeInfo {
  40. TimeInfo();
  41. ~TimeInfo();
  42. TimeInfo(const TimeInfo&);
  43. TimeInfo& operator=(const TimeInfo&);
  44. // Presentation time of the frame.
  45. base::TimeDelta timestamp;
  46. // Presentation duration of the frame.
  47. base::TimeDelta duration;
  48. // Duration of (audio) samples from the beginning and end of this frame
  49. // which should be discarded after decoding. A value of kInfiniteDuration
  50. // for the first value indicates the entire frame should be discarded; the
  51. // second value must be base::TimeDelta() in this case.
  52. DiscardPadding discard_padding;
  53. };
  54. // Allocates buffer with |size| >= 0. |is_key_frame_| will default to false.
  55. explicit DecoderBuffer(size_t size);
  56. DecoderBuffer(const DecoderBuffer&) = delete;
  57. DecoderBuffer& operator=(const DecoderBuffer&) = delete;
  58. // Create a DecoderBuffer whose |data_| is copied from |data|. |data| must not
  59. // be NULL and |size| >= 0. The buffer's |is_key_frame_| will default to
  60. // false.
  61. static scoped_refptr<DecoderBuffer> CopyFrom(const uint8_t* data,
  62. size_t size);
  63. // Create a DecoderBuffer whose |data_| is copied from |data| and |side_data_|
  64. // is copied from |side_data|. Data pointers must not be NULL and sizes must
  65. // be >= 0. The buffer's |is_key_frame_| will default to false.
  66. static scoped_refptr<DecoderBuffer> CopyFrom(const uint8_t* data,
  67. size_t size,
  68. const uint8_t* side_data,
  69. size_t side_data_size);
  70. // Create a DecoderBuffer where data() of |size| bytes resides within the heap
  71. // as byte array. The buffer's |is_key_frame_| will default to false.
  72. //
  73. // Ownership of |data| is transferred to the buffer.
  74. static scoped_refptr<DecoderBuffer> FromArray(std::unique_ptr<uint8_t[]> data,
  75. size_t size);
  76. // Create a DecoderBuffer where data() of |size| bytes resides within the
  77. // memory referred to by |region| at non-negative offset |offset|. The
  78. // buffer's |is_key_frame_| will default to false.
  79. //
  80. // The shared memory will be mapped read-only.
  81. //
  82. // If mapping fails, nullptr will be returned.
  83. static scoped_refptr<DecoderBuffer> FromSharedMemoryRegion(
  84. base::UnsafeSharedMemoryRegion region,
  85. uint64_t offset,
  86. size_t size);
  87. // Create a DecoderBuffer where data() of |size| bytes resides within the
  88. // ReadOnlySharedMemoryRegion referred to by |mapping| at non-negative offset
  89. // |offset|. The buffer's |is_key_frame_| will default to false.
  90. //
  91. // Ownership of |region| is transferred to the buffer.
  92. static scoped_refptr<DecoderBuffer> FromSharedMemoryRegion(
  93. base::ReadOnlySharedMemoryRegion region,
  94. uint64_t offset,
  95. size_t size);
  96. // Create a DecoderBuffer indicating we've reached end of stream.
  97. //
  98. // Calling any method other than end_of_stream() on the resulting buffer
  99. // is disallowed.
  100. static scoped_refptr<DecoderBuffer> CreateEOSBuffer();
  101. const TimeInfo& time_info() const {
  102. DCHECK(!end_of_stream());
  103. return time_info_;
  104. }
  105. base::TimeDelta timestamp() const {
  106. DCHECK(!end_of_stream());
  107. return time_info_.timestamp;
  108. }
  109. // TODO(dalecurtis): This should be renamed at some point, but to avoid a yak
  110. // shave keep as a virtual with hacker_style() for now.
  111. virtual void set_timestamp(base::TimeDelta timestamp);
  112. base::TimeDelta duration() const {
  113. DCHECK(!end_of_stream());
  114. return time_info_.duration;
  115. }
  116. void set_duration(base::TimeDelta duration) {
  117. DCHECK(!end_of_stream());
  118. DCHECK(duration == kNoTimestamp ||
  119. (duration >= base::TimeDelta() && duration != kInfiniteDuration))
  120. << duration.InSecondsF();
  121. time_info_.duration = duration;
  122. }
  123. const uint8_t* data() const {
  124. DCHECK(!end_of_stream());
  125. if (read_only_mapping_.IsValid())
  126. return read_only_mapping_.GetMemoryAs<const uint8_t>();
  127. if (writable_mapping_.IsValid())
  128. return writable_mapping_.GetMemoryAs<const uint8_t>();
  129. return data_.get();
  130. }
  131. // TODO(sandersd): Remove writable_data(). https://crbug.com/834088
  132. uint8_t* writable_data() const {
  133. DCHECK(!end_of_stream());
  134. DCHECK(!read_only_mapping_.IsValid());
  135. DCHECK(!writable_mapping_.IsValid());
  136. return data_.get();
  137. }
  138. size_t data_size() const {
  139. DCHECK(!end_of_stream());
  140. return size_;
  141. }
  142. const uint8_t* side_data() const {
  143. DCHECK(!end_of_stream());
  144. return side_data_.get();
  145. }
  146. size_t side_data_size() const {
  147. DCHECK(!end_of_stream());
  148. return side_data_size_;
  149. }
  150. const DiscardPadding& discard_padding() const {
  151. DCHECK(!end_of_stream());
  152. return time_info_.discard_padding;
  153. }
  154. void set_discard_padding(const DiscardPadding& discard_padding) {
  155. DCHECK(!end_of_stream());
  156. time_info_.discard_padding = discard_padding;
  157. }
  158. // Returns DecryptConfig associated with |this|. Returns null iff |this| is
  159. // not encrypted.
  160. const DecryptConfig* decrypt_config() const {
  161. DCHECK(!end_of_stream());
  162. return decrypt_config_.get();
  163. }
  164. void set_decrypt_config(std::unique_ptr<DecryptConfig> decrypt_config) {
  165. DCHECK(!end_of_stream());
  166. decrypt_config_ = std::move(decrypt_config);
  167. }
  168. // If there's no data in this buffer, it represents end of stream.
  169. bool end_of_stream() const {
  170. return !read_only_mapping_.IsValid() && !writable_mapping_.IsValid() &&
  171. !data_;
  172. }
  173. bool is_key_frame() const {
  174. DCHECK(!end_of_stream());
  175. return is_key_frame_;
  176. }
  177. void set_is_key_frame(bool is_key_frame) {
  178. DCHECK(!end_of_stream());
  179. is_key_frame_ = is_key_frame;
  180. }
  181. // Returns true if all fields in |buffer| matches this buffer
  182. // including |data_| and |side_data_|.
  183. bool MatchesForTesting(const DecoderBuffer& buffer) const;
  184. // As above, except that |data_| and |side_data_| are not compared.
  185. bool MatchesMetadataForTesting(const DecoderBuffer& buffer) const;
  186. // Returns a human-readable string describing |*this|.
  187. std::string AsHumanReadableString(bool verbose = false) const;
  188. // Replaces any existing side data with data copied from |side_data|.
  189. void CopySideDataFrom(const uint8_t* side_data, size_t side_data_size);
  190. protected:
  191. friend class base::RefCountedThreadSafe<DecoderBuffer>;
  192. // Allocates a buffer of size |size| >= 0 and copies |data| into it. If |data|
  193. // is NULL then |data_| is set to NULL and |buffer_size_| to 0.
  194. // |is_key_frame_| will default to false.
  195. DecoderBuffer(const uint8_t* data,
  196. size_t size,
  197. const uint8_t* side_data,
  198. size_t side_data_size);
  199. DecoderBuffer(std::unique_ptr<uint8_t[]> data, size_t size);
  200. DecoderBuffer(base::ReadOnlySharedMemoryMapping mapping, size_t size);
  201. DecoderBuffer(base::WritableSharedMemoryMapping mapping, size_t size);
  202. virtual ~DecoderBuffer();
  203. // Encoded data, if it is stored on the heap.
  204. std::unique_ptr<uint8_t[]> data_;
  205. private:
  206. TimeInfo time_info_;
  207. // Size of the encoded data.
  208. size_t size_;
  209. // Side data. Used for alpha channel in VPx, and for text cues.
  210. size_t side_data_size_ = 0;
  211. std::unique_ptr<uint8_t[]> side_data_;
  212. // Encoded data, if it is stored in a read-only shared memory mapping.
  213. base::ReadOnlySharedMemoryMapping read_only_mapping_;
  214. // Encoded data, if it is stored in a writable shared memory mapping.
  215. base::WritableSharedMemoryMapping writable_mapping_;
  216. // Encryption parameters for the encoded data.
  217. std::unique_ptr<DecryptConfig> decrypt_config_;
  218. // Whether the frame was marked as a keyframe in the container.
  219. bool is_key_frame_ = false;
  220. // Constructor helper method for memory allocations.
  221. void Initialize();
  222. };
  223. } // namespace media
  224. #endif // MEDIA_BASE_DECODER_BUFFER_H_