/src/libde265/libde265/nal-parser.cc
Line | Count | Source (jump to first uncovered line) |
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 | | #include "nal-parser.h" |
22 | | |
23 | | #include <string.h> |
24 | | #include <assert.h> |
25 | | #include <stdlib.h> |
26 | | #include <stdio.h> |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | #include "config.h" |
30 | | #endif |
31 | | |
32 | | |
33 | | NAL_unit::NAL_unit() |
34 | | : skipped_bytes(DE265_SKIPPED_BYTES_INITIAL_SIZE) |
35 | 93.5k | { |
36 | 93.5k | pts=0; |
37 | 93.5k | user_data = NULL; |
38 | | |
39 | 93.5k | nal_data = NULL; |
40 | 93.5k | data_size = 0; |
41 | 93.5k | capacity = 0; |
42 | 93.5k | } |
43 | | |
44 | | NAL_unit::~NAL_unit() |
45 | 93.5k | { |
46 | 93.5k | free(nal_data); |
47 | 93.5k | } |
48 | | |
49 | | void NAL_unit::clear() |
50 | 93.5k | { |
51 | 93.5k | header = nal_header(); |
52 | 93.5k | pts = 0; |
53 | 93.5k | user_data = NULL; |
54 | | |
55 | | // set size to zero but keep memory |
56 | 93.5k | data_size = 0; |
57 | | |
58 | 93.5k | skipped_bytes.clear(); |
59 | 93.5k | } |
60 | | |
61 | | LIBDE265_CHECK_RESULT bool NAL_unit::resize(int new_size) |
62 | 187k | { |
63 | 187k | if (capacity < new_size) { |
64 | 22.2k | unsigned char* newbuffer = (unsigned char*)malloc(new_size); |
65 | 22.2k | if (newbuffer == NULL) { |
66 | 0 | return false; |
67 | 0 | } |
68 | | |
69 | 22.2k | if (nal_data != NULL) { |
70 | 0 | memcpy(newbuffer, nal_data, data_size); |
71 | 0 | free(nal_data); |
72 | 0 | } |
73 | | |
74 | 22.2k | nal_data = newbuffer; |
75 | 22.2k | capacity = new_size; |
76 | 22.2k | } |
77 | 187k | return true; |
78 | 187k | } |
79 | | |
80 | | LIBDE265_CHECK_RESULT bool NAL_unit::append(const unsigned char* in_data, int n) |
81 | 0 | { |
82 | 0 | if (!resize(data_size + n)) { |
83 | 0 | return false; |
84 | 0 | } |
85 | 0 | memcpy(nal_data + data_size, in_data, n); |
86 | 0 | data_size += n; |
87 | 0 | return true; |
88 | 0 | } |
89 | | |
90 | | bool LIBDE265_CHECK_RESULT NAL_unit::set_data(const unsigned char* in_data, int n) |
91 | 93.5k | { |
92 | 93.5k | if (!resize(n)) { |
93 | 0 | return false; |
94 | 0 | } |
95 | 93.5k | memcpy(nal_data, in_data, n); |
96 | 93.5k | data_size = n; |
97 | 93.5k | return true; |
98 | 93.5k | } |
99 | | |
100 | | void NAL_unit::insert_skipped_byte(int pos) |
101 | 69.3k | { |
102 | 69.3k | skipped_bytes.push_back(pos); |
103 | 69.3k | } |
104 | | |
105 | | int NAL_unit::num_skipped_bytes_before(int byte_position, int headerLength) const |
106 | 21.3k | { |
107 | 93.9k | for (int k=skipped_bytes.size()-1;k>=0;k--) |
108 | 88.5k | if (skipped_bytes[k]-headerLength <= byte_position) { |
109 | 16.0k | return k+1; |
110 | 16.0k | } |
111 | | |
112 | 5.33k | return 0; |
113 | 21.3k | } |
114 | | |
115 | | void NAL_unit::remove_stuffing_bytes() |
116 | 93.5k | { |
117 | 93.5k | uint8_t* p = data(); |
118 | | |
119 | 2.11M | for (int i=0;i<size()-2;i++) |
120 | 2.02M | { |
121 | | #if 0 |
122 | | for (int k=i;k<i+64;k++) |
123 | | if (i*0+k<size()) { |
124 | | printf("%c%02x", (k==i) ? '[':' ', data()[k]); |
125 | | } |
126 | | printf("\n"); |
127 | | #endif |
128 | | |
129 | 2.02M | if (p[2]!=3 && p[2]!=0) { |
130 | | // fast forward 3 bytes (2+1) |
131 | 1.36M | p+=2; |
132 | 1.36M | i+=2; |
133 | 1.36M | } |
134 | 662k | else { |
135 | 662k | if (p[0]==0 && p[1]==0 && p[2]==3) { |
136 | | //printf("SKIP NAL @ %d\n",i+2+num_skipped_bytes); |
137 | 69.3k | insert_skipped_byte(i+2 + num_skipped_bytes()); |
138 | | |
139 | 69.3k | memmove(p+2, p+3, size()-i-3); |
140 | 69.3k | set_size(size()-1); |
141 | | |
142 | 69.3k | p++; |
143 | 69.3k | i++; |
144 | 69.3k | } |
145 | 662k | } |
146 | | |
147 | 2.02M | p++; |
148 | 2.02M | } |
149 | 93.5k | } |
150 | | |
151 | | |
152 | | |
153 | | |
154 | | |
155 | | NAL_Parser::NAL_Parser() |
156 | 6.00k | { |
157 | 6.00k | end_of_stream = false; |
158 | 6.00k | end_of_frame = false; |
159 | 6.00k | input_push_state = 0; |
160 | 6.00k | pending_input_NAL = NULL; |
161 | 6.00k | nBytes_in_NAL_queue = 0; |
162 | 6.00k | } |
163 | | |
164 | | |
165 | | NAL_Parser::~NAL_Parser() |
166 | 6.00k | { |
167 | | // --- free NAL queues --- |
168 | | |
169 | | // empty NAL queue |
170 | | |
171 | 6.00k | NAL_unit* nal; |
172 | 53.9k | while ( (nal = pop_from_NAL_queue()) ) { |
173 | 47.9k | free_NAL_unit(nal); |
174 | 47.9k | } |
175 | | |
176 | | // free the pending input NAL |
177 | | |
178 | 6.00k | if (pending_input_NAL != NULL) { |
179 | 0 | free_NAL_unit(pending_input_NAL); |
180 | 0 | } |
181 | | |
182 | | // free all NALs in free-list |
183 | | |
184 | 28.9k | for (int i=0;i<NAL_free_list.size();i++) { |
185 | 22.8k | delete NAL_free_list[i]; |
186 | 22.8k | } |
187 | 6.00k | } |
188 | | |
189 | | |
190 | | LIBDE265_CHECK_RESULT NAL_unit* NAL_Parser::alloc_NAL_unit(int size) |
191 | 93.5k | { |
192 | 93.5k | NAL_unit* nal; |
193 | | |
194 | | // --- get NAL-unit object --- |
195 | | |
196 | 93.5k | if (NAL_free_list.size() > 0) { |
197 | 0 | nal = NAL_free_list.back(); |
198 | 0 | NAL_free_list.pop_back(); |
199 | 0 | } |
200 | 93.5k | else { |
201 | 93.5k | nal = new NAL_unit; |
202 | 93.5k | } |
203 | | |
204 | 93.5k | nal->clear(); |
205 | 93.5k | if (!nal->resize(size)) { |
206 | 0 | free_NAL_unit(nal); |
207 | 0 | return NULL; |
208 | 0 | } |
209 | | |
210 | 93.5k | return nal; |
211 | 93.5k | } |
212 | | |
213 | | void NAL_Parser::free_NAL_unit(NAL_unit* nal) |
214 | 93.5k | { |
215 | 93.5k | if (nal == NULL) { |
216 | | // Allow calling with NULL just like regular "free()" |
217 | 0 | return; |
218 | 0 | } |
219 | 93.5k | if (NAL_free_list.size() < DE265_NAL_FREE_LIST_SIZE) { |
220 | 22.8k | NAL_free_list.push_back(nal); |
221 | 22.8k | } |
222 | 70.6k | else { |
223 | 70.6k | delete nal; |
224 | 70.6k | } |
225 | 93.5k | } |
226 | | |
227 | | NAL_unit* NAL_Parser::pop_from_NAL_queue() |
228 | 99.5k | { |
229 | 99.5k | if (NAL_queue.empty()) { |
230 | 6.00k | return NULL; |
231 | 6.00k | } |
232 | 93.5k | else { |
233 | 93.5k | NAL_unit* nal = NAL_queue.front(); |
234 | 93.5k | NAL_queue.pop(); |
235 | | |
236 | 93.5k | nBytes_in_NAL_queue -= nal->size(); |
237 | | |
238 | 93.5k | return nal; |
239 | 93.5k | } |
240 | 99.5k | } |
241 | | |
242 | | void NAL_Parser::push_to_NAL_queue(NAL_unit* nal) |
243 | 93.5k | { |
244 | 93.5k | NAL_queue.push(nal); |
245 | 93.5k | nBytes_in_NAL_queue += nal->size(); |
246 | 93.5k | } |
247 | | |
248 | | de265_error NAL_Parser::push_data(const unsigned char* data, int len, |
249 | | de265_PTS pts, void* user_data) |
250 | 0 | { |
251 | 0 | end_of_frame = false; |
252 | |
|
253 | 0 | if (pending_input_NAL == NULL) { |
254 | 0 | pending_input_NAL = alloc_NAL_unit(len+3); |
255 | 0 | if (pending_input_NAL == NULL) { |
256 | 0 | return DE265_ERROR_OUT_OF_MEMORY; |
257 | 0 | } |
258 | 0 | pending_input_NAL->pts = pts; |
259 | 0 | pending_input_NAL->user_data = user_data; |
260 | 0 | } |
261 | | |
262 | 0 | NAL_unit* nal = pending_input_NAL; // shortcut |
263 | | |
264 | | // Resize output buffer so that complete input would fit. |
265 | | // We add 3, because in the worst case 3 extra bytes are created for an input byte. |
266 | 0 | if (!nal->resize(nal->size() + len + 3)) { |
267 | 0 | return DE265_ERROR_OUT_OF_MEMORY; |
268 | 0 | } |
269 | | |
270 | 0 | unsigned char* out = nal->data() + nal->size(); |
271 | |
|
272 | 0 | for (int i=0;i<len;i++) { |
273 | | /* |
274 | | printf("state=%d input=%02x (%p) (output size: %d)\n",ctx->input_push_state, *data, data, |
275 | | out - ctx->nal_data.data); |
276 | | */ |
277 | |
|
278 | 0 | switch (input_push_state) { |
279 | 0 | case 0: |
280 | 0 | case 1: |
281 | 0 | if (*data == 0) { input_push_state++; } |
282 | 0 | else { input_push_state=0; } |
283 | 0 | break; |
284 | 0 | case 2: |
285 | 0 | if (*data == 1) { input_push_state=3; } // nal->clear_skipped_bytes(); } |
286 | 0 | else if (*data == 0) { } // *out++ = 0; } |
287 | 0 | else { input_push_state=0; } |
288 | 0 | break; |
289 | 0 | case 3: |
290 | 0 | *out++ = *data; |
291 | 0 | input_push_state = 4; |
292 | 0 | break; |
293 | 0 | case 4: |
294 | 0 | *out++ = *data; |
295 | 0 | input_push_state = 5; |
296 | 0 | break; |
297 | | |
298 | 0 | case 5: |
299 | 0 | if (*data==0) { input_push_state=6; } |
300 | 0 | else { *out++ = *data; } |
301 | 0 | break; |
302 | | |
303 | 0 | case 6: |
304 | 0 | if (*data==0) { input_push_state=7; } |
305 | 0 | else { |
306 | 0 | *out++ = 0; |
307 | 0 | *out++ = *data; |
308 | 0 | input_push_state=5; |
309 | 0 | } |
310 | 0 | break; |
311 | | |
312 | 0 | case 7: |
313 | 0 | if (*data==0) { *out++ = 0; } |
314 | 0 | else if (*data==3) { |
315 | 0 | *out++ = 0; *out++ = 0; input_push_state=5; |
316 | | |
317 | | // remember which byte we removed |
318 | 0 | nal->insert_skipped_byte((out - nal->data()) + nal->num_skipped_bytes()); |
319 | 0 | } |
320 | 0 | else if (*data==1) { |
321 | |
|
322 | | #if DEBUG_INSERT_STREAM_ERRORS |
323 | | if ((rand()%100)<90 && nal_data.size>0) { |
324 | | int pos = rand()%nal_data.size; |
325 | | int bit = rand()%8; |
326 | | nal->nal_data.data[pos] ^= 1<<bit; |
327 | | |
328 | | //printf("inserted error...\n"); |
329 | | } |
330 | | #endif |
331 | |
|
332 | 0 | nal->set_size(out - nal->data());; |
333 | | |
334 | | // push this NAL decoder queue |
335 | 0 | push_to_NAL_queue(nal); |
336 | | |
337 | | |
338 | | // initialize new, empty NAL unit |
339 | |
|
340 | 0 | pending_input_NAL = alloc_NAL_unit(len+3); |
341 | 0 | if (pending_input_NAL == NULL) { |
342 | 0 | return DE265_ERROR_OUT_OF_MEMORY; |
343 | 0 | } |
344 | 0 | pending_input_NAL->pts = pts; |
345 | 0 | pending_input_NAL->user_data = user_data; |
346 | 0 | nal = pending_input_NAL; |
347 | 0 | out = nal->data(); |
348 | |
|
349 | 0 | input_push_state=3; |
350 | | //nal->clear_skipped_bytes(); |
351 | 0 | } |
352 | 0 | else { |
353 | 0 | *out++ = 0; |
354 | 0 | *out++ = 0; |
355 | 0 | *out++ = *data; |
356 | |
|
357 | 0 | input_push_state=5; |
358 | 0 | } |
359 | 0 | break; |
360 | 0 | } |
361 | | |
362 | 0 | data++; |
363 | 0 | } |
364 | | |
365 | 0 | nal->set_size(out - nal->data()); |
366 | 0 | return DE265_OK; |
367 | 0 | } |
368 | | |
369 | | |
370 | | de265_error NAL_Parser::push_NAL(const unsigned char* data, int len, |
371 | | de265_PTS pts, void* user_data) |
372 | 93.5k | { |
373 | | |
374 | | // Cannot use byte-stream input and NAL input at the same time. |
375 | 93.5k | assert(pending_input_NAL == NULL); |
376 | | |
377 | 0 | end_of_frame = false; |
378 | | |
379 | 93.5k | NAL_unit* nal = alloc_NAL_unit(len); |
380 | 93.5k | if (nal == NULL || !nal->set_data(data, len)) { |
381 | 0 | free_NAL_unit(nal); |
382 | 0 | return DE265_ERROR_OUT_OF_MEMORY; |
383 | 0 | } |
384 | 93.5k | nal->pts = pts; |
385 | 93.5k | nal->user_data = user_data; |
386 | | |
387 | 93.5k | nal->remove_stuffing_bytes(); |
388 | | |
389 | 93.5k | push_to_NAL_queue(nal); |
390 | | |
391 | 93.5k | return DE265_OK; |
392 | 93.5k | } |
393 | | |
394 | | |
395 | | de265_error NAL_Parser::flush_data() |
396 | 8.77k | { |
397 | 8.77k | if (pending_input_NAL) { |
398 | 0 | NAL_unit* nal = pending_input_NAL; |
399 | 0 | uint8_t null[2] = { 0,0 }; |
400 | | |
401 | | // append bytes that are implied by the push state |
402 | |
|
403 | 0 | if (input_push_state==6) { |
404 | 0 | if (!nal->append(null,1)) { |
405 | 0 | return DE265_ERROR_OUT_OF_MEMORY; |
406 | 0 | } |
407 | 0 | } |
408 | 0 | if (input_push_state==7) { |
409 | 0 | if (!nal->append(null,2)) { |
410 | 0 | return DE265_ERROR_OUT_OF_MEMORY; |
411 | 0 | } |
412 | 0 | } |
413 | | |
414 | | |
415 | | // only push the NAL if it contains at least the NAL header |
416 | | |
417 | 0 | if (input_push_state>=5) { |
418 | 0 | push_to_NAL_queue(nal); |
419 | 0 | pending_input_NAL = NULL; |
420 | 0 | } |
421 | |
|
422 | 0 | input_push_state = 0; |
423 | 0 | } |
424 | | |
425 | 8.77k | return DE265_OK; |
426 | 8.77k | } |
427 | | |
428 | | |
429 | | void NAL_Parser::remove_pending_input_data() |
430 | 0 | { |
431 | | // --- remove pending input data --- |
432 | |
|
433 | 0 | if (pending_input_NAL) { |
434 | 0 | free_NAL_unit(pending_input_NAL); |
435 | 0 | pending_input_NAL = NULL; |
436 | 0 | } |
437 | |
|
438 | 0 | for (;;) { |
439 | 0 | NAL_unit* nal = pop_from_NAL_queue(); |
440 | 0 | if (nal) { free_NAL_unit(nal); } |
441 | 0 | else break; |
442 | 0 | } |
443 | |
|
444 | 0 | input_push_state = 0; |
445 | 0 | nBytes_in_NAL_queue = 0; |
446 | 0 | } |