summaryrefslogtreecommitdiff
path: root/src/vaapi-recorder.c
blob: 921494df22145b01933471387589a06ea2414dd3 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* 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.
 */

#include "config.h"

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <pthread.h>

#include <va/va.h>
#include <va/va_drm.h>
#include <va/va_drmcommon.h>
#include <va/va_enc_h264.h>
#include <va/va_vpp.h>

#include "compositor.h"
#include "vaapi-recorder.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 SLICE_TYPE_P            0
#define SLICE_TYPE_B            1
#define SLICE_TYPE_I            2

#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

struct vaapi_recorder {
	int drm_fd, output_fd;
	int width, height;
	int frame_count;

	int error;
	int destroying;
	pthread_t worker_thread;
	pthread_mutex_t mutex;
	pthread_cond_t input_cond;

	struct {
		int valid;
		int prime_fd, stride;
	} input;

	VADisplay va_dpy;

	/* video post processing is used for colorspace conversion */
	struct {
		VAConfigID cfg;
		VAContextID ctx;
		VABufferID pipeline_buf;
		VASurfaceID output;
	} vpp;

	struct {
		VAConfigID cfg;
		VAContextID ctx;
		VASurfaceID reference_picture[3];

		int intra_period;
		int output_size;
		int constraint_set_flag;

		struct {
			VAEncSequenceParameterBufferH264 seq;
			VAEncPictureParameterBufferH264 pic;
			VAEncSliceParameterBufferH264 slice;
		} param;
	} encoder;
};

static void *
worker_thread_function(void *);

/* bistream code used for writing the packed headers */

#define BITSTREAM_ALLOCATE_STEPPING	 4096

struct bitstream {
	unsigned int *buffer;
	int bit_offset;
	int max_size_in_dword;
};

static unsigned int
va_swap32(unsigned int val)
{
	unsigned char *pval = (unsigned char *)&val;

	return ((pval[0] << 24) |
		(pval[1] << 16) |
		(pval[2] << 8)  |
		(pval[3] << 0));
}

static void
bitstream_start(struct bitstream *bs)
{
	bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
	bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
	bs->bit_offset = 0;
}

static void
bitstream_end(struct bitstream *bs)
{
	int pos = (bs->bit_offset >> 5);
	int bit_offset = (bs->bit_offset & 0x1f);
	int bit_left = 32 - bit_offset;

	if (bit_offset) {
		bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
	}
}

static void
bitstream_put_ui(struct bitstream *bs, unsigned int val, int size_in_bits)
{
	int pos = (bs->bit_offset >> 5);
	int bit_offset = (bs->bit_offset & 0x1f);
	int bit_left = 32 - bit_offset;

	if (!size_in_bits)
		return;

	bs->bit_offset += size_in_bits;

	if (bit_left > size_in_bits) {
		bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
		return;
	}

	size_in_bits -= bit_left;
	bs->buffer[pos] =
		(bs->buffer[pos] << bit_left) | (val >> size_in_bits);
	bs->buffer[pos] = va_swap32(bs->buffer[pos]);

	if (pos + 1 == bs->max_size_in_dword) {
		bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
		bs->buffer =
			realloc(bs->buffer,
				bs->max_size_in_dword * sizeof(unsigned int));
	}

	bs->buffer[pos + 1] = val;
}

static void
bitstream_put_ue(struct bitstream *bs, unsigned int val)
{
	int size_in_bits = 0;
	int tmp_val = ++val;

	while (tmp_val) {
		tmp_val >>= 1;
		size_in_bits++;
	}

	bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
	bitstream_put_ui(bs, val, size_in_bits);
}

static void
bitstream_put_se(struct bitstream *bs, int val)
{
	unsigned int new_val;

	if (val <= 0)
		new_val = -2 * val;
	else
		new_val = 2 * val - 1;

	bitstream_put_ue(bs, new_val);
}

static void
bitstream_byte_aligning(struct bitstream *bs, int bit)
{
	int bit_offset = (bs->bit_offset & 0x7);
	int bit_left = 8 - bit_offset;
	int new_val;

	if (!bit_offset)
		return;

	if (bit)
		new_val = (1 << bit_left) - 1;
	else
		new_val = 0;

	bitstream_put_ui(bs, new_val, bit_left);
}

