Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / decoder_buffer.h
blob85036969abeb71972ab642fd17d4668d708028be
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.
5 #ifndef MEDIA_BASE_DECODER_BUFFER_H_
6 #define MEDIA_BASE_DECODER_BUFFER_H_
8 #include <string>
9 #include <utility>
11 #include "base/logging.h"
12 #include "base/memory/aligned_memory.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "media/base/decrypt_config.h"
18 #include "media/base/media_export.h"
19 #include "media/base/timestamp_constants.h"
21 namespace media {
23 // A specialized buffer for interfacing with audio / video decoders.
25 // Specifically ensures that data is aligned and padded as necessary by the
26 // underlying decoding framework. On desktop platforms this means memory is
27 // allocated using FFmpeg with particular alignment and padding requirements.
29 // Also includes decoder specific functionality for decryption.
31 // NOTE: It is illegal to call any method when end_of_stream() is true.
32 class MEDIA_EXPORT DecoderBuffer
33 : public base::RefCountedThreadSafe<DecoderBuffer> {
34 public:
35 enum {
36 kPaddingSize = 32,
37 #if defined(ARCH_CPU_ARM_FAMILY)
38 kAlignmentSize = 16
39 #else
40 kAlignmentSize = 32
41 #endif
44 // Allocates buffer with |size| >= 0. Buffer will be padded and aligned
45 // as necessary, and |is_key_frame_| will default to false.
46 explicit DecoderBuffer(int size);
48 // Create a DecoderBuffer whose |data_| is copied from |data|. Buffer will be
49 // padded and aligned as necessary. |data| must not be NULL and |size| >= 0.
50 // The buffer's |is_key_frame_| will default to false.
51 static scoped_refptr<DecoderBuffer> CopyFrom(const uint8* data, int size);
53 // Create a DecoderBuffer whose |data_| is copied from |data| and |side_data_|
54 // is copied from |side_data|. Buffers will be padded and aligned as necessary
55 // Data pointers must not be NULL and sizes must be >= 0. The buffer's
56 // |is_key_frame_| will default to false.
57 static scoped_refptr<DecoderBuffer> CopyFrom(const uint8* data, int size,
58 const uint8* side_data,
59 int side_data_size);
61 // Create a DecoderBuffer indicating we've reached end of stream.
63 // Calling any method other than end_of_stream() on the resulting buffer
64 // is disallowed.
65 static scoped_refptr<DecoderBuffer> CreateEOSBuffer();
67 base::TimeDelta timestamp() const {
68 DCHECK(!end_of_stream());
69 return timestamp_;
72 // TODO(dalecurtis): This should be renamed at some point, but to avoid a yak
73 // shave keep as a virtual with hacker_style() for now.
74 virtual void set_timestamp(base::TimeDelta timestamp);
76 base::TimeDelta duration() const {
77 DCHECK(!end_of_stream());
78 return duration_;
81 void set_duration(base::TimeDelta duration) {
82 DCHECK(!end_of_stream());
83 DCHECK(duration == kNoTimestamp() ||
84 (duration >= base::TimeDelta() && duration != kInfiniteDuration()))
85 << duration.InSecondsF();
86 duration_ = duration;
89 const uint8* data() const {
90 DCHECK(!end_of_stream());
91 return data_.get();
94 uint8* writable_data() const {
95 DCHECK(!end_of_stream());
96 return data_.get();
99 // TODO(servolk): data_size should return size_t instead of int
100 int data_size() const {
101 DCHECK(!end_of_stream());
102 return size_;
105 const uint8* side_data() const {
106 DCHECK(!end_of_stream());
107 return side_data_.get();
110 // TODO(servolk): side_data_size should return size_t instead of int
111 int side_data_size() const {
112 DCHECK(!end_of_stream());
113 return side_data_size_;
116 // A discard window indicates the amount of data which should be discard from
117 // this buffer after decoding. The first value is the amount of the front and
118 // the second the amount off the back. A value of kInfiniteDuration() for the
119 // first value indicates the entire buffer should be discarded; the second
120 // value must be base::TimeDelta() in this case.
121 typedef std::pair<base::TimeDelta, base::TimeDelta> DiscardPadding;
122 const DiscardPadding& discard_padding() const {
123 DCHECK(!end_of_stream());
124 return discard_padding_;
127 void set_discard_padding(const DiscardPadding& discard_padding) {
128 DCHECK(!end_of_stream());
129 discard_padding_ = discard_padding;
132 const DecryptConfig* decrypt_config() const {
133 DCHECK(!end_of_stream());
134 return decrypt_config_.get();
137 void set_decrypt_config(scoped_ptr<DecryptConfig> decrypt_config) {
138 DCHECK(!end_of_stream());
139 decrypt_config_ = decrypt_config.Pass();
142 // If there's no data in this buffer, it represents end of stream.
143 bool end_of_stream() const {
144 return data_ == NULL;
147 // Indicates this buffer is part of a splice around |splice_timestamp_|.
148 // Returns kNoTimestamp() if the buffer is not part of a splice.
149 base::TimeDelta splice_timestamp() const {
150 DCHECK(!end_of_stream());
151 return splice_timestamp_;
154 // When set to anything but kNoTimestamp() indicates this buffer is part of a
155 // splice around |splice_timestamp|.
156 void set_splice_timestamp(base::TimeDelta splice_timestamp) {
157 DCHECK(!end_of_stream());
158 splice_timestamp_ = splice_timestamp;
161 bool is_key_frame() const {
162 DCHECK(!end_of_stream());
163 return is_key_frame_;
166 void set_is_key_frame(bool is_key_frame) {
167 DCHECK(!end_of_stream());
168 is_key_frame_ = is_key_frame;
171 // Returns a human-readable string describing |*this|.
172 std::string AsHumanReadableString();
174 // Replaces any existing side data with data copied from |side_data|.
175 void CopySideDataFrom(const uint8* side_data, int side_data_size);
177 protected:
178 friend class base::RefCountedThreadSafe<DecoderBuffer>;
180 // Allocates a buffer of size |size| >= 0 and copies |data| into it. Buffer
181 // will be padded and aligned as necessary. If |data| is NULL then |data_| is
182 // set to NULL and |buffer_size_| to 0. |is_key_frame_| will default to
183 // false.
184 DecoderBuffer(const uint8* data, int size,
185 const uint8* side_data, int side_data_size);
186 virtual ~DecoderBuffer();
188 private:
189 base::TimeDelta timestamp_;
190 base::TimeDelta duration_;
192 // TODO(servolk): Consider changing size_/side_data_size_ types to size_t.
193 int size_;
194 scoped_ptr<uint8, base::AlignedFreeDeleter> data_;
195 int side_data_size_;
196 scoped_ptr<uint8, base::AlignedFreeDeleter> side_data_;
197 scoped_ptr<DecryptConfig> decrypt_config_;
198 DiscardPadding discard_padding_;
199 base::TimeDelta splice_timestamp_;
200 bool is_key_frame_;
202 // Constructor helper method for memory allocations.
203 void Initialize();
205 DISALLOW_COPY_AND_ASSIGN(DecoderBuffer);
208 } // namespace media
210 #endif // MEDIA_BASE_DECODER_BUFFER_H_