-
Notifications
You must be signed in to change notification settings - Fork 116
/
h264_stream.cpp
2458 lines (2248 loc) · 90.2 KB
/
h264_stream.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
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
/*
* h264bitstream - a library for reading and writing H.264 video
* Copyright (C) 2005-2007 Auroras Entertainment, LLC
* Copyright (C) 2008-2011 Avail-TVN
*
* Written by Alex Izvorski <aizvorski@gmail.com> and Alex Giladi <alex.giladi@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "stdafx.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "bs.h"
#include "h264_stream.h"
#include "h264_sei.h"
FILE* h264_dbgfile = NULL;
//存放信息的字符串
//char* tempstr=(char *)malloc(1000);
extern char tempstr[1000];
//char* outputstr=(char *)malloc(100000);
extern char outputstr[100000];
//#define printf(...) fprintf((h264_dbgfile == NULL ? stdout : h264_dbgfile), __VA_ARGS__)
//MFC下控制台输出
//#define printf(...) TRACE( __VA_ARGS__)
//组合成适合MFC的字符串
//注意MFC中EditControl的换行符是\r\n,需要单独添加
#define printf(...) sprintf( tempstr,__VA_ARGS__);\
strcat(tempstr,"\r\n"); \
strcat(outputstr,tempstr);
/**
Calculate the log base 2 of the argument, rounded up.
Zero or negative arguments return zero
Idea from http://www.southwindsgames.com/blog/2009/01/19/fast-integer-log2-function-in-cc/
*/
int intlog2(int x)
{
int log = 0;
if (x < 0) { x = 0; }
while ((x >> log) > 0)
{
log++;
}
if (log > 0 && x == 1<<(log-1)) { log--; }
return log;
}
int is_slice_type(int slice_type, int cmp_type)
{
if (slice_type >= 5) { slice_type -= 5; }
if (cmp_type >= 5) { cmp_type -= 5; }
if (slice_type == cmp_type) { return 1; }
else { return 0; }
}
/***************************** reading ******************************/
/**
Create a new H264 stream object. Allocates all structures contained within it.
@return the stream object
*/
h264_stream_t* h264_new()
{
h264_stream_t* h = (h264_stream_t*)calloc(1, sizeof(h264_stream_t));
h->nal = (nal_t*)calloc(1, sizeof(nal_t));
// initialize tables
for ( int i = 0; i < 32; i++ ) { h->sps_table[i] = (sps_t*)calloc(1, sizeof(sps_t)); }
for ( int i = 0; i < 256; i++ ) { h->pps_table[i] = (pps_t*)calloc(1, sizeof(pps_t)); }
h->sps = h->sps_table[0];
h->pps = h->pps_table[0];
h->aud = (aud_t*)calloc(1, sizeof(aud_t));
h->num_seis = 0;
h->seis = NULL;
h->sei = NULL; //This is a TEMP pointer at whats in h->seis...
h->sh = (slice_header_t*)calloc(1, sizeof(slice_header_t));
return h;
}
/**
Free an existing H264 stream object. Frees all contained structures.
@param[in,out] h the stream object
*/
void h264_free(h264_stream_t* h)
{
free(h->nal);
for ( int i = 0; i < 32; i++ ) { free( h->sps_table[i] ); }
for ( int i = 0; i < 256; i++ ) { free( h->pps_table[i] ); }
free(h->aud);
if(h->seis != NULL)
{
for( int i = 0; i < h->num_seis; i++ )
{
sei_t* sei = h->seis[i];
sei_free(sei);
}
free(h->seis);
}
free(h->sh);
free(h);
}
/**
Find the beginning and end of a NAL (Network Abstraction Layer) unit in a byte buffer containing H264 bitstream data.
@param[in] buf the buffer
@param[in] size the size of the buffer
@param[out] nal_start the beginning offset of the nal
@param[out] nal_end the end offset of the nal
@return the length of the nal, or 0 if did not find start of nal, or -1 if did not find end of nal
*/
// DEPRECATED - this will be replaced by a similar function with a slightly different API
int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end)
{
int i;
// find start
*nal_start = 0;
*nal_end = 0;
i = 0;
while ( //( next_bits( 24 ) != 0x000001 && next_bits( 32 ) != 0x00000001 )
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) &&
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0x01)
)
{
i++; // skip leading zero
if (i+4 >= size) { return 0; } // did not find nal start
}
if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) // ( next_bits( 24 ) != 0x000001 )
{
i++;
}
if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) { /* error, should never happen */ return 0; }
i+= 3;
*nal_start = i;
while ( //( next_bits( 24 ) != 0x000000 && next_bits( 24 ) != 0x000001 )
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0) &&
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01)
)
{
i++;
// FIXME the next line fails when reading a nal that ends exactly at the end of the data
if (i+3 >= size) { *nal_end = size; return -1; } // did not find nal end, stream ended first
}
*nal_end = i;
return (*nal_end - *nal_start);
}
int more_rbsp_data(h264_stream_t* h, bs_t* b)
{
if ( bs_eof(b) ) { return 0; }
if ( bs_peek_u1(b) == 1 ) { return 0; } // if next bit is 1, we've reached the stop bit
return 1;
}
/**
Convert RBSP data to NAL data (Annex B format).
The size of nal_buf must be 4/3 * the size of the rbsp_buf (rounded up) to guarantee the output will fit.
If that is not true, output may be truncated and an error will be returned.
If that is true, there is no possible error during this conversion.
@param[in] rbsp_buf the rbsp data
@param[in] rbsp_size pointer to the size of the rbsp data
@param[in,out] nal_buf allocated memory in which to put the nal data
@param[in,out] nal_size as input, pointer to the maximum size of the nal data; as output, filled in with the actual size of the nal data
@return actual size of nal data, or -1 on error
*/
// 7.3.1 NAL unit syntax
// 7.4.1.1 Encapsulation of an SODB within an RBSP
int rbsp_to_nal(const uint8_t* rbsp_buf, const int* rbsp_size, uint8_t* nal_buf, int* nal_size)
{
int i;
int j = 1;
int count = 0;
if (*nal_size > 0) { nal_buf[0] = 0x00; } // zero out first byte since we start writing from second byte
for ( i = 0; i < *rbsp_size ; i++ )
{
if ( j >= *nal_size )
{
// error, not enough space
return -1;
}
if ( ( count == 2 ) && !(rbsp_buf[i] & 0xFC) ) // HACK 0xFC
{
nal_buf[j] = 0x03;
j++;
count = 0;
}
nal_buf[j] = rbsp_buf[i];
if ( rbsp_buf[i] == 0x00 )
{
count++;
}
else
{
count = 0;
}
j++;
}
*nal_size = j;
return j;
}
/**
Convert NAL data (Annex B format) to RBSP data.
The size of rbsp_buf must be the same as size of the nal_buf to guarantee the output will fit.
If that is not true, output may be truncated and an error will be returned.
Additionally, certain byte sequences in the input nal_buf are not allowed in the spec and also cause the conversion to fail and an error to be returned.
@param[in] nal_buf the nal data
@param[in,out] nal_size as input, pointer to the size of the nal data; as output, filled in with the actual size of the nal data
@param[in,out] rbsp_buf allocated memory in which to put the rbsp data
@param[in,out] rbsp_size as input, pointer to the maximum size of the rbsp data; as output, filled in with the actual size of rbsp data
@return actual size of rbsp data, or -1 on error
*/
// 7.3.1 NAL unit syntax
// 7.4.1.1 Encapsulation of an SODB within an RBSP
int nal_to_rbsp(const uint8_t* nal_buf, int* nal_size, uint8_t* rbsp_buf, int* rbsp_size)
{
int i;
int j = 0;
int count = 0;
for( i = 1; i < *nal_size; i++ )
{
// in NAL unit, 0x000000, 0x000001 or 0x000002 shall not occur at any byte-aligned position
if( ( count == 2 ) && ( nal_buf[i] < 0x03) )
{
return -1;
}
if( ( count == 2 ) && ( nal_buf[i] == 0x03) )
{
// check the 4th byte after 0x000003, except when cabac_zero_word is used, in which case the last three bytes of this NAL unit must be 0x000003
if((i < *nal_size - 1) && (nal_buf[i+1] > 0x03))
{
return -1;
}
// if cabac_zero_word is used, the final byte of this NAL unit(0x03) is discarded, and the last two bytes of RBSP must be 0x0000
if(i == *nal_size - 1)
{
break;
}
i++;
count = 0;
}
if ( j >= *rbsp_size )
{
// error, not enough space
return -1;
}
rbsp_buf[j] = nal_buf[i];
if(nal_buf[i] == 0x00)
{
count++;
}
else
{
count = 0;
}
j++;
}
*nal_size = i;
*rbsp_size = j;
return j;
}
/**
Read a NAL unit from a byte buffer.
The buffer must start exactly at the beginning of the nal (after the start prefix).
The NAL is read into h->nal and into other fields within h depending on its type (check h->nal->nal_unit_type after reading).
@param[in,out] h the stream object
@param[in] buf the buffer
@param[in] size the size of the buffer
@return the length of data actually read, or -1 on error
*/
//7.3.1 NAL unit syntax
int read_nal_unit(h264_stream_t* h, uint8_t* buf, int size)
{
nal_t* nal = h->nal;
bs_t* b = bs_new(buf, size);
nal->forbidden_zero_bit = bs_read_f(b,1);
nal->nal_ref_idc = bs_read_u(b,2);
nal->nal_unit_type = bs_read_u(b,5);
nal->parsed = NULL;
nal->sizeof_parsed = 0;
bs_free(b);
int nal_size = size;
int rbsp_size = size;
uint8_t* rbsp_buf = (uint8_t*)malloc(rbsp_size);
int rc = nal_to_rbsp(buf, &nal_size, rbsp_buf, &rbsp_size);
if (rc < 0) { free(rbsp_buf); return -1; } // handle conversion error
b = bs_new(rbsp_buf, rbsp_size);
switch ( nal->nal_unit_type )
{
case NAL_UNIT_TYPE_CODED_SLICE_IDR:
case NAL_UNIT_TYPE_CODED_SLICE_NON_IDR:
case NAL_UNIT_TYPE_CODED_SLICE_AUX:
read_slice_layer_rbsp(h, b);
nal->parsed = h->sh;
nal->sizeof_parsed = sizeof(slice_header_t);
break;
case NAL_UNIT_TYPE_SEI:
read_sei_rbsp(h, b);
nal->parsed = h->sei;
nal->sizeof_parsed = sizeof(sei_t);
break;
case NAL_UNIT_TYPE_SPS:
read_seq_parameter_set_rbsp(h, b);
nal->parsed = h->sps;
nal->sizeof_parsed = sizeof(sps_t);
break;
case NAL_UNIT_TYPE_PPS:
read_pic_parameter_set_rbsp(h, b);
nal->parsed = h->pps;
nal->sizeof_parsed = sizeof(pps_t);
break;
case NAL_UNIT_TYPE_AUD:
read_access_unit_delimiter_rbsp(h, b);
nal->parsed = h->aud;
nal->sizeof_parsed = sizeof(aud_t);
break;
case NAL_UNIT_TYPE_END_OF_SEQUENCE:
read_end_of_seq_rbsp(h, b);
break;
case NAL_UNIT_TYPE_END_OF_STREAM:
read_end_of_stream_rbsp(h, b);
break;
//case NAL_UNIT_TYPE_FILLER:
//case NAL_UNIT_TYPE_SPS_EXT:
//case NAL_UNIT_TYPE_UNSPECIFIED:
//case NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_A:
//case NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_B:
//case NAL_UNIT_TYPE_CODED_SLICE_DATA_PARTITION_C:
default:
// here comes the reserved/unspecified/ignored stuff
nal->parsed = NULL;
nal->sizeof_parsed = 0;
return 0;
}
if (bs_overrun(b)) { bs_free(b); free(rbsp_buf); return -1; }
bs_free(b);
free(rbsp_buf);
return nal_size;
}
/**
Read only the NAL headers (enough to determine unit type) from a byte buffer.
@return unit type if read successfully, or -1 if this doesn't look like a nal
*/
int peek_nal_unit(h264_stream_t* h, uint8_t* buf, int size)
{
nal_t* nal = h->nal;
bs_t* b = bs_new(buf, size);
nal->forbidden_zero_bit = bs_read_f(b,1);
nal->nal_ref_idc = bs_read_u(b,2);
nal->nal_unit_type = bs_read_u(b,5);
bs_free(b);
// basic verification, per 7.4.1
if ( nal->forbidden_zero_bit ) { return -1; }
if ( nal->nal_unit_type <= 0 || nal->nal_unit_type > 20 ) { return -1; }
if ( nal->nal_unit_type > 15 && nal->nal_unit_type < 19 ) { return -1; }
if ( nal->nal_ref_idc == 0 )
{
if ( nal->nal_unit_type == NAL_UNIT_TYPE_CODED_SLICE_IDR )
{
return -1;
}
}
else
{
if ( nal->nal_unit_type == NAL_UNIT_TYPE_SEI ||
nal->nal_unit_type == NAL_UNIT_TYPE_AUD ||
nal->nal_unit_type == NAL_UNIT_TYPE_END_OF_SEQUENCE ||
nal->nal_unit_type == NAL_UNIT_TYPE_END_OF_STREAM ||
nal->nal_unit_type == NAL_UNIT_TYPE_FILLER )
{
return -1;
}
}
return nal->nal_unit_type;
}
//7.3.2.1 Sequence parameter set RBSP syntax
void read_seq_parameter_set_rbsp(h264_stream_t* h, bs_t* b)
{
int i;
// NOTE can't read directly into sps because seq_parameter_set_id not yet known and so sps is not selected
int profile_idc = bs_read_u8(b);
int constraint_set0_flag = bs_read_u1(b);
int constraint_set1_flag = bs_read_u1(b);
int constraint_set2_flag = bs_read_u1(b);
int constraint_set3_flag = bs_read_u1(b);
int constraint_set4_flag = bs_read_u1(b);
int constraint_set5_flag = bs_read_u1(b);
int reserved_zero_2bits = bs_read_u(b,2); /* all 0's */
int level_idc = bs_read_u8(b);
int seq_parameter_set_id = bs_read_ue(b);
// select the correct sps
h->sps = h->sps_table[seq_parameter_set_id];
sps_t* sps = h->sps;
memset(sps, 0, sizeof(sps_t));
sps->chroma_format_idc = 1;
sps->profile_idc = profile_idc; // bs_read_u8(b);
sps->constraint_set0_flag = constraint_set0_flag;//bs_read_u1(b);
sps->constraint_set1_flag = constraint_set1_flag;//bs_read_u1(b);
sps->constraint_set2_flag = constraint_set2_flag;//bs_read_u1(b);
sps->constraint_set3_flag = constraint_set3_flag;//bs_read_u1(b);
sps->constraint_set4_flag = constraint_set4_flag;//bs_read_u1(b);
sps->constraint_set5_flag = constraint_set5_flag;//bs_read_u1(b);
sps->reserved_zero_2bits = reserved_zero_2bits;//bs_read_u(b,2);
sps->level_idc = level_idc; //bs_read_u8(b);
sps->seq_parameter_set_id = seq_parameter_set_id; // bs_read_ue(b);
if( sps->profile_idc == 100 || sps->profile_idc == 110 ||
sps->profile_idc == 122 || sps->profile_idc == 144 )
{
sps->chroma_format_idc = bs_read_ue(b);
if( sps->chroma_format_idc == 3 )
{
sps->residual_colour_transform_flag = bs_read_u1(b);
}
sps->bit_depth_luma_minus8 = bs_read_ue(b);
sps->bit_depth_chroma_minus8 = bs_read_ue(b);
sps->qpprime_y_zero_transform_bypass_flag = bs_read_u1(b);
sps->seq_scaling_matrix_present_flag = bs_read_u1(b);
if( sps->seq_scaling_matrix_present_flag )
{
for( i = 0; i < 8; i++ )
{
sps->seq_scaling_list_present_flag[ i ] = bs_read_u1(b);
if( sps->seq_scaling_list_present_flag[ i ] )
{
if( i < 6 )
{
read_scaling_list( b, sps->ScalingList4x4[ i ], 16,
sps->UseDefaultScalingMatrix4x4Flag[ i ]);
}
else
{
read_scaling_list( b, sps->ScalingList8x8[ i - 6 ], 64,
sps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
}
}
}
}
}
sps->log2_max_frame_num_minus4 = bs_read_ue(b);
sps->pic_order_cnt_type = bs_read_ue(b);
if( sps->pic_order_cnt_type == 0 )
{
sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue(b);
}
else if( sps->pic_order_cnt_type == 1 )
{
sps->delta_pic_order_always_zero_flag = bs_read_u1(b);
sps->offset_for_non_ref_pic = bs_read_se(b);
sps->offset_for_top_to_bottom_field = bs_read_se(b);
sps->num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue(b);
for( i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++ )
{
sps->offset_for_ref_frame[ i ] = bs_read_se(b);
}
}
sps->num_ref_frames = bs_read_ue(b);
sps->gaps_in_frame_num_value_allowed_flag = bs_read_u1(b);
sps->pic_width_in_mbs_minus1 = bs_read_ue(b);
sps->pic_height_in_map_units_minus1 = bs_read_ue(b);
sps->frame_mbs_only_flag = bs_read_u1(b);
if( !sps->frame_mbs_only_flag )
{
sps->mb_adaptive_frame_field_flag = bs_read_u1(b);
}
sps->direct_8x8_inference_flag = bs_read_u1(b);
sps->frame_cropping_flag = bs_read_u1(b);
if( sps->frame_cropping_flag )
{
sps->frame_crop_left_offset = bs_read_ue(b);
sps->frame_crop_right_offset = bs_read_ue(b);
sps->frame_crop_top_offset = bs_read_ue(b);
sps->frame_crop_bottom_offset = bs_read_ue(b);
}
sps->vui_parameters_present_flag = bs_read_u1(b);
if( sps->vui_parameters_present_flag )
{
read_vui_parameters(h, b);
}
read_rbsp_trailing_bits(h, b);
}
//7.3.2.1.1 Scaling list syntax
void read_scaling_list(bs_t* b, int* scalingList, int sizeOfScalingList, int useDefaultScalingMatrixFlag )
{
int j;
if(scalingList == NULL)
{
return;
}
int lastScale = 8;
int nextScale = 8;
for( j = 0; j < sizeOfScalingList; j++ )
{
if( nextScale != 0 )
{
int delta_scale = bs_read_se(b);
nextScale = ( lastScale + delta_scale + 256 ) % 256;
useDefaultScalingMatrixFlag = ( j == 0 && nextScale == 0 );
}
scalingList[ j ] = ( nextScale == 0 ) ? lastScale : nextScale;
lastScale = scalingList[ j ];
}
}
//Appendix E.1.1 VUI parameters syntax
void read_vui_parameters(h264_stream_t* h, bs_t* b)
{
sps_t* sps = h->sps;
sps->vui.aspect_ratio_info_present_flag = bs_read_u1(b);
if( sps->vui.aspect_ratio_info_present_flag )
{
sps->vui.aspect_ratio_idc = bs_read_u8(b);
if( sps->vui.aspect_ratio_idc == SAR_Extended )
{
sps->vui.sar_width = bs_read_u(b,16);
sps->vui.sar_height = bs_read_u(b,16);
}
}
sps->vui.overscan_info_present_flag = bs_read_u1(b);
if( sps->vui.overscan_info_present_flag )
{
sps->vui.overscan_appropriate_flag = bs_read_u1(b);
}
sps->vui.video_signal_type_present_flag = bs_read_u1(b);
if( sps->vui.video_signal_type_present_flag )
{
sps->vui.video_format = bs_read_u(b,3);
sps->vui.video_full_range_flag = bs_read_u1(b);
sps->vui.colour_description_present_flag = bs_read_u1(b);
if( sps->vui.colour_description_present_flag )
{
sps->vui.colour_primaries = bs_read_u8(b);
sps->vui.transfer_characteristics = bs_read_u8(b);
sps->vui.matrix_coefficients = bs_read_u8(b);
}
}
sps->vui.chroma_loc_info_present_flag = bs_read_u1(b);
if( sps->vui.chroma_loc_info_present_flag )
{
sps->vui.chroma_sample_loc_type_top_field = bs_read_ue(b);
sps->vui.chroma_sample_loc_type_bottom_field = bs_read_ue(b);
}
sps->vui.timing_info_present_flag = bs_read_u1(b);
if( sps->vui.timing_info_present_flag )
{
sps->vui.num_units_in_tick = bs_read_u(b,32);
sps->vui.time_scale = bs_read_u(b,32);
sps->vui.fixed_frame_rate_flag = bs_read_u1(b);
}
sps->vui.nal_hrd_parameters_present_flag = bs_read_u1(b);
if( sps->vui.nal_hrd_parameters_present_flag )
{
read_hrd_parameters(h, b);
}
sps->vui.vcl_hrd_parameters_present_flag = bs_read_u1(b);
if( sps->vui.vcl_hrd_parameters_present_flag )
{
read_hrd_parameters(h, b);
}
if( sps->vui.nal_hrd_parameters_present_flag || sps->vui.vcl_hrd_parameters_present_flag )
{
sps->vui.low_delay_hrd_flag = bs_read_u1(b);
}
sps->vui.pic_struct_present_flag = bs_read_u1(b);
sps->vui.bitstream_restriction_flag = bs_read_u1(b);
if( sps->vui.bitstream_restriction_flag )
{
sps->vui.motion_vectors_over_pic_boundaries_flag = bs_read_u1(b);
sps->vui.max_bytes_per_pic_denom = bs_read_ue(b);
sps->vui.max_bits_per_mb_denom = bs_read_ue(b);
sps->vui.log2_max_mv_length_horizontal = bs_read_ue(b);
sps->vui.log2_max_mv_length_vertical = bs_read_ue(b);
sps->vui.num_reorder_frames = bs_read_ue(b);
sps->vui.max_dec_frame_buffering = bs_read_ue(b);
}
}
//Appendix E.1.2 HRD parameters syntax
void read_hrd_parameters(h264_stream_t* h, bs_t* b)
{
sps_t* sps = h->sps;
int SchedSelIdx;
sps->hrd.cpb_cnt_minus1 = bs_read_ue(b);
sps->hrd.bit_rate_scale = bs_read_u(b,4);
sps->hrd.cpb_size_scale = bs_read_u(b,4);
for( SchedSelIdx = 0; SchedSelIdx <= sps->hrd.cpb_cnt_minus1; SchedSelIdx++ )
{
sps->hrd.bit_rate_value_minus1[ SchedSelIdx ] = bs_read_ue(b);
sps->hrd.cpb_size_value_minus1[ SchedSelIdx ] = bs_read_ue(b);
sps->hrd.cbr_flag[ SchedSelIdx ] = bs_read_u1(b);
}
sps->hrd.initial_cpb_removal_delay_length_minus1 = bs_read_u(b,5);
sps->hrd.cpb_removal_delay_length_minus1 = bs_read_u(b,5);
sps->hrd.dpb_output_delay_length_minus1 = bs_read_u(b,5);
sps->hrd.time_offset_length = bs_read_u(b,5);
}
/*
UNIMPLEMENTED
//7.3.2.1.2 Sequence parameter set extension RBSP syntax
int read_seq_parameter_set_extension_rbsp(bs_t* b, sps_ext_t* sps_ext) {
seq_parameter_set_id = bs_read_ue(b);
aux_format_idc = bs_read_ue(b);
if( aux_format_idc != 0 ) {
bit_depth_aux_minus8 = bs_read_ue(b);
alpha_incr_flag = bs_read_u1(b);
alpha_opaque_value = bs_read_u(v);
alpha_transparent_value = bs_read_u(v);
}
additional_extension_flag = bs_read_u1(b);
read_rbsp_trailing_bits();
}
*/
//7.3.2.2 Picture parameter set RBSP syntax
void read_pic_parameter_set_rbsp(h264_stream_t* h, bs_t* b)
{
int pps_id = bs_read_ue(b);
pps_t* pps = h->pps = h->pps_table[pps_id] ;
memset(pps, 0, sizeof(pps_t));
int i;
int i_group;
pps->pic_parameter_set_id = pps_id;
pps->seq_parameter_set_id = bs_read_ue(b);
pps->entropy_coding_mode_flag = bs_read_u1(b);
pps->pic_order_present_flag = bs_read_u1(b);
pps->num_slice_groups_minus1 = bs_read_ue(b);
if( pps->num_slice_groups_minus1 > 0 )
{
pps->slice_group_map_type = bs_read_ue(b);
if( pps->slice_group_map_type == 0 )
{
for( i_group = 0; i_group <= pps->num_slice_groups_minus1; i_group++ )
{
pps->run_length_minus1[ i_group ] = bs_read_ue(b);
}
}
else if( pps->slice_group_map_type == 2 )
{
for( i_group = 0; i_group < pps->num_slice_groups_minus1; i_group++ )
{
pps->top_left[ i_group ] = bs_read_ue(b);
pps->bottom_right[ i_group ] = bs_read_ue(b);
}
}
else if( pps->slice_group_map_type == 3 ||
pps->slice_group_map_type == 4 ||
pps->slice_group_map_type == 5 )
{
pps->slice_group_change_direction_flag = bs_read_u1(b);
pps->slice_group_change_rate_minus1 = bs_read_ue(b);
}
else if( pps->slice_group_map_type == 6 )
{
pps->pic_size_in_map_units_minus1 = bs_read_ue(b);
for( i = 0; i <= pps->pic_size_in_map_units_minus1; i++ )
{
pps->slice_group_id[ i ] = bs_read_u(b, intlog2( pps->num_slice_groups_minus1 + 1 ) ); // was u(v)
}
}
}
pps->num_ref_idx_l0_active_minus1 = bs_read_ue(b);
pps->num_ref_idx_l1_active_minus1 = bs_read_ue(b);
pps->weighted_pred_flag = bs_read_u1(b);
pps->weighted_bipred_idc = bs_read_u(b,2);
pps->pic_init_qp_minus26 = bs_read_se(b);
pps->pic_init_qs_minus26 = bs_read_se(b);
pps->chroma_qp_index_offset = bs_read_se(b);
pps->deblocking_filter_control_present_flag = bs_read_u1(b);
pps->constrained_intra_pred_flag = bs_read_u1(b);
pps->redundant_pic_cnt_present_flag = bs_read_u1(b);
pps->_more_rbsp_data_present = more_rbsp_data(h, b);
if( pps->_more_rbsp_data_present )
{
pps->transform_8x8_mode_flag = bs_read_u1(b);
pps->pic_scaling_matrix_present_flag = bs_read_u1(b);
if( pps->pic_scaling_matrix_present_flag )
{
for( i = 0; i < 6 + 2* pps->transform_8x8_mode_flag; i++ )
{
pps->pic_scaling_list_present_flag[ i ] = bs_read_u1(b);
if( pps->pic_scaling_list_present_flag[ i ] )
{
if( i < 6 )
{
read_scaling_list( b, pps->ScalingList4x4[ i ], 16,
pps->UseDefaultScalingMatrix4x4Flag[ i ] );
}
else
{
read_scaling_list( b, pps->ScalingList8x8[ i - 6 ], 64,
pps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] );
}
}
}
}
pps->second_chroma_qp_index_offset = bs_read_se(b);
}
read_rbsp_trailing_bits(h, b);
}
//7.3.2.3 Supplemental enhancement information RBSP syntax
void read_sei_rbsp(h264_stream_t* h, bs_t* b)
{
int i;
for (i = 0; i < h->num_seis; i++)
{
sei_free(h->seis[i]);
}
h->num_seis = 0;
do {
h->num_seis++;
h->seis = (sei_t**)realloc(h->seis, h->num_seis * sizeof(sei_t*));
h->seis[h->num_seis - 1] = sei_new();
h->sei = h->seis[h->num_seis - 1];
read_sei_message(h, b);
} while( more_rbsp_data(h, b) );
read_rbsp_trailing_bits(h, b);
}
int _read_ff_coded_number(bs_t* b)
{
int n1 = 0;
int n2;
do
{
n2 = bs_read_u8(b);
n1 += n2;
} while (n2 == 0xff);
return n1;
}
//7.3.2.3.1 Supplemental enhancement information message syntax
void read_sei_message(h264_stream_t* h, bs_t* b)
{
h->sei->payloadType = _read_ff_coded_number(b);
h->sei->payloadSize = _read_ff_coded_number(b);
read_sei_payload( h, b, h->sei->payloadType, h->sei->payloadSize );
}
//7.3.2.4 Access unit delimiter RBSP syntax
void read_access_unit_delimiter_rbsp(h264_stream_t* h, bs_t* b)
{
h->aud->primary_pic_type = bs_read_u(b,3);
read_rbsp_trailing_bits(h, b);
}
//7.3.2.5 End of sequence RBSP syntax
void read_end_of_seq_rbsp(h264_stream_t* h, bs_t* b)
{
}
//7.3.2.6 End of stream RBSP syntax
void read_end_of_stream_rbsp(h264_stream_t* h, bs_t* b)
{
}
//7.3.2.7 Filler data RBSP syntax
void read_filler_data_rbsp(h264_stream_t* h, bs_t* b)
{
while( bs_next_bits(b, 8) == 0xFF )
{
int ff_byte = bs_read_f(b,8); // equal to 0xFF
}
read_rbsp_trailing_bits(h, b);
}
//7.3.2.8 Slice layer without partitioning RBSP syntax
void read_slice_layer_rbsp(h264_stream_t* h, bs_t* b)
{
read_slice_header(h, b);
slice_data_rbsp_t* slice_data = h->slice_data;
if ( slice_data != NULL )
{
if ( slice_data->rbsp_buf != NULL ) free( slice_data->rbsp_buf );
uint8_t *sptr = b->p + (!!b->bits_left); // CABAC-specific: skip alignment bits, if there are any
slice_data->rbsp_size = b->end - sptr;
slice_data->rbsp_buf = (uint8_t*)malloc(slice_data->rbsp_size);
memcpy( slice_data->rbsp_buf, sptr, slice_data->rbsp_size );
// ugly hack: since next NALU starts at byte border, we are going to be padded by trailing_bits;
return;
}
// FIXME should read or skip data
//slice_data( ); /* all categories of slice_data( ) syntax */
read_rbsp_slice_trailing_bits(h, b);
}
/*
// UNIMPLEMENTED
//7.3.2.9.1 Slice data partition A RBSP syntax
slice_data_partition_a_layer_rbsp( ) {
read_slice_header( ); // only category 2
slice_id = bs_read_ue(b)
read_slice_data( ); // only category 2
read_rbsp_slice_trailing_bits( ); // only category 2
}
//7.3.2.9.2 Slice data partition B RBSP syntax
slice_data_partition_b_layer_rbsp( ) {
slice_id = bs_read_ue(b); // only category 3
if( redundant_pic_cnt_present_flag )
redundant_pic_cnt = bs_read_ue(b);
read_slice_data( ); // only category 3
read_rbsp_slice_trailing_bits( ); // only category 3
}
//7.3.2.9.3 Slice data partition C RBSP syntax
slice_data_partition_c_layer_rbsp( ) {
slice_id = bs_read_ue(b); // only category 4
if( redundant_pic_cnt_present_flag )
redundant_pic_cnt = bs_read_ue(b);
read_slice_data( ); // only category 4
rbsp_slice_trailing_bits( ); // only category 4
}
*/
int
more_rbsp_trailing_data(h264_stream_t* h, bs_t* b) { return !bs_eof(b); }
//7.3.2.10 RBSP slice trailing bits syntax
void read_rbsp_slice_trailing_bits(h264_stream_t* h, bs_t* b)
{
read_rbsp_trailing_bits(h, b);
int cabac_zero_word;
if( h->pps->entropy_coding_mode_flag )
{
while( more_rbsp_trailing_data(h, b) )
{
cabac_zero_word = bs_read_f(b,16); // equal to 0x0000
}
}
}
//7.3.2.11 RBSP trailing bits syntax
void read_rbsp_trailing_bits(h264_stream_t* h, bs_t* b)
{
int rbsp_stop_one_bit = bs_read_u1( b ); // equal to 1
while( !bs_byte_aligned(b) )
{
int rbsp_alignment_zero_bit = bs_read_u1( b ); // equal to 0
}
}
//7.3.3 Slice header syntax
void read_slice_header(h264_stream_t* h, bs_t* b)
{
slice_header_t* sh = h->sh;
memset(sh, 0, sizeof(slice_header_t));
sps_t* sps = NULL; // h->sps;
pps_t* pps = NULL;//h->pps;
nal_t* nal = h->nal;
sh->first_mb_in_slice = bs_read_ue(b);
sh->slice_type = bs_read_ue(b);
sh->pic_parameter_set_id = bs_read_ue(b);
pps = h->pps = h->pps_table[sh->pic_parameter_set_id];
sps = h->sps = h->sps_table[pps->seq_parameter_set_id];
sh->frame_num = bs_read_u(b, sps->log2_max_frame_num_minus4 + 4 ); // was u(v)
if( !sps->frame_mbs_only_flag )
{
sh->field_pic_flag = bs_read_u1(b);
if( sh->field_pic_flag )
{
sh->bottom_field_flag = bs_read_u1(b);
}
}
if( nal->nal_unit_type == 5 )
{
sh->idr_pic_id = bs_read_ue(b);
}
if( sps->pic_order_cnt_type == 0 )
{
sh->pic_order_cnt_lsb = bs_read_u(b, sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ); // was u(v)
if( pps->pic_order_present_flag && !sh->field_pic_flag )
{
sh->delta_pic_order_cnt_bottom = bs_read_se(b);
}
}
if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
{
sh->delta_pic_order_cnt[ 0 ] = bs_read_se(b);
if( pps->pic_order_present_flag && !sh->field_pic_flag )
{
sh->delta_pic_order_cnt[ 1 ] = bs_read_se(b);
}
}
if( pps->redundant_pic_cnt_present_flag )
{
sh->redundant_pic_cnt = bs_read_ue(b);
}
if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) )
{
sh->direct_spatial_mv_pred_flag = bs_read_u1(b);
}
if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_P ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_SP ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) )
{
sh->num_ref_idx_active_override_flag = bs_read_u1(b);
if( sh->num_ref_idx_active_override_flag )
{
sh->num_ref_idx_l0_active_minus1 = bs_read_ue(b); // FIXME does this modify the pps?
if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) )
{
sh->num_ref_idx_l1_active_minus1 = bs_read_ue(b);
}
}
}
read_ref_pic_list_reordering(h, b);
if( ( pps->weighted_pred_flag && ( is_slice_type( sh->slice_type, SH_SLICE_TYPE_P ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_SP ) ) ) ||
( pps->weighted_bipred_idc == 1 && is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) ) )
{
read_pred_weight_table(h, b);
}
if( nal->nal_ref_idc != 0 )
{