static VAStatus
encoder_create_config(struct vaapi_recorder *r)
{
	VAConfigAttrib attrib[2];
	VAStatus status;

	/* FIXME: should check if VAEntrypointEncSlice is supported */

	/* FIXME: should check if specified attributes are supported */

	attrib[0].type = VAConfigAttribRTFormat;
	attrib[0].value = VA_RT_FORMAT_YUV420;

	attrib[1].type = VAConfigAttribRateControl;
	attrib[1].value = VA_RC_CQP;

	status = vaCreateConfig(r->va_dpy, VAProfileH264Main,
				VAEntrypointEncSlice, attrib, 2,
				&r->encoder.cfg);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = vaCreateContext(r->va_dpy, r->encoder.cfg,
				 r->width, r->height, VA_PROGRESSIVE, 0, 0,
				 &r->encoder.ctx);
	if (status != VA_STATUS_SUCCESS) {
		vaDestroyConfig(r->va_dpy, r->encoder.cfg);
		return status;
	}

	return VA_STATUS_SUCCESS;
}

static void
encoder_destroy_config(struct vaapi_recorder *r)
{
	vaDestroyContext(r->va_dpy, r->encoder.ctx);
	vaDestroyConfig(r->va_dpy, r->encoder.cfg);
}

static void
encoder_init_seq_parameters(struct vaapi_recorder *r)
{
	int width_in_mbs, height_in_mbs;
	int frame_cropping_flag = 0;
	int frame_crop_bottom_offset = 0;

	width_in_mbs = (r->width + 15) / 16;
	height_in_mbs = (r->height + 15) / 16;

	r->encoder.param.seq.level_idc = 41;
	r->encoder.param.seq.intra_period = r->encoder.intra_period;
	r->encoder.param.seq.max_num_ref_frames = 4;
	r->encoder.param.seq.picture_width_in_mbs = width_in_mbs;
	r->encoder.param.seq.picture_height_in_mbs = height_in_mbs;
	r->encoder.param.seq.seq_fields.bits.frame_mbs_only_flag = 1;

	/* Tc = num_units_in_tick / time_scale */
	r->encoder.param.seq.time_scale = 1800;
	r->encoder.param.seq.num_units_in_tick = 15;

	if (height_in_mbs * 16 - r->height > 0) {
		frame_cropping_flag = 1;
		frame_crop_bottom_offset = (height_in_mbs * 16 - r->height) / 2;
	}

	r->encoder.param.seq.frame_cropping_flag = frame_cropping_flag;
	r->encoder.param.seq.frame_crop_bottom_offset = frame_crop_bottom_offset;

	r->encoder.param.seq.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = 2;
}

static VABufferID
encoder_update_seq_parameters(struct vaapi_recorder *r)
{
	VABufferID seq_buf;
	VAStatus status;

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncSequenceParameterBufferType,
				sizeof(r->encoder.param.seq),
				1, &r->encoder.param.seq,
				&seq_buf);

	if (status == VA_STATUS_SUCCESS)
		return seq_buf;
	else
		return VA_INVALID_ID;
}

static void
encoder_init_pic_parameters(struct vaapi_recorder *r)
{
	VAEncPictureParameterBufferH264 *pic = &r->encoder.param.pic;

	pic->pic_init_qp = 0;

	/* ENTROPY_MODE_CABAC */
	pic->pic_fields.bits.entropy_coding_mode_flag = 1;

	pic->pic_fields.bits.deblocking_filter_control_present_flag = 1;
}

