1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "pipe/p_video_codec.h"
31 #include "util/u_handle_table.h"
32 #include "util/u_video.h"
34 #include "vl/vl_vlc.h"
35 #include "vl/vl_winsys.h"
37 #include "va_private.h"
40 vlVaBeginPicture(VADriverContextP ctx, VAContextID context_id, VASurfaceID render_target)
47 return VA_STATUS_ERROR_INVALID_CONTEXT;
49 drv = VL_VA_DRIVER(ctx);
51 return VA_STATUS_ERROR_INVALID_CONTEXT;
53 mtx_lock(&drv->mutex);
54 context = handle_table_get(drv->htab, context_id);
56 mtx_unlock(&drv->mutex);
57 return VA_STATUS_ERROR_INVALID_CONTEXT;
60 surf = handle_table_get(drv->htab, render_target);
61 mtx_unlock(&drv->mutex);
62 if (!surf || !surf->buffer)
63 return VA_STATUS_ERROR_INVALID_SURFACE;
65 context->target_id = render_target;
66 surf->ctx = context_id;
67 context->target = surf->buffer;
68 context->mjpeg.sampling_factor = 0;
70 if (!context->decoder) {
73 if (context->templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN &&
74 context->target->buffer_format != PIPE_FORMAT_B8G8R8A8_UNORM &&
75 context->target->buffer_format != PIPE_FORMAT_R8G8B8A8_UNORM &&
76 context->target->buffer_format != PIPE_FORMAT_B8G8R8X8_UNORM &&
77 context->target->buffer_format != PIPE_FORMAT_R8G8B8X8_UNORM &&
78 context->target->buffer_format != PIPE_FORMAT_NV12 &&
79 context->target->buffer_format != PIPE_FORMAT_P016)
80 return VA_STATUS_ERROR_UNIMPLEMENTED;
82 return VA_STATUS_SUCCESS;
85 if (context->decoder->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE)
86 context->needs_begin_frame = true;
88 return VA_STATUS_SUCCESS;
92 vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
93 struct pipe_video_buffer **ref_frame)
95 vlVaSurface *surf = handle_table_get(drv->htab, surface_id);
97 *ref_frame = surf->buffer;
103 getEncParamPreset(vlVaContext *context)
105 //motion estimation preset
106 context->desc.h264enc.motion_est.motion_est_quarter_pixel = 0x00000001;
107 context->desc.h264enc.motion_est.lsmvert = 0x00000002;
108 context->desc.h264enc.motion_est.enc_disable_sub_mode = 0x00000078;
109 context->desc.h264enc.motion_est.enc_en_ime_overw_dis_subm = 0x00000001;
110 context->desc.h264enc.motion_est.enc_ime_overw_dis_subm_no = 0x00000001;
111 context->desc.h264enc.motion_est.enc_ime2_search_range_x = 0x00000004;
112 context->desc.h264enc.motion_est.enc_ime2_search_range_y = 0x00000004;
115 context->desc.h264enc.pic_ctrl.enc_cabac_enable = 0x00000001;
116 context->desc.h264enc.pic_ctrl.enc_constraint_set_flags = 0x00000040;
119 context->desc.h264enc.rate_ctrl.vbv_buffer_size = 20000000;
120 context->desc.h264enc.rate_ctrl.vbv_buf_lv = 48;
121 context->desc.h264enc.rate_ctrl.fill_data_enable = 1;
122 context->desc.h264enc.rate_ctrl.enforce_hrd = 1;
123 context->desc.h264enc.enable_vui = false;
124 if (context->desc.h264enc.rate_ctrl.frame_rate_num == 0 ||
125 context->desc.h264enc.rate_ctrl.frame_rate_den == 0) {
126 context->desc.h264enc.rate_ctrl.frame_rate_num = 30;
127 context->desc.h264enc.rate_ctrl.frame_rate_den = 1;
129 context->desc.h264enc.rate_ctrl.target_bits_picture =
130 context->desc.h264enc.rate_ctrl.target_bitrate *
131 ((float)context->desc.h264enc.rate_ctrl.frame_rate_den /
132 context->desc.h264enc.rate_ctrl.frame_rate_num);
133 context->desc.h264enc.rate_ctrl.peak_bits_picture_integer =
134 context->desc.h264enc.rate_ctrl.peak_bitrate *
135 ((float)context->desc.h264enc.rate_ctrl.frame_rate_den /
136 context->desc.h264enc.rate_ctrl.frame_rate_num);
138 context->desc.h264enc.rate_ctrl.peak_bits_picture_fraction = 0;
139 context->desc.h264enc.ref_pic_mode = 0x00000201;
143 handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
145 VAStatus vaStatus = VA_STATUS_SUCCESS;
147 switch (u_reduce_video_profile(context->templat.profile)) {
148 case PIPE_VIDEO_FORMAT_MPEG12:
149 vlVaHandlePictureParameterBufferMPEG12(drv, context, buf);
152 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
153 vlVaHandlePictureParameterBufferH264(drv, context, buf);
156 case PIPE_VIDEO_FORMAT_VC1:
157 vlVaHandlePictureParameterBufferVC1(drv, context, buf);
160 case PIPE_VIDEO_FORMAT_MPEG4:
161 vlVaHandlePictureParameterBufferMPEG4(drv, context, buf);
164 case PIPE_VIDEO_FORMAT_HEVC:
165 vlVaHandlePictureParameterBufferHEVC(drv, context, buf);
168 case PIPE_VIDEO_FORMAT_JPEG:
169 vlVaHandlePictureParameterBufferMJPEG(drv, context, buf);
176 /* Create the decoder once max_references is known. */
177 if (!context->decoder) {
178 enum pipe_video_format format =
179 u_reduce_video_profile(context->templat.profile);
181 if (!context->target)
182 return VA_STATUS_ERROR_INVALID_CONTEXT;
184 if (context->templat.max_references == 0 &&
185 format != PIPE_VIDEO_FORMAT_JPEG)
186 return VA_STATUS_ERROR_INVALID_BUFFER;
188 if (format == PIPE_VIDEO_FORMAT_MPEG4_AVC)
189 context->templat.level = u_get_h264_level(context->templat.width,
190 context->templat.height, &context->templat.max_references);
192 context->decoder = drv->pipe->create_video_codec(drv->pipe,
195 if (!context->decoder)
196 return VA_STATUS_ERROR_ALLOCATION_FAILED;
198 context->needs_begin_frame = true;
205 handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
207 switch (u_reduce_video_profile(context->templat.profile)) {
208 case PIPE_VIDEO_FORMAT_MPEG12:
209 vlVaHandleIQMatrixBufferMPEG12(context, buf);
212 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
213 vlVaHandleIQMatrixBufferH264(context, buf);
216 case PIPE_VIDEO_FORMAT_MPEG4:
217 vlVaHandleIQMatrixBufferMPEG4(context, buf);
220 case PIPE_VIDEO_FORMAT_HEVC:
221 vlVaHandleIQMatrixBufferHEVC(context, buf);
224 case PIPE_VIDEO_FORMAT_JPEG:
225 vlVaHandleIQMatrixBufferMJPEG(context, buf);
234 handleSliceParameterBuffer(vlVaContext *context, vlVaBuffer *buf)
236 switch (u_reduce_video_profile(context->templat.profile)) {
237 case PIPE_VIDEO_FORMAT_MPEG12:
238 vlVaHandleSliceParameterBufferMPEG12(context, buf);
241 case PIPE_VIDEO_FORMAT_VC1:
242 vlVaHandleSliceParameterBufferVC1(context, buf);
245 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
246 vlVaHandleSliceParameterBufferH264(context, buf);
249 case PIPE_VIDEO_FORMAT_MPEG4:
250 vlVaHandleSliceParameterBufferMPEG4(context, buf);
253 case PIPE_VIDEO_FORMAT_HEVC:
254 vlVaHandleSliceParameterBufferHEVC(context, buf);
257 case PIPE_VIDEO_FORMAT_JPEG:
258 vlVaHandleSliceParameterBufferMJPEG(context, buf);
267 bufHasStartcode(vlVaBuffer *buf, unsigned int code, unsigned int bits)
269 struct vl_vlc vlc = {0};
272 /* search the first 64 bytes for a startcode */
273 vl_vlc_init(&vlc, 1, (const void * const*)&buf->data, &buf->size);
274 for (i = 0; i < 64 && vl_vlc_bits_left(&vlc) >= bits; ++i) {
275 if (vl_vlc_peekbits(&vlc, bits) == code)
277 vl_vlc_eatbits(&vlc, 8);
278 vl_vlc_fillbits(&vlc);
285 handleVASliceDataBufferType(vlVaContext *context, vlVaBuffer *buf)
287 enum pipe_video_format format;
288 unsigned num_buffers = 0;
289 void * const *buffers[2];
291 static const uint8_t start_code_h264[] = { 0x00, 0x00, 0x01 };
292 static const uint8_t start_code_h265[] = { 0x00, 0x00, 0x01 };
293 static const uint8_t start_code_vc1[] = { 0x00, 0x00, 0x01, 0x0d };
295 format = u_reduce_video_profile(context->templat.profile);
297 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
298 if (bufHasStartcode(buf, 0x000001, 24))
301 buffers[num_buffers] = (void *const)&start_code_h264;
302 sizes[num_buffers++] = sizeof(start_code_h264);
304 case PIPE_VIDEO_FORMAT_HEVC:
305 if (bufHasStartcode(buf, 0x000001, 24))
308 buffers[num_buffers] = (void *const)&start_code_h265;
309 sizes[num_buffers++] = sizeof(start_code_h265);
311 case PIPE_VIDEO_FORMAT_VC1:
312 if (bufHasStartcode(buf, 0x0000010d, 32) ||
313 bufHasStartcode(buf, 0x0000010c, 32) ||
314 bufHasStartcode(buf, 0x0000010b, 32))
317 if (context->decoder->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED) {
318 buffers[num_buffers] = (void *const)&start_code_vc1;
319 sizes[num_buffers++] = sizeof(start_code_vc1);
322 case PIPE_VIDEO_FORMAT_MPEG4:
323 if (bufHasStartcode(buf, 0x000001, 24))
326 vlVaDecoderFixMPEG4Startcode(context);
327 buffers[num_buffers] = (void *)context->mpeg4.start_code;
328 sizes[num_buffers++] = context->mpeg4.start_code_size;
329 case PIPE_VIDEO_FORMAT_JPEG:
336 buffers[num_buffers] = buf->data;
337 sizes[num_buffers] = buf->size;
340 if (context->needs_begin_frame) {
341 context->decoder->begin_frame(context->decoder, context->target,
342 &context->desc.base);
343 context->needs_begin_frame = false;
345 context->decoder->decode_bitstream(context->decoder, context->target, &context->desc.base,
346 num_buffers, (const void * const*)buffers, sizes);
350 handleVAEncMiscParameterTypeRateControl(vlVaContext *context, VAEncMiscParameterBuffer *misc)
352 VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
353 if (context->desc.h264enc.rate_ctrl.rate_ctrl_method ==
354 PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT)
355 context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second;
357 context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second * (rc->target_percentage / 100.0);
358 context->desc.h264enc.rate_ctrl.peak_bitrate = rc->bits_per_second;
359 if (context->desc.h264enc.rate_ctrl.target_bitrate < 2000000)
360 context->desc.h264enc.rate_ctrl.vbv_buffer_size = MIN2((context->desc.h264enc.rate_ctrl.target_bitrate * 2.75), 2000000);
362 context->desc.h264enc.rate_ctrl.vbv_buffer_size = context->desc.h264enc.rate_ctrl.target_bitrate;
364 return VA_STATUS_SUCCESS;
368 handleVAEncMiscParameterTypeFrameRate(vlVaContext *context, VAEncMiscParameterBuffer *misc)
370 VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
371 if (fr->framerate & 0xffff0000) {
372 context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate & 0xffff;
373 context->desc.h264enc.rate_ctrl.frame_rate_den = fr->framerate >> 16 & 0xffff;
375 context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate;
376 context->desc.h264enc.rate_ctrl.frame_rate_den = 1;
378 return VA_STATUS_SUCCESS;
382 handleVAEncSequenceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
384 VAEncSequenceParameterBufferH264 *h264 = (VAEncSequenceParameterBufferH264 *)buf->data;
385 if (!context->decoder) {
386 context->templat.max_references = h264->max_num_ref_frames;
387 context->templat.level = h264->level_idc;
388 context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);
389 if (!context->decoder)
390 return VA_STATUS_ERROR_ALLOCATION_FAILED;
393 context->gop_coeff = ((1024 + h264->intra_idr_period - 1) / h264->intra_idr_period + 1) / 2 * 2;
394 if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)
395 context->gop_coeff = VL_VA_ENC_GOP_COEFF;
396 context->desc.h264enc.gop_size = h264->intra_idr_period * context->gop_coeff;
397 context->desc.h264enc.rate_ctrl.frame_rate_num = h264->time_scale / 2;
398 context->desc.h264enc.rate_ctrl.frame_rate_den = h264->num_units_in_tick;
399 return VA_STATUS_SUCCESS;
403 handleVAEncMiscParameterBufferType(vlVaContext *context, vlVaBuffer *buf)
405 VAStatus vaStatus = VA_STATUS_SUCCESS;
406 VAEncMiscParameterBuffer *misc;
409 switch (misc->type) {
410 case VAEncMiscParameterTypeRateControl:
411 vaStatus = handleVAEncMiscParameterTypeRateControl(context, misc);
414 case VAEncMiscParameterTypeFrameRate:
415 vaStatus = handleVAEncMiscParameterTypeFrameRate(context, misc);
426 handleVAEncPictureParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
428 VAEncPictureParameterBufferH264 *h264;
429 vlVaBuffer *coded_buf;
432 context->desc.h264enc.frame_num = h264->frame_num;
433 context->desc.h264enc.not_referenced = false;
434 context->desc.h264enc.is_idr = (h264->pic_fields.bits.idr_pic_flag == 1);
435 context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
436 if (context->desc.h264enc.gop_cnt == 0)
437 context->desc.h264enc.i_remain = context->gop_coeff;
438 else if (context->desc.h264enc.frame_num == 1)
439 context->desc.h264enc.i_remain--;
441 context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;
443 coded_buf = handle_table_get(drv->htab, h264->coded_buf);
444 if (!coded_buf->derived_surface.resource)
445 coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
446 PIPE_USAGE_STREAM, coded_buf->size);
447 context->coded_buf = coded_buf;
449 util_hash_table_set(context->desc.h264enc.frame_idx,
450 UINT_TO_PTR(h264->CurrPic.picture_id),
451 UINT_TO_PTR(h264->frame_num));
453 if (context->desc.h264enc.is_idr)
454 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
456 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
458 context->desc.h264enc.quant_i_frames = h264->pic_init_qp;
459 context->desc.h264enc.quant_b_frames = h264->pic_init_qp;
460 context->desc.h264enc.quant_p_frames = h264->pic_init_qp;
461 context->desc.h264enc.gop_cnt++;
462 if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)
463 context->desc.h264enc.gop_cnt = 0;
465 return VA_STATUS_SUCCESS;
469 handleVAEncSliceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
471 VAEncSliceParameterBufferH264 *h264;
474 context->desc.h264enc.ref_idx_l0 = VA_INVALID_ID;
475 context->desc.h264enc.ref_idx_l1 = VA_INVALID_ID;
477 for (int i = 0; i < 32; i++) {
478 if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {
479 if (context->desc.h264enc.ref_idx_l0 == VA_INVALID_ID)
480 context->desc.h264enc.ref_idx_l0 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
481 UINT_TO_PTR(h264->RefPicList0[i].picture_id)));
483 if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {
484 if (context->desc.h264enc.ref_idx_l1 == VA_INVALID_ID)
485 context->desc.h264enc.ref_idx_l1 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
486 UINT_TO_PTR(h264->RefPicList1[i].picture_id)));
490 if (h264->slice_type == 1)
491 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_B;
492 else if (h264->slice_type == 0)
493 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
494 else if (h264->slice_type == 2) {
495 if (context->desc.h264enc.is_idr){
496 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
497 context->desc.h264enc.idr_pic_id++;
499 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_I;
501 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_SKIP;
503 return VA_STATUS_SUCCESS;
507 vlVaRenderPicture(VADriverContextP ctx, VAContextID context_id, VABufferID *buffers, int num_buffers)
510 vlVaContext *context;
511 VAStatus vaStatus = VA_STATUS_SUCCESS;
516 return VA_STATUS_ERROR_INVALID_CONTEXT;
518 drv = VL_VA_DRIVER(ctx);
520 return VA_STATUS_ERROR_INVALID_CONTEXT;
522 mtx_lock(&drv->mutex);
523 context = handle_table_get(drv->htab, context_id);
525 mtx_unlock(&drv->mutex);
526 return VA_STATUS_ERROR_INVALID_CONTEXT;
529 for (i = 0; i < num_buffers; ++i) {
530 vlVaBuffer *buf = handle_table_get(drv->htab, buffers[i]);
532 mtx_unlock(&drv->mutex);
533 return VA_STATUS_ERROR_INVALID_BUFFER;
537 case VAPictureParameterBufferType:
538 vaStatus = handlePictureParameterBuffer(drv, context, buf);
541 case VAIQMatrixBufferType:
542 handleIQMatrixBuffer(context, buf);
545 case VASliceParameterBufferType:
546 handleSliceParameterBuffer(context, buf);
549 case VASliceDataBufferType:
550 handleVASliceDataBufferType(context, buf);
552 case VAProcPipelineParameterBufferType:
553 vaStatus = vlVaHandleVAProcPipelineParameterBufferType(drv, context, buf);
556 case VAEncSequenceParameterBufferType:
557 vaStatus = handleVAEncSequenceParameterBufferType(drv, context, buf);
560 case VAEncMiscParameterBufferType:
561 vaStatus = handleVAEncMiscParameterBufferType(context, buf);
564 case VAEncPictureParameterBufferType:
565 vaStatus = handleVAEncPictureParameterBufferType(drv, context, buf);
568 case VAEncSliceParameterBufferType:
569 vaStatus = handleVAEncSliceParameterBufferType(drv, context, buf);
572 case VAHuffmanTableBufferType:
573 vlVaHandleHuffmanTableBufferType(context, buf);
580 mtx_unlock(&drv->mutex);
586 vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
589 vlVaContext *context;
590 vlVaBuffer *coded_buf;
593 struct pipe_screen *screen;
595 bool realloc = false;
598 return VA_STATUS_ERROR_INVALID_CONTEXT;
600 drv = VL_VA_DRIVER(ctx);
602 return VA_STATUS_ERROR_INVALID_CONTEXT;
604 mtx_lock(&drv->mutex);
605 context = handle_table_get(drv->htab, context_id);
606 mtx_unlock(&drv->mutex);
608 return VA_STATUS_ERROR_INVALID_CONTEXT;
610 if (!context->decoder) {
611 if (context->templat.profile != PIPE_VIDEO_PROFILE_UNKNOWN)
612 return VA_STATUS_ERROR_INVALID_CONTEXT;
615 return VA_STATUS_SUCCESS;
618 mtx_lock(&drv->mutex);
619 surf = handle_table_get(drv->htab, context->target_id);
620 context->mpeg4.frame_num++;
622 screen = context->decoder->context->screen;
623 interlaced = screen->get_video_param(screen, context->decoder->profile,
624 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
625 PIPE_VIDEO_CAP_SUPPORTS_INTERLACED);
627 if (surf->buffer->interlaced != interlaced) {
628 surf->templat.interlaced = screen->get_video_param(screen, context->decoder->profile,
629 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
630 PIPE_VIDEO_CAP_PREFERS_INTERLACED);
634 if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_JPEG &&
635 surf->buffer->buffer_format == PIPE_FORMAT_NV12) {
636 if (context->mjpeg.sampling_factor == 0x211111 ||
637 context->mjpeg.sampling_factor == 0x221212) {
638 surf->templat.buffer_format = PIPE_FORMAT_YUYV;
640 } else if (context->mjpeg.sampling_factor != 0x221111) {
641 /* Not NV12 either */
642 mtx_unlock(&drv->mutex);
643 return VA_STATUS_ERROR_INVALID_SURFACE;
648 surf->buffer->destroy(surf->buffer);
650 if (vlVaHandleSurfaceAllocate(ctx, surf, &surf->templat) != VA_STATUS_SUCCESS) {
651 mtx_unlock(&drv->mutex);
652 return VA_STATUS_ERROR_ALLOCATION_FAILED;
655 context->target = surf->buffer;
658 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
659 coded_buf = context->coded_buf;
660 getEncParamPreset(context);
661 context->desc.h264enc.frame_num_cnt++;
662 context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
663 context->decoder->encode_bitstream(context->decoder, context->target,
664 coded_buf->derived_surface.resource, &feedback);
665 surf->feedback = feedback;
666 surf->coded_buf = coded_buf;
669 context->decoder->end_frame(context->decoder, context->target, &context->desc.base);
670 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
671 int idr_period = context->desc.h264enc.gop_size / context->gop_coeff;
672 int p_remain_in_idr = idr_period - context->desc.h264enc.frame_num;
673 surf->frame_num_cnt = context->desc.h264enc.frame_num_cnt;
674 surf->force_flushed = false;
675 if (context->first_single_submitted) {
676 context->decoder->flush(context->decoder);
677 context->first_single_submitted = false;
678 surf->force_flushed = true;
680 if (p_remain_in_idr == 1) {
681 if ((context->desc.h264enc.frame_num_cnt % 2) != 0) {
682 context->decoder->flush(context->decoder);
683 context->first_single_submitted = true;
686 context->first_single_submitted = false;
687 surf->force_flushed = true;
690 mtx_unlock(&drv->mutex);
691 return VA_STATUS_SUCCESS;