Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "BlankDecoderModule.h"
8 :
9 : #include "mozilla/CheckedInt.h"
10 : #include "mozilla/UniquePtrExtensions.h"
11 : #include "mozilla/RefPtr.h"
12 : #include "nsRect.h"
13 : #include "nsSize.h"
14 : #include "ImageContainer.h"
15 : #include "MediaData.h"
16 : #include "MediaInfo.h"
17 : #include "VideoUtils.h"
18 :
19 : namespace mozilla {
20 :
21 0 : BlankVideoDataCreator::BlankVideoDataCreator(uint32_t aFrameWidth,
22 : uint32_t aFrameHeight,
23 0 : layers::ImageContainer* aImageContainer)
24 : : mFrameWidth(aFrameWidth)
25 : , mFrameHeight(aFrameHeight)
26 0 : , mImageContainer(aImageContainer)
27 : {
28 0 : mInfo.mDisplay = nsIntSize(mFrameWidth, mFrameHeight);
29 0 : mPicture = gfx::IntRect(0, 0, mFrameWidth, mFrameHeight);
30 0 : }
31 :
32 : already_AddRefed<MediaData>
33 0 : BlankVideoDataCreator::Create(MediaRawData* aSample)
34 : {
35 : // Create a fake YUV buffer in a 420 format. That is, an 8bpp Y plane,
36 : // with a U and V plane that are half the size of the Y plane, i.e 8 bit,
37 : // 2x2 subsampled. Have the data pointer of each frame point to the
38 : // first plane, they'll always be zero'd memory anyway.
39 0 : const CheckedUint32 size = CheckedUint32(mFrameWidth) * mFrameHeight;
40 0 : if (!size.isValid()) {
41 : // Overflow happened.
42 0 : return nullptr;
43 : }
44 0 : auto frame = MakeUniqueFallible<uint8_t[]>(size.value());
45 0 : if (!frame) {
46 0 : return nullptr;
47 : }
48 0 : memset(frame.get(), 0, mFrameWidth * mFrameHeight);
49 0 : VideoData::YCbCrBuffer buffer;
50 :
51 : // Y plane.
52 0 : buffer.mPlanes[0].mData = frame.get();
53 0 : buffer.mPlanes[0].mStride = mFrameWidth;
54 0 : buffer.mPlanes[0].mHeight = mFrameHeight;
55 0 : buffer.mPlanes[0].mWidth = mFrameWidth;
56 0 : buffer.mPlanes[0].mOffset = 0;
57 0 : buffer.mPlanes[0].mSkip = 0;
58 :
59 : // Cb plane.
60 0 : buffer.mPlanes[1].mData = frame.get();
61 0 : buffer.mPlanes[1].mStride = (mFrameWidth + 1) / 2;
62 0 : buffer.mPlanes[1].mHeight = (mFrameHeight + 1) / 2;
63 0 : buffer.mPlanes[1].mWidth = (mFrameWidth + 1) / 2;
64 0 : buffer.mPlanes[1].mOffset = 0;
65 0 : buffer.mPlanes[1].mSkip = 0;
66 :
67 : // Cr plane.
68 0 : buffer.mPlanes[2].mData = frame.get();
69 0 : buffer.mPlanes[2].mStride = (mFrameWidth + 1) / 2;
70 0 : buffer.mPlanes[2].mHeight = (mFrameHeight + 1) / 2;
71 0 : buffer.mPlanes[2].mWidth = (mFrameWidth + 1) / 2;
72 0 : buffer.mPlanes[2].mOffset = 0;
73 0 : buffer.mPlanes[2].mSkip = 0;
74 :
75 0 : return VideoData::CreateAndCopyData(mInfo,
76 : mImageContainer,
77 : aSample->mOffset,
78 : aSample->mTime,
79 : aSample->mDuration,
80 : buffer,
81 0 : aSample->mKeyframe,
82 : aSample->mTime,
83 0 : mPicture);
84 : }
85 :
86 0 : BlankAudioDataCreator::BlankAudioDataCreator(uint32_t aChannelCount, uint32_t aSampleRate)
87 0 : : mFrameSum(0), mChannelCount(aChannelCount), mSampleRate(aSampleRate)
88 : {
89 0 : }
90 :
91 : already_AddRefed<MediaData>
92 0 : BlankAudioDataCreator::Create(MediaRawData* aSample)
93 : {
94 : // Convert duration to frames. We add 1 to duration to account for
95 : // rounding errors, so we get a consistent tone.
96 : CheckedInt64 frames = UsecsToFrames(
97 0 : aSample->mDuration.ToMicroseconds()+1, mSampleRate);
98 0 : if (!frames.isValid()
99 0 : || !mChannelCount
100 0 : || !mSampleRate
101 0 : || frames.value() > (UINT32_MAX / mChannelCount)) {
102 0 : return nullptr;
103 : }
104 0 : AlignedAudioBuffer samples(frames.value() * mChannelCount);
105 0 : if (!samples) {
106 0 : return nullptr;
107 : }
108 : // Fill the sound buffer with an A4 tone.
109 : static const float pi = 3.14159265f;
110 : static const float noteHz = 440.0f;
111 0 : for (int i = 0; i < frames.value(); i++) {
112 0 : float f = sin(2 * pi * noteHz * mFrameSum / mSampleRate);
113 0 : for (unsigned c = 0; c < mChannelCount; c++) {
114 0 : samples[i * mChannelCount + c] = AudioDataValue(f);
115 : }
116 0 : mFrameSum++;
117 : }
118 : RefPtr<AudioData> data(new AudioData(aSample->mOffset,
119 : aSample->mTime,
120 : aSample->mDuration,
121 0 : uint32_t(frames.value()),
122 : Move(samples),
123 : mChannelCount,
124 0 : mSampleRate));
125 0 : return data.forget();
126 : }
127 :
128 : already_AddRefed<MediaDataDecoder>
129 0 : BlankDecoderModule::CreateVideoDecoder(const CreateDecoderParams& aParams)
130 : {
131 0 : const VideoInfo& config = aParams.VideoConfig();
132 : UniquePtr<DummyDataCreator> creator =
133 0 : MakeUnique<BlankVideoDataCreator>(config.mDisplay.width, config.mDisplay.height, aParams.mImageContainer);
134 : RefPtr<MediaDataDecoder> decoder =
135 0 : new DummyMediaDataDecoder(Move(creator), "blank media data decoder", aParams);
136 0 : return decoder.forget();
137 : }
138 :
139 : already_AddRefed<MediaDataDecoder>
140 0 : BlankDecoderModule::CreateAudioDecoder(const CreateDecoderParams& aParams)
141 : {
142 0 : const AudioInfo& config = aParams.AudioConfig();
143 : UniquePtr<DummyDataCreator> creator =
144 0 : MakeUnique<BlankAudioDataCreator>(config.mChannels, config.mRate);
145 : RefPtr<MediaDataDecoder> decoder =
146 0 : new DummyMediaDataDecoder(Move(creator), "blank media data decoder", aParams);
147 0 : return decoder.forget();
148 : }
149 :
150 : bool
151 0 : BlankDecoderModule::SupportsMimeType(const nsACString& aMimeType,
152 : DecoderDoctorDiagnostics* aDiagnostics) const
153 : {
154 0 : return true;
155 : }
156 :
157 0 : already_AddRefed<PlatformDecoderModule> CreateBlankDecoderModule()
158 : {
159 0 : RefPtr<PlatformDecoderModule> pdm = new BlankDecoderModule();
160 0 : return pdm.forget();
161 : }
162 :
163 : } // namespace mozilla
|