content/media/omx/OMXCodecDescriptorUtil.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "OMXCodecDescriptorUtil.h"
     8 namespace android {
    10 // The utility functions in this file concatenate two AVC/H.264 parameter sets,
    11 // sequence parameter set(SPS) and picture parameter set(PPS), into byte stream
    12 // format or construct AVC decoder config descriptor blob from them.
    13 //
    14 // * NAL unit defined in ISO/IEC 14496-10 7.3.1
    15 // * SPS defined ISO/IEC 14496-10 7.3.2.1.1
    16 // * PPS defined in ISO/IEC 14496-10 7.3.2.2
    17 //
    18 // Byte stream format:
    19 // Start code <0x00 0x00 0x00 0x01>  (4 bytes)
    20 // --- (SPS) NAL unit ---
    21 //   ...                             (3 bits)
    22 //   NAL unit type <0x07>            (5 bits)
    23 //   SPS                             (3+ bytes)
    24 //     Profile                         (1 byte)
    25 //     Compatible profiles             (1 byte)
    26 //     Level                           (1 byte)
    27 //   ...
    28 // --- End ---
    29 // Start code <0x00 0x00 0x00 0x01>  (4 bytes)
    30 // --- (PPS) NAL unit ---
    31 //   ...                             (3 bits)
    32 //   NAL unit type <0x08>            (5 bits)
    33 //   PPS                             (1+ bytes)
    34 //   ...
    35 // --- End ---
    36 //
    37 // Descriptor format: (defined in ISO/IEC 14496-15 5.2.4.1.1)
    38 // --- Header (5 bytes) ---
    39 //   Version <0x01>       (1 byte)
    40 //   Profile              (1 byte)
    41 //   Compatible profiles  (1 byte)
    42 //   Level                (1 byte)
    43 //   Reserved <111111>    (6 bits)
    44 //   NAL length type      (2 bits)
    45 // --- Parameter sets ---
    46 //   Reserved <111>       (3 bits)
    47 //   Number of SPS        (5 bits)
    48 //   SPS                  (3+ bytes)
    49 //     Length               (2 bytes)
    50 //     SPS NAL unit         (1+ bytes)
    51 //   ...
    52 //   Number of PPS        (1 byte)
    53 //   PPS                  (3+ bytes)
    54 //     Length               (2 bytes)
    55 //     PPS NAL unit         (1+ bytes)
    56 //   ...
    57 // --- End ---
    59 // NAL unit start code.
    60 static const uint8_t kNALUnitStartCode[] = { 0x00, 0x00, 0x00, 0x01 };
    62 // NAL unit types.
    63 enum {
    64   kNALUnitTypeSPS = 0x07,   // Value for sequence parameter set.
    65   kNALUnitTypePPS = 0x08,   // Value for picture parameter set.
    66   kNALUnitTypeBad = -1,     // Malformed data.
    67 };
    69 // Sequence parameter set or picture parameter set.
    70 struct AVCParamSet {
    71   AVCParamSet(const uint8_t* aPtr, const size_t aSize)
    72     : mPtr(aPtr)
    73     , mSize(aSize)
    74   {
    75     MOZ_ASSERT(mPtr && mSize > 0);
    76   }
    78   size_t Size() {
    79     return mSize + 2; // 2 more bytes for length value.
    80   }
    82   // Append 2 bytes length value and NAL unit bitstream to aOutputBuf.
    83   void AppendTo(nsTArray<uint8_t>* aOutputBuf)
    84   {
    85     // 2 bytes length value.
    86     uint8_t size[] = {
    87       (mSize & 0xFF00) >> 8, // MSB.
    88       mSize & 0x00FF,        // LSB.
    89     };
    90     aOutputBuf->AppendElements(size, sizeof(size));
    92     aOutputBuf->AppendElements(mPtr, mSize);
    93   }
    95   const uint8_t* mPtr; // Pointer to NAL unit.
    96   const size_t mSize;  // NAL unit length in bytes.
    97 };
    99 // Convert SPS and PPS data into decoder config descriptor blob. The generated
   100 // blob will be appended to aOutputBuf.
   101 static status_t
   102 ConvertParamSetsToDescriptorBlob(sp<ABuffer>& aSPS, sp<ABuffer>& aPPS,
   103                                  nsTArray<uint8_t>* aOutputBuf)
   104 {
   105   // Strip start code in the input.
   106   AVCParamSet sps(aSPS->data() + sizeof(kNALUnitStartCode),
   107                   aSPS->size() - sizeof(kNALUnitStartCode));
   108   AVCParamSet pps(aPPS->data() + sizeof(kNALUnitStartCode),
   109                   aPPS->size() - sizeof(kNALUnitStartCode));
   110   size_t paramSetsSize = sps.Size() + pps.Size();
   112   // Profile/level info in SPS.
   113   uint8_t* info = aSPS->data() + 5;
   115   uint8_t header[] = {
   116     0x01,     // Version.
   117     info[0],  // Profile.
   118     info[1],  // Compatible profiles.
   119     info[2],  // Level.
   120     0xFF,     // 6 bits reserved value <111111> + 2 bits NAL length type <11>
   121   };
   123   // Reserve 1 byte for number of SPS & another 1 for number of PPS.
   124   aOutputBuf->SetCapacity(sizeof(header) + paramSetsSize + 2);
   125   // Build the blob.
   126   aOutputBuf->AppendElements(header, sizeof(header)); // 5 bytes Header.
   127   aOutputBuf->AppendElement(0xE0 | 1); // 3 bits <111> + 5 bits number of SPS.
   128   sps.AppendTo(aOutputBuf); // SPS NALU data.
   129   aOutputBuf->AppendElement(1); // 1 byte number of PPS.
   130   pps.AppendTo(aOutputBuf); // PPS NALU data.
   132   return OK;
   133 }
   135 static int
   136 NALType(sp<ABuffer>& aBuffer)
   137 {
   138   if (aBuffer == nullptr) {
   139     return kNALUnitTypeBad;
   140   }
   141   // Start code?
   142   uint8_t* data = aBuffer->data();
   143   if (aBuffer->size() <= 4 ||
   144       memcmp(data, kNALUnitStartCode, sizeof(kNALUnitStartCode))) {
   145     return kNALUnitTypeBad;
   146   }
   148   return data[4] & 0x1F;
   149 }
   151 // Generate AVC/H.264 decoder config blob.
   152 // See MPEG4Writer::Track::makeAVCCodecSpecificData() and
   153 // MPEG4Writer::Track::writeAvccBox() implementation in libstagefright.
   154 status_t
   155 GenerateAVCDescriptorBlob(sp<AMessage>& aConfigData,
   156                           nsTArray<uint8_t>* aOutputBuf,
   157                           OMXVideoEncoder::BlobFormat aFormat)
   158 {
   159   // Search for parameter sets using key "csd-0" and "csd-1".
   160   char key[6] = "csd-";
   161   sp<ABuffer> sps;
   162   sp<ABuffer> pps;
   163   for (int i = 0; i < 2; i++) {
   164     snprintf(key + 4, 2, "%d", i);
   165     sp<ABuffer> paramSet;
   166     bool found = aConfigData->findBuffer(key, &paramSet);
   167     int type = NALType(paramSet);
   168     bool valid = ((type == kNALUnitTypeSPS) || (type == kNALUnitTypePPS));
   170     MOZ_ASSERT(found && valid);
   171     if (!found || !valid) {
   172       return ERROR_MALFORMED;
   173     }
   175     switch (type) {
   176       case kNALUnitTypeSPS:
   177         sps = paramSet;
   178         break;
   179       case kNALUnitTypePPS:
   180         pps = paramSet;
   181         break;
   182       default:
   183         NS_NOTREACHED("Should not get here!");
   184     }
   185   }
   187   MOZ_ASSERT(sps != nullptr && pps != nullptr);
   188   if (sps == nullptr || pps == nullptr) {
   189     return ERROR_MALFORMED;
   190   }
   192   status_t result = OK;
   193   if (aFormat == OMXVideoEncoder::BlobFormat::AVC_NAL) {
   194     // SPS + PPS.
   195     aOutputBuf->AppendElements(sps->data(), sps->size());
   196     aOutputBuf->AppendElements(pps->data(), pps->size());
   197     return OK;
   198   } else {
   199     status_t result = ConvertParamSetsToDescriptorBlob(sps, pps, aOutputBuf);
   200     MOZ_ASSERT(result == OK);
   201     return result;
   202   }
   203 }
   205 } // namespace android

mercurial