Bug 1168674: [ogg] P1. Add OggDemuxer object. r=me

MozReview-Commit-ID: ChEceup4MYh

--HG--
extra : rebase_source : 0dc2c481c3bb3554484aac4227e92261c2341c66
This commit is contained in:
Brion Vibber 2016-07-21 11:28:47 +10:00
parent cc62ca1699
commit 173a4b9f77
9 changed files with 2541 additions and 45 deletions

View File

@ -179,6 +179,11 @@ void OggPacketQueue::Append(ogg_packet* aPacket) {
nsDeque::Push(aPacket);
}
bool OggCodecState::IsPacketReady()
{
return !mPackets.IsEmpty();
}
ogg_packet* OggCodecState::PacketOut() {
if (mPackets.IsEmpty()) {
return nullptr;
@ -186,6 +191,39 @@ ogg_packet* OggCodecState::PacketOut() {
return mPackets.PopFront();
}
ogg_packet* OggCodecState::PacketPeek() {
if (mPackets.IsEmpty()) {
return nullptr;
}
return mPackets.PeekFront();
}
RefPtr<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);
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);
ReleasePacket(packet);
return sample;
}
nsresult OggCodecState::PageIn(ogg_page* aPage) {
if (!mActive)
return NS_OK;
@ -365,6 +403,17 @@ int64_t TheoraState::StartTime(int64_t granulepos) {
return t.value() / mInfo.fps_numerator;
}
int64_t TheoraState::PacketDuration(ogg_packet* aPacket) {
if (!mActive || mInfo.fps_numerator == 0) {
return -1;
}
CheckedInt64 t = CheckedInt64(mInfo.fps_denominator) * USECS_PER_S;
if (!t.isValid()) {
return -1;
}
return t.value() / mInfo.fps_numerator;
}
int64_t
TheoraState::MaxKeyframeOffset()
{
@ -385,6 +434,14 @@ TheoraState::MaxKeyframeOffset()
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)
{
@ -624,6 +681,24 @@ int64_t VorbisState::Time(vorbis_info* aInfo, int64_t aGranulepos)
return t.value() / aInfo->rate;
}
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)
{
@ -984,6 +1059,12 @@ static int GetOpusDeltaGP(ogg_packet* packet)
return nframes;
}
int64_t OpusState::PacketDuration(ogg_packet* aPacket)
{
CheckedInt64 t = CheckedInt64(GetOpusDeltaGP(aPacket)) * USECS_PER_S;
return t.isValid() ? t.value() / 48000 : -1;
}
bool OpusState::ReconstructOpusGranulepos(void)
{
NS_ASSERTION(mUnstamped.Length() > 0, "Must have unstamped packets");

View File

@ -113,6 +113,24 @@ public:
// Returns the start time that a granulepos represents.
virtual int64_t StartTime(int64_t granulepos) { return -1; }
// 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.
virtual int64_t PacketStartTime(ogg_packet* aPacket) {
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;
}
}
// Initializes the codec state.
virtual bool Init();
@ -139,17 +157,35 @@ public:
// decoding.
virtual bool IsHeader(ogg_packet* aPacket) { return false; }
// Returns the next packet in the stream, or nullptr if there are no more
// 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
// 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
// OggCodecState::ReleasePacket(). The packet will have a valid granulepos.
ogg_packet* PacketOut();
// 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();
// 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);
// 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.
virtual RefPtr<MediaRawData> PacketOutAsMediaRawData();
// 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
@ -218,6 +254,7 @@ public:
CodecType GetType() { return TYPE_VORBIS; }
bool DecodeHeader(ogg_packet* aPacket);
int64_t Time(int64_t granulepos);
int64_t PacketDuration(ogg_packet* aPacket);
bool Init();
nsresult Reset();
bool IsHeader(ogg_packet* aPacket);
@ -292,8 +329,10 @@ public:
bool DecodeHeader(ogg_packet* aPacket);
int64_t Time(int64_t granulepos);
int64_t StartTime(int64_t granulepos);
int64_t PacketDuration(ogg_packet* aPacket);
bool Init();
bool IsHeader(ogg_packet* aPacket);
bool IsKeyframe(ogg_packet* aPacket);
nsresult PageIn(ogg_page* aPage);
// Returns the maximum number of microseconds which a keyframe can be offset
@ -305,7 +344,7 @@ public:
th_info mInfo;
th_comment mComment;
th_setup_info *mSetup;
th_setup_info* mSetup;
th_dec_ctx* mCtx;
float mPixelAspectRatio;
@ -329,6 +368,7 @@ public:
CodecType GetType() { return TYPE_OPUS; }
bool DecodeHeader(ogg_packet* aPacket);
int64_t Time(int64_t aGranulepos);
int64_t PacketDuration(ogg_packet* aPacket);
bool Init();
nsresult Reset();
nsresult Reset(bool aStart);
@ -349,7 +389,7 @@ public:
#endif
nsAutoPtr<OpusParser> mParser;
OpusMSDecoder *mDecoder;
OpusMSDecoder* mDecoder;
int mSkip; // Number of samples left to trim before playback.
// Granule position (end sample) of the last decoded Opus packet. This is

