-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathav1encode.c
2978 lines (2473 loc) · 90.8 KB
/
av1encode.c
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
#define LIBVA_UTILS_UPLOAD_DOWNLOAD_YUV_SURFACE 1
#include <stdio.h>
#include <stdbool.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_display.h"
#include "av1encode.h"
#include "libmebo/libmebo_interface.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); \
}
#define MIN(a, b) ((a)>(b)?(b):(a))
#define MAX(a, b) ((a)>(b)?(a):(b))
#define CHECK_NULL(p) \
if(!(p)) \
{ \
fprintf(stderr, "Null pointer at:%s:%d\n", __func__, __LINE__); \
exit(1); \
}
#define CHECK_CONDITION(cond) \
if(!(cond)) \
{ \
fprintf(stderr, "Unexpected condition: %s:%d\n", __func__, __LINE__); \
exit(1); \
}
#define CHECK_BS_NULL(p) \
CHECK_NULL(p) \
CHECK_NULL(p->buffer)
#include "loadsurface.h"
/*****
*
* Bit stream management
*
*
* */
#define BITSTREAM_ALLOCATE_STEPPING 1024 // in byte
struct __bitstream {
uint8_t *buffer; // managed by u8 to avoid swap every 4byte
int bit_offset;
int max_size_in_byte;
};
typedef struct __bitstream bitstream;
static void
bitstream_start(bitstream *bs)
{
CHECK_NULL(bs);
bs->max_size_in_byte = BITSTREAM_ALLOCATE_STEPPING;
bs->buffer = calloc(bs->max_size_in_byte * sizeof(uint8_t), 1);
CHECK_NULL(bs->buffer);
bs->bit_offset = 0;
}
static void
put_ui(bitstream* bs, uint32_t val, int size_in_bits)
{
CHECK_BS_NULL(bs);
CHECK_CONDITION((size_in_bits + bs->bit_offset) <= (8 * bs->max_size_in_byte));
int remain_bits = 8 - (bs->bit_offset % 8);
// make sure val does not overflow size_in_bits
val &= (0xffffffff >> (32 - size_in_bits));
if(size_in_bits <= remain_bits)
{
bs->buffer[bs->bit_offset / 8] |= val << (remain_bits - size_in_bits);
bs->bit_offset += size_in_bits;
}
else
{
put_ui(bs, val >> (size_in_bits - remain_bits), remain_bits);
put_ui(bs, val & (~(0xffffffff << (size_in_bits - remain_bits))), size_in_bits - remain_bits);
}
}
static void
put_aligning_bits(bitstream* bs)
{
CHECK_BS_NULL(bs);
while (bs->bit_offset & 7)
put_ui(bs, 0, 1);; //trailing_zero_bit
}
static void
put_trailing_bits(bitstream* bs)
{
CHECK_BS_NULL(bs);
put_ui(bs, 1, 1); //trailing_one_bit
while (bs->bit_offset & 7)
put_ui(bs, 0, 1);; //trailing_zero_bit
}
static void
bitstream_free(bitstream* bs)
{
CHECK_BS_NULL(bs);
free(bs->buffer);
bs->bit_offset = 0;
bs->max_size_in_byte = 0;
}
static void
bitstream_cat(bitstream *bs1, bitstream *bs2)
{
CHECK_NULL(bs1);
CHECK_NULL(bs2);
CHECK_CONDITION(! (bs1->bit_offset & 7));
if(! (bs1->bit_offset & 7)) //byte aligned
{
memcpy(bs1->buffer + (bs1->bit_offset / 8), bs2->buffer, bs2->bit_offset/8);
bs1->bit_offset += bs2->bit_offset;
bitstream_free(bs2);
}
else
{
//when call this function to concat 2 bitstreams, the first bitstream should always be byte aligned
CHECK_CONDITION(0);
}
}
/******
* definition of para set structure
*
*
*
* */
#define PRIMARY_REF_BITS 3
#define PRIMARY_REF_NONE 7
#define REF_FRAMES_LOG2 3
#define NUM_REF_FRAMES (1 << REF_FRAMES_LOG2)
#define REFS_PER_FRAME 7
#define TOTAL_REFS_PER_FRAME 8
#define MAX_MODE_LF_DELTAS 2
#define MAX_MB_PLANE 3
#define CDEF_MAX_STRENGTHS 8
#define CDEF_STRENGTH_BITS 6
#define CDEF_STRENGTH_DIVISOR 4
#define AV1_MAX_NUM_TILE_COLS 64
#define AV1_MAX_NUM_TILE_ROWS 64
#define MAX_NUM_OPERATING_POINTS 32
#define SURFACE_NUM 16 /* 16 surfaces for source YUV */
enum {
SINGLE_REFERENCE = 0,
COMPOUND_REFERENCE = 1,
REFERENCE_MODE_SELECT = 2,
REFERENCE_MODES = 3,
} REFERENCE_MODE;
enum FRAME_TYPE
{
KEY_FRAME = 0,
INTER_FRAME = 1,
INTRA_ONLY_FRAME = 2,
SWITCH_FRAME = 3,
NUM_FRAME_TYPES,
};
enum INTERP_FILTER{
EIGHTTAP_REGULAR,
EIGHTTAP_SMOOTH,
EIGHTTAP_SHARP,
BILINEAR,
SWITCHABLE,
INTERP_FILTERS_ALL
};
enum AV1_OBU_TYPE
{
OBU_SEQUENCE_HEADER = 1,
OBU_TEMPORAL_DELIMITER = 2,
OBU_FRAME_HEADER = 3,
OBU_TILE_GROUP = 4,
OBU_METADATA = 5,
OBU_FRAME = 6,
OBU_REDUNDANT_FRAME_HEADER = 7,
OBU_PADDING = 15,
};
struct LoopFilterParams
{
int32_t loop_filter_level[4];
int32_t loop_filter_sharpness;
uint8_t loop_filter_delta_enabled;
uint8_t loop_filter_delta_update;
// 0 = Intra, Last, Last2, Last3, GF, BWD, ARF
int8_t loop_filter_ref_deltas[TOTAL_REFS_PER_FRAME];
// 0 = ZERO_MV, MV
int8_t loop_filter_mode_deltas[MAX_MODE_LF_DELTAS];
};
struct TileInfoAv1
{
uint32_t uniform_tile_spacing_flag;
uint32_t TileColsLog2;
uint32_t TileRowsLog2;
uint32_t TileCols;
uint32_t TileRows;
uint32_t TileWidthInSB[AV1_MAX_NUM_TILE_COLS]; // valid for 0 <= i < TileCols
uint32_t TileHeightInSB[AV1_MAX_NUM_TILE_ROWS]; // valid for 0 <= i < TileRows
uint32_t context_update_tile_id;
uint32_t TileSizeBytes;
};
struct QuantizationParams
{
uint32_t base_q_idx;
int32_t DeltaQYDc;
int32_t DeltaQUDc;
int32_t DeltaQUAc;
int32_t DeltaQVDc;
int32_t DeltaQVAc;
uint32_t using_qmatrix;
uint32_t qm_y;
uint32_t qm_u;
uint32_t qm_v;
};
struct CdefParams
{
uint32_t cdef_damping;
uint32_t cdef_bits;
uint32_t cdef_y_pri_strength[CDEF_MAX_STRENGTHS];
uint32_t cdef_y_sec_strength[CDEF_MAX_STRENGTHS];
uint32_t cdef_uv_pri_strength[CDEF_MAX_STRENGTHS];
uint32_t cdef_uv_sec_strength[CDEF_MAX_STRENGTHS];
};
enum RestorationType
{
RESTORE_NONE,
RESTORE_SWITCHABLE,
RESTORE_WIENER,
RESTORE_SGRPROJ,
RESTORE_TYPES = 4,
};
enum {
BITDEPTH_8 = 8,
BITDEPTH_10 = 10
};
enum {
INTRA_FRAME = 0,
LAST_FRAME = 1,
LAST2_FRAME = 2,
LAST3_FRAME = 3,
GOLDEN_FRAME = 4,
BWDREF_FRAME = 5,
ALTREF2_FRAME = 6,
ALTREF_FRAME = 7,
MAX_REF_FRAMES = 8
};
struct LRParams
{
enum RestorationType lr_type[MAX_MB_PLANE];
uint32_t lr_unit_shift;
uint32_t lr_uv_shift;
uint32_t lr_unit_extra_shift;
};
enum TX_MODE{
ONLY_4X4 = 0, // only 4x4 transform used
TX_MODE_LARGEST, // transform size is the largest possible for pu size
TX_MODE_SELECT, // transform specified for each block
TX_MODES,
};
struct ColorConfig
{
uint32_t BitDepth ;
uint32_t mono_chrome ;
uint32_t color_primaries ;
uint32_t transfer_characteristics ;
uint32_t matrix_coefficients ;
uint32_t color_description_present_flag;
uint32_t color_range ;
uint32_t chroma_sample_position ;
uint32_t subsampling_x ;
uint32_t subsampling_y ;
uint32_t separate_uv_delta_q ;
};
typedef struct FrameHeader
{
uint32_t show_existing_frame;
uint32_t frame_to_show_map_idx;
uint64_t frame_presentation_time;
uint32_t display_frame_id;
enum FRAME_TYPE frame_type;
uint32_t show_frame;
uint32_t showable_frame;
uint32_t error_resilient_mode;
uint32_t disable_cdf_update;
uint32_t allow_screen_content_tools;
uint32_t force_integer_mv;
uint32_t frame_size_override_flag;
uint32_t order_hint;
uint32_t primary_ref_frame;
uint8_t refresh_frame_flags;
uint32_t FrameWidth;//input
uint32_t FrameHeight;//input
uint32_t use_superres;
uint32_t SuperresDenom;
uint32_t UpscaledWidth;
uint32_t RenderWidth;
uint32_t RenderHeight;
uint32_t allow_intrabc;
int32_t ref_frame_idx[REFS_PER_FRAME];
uint32_t allow_high_precision_mv;
enum INTERP_FILTER interpolation_filter;
//uint32_t is_motion_mode_switchable;
uint32_t use_ref_frame_mvs;
uint32_t disable_frame_end_update_cdf;
uint32_t sbCols;
uint32_t sbRows;
uint32_t sbSize; //64 by default
struct TileInfoAv1 tile_info;
struct QuantizationParams quantization_params;
uint32_t delta_q_present;
uint32_t delta_q_res;
uint32_t delta_lf_present;
uint32_t delta_lf_res;
uint32_t delta_lf_multi;
uint32_t CodedLossless;
uint32_t AllLossless;
struct LoopFilterParams loop_filter_params;
struct CdefParams cdef_params;
struct LRParams lr_params;
enum TX_MODE TxMode;
uint32_t reference_select;
uint32_t skipModeAllowed;
uint32_t skipModeFrame[2];
uint32_t skip_mode_present;
uint32_t allow_warped_motion;
uint32_t reduced_tx_set;
} FH;
typedef struct SequenceHeader
{
uint32_t seq_profile ;
uint32_t still_picture ;
uint32_t reduced_still_picture_header ;
uint32_t timing_info_present_flag ;
uint32_t decoder_model_info_present_flag ;
uint32_t operating_points_cnt_minus_1 ;
uint32_t operating_point_idc[MAX_NUM_OPERATING_POINTS] ;
uint32_t seq_level_idx[MAX_NUM_OPERATING_POINTS] ;
uint32_t seq_tier[MAX_NUM_OPERATING_POINTS] ;
uint32_t decoder_model_present_for_this_op[MAX_NUM_OPERATING_POINTS] ;
uint32_t initial_display_delay_minus_1[MAX_NUM_OPERATING_POINTS] ;
uint32_t frame_width_bits ; //15 as default value
uint32_t frame_height_bits ; //15 as default value
uint32_t frame_id_numbers_present_flag ; // default 0
uint32_t sbSize ; //default 64
uint32_t enable_filter_intra ;
uint32_t enable_intra_edge_filter ;
uint32_t enable_interintra_compound ;
uint32_t enable_masked_compound ;
uint32_t enable_warped_motion ;
uint32_t enable_dual_filter ;
uint32_t enable_order_hint ;//default set to 1
uint32_t enable_jnt_comp ;
uint32_t enable_ref_frame_mvs ;
uint32_t seq_force_screen_content_tools ;
uint32_t seq_force_integer_mv ;
uint32_t order_hint_bits_minus1 ; //default 8 - 1
uint32_t enable_superres ;
uint32_t enable_cdef ;
uint32_t enable_restoration ;
struct ColorConfig color_config; //default
uint32_t film_grain_param_present ;
uint32_t frame_rate_extN;
uint32_t frame_rate_extD;
} SH;
struct ObuExtensionHeader {
uint32_t temporal_id;
uint32_t spatial_id;
};
struct BitOffsets
{
uint32_t QIndexBitOffset;
uint32_t SegmentationBitOffset;
uint32_t SegmentationBitSize;
uint32_t LoopFilterParamsBitOffset;
uint32_t FrameHdrOBUSizeInBits;
uint32_t FrameHdrOBUSizeByteOffset;
uint32_t UncompressedHeaderByteOffset;
uint32_t CDEFParamsBitOffset;
uint32_t CDEFParamsSizeInBits;
};
static VADisplay va_dpy;
static VAProfile av1_profile;
static VAEntrypoint entryPoint;
static VAConfigAttrib attrib[VAConfigAttribTypeMax];
static VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
static int config_attrib_num = 0;
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;
// buffer
static VAEncSequenceParameterBufferAV1 seq_param;
static VAEncPictureParameterBufferAV1 pic_param;
static VAEncTileGroupBufferAV1 tile_group_param;
// sh fh ips
struct Av1InputParameters ips;
static FH fh;
static SH sh;
struct BitOffsets offsets;
//Default entrypoint for Encode
static VAEntrypoint requested_entrypoint = -1;
static unsigned long long current_frame_encoding = 0;
static unsigned long long current_frame_display = 0;
static int current_frame_type;
#define current_slot (current_frame_display % SURFACE_NUM)
/* 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; moved to input pars
static pthread_mutex_t encode_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t encode_cond = PTHREAD_COND_INITIALIZER;
static pthread_t encode_thread;
static FILE *coded_fp = NULL, *srcyuv_fp = NULL, *recyuv_fp = NULL;
static unsigned long long srcyuv_frames = 0;
static int srcyuv_fourcc = VA_FOURCC_IYUV;
static uint64_t frame_size = 0;
/* 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;
static unsigned int frame_coded = 0;
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 int len_ivf_header;
static int len_seq_header;
static int len_pic_header;
/*
* Helper function for profiling purposes
*/
static unsigned int GetTickCount()
{
struct timeval tv;
if (gettimeofday(&tv, NULL))
return 0;
return tv.tv_usec / 1000 + tv.tv_sec * 1000;
}
static int string_to_fourcc(char *str)
{
CHECK_NULL(str);
int fourcc;
if (!strncmp(str, "NV12", 4))
fourcc = VA_FOURCC_NV12;
else if (!strncmp(str, "IYUV", 4))
fourcc = VA_FOURCC_IYUV;
else if (!strncmp(str, "YV12", 4))
fourcc = VA_FOURCC_YV12;
else if (!strncmp(str, "UYVY", 4))
fourcc = VA_FOURCC_UYVY;
else {
printf("Unknow FOURCC\n");
fourcc = -1;
}
return fourcc;
}
static int string_to_rc(char *str)
{
CHECK_NULL(str);
int rc_mode;
if (!strncmp(str, "NONE", 4))
rc_mode = VA_RC_NONE;
else if (!strncmp(str, "CBR", 3))
rc_mode = VA_RC_CBR;
else if (!strncmp(str, "VBR", 3))
rc_mode = VA_RC_VBR;
else if (!strncmp(str, "VCM", 3))
rc_mode = VA_RC_VCM;
else if (!strncmp(str, "CQP", 3))
rc_mode = VA_RC_CQP;
else if (!strncmp(str, "VBR_CONSTRAINED", 15))
rc_mode = VA_RC_VBR_CONSTRAINED;
else {
printf("Unknown RC mode\n");
rc_mode = -1;
}
return rc_mode;
}
static void print_help()
{
printf("./av1encode <options>\n");
printf(" -n <frames> -f <frame rate> -o <output>\n");
printf(" --intra_period <number>\n");
printf(" --ip_period <number>\n");
printf(" --rcmode <16 for CQP>\n");
printf(" --srcyuv <filename> load YUV from a file\n");
printf(" --fourcc <NV12|IYUV|YV12> source YUV fourcc\n");
printf(" --recyuv <filename> save reconstructed YUV into a file\n");
printf(" --enablePSNR calculate PSNR of recyuv vs. srcyuv\n");
printf(" --level\n");
printf(" --height <number>\n");
printf(" --width <number>\n");
printf(" --base_q_idx <number> 1-255\n");
printf(" --normal_mode select VAEntrypointEncSlice as entrypoint\n");
printf(" --low_power_mode select VAEntrypointEncSliceLP as entrypoint\n");
printf(" --enswbrc 1 or 0 , 1 enable software brc, 0 use hw brc\n");
printf(" sample usage:\n");
printf("./av1encode -n 8 -f 30 --intra_period 4 --ip_period 1 --rcmode CQP --srcyuv ./input.yuv --recyuv ./rec.yuv --fourcc IYUV --level 8 --width 1920 --height 1080 --base_q_idx 128 -o ./out.av1 --LDB --low_power_mode --enswbrc 1\n"
"./av1encode -n 8 -f 30 --intra_period 4 --ip_period 1 --rcmode CBR --srcyuv ./input.yuv --recyuv ./rec.yuv --fourcc IYUV --level 8 --width 1920 --height 1080 --target_bitrate 3360000 -o ./out.av1 --LDB --low_power_mode --enswbrc 0\n"
"./av1encode -n 8 -f 30 --intra_period 4 --ip_period 1 --rcmode VBR --srcyuv ./input.yuv --recyuv ./rec.yuv --fourcc IYUV --level 8 --width 1920 --height 1080 --vbr_max_bitrate 3360000 -o ./out.av1 --LDB --low_power_mode --enswbrc 0\n");
}
static void process_cmdline(int argc, char *argv[])
{
int c;
const struct option long_opts[] = {
{"help", no_argument, NULL, 0 },
{"intra_period", required_argument, NULL, 1 },
{"ip_period", required_argument, NULL, 2 },
{"rcmode", required_argument, NULL, 3 },
{"srcyuv", required_argument, NULL, 4 },
{"recyuv", required_argument, NULL, 5 },
{"fourcc", required_argument, NULL, 6 },
{"syncmode", no_argument, NULL, 7 },
{"enablePSNR", no_argument, NULL, 8 },
{"level", required_argument, NULL, 9 },
{"height", required_argument, NULL, 10 },
{"width", required_argument, NULL, 11 },
{"base_q_idx", required_argument, NULL, 12},
{"LDB", no_argument, NULL, 13},
{"normal_mode", no_argument, NULL, 14},
{"low_power_mode", no_argument, NULL, 15},
{"target_bitrate", required_argument, NULL, 16},
{"vbr_max_bitrate", required_argument, NULL, 17},
{"enswbrc", required_argument, NULL, 18},
{NULL, no_argument, NULL, 0 }
};
int long_index;
while ((c = getopt_long_only(argc, argv, "n:f:o:t:m:u:d:?", long_opts, &long_index)) != EOF)
{
switch (c)
{
case 'n':
ips.frame_count = atoi(optarg);
break;
case 'f':
ips.frame_rate_extN = (int)(atoi(optarg) * 100);
ips.frame_rate_extD = 100;
break;
case 'o':
ips.output = strdup(optarg);
break;
case 1:
ips.intra_period = atoi(optarg);
break;
case 2:
ips.ip_period = atoi(optarg);
break;
case 3:
ips.RateControlMethod = string_to_rc(optarg); //16:cqp 2:CBR 4:VBR
break;
case 4:
ips.srcyuv = strdup(optarg);
break;
case 5:
ips.recyuv = strdup(optarg);
break;
case 6:
srcyuv_fourcc = string_to_fourcc(optarg);
break;
case 7:
ips.encode_syncmode = 1;
break;
case 8:
ips.calc_psnr = 1;
break;
case 9:
ips.level = atoi(optarg);
break;
case 10:
ips.height = atoi(optarg);
ips.frame_height_aligned = (ips.height + 63) & (~63);
break;
case 11:
ips.width = atoi(optarg);
ips.frame_width_aligned = (ips.width + 63) & (~63);
break;
case 12:
ips.base_qindex = atoi(optarg);
break;
case 13:
ips.LDB = 1;
break;
case 14:
requested_entrypoint = VAEntrypointEncSlice;
break;
case 15:
requested_entrypoint = VAEntrypointEncSliceLP;
break;
case 't':
case 16:
ips.target_bitrate = atoi(optarg);
break;
case 'm':
case 17:
ips.vbr_max_bitrate = atoi(optarg);
break;
case 18:
ips.enable_swbrc = atoi(optarg);
printf("ips.enable_swbrc = %d\n", ips.enable_swbrc);
break;
case 'u':
ips.buffer_size = atoi(optarg) * 8000;
break;
case 'd':
ips.initial_buffer_fullness = atoi(optarg) * 8000;
break;
case 0:
case ':':
case '?':
print_help();
exit(0);
}
}
// init other input parameters as default value
ips.MaxBaseQIndex = 255;
ips.MinBaseQIndex = 1;
ips.bit_depth = 8;
if (ips.frame_rate_extD == 0)
{
ips.frame_rate_extN = 3000;
ips.frame_rate_extD = 100;
}
int default_bitrate = (long long int) ips.height * ips.width * 12 * ips.frame_rate_extN / ips.frame_rate_extD / 50;
// For CBR, target bitrate should be set
if(ips.RateControlMethod == VA_RC_CBR)
{
if (ips.target_bitrate == 0)
{
ips.target_bitrate = default_bitrate;
}
}
// For VBR, max bitrate should be set
else if (ips.RateControlMethod == VA_RC_VBR)
{
if (ips.target_bitrate == 0 && ips.vbr_max_bitrate == 0)
{
ips.vbr_max_bitrate = default_bitrate;
}
else if (ips.vbr_max_bitrate == 0)
{
ips.vbr_max_bitrate = ips.target_bitrate;
}
}
// interface with IO
/* open source file */
if (ips.srcyuv) {
srcyuv_fp = fopen(ips.srcyuv, "r");
if (srcyuv_fp == NULL)
printf("Open source YUV file %s failed, use auto-generated YUV data\n", ips.srcyuv);
else {
struct stat tmp;
int ret = fstat(fileno(srcyuv_fp), &tmp);
CHECK_CONDITION(ret == 0);
srcyuv_frames = tmp.st_size / (ips.width * ips.height * 1.5);
printf("Source YUV file %s with %llu frames\n", ips.srcyuv, srcyuv_frames);
if (ips.frame_count == 0)
ips.frame_count = srcyuv_frames;
}
}
/* open source file */
if (ips.recyuv) {
recyuv_fp = fopen(ips.recyuv, "w+");
if (recyuv_fp == NULL)
printf("Open reconstructed YUV file %s failed\n", ips.recyuv);
}
if (ips.output == NULL) {
struct stat buf;
if (stat("/tmp", &buf) == 0)
ips.output = strdup("/tmp/test.av1");
else if (stat("/sdcard", &buf) == 0)
ips.output = strdup("/sdcard/test.av1");
else
ips.output = strdup("./test.av1");
}
/* store coded data into a file */
if (ips.output) {
coded_fp = fopen(ips.output, "w+");
} else {
printf("Copy file string failed");
exit(1);
}
if (coded_fp == NULL) {
printf("Open file %s failed, exit\n", ips.output);
exit(1);
}
}
static char *rc_to_string(int rcmode)
{
switch (rcmode) {
case VA_RC_NONE:
return "NONE";
case VA_RC_CBR:
return "CBR";
case VA_RC_VBR:
return "VBR";
case VA_RC_VCM:
return "VCM";
case VA_RC_CQP:
return "CQP";
case VA_RC_VBR_CONSTRAINED:
return "VBR_CONSTRAINED";
default:
return "Unknown";
}
}
static int print_input()
{
printf("frame count: %d \n", ips.frame_count);
printf("frame rate: %d \n", ips.frame_rate_extN / ips.frame_rate_extD);
printf("Intra period: %d \n", ips.intra_period);
printf("Gop ref dist: %d \n", ips.ip_period);
printf("rcmode: %s \n", rc_to_string(ips.RateControlMethod));
printf("source yuv: %s \n", ips.srcyuv);
printf("recon yuv: %s \n", ips.recyuv);
printf("output bitstream: %s \n", ips.output);
printf("level index: %d \n", ips.level);
printf("frame height: %d \n", ips.height);
printf("frame width: %d \n", ips.width);
printf("base_q_index: %d \n", ips.base_qindex);
printf("target_bitrate: %d bps\n", ips.target_bitrate);
printf("vbr_max_bitrate: %d bps\n", ips.vbr_max_bitrate);
return 0;
}
static int init_va(void)
{
va_dpy = va_open_display();
VAStatus va_status;
int major_ver, minor_ver;
va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
CHECK_VASTATUS(va_status, "vaInitialize");
av1_profile = VAProfileAV1Profile0;
// select entrypoint
int num_entrypoints = vaMaxNumEntrypoints(va_dpy);
VAEntrypoint* entrypoints = malloc(num_entrypoints * sizeof(*entrypoints));
if (!entrypoints) {
fprintf(stderr, "error: failed to initialize VA entrypoints array\n");
exit(1);
}
vaQueryConfigEntrypoints(va_dpy, av1_profile, entrypoints, &num_entrypoints);
int support_encode = 0;
for (int slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++)
{
if (requested_entrypoint == -1) {
//Select the entry point based on what is avaiable
if ((entrypoints[slice_entrypoint] == VAEntrypointEncSlice) ||
(entrypoints[slice_entrypoint] == VAEntrypointEncSliceLP)) {
support_encode = 1;
entryPoint = entrypoints[slice_entrypoint];
break;
}
} else if ((entrypoints[slice_entrypoint] == requested_entrypoint)) {
//Select the entry point based on what was requested in cmd line option
support_encode = 1;
entryPoint = entrypoints[slice_entrypoint];
break;
}
}
if(entrypoints)
free(entrypoints);
if (support_encode == 0) {
printf("Can't find avaiable or requested entrypoints for AV1 profiles\n");
exit(1);
}
unsigned int i;
for (i = 0; i < VAConfigAttribTypeMax; i++)
attrib[i].type = i;
va_status = vaGetConfigAttributes(va_dpy, av1_profile, entryPoint,
&attrib[0], VAConfigAttribTypeMax);
CHECK_VASTATUS(va_status, "vaGetConfigAttributes");
/* check the interested configattrib */
if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
printf("Not find desired YUV420 RT format\n");
exit(1);
} else {
config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
config_attrib_num++;
}
if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
int tmp = attrib[VAConfigAttribRateControl].value;
printf("Support rate control mode (0x%x):", tmp);
if (tmp & VA_RC_NONE)
printf("NONE ");
if (tmp & VA_RC_CBR)
printf("CBR ");
if (tmp & VA_RC_VBR)
printf("VBR ");
if (tmp & VA_RC_VCM)
printf("VCM ");
if (tmp & VA_RC_CQP)
printf("CQP ");
if (tmp & VA_RC_VBR_CONSTRAINED)
printf("VBR_CONSTRAINED ");
printf("\n");
if (ips.RateControlMethod == -1 || !(ips.RateControlMethod & tmp)) {
if (ips.RateControlMethod != -1) {
printf("Warning: Don't support the specified RateControl mode: %s!!!, switch to ", rc_to_string(ips.RateControlMethod));
}
for (i = 0; i < sizeof(rc_default_modes) / sizeof(rc_default_modes[0]); i++) {
if (rc_default_modes[i] & tmp) {
ips.RateControlMethod = rc_default_modes[i];
break;
}
}
printf("RateControl mode: %s\n", rc_to_string(ips.RateControlMethod));
}
config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
config_attrib[config_attrib_num].value = ips.RateControlMethod;
config_attrib_num++;
}
if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
printf("Support VAConfigAttribEncPackedHeaders\n");
config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
if (tmp & VA_ENC_PACKED_HEADER_SEQUENCE) {
printf("Support packed sequence headers\n");
config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SEQUENCE;
}
if (tmp & VA_ENC_PACKED_HEADER_PICTURE) {
printf("Support packed picture headers\n");
config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_PICTURE;
}
if (tmp & VA_ENC_PACKED_HEADER_SLICE) {
printf("Support packed slice headers\n");
config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SLICE;
}
if (tmp & VA_ENC_PACKED_HEADER_MISC) {
printf("Support packed misc headers\n");
config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_MISC;
}
config_attrib_num++;
}
if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
int tmp = attrib[VAConfigAttribEncInterlaced].value;
printf("Support VAConfigAttribEncInterlaced\n");
if (tmp & VA_ENC_INTERLACED_FRAME)
printf("support VA_ENC_INTERLACED_FRAME\n");
if (tmp & VA_ENC_INTERLACED_FIELD)
printf("Support VA_ENC_INTERLACED_FIELD\n");
if (tmp & VA_ENC_INTERLACED_MBAFF)
printf("Support VA_ENC_INTERLACED_MBAFF\n");
if (tmp & VA_ENC_INTERLACED_PAFF)
printf("Support VA_ENC_INTERLACED_PAFF\n");
config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
config_attrib_num++;
}
return 0;