-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvpu_wrapper_hantro_VCencoder.c
executable file
·2476 lines (2187 loc) · 78.8 KB
/
vpu_wrapper_hantro_VCencoder.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
/**
* Copyright 2019-2020,2023 NXP
*
* The following programs are the sole property of NXP,
* and contain its proprietary and confidential information.
*
* History :
* Date (y.m.d) Author Version Description
* 2019-07-10 Hou Qi 0.1 Created
*/
/** Vpu_wrapper_hantro_VCencoder.c
* vpu wrapper file contain all related hantro video encoder api exposed to
* application
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include "hantro_VC8000E_enc/hevcencapi.h"
#include "hantro_VC8000E_enc/enccommon.h"
#include "hantro_VC8000E_enc/base_type.h"
#include "hantro_VC8000E_enc/ewl.h"
#include "utils.h"
#include "vpu_wrapper.h"
static int nVpuLogLevel=0; //bit 0: api log; bit 1: raw dump; bit 2: yuv dump
#ifdef ANDROID
//#define LOG_NDEBUG 0
#define LOG_TAG "VpuWrapper"
#include <utils/Log.h>
#define LOG_PRINTF ALOGD
#define VPU_LOG_LEVELFILE "/data/vpu_log_level"
#define VPU_DUMP_RAWFILE "/data/temp_wrapper.bit"
#define VPU_DUMP_YUVFILE "/data/temp_wrapper.yuv"
#else
#define LOG_PRINTF printf
#define VPU_LOG_LEVELFILE "/etc/vpu_log_level"
#define VPU_DUMP_RAWFILE "temp_wrapper.bit"
#define VPU_DUMP_YUVFILE "temp_wrapper.yuv"
#endif
#define DUMP_ALL_DATA 1
static int g_seek_dump=DUMP_ALL_DATA; /*0: only dump data after seeking; otherwise: dump all data*/
#define VPU_DUMP_RAW (nVpuLogLevel&0x2)
#define VPU_DUMP_YUV (nVpuLogLevel&0x4)
/****************************** binary version info *********************************/
#define SEPARATOR " "
#define BASELINE_SHORT_NAME "VPUWRAPPER_ARM64"
#define OS_NAME "_LINUX"
#define VPUWRAPPER_BINARY_VERSION_STR \
(BASELINE_SHORT_NAME OS_NAME \
SEPARATOR "Build on" \
SEPARATOR __DATE__ SEPARATOR __TIME__)
#define VPU_MEM_ALIGN 0x10
#define VPU_BITS_BUF_SIZE (10*1024*1024) //bitstream buffer size
#define VPU_ENC_SEQ_DATA_SEPERATE
#define VPU_ENC_MAX_RETRIES 10000
#define VPU_ENC_DEFAULT (-255)
#define VPU_ENC_MAX_FRAME_INDEX 30
#define VPU_ENC_MIN_BITRATE 10000
#define VPU_ENC_MAX_BITRATE 60000000
#define VIRT_INDEX 0
#define PHY_INDEX 1
#define FALSE 0
#define TRUE 1
#define MIN_WIDTH 132
#define MIN_HEIGHT 96
#define VPU_ENC_DEFAULT_ALIGNMENT_H 4
#define VPU_ENC_DEFAULT_ALIGNMENT_V 4
#define ENC_QP_DEFAULT 26
#define ENC_MIN_QP_DEFAULT 0
#define ENC_MAX_QP_DEFAULT 51
#define MAX_GOPCONFIG_SIZE 8
#define ENC_MAX_GOP_SIZE 300
#define MOVING_AVERAGE_FRAMES 120
#define LEAST_MONITOR_FRAME 3
#define MAX_GOP_PIC_CONFIG_NUM 48
#define MAX_GOP_SPIC_CONFIG_NUM 16
#define ALIGN(ptr,align) ((align) ? (((unsigned long)(ptr))/(align)*(align)) : ((unsigned long)(ptr)))
#define MemAlign(mem,align) ((((unsigned int)mem)%(align))==0)
#define MemNotAlign(mem,align) ((((unsigned int)mem)%(align))!=0)
#define VPU_ENC_LOG(...) if(nVpuLogLevel&0x1) {LOG_PRINTF(__VA_ARGS__);}
#define VPU_ENC_TRACE
#define VPU_ENC_API(...) if(nVpuLogLevel&0x1) {LOG_PRINTF(__VA_ARGS__);}
#define VPU_ENC_ERROR(...) if(nVpuLogLevel&0x1) {LOG_PRINTF(__VA_ARGS__);}
#define VPU_ENC_ASSERT(exp) if((!(exp))&&(nVpuLogLevel&0x1)) {LOG_PRINTF("%s: %d : assert condition !!!\r\n",__FUNCTION__,__LINE__);}
#define MEMORY_SENTINEL 0xACDCACDC;
typedef struct FrameString
{
int num;
char type;
int poc;
int qpOffset;
double qpFactor;
int temporalId_factor;
int num_ref_pics;
int ref_pics[MAX_GOPCONFIG_SIZE];
int used_by_cur[MAX_GOPCONFIG_SIZE];
}FrameString;
FrameString RpsDefault_GOPSize_1[1] =
{
{1,'P',1,0,0.578,0,1,{-1},{1}},
};
FrameString RpsDefault_H264_GOPSize_1[1] =
{
{1,'P',1,0,0.4,0,1,{-1},{1}},
};
FrameString RpsDefault_GOPSize_2[2] =
{
{1,'P',2,0,0.6,0,1,{-2},{1}},
{2,'B',1,0,0.68,0,2,{-1,1},{1,1}},
};
FrameString RpsDefault_GOPSize_3[3] =
{
{1,'P',3,0,0.5,0,1,{-3},{1}},
{2,'B',1,0,0.5,0,2,{-1,2},{1,1}},
{3,'B',2,0,0.68,0,2,{-1,1},{1,1}},
};
FrameString RpsDefault_GOPSize_4[4] =
{
{1,'P',4,0,0.5,0,1,{-4},{1}},
{2,'B',2,0,0.3536,0,2,{-2,2},{1,1}},
{3,'B',1,0,0.5,0,3,{-1,1,3},{1,1,0}},
{4,'B',3,0,0.5,0,2,{-1,1},{1,1}},
};
FrameString RpsDefault_GOPSize_5[5] =
{
{1,'P',5,0,0.442,0,1,{-5},{1}},
{2,'B',2,0,0.3536,0,2,{-2,3},{1,1}},
{3,'B',1,0,0.68,0,3,{-1,1,4},{1,1,0}},
{4,'B',3,0,0.3536,0,2,{-1,2},{1,1}},
{5,'B',4,0,0.68,0,2,{-1,1},{1,1}},
};
FrameString RpsDefault_GOPSize_6[6] =
{
{1,'P',6,0,0.442,0,1,{-6},{1}},
{2,'B',3,0,0.3536,0,2,{-3,3},{1,1}},
{3,'B',1,0,0.3536,0,3,{-1,2,5},{1,1,0}},
{4,'B',2,0,0.68,0,3,{-1,1,4},{1,1,0}},
{5,'B',4,0,0.3536,0,2,{-1,2},{1,1}},
{6,'B',5,0,0.68,0,2,{-1,1},{1,1}},
};
FrameString RpsDefault_GOPSize_7[7] =
{
{1,'P',7,0,0.442,0,1,{-7},{1}},
{2,'B',3,0,0.3536,0,2,{-3,4},{1,1}},
{3,'B',1,0,0.3536,0,3,{-1,2,6},{1,1,0}},
{4,'B',2,0,0.68,0,3,{-1,1,5},{1,1,0}},
{5,'B',5,0,0.3536,0,2,{-2,2},{1,1}},
{6,'B',4,0,0.68,0,3,{-1,1,3},{1,1,0}},
{7,'B',6,0,0.68,0,2,{-1,1},{1,1}},
};
FrameString RpsDefault_GOPSize_8[8] =
{
{1,'P',8,0,0.442,0,1,{-8},{1}},
{2,'B',4,0,0.3536,0,2,{-4,4},{1,1}},
{3,'B',2,0,0.3536,0,3,{-2,2,6},{1,1,0}},
{4,'B',1,0,0.68,0,4,{-1,1,3,7},{1,1,0,0}},
{5,'B',3,0,0.68,0,3,{-1,1,5},{1,1,0}},
{6,'B',6,0,0.3536,0,2,{-2,2},{1,1}},
{7,'B',5,0,0.68,0,3,{-1,1,3},{1,1,0}},
{8,'B',7,0,0.68,0,2,{-1,1},{1,1}},
};
typedef enum FRAME_TYPE
{
INTRA_FRAME,
PREDICTED_FRAME,
NONINTRA_FRAME
}FRAME_TYPE;
typedef struct FRAME
{
unsigned char* fb_bus_data;
unsigned long fb_bus_address_Luma;
unsigned long fb_bus_address_ChromaU;
unsigned long fb_bus_address_ChromaV;
u32 fb_frameSize;
u32 fb_bufferSize;
FRAME_TYPE frame_type;
u32 bitrate;
}FRAME;
typedef struct STREAM_BUFFER
{
unsigned char* bus_data; // set by common
unsigned long bus_address; // set by common
u32 buf_max_size; // set by common
u32 streamlen; // set by codec
}STREAM_BUFFER;
typedef struct
{
VCEncConfig cfg;
VCEncCodingCtrl codingCfg;
VCEncRateCtrl rcCfg;
VCEncPreProcessingCfg preProcCfg;
i32 gopSize; //set 1: no B frame
u32 nPFrames; //gop size between two key frames
}CONFIG;
// internal ENCODER interface, which wraps up Hantro API
typedef struct ENCODER_PROTOTYPE ENCODER_PROTOTYPE;
struct ENCODER_PROTOTYPE
{
void (*destroy)(ENCODER_PROTOTYPE*);
VpuEncRetCode (*stream_start)(ENCODER_PROTOTYPE*, CONFIG*, STREAM_BUFFER*);
VpuEncRetCode (*stream_end)(ENCODER_PROTOTYPE*, STREAM_BUFFER*);
VpuEncRetCode (*encode)(ENCODER_PROTOTYPE*, FRAME*, STREAM_BUFFER*, CONFIG*);
};
typedef struct
{
VCEncInst inst;
VCEncIn encIn;
ENCODER_PROTOTYPE base;
u32 nIFrameCounter;
u32 nTotalFrames;
VCEncPictureCodingType nextCodingType;
}ENCODER;
typedef struct
{
/* open parameters */
VpuCodStd CodecFormat;
int bStreamStarted;
int bAvcc;
/* perf calculation*/
int totalFrameCnt;
struct timeval tvBegin;
struct timeval tvEnd;
const void *pewl;
/* hantro encoder */
ENCODER_PROTOTYPE *codec;
CONFIG config;
/* bit stream buffer */
unsigned char* pBsBufVirt;
unsigned char* pBsBufPhy;
int nBsBufLen;
int max_cu_size;
}VpuEncObj;
typedef struct
{
VpuEncObj obj;
}VpuEncHandleInternal;
i32 CheckArea(VCEncPictureArea* area, VpuEncObj* pObj)
{
i32 w = (pObj->config.cfg.width + pObj->max_cu_size - 1) / pObj->max_cu_size;
i32 h = (pObj->config.cfg.height + pObj->max_cu_size - 1) / pObj->max_cu_size;
if ((area->left < (u32)w) && (area->right < (u32)w) &&
(area->top < (u32)h) && (area->bottom < (u32)h))
return 1;
return 0;
}
static int AlignWidth(int width, int align)
{
if (!align)
return width;
else if (width - align < MIN_WIDTH)
return MIN_WIDTH;
else
return ((width) / align * align);
}
static int AlignHeight(int height, int align)
{
if (!align)
return height;
else if (height - align < MIN_HEIGHT)
return MIN_HEIGHT;
else
return (height) / align * align;
}
static int CLAMP(int val, int lo, int hi)
{
int tmp = (val > lo) ? val : lo;
return (tmp < hi) ? tmp : hi;
}
static int VpuEncLogLevelParse(int * pLogLevel)
{
int level=0;
FILE* fpVpuLog;
fpVpuLog=fopen(VPU_LOG_LEVELFILE,"r");
if (NULL==fpVpuLog){
//LOG_PRINTF("no vpu log level file: %s \r\n",VPU_LOG_LEVELFILE);
}
else {
char symbol;
int readLen = 0;
readLen = fread(&symbol,1,1,fpVpuLog);
if(feof(fpVpuLog) != 0){
//LOG_PRINTF("\n End of file reached.");
}
else {
level=atoi(&symbol);
//LOG_PRINTF("vpu log level: %d \r\n",level);
if((level<0) || (level>255)){
level=0;
}
}
fclose(fpVpuLog);
}
nVpuLogLevel=level;
//*pLogLevel=level;
return 1;
}
static void dumpStream(unsigned char* pBuf, unsigned int len)
{
FILE * pfile;
pfile = fopen(VPU_DUMP_RAWFILE,"ab");
if(pfile){
fwrite((void*)pBuf, 1, len, pfile);
fclose(pfile);
}
}
static void dumpYUV(unsigned char* pBuf, unsigned int len)
{
FILE * pfile;
pfile = fopen(VPU_DUMP_YUVFILE,"ab");
if(pfile){
fwrite((void*)pBuf, 1, len, pfile);
fclose(pfile);
}
}
/* table for max frame size for each video level */
typedef struct {
int level;
int size; // mbPerFrame, (Width / 16) * (Height / 16)
}EncLevelSizeMap;
/* H264 level size map table, sync with h1 h264 encoder */
static const EncLevelSizeMap H264LevelSizeMapTable[] = {
{VCENC_H264_LEVEL_1, 99},
{VCENC_H264_LEVEL_1_b, 99},
{VCENC_H264_LEVEL_1_1, 396},
{VCENC_H264_LEVEL_1_2, 396},
{VCENC_H264_LEVEL_1_3, 396},
{VCENC_H264_LEVEL_2, 396},
{VCENC_H264_LEVEL_2_1, 792},
{VCENC_H264_LEVEL_2_2, 1620},
{VCENC_H264_LEVEL_3, 1620},
{VCENC_H264_LEVEL_3_1, 3600},
{VCENC_H264_LEVEL_3_2, 5120},
{VCENC_H264_LEVEL_4, 8192},
{VCENC_H264_LEVEL_4_1, 8192},
{VCENC_H264_LEVEL_4_2, 8704},
{VCENC_H264_LEVEL_5, 22080},
{VCENC_H264_LEVEL_5_1, 36864},
{VCENC_H264_LEVEL_5_2, 36864},
{VCENC_H264_LEVEL_6, 35651854},
{VCENC_H264_LEVEL_6_1, 35651854},
{VCENC_H264_LEVEL_6_2, 35651854},
};
static int calculateH264Level(int width, int height)
{
// adjust H264 level based on video resolution because encoder will check this.
int i, mbPerFrame, tableLen;
mbPerFrame = ((width + 15) / 16) * ((height + 15) / 16);
tableLen = sizeof(H264LevelSizeMapTable)/sizeof(H264LevelSizeMapTable[0]);
int level = VCENC_H264_LEVEL_1;
for (i = 0; i < tableLen; i++) {
if (level == H264LevelSizeMapTable[i].level) {
if (mbPerFrame <= H264LevelSizeMapTable[i].size)
break;
else if (i + 1 < tableLen)
level = H264LevelSizeMapTable[i + 1].level;
else
return VCENC_H264_LEVEL_5_1; // mbPerFrame too big, return the highest level
}
}
return level;
}
static int selectH264Level(int level1, int level2) {
int i, tableLen, seq1, seq2;
tableLen = sizeof(H264LevelSizeMapTable)/sizeof(H264LevelSizeMapTable[0]);
for (i = 0; i < tableLen; i++) {
if (level1 == H264LevelSizeMapTable[i].level)
seq1 = i;
if (level2 == H264LevelSizeMapTable[i].level)
seq2 = i;
}
return (seq1 > seq2) ? level1 : level2;
}
static bool isProfileValid(VpuCodStd format, int profile)
{
bool ret = FALSE;
if (format == VPU_V_HEVC) {
switch (profile)
{
case VCENC_HEVC_MAIN_PROFILE:
case VCENC_HEVC_MAIN_STILL_PICTURE_PROFILE:
case VCENC_HEVC_MAIN_10_PROFILE:
ret = TRUE;
break;
default:
break;
}
} else if (format == VPU_V_AVC) {
switch (profile)
{
case VCENC_H264_BASE_PROFILE:
case VCENC_H264_MAIN_PROFILE:
case VCENC_H264_HIGH_PROFILE:
case VCENC_H264_HIGH_10_PROFILE:
ret = TRUE;
break;
default:
break;
}
}
return ret;
}
static bool isLevelValid(VpuCodStd format, int level)
{
bool ret = FALSE;
if (format == VPU_V_HEVC) {
switch (level)
{
case VCENC_HEVC_LEVEL_1:
case VCENC_HEVC_LEVEL_2:
case VCENC_HEVC_LEVEL_2_1:
case VCENC_HEVC_LEVEL_3:
case VCENC_HEVC_LEVEL_3_1:
case VCENC_HEVC_LEVEL_4:
case VCENC_HEVC_LEVEL_4_1:
case VCENC_HEVC_LEVEL_5:
case VCENC_HEVC_LEVEL_5_1:
case VCENC_HEVC_LEVEL_5_2:
case VCENC_HEVC_LEVEL_6:
ret = TRUE;
break;
default:
break;
}
} else if (format == VPU_V_AVC) {
switch (level)
{
case VCENC_H264_LEVEL_1:
case VCENC_H264_LEVEL_1_b:
case VCENC_H264_LEVEL_1_1:
case VCENC_H264_LEVEL_1_2:
case VCENC_H264_LEVEL_1_3:
case VCENC_H264_LEVEL_2:
case VCENC_H264_LEVEL_2_1:
case VCENC_H264_LEVEL_2_2:
case VCENC_H264_LEVEL_3:
case VCENC_H264_LEVEL_3_1:
case VCENC_H264_LEVEL_3_2:
case VCENC_H264_LEVEL_4:
case VCENC_H264_LEVEL_4_1:
case VCENC_H264_LEVEL_4_2:
case VCENC_H264_LEVEL_5:
case VCENC_H264_LEVEL_5_1:
case VCENC_H264_LEVEL_5_2:
case VCENC_H264_LEVEL_6:
case VCENC_H264_LEVEL_6_1:
case VCENC_H264_LEVEL_6_2:
ret = TRUE;
break;
default:
break;
}
}
return ret;
}
static void AdjustBitrate(VpuEncObj* pObj)
{
if (pObj->config.rcCfg.bitPerSecond > VPU_ENC_MAX_BITRATE)
pObj->config.rcCfg.bitPerSecond = VPU_ENC_MAX_BITRATE;
if (pObj->config.rcCfg.bitPerSecond < VPU_ENC_MIN_BITRATE)
{
// workaround to set check bitrate, calculate bitPerSecond = bitPerFrame * pInParam->nFrameRate / compression,
// so that resolution from max - min can get a approprite bitrate
int bitPerFrame = pObj->config.cfg.width * pObj->config.cfg.height * 8;
int compression = 50;
pObj->config.rcCfg.bitPerSecond = bitPerFrame / compression * pObj->config.cfg.frameRateNum
* pObj->config.cfg.frameRateDenom / 1000 * 1000;
}
}
static void VPU_EncInitConfigParams(VCEncIn *pEncIn, VCEncConfig* cfg, CONFIG* params)
{
int idx;
u32 maxRefPics = 0;
u32 maxTemporalId = 0;
cfg->codecFormat = params->cfg.codecFormat;
if (IS_H264(cfg->codecFormat))
{
switch (params->cfg.profile)
{
case VCENC_H264_BASE_PROFILE:
case VCENC_H264_MAIN_PROFILE:
case VCENC_H264_HIGH_PROFILE:
case VCENC_H264_HIGH_10_PROFILE:
cfg->profile = params->cfg.profile;
break;
default:
VPU_ENC_ERROR("Unsupported H264 encoding profile: %d", params->cfg.profile);
return;
break;
}
switch (params->cfg.level)
{
case VCENC_H264_LEVEL_1:
case VCENC_H264_LEVEL_1_b:
case VCENC_H264_LEVEL_1_1:
case VCENC_H264_LEVEL_1_2:
case VCENC_H264_LEVEL_1_3:
case VCENC_H264_LEVEL_2:
case VCENC_H264_LEVEL_2_1:
case VCENC_H264_LEVEL_2_2:
case VCENC_H264_LEVEL_3:
case VCENC_H264_LEVEL_3_1:
case VCENC_H264_LEVEL_3_2:
case VCENC_H264_LEVEL_4:
case VCENC_H264_LEVEL_4_1:
case VCENC_H264_LEVEL_4_2:
case VCENC_H264_LEVEL_5:
case VCENC_H264_LEVEL_5_1:
case VCENC_H264_LEVEL_5_2:
case VCENC_H264_LEVEL_6:
case VCENC_H264_LEVEL_6_1:
case VCENC_H264_LEVEL_6_2:
cfg->level = params->cfg.level;
break;
default:
VPU_ENC_ERROR("Unsupported H264 encoding level: %d", params->cfg.level);
return;
break;
}
}
else
{
switch (params->cfg.profile)
{
case VCENC_HEVC_MAIN_PROFILE:
case VCENC_HEVC_MAIN_STILL_PICTURE_PROFILE:
case VCENC_HEVC_MAIN_10_PROFILE:
cfg->profile = params->cfg.profile;
break;
default:
VPU_ENC_ERROR("Unsupported HEVC encoding profile: %d", params->cfg.profile);
return;
break;
}
switch (params->cfg.level)
{
case VCENC_HEVC_LEVEL_1:
case VCENC_HEVC_LEVEL_2:
case VCENC_HEVC_LEVEL_2_1:
case VCENC_HEVC_LEVEL_3:
case VCENC_HEVC_LEVEL_3_1:
case VCENC_HEVC_LEVEL_4:
case VCENC_HEVC_LEVEL_4_1:
case VCENC_HEVC_LEVEL_5:
case VCENC_HEVC_LEVEL_5_1:
case VCENC_HEVC_LEVEL_5_2:
case VCENC_HEVC_LEVEL_6:
case VCENC_HEVC_LEVEL_6_1:
case VCENC_HEVC_LEVEL_6_2:
cfg->level = params->cfg.level;
break;
default:
VPU_ENC_ERROR("Unsupported HEVC encoding level: %d", params->cfg.level);
return;
break;
}
}
cfg->tier = params->cfg.tier;
cfg->streamType = (params->cfg.streamType == VCENC_BYTE_STREAM) ? VCENC_BYTE_STREAM : VCENC_NAL_UNIT_STREAM;
cfg->width = params->cfg.width;
cfg->height = params->cfg.height;
cfg->frameRateNum = params->cfg.frameRateNum;
cfg->frameRateDenom = params->cfg.frameRateDenom;
cfg->strongIntraSmoothing = params->cfg.strongIntraSmoothing;
cfg->bitDepthLuma = params->cfg.bitDepthLuma;
cfg->bitDepthChroma = params->cfg.bitDepthChroma;
cfg->compressor = params->cfg.compressor;
cfg->interlacedFrame = params->cfg.interlacedFrame;
//specify the amount of reference frame buffers that will be allocated
for (idx = 0; idx < pEncIn->gopConfig.size; idx ++)
{
VCEncGopPicConfig *gopPiccfg = &(pEncIn->gopConfig.pGopPicCfg[idx]);
if (gopPiccfg->codingType != VCENC_INTRA_FRAME)
{
if (maxRefPics < gopPiccfg->numRefPics)
maxRefPics = gopPiccfg->numRefPics;
if (maxTemporalId < gopPiccfg->temporalId)
maxTemporalId = gopPiccfg->temporalId;
}
}
cfg->refFrameAmount = maxRefPics + cfg->interlacedFrame + pEncIn->gopConfig.ltrcnt;
cfg->maxTLayers = maxTemporalId +1;
cfg->enableOutputCuInfo = params->cfg.enableOutputCuInfo;
cfg->rdoLevel = params->cfg.rdoLevel;
cfg->verbose = params->cfg.verbose;
cfg->exp_of_input_alignment = params->cfg.exp_of_input_alignment;
cfg->exp_of_ref_alignment = params-> cfg.exp_of_ref_alignment;
cfg->exp_of_ref_ch_alignment = params->cfg.exp_of_ref_ch_alignment;
cfg->P010RefEnable = params->cfg.P010RefEnable;
cfg->enableSsim = params->cfg.enableSsim;
cfg->ctbRcMode = params->cfg.ctbRcMode;
cfg->parallelCoreNum = params->cfg.parallelCoreNum;
cfg->bPass1AdaptiveGop = (params->gopSize == 0);
cfg->picOrderCntType = params->cfg.picOrderCntType;
cfg->dumpRegister = params->cfg.dumpRegister;
cfg->rasterscan = params->cfg.rasterscan;
cfg->log2MaxPicOrderCntLsb = params->cfg.log2MaxPicOrderCntLsb;
cfg->log2MaxFrameNum = params->cfg.log2MaxFrameNum;
cfg->lookaheadDepth = params->cfg.lookaheadDepth;
cfg->cuInfoVersion = params->cfg.cuInfoVersion;
cfg->codedChromaIdc = params->cfg.codedChromaIdc;
if (params->cfg.parallelCoreNum > 1 && cfg->width * cfg->height < 256 * 256){
VPU_ENC_LOG("Disable multicore for small resolution (< 255*255)");
cfg->parallelCoreNum = params->cfg.parallelCoreNum = 1;
}
}
static void VPU_EncInitCodingCtrlParams(VCEncCodingCtrl* codingCfg, CONFIG* params)
{
int i;
codingCfg->sliceSize = params->codingCfg.sliceSize;
codingCfg->enableCabac = params->codingCfg.enableCabac;
codingCfg->cabacInitFlag = params->codingCfg.cabacInitFlag;
codingCfg->videoFullRange = params->codingCfg.videoFullRange;
codingCfg->disableDeblockingFilter = params->codingCfg.disableDeblockingFilter;
codingCfg->tc_Offset = params->codingCfg.tc_Offset;
codingCfg->beta_Offset = params->codingCfg.beta_Offset;
codingCfg->enableSao = params->codingCfg.enableSao;
codingCfg->enableDeblockOverride = params->codingCfg.enableDeblockOverride;
codingCfg->deblockOverride = params->codingCfg.deblockOverride;
codingCfg->seiMessages = params->codingCfg.seiMessages;
codingCfg->gdrDuration = params->codingCfg.gdrDuration;
codingCfg->roiMapDeltaQpEnable = params->codingCfg.roiMapDeltaQpEnable;
codingCfg->roiMapDeltaQpBlockUnit = params->codingCfg.roiMapDeltaQpBlockUnit;
codingCfg->RoimapCuCtrl_index_enable = params->codingCfg.RoimapCuCtrl_index_enable;
codingCfg->RoimapCuCtrl_enable = params->codingCfg.RoimapCuCtrl_enable;
codingCfg->roiMapDeltaQpBinEnable = params->codingCfg.roiMapDeltaQpBinEnable;
codingCfg->RoimapCuCtrl_ver = params->codingCfg.RoimapCuCtrl_ver;
codingCfg->RoiQpDelta_ver = params->codingCfg.RoiQpDelta_ver;
/* SKIP map */
codingCfg->skipMapEnable = params->codingCfg.skipMapEnable;
codingCfg->enableScalingList = params->codingCfg.enableScalingList;
codingCfg->chroma_qp_offset = params->codingCfg.chroma_qp_offset;
/*stream multi-segment*/
codingCfg->streamMultiSegmentMode = params->codingCfg.streamMultiSegmentMode;
codingCfg->streamMultiSegmentAmount = params->codingCfg.streamMultiSegmentAmount;
/* denoise */
codingCfg->noiseReductionEnable = params->codingCfg.noiseReductionEnable;
codingCfg->noiseLow = params->codingCfg.noiseLow;
codingCfg->firstFrameSigma = params->codingCfg.firstFrameSigma;
/* smart */
codingCfg->smartModeEnable = params->codingCfg.smartModeEnable;
codingCfg->smartH264LumDcTh = params->codingCfg.smartH264LumDcTh;
codingCfg->smartH264CbDcTh = params->codingCfg.smartH264CbDcTh;
codingCfg->smartH264CrDcTh = params->codingCfg.smartH264CrDcTh;
for(i = 0; i < 3; i++) {
codingCfg->smartHevcLumDcTh[i] = params->codingCfg.smartHevcLumDcTh[i];
codingCfg->smartHevcChrDcTh[i] = params->codingCfg.smartHevcChrDcTh[i];
codingCfg->smartHevcLumAcNumTh[i] = params->codingCfg.smartHevcLumAcNumTh[i];
codingCfg->smartHevcChrAcNumTh[i] = params->codingCfg.smartHevcChrAcNumTh[i];
}
codingCfg->smartH264Qp = params->codingCfg.smartH264Qp;
codingCfg->smartHevcLumQp = params->codingCfg.smartHevcLumQp;
codingCfg->smartHevcChrQp = params->codingCfg.smartHevcChrQp;
for(i = 0; i < 4; i++)
codingCfg->smartMeanTh[i] = params->codingCfg.smartMeanTh[i];
codingCfg->smartPixNumCntTh = params->codingCfg.smartPixNumCntTh;
/* tile */
codingCfg->tiles_enabled_flag = params->codingCfg.tiles_enabled_flag && !IS_H264(params->cfg.codecFormat);
codingCfg->num_tile_columns = params->codingCfg.num_tile_columns;
codingCfg->num_tile_rows = params->codingCfg.num_tile_rows;
codingCfg->loop_filter_across_tiles_enabled_flag = params->codingCfg.loop_filter_across_tiles_enabled_flag;
codingCfg->intraArea.top = params->codingCfg.intraArea.top;
codingCfg->intraArea.left = params->codingCfg.intraArea.left;
codingCfg->intraArea.bottom = params->codingCfg.intraArea.bottom;
codingCfg->intraArea.right = params->codingCfg.intraArea.right;
codingCfg->intraArea.enable = params->codingCfg.intraArea.enable;
codingCfg->ipcm1Area.top = params->codingCfg.ipcm1Area.top;
codingCfg->ipcm1Area.left = params->codingCfg.ipcm1Area.left;
codingCfg->ipcm1Area.bottom = params->codingCfg.ipcm1Area.bottom;
codingCfg->ipcm1Area.right = params->codingCfg.ipcm1Area.right;
codingCfg->ipcm1Area.enable = params->codingCfg.ipcm1Area.enable;
codingCfg->ipcm2Area.top = params->codingCfg.ipcm2Area.top;
codingCfg->ipcm2Area.left = params->codingCfg.ipcm2Area.left;
codingCfg->ipcm2Area.bottom = params->codingCfg.ipcm2Area.bottom;
codingCfg->ipcm2Area.right = params->codingCfg.ipcm2Area.right;
codingCfg->ipcm2Area.enable = params->codingCfg.ipcm2Area.enable;
codingCfg->roi1Area.top = params->codingCfg.roi1Area.top;
codingCfg->roi1Area.left = params->codingCfg.roi1Area.left;
codingCfg->roi1Area.bottom = params->codingCfg.roi1Area.bottom;
codingCfg->roi1Area.right = params->codingCfg.roi1Area.right;
codingCfg->roi1Area.enable = params->codingCfg.roi1Area.enable;
codingCfg->roi2Area.top = params->codingCfg.roi2Area.top;
codingCfg->roi2Area.left = params->codingCfg.roi2Area.left;
codingCfg->roi2Area.bottom = params->codingCfg.roi2Area.bottom;
codingCfg->roi2Area.right = params->codingCfg.roi2Area.right;
codingCfg->roi2Area.enable = params->codingCfg.roi2Area.enable;
codingCfg->roi3Area.top = params->codingCfg.roi3Area.top;
codingCfg->roi3Area.left = params->codingCfg.roi3Area.left;
codingCfg->roi3Area.bottom = params->codingCfg.roi3Area.bottom;
codingCfg->roi3Area.right = params->codingCfg.roi3Area.right;
codingCfg->roi3Area.enable = params->codingCfg.roi3Area.enable;
codingCfg->roi4Area.top = params->codingCfg.roi4Area.top;
codingCfg->roi4Area.left = params->codingCfg.roi4Area.left;
codingCfg->roi4Area.bottom = params->codingCfg.roi4Area.bottom;
codingCfg->roi4Area.right = params->codingCfg.roi4Area.right;
codingCfg->roi4Area.enable = params->codingCfg.roi4Area.enable;
codingCfg->roi5Area.top = params->codingCfg.roi5Area.top;
codingCfg->roi5Area.left = params->codingCfg.roi5Area.left;
codingCfg->roi5Area.bottom = params->codingCfg.roi5Area.bottom;
codingCfg->roi5Area.right = params->codingCfg.roi5Area.right;
codingCfg->roi5Area.enable = params->codingCfg.roi5Area.enable;
codingCfg->roi6Area.top = params->codingCfg.roi6Area.top;
codingCfg->roi6Area.left = params->codingCfg.roi6Area.left;
codingCfg->roi6Area.bottom = params->codingCfg.roi6Area.bottom;
codingCfg->roi6Area.right = params->codingCfg.roi6Area.right;
codingCfg->roi6Area.enable = params->codingCfg.roi6Area.enable;
codingCfg->roi7Area.top = params->codingCfg.roi7Area.top;
codingCfg->roi7Area.left = params->codingCfg.roi7Area.left;
codingCfg->roi7Area.bottom = params->codingCfg.roi7Area.bottom;
codingCfg->roi7Area.right = params->codingCfg.roi7Area.right;
codingCfg->roi7Area.enable = params->codingCfg.roi7Area.enable;
codingCfg->roi8Area.top = params->codingCfg.roi8Area.top;
codingCfg->roi8Area.left = params->codingCfg.roi8Area.left;
codingCfg->roi8Area.bottom = params->codingCfg.roi8Area.bottom;
codingCfg->roi8Area.right = params->codingCfg.roi8Area.right;
codingCfg->roi8Area.enable = params->codingCfg.roi8Area.enable;
codingCfg->roi1Qp = params->codingCfg.roi1Qp;
codingCfg->roi2Qp = params->codingCfg.roi2Qp;
codingCfg->roi3Qp = params->codingCfg.roi3Qp;
codingCfg->roi4Qp = params->codingCfg.roi4Qp;
codingCfg->roi5Qp = params->codingCfg.roi5Qp;
codingCfg->roi6Qp = params->codingCfg.roi6Qp;
codingCfg->roi7Qp = params->codingCfg.roi7Qp;
codingCfg->roi8Qp = params->codingCfg.roi8Qp;
}
static void VPU_EncInitRateCtrlParams(VCEncRateCtrl* rcCfg, CONFIG* params)
{
rcCfg->qpHdr = params->rcCfg.qpHdr;
if (params->rcCfg.qpMinPB >= 0)
{
rcCfg->qpMinPB = params->rcCfg.qpMinPB;
if (rcCfg->qpHdr != -1 && rcCfg->qpHdr < rcCfg->qpMinPB)
rcCfg->qpHdr = rcCfg->qpMinPB;
}
if (params->rcCfg.qpMaxPB >= 0)
{
rcCfg->qpMaxPB = params->rcCfg.qpMaxPB;
if (rcCfg->qpHdr != -1 && rcCfg->qpHdr > rcCfg->qpMaxPB)
rcCfg->qpHdr = rcCfg->qpMaxPB;
}
if (params->rcCfg.qpMinI >= 0)
{
rcCfg->qpMinI = params->rcCfg.qpMinI;
if (rcCfg->qpHdr != -1 && rcCfg->qpHdr < rcCfg->qpMinI)
rcCfg->qpHdr = rcCfg->qpMinPB;
}
if (params->rcCfg.qpMaxI >= 0)
{
rcCfg->qpMaxI = params->rcCfg.qpMaxI;
if (rcCfg->qpHdr != -1 && rcCfg->qpHdr > rcCfg->qpMaxI)
rcCfg->qpHdr = rcCfg->qpMaxI;
}
rcCfg->pictureSkip = params->rcCfg.pictureSkip;
rcCfg->pictureRc = params->rcCfg.pictureRc;
rcCfg->ctbRc = params->rcCfg.ctbRc;
rcCfg->blockRCSize = params->rcCfg.blockRCSize;
rcCfg->rcQpDeltaRange = params->rcCfg.rcQpDeltaRange;
rcCfg->rcBaseMBComplexity = params->rcCfg.rcBaseMBComplexity;
rcCfg->bitPerSecond = params->rcCfg.bitPerSecond;
rcCfg->bitVarRangeI = params->rcCfg.bitVarRangeI;
rcCfg->bitVarRangeP = params->rcCfg.bitVarRangeP;
rcCfg->bitVarRangeB = params->rcCfg.bitVarRangeB;
rcCfg->tolMovingBitRate = params->rcCfg.tolMovingBitRate;
rcCfg->longTermQpDelta = params->rcCfg.longTermQpDelta;
rcCfg->monitorFrames = (params->cfg.frameRateNum + params->cfg.frameRateDenom - 1) / params->cfg.frameRateDenom;
params->rcCfg.monitorFrames = rcCfg->monitorFrames;
if(rcCfg->monitorFrames > MOVING_AVERAGE_FRAMES)
rcCfg->monitorFrames = MOVING_AVERAGE_FRAMES;
if (rcCfg->monitorFrames < 10)
{
rcCfg->monitorFrames = (params->cfg.frameRateNum > params->cfg.frameRateDenom) ? 10 : LEAST_MONITOR_FRAME;
}
rcCfg->hrd = params->rcCfg.hrd;
rcCfg->hrdCpbSize = params->rcCfg.hrdCpbSize;
if (params->rcCfg.bitrateWindow != 0)
rcCfg->bitrateWindow = MIN (params->rcCfg.bitrateWindow, ENC_MAX_GOP_SIZE);
if (params->rcCfg.intraQpDelta != 0)
rcCfg->intraQpDelta = params->rcCfg.intraQpDelta;
rcCfg->vbr = params->rcCfg.vbr;
rcCfg->fixedIntraQp = params->rcCfg.fixedIntraQp;
rcCfg->smoothPsnrInGOP = params->rcCfg.smoothPsnrInGOP;
rcCfg->u32StaticSceneIbitPercent = params->rcCfg.u32StaticSceneIbitPercent;
}
static void VPU_EncInitPreProcessorParams(VCEncPreProcessingCfg* preProcCfg, CONFIG* params)
{
preProcCfg->origWidth = params->preProcCfg.origWidth;
preProcCfg->origHeight = params->preProcCfg.origHeight;
preProcCfg->xOffset = params->preProcCfg.xOffset;
preProcCfg->yOffset = params->preProcCfg.yOffset;
preProcCfg->inputType = (VCEncPictureType)params->preProcCfg.inputType;
preProcCfg->rotation = (VCEncPictureRotation)params->preProcCfg.rotation;
preProcCfg->mirror = (VCEncPictureMirror)params->preProcCfg.mirror;
if (params->cfg.interlacedFrame)
preProcCfg->origHeight /= 2;
preProcCfg->colorConversion.type = (VCEncColorConversionType)params->preProcCfg.colorConversion.type;
if (preProcCfg->colorConversion.type == VCENC_RGBTOYUV_USER_DEFINED)
{
preProcCfg->colorConversion.coeffA = 20000;
preProcCfg->colorConversion.coeffB = 44000;
preProcCfg->colorConversion.coeffC = 5000;
preProcCfg->colorConversion.coeffE = 35000;
preProcCfg->colorConversion.coeffF = 38000;
preProcCfg->colorConversion.coeffG = 35000;
preProcCfg->colorConversion.coeffH = 38000;
preProcCfg->colorConversion.LumaOffset = 0;
}
if (preProcCfg->rotation && preProcCfg->rotation != 3)
{
preProcCfg->scaledWidth = params->preProcCfg.scaledHeight;
preProcCfg->scaledHeight = params->preProcCfg.scaledWidth;
}
else
{
preProcCfg->scaledWidth = params->preProcCfg.scaledWidth;
preProcCfg->scaledHeight = params->preProcCfg.scaledHeight;
}
preProcCfg->input_alignment = params->preProcCfg.input_alignment;
preProcCfg->constChromaEn = params->preProcCfg.constChromaEn;
if (params->preProcCfg.constCb != 0)
preProcCfg->constCb = params->preProcCfg.constCb;
if (params->preProcCfg.constCr != 0)
preProcCfg->constCr = params->preProcCfg.constCr;
}
static int VPU_EncParseGopConfigString(FrameString *line, VCEncGopConfig *gopCfg, int frame_idx, int gopSize)
{
if (!line)
return -1;
int frameN, poc, num_ref_pics, i;
char type;
VCEncGopPicConfig* gopPicCfg = NULL;
//frame idx
frameN = line->num;
if ((frameN != (frame_idx + 1)) && (frameN != 0)) return -1;
if (frameN > gopSize)
return 0;
gopPicCfg = &(gopCfg->pGopPicCfg[gopCfg->size++]);
//frame type
type = line->type;
if (type == 'P' || type == 'p')
gopPicCfg->codingType = VCENC_PREDICTED_FRAME;
else if (type == 'B' || type == 'b')
gopPicCfg->codingType = VCENC_BIDIR_PREDICTED_FRAME;
else
return -1;
poc = line->poc;
if (poc < 1 || poc > gopSize) return -1;
gopPicCfg->poc = poc;
gopPicCfg->QpOffset = line->qpOffset;
gopPicCfg->QpFactor = line->qpFactor;
// sqrt(QpFactor) is used in calculating lambda
gopPicCfg->QpFactor = sqrt(gopPicCfg->QpFactor);
gopPicCfg->temporalId = line->temporalId_factor;
//num_ref_pics
num_ref_pics = line->num_ref_pics;
if (num_ref_pics < 0 || num_ref_pics > VCENC_MAX_REF_FRAMES)
{
VPU_ENC_ERROR("GOP Config: Error, num_ref_pic can not be more than %d", VCENC_MAX_REF_FRAMES);
return -1;
}
//ref_pics
for (i = 0; i < num_ref_pics; i++)
{
gopPicCfg->refPics[i].ref_pic = line->ref_pics[i];
}
//used_by_cur
for (i = 0; i < num_ref_pics; i++)
{
gopPicCfg->refPics[i].used_by_cur = line->used_by_cur[i];
}
gopPicCfg->numRefPics = num_ref_pics;
return 0;
}
static int VPU_EncReadGopConfig(FrameString *config, VCEncGopConfig *gopCfg, int gopSize, u8 *gopCfgOffset)
{
int ret = -1;
if (gopCfg->size >= MAX_GOP_PIC_CONFIG_NUM)
return -1;
if (gopCfgOffset)
gopCfgOffset[gopSize] = gopCfg->size;
if(config)
{
int id = 0;
while (config[id].num)
{
VPU_EncParseGopConfigString (&config[id], gopCfg, id, gopSize);
id ++;
}
ret = 0;