[56] | 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 | * |
---|
[1200] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[56] | 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 | |
---|
[608] | 34 | /** |
---|
| 35 | \file NALread.cpp |
---|
[1200] | 36 | \brief reading functionality for NAL units |
---|
[608] | 37 | */ |
---|
| 38 | |
---|
| 39 | |
---|
[56] | 40 | #include <vector> |
---|
| 41 | #include <algorithm> |
---|
| 42 | #include <ostream> |
---|
| 43 | |
---|
| 44 | #include "NALread.h" |
---|
| 45 | #include "TLibCommon/NAL.h" |
---|
| 46 | #include "TLibCommon/TComBitStream.h" |
---|
[1200] | 47 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 48 | #include "TLibCommon/TComCodingStatistics.h" |
---|
| 49 | #endif |
---|
| 50 | #if ENC_DEC_TRACE && DEC_NUH_TRACE |
---|
| 51 | #include "TLibCommon/TComRom.h" |
---|
| 52 | #endif |
---|
[56] | 53 | |
---|
| 54 | using namespace std; |
---|
| 55 | |
---|
| 56 | //! \ingroup TLibDecoder |
---|
| 57 | //! \{ |
---|
[1200] | 58 | static Void convertPayloadToRBSP(vector<uint8_t>& nalUnitBuf, TComInputBitstream *bitstream, Bool isVclNalUnit) |
---|
[56] | 59 | { |
---|
[608] | 60 | UInt zeroCount = 0; |
---|
[56] | 61 | vector<uint8_t>::iterator it_read, it_write; |
---|
| 62 | |
---|
[608] | 63 | UInt pos = 0; |
---|
| 64 | bitstream->clearEmulationPreventionByteLocation(); |
---|
| 65 | for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++, it_write++, pos++) |
---|
[56] | 66 | { |
---|
[608] | 67 | assert(zeroCount < 2 || *it_read >= 0x03); |
---|
| 68 | if (zeroCount == 2 && *it_read == 0x03) |
---|
[56] | 69 | { |
---|
[608] | 70 | bitstream->pushEmulationPreventionByteLocation( pos ); |
---|
| 71 | pos++; |
---|
| 72 | it_read++; |
---|
| 73 | zeroCount = 0; |
---|
[1200] | 74 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 75 | TComCodingStatistics::IncrementStatisticEP(STATS__EMULATION_PREVENTION_3_BYTES, 8, 0); |
---|
| 76 | #endif |
---|
[608] | 77 | if (it_read == nalUnitBuf.end()) |
---|
[56] | 78 | { |
---|
[608] | 79 | break; |
---|
[56] | 80 | } |
---|
[872] | 81 | assert(*it_read <= 0x03); |
---|
[56] | 82 | } |
---|
[608] | 83 | zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0; |
---|
| 84 | *it_write = *it_read; |
---|
[56] | 85 | } |
---|
[608] | 86 | assert(zeroCount == 0); |
---|
[1200] | 87 | |
---|
[608] | 88 | if (isVclNalUnit) |
---|
[56] | 89 | { |
---|
[608] | 90 | // Remove cabac_zero_word from payload if present |
---|
| 91 | Int n = 0; |
---|
[1200] | 92 | |
---|
[608] | 93 | while (it_write[-1] == 0x00) |
---|
[56] | 94 | { |
---|
[608] | 95 | it_write--; |
---|
| 96 | n++; |
---|
[56] | 97 | } |
---|
[1200] | 98 | |
---|
[608] | 99 | if (n > 0) |
---|
| 100 | { |
---|
[1200] | 101 | printf("\nDetected %d instances of cabac_zero_word\n", n/2); |
---|
[608] | 102 | } |
---|
[56] | 103 | } |
---|
| 104 | |
---|
| 105 | nalUnitBuf.resize(it_write - nalUnitBuf.begin()); |
---|
| 106 | } |
---|
| 107 | |
---|
[1200] | 108 | #if ENC_DEC_TRACE && DEC_NUH_TRACE |
---|
| 109 | void xTraceNalUnitHeader(InputNALUnit& nalu) |
---|
| 110 | { |
---|
| 111 | fprintf( g_hTrace, "*********** NAL UNIT (%s) ***********\n", nalUnitTypeToString(nalu.m_nalUnitType) ); |
---|
| 112 | |
---|
| 113 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
| 114 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "forbidden_zero_bit", 1, 0 ); |
---|
| 115 | |
---|
| 116 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
| 117 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "nal_unit_type", 6, nalu.m_nalUnitType ); |
---|
| 118 | |
---|
| 119 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
| 120 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "nuh_layer_id", 6, nalu.m_nuhLayerId ); |
---|
| 121 | |
---|
| 122 | fprintf( g_hTrace, "%8lld ", g_nSymbolCounter++ ); |
---|
| 123 | fprintf( g_hTrace, "%-50s u(%d) : %u\n", "nuh_temporal_id_plus1", 3, nalu.m_temporalId + 1 ); |
---|
| 124 | |
---|
| 125 | fflush ( g_hTrace ); |
---|
| 126 | } |
---|
| 127 | #endif |
---|
| 128 | |
---|
[608] | 129 | Void readNalUnitHeader(InputNALUnit& nalu) |
---|
| 130 | { |
---|
[1200] | 131 | TComInputBitstream& bs = nalu.getBitstream(); |
---|
[608] | 132 | |
---|
| 133 | Bool forbidden_zero_bit = bs.read(1); // forbidden_zero_bit |
---|
| 134 | assert(forbidden_zero_bit == 0); |
---|
| 135 | nalu.m_nalUnitType = (NalUnitType) bs.read(6); // nal_unit_type |
---|
[1200] | 136 | #if NH_MV |
---|
| 137 | nalu.m_nuhLayerId = bs.read(6); // layerId |
---|
[608] | 138 | #else |
---|
[1200] | 139 | nalu.m_nuhLayerId = bs.read(6); // nuh_layer_id |
---|
[608] | 140 | #endif |
---|
| 141 | nalu.m_temporalId = bs.read(3) - 1; // nuh_temporal_id_plus1 |
---|
[1200] | 142 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 143 | TComCodingStatistics::IncrementStatisticEP(STATS__NAL_UNIT_HEADER_BITS, 1+6+6+3, 0); |
---|
| 144 | #endif |
---|
[608] | 145 | |
---|
[1200] | 146 | #if ENC_DEC_TRACE && DEC_NUH_TRACE |
---|
| 147 | xTraceNalUnitHeader(nalu); |
---|
| 148 | #endif |
---|
| 149 | |
---|
| 150 | // only check these rules for base layer |
---|
| 151 | if (nalu.m_nuhLayerId == 0) |
---|
[608] | 152 | { |
---|
[1200] | 153 | if ( nalu.m_temporalId ) |
---|
| 154 | { |
---|
| 155 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_LP |
---|
| 156 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_W_RADL |
---|
| 157 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_BLA_N_LP |
---|
| 158 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_W_RADL |
---|
| 159 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR_N_LP |
---|
| 160 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA |
---|
| 161 | && nalu.m_nalUnitType != NAL_UNIT_VPS |
---|
| 162 | && nalu.m_nalUnitType != NAL_UNIT_SPS |
---|
| 163 | && nalu.m_nalUnitType != NAL_UNIT_EOS |
---|
| 164 | && nalu.m_nalUnitType != NAL_UNIT_EOB ); |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | { |
---|
| 168 | #if NH_MV |
---|
[964] | 169 | // If nal_unit_type is in the range of BLA_W_LP to RSV_IRAP_VCL23, inclusive, i.e. the coded |
---|
| 170 | // slice segment belongs to an IRAP picture, TemporalId shall be equal to 0. |
---|
| 171 | // Otherwise, when nal_unit_type is equal to TSA_R, TSA_N, STSA_R, or STSA_N, TemporalId shall not be equal to 0. |
---|
| 172 | // Otherwise, if nal_unit_type is equal to TSA or TSA_N, TemporalId shall not be equal to 0. |
---|
| 173 | // Otherwise, when nuh_layer_id is equal to 0 and nal_unit_type is equal to STSA_R or STSA_N, |
---|
| 174 | // TemporalId shall not be equal to 0. |
---|
| 175 | |
---|
[872] | 176 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R |
---|
[964] | 177 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N ); |
---|
| 178 | |
---|
[1200] | 179 | assert( nalu.m_nuhLayerId > 0 |
---|
[964] | 180 | || ( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R |
---|
| 181 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N ) ); |
---|
| 182 | #else |
---|
[1200] | 183 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_R |
---|
| 184 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_TSA_N |
---|
| 185 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_R |
---|
| 186 | && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_STSA_N ); |
---|
[964] | 187 | #endif |
---|
[1200] | 188 | } |
---|
[608] | 189 | } |
---|
| 190 | } |
---|
[56] | 191 | /** |
---|
| 192 | * create a NALunit structure with given header values and storage for |
---|
| 193 | * a bitstream |
---|
| 194 | */ |
---|
[1200] | 195 | Void read(InputNALUnit& nalu) |
---|
[56] | 196 | { |
---|
[1200] | 197 | TComInputBitstream &bitstream = nalu.getBitstream(); |
---|
| 198 | vector<uint8_t>& nalUnitBuf=bitstream.getFifo(); |
---|
| 199 | // perform anti-emulation prevention |
---|
| 200 | convertPayloadToRBSP(nalUnitBuf, &bitstream, (nalUnitBuf[0] & 64) == 0); |
---|
| 201 | bitstream.resetToStart(); |
---|
[608] | 202 | readNalUnitHeader(nalu); |
---|
[56] | 203 | } |
---|
| 204 | //! \} |
---|