1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/ogg/OpusParser.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,197 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <string.h> 1.11 + 1.12 +#include "mozilla/DebugOnly.h" 1.13 +#include "mozilla/Endian.h" 1.14 +#include <stdint.h> 1.15 + 1.16 +#include "OpusParser.h" 1.17 + 1.18 +#include "nsDebug.h" 1.19 +#include "MediaDecoderReader.h" 1.20 +#include "VideoUtils.h" 1.21 +#include <algorithm> 1.22 + 1.23 +#include "opus/opus.h" 1.24 +extern "C" { 1.25 +#include "opus/opus_multistream.h" 1.26 +} 1.27 + 1.28 +namespace mozilla { 1.29 + 1.30 +#ifdef PR_LOGGING 1.31 +extern PRLogModuleInfo* gMediaDecoderLog; 1.32 +#define OPUS_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg) 1.33 +#else 1.34 +#define OPUS_LOG(type, msg) 1.35 +#endif 1.36 + 1.37 +OpusParser::OpusParser(): 1.38 + mRate(0), 1.39 + mNominalRate(0), 1.40 + mChannels(0), 1.41 + mPreSkip(0), 1.42 +#ifdef MOZ_SAMPLE_TYPE_FLOAT32 1.43 + mGain(1.0f), 1.44 +#else 1.45 + mGain_Q16(65536), 1.46 +#endif 1.47 + mChannelMapping(0), 1.48 + mStreams(0), 1.49 + mCoupledStreams(0) 1.50 +{ } 1.51 + 1.52 +bool OpusParser::DecodeHeader(unsigned char* aData, size_t aLength) 1.53 +{ 1.54 + if (aLength < 19 || memcmp(aData, "OpusHead", 8)) { 1.55 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: unrecognized header")); 1.56 + return false; 1.57 + } 1.58 + 1.59 + mRate = 48000; // The Opus decoder runs at 48 kHz regardless. 1.60 + 1.61 + int version = aData[8]; 1.62 + // Accept file format versions 0.x. 1.63 + if ((version & 0xf0) != 0) { 1.64 + OPUS_LOG(PR_LOG_DEBUG, ("Rejecting unknown Opus file version %d", version)); 1.65 + return false; 1.66 + } 1.67 + 1.68 + mChannels = aData[9]; 1.69 + if (mChannels<1) { 1.70 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: Number of channels %d", mChannels)); 1.71 + return false; 1.72 + } 1.73 + 1.74 + mPreSkip = LittleEndian::readUint16(aData + 10); 1.75 + mNominalRate = LittleEndian::readUint32(aData + 12); 1.76 + double gain_dB = LittleEndian::readInt16(aData + 16) / 256.0; 1.77 +#ifdef MOZ_SAMPLE_TYPE_FLOAT32 1.78 + mGain = static_cast<float>(pow(10,0.05*gain_dB)); 1.79 +#else 1.80 + mGain_Q16 = static_cast<int32_t>(std::min(65536*pow(10,0.05*gain_dB)+0.5, 1.81 + static_cast<double>(INT32_MAX))); 1.82 +#endif 1.83 + mChannelMapping = aData[18]; 1.84 + 1.85 + if (mChannelMapping == 0) { 1.86 + // Mapping family 0 only allows two channels 1.87 + if (mChannels>2) { 1.88 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: too many channels (%d) for" 1.89 + " mapping family 0.", mChannels)); 1.90 + return false; 1.91 + } 1.92 + mStreams = 1; 1.93 + mCoupledStreams = mChannels - 1; 1.94 + mMappingTable[0] = 0; 1.95 + mMappingTable[1] = 1; 1.96 + } else if (mChannelMapping == 1) { 1.97 + // Currently only up to 8 channels are defined for mapping family 1 1.98 + if (mChannels>8) { 1.99 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: too many channels (%d) for" 1.100 + " mapping family 1.", mChannels)); 1.101 + return false; 1.102 + } 1.103 + if (aLength>static_cast<unsigned>(20+mChannels)) { 1.104 + mStreams = aData[19]; 1.105 + mCoupledStreams = aData[20]; 1.106 + int i; 1.107 + for (i=0; i<mChannels; i++) 1.108 + mMappingTable[i] = aData[21+i]; 1.109 + } else { 1.110 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: channel mapping %d," 1.111 + " but no channel mapping table", mChannelMapping)); 1.112 + return false; 1.113 + } 1.114 + } else { 1.115 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: unsupported channel mapping " 1.116 + "family %d", mChannelMapping)); 1.117 + return false; 1.118 + } 1.119 + if (mStreams < 1) { 1.120 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: no streams")); 1.121 + return false; 1.122 + } 1.123 + if (mCoupledStreams > mStreams) { 1.124 + OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: more coupled streams (%d) than " 1.125 + "total streams (%d)", mCoupledStreams, mStreams)); 1.126 + return false; 1.127 + } 1.128 + 1.129 +#ifdef DEBUG 1.130 + OPUS_LOG(PR_LOG_DEBUG, ("Opus stream header:")); 1.131 + OPUS_LOG(PR_LOG_DEBUG, (" channels: %d", mChannels)); 1.132 + OPUS_LOG(PR_LOG_DEBUG, (" preskip: %d", mPreSkip)); 1.133 + OPUS_LOG(PR_LOG_DEBUG, (" original: %d Hz", mNominalRate)); 1.134 + OPUS_LOG(PR_LOG_DEBUG, (" gain: %.2f dB", gain_dB)); 1.135 + OPUS_LOG(PR_LOG_DEBUG, ("Channel Mapping:")); 1.136 + OPUS_LOG(PR_LOG_DEBUG, (" family: %d", mChannelMapping)); 1.137 + OPUS_LOG(PR_LOG_DEBUG, (" streams: %d", mStreams)); 1.138 +#endif 1.139 + return true; 1.140 +} 1.141 + 1.142 +bool OpusParser::DecodeTags(unsigned char* aData, size_t aLength) 1.143 +{ 1.144 + if (aLength < 16 || memcmp(aData, "OpusTags", 8)) 1.145 + return false; 1.146 + 1.147 + // Copy out the raw comment lines, but only do basic validation 1.148 + // checks against the string packing: too little data, too many 1.149 + // comments, or comments that are too long. Rejecting these cases 1.150 + // helps reduce the propagation of broken files. 1.151 + // We do not ensure they are valid UTF-8 here, nor do we validate 1.152 + // the required ASCII_TAG=value format of the user comments. 1.153 + const unsigned char* buf = aData + 8; 1.154 + uint32_t bytes = aLength - 8; 1.155 + uint32_t len; 1.156 + // Read the vendor string. 1.157 + len = LittleEndian::readUint32(buf); 1.158 + buf += 4; 1.159 + bytes -= 4; 1.160 + if (len > bytes) 1.161 + return false; 1.162 + mVendorString = nsCString(reinterpret_cast<const char*>(buf), len); 1.163 + buf += len; 1.164 + bytes -= len; 1.165 + // Read the user comments. 1.166 + if (bytes < 4) 1.167 + return false; 1.168 + uint32_t ncomments = LittleEndian::readUint32(buf); 1.169 + buf += 4; 1.170 + bytes -= 4; 1.171 + // If there are so many comments even their length fields 1.172 + // won't fit in the packet, stop reading now. 1.173 + if (ncomments > (bytes>>2)) 1.174 + return false; 1.175 + uint32_t i; 1.176 + for (i = 0; i < ncomments; i++) { 1.177 + if (bytes < 4) 1.178 + return false; 1.179 + len = LittleEndian::readUint32(buf); 1.180 + buf += 4; 1.181 + bytes -= 4; 1.182 + if (len > bytes) 1.183 + return false; 1.184 + mTags.AppendElement(nsCString(reinterpret_cast<const char*>(buf), len)); 1.185 + buf += len; 1.186 + bytes -= len; 1.187 + } 1.188 + 1.189 +#ifdef DEBUG 1.190 + OPUS_LOG(PR_LOG_DEBUG, ("Opus metadata header:")); 1.191 + OPUS_LOG(PR_LOG_DEBUG, (" vendor: %s", mVendorString.get())); 1.192 + for (uint32_t i = 0; i < mTags.Length(); i++) { 1.193 + OPUS_LOG(PR_LOG_DEBUG, (" %s", mTags[i].get())); 1.194 + } 1.195 +#endif 1.196 + return true; 1.197 +} 1.198 + 1.199 +} // namespace mozilla 1.200 +