tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AnnexB.h (4150B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef DOM_MEDIA_PLATFORMS_AGNOSTIC_BYTESTREAMS_ANNEX_B_H_
      6 #define DOM_MEDIA_PLATFORMS_AGNOSTIC_BYTESTREAMS_ANNEX_B_H_
      7 
      8 #include "ErrorList.h"
      9 #include "H264.h"
     10 #include "mozilla/RefPtr.h"
     11 #include "mozilla/Result.h"
     12 
     13 template <class>
     14 class nsTArray;
     15 
     16 namespace mozilla {
     17 class BufferReader;
     18 class MediaRawData;
     19 class MediaByteBuffer;
     20 
     21 class AnnexB {
     22 public:
     23  struct NALEntry {
     24    NALEntry(int64_t aOffset, int64_t aSize) : mOffset(aOffset), mSize(aSize) {
     25      MOZ_ASSERT(mOffset >= 0);
     26      MOZ_ASSERT(mSize >= 0);
     27    }
     28    // They should be non-negative, so we use int64_t to assert their value when
     29    // assigning value to them.
     30    int64_t mOffset;
     31    int64_t mSize;
     32  };
     33  // All conversions assume size of NAL length field is 4 bytes.
     34  // Convert a sample from AVCC format to Annex B.
     35  static mozilla::Result<mozilla::Ok, nsresult> ConvertAVCCSampleToAnnexB(
     36      mozilla::MediaRawData* aSample, bool aAddSPS = true);
     37  // All conversions assume size of NAL length field is 4 bytes.
     38  // Convert a sample from HVCC format to Annex B.
     39  static mozilla::Result<mozilla::Ok, nsresult> ConvertHVCCSampleToAnnexB(
     40      mozilla::MediaRawData* aSample, bool aAddSPS = true);
     41 
     42  // Extract extradata for AVCC from an Annex B sample.
     43  static RefPtr<MediaByteBuffer> ExtractExtraDataForAVCC(
     44      const Span<const uint8_t>& aSpan);
     45  // Convert a sample from Annex B to AVCC.
     46  // an AVCC extradata must not be set.
     47  static bool ConvertSampleToAVCC(
     48      mozilla::MediaRawData* aSample,
     49      const RefPtr<mozilla::MediaByteBuffer>& aAVCCHeader = nullptr);
     50  // Convert a sample from Annex B to HVCC. An HVCC extradata must not be set.
     51  static Result<mozilla::Ok, nsresult> ConvertSampleToHVCC(
     52      mozilla::MediaRawData* aSample);
     53 
     54  // Covert sample to 4 bytes NALU byte stream.
     55  static mozilla::Result<mozilla::Ok, nsresult> ConvertAVCCTo4BytesAVCC(
     56      mozilla::MediaRawData* aSample);
     57  static mozilla::Result<mozilla::Ok, nsresult> ConvertHVCCTo4BytesHVCC(
     58      mozilla::MediaRawData* aSample);
     59 
     60  // Parse an AVCC extradata and construct the Annex B sample header.
     61  static already_AddRefed<mozilla::MediaByteBuffer>
     62  ConvertAVCCExtraDataToAnnexB(const mozilla::MediaByteBuffer* aExtraData);
     63  // Parse a HVCC extradata and construct the Annex B sample header.
     64  static already_AddRefed<mozilla::MediaByteBuffer>
     65  ConvertHVCCExtraDataToAnnexB(const mozilla::MediaByteBuffer* aExtraData);
     66 
     67  // Returns true if format is AVCC and sample has valid extradata.
     68  static bool IsAVCC(const mozilla::MediaRawData* aSample);
     69  // Returns true if format is HVCC and sample has valid extradata.
     70  static bool IsHVCC(const mozilla::MediaRawData* aSample);
     71  // Returns true if format is AnnexB.
     72  static bool IsAnnexB(const Span<const uint8_t>& aSpan);
     73 
     74  // Parse NAL entries from the bytes stream to know the offset and the size of
     75  // each NAL in the bytes stream.
     76  static void ParseNALEntries(const Span<const uint8_t>& aSpan,
     77                              nsTArray<AnnexB::NALEntry>& aEntries);
     78 
     79  // Parse NAL entries and check if the NAL_TYPES are all present in data
     80  static bool FindAllNalTypes(const Span<const uint8_t>& aSpan,
     81                              const nsTArray<NAL_TYPES>& aTypes);
     82 
     83 private:
     84  static size_t FindNalType(const Span<const uint8_t>& aSpan,
     85                            const nsTArray<AnnexB::NALEntry>& aNalEntries,
     86                            NAL_TYPES aType, size_t aStartIndex);
     87 
     88  // AVCC box parser helper.
     89  static mozilla::Result<mozilla::Ok, nsresult> ConvertSPSOrPPS(
     90      mozilla::BufferReader& aReader, uint8_t aCount,
     91      mozilla::MediaByteBuffer* aAnnexB);
     92 
     93  // NALU size can vary, this function converts the sample to always 4 bytes
     94  // NALU.
     95  static mozilla::Result<mozilla::Ok, nsresult> ConvertNALUTo4BytesNALU(
     96      mozilla::MediaRawData* aSample, uint8_t aNALUSize);
     97 };
     98 
     99 }  // namespace mozilla
    100 
    101 #endif  // DOM_MEDIA_PLATFORMS_AGNOSTIC_BYTESTREAMS_ANNEX_B_H_