2010-07-26 12:35:16 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Brad Lassey <blassey@mozilla.com>
|
|
|
|
* Kyle Huey <me@kylehuey.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsBuiltinDecoderStateMachine.h"
|
|
|
|
#include "nsBuiltinDecoder.h"
|
|
|
|
#include "nsRawReader.h"
|
|
|
|
#include "nsRawDecoder.h"
|
|
|
|
#include "VideoUtils.h"
|
|
|
|
|
|
|
|
nsRawReader::nsRawReader(nsBuiltinDecoder* aDecoder)
|
|
|
|
: nsBuiltinDecoderReader(aDecoder),
|
2010-07-26 12:36:20 -07:00
|
|
|
mCurrentFrame(0), mFrameSize(0)
|
2010-07-26 12:35:16 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsRawReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRawReader::~nsRawReader()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsRawReader);
|
|
|
|
}
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
nsresult nsRawReader::Init(nsBuiltinDecoderReader* aCloneDonor)
|
2010-07-26 12:35:16 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsRawReader::ResetDecode()
|
|
|
|
{
|
|
|
|
mCurrentFrame = 0;
|
|
|
|
return nsBuiltinDecoderReader::ResetDecode();
|
|
|
|
}
|
|
|
|
|
2011-03-23 20:53:03 -07:00
|
|
|
nsresult nsRawReader::ReadMetadata(nsVideoInfo* aInfo)
|
2010-07-26 12:35:16 -07:00
|
|
|
{
|
2011-07-18 14:35:58 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(),
|
|
|
|
"Should be on decode thread.");
|
2010-07-26 12:35:16 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
2010-07-26 12:35:16 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
if (!ReadFromResource(resource, reinterpret_cast<PRUint8*>(&mMetadata),
|
|
|
|
sizeof(mMetadata)))
|
2010-07-26 12:35:16 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Validate the header
|
|
|
|
if (!(mMetadata.headerPacketID == 0 /* Packet ID of 0 for the header*/ &&
|
|
|
|
mMetadata.codecID == RAW_ID /* "YUV" */ &&
|
|
|
|
mMetadata.majorVersion == 0 &&
|
|
|
|
mMetadata.minorVersion == 1))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRUint32 dummy;
|
|
|
|
if (!MulOverflow32(mMetadata.frameWidth, mMetadata.frameHeight, dummy))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-06-23 15:08:54 -07:00
|
|
|
|
2010-07-26 12:35:16 -07:00
|
|
|
if (mMetadata.aspectDenominator == 0 ||
|
|
|
|
mMetadata.framerateDenominator == 0)
|
|
|
|
return NS_ERROR_FAILURE; // Invalid data
|
2011-06-23 15:08:54 -07:00
|
|
|
|
|
|
|
// Determine and verify frame display size.
|
|
|
|
float pixelAspectRatio = static_cast<float>(mMetadata.aspectNumerator) /
|
2010-07-26 12:35:16 -07:00
|
|
|
mMetadata.aspectDenominator;
|
2011-06-23 15:08:54 -07:00
|
|
|
nsIntSize display(mMetadata.frameWidth, mMetadata.frameHeight);
|
|
|
|
ScaleDisplayByAspectRatio(display, pixelAspectRatio);
|
|
|
|
mPicture = nsIntRect(0, 0, mMetadata.frameWidth, mMetadata.frameHeight);
|
|
|
|
nsIntSize frameSize(mMetadata.frameWidth, mMetadata.frameHeight);
|
|
|
|
if (!nsVideoInfo::ValidateVideoRegion(frameSize, mPicture, display)) {
|
|
|
|
// Video track's frame sizes will overflow. Fail.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mInfo.mHasVideo = true;
|
|
|
|
mInfo.mHasAudio = false;
|
2011-06-23 15:08:54 -07:00
|
|
|
mInfo.mDisplay = display;
|
2010-07-26 12:35:16 -07:00
|
|
|
|
|
|
|
mFrameRate = static_cast<float>(mMetadata.framerateNumerator) /
|
|
|
|
mMetadata.framerateDenominator;
|
|
|
|
|
|
|
|
// Make some sanity checks
|
|
|
|
if (mFrameRate > 45 ||
|
|
|
|
mFrameRate == 0 ||
|
2011-06-23 15:08:54 -07:00
|
|
|
pixelAspectRatio == 0 ||
|
2010-07-26 12:35:16 -07:00
|
|
|
mMetadata.frameWidth > 2000 ||
|
|
|
|
mMetadata.frameHeight > 2000 ||
|
|
|
|
mMetadata.chromaChannelBpp != 4 ||
|
|
|
|
mMetadata.lumaChannelBpp != 8 ||
|
|
|
|
mMetadata.colorspace != 1 /* 4:2:0 */)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mFrameSize = mMetadata.frameWidth * mMetadata.frameHeight *
|
|
|
|
(mMetadata.lumaChannelBpp + mMetadata.chromaChannelBpp) / 8.0 +
|
|
|
|
sizeof(nsRawPacketHeader);
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
PRInt64 length = resource->GetLength();
|
2010-07-26 12:35:16 -07:00
|
|
|
if (length != -1) {
|
2011-05-23 17:23:13 -07:00
|
|
|
mozilla::ReentrantMonitorAutoEnter autoMonitor(mDecoder->GetReentrantMonitor());
|
2011-04-13 15:12:23 -07:00
|
|
|
mDecoder->GetStateMachine()->SetDuration(USECS_PER_S *
|
2010-07-26 12:35:16 -07:00
|
|
|
(length - sizeof(nsRawVideoHeader)) /
|
|
|
|
(mFrameSize * mFrameRate));
|
|
|
|
}
|
|
|
|
|
2011-03-23 20:53:03 -07:00
|
|
|
*aInfo = mInfo;
|
|
|
|
|
2010-07-26 12:35:16 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsRawReader::DecodeAudioData()
|
2010-07-26 12:35:16 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoder->OnStateMachineThread() || mDecoder->OnDecodeThread(),
|
|
|
|
"Should be on state machine thread or decode thread.");
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method that either reads until it gets aLength bytes
|
2011-09-29 16:34:37 -07:00
|
|
|
// or returns false
|
2012-02-14 20:35:01 -08:00
|
|
|
bool nsRawReader::ReadFromResource(MediaResource *aResource, PRUint8* aBuf,
|
2010-07-26 12:35:16 -07:00
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
while (aLength > 0) {
|
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
rv = aResource->Read(reinterpret_cast<char*>(aBuf), aLength, &bytesRead);
|
2011-09-29 16:34:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2010-07-26 12:35:16 -07:00
|
|
|
|
|
|
|
if (bytesRead == 0) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aLength -= bytesRead;
|
|
|
|
aBuf += bytesRead;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsRawReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
2010-07-26 12:35:16 -07:00
|
|
|
PRInt64 aTimeThreshold)
|
|
|
|
{
|
2011-07-18 14:35:58 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(),
|
|
|
|
"Should be on decode thread.");
|
2010-07-26 12:35:16 -07:00
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// Record number of frames decoded and parsed. Automatically update the
|
|
|
|
// stats counters using the AutoNotifyDecoded stack-based class.
|
|
|
|
PRUint32 parsed = 0, decoded = 0;
|
|
|
|
nsMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);
|
|
|
|
|
2010-07-26 12:36:20 -07:00
|
|
|
if (!mFrameSize)
|
2011-09-29 16:34:37 -07:00
|
|
|
return false; // Metadata read failed. We should refuse to play.
|
2010-07-26 12:36:20 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 currentFrameTime = USECS_PER_S * mCurrentFrame / mFrameRate;
|
2010-07-26 12:35:16 -07:00
|
|
|
PRUint32 length = mFrameSize - sizeof(nsRawPacketHeader);
|
|
|
|
|
2011-04-04 04:41:02 -07:00
|
|
|
nsAutoArrayPtr<PRUint8> buffer(new PRUint8[length]);
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
2010-07-26 12:35:16 -07:00
|
|
|
|
|
|
|
// We're always decoding one frame when called
|
|
|
|
while(true) {
|
|
|
|
nsRawPacketHeader header;
|
|
|
|
|
|
|
|
// Read in a packet header and validate
|
2012-02-14 20:35:01 -08:00
|
|
|
if (!(ReadFromResource(resource, reinterpret_cast<PRUint8*>(&header),
|
|
|
|
sizeof(header))) ||
|
2010-07-26 12:35:16 -07:00
|
|
|
!(header.packetID == 0xFF && header.codecID == RAW_ID /* "YUV" */)) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
if (!ReadFromResource(resource, buffer, length)) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
parsed++;
|
|
|
|
|
2010-07-26 12:35:16 -07:00
|
|
|
if (currentFrameTime >= aTimeThreshold)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mCurrentFrame++;
|
2011-04-13 15:12:23 -07:00
|
|
|
currentFrameTime += static_cast<double>(USECS_PER_S) / mFrameRate;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoData::YCbCrBuffer b;
|
|
|
|
b.mPlanes[0].mData = buffer;
|
|
|
|
b.mPlanes[0].mStride = mMetadata.frameWidth * mMetadata.lumaChannelBpp / 8.0;
|
|
|
|
b.mPlanes[0].mHeight = mMetadata.frameHeight;
|
|
|
|
b.mPlanes[0].mWidth = mMetadata.frameWidth;
|
|
|
|
|
|
|
|
PRUint32 cbcrStride = mMetadata.frameWidth * mMetadata.chromaChannelBpp / 8.0;
|
|
|
|
|
|
|
|
b.mPlanes[1].mData = buffer + mMetadata.frameHeight * b.mPlanes[0].mStride;
|
|
|
|
b.mPlanes[1].mStride = cbcrStride;
|
|
|
|
b.mPlanes[1].mHeight = mMetadata.frameHeight / 2;
|
|
|
|
b.mPlanes[1].mWidth = mMetadata.frameWidth / 2;
|
|
|
|
|
|
|
|
b.mPlanes[2].mData = b.mPlanes[1].mData + mMetadata.frameHeight * cbcrStride / 2;
|
|
|
|
b.mPlanes[2].mStride = cbcrStride;
|
|
|
|
b.mPlanes[2].mHeight = mMetadata.frameHeight / 2;
|
|
|
|
b.mPlanes[2].mWidth = mMetadata.frameWidth / 2;
|
|
|
|
|
|
|
|
VideoData *v = VideoData::Create(mInfo,
|
|
|
|
mDecoder->GetImageContainer(),
|
|
|
|
-1,
|
|
|
|
currentFrameTime,
|
2011-04-13 15:12:23 -07:00
|
|
|
currentFrameTime + (USECS_PER_S / mFrameRate),
|
2010-07-26 12:35:16 -07:00
|
|
|
b,
|
|
|
|
1, // In raw video every frame is a keyframe
|
2011-06-23 15:08:54 -07:00
|
|
|
-1,
|
|
|
|
mPicture);
|
2010-07-26 12:35:16 -07:00
|
|
|
if (!v)
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-07-26 12:35:16 -07:00
|
|
|
|
|
|
|
mVideoQueue.Push(v);
|
|
|
|
mCurrentFrame++;
|
2011-03-23 15:28:57 -07:00
|
|
|
decoded++;
|
2011-04-13 15:12:23 -07:00
|
|
|
currentFrameTime += USECS_PER_S / mFrameRate;
|
2010-07-26 12:35:16 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-07-26 12:35:16 -07:00
|
|
|
}
|
|
|
|
|
2010-08-19 15:50:37 -07:00
|
|
|
nsresult nsRawReader::Seek(PRInt64 aTime, PRInt64 aStartTime, PRInt64 aEndTime, PRInt64 aCurrentTime)
|
2010-07-26 12:35:16 -07:00
|
|
|
{
|
2011-07-18 14:35:58 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(),
|
|
|
|
"Should be on decode thread.");
|
2010-07-26 12:35:16 -07:00
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource *resource = mDecoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
2010-07-26 12:35:16 -07:00
|
|
|
|
|
|
|
PRUint32 frame = mCurrentFrame;
|
|
|
|
if (aTime >= UINT_MAX)
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-04-13 15:12:23 -07:00
|
|
|
mCurrentFrame = aTime * mFrameRate / USECS_PER_S;
|
2010-07-26 12:35:16 -07:00
|
|
|
|
|
|
|
PRUint32 offset;
|
|
|
|
if (!MulOverflow32(mCurrentFrame, mFrameSize, offset))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
offset += sizeof(nsRawVideoHeader);
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
nsresult rv = resource->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
2010-07-26 12:35:16 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mVideoQueue.Erase();
|
|
|
|
|
|
|
|
while(mVideoQueue.GetSize() == 0) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool keyframeSkip = false;
|
2010-07-26 12:35:16 -07:00
|
|
|
if (!DecodeVideoFrame(keyframeSkip, 0)) {
|
|
|
|
mCurrentFrame = frame;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2011-05-23 17:23:13 -07:00
|
|
|
mozilla::ReentrantMonitorAutoEnter autoMonitor(mDecoder->GetReentrantMonitor());
|
2010-07-26 12:35:16 -07:00
|
|
|
if (mDecoder->GetDecodeState() ==
|
|
|
|
nsBuiltinDecoderStateMachine::DECODER_STATE_SHUTDOWN) {
|
|
|
|
mCurrentFrame = frame;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<VideoData> video(mVideoQueue.PeekFront());
|
|
|
|
if (video && video->mEndTime < aTime) {
|
|
|
|
mVideoQueue.PopFront();
|
|
|
|
video = nsnull;
|
|
|
|
} else {
|
|
|
|
video.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:43:00 -07:00
|
|
|
nsresult nsRawReader::GetBuffered(nsTimeRanges* aBuffered, PRInt64 aStartTime)
|
2010-08-05 00:40:35 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|