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_
10 #include "base/logging.h"
11 #include "base/memory/aligned_memory.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "media/base/decrypt_config.h"
17 #include "media/base/media_export.h"
21 // A specialized buffer for interfacing with audio / video decoders.
23 // Specifically ensures that data is aligned and padded as necessary by the
24 // underlying decoding framework. On desktop platforms this means memory is
25 // allocated using FFmpeg with particular alignment and padding requirements.
27 // Also includes decoder specific functionality for decryption.
29 // NOTE: It is illegal to call any method when end_of_stream() is true.
30 class MEDIA_EXPORT DecoderBuffer
31 : public base::RefCountedThreadSafe
<DecoderBuffer
> {
35 #if defined(ARCH_CPU_ARM_FAMILY)
42 // Allocates buffer with |size| >= 0. Buffer will be padded and aligned
44 explicit DecoderBuffer(int size
);
46 // Create a DecoderBuffer whose |data_| is copied from |data|. Buffer will be
47 // padded and aligned as necessary. |data| must not be NULL and |size| >= 0.
48 static scoped_refptr
<DecoderBuffer
> CopyFrom(const uint8
* data
, int size
);
50 // Create a DecoderBuffer whose |data_| is copied from |data| and |side_data_|
51 // is copied from |side_data|. Buffers will be padded and aligned as necessary
52 // Data pointers must not be NULL and sizes must be >= 0.
53 static scoped_refptr
<DecoderBuffer
> CopyFrom(const uint8
* data
, int size
,
54 const uint8
* side_data
,
57 // Create a DecoderBuffer indicating we've reached end of stream.
59 // Calling any method other than end_of_stream() on the resulting buffer
61 static scoped_refptr
<DecoderBuffer
> CreateEOSBuffer();
63 base::TimeDelta
timestamp() const {
64 DCHECK(!end_of_stream());
68 void set_timestamp(const base::TimeDelta
& timestamp
) {
69 DCHECK(!end_of_stream());
70 timestamp_
= timestamp
;
73 base::TimeDelta
duration() const {
74 DCHECK(!end_of_stream());
78 void set_duration(const base::TimeDelta
& duration
) {
79 DCHECK(!end_of_stream());
83 const uint8
* data() const {
84 DCHECK(!end_of_stream());
88 uint8
* writable_data() const {
89 DCHECK(!end_of_stream());
93 int data_size() const {
94 DCHECK(!end_of_stream());
98 const uint8
* side_data() const {
99 DCHECK(!end_of_stream());
100 return side_data_
.get();
103 int side_data_size() const {
104 DCHECK(!end_of_stream());
105 return side_data_size_
;
108 base::TimeDelta
discard_padding() const {
109 DCHECK(!end_of_stream());
110 return discard_padding_
;
113 void set_discard_padding(const base::TimeDelta discard_padding
) {
114 DCHECK(!end_of_stream());
115 discard_padding_
= discard_padding
;
118 const DecryptConfig
* decrypt_config() const {
119 DCHECK(!end_of_stream());
120 return decrypt_config_
.get();
123 void set_decrypt_config(scoped_ptr
<DecryptConfig
> decrypt_config
) {
124 DCHECK(!end_of_stream());
125 decrypt_config_
= decrypt_config
.Pass();
128 // If there's no data in this buffer, it represents end of stream.
129 bool end_of_stream() const {
130 return data_
== NULL
;
133 // Indicates this buffer is part of a splice around |splice_timestamp_|.
134 // Returns kNoTimestamp() if the buffer is not part of a splice.
135 base::TimeDelta
splice_timestamp() const {
136 DCHECK(!end_of_stream());
137 return splice_timestamp_
;
140 // When set to anything but kNoTimestamp() indicates this buffer is part of a
141 // splice around |splice_timestamp|.
142 void set_splice_timestamp(base::TimeDelta splice_timestamp
) {
143 DCHECK(!end_of_stream());
144 splice_timestamp_
= splice_timestamp
;
147 // Returns a human-readable string describing |*this|.
148 std::string
AsHumanReadableString();
151 friend class base::RefCountedThreadSafe
<DecoderBuffer
>;
153 // Allocates a buffer of size |size| >= 0 and copies |data| into it. Buffer
154 // will be padded and aligned as necessary. If |data| is NULL then |data_| is
155 // set to NULL and |buffer_size_| to 0.
156 DecoderBuffer(const uint8
* data
, int size
,
157 const uint8
* side_data
, int side_data_size
);
158 virtual ~DecoderBuffer();
161 base::TimeDelta timestamp_
;
162 base::TimeDelta duration_
;
165 scoped_ptr
<uint8
, base::AlignedFreeDeleter
> data_
;
167 scoped_ptr
<uint8
, base::AlignedFreeDeleter
> side_data_
;
168 scoped_ptr
<DecryptConfig
> decrypt_config_
;
169 base::TimeDelta discard_padding_
;
170 base::TimeDelta splice_timestamp_
;
172 // Constructor helper method for memory allocations.
175 DISALLOW_COPY_AND_ASSIGN(DecoderBuffer
);
180 #endif // MEDIA_BASE_DECODER_BUFFER_H_