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 #include "media/base/decoder_buffer.h"
7 #include "base/logging.h"
8 #include "media/base/buffers.h"
9 #include "media/base/decrypt_config.h"
13 // Allocates a block of memory which is padded for use with the SIMD
14 // optimizations used by FFmpeg.
15 static uint8
* AllocateFFmpegSafeBlock(int size
) {
16 uint8
* const block
= reinterpret_cast<uint8
*>(base::AlignedAlloc(
17 size
+ DecoderBuffer::kPaddingSize
, DecoderBuffer::kAlignmentSize
));
18 memset(block
+ size
, 0, DecoderBuffer::kPaddingSize
);
22 DecoderBuffer::DecoderBuffer(int size
)
25 is_key_frame_(false) {
29 DecoderBuffer::DecoderBuffer(const uint8
* data
,
31 const uint8
* side_data
,
34 side_data_size_(side_data_size
),
35 is_key_frame_(false) {
45 memcpy(data_
.get(), data
, size_
);
48 CHECK_EQ(side_data_size
, 0);
52 DCHECK_GT(side_data_size_
, 0);
53 memcpy(side_data_
.get(), side_data
, side_data_size_
);
56 DecoderBuffer::~DecoderBuffer() {}
58 void DecoderBuffer::Initialize() {
60 data_
.reset(AllocateFFmpegSafeBlock(size_
));
61 if (side_data_size_
> 0)
62 side_data_
.reset(AllocateFFmpegSafeBlock(side_data_size_
));
63 splice_timestamp_
= kNoTimestamp();
67 scoped_refptr
<DecoderBuffer
> DecoderBuffer::CopyFrom(const uint8
* data
,
69 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
71 return make_scoped_refptr(new DecoderBuffer(data
, data_size
, NULL
, 0));
75 scoped_refptr
<DecoderBuffer
> DecoderBuffer::CopyFrom(const uint8
* data
,
77 const uint8
* side_data
,
79 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
82 return make_scoped_refptr(new DecoderBuffer(data
, data_size
,
83 side_data
, side_data_size
));
87 scoped_refptr
<DecoderBuffer
> DecoderBuffer::CreateEOSBuffer() {
88 return make_scoped_refptr(new DecoderBuffer(NULL
, 0, NULL
, 0));
91 std::string
DecoderBuffer::AsHumanReadableString() {
92 if (end_of_stream()) {
93 return "end of stream";
97 s
<< "timestamp: " << timestamp_
.InMicroseconds()
98 << " duration: " << duration_
.InMicroseconds()
100 << " side_data_size: " << side_data_size_
101 << " is_key_frame: " << is_key_frame_
102 << " encrypted: " << (decrypt_config_
!= NULL
)
103 << " discard_padding (ms): (" << discard_padding_
.first
.InMilliseconds()
104 << ", " << discard_padding_
.second
.InMilliseconds() << ")";
108 void DecoderBuffer::set_timestamp(base::TimeDelta timestamp
) {
109 DCHECK(!end_of_stream());
110 timestamp_
= timestamp
;
113 void DecoderBuffer::CopySideDataFrom(const uint8
* side_data
,
114 int side_data_size
) {
115 if (side_data_size
> 0) {
116 side_data_size_
= side_data_size
;
117 side_data_
.reset(AllocateFFmpegSafeBlock(side_data_size_
));
118 memcpy(side_data_
.get(), side_data
, side_data_size_
);