static VABufferID
encoder_update_pic_parameters(struct vaapi_recorder *r,
			      VABufferID output_buf)
{
	VAEncPictureParameterBufferH264 *pic = &r->encoder.param.pic;
	VAStatus status;
	VABufferID pic_param_buf;
	VASurfaceID curr_pic, pic0;

	curr_pic = r->encoder.reference_picture[r->frame_count % 2];
	pic0 = r->encoder.reference_picture[(r->frame_count + 1) % 2];

	pic->CurrPic.picture_id = curr_pic;
	pic->CurrPic.TopFieldOrderCnt = r->frame_count * 2;
	pic->ReferenceFrames[0].picture_id = pic0;
	pic->ReferenceFrames[1].picture_id = r->encoder.reference_picture[2];
	pic->ReferenceFrames[2].picture_id = VA_INVALID_ID;

	pic->coded_buf = output_buf;
	pic->frame_num = r->frame_count;

	pic->pic_fields.bits.idr_pic_flag = (r->frame_count == 0);
	pic->pic_fields.bits.reference_pic_flag = 1;

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncPictureParameterBufferType,
				sizeof(VAEncPictureParameterBufferH264), 1,
				pic, &pic_param_buf);

	if (status == VA_STATUS_SUCCESS)
		return pic_param_buf;
	else
		return VA_INVALID_ID;
}

static VABufferID
encoder_update_slice_parameter(struct vaapi_recorder *r, int slice_type)
{
	VABufferID slice_param_buf;
	VAStatus status;

	int width_in_mbs = (r->width + 15) / 16;
	int height_in_mbs = (r->height + 15) / 16;

	memset(&r->encoder.param.slice, 0, sizeof r->encoder.param.slice);

	r->encoder.param.slice.num_macroblocks = width_in_mbs * height_in_mbs;
	r->encoder.param.slice.slice_type = slice_type;

	r->encoder.param.slice.slice_alpha_c0_offset_div2 = 2;
	r->encoder.param.slice.slice_beta_offset_div2 = 2;

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncSliceParameterBufferType,
				sizeof(r->encoder.param.slice), 1,
				&r->encoder.param.slice,
				&slice_param_buf);

	if (status == VA_STATUS_SUCCESS)
		return slice_param_buf;
	else
		return VA_INVALID_ID;
}

static VABufferID
encoder_update_misc_hdr_parameter(struct vaapi_recorder *r)
{
	VAEncMiscParameterBuffer *misc_param;
	VAEncMiscParameterHRD *hrd;
	VABufferID buffer;
	VAStatus status;

	int total_size =
		sizeof(VAEncMiscParameterBuffer) +
		sizeof(VAEncMiscParameterRateControl);

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncMiscParameterBufferType, total_size,
				1, NULL, &buffer);
	if (status != VA_STATUS_SUCCESS)
		return VA_INVALID_ID;

	status = vaMapBuffer(r->va_dpy, buffer, (void **) &misc_param);
	if (status != VA_STATUS_SUCCESS) {
		vaDestroyBuffer(r->va_dpy, buffer);
		return VA_INVALID_ID;
	}

	misc_param->type = VAEncMiscParameterTypeHRD;
	hrd = (VAEncMiscParameterHRD *) misc_param->data;

	hrd->initial_buffer_fullness = 0;
	hrd->buffer_size = 0;

	vaUnmapBuffer(r->va_dpy, buffer);

	return buffer;
}

static int
setup_encoder(struct vaapi_recorder *r)
{
	VAStatus status;

	status = encoder_create_config(r);
	if (status != VA_STATUS_SUCCESS) {
		return -1;
	}

	status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_YUV420,
				  r->width, r->height,
				  r->encoder.reference_picture, 3,
				  NULL, 0);
	if (status != VA_STATUS_SUCCESS) {
		encoder_destroy_config(r);
		return -1;
	}

	/* VAProfileH264Main */
	r->encoder.constraint_set_flag |= (1 << 1); /* Annex A.2.2 */

	r->encoder.output_size = r->width * r->height;

	r->encoder.intra_period = 30;

	encoder_init_seq_parameters(r);
	encoder_init_pic_parameters(r);

	return 0;
}

static void
encoder_destroy(struct vaapi_recorder *r)
{
	vaDestroySurfaces(r->va_dpy, r->encoder.reference_picture, 3);

	encoder_destroy_config(r);
}

static void
nal_start_code_prefix(struct bitstream *bs)
{
	bitstream_put_ui(bs, 0x00000001, 32);
}

