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: */
|
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-06-08 16:31:27 -07:00
|
|
|
#include "nsError.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2012-11-19 07:11:21 -08:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2012-02-14 20:35:01 -08:00
|
|
|
#include "MediaResource.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "WebMReader.h"
|
|
|
|
#include "WebMBufferedParser.h"
|
2013-03-02 11:14:44 -08:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2012-10-25 03:09:41 -07:00
|
|
|
#include "VorbisUtils.h"
|
2014-02-09 00:04:38 -08:00
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
|
2013-12-10 01:36:05 -08:00
|
|
|
#include <algorithm>
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2012-03-11 19:19:31 -07:00
|
|
|
#define VPX_DONT_DEFINE_STDINT_TYPES
|
|
|
|
#include "vpx/vp8dx.h"
|
|
|
|
#include "vpx/vpx_decoder.h"
|
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
#include "OggReader.h"
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
using mozilla::NesteggPacketHolder;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class nsAutoRefTraits<NesteggPacketHolder> : public nsPointerRefTraits<NesteggPacketHolder>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void Release(NesteggPacketHolder* aHolder) { delete aHolder; }
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-02-09 00:04:38 -08:00
|
|
|
using namespace gfx;
|
2012-11-14 11:45:33 -08:00
|
|
|
using namespace layers;
|
2010-06-08 16:31:27 -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;
|
2012-12-17 14:26:55 -08:00
|
|
|
PRLogModuleInfo* gNesteggLog;
|
2012-11-14 11:46:40 -08:00
|
|
|
#define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2010-06-08 16:31:27 -07:00
|
|
|
#ifdef SEEK_LOGGING
|
2012-11-14 11:46:40 -08:00
|
|
|
#define SEEK_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
|
2010-06-08 16:31:27 -07:00
|
|
|
#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;
|
2010-06-08 16:31:27 -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)
|
|
|
|
{
|
2012-11-19 07:11:21 -08:00
|
|
|
NS_ASSERTION(aUserData, "aUserData must point to a valid AbstractMediaDecoder");
|
|
|
|
AbstractMediaDecoder* decoder = reinterpret_cast<AbstractMediaDecoder*>(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) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t 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;
|
|
|
|
}
|
|
|
|
aLength -= bytes;
|
|
|
|
p += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_FAILED(rv) ? -1 : eof ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int webm_seek(int64_t aOffset, int aWhence, void *aUserData)
|
|
|
|
{
|
2012-11-19 07:11:21 -08:00
|
|
|
NS_ASSERTION(aUserData, "aUserData must point to a valid AbstractMediaDecoder");
|
|
|
|
AbstractMediaDecoder* decoder = reinterpret_cast<AbstractMediaDecoder*>(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)
|
|
|
|
{
|
2012-11-19 07:11:21 -08:00
|
|
|
NS_ASSERTION(aUserData, "aUserData must point to a valid AbstractMediaDecoder");
|
|
|
|
AbstractMediaDecoder* decoder = reinterpret_cast<AbstractMediaDecoder*>(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
|
|
|
}
|
|
|
|
|
2012-12-17 14:26:55 -08:00
|
|
|
static void webm_log(nestegg * context,
|
|
|
|
unsigned int severity,
|
|
|
|
char const * format, ...)
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
va_list args;
|
|
|
|
char msg[256];
|
|
|
|
const char * sevStr;
|
|
|
|
|
|
|
|
switch(severity) {
|
|
|
|
case NESTEGG_LOG_DEBUG:
|
|
|
|
sevStr = "DBG";
|
|
|
|
break;
|
|
|
|
case NESTEGG_LOG_INFO:
|
|
|
|
sevStr = "INF";
|
|
|
|
break;
|
|
|
|
case NESTEGG_LOG_WARNING:
|
|
|
|
sevStr = "WRN";
|
|
|
|
break;
|
|
|
|
case NESTEGG_LOG_ERROR:
|
|
|
|
sevStr = "ERR";
|
|
|
|
break;
|
|
|
|
case NESTEGG_LOG_CRITICAL:
|
|
|
|
sevStr = "CRT";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sevStr = "UNK";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
PR_snprintf(msg, sizeof(msg), "%p [Nestegg-%s] ", context, sevStr);
|
|
|
|
PR_vsnprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), format, args);
|
|
|
|
PR_LOG(gNesteggLog, PR_LOG_DEBUG, (msg));
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-19 07:11:21 -08:00
|
|
|
WebMReader::WebMReader(AbstractMediaDecoder* aDecoder)
|
2012-11-14 11:46:40 -08:00
|
|
|
: MediaDecoderReader(aDecoder),
|
2012-07-30 07:20:58 -07:00
|
|
|
mContext(nullptr),
|
2010-06-08 16:31:27 -07:00
|
|
|
mPacketCount(0),
|
|
|
|
mChannels(0),
|
2013-11-22 14:07:00 -08:00
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
mOpusParser(nullptr),
|
|
|
|
mOpusDecoder(nullptr),
|
|
|
|
mSkip(0),
|
2013-12-10 01:36:05 -08:00
|
|
|
mSeekPreroll(0),
|
2013-11-22 14:07:00 -08:00
|
|
|
#endif
|
2010-06-08 16:31:27 -07:00
|
|
|
mVideoTrack(0),
|
|
|
|
mAudioTrack(0),
|
2011-04-13 15:12:23 -07:00
|
|
|
mAudioStartUsec(-1),
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioFrames(0),
|
2014-04-01 18:08:59 -07:00
|
|
|
mAudioCodec(-1),
|
|
|
|
mVideoCodec(-1),
|
2011-09-29 16:34:37 -07:00
|
|
|
mHasVideo(false),
|
2012-05-23 19:40:09 -07:00
|
|
|
mHasAudio(false)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_CTOR(WebMReader);
|
2012-12-17 14:26:55 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gNesteggLog) {
|
|
|
|
gNesteggLog = PR_NewLogModule("Nestegg");
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-30 16:02:46 -08:00
|
|
|
// Zero these member vars to avoid crashes in VP8 destroy and Vorbis clear
|
|
|
|
// functions when destructor is called before |Init|.
|
2013-12-03 16:21:00 -08:00
|
|
|
memset(&mVPX, 0, sizeof(vpx_codec_ctx_t));
|
2012-11-30 16:02:46 -08:00
|
|
|
memset(&mVorbisBlock, 0, sizeof(vorbis_block));
|
|
|
|
memset(&mVorbisDsp, 0, sizeof(vorbis_dsp_state));
|
|
|
|
memset(&mVorbisInfo, 0, sizeof(vorbis_info));
|
|
|
|
memset(&mVorbisComment, 0, sizeof(vorbis_comment));
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
WebMReader::~WebMReader()
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
mVideoPackets.Reset();
|
|
|
|
mAudioPackets.Reset();
|
|
|
|
|
2013-12-03 16:21:00 -08:00
|
|
|
vpx_codec_destroy(&mVPX);
|
2010-08-22 19:50:43 -07:00
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
vorbis_block_clear(&mVorbisBlock);
|
|
|
|
vorbis_dsp_clear(&mVorbisDsp);
|
|
|
|
vorbis_info_clear(&mVorbisInfo);
|
|
|
|
vorbis_comment_clear(&mVorbisComment);
|
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
if (mOpusDecoder) {
|
|
|
|
opus_multistream_decoder_destroy(mOpusDecoder);
|
|
|
|
mOpusDecoder = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_DTOR(WebMReader);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult WebMReader::Init(MediaDecoderReader* aCloneDonor)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
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) {
|
2012-11-14 11:46:40 -08:00
|
|
|
mBufferedState = static_cast<WebMReader*>(aCloneDonor)->mBufferedState;
|
2010-09-20 17:49:50 -07:00
|
|
|
} else {
|
2012-11-14 11:46:40 -08:00
|
|
|
mBufferedState = new WebMBufferedState;
|
2010-09-20 17:49:50 -07:00
|
|
|
}
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult WebMReader::ResetDecode()
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
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;
|
2012-11-14 11:46:40 -08:00
|
|
|
if (NS_FAILED(MediaDecoderReader::ResetDecode())) {
|
2010-06-08 16:31:27 -07:00
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:36:05 -08:00
|
|
|
if (mAudioCodec == NESTEGG_CODEC_VORBIS) {
|
|
|
|
// 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);
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
} else if (mAudioCodec == NESTEGG_CODEC_OPUS) {
|
|
|
|
if (mOpusDecoder) {
|
|
|
|
// Reset the decoder.
|
|
|
|
opus_multistream_decoder_ctl(mOpusDecoder, OPUS_RESET_STATE);
|
|
|
|
mSkip = mOpusParser->mPreSkip;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
mVideoPackets.Reset();
|
|
|
|
mAudioPackets.Reset();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void WebMReader::Cleanup()
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
|
|
|
if (mContext) {
|
|
|
|
nestegg_destroy(mContext);
|
2012-07-30 07:20:58 -07:00
|
|
|
mContext = nullptr;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:22:38 -07:00
|
|
|
nsresult WebMReader::ReadMetadata(MediaInfo* aInfo,
|
|
|
|
MetadataTags** aTags)
|
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;
|
2012-11-19 07:11:21 -08:00
|
|
|
io.userdata = mDecoder;
|
2012-12-06 15:27:08 -08:00
|
|
|
int64_t maxOffset = -1;
|
2012-12-17 14:26:55 -08:00
|
|
|
int r = nestegg_init(&mContext, io, &webm_log, maxOffset);
|
2010-06-08 16:31:27 -07:00
|
|
|
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());
|
2012-11-19 07:11:21 -08:00
|
|
|
mDecoder->SetMediaDuration(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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t track = 0; track < ntracks; ++track) {
|
2010-06-08 16:31:27 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-03 16:21:00 -08:00
|
|
|
vpx_codec_iface_t* dx = nullptr;
|
|
|
|
mVideoCodec = nestegg_track_codec_id(mContext, track);
|
|
|
|
if (mVideoCodec == NESTEGG_CODEC_VP8) {
|
|
|
|
dx = vpx_codec_vp8_dx();
|
|
|
|
} else if (mVideoCodec == NESTEGG_CODEC_VP9) {
|
|
|
|
dx = vpx_codec_vp9_dx();
|
|
|
|
}
|
|
|
|
if (!dx || vpx_codec_dec_init(&mVPX, dx, nullptr, 0)) {
|
|
|
|
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);
|
2014-02-03 17:49:21 -08:00
|
|
|
if (!IsValidVideoRegion(frameSize, pictureRect, displaySize)) {
|
2011-01-27 22:36:03 -08:00
|
|
|
// 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;
|
2013-09-26 22:22:38 -07:00
|
|
|
mInfo.mVideo.mHasVideo = true;
|
2011-06-23 15:08:54 -07:00
|
|
|
|
2013-09-26 22:22:38 -07:00
|
|
|
mInfo.mVideo.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:
|
2014-01-30 14:58:49 -08:00
|
|
|
mInfo.mVideo.mStereoMode = StereoMode::MONO;
|
2010-11-02 16:43:29 -07:00
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_LEFT_RIGHT:
|
2014-01-30 14:58:49 -08:00
|
|
|
mInfo.mVideo.mStereoMode = StereoMode::LEFT_RIGHT;
|
2010-11-02 16:43:29 -07:00
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_BOTTOM_TOP:
|
2014-01-30 14:58:49 -08:00
|
|
|
mInfo.mVideo.mStereoMode = StereoMode::BOTTOM_TOP;
|
2010-11-02 16:43:29 -07:00
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_TOP_BOTTOM:
|
2014-01-30 14:58:49 -08:00
|
|
|
mInfo.mVideo.mStereoMode = StereoMode::TOP_BOTTOM;
|
2010-11-02 16:43:29 -07:00
|
|
|
break;
|
|
|
|
case NESTEGG_VIDEO_STEREO_RIGHT_LEFT:
|
2014-01-30 14:58:49 -08:00
|
|
|
mInfo.mVideo.mStereoMode = StereoMode::RIGHT_LEFT;
|
2010-11-02 16:43:29 -07:00
|
|
|
break;
|
|
|
|
}
|
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;
|
2013-09-26 22:22:38 -07:00
|
|
|
mInfo.mAudio.mHasAudio = true;
|
2013-11-22 14:07:00 -08:00
|
|
|
mAudioCodec = nestegg_track_codec_id(mContext, track);
|
2014-02-02 19:39:00 -08:00
|
|
|
mCodecDelay = params.codec_delay / NS_PER_USEC;
|
2013-11-22 14:07:00 -08:00
|
|
|
|
|
|
|
if (mAudioCodec == NESTEGG_CODEC_VORBIS) {
|
|
|
|
// 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;
|
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
for (uint32_t 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;
|
|
|
|
}
|
|
|
|
ogg_packet opacket = InitOggPacket(data, length, header == 0, false, 0);
|
|
|
|
|
|
|
|
r = vorbis_synthesis_headerin(&mVorbisInfo,
|
|
|
|
&mVorbisComment,
|
|
|
|
&opacket);
|
|
|
|
if (r != 0) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = vorbis_synthesis_init(&mVorbisDsp, &mVorbisInfo);
|
|
|
|
if (r != 0) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = vorbis_block_init(&mVorbisDsp, &mVorbisBlock);
|
|
|
|
if (r != 0) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
mInfo.mAudio.mRate = mVorbisDsp.vi->rate;
|
|
|
|
mInfo.mAudio.mChannels = mVorbisDsp.vi->channels;
|
|
|
|
mChannels = mInfo.mAudio.mChannels;
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
} else if (mAudioCodec == NESTEGG_CODEC_OPUS) {
|
2010-06-08 16:31:27 -07:00
|
|
|
unsigned char* data = 0;
|
|
|
|
size_t length = 0;
|
2013-11-22 14:07:00 -08:00
|
|
|
r = nestegg_track_codec_data(mContext, track, 0, &data, &length);
|
2010-06-08 16:31:27 -07:00
|
|
|
if (r == -1) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
mOpusParser = new OpusParser;
|
|
|
|
if (!mOpusParser->DecodeHeader(data, length)) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
if (!InitOpusDecoder()) {
|
2010-06-08 16:31:27 -07:00
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-02-02 19:39:00 -08:00
|
|
|
if (static_cast<int64_t>(mCodecDelay) != FramesToUsecs(mOpusParser->mPreSkip, mOpusParser->mRate).value()) {
|
|
|
|
LOG(PR_LOG_WARNING,
|
|
|
|
("Invalid Opus header: CodecDelay and pre-skip do not match!\n"));
|
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
mInfo.mAudio.mRate = mOpusParser->mRate;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
mInfo.mAudio.mChannels = mOpusParser->mChannels;
|
2013-12-10 01:36:05 -08:00
|
|
|
mChannels = mInfo.mAudio.mChannels;
|
|
|
|
mSeekPreroll = params.seek_preroll;
|
2013-11-22 14:07:00 -08:00
|
|
|
#endif
|
|
|
|
} else {
|
2010-06-08 16:31:27 -07:00
|
|
|
Cleanup();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 05:30:03 -08:00
|
|
|
// We can't seek in buffered regions if we have no cues.
|
2012-12-23 07:51:17 -08:00
|
|
|
mDecoder->SetMediaSeekable(nestegg_has_cues(mContext) == 1);
|
2012-12-07 05:30:03 -08:00
|
|
|
|
2011-03-23 20:53:03 -07:00
|
|
|
*aInfo = mInfo;
|
|
|
|
|
2012-07-30 17:14:29 -07:00
|
|
|
*aTags = nullptr;
|
|
|
|
|
2010-06-08 16:31:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
bool WebMReader::InitOpusDecoder()
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
NS_ASSERTION(mOpusDecoder == nullptr, "leaking OpusDecoder");
|
|
|
|
|
|
|
|
mOpusDecoder = opus_multistream_decoder_create(mOpusParser->mRate,
|
|
|
|
mOpusParser->mChannels,
|
|
|
|
mOpusParser->mStreams,
|
|
|
|
mOpusParser->mCoupledStreams,
|
|
|
|
mOpusParser->mMappingTable,
|
|
|
|
&r);
|
|
|
|
mSkip = mOpusParser->mPreSkip;
|
|
|
|
|
|
|
|
return r == OPUS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
ogg_packet WebMReader::InitOggPacket(unsigned char* aData,
|
2010-06-08 16:31:27 -07:00
|
|
|
size_t aLength,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aBOS,
|
|
|
|
bool aEOS,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aGranulepos)
|
2010-06-08 16:31:27 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool WebMReader::DecodeAudioPacket(nestegg_packet* aPacket, int64_t 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
|
|
|
}
|
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
const uint32_t rate = mInfo.mAudio.mRate;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t tstamp_usecs = tstamp / NS_PER_USEC;
|
2011-04-13 15:12:23 -07:00
|
|
|
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);
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!tstamp_frames.isValid() || !decoded_frames.isValid()) {
|
2012-02-22 04:28:06 -08:00
|
|
|
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;
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!decoded_frames.isValid()) {
|
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-05-14 12:50:20 -07:00
|
|
|
usecs.isValid() ? usecs.value() : -1,
|
2012-02-22 04:28:06 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t total_frames = 0;
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2010-06-08 16:31:27 -07:00
|
|
|
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
|
|
|
}
|
2013-11-22 14:07:00 -08:00
|
|
|
if (mAudioCodec == NESTEGG_CODEC_VORBIS) {
|
|
|
|
ogg_packet opacket = InitOggPacket(data, length, false, false, -1);
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
if (vorbis_synthesis(&mVorbisBlock, &opacket) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
if (vorbis_synthesis_blockin(&mVorbisDsp,
|
|
|
|
&mVorbisBlock) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
VorbisPCMValue** pcm = 0;
|
|
|
|
int32_t frames = 0;
|
|
|
|
while ((frames = vorbis_synthesis_pcmout(&mVorbisDsp, &pcm)) > 0) {
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * mChannels]);
|
|
|
|
for (uint32_t j = 0; j < mChannels; ++j) {
|
|
|
|
VorbisPCMValue* channel = pcm[j];
|
|
|
|
for (uint32_t i = 0; i < uint32_t(frames); ++i) {
|
|
|
|
buffer[i*mChannels + j] = MOZ_CONVERT_VORBIS_SAMPLE(channel[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, rate);
|
|
|
|
if (!duration.isValid()) {
|
|
|
|
NS_WARNING("Int overflow converting WebM audio duration");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CheckedInt64 total_duration = FramesToUsecs(total_frames, rate);
|
|
|
|
if (!total_duration.isValid()) {
|
|
|
|
NS_WARNING("Int overflow converting WebM audio total_duration");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedInt64 time = total_duration + tstamp_usecs;
|
|
|
|
if (!time.isValid()) {
|
|
|
|
NS_WARNING("Int overflow adding total_duration and tstamp_usecs");
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
total_frames += frames;
|
|
|
|
AudioQueue().Push(new AudioData(aOffset,
|
|
|
|
time.value(),
|
|
|
|
duration.value(),
|
|
|
|
frames,
|
|
|
|
buffer.forget(),
|
|
|
|
mChannels));
|
|
|
|
mAudioFrames += frames;
|
|
|
|
if (vorbis_synthesis_read(&mVorbisDsp, frames) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (mAudioCodec == NESTEGG_CODEC_OPUS) {
|
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
uint32_t channels = mOpusParser->mChannels;
|
|
|
|
|
|
|
|
// Maximum value is 63*2880, so there's no chance of overflow.
|
|
|
|
int32_t frames_number = opus_packet_get_nb_frames(data, length);
|
|
|
|
|
|
|
|
if (frames_number <= 0)
|
|
|
|
return false; // Invalid packet header.
|
|
|
|
int32_t samples = opus_packet_get_samples_per_frame(data,
|
|
|
|
(opus_int32) rate);
|
|
|
|
int32_t frames = frames_number*samples;
|
|
|
|
|
|
|
|
// A valid Opus packet must be between 2.5 and 120 ms long.
|
|
|
|
if (frames < 120 || frames > 5760)
|
|
|
|
return false;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * channels]);
|
|
|
|
|
|
|
|
// Decode to the appropriate sample type.
|
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
|
|
|
int ret = opus_multistream_decode_float(mOpusDecoder,
|
|
|
|
data, length,
|
|
|
|
buffer, frames, false);
|
|
|
|
#else
|
|
|
|
int ret = opus_multistream_decode(mOpusDecoder,
|
|
|
|
data, length,
|
|
|
|
buffer, frames, false);
|
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
NS_ASSERTION(ret == frames, "Opus decoded too few audio samples");
|
2013-12-10 01:36:05 -08:00
|
|
|
CheckedInt64 startTime = tstamp_usecs;
|
2013-11-22 14:07:00 -08:00
|
|
|
|
|
|
|
// Trim the initial frames while the decoder is settling.
|
|
|
|
if (mSkip > 0) {
|
|
|
|
int32_t skipFrames = std::min(mSkip, frames);
|
|
|
|
if (skipFrames == frames) {
|
|
|
|
// discard the whole packet
|
|
|
|
mSkip -= frames;
|
|
|
|
LOG(PR_LOG_DEBUG, ("Opus decoder skipping %d frames"
|
|
|
|
" (whole packet)", frames));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int32_t keepFrames = frames - skipFrames;
|
|
|
|
int samples = keepFrames * channels;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> trimBuffer(new AudioDataValue[samples]);
|
|
|
|
for (int i = 0; i < samples; i++)
|
|
|
|
trimBuffer[i] = buffer[skipFrames*channels + i];
|
2013-12-10 01:36:05 -08:00
|
|
|
startTime = startTime + FramesToUsecs(skipFrames, rate);
|
2013-11-22 14:07:00 -08:00
|
|
|
frames = keepFrames;
|
|
|
|
buffer = trimBuffer;
|
|
|
|
|
|
|
|
mSkip -= skipFrames;
|
|
|
|
LOG(PR_LOG_DEBUG, ("Opus decoder skipping %d frames", skipFrames));
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t discardPadding = 0;
|
|
|
|
r = nestegg_packet_discard_padding(aPacket, &discardPadding);
|
|
|
|
if (discardPadding > 0) {
|
|
|
|
CheckedInt64 discardFrames = UsecsToFrames(discardPadding * NS_PER_USEC, rate);
|
|
|
|
if (!discardFrames.isValid()) {
|
|
|
|
NS_WARNING("Int overflow in DiscardPadding");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int32_t keepFrames = frames - discardFrames.value();
|
|
|
|
if (keepFrames > 0) {
|
|
|
|
int samples = keepFrames * channels;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> trimBuffer(new AudioDataValue[samples]);
|
|
|
|
for (int i = 0; i < samples; i++)
|
|
|
|
trimBuffer[i] = buffer[i];
|
|
|
|
frames = keepFrames;
|
|
|
|
buffer = trimBuffer;
|
|
|
|
} else {
|
|
|
|
LOG(PR_LOG_DEBUG, ("Opus decoder discarding whole packet"
|
|
|
|
" ( %d frames) as padding", frames));
|
|
|
|
return true;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2013-11-22 14:07:00 -08:00
|
|
|
// Apply the header gain if one was specified.
|
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
|
|
|
if (mOpusParser->mGain != 1.0f) {
|
|
|
|
float gain = mOpusParser->mGain;
|
|
|
|
int samples = frames * channels;
|
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
buffer[i] *= gain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mOpusParser->mGain_Q16 != 65536) {
|
|
|
|
int64_t gain_Q16 = mOpusParser->mGain_Q16;
|
|
|
|
int samples = frames * channels;
|
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
int32_t val = static_cast<int32_t>((gain_Q16*buffer[i] + 32768)>>16);
|
|
|
|
buffer[i] = static_cast<AudioDataValue>(MOZ_CLIP_TO_15(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-26 12:06:59 -07:00
|
|
|
// No channel mapping for more than 8 channels.
|
|
|
|
if (channels > 8) {
|
|
|
|
return false;
|
2013-11-22 14:07:00 -08:00
|
|
|
}
|
|
|
|
|
2012-02-22 04:28:06 -08:00
|
|
|
CheckedInt64 duration = FramesToUsecs(frames, rate);
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!duration.isValid()) {
|
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
|
|
|
}
|
2014-02-02 19:39:00 -08:00
|
|
|
CheckedInt64 time = startTime - mCodecDelay;
|
2012-05-14 12:50:20 -07:00
|
|
|
if (!time.isValid()) {
|
2013-12-10 01:36:05 -08:00
|
|
|
NS_WARNING("Int overflow shifting tstamp by codec delay");
|
2012-04-04 02:15:10 -07:00
|
|
|
return false;
|
2012-02-22 04:28:06 -08:00
|
|
|
};
|
2013-11-22 14:07:00 -08:00
|
|
|
AudioQueue().Push(new AudioData(mDecoder->GetResource()->Tell(),
|
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));
|
2013-11-22 14:07:00 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioFrames += frames;
|
2013-11-22 14:07:00 -08:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif /* MOZ_OPUS */
|
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
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsReturnRef<NesteggPacketHolder> WebMReader::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.
|
2012-11-23 11:47:45 -08:00
|
|
|
WebMPacketQueue& otherPackets =
|
2010-06-08 16:31:27 -07:00
|
|
|
aTrackType == VIDEO ? mAudioPackets : mVideoPackets;
|
|
|
|
|
|
|
|
// The packet queue for the type that we are interested in.
|
2012-11-23 11:47:45 -08:00
|
|
|
WebMPacketQueue &packets =
|
2010-06-08 16:31:27 -07:00
|
|
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ourTrack = aTrackType == VIDEO ? mVideoTrack : mAudioTrack;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
// Value of other track
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t otherTrack = aTrackType == VIDEO ? mAudioTrack : mVideoTrack;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
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-08-22 08:56:38 -07:00
|
|
|
int64_t 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
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool WebMReader::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) {
|
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
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool WebMReader::DecodeVideoFrame(bool &aKeyframeSkip,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t 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.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t parsed = 0, decoded = 0;
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
nsAutoRef<NesteggPacketHolder> holder(NextPacket(VIDEO));
|
|
|
|
if (!holder) {
|
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;
|
2014-04-01 18:05:35 -07:00
|
|
|
nsAutoRef<NesteggPacketHolder> next_holder(NextPacket(VIDEO));
|
|
|
|
if (next_holder) {
|
|
|
|
r = nestegg_packet_tstamp(next_holder->mPacket, &next_tstamp);
|
|
|
|
if (r == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PushVideoPacket(next_holder.disown());
|
|
|
|
} else {
|
|
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
|
|
|
int64_t endTime = mDecoder->GetEndMediaTime();
|
|
|
|
if (endTime == -1) {
|
|
|
|
return false;
|
2010-05-30 21:02:00 -07:00
|
|
|
}
|
2014-04-01 18:05:35 -07:00
|
|
|
next_tstamp = endTime * NS_PER_USEC;
|
2010-05-30 21:02:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t tstamp_usecs = tstamp / NS_PER_USEC;
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2010-06-08 16:31:27 -07:00
|
|
|
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);
|
2013-12-03 16:21:00 -08:00
|
|
|
if (mVideoCodec == NESTEGG_CODEC_VP8) {
|
|
|
|
vpx_codec_peek_stream_info(vpx_codec_vp8_dx(), data, length, &si);
|
|
|
|
} else if (mVideoCodec == NESTEGG_CODEC_VP9) {
|
|
|
|
vpx_codec_peek_stream_info(vpx_codec_vp9_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
|
|
|
}
|
|
|
|
|
2013-12-03 16:21:00 -08:00
|
|
|
if (vpx_codec_decode(&mVPX, data, length, nullptr, 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;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:34:10 -07:00
|
|
|
vpx_codec_iter_t iter = nullptr;
|
2010-06-08 16:31:27 -07:00
|
|
|
vpx_image_t *img;
|
|
|
|
|
2013-12-03 16:21:00 -08:00
|
|
|
while ((img = vpx_codec_get_frame(&mVPX, &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;
|
2012-05-31 17:54:23 -07:00
|
|
|
b.mPlanes[0].mOffset = b.mPlanes[0].mSkip = 0;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
b.mPlanes[1].mData = img->planes[1];
|
|
|
|
b.mPlanes[1].mStride = img->stride[1];
|
2012-10-15 20:03:43 -07:00
|
|
|
b.mPlanes[1].mHeight = (img->d_h + 1) >> img->y_chroma_shift;
|
|
|
|
b.mPlanes[1].mWidth = (img->d_w + 1) >> img->x_chroma_shift;
|
2012-05-31 17:54:23 -07:00
|
|
|
b.mPlanes[1].mOffset = b.mPlanes[1].mSkip = 0;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
|
|
|
b.mPlanes[2].mData = img->planes[2];
|
|
|
|
b.mPlanes[2].mStride = img->stride[2];
|
2012-10-15 20:03:43 -07:00
|
|
|
b.mPlanes[2].mHeight = (img->d_h + 1) >> img->y_chroma_shift;
|
|
|
|
b.mPlanes[2].mWidth = (img->d_w + 1) >> img->x_chroma_shift;
|
2012-05-31 17:54:23 -07:00
|
|
|
b.mPlanes[2].mOffset = b.mPlanes[2].mSkip = 0;
|
2010-06-08 16:31:27 -07:00
|
|
|
|
2014-02-09 00:04:38 -08:00
|
|
|
IntRect picture = ToIntRect(mPicture);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (img->d_w != static_cast<uint32_t>(mInitialFrame.width) ||
|
|
|
|
img->d_h != static_cast<uint32_t>(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;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:22:38 -07:00
|
|
|
VideoData *v = VideoData::Create(mInfo.mVideo,
|
2010-06-08 16:31:27 -07:00
|
|
|
mDecoder->GetImageContainer(),
|
2010-11-28 12:06:38 -08:00
|
|
|
holder->mOffset,
|
2011-04-13 15:12:23 -07:00
|
|
|
tstamp_usecs,
|
2013-10-24 19:44:58 -07:00
|
|
|
(next_tstamp / NS_PER_USEC) - tstamp_usecs,
|
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...");
|
2012-12-06 15:27:08 -08:00
|
|
|
VideoQueue().Push(v);
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-12-06 15:27:08 -08:00
|
|
|
void
|
|
|
|
WebMReader::PushVideoPacket(NesteggPacketHolder* aItem)
|
|
|
|
{
|
|
|
|
mVideoPackets.PushFront(aItem);
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult WebMReader::Seek(int64_t aTarget, int64_t aStartTime, int64_t aEndTime,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t 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.");
|
|
|
|
|
2012-12-06 15:27:08 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Reader [%p] for Decoder [%p]: About to seek to %fs",
|
|
|
|
this, mDecoder, aTarget/1000000.0));
|
2011-07-13 14:24:35 -07:00
|
|
|
if (NS_FAILED(ResetDecode())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t trackToSeek = mHasVideo ? mVideoTrack : mAudioTrack;
|
2013-12-10 01:36:05 -08:00
|
|
|
uint64_t target = aTarget * NS_PER_USEC;
|
|
|
|
if (mSeekPreroll) {
|
|
|
|
target = std::max(static_cast<uint64_t>(aStartTime * NS_PER_USEC), target - mSeekPreroll);
|
|
|
|
}
|
|
|
|
int r = nestegg_track_seek(mContext, trackToSeek, target);
|
2011-07-13 14:24:35 -07:00
|
|
|
if (r != 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
2014-03-31 20:39:04 -07:00
|
|
|
return NS_OK;
|
2010-06-08 16:31:27 -07:00
|
|
|
}
|
|
|
|
|
2013-03-02 11:14:44 -08:00
|
|
|
nsresult WebMReader::GetBuffered(dom::TimeRanges* aBuffered, int64_t 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.
|
2010-09-23 18:57:30 -07:00
|
|
|
bool isFullyCached = resource->IsDataCachedToEndOfResource(0);
|
|
|
|
if (isFullyCached) {
|
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
|
|
|
}
|
2010-09-23 18:57:30 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bufferedLength = 0;
|
2010-09-23 18:57:30 -07:00
|
|
|
aBuffered->GetLength(&bufferedLength);
|
|
|
|
|
|
|
|
// Either we the file is not fully cached, or we couldn't find a duration in
|
|
|
|
// the WebM bitstream.
|
|
|
|
if (!isFullyCached || !bufferedLength) {
|
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
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < ranges.Length(); index++) {
|
|
|
|
uint64_t start, end;
|
2010-09-23 18:57:30 -07:00
|
|
|
bool rv = mBufferedState->CalculateBufferedForRange(ranges[index].mStart,
|
|
|
|
ranges[index].mEnd,
|
|
|
|
&start, &end);
|
|
|
|
if (rv) {
|
|
|
|
double startTime = start * timecodeScale / NS_PER_S - aStartTime;
|
|
|
|
double endTime = end * timecodeScale / NS_PER_S - aStartTime;
|
|
|
|
// If this range extends to the end of the file, the true end time
|
|
|
|
// is the file's duration.
|
|
|
|
if (resource->IsDataCachedToEndOfResource(ranges[index].mStart)) {
|
|
|
|
uint64_t duration = 0;
|
|
|
|
if (nestegg_duration(mContext, &duration) == 0) {
|
|
|
|
endTime = duration / NS_PER_S;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aBuffered->Add(startTime, endTime);
|
|
|
|
}
|
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-11-14 11:46:40 -08:00
|
|
|
void WebMReader::NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t 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
|
|
|
}
|
2012-09-17 13:45:38 -07:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|