Coverage Report

Created: 2026-05-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
3.89k
{
61
3.89k
OutputBitstream bsNALUHeader;
62
3.89k
  int forbiddenZero = 0;
63
3.89k
  bsNALUHeader.write(forbiddenZero, 1);   // forbidden_zero_bit
64
3.89k
  int nuhReservedZeroBit = 0;
65
3.89k
  bsNALUHeader.write(nuhReservedZeroBit, 1);   // nuh_reserved_zero_bit
66
3.89k
  CHECK(nalu.m_nuhLayerId > 63, "nuh_layer_id > 63");
67
3.89k
  bsNALUHeader.write(nalu.m_nuhLayerId, 6);       // nuh_layer_id
68
3.89k
  bsNALUHeader.write(nalu.m_nalUnitType, 5);      // nal_unit_type
69
3.89k
  bsNALUHeader.write(nalu.m_temporalId + 1, 3);   // nuh_temporal_id_plus1
70
71
3.89k
  out.write(reinterpret_cast<const char*>(bsNALUHeader.getByteStream()), bsNALUHeader.getByteStreamLength());
72
3.89k
}
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
3.89k
{
79
3.89k
  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
3.89k
  std::vector<uint8_t>& rbsp   = nalu.m_Bitstream.getFIFO();
101
102
3.89k
  std::vector<uint8_t> outputBuffer;
103
3.89k
  outputBuffer.resize(rbsp.size()*2+1); //there can never be enough emulation_prevention_three_bytes to require this much space
104
3.89k
  std::size_t outputAmount = 0;
105
3.89k
  int         zeroCount    = 0;
106
251k
  for (std::vector<uint8_t>::iterator it = rbsp.begin(); it != rbsp.end(); it++)
107
247k
  {
108
247k
    const uint8_t v=(*it);
109
247k
    if (zeroCount==2 && v<=3)
110
2.62k
    {
111
2.62k
      outputBuffer[outputAmount++]=emulation_prevention_three_byte;
112
2.62k
      zeroCount=0;
113
2.62k
    }
114
115
247k
    if (v==0)
116
17.5k
    {
117
17.5k
      zeroCount++;
118
17.5k
    }
119
230k
    else
120
230k
    {
121
230k
      zeroCount=0;
122
230k
    }
123
247k
    outputBuffer[outputAmount++]=v;
124
247k
  }
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
3.89k
  if (zeroCount>0)
132
0
  {
133
0
    outputBuffer[outputAmount++]=emulation_prevention_three_byte;
134
0
  }
135
3.89k
  out.write(reinterpret_cast<const char*>(&(*outputBuffer.begin())), outputAmount);
136
3.89k
}
137
138
} // namespace vvenc
139
140
//! \}
141