digiKam
nal-parser.h
Go to the documentation of this file.
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:
40 
42 
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 
55  int size() const { return data_size; }
56  void set_size(int s) { data_size=s; }
57  unsigned char* data() { return nal_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;
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  */
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 
88 {
89  public:
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 
101  void mark_end_of_stream() { end_of_stream=true; }
102  void mark_end_of_frame() { end_of_frame=true; }
104 
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 
112  int size = NAL_queue.size();
113  if (pending_input_NAL) { size++; }
114  return size;
115  }
116 
118  return NAL_queue.size();
119  }
120 
122 
123 
124  int get_NAL_queue_length() const { return NAL_queue.size(); }
125  bool is_end_of_stream() const { return end_of_stream; }
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
Definition: nal-parser.h:88
NAL_unit * pop_from_NAL_queue()
de265_error flush_data()
void free_NAL_unit(NAL_unit *)
void remove_pending_input_data()
bool is_end_of_frame() const
Definition: nal-parser.h:126
int number_of_NAL_units_pending() const
Definition: nal-parser.h:111
int get_NAL_queue_length() const
Definition: nal-parser.h:124
void mark_end_of_frame()
Definition: nal-parser.h:102
de265_error push_NAL(const unsigned char *data, int len, de265_PTS pts, void *user_data=NULL)
bool is_end_of_stream() const
Definition: nal-parser.h:125
int bytes_in_input_queue() const
Definition: nal-parser.h:105
de265_error push_data(const unsigned char *data, int len, de265_PTS pts, void *user_data=NULL)
void mark_end_of_stream()
Definition: nal-parser.h:101
int number_of_complete_NAL_units_pending() const
Definition: nal-parser.h:117
Definition: nal-parser.h:36
void clear()
const unsigned char * data() const
Definition: nal-parser.h:58
int size() const
Definition: nal-parser.h:55
LIBDE265_CHECK_RESULT bool append(const unsigned char *data, int n)
unsigned char * data()
Definition: nal-parser.h:57
LIBDE265_CHECK_RESULT bool resize(int new_size)
LIBDE265_CHECK_RESULT bool set_data(const unsigned char *data, int n)
int num_skipped_bytes() const
Definition: nal-parser.h:64
nal_header header
Definition: nal-parser.h:41
de265_PTS pts
Definition: nal-parser.h:43
void insert_skipped_byte(int pos)
int num_skipped_bytes_before(int byte_position, int headerLength) const
void * user_data
Definition: nal-parser.h:44
void remove_stuffing_bytes()
void set_size(int s)
Definition: nal-parser.h:56
int64_t de265_PTS
Definition: de265.h:166
de265_error
Definition: de265.h:82
Definition: nal.h:36
#define LIBDE265_CHECK_RESULT
Definition: util.h:56