st/va: fix gop size for rate control
[mesa.git] / src / gallium / state_trackers / va / picture.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_video_codec.h"
30
31 #include "util/u_handle_table.h"
32 #include "util/u_video.h"
33
34 #include "vl/vl_vlc.h"
35 #include "vl/vl_winsys.h"
36
37 #include "va_private.h"
38
39 VAStatus
40 vlVaBeginPicture(VADriverContextP ctx, VAContextID context_id, VASurfaceID render_target)
41 {
42 vlVaDriver *drv;
43 vlVaContext *context;
44 vlVaSurface *surf;
45
46 if (!ctx)
47 return VA_STATUS_ERROR_INVALID_CONTEXT;
48
49 drv = VL_VA_DRIVER(ctx);
50 if (!drv)
51 return VA_STATUS_ERROR_INVALID_CONTEXT;
52
53 pipe_mutex_lock(drv->mutex);
54 context = handle_table_get(drv->htab, context_id);
55 if (!context) {
56 pipe_mutex_unlock(drv->mutex);
57 return VA_STATUS_ERROR_INVALID_CONTEXT;
58 }
59
60 surf = handle_table_get(drv->htab, render_target);
61 pipe_mutex_unlock(drv->mutex);
62 if (!surf || !surf->buffer)
63 return VA_STATUS_ERROR_INVALID_SURFACE;
64
65 context->target_id = render_target;
66 surf->ctx = context_id;
67 context->target = surf->buffer;
68
69 if (!context->decoder) {
70
71 /* VPP */
72 if (context->templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN &&
73 context->target->buffer_format != PIPE_FORMAT_B8G8R8A8_UNORM &&
74 context->target->buffer_format != PIPE_FORMAT_R8G8B8A8_UNORM &&
75 context->target->buffer_format != PIPE_FORMAT_B8G8R8X8_UNORM &&
76 context->target->buffer_format != PIPE_FORMAT_R8G8B8X8_UNORM &&
77 context->target->buffer_format != PIPE_FORMAT_NV12)
78 return VA_STATUS_ERROR_UNIMPLEMENTED;
79
80 return VA_STATUS_SUCCESS;
81 }
82
83 if (context->decoder->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE)
84 context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
85
86 return VA_STATUS_SUCCESS;
87 }
88
89 void
90 vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
91 struct pipe_video_buffer **ref_frame)
92 {
93 vlVaSurface *surf = handle_table_get(drv->htab, surface_id);
94 if (surf)
95 *ref_frame = surf->buffer;
96 else
97 *ref_frame = NULL;
98 }
99
100 static void
101 getEncParamPreset(vlVaContext *context)
102 {
103 //motion estimation preset
104 context->desc.h264enc.motion_est.motion_est_quarter_pixel = 0x00000001;
105 context->desc.h264enc.motion_est.lsmvert = 0x00000002;
106 context->desc.h264enc.motion_est.enc_disable_sub_mode = 0x00000078;
107 context->desc.h264enc.motion_est.enc_en_ime_overw_dis_subm = 0x00000001;
108 context->desc.h264enc.motion_est.enc_ime_overw_dis_subm_no = 0x00000001;
109 context->desc.h264enc.motion_est.enc_ime2_search_range_x = 0x00000004;
110 context->desc.h264enc.motion_est.enc_ime2_search_range_y = 0x00000004;
111
112 //pic control preset
113 context->desc.h264enc.pic_ctrl.enc_cabac_enable = 0x00000001;
114 context->desc.h264enc.pic_ctrl.enc_constraint_set_flags = 0x00000040;
115
116 //rate control
117 context->desc.h264enc.rate_ctrl.vbv_buffer_size = 20000000;
118 context->desc.h264enc.rate_ctrl.vbv_buf_lv = 48;
119 context->desc.h264enc.rate_ctrl.fill_data_enable = 1;
120 context->desc.h264enc.rate_ctrl.enforce_hrd = 1;
121 context->desc.h264enc.enable_vui = false;
122 if (context->desc.h264enc.rate_ctrl.frame_rate_num == 0)
123 context->desc.h264enc.rate_ctrl.frame_rate_num = 30;
124 context->desc.h264enc.rate_ctrl.target_bits_picture =
125 context->desc.h264enc.rate_ctrl.target_bitrate / context->desc.h264enc.rate_ctrl.frame_rate_num;
126 context->desc.h264enc.rate_ctrl.peak_bits_picture_integer =
127 context->desc.h264enc.rate_ctrl.peak_bitrate / context->desc.h264enc.rate_ctrl.frame_rate_num;
128 context->desc.h264enc.rate_ctrl.peak_bits_picture_fraction = 0;
129
130 context->desc.h264enc.ref_pic_mode = 0x00000201;
131 }
132
133 static VAStatus
134 handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
135 {
136 VAStatus vaStatus = VA_STATUS_SUCCESS;
137
138 switch (u_reduce_video_profile(context->templat.profile)) {
139 case PIPE_VIDEO_FORMAT_MPEG12:
140 vlVaHandlePictureParameterBufferMPEG12(drv, context, buf);
141 break;
142
143 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
144 vlVaHandlePictureParameterBufferH264(drv, context, buf);
145 break;
146
147 case PIPE_VIDEO_FORMAT_VC1:
148 vlVaHandlePictureParameterBufferVC1(drv, context, buf);
149 break;
150
151 case PIPE_VIDEO_FORMAT_MPEG4:
152 vlVaHandlePictureParameterBufferMPEG4(drv, context, buf);
153 break;
154
155 case PIPE_VIDEO_FORMAT_HEVC:
156 vlVaHandlePictureParameterBufferHEVC(drv, context, buf);
157 break;
158
159 default:
160 break;
161 }
162
163 /* Create the decoder once max_references is known. */
164 if (!context->decoder) {
165 if (!context->target)
166 return VA_STATUS_ERROR_INVALID_CONTEXT;
167
168 if (context->templat.max_references == 0)
169 return VA_STATUS_ERROR_INVALID_BUFFER;
170
171 if (u_reduce_video_profile(context->templat.profile) ==
172 PIPE_VIDEO_FORMAT_MPEG4_AVC)
173 context->templat.level = u_get_h264_level(context->templat.width,
174 context->templat.height, &context->templat.max_references);
175
176 context->decoder = drv->pipe->create_video_codec(drv->pipe,
177 &context->templat);
178
179 if (!context->decoder)
180 return VA_STATUS_ERROR_ALLOCATION_FAILED;
181
182 context->decoder->begin_frame(context->decoder, context->target,
183 &context->desc.base);
184 }
185
186 return vaStatus;
187 }
188
189 static void
190 handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
191 {
192 switch (u_reduce_video_profile(context->templat.profile)) {
193 case PIPE_VIDEO_FORMAT_MPEG12:
194 vlVaHandleIQMatrixBufferMPEG12(context, buf);
195 break;
196
197 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
198 vlVaHandleIQMatrixBufferH264(context, buf);
199 break;
200
201 case PIPE_VIDEO_FORMAT_MPEG4:
202 vlVaHandleIQMatrixBufferMPEG4(context, buf);
203 break;
204
205 case PIPE_VIDEO_FORMAT_HEVC:
206 vlVaHandleIQMatrixBufferHEVC(context, buf);
207 break;
208
209 default:
210 break;
211 }
212 }
213
214 static void
215 handleSliceParameterBuffer(vlVaContext *context, vlVaBuffer *buf)
216 {
217 switch (u_reduce_video_profile(context->templat.profile)) {
218 case PIPE_VIDEO_FORMAT_MPEG12:
219 vlVaHandleSliceParameterBufferMPEG12(context, buf);
220 break;
221
222 case PIPE_VIDEO_FORMAT_VC1:
223 vlVaHandleSliceParameterBufferVC1(context, buf);
224 break;
225
226 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
227 vlVaHandleSliceParameterBufferH264(context, buf);
228 break;
229
230 case PIPE_VIDEO_FORMAT_MPEG4:
231 vlVaHandleSliceParameterBufferMPEG4(context, buf);
232 break;
233
234 case PIPE_VIDEO_FORMAT_HEVC:
235 vlVaHandleSliceParameterBufferHEVC(context, buf);
236 break;
237
238 default:
239 break;
240 }
241 }
242
243 static unsigned int
244 bufHasStartcode(vlVaBuffer *buf, unsigned int code, unsigned int bits)
245 {
246 struct vl_vlc vlc = {0};
247 int i;
248
249 /* search the first 64 bytes for a startcode */
250 vl_vlc_init(&vlc, 1, (const void * const*)&buf->data, &buf->size);
251 for (i = 0; i < 64 && vl_vlc_bits_left(&vlc) >= bits; ++i) {
252 if (vl_vlc_peekbits(&vlc, bits) == code)
253 return 1;
254 vl_vlc_eatbits(&vlc, 8);
255 vl_vlc_fillbits(&vlc);
256 }
257
258 return 0;
259 }
260
261 static void
262 handleVASliceDataBufferType(vlVaContext *context, vlVaBuffer *buf)
263 {
264 enum pipe_video_format format;
265 unsigned num_buffers = 0;
266 void * const *buffers[2];
267 unsigned sizes[2];
268 static const uint8_t start_code_h264[] = { 0x00, 0x00, 0x01 };
269 static const uint8_t start_code_h265[] = { 0x00, 0x00, 0x01 };
270 static const uint8_t start_code_vc1[] = { 0x00, 0x00, 0x01, 0x0d };
271
272 format = u_reduce_video_profile(context->templat.profile);
273 switch (format) {
274 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
275 if (bufHasStartcode(buf, 0x000001, 24))
276 break;
277
278 buffers[num_buffers] = (void *const)&start_code_h264;
279 sizes[num_buffers++] = sizeof(start_code_h264);
280 break;
281 case PIPE_VIDEO_FORMAT_HEVC:
282 if (bufHasStartcode(buf, 0x000001, 24))
283 break;
284
285 buffers[num_buffers] = (void *const)&start_code_h265;
286 sizes[num_buffers++] = sizeof(start_code_h265);
287 break;
288 case PIPE_VIDEO_FORMAT_VC1:
289 if (bufHasStartcode(buf, 0x0000010d, 32) ||
290 bufHasStartcode(buf, 0x0000010c, 32) ||
291 bufHasStartcode(buf, 0x0000010b, 32))
292 break;
293
294 if (context->decoder->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED) {
295 buffers[num_buffers] = (void *const)&start_code_vc1;
296 sizes[num_buffers++] = sizeof(start_code_vc1);
297 }
298 break;
299 case PIPE_VIDEO_FORMAT_MPEG4:
300 if (bufHasStartcode(buf, 0x000001, 24))
301 break;
302
303 vlVaDecoderFixMPEG4Startcode(context);
304 buffers[num_buffers] = (void *)context->mpeg4.start_code;
305 sizes[num_buffers++] = context->mpeg4.start_code_size;
306 default:
307 break;
308 }
309
310 buffers[num_buffers] = buf->data;
311 sizes[num_buffers] = buf->size;
312 ++num_buffers;
313 context->decoder->decode_bitstream(context->decoder, context->target, &context->desc.base,
314 num_buffers, (const void * const*)buffers, sizes);
315 }
316
317 static VAStatus
318 handleVAEncMiscParameterTypeRateControl(vlVaContext *context, VAEncMiscParameterBuffer *misc)
319 {
320 VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
321 if (context->desc.h264enc.rate_ctrl.rate_ctrl_method ==
322 PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT)
323 context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second;
324 else
325 context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second * (rc->target_percentage / 100.0);
326 context->desc.h264enc.rate_ctrl.peak_bitrate = rc->bits_per_second;
327 if (context->desc.h264enc.rate_ctrl.target_bitrate < 2000000)
328 context->desc.h264enc.rate_ctrl.vbv_buffer_size = MIN2((context->desc.h264enc.rate_ctrl.target_bitrate * 2.75), 2000000);
329 else
330 context->desc.h264enc.rate_ctrl.vbv_buffer_size = context->desc.h264enc.rate_ctrl.target_bitrate;
331
332 return VA_STATUS_SUCCESS;
333 }
334
335 static VAStatus
336 handleVAEncMiscParameterTypeFrameRate(vlVaContext *context, VAEncMiscParameterBuffer *misc)
337 {
338 VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
339 context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate;
340 return VA_STATUS_SUCCESS;
341 }
342
343 static VAStatus
344 handleVAEncSequenceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
345 {
346 VAEncSequenceParameterBufferH264 *h264 = (VAEncSequenceParameterBufferH264 *)buf->data;
347 if (!context->decoder) {
348 context->templat.max_references = h264->max_num_ref_frames;
349 context->templat.level = h264->level_idc;
350 context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);
351 if (!context->decoder)
352 return VA_STATUS_ERROR_ALLOCATION_FAILED;
353 }
354
355 context->gop_coeff = ((1024 + h264->intra_idr_period - 1) / h264->intra_idr_period + 1) / 2 * 2;
356 if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)
357 context->gop_coeff = VL_VA_ENC_GOP_COEFF;
358 context->desc.h264enc.gop_size = h264->intra_idr_period * context->gop_coeff;
359 context->desc.h264enc.rate_ctrl.frame_rate_num = h264->time_scale / 2;
360 context->desc.h264enc.rate_ctrl.frame_rate_den = 1;
361 return VA_STATUS_SUCCESS;
362 }
363
364 static VAStatus
365 handleVAEncMiscParameterBufferType(vlVaContext *context, vlVaBuffer *buf)
366 {
367 VAStatus vaStatus = VA_STATUS_SUCCESS;
368 VAEncMiscParameterBuffer *misc;
369 misc = buf->data;
370
371 switch (misc->type) {
372 case VAEncMiscParameterTypeRateControl:
373 vaStatus = handleVAEncMiscParameterTypeRateControl(context, misc);
374 break;
375
376 case VAEncMiscParameterTypeFrameRate:
377 vaStatus = handleVAEncMiscParameterTypeFrameRate(context, misc);
378 break;
379
380 default:
381 break;
382 }
383
384 return vaStatus;
385 }
386
387 static VAStatus
388 handleVAEncPictureParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
389 {
390 VAEncPictureParameterBufferH264 *h264;
391 vlVaBuffer *coded_buf;
392
393 h264 = buf->data;
394 context->desc.h264enc.frame_num = h264->frame_num;
395 context->desc.h264enc.not_referenced = false;
396 context->desc.h264enc.is_idr = (h264->pic_fields.bits.idr_pic_flag == 1);
397 context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
398 if (context->desc.h264enc.gop_cnt == 0)
399 context->desc.h264enc.i_remain = context->gop_coeff;
400 else if (context->desc.h264enc.frame_num == 1)
401 context->desc.h264enc.i_remain--;
402
403 context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;
404
405 coded_buf = handle_table_get(drv->htab, h264->coded_buf);
406 if (!coded_buf->derived_surface.resource)
407 coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
408 PIPE_USAGE_STREAM, coded_buf->size);
409 context->coded_buf = coded_buf;
410
411 context->desc.h264enc.frame_idx[h264->CurrPic.picture_id] = h264->frame_num;
412 if (context->desc.h264enc.is_idr)
413 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
414 else
415 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
416
417 context->desc.h264enc.quant_i_frames = h264->pic_init_qp;
418 context->desc.h264enc.quant_b_frames = h264->pic_init_qp;
419 context->desc.h264enc.quant_p_frames = h264->pic_init_qp;
420 context->desc.h264enc.gop_cnt++;
421 if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)
422 context->desc.h264enc.gop_cnt = 0;
423
424 return VA_STATUS_SUCCESS;
425 }
426
427 static VAStatus
428 handleVAEncSliceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
429 {
430 VAEncSliceParameterBufferH264 *h264;
431
432 h264 = buf->data;
433 context->desc.h264enc.ref_idx_l0 = VA_INVALID_ID;
434 context->desc.h264enc.ref_idx_l1 = VA_INVALID_ID;
435
436 for (int i = 0; i < 32; i++) {
437 if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {
438 if (context->desc.h264enc.ref_idx_l0 == VA_INVALID_ID)
439 context->desc.h264enc.ref_idx_l0 = context->desc.h264enc.frame_idx[h264->RefPicList0[i].picture_id];
440 }
441 if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {
442 if (context->desc.h264enc.ref_idx_l1 == VA_INVALID_ID)
443 context->desc.h264enc.ref_idx_l1 = context->desc.h264enc.frame_idx[h264->RefPicList1[i].picture_id];
444 }
445 }
446
447 if (h264->slice_type == 1)
448 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_B;
449 else if (h264->slice_type == 0)
450 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
451 else if (h264->slice_type == 2) {
452 if (context->desc.h264enc.is_idr){
453 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
454 context->desc.h264enc.idr_pic_id++;
455 } else
456 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_I;
457 } else
458 context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_SKIP;
459
460 return VA_STATUS_SUCCESS;
461 }
462
463 VAStatus
464 vlVaRenderPicture(VADriverContextP ctx, VAContextID context_id, VABufferID *buffers, int num_buffers)
465 {
466 vlVaDriver *drv;
467 vlVaContext *context;
468 VAStatus vaStatus = VA_STATUS_SUCCESS;
469
470 unsigned i;
471
472 if (!ctx)
473 return VA_STATUS_ERROR_INVALID_CONTEXT;
474
475 drv = VL_VA_DRIVER(ctx);
476 if (!drv)
477 return VA_STATUS_ERROR_INVALID_CONTEXT;
478
479 pipe_mutex_lock(drv->mutex);
480 context = handle_table_get(drv->htab, context_id);
481 if (!context) {
482 pipe_mutex_unlock(drv->mutex);
483 return VA_STATUS_ERROR_INVALID_CONTEXT;
484 }
485
486 for (i = 0; i < num_buffers; ++i) {
487 vlVaBuffer *buf = handle_table_get(drv->htab, buffers[i]);
488 if (!buf) {
489 pipe_mutex_unlock(drv->mutex);
490 return VA_STATUS_ERROR_INVALID_BUFFER;
491 }
492
493 switch (buf->type) {
494 case VAPictureParameterBufferType:
495 vaStatus = handlePictureParameterBuffer(drv, context, buf);
496 break;
497
498 case VAIQMatrixBufferType:
499 handleIQMatrixBuffer(context, buf);
500 break;
501
502 case VASliceParameterBufferType:
503 handleSliceParameterBuffer(context, buf);
504 break;
505
506 case VASliceDataBufferType:
507 handleVASliceDataBufferType(context, buf);
508 break;
509 case VAProcPipelineParameterBufferType:
510 vaStatus = vlVaHandleVAProcPipelineParameterBufferType(drv, context, buf);
511 break;
512
513 case VAEncSequenceParameterBufferType:
514 vaStatus = handleVAEncSequenceParameterBufferType(drv, context, buf);
515 break;
516
517 case VAEncMiscParameterBufferType:
518 vaStatus = handleVAEncMiscParameterBufferType(context, buf);
519 break;
520
521 case VAEncPictureParameterBufferType:
522 vaStatus = handleVAEncPictureParameterBufferType(drv, context, buf);
523 break;
524
525 case VAEncSliceParameterBufferType:
526 vaStatus = handleVAEncSliceParameterBufferType(drv, context, buf);
527 break;
528
529 default:
530 break;
531 }
532 }
533 pipe_mutex_unlock(drv->mutex);
534
535 return vaStatus;
536 }
537
538 VAStatus
539 vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
540 {
541 vlVaDriver *drv;
542 vlVaContext *context;
543 vlVaBuffer *coded_buf;
544 vlVaSurface *surf;
545 void *feedback;
546
547 if (!ctx)
548 return VA_STATUS_ERROR_INVALID_CONTEXT;
549
550 drv = VL_VA_DRIVER(ctx);
551 if (!drv)
552 return VA_STATUS_ERROR_INVALID_CONTEXT;
553
554 pipe_mutex_lock(drv->mutex);
555 context = handle_table_get(drv->htab, context_id);
556 pipe_mutex_unlock(drv->mutex);
557 if (!context)
558 return VA_STATUS_ERROR_INVALID_CONTEXT;
559
560 if (!context->decoder) {
561 if (context->templat.profile != PIPE_VIDEO_PROFILE_UNKNOWN)
562 return VA_STATUS_ERROR_INVALID_CONTEXT;
563
564 /* VPP */
565 return VA_STATUS_SUCCESS;
566 }
567
568 pipe_mutex_lock(drv->mutex);
569 surf = handle_table_get(drv->htab, context->target_id);
570 context->mpeg4.frame_num++;
571
572 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
573 coded_buf = context->coded_buf;
574 getEncParamPreset(context);
575 context->desc.h264enc.frame_num_cnt++;
576 context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
577 context->decoder->encode_bitstream(context->decoder, context->target,
578 coded_buf->derived_surface.resource, &feedback);
579 surf->feedback = feedback;
580 surf->coded_buf = coded_buf;
581 }
582
583 context->decoder->end_frame(context->decoder, context->target, &context->desc.base);
584 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
585 int idr_period = context->desc.h264enc.gop_size / context->gop_coeff;
586 int p_remain_in_idr = idr_period - context->desc.h264enc.frame_num;
587 surf->frame_num_cnt = context->desc.h264enc.frame_num_cnt;
588 surf->force_flushed = false;
589 if (context->first_single_submitted) {
590 context->decoder->flush(context->decoder);
591 context->first_single_submitted = false;
592 surf->force_flushed = true;
593 }
594 if (p_remain_in_idr == 1) {
595 if ((context->desc.h264enc.frame_num_cnt % 2) != 0) {
596 context->decoder->flush(context->decoder);
597 context->first_single_submitted = true;
598 }
599 else
600 context->first_single_submitted = false;
601 surf->force_flushed = true;
602 }
603 }
604 pipe_mutex_unlock(drv->mutex);
605 return VA_STATUS_SUCCESS;
606 }