forked from intel/libva-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhevcencode.c
More file actions
3425 lines (2947 loc) · 129 KB
/
hevcencode.c
File metadata and controls
3425 lines (2947 loc) · 129 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
/*
* Copyright (c) 2018 Intel Corporation. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define LIBVA_UTILS_UPLOAD_DOWNLOAD_YUV_SURFACE 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <assert.h>
#include <pthread.h>
#include <errno.h>
#include <math.h>
#include <va/va.h>
#include <va/va_enc_hevc.h>
#include "va_display.h"
#define ALIGN16(x) ((x+15)&~15)
#define CHECK_VASTATUS(va_status,func) \
if (va_status != VA_STATUS_SUCCESS) { \
fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
exit(1); \
}
#include "loadsurface.h"
#define NAL_REF_IDC_NONE 0
#define NAL_REF_IDC_LOW 1
#define NAL_REF_IDC_MEDIUM 2
#define NAL_REF_IDC_HIGH 3
#define FRAME_I 1
#define FRAME_P 2
#define FRAME_B 3
#define FRAME_IDR 7
// SLICE TYPE HEVC ENUM
enum
{
SLICE_B = 0,
SLICE_P = 1,
SLICE_I = 2,
};
#define IS_I_SLICE(type) (SLICE_I == (type))
#define IS_P_SLICE(type) (SLICE_P == (type))
#define IS_B_SLICE(type) (SLICE_B == (type))
#define ENTROPY_MODE_CAVLC 0
#define ENTROPY_MODE_CABAC 1
#define PROFILE_IDC_MAIN 1
#define PROFILE_IDC_MAIN10 2
#define BITSTREAM_ALLOCATE_STEPPING 4096
#define LCU_SIZE 32
#define SURFACE_NUM 16 /* 16 surfaces for source YUV */
#define SURFACE_NUM 16 /* 16 surfaces for reference */
enum NALUType
{
NALU_TRAIL_N = 0x00, // Coded slice segment of a non-TSA, non-STSA trailing picture - slice_segment_layer_rbsp, VLC
NALU_TRAIL_R = 0x01, // Coded slice segment of a non-TSA, non-STSA trailing picture - slice_segment_layer_rbsp, VLC
NALU_TSA_N = 0x02, // Coded slice segment of a TSA picture - slice_segment_layer_rbsp, VLC
NALU_TSA_R = 0x03, // Coded slice segment of a TSA picture - slice_segment_layer_rbsp, VLC
NALU_STSA_N = 0x04, // Coded slice of an STSA picture - slice_layer_rbsp, VLC
NALU_STSA_R = 0x05, // Coded slice of an STSA picture - slice_layer_rbsp, VLC
NALU_RADL_N = 0x06, // Coded slice of an RADL picture - slice_layer_rbsp, VLC
NALU_RADL_R = 0x07, // Coded slice of an RADL picture - slice_layer_rbsp, VLC
NALU_RASL_N = 0x08, // Coded slice of an RASL picture - slice_layer_rbsp, VLC
NALU_RASL_R = 0x09, // Coded slice of an RASL picture - slice_layer_rbsp, VLC
/* 0x0a..0x0f - Reserved */
NALU_BLA_W_LP = 0x10, // Coded slice segment of an BLA picture - slice_segment_layer_rbsp, VLC
NALU_BLA_W_DLP = 0x11, // Coded slice segment of an BLA picture - slice_segment_layer_rbsp, VLC
NALU_BLA_N_LP = 0x12, // Coded slice segment of an BLA picture - slice_segment_layer_rbsp, VLC
NALU_IDR_W_DLP = 0x13, // Coded slice segment of an IDR picture - slice_segment_layer_rbsp, VLC
NALU_IDR_N_LP = 0x14, // Coded slice segment of an IDR picture - slice_segment_layer_rbsp, VLC
NALU_CRA = 0x15, // Coded slice segment of an CRA picture - slice_segment_layer_rbsp, VLC
/* 0x16..0x1f - Reserved */
NALU_VPS = 0x20, // Video parameter set - video_parameter_set_rbsp, non-VLC
NALU_SPS = 0x21, // Sequence parameter set - seq_parameter_set_rbsp, non-VLC
NALU_PPS = 0x22, // Picture parameter set - pic_parameter_set_rbsp, non-VLC
NALU_AUD = 0x23, // Access unit delimiter - access_unit_delimiter_rbsp, non-VLC
NALU_EOS = 0x24, // End of sequence - end_of_seq_rbsp, non-VLC
NALU_EOB = 0x25, // End of bitsteam - end_of_bitsteam_rbsp, non-VLC
NALU_FD = 0x26, // Filler data - filler_data_rbsp, non-VLC
NALU_PREFIX_SEI = 0x27, // Supplemental enhancement information (SEI) - sei_rbsp, non_VLC
NALU_SUFFIX_SEI = 0x28, // Supplemental enhancement information (SEI) - sei_rbsp, non_VLC
/* 0x29..0x2f - Reserved */
/* 0x30..0x3f - Unspecified */
//this should be the last element of this enum
//chagne this value if NAL unit type increased
MAX_HEVC_NAL_TYPE = 0x3f,
};
// Config const values
#define MAX_TEMPORAL_SUBLAYERS 8
#define MAX_LAYER_ID 64
#define MAX_LONGTERM_REF_PIC 32
#define NUM_OF_EXTRA_SLICEHEADER_BITS 3
struct ProfileTierParamSet
{
uint8_t general_profile_space; //u(2)
int general_tier_flag; //u(1)
uint8_t general_profile_idc; //u(5)
int general_profile_compatibility_flag[32]; //u(1)
int general_progressive_source_flag; //u(1)
int general_interlaced_source_flag; //u(1)
int general_non_packed_constraint_flag; //u(1)
int general_frame_only_constraint_flag; //u(1)
int general_reserved_zero_43bits[43]; //u(1)
int general_reserved_zero_bit; //u(1)
uint8_t general_level_idc; //u(8)
};
// Video parameter set structure
struct VideoParamSet
{
uint8_t vps_video_parameter_set_id; //u(4)
int vps_base_layer_internal_flag; //u(1)
int vps_base_layer_available_flag; //u(1)
uint8_t vps_max_layers_minus1; //u(6)
uint8_t vps_max_sub_layers_minus1; //u(3)
int vps_temporal_id_nesting_flag; //u(1)
uint16_t vps_reserved_0xffff_16bits; //u(16)
struct ProfileTierParamSet ptps;
uint8_t vps_max_nuh_reserved_zero_layer_id;
uint32_t vps_max_op_sets;
uint32_t vps_num_op_sets_minus1;
int vps_sub_layer_ordering_info_present_flag; //u(1)
uint32_t vps_max_dec_pic_buffering_minus1[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
uint32_t vps_max_num_reorder_pics[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
uint32_t vps_max_latency_increase_plus1[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
uint8_t vps_max_layer_id; //u(6)
uint32_t vps_num_layer_sets_minus1; //ue(v)
int layer_id_included_flag[MAX_TEMPORAL_SUBLAYERS][MAX_LAYER_ID]; //u(1)
int vps_timing_info_present_flag; //u(1)
uint32_t vps_num_units_in_tick; //u(32)
uint32_t vps_time_scale; //u(32
int vps_poc_proportional_to_timing_flag; //u(1)
uint32_t vps_num_ticks_poc_diff_one_minus1; //ue(v)
uint32_t vps_num_hrd_parameters; //ue(v)
uint32_t hrd_layer_set_idx[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
int cprms_present_flag[MAX_TEMPORAL_SUBLAYERS]; //u(1)
int vps_extension_flag; //u(1)
int vps_extension_data_flag; //u(1)
};
struct ShortTermRefPicParamSet
{
int inter_ref_pic_set_prediction_flag; //u(1)
uint32_t delta_idx_minus1; //ue(v)
uint8_t delta_rps_sign; //u(1)
uint32_t abs_delta_rps_minus1; //ue(v)
uint8_t used_by_curr_pic_flag[32]; //u(1)
uint8_t use_delta_flag[32]; //u(1)
uint32_t num_negative_pics; //ue(v)
uint32_t num_positive_pics; //ue(v)
uint32_t delta_poc_s0_minus1[32]; //ue(v)
uint8_t used_by_curr_pic_s0_flag[32]; //u(1)
uint32_t delta_poc_s1_minus1[32]; //ue(v)
uint8_t used_by_curr_pic_s1_flag[32]; //u(1)
};
struct SeqParamSet
{
uint8_t sps_video_parameter_set_id; //u(4)
uint8_t sps_max_sub_layers_minus1; //u(3)
int sps_temporal_id_nesting_flag; //u(1)
struct ProfileTierParamSet ptps;
uint32_t sps_seq_parameter_set_id; //ue(v)
uint32_t chroma_format_idc; //ue(v)
int separate_colour_plane_flag; //u(1)
uint32_t pic_width_in_luma_samples; //ue(v)
uint32_t pic_height_in_luma_samples; //ue(v)
int conformance_window_flag; //u(1)
uint32_t conf_win_left_offset; //ue(v)
uint32_t conf_win_right_offset; //ue(v)
uint32_t conf_win_top_offset; //ue(v)
uint32_t conf_win_bottom_offset; //ue(v)
uint32_t bit_depth_luma_minus8; //ue(v)
uint32_t bit_depth_chroma_minus8; //ue(v)
uint32_t log2_max_pic_order_cnt_lsb_minus4; //ue(v)
int sps_sub_layer_ordering_info_present_flag; //u(1)
uint32_t sps_max_dec_pic_buffering_minus1[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
uint32_t sps_max_num_reorder_pics[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
uint32_t sps_max_latency_increase_plus1[MAX_TEMPORAL_SUBLAYERS]; //ue(v)
uint32_t log2_min_luma_coding_block_size_minus3; //ue(v)
uint32_t log2_diff_max_min_luma_coding_block_size;
uint32_t log2_max_coding_block_size_minus3; //ue(v)
uint32_t log2_min_luma_transform_block_size_minus2; //ue(v)
uint32_t log2_diff_max_min_luma_transform_block_size; //ue(v)
uint32_t max_transform_hierarchy_depth_inter; //ue(v)
uint32_t max_transform_hierarchy_depth_intra; //ue(v)
uint8_t scaling_list_enabled_flag; //u(1)
uint8_t sps_scaling_list_data_present_flag; //u(1)
uint8_t amp_enabled_flag; //u(1)
uint8_t sample_adaptive_offset_enabled_flag; //u(1)
uint8_t pcm_enabled_flag; //u(1)
uint8_t pcm_sample_bit_depth_luma_minus1; //u(4)
uint8_t pcm_sample_bit_depth_chroma_minus1; //u(4)
uint32_t log2_min_pcm_luma_coding_block_size_minus3;
uint32_t log2_max_pcm_luma_coding_block_size_minus3; //ue(v)
uint32_t log2_diff_max_min_pcm_luma_coding_block_size; //ue(v)
uint8_t pcm_loop_filter_disabled_flag; //u(1)
uint32_t num_short_term_ref_pic_sets; //ue(v)
struct ShortTermRefPicParamSet strp[66];
uint8_t long_term_ref_pics_present_flag; //u(1)
uint32_t num_long_term_ref_pics_sps; //ue(v)
uint32_t lt_ref_pic_poc_lsb_sps[MAX_LONGTERM_REF_PIC]; //u(v)
uint8_t used_by_curr_pic_lt_sps_flag[MAX_LONGTERM_REF_PIC]; //u(1)
uint8_t sps_temporal_mvp_enabled_flag; //u(1)
uint8_t strong_intra_smoothing_enabled_flag; //u(1)
uint8_t vui_parameters_present_flag; //u(1)
//VuiParameters vui_parameters;
int sps_extension_present_flag; //u(1)
int sps_range_extension_flag; //u(1)
int sps_multilayer_extension_flag; //u(1)
int sps_3d_extension_flag; //u(1)
uint8_t sps_extension_5bits; //u(5)
int sps_extension_data_flag; //u(1)
};
struct PicParamSet
{
uint32_t pps_pic_parameter_set_id; //ue(v)
uint32_t pps_seq_parameter_set_id; //ue(v)
int dependent_slice_segments_enabled_flag; //u(1)
int output_flag_present_flag; //u(1)
uint8_t num_extra_slice_header_bits; //u(3)
int sign_data_hiding_enabled_flag; //u(1)
int cabac_init_present_flag; //u(1)
uint32_t num_ref_idx_l0_default_active_minus1; //ue(v)
uint32_t num_ref_idx_l1_default_active_minus1; //ue(v)
int32_t init_qp_minus26; //se(v)
int constrained_intra_pred_flag; //u(1)
int transform_skip_enabled_flag; //u(1)
int cu_qp_delta_enabled_flag; //u(1)
uint32_t diff_cu_qp_delta_depth; //ue(v)
uint32_t pps_cb_qp_offset; //se(v)
uint32_t pps_cr_qp_offset; //se(v)
int pps_slice_chroma_qp_offsets_present_flag; //u(1)
int weighted_pred_flag; //u(1)
int weighted_bipred_flag; //u(1)
int transquant_bypass_enabled_flag; //u(1)
int tiles_enabled_flag; //u(1)
int entropy_coding_sync_enabled_flag; //u(1)
uint32_t num_tile_columns_minus1; //ue(v)
uint32_t num_tile_rows_minus1; //ue(v)
int uniform_spacing_flag; //u(1)
uint32_t *column_width_minus1; //ue(v)
uint32_t *row_height_minus1; //ue(v)
int loop_filter_across_tiles_enabled_flag; //u(1)
int pps_loop_filter_across_slices_enabled_flag; //u(1)
int deblocking_filter_control_present_flag; //u(1)
int deblocking_filter_override_enabled_flag; //u(1)
int pps_deblocking_filter_disabled_flag; //u(1)
int32_t pps_beta_offset_div2; //se(v)
int32_t pps_tc_offset_div2; //se(v)
int pps_scaling_list_data_present_flag; //u(1)
int lists_modification_present_flag; //u(1)
uint32_t log2_parallel_merge_level_minus2; //ue(v)
int slice_segment_header_extension_present_flag; //u(1)
int pps_extension_present_flag; //u(1)
int pps_range_extension_flag; //u(1)
int pps_multilayer_extension_flag; //u(1)
int pps_3d_extension_flag; //u(1)
uint8_t pps_extension_5bits; //u(5)
uint8_t pps_extension_data_flag; //u(1)
uint32_t log2_max_transform_skip_block_size_minus2; //ue(v)
uint8_t cross_component_prediction_enabled_flag; //ue(1)
uint8_t chroma_qp_offset_list_enabled_flag; //ue(1)
uint32_t diff_cu_chroma_qp_offset_depth; //ue(v)
uint32_t chroma_qp_offset_list_len_minus1; //ue(v)
uint32_t cb_qp_offset_list[6]; //se(v)
uint32_t cr_qp_offset_list[6]; //se(v)
uint32_t log2_sao_offset_scale_luma; //ue(v)
uint32_t log2_sao_offset_scale_chroma; //ue(v)
};
struct SliceHeader
{
int first_slice_segment_in_pic_flag; //u(1)
int no_output_of_prior_pics_flag; //u(1)
uint32_t slice_pic_parameter_set_id; //ue(v)
int dependent_slice_segment_flag; //u(1)
uint32_t picture_width_in_ctus;
uint32_t picture_height_in_ctus;
uint32_t slice_segment_address; //u(v)
int slice_reserved_undetermined_flag[NUM_OF_EXTRA_SLICEHEADER_BITS]; //u(1)
uint32_t slice_type; //ue(v)
int pic_output_flag; //u(1)
uint8_t colour_plane_id; //u(2)
uint32_t pic_order_cnt_lsb;
uint32_t num_negative_pics;
uint32_t num_positive_pics;
uint32_t delta_poc_s0_minus1;
struct ShortTermRefPicParamSet strp;
int short_term_ref_pic_set_sps_flag; //u(1)
uint32_t short_term_ref_pic_set_idx; //u(v)
uint32_t num_long_term_sps; //ue(v)
uint32_t num_long_term_pics; //ue(v)
uint32_t *lt_idx_sps; //u(v)
uint32_t *poc_lsb_lt; //u(v)
int *used_by_curr_pic_lt_flag; //u(1)
int *delta_poc_msb_present_flag; //u(1)
uint32_t *delta_poc_msb_cycle_lt; //ue(v)
int slice_temporal_mvp_enabled_flag; //u(1)
int slice_sao_luma_flag; //u(1)
int slice_sao_chroma_flag; //u(1)
int num_ref_idx_active_override_flag; //u(1)
uint32_t num_ref_idx_l0_active_minus1; //ue(v)
uint32_t num_ref_idx_l1_active_minus1;
uint32_t num_poc_total_cur;
int ref_pic_list_modification_flag_l0;
int ref_pic_list_modification_flag_l1;
uint32_t* list_entry_l0;
uint32_t* list_entry_l1;
int ref_pic_list_combination_flag;
uint32_t num_ref_idx_lc_active_minus1;
uint32_t ref_pic_list_modification_flag_lc;
int pic_from_list_0_flag;
uint32_t ref_idx_list_curr;
int mvd_l1_zero_flag; //u(1)
int cabac_init_present_flag;
int pic_temporal_mvp_enable_flag;
int collocated_from_l0_flag; //u(1)
uint32_t collocated_ref_idx; //ue(v)
uint32_t five_minus_max_num_merge_cand; //ue(v)
int32_t delta_pic_order_cnt_bottom; //se(v)
int32_t slice_qp_delta; //se(v)
int32_t slice_qp_delta_cb; //se(v)
int32_t slice_qp_delta_cr; //se(v)
int cu_chroma_qp_offset_enabled_flag; //u(1)
int deblocking_filter_override_flag; //u(1)
int disable_deblocking_filter_flag; //u(1)
int32_t beta_offset_div2; //se(v)
int32_t tc_offset_div2; //se(v)
int slice_loop_filter_across_slices_enabled_flag; //u(1)
uint32_t num_entry_point_offsets; //ue(v)
uint32_t offset_len_minus1; //ue(v)
uint32_t *entry_point_offset; //u(v)
uint32_t slice_segment_header_extension_length; //ue(v)
uint8_t *slice_segment_header_extension_data_byte; //u(8)
};
static struct VideoParamSet vps;
static struct SeqParamSet sps;
static struct PicParamSet pps;
static struct SliceHeader ssh;
static VADisplay va_dpy;
static VAProfile hevc_profile = ~0;
static int real_hevc_profile = 0;
static int p2b = 1;
static VAConfigAttrib attrib[VAConfigAttribTypeMax];
static VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
static int config_attrib_num = 0, enc_packed_header_idx;
static VASurfaceID src_surface[SURFACE_NUM];
static VABufferID coded_buf[SURFACE_NUM];
static VASurfaceID ref_surface[SURFACE_NUM];
static VAConfigID config_id;
static VAContextID context_id;
static struct ProfileTierParamSet protier_param;
static VAEncSequenceParameterBufferHEVC seq_param;
static VAEncPictureParameterBufferHEVC pic_param;
static VAEncSliceParameterBufferHEVC slice_param;
static VAPictureHEVC CurrentCurrPic;
static VAPictureHEVC ReferenceFrames[16], RefPicList0_P[32], RefPicList0_B[32], RefPicList1_B[32];
static unsigned int MaxPicOrderCntLsb = (2<<8);
static unsigned int num_ref_frames = 2;
static unsigned int num_active_ref_p = 1;
static unsigned int numShortTerm = 0;
static int constraint_set_flag = 0;
static int hevc_packedheader = 0;
static int hevc_maxref = 16;
static char *coded_fn = NULL, *srcyuv_fn = NULL, *recyuv_fn = NULL;
static FILE *coded_fp = NULL, *srcyuv_fp = NULL, *recyuv_fp = NULL;
static unsigned long long srcyuv_frames = 0;
static int srcyuv_fourcc = VA_FOURCC_NV12;
static int calc_psnr = 0;
static int frame_width = 176;
static int frame_height = 144;
static int frame_width_aligned;
static int frame_height_aligned;
static int frame_rate = 30;
static unsigned int frame_count = 60;
static unsigned int frame_coded = 0;
static unsigned int frame_bitrate = 0;
static unsigned int frame_slices = 1;
static double frame_size = 0;
static int initial_qp = 26;
static int minimal_qp = 0;
static int intra_period = 30;
static int intra_idr_period = 60;
static int ip_period = 1;
static int rc_mode = -1;
static int rc_default_modes[] = {
VA_RC_VBR,
VA_RC_CQP,
VA_RC_VBR_CONSTRAINED,
VA_RC_CBR,
VA_RC_VCM,
VA_RC_NONE,
};
static unsigned long long current_frame_encoding = 0;
static unsigned long long current_frame_display = 0;
static unsigned long long current_IDR_display = 0;
static unsigned int current_frame_num = 0;
static int current_frame_type;
#define current_slot (current_frame_display % SURFACE_NUM)
static int misc_priv_type = 0;
static int misc_priv_value = 0;
#define MIN(a, b) ((a)>(b)?(b):(a))
#define MAX(a, b) ((a)>(b)?(a):(b))
/* thread to save coded data/upload source YUV */
struct storage_task_t {
void *next;
unsigned long long display_order;
unsigned long long encode_order;
};
static struct storage_task_t *storage_task_header = NULL, *storage_task_tail = NULL;
#define SRC_SURFACE_IN_ENCODING 0
#define SRC_SURFACE_IN_STORAGE 1
static int srcsurface_status[SURFACE_NUM];
static int encode_syncmode = 0;
static pthread_mutex_t encode_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t encode_cond = PTHREAD_COND_INITIALIZER;
static pthread_t encode_thread;
/* for performance profiling */
static unsigned int UploadPictureTicks=0;
static unsigned int BeginPictureTicks=0;
static unsigned int RenderPictureTicks=0;
static unsigned int EndPictureTicks=0;
static unsigned int SyncPictureTicks=0;
static unsigned int SavePictureTicks=0;
static unsigned int TotalTicks=0;
struct __bitstream {
unsigned int *buffer;
int bit_offset;
int max_size_in_dword;
};
typedef struct __bitstream bitstream;
static unsigned int
va_swap32(unsigned int val)
{
unsigned char *pval = (unsigned char *)&val;
return ((pval[0] << 24) |
(pval[1] << 16) |
(pval[2] << 8) |
(pval[3] << 0));
}
static void
bitstream_start(bitstream *bs)
{
bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
assert(bs->buffer);
bs->bit_offset = 0;
}
static void
bitstream_end(bitstream *bs)
{
int pos = (bs->bit_offset >> 5);
int bit_offset = (bs->bit_offset & 0x1f);
int bit_left = 32 - bit_offset;
if (bit_offset) {
bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
}
}
static void
put_ui(bitstream *bs, unsigned int val, int size_in_bits)
{
int pos = (bs->bit_offset >> 5);
int bit_offset = (bs->bit_offset & 0x1f);
int bit_left = 32 - bit_offset;
if (!size_in_bits)
return;
bs->bit_offset += size_in_bits;
if (bit_left > size_in_bits) {
bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
} else {
size_in_bits -= bit_left;
bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
bs->buffer[pos] = va_swap32(bs->buffer[pos]);
if (pos + 1 == bs->max_size_in_dword) {
bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
assert(bs->buffer);
}
bs->buffer[pos + 1] = val;
}
}
static void
put_ue(bitstream *bs, unsigned int val)
{
int size_in_bits = 0;
int tmp_val = ++val;
while (tmp_val) {
tmp_val >>= 1;
size_in_bits++;
}
put_ui(bs, 0, size_in_bits - 1); // leading zero
put_ui(bs, val, size_in_bits);
}
static void
put_se(bitstream *bs, int val)
{
unsigned int new_val;
if (val <= 0)
new_val = -2 * val;
else
new_val = 2 * val - 1;
put_ue(bs, new_val);
}
static void
byte_aligning(bitstream *bs, int bit)
{
int bit_offset = (bs->bit_offset & 0x7);
int bit_left = 8 - bit_offset;
int new_val;
if (!bit_offset)
return;
assert(bit == 0 || bit == 1);
if (bit)
new_val = (1 << bit_left) - 1;
else
new_val = 0;
put_ui(bs, new_val, bit_left);
}
static void
rbsp_trailing_bits(bitstream *bs)
{
put_ui(bs, 1, 1);
byte_aligning(bs, 0);
}
static void nal_start_code_prefix(bitstream *bs, int nal_unit_type)
{
if(nal_unit_type == NALU_VPS ||
nal_unit_type == NALU_SPS ||
nal_unit_type == NALU_PPS ||
nal_unit_type == NALU_AUD)
put_ui(bs, 0x00000001, 32);
else
put_ui(bs, 0x000001, 24);
}
static void nal_header(bitstream *bs,int nal_unit_type)
{
put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
put_ui(bs, nal_unit_type, 6);
put_ui(bs, 0, 6);
put_ui(bs, 1, 3);
}
static int calc_poc(int pic_order_cnt_lsb)
{
static int picOrderCntMsb_ref = 0, pic_order_cnt_lsb_ref = 0;
int prevPicOrderCntMsb, prevPicOrderCntLsb;
int picOrderCntMsb, picOrderCnt;
if (current_frame_type == FRAME_IDR)
prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
else {
prevPicOrderCntMsb = picOrderCntMsb_ref;
prevPicOrderCntLsb = pic_order_cnt_lsb_ref;
}
if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (int)(MaxPicOrderCntLsb / 2)))
picOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
((pic_order_cnt_lsb - prevPicOrderCntLsb) > (int)(MaxPicOrderCntLsb / 2)))
picOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
else
picOrderCntMsb = prevPicOrderCntMsb;
picOrderCnt = picOrderCntMsb + pic_order_cnt_lsb;
if (current_frame_type != FRAME_B) {
picOrderCntMsb_ref = picOrderCntMsb;
pic_order_cnt_lsb_ref = pic_order_cnt_lsb;
}
return picOrderCnt;
}
static void fill_profile_tier_level(
uint8_t vps_max_layers_minus1,
struct ProfileTierParamSet *ptps,
uint8_t profilePresentFlag)
{
if (!profilePresentFlag)
return;
memset(ptps, 0, sizeof(*ptps));
ptps->general_profile_space = 0;
ptps->general_tier_flag = 0;
ptps->general_profile_idc = real_hevc_profile;
memset(ptps->general_profile_compatibility_flag,0,32*sizeof(int));
ptps->general_profile_compatibility_flag[ptps->general_profile_idc] = 1;
ptps->general_progressive_source_flag = 1;
ptps->general_interlaced_source_flag = 0;
ptps->general_non_packed_constraint_flag = 0;
ptps->general_frame_only_constraint_flag = 1;
ptps->general_level_idc = 30;
ptps->general_level_idc = ptps->general_level_idc * 3;
}
static void fill_vps_header(struct VideoParamSet *vps)
{
int i = 0;
memset(vps, 0, sizeof(*vps));
vps->vps_video_parameter_set_id = 0;
vps->vps_base_layer_internal_flag = 1;
vps->vps_base_layer_available_flag = 1;
vps->vps_max_layers_minus1 = 0;
vps->vps_max_sub_layers_minus1 = 0; // max temporal layer minus 1
vps->vps_temporal_id_nesting_flag = 1;
vps->vps_reserved_0xffff_16bits = 0xFFFF;
// hevc::ProfileTierParamSet ptps;
memset(&vps->ptps, 0, sizeof(vps->ptps));
fill_profile_tier_level(vps->vps_max_layers_minus1, &protier_param, 1);
vps->vps_sub_layer_ordering_info_present_flag = 0;
for (i = 0; i < MAX_TEMPORAL_SUBLAYERS; i++)
{
vps->vps_max_dec_pic_buffering_minus1[i] = intra_period == 1 ? 1 : 6;
vps->vps_max_num_reorder_pics[i] = ip_period != 0 ? ip_period -1 : 0;
vps->vps_max_latency_increase_plus1[i] = 0;
}
vps->vps_max_layer_id = 0;
vps->vps_num_layer_sets_minus1 = 0;
vps->vps_sub_layer_ordering_info_present_flag = 0;
vps->vps_max_nuh_reserved_zero_layer_id = 0;
vps->vps_max_op_sets = 1;
vps->vps_timing_info_present_flag = 0;
vps->vps_extension_flag = 0;
}
static void fill_short_term_ref_pic_header(
struct ShortTermRefPicParamSet *strp,
uint8_t strp_index)
{
uint32_t i = 0;
// inter_ref_pic_set_prediction_flag is always 0 now
strp->inter_ref_pic_set_prediction_flag = 0;
/* don't need to set below parameters since inter_ref_pic_set_prediction_flag equal to 0
strp->delta_idx_minus1 should be set to 0 since strp_index != num_short_term_ref_pic_sets in sps
strp->delta_rps_sign;
strp->abs_delta_rps_minus1;
strp->used_by_curr_pic_flag[j];
strp->use_delta_flag[j];
*/
strp->num_negative_pics = num_active_ref_p;
int num_positive_pics = ip_period > 1 ? 1 : 0;
strp->num_positive_pics = strp_index == 0 ? 0 : num_positive_pics;
if (strp_index == 0)
{
for (i = 0; i < strp->num_negative_pics; i++)
{
strp->delta_poc_s0_minus1[i] = ip_period - 1;
strp->used_by_curr_pic_s0_flag[i] = 1;
}
}
else
{
for (i = 0; i < strp->num_negative_pics; i++)
{
strp->delta_poc_s0_minus1[i] = (i == 0) ?
(strp_index - 1) : (ip_period - 1);
strp->used_by_curr_pic_s0_flag[i] = 1;
}
for (i = 0; i < strp->num_positive_pics; i++)
{
strp->delta_poc_s1_minus1[i] = ip_period - 1 - strp_index;
strp->used_by_curr_pic_s1_flag[i] = 1;
}
}
}
void fill_sps_header(struct SeqParamSet *sps, int id)
{
int i = 0;
memset(sps, 0, sizeof(struct SeqParamSet));
sps->sps_video_parameter_set_id = 0;
sps->sps_max_sub_layers_minus1 = 0;
sps->sps_temporal_id_nesting_flag = 1;
fill_profile_tier_level(sps->sps_max_sub_layers_minus1, &sps->ptps, 1);
sps->sps_seq_parameter_set_id = id;
sps->chroma_format_idc = 1;
if (sps->chroma_format_idc == 3)
{
sps->separate_colour_plane_flag = 0;
}
frame_width_aligned = ALIGN16(frame_width);
frame_height_aligned = ALIGN16(frame_height);
sps->pic_width_in_luma_samples = frame_width_aligned;
sps->pic_height_in_luma_samples = frame_height_aligned;
if (frame_width_aligned != frame_width ||
frame_height_aligned != frame_height)
{
sps->conformance_window_flag = 1;
sps->conf_win_left_offset = 0;
sps->conf_win_top_offset = 0;
switch (sps->chroma_format_idc)
{
case 0:
case 3: // 4:4:4 format
sps->conf_win_right_offset = (frame_width_aligned - frame_width);
sps->conf_win_bottom_offset = (frame_height_aligned - frame_height);
break;
case 2: // 4:2:2 format
sps->conf_win_right_offset = (frame_width_aligned - frame_width) >> 1;
sps->conf_win_bottom_offset = (frame_height_aligned - frame_height);
break;
case 1:
default: // 4:2:0 format
sps->conf_win_right_offset = (frame_width_aligned - frame_width) >> 1;
sps->conf_win_bottom_offset = (frame_height_aligned - frame_height) >> 1;
break;
}
}
else
{
sps->conformance_window_flag = 0;
}
sps->bit_depth_luma_minus8 = 0;
sps->bit_depth_chroma_minus8 = 0;
sps->log2_max_pic_order_cnt_lsb_minus4 =MAX((ceil(log(ip_period - 1 + 4)/log(2.0))+3), 4) - 4;
sps->sps_sub_layer_ordering_info_present_flag = 0;
for (i = 0; i < MAX_TEMPORAL_SUBLAYERS; i++)
{
sps->sps_max_dec_pic_buffering_minus1[i] = intra_period == 1 ? 1 : 6;
sps->sps_max_num_reorder_pics[i] = ip_period != 0 ? ip_period - 1 : 0;
sps->sps_max_latency_increase_plus1[i] = 0;
}
sps->log2_min_luma_coding_block_size_minus3 = 0;
int log2_max_luma_coding_block_size = log2(LCU_SIZE);
int log2_min_luma_coding_block_size = sps->log2_min_luma_coding_block_size_minus3 + 3;
sps->log2_diff_max_min_luma_coding_block_size = log2_max_luma_coding_block_size -
log2_min_luma_coding_block_size;
sps->log2_min_luma_transform_block_size_minus2 = 0;
sps->log2_diff_max_min_luma_transform_block_size = 3;
sps->max_transform_hierarchy_depth_inter = 2;
sps->max_transform_hierarchy_depth_intra = 2;
sps->scaling_list_enabled_flag = 0;
//sps->sps_scaling_list_data_present_flag; // ignore since scaling_list_enabled_flag equal to 0
sps->amp_enabled_flag = 1;
sps->sample_adaptive_offset_enabled_flag = 0;
sps->pcm_enabled_flag = 0;
/* ignore below parameters seting since pcm_enabled_flag equal to 0
pcm_sample_bit_depth_luma_minus1;
pcm_sample_bit_depth_chroma_minus1;
log2_min_pcm_luma_coding_block_size_minus3;
log2_diff_max_min_pcm_luma_coding_block_size;
pcm_loop_filter_disabled_flag;
*/
sps->num_short_term_ref_pic_sets = ip_period;
memset(&sps->strp[0], 0, sizeof(sps->strp));
for (i = 0; i < MIN(sps->num_short_term_ref_pic_sets, 64); i++)
fill_short_term_ref_pic_header(&sps->strp[i], i);
sps->long_term_ref_pics_present_flag = 0;
/* ignore below parameters seting since long_term_ref_pics_present_flag equal to 0
num_long_term_ref_pics_sps;
lt_ref_pic_poc_lsb_sps[kMaxLongTermRefPic];
used_by_curr_pic_lt_sps_flag[kMaxLongTermRefPic];
*/
sps->sps_temporal_mvp_enabled_flag = 1;
sps->strong_intra_smoothing_enabled_flag = 0;
sps->vui_parameters_present_flag = 0;
sps->sps_extension_present_flag = 0;
/* ignore below parameters seting since sps_extension_present_flag equal to 0
sps->sps_range_extension_flag
sps->sps_multilayer_extension_flag
sps->sps_3d_extension_flag
sps->sps_extension_5bits
sps->sps_extension_data_flag
*/
}
static void fill_pps_header(
struct PicParamSet *pps,
uint32_t pps_id,
uint32_t sps_id)
{
memset(pps, 0, sizeof(struct PicParamSet));
pps->pps_pic_parameter_set_id = pps_id;
pps->pps_seq_parameter_set_id = sps_id;
pps->dependent_slice_segments_enabled_flag = 0;
pps->output_flag_present_flag = 0;
pps->num_extra_slice_header_bits = 0;
pps->sign_data_hiding_enabled_flag = 0;
pps->cabac_init_present_flag = 1;
pps->num_ref_idx_l0_default_active_minus1 = 0;
pps->num_ref_idx_l1_default_active_minus1 = 0;
pps->init_qp_minus26 = initial_qp - 26;
pps->constrained_intra_pred_flag = 0;
pps->transform_skip_enabled_flag = 0;
pps->cu_qp_delta_enabled_flag =0;
if (pps->cu_qp_delta_enabled_flag)
pps->diff_cu_qp_delta_depth = 0;
pps->pps_cb_qp_offset = 0;
pps->pps_cr_qp_offset = 0;
pps->pps_slice_chroma_qp_offsets_present_flag = 0;
pps->weighted_pred_flag = 0;
pps->weighted_bipred_flag = 0;
pps->transquant_bypass_enabled_flag = 0;
pps->entropy_coding_sync_enabled_flag = 0;
pps->tiles_enabled_flag = 0;
pps->pps_loop_filter_across_slices_enabled_flag = 0;
pps->deblocking_filter_control_present_flag = 1;
pps->deblocking_filter_override_enabled_flag = 0,
pps->pps_deblocking_filter_disabled_flag = 0,
pps->pps_beta_offset_div2 = 2,
pps->pps_tc_offset_div2 = 0,
pps->pps_scaling_list_data_present_flag = 0;
pps->lists_modification_present_flag = 0;
pps->log2_parallel_merge_level_minus2 = 0;
pps->slice_segment_header_extension_present_flag = 0;
pps->pps_extension_present_flag = 0;
pps->pps_range_extension_flag = 0;
}
static void fill_slice_header(
uint32_t count,
struct PicParamSet *pps,
struct SliceHeader *slice)
{
memset(slice, 0, sizeof(struct SliceHeader));
slice->pic_output_flag = 1;
slice->colour_plane_id = 0;
slice->no_output_of_prior_pics_flag = 0;
slice->pic_order_cnt_lsb = calc_poc((current_frame_display - current_IDR_display) % MaxPicOrderCntLsb);
//slice_segment_address (u(v))
slice->picture_height_in_ctus = (frame_height + LCU_SIZE -1)/LCU_SIZE;
slice->picture_width_in_ctus = (frame_width + LCU_SIZE -1)/LCU_SIZE;
slice->slice_segment_address = 0;
slice->first_slice_segment_in_pic_flag = ((slice->slice_segment_address == 0) ? 1 : 0);
slice->slice_type = current_frame_type == FRAME_P ? (p2b ? SLICE_B :SLICE_P):
current_frame_type == FRAME_B ? SLICE_B : SLICE_I;
slice->dependent_slice_segment_flag = 0;
slice->short_term_ref_pic_set_sps_flag = 1;
slice->num_ref_idx_active_override_flag = 0;
slice->short_term_ref_pic_set_idx = slice->pic_order_cnt_lsb % ip_period;
slice->strp.num_negative_pics = numShortTerm;
slice->strp.num_positive_pics = 0;
slice->slice_sao_luma_flag = 0;
slice->slice_sao_chroma_flag = 0;
slice->slice_temporal_mvp_enabled_flag = 1;
slice->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
slice->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
slice->num_poc_total_cur = 0;
// for I slice
if (current_frame_type == FRAME_I || current_frame_type == FRAME_IDR)
{
slice->ref_pic_list_modification_flag_l0 = 0;
slice->list_entry_l0 = 0;
slice->ref_pic_list_modification_flag_l1 = 0;
slice->list_entry_l1 = 0;
}
else
{
slice->ref_pic_list_modification_flag_l0 = 1;
slice->num_poc_total_cur = 2;
}
slice->ref_pic_list_combination_flag = 0;
slice->num_ref_idx_lc_active_minus1 = 0;
slice->ref_pic_list_modification_flag_lc = 0;
slice->pic_from_list_0_flag = 0;
slice->ref_idx_list_curr = 0;
slice->mvd_l1_zero_flag = 0;
slice->cabac_init_present_flag = 0;
slice->slice_qp_delta = 0;
slice->slice_qp_delta_cb = pps->pps_cb_qp_offset;
slice->slice_qp_delta_cr = pps->pps_cr_qp_offset;
slice->deblocking_filter_override_flag = 0;
slice->disable_deblocking_filter_flag = 0;
slice->tc_offset_div2 = pps->pps_tc_offset_div2;
slice->beta_offset_div2 = pps->pps_beta_offset_div2;
slice->collocated_from_l0_flag = 1;
slice->collocated_ref_idx = pps->num_ref_idx_l0_default_active_minus1;
slice->five_minus_max_num_merge_cand = 0;
slice->slice_loop_filter_across_slices_enabled_flag = 0;
slice->num_entry_point_offsets = 0;
slice->offset_len_minus1 = 0;
}
static void protier_rbsp(bitstream *bs)
{
uint32_t i = 0;
put_ui(bs, protier_param.general_profile_space, 2);
put_ui(bs, protier_param.general_tier_flag, 1);
put_ui(bs, protier_param.general_profile_idc, 5);
for (i = 0; i < 32; i++)
put_ui(bs, protier_param.general_profile_compatibility_flag[i], 1);
put_ui(bs, protier_param.general_progressive_source_flag, 1);
put_ui(bs, protier_param.general_interlaced_source_flag, 1);
put_ui(bs, protier_param.general_non_packed_constraint_flag, 1);
put_ui(bs, protier_param.general_frame_only_constraint_flag, 1);
put_ui(bs, 0, 16);
put_ui(bs, 0, 16);
put_ui(bs, 0, 12);
put_ui(bs, protier_param.general_level_idc, 8);
}
void pack_short_term_ref_pic_setp(
bitstream *bs,
struct ShortTermRefPicParamSet* strp,
int first_strp)
{