Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / decoder_buffer.cc
bloba8624b1e8e705090aea221e23a2ee58154502580
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"
8 namespace media {
10 // Allocates a block of memory which is padded for use with the SIMD
11 // optimizations used by FFmpeg.
12 static uint8* AllocateFFmpegSafeBlock(int size) {
13 uint8* const block = reinterpret_cast<uint8*>(base::AlignedAlloc(
14 size + DecoderBuffer::kPaddingSize, DecoderBuffer::kAlignmentSize));
15 memset(block + size, 0, DecoderBuffer::kPaddingSize);
16 return block;
19 DecoderBuffer::DecoderBuffer(int size)
20 : size_(size),
21 side_data_size_(0),
22 is_key_frame_(false) {
23 Initialize();
26 DecoderBuffer::DecoderBuffer(const uint8* data,
27 int size,
28 const uint8* side_data,
29 int side_data_size)
30 : size_(size),
31 side_data_size_(side_data_size),
32 is_key_frame_(false) {
33 if (!data) {
34 CHECK_EQ(size_, 0);
35 CHECK(!side_data);
36 return;
39 Initialize();
41 DCHECK_GE(size_, 0);
42 memcpy(data_.get(), data, size_);
44 if (!side_data) {
45 CHECK_EQ(side_data_size, 0);
46 return;
49 DCHECK_GT(side_data_size_, 0);
50 memcpy(side_data_.get(), side_data, side_data_size_);
53 DecoderBuffer::~DecoderBuffer() {}
55 void DecoderBuffer::Initialize() {
56 CHECK_GE(size_, 0);
57 data_.reset(AllocateFFmpegSafeBlock(size_));
58 if (side_data_size_ > 0)
59 side_data_.reset(AllocateFFmpegSafeBlock(side_data_size_));
60 splice_timestamp_ = kNoTimestamp();
63 // static
64 scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
65 int data_size) {
66 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
67 CHECK(data);
68 return make_scoped_refptr(new DecoderBuffer(data, data_size, NULL, 0));
71 // static
72 scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
73 int data_size,
74 const uint8* side_data,
75 int side_data_size) {
76 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
77 CHECK(data);
78 CHECK(side_data);
79 return make_scoped_refptr(new DecoderBuffer(data, data_size,
80 side_data, side_data_size));
83 // static
84 scoped_refptr<DecoderBuffer> DecoderBuffer::CreateEOSBuffer() {
85 return make_scoped_refptr(new DecoderBuffer(NULL, 0, NULL, 0));
88 std::string DecoderBuffer::AsHumanReadableString() {
89 if (end_of_stream()) {
90 return "end of stream";
93 std::ostringstream s;
94 s << "timestamp: " << timestamp_.InMicroseconds()
95 << " duration: " << duration_.InMicroseconds()
96 << " size: " << size_
97 << " side_data_size: " << side_data_size_
98 << " is_key_frame: " << is_key_frame_
99 << " encrypted: " << (decrypt_config_ != NULL)
100 << " discard_padding (ms): (" << discard_padding_.first.InMilliseconds()
101 << ", " << discard_padding_.second.InMilliseconds() << ")";
102 return s.str();
105 void DecoderBuffer::set_timestamp(base::TimeDelta timestamp) {
106 DCHECK(!end_of_stream());
107 timestamp_ = timestamp;
110 void DecoderBuffer::CopySideDataFrom(const uint8* side_data,
111 int side_data_size) {
112 if (side_data_size > 0) {
113 side_data_size_ = side_data_size;
114 side_data_.reset(AllocateFFmpegSafeBlock(side_data_size_));
115 memcpy(side_data_.get(), side_data, side_data_size_);
116 } else {
117 side_data_.reset();
118 side_data_size_ = 0;
122 } // namespace media