no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / media / VideoSegment.cpp
blobc34a5fd553b6d5edc9b34a350efe1039541dbbaa
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 file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "VideoSegment.h"
8 #include "gfx2DGlue.h"
9 #include "ImageContainer.h"
10 #include "VideoUtils.h"
11 #include "mozilla/UniquePtr.h"
13 namespace mozilla {
15 using namespace layers;
17 VideoFrame::VideoFrame(already_AddRefed<Image> aImage,
18 const gfx::IntSize& aIntrinsicSize)
19 : mImage(aImage),
20 mIntrinsicSize(aIntrinsicSize),
21 mForceBlack(false),
22 mPrincipalHandle(PRINCIPAL_HANDLE_NONE) {}
24 VideoFrame::VideoFrame()
25 : mIntrinsicSize(0, 0),
26 mForceBlack(false),
27 mPrincipalHandle(PRINCIPAL_HANDLE_NONE) {}
29 VideoFrame::~VideoFrame() = default;
31 void VideoFrame::SetNull() {
32 mImage = nullptr;
33 mIntrinsicSize = gfx::IntSize(0, 0);
34 mPrincipalHandle = PRINCIPAL_HANDLE_NONE;
37 void VideoFrame::TakeFrom(VideoFrame* aFrame) {
38 mImage = std::move(aFrame->mImage);
39 mIntrinsicSize = aFrame->mIntrinsicSize;
40 mForceBlack = aFrame->GetForceBlack();
41 mPrincipalHandle = aFrame->mPrincipalHandle;
44 /* static */
45 already_AddRefed<Image> VideoFrame::CreateBlackImage(
46 const gfx::IntSize& aSize) {
47 RefPtr<ImageContainer> container =
48 MakeAndAddRef<ImageContainer>(ImageContainer::ASYNCHRONOUS);
49 RefPtr<PlanarYCbCrImage> image = container->CreatePlanarYCbCrImage();
50 if (!image) {
51 return nullptr;
54 gfx::IntSize cbcrSize((aSize.width + 1) / 2, (aSize.height + 1) / 2);
55 int yLen = aSize.width * aSize.height;
56 int cbcrLen = cbcrSize.width * cbcrSize.height;
58 // Generate a black image.
59 auto frame = MakeUnique<uint8_t[]>(yLen + 2 * cbcrLen);
60 // Fill Y plane.
61 memset(frame.get(), 0x10, yLen);
62 // Fill Cb/Cr planes.
63 memset(frame.get() + yLen, 0x80, 2 * cbcrLen);
65 layers::PlanarYCbCrData data;
66 data.mYChannel = frame.get();
67 data.mYStride = aSize.width;
68 data.mCbCrStride = cbcrSize.width;
69 data.mCbChannel = frame.get() + yLen;
70 data.mCrChannel = data.mCbChannel + cbcrLen;
71 data.mPictureRect = gfx::IntRect(0, 0, aSize.width, aSize.height);
72 data.mStereoMode = StereoMode::MONO;
73 data.mYUVColorSpace = gfx::YUVColorSpace::BT601;
74 // This could be made FULL once bug 1568745 is complete. A black pixel being
75 // 0x00, 0x80, 0x80
76 data.mColorRange = gfx::ColorRange::LIMITED;
77 data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT;
79 // Copies data, so we can free data.
80 if (!image->CopyData(data)) {
81 return nullptr;
84 return image.forget();
87 void VideoSegment::AppendFrame(already_AddRefed<Image>&& aImage,
88 const IntSize& aIntrinsicSize,
89 const PrincipalHandle& aPrincipalHandle,
90 bool aForceBlack, TimeStamp aTimeStamp) {
91 VideoChunk* chunk = AppendChunk(0);
92 chunk->mTimeStamp = aTimeStamp;
93 VideoFrame frame(std::move(aImage), aIntrinsicSize);
94 MOZ_ASSERT_IF(!IsNull(), !aTimeStamp.IsNull());
95 frame.SetForceBlack(aForceBlack);
96 frame.SetPrincipalHandle(aPrincipalHandle);
97 chunk->mFrame.TakeFrom(&frame);
100 VideoSegment::VideoSegment()
101 : MediaSegmentBase<VideoSegment, VideoChunk>(VIDEO) {}
103 VideoSegment::VideoSegment(VideoSegment&& aSegment)
104 : MediaSegmentBase<VideoSegment, VideoChunk>(std::move(aSegment)) {}
106 VideoSegment::~VideoSegment() = default;
108 } // namespace mozilla