-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAVCFrameChecker.cpp
More file actions
1921 lines (1665 loc) · 57.7 KB
/
AVCFrameChecker.cpp
File metadata and controls
1921 lines (1665 loc) · 57.7 KB
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "AVCFrameChecker.h"
#include <windows.h>
#include <cassert>
#include <string>
#include <cstdio>
#include <math.h>
#include <new>
static const float eps = 1E-5;
static const uint32_t naluinitsize = 16000; ///< 16k initial buffer
static const char *nalu_names[] =
{
"UNKNOWN", ///< 0: UNKNOWN
"SLICE", ///< 1: SLICE
"SLICE_DPA", ///< 2: SLICE_DPA
"SLICE_DPB", ///< 3: SLICE_DPB
"SLICE_DPC", ///< 4: SLICE_DPC
"SLICE_IDR", ///< 5: SLICE_IDR
"SEI", ///< 6: SEI
"SPS", ///< 7: SPS
"PPS", ///< 8: PPS
"AU_DELIMITER", ///< 9: AU_DELIMITER
"END_SEQUENCE", ///< 10:END_SEQUENCE
"END_STREAM", ///< 11:END_STREAM
"FILLER_DATA", ///< 12:FILLER_DATA
"SPS_EXT", ///< 13:SPS_EXT
"OTHER", ///< 14:NALU_prefix
"OTHER", ///< 15:SPS_subset
"Reserved", ///< 16:
"Reserved", ///< 17:
"Reserved", ///< 18:
"AUXILIARY_SLICE", ///< 19:AUXILIARY_SLICE
"SLICE_EXTENSION", ///< 20:SLICE_EXTENSION
"Reserved", ///< 21:
"Reserved", ///< 22:
"Reserved", ///< 23:
"Unspecified", ///< 24:
"Unspecified", ///< 25:
"Unspecified", ///< 26:
"Unspecified", ///< 27:
"Unspecified", ///< 28:
"Unspecified", ///< 29:
"Unspecified", ///< 30:
"Unspecified" ///< 31:
};
///////////////////////SLICE_EXTENSION///////////////////////////////////////////////////
////
/*
Most of the comments below were cut&paste from ITU-T Rec. H.264
as found here: http://www.itu.int/rec/T-REC-H.264/e
*/
/* Useful definitions:
* access unit: A set of NAL units always containing exactly one
primary coded picture. In addition to the primary coded picture, an
access unit may also contain one or more redundant coded pictures
or other NAL units not containing slices or slice data partitions
of a coded picture. The decoding of an access unit always results
in a decoded picture.
* instantaneous decoding refresh (IDR) access unit: An access unit in
which the primary coded picture is an IDR picture.
* instantaneous decoding refresh (IDR) picture: A coded picture
containing only slices with I or SI slice types that causes the
decoding process to mark all reference pictures as "unused for
reference" immediately after decoding the IDR picture. After the
decoding of an IDR picture all following coded pictures in decoding
order can be decoded without inter prediction from any picture
decoded prior to the IDR picture. The first picture of each coded
video sequence is an IDR picture.
* NAL unit: A syntax structure containing an indication of the type
of data to follow and bytes containing that data in the form of an
RBSP interspersed as necessary with emulation prevention bytes.
* raw byte sequence payload (RBSP): A syntax structure containing an
integer number of bytes that is encapsulated in a NAL unit. An RBSP
is either empty or has the form of a string of data bits containing
syntax elements followed by an RBSP stop bit and followed by zero
or more subsequent bits equal to 0.
* raw byte sequence payload (RBSP) stop bit: A bit equal to 1 present
within a raw byte sequence payload (RBSP) after a string of data
bits. The location of the end of the string of data bits within an
RBSP can be identified by searching from the end of the RBSP for
the RBSP stop bit, which is the last non-zero bit in the RBSP.
* parity: The parity of a field can be top or bottom.
* picture: A collective term for a field or a frame.
* picture parameter set: A syntax structure containing syntax
elements that apply to zero or more entire coded pictures as
determined by the pic_parameter_set_id syntax element found in each
slice header.
* primary coded picture: The coded representation of a picture to be
used by the decoding process for a bitstream conforming to this
Recommendation | International Standard. The primary coded picture
contains all macroblocks of the picture. The only pictures that
have a normative effect on the decoding process are primary coded
pictures. See also redundant coded picture.
* VCL: Video Coding Layer
- The VCL is specified to efficiently represent the content of the
video data. The NAL is specified to format that data and provide
header information in a manner appropriate for conveyance on a
variety of communication channels or storage media. All data are
contained in NAL units, each of which contains an integer number of
bytes. A NAL unit specifies a generic format for use in both
packet-oriented and bitstream systems. The format of NAL units for
both packet-oriented transport and byte stream is identical except
that each NAL unit can be preceded by a start code prefix and extra
padding bytes in the byte stream format.
*/
AVCFrameChecker::AVCFrameChecker()
{
pthread_mutex_init(&m_mutex, NULL);
m_rbsp_buffer_size = 188 * 2;
m_rbsp_buffer = new uint8_t[m_rbsp_buffer_size];
if (m_rbsp_buffer == 0)
{
m_rbsp_buffer_size = 0;
}
reset();
m_I_is_keyframe = true;
m_au_contains_keyframe_message = false;
alloc_nalu();
alloc_au();
}
AVCFrameChecker::~AVCFrameChecker()
{
pthread_mutex_lock(&m_mutex);
while (!m_aus.empty())
{
H264AU *au = m_aus.front();
m_aus.pop_front();
freeAU(au);
}
pthread_mutex_unlock(&m_mutex);
freeAU(m_au);
free_nalu(m_nalu);
delete[] m_rbsp_buffer;
pthread_mutex_destroy(&m_mutex);
}
void
AVCFrameChecker::reset()
{
m_seen_sps = false;
m_on_frame = false;
m_au_pending = false;
m_is_keyframe = false;
m_on_key_frame = false;
m_long_start_code = false;
m_au = 0;
m_nalu = 0;
m_pic_width = 0;
m_pic_height = 0;
m_sar_width = 0;
m_sar_height = 0;
m_timeScale = 0;
m_fixedRate = 0;
m_au_offset = 0;
m_sps_offset = 0;
m_pkt_offset = 0;
m_unitsInTick = 0;
m_num_ref_frames = 0;
m_pic_order_cnt_lsb = 0;
m_pic_order_cnt_type = 0;
m_log2_max_frame_num = 0;
m_prev_pic_order_cnt_lsb = 0;
m_prev_pic_order_cnt_type = 0;
m_frame_crop_top_offset = 0;
m_frame_crop_left_offset = 0;
m_frame_crop_right_offset = 0;
m_frame_crop_bottom_offset = 0;
m_delta_pic_order_cnt_bottom = 0;
m_delta_pic_order_cnt[0] = 0;
m_delta_pic_order_cnt[1] = 0;
m_prev_delta_pic_order_cnt[0] = 0;
m_prev_delta_pic_order_cnt[1] = 0;
m_prev_delta_pic_order_cnt_bottom = 0;
m_redundant_pic_cnt_present_flag = 0;
m_redundant_pic_cnt = 0;
m_aspect_ratio_idc = 0;
m_frame_start_offset = 0;
m_keyframe_start_offset = 0;
m_log2_max_pic_order_cnt_lsb = 0;
m_seq_parameter_set_id = 0;
m_delta_pic_order_always_zero_flag = 0;
m_separate_colour_plane_flag = 0;
m_frame_num = -1;
m_prev_frame_num = -1;
m_pic_parameter_set_id = -1;
m_prev_pic_parameter_set_id = -1;
m_prev_field_pic_flag = -1;
m_field_pic_flag = -1;
m_bottom_field_flag = -1;
m_prev_bottom_field_flag = -1;
m_chroma_format_idc = 1;
m_frame_mbs_only_flag = -1;
m_pic_order_present_flag = -1;
m_slice_type = SLICE_UNDEF;
m_prev_nal_ref_idc = m_nal_ref_idc = 111; // != [0|1|2|3]
m_prev_nal_unit_type = m_nal_unit_type = UNKNOWN;
///< The value of idr_pic_id shall be in the range of 0 to 65535, inclusive.
m_prev_idr_pic_id = m_idr_pic_id = 65536;
m_sync_accumulator = 0xffffffffffffffff;
resetRBSP();
}
void
AVCFrameChecker::alloc_au()
{
///< create new AU
m_au = new(std::nothrow) H264AU;
if (m_au)
{
m_au->slices = 0;
}
}
void
AVCFrameChecker::alloc_nalu()
{
///< create new NALU
m_nalu = new H264NALU;
if (m_nalu)
{
m_nalu->len = 0;
m_nalu->data = new(std::nothrow) uint8_t[naluinitsize];
assert(m_nalu->data);
if (!m_nalu->data)
{
delete m_nalu;
m_nalu = 0;
}
m_nalu->size = naluinitsize;
}
}
void
AVCFrameChecker::freeAU(H264AU *au)
{
if (au)
{
while (!au->nalus.empty())
{
H264NALU *nalu = au->nalus.front();
au->nalus.pop_front();
free_nalu(nalu);
}
delete au;
}
}
void
AVCFrameChecker::free_nalu(H264NALU *nalu)
{
if (nalu)
{
delete[] nalu->data;
delete nalu;
}
}
bool
AVCFrameChecker::fill_nalu(const uint8_t *data, int32_t len)
{
if (len > 0)
{
if (len + m_nalu->len > m_nalu->size)
{
int size = len + m_nalu->len + 10000; ///< 10k more
uint8_t *nalubuf = new(std::nothrow) uint8_t[size];
if (!nalubuf)
{
assert(0);
log("H264Parser::fillRBSP: FAILED to allocate NALU buffer!");
return false;
}
m_nalu->size = size;
memcpy(nalubuf, m_nalu->data, m_nalu->len);
delete[] m_nalu->data;
m_nalu->data = nalubuf;
}
memcpy(m_nalu->data + m_nalu->len, data, len);
}
m_nalu->len += len; ///< 'len' could be negative
return true;
}
bool
AVCFrameChecker::fill_nalu(const uint8_t * data, int32_t len, bool found_start_code)
{
if (found_start_code)
{
len -= ((m_sync_accumulator & 0x000000ff00000000) == 0 ? 5 : 4);
}
return fill_nalu(data, len);
}
void
AVCFrameChecker::finish_nalu()
{
if (m_nalu->len)
{
if (isSlice(m_nalu->type))
{
m_au->key = (m_nalu->type & 0x1f) == 5;
m_au->name = m_au->key ? "IDR" : "non-IDR";
++m_au->slices;
}
m_au->nalus.push_back(m_nalu);
alloc_nalu();
}
}
void
AVCFrameChecker::finish_au()
{
pthread_mutex_lock(&m_mutex);
if (m_au->nalus.size())
{
m_aus.push_back(m_au);
alloc_au();
}
pthread_mutex_unlock(&m_mutex);
}
void
AVCFrameChecker::write_nalu_header()
{
uint8_t head[5] = {'\x0', '\x0', '\x0', '\x1', m_sync_accumulator & 0xff};
m_nalu->type = m_nal_unit_type;
m_nalu->name = nalu_names[m_nal_unit_type];
fill_nalu(&head[m_long_start_code ? 0 : 1], m_long_start_code ? 5 : 4);
}
bool
AVCFrameChecker::is_new_au()
{
/* An access unit consists of one primary coded picture, zero or more
corresponding redundant coded pictures, and zero or more non-VCL NAL
units. The association of VCL NAL units to primary or redundant coded
pictures is described in subclause 7.4.1.2.5.
The first access unit in the bitstream starts with the first NAL unit
of the bitstream.
The first of any of the following NAL units after the last VCL NAL
unit of a primary coded picture specifies the start of a new access
unit.
- access unit delimiter NAL unit (when present)
- sequence parameter set NAL unit (when present)
- picture parameter set NAL unit (when present)
- SEI NAL unit (when present)
- NAL units with nal_unit_type in the range of 14 to 18, inclusive
- first VCL NAL unit of a primary coded picture (always present)
*/
/* 7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded
picture This subclause specifies constraints on VCL NAL unit syntax
that are sufficient to enable the detection of the first VCL NAL unit
of each primary coded picture.
Any coded slice NAL unit or coded slice data partition A NAL unit of
the primary coded picture of the current access unit shall be
different from any coded slice NAL unit or coded slice data partition
A NAL unit of the primary coded picture of the previous access unit in
one or more of the following ways.
- frame_num differs in value. The value of frame_num used to
test this condition is the value of frame_num that appears in
the syntax of the slice header, regardless of whether that value
is inferred to have been equal to 0 for subsequent use in the
decoding process due to the presence of
memory_management_control_operation equal to 5.
Note: If the current picture is an IDR picture FrameNum and
PrevRefFrameNum are set equal to 0.
- pic_parameter_set_id differs in value.
- field_pic_flag differs in value.
- bottom_field_flag is present in both and differs in value.
- nal_ref_idc differs in value with one of the nal_ref_idc
values being equal to 0.
- pic_order_cnt_type is equal to 0 for both and either
pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom
differs in value.
- pic_order_cnt_type is equal to 1 for both and either
delta_pic_order_cnt[0] differs in value, or
delta_pic_order_cnt[1] differs in value.
- nal_unit_type differs in value with one of the nal_unit_type values
being equal to 5.
- nal_unit_type is equal to 5 for both and idr_pic_id differs in
value.
NOTE - Some of the VCL NAL units in redundant coded pictures or some
non-VCL NAL units (e.g. an access unit delimiter NAL unit) may also
be used for the detection of the boundary between access units, and
may therefore aid in the detection of the start of a new primary
coded picture.
*/
bool result = false;
if (m_prev_frame_num != -1)
{
///< Need previous slice information for comparison
if (m_nal_unit_type != SLICE_IDR && m_frame_num != m_prev_frame_num)
{
result = true;
}
else if (m_prev_pic_parameter_set_id != -1 && m_pic_parameter_set_id != m_prev_pic_parameter_set_id)
{
result = true;
}
else if (m_field_pic_flag != m_prev_field_pic_flag)
{
result = true;
}
else if ((m_bottom_field_flag != -1 && m_prev_bottom_field_flag != -1) && m_bottom_field_flag != m_prev_bottom_field_flag)
{
result = true;
}
else if ((m_nal_ref_idc == 0 || m_prev_nal_ref_idc == 0) && m_nal_ref_idc != m_prev_nal_ref_idc)
{
result = true;
}
else if ((m_pic_order_cnt_type == 0 && m_prev_pic_order_cnt_type == 0) &&
(m_pic_order_cnt_lsb != m_prev_pic_order_cnt_lsb || m_delta_pic_order_cnt_bottom != m_prev_delta_pic_order_cnt_bottom))
{
result = true;
}
else if ((m_pic_order_cnt_type == 1 && m_prev_pic_order_cnt_type == 1) &&
(m_delta_pic_order_cnt[0] != m_prev_delta_pic_order_cnt[0] || m_delta_pic_order_cnt[1] != m_prev_delta_pic_order_cnt[1]))
{
result = true;
}
else if ((m_nal_unit_type == SLICE_IDR || m_prev_nal_unit_type == SLICE_IDR) && m_nal_unit_type != m_prev_nal_unit_type)
{
result = true;
}
else if ((m_nal_unit_type == SLICE_IDR && m_prev_nal_unit_type == SLICE_IDR) && m_idr_pic_id != m_prev_idr_pic_id)
{
result = true;
}
}
m_prev_frame_num = m_frame_num;
m_prev_pic_parameter_set_id = m_pic_parameter_set_id;
m_prev_field_pic_flag = m_field_pic_flag;
m_prev_bottom_field_flag = m_bottom_field_flag;
m_prev_nal_ref_idc = m_nal_ref_idc;
m_prev_pic_order_cnt_lsb = m_pic_order_cnt_lsb;
m_prev_delta_pic_order_cnt_bottom = m_delta_pic_order_cnt_bottom;
m_prev_delta_pic_order_cnt[0] = m_delta_pic_order_cnt[0];
m_prev_delta_pic_order_cnt[1] = m_delta_pic_order_cnt[1];
m_prev_nal_unit_type = m_nal_unit_type;
m_prev_idr_pic_id = m_idr_pic_id;
return result;
}
void
AVCFrameChecker::resetRBSP(void)
{
m_rbsp_index = 0;
m_consecutive_zeros = 0;
m_have_unfinished_NAL = false;
}
bool
AVCFrameChecker::fillRBSP(const uint8_t *byteP, uint32_t byte_count, bool found_start_code)
{
///< bitstream buffer must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual data
uint32_t required_size = m_rbsp_index + byte_count + FF_INPUT_BUFFER_PADDING_SIZE;
if (m_rbsp_buffer_size < required_size)
{
///< Round up to packet size
required_size = ((required_size / 188) + 1) * 188;
///< Need a bigger buffer
uint8_t *new_buffer = new(std::nothrow) uint8_t[required_size];
if (new_buffer == NULL)
{
///< Allocation failed. Discard the new bytes
log("H264Parser::fillRBSP: FAILED to allocate RBSP buffer!");
return false;
}
///< copy across bytes from old buffer
memcpy(new_buffer, m_rbsp_buffer, m_rbsp_index);
delete[] m_rbsp_buffer;
m_rbsp_buffer = new_buffer;
m_rbsp_buffer_size = required_size;
}
///< fill rbsp while we have data
while (byte_count)
{
///< copy the byte into the rbsp, unless it is the 0x03 in a 0x000003
if (m_consecutive_zeros < 2 || *byteP != 0x03)
{
m_rbsp_buffer[m_rbsp_index++] = *byteP;
}
if (*byteP == 0)
{
++m_consecutive_zeros;
assert(m_consecutive_zeros <= 3);
if (m_consecutive_zeros == 3)
{
assert(*(byteP + 1) == 1);
}
}
else
{
m_consecutive_zeros = 0;
}
++byteP;
--byte_count;
}
/* If we've found the next start code then that, plus the first byte of
* the next NAL, plus the preceding zero bytes will all be in the rbsp
* buffer. Move rbsp_index++ back to the end of the actual rbsp data. We
* need to know the correct size of the rbsp to decode some NALs.
*/
if (found_start_code)
{
if (m_rbsp_index >= 4)
{
m_rbsp_index -= 4;
while (m_rbsp_index > 0 && m_rbsp_buffer[m_rbsp_index - 1] == 0) ///< zhp: last byte of a nalu is not '\0'
{
--m_rbsp_index;
}
}
else
{
/* This should never happen. */
assert(0);
log("H264Parser::fillRBSP: Found start code, rbsp_index is %d but it should be >4", m_rbsp_index);
}
}
///< stick some 0xff on the end for get_bits to run into
memset(&m_rbsp_buffer[m_rbsp_index], 0xff, FF_INPUT_BUFFER_PADDING_SIZE);
return true;
}
// == NAL_type AU_delimiter: primary_pic_type = 5
int
AVCFrameChecker::isKeySlice(uint32_t slice_type)
{
return (slice_type == SLICE_I || slice_type == SLICE_SI ||
slice_type == SLICE_I_a || slice_type == SLICE_SI_a);
}
bool
AVCFrameChecker::isSlice(uint8_t nal_type)
{
return (nal_type == SLICE || nal_type == SLICE_DPA || nal_type == SLICE_IDR);
}
void
AVCFrameChecker::set_AU_pending()
{
if (!m_au_pending)
{
m_au_pending = true;
m_au_offset = m_pkt_offset;
m_au_contains_keyframe_message = false;
finish_au(); ///< store the last AU if any.
}
}
void
AVCFrameChecker::flush()
{
if (m_have_unfinished_NAL)
{
finish_nalu();
finish_au();
resetRBSP();
}
}
uint32_t
AVCFrameChecker::addBytes(const uint8_t * bytes, const uint32_t byte_count, const uint64_t stream_offset)
{
const uint8_t *startP = bytes;
const uint8_t *endP;
bool found_start_code;
bool good_nal_unit;
m_on_frame = false;
m_on_key_frame = false;
///< parse at most 'byte_count' bytes until we get a new frame.
while (startP < bytes + byte_count && !m_on_frame)
{
endP = find_start_code(startP, bytes + byte_count, &m_sync_accumulator);
found_start_code = (m_sync_accumulator & 0xffffff00) == 0x00000100;
fill_nalu(startP, endP - startP, found_start_code);
/* Between startP and endP we potentially have some more
* bytes of a NAL that we've been parsing (plus some bytes of the next start code)
*/
if (m_have_unfinished_NAL)
{
///< append all bytes to the RBSP buffer before the start code if there is any.
if (!fillRBSP(startP, endP - startP, found_start_code))
{
resetRBSP();
return endP - bytes;
}
///< process one NALU (call may set m_have_unfinished_NAL to false)
processRBSP(found_start_code);
}
///< process the current NAL
///< we have dealt with every byte up to endP
startP = endP;
///< a new NAL starts
if (found_start_code)
{
assert(!m_have_unfinished_NAL);
if (m_have_unfinished_NAL)
{
/* We've found a new start code, without completely parsing the previous NAL.
* Either there's a problem with the stream or with this parser.
*/
log("H264Parser::addBytes: Found new start code, but previous NAL is incomplete!");
}
/* Prepare for the new NAL */
resetRBSP();
/* If we find the start of an AU somewhere from here to the next start code,
* the offset to associate with it is the one passed in to this call,
* not any of the subsequent calls.
*/
///< offset of the current NAL
m_long_start_code = (m_sync_accumulator & 0x000000ff00000000) == 0;
m_pkt_offset = stream_offset + (startP - (m_long_start_code ? 5 : 4) - bytes);
/* nal_unit_type specifies the type of RBSP data structure
contained in the NAL unit as specified in Table 7-1.
VCL NAL units are specified as those NAL units having
nal_unit_type equal to 1 to 5, inclusive. All remaining
NAL units are called non-VCL NAL units:
0 Unspecified
1 Coded slice of a non-IDR picture slice_layer_without_partitioning_rbsp( )
2 Coded slice data partition A slice_data_partition_a_layer_rbsp( )
3 Coded slice data partition B slice_data_partition_b_layer_rbsp( )
4 Coded slice data partition C slice_data_partition_c_layer_rbsp( )
5 Coded slice of an IDR picture slice_layer_without_partitioning_rbsp( )
6 Supplemental enhancement information (SEI) 5 sei_rbsp( )
7 Sequence parameter set (SPS) seq_parameter_set_rbsp( )
8 Picture parameter set pic_parameter_set_rbsp( )
9 Access unit delimiter access_unit_delimiter_rbsp( )
10 End of sequence end_of_seq_rbsp( )
11 End of stream end_of_stream_rbsp( )
*/
m_nal_unit_type = m_sync_accumulator & 0x1f;
m_nal_ref_idc = (m_sync_accumulator >> 5) & 0x3;
good_nal_unit = true;
if (m_nal_ref_idc)
{
/* nal_ref_idc shall be equal to 0 for all NAL units having
* nal_unit_type equal to 6, 9, 10, 11, or 12.
*/
if (m_nal_unit_type == SEI || (m_nal_unit_type >= AU_DELIMITER && m_nal_unit_type <= FILLER_DATA))
{
good_nal_unit = false;
}
}
else
{
/* nal_ref_idc shall not be equal to 0 for NAL units with
* nal_unit_type equal to 5
*/
if (m_nal_unit_type == SLICE_IDR)
{
good_nal_unit = false;
}
}
assert(good_nal_unit);
if (good_nal_unit)
{
finish_nalu(); ///< the last NALU if any.
if (isSlice(m_nal_unit_type) ||
m_nal_unit_type == SPS || m_nal_unit_type == PPS || m_nal_unit_type == SEI)
{
/* This is a NAL we need to parse.
* We may have the body of it in the part of the stream past to us this call,
* or we may get the rest in subsequent calls to addBytes. Either way, we set
* have_unfinished_NAL, so that we start filling the RBSP buffer.
*/
m_have_unfinished_NAL = true;
}
else if (m_nal_unit_type == AU_DELIMITER ||
(m_nal_unit_type > SPS_EXT && m_nal_unit_type < AUXILIARY_SLICE))
{
set_AU_pending();
}
else
{
///< ignore all other NALs.
log("H264Parser::addbytes: drop one NAL unit(type = %d).", m_nal_unit_type);
}
write_nalu_header();
}
else
{
///< malformed NAL, drop this one.
log("H264Parser::addbytes: malformed NAL units.");
}
}
}
return startP - bytes;
}
void
AVCFrameChecker::processRBSP(bool rbsp_complete)
{
GetBitContext gb;
init_get_bits(&gb, m_rbsp_buffer, 8 * m_rbsp_index);
if (m_nal_unit_type == SEI)
{
///< SEI cannot be parsed without knowing its size.
if (!rbsp_complete)
{
return;
}
set_AU_pending();
decode_SEI(&gb);
}
else if (m_nal_unit_type == SPS)
{
///< best wait until we have the whole thing
if (!rbsp_complete)
{
return;
}
set_AU_pending();
if (!m_seen_sps)
{
m_sps_offset = m_pkt_offset;
}
decode_SPS(&gb);
}
else if (m_nal_unit_type == PPS)
{
///< best wait until we have the whole thing
if (!rbsp_complete)
{
return;
}
set_AU_pending();
decode_PPS(&gb);
}
else
{
///< slice
///< Only parse the slice headers, so return only if we have insufficient bytes.
if (!rbsp_complete/* && m_rbsp_index < MAX_SLICE_HEADER_SIZE*/) ///<!!! we wait for the whole slice just to copy the stream.
{
return;
}
decode_Header(&gb);
m_au->type = fieldType();
if (is_new_au())
{
set_AU_pending();
}
}
///< we managed to parse a sufficient prefix of the current NAL, so go onto the next.
m_have_unfinished_NAL = false;
if (m_au_pending && isSlice(m_nal_unit_type))
{
/* once we know the slice type of a new AU, we can
* determine if it is a keyframe or just a frame
*/
m_au_pending = false;
m_on_frame = true;
m_frame_start_offset = m_au_offset;
if (m_is_keyframe || m_au_contains_keyframe_message)
{
m_on_key_frame = true;
m_keyframe_start_offset = m_au_offset;
}
}
}
/*
* 7.4.3 slice header semantics
*/
bool
AVCFrameChecker::decode_Header(GetBitContext *gb)
{
m_is_keyframe = false;
if (m_log2_max_frame_num == 0)
{
///< SPS has not been parsed yet
return false;
}
/* first_mb_in_slice specifies the address of the first macroblock
in the slice. When arbitrary slice order is not allowed as
specified in Annex A, the value of first_mb_in_slice is
constrained as follows.
- If separate_colour_plane_flag is equal to 0, the value of
first_mb_in_slice shall not be less than the value of
first_mb_in_slice for any other slice of the current picture
that precedes the current slice in decoding order.
- Otherwise (separate_colour_plane_flag is equal to 1), the value of
first_mb_in_slice shall not be less than the value of
first_mb_in_slice for any other slice of the current picture
that precedes the current slice in decoding order and has the
same value of colour_plane_id.
*/
uint8_t first_mb_in_slice = get_ue_golomb(gb);
/* slice_type specifies the coding type of the slice according to
Table 7-6. e.g. P, B, I, SP, SI
When nal_unit_type is equal to 5 (IDR picture), slice_type shall
be equal to 2, 4, 7, or 9 (I or SI)
*/
m_slice_type = get_ue_golomb_31(gb);
/* s->pict_type = golomb_to_pict_type[slice_type % 5];
*/
/* pic_parameter_set_id specifies the picture parameter set in
use. The value of pic_parameter_set_id shall be in the range of
0 to 255, inclusive.
*/
m_pic_parameter_set_id = get_ue_golomb(gb);
/* separate_colour_plane_flag equal to 1 specifies that the three
colour components of the 4:4:4 chroma format are coded
separately. separate_colour_plane_flag equal to 0 specifies that
the colour components are not coded separately. When
separate_colour_plane_flag is not present, it shall be inferred
to be equal to 0. When separate_colour_plane_flag is equal to 1,
the primary coded picture consists of three separate components,
each of which consists of coded samples of one colour plane (Y,
Cb or Cr) that each use the monochrome coding syntax. In this
case, each colour plane is associated with a specific
colour_plane_id value.
*/
if (m_separate_colour_plane_flag)
{
get_bits(gb, 2); ///< colour_plane_id
}
/* frame_num is used as an identifier for pictures and shall be
represented by log2_max_frame_num_minus4 + 4 bits in the
bitstream....
If the current picture is an IDR picture, frame_num shall be equal to 0.
When max_num_ref_frames is equal to 0, slice_type shall be equal to 2, 4, 7, or 9.
*/
m_frame_num = get_bits(gb, m_log2_max_frame_num);
/* field_pic_flag equal to 1 specifies that the slice is a slice of a
coded field. field_pic_flag equal to 0 specifies that the slice is a
slice of a coded frame. When field_pic_flag is not present it shall be
inferred to be equal to 0.
bottom_field_flag equal to 1 specifies that the slice is part of a
coded bottom field. bottom_field_flag equal to 0 specifies that the
picture is a coded top field. When this syntax element is not present
for the current slice, it shall be inferred to be equal to 0.
*/
if (!m_frame_mbs_only_flag)
{
m_field_pic_flag = get_bits1(gb);
m_bottom_field_flag = m_field_pic_flag ? get_bits1(gb) : 0;
}
else
{
m_field_pic_flag = 0;
m_bottom_field_flag = -1;
}
/* idr_pic_id identifies an IDR picture. The values of idr_pic_id
in all the slices of an IDR picture shall remain unchanged. When
two consecutive access units in decoding order are both IDR
access units, the value of idr_pic_id in the slices of the first
such IDR access unit shall differ from the idr_pic_id in the
second such IDR access unit. The value of idr_pic_id shall be in
the range of 0 to 65535, inclusive.
*/
if (m_nal_unit_type == SLICE_IDR)
{
m_idr_pic_id = get_ue_golomb(gb);
m_is_keyframe = true;
}
else
{
m_is_keyframe = (m_I_is_keyframe && isKeySlice(m_slice_type));
}
/* pic_order_cnt_lsb specifies the picture order count modulo
MaxPicOrderCntLsb for the top field of a coded frame or for a coded
field. The size of the pic_order_cnt_lsb syntax element is
log2_max_pic_order_cnt_lsb_minus4 + 4 bits. The value of the
pic_order_cnt_lsb shall be in the range of 0 to MaxPicOrderCntLsb â?1,
inclusive.
delta_pic_order_cnt_bottom specifies the picture order count
difference between the bottom field and the top field of a coded