2010-04-02 07:03:07 +04: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 15:12:37 +04: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 23:46:40 +04:00
|
|
|
#if !defined(OggCodecState_h_)
|
|
|
|
# define OggCodecState_h_
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
# include <ogg/ogg.h>
|
2012-05-31 22:13:17 +04:00
|
|
|
// For MOZ_SAMPLE_TYPE_*
|
2017-02-07 11:15:59 +03:00
|
|
|
# include "FlacFrameParser.h"
|
2020-04-22 14:41:42 +03:00
|
|
|
# include "OggRLBoxTypes.h"
|
2017-02-07 11:15:59 +03:00
|
|
|
# include "VideoUtils.h"
|
2010-04-02 07:03:07 +04:00
|
|
|
# include <nsDeque.h>
|
2010-08-20 02:50:37 +04:00
|
|
|
# include <nsTArray.h>
|
|
|
|
# include <nsClassHashtable.h>
|
2019-01-18 12:16:18 +03:00
|
|
|
|
2016-11-09 04:19:06 +03:00
|
|
|
# include <theora/theoradec.h>
|
|
|
|
# ifdef MOZ_TREMOR
|
|
|
|
# include <tremor/ivorbiscodec.h>
|
|
|
|
# else
|
|
|
|
# include <vorbis/codec.h>
|
|
|
|
# endif
|
2012-01-11 12:23:07 +04:00
|
|
|
|
2011-05-08 10:24:09 +04: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
|
|
|
|
|
2016-11-09 04:19:06 +03:00
|
|
|
struct OpusMSDecoder;
|
2013-11-23 02:06:00 +04:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2020-04-22 14:41:42 +03:00
|
|
|
inline constexpr char RLBOX_SAFE_DEBUG_ASSERTION[] =
|
|
|
|
"Tainted data is being inspected only for debugging purposes. This is not "
|
|
|
|
"a condition that is critical for safety of the renderer.";
|
|
|
|
|
|
|
|
inline constexpr char RLBOX_OGG_STATE_ASSERT_REASON[] =
|
|
|
|
"Tainted data is being inspected only to check the internal state of "
|
|
|
|
"libogg structures. This is not a condition that is critical for safety of "
|
|
|
|
"the renderer.";
|
|
|
|
|
|
|
|
inline constexpr char RLBOX_OGG_PAGE_SERIAL_REASON[] =
|
|
|
|
"We are checking the serial of the page. If libogg is operating correctly, "
|
|
|
|
"we check serial numbers to make sure the Firefox renderer is correctly "
|
|
|
|
"passing streams to the correct source. If libogg has been corrupted, it "
|
|
|
|
"could return an incorrect serial, however this would mean that an OGG "
|
|
|
|
"file has intentionally corrupted data across multiple logical streams. "
|
|
|
|
"This however cannot compromise memory safety of the renderer.";
|
|
|
|
|
2016-11-09 04:19:06 +03:00
|
|
|
class OpusParser;
|
|
|
|
|
2017-02-07 12:56:41 +03:00
|
|
|
struct OggPacketDeletePolicy {
|
|
|
|
void operator()(ogg_packet* aPacket) const {
|
|
|
|
delete[] aPacket->packet;
|
|
|
|
delete aPacket;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-07 13:05:27 +03:00
|
|
|
using OggPacketPtr = UniquePtr<ogg_packet, OggPacketDeletePolicy>;
|
|
|
|
|
2012-11-23 23:47:45 +04:00
|
|
|
// Deallocates a packet, used in OggPacketQueue below.
|
2020-06-25 05:39:23 +03:00
|
|
|
class OggPacketDeallocator : public nsDequeFunctor<ogg_packet> {
|
|
|
|
virtual void operator()(ogg_packet* aPacket) override {
|
|
|
|
OggPacketDeletePolicy()(aPacket);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-08 10:24:09 +04:00
|
|
|
// A queue of ogg_packets. When we read a page, we extract the page's packets
|
2012-11-14 23:46:40 +04:00
|
|
|
// and buffer them in the owning stream's OggCodecState. This is because
|
2011-05-08 10:24:09 +04: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.
|
2020-06-25 05:39:23 +03:00
|
|
|
class OggPacketQueue : private nsDeque<ogg_packet> {
|
2010-04-02 07:03:07 +04:00
|
|
|
public:
|
2017-02-07 11:15:59 +03:00
|
|
|
OggPacketQueue() : nsDeque(new OggPacketDeallocator()) {}
|
2012-11-23 23:47:45 +04:00
|
|
|
~OggPacketQueue() { Erase(); }
|
2020-06-25 05:39:23 +03:00
|
|
|
bool IsEmpty() { return nsDeque<ogg_packet>::GetSize() == 0; }
|
2017-02-09 08:20:01 +03:00
|
|
|
void Append(OggPacketPtr aPacket);
|
|
|
|
OggPacketPtr PopFront() {
|
2020-06-25 05:39:23 +03:00
|
|
|
return OggPacketPtr(nsDeque<ogg_packet>::PopFront());
|
2017-02-09 08:20:01 +03:00
|
|
|
}
|
2020-06-25 05:39:23 +03:00
|
|
|
ogg_packet* PeekFront() { return nsDeque<ogg_packet>::PeekFront(); }
|
|
|
|
OggPacketPtr Pop() { return OggPacketPtr(nsDeque<ogg_packet>::Pop()); }
|
2016-12-07 04:41:52 +03:00
|
|
|
ogg_packet* operator[](size_t aIndex) const {
|
2020-06-25 05:39:23 +03:00
|
|
|
return nsDeque<ogg_packet>::ObjectAt(aIndex);
|
2016-12-07 04:41:52 +03:00
|
|
|
}
|
2020-06-25 05:39:23 +03:00
|
|
|
size_t Length() const { return nsDeque<ogg_packet>::GetSize(); }
|
2017-02-09 08:20:01 +03:00
|
|
|
void PushFront(OggPacketPtr aPacket) {
|
2020-06-25 05:39:23 +03:00
|
|
|
nsDeque<ogg_packet>::PushFront(aPacket.release());
|
2017-02-09 08:20:01 +03:00
|
|
|
}
|
2020-06-25 05:39:23 +03:00
|
|
|
void Erase() { nsDeque<ogg_packet>::Erase(); }
|
2010-04-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Encapsulates the data required for decoding an ogg bitstream and for
|
|
|
|
// converting granulepos to timestamps.
|
2016-07-27 10:01:27 +03:00
|
|
|
class OggCodecState {
|
2011-05-08 10:24:09 +04:00
|
|
|
public:
|
2012-11-14 23:45:31 +04:00
|
|
|
typedef mozilla::MetadataTags MetadataTags;
|
2010-04-02 07:03:07 +04:00
|
|
|
// Ogg types we know about
|
2016-07-27 10:01:27 +03:00
|
|
|
enum CodecType {
|
2010-04-02 07:03:07 +04:00
|
|
|
TYPE_VORBIS = 0,
|
2016-08-04 10:21:53 +03:00
|
|
|
TYPE_THEORA,
|
|
|
|
TYPE_OPUS,
|
|
|
|
TYPE_SKELETON,
|
|
|
|
TYPE_FLAC,
|
|
|
|
TYPE_UNKNOWN
|
2010-04-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
virtual ~OggCodecState();
|
2016-11-09 04:19:06 +03:00
|
|
|
|
2011-05-08 10:24:09 +04:00
|
|
|
// Factory for creating nsCodecStates. Use instead of constructor.
|
|
|
|
// aPage should be a beginning-of-stream page.
|
2021-02-16 18:53:33 +03:00
|
|
|
static UniquePtr<OggCodecState> Create(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aPage,
|
|
|
|
uint32_t aSerial);
|
2016-11-09 04:19:06 +03:00
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
virtual CodecType GetType() { return TYPE_UNKNOWN; }
|
2012-05-31 22:13:17 +04:00
|
|
|
|
2013-03-14 04:57:18 +04:00
|
|
|
// Reads a header packet. Returns false if an error was encountered
|
|
|
|
// while reading header packets. Callers should check DoneReadingHeaders()
|
|
|
|
// to determine if the last header has been read.
|
2012-05-31 22:13:17 +04:00
|
|
|
// This function takes ownership of the packet and is responsible for
|
|
|
|
// releasing it or queuing it for later processing.
|
2017-02-09 08:52:41 +03:00
|
|
|
virtual bool DecodeHeader(OggPacketPtr aPacket) {
|
2011-09-30 03:34:37 +04:00
|
|
|
return (mDoneReadingHeaders = true);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-09-18 02:42:36 +04:00
|
|
|
// Build a hash table with tag metadata parsed from the stream.
|
2018-11-20 18:10:31 +03:00
|
|
|
virtual UniquePtr<MetadataTags> GetTags() { return nullptr; }
|
2012-09-18 02:42:36 +04:00
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
// Returns the end time that a granulepos represents.
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual int64_t Time(int64_t granulepos) { return -1; }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Returns the start time that a granulepos represents.
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual int64_t StartTime(int64_t granulepos) { return -1; }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2016-07-21 04:28:47 +03:00
|
|
|
// Returns the duration of the given packet, if it can be determined.
|
|
|
|
virtual int64_t PacketDuration(ogg_packet* aPacket) { return -1; }
|
|
|
|
|
|
|
|
// Returns the start time of the given packet, if it can be determined.
|
2016-07-27 10:01:27 +03:00
|
|
|
virtual int64_t PacketStartTime(ogg_packet* aPacket) {
|
2016-07-21 04:28:47 +03:00
|
|
|
if (aPacket->granulepos < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int64_t endTime = Time(aPacket->granulepos);
|
|
|
|
int64_t duration = PacketDuration(aPacket);
|
|
|
|
if (duration > endTime) {
|
|
|
|
// Audio preskip may eat a whole packet or more.
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return endTime - duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
// Initializes the codec state.
|
2016-08-04 10:21:53 +03:00
|
|
|
virtual bool Init() { return true; }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-09-30 03:34:37 +04:00
|
|
|
// Returns true when this bitstream has finished reading all its
|
2010-04-02 07:03:07 +04:00
|
|
|
// header packets.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool DoneReadingHeaders() { return mDoneReadingHeaders; }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Deactivates the bitstream. Only the primary video and audio bitstreams
|
|
|
|
// should be active.
|
2016-07-27 10:01:27 +03:00
|
|
|
void Deactivate() {
|
2011-09-30 03:34:37 +04:00
|
|
|
mActive = false;
|
|
|
|
mDoneReadingHeaders = true;
|
2011-05-08 10:24:09 +04:00
|
|
|
Reset();
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Resets decoding state.
|
|
|
|
virtual nsresult Reset();
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
// Returns true if the OggCodecState thinks this packet is a header
|
2011-05-08 10:24:09 +04: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-29 10:19:26 +04:00
|
|
|
virtual bool IsHeader(ogg_packet* aPacket) { return false; }
|
2011-05-08 10:24:09 +04:00
|
|
|
|
2016-07-21 04:28:47 +03:00
|
|
|
// Returns true if the OggCodecState thinks this packet represents a
|
|
|
|
// keyframe, from which decoding can restart safely.
|
|
|
|
virtual bool IsKeyframe(ogg_packet* aPacket) { return true; }
|
|
|
|
|
|
|
|
// Returns true if there is a packet available for dequeueing in the stream.
|
|
|
|
bool IsPacketReady();
|
|
|
|
|
|
|
|
// Returns the next raw packet in the stream, or nullptr if there are no more
|
2011-05-08 10:24:09 +04:00
|
|
|
// packets buffered in the packet queue. More packets can be buffered by
|
2017-02-09 08:39:56 +03:00
|
|
|
// inserting one or more pages into the stream by calling PageIn().
|
|
|
|
// The packet will have a valid granulepos.
|
2017-02-09 08:20:01 +03:00
|
|
|
OggPacketPtr PacketOut();
|
2011-05-08 10:24:09 +04:00
|
|
|
|
2016-07-21 04:28:47 +03:00
|
|
|
// Returns the next raw packet in the stream, or nullptr if there are no more
|
|
|
|
// packets buffered in the packet queue, without consuming it.
|
|
|
|
// The packet will have a valid granulepos.
|
|
|
|
ogg_packet* PacketPeek();
|
|
|
|
|
2016-07-27 09:19:03 +03:00
|
|
|
// Moves all raw packets from aOther to the front of the current packet queue.
|
|
|
|
void PushFront(OggPacketQueue&& aOther);
|
|
|
|
|
2016-07-21 04:28:47 +03:00
|
|
|
// Returns the next packet in the stream as a MediaRawData, or nullptr
|
|
|
|
// if there are no more packets buffered in the packet queue. More packets
|
|
|
|
// can be buffered by inserting one or more pages into the stream by calling
|
|
|
|
// PageIn(). The packet will have a valid granulepos.
|
2016-11-30 09:18:58 +03:00
|
|
|
virtual already_AddRefed<MediaRawData> PacketOutAsMediaRawData();
|
2016-07-21 04:28:47 +03:00
|
|
|
|
2011-05-08 10:24:09 +04:00
|
|
|
// 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.
|
2020-04-22 14:41:42 +03:00
|
|
|
virtual nsresult PageIn(tainted_opaque_ogg<ogg_page*> aPage);
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2016-12-08 21:38:04 +03:00
|
|
|
// Returns the maximum number of microseconds which a keyframe can be offset
|
|
|
|
// from any given interframe.b
|
|
|
|
virtual int64_t MaxKeyframeOffset() { return 0; }
|
|
|
|
// Public access for mTheoraInfo.keyframe_granule_shift
|
|
|
|
virtual int32_t KeyFrameGranuleJobs() { return 0; }
|
|
|
|
|
2016-11-09 04:19:06 +03:00
|
|
|
// Number of packets read.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t mPacketCount;
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Serial number of the bitstream.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mSerial;
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Ogg specific state.
|
2020-04-22 14:41:42 +03:00
|
|
|
tainted_opaque_ogg<ogg_stream_state*> mState;
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2011-05-08 10:24:09 +04:00
|
|
|
// Queue of as yet undecoded packets. Packets are guaranteed to have
|
|
|
|
// a valid granulepos.
|
2012-11-23 23:47:45 +04:00
|
|
|
OggPacketQueue mPackets;
|
2010-04-02 07:03:07 +04:00
|
|
|
|
|
|
|
// Is the bitstream active; whether we're decoding and playing this bitstream.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mActive;
|
2016-11-09 04:19:06 +03:00
|
|
|
|
2011-09-30 03:34:37 +04:00
|
|
|
// True when all headers packets have been read.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDoneReadingHeaders;
|
2011-05-08 10:24:09 +04:00
|
|
|
|
2020-04-22 14:41:42 +03:00
|
|
|
// All invocations of libogg functionality from the demuxer is sandboxed using
|
|
|
|
// wasm library sandboxes on supported platforms. This is the sandbox
|
|
|
|
// instance.
|
|
|
|
rlbox_sandbox_ogg* mSandbox;
|
|
|
|
|
2016-12-05 10:06:11 +03:00
|
|
|
virtual const TrackInfo* GetInfo() const {
|
|
|
|
MOZ_RELEASE_ASSERT(false, "Can't be called directly");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-04 10:21:53 +03: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.
|
2018-11-20 18:10:31 +03:00
|
|
|
static bool AddVorbisComment(UniquePtr<MetadataTags>& aTags,
|
|
|
|
const char* aComment, uint32_t aLength);
|
2016-08-04 10:21:53 +03:00
|
|
|
|
2018-11-20 18:10:31 +03:00
|
|
|
protected:
|
2012-11-14 23:46:40 +04:00
|
|
|
// Constructs a new OggCodecState. aActive denotes whether the stream is
|
2011-05-08 10:24:09 +04:00
|
|
|
// active. For streams of unsupported or unknown types, aActive should be
|
2011-09-30 03:34:37 +04:00
|
|
|
// false.
|
2020-04-22 14:41:42 +03:00
|
|
|
OggCodecState(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aBosPage, uint32_t aSerial,
|
|
|
|
bool aActive);
|
2011-05-08 10:24:09 +04: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-29 10:19:26 +04:00
|
|
|
nsresult PacketOutUntilGranulepos(bool& aFoundGranulepos);
|
2011-05-08 10:24:09 +04:00
|
|
|
|
|
|
|
// Temporary buffer in which to store packets while we're reading packets
|
|
|
|
// in order to capture granulepos.
|
2017-02-09 05:14:36 +03:00
|
|
|
nsTArray<OggPacketPtr> mUnstamped;
|
2012-09-18 02:42:36 +04:00
|
|
|
|
2017-02-07 11:15:59 +03:00
|
|
|
bool SetCodecSpecificConfig(MediaByteBuffer* aBuffer,
|
|
|
|
OggPacketQueue& aHeaders);
|
2016-12-12 05:54:19 +03:00
|
|
|
|
2016-08-04 10:21:53 +03:00
|
|
|
private:
|
|
|
|
bool InternalInit();
|
2010-04-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
class VorbisState : public OggCodecState {
|
2010-04-02 07:03:07 +04:00
|
|
|
public:
|
2020-04-22 14:41:42 +03:00
|
|
|
explicit VorbisState(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aBosPage,
|
|
|
|
uint32_t aSerial);
|
2012-11-14 23:46:40 +04:00
|
|
|
virtual ~VorbisState();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2016-07-26 09:39:44 +03:00
|
|
|
CodecType GetType() override { return TYPE_VORBIS; }
|
2017-02-09 08:52:41 +03:00
|
|
|
bool DecodeHeader(OggPacketPtr aPacket) override;
|
2016-07-26 09:39:44 +03:00
|
|
|
int64_t Time(int64_t granulepos) override;
|
|
|
|
int64_t PacketDuration(ogg_packet* aPacket) override;
|
|
|
|
bool Init() override;
|
|
|
|
nsresult Reset() override;
|
|
|
|
bool IsHeader(ogg_packet* aPacket) override;
|
2020-04-22 14:41:42 +03:00
|
|
|
nsresult PageIn(tainted_opaque_ogg<ogg_page*> aPage) override;
|
2016-12-07 04:41:52 +03:00
|
|
|
const TrackInfo* GetInfo() const override { return &mInfo; }
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2012-09-18 02:42:36 +04:00
|
|
|
// Return a hash table with tag metadata.
|
2018-11-20 18:10:31 +03:00
|
|
|
UniquePtr<MetadataTags> GetTags() override;
|
2012-09-18 02:42:36 +04:00
|
|
|
|
2016-12-07 04:41:52 +03:00
|
|
|
private:
|
|
|
|
AudioInfo mInfo;
|
|
|
|
vorbis_info mVorbisInfo;
|
2010-04-02 07:03:07 +04:00
|
|
|
vorbis_comment mComment;
|
|
|
|
vorbis_dsp_state mDsp;
|
|
|
|
vorbis_block mBlock;
|
2016-12-07 04:41:52 +03:00
|
|
|
OggPacketQueue mHeaders;
|
2011-05-08 10:24:09 +04:00
|
|
|
|
2016-12-07 04:41:52 +03:00
|
|
|
// Returns the end time that a granulepos represents.
|
|
|
|
static int64_t Time(vorbis_info* aInfo, int64_t aGranulePos);
|
2011-05-08 10:24:09 +04:00
|
|
|
|
|
|
|
// Reconstructs the granulepos of Vorbis packets stored in the mUnstamped
|
|
|
|
// array.
|
2019-11-28 03:12:44 +03:00
|
|
|
void ReconstructVorbisGranulepos();
|
2011-05-08 10:24:09 +04:00
|
|
|
|
|
|
|
// 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 19:56:38 +04:00
|
|
|
int64_t mGranulepos;
|
2011-05-08 10:24:09 +04: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-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
2011-05-08 10:24:09 +04:00
|
|
|
// Returns 1 if the Theora info struct is decoding a media of Theora
|
2011-09-07 09:50:26 +04:00
|
|
|
// version (maj,min,sub) or later, otherwise returns 0.
|
2011-05-08 10:24:09 +04:00
|
|
|
int TheoraVersion(th_info* info, unsigned char maj, unsigned char min,
|
|
|
|
unsigned char sub);
|
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
class TheoraState : public OggCodecState {
|
2010-04-02 07:03:07 +04:00
|
|
|
public:
|
2020-04-22 14:41:42 +03:00
|
|
|
explicit TheoraState(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aBosPage,
|
|
|
|
uint32_t aSerial);
|
2012-11-14 23:46:40 +04:00
|
|
|
virtual ~TheoraState();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2016-07-26 09:39:44 +03:00
|
|
|
CodecType GetType() override { return TYPE_THEORA; }
|
2017-02-09 08:52:41 +03:00
|
|
|
bool DecodeHeader(OggPacketPtr aPacket) override;
|
2016-07-26 09:39:44 +03:00
|
|
|
int64_t Time(int64_t granulepos) override;
|
|
|
|
int64_t StartTime(int64_t granulepos) override;
|
|
|
|
int64_t PacketDuration(ogg_packet* aPacket) override;
|
|
|
|
bool Init() override;
|
2016-12-12 05:54:19 +03:00
|
|
|
nsresult Reset() override;
|
2016-07-26 09:39:44 +03:00
|
|
|
bool IsHeader(ogg_packet* aPacket) override;
|
|
|
|
bool IsKeyframe(ogg_packet* aPacket) override;
|
2020-04-22 14:41:42 +03:00
|
|
|
nsresult PageIn(tainted_opaque_ogg<ogg_page*> aPage) override;
|
2016-12-12 05:54:19 +03:00
|
|
|
const TrackInfo* GetInfo() const override { return &mInfo; }
|
2016-12-08 21:38:04 +03:00
|
|
|
int64_t MaxKeyframeOffset() override;
|
|
|
|
int32_t KeyFrameGranuleJobs() override {
|
|
|
|
return mTheoraInfo.keyframe_granule_shift;
|
|
|
|
}
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2016-12-12 05:54:19 +03:00
|
|
|
private:
|
2010-12-17 04:39:01 +03:00
|
|
|
// Returns the end time that a granulepos represents.
|
2016-11-09 04:19:06 +03:00
|
|
|
static int64_t Time(th_info* aInfo, int64_t aGranulePos);
|
2011-05-08 10:24:09 +04:00
|
|
|
|
2016-12-12 05:54:19 +03:00
|
|
|
th_info mTheoraInfo;
|
2010-04-02 07:03:07 +04:00
|
|
|
th_comment mComment;
|
2016-07-21 04:28:47 +03:00
|
|
|
th_setup_info* mSetup;
|
2010-04-02 07:03:07 +04:00
|
|
|
th_dec_ctx* mCtx;
|
|
|
|
|
2016-12-12 05:54:19 +03:00
|
|
|
VideoInfo mInfo;
|
|
|
|
OggPacketQueue mHeaders;
|
2011-05-08 10:24:09 +04:00
|
|
|
|
|
|
|
// 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-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
class OpusState : public OggCodecState {
|
2012-05-02 04:29:34 +04:00
|
|
|
public:
|
2020-04-22 14:41:42 +03:00
|
|
|
explicit OpusState(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aBosPage, uint32_t aSerial);
|
2012-11-14 23:46:40 +04:00
|
|
|
virtual ~OpusState();
|
2012-05-02 04:29:34 +04:00
|
|
|
|
2016-07-26 09:39:44 +03:00
|
|
|
CodecType GetType() override { return TYPE_OPUS; }
|
2017-02-09 08:52:41 +03:00
|
|
|
bool DecodeHeader(OggPacketPtr aPacket) override;
|
2016-07-26 09:39:44 +03:00
|
|
|
int64_t Time(int64_t aGranulepos) override;
|
|
|
|
int64_t PacketDuration(ogg_packet* aPacket) override;
|
|
|
|
bool Init() override;
|
|
|
|
nsresult Reset() override;
|
2012-05-31 22:13:17 +04:00
|
|
|
nsresult Reset(bool aStart);
|
2016-07-26 09:39:44 +03:00
|
|
|
bool IsHeader(ogg_packet* aPacket) override;
|
2020-04-22 14:41:42 +03:00
|
|
|
nsresult PageIn(tainted_opaque_ogg<ogg_page*> aPage) override;
|
2016-11-30 09:44:49 +03:00
|
|
|
already_AddRefed<MediaRawData> PacketOutAsMediaRawData() override;
|
2016-12-05 12:34:38 +03:00
|
|
|
const TrackInfo* GetInfo() const override { return &mInfo; }
|
|
|
|
|
2012-05-31 22:13:11 +04:00
|
|
|
// Returns the end time that a granulepos represents.
|
2012-08-22 19:56:38 +04:00
|
|
|
static int64_t Time(int aPreSkip, int64_t aGranulepos);
|
2012-05-31 22:13:11 +04:00
|
|
|
|
2016-12-05 12:34:38 +03:00
|
|
|
// Construct and return a table of tags from the metadata header.
|
2018-11-20 18:10:31 +03:00
|
|
|
UniquePtr<MetadataTags> GetTags() override;
|
2012-08-10 02:53:23 +04:00
|
|
|
|
2016-12-05 12:34:38 +03:00
|
|
|
private:
|
2020-02-22 01:44:00 +03:00
|
|
|
UniquePtr<OpusParser> mParser;
|
2016-07-21 04:28:47 +03:00
|
|
|
OpusMSDecoder* mDecoder;
|
2012-05-02 04:29:34 +04:00
|
|
|
|
2012-05-31 22:13:17 +04: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 19:56:38 +04:00
|
|
|
int64_t mPrevPacketGranulepos;
|
2012-05-02 04:29:34 +04: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 22:13:17 +04: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 19:56:38 +04:00
|
|
|
int64_t mPrevPageGranulepos;
|
2016-12-05 12:34:38 +03:00
|
|
|
AudioInfo mInfo;
|
|
|
|
OggPacketQueue mHeaders;
|
2012-05-02 04:29:34 +04:00
|
|
|
};
|
|
|
|
|
2010-08-20 02:50:37 +04:00
|
|
|
// Constructs a 32bit version number out of two 16 bit major,minor
|
|
|
|
// version numbers.
|
|
|
|
# define SKELETON_VERSION(major, minor) (((major) << 16) | (minor))
|
|
|
|
|
2017-02-07 11:15:59 +03:00
|
|
|
enum EMsgHeaderType {
|
2014-09-03 07:01:37 +04:00
|
|
|
eContentType,
|
|
|
|
eRole,
|
|
|
|
eName,
|
|
|
|
eLanguage,
|
|
|
|
eTitle,
|
|
|
|
eDisplayHint,
|
|
|
|
eAltitude,
|
|
|
|
eTrackOrder,
|
|
|
|
eTrackDependencies
|
|
|
|
};
|
|
|
|
|
2020-03-01 15:39:18 +03:00
|
|
|
struct FieldPatternType {
|
2014-09-03 07:01:37 +04:00
|
|
|
const char* mPatternToRecognize;
|
|
|
|
EMsgHeaderType mMsgHeaderType;
|
2020-03-01 15:39:18 +03:00
|
|
|
};
|
2014-09-03 07:01:37 +04:00
|
|
|
|
|
|
|
// Stores the message information for different logical bitstream.
|
2020-03-01 15:39:18 +03:00
|
|
|
struct MessageField {
|
2014-09-03 07:01:37 +04:00
|
|
|
nsClassHashtable<nsUint32HashKey, nsCString> mValuesStore;
|
2020-03-01 15:39:18 +03:00
|
|
|
};
|
2014-09-03 07:01:37 +04:00
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
class SkeletonState : public OggCodecState {
|
2010-04-02 07:03:07 +04:00
|
|
|
public:
|
2020-04-22 14:41:42 +03:00
|
|
|
explicit SkeletonState(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aBosPage,
|
|
|
|
uint32_t aSerial);
|
2012-11-14 23:46:40 +04:00
|
|
|
~SkeletonState();
|
2014-09-03 07:01:37 +04:00
|
|
|
|
|
|
|
nsClassHashtable<nsUint32HashKey, MessageField> mMsgFieldStore;
|
|
|
|
|
2016-07-26 09:39:44 +03:00
|
|
|
CodecType GetType() override { return TYPE_SKELETON; }
|
2017-02-09 08:52:41 +03:00
|
|
|
bool DecodeHeader(OggPacketPtr aPacket) override;
|
2016-07-26 09:39:44 +03:00
|
|
|
int64_t Time(int64_t granulepos) override { return -1; }
|
|
|
|
bool IsHeader(ogg_packet* aPacket) override { return true; }
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2011-09-30 03:34:37 +04:00
|
|
|
// Return true if the given time (in milliseconds) is within
|
2011-03-24 04:16:30 +03:00
|
|
|
// the presentation time defined in the skeleton track.
|
2012-08-22 19:56:38 +04:00
|
|
|
bool IsPresentable(int64_t aTime) { return aTime >= mPresentationTime; }
|
2010-08-20 02:50:37 +04:00
|
|
|
|
|
|
|
// Stores the offset of the page on which a keyframe starts,
|
|
|
|
// and its presentation time.
|
2016-07-27 10:01:27 +03:00
|
|
|
class nsKeyPoint {
|
2010-08-20 02:50:37 +04:00
|
|
|
public:
|
2016-07-27 10:01:27 +03:00
|
|
|
nsKeyPoint() : mOffset(INT64_MAX), mTime(INT64_MAX) {}
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsKeyPoint(int64_t aOffset, int64_t aTime)
|
2016-07-27 10:01:27 +03:00
|
|
|
: mOffset(aOffset), mTime(aTime) {}
|
2010-08-20 02:50:37 +04:00
|
|
|
|
|
|
|
// Offset from start of segment/link-in-the-chain in bytes.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mOffset;
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// Presentation time in usecs.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mTime;
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
bool IsNull() { return mOffset == INT64_MAX && mTime == INT64_MAX; }
|
2010-08-20 02:50:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Stores a keyframe's byte-offset, presentation time and the serialno
|
|
|
|
// of the stream it belongs to.
|
2016-07-27 10:01:27 +03:00
|
|
|
class nsSeekTarget {
|
2010-08-20 02:50:37 +04:00
|
|
|
public:
|
2017-02-07 11:15:59 +03:00
|
|
|
nsSeekTarget() : mSerial(0) {}
|
2010-08-20 02:50:37 +04:00
|
|
|
nsKeyPoint mKeyPoint;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mSerial;
|
2016-07-27 10:01:27 +03:00
|
|
|
bool IsNull() { return mKeyPoint.IsNull() && mSerial == 0; }
|
2010-08-20 02:50:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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 19:56:38 +04:00
|
|
|
nsresult IndexedSeekTarget(int64_t aTarget, nsTArray<uint32_t>& aTracks,
|
2010-08-20 02:50:37 +04:00
|
|
|
nsSeekTarget& aResult);
|
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
bool HasIndex() const { return mIndex.Count() > 0; }
|
2010-08-20 02:50:37 +04: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 19:56:38 +04:00
|
|
|
nsresult GetDuration(const nsTArray<uint32_t>& aTracks, int64_t& aDuration);
|
2010-08-20 02:50:37 +04:00
|
|
|
|
|
|
|
private:
|
2011-09-30 03:34:37 +04:00
|
|
|
// Decodes an index packet. Returns false on failure.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool DecodeIndex(ogg_packet* aPacket);
|
2014-09-03 07:01:37 +04:00
|
|
|
// Decodes an fisbone packet. Returns false on failure.
|
|
|
|
bool DecodeFisbone(ogg_packet* aPacket);
|
2010-08-20 02:50:37 +04: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 19:56:38 +04:00
|
|
|
nsresult IndexedSeekTargetForTrack(uint32_t aSerialno, int64_t aTarget,
|
2010-08-20 02:50:37 +04:00
|
|
|
nsKeyPoint& aResult);
|
|
|
|
|
|
|
|
// Version of the decoded skeleton track, as per the SKELETON_VERSION macro.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mVersion;
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2011-03-24 04:16:30 +03:00
|
|
|
// Presentation time of the resource in milliseconds
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mPresentationTime;
|
2011-03-24 04:16:30 +03:00
|
|
|
|
2010-08-20 02:50:37 +04:00
|
|
|
// Length of the resource in bytes.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mLength;
|
2010-08-20 02:50:37 +04:00
|
|
|
|
|
|
|
// Stores the keyframe index and duration information for a particular
|
|
|
|
// stream.
|
2016-07-27 10:01:27 +03:00
|
|
|
class nsKeyFrameIndex {
|
2010-08-20 02:50:37 +04:00
|
|
|
public:
|
2016-11-09 04:19:06 +03:00
|
|
|
nsKeyFrameIndex(int64_t aStartTime, int64_t aEndTime)
|
2016-07-27 10:01:27 +03:00
|
|
|
: mStartTime(aStartTime), mEndTime(aEndTime) {
|
2010-08-20 02:50:37 +04:00
|
|
|
MOZ_COUNT_CTOR(nsKeyFrameIndex);
|
|
|
|
}
|
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(nsKeyFrameIndex)
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
void Add(int64_t aOffset, int64_t aTimeMs) {
|
2010-08-20 02:50:37 +04:00
|
|
|
mKeyPoints.AppendElement(nsKeyPoint(aOffset, aTimeMs));
|
|
|
|
}
|
|
|
|
|
2016-07-27 10:01:27 +03:00
|
|
|
const nsKeyPoint& Get(uint32_t aIndex) const { return mKeyPoints[aIndex]; }
|
2010-08-20 02:50:37 +04:00
|
|
|
|
|
|
|
uint32_t Length() const { return mKeyPoints.Length(); }
|
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// Presentation time of the first sample in this stream in usecs.
|
2012-08-22 19:56:38 +04:00
|
|
|
const int64_t mStartTime;
|
2010-08-20 02:50:37 +04:00
|
|
|
|
2011-04-14 02:12:23 +04:00
|
|
|
// End time of the last sample in this stream in usecs.
|
2012-08-22 19:56:38 +04:00
|
|
|
const int64_t mEndTime;
|
2010-08-20 02:50:37 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<nsKeyPoint> mKeyPoints;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Maps Ogg serialnos to the index-keypoint list.
|
|
|
|
nsClassHashtable<nsUint32HashKey, nsKeyFrameIndex> mIndex;
|
2010-04-02 07:03:07 +04:00
|
|
|
};
|
|
|
|
|
2016-08-04 10:21:53 +03:00
|
|
|
class FlacState : public OggCodecState {
|
|
|
|
public:
|
2020-04-22 14:41:42 +03:00
|
|
|
explicit FlacState(rlbox_sandbox_ogg* aSandbox,
|
|
|
|
tainted_opaque_ogg<ogg_page*> aBosPage, uint32_t aSerial);
|
2016-08-04 10:21:53 +03:00
|
|
|
|
|
|
|
CodecType GetType() override { return TYPE_FLAC; }
|
2017-02-09 08:52:41 +03:00
|
|
|
bool DecodeHeader(OggPacketPtr aPacket) override;
|
2016-08-04 10:21:53 +03:00
|
|
|
int64_t Time(int64_t granulepos) override;
|
|
|
|
int64_t PacketDuration(ogg_packet* aPacket) override;
|
|
|
|
bool IsHeader(ogg_packet* aPacket) override;
|
2020-04-22 14:41:42 +03:00
|
|
|
nsresult PageIn(tainted_opaque_ogg<ogg_page*> aPage) override;
|
2016-08-04 10:21:53 +03:00
|
|
|
|
|
|
|
// Return a hash table with tag metadata.
|
2018-11-20 18:10:31 +03:00
|
|
|
UniquePtr<MetadataTags> GetTags() override;
|
2016-08-04 10:21:53 +03:00
|
|
|
|
2016-12-05 10:06:11 +03:00
|
|
|
const TrackInfo* GetInfo() const override;
|
2016-08-04 10:21:53 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool ReconstructFlacGranulepos(void);
|
|
|
|
|
|
|
|
FlacFrameParser mParser;
|
|
|
|
};
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
#endif
|