michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/Endian.h" michael@0: #include michael@0: michael@0: #include "nsDebug.h" michael@0: #include "MediaDecoderReader.h" michael@0: #include "OggCodecState.h" michael@0: #include "OggDecoder.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "VideoUtils.h" michael@0: #include michael@0: michael@0: // On Android JellyBean, the hardware.h header redefines version_major and michael@0: // version_minor, which breaks our build. See: michael@0: // https://bugzilla.mozilla.org/show_bug.cgi?id=912702#c6 michael@0: #ifdef MOZ_WIDGET_GONK michael@0: #ifdef version_major michael@0: #undef version_major michael@0: #endif michael@0: #ifdef version_minor michael@0: #undef version_minor michael@0: #endif michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: michael@0: #ifdef PR_LOGGING michael@0: extern PRLogModuleInfo* gMediaDecoderLog; michael@0: #define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg) michael@0: #else michael@0: #define LOG(type, msg) michael@0: #endif michael@0: michael@0: /** Decoder base class for Ogg-encapsulated streams. */ michael@0: OggCodecState* michael@0: OggCodecState::Create(ogg_page* aPage) michael@0: { michael@0: NS_ASSERTION(ogg_page_bos(aPage), "Only call on BOS page!"); michael@0: nsAutoPtr codecState; michael@0: if (aPage->body_len > 6 && memcmp(aPage->body+1, "theora", 6) == 0) { michael@0: codecState = new TheoraState(aPage); michael@0: } else if (aPage->body_len > 6 && memcmp(aPage->body+1, "vorbis", 6) == 0) { michael@0: codecState = new VorbisState(aPage); michael@0: #ifdef MOZ_OPUS michael@0: } else if (aPage->body_len > 8 && memcmp(aPage->body, "OpusHead", 8) == 0) { michael@0: codecState = new OpusState(aPage); michael@0: #endif michael@0: } else if (aPage->body_len > 8 && memcmp(aPage->body, "fishead\0", 8) == 0) { michael@0: codecState = new SkeletonState(aPage); michael@0: } else { michael@0: codecState = new OggCodecState(aPage, false); michael@0: } michael@0: return codecState->OggCodecState::Init() ? codecState.forget() : nullptr; michael@0: } michael@0: michael@0: OggCodecState::OggCodecState(ogg_page* aBosPage, bool aActive) : michael@0: mPacketCount(0), michael@0: mSerial(ogg_page_serialno(aBosPage)), michael@0: mActive(aActive), michael@0: mDoneReadingHeaders(!aActive) michael@0: { michael@0: MOZ_COUNT_CTOR(OggCodecState); michael@0: memset(&mState, 0, sizeof(ogg_stream_state)); michael@0: } michael@0: michael@0: OggCodecState::~OggCodecState() { michael@0: MOZ_COUNT_DTOR(OggCodecState); michael@0: Reset(); michael@0: #ifdef DEBUG michael@0: int ret = michael@0: #endif michael@0: ogg_stream_clear(&mState); michael@0: NS_ASSERTION(ret == 0, "ogg_stream_clear failed"); michael@0: } michael@0: michael@0: nsresult OggCodecState::Reset() { michael@0: if (ogg_stream_reset(&mState) != 0) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: mPackets.Erase(); michael@0: ClearUnstamped(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void OggCodecState::ClearUnstamped() michael@0: { michael@0: for (uint32_t i = 0; i < mUnstamped.Length(); ++i) { michael@0: OggCodecState::ReleasePacket(mUnstamped[i]); michael@0: } michael@0: mUnstamped.Clear(); michael@0: } michael@0: michael@0: bool OggCodecState::Init() { michael@0: int ret = ogg_stream_init(&mState, mSerial); michael@0: return ret == 0; michael@0: } michael@0: michael@0: bool OggCodecState::IsValidVorbisTagName(nsCString& aName) michael@0: { michael@0: // Tag names must consist of ASCII 0x20 through 0x7D, michael@0: // excluding 0x3D '=' which is the separator. michael@0: uint32_t length = aName.Length(); michael@0: const char* data = aName.Data(); michael@0: for (uint32_t i = 0; i < length; i++) { michael@0: if (data[i] < 0x20 || data[i] > 0x7D || data[i] == '=') { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool OggCodecState::AddVorbisComment(MetadataTags* aTags, michael@0: const char* aComment, michael@0: uint32_t aLength) michael@0: { michael@0: const char* div = (const char*)memchr(aComment, '=', aLength); michael@0: if (!div) { michael@0: LOG(PR_LOG_DEBUG, ("Skipping comment: no separator")); michael@0: return false; michael@0: } michael@0: nsCString key = nsCString(aComment, div-aComment); michael@0: if (!IsValidVorbisTagName(key)) { michael@0: LOG(PR_LOG_DEBUG, ("Skipping comment: invalid tag name")); michael@0: return false; michael@0: } michael@0: uint32_t valueLength = aLength - (div-aComment); michael@0: nsCString value = nsCString(div + 1, valueLength); michael@0: if (!IsUTF8(value)) { michael@0: LOG(PR_LOG_DEBUG, ("Skipping comment: invalid UTF-8 in value")); michael@0: return false; michael@0: } michael@0: aTags->Put(key, value); michael@0: return true; michael@0: } michael@0: michael@0: void VorbisState::RecordVorbisPacketSamples(ogg_packet* aPacket, michael@0: long aSamples) michael@0: { michael@0: #ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION michael@0: mVorbisPacketSamples[aPacket] = aSamples; michael@0: #endif michael@0: } michael@0: michael@0: void VorbisState::ValidateVorbisPacketSamples(ogg_packet* aPacket, michael@0: long aSamples) michael@0: { michael@0: #ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION michael@0: NS_ASSERTION(mVorbisPacketSamples[aPacket] == aSamples, michael@0: "Decoded samples for Vorbis packet don't match expected!"); michael@0: mVorbisPacketSamples.erase(aPacket); michael@0: #endif michael@0: } michael@0: michael@0: void VorbisState::AssertHasRecordedPacketSamples(ogg_packet* aPacket) michael@0: { michael@0: #ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION michael@0: NS_ASSERTION(mVorbisPacketSamples.count(aPacket) == 1, michael@0: "Must have recorded packet samples"); michael@0: #endif michael@0: } michael@0: michael@0: static ogg_packet* Clone(ogg_packet* aPacket) { michael@0: ogg_packet* p = new ogg_packet(); michael@0: memcpy(p, aPacket, sizeof(ogg_packet)); michael@0: p->packet = new unsigned char[p->bytes]; michael@0: memcpy(p->packet, aPacket->packet, p->bytes); michael@0: return p; michael@0: } michael@0: michael@0: void OggCodecState::ReleasePacket(ogg_packet* aPacket) { michael@0: if (aPacket) michael@0: delete [] aPacket->packet; michael@0: delete aPacket; michael@0: } michael@0: michael@0: void OggPacketQueue::Append(ogg_packet* aPacket) { michael@0: nsDeque::Push(aPacket); michael@0: } michael@0: michael@0: ogg_packet* OggCodecState::PacketOut() { michael@0: if (mPackets.IsEmpty()) { michael@0: return nullptr; michael@0: } michael@0: return mPackets.PopFront(); michael@0: } michael@0: michael@0: nsresult OggCodecState::PageIn(ogg_page* aPage) { michael@0: if (!mActive) michael@0: return NS_OK; michael@0: NS_ASSERTION(static_cast(ogg_page_serialno(aPage)) == mSerial, michael@0: "Page must be for this stream!"); michael@0: if (ogg_stream_pagein(&mState, aPage) == -1) michael@0: return NS_ERROR_FAILURE; michael@0: int r; michael@0: do { michael@0: ogg_packet packet; michael@0: r = ogg_stream_packetout(&mState, &packet); michael@0: if (r == 1) { michael@0: mPackets.Append(Clone(&packet)); michael@0: } michael@0: } while (r != 0); michael@0: if (ogg_stream_check(&mState)) { michael@0: NS_WARNING("Unrecoverable error in ogg_stream_packetout"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult OggCodecState::PacketOutUntilGranulepos(bool& aFoundGranulepos) { michael@0: int r; michael@0: aFoundGranulepos = false; michael@0: // Extract packets from the sync state until either no more packets michael@0: // come out, or we get a data packet with non -1 granulepos. michael@0: do { michael@0: ogg_packet packet; michael@0: r = ogg_stream_packetout(&mState, &packet); michael@0: if (r == 1) { michael@0: ogg_packet* clone = Clone(&packet); michael@0: if (IsHeader(&packet)) { michael@0: // Header packets go straight into the packet queue. michael@0: mPackets.Append(clone); michael@0: } else { michael@0: // We buffer data packets until we encounter a granulepos. We'll michael@0: // then use the granulepos to figure out the granulepos of the michael@0: // preceeding packets. michael@0: mUnstamped.AppendElement(clone); michael@0: aFoundGranulepos = packet.granulepos > 0; michael@0: } michael@0: } michael@0: } while (r != 0 && !aFoundGranulepos); michael@0: if (ogg_stream_check(&mState)) { michael@0: NS_WARNING("Unrecoverable error in ogg_stream_packetout"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: TheoraState::TheoraState(ogg_page* aBosPage) : michael@0: OggCodecState(aBosPage, true), michael@0: mSetup(0), michael@0: mCtx(0), michael@0: mPixelAspectRatio(0) michael@0: { michael@0: MOZ_COUNT_CTOR(TheoraState); michael@0: th_info_init(&mInfo); michael@0: th_comment_init(&mComment); michael@0: } michael@0: michael@0: TheoraState::~TheoraState() { michael@0: MOZ_COUNT_DTOR(TheoraState); michael@0: th_setup_free(mSetup); michael@0: th_decode_free(mCtx); michael@0: th_comment_clear(&mComment); michael@0: th_info_clear(&mInfo); michael@0: } michael@0: michael@0: bool TheoraState::Init() { michael@0: if (!mActive) michael@0: return false; michael@0: michael@0: int64_t n = mInfo.aspect_numerator; michael@0: int64_t d = mInfo.aspect_denominator; michael@0: michael@0: mPixelAspectRatio = (n == 0 || d == 0) ? michael@0: 1.0f : static_cast(n) / static_cast(d); michael@0: michael@0: // Ensure the frame and picture regions aren't larger than our prescribed michael@0: // maximum, or zero sized. michael@0: nsIntSize frame(mInfo.frame_width, mInfo.frame_height); michael@0: nsIntRect picture(mInfo.pic_x, mInfo.pic_y, mInfo.pic_width, mInfo.pic_height); michael@0: if (!IsValidVideoRegion(frame, picture, frame)) { michael@0: return mActive = false; michael@0: } michael@0: michael@0: mCtx = th_decode_alloc(&mInfo, mSetup); michael@0: if (mCtx == nullptr) { michael@0: return mActive = false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: TheoraState::DecodeHeader(ogg_packet* aPacket) michael@0: { michael@0: nsAutoRef autoRelease(aPacket); michael@0: mPacketCount++; michael@0: int ret = th_decode_headerin(&mInfo, michael@0: &mComment, michael@0: &mSetup, michael@0: aPacket); michael@0: michael@0: // We must determine when we've read the last header packet. michael@0: // th_decode_headerin() does not tell us when it's read the last header, so michael@0: // we must keep track of the headers externally. michael@0: // michael@0: // There are 3 header packets, the Identification, Comment, and Setup michael@0: // headers, which must be in that order. If they're out of order, the file michael@0: // is invalid. If we've successfully read a header, and it's the setup michael@0: // header, then we're done reading headers. The first byte of each packet michael@0: // determines it's type as follows: michael@0: // 0x80 -> Identification header michael@0: // 0x81 -> Comment header michael@0: // 0x82 -> Setup header michael@0: // See http://www.theora.org/doc/Theora.pdf Chapter 6, "Bitstream Headers", michael@0: // for more details of the Ogg/Theora containment scheme. michael@0: bool isSetupHeader = aPacket->bytes > 0 && aPacket->packet[0] == 0x82; michael@0: if (ret < 0 || mPacketCount > 3) { michael@0: // We've received an error, or the first three packets weren't valid michael@0: // header packets. Assume bad input. michael@0: // Our caller will deactivate the bitstream. michael@0: return false; michael@0: } else if (ret > 0 && isSetupHeader && mPacketCount == 3) { michael@0: // Successfully read the three header packets. michael@0: mDoneReadingHeaders = true; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: int64_t michael@0: TheoraState::Time(int64_t granulepos) { michael@0: if (!mActive) { michael@0: return -1; michael@0: } michael@0: return TheoraState::Time(&mInfo, granulepos); michael@0: } michael@0: michael@0: bool michael@0: TheoraState::IsHeader(ogg_packet* aPacket) { michael@0: return th_packet_isheader(aPacket); michael@0: } michael@0: michael@0: # define TH_VERSION_CHECK(_info,_maj,_min,_sub) \ michael@0: (((_info)->version_major>(_maj)||(_info)->version_major==(_maj))&& \ michael@0: (((_info)->version_minor>(_min)||(_info)->version_minor==(_min))&& \ michael@0: (_info)->version_subminor>=(_sub))) michael@0: michael@0: int64_t TheoraState::Time(th_info* aInfo, int64_t aGranulepos) michael@0: { michael@0: if (aGranulepos < 0 || aInfo->fps_numerator == 0) { michael@0: return -1; michael@0: } michael@0: // Implementation of th_granule_frame inlined here to operate michael@0: // on the th_info structure instead of the theora_state. michael@0: int shift = aInfo->keyframe_granule_shift; michael@0: ogg_int64_t iframe = aGranulepos >> shift; michael@0: ogg_int64_t pframe = aGranulepos - (iframe << shift); michael@0: int64_t frameno = iframe + pframe - TH_VERSION_CHECK(aInfo, 3, 2, 1); michael@0: CheckedInt64 t = ((CheckedInt64(frameno) + 1) * USECS_PER_S) * aInfo->fps_denominator; michael@0: if (!t.isValid()) michael@0: return -1; michael@0: t /= aInfo->fps_numerator; michael@0: return t.isValid() ? t.value() : -1; michael@0: } michael@0: michael@0: int64_t TheoraState::StartTime(int64_t granulepos) { michael@0: if (granulepos < 0 || !mActive || mInfo.fps_numerator == 0) { michael@0: return -1; michael@0: } michael@0: CheckedInt64 t = (CheckedInt64(th_granule_frame(mCtx, granulepos)) * USECS_PER_S) * mInfo.fps_denominator; michael@0: if (!t.isValid()) michael@0: return -1; michael@0: return t.value() / mInfo.fps_numerator; michael@0: } michael@0: michael@0: int64_t michael@0: TheoraState::MaxKeyframeOffset() michael@0: { michael@0: // Determine the maximum time in microseconds by which a key frame could michael@0: // offset for the theora bitstream. Theora granulepos encode time as: michael@0: // ((key_frame_number << granule_shift) + frame_offset). michael@0: // Therefore the maximum possible time by which any frame could be offset michael@0: // from a keyframe is the duration of (1 << granule_shift) - 1) frames. michael@0: int64_t frameDuration; michael@0: michael@0: // Max number of frames keyframe could possibly be offset. michael@0: int64_t keyframeDiff = (1 << mInfo.keyframe_granule_shift) - 1; michael@0: michael@0: // Length of frame in usecs. michael@0: frameDuration = (mInfo.fps_denominator * USECS_PER_S) / mInfo.fps_numerator; michael@0: michael@0: // Total time in usecs keyframe can be offset from any given frame. michael@0: return frameDuration * keyframeDiff; michael@0: } michael@0: michael@0: nsresult michael@0: TheoraState::PageIn(ogg_page* aPage) michael@0: { michael@0: if (!mActive) michael@0: return NS_OK; michael@0: NS_ASSERTION(static_cast(ogg_page_serialno(aPage)) == mSerial, michael@0: "Page must be for this stream!"); michael@0: if (ogg_stream_pagein(&mState, aPage) == -1) michael@0: return NS_ERROR_FAILURE; michael@0: bool foundGp; michael@0: nsresult res = PacketOutUntilGranulepos(foundGp); michael@0: if (NS_FAILED(res)) michael@0: return res; michael@0: if (foundGp && mDoneReadingHeaders) { michael@0: // We've found a packet with a granulepos, and we've loaded our metadata michael@0: // and initialized our decoder. Determine granulepos of buffered packets. michael@0: ReconstructTheoraGranulepos(); michael@0: for (uint32_t i = 0; i < mUnstamped.Length(); ++i) { michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: #ifdef DEBUG michael@0: NS_ASSERTION(!IsHeader(packet), "Don't try to recover header packet gp"); michael@0: NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now"); michael@0: #endif michael@0: mPackets.Append(packet); michael@0: } michael@0: mUnstamped.Clear(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Returns 1 if the Theora info struct is decoding a media of Theora michael@0: // version (maj,min,sub) or later, otherwise returns 0. michael@0: int michael@0: TheoraVersion(th_info* info, michael@0: unsigned char maj, michael@0: unsigned char min, michael@0: unsigned char sub) michael@0: { michael@0: ogg_uint32_t ver = (maj << 16) + (min << 8) + sub; michael@0: ogg_uint32_t th_ver = (info->version_major << 16) + michael@0: (info->version_minor << 8) + michael@0: info->version_subminor; michael@0: return (th_ver >= ver) ? 1 : 0; michael@0: } michael@0: michael@0: void TheoraState::ReconstructTheoraGranulepos() michael@0: { michael@0: if (mUnstamped.Length() == 0) { michael@0: return; michael@0: } michael@0: ogg_int64_t lastGranulepos = mUnstamped[mUnstamped.Length() - 1]->granulepos; michael@0: NS_ASSERTION(lastGranulepos != -1, "Must know last granulepos"); michael@0: michael@0: // Reconstruct the granulepos (and thus timestamps) of the decoded michael@0: // frames. Granulepos are stored as ((keyframe<> shift; michael@0: michael@0: // The lastFrame, firstFrame, keyframe variables, as well as the frame michael@0: // variable in the loop below, store the frame number for Theora michael@0: // version >= 3.2.1 streams, and store the frame index for Theora michael@0: // version < 3.2.1 streams. michael@0: for (uint32_t i = 0; i < mUnstamped.Length() - 1; ++i) { michael@0: ogg_int64_t frame = firstFrame + i; michael@0: ogg_int64_t granulepos; michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: bool isKeyframe = th_packet_iskeyframe(packet) == 1; michael@0: michael@0: if (isKeyframe) { michael@0: granulepos = frame << shift; michael@0: keyframe = frame; michael@0: } else if (frame >= keyframe && michael@0: frame - keyframe < ((ogg_int64_t)1 << shift)) michael@0: { michael@0: // (frame - keyframe) won't overflow the "offset" segment of the michael@0: // granulepos, so it's safe to calculate the granulepos. michael@0: granulepos = (keyframe << shift) + (frame - keyframe); michael@0: } else { michael@0: // (frame - keyframeno) will overflow the "offset" segment of the michael@0: // granulepos, so we take "keyframe" to be the max possible offset michael@0: // frame instead. michael@0: ogg_int64_t k = std::max(frame - (((ogg_int64_t)1 << shift) - 1), version_3_2_1); michael@0: granulepos = (k << shift) + (frame - k); michael@0: } michael@0: // Theora 3.2.1+ granulepos store frame number [1..N], so granulepos michael@0: // should be > 0. michael@0: // Theora 3.2.0 granulepos store the frame index [0..(N-1)], so michael@0: // granulepos should be >= 0. michael@0: NS_ASSERTION(granulepos >= version_3_2_1, michael@0: "Invalid granulepos for Theora version"); michael@0: michael@0: // Check that the frame's granule number is one more than the michael@0: // previous frame's. michael@0: NS_ASSERTION(i == 0 || michael@0: th_granule_frame(mCtx, granulepos) == michael@0: th_granule_frame(mCtx, mUnstamped[i-1]->granulepos) + 1, michael@0: "Granulepos calculation is incorrect!"); michael@0: michael@0: packet->granulepos = granulepos; michael@0: } michael@0: michael@0: // Check that the second to last frame's granule number is one less than michael@0: // the last frame's (the known granule number). If not our granulepos michael@0: // recovery missed a beat. michael@0: NS_ASSERTION(mUnstamped.Length() < 2 || michael@0: th_granule_frame(mCtx, mUnstamped[mUnstamped.Length()-2]->granulepos) + 1 == michael@0: th_granule_frame(mCtx, lastGranulepos), michael@0: "Granulepos recovery should catch up with packet->granulepos!"); michael@0: } michael@0: michael@0: nsresult VorbisState::Reset() michael@0: { michael@0: nsresult res = NS_OK; michael@0: if (mActive && vorbis_synthesis_restart(&mDsp) != 0) { michael@0: res = NS_ERROR_FAILURE; michael@0: } michael@0: if (NS_FAILED(OggCodecState::Reset())) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mGranulepos = 0; michael@0: mPrevVorbisBlockSize = 0; michael@0: michael@0: return res; michael@0: } michael@0: michael@0: VorbisState::VorbisState(ogg_page* aBosPage) : michael@0: OggCodecState(aBosPage, true), michael@0: mPrevVorbisBlockSize(0), michael@0: mGranulepos(0) michael@0: { michael@0: MOZ_COUNT_CTOR(VorbisState); michael@0: vorbis_info_init(&mInfo); michael@0: vorbis_comment_init(&mComment); michael@0: memset(&mDsp, 0, sizeof(vorbis_dsp_state)); michael@0: memset(&mBlock, 0, sizeof(vorbis_block)); michael@0: } michael@0: michael@0: VorbisState::~VorbisState() { michael@0: MOZ_COUNT_DTOR(VorbisState); michael@0: Reset(); michael@0: vorbis_block_clear(&mBlock); michael@0: vorbis_dsp_clear(&mDsp); michael@0: vorbis_info_clear(&mInfo); michael@0: vorbis_comment_clear(&mComment); michael@0: } michael@0: michael@0: bool VorbisState::DecodeHeader(ogg_packet* aPacket) { michael@0: nsAutoRef autoRelease(aPacket); michael@0: mPacketCount++; michael@0: int ret = vorbis_synthesis_headerin(&mInfo, michael@0: &mComment, michael@0: aPacket); michael@0: // We must determine when we've read the last header packet. michael@0: // vorbis_synthesis_headerin() does not tell us when it's read the last michael@0: // header, so we must keep track of the headers externally. michael@0: // michael@0: // There are 3 header packets, the Identification, Comment, and Setup michael@0: // headers, which must be in that order. If they're out of order, the file michael@0: // is invalid. If we've successfully read a header, and it's the setup michael@0: // header, then we're done reading headers. The first byte of each packet michael@0: // determines it's type as follows: michael@0: // 0x1 -> Identification header michael@0: // 0x3 -> Comment header michael@0: // 0x5 -> Setup header michael@0: // For more details of the Vorbis/Ogg containment scheme, see the Vorbis I michael@0: // Specification, Chapter 4, Codec Setup and Packet Decode: michael@0: // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-580004 michael@0: michael@0: bool isSetupHeader = aPacket->bytes > 0 && aPacket->packet[0] == 0x5; michael@0: michael@0: if (ret < 0 || mPacketCount > 3) { michael@0: // We've received an error, or the first three packets weren't valid michael@0: // header packets. Assume bad input. Our caller will deactivate the michael@0: // bitstream. michael@0: return false; michael@0: } else if (ret == 0 && isSetupHeader && mPacketCount == 3) { michael@0: // Successfully read the three header packets. michael@0: // The bitstream remains active. michael@0: mDoneReadingHeaders = true; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool VorbisState::Init() michael@0: { michael@0: if (!mActive) michael@0: return false; michael@0: michael@0: int ret = vorbis_synthesis_init(&mDsp, &mInfo); michael@0: if (ret != 0) { michael@0: NS_WARNING("vorbis_synthesis_init() failed initializing vorbis bitstream"); michael@0: return mActive = false; michael@0: } michael@0: ret = vorbis_block_init(&mDsp, &mBlock); michael@0: if (ret != 0) { michael@0: NS_WARNING("vorbis_block_init() failed initializing vorbis bitstream"); michael@0: if (mActive) { michael@0: vorbis_dsp_clear(&mDsp); michael@0: } michael@0: return mActive = false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: int64_t VorbisState::Time(int64_t granulepos) michael@0: { michael@0: if (!mActive) { michael@0: return -1; michael@0: } michael@0: michael@0: return VorbisState::Time(&mInfo, granulepos); michael@0: } michael@0: michael@0: int64_t VorbisState::Time(vorbis_info* aInfo, int64_t aGranulepos) michael@0: { michael@0: if (aGranulepos == -1 || aInfo->rate == 0) { michael@0: return -1; michael@0: } michael@0: CheckedInt64 t = CheckedInt64(aGranulepos) * USECS_PER_S; michael@0: if (!t.isValid()) michael@0: t = 0; michael@0: return t.value() / aInfo->rate; michael@0: } michael@0: michael@0: bool michael@0: VorbisState::IsHeader(ogg_packet* aPacket) michael@0: { michael@0: // The first byte in each Vorbis header packet is either 0x01, 0x03, or 0x05, michael@0: // i.e. the first bit is odd. Audio data packets have their first bit as 0x0. michael@0: // Any packet with its first bit set cannot be a data packet, it's a michael@0: // (possibly invalid) header packet. michael@0: // See: http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-610004.2.1 michael@0: return aPacket->bytes > 0 ? (aPacket->packet[0] & 0x1) : false; michael@0: } michael@0: michael@0: MetadataTags* michael@0: VorbisState::GetTags() michael@0: { michael@0: MetadataTags* tags; michael@0: NS_ASSERTION(mComment.user_comments, "no vorbis comment strings!"); michael@0: NS_ASSERTION(mComment.comment_lengths, "no vorbis comment lengths!"); michael@0: tags = new MetadataTags; michael@0: for (int i = 0; i < mComment.comments; i++) { michael@0: AddVorbisComment(tags, mComment.user_comments[i], michael@0: mComment.comment_lengths[i]); michael@0: } michael@0: return tags; michael@0: } michael@0: michael@0: nsresult michael@0: VorbisState::PageIn(ogg_page* aPage) michael@0: { michael@0: if (!mActive) michael@0: return NS_OK; michael@0: NS_ASSERTION(static_cast(ogg_page_serialno(aPage)) == mSerial, michael@0: "Page must be for this stream!"); michael@0: if (ogg_stream_pagein(&mState, aPage) == -1) michael@0: return NS_ERROR_FAILURE; michael@0: bool foundGp; michael@0: nsresult res = PacketOutUntilGranulepos(foundGp); michael@0: if (NS_FAILED(res)) michael@0: return res; michael@0: if (foundGp && mDoneReadingHeaders) { michael@0: // We've found a packet with a granulepos, and we've loaded our metadata michael@0: // and initialized our decoder. Determine granulepos of buffered packets. michael@0: ReconstructVorbisGranulepos(); michael@0: for (uint32_t i = 0; i < mUnstamped.Length(); ++i) { michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: AssertHasRecordedPacketSamples(packet); michael@0: NS_ASSERTION(!IsHeader(packet), "Don't try to recover header packet gp"); michael@0: NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now"); michael@0: mPackets.Append(packet); michael@0: } michael@0: mUnstamped.Clear(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult VorbisState::ReconstructVorbisGranulepos() michael@0: { michael@0: // The number of samples in a Vorbis packet is: michael@0: // window_blocksize(previous_packet)/4+window_blocksize(current_packet)/4 michael@0: // See: http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-230001.3.2 michael@0: // So we maintain mPrevVorbisBlockSize, the block size of the last packet michael@0: // encountered. We also maintain mGranulepos, which is the granulepos of michael@0: // the last encountered packet. This enables us to give granulepos to michael@0: // packets when the last packet in mUnstamped doesn't have a granulepos michael@0: // (for example if the stream was truncated). michael@0: // michael@0: // We validate our prediction of the number of samples decoded when michael@0: // VALIDATE_VORBIS_SAMPLE_CALCULATION is defined by recording the predicted michael@0: // number of samples, and verifing we extract that many when decoding michael@0: // each packet. michael@0: michael@0: NS_ASSERTION(mUnstamped.Length() > 0, "Length must be > 0"); michael@0: ogg_packet* last = mUnstamped[mUnstamped.Length()-1]; michael@0: NS_ASSERTION(last->e_o_s || last->granulepos >= 0, michael@0: "Must know last granulepos!"); michael@0: if (mUnstamped.Length() == 1) { michael@0: ogg_packet* packet = mUnstamped[0]; michael@0: long blockSize = vorbis_packet_blocksize(&mInfo, packet); michael@0: if (blockSize < 0) { michael@0: // On failure vorbis_packet_blocksize returns < 0. If we've got michael@0: // a bad packet, we just assume that decode will have to skip this michael@0: // packet, i.e. assume 0 samples are decodable from this packet. michael@0: blockSize = 0; michael@0: mPrevVorbisBlockSize = 0; michael@0: } michael@0: long samples = mPrevVorbisBlockSize / 4 + blockSize / 4; michael@0: mPrevVorbisBlockSize = blockSize; michael@0: if (packet->granulepos == -1) { michael@0: packet->granulepos = mGranulepos + samples; michael@0: } michael@0: michael@0: // Account for a partial last frame michael@0: if (packet->e_o_s && packet->granulepos >= mGranulepos) { michael@0: samples = packet->granulepos - mGranulepos; michael@0: } michael@0: michael@0: mGranulepos = packet->granulepos; michael@0: RecordVorbisPacketSamples(packet, samples); michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool unknownGranulepos = last->granulepos == -1; michael@0: int totalSamples = 0; michael@0: for (int32_t i = mUnstamped.Length() - 1; i > 0; i--) { michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: ogg_packet* prev = mUnstamped[i-1]; michael@0: ogg_int64_t granulepos = packet->granulepos; michael@0: NS_ASSERTION(granulepos != -1, "Must know granulepos!"); michael@0: long prevBlockSize = vorbis_packet_blocksize(&mInfo, prev); michael@0: long blockSize = vorbis_packet_blocksize(&mInfo, packet); michael@0: michael@0: if (blockSize < 0 || prevBlockSize < 0) { michael@0: // On failure vorbis_packet_blocksize returns < 0. If we've got michael@0: // a bad packet, we just assume that decode will have to skip this michael@0: // packet, i.e. assume 0 samples are decodable from this packet. michael@0: blockSize = 0; michael@0: prevBlockSize = 0; michael@0: } michael@0: michael@0: long samples = prevBlockSize / 4 + blockSize / 4; michael@0: totalSamples += samples; michael@0: prev->granulepos = granulepos - samples; michael@0: RecordVorbisPacketSamples(packet, samples); michael@0: } michael@0: michael@0: if (unknownGranulepos) { michael@0: for (uint32_t i = 0; i < mUnstamped.Length(); i++) { michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: packet->granulepos += mGranulepos + totalSamples + 1; michael@0: } michael@0: } michael@0: michael@0: ogg_packet* first = mUnstamped[0]; michael@0: long blockSize = vorbis_packet_blocksize(&mInfo, first); michael@0: if (blockSize < 0) { michael@0: mPrevVorbisBlockSize = 0; michael@0: blockSize = 0; michael@0: } michael@0: michael@0: long samples = (mPrevVorbisBlockSize == 0) ? 0 : michael@0: mPrevVorbisBlockSize / 4 + blockSize / 4; michael@0: int64_t start = first->granulepos - samples; michael@0: RecordVorbisPacketSamples(first, samples); michael@0: michael@0: if (last->e_o_s && start < mGranulepos) { michael@0: // We've calculated that there are more samples in this page than its michael@0: // granulepos claims, and it's the last page in the stream. This is legal, michael@0: // and we will need to prune the trailing samples when we come to decode it. michael@0: // We must correct the timestamps so that they follow the last Vorbis page's michael@0: // samples. michael@0: int64_t pruned = mGranulepos - start; michael@0: for (uint32_t i = 0; i < mUnstamped.Length() - 1; i++) { michael@0: mUnstamped[i]->granulepos += pruned; michael@0: } michael@0: #ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION michael@0: mVorbisPacketSamples[last] -= pruned; michael@0: #endif michael@0: } michael@0: michael@0: mPrevVorbisBlockSize = vorbis_packet_blocksize(&mInfo, last); michael@0: mPrevVorbisBlockSize = std::max(static_cast(0), mPrevVorbisBlockSize); michael@0: mGranulepos = last->granulepos; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: #ifdef MOZ_OPUS michael@0: OpusState::OpusState(ogg_page* aBosPage) : michael@0: OggCodecState(aBosPage, true), michael@0: mParser(nullptr), michael@0: mDecoder(nullptr), michael@0: mSkip(0), michael@0: mPrevPacketGranulepos(0), michael@0: mPrevPageGranulepos(0) michael@0: { michael@0: MOZ_COUNT_CTOR(OpusState); michael@0: } michael@0: michael@0: OpusState::~OpusState() { michael@0: MOZ_COUNT_DTOR(OpusState); michael@0: Reset(); michael@0: michael@0: if (mDecoder) { michael@0: opus_multistream_decoder_destroy(mDecoder); michael@0: mDecoder = nullptr; michael@0: } michael@0: } michael@0: michael@0: nsresult OpusState::Reset() michael@0: { michael@0: return Reset(false); michael@0: } michael@0: michael@0: nsresult OpusState::Reset(bool aStart) michael@0: { michael@0: nsresult res = NS_OK; michael@0: michael@0: if (mActive && mDecoder) { michael@0: // Reset the decoder. michael@0: opus_multistream_decoder_ctl(mDecoder, OPUS_RESET_STATE); michael@0: // Let the seek logic handle pre-roll if we're not seeking to the start. michael@0: mSkip = aStart ? mParser->mPreSkip : 0; michael@0: // This lets us distinguish the first page being the last page vs. just michael@0: // not having processed the previous page when we encounter the last page. michael@0: mPrevPageGranulepos = aStart ? 0 : -1; michael@0: mPrevPacketGranulepos = aStart ? 0 : -1; michael@0: } michael@0: michael@0: // Clear queued data. michael@0: if (NS_FAILED(OggCodecState::Reset())) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: LOG(PR_LOG_DEBUG, ("Opus decoder reset, to skip %d", mSkip)); michael@0: michael@0: return res; michael@0: } michael@0: michael@0: bool OpusState::Init(void) michael@0: { michael@0: if (!mActive) michael@0: return false; michael@0: michael@0: int error; michael@0: michael@0: NS_ASSERTION(mDecoder == nullptr, "leaking OpusDecoder"); michael@0: michael@0: mDecoder = opus_multistream_decoder_create(mParser->mRate, michael@0: mParser->mChannels, michael@0: mParser->mStreams, michael@0: mParser->mCoupledStreams, michael@0: mParser->mMappingTable, michael@0: &error); michael@0: michael@0: mSkip = mParser->mPreSkip; michael@0: michael@0: LOG(PR_LOG_DEBUG, ("Opus decoder init, to skip %d", mSkip)); michael@0: michael@0: return error == OPUS_OK; michael@0: } michael@0: michael@0: bool OpusState::DecodeHeader(ogg_packet* aPacket) michael@0: { michael@0: nsAutoRef autoRelease(aPacket); michael@0: switch(mPacketCount++) { michael@0: // Parse the id header. michael@0: case 0: { michael@0: mParser = new OpusParser; michael@0: if(!mParser->DecodeHeader(aPacket->packet, aPacket->bytes)) { michael@0: return false; michael@0: } michael@0: mRate = mParser->mRate; michael@0: mChannels = mParser->mChannels; michael@0: mPreSkip = mParser->mPreSkip; michael@0: #ifdef MOZ_SAMPLE_TYPE_FLOAT32 michael@0: mGain = mParser->mGain; michael@0: #else michael@0: mGain_Q16 = mParser->mGain_Q16; michael@0: #endif michael@0: } michael@0: break; michael@0: michael@0: // Parse the metadata header. michael@0: case 1: { michael@0: if(!mParser->DecodeTags(aPacket->packet, aPacket->bytes)) { michael@0: return false; michael@0: } michael@0: } michael@0: break; michael@0: michael@0: // We made it to the first data packet (which includes reconstructing michael@0: // timestamps for it in PageIn). Success! michael@0: default: { michael@0: mDoneReadingHeaders = true; michael@0: // Put it back on the queue so we can decode it. michael@0: mPackets.PushFront(autoRelease.disown()); michael@0: } michael@0: break; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /* Construct and return a tags hashmap from our internal array */ michael@0: MetadataTags* OpusState::GetTags() michael@0: { michael@0: MetadataTags* tags; michael@0: michael@0: tags = new MetadataTags; michael@0: for (uint32_t i = 0; i < mParser->mTags.Length(); i++) { michael@0: AddVorbisComment(tags, mParser->mTags[i].Data(), mParser->mTags[i].Length()); michael@0: } michael@0: michael@0: return tags; michael@0: } michael@0: michael@0: /* Return the timestamp (in microseconds) equivalent to a granulepos. */ michael@0: int64_t OpusState::Time(int64_t aGranulepos) michael@0: { michael@0: if (!mActive) michael@0: return -1; michael@0: michael@0: return Time(mParser->mPreSkip, aGranulepos); michael@0: } michael@0: michael@0: int64_t OpusState::Time(int aPreSkip, int64_t aGranulepos) michael@0: { michael@0: if (aGranulepos < 0) michael@0: return -1; michael@0: michael@0: // Ogg Opus always runs at a granule rate of 48 kHz. michael@0: CheckedInt64 t = CheckedInt64(aGranulepos - aPreSkip) * USECS_PER_S; michael@0: return t.isValid() ? t.value() / 48000 : -1; michael@0: } michael@0: michael@0: bool OpusState::IsHeader(ogg_packet* aPacket) michael@0: { michael@0: return aPacket->bytes >= 16 && michael@0: (!memcmp(aPacket->packet, "OpusHead", 8) || michael@0: !memcmp(aPacket->packet, "OpusTags", 8)); michael@0: } michael@0: michael@0: nsresult OpusState::PageIn(ogg_page* aPage) michael@0: { michael@0: if (!mActive) michael@0: return NS_OK; michael@0: NS_ASSERTION(static_cast(ogg_page_serialno(aPage)) == mSerial, michael@0: "Page must be for this stream!"); michael@0: if (ogg_stream_pagein(&mState, aPage) == -1) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: bool haveGranulepos; michael@0: nsresult rv = PacketOutUntilGranulepos(haveGranulepos); michael@0: if (NS_FAILED(rv) || !haveGranulepos || mPacketCount < 2) michael@0: return rv; michael@0: if(!ReconstructOpusGranulepos()) michael@0: return NS_ERROR_FAILURE; michael@0: for (uint32_t i = 0; i < mUnstamped.Length(); i++) { michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: NS_ASSERTION(!IsHeader(packet), "Don't try to play a header packet"); michael@0: NS_ASSERTION(packet->granulepos != -1, "Packet should have a granulepos"); michael@0: mPackets.Append(packet); michael@0: } michael@0: mUnstamped.Clear(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Helper method to return the change in granule position due to an Opus packet michael@0: // (as distinct from the number of samples in the packet, which depends on the michael@0: // decoder rate). It should work with a multistream Opus file, and continue to michael@0: // work should we ever allow the decoder to decode at a rate other than 48 kHz. michael@0: // It even works before we've created the actual Opus decoder. michael@0: static int GetOpusDeltaGP(ogg_packet* packet) michael@0: { michael@0: int nframes; michael@0: nframes = opus_packet_get_nb_frames(packet->packet, packet->bytes); michael@0: if (nframes > 0) { michael@0: return nframes*opus_packet_get_samples_per_frame(packet->packet, 48000); michael@0: } michael@0: NS_WARNING("Invalid Opus packet."); michael@0: return nframes; michael@0: } michael@0: michael@0: bool OpusState::ReconstructOpusGranulepos(void) michael@0: { michael@0: NS_ASSERTION(mUnstamped.Length() > 0, "Must have unstamped packets"); michael@0: ogg_packet* last = mUnstamped[mUnstamped.Length()-1]; michael@0: NS_ASSERTION(last->e_o_s || last->granulepos > 0, michael@0: "Must know last granulepos!"); michael@0: int64_t gp; michael@0: // If this is the last page, and we've seen at least one previous page (or michael@0: // this is the first page)... michael@0: if (last->e_o_s) { michael@0: if (mPrevPageGranulepos != -1) { michael@0: // If this file only has one page and the final granule position is michael@0: // smaller than the pre-skip amount, we MUST reject the stream. michael@0: if (!mDoneReadingHeaders && last->granulepos < mPreSkip) michael@0: return false; michael@0: int64_t last_gp = last->granulepos; michael@0: gp = mPrevPageGranulepos; michael@0: // Loop through the packets forwards, adding the current packet's michael@0: // duration to the previous granulepos to get the value for the michael@0: // current packet. michael@0: for (uint32_t i = 0; i < mUnstamped.Length() - 1; ++i) { michael@0: ogg_packet* packet = mUnstamped[i]; michael@0: int offset = GetOpusDeltaGP(packet); michael@0: // Check for error (negative offset) and overflow. michael@0: if (offset >= 0 && gp <= INT64_MAX - offset) { michael@0: gp += offset; michael@0: if (gp >= last_gp) { michael@0: NS_WARNING("Opus end trimming removed more than a full packet."); michael@0: // We were asked to remove a full packet's worth of data or more. michael@0: // Encoders SHOULD NOT produce streams like this, but we'll handle michael@0: // it for them anyway. michael@0: gp = last_gp; michael@0: for (uint32_t j = i+1; j < mUnstamped.Length(); ++j) { michael@0: OggCodecState::ReleasePacket(mUnstamped[j]); michael@0: } michael@0: mUnstamped.RemoveElementsAt(i+1, mUnstamped.Length() - (i+1)); michael@0: last = packet; michael@0: last->e_o_s = 1; michael@0: } michael@0: } michael@0: packet->granulepos = gp; michael@0: } michael@0: mPrevPageGranulepos = last_gp; michael@0: return true; michael@0: } else { michael@0: NS_WARNING("No previous granule position to use for Opus end trimming."); michael@0: // If we don't have a previous granule position, fall through. michael@0: // We simply won't trim any samples from the end. michael@0: // TODO: Are we guaranteed to have seen a previous page if there is one? michael@0: } michael@0: } michael@0: michael@0: gp = last->granulepos; michael@0: // Loop through the packets backwards, subtracting the next michael@0: // packet's duration from its granulepos to get the value michael@0: // for the current packet. michael@0: for (uint32_t i = mUnstamped.Length() - 1; i > 0; i--) { michael@0: int offset = GetOpusDeltaGP(mUnstamped[i]); michael@0: // Check for error (negative offset) and overflow. michael@0: if (offset >= 0) { michael@0: if (offset <= gp) { michael@0: gp -= offset; michael@0: } else { michael@0: // If the granule position of the first data page is smaller than the michael@0: // number of decodable audio samples on that page, then we MUST reject michael@0: // the stream. michael@0: if (!mDoneReadingHeaders) michael@0: return false; michael@0: // It's too late to reject the stream. michael@0: // If we get here, this almost certainly means the file has screwed-up michael@0: // timestamps somewhere after the first page. michael@0: NS_WARNING("Clamping negative Opus granulepos to zero."); michael@0: gp = 0; michael@0: } michael@0: } michael@0: mUnstamped[i - 1]->granulepos = gp; michael@0: } michael@0: michael@0: // Check to make sure the first granule position is at least as large as the michael@0: // total number of samples decodable from the first page with completed michael@0: // packets. This requires looking at the duration of the first packet, too. michael@0: // We MUST reject such streams. michael@0: if (!mDoneReadingHeaders && GetOpusDeltaGP(mUnstamped[0]) > gp) michael@0: return false; michael@0: mPrevPageGranulepos = last->granulepos; michael@0: return true; michael@0: } michael@0: #endif /* MOZ_OPUS */ michael@0: michael@0: SkeletonState::SkeletonState(ogg_page* aBosPage) : michael@0: OggCodecState(aBosPage, true), michael@0: mVersion(0), michael@0: mPresentationTime(0), michael@0: mLength(0) michael@0: { michael@0: MOZ_COUNT_CTOR(SkeletonState); michael@0: } michael@0: michael@0: SkeletonState::~SkeletonState() michael@0: { michael@0: MOZ_COUNT_DTOR(SkeletonState); michael@0: } michael@0: michael@0: // Support for Ogg Skeleton 4.0, as per specification at: michael@0: // http://wiki.xiph.org/Ogg_Skeleton_4 michael@0: michael@0: // Minimum length in bytes of a Skeleton header packet. michael@0: static const long SKELETON_MIN_HEADER_LEN = 28; michael@0: static const long SKELETON_4_0_MIN_HEADER_LEN = 80; michael@0: michael@0: // Minimum length in bytes of a Skeleton 4.0 index packet. michael@0: static const long SKELETON_4_0_MIN_INDEX_LEN = 42; michael@0: michael@0: // Minimum possible size of a compressed index keypoint. michael@0: static const size_t MIN_KEY_POINT_SIZE = 2; michael@0: michael@0: // Byte offset of the major and minor version numbers in the michael@0: // Ogg Skeleton 4.0 header packet. michael@0: static const size_t SKELETON_VERSION_MAJOR_OFFSET = 8; michael@0: static const size_t SKELETON_VERSION_MINOR_OFFSET = 10; michael@0: michael@0: // Byte-offsets of the presentation time numerator and denominator michael@0: static const size_t SKELETON_PRESENTATION_TIME_NUMERATOR_OFFSET = 12; michael@0: static const size_t SKELETON_PRESENTATION_TIME_DENOMINATOR_OFFSET = 20; michael@0: michael@0: // Byte-offsets of the length of file field in the Skeleton 4.0 header packet. michael@0: static const size_t SKELETON_FILE_LENGTH_OFFSET = 64; michael@0: michael@0: // Byte-offsets of the fields in the Skeleton index packet. michael@0: static const size_t INDEX_SERIALNO_OFFSET = 6; michael@0: static const size_t INDEX_NUM_KEYPOINTS_OFFSET = 10; michael@0: static const size_t INDEX_TIME_DENOM_OFFSET = 18; michael@0: static const size_t INDEX_FIRST_NUMER_OFFSET = 26; michael@0: static const size_t INDEX_LAST_NUMER_OFFSET = 34; michael@0: static const size_t INDEX_KEYPOINT_OFFSET = 42; michael@0: michael@0: static bool IsSkeletonBOS(ogg_packet* aPacket) michael@0: { michael@0: return aPacket->bytes >= SKELETON_MIN_HEADER_LEN && michael@0: memcmp(reinterpret_cast(aPacket->packet), "fishead", 8) == 0; michael@0: } michael@0: michael@0: static bool IsSkeletonIndex(ogg_packet* aPacket) michael@0: { michael@0: return aPacket->bytes >= SKELETON_4_0_MIN_INDEX_LEN && michael@0: memcmp(reinterpret_cast(aPacket->packet), "index", 5) == 0; michael@0: } michael@0: michael@0: // Reads a variable length encoded integer at p. Will not read michael@0: // past aLimit. Returns pointer to character after end of integer. michael@0: static const unsigned char* ReadVariableLengthInt(const unsigned char* p, michael@0: const unsigned char* aLimit, michael@0: int64_t& n) michael@0: { michael@0: int shift = 0; michael@0: int64_t byte = 0; michael@0: n = 0; michael@0: while (p < aLimit && michael@0: (byte & 0x80) != 0x80 && michael@0: shift < 57) michael@0: { michael@0: byte = static_cast(*p); michael@0: n |= ((byte & 0x7f) << shift); michael@0: shift += 7; michael@0: p++; michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: bool SkeletonState::DecodeIndex(ogg_packet* aPacket) michael@0: { michael@0: NS_ASSERTION(aPacket->bytes >= SKELETON_4_0_MIN_INDEX_LEN, michael@0: "Index must be at least minimum size"); michael@0: if (!mActive) { michael@0: return false; michael@0: } michael@0: michael@0: uint32_t serialno = LittleEndian::readUint32(aPacket->packet + INDEX_SERIALNO_OFFSET); michael@0: int64_t numKeyPoints = LittleEndian::readInt64(aPacket->packet + INDEX_NUM_KEYPOINTS_OFFSET); michael@0: michael@0: int64_t endTime = 0, startTime = 0; michael@0: const unsigned char* p = aPacket->packet; michael@0: michael@0: int64_t timeDenom = LittleEndian::readInt64(aPacket->packet + INDEX_TIME_DENOM_OFFSET); michael@0: if (timeDenom == 0) { michael@0: LOG(PR_LOG_DEBUG, ("Ogg Skeleton Index packet for stream %u has 0 " michael@0: "timestamp denominator.", serialno)); michael@0: return (mActive = false); michael@0: } michael@0: michael@0: // Extract the start time. michael@0: CheckedInt64 t = CheckedInt64(LittleEndian::readInt64(p + INDEX_FIRST_NUMER_OFFSET)) * USECS_PER_S; michael@0: if (!t.isValid()) { michael@0: return (mActive = false); michael@0: } else { michael@0: startTime = t.value() / timeDenom; michael@0: } michael@0: michael@0: // Extract the end time. michael@0: t = LittleEndian::readInt64(p + INDEX_LAST_NUMER_OFFSET) * USECS_PER_S; michael@0: if (!t.isValid()) { michael@0: return (mActive = false); michael@0: } else { michael@0: endTime = t.value() / timeDenom; michael@0: } michael@0: michael@0: // Check the numKeyPoints value read, ensure we're not going to run out of michael@0: // memory while trying to decode the index packet. michael@0: CheckedInt64 minPacketSize = (CheckedInt64(numKeyPoints) * MIN_KEY_POINT_SIZE) + INDEX_KEYPOINT_OFFSET; michael@0: if (!minPacketSize.isValid()) michael@0: { michael@0: return (mActive = false); michael@0: } michael@0: michael@0: int64_t sizeofIndex = aPacket->bytes - INDEX_KEYPOINT_OFFSET; michael@0: int64_t maxNumKeyPoints = sizeofIndex / MIN_KEY_POINT_SIZE; michael@0: if (aPacket->bytes < minPacketSize.value() || michael@0: numKeyPoints > maxNumKeyPoints || michael@0: numKeyPoints < 0) michael@0: { michael@0: // Packet size is less than the theoretical minimum size, or the packet is michael@0: // claiming to store more keypoints than it's capable of storing. This means michael@0: // that the numKeyPoints field is too large or small for the packet to michael@0: // possibly contain as many packets as it claims to, so the numKeyPoints michael@0: // field is possibly malicious. Don't try decoding this index, we may run michael@0: // out of memory. michael@0: LOG(PR_LOG_DEBUG, ("Possibly malicious number of key points reported " michael@0: "(%lld) in index packet for stream %u.", michael@0: numKeyPoints, michael@0: serialno)); michael@0: return (mActive = false); michael@0: } michael@0: michael@0: nsAutoPtr keyPoints(new nsKeyFrameIndex(startTime, endTime)); michael@0: michael@0: p = aPacket->packet + INDEX_KEYPOINT_OFFSET; michael@0: const unsigned char* limit = aPacket->packet + aPacket->bytes; michael@0: int64_t numKeyPointsRead = 0; michael@0: CheckedInt64 offset = 0; michael@0: CheckedInt64 time = 0; michael@0: while (p < limit && michael@0: numKeyPointsRead < numKeyPoints) michael@0: { michael@0: int64_t delta = 0; michael@0: p = ReadVariableLengthInt(p, limit, delta); michael@0: offset += delta; michael@0: if (p == limit || michael@0: !offset.isValid() || michael@0: offset.value() > mLength || michael@0: offset.value() < 0) michael@0: { michael@0: return (mActive = false); michael@0: } michael@0: p = ReadVariableLengthInt(p, limit, delta); michael@0: time += delta; michael@0: if (!time.isValid() || michael@0: time.value() > endTime || michael@0: time.value() < startTime) michael@0: { michael@0: return (mActive = false); michael@0: } michael@0: CheckedInt64 timeUsecs = time * USECS_PER_S; michael@0: if (!timeUsecs.isValid()) michael@0: return mActive = false; michael@0: timeUsecs /= timeDenom; michael@0: keyPoints->Add(offset.value(), timeUsecs.value()); michael@0: numKeyPointsRead++; michael@0: } michael@0: michael@0: int32_t keyPointsRead = keyPoints->Length(); michael@0: if (keyPointsRead > 0) { michael@0: mIndex.Put(serialno, keyPoints.forget()); michael@0: } michael@0: michael@0: LOG(PR_LOG_DEBUG, ("Loaded %d keypoints for Skeleton on stream %u", michael@0: keyPointsRead, serialno)); michael@0: return true; michael@0: } michael@0: michael@0: nsresult SkeletonState::IndexedSeekTargetForTrack(uint32_t aSerialno, michael@0: int64_t aTarget, michael@0: nsKeyPoint& aResult) michael@0: { michael@0: nsKeyFrameIndex* index = nullptr; michael@0: mIndex.Get(aSerialno, &index); michael@0: michael@0: if (!index || michael@0: index->Length() == 0 || michael@0: aTarget < index->mStartTime || michael@0: aTarget > index->mEndTime) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // Binary search to find the last key point with time less than target. michael@0: int start = 0; michael@0: int end = index->Length() - 1; michael@0: while (end > start) { michael@0: int mid = start + ((end - start + 1) >> 1); michael@0: if (index->Get(mid).mTime == aTarget) { michael@0: start = mid; michael@0: break; michael@0: } else if (index->Get(mid).mTime < aTarget) { michael@0: start = mid; michael@0: } else { michael@0: end = mid - 1; michael@0: } michael@0: } michael@0: michael@0: aResult = index->Get(start); michael@0: NS_ASSERTION(aResult.mTime <= aTarget, "Result should have time <= target"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult SkeletonState::IndexedSeekTarget(int64_t aTarget, michael@0: nsTArray& aTracks, michael@0: nsSeekTarget& aResult) michael@0: { michael@0: if (!mActive || mVersion < SKELETON_VERSION(4,0)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: // Loop over all requested tracks' indexes, and get the keypoint for that michael@0: // seek target. Record the keypoint with the lowest offset, this will be michael@0: // our seek result. User must seek to the one with lowest offset to ensure we michael@0: // pass "keyframes" on all tracks when we decode forwards to the seek target. michael@0: nsSeekTarget r; michael@0: for (uint32_t i=0; i& aTracks, michael@0: int64_t& aDuration) michael@0: { michael@0: if (!mActive || michael@0: mVersion < SKELETON_VERSION(4,0) || michael@0: !HasIndex() || michael@0: aTracks.Length() == 0) michael@0: { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: int64_t endTime = INT64_MIN; michael@0: int64_t startTime = INT64_MAX; michael@0: for (uint32_t i=0; imEndTime > endTime) { michael@0: endTime = index->mEndTime; michael@0: } michael@0: if (index->mStartTime < startTime) { michael@0: startTime = index->mStartTime; michael@0: } michael@0: } michael@0: NS_ASSERTION(endTime > startTime, "Duration must be positive"); michael@0: CheckedInt64 duration = CheckedInt64(endTime) - startTime; michael@0: aDuration = duration.isValid() ? duration.value() : 0; michael@0: return duration.isValid() ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: bool SkeletonState::DecodeHeader(ogg_packet* aPacket) michael@0: { michael@0: nsAutoRef autoRelease(aPacket); michael@0: if (IsSkeletonBOS(aPacket)) { michael@0: uint16_t verMajor = LittleEndian::readUint16(aPacket->packet + SKELETON_VERSION_MAJOR_OFFSET); michael@0: uint16_t verMinor = LittleEndian::readUint16(aPacket->packet + SKELETON_VERSION_MINOR_OFFSET); michael@0: michael@0: // Read the presentation time. We read this before the version check as the michael@0: // presentation time exists in all versions. michael@0: int64_t n = LittleEndian::readInt64(aPacket->packet + SKELETON_PRESENTATION_TIME_NUMERATOR_OFFSET); michael@0: int64_t d = LittleEndian::readInt64(aPacket->packet + SKELETON_PRESENTATION_TIME_DENOMINATOR_OFFSET); michael@0: mPresentationTime = d == 0 ? 0 : (static_cast(n) / static_cast(d)) * USECS_PER_S; michael@0: michael@0: mVersion = SKELETON_VERSION(verMajor, verMinor); michael@0: // We can only care to parse Skeleton version 4.0+. michael@0: if (mVersion < SKELETON_VERSION(4,0) || michael@0: mVersion >= SKELETON_VERSION(5,0) || michael@0: aPacket->bytes < SKELETON_4_0_MIN_HEADER_LEN) michael@0: return false; michael@0: michael@0: // Extract the segment length. michael@0: mLength = LittleEndian::readInt64(aPacket->packet + SKELETON_FILE_LENGTH_OFFSET); michael@0: michael@0: LOG(PR_LOG_DEBUG, ("Skeleton segment length: %lld", mLength)); michael@0: michael@0: // Initialize the serialno-to-index map. michael@0: return true; michael@0: } else if (IsSkeletonIndex(aPacket) && mVersion >= SKELETON_VERSION(4,0)) { michael@0: return DecodeIndex(aPacket); michael@0: } else if (aPacket->e_o_s) { michael@0: mDoneReadingHeaders = true; michael@0: return true; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: michael@0: } // namespace mozilla michael@0: