| 1 | /* The copyright in this software is being made available under the BSD |
|---|
| 2 | * License, included below. This software may be subject to other third party |
|---|
| 3 | * and contributor rights, including patent rights, and no such rights are |
|---|
| 4 | * granted under this license. |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) 2010-2013, ITU/ISO/IEC |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * Redistribution and use in source and binary forms, with or without |
|---|
| 10 | * modification, are permitted provided that the following conditions are met: |
|---|
| 11 | * |
|---|
| 12 | * * Redistributions of source code must retain the above copyright notice, |
|---|
| 13 | * this list of conditions and the following disclaimer. |
|---|
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 15 | * this list of conditions and the following disclaimer in the documentation |
|---|
| 16 | * and/or other materials provided with the distribution. |
|---|
| 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
|---|
| 18 | * be used to endorse or promote products derived from this software without |
|---|
| 19 | * specific prior written permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|---|
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|---|
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|---|
| 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | \file NALread.cpp |
|---|
| 36 | \brief reading funtionality for NAL units |
|---|
| 37 | */ |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | #include <vector> |
|---|
| 41 | #include <algorithm> |
|---|
| 42 | #include <ostream> |
|---|
| 43 | |
|---|
| 44 | #include "NALread.h" |
|---|
| 45 | #include "TLibCommon/NAL.h" |
|---|
| 46 | #include "TLibCommon/TComBitStream.h" |
|---|
| 47 | |
|---|
| 48 | using namespace std; |
|---|
| 49 | |
|---|
| 50 | //! \ingroup TLibDecoder |
|---|
| 51 | //! \{ |
|---|
| 52 | static void convertPayloadToRBSP(vector<uint8_t>& nalUnitBuf, Bool isVclNalUnit) |
|---|
| 53 | { |
|---|
| 54 | UInt zeroCount = 0; |
|---|
| 55 | vector<uint8_t>::iterator it_read, it_write; |
|---|
| 56 | |
|---|
| 57 | for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++, it_write++) |
|---|
| 58 | { |
|---|
| 59 | assert(zeroCount < 2 || *it_read >= 0x03); |
|---|
| 60 | if (zeroCount == 2 && *it_read == 0x03) |
|---|
| 61 | { |
|---|
| 62 | it_read++; |
|---|
| 63 | zeroCount = 0; |
|---|
| 64 | if (it_read == nalUnitBuf.end()) |
|---|
| 65 | { |
|---|
| 66 | break; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0; |
|---|
| 70 | *it_write = *it_read; |
|---|
| 71 | } |
|---|
| 72 | assert(zeroCount == 0); |
|---|
| 73 | |
|---|
| 74 | if (isVclNalUnit) |
|---|
| 75 | { |
|---|
| 76 | // Remove cabac_zero_word from payload if present |
|---|
| 77 | Int n = 0; |
|---|
| 78 | |
|---|
| 79 | while (it_write[-1] == 0x00) |
|---|
| 80 | { |
|---|
| 81 | it_write--; |
|---|
| 82 | n++; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if (n > 0) |
|---|
| 86 | { |
|---|
| 87 | printf("\nDetected %d instances of cabac_zero_word", n/2); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | nalUnitBuf.resize(it_write - nalUnitBuf.begin()); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | Void readNalUnitHeader(InputNALUnit& nalu) |
|---|
| 95 | { |
|---|
| 96 | TComInputBitstream& bs = *nalu.m_Bitstream; |
|---|
| 97 | |
|---|
| 98 | Bool forbidden_zero_bit = bs.read(1); // forbidden_zero_bit |
|---|
| 99 | assert(forbidden_zero_bit == 0); |
|---|
| 100 | nalu.m_nalUnitType = (NalUnitType) bs.read(6); // nal_unit_type |
|---|
| 101 | nalu.m_reservedZero6Bits = bs.read(6); // nuh_reserved_zero_6bits |
|---|
| 102 | #if SVC_EXTENSION |
|---|
| 103 | nalu.m_layerId = nalu.m_reservedZero6Bits; |
|---|
| 104 | #else |
|---|
| 105 | assert(nalu.m_reservedZero6Bits == 0); |
|---|
| 106 | #endif |
|---|
| 107 | nalu.m_temporalId = bs.read(3) - 1; // nuh_temporal_id_plus1 |
|---|
| 108 | |
|---|
| 109 | if ( nalu.m_temporalId ) |
|---|
| 110 | { |
|---|
| 111 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA |
|---|
| 112 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLANT |
|---|
| 113 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP |
|---|
| 114 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR |
|---|
| 115 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP |
|---|
| 116 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA |
|---|
| 117 | && nalu.m_nalUnitType != NAL_UNIT_VPS |
|---|
| 118 | && nalu.m_nalUnitType != NAL_UNIT_SPS |
|---|
| 119 | && nalu.m_nalUnitType != NAL_UNIT_EOS |
|---|
| 120 | && nalu.m_nalUnitType != NAL_UNIT_EOB ); |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TLA |
|---|
| 125 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N |
|---|
| 126 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R |
|---|
| 127 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N ); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | /** |
|---|
| 131 | * create a NALunit structure with given header values and storage for |
|---|
| 132 | * a bitstream |
|---|
| 133 | */ |
|---|
| 134 | void read(InputNALUnit& nalu, vector<uint8_t>& nalUnitBuf) |
|---|
| 135 | { |
|---|
| 136 | /* perform anti-emulation prevention */ |
|---|
| 137 | TComInputBitstream *pcBitstream = new TComInputBitstream(NULL); |
|---|
| 138 | convertPayloadToRBSP(nalUnitBuf, (nalUnitBuf[0] & 64) == 0); |
|---|
| 139 | |
|---|
| 140 | nalu.m_Bitstream = new TComInputBitstream(&nalUnitBuf); |
|---|
| 141 | delete pcBitstream; |
|---|
| 142 | readNalUnitHeader(nalu); |
|---|
| 143 | } |
|---|
| 144 | //! \} |
|---|