/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 | | #include <memory> |
33 | | |
34 | | |
35 | | class NAL_unit { |
36 | | public: |
37 | 0 | NAL_unit() = default; |
38 | | ~NAL_unit(); |
39 | | |
40 | | nal_header header; |
41 | | |
42 | | de265_PTS pts = 0; |
43 | | void* user_data = nullptr; |
44 | | |
45 | | |
46 | | // --- rbsp data --- |
47 | | |
48 | | [[nodiscard]] bool resize(int new_size); |
49 | | [[nodiscard]] bool append(const unsigned char* data, int n); |
50 | | [[nodiscard]] bool set_data(const unsigned char* data, int n); |
51 | | |
52 | 0 | int size() const { return data_size; } |
53 | 0 | void set_size(int s) { data_size=s; } |
54 | 0 | unsigned char* data() { return nal_data; } |
55 | 0 | const unsigned char* data() const { return nal_data; } |
56 | | |
57 | | |
58 | | // --- skipped stuffing bytes --- |
59 | | |
60 | | uint32_t num_skipped_bytes_before(uint32_t byte_position, uint32_t headerLength) const; |
61 | 0 | uint32_t num_skipped_bytes() const { return skipped_bytes.size(); } |
62 | | |
63 | | //void clear_skipped_bytes() { skipped_bytes.clear(); } |
64 | | |
65 | | /* Mark a byte as skipped. It is assumed that the byte is already removed |
66 | | from the input data. The NAL data is not modified. |
67 | | */ |
68 | | void insert_skipped_byte(uint32_t pos); |
69 | | |
70 | | /* Remove all stuffing bytes from NAL data. The NAL data is modified and |
71 | | the removed bytes are marked as skipped bytes. |
72 | | */ |
73 | | void remove_stuffing_bytes(); |
74 | | |
75 | | private: |
76 | | unsigned char* nal_data = nullptr; |
77 | | int data_size = 0; |
78 | | int capacity = 0; |
79 | | |
80 | | std::vector<uint32_t> skipped_bytes; // up to position[x], there were 'x' skipped bytes |
81 | | }; |
82 | | |
83 | | |
84 | | class NAL_Parser |
85 | | { |
86 | | public: |
87 | | NAL_Parser(); |
88 | | ~NAL_Parser(); |
89 | | |
90 | | // Point the parser at the live security limits struct so that runtime |
91 | | // changes (via de265_get_security_limits()) take effect immediately. |
92 | 0 | void set_security_limits(const de265_security_limits* limits) { m_security_limits = limits; } |
93 | | |
94 | | de265_error push_data(const unsigned char* data, int len, |
95 | | de265_PTS pts, void* user_data = nullptr); |
96 | | |
97 | | de265_error push_NAL(const unsigned char* data, int len, |
98 | | de265_PTS pts, void* user_data = nullptr); |
99 | | |
100 | | std::unique_ptr<NAL_unit> pop_from_NAL_queue(); |
101 | | de265_error flush_data(); |
102 | 0 | void mark_end_of_stream() { end_of_stream=true; } |
103 | 0 | void mark_end_of_frame() { end_of_frame=true; } |
104 | | void remove_pending_input_data(); |
105 | | |
106 | 0 | int bytes_in_input_queue() const { |
107 | 0 | int size = nBytes_in_NAL_queue; |
108 | 0 | if (pending_input_NAL) { size += pending_input_NAL->size(); } |
109 | 0 | return size; |
110 | 0 | } |
111 | | |
112 | 0 | int number_of_NAL_units_pending() const { |
113 | 0 | int size = NAL_queue.size(); |
114 | 0 | if (pending_input_NAL) { size++; } |
115 | 0 | return size; |
116 | 0 | } |
117 | | |
118 | 0 | int number_of_complete_NAL_units_pending() const { |
119 | 0 | return NAL_queue.size(); |
120 | 0 | } |
121 | | |
122 | | // Release a NAL. Takes ownership by value, so a move transfers the object here |
123 | | // and leaves the caller holding nullptr; a redundant release therefore passes |
124 | | // nullptr and is a harmless no-op, which is what makes a double release |
125 | | // impossible to express. |
126 | | void free_NAL_unit(std::unique_ptr<NAL_unit> nal); |
127 | | |
128 | | |
129 | 0 | int get_NAL_queue_length() const { return NAL_queue.size(); } |
130 | 0 | 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 | | std::unique_ptr<NAL_unit> pending_input_NAL; |
141 | | |
142 | | const de265_security_limits* m_security_limits = nullptr; |
143 | | |
144 | | |
145 | | // NAL level |
146 | | |
147 | | std::queue<std::unique_ptr<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(std::unique_ptr<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 | 0 | bool nal_size_within_limit(int64_t nal_size) const { |
155 | 0 | return m_security_limits == nullptr || |
156 | 0 | m_security_limits->max_NAL_size_bytes == 0 || |
157 | 0 | nal_size <= m_security_limits->max_NAL_size_bytes; |
158 | 0 | } |
159 | | |
160 | | |
161 | | [[nodiscard]] std::unique_ptr<NAL_unit> alloc_NAL_unit(int size); |
162 | | }; |
163 | | |
164 | | |
165 | | #endif |