-
Notifications
You must be signed in to change notification settings - Fork 1
/
h264.cpp
667 lines (604 loc) · 18.6 KB
/
h264.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2004. All Rights Reserved.
*
* Contributor(s):
* Bill May [email protected]
*/
#include "mpeg4ip.h"
#include "mp4av_h264.h"
#include "mpeg4ip_bitstream.h"
//#define BOUND_VERBOSE 1
static uint8_t exp_golomb_bits[256] = {
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
};
uint32_t h264_ue (CBitstream *bs)
{
uint32_t bits, read;
int bits_left;
uint8_t coded;
bool done = false;
bits = 0;
// we want to read 8 bits at a time - if we don't have 8 bits,
// read what's left, and shift. The exp_golomb_bits calc remains the
// same.
while (done == false) {
bits_left = bs->bits_remain();
if (bits_left < 8) {
read = bs->PeekBits(bits_left) << (8 - bits_left);
done = true;
} else {
read = bs->PeekBits(8);
if (read == 0) {
bs->GetBits(8);
bits += 8;
} else {
done = true;
}
}
}
coded = exp_golomb_bits[read];
bs->GetBits(coded);
bits += coded;
// printf("ue - bits %d\n", bits);
return bs->GetBits(bits + 1) - 1;
}
int32_t h264_se (CBitstream *bs)
{
uint32_t ret;
ret = h264_ue(bs);
if ((ret & 0x1) == 0) {
ret >>= 1;
int32_t temp = 0 - ret;
return temp;
}
return (ret + 1) >> 1;
}
static void h264_decode_annexb( uint8_t *dst, int *dstlen,
const uint8_t *src, const int srclen )
{
uint8_t *dst_sav = dst;
const uint8_t *end = &src[srclen];
while (src < end)
{
if (src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
src[2] == 0x03)
{
*dst++ = 0x00;
*dst++ = 0x00;
src += 3;
continue;
}
*dst++ = *src++;
}
*dstlen = dst - dst_sav;
}
extern "C" bool h264_is_start_code (const uint8_t *pBuf)
{
if (pBuf[0] == 0 &&
pBuf[1] == 0 &&
((pBuf[2] == 1) ||
((pBuf[2] == 0) && pBuf[3] == 1))) {
return true;
}
return false;
}
extern "C" uint32_t h264_find_next_start_code (const uint8_t *pBuf,
uint32_t bufLen)
{
uint32_t val, temp;
uint32_t offset;
offset = 0;
if (pBuf[0] == 0 &&
pBuf[1] == 0 &&
((pBuf[2] == 1) ||
((pBuf[2] == 0) && pBuf[3] == 1))) {
pBuf += 3;
offset = 3;
}
val = 0xffffffff;
while (offset < bufLen - 3) {
val <<= 8;
temp = val & 0xff000000;
val &= 0x00ffffff;
val |= *pBuf++;
offset++;
if (val == H264_START_CODE) {
if (temp == 0) return offset - 4;
return offset - 3;
}
}
return 0;
}
extern "C" uint8_t h264_nal_unit_type (const uint8_t *buffer)
{
uint32_t offset;
if (buffer[2] == 1) offset = 3;
else offset = 4;
return buffer[offset] & 0x1f;
}
extern "C" int h264_nal_unit_type_is_slice (uint8_t type)
{
if (type >= H264_NAL_TYPE_NON_IDR_SLICE &&
type <= H264_NAL_TYPE_IDR_SLICE) {
return true;
}
return false;
}
/*
* determine if the slice we decoded is a sync point
*/
extern "C" bool h264_slice_is_idr (h264_decode_t *dec)
{
if (dec->nal_unit_type != H264_NAL_TYPE_IDR_SLICE)
return false;
if (H264_TYPE_IS_I(dec->slice_type)) return true;
if (H264_TYPE_IS_SI(dec->slice_type)) return true;
return false;
}
extern "C" uint8_t h264_nal_ref_idc (const uint8_t *buffer)
{
uint32_t offset;
if (buffer[2] == 1) offset = 3;
else offset = 4;
return (buffer[offset] >> 5) & 0x3;
}
static void scaling_list (uint ix, uint sizeOfScalingList, CBitstream *bs)
{
uint lastScale = 8, nextScale = 8;
uint jx;
int deltaScale;
for (jx = 0; jx < sizeOfScalingList; jx++) {
if (nextScale != 0) {
deltaScale = h264_se(bs);
nextScale = (lastScale + deltaScale + 256) % 256;
printf(" delta: %d\n", deltaScale);
}
if (nextScale == 0) {
lastScale = lastScale;
} else {
lastScale = nextScale;
}
printf(" scaling list[%u][%u]: %u\n", ix, jx, lastScale);
}
}
int h264_read_seq_info (const uint8_t *buffer,
uint32_t buflen,
h264_decode_t *dec)
{
CBitstream bs;
uint32_t header;
uint8_t tmp[2048]; /* Should be enough for all SPS (we have at worst 13 bytes and 496 se/ue in frext) */
int tmp_len;
uint32_t temp;
if (buffer[2] == 1) header = 4;
else header = 5;
h264_decode_annexb( tmp, &tmp_len, buffer + header, MIN(buflen-header,2048) );
bs.init(tmp, tmp_len * 8);
bs.set_verbose(true);
try {
dec->profile = bs.GetBits(8);
bs.GetBits(1 + 1 + 1 + 5);
dec->level = bs.GetBits(8);
printf(" seq parameter set id: %u\n", h264_ue(&bs));
if (dec->profile == 100 || dec->profile == 110 ||
dec->profile == 122 || dec->profile == 144) {
dec->chroma_format_idc = h264_ue(&bs);
printf(" chroma format idx: %u\n", dec->chroma_format_idc);
if (dec->chroma_format_idc == 3) {
dec->residual_colour_transform_flag = bs.GetBits(1);
printf(" resigual colour transform flag: %u\n", dec->residual_colour_transform_flag);
}
dec->bit_depth_luma_minus8 = h264_ue(&bs);
printf(" bit depth luma minus8: %u\n", dec->bit_depth_luma_minus8);
dec->bit_depth_chroma_minus8 = h264_ue(&bs);
printf(" bit depth chroma minus8: %u\n", dec->bit_depth_luma_minus8);
dec->qpprime_y_zero_transform_bypass_flag = bs.GetBits(1);
printf(" Qpprime Y Zero Transform Bypass flag: %u\n",
dec->qpprime_y_zero_transform_bypass_flag);
dec->seq_scaling_matrix_present_flag = bs.GetBits(1);
printf(" Seq Scaling Matrix Present Flag: %u\n",
dec->seq_scaling_matrix_present_flag);
if (dec->seq_scaling_matrix_present_flag) {
for (uint ix = 0; ix < 8; ix++) {
temp = bs.GetBits(1);
printf(" Seq Scaling List[%u] Present Flag: %u\n", ix, temp);
if (temp) {
scaling_list(ix, ix < 6 ? 16 : 64, &bs);
}
}
}
}
dec->log2_max_frame_num_minus4 = h264_ue(&bs);
dec->pic_order_cnt_type = h264_ue(&bs);
if (dec->pic_order_cnt_type == 0) {
dec->log2_max_pic_order_cnt_lsb_minus4 = h264_ue(&bs);
} else if (dec->pic_order_cnt_type == 1) {
dec->delta_pic_order_always_zero_flag = bs.GetBits(1);
dec->offset_for_non_ref_pic = h264_se(&bs); // offset_for_non_ref_pic
dec->offset_for_non_ref_pic = h264_se(&bs); // offset_for_top_to_bottom_field
dec->pic_order_cnt_cycle_length = h264_ue(&bs); // poc_cycle_length
for (uint32_t ix = 0; ix < dec->pic_order_cnt_cycle_length; ix++) {
dec->offset_for_ref_frame[MIN(ix,255)] = h264_se(&bs); // offset for ref fram -
}
}
h264_ue(&bs); // num_ref_frames
bs.GetBits(1); // gaps_in_frame_num_value_allowed_flag
uint32_t PicWidthInMbs = h264_ue(&bs) + 1;
dec->pic_width = PicWidthInMbs * 16;
uint32_t PicHeightInMapUnits = h264_ue(&bs) + 1;
dec->frame_mbs_only_flag = bs.GetBits(1);
dec->pic_height =
(2 - dec->frame_mbs_only_flag) * PicHeightInMapUnits * 16;
#if 0
if (!dec->frame_mbs_only_flag) {
printf(" mb_adaptive_frame_field_flag: %u\n", bs->GetBits(1));
}
printf(" direct_8x8_inference_flag: %u\n", bs->GetBits(1));
temp = bs->GetBits(1);
printf(" frame_cropping_flag: %u\n", temp);
if (temp) {
printf(" frame_crop_left_offset: %u\n", h264_ue(bs));
printf(" frame_crop_right_offset: %u\n", h264_ue(bs));
printf(" frame_crop_top_offset: %u\n", h264_ue(bs));
printf(" frame_crop_bottom_offset: %u\n", h264_ue(bs));
}
temp = bs->GetBits(1);
printf(" vui_parameters_present_flag: %u\n", temp);
if (temp) {
h264_vui_parameters(bs);
}
#endif
} catch (...) {
return -1;
}
return 0;
}
extern "C" int h264_find_slice_type (const uint8_t *buffer,
uint32_t buflen,
uint8_t *slice_type,
bool noheader)
{
uint32_t header;
if (noheader) header = 1;
else {
if (buffer[2] == 1) header = 4;
else header = 5;
}
CBitstream bs;
bs.init(buffer + header, (buflen - header) * 8);
try {
h264_ue(&bs); // first_mb_in_slice
*slice_type = h264_ue(&bs); // slice type
} catch (...) {
return -1;
}
return 0;
}
int h264_read_slice_info (const uint8_t *buffer,
uint32_t buflen,
h264_decode_t *dec)
{
uint32_t header;
uint8_t tmp[512]; /* Enough for the begining of the slice header */
int tmp_len;
if (buffer[2] == 1) header = 4;
else header = 5;
CBitstream bs;
h264_decode_annexb( tmp, &tmp_len, buffer + header, MIN(buflen-header,512) );
bs.init(tmp, tmp_len * 8);
try {
dec->field_pic_flag = 0;
dec->bottom_field_flag = 0;
dec->delta_pic_order_cnt[0] = 0;
dec->delta_pic_order_cnt[1] = 0;
h264_ue(&bs); // first_mb_in_slice
dec->slice_type = h264_ue(&bs); // slice type
h264_ue(&bs); // pic_parameter_set
dec->frame_num = bs.GetBits(dec->log2_max_frame_num_minus4 + 4);
if (!dec->frame_mbs_only_flag) {
dec->field_pic_flag = bs.GetBits(1);
if (dec->field_pic_flag) {
dec->bottom_field_flag = bs.GetBits(1);
}
}
if (dec->nal_unit_type == H264_NAL_TYPE_IDR_SLICE) {
dec->idr_pic_id = h264_ue(&bs);
}
switch (dec->pic_order_cnt_type) {
case 0:
dec->pic_order_cnt_lsb = bs.GetBits(dec->log2_max_pic_order_cnt_lsb_minus4 + 4);
if (dec->pic_order_present_flag && !dec->field_pic_flag) {
dec->delta_pic_order_cnt_bottom = h264_se(&bs);
}
break;
case 1:
if (!dec->delta_pic_order_always_zero_flag) {
dec->delta_pic_order_cnt[0] = h264_se(&bs);
}
if (dec->pic_order_present_flag && !dec->field_pic_flag) {
dec->delta_pic_order_cnt[1] = h264_se(&bs);
}
break;
}
} catch (...) {
return -1;
}
return 0;
}
static void h264_compute_poc( h264_decode_t *dec ) {
const int max_frame_num = 1 << (dec->log2_max_frame_num_minus4 + 4);
int field_poc[2] = {0,0};
enum {
H264_PICTURE_FRAME,
H264_PICTURE_FIELD_TOP,
H264_PICTURE_FIELD_BOTTOM,
} pic_type;
/* FIXME FIXME it doesn't handle the case where there is a MMCO == 5
* (MMCO 5 "emulates" an idr) */
/* picture type */
if (dec->frame_mbs_only_flag || !dec->field_pic_flag)
pic_type = H264_PICTURE_FRAME;
else if (dec->bottom_field_flag)
pic_type = H264_PICTURE_FIELD_BOTTOM;
else
pic_type = H264_PICTURE_FIELD_TOP;
/* frame_num_offset */
if (dec->nal_unit_type == H264_NAL_TYPE_IDR_SLICE) {
dec->pic_order_cnt_lsb_prev = 0;
dec->pic_order_cnt_msb_prev = 0;
dec->frame_num_offset = 0;
} else {
if (dec->frame_num < dec->frame_num_prev)
dec->frame_num_offset = dec->frame_num_offset_prev + max_frame_num;
else
dec->frame_num_offset = dec->frame_num_offset_prev;
}
/* */
if(dec->pic_order_cnt_type == 0) {
const unsigned int max_poc_lsb = 1 << (dec->log2_max_pic_order_cnt_lsb_minus4 + 4);
if (dec->pic_order_cnt_lsb < dec->pic_order_cnt_lsb_prev &&
dec->pic_order_cnt_lsb_prev - dec->pic_order_cnt_lsb >= max_poc_lsb / 2)
dec->pic_order_cnt_msb = dec->pic_order_cnt_msb_prev + max_poc_lsb;
else if (dec->pic_order_cnt_lsb > dec->pic_order_cnt_lsb_prev &&
dec->pic_order_cnt_lsb - dec->pic_order_cnt_lsb_prev > max_poc_lsb / 2)
dec->pic_order_cnt_msb = dec->pic_order_cnt_msb_prev - max_poc_lsb;
else
dec->pic_order_cnt_msb = dec->pic_order_cnt_msb_prev;
field_poc[0] = dec->pic_order_cnt_msb + dec->pic_order_cnt_lsb;
field_poc[1] = field_poc[0];
if (pic_type == H264_PICTURE_FRAME)
field_poc[1] += dec->delta_pic_order_cnt_bottom;
} else if (dec->pic_order_cnt_type == 1) {
int abs_frame_num, expected_delta_per_poc_cycle, expected_poc;
if (dec->pic_order_cnt_cycle_length != 0)
abs_frame_num = dec->frame_num_offset + dec->frame_num;
else
abs_frame_num = 0;
if (dec->nal_ref_idc == 0 && abs_frame_num > 0)
abs_frame_num--;
expected_delta_per_poc_cycle = 0;
for (int i = 0; i < (int)dec->pic_order_cnt_cycle_length; i++ )
expected_delta_per_poc_cycle += dec->offset_for_ref_frame[i];
if (abs_frame_num > 0) {
const int poc_cycle_cnt = ( abs_frame_num - 1 ) / dec->pic_order_cnt_cycle_length;
const int frame_num_in_poc_cycle = ( abs_frame_num - 1 ) % dec->pic_order_cnt_cycle_length;
expected_poc = poc_cycle_cnt * expected_delta_per_poc_cycle;
for (int i = 0; i <= frame_num_in_poc_cycle; i++)
expected_poc += dec->offset_for_ref_frame[i];
} else {
expected_poc = 0;
}
if (dec->nal_ref_idc == 0)
expected_poc += dec->offset_for_non_ref_pic;
field_poc[0] = expected_poc + dec->delta_pic_order_cnt[0];
field_poc[1] = field_poc[0] + dec->offset_for_top_to_bottom_field;
if (pic_type == H264_PICTURE_FRAME)
field_poc[1] += dec->delta_pic_order_cnt[1];
} else if (dec->pic_order_cnt_type == 2) {
int poc;
if (dec->nal_unit_type == H264_NAL_TYPE_IDR_SLICE) {
poc = 0;
} else {
const int abs_frame_num = dec->frame_num_offset + dec->frame_num;
if (dec->nal_ref_idc != 0)
poc = 2 * abs_frame_num;
else
poc = 2 * abs_frame_num - 1;
}
field_poc[0] = poc;
field_poc[1] = poc;
}
/* */
if (pic_type == H264_PICTURE_FRAME)
dec->pic_order_cnt = MIN(field_poc[0], field_poc[1] );
else if (pic_type == H264_PICTURE_FIELD_TOP)
dec->pic_order_cnt = field_poc[0];
else
dec->pic_order_cnt = field_poc[1];
}
extern "C" int h264_detect_boundary (const uint8_t *buffer,
uint32_t buflen,
h264_decode_t *decode)
{
uint8_t temp;
h264_decode_t new_decode;
int ret;
int slice = 0;
memcpy(&new_decode, decode, sizeof(new_decode));
temp = new_decode.nal_unit_type = h264_nal_unit_type(buffer);
new_decode.nal_ref_idc = h264_nal_ref_idc(buffer);
ret = 0;
switch (temp) {
case H264_NAL_TYPE_ACCESS_UNIT:
case H264_NAL_TYPE_END_OF_SEQ:
case H264_NAL_TYPE_END_OF_STREAM:
#ifdef BOUND_VERBOSE
printf("nal type %d\n", temp);
#endif
ret = 1;
break;
case H264_NAL_TYPE_NON_IDR_SLICE:
case H264_NAL_TYPE_DP_A_SLICE:
case H264_NAL_TYPE_DP_B_SLICE:
case H264_NAL_TYPE_DP_C_SLICE:
case H264_NAL_TYPE_IDR_SLICE:
slice = 1;
// slice buffer - read the info into the new_decode, and compare.
if (h264_read_slice_info(buffer, buflen, &new_decode) < 0) {
// need more memory
return -1;
}
if (decode->nal_unit_type > H264_NAL_TYPE_IDR_SLICE ||
decode->nal_unit_type < H264_NAL_TYPE_NON_IDR_SLICE) {
break;
}
if (decode->frame_num != new_decode.frame_num) {
#ifdef BOUND_VERBOSE
printf("frame num values different\n");
#endif
ret = 1;
break;
}
if (decode->field_pic_flag != new_decode.field_pic_flag) {
ret = 1;
#ifdef BOUND_VERBOSE
printf("field pic values different\n");
#endif
break;
}
if (decode->nal_ref_idc != new_decode.nal_ref_idc &&
(decode->nal_ref_idc == 0 ||
new_decode.nal_ref_idc == 0)) {
#ifdef BOUND_VERBOSE
printf("nal ref idc values differ\n");
#endif
ret = 1;
break;
}
if (decode->frame_num == new_decode.frame_num &&
decode->pic_order_cnt_type == new_decode.pic_order_cnt_type) {
if (decode->pic_order_cnt_type == 0) {
if (decode->pic_order_cnt_lsb != new_decode.pic_order_cnt_lsb) {
#ifdef BOUND_VERBOSE
printf("pic order 1\n");
#endif
ret = 1;
break;
}
if (decode->delta_pic_order_cnt_bottom != new_decode.delta_pic_order_cnt_bottom) {
ret = 1;
#ifdef BOUND_VERBOSE
printf("delta pic order cnt bottom 1\n");
#endif
break;
}
} else if (decode->pic_order_cnt_type == 1) {
if (decode->delta_pic_order_cnt[0] != new_decode.delta_pic_order_cnt[0]) {
ret =1;
#ifdef BOUND_VERBOSE
printf("delta pic order cnt [0]\n");
#endif
break;
}
if (decode->delta_pic_order_cnt[1] != new_decode.delta_pic_order_cnt[1]) {
ret = 1;
#ifdef BOUND_VERBOSE
printf("delta pic order cnt [1]\n");
#endif
break;
}
}
}
if (decode->nal_unit_type == H264_NAL_TYPE_IDR_SLICE &&
new_decode.nal_unit_type == H264_NAL_TYPE_IDR_SLICE) {
if (decode->idr_pic_id != new_decode.idr_pic_id) {
#ifdef BOUND_VERBOSE
printf("idr_pic id\n");
#endif
ret = 1;
break;
}
}
break;
case H264_NAL_TYPE_SEQ_PARAM:
if (h264_read_seq_info(buffer, buflen, &new_decode) < 0) {
return -1;
}
// fall through
default:
if (decode->nal_unit_type <= H264_NAL_TYPE_IDR_SLICE) ret = 1;
else ret = 0;
}
/* save _prev values */
if (ret)
{
new_decode.frame_num_offset_prev = decode->frame_num_offset;
if (decode->pic_order_cnt_type != 2 || decode->nal_ref_idc != 0)
new_decode.frame_num_prev = decode->frame_num;
if (decode->nal_ref_idc != 0)
{
new_decode.pic_order_cnt_lsb_prev = decode->pic_order_cnt_lsb;
new_decode.pic_order_cnt_msb_prev = decode->pic_order_cnt_msb;
}
}
if( slice ) { // XXX we compute poc for every slice in a picture (but it's not needed)
h264_compute_poc( &new_decode );
}
// other types (6, 7, 8,
#ifdef BOUND_VERBOSE
if (ret == 0) {
printf("no change\n");
}
#endif
memcpy(decode, &new_decode, sizeof(*decode));
return ret;
}
uint32_t h264_read_sei_value (const uint8_t *buffer, uint32_t *size)
{
uint32_t ret = 0;
*size = 1;
while (buffer[*size] == 0xff) {
ret += 255;
*size = *size + 1;
}
ret += *buffer;
return ret;
}
extern "C" const char *h264_get_slice_name (uint8_t slice_type)
{
if (H264_TYPE_IS_P(slice_type)) return "P";
if (H264_TYPE_IS_B(slice_type)) return "B";
if (H264_TYPE_IS_I(slice_type)) return "I";
if (H264_TYPE_IS_SI(slice_type)) return "SI";
if (H264_TYPE_IS_SP(slice_type)) return "SP";
return "UNK";
}