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/decrypt_config.h"
18 #include "media/base/media_export.h"
22 // A specialized buffer for interfacing with audio / video decoders.
24 // Specifically ensures that data is aligned and padded as necessary by the
25 // underlying decoding framework. On desktop platforms this means memory is
26 // allocated using FFmpeg with particular alignment and padding requirements.
28 // Also includes decoder specific functionality for decryption.
30 // NOTE: It is illegal to call any method when end_of_stream() is true.
31 class MEDIA_EXPORT DecoderBuffer
32 : public base::RefCountedThreadSafe
<DecoderBuffer
> {
36 #if defined(ARCH_CPU_ARM_FAMILY)
43 // Allocates buffer with |size| >= 0. Buffer will be padded and aligned
45 explicit DecoderBuffer(int size
);
47 // Create a DecoderBuffer whose |data_| is copied from |data|. Buffer will be
48 // padded and aligned as necessary. |data| must not be NULL and |size| >= 0.
49 static scoped_refptr
<DecoderBuffer
> CopyFrom(const uint8
* data
, int size
);
51 // Create a DecoderBuffer whose |data_| is copied from |data| and |side_data_|
52 // is copied from |side_data|. Buffers will be padded and aligned as necessary
53 // Data pointers must not be NULL and sizes must be >= 0.
54 static scoped_refptr
<DecoderBuffer
> CopyFrom(const uint8
* data
, int size
,
55 const uint8
* side_data
,
58 // Create a DecoderBuffer indicating we've reached end of stream.
60 // Calling any method other than end_of_stream() on the resulting buffer
62 static scoped_refptr
<DecoderBuffer
> CreateEOSBuffer();
64 base::TimeDelta
timestamp() const {
65 DCHECK(!end_of_stream());
69 void set_timestamp(const base::TimeDelta
& timestamp
) {
70 DCHECK(!end_of_stream());
71 timestamp_
= timestamp
;
74 base::TimeDelta
duration() const {
75 DCHECK(!end_of_stream());
79 void set_duration(const base::TimeDelta
& duration
) {
80 DCHECK(!end_of_stream());
84 const uint8
* data() const {
85 DCHECK(!end_of_stream());
89 uint8
* writable_data() const {
90 DCHECK(!end_of_stream());
94 int data_size() const {
95 DCHECK(!end_of_stream());
99 const uint8
* side_data() const {
100 DCHECK(!end_of_stream());
101 return side_data_
.get();
104 int side_data_size() const {
105 DCHECK(!end_of_stream());
106 return side_data_size_
;
109 // A discard window indicates the amount of data which should be discard from
110 // this buffer after decoding. The first value is the amount of the front and
111 // the second the amount off the back. A value of kInfiniteDuration() for the
112 // first value indicates the entire buffer should be discarded; the second
113 // value must be base::TimeDelta() in this case.
114 typedef std::pair
<base::TimeDelta
, base::TimeDelta
> DiscardPadding
;
115 const DiscardPadding
& discard_padding() const {
116 DCHECK(!end_of_stream());
117 return discard_padding_
;
120 void set_discard_padding(const DiscardPadding
& discard_padding
) {
121 DCHECK(!end_of_stream());
122 discard_padding_
= discard_padding
;
125 const DecryptConfig
* decrypt_config() const {
126 DCHECK(!end_of_stream());
127 return decrypt_config_
.get();
130 void set_decrypt_config(scoped_ptr
<DecryptConfig
> decrypt_config
) {
131 DCHECK(!end_of_stream());
132 decrypt_config_
= decrypt_config
.Pass();
135 // If there's no data in this buffer, it represents end of stream.
136 bool end_of_stream() const {
137 return data_
== NULL
;
140 // Indicates this buffer is part of a splice around |splice_timestamp_|.
141 // Returns kNoTimestamp() if the buffer is not part of a splice.
142 base::TimeDelta
splice_timestamp() const {
143 DCHECK(!end_of_stream());
144 return splice_timestamp_
;
147 // When set to anything but kNoTimestamp() indicates this buffer is part of a
148 // splice around |splice_timestamp|.
149 void set_splice_timestamp(base::TimeDelta splice_timestamp
) {
150 DCHECK(!end_of_stream());
151 splice_timestamp_
= splice_timestamp
;
154 // Returns a human-readable string describing |*this|.
155 std::string
AsHumanReadableString();
158 friend class base::RefCountedThreadSafe
<DecoderBuffer
>;
160 // Allocates a buffer of size |size| >= 0 and copies |data| into it. Buffer
161 // will be padded and aligned as necessary. If |data| is NULL then |data_| is
162 // set to NULL and |buffer_size_| to 0.
163 DecoderBuffer(const uint8
* data
, int size
,
164 const uint8
* side_data
, int side_data_size
);
165 virtual ~DecoderBuffer();
168 base::TimeDelta timestamp_
;
169 base::TimeDelta duration_
;
172 scoped_ptr
<uint8
, base::AlignedFreeDeleter
> data_
;
174 scoped_ptr
<uint8
, base::AlignedFreeDeleter
> side_data_
;
175 scoped_ptr
<DecryptConfig
> decrypt_config_
;
176 DiscardPadding discard_padding_
;
177 base::TimeDelta splice_timestamp_
;
179 // Constructor helper method for memory allocations.
182 DISALLOW_COPY_AND_ASSIGN(DecoderBuffer
);
187 #endif // MEDIA_BASE_DECODER_BUFFER_H_