Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvenc/source/Lib/EncoderLib/NALwrite.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
43
44
#include "NALwrite.h"
45
#include "CommonLib/BitStream.h"
46
#include "CommonLib/Nal.h"
47
#include <vector>
48
#include <algorithm>
49
#include <ostream>
50
51
52
//! \ingroup EncoderLib
53
//! \{
54
55
namespace vvenc {
56
57
static const uint8_t emulation_prevention_three_byte = 3;
58
59
void writeNalUnitHeader(std::ostream& out, OutputNALUnit& nalu)       // nal_unit_header()
60
0
{
61
0
OutputBitstream bsNALUHeader;
62
0
  int forbiddenZero = 0;
63
0
  bsNALUHeader.write(forbiddenZero, 1);   // forbidden_zero_bit
64
0
  int nuhReservedZeroBit = 0;
65
0
  bsNALUHeader.write(nuhReservedZeroBit, 1);   // nuh_reserved_zero_bit
66
0
  CHECK(nalu.m_nuhLayerId > 63, "nuh_layer_id > 63");
67
0
  bsNALUHeader.write(nalu.m_nuhLayerId, 6);       // nuh_layer_id
68
0
  bsNALUHeader.write(nalu.m_nalUnitType, 5);      // nal_unit_type
69
0
  bsNALUHeader.write(nalu.m_temporalId + 1, 3);   // nuh_temporal_id_plus1
70
71
0
  out.write(reinterpret_cast<const char*>(bsNALUHeader.getByteStream()), bsNALUHeader.getByteStreamLength());
72
0
}
73
/**
74
 * write nalu to bytestream out, performing RBSP anti startcode
75
 * emulation as required.  nalu.m_RBSPayload must be byte aligned.
76
 */
77
void write(std::ostream& out, OutputNALUnit& nalu)
78
0
{
79
0
  writeNalUnitHeader(out, nalu);
80
  /* write out rsbp_byte's, inserting any required
81
   * emulation_prevention_three_byte's */
82
  /* 7.4.1 ...
83
   * emulation_prevention_three_byte is a byte equal to 0x03. When an
84
   * emulation_prevention_three_byte is present in the NAL unit, it shall be
85
   * discarded by the decoding process.
86
   * The last byte of the NAL unit shall not be equal to 0x00.
87
   * Within the NAL unit, the following three-byte sequences shall not occur at
88
   * any byte-aligned position:
89
   *  - 0x000000
90
   *  - 0x000001
91
   *  - 0x000002
92
   * Within the NAL unit, any four-byte sequence that starts with 0x000003
93
   * other than the following sequences shall not occur at any byte-aligned
94
   * position:
95
   *  - 0x00000300
96
   *  - 0x00000301
97
   *  - 0x00000302
98
   *  - 0x00000303
99
   */
100
0
  std::vector<uint8_t>& rbsp   = nalu.m_Bitstream.getFIFO();
101
102
0
  std::vector<uint8_t> outputBuffer;
103
0
  outputBuffer.resize(rbsp.size()*2+1); //there can never be enough emulation_prevention_three_bytes to require this much space
104
0
  std::size_t outputAmount = 0;
105
0
  int         zeroCount    = 0;
106
0
  for (std::vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end(); it++)
107
0
  {
108
0
    const uint8_t v=(*it);
109
0
    if (zeroCount==2 && v<=3)
110
0
    {
111
0
      outputBuffer[outputAmount++]=emulation_prevention_three_byte;
112
0
      zeroCount=0;
113
0
    }
114
115
0
    if (v==0)
116
0
    {
117
0
      zeroCount++;
118
0
    }
119
0
    else
120
0
    {
121
0
      zeroCount=0;
122
0
    }
123
0
    outputBuffer[outputAmount++]=v;
124
0
  }
125
126
  /* 7.4.1.1
127
   * ... when the last byte of the RBSP data is equal to 0x00 (which can
128
   * only occur when the RBSP ends in a cabac_zero_word), a final byte equal
129
   * to 0x03 is appended to the end of the data.
130
   */
131
0
  if (zeroCount>0)
132
0
  {
133
0
    outputBuffer[outputAmount++]=emulation_prevention_three_byte;
134
0
  }
135
0
  out.write(reinterpret_cast<const char*>(&(*outputBuffer.begin())), outputAmount);
136
0
}
137
138
} // namespace vvenc
139
140
//! \}
141