2010-06-08 16:31:27 -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: */
|
|
|
|
/* ***** 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 the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
* Chris Pearce <chris@pearce.org.nz>
|
|
|
|
*
|
|
|
|
* 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 "nsError.h"
|
|
|
|
#include "nsBuiltinDecoderStateMachine.h"
|
|
|
|
#include "nsBuiltinDecoder.h"
|
2012-02-14 20:35:01 -08:00
|
|
|
#include "MediaResource.h"
|
2010-06-08 16:31:27 -07:00
|
|
|
#include "nsWebMReader.h"
|
|
|
|
#include "VideoUtils.h"
|
2010-09-13 01:45:50 -07:00
|
|
|
#include "nsTimeRanges.h"
|
2011-07-06 19:00:02 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-11-02 16:43:29 -07:00
|
|
|
using namespace mozilla::layers;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
|
|
//#define SEEK_LOGGING
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* gBuiltinDecoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gBuiltinDecoderLog, type, msg)
|
|
|
|
#ifdef SEEK_LOGGING
|
|
|
|
#define SEEK_LOG(type, msg) PR_LOG(gBuiltinDecoderLog, type, msg)
|
|
|
|
#else
|
|
|
|
#define SEEK_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#define SEEK_LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
static const unsigned NS_PER_USEC = 1000;
|
2011-01-16 19:03:00 -08:00
|
|
|
static const double NS_PER_S = 1e9;
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// If a seek request is within SEEK_DECODE_MARGIN microseconds of the
|
2011-01-16 19:03:00 -08:00
|
|
|
// current time, decode ahead from the current frame rather than performing
|
|
|
|
// a full seek.
|
2011-04-13 15:12:23 -07:00
|
|
|
static const int SEEK_DECODE_MARGIN = 250000;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2011-12-13 06:17:59 -08:00
|
|
|
template <>
|
2010-11-28 12:06:38 -08:00
|
|
|
class nsAutoRefTraits<NesteggPacketHolder> : public nsPointerRefTraits<NesteggPacketHolder>
|
2010-10-13 18:50:20 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-28 12:06:38 -08:00
|
|
|
static void Release(NesteggPacketHolder* aHolder) { delete aHolder; }
|
2010-10-13 18:50:20 -07:00
|
|
|
};
|
|
|
|
|
2012-02-14 20:35:01 -08:00
|
|
|
// Functions for reading and seeking using MediaResource required for
|
2010-06-08 16:31:27 -07:00
|
|
|
// nestegg_io. The 'user data' passed to these functions is the
|
2012-02-14 20:35:01 -08:00
|
|
|
// decoder from which the media resource is obtained.
|
2010-06-08 16:31:27 -07:00
|
|
|
static int webm_read(void *aBuffer, size_t aLength, void *aUserData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aUserData, "aUserData must point to a valid nsBuiltinDecoder");
|
|
|
|
nsBuiltinDecoder* decoder = reinterpret_cast<nsBuiltinDecoder*>(aUserData);
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = decoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool eof = false;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
char *p = static_cast<char *>(aBuffer);
|
|
|
|
while (NS_SUCCEEDED(rv) && aLength > 0) {
|
|
|
|
PRUint32 bytes = 0;
|
2012-02-14 20:35:01 -08:00
|
|
|
rv = resource->Read(p, aLength, &bytes);
|
2010-06-08 16:31:27 -07:00
|
|
|
if (bytes == 0) {
|
2011-09-29 16:34:37 -07:00
|
|
|
eof = true;
|
2010-06-08 16:31:27 -07:00
|
|
|
break;
|
|
|
|
}
|
2010-06-15 14:56:28 -07:00
|
|
|
decoder->NotifyBytesConsumed(bytes);
|
2010-06-08 16:31:27 -07:00
|
|
|
aLength -= bytes;
|
|
|
|
p += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_FAILED(rv) ? -1 : eof ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int webm_seek(int64_t aOffset, int aWhence, void *aUserData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aUserData, "aUserData must point to a valid nsBuiltinDecoder");
|
|
|
|
nsBuiltinDecoder* decoder = reinterpret_cast<nsBuiltinDecoder*>(aUserData);
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = decoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
|
|
|
nsresult rv = resource->Seek(aWhence, aOffset);
|
2010-06-08 16:31:27 -07:00
|
|
|
return NS_SUCCEEDED(rv) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t webm_tell(void *aUserData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aUserData, "aUserData must point to a valid nsBuiltinDecoder");
|
|
|
|
nsBuiltinDecoder* decoder = reinterpret_cast<nsBuiltinDecoder*>(aUserData);
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = decoder->GetResource();
|
|
|
|
NS_ASSERTION(resource, "Decoder has no media resource");
|
|
|
|
return resource->Tell();
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsWebMReader::nsWebMReader(nsBuiltinDecoder* aDecoder)
|
|
|
|
: nsBuiltinDecoderReader(aDecoder),
|
|
|
|
mContext(nsnull),
|
|
|
|
mPacketCount(0),
|
|
|
|
mChannels(0),
|
|
|
|
mVideoTrack(0),
|
|
|
|
mAudioTrack(0),
|
2011-04-13 15:12:23 -07:00
|
|
|
mAudioStartUsec(-1),
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioFrames(0),
|
2012-02-13 18:35:26 -08:00
|
|
|
mForceStereoMode(0),
|
2011-09-29 16:34:37 -07:00
|
|
|
mHasVideo(false),
|
2012-01-01 09:10:49 -08:00
|
|
|
mHasAudio(false),
|
2012-02-13 18:35:26 -08:00
|
|
|
mStereoModeForced(false)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsWebMReader);
|
2012-01-01 09:10:49 -08:00
|
|
|
|
2012-02-13 18:35:26 -08:00
|
|
|
mStereoModeForced =
|
|
|
|
NS_SUCCEEDED(Preferences::GetInt(
|
|
|
|
"media.webm.force_stereo_mode",
|
|
|
|
&mForceStereoMode));
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsWebMReader::~nsWebMReader()
|
|
|
|
{
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
mVideoPackets.Reset();
|
|
|
|
mAudioPackets.Reset();
|
|
|
|
|
2010-08-22 19:50:43 -07:00
|
|
|
vpx_codec_destroy(&mVP8);
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
vorbis_block_clear(&mVorbisBlock);
|
|
|
|
vorbis_dsp_clear(&mVorbisDsp);
|
|
|
|
vorbis_info_clear(&mVorbisInfo);
|
|
|
|
vorbis_comment_clear(&mVorbisComment);
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsWebMReader);
|
|
|
|
}
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
nsresult nsWebMReader::Init(nsBuiltinDecoderReader* aCloneDonor)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2011-12-14 21:40:22 -08:00
|
|
|
if (vpx_codec_dec_init(&mVP8, vpx_codec_vp8_dx(), NULL, 0)) {
|
2010-06-08 16:31:27 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
vorbis_info_init(&mVorbisInfo);
|
|
|
|
vorbis_comment_init(&mVorbisComment);
|
|
|
|
memset(&mVorbisDsp, 0, sizeof(vorbis_dsp_state));
|
|
|
|
memset(&mVorbisBlock, 0, sizeof(vorbis_block));
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
if (aCloneDonor) {
|
|
|
|
mBufferedState = static_cast<nsWebMReader*>(aCloneDonor)->mBufferedState;
|
|
|
|
} else {
|
|
|
|
mBufferedState = new nsWebMBufferedState;
|
|
|
|
}
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsWebMReader::ResetDecode()
|
|
|
|
{
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioFrames = 0;
|
2011-04-13 15:12:23 -07:00
|
|
|
mAudioStartUsec = -1;
|
2010-06-08 16:31:27 -07:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (NS_FAILED(nsBuiltinDecoderReader::ResetDecode())) {
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore failed results from vorbis_synthesis_restart. They
|
|
|
|
// aren't fatal and it fails when ResetDecode is called at a
|
|
|
|
// time when no vorbis data has been read.
|
|
|
|
vorbis_synthesis_restart(&mVorbisDsp);
|
|
|
|
|
|
|
|
mVideoPackets.Reset();
|
|
|
|
mAudioPackets.Reset();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsWebMReader::Cleanup()
|
|
|
|
{
|
|
|
|
if (mContext) {
|
|
|
|
nestegg_destroy(mContext);
|
|
|
|
mContext = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 20:53:03 -07:00
|
|
|
nsresult nsWebMReader::ReadMetadata(nsVideoInfo* aInfo)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
nestegg_io io;
|
|
|
|
io.read = webm_read;
|
|
|
|
io.seek = webm_seek;
|
|
|
|
io.tell = webm_tell;
|
|
|
|
io.userdata = static_cast<nsBuiltinDecoder*>(mDecoder);
|
|
|
|
int r = nestegg_init(&mContext, io, NULL);
|
|
|
|
if (r == -1) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t duration = 0;
|
|
|
|
r = nestegg_duration(mContext, &duration);
|
|
|
|
if (r == 0) {
|
2011-07-11 20:39:28 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-04-13 15:12:23 -07:00
|
|
|
mDecoder->GetStateMachine()->SetDuration(duration / NS_PER_USEC);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ntracks = 0;
|
|
|
|
r = nestegg_track_count(mContext, &ntracks);
|
|
|
|
if (r == -1) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mInfo.mHasAudio = false;
|
|
|
|
mInfo.mHasVideo = false;
|
2010-06-08 16:31:27 -07:00
|
|
|
for (PRUint32 track = 0; track < ntracks; ++track) {
|
|
|
|
int id = nestegg_track_codec_id(mContext, track);
|
|
|
|
if (id == -1) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
int type = nestegg_track_type(mContext, track);
|
|
|
|
if (!mHasVideo && type == NESTEGG_TRACK_VIDEO) {
|
|
|
|
nestegg_video_params params;
|
|
|
|
r = nestegg_track_video_params(mContext, track, ¶ms);
|
|
|
|
if (r == -1) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-01-27 22:36:03 -08:00
|
|
|
// Picture region, taking into account cropping, before scaling
|
|
|
|
// to the display size.
|
|
|
|
nsIntRect pictureRect(params.crop_left,
|
2011-06-23 15:08:54 -07:00
|
|
|
params.crop_top,
|
|
|
|
params.width - (params.crop_right + params.crop_left),
|
|
|
|
params.height - (params.crop_bottom + params.crop_top));
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// If the cropping data appears invalid then use the frame data
|
2011-01-27 22:36:03 -08:00
|
|
|
if (pictureRect.width <= 0 ||
|
|
|
|
pictureRect.height <= 0 ||
|
|
|
|
pictureRect.x < 0 ||
|
|
|
|
pictureRect.y < 0)
|
|
|
|
{
|
|
|
|
pictureRect.x = 0;
|
|
|
|
pictureRect.y = 0;
|
|
|
|
pictureRect.width = params.width;
|
|
|
|
pictureRect.height = params.height;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2011-01-27 22:36:03 -08:00
|
|
|
// Validate the container-reported frame and pictureRect sizes. This ensures
|
|
|
|
// that our video frame creation code doesn't overflow.
|
|
|
|
nsIntSize displaySize(params.display_width, params.display_height);
|
|
|
|
nsIntSize frameSize(params.width, params.height);
|
|
|
|
if (!nsVideoInfo::ValidateVideoRegion(frameSize, pictureRect, displaySize)) {
|
|
|
|
// Video track's frame sizes will overflow. Ignore the video track.
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-23 15:08:54 -07:00
|
|
|
|
2011-01-27 22:36:03 -08:00
|
|
|
mVideoTrack = track;
|
2011-09-29 16:34:37 -07:00
|
|
|
mHasVideo = true;
|
|
|
|
mInfo.mHasVideo = true;
|
2011-06-23 15:08:54 -07:00
|
|
|
|
2011-01-27 22:36:03 -08:00
|
|
|
mInfo.mDisplay = displaySize;
|
2011-06-23 15:08:54 -07:00
|
|
|
mPicture = pictureRect;
|
|
|
|
mInitialFrame = frameSize;
|
2011-01-27 22:36:03 -08:00
|
|
|
|
2010-11-02 16:43:29 -07:00
|
|
|
switch (params.stereo_mode) {
|
|
|
|
case NESTEGG_VIDEO_MONO:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_MONO;
|
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_LEFT_RIGHT:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_LEFT_RIGHT;
|
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_BOTTOM_TOP:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_BOTTOM_TOP;
|
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_TOP_BOTTOM:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_TOP_BOTTOM;
|
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_RIGHT_LEFT:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_RIGHT_LEFT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-13 18:35:26 -08:00
|
|
|
// Switch only when stereo mode is explicitly set.
|
|
|
|
if (mStereoModeForced) {
|
|
|
|
switch (mForceStereoMode) {
|
|
|
|
case 1:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_LEFT_RIGHT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_RIGHT_LEFT;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_TOP_BOTTOM;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_BOTTOM_TOP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mInfo.mStereoMode = STEREO_MODE_MONO;
|
|
|
|
}
|
2010-11-02 16:43:29 -07:00
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
else if (!mHasAudio && type == NESTEGG_TRACK_AUDIO) {
|
|
|
|
nestegg_audio_params params;
|
|
|
|
r = nestegg_track_audio_params(mContext, track, ¶ms);
|
|
|
|
if (r == -1) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAudioTrack = track;
|
2011-09-29 16:34:37 -07:00
|
|
|
mHasAudio = true;
|
|
|
|
mInfo.mHasAudio = true;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Get the Vorbis header data
|
|
|
|
unsigned int nheaders = 0;
|
|
|
|
r = nestegg_track_codec_data_count(mContext, track, &nheaders);
|
|
|
|
if (r == -1 || nheaders != 3) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 header = 0; header < nheaders; ++header) {
|
|
|
|
unsigned char* data = 0;
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
r = nestegg_track_codec_data(mContext, track, header, &data, &length);
|
|
|
|
if (r == -1) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
ogg_packet opacket = InitOggPacket(data, length, header == 0, false, 0);
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
r = vorbis_synthesis_headerin(&mVorbisInfo,
|
|
|
|
&mVorbisComment,
|
|
|
|
&opacket);
|
2010-10-16 12:57:45 -07:00
|
|
|
if (r != 0) {
|
2010-06-08 16:31:27 -07:00
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = vorbis_synthesis_init(&mVorbisDsp, &mVorbisInfo);
|
2010-10-16 12:57:45 -07:00
|
|
|
if (r != 0) {
|
2010-06-08 16:31:27 -07:00
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = vorbis_block_init(&mVorbisDsp, &mVorbisBlock);
|
2010-10-16 12:57:45 -07:00
|
|
|
if (r != 0) {
|
2010-06-08 16:31:27 -07:00
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInfo.mAudioRate = mVorbisDsp.vi->rate;
|
|
|
|
mInfo.mAudioChannels = mVorbisDsp.vi->channels;
|
|
|
|
mChannels = mInfo.mAudioChannels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 20:53:03 -07:00
|
|
|
*aInfo = mInfo;
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ogg_packet nsWebMReader::InitOggPacket(unsigned char* aData,
|
|
|
|
size_t aLength,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aBOS,
|
|
|
|
bool aEOS,
|
2010-06-08 16:31:27 -07:00
|
|
|
PRInt64 aGranulepos)
|
|
|
|
{
|
|
|
|
ogg_packet packet;
|
|
|
|
packet.packet = aData;
|
|
|
|
packet.bytes = aLength;
|
|
|
|
packet.b_o_s = aBOS;
|
|
|
|
packet.e_o_s = aEOS;
|
|
|
|
packet.granulepos = aGranulepos;
|
|
|
|
packet.packetno = mPacketCount++;
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsWebMReader::DecodeAudioPacket(nestegg_packet* aPacket, PRInt64 aOffset)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
int r = 0;
|
|
|
|
unsigned int count = 0;
|
|
|
|
r = nestegg_packet_count(aPacket, &count);
|
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t tstamp = 0;
|
|
|
|
r = nestegg_packet_tstamp(aPacket, &tstamp);
|
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
const PRUint32 rate = mVorbisDsp.vi->rate;
|
2011-04-13 15:12:23 -07:00
|
|
|
PRUint64 tstamp_usecs = tstamp / NS_PER_USEC;
|
|
|
|
if (mAudioStartUsec == -1) {
|
2010-08-12 19:28:15 -07:00
|
|
|
// This is the first audio chunk. Assume the start time of our decode
|
|
|
|
// is the start of this chunk.
|
2011-04-13 15:12:23 -07:00
|
|
|
mAudioStartUsec = tstamp_usecs;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2011-08-15 22:19:51 -07:00
|
|
|
// If there's a gap between the start of this audio chunk and the end of
|
|
|
|
// the previous audio chunk, we need to increment the packet count so that
|
2010-08-12 19:28:15 -07:00
|
|
|
// the vorbis decode doesn't use data from before the gap to help decode
|
|
|
|
// from after the gap.
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedInt64 tstamp_frames = UsecsToFrames(tstamp_usecs, rate);
|
|
|
|
CheckedInt64 decoded_frames = UsecsToFrames(mAudioStartUsec, rate);
|
|
|
|
if (!tstamp_frames.valid() || !decoded_frames.valid()) {
|
|
|
|
NS_WARNING("Int overflow converting WebM times to frames");
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2012-02-22 04:28:06 -08:00
|
|
|
decoded_frames += mAudioFrames;
|
|
|
|
if (!decoded_frames.valid()) {
|
2011-09-26 20:31:18 -07:00
|
|
|
NS_WARNING("Int overflow adding decoded_frames");
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2012-02-22 04:28:06 -08:00
|
|
|
if (tstamp_frames.value() > decoded_frames.value()) {
|
2010-08-12 19:28:15 -07:00
|
|
|
#ifdef DEBUG
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedInt64 usecs = FramesToUsecs(tstamp_frames.value() - decoded_frames.value(), rate);
|
2011-09-26 20:31:18 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("WebMReader detected gap of %lld, %lld frames, in audio stream\n",
|
2012-02-22 04:28:06 -08:00
|
|
|
usecs.valid() ? usecs.value(): -1,
|
|
|
|
tstamp_frames.value() - decoded_frames.value()));
|
2010-08-12 19:28:15 -07:00
|
|
|
#endif
|
|
|
|
mPacketCount++;
|
2011-04-13 15:12:23 -07:00
|
|
|
mAudioStartUsec = tstamp_usecs;
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioFrames = 0;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 total_frames = 0;
|
2010-06-08 16:31:27 -07:00
|
|
|
for (PRUint32 i = 0; i < count; ++i) {
|
|
|
|
unsigned char* data;
|
|
|
|
size_t length;
|
|
|
|
r = nestegg_packet_data(aPacket, i, &data, &length);
|
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
ogg_packet opacket = InitOggPacket(data, length, false, false, -1);
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
if (vorbis_synthesis(&mVorbisBlock, &opacket) != 0) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vorbis_synthesis_blockin(&mVorbisDsp,
|
|
|
|
&mVorbisBlock) != 0) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-10-18 19:55:45 -07:00
|
|
|
VorbisPCMValue** pcm = 0;
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 frames = 0;
|
|
|
|
while ((frames = vorbis_synthesis_pcmout(&mVorbisDsp, &pcm)) > 0) {
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * mChannels]);
|
2010-10-18 19:55:45 -07:00
|
|
|
for (PRUint32 j = 0; j < mChannels; ++j) {
|
|
|
|
VorbisPCMValue* channel = pcm[j];
|
2011-09-26 20:31:18 -07:00
|
|
|
for (PRUint32 i = 0; i < PRUint32(frames); ++i) {
|
2010-10-18 19:55:45 -07:00
|
|
|
buffer[i*mChannels + j] = MOZ_CONVERT_VORBIS_SAMPLE(channel[i]);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, rate);
|
|
|
|
if (!duration.valid()) {
|
2010-08-12 19:28:15 -07:00
|
|
|
NS_WARNING("Int overflow converting WebM audio duration");
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedInt64 total_duration = FramesToUsecs(total_frames, rate);
|
|
|
|
if (!total_duration.valid()) {
|
2010-08-12 19:28:15 -07:00
|
|
|
NS_WARNING("Int overflow converting WebM audio total_duration");
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedInt64 time = total_duration + tstamp_usecs;
|
|
|
|
if (!time.valid()) {
|
|
|
|
NS_WARNING("Int overflow adding total_duration and tstamp_usecs");
|
|
|
|
nestegg_free_packet(aPacket);
|
|
|
|
return PR_FALSE;
|
|
|
|
};
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
total_frames += frames;
|
2011-08-15 22:19:51 -07:00
|
|
|
mAudioQueue.Push(new AudioData(aOffset,
|
2012-02-22 04:28:06 -08:00
|
|
|
time.value(),
|
|
|
|
duration.value(),
|
2011-09-26 20:31:18 -07:00
|
|
|
frames,
|
2011-07-28 18:54:21 -07:00
|
|
|
buffer.forget(),
|
|
|
|
mChannels));
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioFrames += frames;
|
|
|
|
if (vorbis_synthesis_read(&mVorbisDsp, frames) != 0) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
nsReturnRef<NesteggPacketHolder> nsWebMReader::NextPacket(TrackType aTrackType)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
|
|
|
// The packet queue that packets will be pushed on if they
|
|
|
|
// are not the type we are interested in.
|
|
|
|
PacketQueue& otherPackets =
|
|
|
|
aTrackType == VIDEO ? mAudioPackets : mVideoPackets;
|
|
|
|
|
|
|
|
// The packet queue for the type that we are interested in.
|
|
|
|
PacketQueue &packets =
|
|
|
|
aTrackType == VIDEO ? mVideoPackets : mAudioPackets;
|
|
|
|
|
|
|
|
// Flag to indicate that we do need to playback these types of
|
|
|
|
// packets.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasType = aTrackType == VIDEO ? mHasVideo : mHasAudio;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Flag to indicate that we do need to playback the other type
|
|
|
|
// of track.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasOtherType = aTrackType == VIDEO ? mHasAudio : mHasVideo;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Track we are interested in
|
|
|
|
PRUint32 ourTrack = aTrackType == VIDEO ? mVideoTrack : mAudioTrack;
|
|
|
|
|
|
|
|
// Value of other track
|
|
|
|
PRUint32 otherTrack = aTrackType == VIDEO ? mAudioTrack : mVideoTrack;
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
nsAutoRef<NesteggPacketHolder> holder;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
if (packets.GetSize() > 0) {
|
2010-11-28 12:06:38 -08:00
|
|
|
holder.own(packets.PopFront());
|
2010-10-13 18:50:20 -07:00
|
|
|
} else {
|
2010-06-08 16:31:27 -07:00
|
|
|
// Keep reading packets until we find a packet
|
|
|
|
// for the track we want.
|
|
|
|
do {
|
2010-11-28 12:06:38 -08:00
|
|
|
nestegg_packet* packet;
|
|
|
|
int r = nestegg_read_packet(mContext, &packet);
|
2010-06-08 16:31:27 -07:00
|
|
|
if (r <= 0) {
|
2010-11-28 12:06:38 -08:00
|
|
|
return nsReturnRef<NesteggPacketHolder>();
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2012-02-14 20:35:01 -08:00
|
|
|
PRInt64 offset = mDecoder->GetResource()->Tell();
|
2010-11-28 12:06:38 -08:00
|
|
|
holder.own(new NesteggPacketHolder(packet, offset));
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
unsigned int track = 0;
|
|
|
|
r = nestegg_packet_track(packet, &track);
|
|
|
|
if (r == -1) {
|
2010-11-28 12:06:38 -08:00
|
|
|
return nsReturnRef<NesteggPacketHolder>();
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasOtherType && otherTrack == track) {
|
|
|
|
// Save the packet for when we want these packets
|
2010-11-28 12:06:38 -08:00
|
|
|
otherPackets.Push(holder.disown());
|
2010-06-08 16:31:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The packet is for the track we want to play
|
|
|
|
if (hasType && ourTrack == track) {
|
|
|
|
break;
|
|
|
|
}
|
2011-09-29 16:34:37 -07:00
|
|
|
} while (true);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
return holder.out();
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsWebMReader::DecodeAudioData()
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
nsAutoRef<NesteggPacketHolder> holder(NextPacket(AUDIO));
|
|
|
|
if (!holder) {
|
2010-06-08 16:31:27 -07:00
|
|
|
mAudioQueue.Finish();
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
return DecodeAudioPacket(holder->mPacket, holder->mOffset);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsWebMReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
2010-05-30 21:02:00 -07:00
|
|
|
PRInt64 aTimeThreshold)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
2010-06-08 16:31:27 -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-11-28 12:06:38 -08:00
|
|
|
nsAutoRef<NesteggPacketHolder> holder(NextPacket(VIDEO));
|
|
|
|
if (!holder) {
|
2010-06-08 16:31:27 -07:00
|
|
|
mVideoQueue.Finish();
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
nestegg_packet* packet = holder->mPacket;
|
2010-06-08 16:31:27 -07:00
|
|
|
unsigned int track = 0;
|
2010-10-13 18:50:20 -07:00
|
|
|
int r = nestegg_packet_track(packet, &track);
|
2010-06-08 16:31:27 -07:00
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int count = 0;
|
|
|
|
r = nestegg_packet_count(packet, &count);
|
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t tstamp = 0;
|
|
|
|
r = nestegg_packet_tstamp(packet, &tstamp);
|
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-05-30 21:02:00 -07:00
|
|
|
// The end time of this frame is the start time of the next frame. Fetch
|
|
|
|
// the timestamp of the next packet for this track. If we've reached the
|
2012-02-14 20:35:01 -08:00
|
|
|
// end of the resource, use the file's duration as the end time of this
|
2010-05-30 21:02:00 -07:00
|
|
|
// video frame.
|
|
|
|
uint64_t next_tstamp = 0;
|
|
|
|
{
|
2010-11-28 12:06:38 -08:00
|
|
|
nsAutoRef<NesteggPacketHolder> next_holder(NextPacket(VIDEO));
|
|
|
|
if (next_holder) {
|
|
|
|
r = nestegg_packet_tstamp(next_holder->mPacket, &next_tstamp);
|
2010-05-30 21:02:00 -07:00
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-30 21:02:00 -07:00
|
|
|
}
|
2010-11-28 12:06:38 -08:00
|
|
|
mVideoPackets.PushFront(next_holder.disown());
|
2010-05-30 21:02:00 -07:00
|
|
|
} else {
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
2010-10-06 15:58:36 -07:00
|
|
|
nsBuiltinDecoderStateMachine* s =
|
|
|
|
static_cast<nsBuiltinDecoderStateMachine*>(mDecoder->GetStateMachine());
|
|
|
|
PRInt64 endTime = s->GetEndMediaTime();
|
|
|
|
if (endTime == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-05-30 21:02:00 -07:00
|
|
|
}
|
2011-04-13 15:12:23 -07:00
|
|
|
next_tstamp = endTime * NS_PER_USEC;
|
2010-05-30 21:02:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 tstamp_usecs = tstamp / NS_PER_USEC;
|
2010-06-08 16:31:27 -07:00
|
|
|
for (PRUint32 i = 0; i < count; ++i) {
|
|
|
|
unsigned char* data;
|
|
|
|
size_t length;
|
|
|
|
r = nestegg_packet_data(packet, i, &data, &length);
|
|
|
|
if (r == -1) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vpx_codec_stream_info_t si;
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.sz = sizeof(si);
|
2011-12-14 21:40:22 -08:00
|
|
|
vpx_codec_peek_stream_info(vpx_codec_vp8_dx(), data, length, &si);
|
2011-04-13 15:12:23 -07:00
|
|
|
if (aKeyframeSkip && (!si.is_kf || tstamp_usecs < aTimeThreshold)) {
|
2011-03-23 15:28:57 -07:00
|
|
|
// Skipping to next keyframe...
|
2011-03-23 15:28:57 -07:00
|
|
|
parsed++; // Assume 1 frame per chunk.
|
2011-03-23 15:28:57 -07:00
|
|
|
continue;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aKeyframeSkip && si.is_kf) {
|
2011-09-29 16:34:37 -07:00
|
|
|
aKeyframeSkip = false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
if (vpx_codec_decode(&mVP8, data, length, NULL, 0)) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the timestamp of the video frame is less than
|
|
|
|
// the time threshold required then it is not added
|
|
|
|
// to the video queue and won't be displayed.
|
2011-04-13 15:12:23 -07:00
|
|
|
if (tstamp_usecs < aTimeThreshold) {
|
2011-03-23 15:28:57 -07:00
|
|
|
parsed++; // Assume 1 frame per chunk.
|
2010-06-08 16:31:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
vpx_codec_iter_t iter = NULL;
|
|
|
|
vpx_image_t *img;
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
while ((img = vpx_codec_get_frame(&mVP8, &iter))) {
|
2010-06-08 16:31:27 -07:00
|
|
|
NS_ASSERTION(img->fmt == IMG_FMT_I420, "WebM image format is not I420");
|
|
|
|
|
|
|
|
// Chroma shifts are rounded down as per the decoding examples in the VP8 SDK
|
|
|
|
VideoData::YCbCrBuffer b;
|
|
|
|
b.mPlanes[0].mData = img->planes[0];
|
|
|
|
b.mPlanes[0].mStride = img->stride[0];
|
|
|
|
b.mPlanes[0].mHeight = img->d_h;
|
|
|
|
b.mPlanes[0].mWidth = img->d_w;
|
|
|
|
|
|
|
|
b.mPlanes[1].mData = img->planes[1];
|
|
|
|
b.mPlanes[1].mStride = img->stride[1];
|
|
|
|
b.mPlanes[1].mHeight = img->d_h >> img->y_chroma_shift;
|
|
|
|
b.mPlanes[1].mWidth = img->d_w >> img->x_chroma_shift;
|
|
|
|
|
|
|
|
b.mPlanes[2].mData = img->planes[2];
|
|
|
|
b.mPlanes[2].mStride = img->stride[2];
|
|
|
|
b.mPlanes[2].mHeight = img->d_h >> img->y_chroma_shift;
|
|
|
|
b.mPlanes[2].mWidth = img->d_w >> img->x_chroma_shift;
|
|
|
|
|
2011-06-23 15:08:54 -07:00
|
|
|
nsIntRect picture = mPicture;
|
2011-09-29 16:34:37 -07:00
|
|
|
if (img->d_w != static_cast<PRUint32>(mInitialFrame.width) ||
|
|
|
|
img->d_h != static_cast<PRUint32>(mInitialFrame.height)) {
|
2011-06-23 15:08:54 -07:00
|
|
|
// Frame size is different from what the container reports. This is legal
|
|
|
|
// in WebM, and we will preserve the ratio of the crop rectangle as it
|
|
|
|
// was reported relative to the picture size reported by the container.
|
|
|
|
picture.x = (mPicture.x * img->d_w) / mInitialFrame.width;
|
|
|
|
picture.y = (mPicture.y * img->d_h) / mInitialFrame.height;
|
|
|
|
picture.width = (img->d_w * mPicture.width) / mInitialFrame.width;
|
|
|
|
picture.height = (img->d_h * mPicture.height) / mInitialFrame.height;
|
|
|
|
}
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
VideoData *v = VideoData::Create(mInfo,
|
|
|
|
mDecoder->GetImageContainer(),
|
2010-11-28 12:06:38 -08:00
|
|
|
holder->mOffset,
|
2011-04-13 15:12:23 -07:00
|
|
|
tstamp_usecs,
|
|
|
|
next_tstamp / NS_PER_USEC,
|
2010-06-08 16:31:27 -07:00
|
|
|
b,
|
|
|
|
si.is_kf,
|
2011-06-23 15:08:54 -07:00
|
|
|
-1,
|
|
|
|
picture);
|
2010-06-08 16:31:27 -07:00
|
|
|
if (!v) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
parsed++;
|
|
|
|
decoded++;
|
|
|
|
NS_ASSERTION(decoded <= parsed,
|
|
|
|
"Expect only 1 frame per chunk per packet in WebM...");
|
2010-06-08 16:31:27 -07:00
|
|
|
mVideoQueue.Push(v);
|
|
|
|
}
|
|
|
|
}
|
2010-10-13 18:50:20 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-09-13 01:45:50 -07:00
|
|
|
nsresult nsWebMReader::Seek(PRInt64 aTarget, PRInt64 aStartTime, PRInt64 aEndTime,
|
|
|
|
PRInt64 aCurrentTime)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2011-07-11 20:39:28 -07:00
|
|
|
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
2011-11-23 15:01:50 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p About to seek to %fs", mDecoder, aTarget/1000000.0));
|
2011-07-13 14:24:35 -07:00
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
PRUint32 trackToSeek = mHasVideo ? mVideoTrack : mAudioTrack;
|
|
|
|
int r = nestegg_track_seek(mContext, trackToSeek, aTarget * NS_PER_USEC);
|
|
|
|
if (r != 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
return DecodeToTarget(aTarget);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2010-08-25 01:43:00 -07:00
|
|
|
nsresult nsWebMReader::GetBuffered(nsTimeRanges* aBuffered, PRInt64 aStartTime)
|
2010-08-05 00:40:35 -07:00
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2011-04-14 01:49:29 -07:00
|
|
|
uint64_t timecodeScale;
|
2010-09-20 17:49:50 -07:00
|
|
|
if (!mContext || nestegg_tstamp_scale(mContext, &timecodeScale) == -1) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-13 01:45:50 -07:00
|
|
|
// Special case completely cached files. This also handles local files.
|
2012-02-14 20:35:01 -08:00
|
|
|
if (resource->IsDataCachedToEndOfResource(0)) {
|
2010-09-13 01:45:50 -07:00
|
|
|
uint64_t duration = 0;
|
2010-09-20 17:49:50 -07:00
|
|
|
if (nestegg_duration(mContext, &duration) == 0) {
|
2010-10-06 15:58:36 -07:00
|
|
|
aBuffered->Add(0, duration / NS_PER_S);
|
2010-09-13 01:45:50 -07:00
|
|
|
}
|
|
|
|
} else {
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = mDecoder->GetResource();
|
|
|
|
nsTArray<MediaByteRange> ranges;
|
|
|
|
nsresult res = resource->GetCachedRanges(ranges);
|
2011-03-23 15:28:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 startTimeOffsetNS = aStartTime * NS_PER_USEC;
|
2011-03-23 15:28:58 -07:00
|
|
|
for (PRUint32 index = 0; index < ranges.Length(); index++) {
|
2010-10-06 15:58:36 -07:00
|
|
|
mBufferedState->CalculateBufferedForRange(aBuffered,
|
2011-03-23 15:28:58 -07:00
|
|
|
ranges[index].mStart,
|
|
|
|
ranges[index].mEnd,
|
2010-10-06 15:58:36 -07:00
|
|
|
timecodeScale,
|
|
|
|
startTimeOffsetNS);
|
2010-09-13 01:45:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 00:40:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-09-13 01:45:50 -07:00
|
|
|
|
2012-01-31 14:05:51 -08:00
|
|
|
void nsWebMReader::NotifyDataArrived(const char* aBuffer, PRUint32 aLength, PRInt64 aOffset)
|
2010-09-13 01:45:50 -07:00
|
|
|
{
|
2010-09-20 17:49:50 -07:00
|
|
|
mBufferedState->NotifyDataArrived(aBuffer, aLength, aOffset);
|
2010-09-13 01:45:50 -07:00
|
|
|
}
|