static void
nal_header(struct bitstream *bs, int nal_ref_idc, int nal_unit_type)
{
	/* forbidden_zero_bit: 0 */
	bitstream_put_ui(bs, 0, 1);

	bitstream_put_ui(bs, nal_ref_idc, 2);
	bitstream_put_ui(bs, nal_unit_type, 5);
}

static void
rbsp_trailing_bits(struct bitstream *bs)
{
	bitstream_put_ui(bs, 1, 1);
	bitstream_byte_aligning(bs, 0);
}

static void sps_rbsp(struct bitstream *bs,
		     VAEncSequenceParameterBufferH264 *seq,
		     int constraint_set_flag)
{
	int i;

	bitstream_put_ui(bs, PROFILE_IDC_MAIN, 8);

	/* constraint_set[0-3] flag */
	for (i = 0; i < 4; i++) {
		int set = (constraint_set_flag & (1 << i)) ? 1 : 0;
		bitstream_put_ui(bs, set, 1);
	}

	/* reserved_zero_4bits */
	bitstream_put_ui(bs, 0, 4);
	bitstream_put_ui(bs, seq->level_idc, 8);
	bitstream_put_ue(bs, seq->seq_parameter_set_id);

	bitstream_put_ue(bs, seq->seq_fields.bits.log2_max_frame_num_minus4);
	bitstream_put_ue(bs, seq->seq_fields.bits.pic_order_cnt_type);
	bitstream_put_ue(bs,
			 seq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4);

	bitstream_put_ue(bs, seq->max_num_ref_frames);

	/* gaps_in_frame_num_value_allowed_flag */
	bitstream_put_ui(bs, 0, 1);

	/* pic_width_in_mbs_minus1, pic_height_in_map_units_minus1 */
	bitstream_put_ue(bs, seq->picture_width_in_mbs - 1);
	bitstream_put_ue(bs, seq->picture_height_in_mbs - 1);

	bitstream_put_ui(bs, seq->seq_fields.bits.frame_mbs_only_flag, 1);
	bitstream_put_ui(bs, seq->seq_fields.bits.direct_8x8_inference_flag, 1);

	bitstream_put_ui(bs, seq->frame_cropping_flag, 1);

	if (seq->frame_cropping_flag) {
		bitstream_put_ue(bs, seq->frame_crop_left_offset);
		bitstream_put_ue(bs, seq->frame_crop_right_offset);
		bitstream_put_ue(bs, seq->frame_crop_top_offset);
		bitstream_put_ue(bs, seq->frame_crop_bottom_offset);
	}

	/* vui_parameters_present_flag */
	bitstream_put_ui(bs, 1, 1);

	/* aspect_ratio_info_present_flag */
	bitstream_put_ui(bs, 0, 1);
	/* overscan_info_present_flag */
	bitstream_put_ui(bs, 0, 1);

	/* video_signal_type_present_flag */
	bitstream_put_ui(bs, 0, 1);
	/* chroma_loc_info_present_flag */
	bitstream_put_ui(bs, 0, 1);

	/* timing_info_present_flag */
	bitstream_put_ui(bs, 1, 1);
	bitstream_put_ui(bs, seq->num_units_in_tick, 32);
	bitstream_put_ui(bs, seq->time_scale, 32);
	/* fixed_frame_rate_flag */
	bitstream_put_ui(bs, 1, 1);

	/* nal_hrd_parameters_present_flag */
	bitstream_put_ui(bs, 0, 1);

	/* vcl_hrd_parameters_present_flag */
	bitstream_put_ui(bs, 0, 1);

	/* low_delay_hrd_flag */
	bitstream_put_ui(bs, 0, 1);

	/* pic_struct_present_flag */
	bitstream_put_ui(bs, 0, 1);
	/* bitstream_restriction_flag */
	bitstream_put_ui(bs, 0, 1);

	rbsp_trailing_bits(bs);
}

