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
, int size
,
30 const uint8
* side_data
, int side_data_size
)
32 side_data_size_(side_data_size
),
33 is_key_frame_(false) {
43 memcpy(data_
.get(), data
, size_
);
46 CHECK_EQ(side_data_size
, 0);
50 DCHECK_GT(side_data_size_
, 0);
51 memcpy(side_data_
.get(), side_data
, side_data_size_
);
54 DecoderBuffer::~DecoderBuffer() {}
56 void DecoderBuffer::Initialize() {
58 data_
.reset(AllocateFFmpegSafeBlock(size_
));
59 if (side_data_size_
> 0)
60 side_data_
.reset(AllocateFFmpegSafeBlock(side_data_size_
));
61 splice_timestamp_
= kNoTimestamp();
65 scoped_refptr
<DecoderBuffer
> DecoderBuffer::CopyFrom(const uint8
* data
,
67 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
69 return make_scoped_refptr(new DecoderBuffer(data
, data_size
, NULL
, 0));
73 scoped_refptr
<DecoderBuffer
> DecoderBuffer::CopyFrom(const uint8
* data
,
75 const uint8
* side_data
,
77 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
80 return make_scoped_refptr(new DecoderBuffer(data
, data_size
,
81 side_data
, side_data_size
));
85 scoped_refptr
<DecoderBuffer
> DecoderBuffer::CreateEOSBuffer() {
86 return make_scoped_refptr(new DecoderBuffer(NULL
, 0, NULL
, 0));
89 std::string
DecoderBuffer::AsHumanReadableString() {
90 if (end_of_stream()) {
91 return "end of stream";
95 s
<< "timestamp: " << timestamp_
.InMicroseconds()
96 << " duration: " << duration_
.InMicroseconds()
98 << " side_data_size: " << side_data_size_
99 << " is_key_frame: " << is_key_frame_
100 << " encrypted: " << (decrypt_config_
!= NULL
)
101 << " discard_padding (ms): (" << discard_padding_
.first
.InMilliseconds()
102 << ", " << discard_padding_
.second
.InMilliseconds() << ")";
106 void DecoderBuffer::set_timestamp(base::TimeDelta timestamp
) {
107 DCHECK(!end_of_stream());
108 timestamp_
= timestamp
;
111 void DecoderBuffer::CopySideDataFrom(const uint8
* side_data
,
112 int side_data_size
) {
113 if (side_data_size
> 0) {
114 side_data_size_
= side_data_size
;
115 side_data_
.reset(AllocateFFmpegSafeBlock(side_data_size_
));
116 memcpy(side_data_
.get(), side_data
, side_data_size_
);