mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:30:27 +00:00
fdb63ff9b9
- Bug 1163201 - Part 1: Remove instances of #ifdef PR_LOGGING in dom/. r=froydnj (9979c0e74) - Bug 1183972 - No sync-dispatch of new GMPParent - r=cpearce (93339b530) - Bug 1142935 - reset transports with NuwaAddConstructor(). r=tlee (277406812) - Bug 1121676 - Use a lock to protect the list of top-level actors (r=bent) (3d0be2f87) - Bug 1121676 - Use static mutex to protect top-level protocols (r=bent) (4491dd318) - Bug 1163201 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (7de4b9a97) - Bug 1163201 - Part 3: Remove mSamples in |MediaEngineWebRTCAudioSource|. r=cpeterson (452442773) - Bug 1163201 - Part 4: Fix b2g build. r=bustage (a824ea36d) - Bug 1165518 - Part 1: Add Logging.h. r=froydnj (09d68aaa6) - Bug 1162850 - Don't stop looking for style sheet load finishes after the FontFaceSet gets a DOMContentLoaded. r=jdaggett (c29fbffa0) - Bug 1056479 p0 - rename ambiguous GetFontList method in Android fontlist. r=m_kato (76239d7a0) - Bug 1056479 p1 - add language to FindFamily parameters. r=jfkthame (2271bd7d0) - Bug 1056479 p1a - use lang as part of pref font fallback. r=karlt (5f5fd66c5) - cleanup GetTableFromFontData() to match gecko code again (78076fc26) - Bug 1056479 p2 - implement platform fontlist based on fontconfig. r=karlt (6a7631e44) - Bug 1056479 p3 - fixup various reftests for Linux. r=jfkthame (b25360708) - Bug 1056479 p4 - fix accessibility api for font-weight. r=jfkthame (efa8f5080) - Bug 1056479 p5 - fixup printpreview test. r=jfkthame (3fe2ddc0b) - Bug 1056479 p6 - handle font updates. r=jfkthame (eb78b2c54) - Bug 1056479 p7 - fixup assertion for non-italic fallback. r=m_kato (f5e9f539e) - Bug 1056479 p8 - switch gfxFontConfig to gfxFontconfig. r=karlt (4da146b50) - Bug 1056479 p9 - fix build bustage. r=birtles (28f246c2b) - Bug 1056479 p10 - activate bundled fonts. r=m_kato (d7627c3fa) - Bug 1056479 p10 - activate bundled fonts. r=m_kato (251c02315) - Bug 1056479 followup: Annotate gfxPlatformGtk::CreatePlatformFontList() as 'override'. rs=ehsan (993e65d6e)
242 lines
7.8 KiB
C++
242 lines
7.8 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
/* 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 "VorbisTrackEncoder.h"
|
|
#include <ogg/ogg.h>
|
|
#include <vorbis/vorbisenc.h>
|
|
#include "WebMWriter.h"
|
|
#include "GeckoProfiler.h"
|
|
|
|
// One actually used: Encoding using a VBR quality mode. The usable range is -.1
|
|
// (lowest quality, smallest file) to 1. (highest quality, largest file).
|
|
// Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
|
|
// ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
|
|
static const float BASE_QUALITY = 0.4f;
|
|
|
|
namespace mozilla {
|
|
|
|
#undef LOG
|
|
PRLogModuleInfo* gVorbisTrackEncoderLog;
|
|
#define VORBISLOG(msg, ...) PR_LOG(gVorbisTrackEncoderLog, PR_LOG_DEBUG, \
|
|
(msg, ##__VA_ARGS__))
|
|
|
|
VorbisTrackEncoder::VorbisTrackEncoder()
|
|
: AudioTrackEncoder()
|
|
{
|
|
MOZ_COUNT_CTOR(VorbisTrackEncoder);
|
|
if (!gVorbisTrackEncoderLog) {
|
|
gVorbisTrackEncoderLog = PR_NewLogModule("VorbisTrackEncoder");
|
|
}
|
|
}
|
|
|
|
VorbisTrackEncoder::~VorbisTrackEncoder()
|
|
{
|
|
MOZ_COUNT_DTOR(VorbisTrackEncoder);
|
|
if (mInitialized) {
|
|
vorbis_block_clear(&mVorbisBlock);
|
|
vorbis_dsp_clear(&mVorbisDsp);
|
|
vorbis_info_clear(&mVorbisInfo);
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
VorbisTrackEncoder::Init(int aChannels, int aSamplingRate)
|
|
{
|
|
NS_ENSURE_TRUE(aChannels > 0, NS_ERROR_INVALID_ARG);
|
|
NS_ENSURE_TRUE(aChannels <= 8, NS_ERROR_INVALID_ARG);
|
|
NS_ENSURE_TRUE(aSamplingRate >= 8000, NS_ERROR_INVALID_ARG);
|
|
NS_ENSURE_TRUE(aSamplingRate <= 192000, NS_ERROR_INVALID_ARG);
|
|
|
|
// This monitor is used to wake up other methods that are waiting for encoder
|
|
// to be completely initialized.
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
mChannels = aChannels;
|
|
mSamplingRate = aSamplingRate;
|
|
|
|
int ret = 0;
|
|
vorbis_info_init(&mVorbisInfo);
|
|
double quality = mAudioBitrate ? (double)mAudioBitrate/aSamplingRate :
|
|
BASE_QUALITY;
|
|
|
|
printf("quality %f \n", quality);
|
|
ret = vorbis_encode_init_vbr(&mVorbisInfo, mChannels, mSamplingRate,
|
|
quality);
|
|
|
|
mInitialized = (ret == 0);
|
|
|
|
if (mInitialized) {
|
|
// Set up the analysis state and auxiliary encoding storage
|
|
vorbis_analysis_init(&mVorbisDsp, &mVorbisInfo);
|
|
vorbis_block_init(&mVorbisDsp, &mVorbisBlock);
|
|
}
|
|
|
|
mon.NotifyAll();
|
|
|
|
return ret == 0 ? NS_OK : NS_ERROR_FAILURE;
|
|
}
|
|
|
|
void VorbisTrackEncoder::WriteLacing(nsTArray<uint8_t> *aOutput, int32_t aLacing)
|
|
{
|
|
while (aLacing >= 255) {
|
|
aLacing -= 255;
|
|
aOutput->AppendElement(255);
|
|
}
|
|
aOutput->AppendElement((uint8_t)aLacing);
|
|
}
|
|
|
|
already_AddRefed<TrackMetadataBase>
|
|
VorbisTrackEncoder::GetMetadata()
|
|
{
|
|
PROFILER_LABEL("VorbisTrackEncoder", "GetMetadata",
|
|
js::ProfileEntry::Category::OTHER);
|
|
{
|
|
// Wait if encoder is not initialized.
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
while (!mCanceled && !mInitialized) {
|
|
mon.Wait();
|
|
}
|
|
}
|
|
|
|
if (mCanceled || mEncodingComplete) {
|
|
return nullptr;
|
|
}
|
|
|
|
// Vorbis codec specific data
|
|
// http://matroska.org/technical/specs/codecid/index.html
|
|
nsRefPtr<VorbisMetadata> meta = new VorbisMetadata();
|
|
meta->mBitDepth = 32; // float for desktop
|
|
meta->mChannels = mChannels;
|
|
meta->mSamplingFrequency = mSamplingRate;
|
|
ogg_packet header;
|
|
ogg_packet header_comm;
|
|
ogg_packet header_code;
|
|
// Add comment
|
|
vorbis_comment vorbisComment;
|
|
vorbis_comment_init(&vorbisComment);
|
|
vorbis_comment_add_tag(&vorbisComment, "ENCODER",
|
|
NS_LITERAL_CSTRING("Mozilla VorbisTrackEncoder " MOZ_APP_UA_VERSION).get());
|
|
vorbis_analysis_headerout(&mVorbisDsp, &vorbisComment,
|
|
&header,&header_comm, &header_code);
|
|
vorbis_comment_clear(&vorbisComment);
|
|
// number of distinct packets - 1
|
|
meta->mData.AppendElement(2);
|
|
// Xiph-style lacing header.bytes, header_comm.bytes
|
|
WriteLacing(&(meta->mData), header.bytes);
|
|
WriteLacing(&(meta->mData), header_comm.bytes);
|
|
|
|
// Append the three packets
|
|
meta->mData.AppendElements(header.packet, header.bytes);
|
|
meta->mData.AppendElements(header_comm.packet, header_comm.bytes);
|
|
meta->mData.AppendElements(header_code.packet, header_code.bytes);
|
|
|
|
return meta.forget();
|
|
}
|
|
|
|
void
|
|
VorbisTrackEncoder::GetEncodedFrames(EncodedFrameContainer& aData)
|
|
{
|
|
// vorbis does some data preanalysis, then divvies up blocks for
|
|
// more involved (potentially parallel) processing. Get a single
|
|
// block for encoding now.
|
|
while (vorbis_analysis_blockout(&mVorbisDsp, &mVorbisBlock) == 1) {
|
|
ogg_packet oggPacket;
|
|
if (vorbis_analysis(&mVorbisBlock, &oggPacket) == 0) {
|
|
VORBISLOG("vorbis_analysis_blockout block size %d", oggPacket.bytes);
|
|
EncodedFrame* audiodata = new EncodedFrame();
|
|
audiodata->SetFrameType(EncodedFrame::VORBIS_AUDIO_FRAME);
|
|
nsTArray<uint8_t> frameData;
|
|
frameData.AppendElements(oggPacket.packet, oggPacket.bytes);
|
|
audiodata->SwapInFrameData(frameData);
|
|
aData.AppendEncodedFrame(audiodata);
|
|
}
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
VorbisTrackEncoder::GetEncodedTrack(EncodedFrameContainer& aData)
|
|
{
|
|
if (mEosSetInEncoder) {
|
|
return NS_OK;
|
|
}
|
|
|
|
PROFILER_LABEL("VorbisTrackEncoder", "GetEncodedTrack",
|
|
js::ProfileEntry::Category::OTHER);
|
|
|
|
nsAutoPtr<AudioSegment> sourceSegment;
|
|
sourceSegment = new AudioSegment();
|
|
{
|
|
// Move all the samples from mRawSegment to sourceSegment. We only hold
|
|
// the monitor in this block.
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
// Wait if mEncoder is not initialized, or when not enough raw data, but is
|
|
// not the end of stream nor is being canceled.
|
|
while (!mCanceled && mRawSegment.GetDuration() < GetPacketDuration() &&
|
|
!mEndOfStream) {
|
|
mon.Wait();
|
|
}
|
|
VORBISLOG("GetEncodedTrack passes wait, duration is %lld\n",
|
|
mRawSegment.GetDuration());
|
|
if (mCanceled || mEncodingComplete) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
sourceSegment->AppendFrom(&mRawSegment);
|
|
}
|
|
|
|
if (mEndOfStream && (sourceSegment->GetDuration() == 0)
|
|
&& !mEosSetInEncoder) {
|
|
mEncodingComplete = true;
|
|
mEosSetInEncoder = true;
|
|
VORBISLOG("[Vorbis] Done encoding.");
|
|
vorbis_analysis_wrote(&mVorbisDsp, 0);
|
|
GetEncodedFrames(aData);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
// Start encoding data.
|
|
AudioSegment::ChunkIterator iter(*sourceSegment);
|
|
|
|
AudioDataValue **vorbisBuffer =
|
|
vorbis_analysis_buffer(&mVorbisDsp, (int)sourceSegment->GetDuration());
|
|
|
|
int framesCopied = 0;
|
|
nsAutoTArray<AudioDataValue, 9600> interleavedPcm;
|
|
nsAutoTArray<AudioDataValue, 9600> nonInterleavedPcm;
|
|
interleavedPcm.SetLength(sourceSegment->GetDuration() * mChannels);
|
|
nonInterleavedPcm.SetLength(sourceSegment->GetDuration() * mChannels);
|
|
while (!iter.IsEnded()) {
|
|
AudioChunk chunk = *iter;
|
|
int frameToCopy = chunk.GetDuration();
|
|
if (!chunk.IsNull()) {
|
|
InterleaveTrackData(chunk, frameToCopy, mChannels,
|
|
interleavedPcm.Elements() + framesCopied * mChannels);
|
|
} else { // empty data
|
|
memset(interleavedPcm.Elements() + framesCopied * mChannels, 0,
|
|
frameToCopy * mChannels * sizeof(AudioDataValue));
|
|
}
|
|
framesCopied += frameToCopy;
|
|
iter.Next();
|
|
}
|
|
// De-interleave the interleavedPcm.
|
|
DeInterleaveTrackData(interleavedPcm.Elements(), framesCopied, mChannels,
|
|
nonInterleavedPcm.Elements());
|
|
// Copy the nonInterleavedPcm to vorbis buffer.
|
|
for(uint8_t i = 0; i < mChannels; ++i) {
|
|
memcpy(vorbisBuffer[i], nonInterleavedPcm.Elements() + framesCopied * i,
|
|
framesCopied * sizeof(AudioDataValue));
|
|
}
|
|
|
|
// Now the vorbisBuffer contain the all data in non-interleaved.
|
|
// Tell the library how much we actually submitted.
|
|
vorbis_analysis_wrote(&mVorbisDsp, framesCopied);
|
|
VORBISLOG("vorbis_analysis_wrote framesCopied %d\n", framesCopied);
|
|
GetEncodedFrames(aData);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
} // namespace mozilla
|