static void pps_rbsp(struct bitstream *bs,
		     VAEncPictureParameterBufferH264 *pic)
{
	/* pic_parameter_set_id, seq_parameter_set_id */
	bitstream_put_ue(bs, pic->pic_parameter_set_id);
	bitstream_put_ue(bs, pic->seq_parameter_set_id);

	bitstream_put_ui(bs, pic->pic_fields.bits.entropy_coding_mode_flag, 1);

	/* pic_order_present_flag: 0 */
	bitstream_put_ui(bs, 0, 1);

	/* num_slice_groups_minus1 */
	bitstream_put_ue(bs, 0);

	bitstream_put_ue(bs, pic->num_ref_idx_l0_active_minus1);
	bitstream_put_ue(bs, pic->num_ref_idx_l1_active_minus1);

	bitstream_put_ui(bs, pic->pic_fields.bits.weighted_pred_flag, 1);
	bitstream_put_ui(bs, pic->pic_fields.bits.weighted_bipred_idc, 2);

	/* pic_init_qp_minus26, pic_init_qs_minus26, chroma_qp_index_offset */
	bitstream_put_se(bs, pic->pic_init_qp - 26);
	bitstream_put_se(bs, 0);
	bitstream_put_se(bs, 0);

	bitstream_put_ui(bs, pic->pic_fields.bits.deblocking_filter_control_present_flag, 1);

	/* constrained_intra_pred_flag, redundant_pic_cnt_present_flag */
	bitstream_put_ui(bs, 0, 1);
	bitstream_put_ui(bs, 0, 1);

	bitstream_put_ui(bs, pic->pic_fields.bits.transform_8x8_mode_flag, 1);

	/* pic_scaling_matrix_present_flag */
	bitstream_put_ui(bs, 0, 1);
	bitstream_put_se(bs, pic->second_chroma_qp_index_offset );

	rbsp_trailing_bits(bs);
}

static int
build_packed_pic_buffer(struct vaapi_recorder *r,
			void **header_buffer)
{
	struct bitstream bs;

	bitstream_start(&bs);
	nal_start_code_prefix(&bs);
	nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
	pps_rbsp(&bs, &r->encoder.param.pic);
	bitstream_end(&bs);

	*header_buffer = bs.buffer;
	return bs.bit_offset;
}

static int
build_packed_seq_buffer(struct vaapi_recorder *r,
			void **header_buffer)
{
	struct bitstream bs;

	bitstream_start(&bs);
	nal_start_code_prefix(&bs);
	nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
	sps_rbsp(&bs, &r->encoder.param.seq, r->encoder.constraint_set_flag);
	bitstream_end(&bs);

	*header_buffer = bs.buffer;
	return bs.bit_offset;
}

static int
create_packed_header_buffers(struct vaapi_recorder *r, VABufferID *buffers,
			     VAEncPackedHeaderType type,
			     void *data, int bit_length)
{
	VAEncPackedHeaderParameterBuffer packed_header;
	VAStatus status;

	packed_header.type = type;
	packed_header.bit_length = bit_length;
	packed_header.has_emulation_bytes = 0;

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncPackedHeaderParameterBufferType,
				sizeof packed_header, 1, &packed_header,
				&buffers[0]);
	if (status != VA_STATUS_SUCCESS)
		return 0;

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncPackedHeaderDataBufferType,
				(bit_length + 7) / 8, 1, data, &buffers[1]);
	if (status != VA_STATUS_SUCCESS) {
		vaDestroyBuffer(r->va_dpy, buffers[0]);
		return 0;
	}

	return 2;
}

static int
encoder_prepare_headers(struct vaapi_recorder *r, VABufferID *buffers)
{
	VABufferID *p;

	int bit_length;
	void *data;

	p = buffers;

	bit_length = build_packed_seq_buffer(r, &data);
	p += create_packed_header_buffers(r, p, VAEncPackedHeaderSequence,
					  data, bit_length);
	free(data);

	bit_length = build_packed_pic_buffer(r, &data);
	p += create_packed_header_buffers(r, p, VAEncPackedHeaderPicture,
					  data, bit_length);
	free(data);

	return p - buffers;
}

