Permalink
Browse files

Bug 674225 - Cleanup from review comments.

Fix formatting, add comments, remove debug output,
plus some code cleanup. Thanks to Chris Pearce
for the review.
  • Loading branch information...
1 parent 2c12df7 commit 65bb143409c32dafcfd895687abccfad3bc45b1c @rillian committed Apr 3, 2012
Showing with 59 additions and 57 deletions.
  1. +28 −30 content/media/ogg/nsOggCodecState.cpp
  2. +7 −6 content/media/ogg/nsOggCodecState.h
  3. +24 −21 content/media/ogg/nsOggReader.cpp
@@ -781,23 +781,26 @@ nsresult nsVorbisState::ReconstructVorbisGranulepos()
nsOpusState::nsOpusState(ogg_page* aBosPage) :
- nsOggCodecState(aBosPage, true),
- mRate(0),
- mNominalRate(0),
- mChannels(0),
- mDecoder(NULL)
+ nsOggCodecState(aBosPage, true),
+ mRate(0),
+ mNominalRate(0),
+ mChannels(0),
+ mPreSkip(0),
+ mGain(0.0),
+ mChannelMapping(0),
+ mDecoder(NULL)
{
- MOZ_COUNT_CTOR(nsOpusState);
+ MOZ_COUNT_CTOR(nsOpusState);
}
nsOpusState::~nsOpusState() {
- MOZ_COUNT_DTOR(nsOpusState);
- Reset();
+ MOZ_COUNT_DTOR(nsOpusState);
+ Reset();
- if (mDecoder) {
- opus_decoder_destroy(mDecoder);
- mDecoder = NULL;
- }
+ if (mDecoder) {
+ opus_decoder_destroy(mDecoder);
+ mDecoder = NULL;
+ }
}
nsresult nsOpusState::Reset()
@@ -842,15 +845,13 @@ bool nsOpusState::DecodeHeader(ogg_packet* aPacket)
// try parsing as the metadata header
if (!memcmp(aPacket->packet, "OpusTags", 8)) {
- mDoneReadingHeaders = true; // last opus header
- mActive = true;
- return true;
+ mDoneReadingHeaders = true; // last opus header
+ mActive = true;
+ return true;
}
// otherwise, parse as the id header
- if (memcmp(aPacket->packet, "OpusHead\0", 9))
- return NS_ERROR_FAILURE;
- if (aPacket->bytes < 19)
+ if (aPacket->bytes < 19 || memcmp(aPacket->packet, "OpusHead\0", 9))
return NS_ERROR_FAILURE;
mRate = 48000; // decoder runs at 48 kHz regardless
@@ -877,8 +878,6 @@ PRInt64 nsOpusState::Time(PRInt64 granulepos)
return -1;
/* Ogg Opus always runs at a granule rate of 48 kHz */
- //CheckedInt64 t = CheckedInt64(granulepos - mPreSkip) * USECS_PER_S;
- // HACK: don't subtract preskip to avoid generating negative timestamps.
CheckedInt64 t = CheckedInt64(granulepos) * USECS_PER_S;
if (!t.valid())
return -1;
@@ -901,7 +900,7 @@ nsresult nsOpusState::PageIn(ogg_page* aPage)
if (!mActive)
return NS_OK;
NS_ASSERTION(static_cast<PRUint32>(ogg_page_serialno(aPage)) == mSerial,
- "Page is not for this stream");
+ "Page must be for this stream!");
if (ogg_stream_pagein(&mState, aPage) == -1)
return NS_ERROR_FAILURE;
@@ -928,28 +927,27 @@ void nsOpusState::ReconstructGranulepos(void)
ogg_packet* last = mUnstamped[mUnstamped.Length()-1];
NS_ASSERTION(last->e_o_s || last->granulepos > 0,
"Must know last granulepos!");
- if (mUnstamped.Length() == 1)
- return; // nothing to do
// loop through the packets backwards, subtracting the next
// packet's duration from its granulepos to get the value
// for the current packet.
for (PRUint32 i = mUnstamped.Length() - 1; i > 0; i--) {
ogg_packet* next = mUnstamped[i];
int offset = opus_decoder_get_nb_samples(mDecoder,
- next->packet, next->bytes);
- // check for error (negative) and overflow
+ next->packet,
+ next->bytes);
+ // check for error (negative offset) and overflow
if (offset > 0 && offset < next->granulepos)
mUnstamped[i - 1]->granulepos = next->granulepos - offset;
}
}
-nsSkeletonState::nsSkeletonState(ogg_page* aBosPage)
- : nsOggCodecState(aBosPage, true),
- mVersion(0),
- mPresentationTime(0),
- mLength(0)
+nsSkeletonState::nsSkeletonState(ogg_page* aBosPage) :
+ nsOggCodecState(aBosPage, true),
+ mVersion(0),
+ mPresentationTime(0),
+ mLength(0)
{
MOZ_COUNT_CTOR(nsSkeletonState);
}
@@ -336,12 +336,13 @@ class nsOpusState : public nsOggCodecState {
bool IsHeader(ogg_packet* aPacket);
nsresult PageIn(ogg_page* aPage);
- int mRate;
- int mNominalRate;
- int mChannels;
- int mPreSkip;
- float mGain;
- int mChannelMapping;
+ // Various fields from the Ogg Opus header
+ int mRate; // Sample rate the decoder uses (always 48 kHz)
+ int mNominalRate; // Original sample rate of the data (informational)
+ int mChannels; // Number of channels decoder will produce
+ int mPreSkip; // Number of samples to strip after decoder reset
+ float mGain; // Gain (dB) to apply to decoder output
+ int mChannelMapping; // Channel mapping family
OpusDecoder *mDecoder;
@@ -230,7 +230,8 @@ nsresult nsOggReader::ReadMetadata(nsVideoInfo* aInfo)
if (mOpusEnabled)
mOpusState = static_cast<nsOpusState*>(codecState);
else
- printf("media.opus.enabled is not set\n");
+ NS_WARNING("Opus decoding disabled."
+ " See media.opus.enabled in about:config");
}
if (codecState &&
codecState->GetType() == nsOggCodecState::TYPE_SKELETON &&
@@ -413,7 +414,8 @@ nsresult nsOggReader::DecodeOpus(ogg_packet* aPacket) {
NS_ASSERTION(aPacket->granulepos != -1, "Must know opus granulepos!");
PRInt32 frames = opus_decoder_get_nb_samples(mOpusState->mDecoder,
- aPacket->packet, aPacket->bytes);
+ aPacket->packet,
+ aPacket->bytes);
if (frames <= 0)
return NS_ERROR_FAILURE;
PRUint32 channels = mOpusState->mChannels;
@@ -423,7 +425,9 @@ nsresult nsOggReader::DecodeOpus(ogg_packet* aPacket) {
NS_ASSERTION(MOZ_SAMPLE_TYPE_FLOAT32, "need to hook up fixed-point decode");
int ret = opus_decode_float(mOpusState->mDecoder,
- aPacket->packet, aPacket->bytes, buffer, frames, false);
+ aPacket->packet,
+ aPacket->bytes,
+ buffer, frames, false);
if (ret < 0)
return NS_ERROR_FAILURE;
NS_ASSERTION(ret == frames, "Opus decoded too few audio samples");
@@ -444,26 +448,23 @@ nsresult nsOggReader::DecodeOpus(ogg_packet* aPacket) {
bool nsOggReader::DecodeAudioData()
{
NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
- NS_ASSERTION(mVorbisState!=0 || mOpusState!=0,
+ NS_ASSERTION(mVorbisState != nsnull || mOpusState != nsnull,
"Need audio codec state to decode audio");
// Read the next data packet. Skip any non-data packets we encounter.
ogg_packet* packet = 0;
- if (mVorbisState) {
- do {
- if (packet) {
- nsOggCodecState::ReleasePacket(packet);
- }
- packet = NextOggPacket(mVorbisState);
- } while (packet && mVorbisState->IsHeader(packet));
- } else if (mOpusState) {
- do {
- if (packet) {
- nsOggCodecState::ReleasePacket(packet);
- }
- packet = NextOggPacket(mOpusState);
- } while (packet && mOpusState->IsHeader(packet));
- }
+ nsOggCodecState* codecState;
+ if (mVorbisState)
+ codecState = static_cast<nsOggCodecState*>(mVorbisState);
+ else
+ codecState = static_cast<nsOggCodecState*>(mOpusState);
+ do {
+ if (packet) {
+ nsOggCodecState::ReleasePacket(packet);
+ }
+ packet = NextOggPacket(codecState);
+ } while (packet && codecState->IsHeader(packet));
+
if (!packet) {
mAudioQueue.Finish();
return false;
@@ -472,10 +473,12 @@ bool nsOggReader::DecodeAudioData()
NS_ASSERTION(packet && packet->granulepos != -1,
"Must have packet with known granulepos");
nsAutoReleasePacket autoRelease(packet);
- if (mVorbisState)
+ if (mVorbisState) {
DecodeVorbis(packet);
- else
+ } else if (mOpusState) {
DecodeOpus(packet);
+ }
+
if (packet->e_o_s) {
// We've encountered an end of bitstream packet, or we've hit the end of
// file while trying to decode, so inform the audio queue that there'll

0 comments on commit 65bb143

Please sign in to comment.