Bug 945532 - Kill LE* functions in OggCodecState.cpp and use Endian.h functions instead. r=kinetik
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 03 Dec 2013 12:30:45 +0900
changeset 174058 f729bbb4cc7d8ac298159f6ed0482204fa69379a
parent 174057 0b91759b57bc9eec07880345b340414b6286536b
child 174059 357c5de8547b92b11761fd6b67abc8014dcc3f51
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs945532
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 945532 - Kill LE* functions in OggCodecState.cpp and use Endian.h functions instead. r=kinetik
content/media/ogg/OggCodecState.cpp
content/media/ogg/OpusParser.cpp
--- a/content/media/ogg/OggCodecState.cpp
+++ b/content/media/ogg/OggCodecState.cpp
@@ -2,16 +2,17 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* 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 <string.h>
 
 #include "mozilla/DebugOnly.h"
+#include "mozilla/Endian.h"
 #include <stdint.h>
 
 #include "nsDebug.h"
 #include "MediaDecoderReader.h"
 #include "OggCodecState.h"
 #include "OggDecoder.h"
 #include "nsTraceRefcnt.h"
 #include "VideoUtils.h"
@@ -33,45 +34,16 @@ namespace mozilla {
 
 #ifdef PR_LOGGING
 extern PRLogModuleInfo* gMediaDecoderLog;
 #define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
 #else
 #define LOG(type, msg)
 #endif
 
-// Reads a little-endian encoded unsigned 32bit integer at p.
-static uint32_t LEUint32(const unsigned char* p)
-{
-  return p[0] +
-        (p[1] << 8) +
-        (p[2] << 16) +
-        (p[3] << 24);
-}
-
-// Reads a little-endian encoded 64bit integer at p.
-static int64_t LEInt64(const unsigned char* p)
-{
-  uint32_t lo = LEUint32(p);
-  uint32_t hi = LEUint32(p + 4);
-  return static_cast<int64_t>(lo) | (static_cast<int64_t>(hi) << 32);
-}
-
-// Reads a little-endian encoded unsigned 16bit integer at p.
-static uint16_t LEUint16(const unsigned char* p)
-{
-  return p[0] + (p[1] << 8);
-}
-
-// Reads a little-endian encoded signed 16bit integer at p.
-inline int16_t LEInt16(const unsigned char* p)
-{
-  return static_cast<int16_t>(LEUint16(p));
-}
-
 /** Decoder base class for Ogg-encapsulated streams. */
 OggCodecState*
 OggCodecState::Create(ogg_page* aPage)
 {
   NS_ASSERTION(ogg_page_bos(aPage), "Only call on BOS page!");
   nsAutoPtr<OggCodecState> codecState;
   if (aPage->body_len > 6 && memcmp(aPage->body+1, "theora", 6) == 0) {
     codecState = new TheoraState(aPage);
@@ -1191,39 +1163,39 @@ static const unsigned char* ReadVariable
 bool SkeletonState::DecodeIndex(ogg_packet* aPacket)
 {
   NS_ASSERTION(aPacket->bytes >= SKELETON_4_0_MIN_INDEX_LEN,
                "Index must be at least minimum size");
   if (!mActive) {
     return false;
   }
 
-  uint32_t serialno = LEUint32(aPacket->packet + INDEX_SERIALNO_OFFSET);
-  int64_t numKeyPoints = LEInt64(aPacket->packet + INDEX_NUM_KEYPOINTS_OFFSET);
+  uint32_t serialno = LittleEndian::readUint32(aPacket->packet + INDEX_SERIALNO_OFFSET);
+  int64_t numKeyPoints = LittleEndian::readInt64(aPacket->packet + INDEX_NUM_KEYPOINTS_OFFSET);
 
   int64_t endTime = 0, startTime = 0;
   const unsigned char* p = aPacket->packet;
 
-  int64_t timeDenom = LEInt64(aPacket->packet + INDEX_TIME_DENOM_OFFSET);
+  int64_t timeDenom = LittleEndian::readInt64(aPacket->packet + INDEX_TIME_DENOM_OFFSET);
   if (timeDenom == 0) {
     LOG(PR_LOG_DEBUG, ("Ogg Skeleton Index packet for stream %u has 0 "
                        "timestamp denominator.", serialno));
     return (mActive = false);
   }
 
   // Extract the start time.
-  CheckedInt64 t = CheckedInt64(LEInt64(p + INDEX_FIRST_NUMER_OFFSET)) * USECS_PER_S;
+  CheckedInt64 t = CheckedInt64(LittleEndian::readInt64(p + INDEX_FIRST_NUMER_OFFSET)) * USECS_PER_S;
   if (!t.isValid()) {
     return (mActive = false);
   } else {
     startTime = t.value() / timeDenom;
   }
 
   // Extract the end time.
-  t = LEInt64(p + INDEX_LAST_NUMER_OFFSET) * USECS_PER_S;
+  t = LittleEndian::readInt64(p + INDEX_LAST_NUMER_OFFSET) * USECS_PER_S;
   if (!t.isValid()) {
     return (mActive = false);
   } else {
     endTime = t.value() / timeDenom;
   }
 
   // Check the numKeyPoints value read, ensure we're not going to run out of
   // memory while trying to decode the index packet.
@@ -1394,34 +1366,34 @@ nsresult SkeletonState::GetDuration(cons
   aDuration = duration.isValid() ? duration.value() : 0;
   return duration.isValid() ? NS_OK : NS_ERROR_FAILURE;
 }
 
 bool SkeletonState::DecodeHeader(ogg_packet* aPacket)
 {
   nsAutoRef<ogg_packet> autoRelease(aPacket);
   if (IsSkeletonBOS(aPacket)) {
-    uint16_t verMajor = LEUint16(aPacket->packet + SKELETON_VERSION_MAJOR_OFFSET);
-    uint16_t verMinor = LEUint16(aPacket->packet + SKELETON_VERSION_MINOR_OFFSET);
+    uint16_t verMajor = LittleEndian::readUint16(aPacket->packet + SKELETON_VERSION_MAJOR_OFFSET);
+    uint16_t verMinor = LittleEndian::readUint16(aPacket->packet + SKELETON_VERSION_MINOR_OFFSET);
 
     // Read the presentation time. We read this before the version check as the
     // presentation time exists in all versions.
-    int64_t n = LEInt64(aPacket->packet + SKELETON_PRESENTATION_TIME_NUMERATOR_OFFSET);
-    int64_t d = LEInt64(aPacket->packet + SKELETON_PRESENTATION_TIME_DENOMINATOR_OFFSET);
+    int64_t n = LittleEndian::readInt64(aPacket->packet + SKELETON_PRESENTATION_TIME_NUMERATOR_OFFSET);
+    int64_t d = LittleEndian::readInt64(aPacket->packet + SKELETON_PRESENTATION_TIME_DENOMINATOR_OFFSET);
     mPresentationTime = d == 0 ? 0 : (static_cast<float>(n) / static_cast<float>(d)) * USECS_PER_S;
 
     mVersion = SKELETON_VERSION(verMajor, verMinor);
     // We can only care to parse Skeleton version 4.0+.
     if (mVersion < SKELETON_VERSION(4,0) ||
         mVersion >= SKELETON_VERSION(5,0) ||
         aPacket->bytes < SKELETON_4_0_MIN_HEADER_LEN)
       return false;
 
     // Extract the segment length.
-    mLength = LEInt64(aPacket->packet + SKELETON_FILE_LENGTH_OFFSET);
+    mLength = LittleEndian::readInt64(aPacket->packet + SKELETON_FILE_LENGTH_OFFSET);
 
     LOG(PR_LOG_DEBUG, ("Skeleton segment length: %lld", mLength));
 
     // Initialize the serialno-to-index map.
     return true;
   } else if (IsSkeletonIndex(aPacket) && mVersion >= SKELETON_VERSION(4,0)) {
     return DecodeIndex(aPacket);
   } else if (aPacket->e_o_s) {
--- a/content/media/ogg/OpusParser.cpp
+++ b/content/media/ogg/OpusParser.cpp
@@ -2,16 +2,17 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* 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 <string.h>
 
 #include "mozilla/DebugOnly.h"
+#include "mozilla/Endian.h"
 #include <stdint.h>
 
 #include "OpusParser.h"
 
 #include "nsDebug.h"
 #include "MediaDecoderReader.h"
 #include "VideoUtils.h"
 #include <algorithm>
@@ -62,19 +63,19 @@ bool OpusParser::DecodeHeader(unsigned c
     }
 
     mChannels = aData[9];
     if (mChannels<1) {
       OPUS_LOG(PR_LOG_DEBUG, ("Invalid Opus file: Number of channels %d", mChannels));
       return false;
     }
 
-    mPreSkip = LEUint16(aData + 10);
-    mNominalRate = LEUint32(aData + 12);
-    double gain_dB = LEInt16(aData + 16) / 256.0;
+    mPreSkip = LittleEndian::readUint16(aData + 10);
+    mNominalRate = LittleEndian::readUint32(aData + 12);
+    double gain_dB = LittleEndian::readInt16(aData + 16) / 256.0;
 #ifdef MOZ_SAMPLE_TYPE_FLOAT32
     mGain = static_cast<float>(pow(10,0.05*gain_dB));
 #else
     mGain_Q16 = static_cast<int32_t>(std::min(65536*pow(10,0.05*gain_dB)+0.5,
                                             static_cast<double>(INT32_MAX)));
 #endif
     mChannelMapping = aData[18];
 
@@ -145,39 +146,39 @@ bool OpusParser::DecodeTags(unsigned cha
   // comments, or comments that are too long. Rejecting these cases
   // helps reduce the propagation of broken files.
   // We do not ensure they are valid UTF-8 here, nor do we validate
   // the required ASCII_TAG=value format of the user comments.
   const unsigned char* buf = aData + 8;
   uint32_t bytes = aLength - 8;
   uint32_t len;
   // Read the vendor string.
-  len = LEUint32(buf);
+  len = LittleEndian::readUint32(buf);
   buf += 4;
   bytes -= 4;
   if (len > bytes)
     return false;
   mVendorString = nsCString(reinterpret_cast<const char*>(buf), len);
   buf += len;
   bytes -= len;
   // Read the user comments.
   if (bytes < 4)
     return false;
-  uint32_t ncomments = LEUint32(buf);
+  uint32_t ncomments = LittleEndian::readUint32(buf);
   buf += 4;
   bytes -= 4;
   // If there are so many comments even their length fields
   // won't fit in the packet, stop reading now.
   if (ncomments > (bytes>>2))
     return false;
   uint32_t i;
   for (i = 0; i < ncomments; i++) {
     if (bytes < 4)
       return false;
-    len = LEUint32(buf);
+    len = LittleEndian::readUint32(buf);
     buf += 4;
     bytes -= 4;
     if (len > bytes)
       return false;
     mTags.AppendElement(nsCString(reinterpret_cast<const char*>(buf), len));
     buf += len;
     bytes -= len;
   }