[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 | * |
---|
| 6 | * Copyright (c) 2010-2012, 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 | #include <vector> |
---|
| 35 | #include <algorithm> |
---|
| 36 | #include <ostream> |
---|
| 37 | |
---|
| 38 | #include "NALread.h" |
---|
| 39 | #include "TLibCommon/NAL.h" |
---|
| 40 | #include "TLibCommon/TComBitStream.h" |
---|
| 41 | |
---|
| 42 | using namespace std; |
---|
| 43 | |
---|
| 44 | //! \ingroup TLibDecoder |
---|
| 45 | //! \{ |
---|
| 46 | static void convertPayloadToRBSP(vector<uint8_t>& nalUnitBuf, TComInputBitstream *pcBitstream) |
---|
| 47 | { |
---|
| 48 | unsigned zeroCount = 0; |
---|
| 49 | vector<uint8_t>::iterator it_read, it_write; |
---|
| 50 | |
---|
| 51 | UInt *auiStoredTileMarkerLocation = new UInt[MAX_MARKER_PER_NALU]; |
---|
| 52 | // Remove tile markers and note the bitstream location |
---|
| 53 | for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++ ) |
---|
| 54 | { |
---|
| 55 | Bool bTileMarkerFound = false; |
---|
| 56 | if ( ( it_read - nalUnitBuf.begin() ) < ( nalUnitBuf.size() - 2 ) ) |
---|
| 57 | { |
---|
| 58 | if ( (*(it_read) == 0x00) && (*(it_read+1) == 0x00) && (*(it_read+2) == 0x02) ) // tile marker detected |
---|
| 59 | { |
---|
| 60 | it_read += 2; |
---|
| 61 | UInt uiDistance = (UInt) (it_write - nalUnitBuf.begin()); |
---|
| 62 | UInt uiCount = pcBitstream->getTileMarkerLocationCount(); |
---|
| 63 | bTileMarkerFound = true; |
---|
| 64 | pcBitstream->setTileMarkerLocation( uiCount, uiDistance ); |
---|
| 65 | auiStoredTileMarkerLocation[uiCount] = uiDistance; |
---|
| 66 | pcBitstream->setTileMarkerLocationCount( uiCount + 1 ); |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if (!bTileMarkerFound) |
---|
| 72 | { |
---|
| 73 | *it_write = *it_read; |
---|
| 74 | it_write++; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | nalUnitBuf.resize(it_write - nalUnitBuf.begin()); |
---|
| 78 | |
---|
| 79 | for (it_read = it_write = nalUnitBuf.begin(); it_read != nalUnitBuf.end(); it_read++, it_write++) |
---|
| 80 | { |
---|
| 81 | if (zeroCount == 2 && *it_read == 0x03) |
---|
| 82 | { |
---|
| 83 | // update tile marker location |
---|
| 84 | UInt uiDistance = (UInt) (it_read - nalUnitBuf.begin()); |
---|
| 85 | for (UInt uiIdx=0; uiIdx<pcBitstream->getTileMarkerLocationCount(); uiIdx++) |
---|
| 86 | { |
---|
| 87 | if (auiStoredTileMarkerLocation[ uiIdx ] >= uiDistance) |
---|
| 88 | { |
---|
| 89 | pcBitstream->setTileMarkerLocation( uiIdx, pcBitstream->getTileMarkerLocation( uiIdx )-1 ); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | it_read++; |
---|
| 93 | zeroCount = 0; |
---|
| 94 | } |
---|
| 95 | zeroCount = (*it_read == 0x00) ? zeroCount+1 : 0; |
---|
| 96 | *it_write = *it_read; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | nalUnitBuf.resize(it_write - nalUnitBuf.begin()); |
---|
| 100 | delete [] auiStoredTileMarkerLocation; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * create a NALunit structure with given header values and storage for |
---|
| 105 | * a bitstream |
---|
| 106 | */ |
---|
| 107 | void read(InputNALUnit& nalu, vector<uint8_t>& nalUnitBuf) |
---|
| 108 | { |
---|
| 109 | /* perform anti-emulation prevention */ |
---|
| 110 | TComInputBitstream *pcBitstream = new TComInputBitstream(NULL); |
---|
| 111 | convertPayloadToRBSP(nalUnitBuf, pcBitstream); |
---|
| 112 | |
---|
| 113 | nalu.m_Bitstream = new TComInputBitstream(&nalUnitBuf); |
---|
| 114 | // copy the tile marker location information |
---|
| 115 | nalu.m_Bitstream->setTileMarkerLocationCount( pcBitstream->getTileMarkerLocationCount() ); |
---|
| 116 | for (UInt uiIdx=0; uiIdx < nalu.m_Bitstream->getTileMarkerLocationCount(); uiIdx++) |
---|
| 117 | { |
---|
| 118 | nalu.m_Bitstream->setTileMarkerLocation( uiIdx, pcBitstream->getTileMarkerLocation(uiIdx) ); |
---|
| 119 | } |
---|
| 120 | delete pcBitstream; |
---|
| 121 | TComInputBitstream& bs = *nalu.m_Bitstream; |
---|
| 122 | |
---|
| 123 | bool forbidden_zero_bit = bs.read(1); |
---|
| 124 | assert(forbidden_zero_bit == 0); |
---|
| 125 | |
---|
| 126 | #if NAL_REF_FLAG |
---|
| 127 | nalu.m_nalRefFlag = (bs.read(1) != 0 ); |
---|
| 128 | nalu.m_nalUnitType = (NalUnitType) bs.read(6); |
---|
| 129 | #else |
---|
| 130 | nalu.m_nalRefIDC = (NalRefIdc) bs.read(2); |
---|
| 131 | nalu.m_nalUnitType = (NalUnitType) bs.read(5); |
---|
| 132 | #endif |
---|
| 133 | |
---|
| 134 | #if H0388 |
---|
| 135 | nalu.m_temporalId = bs.read(3); |
---|
| 136 | // unsigned reserved_one_5bits = bs.read(5); |
---|
| 137 | // assert(reserved_one_5bits == 1); |
---|
| 138 | nalu.m_viewId = bs.read(4)-1; |
---|
| 139 | nalu.m_isDepth = bs.read(1); |
---|
| 140 | |
---|
| 141 | #if H0566_TLA |
---|
| 142 | if ( nalu.m_temporalId ) |
---|
| 143 | { |
---|
| 144 | assert( nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_CRA && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDR && nalu.m_nalUnitType != NAL_UNIT_CODED_SLICE_IDV ); |
---|
| 145 | } |
---|
| 146 | #endif |
---|
| 147 | #else |
---|
| 148 | switch (nalu.m_nalUnitType) |
---|
| 149 | { |
---|
| 150 | case NAL_UNIT_CODED_SLICE: |
---|
| 151 | case NAL_UNIT_CODED_SLICE_IDR: |
---|
| 152 | #if H0566_TLA |
---|
| 153 | case NAL_UNIT_CODED_SLICE_IDV: |
---|
| 154 | case NAL_UNIT_CODED_SLICE_CRA: |
---|
| 155 | case NAL_UNIT_CODED_SLICE_TLA: |
---|
| 156 | #else |
---|
| 157 | case NAL_UNIT_CODED_SLICE_CDR: |
---|
| 158 | #endif |
---|
| 159 | { |
---|
| 160 | nalu.m_temporalId = bs.read(3); |
---|
| 161 | nalu.m_OutputFlag = bs.read(1); |
---|
| 162 | // unsigned reserved_one_4bits = bs.read(4); |
---|
| 163 | // assert(reserved_one_4bits == 1); |
---|
| 164 | nalu.m_viewId = bs.read(3)-1; |
---|
| 165 | nalu.m_isDepth = bs.read(1); |
---|
| 166 | |
---|
| 167 | #if H0566_TLA |
---|
| 168 | if (nalu.m_temporalId == 0) |
---|
| 169 | { |
---|
| 170 | assert(nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDV ); |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | assert(nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE || nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TLA); |
---|
| 175 | } |
---|
| 176 | #endif |
---|
| 177 | } |
---|
| 178 | break; |
---|
| 179 | default: |
---|
| 180 | nalu.m_temporalId = 0; |
---|
| 181 | nalu.m_OutputFlag = true; |
---|
| 182 | break; |
---|
| 183 | } |
---|
| 184 | #endif |
---|
| 185 | } |
---|
| 186 | //! \} |
---|