Bug 1552530 - Templatize ByteWriter.h to allow writing big or little endian, and fix call sites. r=jya
authorPaul Adenot <paul@paul.cx>
Tue, 21 May 2019 10:10:04 +0000
changeset 474707 c4045738b0cdb76f1ff2c492c390751147bce62c
parent 474706 d4c346b146a1b9f123fa3a704f23ff1196209fae
child 474708 6d2b52f066bf543dbbcbc3299de5147332392d82
push id113168
push userrmaries@mozilla.com
push dateTue, 21 May 2019 16:39:23 +0000
treeherdermozilla-inbound@3c0f78074b72 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1552530
milestone69.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 1552530 - Templatize ByteWriter.h to allow writing big or little endian, and fix call sites. r=jya Differential Revision: https://phabricator.services.mozilla.com/D31766
dom/media/ByteWriter.h
dom/media/platforms/agnostic/bytestreams/AnnexB.cpp
dom/media/platforms/agnostic/bytestreams/H264.cpp
--- a/dom/media/ByteWriter.h
+++ b/dom/media/ByteWriter.h
@@ -5,50 +5,51 @@
 #ifndef BYTE_WRITER_H_
 #define BYTE_WRITER_H_
 
 #include "mozilla/EndianUtils.h"
 #include "nsTArray.h"
 
 namespace mozilla {
 
+template <typename Endianess>
 class ByteWriter {
  public:
   explicit ByteWriter(nsTArray<uint8_t>& aData) : mPtr(aData) {}
   ~ByteWriter() {}
 
   MOZ_MUST_USE bool WriteU8(uint8_t aByte) { return Write(&aByte, 1); }
 
   MOZ_MUST_USE bool WriteU16(uint16_t aShort) {
     uint8_t c[2];
-    mozilla::BigEndian::writeUint16(&c[0], aShort);
+    Endianess::writeUint16(&c[0], aShort);
     return Write(&c[0], 2);
   }
 
   MOZ_MUST_USE bool WriteU32(uint32_t aLong) {
     uint8_t c[4];
-    mozilla::BigEndian::writeUint32(&c[0], aLong);
+    Endianess::writeUint32(&c[0], aLong);
     return Write(&c[0], 4);
   }
 
   MOZ_MUST_USE bool Write32(int32_t aLong) {
     uint8_t c[4];
-    mozilla::BigEndian::writeInt32(&c[0], aLong);
+    Endianess::writeInt32(&c[0], aLong);
     return Write(&c[0], 4);
   }
 
   MOZ_MUST_USE bool WriteU64(uint64_t aLongLong) {
     uint8_t c[8];
-    mozilla::BigEndian::writeUint64(&c[0], aLongLong);
+    Endianess::writeUint64(&c[0], aLongLong);
     return Write(&c[0], 8);
   }
 
   MOZ_MUST_USE bool Write64(int64_t aLongLong) {
     uint8_t c[8];
-    mozilla::BigEndian::writeInt64(&c[0], aLongLong);
+    Endianess::writeInt64(&c[0], aLongLong);
     return Write(&c[0], 8);
   }
 
   MOZ_MUST_USE bool Write(const uint8_t* aSrc, size_t aCount) {
     return mPtr.AppendElements(aSrc, aCount, mozilla::fallible);
   }
 
  private:
--- a/dom/media/platforms/agnostic/bytestreams/AnnexB.cpp
+++ b/dom/media/platforms/agnostic/bytestreams/AnnexB.cpp
@@ -29,17 +29,17 @@ Result<Ok, nsresult> AnnexB::ConvertSamp
   if (aSample->Size() < 4) {
     // Nothing to do, it's corrupted anyway.
     return Ok();
   }
 
   BufferReader reader(aSample->Data(), aSample->Size());
 
   nsTArray<uint8_t> tmp;
-  ByteWriter writer(tmp);
+  ByteWriter<BigEndian> writer(tmp);
 
   while (reader.Remaining() >= 4) {
     uint32_t nalLen;
     MOZ_TRY_VAR(nalLen, reader.ReadU32());
     const uint8_t* p = reader.Read(nalLen);
 
     if (!writer.Write(kAnnexBDelimiter, ArrayLength(kAnnexBDelimiter))) {
       return Err(NS_ERROR_OUT_OF_MEMORY);
@@ -200,17 +200,17 @@ static Result<Ok, nsresult> FindStartCod
     if (data == 0) {
       aStartSize = 4;
     }
   }
   mozilla::Unused << aBr.Read(3);
   return Ok();
 }
 
-static Result<mozilla::Ok, nsresult> ParseNALUnits(ByteWriter& aBw,
+static Result<mozilla::Ok, nsresult> ParseNALUnits(ByteWriter<BigEndian>& aBw,
                                                    BufferReader& aBr) {
   size_t startSize;
 
   auto rv = FindStartCode(aBr, startSize);
   if (rv.isOk()) {
     size_t startOffset = aBr.Offset();
     while (FindStartCode(aBr, startSize).isOk()) {
       size_t offset = aBr.Offset();
@@ -237,17 +237,17 @@ bool AnnexB::ConvertSampleToAVCC(mozilla
     return ConvertSampleTo4BytesAVCC(aSample).isOk();
   }
   if (!IsAnnexB(aSample)) {
     // Not AnnexB, nothing to convert.
     return true;
   }
 
   nsTArray<uint8_t> nalu;
-  ByteWriter writer(nalu);
+  ByteWriter<BigEndian> writer(nalu);
   BufferReader reader(aSample->Data(), aSample->Size());
 
   if (ParseNALUnits(writer, reader).isErr()) {
     return false;
   }
   UniquePtr<MediaRawDataWriter> samplewriter(aSample->CreateWriter());
   if (!samplewriter->Replace(nalu.Elements(), nalu.Length())) {
     return false;
@@ -275,17 +275,17 @@ Result<mozilla::Ok, nsresult> AnnexB::Co
   MOZ_ASSERT(IsAVCC(aSample));
 
   int nalLenSize = ((*aSample->mExtraData)[4] & 3) + 1;
 
   if (nalLenSize == 4) {
     return Ok();
   }
   nsTArray<uint8_t> dest;
-  ByteWriter writer(dest);
+  ByteWriter<BigEndian> writer(dest);
   BufferReader reader(aSample->Data(), aSample->Size());
   while (reader.Remaining() > nalLenSize) {
     uint32_t nalLen;
     switch (nalLenSize) {
       case 1:
         MOZ_TRY_VAR(nalLen, reader.ReadU8());
         break;
       case 2:
--- a/dom/media/platforms/agnostic/bytestreams/H264.cpp
+++ b/dom/media/platforms/agnostic/bytestreams/H264.cpp
@@ -993,21 +993,21 @@ uint32_t H264::ComputeMaxRefFrames(const
 /* static */ already_AddRefed<mozilla::MediaByteBuffer> H264::ExtractExtraData(
     const mozilla::MediaRawData* aSample) {
   MOZ_ASSERT(AnnexB::IsAVCC(aSample));
 
   RefPtr<mozilla::MediaByteBuffer> extradata = new mozilla::MediaByteBuffer;
 
   // SPS content
   nsTArray<uint8_t> sps;
-  ByteWriter spsw(sps);
+  ByteWriter<BigEndian> spsw(sps);
   int numSps = 0;
   // PPS content
   nsTArray<uint8_t> pps;
-  ByteWriter ppsw(pps);
+  ByteWriter<BigEndian> ppsw(pps);
   int numPps = 0;
 
   int nalLenSize = ((*aSample->mExtraData)[4] & 3) + 1;
 
   size_t sampleSize = aSample->Size();
   if (aSample->mCrypto.IsEncrypted()) {
     // The content is encrypted, we can only parse the non-encrypted data.
     MOZ_ASSERT(aSample->mCrypto.mPlainSizes.Length() > 0);