Mirror of roytam1's UXP fork just in case Moonchild and Tobin decide to go after him
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1828 lines
55 KiB

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#include <string.h>
#include "mozilla/EndianUtils.h"
#include <stdint.h>
#include "nsDebug.h"
#include "OggCodecState.h"
#include "OpusParser.h"
#include "VideoUtils.h"
#include <algorithm>
#include <opus/opus.h>
#include "opus/opus_multistream.h"
namespace mozilla {
extern LazyLogModule gMediaDecoderLog;
#define LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
/** Decoder base class for Ogg-encapsulated streams. */
OggCodecState*
OggCodecState::Create(ogg_page* aPage)
{
NS_ASSERTION(ogg_page_bos(aPage), "Only call on BOS page!");
nsAutoPtr<OggCodecState> codecState;
if (aPage->body_len > 6 && memcmp(aPage->body+1, "theora", 6) == 0) {
codecState = new TheoraState(aPage);
} else if (aPage->body_len > 6 && memcmp(aPage->body+1, "vorbis", 6) == 0) {
codecState = new VorbisState(aPage);
} else if (aPage->body_len > 8 && memcmp(aPage->body, "OpusHead", 8) == 0) {
codecState = new OpusState(aPage);
} else if (aPage->body_len > 8 && memcmp(aPage->body, "fishead\0", 8) == 0) {
codecState = new SkeletonState(aPage);
} else if (aPage->body_len > 5 && memcmp(aPage->body, "\177FLAC", 5) == 0) {
codecState = new FlacState(aPage);
} else {
codecState = new OggCodecState(aPage, false);
}
return codecState->OggCodecState::InternalInit() ? codecState.forget() : nullptr;
}
OggCodecState::OggCodecState(ogg_page* aBosPage, bool aActive)
: mPacketCount(0)
, mSerial(ogg_page_serialno(aBosPage))
, mActive(aActive)
, mDoneReadingHeaders(!aActive)
{
MOZ_COUNT_CTOR(OggCodecState);
memset(&mState, 0, sizeof(ogg_stream_state));
}
OggCodecState::~OggCodecState()
{
MOZ_COUNT_DTOR(OggCodecState);
Reset();
#ifdef DEBUG
int ret =
#endif
ogg_stream_clear(&mState);
NS_ASSERTION(ret == 0, "ogg_stream_clear failed");
}
nsresult
OggCodecState::Reset()
{
if (ogg_stream_reset(&mState) != 0) {
return NS_ERROR_FAILURE;
}
mPackets.Erase();
ClearUnstamped();
return NS_OK;
}
void
OggCodecState::ClearUnstamped()
{
for (uint32_t i = 0; i < mUnstamped.Length(); ++i) {
OggCodecState::ReleasePacket(mUnstamped[i]);
}
mUnstamped.Clear();
}
bool
OggCodecState::InternalInit()
{
int ret = ogg_stream_init(&mState, mSerial);
return ret == 0;
}
bool
OggCodecState::IsValidVorbisTagName(nsCString& aName)
{
// Tag names must consist of ASCII 0x20 through 0x7D,
// excluding 0x3D '=' which is the separator.
uint32_t length = aName.Length();
const char* data = aName.Data();
for (uint32_t i = 0; i < length; i++) {
if (data[i] < 0x20 || data[i] > 0x7D || data[i] == '=') {
return false;
}
}
return true;
}
bool
OggCodecState::AddVorbisComment(MetadataTags* aTags,
const char* aComment,
uint32_t aLength)
{
const char* div = (const char*)memchr(aComment, '=', aLength);
if (!div) {
LOG(LogLevel::Debug, ("Skipping comment: no separator"));
return false;
}
nsCString key = nsCString(aComment, div-aComment);
if (!IsValidVorbisTagName(key)) {
LOG(LogLevel::Debug, ("Skipping comment: invalid tag name"));
return false;
}
uint32_t valueLength = aLength - (div-aComment);
nsCString value = nsCString(div + 1, valueLength);
if (!IsUTF8(value)) {
LOG(LogLevel::Debug, ("Skipping comment: invalid UTF-8 in value"));
return false;
}
aTags->Put(key, value);
return true;
}
void
VorbisState::RecordVorbisPacketSamples(ogg_packet* aPacket, long aSamples)
{
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
mVorbisPacketSamples[aPacket] = aSamples;
#endif
}
void
VorbisState::ValidateVorbisPacketSamples(ogg_packet* aPacket, long aSamples)
{
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
NS_ASSERTION(mVorbisPacketSamples[aPacket] == aSamples,
"Decoded samples for Vorbis packet don't match expected!");
mVorbisPacketSamples.erase(aPacket);
#endif
}
void
VorbisState::AssertHasRecordedPacketSamples(ogg_packet* aPacket)
{
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
NS_ASSERTION(mVorbisPacketSamples.count(aPacket) == 1,
"Must have recorded packet samples");
#endif
}
static ogg_packet*
Clone(ogg_packet* aPacket)
{
ogg_packet* p = new ogg_packet();
memcpy(p, aPacket, sizeof(ogg_packet));
p->packet = new unsigned char[p->bytes];
memcpy(p->packet, aPacket->packet, p->bytes);
return p;
}
void
OggCodecState::ReleasePacket(ogg_packet* aPacket)
{
if (aPacket)
delete [] aPacket->packet;
delete aPacket;
}
void
OggPacketQueue::Append(ogg_packet* aPacket)
{
nsDeque::Push(aPacket);
}
bool
OggCodecState::IsPacketReady()
{
return !mPackets.IsEmpty();
}
ogg_packet*
OggCodecState::PacketOut()
{
if (mPackets.IsEmpty()) {
return nullptr;
}
return mPackets.PopFront();
}
ogg_packet*
OggCodecState::PacketPeek()
{
if (mPackets.IsEmpty()) {
return nullptr;
}
return mPackets.PeekFront();
}
void
OggCodecState::PushFront(OggPacketQueue &&aOther)
{
while (!aOther.IsEmpty()) {
mPackets.PushFront(aOther.Pop());
}
}
already_AddRefed<MediaRawData>
OggCodecState::PacketOutAsMediaRawData()
{
ogg_packet* packet = PacketOut();
if (!packet) {
return nullptr;
}
NS_ASSERTION(!IsHeader(packet), "PacketOutAsMediaRawData can only be called on non-header packets");
RefPtr<MediaRawData> sample = new MediaRawData(packet->packet, packet->bytes);
if (packet->bytes && !sample->Data()) {
// OOM.
ReleasePacket(packet);
return nullptr;
}
int64_t end_tstamp = Time(packet->granulepos);
NS_ASSERTION(end_tstamp >= 0, "timestamp invalid");
int64_t duration = PacketDuration(packet);
NS_ASSERTION(duration >= 0, "duration invalid");
sample->mTimecode = packet->granulepos;
sample->mTime = end_tstamp - duration;
sample->mDuration = duration;
sample->mKeyframe = IsKeyframe(packet);
sample->mEOS = packet->e_o_s;
ReleasePacket(packet);
return sample.forget();
}
nsresult
OggCodecState::PageIn(ogg_page* aPage)
{
if (!mActive) {
return NS_OK;
}
NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
"Page must be for this stream!");
if (ogg_stream_pagein(&mState, aPage) == -1) {
return NS_ERROR_FAILURE;
}
int r;
do {
ogg_packet packet;
r = ogg_stream_packetout(&mState, &packet);
if (r == 1) {
mPackets.Append(Clone(&packet));
}
} while (r != 0);
if (ogg_stream_check(&mState)) {
NS_WARNING("Unrecoverable error in ogg_stream_packetout");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult
OggCodecState::PacketOutUntilGranulepos(bool& aFoundGranulepos)
{
int r;
aFoundGranulepos = false;
// Extract packets from the sync state until either no more packets
// come out, or we get a data packet with non -1 granulepos.
do {
ogg_packet packet;
r = ogg_stream_packetout(&mState, &packet);
if (r == 1) {
ogg_packet* clone = Clone(&packet);
if (IsHeader(&packet)) {
// Header packets go straight into the packet queue.
mPackets.Append(clone);
} else {
// We buffer data packets until we encounter a granulepos. We'll
// then use the granulepos to figure out the granulepos of the
// preceeding packets.
mUnstamped.AppendElement(clone);
aFoundGranulepos = packet.granulepos > 0;
}
}
} while (r != 0 && !aFoundGranulepos);
if (ogg_stream_check(&mState)) {
NS_WARNING("Unrecoverable error in ogg_stream_packetout");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
TheoraState::TheoraState(ogg_page* aBosPage)
: OggCodecState(aBosPage, true)
, mSetup(0)
, mCtx(0)
, mPixelAspectRatio(0)
{
MOZ_COUNT_CTOR(TheoraState);
th_info_init(&mInfo);
th_comment_init(&mComment);
}
TheoraState::~TheoraState()
{
MOZ_COUNT_DTOR(TheoraState);
th_setup_free(mSetup);
th_decode_free(mCtx);
th_comment_clear(&mComment);
th_info_clear(&mInfo);
}
bool
TheoraState::Init()
{
if (!mActive) {
return false;
}
int64_t n = mInfo.aspect_numerator;
int64_t d = mInfo.aspect_denominator;
mPixelAspectRatio = (n == 0 || d == 0)
? 1.0f : static_cast<float>(n) / static_cast<float>(d);
// Ensure the frame and picture regions aren't larger than our prescribed
// maximum, or zero sized.
nsIntSize frame(mInfo.frame_width, mInfo.frame_height);
nsIntRect picture(mInfo.pic_x, mInfo.pic_y, mInfo.pic_width, mInfo.pic_height);
if (!IsValidVideoRegion(frame, picture, frame)) {
return mActive = false;
}
mCtx = th_decode_alloc(&mInfo, mSetup);
if (!mCtx) {
return mActive = false;
}
return true;
}
bool
TheoraState::DecodeHeader(ogg_packet* aPacket)
{
nsAutoRef<ogg_packet> autoRelease(aPacket);
mPacketCount++;
int ret = th_decode_headerin(&mInfo,
&mComment,
&mSetup,
aPacket);
// We must determine when we've read the last header packet.
// th_decode_headerin() does not tell us when it's read the last header, so
// we must keep track of the headers externally.
//
// There are 3 header packets, the Identification, Comment, and Setup
// headers, which must be in that order. If they're out of order, the file
// is invalid. If we've successfully read a header, and it's the setup
// header, then we're done reading headers. The first byte of each packet
// determines it's type as follows:
// 0x80 -> Identification header
// 0x81 -> Comment header
// 0x82 -> Setup header
// See http://www.theora.org/doc/Theora.pdf Chapter 6, "Bitstream Headers",
// for more details of the Ogg/Theora containment scheme.
bool isSetupHeader = aPacket->bytes > 0 && aPacket->packet[0] == 0x82;
if (ret < 0 || mPacketCount > 3) {
// We've received an error, or the first three packets weren't valid
// header packets. Assume bad input.
// Our caller will deactivate the bitstream.
return false;
} else if (ret > 0 && isSetupHeader && mPacketCount == 3) {
// Successfully read the three header packets.
mDoneReadingHeaders = true;
}
return true;
}
int64_t
TheoraState::Time(int64_t granulepos)
{
if (!mActive) {
return -1;
}
return TheoraState::Time(&mInfo, granulepos);
}
bool
TheoraState::IsHeader(ogg_packet* aPacket)
{
return th_packet_isheader(aPacket);
}
# define TH_VERSION_CHECK(_info,_maj,_min,_sub) \
(((_info)->version_major>(_maj)||(_info)->version_major==(_maj))&& \
(((_info)->version_minor>(_min)||(_info)->version_minor==(_min))&& \
(_info)->version_subminor>=(_sub)))
int64_t
TheoraState::Time(th_info* aInfo, int64_t aGranulepos)
{
if (aGranulepos < 0 || aInfo->fps_numerator == 0) {
return -1;
}
// Implementation of th_granule_frame inlined here to operate
// on the th_info structure instead of the theora_state.
int shift = aInfo->keyframe_granule_shift;
ogg_int64_t iframe = aGranulepos >> shift;
ogg_int64_t pframe = aGranulepos - (iframe << shift);
int64_t frameno = iframe + pframe - TH_VERSION_CHECK(aInfo, 3, 2, 1);
CheckedInt64 t =
((CheckedInt64(frameno) + 1) * USECS_PER_S) * aInfo->fps_denominator;
if (!t.isValid()) {
return -1;
}
t /= aInfo->fps_numerator;
return t.isValid() ? t.value() : -1;
}
int64_t TheoraState::StartTime(int64_t granulepos)
{
if (granulepos < 0 || !mActive || mInfo.fps_numerator == 0) {
return -1;
}
CheckedInt64 t =
(CheckedInt64(th_granule_frame(mCtx, granulepos)) * USECS_PER_S)
* mInfo.fps_denominator;
if (!t.isValid()) {
return -1;
}
return t.value() / mInfo.fps_numerator;
}
int64_t
TheoraState::PacketDuration(ogg_packet* aPacket)
{
if (!mActive || mInfo.fps_numerator == 0) {
return -1;
}
CheckedInt64 t =
SaferMultDiv(mInfo.fps_denominator, USECS_PER_S, mInfo.fps_numerator);
return t.isValid() ? t.value() : -1;
}
int64_t
TheoraState::MaxKeyframeOffset()
{
// Determine the maximum time in microseconds by which a key frame could
// offset for the theora bitstream. Theora granulepos encode time as:
// ((key_frame_number << granule_shift) + frame_offset).
// Therefore the maximum possible time by which any frame could be offset
// from a keyframe is the duration of (1 << granule_shift) - 1) frames.
int64_t frameDuration;
// Max number of frames keyframe could possibly be offset.
int64_t keyframeDiff = (1 << mInfo.keyframe_granule_shift) - 1;
// Length of frame in usecs.
frameDuration = (mInfo.fps_denominator * USECS_PER_S) / mInfo.fps_numerator;
// Total time in usecs keyframe can be offset from any given frame.
return frameDuration * keyframeDiff;
}
bool
TheoraState::IsKeyframe(ogg_packet* pkt)
{
// first bit of packet is 1 for header, 0 for data
// second bit of packet is 1 for inter frame, 0 for intra frame
return (pkt->bytes >= 1 && (pkt->packet[0] & 0x40) == 0x00);
}
nsresult
TheoraState::PageIn(ogg_page* aPage)
{
if (!mActive)
return NS_OK;
NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
"Page must be for this stream!");
if (ogg_stream_pagein(&mState, aPage) == -1)
return NS_ERROR_FAILURE;
bool foundGp;
nsresult res = PacketOutUntilGranulepos(foundGp);
if (NS_FAILED(res))
return res;
if (foundGp && mDoneReadingHeaders) {
// We've found a packet with a granulepos, and we've loaded our metadata
// and initialized our decoder. Determine granulepos of buffered packets.
ReconstructTheoraGranulepos();
for (uint32_t i = 0; i < mUnstamped.Length(); ++i) {
ogg_packet* packet = mUnstamped[i];
#ifdef DEBUG
NS_ASSERTION(!IsHeader(packet), "Don't try to recover header packet gp");
NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now");
#endif
mPackets.Append(packet);
}
mUnstamped.Clear();
}
return NS_OK;
}
// Returns 1 if the Theora info struct is decoding a media of Theora
// version (maj,min,sub) or later, otherwise returns 0.
int
TheoraVersion(th_info* info,
unsigned char maj,
unsigned char min,
unsigned char sub)
{
ogg_uint32_t ver = (maj << 16) + (min << 8) + sub;
ogg_uint32_t th_ver = (info->version_major << 16) +
(info->version_minor << 8) +
info->version_subminor;
return (th_ver >= ver) ? 1 : 0;
}
void
TheoraState::ReconstructTheoraGranulepos()
{
if (mUnstamped.Length() == 0) {
return;
}
ogg_int64_t lastGranulepos = mUnstamped[mUnstamped.Length() - 1]->granulepos;
NS_ASSERTION(lastGranulepos != -1, "Must know last granulepos");
// Reconstruct the granulepos (and thus timestamps) of the decoded
// frames. Granulepos are stored as ((keyframe<<shift)+offset). We
// know the granulepos of the last frame in the list, so we can infer
// the granulepos of the intermediate frames using their frame numbers.
ogg_int64_t shift = mInfo.keyframe_granule_shift;
ogg_int64_t version_3_2_1 = TheoraVersion(&mInfo,3,2,1);
ogg_int64_t lastFrame = th_granule_frame(mCtx,
lastGranulepos) + version_3_2_1;
ogg_int64_t firstFrame = lastFrame - mUnstamped.Length() + 1;
// Until we encounter a keyframe, we'll assume that the "keyframe"
// segment of the granulepos is the first frame, or if that causes
// the "offset" segment to overflow, we assume the required
// keyframe is maximumally offset. Until we encounter a keyframe
// the granulepos will probably be wrong, but we can't decode the
// frame anyway (since we don't have its keyframe) so it doesn't really
// matter.
ogg_int64_t keyframe = lastGranulepos >> shift;
// The lastFrame, firstFrame, keyframe variables, as well as the frame
// variable in the loop below, store the frame number for Theora
// version >= 3.2.1 streams, and store the frame index for Theora
// version < 3.2.1 streams.
for (uint32_t i = 0; i < mUnstamped.Length() - 1; ++i) {
ogg_int64_t frame = firstFrame + i;
ogg_int64_t granulepos;
ogg_packet* packet = mUnstamped[i];
bool isKeyframe = th_packet_iskeyframe(packet) == 1;
if (isKeyframe) {
granulepos = frame << shift;
keyframe = frame;
} else if (frame >= keyframe &&
frame - keyframe < ((ogg_int64_t)1 << shift))
{
// (frame - keyframe) won't overflow the "offset" segment of the
// granulepos, so it's safe to calculate the granulepos.
granulepos = (keyframe << shift) + (frame - keyframe);
} else {
// (frame - keyframeno) will overflow the "offset" segment of the
// granulepos, so we take "keyframe" to be the max possible offset
// frame instead.
ogg_int64_t k = std::max(frame - (((ogg_int64_t)1 << shift) - 1), version_3_2_1);
granulepos = (k << shift) + (frame - k);
}
// Theora 3.2.1+ granulepos store frame number [1..N], so granulepos
// should be > 0.
// Theora 3.2.0 granulepos store the frame index [0..(N-1)], so
// granulepos should be >= 0.
NS_ASSERTION(granulepos >= version_3_2_1,
"Invalid granulepos for Theora version");
// Check that the frame's granule number is one more than the
// previous frame's.
NS_ASSERTION(i == 0 ||
th_granule_frame(mCtx, granulepos) ==
th_granule_frame(mCtx, mUnstamped[i-1]->granulepos) + 1,
"Granulepos calculation is incorrect!");
packet->granulepos = granulepos;
}
// Check that the second to last frame's granule number is one less than
// the last frame's (the known granule number). If not our granulepos
// recovery missed a beat.
NS_ASSERTION(mUnstamped.Length() < 2 ||
th_granule_frame(mCtx, mUnstamped[mUnstamped.Length()-2]->granulepos) + 1 ==
th_granule_frame(mCtx, lastGranulepos),
"Granulepos recovery should catch up with packet->granulepos!");
}
nsresult
VorbisState::Reset()
{
nsresult res = NS_OK;
if (mActive && vorbis_synthesis_restart(&mDsp) != 0) {
res = NS_ERROR_FAILURE;
}
if (NS_FAILED(OggCodecState::Reset())) {
return NS_ERROR_FAILURE;
}
mGranulepos = 0;
mPrevVorbisBlockSize = 0;
return res;
}
VorbisState::VorbisState(ogg_page* aBosPage)
: OggCodecState(aBosPage, true)
, mPrevVorbisBlockSize(0)
, mGranulepos(0)
{
MOZ_COUNT_CTOR(VorbisState);
vorbis_info_init(&mInfo);
vorbis_comment_init(&mComment);
memset(&mDsp, 0, sizeof(vorbis_dsp_state));
memset(&mBlock, 0, sizeof(vorbis_block));
}
VorbisState::~VorbisState()
{
MOZ_COUNT_DTOR(VorbisState);
Reset();
vorbis_block_clear(&mBlock);
vorbis_dsp_clear(&mDsp);
vorbis_info_clear(&mInfo);
vorbis_comment_clear(&mComment);
}
bool
VorbisState::DecodeHeader(ogg_packet* aPacket)
{
nsAutoRef<ogg_packet> autoRelease(aPacket);
mPacketCount++;
int ret = vorbis_synthesis_headerin(&mInfo,
&mComment,
aPacket);
// We must determine when we've read the last header packet.
// vorbis_synthesis_headerin() does not tell us when it's read the last
// header, so we must keep track of the headers externally.
//
// There are 3 header packets, the Identification, Comment, and Setup
// headers, which must be in that order. If they're out of order, the file
// is invalid. If we've successfully read a header, and it's the setup
// header, then we're done reading headers. The first byte of each packet
// determines it's type as follows:
// 0x1 -> Identification header
// 0x3 -> Comment header
// 0x5 -> Setup header
// For more details of the Vorbis/Ogg containment scheme, see the Vorbis I
// Specification, Chapter 4, Codec Setup and Packet Decode:
// http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-580004
bool isSetupHeader = aPacket->bytes > 0 && aPacket->packet[0] == 0x5;
if (ret < 0 || mPacketCount > 3) {
// We've received an error, or the first three packets weren't valid
// header packets. Assume bad input. Our caller will deactivate the
// bitstream.
return false;
} else if (ret == 0 && isSetupHeader && mPacketCount == 3) {
// Successfully read the three header packets.
// The bitstream remains active.
mDoneReadingHeaders = true;
}
return true;
}
bool
VorbisState::Init()
{
if (!mActive) {
return false;
}
int ret = vorbis_synthesis_init(&mDsp, &mInfo);
if (ret != 0) {
NS_WARNING("vorbis_synthesis_init() failed initializing vorbis bitstream");
return mActive = false;
}
ret = vorbis_block_init(&mDsp, &mBlock);
if (ret != 0) {
NS_WARNING("vorbis_block_init() failed initializing vorbis bitstream");
if (mActive) {
vorbis_dsp_clear(&mDsp);
}
return mActive = false;
}
return true;
}
int64_t
VorbisState::Time(int64_t granulepos)
{
if (!mActive) {
return -1;
}
return VorbisState::Time(&mInfo, granulepos);
}
int64_t
VorbisState::Time(vorbis_info* aInfo, int64_t aGranulepos)
{
if (aGranulepos == -1 || aInfo->rate == 0) {
return -1;
}
CheckedInt64 t = SaferMultDiv(aGranulepos, USECS_PER_S, aInfo->rate);
return t.isValid() ? t.value() : 0;
}
int64_t
VorbisState::PacketDuration(ogg_packet* aPacket)
{
if (!mActive) {
return -1;
}
if (aPacket->granulepos == -1) {
return -1;
}
// @FIXME store these in a more stable place
if (mVorbisPacketSamples.count(aPacket) == 0) {
// We haven't seen this packet, don't know its size?
return -1;
}
long samples = mVorbisPacketSamples[aPacket];
return Time(samples);
}
bool
VorbisState::IsHeader(ogg_packet* aPacket)
{
// The first byte in each Vorbis header packet is either 0x01, 0x03, or 0x05,
// i.e. the first bit is odd. Audio data packets have their first bit as 0x0.
// Any packet with its first bit set cannot be a data packet, it's a
// (possibly invalid) header packet.
// See: http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-610004.2.1
return aPacket->bytes > 0 ? (aPacket->packet[0] & 0x1) : false;
}
MetadataTags*
VorbisState::GetTags()
{
MetadataTags* tags;
NS_ASSERTION(mComment.user_comments, "no vorbis comment strings!");
NS_ASSERTION(mComment.comment_lengths, "no vorbis comment lengths!");
tags = new MetadataTags;
for (int i = 0; i < mComment.comments; i++) {
AddVorbisComment(tags, mComment.user_comments[i],
mComment.comment_lengths[i]);
}
return tags;
}
nsresult
VorbisState::PageIn(ogg_page* aPage)
{
if (!mActive) {
return NS_OK;
}
NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
"Page must be for this stream!");
if (ogg_stream_pagein(&mState, aPage) == -1)
return NS_ERROR_FAILURE;
bool foundGp;
nsresult res = PacketOutUntilGranulepos(foundGp);
if (NS_FAILED(res)) {
return res;
}
if (foundGp && mDoneReadingHeaders) {
// We've found a packet with a granulepos, and we've loaded our metadata
// and initialized our decoder. Determine granulepos of buffered packets.
ReconstructVorbisGranulepos();
for (uint32_t i = 0; i < mUnstamped.Length(); ++i) {
ogg_packet* packet = mUnstamped[i];
AssertHasRecordedPacketSamples(packet);
NS_ASSERTION(!IsHeader(packet), "Don't try to recover header packet gp");
NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now");
mPackets.Append(packet);
}
mUnstamped.Clear();
}
return NS_OK;
}
nsresult
VorbisState::ReconstructVorbisGranulepos()
{
// The number of samples in a Vorbis packet is:
// window_blocksize(previous_packet)/4+window_blocksize(current_packet)/4
// See: http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-230001.3.2
// So we maintain mPrevVorbisBlockSize, the block size of the last packet
// encountered. We also maintain mGranulepos, which is the granulepos of
// the last encountered packet. This enables us to give granulepos to
// packets when the last packet in mUnstamped doesn't have a granulepos
// (for example if the stream was truncated).
//
// We validate our prediction of the number of samples decoded when
// VALIDATE_VORBIS_SAMPLE_CALCULATION is defined by recording the predicted
// number of samples, and verifing we extract that many when decoding
// each packet.
NS_ASSERTION(mUnstamped.Length() > 0, "Length must be > 0");
ogg_packet* last = mUnstamped.LastElement();
NS_ASSERTION(last->e_o_s || last->granulepos >= 0,
"Must know last granulepos!");
if (mUnstamped.Length() == 1) {
ogg_packet* packet = mUnstamped[0];
long blockSize = vorbis_packet_blocksize(&mInfo, packet);
if (blockSize < 0) {
// On failure vorbis_packet_blocksize returns < 0. If we've got
// a bad packet, we just assume that decode will have to skip this
// packet, i.e. assume 0 samples are decodable from this packet.
blockSize = 0;
mPrevVorbisBlockSize = 0;
}
long samples = mPrevVorbisBlockSize / 4 + blockSize / 4;
mPrevVorbisBlockSize = blockSize;
if (packet->granulepos == -1) {
packet->granulepos = mGranulepos + samples;
}
// Account for a partial last frame
if (packet->e_o_s && packet->granulepos >= mGranulepos) {
samples = packet->granulepos - mGranulepos;
}
mGranulepos = packet->granulepos;
RecordVorbisPacketSamples(packet, samples);
return NS_OK;
}
bool unknownGranulepos = last->granulepos == -1;
int totalSamples = 0;
for (int32_t i = mUnstamped.Length() - 1; i > 0; i--) {
ogg_packet* packet = mUnstamped[i];
ogg_packet* prev = mUnstamped[i-1];
ogg_int64_t granulepos = packet->granulepos;
NS_ASSERTION(granulepos != -1, "Must know granulepos!");
long prevBlockSize = vorbis_packet_blocksize(&mInfo, prev);
long blockSize = vorbis_packet_blocksize(&mInfo, packet);
if (blockSize < 0 || prevBlockSize < 0) {
// On failure vorbis_packet_blocksize returns < 0. If we've got
// a bad packet, we just assume that decode will have to skip this
// packet, i.e. assume 0 samples are decodable from this packet.
blockSize = 0;
prevBlockSize = 0;
}
long samples = prevBlockSize / 4 + blockSize / 4;
totalSamples += samples;
prev->granulepos = granulepos - samples;
RecordVorbisPacketSamples(packet, samples);
}
if (unknownGranulepos) {
for (uint32_t i = 0; i < mUnstamped.Length(); i++) {
ogg_packet* packet = mUnstamped[i];
packet->granulepos += mGranulepos + totalSamples + 1;
}
}
ogg_packet* first = mUnstamped[0];
long blockSize = vorbis_packet_blocksize(&mInfo, first);
if (blockSize < 0) {
mPrevVorbisBlockSize = 0;
blockSize = 0;
}
long samples = (mPrevVorbisBlockSize == 0) ? 0 :
mPrevVorbisBlockSize / 4 + blockSize / 4;
int64_t start = first->granulepos - samples;
RecordVorbisPacketSamples(first, samples);
if (last->e_o_s && start < mGranulepos) {
// We've calculated that there are more samples in this page than its
// granulepos claims, and it's the last page in the stream. This is legal,
// and we will need to prune the trailing samples when we come to decode it.
// We must correct the timestamps so that they follow the last Vorbis page's
// samples.
int64_t pruned = mGranulepos - start;
for (uint32_t i = 0; i < mUnstamped.Length() - 1; i++) {
mUnstamped[i]->granulepos += pruned;
}
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
mVorbisPacketSamples[last] -= pruned;
#endif
}
mPrevVorbisBlockSize = vorbis_packet_blocksize(&mInfo, last);
mPrevVorbisBlockSize = std::max(static_cast<long>(0), mPrevVorbisBlockSize);
mGranulepos = last->granulepos;
return NS_OK;
}
OpusState::OpusState(ogg_page* aBosPage)
: OggCodecState(aBosPage, true)
, mParser(nullptr)
, mDecoder(nullptr)
, mSkip(0)
, mPrevPacketGranulepos(0)
, mPrevPageGranulepos(0)
{
MOZ_COUNT_CTOR(OpusState);
}
OpusState::~OpusState()
{
MOZ_COUNT_DTOR(OpusState);
Reset();
if (mDecoder) {
opus_multistream_decoder_destroy(mDecoder);
mDecoder = nullptr;
}
}
nsresult
OpusState::Reset()
{
return Reset(false);
}
nsresult
OpusState::Reset(bool aStart)
{
nsresult res = NS_OK;
if (mActive && mDecoder) {
// Reset the decoder.
opus_multistream_decoder_ctl(mDecoder, OPUS_RESET_STATE);
// Let the seek logic handle pre-roll if we're not seeking to the start.
mSkip = aStart ? mParser->mPreSkip : 0;
// This lets us distinguish the first page being the last page vs. just
// not having processed the previous page when we encounter the last page.
mPrevPageGranulepos = aStart ? 0 : -1;
mPrevPacketGranulepos = aStart ? 0 : -1;
}
// Clear queued data.
if (NS_FAILED(OggCodecState::Reset())) {
return NS_ERROR_FAILURE;
}
LOG(LogLevel::Debug, ("Opus decoder reset, to skip %d", mSkip));
return res;
}
bool
OpusState::Init(void)
{
if (!mActive) {
return false;
}
int error;
NS_ASSERTION(mDecoder == nullptr, "leaking OpusDecoder");
mDecoder = opus_multistream_decoder_create(mParser->mRate,
mParser->mChannels,
mParser->mStreams,
mParser->mCoupledStreams,
mParser->mMappingTable,
&error);
mSkip = mParser->mPreSkip;
LOG(LogLevel::Debug, ("Opus decoder init, to skip %d", mSkip));
return error == OPUS_OK;
}
bool
OpusState::DecodeHeader(ogg_packet* aPacket)
{
nsAutoRef<ogg_packet> autoRelease(aPacket);
switch(mPacketCount++) {
// Parse the id header.
case 0:
mParser = new OpusParser;
if (!mParser->DecodeHeader(aPacket->packet, aPacket->bytes)) {
return false;
}
mRate = mParser->mRate;
mChannels = mParser->mChannels;
mPreSkip = mParser->mPreSkip;
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
mGain = mParser->mGain;
#else
mGain_Q16 = mParser->mGain_Q16;
#endif
break;
// Parse the metadata header.
case 1:
if (!mParser->DecodeTags(aPacket->packet, aPacket->bytes)) {
return false;
}
break;
// We made it to the first data packet (which includes reconstructing
// timestamps for it in PageIn). Success!
default:
mDoneReadingHeaders = true;
// Put it back on the queue so we can decode it.
mPackets.PushFront(autoRelease.disown());
break;
}
return true;
}
/* Construct and return a tags hashmap from our internal array */
MetadataTags*
OpusState::GetTags()
{
MetadataTags* tags;
tags = new MetadataTags;
for (uint32_t i = 0; i < mParser->mTags.Length(); i++) {
AddVorbisComment(tags, mParser->mTags[i].Data(), mParser->mTags[i].Length());
}
return tags;
}
/* Return the timestamp (in microseconds) equivalent to a granulepos. */
int64_t
OpusState::Time(int64_t aGranulepos)
{
if (!mActive) {
return -1;
}
return Time(mParser->mPreSkip, aGranulepos);
}
int64_t
OpusState::Time(int aPreSkip, int64_t aGranulepos)
{
if (aGranulepos < 0) {
return -1;
}
// Ogg Opus always runs at a granule rate of 48 kHz.
CheckedInt64 t = SaferMultDiv(aGranulepos - aPreSkip, USECS_PER_S, 48000);
return t.isValid() ? t.value() : -1;
}
bool
OpusState::IsHeader(ogg_packet* aPacket)
{
return aPacket->bytes >= 16 &&
(!memcmp(aPacket->packet, "OpusHead", 8) ||
!memcmp(aPacket->packet, "OpusTags", 8));
}
nsresult
OpusState::PageIn(ogg_page* aPage)
{
if (!mActive) {
return NS_OK;
}
NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
"Page must be for this stream!");
if (ogg_stream_pagein(&mState, aPage) == -1)
return NS_ERROR_FAILURE;
bool haveGranulepos;
nsresult rv = PacketOutUntilGranulepos(haveGranulepos);
if (NS_FAILED(rv) || !haveGranulepos || mPacketCount < 2) {
return rv;
}
if (!ReconstructOpusGranulepos()) {
return NS_ERROR_FAILURE;
}
for (uint32_t i = 0; i < mUnstamped.Length(); i++) {
ogg_packet* packet = mUnstamped[i];
NS_ASSERTION(!IsHeader(packet), "Don't try to play a header packet");
NS_ASSERTION(packet->granulepos != -1, "Packet should have a granulepos");
mPackets.Append(packet);
}
mUnstamped.Clear();
return NS_OK;
}
// Helper method to return the change in granule position due to an Opus packet
// (as distinct from the number of samples in the packet, which depends on the
// decoder rate). It should work with a multistream Opus file, and continue to
// work should we ever allow the decoder to decode at a rate other than 48 kHz.
// It even works before we've created the actual Opus decoder.
static int
GetOpusDeltaGP(ogg_packet* packet)