Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / decoder_buffer.h
blob665313ddcc1a698a23452c706355c80ed99f98d3
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/buffers.h"
18 #include "media/base/decrypt_config.h"
19 #include "media/base/media_export.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 int data_size() const {
100 DCHECK(!end_of_stream());
101 return size_;
104 const uint8* side_data() const {
105 DCHECK(!end_of_stream());
106 return side_data_.get();
109 int side_data_size() const {
110 DCHECK(!end_of_stream());
111 return side_data_size_;
114 // A discard window indicates the amount of data which should be discard from
115 // this buffer after decoding. The first value is the amount of the front and
116 // the second the amount off the back. A value of kInfiniteDuration() for the
117 // first value indicates the entire buffer should be discarded; the second
118 // value must be base::TimeDelta() in this case.
119 typedef std::pair<base::TimeDelta, base::TimeDelta> DiscardPadding;
120 const DiscardPadding& discard_padding() const {
121 DCHECK(!end_of_stream());
122 return discard_padding_;
125 void set_discard_padding(const DiscardPadding& discard_padding) {
126 DCHECK(!end_of_stream());
127 discard_padding_ = discard_padding;
130 const DecryptConfig* decrypt_config() const {
131 DCHECK(!end_of_stream());
132 return decrypt_config_.get();
135 void set_decrypt_config(scoped_ptr<DecryptConfig> decrypt_config) {
136 DCHECK(!end_of_stream());
137 decrypt_config_ = decrypt_config.Pass();
140 // If there's no data in this buffer, it represents end of stream.
141 bool end_of_stream() const {
142 return data_ == NULL;
145 // Indicates this buffer is part of a splice around |splice_timestamp_|.
146 // Returns kNoTimestamp() if the buffer is not part of a splice.
147 base::TimeDelta splice_timestamp() const {
148 DCHECK(!end_of_stream());
149 return splice_timestamp_;
152 // When set to anything but kNoTimestamp() indicates this buffer is part of a
153 // splice around |splice_timestamp|.
154 void set_splice_timestamp(base::TimeDelta splice_timestamp) {
155 DCHECK(!end_of_stream());
156 splice_timestamp_ = splice_timestamp;
159 bool is_key_frame() const {
160 DCHECK(!end_of_stream());
161 return is_key_frame_;
164 void set_is_key_frame(bool is_key_frame) {
165 DCHECK(!end_of_stream());
166 is_key_frame_ = is_key_frame;
169 // Returns a human-readable string describing |*this|.
170 std::string AsHumanReadableString();
172 // Replaces any existing side data with data copied from |side_data|.
173 void CopySideDataFrom(const uint8* side_data, int side_data_size);
175 protected:
176 friend class base::RefCountedThreadSafe<DecoderBuffer>;
178 // Allocates a buffer of size |size| >= 0 and copies |data| into it. Buffer
179 // will be padded and aligned as necessary. If |data| is NULL then |data_| is
180 // set to NULL and |buffer_size_| to 0. |is_key_frame_| will default to
181 // false.
182 DecoderBuffer(const uint8* data, int size,
183 const uint8* side_data, int side_data_size);
184 virtual ~DecoderBuffer();
186 private:
187 base::TimeDelta timestamp_;
188 base::TimeDelta duration_;
190 int size_;
191 scoped_ptr<uint8, base::AlignedFreeDeleter> data_;
192 int side_data_size_;
193 scoped_ptr<uint8, base::AlignedFreeDeleter> side_data_;
194 scoped_ptr<DecryptConfig> decrypt_config_;
195 DiscardPadding discard_padding_;
196 base::TimeDelta splice_timestamp_;
197 bool is_key_frame_;
199 // Constructor helper method for memory allocations.
200 void Initialize();
202 DISALLOW_COPY_AND_ASSIGN(DecoderBuffer);
205 } // namespace media
207 #endif // MEDIA_BASE_DECODER_BUFFER_H_