Coverage Report

Created: 2026-06-15 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/nal-parser.h
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef DE265_NAL_PARSER_H
22
#define DE265_NAL_PARSER_H
23
24
#include "libde265/sps.h"
25
#include "libde265/pps.h"
26
#include "libde265/nal.h"
27
#include "libde265/util.h"
28
#include "libde265/de265.h"
29
30
#include <vector>
31
#include <queue>
32
33
constexpr int DE265_NAL_FREE_LIST_SIZE = 16;
34
constexpr int DE265_SKIPPED_BYTES_INITIAL_SIZE = 16;
35
36
37
class NAL_unit {
38
 public:
39
  NAL_unit();
40
  ~NAL_unit();
41
42
  nal_header header;
43
44
  de265_PTS  pts = 0;
45
  void*      user_data = nullptr;
46
47
48
  void clear();
49
50
  // --- rbsp data ---
51
52
  LIBDE265_CHECK_RESULT bool resize(int new_size);
53
  LIBDE265_CHECK_RESULT bool append(const unsigned char* data, int n);
54
  LIBDE265_CHECK_RESULT bool set_data(const unsigned char* data, int n);
55
56
4.41k
  int size() const { return data_size; }
57
1.14k
  void set_size(int s) { data_size=s; }
58
2.11k
  unsigned char* data() { return nal_data; }
59
0
  const unsigned char* data() const { return nal_data; }
60
61
62
  // --- skipped stuffing bytes ---
63
64
  uint32_t num_skipped_bytes_before(uint32_t byte_position, uint32_t headerLength) const;
65
0
  uint32_t num_skipped_bytes() const { return skipped_bytes.size(); }
66
67
  //void clear_skipped_bytes() { skipped_bytes.clear(); }
68
69
  /* Mark a byte as skipped. It is assumed that the byte is already removed
70
     from the input data. The NAL data is not modified.
71
  */
72
  void insert_skipped_byte(uint32_t pos);
73
74
  /* Remove all stuffing bytes from NAL data. The NAL data is modified and
75
     the removed bytes are marked as skipped bytes.
76
   */
77
  void remove_stuffing_bytes();
78
79
 private:
80
  unsigned char* nal_data = nullptr;
81
  int data_size = 0;
82
  int capacity = 0;
83
84
  std::vector<uint32_t> skipped_bytes; // up to position[x], there were 'x' skipped bytes
85
};
86
87
88
class NAL_Parser
89
{
90
 public:
91
  NAL_Parser();
92
  ~NAL_Parser();
93
94
  // Point the parser at the live security limits struct so that runtime
95
  // changes (via de265_get_security_limits()) take effect immediately.
96
634
  void set_security_limits(const de265_security_limits* limits) { m_security_limits = limits; }
97
98
  de265_error push_data(const unsigned char* data, int len,
99
                        de265_PTS pts, void* user_data = nullptr);
100
101
  de265_error push_NAL(const unsigned char* data, int len,
102
                       de265_PTS pts, void* user_data = nullptr);
103
104
  NAL_unit*   pop_from_NAL_queue();
105
  de265_error flush_data();
106
322
  void        mark_end_of_stream() { end_of_stream=true; }
107
0
  void        mark_end_of_frame() { end_of_frame=true; }
108
  void  remove_pending_input_data();
109
110
0
  int bytes_in_input_queue() const {
111
0
    int size = nBytes_in_NAL_queue;
112
0
    if (pending_input_NAL) { size += pending_input_NAL->size(); }
113
0
    return size;
114
0
  }
115
116
0
  int number_of_NAL_units_pending() const {
117
0
    int size = NAL_queue.size();
118
0
    if (pending_input_NAL) { size++; }
119
0
    return size;
120
0
  }
121
122
0
  int number_of_complete_NAL_units_pending() const {
123
0
    return NAL_queue.size();
124
0
  }
125
126
  void free_NAL_unit(NAL_unit*);
127
128
129
17.2k
  int get_NAL_queue_length() const { return NAL_queue.size(); }
130
16.2k
  bool is_end_of_stream() const { return end_of_stream; }
131
0
  bool is_end_of_frame() const { return end_of_frame; }
132
133
 private:
134
  // byte-stream level
135
136
  bool end_of_stream = false; // data in pending_input_data is end of stream
137
  bool end_of_frame = false;  // data in pending_input_data is end of frame
138
  int  input_push_state = 0;
139
140
  NAL_unit* pending_input_NAL = nullptr;
141
142
  const de265_security_limits* m_security_limits = nullptr;
143
144
145
  // NAL level
146
147
  std::queue<NAL_unit*> NAL_queue;  // enqueued NALs have suffing bytes removed
148
  int nBytes_in_NAL_queue = 0; // data bytes currently in NAL_queue
149
150
  void push_to_NAL_queue(NAL_unit*);
151
152
  // Returns true if a NAL unit of the given size is within the configured
153
  // security limit (or if no limit is set).
154
1.14k
  bool nal_size_within_limit(int64_t nal_size) const {
155
1.14k
    return m_security_limits == nullptr ||
156
1.14k
           m_security_limits->max_NAL_size_bytes == 0 ||
157
1.14k
           nal_size <= m_security_limits->max_NAL_size_bytes;
158
1.14k
  }
159
160
161
  // pool of unused NAL memory
162
163
  std::vector<NAL_unit*> NAL_free_list;  // maximum size: DE265_NAL_FREE_LIST_SIZE
164
165
  LIBDE265_CHECK_RESULT NAL_unit* alloc_NAL_unit(int size);
166
};
167
168
169
#endif