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 const DecryptConfig
* decrypt_config() const {
109 DCHECK(!end_of_stream());
110 return decrypt_config_
.get();
113 void set_decrypt_config(scoped_ptr
<DecryptConfig
> decrypt_config
) {
114 DCHECK(!end_of_stream());
115 decrypt_config_
= decrypt_config
.Pass();
118 // If there's no data in this buffer, it represents end of stream.
119 bool end_of_stream() const {
120 return data_
== NULL
;
123 // Returns a human-readable string describing |*this|.
124 std::string
AsHumanReadableString();
127 friend class base::RefCountedThreadSafe
<DecoderBuffer
>;
129 // Allocates a buffer of size |size| >= 0 and copies |data| into it. Buffer
130 // will be padded and aligned as necessary. If |data| is NULL then |data_| is
131 // set to NULL and |buffer_size_| to 0.
132 DecoderBuffer(const uint8
* data
, int size
,
133 const uint8
* side_data
, int side_data_size
);
134 virtual ~DecoderBuffer();
137 base::TimeDelta timestamp_
;
138 base::TimeDelta duration_
;
141 scoped_ptr
<uint8
, base::ScopedPtrAlignedFree
> data_
;
143 scoped_ptr
<uint8
, base::ScopedPtrAlignedFree
> side_data_
;
144 scoped_ptr
<DecryptConfig
> decrypt_config_
;
146 // Constructor helper method for memory allocations.
149 DISALLOW_COPY_AND_ASSIGN(DecoderBuffer
);
154 #endif // MEDIA_BASE_DECODER_BUFFER_H_