static VAStatus
encoder_render_picture(struct vaapi_recorder *r, VASurfaceID input,
		       VABufferID *buffers, int count)
{
	VAStatus status;

	status = vaBeginPicture(r->va_dpy, r->encoder.ctx, input);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = vaRenderPicture(r->va_dpy, r->encoder.ctx, buffers, count);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = vaEndPicture(r->va_dpy, r->encoder.ctx);
	if (status != VA_STATUS_SUCCESS)
		return status;

	return vaSyncSurface(r->va_dpy, input);
}

static VABufferID
encoder_create_output_buffer(struct vaapi_recorder *r)
{
	VABufferID output_buf;
	VAStatus status;

	status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
				VAEncCodedBufferType, r->encoder.output_size,
				1, NULL, &output_buf);
	if (status == VA_STATUS_SUCCESS)
		return output_buf;
	else
		return VA_INVALID_ID;
}

enum output_write_status {
	OUTPUT_WRITE_SUCCESS,
	OUTPUT_WRITE_OVERFLOW,
	OUTPUT_WRITE_FATAL
};

static enum output_write_status
encoder_write_output(struct vaapi_recorder *r, VABufferID output_buf)
{
	VACodedBufferSegment *segment;
	VAStatus status;
	int count;

	status = vaMapBuffer(r->va_dpy, output_buf, (void **) &segment);
	if (status != VA_STATUS_SUCCESS)
		return OUTPUT_WRITE_FATAL;

	if (segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
		r->encoder.output_size *= 2;
		vaUnmapBuffer(r->va_dpy, output_buf);
		return OUTPUT_WRITE_OVERFLOW;
	}

	count = write(r->output_fd, segment->buf, segment->size);

	vaUnmapBuffer(r->va_dpy, output_buf);

	if (count < 0)
		return OUTPUT_WRITE_FATAL;

	return OUTPUT_WRITE_SUCCESS;
}

static void
encoder_encode(struct vaapi_recorder *r, VASurfaceID input)
{
	VABufferID output_buf = VA_INVALID_ID;

	VABufferID buffers[8];
	int count = 0;
	int i, slice_type;
	enum output_write_status ret;

	if ((r->frame_count % r->encoder.intra_period) == 0)
		slice_type = SLICE_TYPE_I;
	else
		slice_type = SLICE_TYPE_P;

	buffers[count++] = encoder_update_seq_parameters(r);
	buffers[count++] = encoder_update_misc_hdr_parameter(r);
	buffers[count++] = encoder_update_slice_parameter(r, slice_type);

	for (i = 0; i < count; i++)
		if (buffers[i] == VA_INVALID_ID)
			goto bail;

	if (r->frame_count == 0)
		count += encoder_prepare_headers(r, buffers + count);

	do {
		output_buf = encoder_create_output_buffer(r);
		if (output_buf == VA_INVALID_ID)
			goto bail;

		buffers[count++] =
			encoder_update_pic_parameters(r, output_buf);
		if (buffers[count - 1] == VA_INVALID_ID)
			goto bail;

		encoder_render_picture(r, input, buffers, count);
		ret = encoder_write_output(r, output_buf);

		vaDestroyBuffer(r->va_dpy, output_buf);
		output_buf = VA_INVALID_ID;

		vaDestroyBuffer(r->va_dpy, buffers[--count]);
	} while (ret == OUTPUT_WRITE_OVERFLOW);

	if (ret == OUTPUT_WRITE_FATAL)
		r->error = errno;

	for (i = 0; i < count; i++)
		vaDestroyBuffer(r->va_dpy, buffers[i]);

	r->frame_count++;
	return;

bail:
	for (i = 0; i < count; i++)
		vaDestroyBuffer(r->va_dpy, buffers[i]);
	if (output_buf != VA_INVALID_ID)
		vaDestroyBuffer(r->va_dpy, output_buf);
}


