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_
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"
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
> {
37 #if defined(ARCH_CPU_ARM_FAMILY)
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
,
61 // Create a DecoderBuffer indicating we've reached end of stream.
63 // Calling any method other than end_of_stream() on the resulting buffer
65 static scoped_refptr
<DecoderBuffer
> CreateEOSBuffer();
67 base::TimeDelta
timestamp() const {
68 DCHECK(!end_of_stream());
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());
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();
89 const uint8
* data() const {
90 DCHECK(!end_of_stream());
94 uint8
* writable_data() const {
95 DCHECK(!end_of_stream());
99 int data_size() const {
100 DCHECK(!end_of_stream());
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();
173 friend class base::RefCountedThreadSafe
<DecoderBuffer
>;
175 // Allocates a buffer of size |size| >= 0 and copies |data| into it. Buffer
176 // will be padded and aligned as necessary. If |data| is NULL then |data_| is
177 // set to NULL and |buffer_size_| to 0. |is_key_frame_| will default to
179 DecoderBuffer(const uint8
* data
, int size
,
180 const uint8
* side_data
, int side_data_size
);
181 virtual ~DecoderBuffer();
184 base::TimeDelta timestamp_
;
185 base::TimeDelta duration_
;
188 scoped_ptr
<uint8
, base::AlignedFreeDeleter
> data_
;
190 scoped_ptr
<uint8
, base::AlignedFreeDeleter
> side_data_
;
191 scoped_ptr
<DecryptConfig
> decrypt_config_
;
192 DiscardPadding discard_padding_
;
193 base::TimeDelta splice_timestamp_
;
196 // Constructor helper method for memory allocations.
199 DISALLOW_COPY_AND_ASSIGN(DecoderBuffer
);
204 #endif // MEDIA_BASE_DECODER_BUFFER_H_