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.
4
5#include "media/base/decoder_buffer.h"
6
7#include "base/logging.h"
8#include "media/base/buffers.h"
9#include "media/base/decrypt_config.h"
10
11namespace media {
12
13DecoderBuffer::DecoderBuffer(int size)
14    : size_(size),
15      side_data_size_(0) {
16  Initialize();
17}
18
19DecoderBuffer::DecoderBuffer(const uint8* data, int size,
20                             const uint8* side_data, int side_data_size)
21    : size_(size),
22      side_data_size_(side_data_size) {
23  if (!data) {
24    CHECK_EQ(size_, 0);
25    CHECK(!side_data);
26    return;
27  }
28
29  Initialize();
30  memcpy(data_.get(), data, size_);
31  if (side_data)
32    memcpy(side_data_.get(), side_data, side_data_size_);
33}
34
35DecoderBuffer::~DecoderBuffer() {}
36
37void DecoderBuffer::Initialize() {
38  CHECK_GE(size_, 0);
39  data_.reset(reinterpret_cast<uint8*>(
40      base::AlignedAlloc(size_ + kPaddingSize, kAlignmentSize)));
41  memset(data_.get() + size_, 0, kPaddingSize);
42  if (side_data_size_ > 0) {
43    side_data_.reset(reinterpret_cast<uint8*>(
44        base::AlignedAlloc(side_data_size_ + kPaddingSize, kAlignmentSize)));
45    memset(side_data_.get() + side_data_size_, 0, kPaddingSize);
46  }
47  splice_timestamp_ = kNoTimestamp();
48}
49
50// static
51scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
52                                                     int data_size) {
53  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
54  CHECK(data);
55  return make_scoped_refptr(new DecoderBuffer(data, data_size, NULL, 0));
56}
57
58// static
59scoped_refptr<DecoderBuffer> DecoderBuffer::CopyFrom(const uint8* data,
60                                                     int data_size,
61                                                     const uint8* side_data,
62                                                     int side_data_size) {
63  // If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
64  CHECK(data);
65  CHECK(side_data);
66  return make_scoped_refptr(new DecoderBuffer(data, data_size,
67                                              side_data, side_data_size));
68}
69
70// static
71scoped_refptr<DecoderBuffer> DecoderBuffer::CreateEOSBuffer() {
72  return make_scoped_refptr(new DecoderBuffer(NULL, 0, NULL, 0));
73}
74
75std::string DecoderBuffer::AsHumanReadableString() {
76  if (end_of_stream()) {
77    return "end of stream";
78  }
79
80  std::ostringstream s;
81  s << "timestamp: " << timestamp_.InMicroseconds()
82    << " duration: " << duration_.InMicroseconds()
83    << " size: " << size_
84    << " side_data_size: " << side_data_size_
85    << " encrypted: " << (decrypt_config_ != NULL)
86    << " discard_padding (ms): (" << discard_padding_.first.InMilliseconds()
87    << ", " << discard_padding_.second.InMilliseconds() << ")";
88  return s.str();
89}
90
91void DecoderBuffer::set_timestamp(base::TimeDelta timestamp) {
92  DCHECK(!end_of_stream());
93  timestamp_ = timestamp;
94}
95
96}  // namespace media
97