static int
setup_vpp(struct vaapi_recorder *r)
{
	VAStatus status;

	status = vaCreateConfig(r->va_dpy, VAProfileNone,
				VAEntrypointVideoProc, NULL, 0,
				&r->vpp.cfg);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("vaapi: failed to create VPP config\n");
		return -1;
	}

	status = vaCreateContext(r->va_dpy, r->vpp.cfg, r->width, r->height,
				 0, NULL, 0, &r->vpp.ctx);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("vaapi: failed to create VPP context\n");
		goto err_cfg;
	}

	status = vaCreateBuffer(r->va_dpy, r->vpp.ctx,
				VAProcPipelineParameterBufferType,
				sizeof(VAProcPipelineParameterBuffer),
				1, NULL, &r->vpp.pipeline_buf);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("vaapi: failed to create VPP pipeline buffer\n");
		goto err_ctx;
	}

	status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_YUV420,
				  r->width, r->height, &r->vpp.output, 1,
				  NULL, 0);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("vaapi: failed to create YUV surface\n");
		goto err_buf;
	}

	return 0;

err_buf:
	vaDestroyBuffer(r->va_dpy, r->vpp.pipeline_buf);
err_ctx:
	vaDestroyConfig(r->va_dpy, r->vpp.ctx);
err_cfg:
	vaDestroyConfig(r->va_dpy, r->vpp.cfg);

	return -1;
}

static void
vpp_destroy(struct vaapi_recorder *r)
{
	vaDestroySurfaces(r->va_dpy, &r->vpp.output, 1);
	vaDestroyBuffer(r->va_dpy, r->vpp.pipeline_buf);
	vaDestroyConfig(r->va_dpy, r->vpp.ctx);
	vaDestroyConfig(r->va_dpy, r->vpp.cfg);
}

static int
setup_worker_thread(struct vaapi_recorder *r)
{
	pthread_mutex_init(&r->mutex, NULL);
	pthread_cond_init(&r->input_cond, NULL);
	pthread_create(&r->worker_thread, NULL, worker_thread_function, r);

	return 1;
}

static void
destroy_worker_thread(struct vaapi_recorder *r)
{
	pthread_mutex_lock(&r->mutex);

	/* Make sure the worker thread finishes */
	r->destroying = 1;
	pthread_cond_signal(&r->input_cond);

	pthread_mutex_unlock(&r->mutex);

	pthread_join(r->worker_thread, NULL);

	pthread_mutex_destroy(&r->mutex);
	pthread_cond_destroy(&r->input_cond);
}

struct vaapi_recorder *
vaapi_recorder_create(int drm_fd, int width, int height, const char *filename)
{
	struct vaapi_recorder *r;
	VAStatus status;
	int major, minor;
	int flags;

	r = calloc(1, sizeof *r);
	if (!r)
		return NULL;

	r->width = width;
	r->height = height;
	r->drm_fd = drm_fd;

	if (setup_worker_thread(r) < 0)
		goto err_free;

	flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
	r->output_fd = open(filename, flags, 0644);
	if (r->output_fd < 0)
		goto err_thread;

	r->va_dpy = vaGetDisplayDRM(drm_fd);
	if (!r->va_dpy) {
		weston_log("failed to create VA display\n");
		goto err_fd;
	}

	status = vaInitialize(r->va_dpy, &major, &minor);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("vaapi: failed to initialize display\n");
		goto err_fd;
	}

	if (setup_vpp(r) < 0) {
		weston_log("vaapi: failed to initialize VPP pipeline\n");
		goto err_va_dpy;
	}

	if (setup_encoder(r) < 0) {
		goto err_vpp;
	}

	return r;

err_vpp:
	vpp_destroy(r);
err_va_dpy:
	vaTerminate(r->va_dpy);
err_fd:
	close(r->output_fd);
err_thread:
	destroy_worker_thread(r);
err_free:
	free(r);

	return NULL;
}

void
vaapi_recorder_destroy(struct vaapi_recorder *r)
{
	destroy_worker_thread(r);

	encoder_destroy(r);
	vpp_destroy(r);

	vaTerminate(r->va_dpy);

	close(r->output_fd);
	close(r->drm_fd);

	free(r);
}

