2010-04-01 20:03:07 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2013-09-05 10:29:38 -07:00
|
|
|
#include "MediaDecoderReader.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#ifdef MOZ_OMX_DECODER
|
|
|
|
#include "GrallocImages.h"
|
|
|
|
#endif
|
2012-11-19 07:11:21 -08:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2010-04-27 01:53:44 -07:00
|
|
|
#include "VideoUtils.h"
|
2012-08-20 21:06:46 -07:00
|
|
|
#include "ImageContainer.h"
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2012-01-11 00:23:07 -08:00
|
|
|
#include "mozilla/mozalloc.h"
|
2013-07-30 07:25:31 -07:00
|
|
|
#include <stdint.h>
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2012-01-11 00:23:07 -08:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using layers::ImageContainer;
|
|
|
|
using layers::PlanarYCbCrImage;
|
2013-10-01 17:57:50 -07:00
|
|
|
using layers::PlanarYCbCrData;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-01-27 22:36:03 -08:00
|
|
|
// Verify these values are sane. Once we've checked the frame sizes, we then
|
|
|
|
// can do less integer overflow checking.
|
2013-11-11 00:03:59 -08:00
|
|
|
static_assert(MAX_VIDEO_WIDTH < PlanarYCbCrImage::MAX_DIMENSION,
|
|
|
|
"MAX_VIDEO_WIDTH is too large");
|
|
|
|
static_assert(MAX_VIDEO_HEIGHT < PlanarYCbCrImage::MAX_DIMENSION,
|
|
|
|
"MAX_VIDEO_HEIGHT is too large");
|
|
|
|
static_assert(PlanarYCbCrImage::MAX_DIMENSION < UINT32_MAX / PlanarYCbCrImage::MAX_DIMENSION,
|
|
|
|
"MAX_DIMENSION*MAX_DIMENSION doesn't fit in 32 bits");
|
2011-01-27 22:36:03 -08:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
|
|
//#define SEEK_LOGGING
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 11:46:40 -08:00
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
2013-11-20 19:02:42 -08:00
|
|
|
#define DECODER_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2010-04-01 20:03:07 -07:00
|
|
|
#ifdef SEEK_LOGGING
|
2012-11-14 11:46:40 -08:00
|
|
|
#define SEEK_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2010-04-01 20:03:07 -07:00
|
|
|
#else
|
|
|
|
#define SEEK_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
#else
|
2013-11-20 19:02:42 -08:00
|
|
|
#define DECODER_LOG(type, msg)
|
2010-04-01 20:03:07 -07:00
|
|
|
#define SEEK_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
2012-04-29 20:12:42 -07:00
|
|
|
void
|
|
|
|
AudioData::EnsureAudioBuffer()
|
|
|
|
{
|
|
|
|
if (mAudioBuffer)
|
|
|
|
return;
|
|
|
|
mAudioBuffer = SharedBuffer::Create(mFrames*mChannels*sizeof(AudioDataValue));
|
|
|
|
|
|
|
|
AudioDataValue* data = static_cast<AudioDataValue*>(mAudioBuffer->Data());
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mFrames; ++i) {
|
|
|
|
for (uint32_t j = 0; j < mChannels; ++j) {
|
2012-04-29 20:12:42 -07:00
|
|
|
data[j*mFrames + i] = mAudioData[i*mChannels + j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2010-05-18 20:04:33 -07:00
|
|
|
ValidatePlane(const VideoData::YCbCrBuffer::Plane& aPlane)
|
|
|
|
{
|
|
|
|
return aPlane.mWidth <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPlane.mHeight <= PlanarYCbCrImage::MAX_DIMENSION &&
|
2011-01-27 22:36:03 -08:00
|
|
|
aPlane.mWidth * aPlane.mHeight < MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT &&
|
2010-05-18 20:04:33 -07:00
|
|
|
aPlane.mStride > 0;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2012-08-21 03:22:58 -07:00
|
|
|
static bool
|
|
|
|
IsYV12Format(const VideoData::YCbCrBuffer::Plane& aYPlane,
|
|
|
|
const VideoData::YCbCrBuffer::Plane& aCbPlane,
|
|
|
|
const VideoData::YCbCrBuffer::Plane& aCrPlane)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
aYPlane.mWidth % 2 == 0 &&
|
|
|
|
aYPlane.mHeight % 2 == 0 &&
|
|
|
|
aYPlane.mWidth / 2 == aCbPlane.mWidth &&
|
|
|
|
aYPlane.mHeight / 2 == aCbPlane.mHeight &&
|
|
|
|
aCbPlane.mWidth == aCrPlane.mWidth &&
|
|
|
|
aCbPlane.mHeight == aCrPlane.mHeight;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-11-28 11:40:07 -08:00
|
|
|
VideoInfo::ValidateVideoRegion(const nsIntSize& aFrame,
|
2013-10-24 19:44:58 -07:00
|
|
|
const nsIntRect& aPicture,
|
|
|
|
const nsIntSize& aDisplay)
|
2011-01-27 22:36:03 -08:00
|
|
|
{
|
|
|
|
return
|
|
|
|
aFrame.width <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aFrame.height <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aFrame.width * aFrame.height <= MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT &&
|
|
|
|
aFrame.width * aFrame.height != 0 &&
|
|
|
|
aPicture.width <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPicture.x < PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPicture.x + aPicture.width < PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPicture.height <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPicture.y < PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPicture.y + aPicture.height < PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aPicture.width * aPicture.height <= MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT &&
|
|
|
|
aPicture.width * aPicture.height != 0 &&
|
|
|
|
aDisplay.width <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aDisplay.height <= PlanarYCbCrImage::MAX_DIMENSION &&
|
|
|
|
aDisplay.width * aDisplay.height <= MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT &&
|
|
|
|
aDisplay.width * aDisplay.height != 0;
|
|
|
|
}
|
|
|
|
|
2013-10-24 19:44:58 -07:00
|
|
|
VideoData::VideoData(int64_t aOffset, int64_t aTime, int64_t aDuration, int64_t aTimecode)
|
|
|
|
: MediaData(VIDEO_FRAME, aOffset, aTime, aDuration),
|
2012-08-20 21:06:46 -07:00
|
|
|
mTimecode(aTimecode),
|
|
|
|
mDuplicate(true),
|
|
|
|
mKeyframe(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(VideoData);
|
2013-10-24 19:44:58 -07:00
|
|
|
NS_ASSERTION(mDuration >= 0, "Frame must have non-negative duration.");
|
2012-08-20 21:06:46 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
VideoData::VideoData(int64_t aOffset,
|
2013-10-24 19:44:58 -07:00
|
|
|
int64_t aTime,
|
|
|
|
int64_t aDuration,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntSize aDisplay)
|
|
|
|
: MediaData(VIDEO_FRAME, aOffset, aTime, aDuration),
|
|
|
|
mDisplay(aDisplay),
|
2012-08-20 21:06:46 -07:00
|
|
|
mTimecode(aTimecode),
|
|
|
|
mDuplicate(false),
|
|
|
|
mKeyframe(aKeyframe)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(VideoData);
|
2013-10-24 19:44:58 -07:00
|
|
|
NS_ASSERTION(mDuration >= 0, "Frame must have non-negative duration.");
|
2012-08-20 21:06:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoData::~VideoData()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(VideoData);
|
|
|
|
}
|
|
|
|
|
2013-10-24 19:44:58 -07:00
|
|
|
/* static */
|
|
|
|
VideoData* VideoData::ShallowCopyUpdateDuration(VideoData* aOther,
|
|
|
|
int64_t aDuration)
|
|
|
|
{
|
|
|
|
VideoData* v = new VideoData(aOther->mOffset,
|
|
|
|
aOther->mTime,
|
|
|
|
aDuration,
|
|
|
|
aOther->mKeyframe,
|
|
|
|
aOther->mTimecode,
|
|
|
|
aOther->mDisplay);
|
|
|
|
v->mImage = aOther->mImage;
|
|
|
|
return v;
|
|
|
|
}
|
2012-08-20 21:06:46 -07:00
|
|
|
|
2012-11-28 11:40:07 -08:00
|
|
|
VideoData* VideoData::Create(VideoInfo& aInfo,
|
2010-05-18 20:04:33 -07:00
|
|
|
ImageContainer* aContainer,
|
2013-03-13 13:11:15 -07:00
|
|
|
Image* aImage,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
int64_t aDuration,
|
2010-05-18 20:04:33 -07:00
|
|
|
const YCbCrBuffer& aBuffer,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aKeyframe,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aTimecode,
|
2011-06-23 15:08:54 -07:00
|
|
|
nsIntRect aPicture)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2013-03-13 13:11:15 -07:00
|
|
|
if (!aImage && !aContainer) {
|
2012-04-29 20:12:42 -07:00
|
|
|
// Create a dummy VideoData with no image. This gives us something to
|
|
|
|
// send to media streams if necessary.
|
|
|
|
nsAutoPtr<VideoData> v(new VideoData(aOffset,
|
|
|
|
aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
aDuration,
|
2012-04-29 20:12:42 -07:00
|
|
|
aKeyframe,
|
|
|
|
aTimecode,
|
|
|
|
aInfo.mDisplay));
|
|
|
|
return v.forget();
|
2010-05-18 20:04:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following situation should never happen unless there is a bug
|
|
|
|
// in the decoder
|
|
|
|
if (aBuffer.mPlanes[1].mWidth != aBuffer.mPlanes[2].mWidth ||
|
|
|
|
aBuffer.mPlanes[1].mHeight != aBuffer.mPlanes[2].mHeight) {
|
|
|
|
NS_ERROR("C planes with different sizes");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-18 20:04:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following situations could be triggered by invalid input
|
2011-06-23 15:08:54 -07:00
|
|
|
if (aPicture.width <= 0 || aPicture.height <= 0) {
|
2010-05-18 20:04:33 -07:00
|
|
|
NS_WARNING("Empty picture rect");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-18 20:04:33 -07:00
|
|
|
}
|
|
|
|
if (!ValidatePlane(aBuffer.mPlanes[0]) || !ValidatePlane(aBuffer.mPlanes[1]) ||
|
|
|
|
!ValidatePlane(aBuffer.mPlanes[2])) {
|
|
|
|
NS_WARNING("Invalid plane size");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-18 20:04:33 -07:00
|
|
|
}
|
2011-01-27 22:36:03 -08:00
|
|
|
|
2010-05-18 20:04:33 -07:00
|
|
|
// Ensure the picture size specified in the headers can be extracted out of
|
|
|
|
// the frame we've been supplied without indexing out of bounds.
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedUint32 xLimit = aPicture.x + CheckedUint32(aPicture.width);
|
|
|
|
CheckedUint32 yLimit = aPicture.y + CheckedUint32(aPicture.height);
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!xLimit.isValid() || xLimit.value() > aBuffer.mPlanes[0].mStride ||
|
|
|
|
!yLimit.isValid() || yLimit.value() > aBuffer.mPlanes[0].mHeight)
|
2010-05-18 20:04:33 -07:00
|
|
|
{
|
|
|
|
// The specified picture dimensions can't be contained inside the video
|
|
|
|
// frame, we'll stomp memory if we try to copy it. Fail.
|
|
|
|
NS_WARNING("Overflowing picture rect");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-05-18 20:04:33 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 15:08:54 -07:00
|
|
|
nsAutoPtr<VideoData> v(new VideoData(aOffset,
|
|
|
|
aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
aDuration,
|
2011-06-23 15:08:54 -07:00
|
|
|
aKeyframe,
|
|
|
|
aTimecode,
|
|
|
|
aInfo.mDisplay));
|
2012-08-21 03:22:58 -07:00
|
|
|
const YCbCrBuffer::Plane &Y = aBuffer.mPlanes[0];
|
|
|
|
const YCbCrBuffer::Plane &Cb = aBuffer.mPlanes[1];
|
|
|
|
const YCbCrBuffer::Plane &Cr = aBuffer.mPlanes[2];
|
|
|
|
|
2013-03-13 13:11:15 -07:00
|
|
|
if (!aImage) {
|
|
|
|
// Currently our decoder only knows how to output to PLANAR_YCBCR
|
|
|
|
// format.
|
|
|
|
ImageFormat format[2] = {PLANAR_YCBCR, GRALLOC_PLANAR_YCBCR};
|
|
|
|
if (IsYV12Format(Y, Cb, Cr)) {
|
|
|
|
v->mImage = aContainer->CreateImage(format, 2);
|
|
|
|
} else {
|
|
|
|
v->mImage = aContainer->CreateImage(format, 1);
|
|
|
|
}
|
2012-08-21 03:22:58 -07:00
|
|
|
} else {
|
2013-03-13 13:11:15 -07:00
|
|
|
v->mImage = aImage;
|
2012-08-21 03:22:58 -07:00
|
|
|
}
|
2013-03-13 13:11:15 -07:00
|
|
|
|
2010-05-18 20:04:33 -07:00
|
|
|
if (!v->mImage) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2012-08-21 03:22:58 -07:00
|
|
|
NS_ASSERTION(v->mImage->GetFormat() == PLANAR_YCBCR ||
|
|
|
|
v->mImage->GetFormat() == GRALLOC_PLANAR_YCBCR,
|
2010-05-18 20:04:33 -07:00
|
|
|
"Wrong format?");
|
|
|
|
PlanarYCbCrImage* videoImage = static_cast<PlanarYCbCrImage*>(v->mImage.get());
|
|
|
|
|
2013-10-01 17:57:50 -07:00
|
|
|
PlanarYCbCrData data;
|
2012-08-25 20:22:51 -07:00
|
|
|
data.mYChannel = Y.mData + Y.mOffset;
|
2012-05-31 17:54:23 -07:00
|
|
|
data.mYSize = gfxIntSize(Y.mWidth, Y.mHeight);
|
|
|
|
data.mYStride = Y.mStride;
|
2012-08-21 03:18:20 -07:00
|
|
|
data.mYSkip = Y.mSkip;
|
2012-08-25 20:22:51 -07:00
|
|
|
data.mCbChannel = Cb.mData + Cb.mOffset;
|
|
|
|
data.mCrChannel = Cr.mData + Cr.mOffset;
|
2012-05-31 17:54:23 -07:00
|
|
|
data.mCbCrSize = gfxIntSize(Cb.mWidth, Cb.mHeight);
|
|
|
|
data.mCbCrStride = Cb.mStride;
|
2012-08-21 03:18:20 -07:00
|
|
|
data.mCbSkip = Cb.mSkip;
|
|
|
|
data.mCrSkip = Cr.mSkip;
|
2011-06-23 15:08:54 -07:00
|
|
|
data.mPicX = aPicture.x;
|
|
|
|
data.mPicY = aPicture.y;
|
|
|
|
data.mPicSize = gfxIntSize(aPicture.width, aPicture.height);
|
2010-11-02 16:43:29 -07:00
|
|
|
data.mStereoMode = aInfo.mStereoMode;
|
2010-05-18 20:04:33 -07:00
|
|
|
|
2012-09-03 00:36:04 -07:00
|
|
|
videoImage->SetDelayedConversion(true);
|
2013-03-13 13:11:15 -07:00
|
|
|
if (!aImage) {
|
|
|
|
videoImage->SetData(data);
|
|
|
|
} else {
|
|
|
|
videoImage->SetDataNoCopy(data);
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
return v.forget();
|
|
|
|
}
|
|
|
|
|
2013-03-13 13:11:15 -07:00
|
|
|
VideoData* VideoData::Create(VideoInfo& aInfo,
|
|
|
|
ImageContainer* aContainer,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
int64_t aDuration,
|
2013-03-13 13:11:15 -07:00
|
|
|
const YCbCrBuffer& aBuffer,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture)
|
|
|
|
{
|
2013-10-24 19:44:58 -07:00
|
|
|
return Create(aInfo, aContainer, nullptr, aOffset, aTime, aDuration, aBuffer,
|
2013-03-13 13:11:15 -07:00
|
|
|
aKeyframe, aTimecode, aPicture);
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoData* VideoData::Create(VideoInfo& aInfo,
|
|
|
|
Image* aImage,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
int64_t aDuration,
|
2013-03-13 13:11:15 -07:00
|
|
|
const YCbCrBuffer& aBuffer,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture)
|
|
|
|
{
|
2013-10-24 19:44:58 -07:00
|
|
|
return Create(aInfo, nullptr, aImage, aOffset, aTime, aDuration, aBuffer,
|
2013-03-13 13:11:15 -07:00
|
|
|
aKeyframe, aTimecode, aPicture);
|
|
|
|
}
|
|
|
|
|
2012-12-12 14:53:58 -08:00
|
|
|
VideoData* VideoData::CreateFromImage(VideoInfo& aInfo,
|
|
|
|
ImageContainer* aContainer,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
int64_t aDuration,
|
2012-12-12 14:53:58 -08:00
|
|
|
const nsRefPtr<Image>& aImage,
|
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture)
|
|
|
|
{
|
|
|
|
nsAutoPtr<VideoData> v(new VideoData(aOffset,
|
|
|
|
aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
aDuration,
|
2012-12-12 14:53:58 -08:00
|
|
|
aKeyframe,
|
|
|
|
aTimecode,
|
|
|
|
aInfo.mDisplay));
|
|
|
|
v->mImage = aImage;
|
|
|
|
return v.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:21:22 -07:00
|
|
|
#ifdef MOZ_OMX_DECODER
|
2012-11-28 11:40:07 -08:00
|
|
|
VideoData* VideoData::Create(VideoInfo& aInfo,
|
2012-09-26 21:33:43 -07:00
|
|
|
ImageContainer* aContainer,
|
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
int64_t aDuration,
|
2013-03-13 13:11:15 -07:00
|
|
|
mozilla::layers::GraphicBufferLocked* aBuffer,
|
2012-09-26 21:33:43 -07:00
|
|
|
bool aKeyframe,
|
|
|
|
int64_t aTimecode,
|
|
|
|
nsIntRect aPicture)
|
|
|
|
{
|
|
|
|
if (!aContainer) {
|
|
|
|
// Create a dummy VideoData with no image. This gives us something to
|
|
|
|
// send to media streams if necessary.
|
|
|
|
nsAutoPtr<VideoData> v(new VideoData(aOffset,
|
|
|
|
aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
aDuration,
|
2012-09-26 21:33:43 -07:00
|
|
|
aKeyframe,
|
|
|
|
aTimecode,
|
|
|
|
aInfo.mDisplay));
|
|
|
|
return v.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following situations could be triggered by invalid input
|
|
|
|
if (aPicture.width <= 0 || aPicture.height <= 0) {
|
|
|
|
NS_WARNING("Empty picture rect");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the picture size specified in the headers can be extracted out of
|
|
|
|
// the frame we've been supplied without indexing out of bounds.
|
|
|
|
CheckedUint32 xLimit = aPicture.x + CheckedUint32(aPicture.width);
|
|
|
|
CheckedUint32 yLimit = aPicture.y + CheckedUint32(aPicture.height);
|
|
|
|
if (!xLimit.isValid() || !yLimit.isValid())
|
|
|
|
{
|
|
|
|
// The specified picture dimensions can't be contained inside the video
|
|
|
|
// frame, we'll stomp memory if we try to copy it. Fail.
|
|
|
|
NS_WARNING("Overflowing picture rect");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<VideoData> v(new VideoData(aOffset,
|
|
|
|
aTime,
|
2013-10-24 19:44:58 -07:00
|
|
|
aDuration,
|
2012-09-26 21:33:43 -07:00
|
|
|
aKeyframe,
|
|
|
|
aTimecode,
|
|
|
|
aInfo.mDisplay));
|
|
|
|
|
2013-07-24 19:13:35 -07:00
|
|
|
ImageFormat format = GRALLOC_PLANAR_YCBCR;
|
2012-09-26 21:33:43 -07:00
|
|
|
v->mImage = aContainer->CreateImage(&format, 1);
|
|
|
|
if (!v->mImage) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-07-24 19:13:35 -07:00
|
|
|
NS_ASSERTION(v->mImage->GetFormat() == GRALLOC_PLANAR_YCBCR,
|
2012-09-26 21:33:43 -07:00
|
|
|
"Wrong format?");
|
2013-07-24 19:13:35 -07:00
|
|
|
typedef mozilla::layers::GrallocImage GrallocImage;
|
|
|
|
GrallocImage* videoImage = static_cast<GrallocImage*>(v->mImage.get());
|
|
|
|
GrallocImage::GrallocData data;
|
2012-09-26 21:33:43 -07:00
|
|
|
|
|
|
|
data.mPicSize = gfxIntSize(aPicture.width, aPicture.height);
|
|
|
|
data.mGraphicBuffer = aBuffer;
|
|
|
|
|
|
|
|
videoImage->SetData(data);
|
|
|
|
|
|
|
|
return v.forget();
|
|
|
|
}
|
2013-05-02 14:21:22 -07:00
|
|
|
#endif // MOZ_OMX_DECODER
|
2012-09-26 21:33:43 -07:00
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void* MediaDecoderReader::VideoQueueMemoryFunctor::operator()(void* anObject) {
|
2012-08-20 21:06:46 -07:00
|
|
|
const VideoData* v = static_cast<const VideoData*>(anObject);
|
|
|
|
if (!v->mImage) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-07 13:33:56 -07:00
|
|
|
if (v->mImage->GetFormat() == PLANAR_YCBCR) {
|
|
|
|
mozilla::layers::PlanarYCbCrImage* vi = static_cast<mozilla::layers::PlanarYCbCrImage*>(v->mImage.get());
|
|
|
|
mResult += vi->GetDataSize();
|
|
|
|
}
|
2012-08-20 21:06:46 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-19 07:11:21 -08:00
|
|
|
MediaDecoderReader::MediaDecoderReader(AbstractMediaDecoder* aDecoder)
|
2013-08-29 02:43:44 -07:00
|
|
|
: mDecoder(aDecoder),
|
|
|
|
mIgnoreAudioOutputFormat(false)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_CTOR(MediaDecoderReader);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaDecoderReader::~MediaDecoderReader()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
ResetDecode();
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_DTOR(MediaDecoderReader);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult MediaDecoderReader::ResetDecode()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
2012-12-06 15:27:08 -08:00
|
|
|
VideoQueue().Reset();
|
|
|
|
AudioQueue().Reset();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
VideoData* MediaDecoderReader::DecodeToFirstVideoData()
|
2012-11-06 14:33:01 -08:00
|
|
|
{
|
|
|
|
bool eof = false;
|
2012-12-06 15:27:08 -08:00
|
|
|
while (!eof && VideoQueue().GetSize() == 0) {
|
2012-11-06 14:33:01 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->IsShutdown()) {
|
2012-11-06 14:33:01 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool keyframeSkip = false;
|
|
|
|
eof = !DecodeVideoFrame(keyframeSkip, 0);
|
|
|
|
}
|
|
|
|
VideoData* d = nullptr;
|
2012-12-06 15:27:08 -08:00
|
|
|
return (d = VideoQueue().PeekFront()) ? d : nullptr;
|
2012-11-06 14:33:01 -08:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
AudioData* MediaDecoderReader::DecodeToFirstAudioData()
|
2012-11-06 14:33:01 -08:00
|
|
|
{
|
|
|
|
bool eof = false;
|
2012-12-06 15:27:08 -08:00
|
|
|
while (!eof && AudioQueue().GetSize() == 0) {
|
2012-11-06 14:33:01 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->IsShutdown()) {
|
2012-11-06 14:33:01 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eof = !DecodeAudioData();
|
|
|
|
}
|
|
|
|
AudioData* d = nullptr;
|
2012-12-06 15:27:08 -08:00
|
|
|
return (d = AudioQueue().PeekFront()) ? d : nullptr;
|
2012-11-06 14:33:01 -08:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
VideoData* MediaDecoderReader::FindStartTime(int64_t& aOutStartTime)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnStateMachineThread() || mDecoder->OnDecodeThread(),
|
|
|
|
"Should be on state machine or decode thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Extract the start times of the bitstreams in order to calculate
|
|
|
|
// the duration.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t videoStartTime = INT64_MAX;
|
|
|
|
int64_t audioStartTime = INT64_MAX;
|
2012-07-30 07:20:58 -07:00
|
|
|
VideoData* videoData = nullptr;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
if (HasVideo()) {
|
2012-11-06 14:33:01 -08:00
|
|
|
videoData = DecodeToFirstVideoData();
|
2010-04-01 20:03:07 -07:00
|
|
|
if (videoData) {
|
|
|
|
videoStartTime = videoData->mTime;
|
2013-11-20 19:02:42 -08:00
|
|
|
DECODER_LOG(PR_LOG_DEBUG, ("MediaDecoderReader::FindStartTime() video=%lld", videoStartTime));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
2012-11-06 14:33:01 -08:00
|
|
|
AudioData* audioData = DecodeToFirstAudioData();
|
2011-08-15 22:19:51 -07:00
|
|
|
if (audioData) {
|
|
|
|
audioStartTime = audioData->mTime;
|
2013-11-20 19:02:42 -08:00
|
|
|
DECODER_LOG(PR_LOG_DEBUG, ("MediaDecoderReader::FindStartTime() audio=%lld", audioStartTime));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
int64_t startTime = std::min(videoStartTime, audioStartTime);
|
2012-01-11 00:23:07 -08:00
|
|
|
if (startTime != INT64_MAX) {
|
2010-04-01 20:03:07 -07:00
|
|
|
aOutStartTime = startTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
return videoData;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult MediaDecoderReader::DecodeToTarget(int64_t aTarget)
|
2010-08-12 19:28:15 -07:00
|
|
|
{
|
2013-11-20 19:02:42 -08:00
|
|
|
DECODER_LOG(PR_LOG_DEBUG, ("MediaDecoderReader::DecodeToTarget(%lld) Begin", aTarget));
|
2013-08-12 21:49:25 -07:00
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
// Decode forward to the target frame. Start with video, if we have it.
|
|
|
|
if (HasVideo()) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool eof = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t startTime = -1;
|
2011-09-29 21:15:24 -07:00
|
|
|
nsAutoPtr<VideoData> video;
|
2010-08-12 19:28:15 -07:00
|
|
|
while (HasVideo() && !eof) {
|
2012-12-06 15:27:08 -08:00
|
|
|
while (VideoQueue().GetSize() == 0 && !eof) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool skip = false;
|
2010-08-12 19:28:15 -07:00
|
|
|
eof = !DecodeVideoFrame(skip, 0);
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->IsShutdown()) {
|
2010-08-12 19:28:15 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-06 15:27:08 -08:00
|
|
|
if (VideoQueue().GetSize() == 0) {
|
2011-09-29 21:15:24 -07:00
|
|
|
// Hit end of file, we want to display the last frame of the video.
|
|
|
|
if (video) {
|
2012-12-06 15:27:08 -08:00
|
|
|
VideoQueue().PushFront(video.forget());
|
2011-09-29 21:15:24 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-12-06 15:27:08 -08:00
|
|
|
video = VideoQueue().PeekFront();
|
2010-08-12 19:28:15 -07:00
|
|
|
// If the frame end time is less than the seek target, we won't want
|
|
|
|
// to display this frame after the seek, so discard it.
|
2013-10-24 19:44:58 -07:00
|
|
|
if (video && video->GetEndTime() <= aTarget) {
|
2010-08-12 19:28:15 -07:00
|
|
|
if (startTime == -1) {
|
|
|
|
startTime = video->mTime;
|
|
|
|
}
|
2012-12-06 15:27:08 -08:00
|
|
|
VideoQueue().PopFront();
|
2010-08-12 19:28:15 -07:00
|
|
|
} else {
|
|
|
|
video.forget();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->IsShutdown()) {
|
2010-08-12 19:28:15 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2013-11-20 19:02:42 -08:00
|
|
|
DECODER_LOG(PR_LOG_DEBUG, ("First video frame after decode is %lld", startTime));
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasAudio()) {
|
|
|
|
// Decode audio forward to the seek target.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool eof = false;
|
2010-08-12 19:28:15 -07:00
|
|
|
while (HasAudio() && !eof) {
|
2012-12-06 15:27:08 -08:00
|
|
|
while (!eof && AudioQueue().GetSize() == 0) {
|
2010-08-12 19:28:15 -07:00
|
|
|
eof = !DecodeAudioData();
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
2012-11-19 07:11:21 -08:00
|
|
|
if (mDecoder->IsShutdown()) {
|
2010-08-12 19:28:15 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-06 15:27:08 -08:00
|
|
|
const AudioData* audio = AudioQueue().PeekFront();
|
2011-07-08 18:10:40 -07:00
|
|
|
if (!audio)
|
|
|
|
break;
|
2013-09-26 22:22:38 -07:00
|
|
|
CheckedInt64 startFrame = UsecsToFrames(audio->mTime, mInfo.mAudio.mRate);
|
|
|
|
CheckedInt64 targetFrame = UsecsToFrames(aTarget, mInfo.mAudio.mRate);
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!startFrame.isValid() || !targetFrame.isValid()) {
|
2011-07-08 18:10:40 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-22 04:28:06 -08:00
|
|
|
if (startFrame.value() + audio->mFrames <= targetFrame.value()) {
|
2011-09-26 20:31:18 -07:00
|
|
|
// Our seek target lies after the frames in this AudioData. Pop it
|
2011-07-08 18:10:40 -07:00
|
|
|
// off the queue, and keep decoding forwards.
|
2012-12-06 15:27:08 -08:00
|
|
|
delete AudioQueue().PopFront();
|
2012-07-30 07:20:58 -07:00
|
|
|
audio = nullptr;
|
2011-07-08 18:10:40 -07:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-22 04:28:06 -08:00
|
|
|
if (startFrame.value() > targetFrame.value()) {
|
2011-07-31 19:45:58 -07:00
|
|
|
// The seek target doesn't lie in the audio block just after the last
|
2011-09-26 20:31:18 -07:00
|
|
|
// audio frames we've seen which were before the seek target. This
|
2011-07-31 19:45:58 -07:00
|
|
|
// could have been the first audio data we've seen after seek, i.e. the
|
|
|
|
// seek terminated after the seek target in the audio stream. Just
|
|
|
|
// abort the audio decode-to-target, the state machine will play
|
|
|
|
// silence to cover the gap. Typically this happens in poorly muxed
|
|
|
|
// files.
|
|
|
|
NS_WARNING("Audio not synced after seek, maybe a poorly muxed file?");
|
|
|
|
break;
|
|
|
|
}
|
2011-07-08 18:10:40 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// The seek target lies somewhere in this AudioData's frames, strip off
|
|
|
|
// any frames which lie before the seek target, so we'll begin playback
|
2011-07-08 18:10:40 -07:00
|
|
|
// exactly at the seek target.
|
2012-02-22 04:28:06 -08:00
|
|
|
NS_ASSERTION(targetFrame.value() >= startFrame.value(),
|
|
|
|
"Target must at or be after data start.");
|
|
|
|
NS_ASSERTION(targetFrame.value() < startFrame.value() + audio->mFrames,
|
|
|
|
"Data must end after target.");
|
2011-07-08 18:10:40 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t framesToPrune = targetFrame.value() - startFrame.value();
|
2011-09-26 20:31:18 -07:00
|
|
|
if (framesToPrune > audio->mFrames) {
|
|
|
|
// We've messed up somehow. Don't try to trim frames, the |frames|
|
2011-07-08 18:10:40 -07:00
|
|
|
// variable below will overflow.
|
2011-09-26 20:31:18 -07:00
|
|
|
NS_WARNING("Can't prune more frames that we have!");
|
2010-08-12 19:28:15 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t frames = audio->mFrames - static_cast<uint32_t>(framesToPrune);
|
|
|
|
uint32_t channels = audio->mChannels;
|
2011-09-26 20:31:18 -07:00
|
|
|
nsAutoArrayPtr<AudioDataValue> audioData(new AudioDataValue[frames * channels]);
|
2011-07-08 18:10:40 -07:00
|
|
|
memcpy(audioData.get(),
|
2011-09-26 20:31:18 -07:00
|
|
|
audio->mAudioData.get() + (framesToPrune * channels),
|
|
|
|
frames * channels * sizeof(AudioDataValue));
|
2013-09-26 22:22:38 -07:00
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, mInfo.mAudio.mRate);
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!duration.isValid()) {
|
2011-07-08 18:10:40 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-08-15 22:19:51 -07:00
|
|
|
nsAutoPtr<AudioData> data(new AudioData(audio->mOffset,
|
2011-07-08 18:10:40 -07:00
|
|
|
aTarget,
|
2012-02-22 04:28:06 -08:00
|
|
|
duration.value(),
|
2011-09-26 20:31:18 -07:00
|
|
|
frames,
|
2011-07-08 18:10:40 -07:00
|
|
|
audioData.forget(),
|
|
|
|
channels));
|
2012-12-06 15:27:08 -08:00
|
|
|
delete AudioQueue().PopFront();
|
|
|
|
AudioQueue().PushFront(data.forget());
|
2011-07-08 18:10:40 -07:00
|
|
|
break;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-12 21:49:25 -07:00
|
|
|
|
2013-11-20 19:02:42 -08:00
|
|
|
DECODER_LOG(PR_LOG_DEBUG, ("MediaDecoderReader::DecodeToTarget(%lld) End", aTarget));
|
2013-08-12 21:49:25 -07:00
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-20 20:31:05 -07:00
|
|
|
nsresult
|
|
|
|
MediaDecoderReader::GetBuffered(mozilla::dom::TimeRanges* aBuffered,
|
|
|
|
int64_t aStartTime)
|
|
|
|
{
|
|
|
|
MediaResource* stream = mDecoder->GetResource();
|
|
|
|
int64_t durationUs = 0;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
durationUs = mDecoder->GetMediaDuration();
|
|
|
|
}
|
|
|
|
GetEstimatedBufferedTimeRanges(stream, durationUs, aBuffered);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|
2010-04-01 20:03:07 -07:00
|
|
|
|