1 // Copyright 2015 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/cdm/cdm_helpers.h"
6
7 #include "base/logging.h"
8 #include "ui/gfx/color_space.h"
9
10 namespace media {
11
12 namespace {
13
14 // See ISO 23001-8:2016, section 7. Value 2 means "Unspecified".
15 constexpr cdm::ColorSpace kUnspecifiedColorSpace = {2, 2, 2,
16 cdm::ColorRange::kInvalid};
17
ToGfxColorSpaceRange(cdm::ColorRange range)18 gfx::ColorSpace::RangeID ToGfxColorSpaceRange(cdm::ColorRange range) {
19 switch (range) {
20 case cdm::ColorRange::kInvalid:
21 return gfx::ColorSpace::RangeID::INVALID;
22 case cdm::ColorRange::kLimited:
23 return gfx::ColorSpace::RangeID::LIMITED;
24 case cdm::ColorRange::kFull:
25 return gfx::ColorSpace::RangeID::FULL;
26 case cdm::ColorRange::kDerived:
27 return gfx::ColorSpace::RangeID::DERIVED;
28 }
29 }
30
31 } // namespace
32
DecryptedBlockImpl()33 DecryptedBlockImpl::DecryptedBlockImpl() : buffer_(nullptr), timestamp_(0) {}
34
~DecryptedBlockImpl()35 DecryptedBlockImpl::~DecryptedBlockImpl() {
36 if (buffer_)
37 buffer_->Destroy();
38 }
39
SetDecryptedBuffer(cdm::Buffer * buffer)40 void DecryptedBlockImpl::SetDecryptedBuffer(cdm::Buffer* buffer) {
41 buffer_ = buffer;
42 }
43
DecryptedBuffer()44 cdm::Buffer* DecryptedBlockImpl::DecryptedBuffer() {
45 return buffer_;
46 }
47
SetTimestamp(int64_t timestamp)48 void DecryptedBlockImpl::SetTimestamp(int64_t timestamp) {
49 timestamp_ = timestamp;
50 }
51
Timestamp() const52 int64_t DecryptedBlockImpl::Timestamp() const {
53 return timestamp_;
54 }
55
VideoFrameImpl()56 VideoFrameImpl::VideoFrameImpl()
57 : format_(cdm::kUnknownVideoFormat),
58 color_space_(kUnspecifiedColorSpace),
59 frame_buffer_(nullptr),
60 timestamp_(0) {
61 for (uint32_t i = 0; i < cdm::kMaxPlanes; ++i) {
62 plane_offsets_[i] = 0;
63 strides_[i] = 0;
64 }
65 }
66
~VideoFrameImpl()67 VideoFrameImpl::~VideoFrameImpl() {
68 if (frame_buffer_)
69 frame_buffer_->Destroy();
70 }
71
SetFormat(cdm::VideoFormat format)72 void VideoFrameImpl::SetFormat(cdm::VideoFormat format) {
73 format_ = format;
74 }
75
Format() const76 cdm::VideoFormat VideoFrameImpl::Format() const {
77 return format_;
78 }
79
SetSize(cdm::Size size)80 void VideoFrameImpl::SetSize(cdm::Size size) {
81 size_ = size;
82 }
83
Size() const84 cdm::Size VideoFrameImpl::Size() const {
85 return size_;
86 }
87
SetFrameBuffer(cdm::Buffer * frame_buffer)88 void VideoFrameImpl::SetFrameBuffer(cdm::Buffer* frame_buffer) {
89 frame_buffer_ = frame_buffer;
90 }
91
FrameBuffer()92 cdm::Buffer* VideoFrameImpl::FrameBuffer() {
93 return frame_buffer_;
94 }
95
SetPlaneOffset(cdm::VideoPlane plane,uint32_t offset)96 void VideoFrameImpl::SetPlaneOffset(cdm::VideoPlane plane, uint32_t offset) {
97 DCHECK(plane < cdm::kMaxPlanes);
98 plane_offsets_[plane] = offset;
99 }
100
PlaneOffset(cdm::VideoPlane plane)101 uint32_t VideoFrameImpl::PlaneOffset(cdm::VideoPlane plane) {
102 DCHECK(plane < cdm::kMaxPlanes);
103 return plane_offsets_[plane];
104 }
105
SetStride(cdm::VideoPlane plane,uint32_t stride)106 void VideoFrameImpl::SetStride(cdm::VideoPlane plane, uint32_t stride) {
107 DCHECK(plane < cdm::kMaxPlanes);
108 strides_[plane] = stride;
109 }
110
Stride(cdm::VideoPlane plane)111 uint32_t VideoFrameImpl::Stride(cdm::VideoPlane plane) {
112 DCHECK(plane < cdm::kMaxPlanes);
113 return strides_[plane];
114 }
115
SetTimestamp(int64_t timestamp)116 void VideoFrameImpl::SetTimestamp(int64_t timestamp) {
117 timestamp_ = timestamp;
118 }
119
Timestamp() const120 int64_t VideoFrameImpl::Timestamp() const {
121 return timestamp_;
122 }
123
SetColorSpace(cdm::ColorSpace color_space)124 void VideoFrameImpl::SetColorSpace(cdm::ColorSpace color_space) {
125 color_space_ = color_space;
126 }
127
MediaColorSpace() const128 media::VideoColorSpace VideoFrameImpl::MediaColorSpace() const {
129 return media::VideoColorSpace(
130 color_space_.primary_id, color_space_.transfer_id, color_space_.matrix_id,
131 ToGfxColorSpaceRange(color_space_.range));
132 }
133
AudioFramesImpl()134 AudioFramesImpl::AudioFramesImpl()
135 : buffer_(nullptr), format_(cdm::kUnknownAudioFormat) {}
136
~AudioFramesImpl()137 AudioFramesImpl::~AudioFramesImpl() {
138 if (buffer_)
139 buffer_->Destroy();
140 }
141
SetFrameBuffer(cdm::Buffer * buffer)142 void AudioFramesImpl::SetFrameBuffer(cdm::Buffer* buffer) {
143 buffer_ = buffer;
144 }
145
FrameBuffer()146 cdm::Buffer* AudioFramesImpl::FrameBuffer() {
147 return buffer_;
148 }
149
SetFormat(cdm::AudioFormat format)150 void AudioFramesImpl::SetFormat(cdm::AudioFormat format) {
151 format_ = format;
152 }
153
Format() const154 cdm::AudioFormat AudioFramesImpl::Format() const {
155 return format_;
156 }
157
PassFrameBuffer()158 cdm::Buffer* AudioFramesImpl::PassFrameBuffer() {
159 cdm::Buffer* temp_buffer = buffer_;
160 buffer_ = nullptr;
161 return temp_buffer;
162 }
163
164 } // namespace media
165