static VAStatus
create_surface_from_fd(struct vaapi_recorder *r, int prime_fd,
		       int stride, VASurfaceID *surface)
{
	VASurfaceAttrib va_attribs[2];
	VASurfaceAttribExternalBuffers va_attrib_extbuf;
	VAStatus status;

	unsigned long buffer_fd = prime_fd;

	va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX;
	va_attrib_extbuf.width = r->width;
	va_attrib_extbuf.height = r->height;
	va_attrib_extbuf.data_size = r->height * stride;
	va_attrib_extbuf.num_planes = 1;
	va_attrib_extbuf.pitches[0] = stride;
	va_attrib_extbuf.offsets[0] = 0;
	va_attrib_extbuf.buffers = &buffer_fd;
	va_attrib_extbuf.num_buffers = 1;
	va_attrib_extbuf.flags = 0;
	va_attrib_extbuf.private_data = NULL;

	va_attribs[0].type = VASurfaceAttribMemoryType;
	va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
	va_attribs[0].value.type = VAGenericValueTypeInteger;
	va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;

	va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor;
	va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
	va_attribs[1].value.type = VAGenericValueTypePointer;
	va_attribs[1].value.value.p = &va_attrib_extbuf;

	status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_RGB32,
				  r->width, r->height, surface, 1,
				  va_attribs, 2);

	return status;
}

static VAStatus
convert_rgb_to_yuv(struct vaapi_recorder *r, VASurfaceID rgb_surface)
{
	VAProcPipelineParameterBuffer *pipeline_param;
	VAStatus status;

	status = vaMapBuffer(r->va_dpy, r->vpp.pipeline_buf,
			     (void **) &pipeline_param);
	if (status != VA_STATUS_SUCCESS)
		return status;

	memset(pipeline_param, 0, sizeof *pipeline_param);

	pipeline_param->surface = rgb_surface;
	pipeline_param->surface_color_standard  = VAProcColorStandardNone;

	pipeline_param->output_background_color = 0xff000000;
	pipeline_param->output_color_standard   = VAProcColorStandardNone;

	status = vaUnmapBuffer(r->va_dpy, r->vpp.pipeline_buf);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = vaBeginPicture(r->va_dpy, r->vpp.ctx, r->vpp.output);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = vaRenderPicture(r->va_dpy, r->vpp.ctx,
				 &r->vpp.pipeline_buf, 1);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = vaEndPicture(r->va_dpy, r->vpp.ctx);
	if (status != VA_STATUS_SUCCESS)
		return status;

	return status;
}

static void
recorder_frame(struct vaapi_recorder *r)
{
	VASurfaceID rgb_surface;
	VAStatus status;

	status = create_surface_from_fd(r, r->input.prime_fd,
					r->input.stride, &rgb_surface);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("[libva recorder] "
			   "failed to create surface from bo\n");
		return;
	}

	close(r->input.prime_fd);

	status = convert_rgb_to_yuv(r, rgb_surface);
	if (status != VA_STATUS_SUCCESS) {
		weston_log("[libva recorder] "
			   "color space conversion failed\n");
		return;
	}

	encoder_encode(r, r->vpp.output);

	vaDestroySurfaces(r->va_dpy, &rgb_surface, 1);
}

static void *
worker_thread_function(void *data)
{
	struct vaapi_recorder *r = data;

	pthread_mutex_lock(&r->mutex);

	while (!r->destroying) {
		if (!r->input.valid)
			pthread_cond_wait(&r->input_cond, &r->mutex);

		/* If the thread is awaken by destroy_worker_thread(),
		 * there might not be valid input */
		if (!r->input.valid)
			continue;

		recorder_frame(r);
		r->input.valid = 0;
	}

	pthread_mutex_unlock(&r->mutex);

	return NULL;
}

int
vaapi_recorder_frame(struct vaapi_recorder *r, int prime_fd, int stride)
{
	int ret = 0;

	pthread_mutex_lock(&r->mutex);

	if (r->error) {
		errno = r->error;
		ret = -1;
		goto unlock;
	}

	/* The mutex is never released while encoding, so this point should
	 * never be reached if input.valid is true. */
	assert(!r->input.valid);

	r->input.prime_fd = prime_fd;
	r->input.stride = stride;
	r->input.valid = 1;
	pthread_cond_signal(&r->input_cond);

unlock:
	pthread_mutex_unlock(&r->mutex);

	return ret;
}