forked from intel/libva-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavcenc.c
2223 lines (1825 loc) · 77.6 KB
/
avcenc.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 (c) 2012 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.
*/
/*
* Simple AVC encoder based on libVA.
*
* Usage:
* ./avcenc <width> <height> <input file> <output file> [qp]
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include <pthread.h>
#include <va/va.h>
#include <va/va_enc_h264.h>
#include "va_display.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 NAL_NON_IDR 1
#define NAL_IDR 5
#define NAL_SPS 7
#define NAL_PPS 8
#define NAL_SEI 6
#define NAL_DELIMITER 9
#define SLICE_TYPE_P 0
#define SLICE_TYPE_B 1
#define SLICE_TYPE_I 2
#define FRAME_IDR 7
#define ENTROPY_MODE_CAVLC 0
#define ENTROPY_MODE_CABAC 1
#define PROFILE_IDC_BASELINE 66
#define PROFILE_IDC_MAIN 77
#define PROFILE_IDC_HIGH 100
#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); \
}
static VADisplay va_dpy;
static int picture_width, picture_width_in_mbs;
static int picture_height, picture_height_in_mbs;
static int frame_size;
static unsigned char *newImageBuffer = 0;
static int qp_value = 26;
static int intra_period = 30;
static int frame_bit_rate = -1;
static int frame_rate = 30;
static int ip_period = 1;
static int roi_test_enable = 0;
static int aud_nal_enable = 1;
static VAEntrypoint select_entrypoint = VAEntrypointEncSlice;
#define MAX_SLICES 32
static unsigned int MaxFrameNum = (1 << 12);
static unsigned int Log2MaxFrameNum = 12;
static unsigned int Log2MaxPicOrderCntLsb = 8;
static const struct option longopts[] = {
{"qp", required_argument, 0, 1},
{"fb", required_argument, 0, 2},
{"mode", required_argument, 0, 3},
{"low-power", no_argument, 0, 4},
{"roi-test", no_argument, 0, 5},
{"frames", required_argument, 0, 6},
{ NULL, 0, NULL, 0}
};
static int
build_packed_pic_buffer(unsigned char **header_buffer);
static int
build_packed_seq_buffer(unsigned char **header_buffer);
static int
build_nal_delimiter(unsigned char **header_buffer);
static int
build_packed_sei_pic_timing(unsigned int cpb_removal_length,
unsigned int dpb_output_length,
unsigned char **sei_buffer);
static int
build_packed_idr_sei_buffer_timing(unsigned int init_cpb_removal_delay_length,
unsigned int cpb_removal_length,
unsigned int dpb_output_length,
unsigned char **sei_buffer);
struct upload_thread_param {
FILE *yuv_fp;
VASurfaceID surface_id;
};
static void
upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id);
static struct {
VAProfile profile;
int constraint_set_flag;
VAEncSequenceParameterBufferH264 seq_param;
VAEncPictureParameterBufferH264 pic_param;
VAEncSliceParameterBufferH264 slice_param[MAX_SLICES];
VAContextID context_id;
VAConfigID config_id;
VABufferID seq_param_buf_id; /* Sequence level parameter */
VABufferID pic_param_buf_id; /* Picture level parameter */
VABufferID slice_param_buf_id[MAX_SLICES]; /* Slice level parameter, multil slices */
VABufferID codedbuf_buf_id; /* Output buffer, compressed data */
VABufferID packed_seq_header_param_buf_id;
VABufferID packed_seq_buf_id;
VABufferID packed_pic_header_param_buf_id;
VABufferID packed_pic_buf_id;
VABufferID packed_sei_header_param_buf_id; /* the SEI buffer */
VABufferID packed_sei_buf_id;
VABufferID misc_parameter_hrd_buf_id;
VABufferID misc_parameter_roi_buf_id;
VABufferID packed_aud_header_param_buf_id;
VABufferID packed_aud_buf_id;
int num_slices;
int codedbuf_i_size;
int codedbuf_pb_size;
int current_input_surface;
int rate_control_method;
struct upload_thread_param upload_thread_param;
pthread_t upload_thread_id;
int upload_thread_value;
int i_initial_cpb_removal_delay;
int i_initial_cpb_removal_delay_offset;
int i_initial_cpb_removal_delay_length;
int i_cpb_removal_delay;
int i_cpb_removal_delay_length;
int i_dpb_output_delay_length;
int time_offset_length;
unsigned long long idr_frame_num;
unsigned long long prev_idr_cpb_removal;
unsigned long long current_idr_cpb_removal;
unsigned long long current_cpb_removal;
/* This is relative to the current_cpb_removal */
unsigned int current_dpb_removal_delta;
} avcenc_context;
static VAPictureH264 ReferenceFrames[16], RefPicList0[32], RefPicList1[32];
static void create_encode_pipe()
{
VAEntrypoint entrypoints[5];
int num_entrypoints, slice_entrypoint;
VAConfigAttrib attrib[3];
int major_ver, minor_ver;
VAStatus va_status;
va_dpy = va_open_display();
va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
CHECK_VASTATUS(va_status, "vaInitialize");
vaQueryConfigEntrypoints(va_dpy, avcenc_context.profile, entrypoints,
&num_entrypoints);
for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
if (entrypoints[slice_entrypoint] == select_entrypoint)
break;
}
if (slice_entrypoint == num_entrypoints) {
/* not find Slice entry point */
assert(0);
}
/* find out the format for the render target, and rate control mode */
attrib[0].type = VAConfigAttribRTFormat;
attrib[1].type = VAConfigAttribRateControl;
/* This is to query whether the ROI is supported */
attrib[2].type = VAConfigAttribEncROI;
vaGetConfigAttributes(va_dpy, avcenc_context.profile, select_entrypoint,
&attrib[0], 3);
if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
/* not find desired YUV420 RT format */
assert(0);
}
if ((attrib[1].value & avcenc_context.rate_control_method) == 0) {
/* Can't find matched RC mode */
printf("Can't find the desired RC mode, exit\n");
assert(0);
}
if (roi_test_enable) {
if (attrib[2].value != VA_ATTRIB_NOT_SUPPORTED) {
VAConfigAttribValEncROI *roi_config = (VAConfigAttribValEncROI *) & (attrib[2].value);
if (roi_config->bits.num_roi_regions == 0 ||
roi_config->bits.roi_rc_qp_delta_support == 0) {
roi_test_enable = 0;
printf("WARNING: do not support ROI or ROI delta QP ! \n");
}
} else {
roi_test_enable = 0;
printf("WARNING: do not support VAConfigAttribValEncROI! \n");
}
}
attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
attrib[1].value = avcenc_context.rate_control_method; /* set to desired RC mode */
if (roi_test_enable) {
va_status = vaCreateConfig(va_dpy, avcenc_context.profile, select_entrypoint,
&attrib[0], 3, &avcenc_context.config_id);
} else {
va_status = vaCreateConfig(va_dpy, avcenc_context.profile, select_entrypoint,
&attrib[0], 2, &avcenc_context.config_id);
}
CHECK_VASTATUS(va_status, "vaCreateConfig");
/* Create a context for this decode pipe */
va_status = vaCreateContext(va_dpy, avcenc_context.config_id,
picture_width, picture_height,
VA_PROGRESSIVE,
0, 0,
&avcenc_context.context_id);
CHECK_VASTATUS(va_status, "vaCreateContext");
}
static void destory_encode_pipe()
{
vaDestroyContext(va_dpy, avcenc_context.context_id);
vaDestroyConfig(va_dpy, avcenc_context.config_id);
vaTerminate(va_dpy);
va_close_display(va_dpy);
}
/***************************************************
*
* The encode pipe resource define
*
***************************************************/
#define SID_INPUT_PICTURE_0 0
#define SID_INPUT_PICTURE_1 1
#define SID_REFERENCE_PICTURE_L0 2
#define SID_REFERENCE_PICTURE_L1 3
#define SID_RECON_PICTURE 4
#define SID_NUMBER SID_RECON_PICTURE + 1
#define SURFACE_NUM 16 /* 16 surfaces for reference */
static VASurfaceID surface_ids[SID_NUMBER];
static VASurfaceID ref_surface[SURFACE_NUM];
static int use_slot[SURFACE_NUM];
static unsigned long long current_frame_display = 0;
static unsigned long long current_IDR_display = 0;
static VAPictureH264 CurrentCurrPic;
#define current_slot (current_frame_display % SURFACE_NUM)
static int frame_number;
static unsigned long long enc_frame_number;
static int current_frame_type;
static int current_frame_num;
static unsigned int current_poc;
static unsigned int num_ref_frames = 2;
static unsigned int numShortTerm = 0;
/***************************************************/
static int get_free_slot()
{
int i, index = -1;
for (i = 0; i < SURFACE_NUM; i++) {
if (use_slot[i] == 0) {
index = i;
break;
}
}
if (index < 0) {
printf("WARNING: No free slot to store the reconstructed frame \n");
index = SURFACE_NUM - 1;
}
return index;
}
static void *
upload_thread_function(void *data)
{
struct upload_thread_param *param = data;
upload_yuv_to_surface(param->yuv_fp, param->surface_id);
return NULL;
}
static void alloc_encode_resource(FILE *yuv_fp)
{
VAStatus va_status;
// Create surface
va_status = vaCreateSurfaces(
va_dpy,
VA_RT_FORMAT_YUV420, picture_width, picture_height,
surface_ids, SID_NUMBER,
NULL, 0
);
CHECK_VASTATUS(va_status, "vaCreateSurfaces");
// Create surface
va_status = vaCreateSurfaces(
va_dpy,
VA_RT_FORMAT_YUV420, picture_width, picture_height,
ref_surface, SURFACE_NUM,
NULL, 0
);
CHECK_VASTATUS(va_status, "vaCreateSurfaces");
newImageBuffer = (unsigned char *)malloc(frame_size);
/* firstly upload YUV data to SID_INPUT_PICTURE_1 */
avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
avcenc_context.upload_thread_param.surface_id = surface_ids[SID_INPUT_PICTURE_1];
avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
NULL,
upload_thread_function,
(void*)&avcenc_context.upload_thread_param);
}
static void release_encode_resource()
{
pthread_join(avcenc_context.upload_thread_id, NULL);
free(newImageBuffer);
// Release all the surfaces resource
vaDestroySurfaces(va_dpy, surface_ids, SID_NUMBER);
// Release all the reference surfaces
vaDestroySurfaces(va_dpy, ref_surface, SURFACE_NUM);
}
static void avcenc_update_sei_param(int is_idr)
{
VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
unsigned int length_in_bits;
unsigned char *packed_sei_buffer = NULL;
VAStatus va_status;
if (is_idr)
length_in_bits = build_packed_idr_sei_buffer_timing(
avcenc_context.i_initial_cpb_removal_delay_length,
avcenc_context.i_cpb_removal_delay_length,
avcenc_context.i_dpb_output_delay_length,
&packed_sei_buffer);
else
length_in_bits = build_packed_sei_pic_timing(
avcenc_context.i_cpb_removal_delay_length,
avcenc_context.i_dpb_output_delay_length,
&packed_sei_buffer);
packed_header_param_buffer.type = VAEncPackedHeaderRawData;
packed_header_param_buffer.bit_length = length_in_bits;
packed_header_param_buffer.has_emulation_bytes = 0;
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderParameterBufferType,
sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
&avcenc_context.packed_sei_header_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderDataBufferType,
(length_in_bits + 7) / 8, 1, packed_sei_buffer,
&avcenc_context.packed_sei_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
free(packed_sei_buffer);
return;
}
#define partition(ref, field, key, ascending) \
while (i <= j) { \
if (ascending) { \
while (ref[i].field < key) \
i++; \
while (ref[j].field > key) \
j--; \
} else { \
while (ref[i].field > key) \
i++; \
while (ref[j].field < key) \
j--; \
} \
if (i <= j) { \
tmp = ref[i]; \
ref[i] = ref[j]; \
ref[j] = tmp; \
i++; \
j--; \
} \
} \
static void sort_one(VAPictureH264 ref[], int left, int right,
int ascending, int frame_idx)
{
int i = left, j = right;
unsigned int key;
VAPictureH264 tmp;
if (frame_idx) {
key = ref[(left + right) / 2].frame_idx;
partition(ref, frame_idx, key, ascending);
} else {
key = ref[(left + right) / 2].TopFieldOrderCnt;
partition(ref, TopFieldOrderCnt, (signed int)key, ascending);
}
/* recursion */
if (left < j)
sort_one(ref, left, j, ascending, frame_idx);
if (i < right)
sort_one(ref, i, right, ascending, frame_idx);
}
static void sort_two(VAPictureH264 ref[], int left, int right, unsigned int key, unsigned int frame_idx,
int partition_ascending, int list0_ascending, int list1_ascending)
{
int i = left, j = right;
VAPictureH264 tmp;
if (frame_idx) {
partition(ref, frame_idx, key, partition_ascending);
} else {
partition(ref, TopFieldOrderCnt, (signed int)key, partition_ascending);
}
sort_one(ref, left, i - 1, list0_ascending, frame_idx);
sort_one(ref, j + 1, right, list1_ascending, frame_idx);
}
static int update_RefPicList()
{
if (current_frame_type == SLICE_TYPE_P) {
memcpy(RefPicList0, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
sort_one(RefPicList0, 0, numShortTerm - 1, 0, 1);
}
if (current_frame_type == SLICE_TYPE_B) {
memcpy(RefPicList0, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
sort_two(RefPicList0, 0, numShortTerm - 1, current_poc, 0,
1, 0, 1);
memcpy(RefPicList1, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
sort_two(RefPicList1, 0, numShortTerm - 1, current_poc, 0,
0, 1, 0);
}
return 0;
}
static void avcenc_update_picture_parameter(int slice_type, int is_idr)
{
VAEncPictureParameterBufferH264 *pic_param;
VAStatus va_status;
int recon_index;
recon_index = get_free_slot();
// Picture level
pic_param = &avcenc_context.pic_param;
pic_param->CurrPic.picture_id = ref_surface[recon_index];
pic_param->CurrPic.frame_idx = current_frame_num;
pic_param->CurrPic.flags = 0;
pic_param->CurrPic.TopFieldOrderCnt = current_poc;
pic_param->CurrPic.BottomFieldOrderCnt = pic_param->CurrPic.TopFieldOrderCnt;
assert(avcenc_context.codedbuf_buf_id != VA_INVALID_ID);
pic_param->coded_buf = avcenc_context.codedbuf_buf_id;
pic_param->frame_num = current_frame_num;
pic_param->pic_fields.bits.idr_pic_flag = !!is_idr;
pic_param->pic_fields.bits.reference_pic_flag = (slice_type != SLICE_TYPE_B);
CurrentCurrPic = pic_param->CurrPic;
if (slice_type == SLICE_TYPE_P || slice_type == SLICE_TYPE_B)
memset(pic_param->ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
if ((slice_type == SLICE_TYPE_P) || (slice_type == SLICE_TYPE_B)) {
pic_param->ReferenceFrames[0] = RefPicList0[0];
}
if (slice_type == SLICE_TYPE_B) {
pic_param->ReferenceFrames[1] = RefPicList1[0];
}
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPictureParameterBufferType,
sizeof(*pic_param), 1, pic_param,
&avcenc_context.pic_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
}
#ifndef VA_FOURCC_I420
#define VA_FOURCC_I420 0x30323449
#endif
static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
{
VAImage surface_image;
VAStatus va_status;
void *surface_p = NULL;
unsigned char *y_src, *u_src, *v_src;
unsigned char *y_dst, *u_dst, *v_dst;
int y_size = picture_width * picture_height;
int u_size = (picture_width >> 1) * (picture_height >> 1);
int row, col;
size_t n_items;
do {
n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
} while (n_items != 1);
va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
CHECK_VASTATUS(va_status, "vaDeriveImage");
vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
assert(VA_STATUS_SUCCESS == va_status);
y_src = newImageBuffer;
u_src = newImageBuffer + y_size; /* UV offset for NV12 */
v_src = newImageBuffer + y_size + u_size;
y_dst = surface_p + surface_image.offsets[0];
u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
v_dst = surface_p + surface_image.offsets[2];
/* Y plane */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width);
y_dst += surface_image.pitches[0];
y_src += picture_width;
}
if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
for (row = 0; row < surface_image.height / 2; row++) {
for (col = 0; col < surface_image.width / 2; col++) {
u_dst[col * 2] = u_src[col];
u_dst[col * 2 + 1] = v_src[col];
}
u_dst += surface_image.pitches[1];
u_src += (picture_width / 2);
v_src += (picture_width / 2);
}
} else if (surface_image.format.fourcc == VA_FOURCC_YV12 ||
surface_image.format.fourcc == VA_FOURCC_I420) {
const int U = surface_image.format.fourcc == VA_FOURCC_I420 ? 1 : 2;
const int V = surface_image.format.fourcc == VA_FOURCC_I420 ? 2 : 1;
u_dst = surface_p + surface_image.offsets[U];
v_dst = surface_p + surface_image.offsets[V];
for (row = 0; row < surface_image.height / 2; row++) {
memcpy(u_dst, u_src, surface_image.width / 2);
memcpy(v_dst, v_src, surface_image.width / 2);
u_dst += surface_image.pitches[U];
v_dst += surface_image.pitches[V];
u_src += (picture_width / 2);
v_src += (picture_width / 2);
}
}
vaUnmapBuffer(va_dpy, surface_image.buf);
vaDestroyImage(va_dpy, surface_image.image_id);
}
static void avcenc_update_slice_parameter(int slice_type)
{
VAEncSliceParameterBufferH264 *slice_param;
VAStatus va_status;
int i;
// Slice level
i = 0;
slice_param = &avcenc_context.slice_param[i];
slice_param->macroblock_address = 0;
slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs;
slice_param->pic_parameter_set_id = 0;
slice_param->slice_type = slice_type;
slice_param->direct_spatial_mv_pred_flag = 1;
slice_param->num_ref_idx_l0_active_minus1 = 0; /* FIXME: ??? */
slice_param->num_ref_idx_l1_active_minus1 = 0;
slice_param->cabac_init_idc = 0;
slice_param->slice_qp_delta = 0;
slice_param->disable_deblocking_filter_idc = 0;
slice_param->slice_alpha_c0_offset_div2 = 2;
slice_param->slice_beta_offset_div2 = 2;
slice_param->idr_pic_id = 0;
/* FIXME: fill other fields */
if ((slice_type == SLICE_TYPE_P) || (slice_type == SLICE_TYPE_B)) {
memset(slice_param->RefPicList0, 0xFF, 32 * sizeof(VAPictureH264));
slice_param->RefPicList0[0] = RefPicList0[0];
}
if (slice_type == SLICE_TYPE_B) {
memset(slice_param->RefPicList1, 0xFF, 32 * sizeof(VAPictureH264));
slice_param->RefPicList1[0] = RefPicList1[0];
}
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncSliceParameterBufferType,
sizeof(*slice_param), 1, slice_param,
&avcenc_context.slice_param_buf_id[i]);
CHECK_VASTATUS(va_status, "vaCreateBuffer");;
i++;
#if 0
slice_param = &avcenc_context.slice_param[i];
slice_param->macroblock_address = picture_height_in_mbs * picture_width_in_mbs / 2;
slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs / 2;
slice_param->pic_parameter_set_id = 0;
slice_param->slice_type = slice_type;
slice_param->direct_spatial_mv_pred_flag = 0;
slice_param->num_ref_idx_l0_active_minus1 = 0; /* FIXME: ??? */
slice_param->num_ref_idx_l1_active_minus1 = 0;
slice_param->cabac_init_idc = 0;
slice_param->slice_qp_delta = 0;
slice_param->disable_deblocking_filter_idc = 0;
slice_param->slice_alpha_c0_offset_div2 = 2;
slice_param->slice_beta_offset_div2 = 2;
slice_param->idr_pic_id = 0;
/* FIXME: fill other fields */
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncSliceParameterBufferType,
sizeof(*slice_param), 1, slice_param,
&avcenc_context.slice_param_buf_id[i]);
CHECK_VASTATUS(va_status, "vaCreateBuffer");;
i++;
#endif
avcenc_context.num_slices = i;
}
static int update_ReferenceFrames(void)
{
int i;
/* B-frame is not used for reference */
if (current_frame_type == SLICE_TYPE_B)
return 0;
CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
numShortTerm++;
if (numShortTerm > num_ref_frames)
numShortTerm = num_ref_frames;
for (i = numShortTerm - 1; i > 0; i--)
ReferenceFrames[i] = ReferenceFrames[i - 1];
ReferenceFrames[0] = CurrentCurrPic;
if (current_frame_type != SLICE_TYPE_B)
current_frame_num++;
if (current_frame_num > MaxFrameNum)
current_frame_num = 0;
/* Update the use_slot. Only when the surface is used in reference
* frame list, the use_slot[index] is set
*/
for (i = 0; i < SURFACE_NUM; i++) {
int j;
bool found;
found = false;
for (j = 0; j < numShortTerm; j++) {
if (ref_surface[i] == ReferenceFrames[j].picture_id) {
found = true;
break;
}
}
if (found)
use_slot[i] = 1;
else
use_slot[i] = 0;
}
return 0;
}
static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice_type, int is_idr)
{
VAStatus va_status;
if (avcenc_context.upload_thread_value != 0) {
fprintf(stderr, "FATAL error!!!\n");
exit(1);
}
pthread_join(avcenc_context.upload_thread_id, NULL);
avcenc_context.upload_thread_value = -1;
if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
avcenc_context.current_input_surface = SID_INPUT_PICTURE_1;
else
avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
if (aud_nal_enable) {
VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
unsigned int length_in_bits;
unsigned char *packed_buffer = NULL;
length_in_bits = build_nal_delimiter(&packed_buffer);
packed_header_param_buffer.type = VAEncPackedHeaderRawData;
packed_header_param_buffer.bit_length = length_in_bits;
packed_header_param_buffer.has_emulation_bytes = 1;
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderParameterBufferType,
sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
&avcenc_context.packed_aud_header_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderDataBufferType,
(length_in_bits + 7) / 8, 1, packed_buffer,
&avcenc_context.packed_aud_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
free(packed_buffer);
}
if (is_idr) {
VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
unsigned int length_in_bits;
unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
assert(slice_type == SLICE_TYPE_I);
length_in_bits = build_packed_seq_buffer(&packed_seq_buffer);
packed_header_param_buffer.type = VAEncPackedHeaderSequence;
packed_header_param_buffer.bit_length = length_in_bits;
packed_header_param_buffer.has_emulation_bytes = 0;
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderParameterBufferType,
sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
&avcenc_context.packed_seq_header_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderDataBufferType,
(length_in_bits + 7) / 8, 1, packed_seq_buffer,
&avcenc_context.packed_seq_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
length_in_bits = build_packed_pic_buffer(&packed_pic_buffer);
packed_header_param_buffer.type = VAEncPackedHeaderPicture;
packed_header_param_buffer.bit_length = length_in_bits;
packed_header_param_buffer.has_emulation_bytes = 0;
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderParameterBufferType,
sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
&avcenc_context.packed_pic_header_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncPackedHeaderDataBufferType,
(length_in_bits + 7) / 8, 1, packed_pic_buffer,
&avcenc_context.packed_pic_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
free(packed_seq_buffer);
free(packed_pic_buffer);
}
/* sequence parameter set */
VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
va_status = vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncSequenceParameterBufferType,
sizeof(*seq_param), 1, seq_param,
&avcenc_context.seq_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
/* hrd parameter */
VAEncMiscParameterBuffer *misc_param;
VAEncMiscParameterHRD *misc_hrd_param;
vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncMiscParameterBufferType,
sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
1,
NULL,
&avcenc_context.misc_parameter_hrd_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
vaMapBuffer(va_dpy,
avcenc_context.misc_parameter_hrd_buf_id,
(void **)&misc_param);
misc_param->type = VAEncMiscParameterTypeHRD;
misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
if (frame_bit_rate > 0) {
misc_hrd_param->initial_buffer_fullness = frame_bit_rate * 1000 * 4;
misc_hrd_param->buffer_size = frame_bit_rate * 1000 * 8;
} else {
misc_hrd_param->initial_buffer_fullness = 0;
misc_hrd_param->buffer_size = 0;
}
vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_hrd_buf_id);
/* ROI parameter: hard code for test on only one region (0,0,120,120) with qp_delta=4 */
if (roi_test_enable) {
VAEncMiscParameterBufferROI *misc_roi_param;
int roi_num = 1;
vaCreateBuffer(va_dpy,
avcenc_context.context_id,
VAEncMiscParameterBufferType,
sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterBufferROI) + roi_num * sizeof(VAEncROI),
1,
NULL,
&avcenc_context.misc_parameter_roi_buf_id);
vaMapBuffer(va_dpy,
avcenc_context.misc_parameter_roi_buf_id,
(void **)&misc_param);
misc_param->type = VAEncMiscParameterTypeROI;
misc_roi_param = (VAEncMiscParameterBufferROI *)misc_param->data;
{
misc_roi_param->roi_flags.bits.roi_value_is_qp_delta = 1;
/*
* Max/Min delta_qp is only used in CBR mode. It is ingored under CQP mode.
* max_delta_qp means the allowed upper bound of qp delta. (qp + X)
* min_delta_qp means the allowed lower bound of qp delta. (qp -X)
* So it will be better that it is positive. Otherwise the driver will
* use the default bound setting.
*/
misc_roi_param->max_delta_qp = 3;
misc_roi_param->min_delta_qp = 3;
/* one example of ROI region conf.
* please change it on the fly.
*/
VAEncROI *region_roi = (VAEncROI *)((char *)misc_param + sizeof(VAEncMiscParameterBuffer) +
sizeof(VAEncMiscParameterBufferROI));
/*
* Under CQP mode roi_value specifies the qp_delta that is added to frame qp
* Under CBR mode roi_value specifies the important level (positive means that
* it is important. negative means that it is less important).
*/
region_roi->roi_value = 4;
region_roi->roi_rectangle.x = 0;
region_roi->roi_rectangle.y = 0;
region_roi->roi_rectangle.width = (120 < picture_width / 4) ? 120 : picture_width / 4;
region_roi->roi_rectangle.height = (120 < picture_height / 4) ? 120 : picture_height / 4;
misc_roi_param->roi = region_roi;
misc_roi_param->num_roi = 1;
}
vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_roi_buf_id);
}
return 0;
}
int avcenc_render_picture()
{
VAStatus va_status;
VABufferID va_buffers[20];
unsigned int num_va_buffers = 0;
int i;
if (avcenc_context.packed_aud_header_param_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_aud_header_param_buf_id;
if (avcenc_context.packed_aud_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_aud_buf_id;
va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
if (avcenc_context.packed_seq_header_param_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_seq_header_param_buf_id;
if (avcenc_context.packed_seq_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_seq_buf_id;
if (avcenc_context.packed_pic_header_param_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_pic_header_param_buf_id;
if (avcenc_context.packed_pic_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_pic_buf_id;
if (avcenc_context.packed_sei_header_param_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_sei_header_param_buf_id;
if (avcenc_context.packed_sei_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.packed_sei_buf_id;
if (avcenc_context.misc_parameter_hrd_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_hrd_buf_id;
if (avcenc_context.misc_parameter_roi_buf_id != VA_INVALID_ID)
va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_roi_buf_id;
va_status = vaBeginPicture(va_dpy,
avcenc_context.context_id,
surface_ids[avcenc_context.current_input_surface]);
CHECK_VASTATUS(va_status, "vaBeginPicture");
va_status = vaRenderPicture(va_dpy,
avcenc_context.context_id,
va_buffers,
num_va_buffers);
CHECK_VASTATUS(va_status, "vaRenderPicture");
for (i = 0; i < avcenc_context.num_slices; i++) {
va_status = vaRenderPicture(va_dpy,
avcenc_context.context_id,
&avcenc_context.slice_param_buf_id[i],
1);
CHECK_VASTATUS(va_status, "vaRenderPicture");
}
va_status = vaEndPicture(va_dpy, avcenc_context.context_id);
CHECK_VASTATUS(va_status, "vaEndPicture");
return 0;
}
static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
{
VAStatus va_status;
unsigned int i;