2010-04-01 20:03:07 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-11-14 11:46:40 -08:00
|
|
|
#if !defined(OggCodecState_h_)
|
|
|
|
#define OggCodecState_h_
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
#include <ogg/ogg.h>
|
|
|
|
#include <theora/theoradec.h>
|
2010-10-18 19:55:45 -07:00
|
|
|
#ifdef MOZ_TREMOR
|
|
|
|
#include <tremor/ivorbiscodec.h>
|
|
|
|
#else
|
2010-04-01 20:03:07 -07:00
|
|
|
#include <vorbis/codec.h>
|
2010-10-18 19:55:45 -07:00
|
|
|
#endif
|
2012-05-01 17:29:34 -07:00
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
#include <opus/opus.h>
|
2012-08-09 15:53:23 -07:00
|
|
|
#include "opus/opus_multistream.h"
|
2012-05-31 11:13:17 -07:00
|
|
|
// For MOZ_SAMPLE_TYPE_*
|
2012-09-17 15:42:36 -07:00
|
|
|
#include "nsHTMLMediaElement.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
|
|
|
#include "MediaDecoderReader.h"
|
2012-05-01 17:29:34 -07:00
|
|
|
#endif
|
2012-05-31 11:13:17 -07:00
|
|
|
#include <nsAutoRef.h>
|
2010-04-01 20:03:07 -07:00
|
|
|
#include <nsDeque.h>
|
2010-08-19 15:50:37 -07:00
|
|
|
#include <nsTArray.h>
|
|
|
|
#include <nsClassHashtable.h>
|
|
|
|
#include "VideoUtils.h"
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2012-02-29 19:56:43 -08:00
|
|
|
#include "mozilla/StandardInteger.h"
|
2012-01-11 00:23:07 -08:00
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// Uncomment the following to validate that we're predicting the number
|
|
|
|
// of Vorbis samples in each packet correctly.
|
|
|
|
#define VALIDATE_VORBIS_SAMPLE_CALCULATION
|
|
|
|
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
|
|
|
|
#include <map>
|
|
|
|
#endif
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Deallocates a packet, used in PacketQueue below.
|
2011-05-07 23:24:09 -07:00
|
|
|
class OggPacketDeallocator : public nsDequeFunctor {
|
|
|
|
virtual void* operator() (void* aPacket) {
|
|
|
|
ogg_packet* p = static_cast<ogg_packet*>(aPacket);
|
|
|
|
delete [] p->packet;
|
2010-04-01 20:03:07 -07:00
|
|
|
delete p;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// A queue of ogg_packets. When we read a page, we extract the page's packets
|
2012-11-14 11:46:40 -08:00
|
|
|
// and buffer them in the owning stream's OggCodecState. This is because
|
2011-05-07 23:24:09 -07:00
|
|
|
// if we're skipping up to the next keyframe in very large frame sized videos,
|
|
|
|
// there may be several megabytes of data between keyframes, and the
|
|
|
|
// ogg_stream_state would end up resizing its buffer every time we added a
|
|
|
|
// new 4KB page to the bitstream, which kills performance on Windows. This
|
|
|
|
// also gives us the option to timestamp packets rather than decoded
|
|
|
|
// frames/samples, reducing the amount of frames/samples we must decode to
|
|
|
|
// determine start-time at a particular offset, and gives us finer control
|
|
|
|
// over memory usage.
|
2012-11-14 11:46:40 -08:00
|
|
|
class PacketQueue : private nsDeque {
|
2010-04-01 20:03:07 -07:00
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
PacketQueue() : nsDeque(new OggPacketDeallocator()) {}
|
|
|
|
~PacketQueue() { Erase(); }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsEmpty() { return nsDeque::GetSize() == 0; }
|
2011-05-07 23:24:09 -07:00
|
|
|
void Append(ogg_packet* aPacket);
|
|
|
|
ogg_packet* PopFront() { return static_cast<ogg_packet*>(nsDeque::PopFront()); }
|
|
|
|
ogg_packet* PeekFront() { return static_cast<ogg_packet*>(nsDeque::PeekFront()); }
|
|
|
|
void PushFront(ogg_packet* aPacket) { nsDeque::PushFront(aPacket); }
|
|
|
|
void PushBack(ogg_packet* aPacket) { nsDeque::PushFront(aPacket); }
|
2010-04-01 20:03:07 -07:00
|
|
|
void Erase() { nsDeque::Erase(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Encapsulates the data required for decoding an ogg bitstream and for
|
|
|
|
// converting granulepos to timestamps.
|
2012-11-14 11:46:40 -08:00
|
|
|
class OggCodecState {
|
2011-05-07 23:24:09 -07:00
|
|
|
public:
|
2012-11-14 11:45:31 -08:00
|
|
|
typedef mozilla::MetadataTags MetadataTags;
|
2010-04-01 20:03:07 -07:00
|
|
|
// Ogg types we know about
|
|
|
|
enum CodecType {
|
|
|
|
TYPE_VORBIS=0,
|
|
|
|
TYPE_THEORA=1,
|
2012-05-01 17:29:34 -07:00
|
|
|
TYPE_OPUS=2,
|
|
|
|
TYPE_SKELETON=3,
|
|
|
|
TYPE_UNKNOWN=4
|
2010-04-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
virtual ~OggCodecState();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// Factory for creating nsCodecStates. Use instead of constructor.
|
|
|
|
// aPage should be a beginning-of-stream page.
|
2012-11-14 11:46:40 -08:00
|
|
|
static OggCodecState* Create(ogg_page* aPage);
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
virtual CodecType GetType() { return TYPE_UNKNOWN; }
|
2012-05-31 11:13:17 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Reads a header packet. Returns true when last header has been read.
|
2012-05-31 11:13:17 -07:00
|
|
|
// This function takes ownership of the packet and is responsible for
|
|
|
|
// releasing it or queuing it for later processing.
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool DecodeHeader(ogg_packet* aPacket) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return (mDoneReadingHeaders = true);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:42:36 -07:00
|
|
|
// Build a hash table with tag metadata parsed from the stream.
|
2012-11-08 16:40:08 -08:00
|
|
|
virtual MetadataTags* GetTags() {
|
2012-09-17 15:42:36 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// Returns the end time that a granulepos represents.
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int64_t Time(int64_t granulepos) { return -1; }
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Returns the start time that a granulepos represents.
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int64_t StartTime(int64_t granulepos) { return -1; }
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Initializes the codec state.
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool Init();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Returns true when this bitstream has finished reading all its
|
2010-04-01 20:03:07 -07:00
|
|
|
// header packets.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DoneReadingHeaders() { return mDoneReadingHeaders; }
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Deactivates the bitstream. Only the primary video and audio bitstreams
|
|
|
|
// should be active.
|
2011-05-07 23:24:09 -07:00
|
|
|
void Deactivate() {
|
2011-09-29 16:34:37 -07:00
|
|
|
mActive = false;
|
|
|
|
mDoneReadingHeaders = true;
|
2011-05-07 23:24:09 -07:00
|
|
|
Reset();
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Resets decoding state.
|
|
|
|
virtual nsresult Reset();
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
// Returns true if the OggCodecState thinks this packet is a header
|
2011-05-07 23:24:09 -07:00
|
|
|
// packet. Note this does not verify the validity of the header packet,
|
|
|
|
// it just guarantees that the packet is marked as a header packet (i.e.
|
|
|
|
// it is definintely not a data packet). Do not use this to identify
|
|
|
|
// streams, use it to filter header packets from data packets while
|
|
|
|
// decoding.
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool IsHeader(ogg_packet* aPacket) { return false; }
|
2011-05-07 23:24:09 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
// Returns the next packet in the stream, or nullptr if there are no more
|
2011-05-07 23:24:09 -07:00
|
|
|
// packets buffered in the packet queue. More packets can be buffered by
|
|
|
|
// inserting one or more pages into the stream by calling PageIn(). The
|
|
|
|
// caller is responsible for deleting returned packet's using
|
2012-11-14 11:46:40 -08:00
|
|
|
// OggCodecState::ReleasePacket(). The packet will have a valid granulepos.
|
2011-05-07 23:24:09 -07:00
|
|
|
ogg_packet* PacketOut();
|
|
|
|
|
|
|
|
// Releases the memory used by a cloned packet. Every packet returned by
|
|
|
|
// PacketOut() must be free'd using this function.
|
|
|
|
static void ReleasePacket(ogg_packet* aPacket);
|
|
|
|
|
|
|
|
// Extracts all packets from the page, and inserts them into the packet
|
|
|
|
// queue. They can be extracted by calling PacketOut(). Packets from an
|
|
|
|
// inactive stream are not buffered, i.e. this call has no effect for
|
|
|
|
// inactive streams. Multiple pages may need to be inserted before
|
|
|
|
// PacketOut() starts to return packets, as granulepos may need to be
|
|
|
|
// captured.
|
|
|
|
virtual nsresult PageIn(ogg_page* aPage);
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Number of packets read.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t mPacketCount;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Serial number of the bitstream.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mSerial;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Ogg specific state.
|
|
|
|
ogg_stream_state mState;
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// Queue of as yet undecoded packets. Packets are guaranteed to have
|
|
|
|
// a valid granulepos.
|
2012-11-14 11:46:40 -08:00
|
|
|
PacketQueue mPackets;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Is the bitstream active; whether we're decoding and playing this bitstream.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mActive;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// True when all headers packets have been read.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDoneReadingHeaders;
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
protected:
|
2012-11-14 11:46:40 -08:00
|
|
|
// Constructs a new OggCodecState. aActive denotes whether the stream is
|
2011-05-07 23:24:09 -07:00
|
|
|
// active. For streams of unsupported or unknown types, aActive should be
|
2011-09-29 16:34:37 -07:00
|
|
|
// false.
|
2012-11-14 11:46:40 -08:00
|
|
|
OggCodecState(ogg_page* aBosPage, bool aActive);
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
// Deallocates all packets stored in mUnstamped, and clears the array.
|
|
|
|
void ClearUnstamped();
|
|
|
|
|
|
|
|
// Extracts packets out of mState until a data packet with a non -1
|
|
|
|
// granulepos is encountered, or no more packets are readable. Header
|
|
|
|
// packets are pushed into the packet queue immediately, and data packets
|
|
|
|
// are buffered in mUnstamped. Once a non -1 granulepos packet is read
|
|
|
|
// the granulepos of the packets in mUnstamped can be inferred, and they
|
|
|
|
// can be pushed over to mPackets. Used by PageIn() implementations in
|
|
|
|
// subclasses.
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult PacketOutUntilGranulepos(bool& aFoundGranulepos);
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
// Temporary buffer in which to store packets while we're reading packets
|
|
|
|
// in order to capture granulepos.
|
|
|
|
nsTArray<ogg_packet*> mUnstamped;
|
2012-09-17 15:42:36 -07:00
|
|
|
|
|
|
|
// Validation utility for vorbis-style tag names.
|
|
|
|
static bool IsValidVorbisTagName(nsCString& aName);
|
|
|
|
|
|
|
|
// Utility method to parse and add a vorbis-style comment
|
|
|
|
// to a metadata hash table. Most Ogg-encapsulated codecs
|
|
|
|
// use the vorbis comment format for metadata.
|
2012-11-08 16:40:08 -08:00
|
|
|
static bool AddVorbisComment(MetadataTags* aTags,
|
2012-09-17 15:42:36 -07:00
|
|
|
const char* aComment,
|
|
|
|
uint32_t aLength);
|
2010-04-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class VorbisState : public OggCodecState {
|
2010-04-01 20:03:07 -07:00
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
VorbisState(ogg_page* aBosPage);
|
|
|
|
virtual ~VorbisState();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
CodecType GetType() { return TYPE_VORBIS; }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DecodeHeader(ogg_packet* aPacket);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t Time(int64_t granulepos);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Init();
|
2011-05-07 23:24:09 -07:00
|
|
|
nsresult Reset();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsHeader(ogg_packet* aPacket);
|
2011-05-07 23:24:09 -07:00
|
|
|
nsresult PageIn(ogg_page* aPage);
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2012-09-17 15:42:36 -07:00
|
|
|
// Return a hash table with tag metadata.
|
2012-11-08 16:40:08 -08:00
|
|
|
MetadataTags* GetTags();
|
2012-09-17 15:42:36 -07:00
|
|
|
|
2010-12-16 17:39:01 -08:00
|
|
|
// Returns the end time that a granulepos represents.
|
2012-08-22 08:56:38 -07:00
|
|
|
static int64_t Time(vorbis_info* aInfo, int64_t aGranulePos);
|
2011-05-07 23:24:09 -07:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
vorbis_info mInfo;
|
|
|
|
vorbis_comment mComment;
|
|
|
|
vorbis_dsp_state mDsp;
|
|
|
|
vorbis_block mBlock;
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Reconstructs the granulepos of Vorbis packets stored in the mUnstamped
|
|
|
|
// array.
|
|
|
|
nsresult ReconstructVorbisGranulepos();
|
|
|
|
|
|
|
|
// The "block size" of the previously decoded Vorbis packet, or 0 if we've
|
|
|
|
// not yet decoded anything. This is used to calculate the number of samples
|
|
|
|
// in a Vorbis packet, since each Vorbis packet depends on the previous
|
|
|
|
// packet while being decoded.
|
|
|
|
long mPrevVorbisBlockSize;
|
|
|
|
|
|
|
|
// Granulepos (end sample) of the last decoded Vorbis packet. This is used
|
|
|
|
// to calculate the Vorbis granulepos when we don't find a granulepos to
|
|
|
|
// back-propagate from.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mGranulepos;
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
|
|
|
|
// When validating that we've correctly predicted Vorbis packets' number
|
|
|
|
// of samples, we store each packet's predicted number of samples in this
|
|
|
|
// map, and verify we decode the predicted number of samples.
|
|
|
|
std::map<ogg_packet*, long> mVorbisPacketSamples;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Records that aPacket is predicted to have aSamples samples.
|
|
|
|
// This function has no effect if VALIDATE_VORBIS_SAMPLE_CALCULATION
|
|
|
|
// is not defined.
|
|
|
|
void RecordVorbisPacketSamples(ogg_packet* aPacket, long aSamples);
|
|
|
|
|
|
|
|
// Verifies that aPacket has had its number of samples predicted.
|
|
|
|
// This function has no effect if VALIDATE_VORBIS_SAMPLE_CALCULATION
|
|
|
|
// is not defined.
|
|
|
|
void AssertHasRecordedPacketSamples(ogg_packet* aPacket);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Asserts that the number of samples predicted for aPacket is aSamples.
|
|
|
|
// This function has no effect if VALIDATE_VORBIS_SAMPLE_CALCULATION
|
|
|
|
// is not defined.
|
|
|
|
void ValidateVorbisPacketSamples(ogg_packet* aPacket, long aSamples);
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
// Returns 1 if the Theora info struct is decoding a media of Theora
|
2011-09-06 22:50:26 -07:00
|
|
|
// version (maj,min,sub) or later, otherwise returns 0.
|
2011-05-07 23:24:09 -07:00
|
|
|
int TheoraVersion(th_info* info,
|
|
|
|
unsigned char maj,
|
|
|
|
unsigned char min,
|
|
|
|
unsigned char sub);
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class TheoraState : public OggCodecState {
|
2010-04-01 20:03:07 -07:00
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
TheoraState(ogg_page* aBosPage);
|
|
|
|
virtual ~TheoraState();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-05-07 23:24:09 -07:00
|
|
|
CodecType GetType() { return TYPE_THEORA; }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DecodeHeader(ogg_packet* aPacket);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t Time(int64_t granulepos);
|
|
|
|
int64_t StartTime(int64_t granulepos);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Init();
|
|
|
|
bool IsHeader(ogg_packet* aPacket);
|
2011-05-07 23:24:09 -07:00
|
|
|
nsresult PageIn(ogg_page* aPage);
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// Returns the maximum number of microseconds which a keyframe can be offset
|
2010-04-01 20:03:07 -07:00
|
|
|
// from any given interframe.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t MaxKeyframeOffset();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-12-16 17:39:01 -08:00
|
|
|
// Returns the end time that a granulepos represents.
|
2012-08-22 08:56:38 -07:00
|
|
|
static int64_t Time(th_info* aInfo, int64_t aGranulePos);
|
2011-05-07 23:24:09 -07:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
th_info mInfo;
|
|
|
|
th_comment mComment;
|
|
|
|
th_setup_info *mSetup;
|
|
|
|
th_dec_ctx* mCtx;
|
|
|
|
|
2010-05-23 14:36:10 -07:00
|
|
|
float mPixelAspectRatio;
|
2011-05-07 23:24:09 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Reconstructs the granulepos of Theora packets stored in the
|
|
|
|
// mUnstamped array. mUnstamped must be filled with consecutive packets from
|
|
|
|
// the stream, with the last packet having a known granulepos. Using this
|
|
|
|
// known granulepos, and the known frame numbers, we recover the granulepos
|
|
|
|
// of all frames in the array. This enables us to determine their timestamps.
|
|
|
|
void ReconstructTheoraGranulepos();
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class OpusState : public OggCodecState {
|
2012-05-01 17:29:34 -07:00
|
|
|
#ifdef MOZ_OPUS
|
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
OpusState(ogg_page* aBosPage);
|
|
|
|
virtual ~OpusState();
|
2012-05-01 17:29:34 -07:00
|
|
|
|
|
|
|
CodecType GetType() { return TYPE_OPUS; }
|
|
|
|
bool DecodeHeader(ogg_packet* aPacket);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t Time(int64_t aGranulepos);
|
2012-05-01 17:29:34 -07:00
|
|
|
bool Init();
|
|
|
|
nsresult Reset();
|
2012-05-31 11:13:17 -07:00
|
|
|
nsresult Reset(bool aStart);
|
2012-05-01 17:29:34 -07:00
|
|
|
bool IsHeader(ogg_packet* aPacket);
|
|
|
|
nsresult PageIn(ogg_page* aPage);
|
|
|
|
|
2012-05-31 11:13:11 -07:00
|
|
|
// Returns the end time that a granulepos represents.
|
2012-08-22 08:56:38 -07:00
|
|
|
static int64_t Time(int aPreSkip, int64_t aGranulepos);
|
2012-05-31 11:13:11 -07:00
|
|
|
|
2012-05-01 17:29:34 -07:00
|
|
|
// Various fields from the Ogg Opus header.
|
|
|
|
int mRate; // Sample rate the decoder uses (always 48 kHz).
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mNominalRate; // Original sample rate of the data (informational).
|
2012-05-01 17:29:34 -07:00
|
|
|
int mChannels; // Number of channels the stream encodes.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t mPreSkip; // Number of samples to strip after decoder reset.
|
2012-05-31 11:13:17 -07:00
|
|
|
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
|
|
|
|
float mGain; // Gain to apply to decoder output.
|
|
|
|
#else
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mGain_Q16; // Gain to apply to the decoder output.
|
2012-05-31 11:13:17 -07:00
|
|
|
#endif
|
2012-05-01 17:29:34 -07:00
|
|
|
int mChannelMapping; // Channel mapping family.
|
|
|
|
int mStreams; // Number of packed streams in each packet.
|
2012-08-09 15:53:23 -07:00
|
|
|
int mCoupledStreams; // Number of packed coupled streams in each packet.
|
|
|
|
unsigned char mMappingTable[255]; // Channel mapping table.
|
|
|
|
|
|
|
|
OpusMSDecoder *mDecoder;
|
2012-05-01 17:29:34 -07:00
|
|
|
|
2012-05-30 21:03:14 -07:00
|
|
|
int mSkip; // Number of samples left to trim before playback.
|
2012-05-31 11:13:17 -07:00
|
|
|
// Granule position (end sample) of the last decoded Opus packet. This is
|
|
|
|
// used to calculate the amount we should trim from the last packet.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mPrevPacketGranulepos;
|
2012-05-01 17:29:34 -07:00
|
|
|
|
2012-09-17 15:42:36 -07:00
|
|
|
// Construct and return a table of tags from the metadata header.
|
2012-11-08 16:40:08 -08:00
|
|
|
MetadataTags* GetTags();
|
2012-09-17 15:42:36 -07:00
|
|
|
|
2012-05-01 17:29:34 -07:00
|
|
|
private:
|
|
|
|
|
2012-09-17 15:42:36 -07:00
|
|
|
nsCString mVendorString; // Encoder vendor string from the header.
|
|
|
|
nsTArray<nsCString> mTags; // Unparsed comment strings from the header.
|
|
|
|
|
2012-05-01 17:29:34 -07:00
|
|
|
// Reconstructs the granulepos of Opus packets stored in the
|
|
|
|
// mUnstamped array. mUnstamped must be filled with consecutive packets from
|
|
|
|
// the stream, with the last packet having a known granulepos. Using this
|
|
|
|
// known granulepos, and the known frame numbers, we recover the granulepos
|
|
|
|
// of all frames in the array. This enables us to determine their timestamps.
|
2012-05-31 11:13:17 -07:00
|
|
|
bool ReconstructOpusGranulepos();
|
|
|
|
|
|
|
|
// Granule position (end sample) of the last decoded Opus page. This is
|
|
|
|
// used to calculate the Opus per-packet granule positions on the last page,
|
|
|
|
// where we may need to trim some samples from the end.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mPrevPageGranulepos;
|
2012-05-31 11:13:17 -07:00
|
|
|
|
2012-05-01 17:29:34 -07:00
|
|
|
#endif /* MOZ_OPUS */
|
|
|
|
};
|
|
|
|
|
2010-08-19 15:50:37 -07:00
|
|
|
// Constructs a 32bit version number out of two 16 bit major,minor
|
|
|
|
// version numbers.
|
|
|
|
#define SKELETON_VERSION(major, minor) (((major)<<16)|(minor))
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
class SkeletonState : public OggCodecState {
|
2010-04-01 20:03:07 -07:00
|
|
|
public:
|
2012-11-14 11:46:40 -08:00
|
|
|
SkeletonState(ogg_page* aBosPage);
|
|
|
|
~SkeletonState();
|
2011-05-07 23:24:09 -07:00
|
|
|
CodecType GetType() { return TYPE_SKELETON; }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DecodeHeader(ogg_packet* aPacket);
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t Time(int64_t granulepos) { return -1; }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Init() { return true; }
|
|
|
|
bool IsHeader(ogg_packet* aPacket) { return true; }
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Return true if the given time (in milliseconds) is within
|
2011-03-23 18:16:30 -07:00
|
|
|
// the presentation time defined in the skeleton track.
|
2012-08-22 08:56:38 -07:00
|
|
|
bool IsPresentable(int64_t aTime) { return aTime >= mPresentationTime; }
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
// Stores the offset of the page on which a keyframe starts,
|
|
|
|
// and its presentation time.
|
|
|
|
class nsKeyPoint {
|
|
|
|
public:
|
|
|
|
nsKeyPoint()
|
2012-01-11 00:23:07 -08:00
|
|
|
: mOffset(INT64_MAX),
|
|
|
|
mTime(INT64_MAX) {}
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsKeyPoint(int64_t aOffset, int64_t aTime)
|
2010-08-19 15:50:37 -07:00
|
|
|
: mOffset(aOffset),
|
|
|
|
mTime(aTime) {}
|
|
|
|
|
|
|
|
// Offset from start of segment/link-in-the-chain in bytes.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mOffset;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// Presentation time in usecs.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mTime;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsNull() {
|
2012-01-11 00:23:07 -08:00
|
|
|
return mOffset == INT64_MAX &&
|
|
|
|
mTime == INT64_MAX;
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Stores a keyframe's byte-offset, presentation time and the serialno
|
|
|
|
// of the stream it belongs to.
|
|
|
|
class nsSeekTarget {
|
|
|
|
public:
|
|
|
|
nsSeekTarget() : mSerial(0) {}
|
|
|
|
nsKeyPoint mKeyPoint;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mSerial;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsNull() {
|
2010-08-19 15:50:37 -07:00
|
|
|
return mKeyPoint.IsNull() &&
|
|
|
|
mSerial == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Determines from the seek index the keyframe which you must seek back to
|
|
|
|
// in order to get all keyframes required to render all streams with
|
|
|
|
// serialnos in aTracks, at time aTarget.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult IndexedSeekTarget(int64_t aTarget,
|
|
|
|
nsTArray<uint32_t>& aTracks,
|
2010-08-19 15:50:37 -07:00
|
|
|
nsSeekTarget& aResult);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasIndex() const {
|
2010-11-07 22:43:24 -08:00
|
|
|
return mIndex.IsInitialized() && mIndex.Count() > 0;
|
2010-08-19 15:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the duration of the active tracks in the media, if we have
|
|
|
|
// an index. aTracks must be filled with the serialnos of the active tracks.
|
|
|
|
// The duration is calculated as the greatest end time of all active tracks,
|
|
|
|
// minus the smalled start time of all the active tracks.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult GetDuration(const nsTArray<uint32_t>& aTracks, int64_t& aDuration);
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Decodes an index packet. Returns false on failure.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DecodeIndex(ogg_packet* aPacket);
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
// Gets the keypoint you must seek to in order to get the keyframe required
|
|
|
|
// to render the stream at time aTarget on stream with serial aSerialno.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult IndexedSeekTargetForTrack(uint32_t aSerialno,
|
|
|
|
int64_t aTarget,
|
2010-08-19 15:50:37 -07:00
|
|
|
nsKeyPoint& aResult);
|
|
|
|
|
|
|
|
// Version of the decoded skeleton track, as per the SKELETON_VERSION macro.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mVersion;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2011-03-23 18:16:30 -07:00
|
|
|
// Presentation time of the resource in milliseconds
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mPresentationTime;
|
2011-03-23 18:16:30 -07:00
|
|
|
|
2010-08-19 15:50:37 -07:00
|
|
|
// Length of the resource in bytes.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mLength;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
// Stores the keyframe index and duration information for a particular
|
|
|
|
// stream.
|
|
|
|
class nsKeyFrameIndex {
|
|
|
|
public:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsKeyFrameIndex(int64_t aStartTime, int64_t aEndTime)
|
2010-08-19 15:50:37 -07:00
|
|
|
: mStartTime(aStartTime),
|
|
|
|
mEndTime(aEndTime)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsKeyFrameIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsKeyFrameIndex() {
|
|
|
|
MOZ_COUNT_DTOR(nsKeyFrameIndex);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void Add(int64_t aOffset, int64_t aTimeMs) {
|
2010-08-19 15:50:37 -07:00
|
|
|
mKeyPoints.AppendElement(nsKeyPoint(aOffset, aTimeMs));
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const nsKeyPoint& Get(uint32_t aIndex) const {
|
2010-08-19 15:50:37 -07:00
|
|
|
return mKeyPoints[aIndex];
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t Length() const {
|
2010-08-19 15:50:37 -07:00
|
|
|
return mKeyPoints.Length();
|
|
|
|
}
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// Presentation time of the first sample in this stream in usecs.
|
2012-08-22 08:56:38 -07:00
|
|
|
const int64_t mStartTime;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// End time of the last sample in this stream in usecs.
|
2012-08-22 08:56:38 -07:00
|
|
|
const int64_t mEndTime;
|
2010-08-19 15:50:37 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<nsKeyPoint> mKeyPoints;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Maps Ogg serialnos to the index-keypoint list.
|
|
|
|
nsClassHashtable<nsUint32HashKey, nsKeyFrameIndex> mIndex;
|
2010-04-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-31 11:13:17 -07:00
|
|
|
// This allows the use of nsAutoRefs for an ogg_packet that properly free the
|
|
|
|
// contents of the packet.
|
|
|
|
template <>
|
|
|
|
class nsAutoRefTraits<ogg_packet> : public nsPointerRefTraits<ogg_packet>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void Release(ogg_packet* aPacket) {
|
2012-11-14 11:46:40 -08:00
|
|
|
mozilla::OggCodecState::ReleasePacket(aPacket);
|
2012-05-31 11:13:17 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
#endif
|