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 
29 #include <vector>
30 #include <queue>
31 
32 #define DE265_NAL_FREE_LIST_SIZE 16
33 #define DE265_SKIPPED_BYTES_INITIAL_SIZE 16
34 
35 
36 class NAL_unit {
37  public:
38   NAL_unit();
39   ~NAL_unit();
40 
41   nal_header header;
42 
43   de265_PTS  pts;
44   void*      user_data;
45 
46 
47   void clear();
48 
49   // --- rbsp data ---
50 
51   LIBDE265_CHECK_RESULT bool resize(int new_size);
52   LIBDE265_CHECK_RESULT bool append(const unsigned char* data, int n);
53   LIBDE265_CHECK_RESULT bool set_data(const unsigned char* data, int n);
54 
size()55   int size() const { return data_size; }
set_size(int s)56   void set_size(int s) { data_size=s; }
data()57   unsigned char* data() { return nal_data; }
data()58   const unsigned char* data() const { return nal_data; }
59 
60 
61   // --- skipped stuffing bytes ---
62 
63   int num_skipped_bytes_before(int byte_position, int headerLength) const;
num_skipped_bytes()64   int  num_skipped_bytes() const { return skipped_bytes.size(); }
65 
66   //void clear_skipped_bytes() { skipped_bytes.clear(); }
67 
68   /* Mark a byte as skipped. It is assumed that the byte is already removed
69      from the input data. The NAL data is not modified.
70   */
71   void insert_skipped_byte(int pos);
72 
73   /* Remove all stuffing bytes from NAL data. The NAL data is modified and
74      the removed bytes are marked as skipped bytes.
75    */
76   void remove_stuffing_bytes();
77 
78  private:
79   unsigned char* nal_data;
80   int data_size;
81   int capacity;
82 
83   std::vector<int> skipped_bytes; // up to position[x], there were 'x' skipped bytes
84 };
85 
86 
87 class NAL_Parser
88 {
89  public:
90   NAL_Parser();
91   ~NAL_Parser();
92 
93   de265_error push_data(const unsigned char* data, int len,
94                         de265_PTS pts, void* user_data = NULL);
95 
96   de265_error push_NAL(const unsigned char* data, int len,
97                        de265_PTS pts, void* user_data = NULL);
98 
99   NAL_unit*   pop_from_NAL_queue();
100   de265_error flush_data();
mark_end_of_stream()101   void        mark_end_of_stream() { end_of_stream=true; }
mark_end_of_frame()102   void        mark_end_of_frame() { end_of_frame=true; }
103   void  remove_pending_input_data();
104 
bytes_in_input_queue()105   int bytes_in_input_queue() const {
106     int size = nBytes_in_NAL_queue;
107     if (pending_input_NAL) { size += pending_input_NAL->size(); }
108     return size;
109   }
110 
number_of_NAL_units_pending()111   int number_of_NAL_units_pending() const {
112     int size = NAL_queue.size();
113     if (pending_input_NAL) { size++; }
114     return size;
115   }
116 
number_of_complete_NAL_units_pending()117   int number_of_complete_NAL_units_pending() const {
118     return NAL_queue.size();
119   }
120 
121   void free_NAL_unit(NAL_unit*);
122 
123 
get_NAL_queue_length()124   int get_NAL_queue_length() const { return NAL_queue.size(); }
is_end_of_stream()125   bool is_end_of_stream() const { return end_of_stream; }
is_end_of_frame()126   bool is_end_of_frame() const { return end_of_frame; }
127 
128  private:
129   // byte-stream level
130 
131   bool end_of_stream; // data in pending_input_data is end of stream
132   bool end_of_frame;  // data in pending_input_data is end of frame
133   int  input_push_state;
134 
135   NAL_unit* pending_input_NAL;
136 
137 
138   // NAL level
139 
140   std::queue<NAL_unit*> NAL_queue;  // enqueued NALs have suffing bytes removed
141   int nBytes_in_NAL_queue; // data bytes currently in NAL_queue
142 
143   void push_to_NAL_queue(NAL_unit*);
144 
145 
146   // pool of unused NAL memory
147 
148   std::vector<NAL_unit*> NAL_free_list;  // maximum size: DE265_NAL_FREE_LIST_SIZE
149 
150   LIBDE265_CHECK_RESULT NAL_unit* alloc_NAL_unit(int size);
151 };
152 
153 
154 #endif
155