View File

@ -0,0 +1,37 @@
/* -*- 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 "mozilla/DebugOnly.h"
#include "OggCodecStore.h"
namespace mozilla {
OggCodecStore::OggCodecStore()
: mMonitor("CodecStore")
{
}
void OggCodecStore::Add(uint32_t serial, OggCodecState* codecState)
{
MonitorAutoLock mon(mMonitor);
mCodecStates.Put(serial, codecState);
}
bool OggCodecStore::Contains(uint32_t serial)
{
MonitorAutoLock mon(mMonitor);
return mCodecStates.Get(serial, nullptr);
}
OggCodecState* OggCodecStore::Get(uint32_t serial)
{
MonitorAutoLock mon(mMonitor);
return mCodecStates.Get(serial);
}
} // namespace mozilla

View File

@ -0,0 +1,38 @@
/* -*- 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/. */
#if !defined(OggCodecStore_h_)
#define OggCodecStore_h_
#include <ogg/ogg.h>
#include "OggCodecState.h"
#include "VideoUtils.h"
#include "mozilla/Monitor.h"
namespace mozilla {
// Thread safe container to store the codec information and the serial for each
// streams.
class OggCodecStore
{
public:
OggCodecStore();
void Add(uint32_t serial, OggCodecState* codecState);
bool Contains(uint32_t serial);
OggCodecState* Get(uint32_t serial);
bool IsKnownStream(uint32_t aSerial);
private:
// Maps Ogg serialnos to OggStreams.
nsClassHashtable<nsUint32HashKey, OggCodecState> mCodecStates;
// Protects the |mCodecStates| and the |mKnownStreams| members.
Monitor mMonitor;
};
} // namespace mozilla
#endif

1994
dom/media/ogg/OggDemuxer.cpp Normal file

File diff suppressed because it is too large Load Diff

343
dom/media/ogg/OggDemuxer.h Normal file
View File

