Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / base / decoder_buffer.h
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.
4
5 #ifndef MEDIA_BASE_DECODER_BUFFER_H_
6 #define MEDIA_BASE_DECODER_BUFFER_H_
7
8 #include <string>
9 #include <utility>
10
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"
19
20 namespace media {
21
22 // A specialized buffer for interfacing with audio / video decoders.
23 //
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.
27 //
28 // Also includes decoder specific functionality for decryption.
29 //
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> {
33  public:
34   enum {
35     kPaddingSize = 16,
36 #if defined(ARCH_CPU_ARM_FAMILY)
37     kAlignmentSize = 16
38 #else
39     kAlignmentSize = 32
40 #endif
41   };
42
43   // Allocates buffer with |size| >= 0.  Buffer will be padded and aligned
44   // as necessary.
45   explicit DecoderBuffer(int size);
46
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);
50
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,
56                                                int side_data_size);
57
58   // Create a DecoderBuffer indicating we've reached end of stream.
59   //
60   // Calling any method other than end_of_stream() on the resulting buffer
61   // is disallowed.
62   static scoped_refptr<DecoderBuffer> CreateEOSBuffer();
63
64   base::TimeDelta timestamp() const {
65     DCHECK(!end_of_stream());
66     return timestamp_;
67   }
68
69   void set_timestamp(const base::TimeDelta& timestamp) {
70     DCHECK(!end_of_stream());
71     timestamp_ = timestamp;
72   }
73
74   base::TimeDelta duration() const {
75     DCHECK(!end_of_stream());
76     return duration_;
77   }
78
79   void set_duration(const base::TimeDelta& duration) {
80     DCHECK(!end_of_stream());
81     duration_ = duration;
82   }
83
84   const uint8* data() const {
85     DCHECK(!end_of_stream());
86     return data_.get();
87   }
88
89   uint8* writable_data() const {
90     DCHECK(!end_of_stream());
91     return data_.get();
92   }
93
94   int data_size() const {
95     DCHECK(!end_of_stream());
96     return size_;
97   }
98
99   const uint8* side_data() const {
100     DCHECK(!end_of_stream());
101     return side_data_.get();
102   }
103
104   int side_data_size() const {
105     DCHECK(!end_of_stream());
106     return side_data_size_;
107   }
108
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.
112   typedef std::pair<base::TimeDelta, base::TimeDelta> DiscardPadding;
113   const DiscardPadding& discard_padding() const {
114     DCHECK(!end_of_stream());
115     return discard_padding_;
116   }
117
118   void set_discard_padding(const DiscardPadding& discard_padding) {
119     DCHECK(!end_of_stream());
120     discard_padding_ = discard_padding;
121   }
122
123   const DecryptConfig* decrypt_config() const {
124     DCHECK(!end_of_stream());
125     return decrypt_config_.get();
126   }
127
128   void set_decrypt_config(scoped_ptr<DecryptConfig> decrypt_config) {
129     DCHECK(!end_of_stream());
130     decrypt_config_ = decrypt_config.Pass();
131   }
132
133   // If there's no data in this buffer, it represents end of stream.
134   bool end_of_stream() const {
135     return data_ == NULL;
136   }
137
138   // Indicates this buffer is part of a splice around |splice_timestamp_|.
139   // Returns kNoTimestamp() if the buffer is not part of a splice.
140   base::TimeDelta splice_timestamp() const {
141     DCHECK(!end_of_stream());
142     return splice_timestamp_;
143   }
144
145   // When set to anything but kNoTimestamp() indicates this buffer is part of a
146   // splice around |splice_timestamp|.
147   void set_splice_timestamp(base::TimeDelta splice_timestamp) {
148     DCHECK(!end_of_stream());
149     splice_timestamp_ = splice_timestamp;
150   }
151
152   // Returns a human-readable string describing |*this|.
153   std::string AsHumanReadableString();
154
155  protected:
156   friend class base::RefCountedThreadSafe<DecoderBuffer>;
157
158   // Allocates a buffer of size |size| >= 0 and copies |data| into it.  Buffer
159   // will be padded and aligned as necessary.  If |data| is NULL then |data_| is
160   // set to NULL and |buffer_size_| to 0.
161   DecoderBuffer(const uint8* data, int size,
162                 const uint8* side_data, int side_data_size);
163   virtual ~DecoderBuffer();
164
165  private:
166   base::TimeDelta timestamp_;
167   base::TimeDelta duration_;
168
169   int size_;
170   scoped_ptr<uint8, base::AlignedFreeDeleter> data_;
171   int side_data_size_;
172   scoped_ptr<uint8, base::AlignedFreeDeleter> side_data_;
173   scoped_ptr<DecryptConfig> decrypt_config_;
174   DiscardPadding discard_padding_;
175   base::TimeDelta splice_timestamp_;
176
177   // Constructor helper method for memory allocations.
178   void Initialize();
179
180   DISALLOW_COPY_AND_ASSIGN(DecoderBuffer);
181 };
182
183 }  // namespace media
184
185 #endif  // MEDIA_BASE_DECODER_BUFFER_H_