Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "WidevineVideoFrame.h"
7 :
8 : #include "WidevineUtils.h"
9 : #include "mozilla/CheckedInt.h"
10 : #include "mozilla/IntegerPrintfMacros.h"
11 :
12 : using namespace cdm;
13 :
14 : namespace mozilla {
15 :
16 0 : WidevineVideoFrame::WidevineVideoFrame()
17 : : mFormat(kUnknownVideoFormat)
18 : , mSize(0,0)
19 : , mBuffer(nullptr)
20 0 : , mTimestamp(0)
21 : {
22 0 : CDM_LOG("WidevineVideoFrame::WidevineVideoFrame() this=%p", this);
23 0 : memset(mPlaneOffsets, 0, sizeof(mPlaneOffsets));
24 0 : memset(mPlaneStrides, 0, sizeof(mPlaneStrides));
25 0 : }
26 :
27 0 : WidevineVideoFrame::WidevineVideoFrame(WidevineVideoFrame&& aOther)
28 0 : : mFormat(aOther.mFormat)
29 : , mSize(aOther.mSize)
30 0 : , mBuffer(aOther.mBuffer)
31 0 : , mTimestamp(aOther.mTimestamp)
32 : {
33 0 : CDM_LOG("WidevineVideoFrame::WidevineVideoFrame(WidevineVideoFrame&&) this=%p, other=%p",
34 : this, &aOther);
35 0 : memcpy(mPlaneOffsets, aOther.mPlaneOffsets, sizeof(mPlaneOffsets));
36 0 : memcpy(mPlaneStrides, aOther.mPlaneStrides, sizeof(mPlaneStrides));
37 0 : aOther.mBuffer = nullptr;
38 0 : }
39 :
40 0 : WidevineVideoFrame::~WidevineVideoFrame()
41 : {
42 0 : if (mBuffer) {
43 0 : mBuffer->Destroy();
44 0 : mBuffer = nullptr;
45 : }
46 0 : }
47 :
48 : void
49 0 : WidevineVideoFrame::SetFormat(cdm::VideoFormat aFormat)
50 : {
51 0 : CDM_LOG("WidevineVideoFrame::SetFormat(%d) this=%p", aFormat, this);
52 0 : mFormat = aFormat;
53 0 : }
54 :
55 : cdm::VideoFormat
56 0 : WidevineVideoFrame::Format() const
57 : {
58 0 : return mFormat;
59 : }
60 :
61 : void
62 0 : WidevineVideoFrame::SetSize(cdm::Size aSize)
63 : {
64 0 : CDM_LOG("WidevineVideoFrame::SetSize(%d,%d) this=%p", aSize.width, aSize.height, this);
65 0 : mSize.width = aSize.width;
66 0 : mSize.height = aSize.height;
67 0 : }
68 :
69 : cdm::Size
70 0 : WidevineVideoFrame::Size() const
71 : {
72 0 : return mSize;
73 : }
74 :
75 : void
76 0 : WidevineVideoFrame::SetFrameBuffer(cdm::Buffer* aFrameBuffer)
77 : {
78 0 : CDM_LOG("WidevineVideoFrame::SetFrameBuffer(%p) this=%p", aFrameBuffer, this);
79 0 : MOZ_ASSERT(!mBuffer);
80 0 : mBuffer = aFrameBuffer;
81 0 : }
82 :
83 : cdm::Buffer*
84 0 : WidevineVideoFrame::FrameBuffer()
85 : {
86 0 : return mBuffer;
87 : }
88 :
89 : void
90 0 : WidevineVideoFrame::SetPlaneOffset(cdm::VideoFrame::VideoPlane aPlane, uint32_t aOffset)
91 : {
92 0 : CDM_LOG("WidevineVideoFrame::SetPlaneOffset(%d, %d) this=%p", aPlane, aOffset, this);
93 0 : mPlaneOffsets[aPlane] = aOffset;
94 0 : }
95 :
96 : uint32_t
97 0 : WidevineVideoFrame::PlaneOffset(cdm::VideoFrame::VideoPlane aPlane)
98 : {
99 0 : return mPlaneOffsets[aPlane];
100 : }
101 :
102 : void
103 0 : WidevineVideoFrame::SetStride(cdm::VideoFrame::VideoPlane aPlane, uint32_t aStride)
104 : {
105 0 : CDM_LOG("WidevineVideoFrame::SetStride(%d, %d) this=%p", aPlane, aStride, this);
106 0 : mPlaneStrides[aPlane] = aStride;
107 0 : }
108 :
109 : uint32_t
110 0 : WidevineVideoFrame::Stride(cdm::VideoFrame::VideoPlane aPlane)
111 : {
112 0 : return mPlaneStrides[aPlane];
113 : }
114 :
115 : void
116 0 : WidevineVideoFrame::SetTimestamp(int64_t timestamp)
117 : {
118 0 : CDM_LOG("WidevineVideoFrame::SetTimestamp(%" PRId64 ") this=%p", timestamp, this);
119 0 : mTimestamp = timestamp;
120 0 : }
121 :
122 : int64_t
123 0 : WidevineVideoFrame::Timestamp() const
124 : {
125 0 : return mTimestamp;
126 : }
127 :
128 : bool
129 0 : WidevineVideoFrame::InitToBlack(uint32_t aWidth, uint32_t aHeight, int64_t aTimeStamp)
130 : {
131 0 : CheckedInt<size_t> ySizeChk = aWidth;
132 0 : ySizeChk *= aHeight;
133 : // If w*h didn't overflow, half of them won't.
134 0 : const size_t uSize = ((aWidth + 1) / 2) * ((aHeight + 1) / 2);
135 0 : CheckedInt<size_t> yuSizeChk = ySizeChk + uSize;
136 0 : if (!yuSizeChk.isValid()) {
137 0 : return false;
138 : }
139 0 : WidevineBuffer* buffer = new WidevineBuffer(yuSizeChk.value());
140 0 : const size_t& ySize = ySizeChk.value();
141 : // Black in YCbCr is (0,128,128).
142 0 : memset(buffer->Data(), 0, ySize);
143 0 : memset(buffer->Data() + ySize, 128, uSize);
144 0 : if (mBuffer) {
145 0 : mBuffer->Destroy();
146 0 : mBuffer = nullptr;
147 : }
148 0 : SetFormat(VideoFormat::kI420);
149 0 : SetSize(cdm::Size(aWidth, aHeight));
150 0 : SetFrameBuffer(buffer);
151 0 : SetPlaneOffset(VideoFrame::kYPlane, 0);
152 0 : SetStride(VideoFrame::kYPlane, aWidth);
153 : // Note: U and V planes are stored at the same place in order to
154 : // save memory since their contents are the same.
155 0 : SetPlaneOffset(VideoFrame::kUPlane, ySize);
156 0 : SetStride(VideoFrame::kUPlane, (aWidth + 1) / 2);
157 0 : SetPlaneOffset(VideoFrame::kVPlane, ySize);
158 0 : SetStride(VideoFrame::kVPlane, (aWidth + 1) / 2);
159 0 : SetTimestamp(aTimeStamp);
160 0 : return true;
161 : }
162 :
163 : } // namespace mozilla
|