Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / base / decoder_buffer.cc
bloba9c177998616def665173feeb2eac40587718898
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"
11 namespace media {
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);
19 return block;
22 DecoderBuffer::DecoderBuffer(int size)
23 : size_(size),
24 side_data_size_(0),
25 is_key_frame_(false) {
26 Initialize();
29 DecoderBuffer::DecoderBuffer(const uint8* data,
30 int size,
31 const uint8* side_data,
32 int side_data_size)
33 : size_(size),
34 side_data_size_(side_data_size),
35 is_key_frame_(false) {
36 if (!data) {
37 CHECK_EQ(size_, 0);
38 CHECK(!side_data);
39 return;
42 Initialize();
44 DCHECK_GE(size_, 0);
45 memcpy(data_.get(), data, size_);
47 if (!side_data) {
48 CHECK_EQ(side_data_size, 0);
49 return;
52 DCHECK_GT(side_data_size_, 0);
53 memcpy(side_data_.get(), side_data, side_data_size_);
56 DecoderBuffer::~DecoderBuffer() {}
58 void DecoderBuffer::Initialize() {
59 CHECK_GE(size_, 0);
60 data_.reset(AllocateFFmpegSafeBlock(size_));
61 if (side_data_size_ > 0)
62 side_data_.reset(AllocateFFmpegSafeBlock(side_data_size_));
63 splice_timestamp_ = kNoTimestamp();
66 // static
67 scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
68 int data_size) {
69 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
70 CHECK(data);
71 return make_scoped_refptr(new DecoderBuffer(data, data_size, NULL, 0));
74 // static
75 scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
76 int data_size,
77 const uint8* side_data,
78 int side_data_size) {
79 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
80 CHECK(data);
81 CHECK(side_data);
82 return make_scoped_refptr(new DecoderBuffer(data, data_size,
83 side_data, side_data_size));
86 // static
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";
96 std::ostringstream s;
97 s << "timestamp: " << timestamp_.InMicroseconds()
98 << " duration: " << duration_.InMicroseconds()
99 << " size: " << size_
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() << ")";
105 return s.str();
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_);
119 } else {
120 side_data_.reset();
121 side_data_size_ = 0;
125 } // namespace media