@ -0,0 +1,343 @@
/* -*- 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/. */
#if !defined(OggDemuxer_h_)
#define OggDemuxer_h_
#include "nsTArray.h"
#include "MediaDataDemuxer.h"
#include "OggCodecState.h"
#include "OggCodecStore.h"
namespace mozilla {
class OggTrackDemuxer;
class OggHeaders;
class OggDemuxer : public MediaDataDemuxer
{
public:
explicit OggDemuxer(MediaResource* aResource);
RefPtr<InitPromise> Init() override;
bool HasTrackType(TrackInfo::TrackType aType) const override;
uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override;
already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(TrackInfo::TrackType aType,
uint32_t aTrackNumber) override;
bool IsSeekable() const override;
UniquePtr<EncryptionInfo> GetCrypto() override;
private:
// helpers for friend OggTrackDemuxer
UniquePtr<TrackInfo> GetTrackInfo(TrackInfo::TrackType aType, size_t aTrackNumber) const;
struct nsAutoOggSyncState {
nsAutoOggSyncState() {
ogg_sync_init(&mState);
}
~nsAutoOggSyncState() {
ogg_sync_clear(&mState);
}
ogg_sync_state mState;
};
media::TimeIntervals GetBuffered();
void FindStartTime(int64_t& aOutStartTime);
nsresult SeekInternal(const media::TimeUnit& aTarget);
// Seeks to the keyframe preceding the target time using available
// keyframe indexes.
enum IndexedSeekResult {
SEEK_OK, // Success.
SEEK_INDEX_FAIL, // Failure due to no index, or invalid index.
SEEK_FATAL_ERROR // Error returned by a stream operation.
};
IndexedSeekResult SeekToKeyframeUsingIndex(int64_t aTarget);
// Rolls back a seek-using-index attempt, returning a failure error code.
IndexedSeekResult RollbackIndexedSeek(int64_t aOffset);
// Represents a section of contiguous media, with a start and end offset,
// and the timestamps of the start and end of that range, that is cached.
// Used to denote the extremities of a range in which we can seek quickly
// (because it's cached).
class SeekRange {
public:
SeekRange()
: mOffsetStart(0),
mOffsetEnd(0),
mTimeStart(0),
mTimeEnd(0)
{}
SeekRange(int64_t aOffsetStart,
int64_t aOffsetEnd,
int64_t aTimeStart,
int64_t aTimeEnd)
: mOffsetStart(aOffsetStart),
mOffsetEnd(aOffsetEnd),
mTimeStart(aTimeStart),
mTimeEnd(aTimeEnd)
{}
bool IsNull() const {
return mOffsetStart == 0 &&
mOffsetEnd == 0 &&
mTimeStart == 0 &&
mTimeEnd == 0;
}
int64_t mOffsetStart, mOffsetEnd; // in bytes.
int64_t mTimeStart, mTimeEnd; // in usecs.
};
nsresult GetSeekRanges(nsTArray<SeekRange>& aRanges);
SeekRange SelectSeekRange(const nsTArray<SeekRange>& ranges,
int64_t aTarget,
int64_t aStartTime,
int64_t aEndTime,
bool aExact);
// Seeks to aTarget usecs in the buffered range aRange using bisection search,
// or to the keyframe prior to aTarget if we have video. aAdjustedTarget is
// an adjusted version of the target used to account for Opus pre-roll, if
// necessary. aStartTime must be the presentation time at the start of media,
// and aEndTime the time at end of media. aRanges must be the time/byte ranges
// buffered in the media cache as per GetSeekRanges().
nsresult SeekInBufferedRange(int64_t aTarget,
int64_t aAdjustedTarget,
int64_t aStartTime,
int64_t aEndTime,
const nsTArray<SeekRange>& aRanges,
const SeekRange& aRange);
// Seeks to before aTarget usecs in media using bisection search. If the media
// has video, this will seek to before the keyframe required to render the
// media at aTarget. Will use aRanges in order to narrow the bisection
// search space. aStartTime must be the presentation time at the start of
// media, and aEndTime the time at end of media. aRanges must be the time/byte
// ranges buffered in the media cache as per GetSeekRanges().
nsresult SeekInUnbuffered(int64_t aTarget,
int64_t aStartTime,
int64_t aEndTime,
const nsTArray<SeekRange>& aRanges);
// Performs a seek bisection to move the media stream's read cursor to the
// last ogg page boundary which has end time before aTarget usecs on both the
// Theora and Vorbis bitstreams. Limits its search to data inside aRange;
// i.e. it will only read inside of the aRange's start and end offsets.
// aFuzz is the number of usecs of leniency we'll allow; we'll terminate the
// seek when we land in the range (aTime - aFuzz, aTime) usecs.
nsresult SeekBisection(int64_t aTarget,
const SeekRange& aRange,
uint32_t aFuzz);
// Chunk size to read when reading Ogg files. Average Ogg page length
// is about 4300 bytes, so we read the file in chunks larger than that.
static const int PAGE_STEP = 8192;
enum PageSyncResult {
PAGE_SYNC_ERROR = 1,
PAGE_SYNC_END_OF_RANGE= 2,
PAGE_SYNC_OK = 3
};
static PageSyncResult PageSync(MediaResourceIndex* aResource,
ogg_sync_state* aState,
bool aCachedDataOnly,
int64_t aOffset,
int64_t aEndOffset,
ogg_page* aPage,
int& aSkippedBytes);
// Demux next Ogg packet
RefPtr<MediaRawData> GetNextPacket(TrackInfo::TrackType aType);
nsresult ResetTrackState(TrackInfo::TrackType aType);
nsresult Reset();
static const nsString GetKind(const nsCString& aRole);
static void InitTrack(MessageField* aMsgInfo,
TrackInfo* aInfo,
bool aEnable);
// Really private!
~OggDemuxer();
void Cleanup();
// Read enough of the file to identify track information and header
// packets necessary for decoding to begin.
nsresult ReadMetadata();
// Read a page of data from the Ogg file. Returns true if a page has been
// read, false if the page read failed or end of file reached.
bool ReadOggPage(ogg_page* aPage);
// Send a page off to the individual streams it belongs to.
// Reconstructed packets, if any are ready, will be available
// on the individual OggCodecStates.
nsresult DemuxOggPage(ogg_page* aPage);
// Read data and demux until a packet is available on the given stream state
void DemuxUntilPacketAvailable(OggCodecState* aState);
// Reads and decodes header packets for aState, until either header decode
// fails, or is complete. Initializes the codec state before returning.
// Returns true if reading headers and initializtion of the stream
// succeeds.
bool ReadHeaders(OggCodecState* aState, OggHeaders& aHeaders);
// Reads the next link in the chain.
bool ReadOggChain();
// Set this media as being a chain and notifies the state machine that the
// media is no longer seekable.
void SetChained();
// Fills aTracks with the serial numbers of each active stream, for use by
// various SkeletonState functions.
void BuildSerialList(nsTArray<uint32_t>& aTracks);
// Setup target bitstreams for decoding.
void SetupTargetTheora(TheoraState* aTheoraState, OggHeaders& aHeaders);
void SetupTargetVorbis(VorbisState* aVorbisState, OggHeaders& aHeaders);
void SetupTargetOpus(OpusState* aOpusState, OggHeaders& aHeaders);
void SetupTargetSkeleton();
void SetupMediaTracksInfo(const nsTArray<uint32_t>& aSerials);
// Compute an ogg page's checksum
ogg_uint32_t GetPageChecksum(ogg_page* aPage);
// Get the end time of aEndOffset. This is the playback position we'd reach
// after playback finished at aEndOffset.
int64_t RangeEndTime(int64_t aEndOffset);
// Get the end time of aEndOffset, without reading before aStartOffset.
// This is the playback position we'd reach after playback finished at
// aEndOffset. If bool aCachedDataOnly is true, then we'll only read
// from data which is cached in the media cached, otherwise we'll do
// regular blocking reads from the media stream. If bool aCachedDataOnly
// is true, this can safely be called on the main thread, otherwise it
// must be called on the state machine thread.
int64_t RangeEndTime(int64_t aStartOffset,
int64_t aEndOffset,
bool aCachedDataOnly);
// Get the start time of the range beginning at aOffset. This is the start
// time of the first frame and or audio sample we'd be able to play if we
// started playback at aOffset.
int64_t RangeStartTime(int64_t aOffset);
MediaInfo mInfo;
nsTArray<RefPtr<OggTrackDemuxer>> mDemuxers;
// Map of codec-specific bitstream states.
OggCodecStore mCodecStore;
// Decode state of the Theora bitstream we're decoding, if we have video.
TheoraState* mTheoraState;
// Decode state of the Vorbis bitstream we're decoding, if we have audio.
VorbisState* mVorbisState;
// Decode state of the Opus bitstream we're decoding, if we have one.
OpusState* mOpusState;
// Get the bitstream decode state for the given track type
OggCodecState* GetTrackCodecState(TrackInfo::TrackType aType) const;
// Represents the user pref media.opus.enabled at the time our
// contructor was called. We can't check it dynamically because
// we're not on the main thread;
bool mOpusEnabled;
// Decode state of the Skeleton bitstream.
SkeletonState* mSkeletonState;
// Ogg decoding state.
ogg_sync_state mOggState;
// Vorbis/Opus/Theora data used to compute timestamps. This is written on the
// decoder thread and read on the main thread. All reading on the main
// thread must be done after metadataloaded. We can't use the existing
// data in the codec states due to threading issues. You must check the
// associated mTheoraState or mVorbisState pointer is non-null before
// using this codec data.
uint32_t mVorbisSerial;
uint32_t mOpusSerial;
uint32_t mTheoraSerial;
vorbis_info mVorbisInfo;
int mOpusPreSkip;
th_info mTheoraInfo;
Maybe<int64_t> mStartTime;
// Booleans to indicate if we have audio and/or video data
bool HasVideo() const;
bool HasAudio() const;
bool HasSkeleton() const {
return mSkeletonState != 0 && mSkeletonState->mActive;
}
bool HaveStartTime () const;
int64_t StartTime() const;
// The picture region inside Theora frame to be displayed, if we have
// a Theora video track.
nsIntRect mPicture;
// True if we are decoding a chained ogg.
bool mIsChained;
// Number of audio frames decoded so far.
int64_t mDecodedAudioFrames;
MediaResourceIndex mResource;
friend class OggTrackDemuxer;
};
class OggTrackDemuxer : public MediaTrackDemuxer
{
public:
OggTrackDemuxer(OggDemuxer* aParent,
TrackInfo::TrackType aType,
uint32_t aTrackNumber);
UniquePtr<TrackInfo> GetInfo() const override;
RefPtr<SeekPromise> Seek(media::TimeUnit aTime) override;
RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) override;
void Reset() override;
RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint(media::TimeUnit aTimeThreshold) override;
media::TimeIntervals GetBuffered() override;
void BreakCycles() override;
private:
~OggTrackDemuxer();
void SetNextKeyFrameTime();
RefPtr<MediaRawData> NextSample();
RefPtr<OggDemuxer> mParent;
TrackInfo::TrackType mType;
UniquePtr<TrackInfo> mInfo;
// Queued sample extracted by the demuxer, but not yet returned.
RefPtr<MediaRawData> mQueuedSample;
};
} // namespace mozilla
#endif

