Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gmp/widevine-adapter/WidevineVideoFrame.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "GMPLog.h"
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
WidevineVideoFrame::WidevineVideoFrame()
17
  : mFormat(kUnknownVideoFormat)
18
  , mSize{ 0, 0 }
19
  , mBuffer(nullptr)
20
  , mTimestamp(0)
21
0
{
22
0
  MOZ_ASSERT(mSize.height == 0 && mSize.width == 0, "Size should be zeroed");
23
0
  GMP_LOG("WidevineVideoFrame::WidevineVideoFrame() this=%p", this);
24
0
  memset(mPlaneOffsets, 0, sizeof(mPlaneOffsets));
25
0
  memset(mPlaneStrides, 0, sizeof(mPlaneStrides));
26
0
}
27
28
WidevineVideoFrame::WidevineVideoFrame(WidevineVideoFrame&& aOther)
29
  : mFormat(aOther.mFormat)
30
  , mSize(aOther.mSize)
31
  , mBuffer(aOther.mBuffer)
32
  , mTimestamp(aOther.mTimestamp)
33
0
{
34
0
  GMP_LOG("WidevineVideoFrame::WidevineVideoFrame(WidevineVideoFrame&&) "
35
0
          "this=%p, other=%p",
36
0
          this,
37
0
          &aOther);
38
0
  memcpy(mPlaneOffsets, aOther.mPlaneOffsets, sizeof(mPlaneOffsets));
39
0
  memcpy(mPlaneStrides, aOther.mPlaneStrides, sizeof(mPlaneStrides));
40
0
  aOther.mBuffer = nullptr;
41
0
}
42
43
WidevineVideoFrame::~WidevineVideoFrame()
44
0
{
45
0
  if (mBuffer) {
46
0
    mBuffer->Destroy();
47
0
    mBuffer = nullptr;
48
0
  }
49
0
}
50
51
void
52
WidevineVideoFrame::SetFormat(cdm::VideoFormat aFormat)
53
0
{
54
0
  GMP_LOG("WidevineVideoFrame::SetFormat(%d) this=%p", aFormat, this);
55
0
  mFormat = aFormat;
56
0
}
57
58
cdm::VideoFormat
59
WidevineVideoFrame::Format() const
60
0
{
61
0
  return mFormat;
62
0
}
63
64
void
65
WidevineVideoFrame::SetSize(cdm::Size aSize)
66
0
{
67
0
  GMP_LOG("WidevineVideoFrame::SetSize(%d,%d) this=%p",
68
0
          aSize.width,
69
0
          aSize.height,
70
0
          this);
71
0
  mSize.width = aSize.width;
72
0
  mSize.height = aSize.height;
73
0
}
74
75
cdm::Size
76
WidevineVideoFrame::Size() const
77
0
{
78
0
  return mSize;
79
0
}
80
81
void
82
WidevineVideoFrame::SetFrameBuffer(cdm::Buffer* aFrameBuffer)
83
0
{
84
0
  GMP_LOG("WidevineVideoFrame::SetFrameBuffer(%p) this=%p", aFrameBuffer, this);
85
0
  MOZ_ASSERT(!mBuffer);
86
0
  mBuffer = aFrameBuffer;
87
0
}
88
89
cdm::Buffer*
90
WidevineVideoFrame::FrameBuffer()
91
0
{
92
0
  return mBuffer;
93
0
}
94
95
void
96
WidevineVideoFrame::SetPlaneOffset(cdm::VideoFrame::VideoPlane aPlane, uint32_t aOffset)
97
0
{
98
0
  GMP_LOG("WidevineVideoFrame::SetPlaneOffset(%d, %" PRIu32 ") this=%p",
99
0
          aPlane,
100
0
          aOffset,
101
0
          this);
102
0
  mPlaneOffsets[aPlane] = aOffset;
103
0
}
104
105
uint32_t
106
WidevineVideoFrame::PlaneOffset(cdm::VideoFrame::VideoPlane aPlane)
107
0
{
108
0
  return mPlaneOffsets[aPlane];
109
0
}
110
111
void
112
WidevineVideoFrame::SetStride(cdm::VideoFrame::VideoPlane aPlane, uint32_t aStride)
113
0
{
114
0
  GMP_LOG(
115
0
    "WidevineVideoFrame::SetStride(%d, %" PRIu32 ") this=%p", aPlane, aStride, this);
116
0
  mPlaneStrides[aPlane] = aStride;
117
0
}
118
119
uint32_t
120
WidevineVideoFrame::Stride(cdm::VideoFrame::VideoPlane aPlane)
121
0
{
122
0
  return mPlaneStrides[aPlane];
123
0
}
124
125
void
126
WidevineVideoFrame::SetTimestamp(int64_t timestamp)
127
0
{
128
0
  GMP_LOG(
129
0
    "WidevineVideoFrame::SetTimestamp(%" PRId64 ") this=%p", timestamp, this);
130
0
  mTimestamp = timestamp;
131
0
}
132
133
int64_t
134
WidevineVideoFrame::Timestamp() const
135
0
{
136
0
  return mTimestamp;
137
0
}
138
139
bool
140
WidevineVideoFrame::InitToBlack(int32_t aWidth,
141
                                int32_t aHeight,
142
                                int64_t aTimeStamp)
143
0
{
144
0
  MOZ_ASSERT(aWidth >= 0 && aHeight >= 0,
145
0
             "Frame dimensions should be positive");
146
0
  CheckedInt<size_t> ySizeChk = aWidth;
147
0
  ySizeChk *= aHeight;
148
0
  // If w*h didn't overflow, half of them won't.
149
0
  const size_t uSize = ((aWidth + 1) / 2) * ((aHeight + 1) / 2);
150
0
  CheckedInt<size_t> yuSizeChk = ySizeChk + uSize;
151
0
  if (!yuSizeChk.isValid()) {
152
0
    return false;
153
0
  }
154
0
  WidevineBuffer* buffer = new WidevineBuffer(yuSizeChk.value());
155
0
  const size_t& ySize = ySizeChk.value();
156
0
  // Black in YCbCr is (0,128,128).
157
0
  memset(buffer->Data(), 0, ySize);
158
0
  memset(buffer->Data() + ySize, 128, uSize);
159
0
  if (mBuffer) {
160
0
    mBuffer->Destroy();
161
0
    mBuffer = nullptr;
162
0
  }
163
0
  SetFormat(VideoFormat::kI420);
164
0
  SetSize(cdm::Size{ aWidth, aHeight });
165
0
  SetFrameBuffer(buffer);
166
0
  SetPlaneOffset(VideoFrame::kYPlane, 0);
167
0
  SetStride(VideoFrame::kYPlane, aWidth);
168
0
  // Note: U and V planes are stored at the same place in order to
169
0
  // save memory since their contents are the same.
170
0
  SetPlaneOffset(VideoFrame::kUPlane, ySize);
171
0
  SetStride(VideoFrame::kUPlane, (aWidth + 1) / 2);
172
0
  SetPlaneOffset(VideoFrame::kVPlane, ySize);
173
0
  SetStride(VideoFrame::kVPlane, (aWidth + 1) / 2);
174
0
  SetTimestamp(aTimeStamp);
175
0
  return true;
176
0
}
177
178
} // namespace mozilla