Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / decoder_buffer.cc
blobbc1ee9068f966078e6cffe8a70f9f87703ea8d18
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, int size,
30 const uint8* side_data, int side_data_size)
31 : size_(size),
32 side_data_size_(side_data_size),
33 is_key_frame_(false) {
34 if (!data) {
35 CHECK_EQ(size_, 0);
36 CHECK(!side_data);
37 return;
40 Initialize();
42 DCHECK_GE(size_, 0);
43 memcpy(data_.get(), data, size_);
45 if (!side_data) {
46 CHECK_EQ(side_data_size, 0);
47 return;
50 DCHECK_GT(side_data_size_, 0);
51 memcpy(side_data_.get(), side_data, side_data_size_);
54 DecoderBuffer::~DecoderBuffer() {}
56 void DecoderBuffer::Initialize() {
57 CHECK_GE(size_, 0);
58 data_.reset(AllocateFFmpegSafeBlock(size_));
59 if (side_data_size_ > 0)
60 side_data_.reset(AllocateFFmpegSafeBlock(side_data_size_));
61 splice_timestamp_ = kNoTimestamp();
64 // static
65 scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
66 int data_size) {
67 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
68 CHECK(data);
69 return make_scoped_refptr(new DecoderBuffer(data, data_size, NULL, 0));
72 // static
73 scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
74 int data_size,
75 const uint8* side_data,
76 int side_data_size) {
77 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
78 CHECK(data);
79 CHECK(side_data);
80 return make_scoped_refptr(new DecoderBuffer(data, data_size,
81 side_data, side_data_size));
84 // static
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";
94 std::ostringstream s;
95 s << "timestamp: " << timestamp_.InMicroseconds()
96 << " duration: " << duration_.InMicroseconds()
97 << " size: " << size_
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() << ")";
103 return s.str();
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_);
117 } else {
118 side_data_.reset();
119 side_data_size_ = 0;
123 } // namespace media