content/media/encoder/VorbisTrackEncoder.cpp

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #include "VorbisTrackEncoder.h"
michael@0 6 #include <ogg/ogg.h>
michael@0 7 #include <vorbis/vorbisenc.h>
michael@0 8 #include "WebMWriter.h"
michael@0 9
michael@0 10 // One actually used: Encoding using a VBR quality mode. The usable range is -.1
michael@0 11 // (lowest quality, smallest file) to 1. (highest quality, largest file).
michael@0 12 // Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
michael@0 13 // ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
michael@0 14 static const float BASE_QUALITY = 0.4f;
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17
michael@0 18 #undef LOG
michael@0 19 #ifdef PR_LOGGING
michael@0 20 PRLogModuleInfo* gVorbisTrackEncoderLog;
michael@0 21 #define VORBISLOG(msg, ...) PR_LOG(gVorbisTrackEncoderLog, PR_LOG_DEBUG, \
michael@0 22 (msg, ##__VA_ARGS__))
michael@0 23 #else
michael@0 24 #define VORBISLOG(msg, ...)
michael@0 25 #endif
michael@0 26
michael@0 27 VorbisTrackEncoder::VorbisTrackEncoder()
michael@0 28 : AudioTrackEncoder()
michael@0 29 {
michael@0 30 MOZ_COUNT_CTOR(VorbisTrackEncoder);
michael@0 31 #ifdef PR_LOGGING
michael@0 32 if (!gVorbisTrackEncoderLog) {
michael@0 33 gVorbisTrackEncoderLog = PR_NewLogModule("VorbisTrackEncoder");
michael@0 34 }
michael@0 35 #endif
michael@0 36 }
michael@0 37
michael@0 38 VorbisTrackEncoder::~VorbisTrackEncoder()
michael@0 39 {
michael@0 40 MOZ_COUNT_DTOR(VorbisTrackEncoder);
michael@0 41 if (mInitialized) {
michael@0 42 vorbis_block_clear(&mVorbisBlock);
michael@0 43 vorbis_dsp_clear(&mVorbisDsp);
michael@0 44 vorbis_info_clear(&mVorbisInfo);
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 nsresult
michael@0 49 VorbisTrackEncoder::Init(int aChannels, int aSamplingRate)
michael@0 50 {
michael@0 51 if (aChannels <= 0 || aChannels > 8) {
michael@0 52 VORBISLOG("aChannels <= 0 || aChannels > 8");
michael@0 53 return NS_ERROR_INVALID_ARG;
michael@0 54 }
michael@0 55
michael@0 56 // This monitor is used to wake up other methods that are waiting for encoder
michael@0 57 // to be completely initialized.
michael@0 58 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
michael@0 59 mChannels = aChannels;
michael@0 60 mSamplingRate = aSamplingRate;
michael@0 61
michael@0 62 int ret = 0;
michael@0 63 vorbis_info_init(&mVorbisInfo);
michael@0 64
michael@0 65 ret = vorbis_encode_init_vbr(&mVorbisInfo, mChannels, mSamplingRate,
michael@0 66 BASE_QUALITY);
michael@0 67
michael@0 68 mInitialized = (ret == 0);
michael@0 69
michael@0 70 if (mInitialized) {
michael@0 71 // Set up the analysis state and auxiliary encoding storage
michael@0 72 vorbis_analysis_init(&mVorbisDsp, &mVorbisInfo);
michael@0 73 vorbis_block_init(&mVorbisDsp, &mVorbisBlock);
michael@0 74 }
michael@0 75
michael@0 76 mon.NotifyAll();
michael@0 77
michael@0 78 return ret == 0 ? NS_OK : NS_ERROR_FAILURE;
michael@0 79 }
michael@0 80
michael@0 81 void VorbisTrackEncoder::WriteLacing(nsTArray<uint8_t> *aOutput, int32_t aLacing)
michael@0 82 {
michael@0 83 while (aLacing >= 255) {
michael@0 84 aLacing -= 255;
michael@0 85 aOutput->AppendElement(255);
michael@0 86 }
michael@0 87 aOutput->AppendElement((uint8_t)aLacing);
michael@0 88 }
michael@0 89
michael@0 90 already_AddRefed<TrackMetadataBase>
michael@0 91 VorbisTrackEncoder::GetMetadata()
michael@0 92 {
michael@0 93 {
michael@0 94 // Wait if encoder is not initialized.
michael@0 95 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
michael@0 96 while (!mCanceled && !mInitialized) {
michael@0 97 mon.Wait();
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 if (mCanceled || mEncodingComplete) {
michael@0 102 return nullptr;
michael@0 103 }
michael@0 104
michael@0 105 // Vorbis codec specific data
michael@0 106 // http://matroska.org/technical/specs/codecid/index.html
michael@0 107 nsRefPtr<VorbisMetadata> meta = new VorbisMetadata();
michael@0 108 meta->mBitDepth = 32; // float for desktop
michael@0 109 meta->mChannels = mChannels;
michael@0 110 meta->mSamplingFrequency = mSamplingRate;
michael@0 111 ogg_packet header;
michael@0 112 ogg_packet header_comm;
michael@0 113 ogg_packet header_code;
michael@0 114 // Add comment
michael@0 115 vorbis_comment vorbisComment;
michael@0 116 vorbis_comment_init(&vorbisComment);
michael@0 117 vorbis_comment_add_tag(&vorbisComment, "ENCODER",
michael@0 118 NS_LITERAL_CSTRING("Mozilla VorbisTrackEncoder " MOZ_APP_UA_VERSION).get());
michael@0 119 vorbis_analysis_headerout(&mVorbisDsp, &vorbisComment,
michael@0 120 &header,&header_comm, &header_code);
michael@0 121 vorbis_comment_clear(&vorbisComment);
michael@0 122 // number of distinct packets - 1
michael@0 123 meta->mData.AppendElement(2);
michael@0 124 // Xiph-style lacing header.bytes, header_comm.bytes
michael@0 125 WriteLacing(&(meta->mData), header.bytes);
michael@0 126 WriteLacing(&(meta->mData), header_comm.bytes);
michael@0 127
michael@0 128 // Append the three packets
michael@0 129 meta->mData.AppendElements(header.packet, header.bytes);
michael@0 130 meta->mData.AppendElements(header_comm.packet, header_comm.bytes);
michael@0 131 meta->mData.AppendElements(header_code.packet, header_code.bytes);
michael@0 132
michael@0 133 return meta.forget();
michael@0 134 }
michael@0 135
michael@0 136 void
michael@0 137 VorbisTrackEncoder::GetEncodedFrames(EncodedFrameContainer& aData)
michael@0 138 {
michael@0 139 // vorbis does some data preanalysis, then divvies up blocks for
michael@0 140 // more involved (potentially parallel) processing. Get a single
michael@0 141 // block for encoding now.
michael@0 142 while (vorbis_analysis_blockout(&mVorbisDsp, &mVorbisBlock) == 1) {
michael@0 143 ogg_packet oggPacket;
michael@0 144 if (vorbis_analysis(&mVorbisBlock, &oggPacket) == 0) {
michael@0 145 VORBISLOG("vorbis_analysis_blockout block size %d", oggPacket.bytes);
michael@0 146 EncodedFrame* audiodata = new EncodedFrame();
michael@0 147 audiodata->SetFrameType(EncodedFrame::VORBIS_AUDIO_FRAME);
michael@0 148 nsTArray<uint8_t> frameData;
michael@0 149 frameData.AppendElements(oggPacket.packet, oggPacket.bytes);
michael@0 150 audiodata->SwapInFrameData(frameData);
michael@0 151 aData.AppendEncodedFrame(audiodata);
michael@0 152 }
michael@0 153 }
michael@0 154 }
michael@0 155
michael@0 156 nsresult
michael@0 157 VorbisTrackEncoder::GetEncodedTrack(EncodedFrameContainer& aData)
michael@0 158 {
michael@0 159 if (mEosSetInEncoder) {
michael@0 160 return NS_OK;
michael@0 161 }
michael@0 162
michael@0 163 nsAutoPtr<AudioSegment> sourceSegment;
michael@0 164 sourceSegment = new AudioSegment();
michael@0 165 {
michael@0 166 // Move all the samples from mRawSegment to sourceSegment. We only hold
michael@0 167 // the monitor in this block.
michael@0 168 ReentrantMonitorAutoEnter mon(mReentrantMonitor);
michael@0 169
michael@0 170 // Wait if mEncoder is not initialized, or when not enough raw data, but is
michael@0 171 // not the end of stream nor is being canceled.
michael@0 172 while (!mCanceled && mRawSegment.GetDuration() < GetPacketDuration() &&
michael@0 173 !mEndOfStream) {
michael@0 174 mon.Wait();
michael@0 175 }
michael@0 176 VORBISLOG("GetEncodedTrack passes wait, duration is %lld\n",
michael@0 177 mRawSegment.GetDuration());
michael@0 178 if (mCanceled || mEncodingComplete) {
michael@0 179 return NS_ERROR_FAILURE;
michael@0 180 }
michael@0 181
michael@0 182 sourceSegment->AppendFrom(&mRawSegment);
michael@0 183 }
michael@0 184
michael@0 185 if (mEndOfStream && (sourceSegment->GetDuration() == 0)
michael@0 186 && !mEosSetInEncoder) {
michael@0 187 mEncodingComplete = true;
michael@0 188 mEosSetInEncoder = true;
michael@0 189 VORBISLOG("[Vorbis] Done encoding.");
michael@0 190 vorbis_analysis_wrote(&mVorbisDsp, 0);
michael@0 191 GetEncodedFrames(aData);
michael@0 192
michael@0 193 return NS_OK;
michael@0 194 }
michael@0 195
michael@0 196 // Start encoding data.
michael@0 197 AudioSegment::ChunkIterator iter(*sourceSegment);
michael@0 198
michael@0 199 AudioDataValue **vorbisBuffer =
michael@0 200 vorbis_analysis_buffer(&mVorbisDsp, (int)sourceSegment->GetDuration());
michael@0 201
michael@0 202 int framesCopied = 0;
michael@0 203 nsAutoTArray<AudioDataValue, 9600> interleavedPcm;
michael@0 204 nsAutoTArray<AudioDataValue, 9600> nonInterleavedPcm;
michael@0 205 interleavedPcm.SetLength(sourceSegment->GetDuration() * mChannels);
michael@0 206 nonInterleavedPcm.SetLength(sourceSegment->GetDuration() * mChannels);
michael@0 207 while (!iter.IsEnded()) {
michael@0 208 AudioChunk chunk = *iter;
michael@0 209 int frameToCopy = chunk.GetDuration();
michael@0 210 if (!chunk.IsNull()) {
michael@0 211 InterleaveTrackData(chunk, frameToCopy, mChannels,
michael@0 212 interleavedPcm.Elements() + framesCopied * mChannels);
michael@0 213 } else { // empty data
michael@0 214 memset(interleavedPcm.Elements() + framesCopied * mChannels, 0,
michael@0 215 frameToCopy * mChannels * sizeof(AudioDataValue));
michael@0 216 }
michael@0 217 framesCopied += frameToCopy;
michael@0 218 iter.Next();
michael@0 219 }
michael@0 220 // De-interleave the interleavedPcm.
michael@0 221 DeInterleaveTrackData(interleavedPcm.Elements(), framesCopied, mChannels,
michael@0 222 nonInterleavedPcm.Elements());
michael@0 223 // Copy the nonInterleavedPcm to vorbis buffer.
michael@0 224 for(uint8_t i = 0; i < mChannels; ++i) {
michael@0 225 memcpy(vorbisBuffer[i], nonInterleavedPcm.Elements() + framesCopied * i,
michael@0 226 framesCopied * sizeof(AudioDataValue));
michael@0 227 }
michael@0 228
michael@0 229 // Now the vorbisBuffer contain the all data in non-interleaved.
michael@0 230 // Tell the library how much we actually submitted.
michael@0 231 vorbis_analysis_wrote(&mVorbisDsp, framesCopied);
michael@0 232 VORBISLOG("vorbis_analysis_wrote framesCopied %d\n", framesCopied);
michael@0 233 GetEncodedFrames(aData);
michael@0 234
michael@0 235 return NS_OK;
michael@0 236 }
michael@0 237
michael@0 238 } // namespace mozilla

mercurial