View File

@ -2023,27 +2023,4 @@ RefPtr<VideoData> OggReader::SyncDecodeToFirstVideoData()
return VideoQueue().PeekFront();
}
OggCodecStore::OggCodecStore()
: mMonitor("CodecStore")
{
}
void OggCodecStore::Add(uint32_t serial, OggCodecState* codecState)
{
MonitorAutoLock mon(mMonitor);
mCodecStates.Put(serial, codecState);
}
bool OggCodecStore::Contains(uint32_t serial)
{
MonitorAutoLock mon(mMonitor);
return mCodecStates.Get(serial, nullptr);
}
OggCodecState* OggCodecStore::Get(uint32_t serial)
{
MonitorAutoLock mon(mMonitor);
return mCodecStates.Get(serial);
}
} // namespace mozilla

View File

@ -19,28 +19,10 @@
#include "VideoUtils.h"
#include "mozilla/Monitor.h"
#include "OggDecoder.h"
#include "OggCodecStore.h"
namespace mozilla {
// Thread safe container to store the codec information and the serial for each
// streams.
class OggCodecStore
{
public:
OggCodecStore();
void Add(uint32_t serial, OggCodecState* codecState);
bool Contains(uint32_t serial);
OggCodecState* Get(uint32_t serial);
bool IsKnownStream(uint32_t aSerial);
private:
// Maps Ogg serialnos to OggStreams.
nsClassHashtable<nsUint32HashKey, OggCodecState> mCodecStates;
// Protects the |mCodecStates| and the |mKnownStreams| members.
Monitor mMonitor;
};
class OggReader final : public MediaDecoderReader
{
public:

View File

@ -6,7 +6,9 @@
EXPORTS += [
'OggCodecState.h',
'OggCodecStore.h',
'OggDecoder.h',
'OggDemuxer.h',
'OggReader.h',
'OggWriter.h',
'OpusParser.h',
@ -14,7 +16,9 @@ EXPORTS += [
UNIFIED_SOURCES += [
'OggCodecState.cpp',
'OggCodecStore.cpp',
'OggDecoder.cpp',
'OggDemuxer.cpp',
'OggReader.cpp',
'OggWriter.cpp',
'OpusParser.cpp',