2 * Copyright © 2013 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 /* Copyright (c) 2012 Intel Corporation. All Rights Reserved.
25 * Permission is hereby granted, free of charge, to any person obtaining a
26 * copy of this software and associated documentation files (the
27 * "Software"), to deal in the Software without restriction, including
28 * without limitation the rights to use, copy, modify, merge, publish,
29 * distribute, sub license, and/or sell copies of the Software, and to
30 * permit persons to whom the Software is furnished to do so, subject to
31 * the following conditions:
33 * The above copyright notice and this permission notice (including the
34 * next paragraph) shall be included in all copies or substantial portions
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
40 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
41 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
42 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
43 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 #include <sys/types.h>
59 #include <va/va_drm.h>
60 #include <va/va_drmcommon.h>
61 #include <va/va_enc_h264.h>
62 #include <va/va_vpp.h>
64 #include "compositor.h"
65 #include "vaapi-recorder.h"
67 #define NAL_REF_IDC_NONE 0
68 #define NAL_REF_IDC_LOW 1
69 #define NAL_REF_IDC_MEDIUM 2
70 #define NAL_REF_IDC_HIGH 3
78 #define SLICE_TYPE_P 0
79 #define SLICE_TYPE_B 1
80 #define SLICE_TYPE_I 2
82 #define ENTROPY_MODE_CAVLC 0
83 #define ENTROPY_MODE_CABAC 1
85 #define PROFILE_IDC_BASELINE 66
86 #define PROFILE_IDC_MAIN 77
87 #define PROFILE_IDC_HIGH 100
89 struct vaapi_recorder {
95 pthread_t worker_thread;
96 pthread_mutex_t mutex;
97 pthread_cond_t input_cond;
101 int prime_fd, stride;
106 /* video post processing is used for colorspace conversion */
110 VABufferID pipeline_buf;
117 VASurfaceID reference_picture[3];
121 int constraint_set_flag;
124 VAEncSequenceParameterBufferH264 seq;
125 VAEncPictureParameterBufferH264 pic;
126 VAEncSliceParameterBufferH264 slice;
132 worker_thread_function(void *);
134 /* bistream code used for writing the packed headers */
136 #define BITSTREAM_ALLOCATE_STEPPING 4096
139 unsigned int *buffer;
141 int max_size_in_dword;
145 va_swap32(unsigned int val)
147 unsigned char *pval = (unsigned char *)&val;
149 return ((pval[0] << 24) |
156 bitstream_start(struct bitstream *bs)
158 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
159 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
164 bitstream_end(struct bitstream *bs)
166 int pos = (bs->bit_offset >> 5);
167 int bit_offset = (bs->bit_offset & 0x1f);
168 int bit_left = 32 - bit_offset;
171 bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
176 bitstream_put_ui(struct bitstream *bs, unsigned int val, int size_in_bits)
178 int pos = (bs->bit_offset >> 5);
179 int bit_offset = (bs->bit_offset & 0x1f);
180 int bit_left = 32 - bit_offset;
185 bs->bit_offset += size_in_bits;
187 if (bit_left > size_in_bits) {
188 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
192 size_in_bits -= bit_left;
194 (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
195 bs->buffer[pos] = va_swap32(bs->buffer[pos]);
197 if (pos + 1 == bs->max_size_in_dword) {
198 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
201 bs->max_size_in_dword * sizeof(unsigned int));
204 bs->buffer[pos + 1] = val;
208 bitstream_put_ue(struct bitstream *bs, unsigned int val)
210 int size_in_bits = 0;
218 bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
219 bitstream_put_ui(bs, val, size_in_bits);
223 bitstream_put_se(struct bitstream *bs, int val)
225 unsigned int new_val;
230 new_val = 2 * val - 1;
232 bitstream_put_ue(bs, new_val);
236 bitstream_byte_aligning(struct bitstream *bs, int bit)
238 int bit_offset = (bs->bit_offset & 0x7);
239 int bit_left = 8 - bit_offset;
246 new_val = (1 << bit_left) - 1;
250 bitstream_put_ui(bs, new_val, bit_left);
254 encoder_create_config(struct vaapi_recorder *r)
256 VAConfigAttrib attrib[2];
259 /* FIXME: should check if VAEntrypointEncSlice is supported */
261 /* FIXME: should check if specified attributes are supported */
263 attrib[0].type = VAConfigAttribRTFormat;
264 attrib[0].value = VA_RT_FORMAT_YUV420;
266 attrib[1].type = VAConfigAttribRateControl;
267 attrib[1].value = VA_RC_CQP;
269 status = vaCreateConfig(r->va_dpy, VAProfileH264Main,
270 VAEntrypointEncSlice, attrib, 2,
272 if (status != VA_STATUS_SUCCESS)
275 status = vaCreateContext(r->va_dpy, r->encoder.cfg,
276 r->width, r->height, VA_PROGRESSIVE, 0, 0,
278 if (status != VA_STATUS_SUCCESS) {
279 vaDestroyConfig(r->va_dpy, r->encoder.cfg);
283 return VA_STATUS_SUCCESS;
287 encoder_destroy_config(struct vaapi_recorder *r)
289 vaDestroyContext(r->va_dpy, r->encoder.ctx);
290 vaDestroyConfig(r->va_dpy, r->encoder.cfg);
294 encoder_init_seq_parameters(struct vaapi_recorder *r)
296 int width_in_mbs, height_in_mbs;
297 int frame_cropping_flag = 0;
298 int frame_crop_bottom_offset = 0;
300 width_in_mbs = (r->width + 15) / 16;
301 height_in_mbs = (r->height + 15) / 16;
303 r->encoder.param.seq.level_idc = 41;
304 r->encoder.param.seq.intra_period = r->encoder.intra_period;
305 r->encoder.param.seq.max_num_ref_frames = 4;
306 r->encoder.param.seq.picture_width_in_mbs = width_in_mbs;
307 r->encoder.param.seq.picture_height_in_mbs = height_in_mbs;
308 r->encoder.param.seq.seq_fields.bits.frame_mbs_only_flag = 1;
310 /* Tc = num_units_in_tick / time_scale */
311 r->encoder.param.seq.time_scale = 1800;
312 r->encoder.param.seq.num_units_in_tick = 15;
314 if (height_in_mbs * 16 - r->height > 0) {
315 frame_cropping_flag = 1;
316 frame_crop_bottom_offset = (height_in_mbs * 16 - r->height) / 2;
319 r->encoder.param.seq.frame_cropping_flag = frame_cropping_flag;
320 r->encoder.param.seq.frame_crop_bottom_offset = frame_crop_bottom_offset;
322 r->encoder.param.seq.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = 2;
326 encoder_update_seq_parameters(struct vaapi_recorder *r)
331 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
332 VAEncSequenceParameterBufferType,
333 sizeof(r->encoder.param.seq),
334 1, &r->encoder.param.seq,
337 if (status == VA_STATUS_SUCCESS)
340 return VA_INVALID_ID;
344 encoder_init_pic_parameters(struct vaapi_recorder *r)
346 VAEncPictureParameterBufferH264 *pic = &r->encoder.param.pic;
348 pic->pic_init_qp = 0;
350 /* ENTROPY_MODE_CABAC */
351 pic->pic_fields.bits.entropy_coding_mode_flag = 1;
353 pic->pic_fields.bits.deblocking_filter_control_present_flag = 1;
357 encoder_update_pic_parameters(struct vaapi_recorder *r,
358 VABufferID output_buf)
360 VAEncPictureParameterBufferH264 *pic = &r->encoder.param.pic;
362 VABufferID pic_param_buf;
363 VASurfaceID curr_pic, pic0;
365 curr_pic = r->encoder.reference_picture[r->frame_count % 2];
366 pic0 = r->encoder.reference_picture[(r->frame_count + 1) % 2];
368 pic->CurrPic.picture_id = curr_pic;
369 pic->CurrPic.TopFieldOrderCnt = r->frame_count * 2;
370 pic->ReferenceFrames[0].picture_id = pic0;
371 pic->ReferenceFrames[1].picture_id = r->encoder.reference_picture[2];
372 pic->ReferenceFrames[2].picture_id = VA_INVALID_ID;
374 pic->coded_buf = output_buf;
375 pic->frame_num = r->frame_count;
377 pic->pic_fields.bits.idr_pic_flag = (r->frame_count == 0);
378 pic->pic_fields.bits.reference_pic_flag = 1;
380 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
381 VAEncPictureParameterBufferType,
382 sizeof(VAEncPictureParameterBufferH264), 1,
383 pic, &pic_param_buf);
385 if (status == VA_STATUS_SUCCESS)
386 return pic_param_buf;
388 return VA_INVALID_ID;
392 encoder_update_slice_parameter(struct vaapi_recorder *r, int slice_type)
394 VABufferID slice_param_buf;
397 int width_in_mbs = (r->width + 15) / 16;
398 int height_in_mbs = (r->height + 15) / 16;
400 memset(&r->encoder.param.slice, 0, sizeof r->encoder.param.slice);
402 r->encoder.param.slice.num_macroblocks = width_in_mbs * height_in_mbs;
403 r->encoder.param.slice.slice_type = slice_type;
405 r->encoder.param.slice.slice_alpha_c0_offset_div2 = 2;
406 r->encoder.param.slice.slice_beta_offset_div2 = 2;
408 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
409 VAEncSliceParameterBufferType,
410 sizeof(r->encoder.param.slice), 1,
411 &r->encoder.param.slice,
414 if (status == VA_STATUS_SUCCESS)
415 return slice_param_buf;
417 return VA_INVALID_ID;
421 encoder_update_misc_hdr_parameter(struct vaapi_recorder *r)
423 VAEncMiscParameterBuffer *misc_param;
424 VAEncMiscParameterHRD *hrd;
429 sizeof(VAEncMiscParameterBuffer) +
430 sizeof(VAEncMiscParameterRateControl);
432 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
433 VAEncMiscParameterBufferType, total_size,
435 if (status != VA_STATUS_SUCCESS)
436 return VA_INVALID_ID;
438 status = vaMapBuffer(r->va_dpy, buffer, (void **) &misc_param);
439 if (status != VA_STATUS_SUCCESS) {
440 vaDestroyBuffer(r->va_dpy, buffer);
441 return VA_INVALID_ID;
444 misc_param->type = VAEncMiscParameterTypeHRD;
445 hrd = (VAEncMiscParameterHRD *) misc_param->data;
447 hrd->initial_buffer_fullness = 0;
448 hrd->buffer_size = 0;
450 vaUnmapBuffer(r->va_dpy, buffer);
456 setup_encoder(struct vaapi_recorder *r)
460 status = encoder_create_config(r);
461 if (status != VA_STATUS_SUCCESS) {
465 status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_YUV420,
467 r->encoder.reference_picture, 3,
469 if (status != VA_STATUS_SUCCESS) {
470 encoder_destroy_config(r);
474 /* VAProfileH264Main */
475 r->encoder.constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
477 r->encoder.output_size = r->width * r->height;
479 r->encoder.intra_period = 30;
481 encoder_init_seq_parameters(r);
482 encoder_init_pic_parameters(r);
488 encoder_destroy(struct vaapi_recorder *r)
490 vaDestroySurfaces(r->va_dpy, r->encoder.reference_picture, 3);
492 encoder_destroy_config(r);
496 nal_start_code_prefix(struct bitstream *bs)
498 bitstream_put_ui(bs, 0x00000001, 32);
502 nal_header(struct bitstream *bs, int nal_ref_idc, int nal_unit_type)
504 /* forbidden_zero_bit: 0 */
505 bitstream_put_ui(bs, 0, 1);
507 bitstream_put_ui(bs, nal_ref_idc, 2);
508 bitstream_put_ui(bs, nal_unit_type, 5);
512 rbsp_trailing_bits(struct bitstream *bs)
514 bitstream_put_ui(bs, 1, 1);
515 bitstream_byte_aligning(bs, 0);
518 static void sps_rbsp(struct bitstream *bs,
519 VAEncSequenceParameterBufferH264 *seq,
520 int constraint_set_flag)
524 bitstream_put_ui(bs, PROFILE_IDC_MAIN, 8);
526 /* constraint_set[0-3] flag */
527 for (i = 0; i < 4; i++) {
528 int set = (constraint_set_flag & (1 << i)) ? 1 : 0;
529 bitstream_put_ui(bs, set, 1);
532 /* reserved_zero_4bits */
533 bitstream_put_ui(bs, 0, 4);
534 bitstream_put_ui(bs, seq->level_idc, 8);
535 bitstream_put_ue(bs, seq->seq_parameter_set_id);
537 bitstream_put_ue(bs, seq->seq_fields.bits.log2_max_frame_num_minus4);
538 bitstream_put_ue(bs, seq->seq_fields.bits.pic_order_cnt_type);
540 seq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4);
542 bitstream_put_ue(bs, seq->max_num_ref_frames);
544 /* gaps_in_frame_num_value_allowed_flag */
545 bitstream_put_ui(bs, 0, 1);
547 /* pic_width_in_mbs_minus1, pic_height_in_map_units_minus1 */
548 bitstream_put_ue(bs, seq->picture_width_in_mbs - 1);
549 bitstream_put_ue(bs, seq->picture_height_in_mbs - 1);
551 bitstream_put_ui(bs, seq->seq_fields.bits.frame_mbs_only_flag, 1);
552 bitstream_put_ui(bs, seq->seq_fields.bits.direct_8x8_inference_flag, 1);
554 bitstream_put_ui(bs, seq->frame_cropping_flag, 1);
556 if (seq->frame_cropping_flag) {
557 bitstream_put_ue(bs, seq->frame_crop_left_offset);
558 bitstream_put_ue(bs, seq->frame_crop_right_offset);
559 bitstream_put_ue(bs, seq->frame_crop_top_offset);
560 bitstream_put_ue(bs, seq->frame_crop_bottom_offset);
563 /* vui_parameters_present_flag */
564 bitstream_put_ui(bs, 1, 1);
566 /* aspect_ratio_info_present_flag */
567 bitstream_put_ui(bs, 0, 1);
568 /* overscan_info_present_flag */
569 bitstream_put_ui(bs, 0, 1);
571 /* video_signal_type_present_flag */
572 bitstream_put_ui(bs, 0, 1);
573 /* chroma_loc_info_present_flag */
574 bitstream_put_ui(bs, 0, 1);
576 /* timing_info_present_flag */
577 bitstream_put_ui(bs, 1, 1);
578 bitstream_put_ui(bs, seq->num_units_in_tick, 32);
579 bitstream_put_ui(bs, seq->time_scale, 32);
580 /* fixed_frame_rate_flag */
581 bitstream_put_ui(bs, 1, 1);
583 /* nal_hrd_parameters_present_flag */
584 bitstream_put_ui(bs, 0, 1);
586 /* vcl_hrd_parameters_present_flag */
587 bitstream_put_ui(bs, 0, 1);
589 /* low_delay_hrd_flag */
590 bitstream_put_ui(bs, 0, 1);
592 /* pic_struct_present_flag */
593 bitstream_put_ui(bs, 0, 1);
594 /* bitstream_restriction_flag */
595 bitstream_put_ui(bs, 0, 1);
597 rbsp_trailing_bits(bs);
600 static void pps_rbsp(struct bitstream *bs,
601 VAEncPictureParameterBufferH264 *pic)
603 /* pic_parameter_set_id, seq_parameter_set_id */
604 bitstream_put_ue(bs, pic->pic_parameter_set_id);
605 bitstream_put_ue(bs, pic->seq_parameter_set_id);
607 bitstream_put_ui(bs, pic->pic_fields.bits.entropy_coding_mode_flag, 1);
609 /* pic_order_present_flag: 0 */
610 bitstream_put_ui(bs, 0, 1);
612 /* num_slice_groups_minus1 */
613 bitstream_put_ue(bs, 0);
615 bitstream_put_ue(bs, pic->num_ref_idx_l0_active_minus1);
616 bitstream_put_ue(bs, pic->num_ref_idx_l1_active_minus1);
618 bitstream_put_ui(bs, pic->pic_fields.bits.weighted_pred_flag, 1);
619 bitstream_put_ui(bs, pic->pic_fields.bits.weighted_bipred_idc, 2);
621 /* pic_init_qp_minus26, pic_init_qs_minus26, chroma_qp_index_offset */
622 bitstream_put_se(bs, pic->pic_init_qp - 26);
623 bitstream_put_se(bs, 0);
624 bitstream_put_se(bs, 0);
626 bitstream_put_ui(bs, pic->pic_fields.bits.deblocking_filter_control_present_flag, 1);
628 /* constrained_intra_pred_flag, redundant_pic_cnt_present_flag */
629 bitstream_put_ui(bs, 0, 1);
630 bitstream_put_ui(bs, 0, 1);
632 bitstream_put_ui(bs, pic->pic_fields.bits.transform_8x8_mode_flag, 1);
634 /* pic_scaling_matrix_present_flag */
635 bitstream_put_ui(bs, 0, 1);
636 bitstream_put_se(bs, pic->second_chroma_qp_index_offset );
638 rbsp_trailing_bits(bs);
642 build_packed_pic_buffer(struct vaapi_recorder *r,
643 void **header_buffer)
647 bitstream_start(&bs);
648 nal_start_code_prefix(&bs);
649 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
650 pps_rbsp(&bs, &r->encoder.param.pic);
653 *header_buffer = bs.buffer;
654 return bs.bit_offset;
658 build_packed_seq_buffer(struct vaapi_recorder *r,
659 void **header_buffer)
663 bitstream_start(&bs);
664 nal_start_code_prefix(&bs);
665 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
666 sps_rbsp(&bs, &r->encoder.param.seq, r->encoder.constraint_set_flag);
669 *header_buffer = bs.buffer;
670 return bs.bit_offset;
674 create_packed_header_buffers(struct vaapi_recorder *r, VABufferID *buffers,
675 VAEncPackedHeaderType type,
676 void *data, int bit_length)
678 VAEncPackedHeaderParameterBuffer packed_header;
681 packed_header.type = type;
682 packed_header.bit_length = bit_length;
683 packed_header.has_emulation_bytes = 0;
685 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
686 VAEncPackedHeaderParameterBufferType,
687 sizeof packed_header, 1, &packed_header,
689 if (status != VA_STATUS_SUCCESS)
692 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
693 VAEncPackedHeaderDataBufferType,
694 (bit_length + 7) / 8, 1, data, &buffers[1]);
695 if (status != VA_STATUS_SUCCESS) {
696 vaDestroyBuffer(r->va_dpy, buffers[0]);
704 encoder_prepare_headers(struct vaapi_recorder *r, VABufferID *buffers)
713 bit_length = build_packed_seq_buffer(r, &data);
714 p += create_packed_header_buffers(r, p, VAEncPackedHeaderSequence,
718 bit_length = build_packed_pic_buffer(r, &data);
719 p += create_packed_header_buffers(r, p, VAEncPackedHeaderPicture,
727 encoder_render_picture(struct vaapi_recorder *r, VASurfaceID input,
728 VABufferID *buffers, int count)
732 status = vaBeginPicture(r->va_dpy, r->encoder.ctx, input);
733 if (status != VA_STATUS_SUCCESS)
736 status = vaRenderPicture(r->va_dpy, r->encoder.ctx, buffers, count);
737 if (status != VA_STATUS_SUCCESS)
740 status = vaEndPicture(r->va_dpy, r->encoder.ctx);
741 if (status != VA_STATUS_SUCCESS)
744 return vaSyncSurface(r->va_dpy, input);
748 encoder_create_output_buffer(struct vaapi_recorder *r)
750 VABufferID output_buf;
753 status = vaCreateBuffer(r->va_dpy, r->encoder.ctx,
754 VAEncCodedBufferType, r->encoder.output_size,
755 1, NULL, &output_buf);
756 if (status == VA_STATUS_SUCCESS)
759 return VA_INVALID_ID;
763 encoder_write_output(struct vaapi_recorder *r, VABufferID output_buf)
765 VACodedBufferSegment *segment;
769 status = vaMapBuffer(r->va_dpy, output_buf, (void **) &segment);
770 if (status != VA_STATUS_SUCCESS)
773 if (segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
774 r->encoder.output_size *= 2;
775 vaUnmapBuffer(r->va_dpy, output_buf);
779 count = write(r->output_fd, segment->buf, segment->size);
781 vaUnmapBuffer(r->va_dpy, output_buf);
787 encoder_encode(struct vaapi_recorder *r, VASurfaceID input)
789 VABufferID output_buf = VA_INVALID_ID;
791 VABufferID buffers[8];
797 if ((r->frame_count % r->encoder.intra_period) == 0)
798 slice_type = SLICE_TYPE_I;
800 slice_type = SLICE_TYPE_P;
802 buffers[count++] = encoder_update_seq_parameters(r);
803 buffers[count++] = encoder_update_misc_hdr_parameter(r);
804 buffers[count++] = encoder_update_slice_parameter(r, slice_type);
806 for (i = 0; i < count; i++)
807 if (buffers[i] == VA_INVALID_ID)
810 if (r->frame_count == 0)
811 count += encoder_prepare_headers(r, buffers + count);
814 output_buf = encoder_create_output_buffer(r);
815 if (output_buf == VA_INVALID_ID)
819 encoder_update_pic_parameters(r, output_buf);
820 if (buffers[count - 1] == VA_INVALID_ID)
823 encoder_render_picture(r, input, buffers, count);
824 ret = encoder_write_output(r, output_buf);
826 vaDestroyBuffer(r->va_dpy, output_buf);
827 output_buf = VA_INVALID_ID;
829 vaDestroyBuffer(r->va_dpy, buffers[--count]);
832 for (i = 0; i < count; i++)
833 vaDestroyBuffer(r->va_dpy, buffers[i]);
839 for (i = 0; i < count; i++)
840 vaDestroyBuffer(r->va_dpy, buffers[i]);
841 if (output_buf != VA_INVALID_ID)
842 vaDestroyBuffer(r->va_dpy, output_buf);
847 setup_vpp(struct vaapi_recorder *r)
851 status = vaCreateConfig(r->va_dpy, VAProfileNone,
852 VAEntrypointVideoProc, NULL, 0,
854 if (status != VA_STATUS_SUCCESS) {
855 weston_log("vaapi: failed to create VPP config\n");
859 status = vaCreateContext(r->va_dpy, r->vpp.cfg, r->width, r->height,
860 0, NULL, 0, &r->vpp.ctx);
861 if (status != VA_STATUS_SUCCESS) {
862 weston_log("vaapi: failed to create VPP context\n");
866 status = vaCreateBuffer(r->va_dpy, r->vpp.ctx,
867 VAProcPipelineParameterBufferType,
868 sizeof(VAProcPipelineParameterBuffer),
869 1, NULL, &r->vpp.pipeline_buf);
870 if (status != VA_STATUS_SUCCESS) {
871 weston_log("vaapi: failed to create VPP pipeline buffer\n");
875 status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_YUV420,
876 r->width, r->height, &r->vpp.output, 1,
878 if (status != VA_STATUS_SUCCESS) {
879 weston_log("vaapi: failed to create YUV surface\n");
886 vaDestroyBuffer(r->va_dpy, r->vpp.pipeline_buf);
888 vaDestroyConfig(r->va_dpy, r->vpp.ctx);
890 vaDestroyConfig(r->va_dpy, r->vpp.cfg);
896 vpp_destroy(struct vaapi_recorder *r)
898 vaDestroySurfaces(r->va_dpy, &r->vpp.output, 1);
899 vaDestroyBuffer(r->va_dpy, r->vpp.pipeline_buf);
900 vaDestroyConfig(r->va_dpy, r->vpp.ctx);
901 vaDestroyConfig(r->va_dpy, r->vpp.cfg);
905 setup_worker_thread(struct vaapi_recorder *r)
907 pthread_mutex_init(&r->mutex, NULL);
908 pthread_cond_init(&r->input_cond, NULL);
909 pthread_create(&r->worker_thread, NULL, worker_thread_function, r);
915 destroy_worker_thread(struct vaapi_recorder *r)
917 pthread_mutex_lock(&r->mutex);
919 /* Make sure the worker thread finishes */
921 pthread_cond_signal(&r->input_cond);
923 pthread_mutex_unlock(&r->mutex);
925 pthread_join(r->worker_thread, NULL);
927 pthread_mutex_destroy(&r->mutex);
928 pthread_cond_destroy(&r->input_cond);
931 struct vaapi_recorder *
932 vaapi_recorder_create(int drm_fd, int width, int height, const char *filename)
934 struct vaapi_recorder *r;
939 r = calloc(1, sizeof *r);
946 flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
947 r->output_fd = open(filename, flags, 0644);
949 if (setup_worker_thread(r) < 0)
952 if (r->output_fd < 0)
955 r->va_dpy = vaGetDisplayDRM(drm_fd);
957 weston_log("failed to create VA display\n");
961 status = vaInitialize(r->va_dpy, &major, &minor);
962 if (status != VA_STATUS_SUCCESS) {
963 weston_log("vaapi: failed to initialize display\n");
967 if (setup_vpp(r) < 0) {
968 weston_log("vaapi: failed to initialize VPP pipeline\n");
972 if (setup_encoder(r) < 0) {
981 vaTerminate(r->va_dpy);
985 destroy_worker_thread(r);
993 vaapi_recorder_destroy(struct vaapi_recorder *r)
995 destroy_worker_thread(r);
1000 vaTerminate(r->va_dpy);
1002 close(r->output_fd);
1008 create_surface_from_fd(struct vaapi_recorder *r, int prime_fd,
1009 int stride, VASurfaceID *surface)
1011 VASurfaceAttrib va_attribs[2];
1012 VASurfaceAttribExternalBuffers va_attrib_extbuf;
1015 unsigned long buffer_fd = prime_fd;
1017 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX;
1018 va_attrib_extbuf.width = r->width;
1019 va_attrib_extbuf.height = r->height;
1020 va_attrib_extbuf.data_size = r->height * stride;
1021 va_attrib_extbuf.num_planes = 1;
1022 va_attrib_extbuf.pitches[0] = stride;
1023 va_attrib_extbuf.offsets[0] = 0;
1024 va_attrib_extbuf.buffers = &buffer_fd;
1025 va_attrib_extbuf.num_buffers = 1;
1026 va_attrib_extbuf.flags = 0;
1027 va_attrib_extbuf.private_data = NULL;
1029 va_attribs[0].type = VASurfaceAttribMemoryType;
1030 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
1031 va_attribs[0].value.type = VAGenericValueTypeInteger;
1032 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
1034 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor;
1035 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
1036 va_attribs[1].value.type = VAGenericValueTypePointer;
1037 va_attribs[1].value.value.p = &va_attrib_extbuf;
1039 status = vaCreateSurfaces(r->va_dpy, VA_RT_FORMAT_RGB32,
1040 r->width, r->height, surface, 1,
1047 convert_rgb_to_yuv(struct vaapi_recorder *r, VASurfaceID rgb_surface)
1049 VAProcPipelineParameterBuffer *pipeline_param;
1052 status = vaMapBuffer(r->va_dpy, r->vpp.pipeline_buf,
1053 (void **) &pipeline_param);
1054 if (status != VA_STATUS_SUCCESS)
1057 memset(pipeline_param, 0, sizeof *pipeline_param);
1059 pipeline_param->surface = rgb_surface;
1060 pipeline_param->surface_color_standard = VAProcColorStandardNone;
1062 pipeline_param->output_background_color = 0xff000000;
1063 pipeline_param->output_color_standard = VAProcColorStandardNone;
1065 status = vaUnmapBuffer(r->va_dpy, r->vpp.pipeline_buf);
1066 if (status != VA_STATUS_SUCCESS)
1069 status = vaBeginPicture(r->va_dpy, r->vpp.ctx, r->vpp.output);
1070 if (status != VA_STATUS_SUCCESS)
1073 status = vaRenderPicture(r->va_dpy, r->vpp.ctx,
1074 &r->vpp.pipeline_buf, 1);
1075 if (status != VA_STATUS_SUCCESS)
1078 status = vaEndPicture(r->va_dpy, r->vpp.ctx);
1079 if (status != VA_STATUS_SUCCESS)
1086 recorder_frame(struct vaapi_recorder *r)
1088 VASurfaceID rgb_surface;
1091 status = create_surface_from_fd(r, r->input.prime_fd,
1092 r->input.stride, &rgb_surface);
1093 if (status != VA_STATUS_SUCCESS) {
1094 weston_log("[libva recorder] "
1095 "failed to create surface from bo\n");
1099 close(r->input.prime_fd);
1101 status = convert_rgb_to_yuv(r, rgb_surface);
1102 if (status != VA_STATUS_SUCCESS) {
1103 weston_log("[libva recorder] "
1104 "color space conversion failed\n");
1108 encoder_encode(r, r->vpp.output);
1110 vaDestroySurfaces(r->va_dpy, &rgb_surface, 1);
1114 worker_thread_function(void *data)
1116 struct vaapi_recorder *r = data;
1118 pthread_mutex_lock(&r->mutex);
1120 while (!r->destroying) {
1121 if (!r->input.valid)
1122 pthread_cond_wait(&r->input_cond, &r->mutex);
1124 /* If the thread is awaken by destroy_worker_thread(),
1125 * there might not be valid input */
1126 if (!r->input.valid)
1133 pthread_mutex_unlock(&r->mutex);
1139 vaapi_recorder_frame(struct vaapi_recorder *r, int prime_fd, int stride)
1141 pthread_mutex_lock(&r->mutex);
1143 /* The mutex is never released while encoding, so this point should
1144 * never be reached if input.valid is true. */
1145 assert(!r->input.valid);
1147 r->input.prime_fd = prime_fd;
1148 r->input.stride = stride;
1150 pthread_cond_signal(&r->input_cond);
1152 pthread_mutex_unlock(&r->mutex);