gallium: rename 'state tracker' to 'frontend'
[mesa.git] / src / gallium / frontends / 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 mtx_lock(&drv->mutex);
54 context = handle_table_get(drv->htab, context_id);
55 if (!context) {
56 mtx_unlock(&drv->mutex);
57 return VA_STATUS_ERROR_INVALID_CONTEXT;
58 }
59
60 if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_MPEG12) {
61 context->desc.mpeg12.intra_matrix = NULL;
62 context->desc.mpeg12.non_intra_matrix = NULL;
63 }
64
65 surf = handle_table_get(drv->htab, render_target);
66 mtx_unlock(&drv->mutex);
67 if (!surf || !surf->buffer)
68 return VA_STATUS_ERROR_INVALID_SURFACE;
69
70 context->target_id = render_target;
71 surf->ctx = context_id;
72 context->target = surf->buffer;
73 context->mjpeg.sampling_factor = 0;
74
75 if (!context->decoder) {
76
77 /* VPP */
78 if (context->templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN &&
79 context->target->buffer_format != PIPE_FORMAT_B8G8R8A8_UNORM &&
80 context->target->buffer_format != PIPE_FORMAT_R8G8B8A8_UNORM &&
81 context->target->buffer_format != PIPE_FORMAT_B8G8R8X8_UNORM &&
82 context->target->buffer_format != PIPE_FORMAT_R8G8B8X8_UNORM &&
83 context->target->buffer_format != PIPE_FORMAT_NV12 &&
84 context->target->buffer_format != PIPE_FORMAT_P010 &&
85 context->target->buffer_format != PIPE_FORMAT_P016)
86 return VA_STATUS_ERROR_UNIMPLEMENTED;
87
88 return VA_STATUS_SUCCESS;
89 }
90
91 if (context->decoder->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE)
92 context->needs_begin_frame = true;
93
94 return VA_STATUS_SUCCESS;
95 }
96
97 void
98 vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
99 struct pipe_video_buffer **ref_frame)
100 {
101 vlVaSurface *surf = handle_table_get(drv->htab, surface_id);
102 if (surf)
103 *ref_frame = surf->buffer;
104 else
105 *ref_frame = NULL;
106 }
107
108 static VAStatus
109 handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
110 {
111 VAStatus vaStatus = VA_STATUS_SUCCESS;
112
113 switch (u_reduce_video_profile(context->templat.profile)) {
114 case PIPE_VIDEO_FORMAT_MPEG12:
115 vlVaHandlePictureParameterBufferMPEG12(drv, context, buf);
116 break;
117
118 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
119 vlVaHandlePictureParameterBufferH264(drv, context, buf);
120 break;
121
122 case PIPE_VIDEO_FORMAT_VC1:
123 vlVaHandlePictureParameterBufferVC1(drv, context, buf);
124 break;
125
126 case PIPE_VIDEO_FORMAT_MPEG4:
127 vlVaHandlePictureParameterBufferMPEG4(drv, context, buf);
128 break;
129
130 case PIPE_VIDEO_FORMAT_HEVC:
131 vlVaHandlePictureParameterBufferHEVC(drv, context, buf);
132 break;
133
134 case PIPE_VIDEO_FORMAT_JPEG:
135 vlVaHandlePictureParameterBufferMJPEG(drv, context, buf);
136 break;
137
138 case PIPE_VIDEO_FORMAT_VP9:
139 vlVaHandlePictureParameterBufferVP9(drv, context, buf);
140 break;
141
142 default:
143 break;
144 }
145
146 /* Create the decoder once max_references is known. */
147 if (!context->decoder) {
148 enum pipe_video_format format =
149 u_reduce_video_profile(context->templat.profile);
150
151 if (!context->target)
152 return VA_STATUS_ERROR_INVALID_CONTEXT;
153
154 if (context->templat.max_references == 0 &&
155 format != PIPE_VIDEO_FORMAT_JPEG)
156 return VA_STATUS_ERROR_INVALID_BUFFER;
157
158 if (format == PIPE_VIDEO_FORMAT_MPEG4_AVC)
159 context->templat.level = u_get_h264_level(context->templat.width,
160 context->templat.height, &context->templat.max_references);
161
162 context->decoder = drv->pipe->create_video_codec(drv->pipe,
163 &context->templat);
164
165 if (!context->decoder)
166 return VA_STATUS_ERROR_ALLOCATION_FAILED;
167
168 context->needs_begin_frame = true;
169 }
170
171 return vaStatus;
172 }
173
174 static void
175 handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
176 {
177 switch (u_reduce_video_profile(context->templat.profile)) {
178 case PIPE_VIDEO_FORMAT_MPEG12:
179 vlVaHandleIQMatrixBufferMPEG12(context, buf);
180 break;
181
182 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
183 vlVaHandleIQMatrixBufferH264(context, buf);
184 break;
185
186 case PIPE_VIDEO_FORMAT_MPEG4:
187 vlVaHandleIQMatrixBufferMPEG4(context, buf);
188 break;
189
190 case PIPE_VIDEO_FORMAT_HEVC:
191 vlVaHandleIQMatrixBufferHEVC(context, buf);
192 break;
193
194 case PIPE_VIDEO_FORMAT_JPEG:
195 vlVaHandleIQMatrixBufferMJPEG(context, buf);
196 break;
197
198 default:
199 break;
200 }
201 }
202
203 static void
204 handleSliceParameterBuffer(vlVaContext *context, vlVaBuffer *buf)
205 {
206 switch (u_reduce_video_profile(context->templat.profile)) {
207 case PIPE_VIDEO_FORMAT_MPEG12:
208 vlVaHandleSliceParameterBufferMPEG12(context, buf);
209 break;
210
211 case PIPE_VIDEO_FORMAT_VC1:
212 vlVaHandleSliceParameterBufferVC1(context, buf);
213 break;
214
215 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
216 vlVaHandleSliceParameterBufferH264(context, buf);
217 break;
218
219 case PIPE_VIDEO_FORMAT_MPEG4:
220 vlVaHandleSliceParameterBufferMPEG4(context, buf);
221 break;
222
223 case PIPE_VIDEO_FORMAT_HEVC:
224 vlVaHandleSliceParameterBufferHEVC(context, buf);
225 break;
226
227 case PIPE_VIDEO_FORMAT_JPEG:
228 vlVaHandleSliceParameterBufferMJPEG(context, buf);
229 break;
230
231 case PIPE_VIDEO_FORMAT_VP9:
232 vlVaHandleSliceParameterBufferVP9(context, buf);
233 break;
234
235 default:
236 break;
237 }
238 }
239
240 static unsigned int
241 bufHasStartcode(vlVaBuffer *buf, unsigned int code, unsigned int bits)
242 {
243 struct vl_vlc vlc = {0};
244 int i;
245
246 /* search the first 64 bytes for a startcode */
247 vl_vlc_init(&vlc, 1, (const void * const*)&buf->data, &buf->size);
248 for (i = 0; i < 64 && vl_vlc_bits_left(&vlc) >= bits; ++i) {
249 if (vl_vlc_peekbits(&vlc, bits) == code)
250 return 1;
251 vl_vlc_eatbits(&vlc, 8);
252 vl_vlc_fillbits(&vlc);
253 }
254
255 return 0;
256 }
257
258 static void
259 handleVASliceDataBufferType(vlVaContext *context, vlVaBuffer *buf)
260 {
261 enum pipe_video_format format;
262 unsigned num_buffers = 0;
263 void * const *buffers[3];
264 unsigned sizes[3];
265 static const uint8_t start_code_h264[] = { 0x00, 0x00, 0x01 };
266 static const uint8_t start_code_h265[] = { 0x00, 0x00, 0x01 };
267 static const uint8_t start_code_vc1[] = { 0x00, 0x00, 0x01, 0x0d };
268 static const uint8_t eoi_jpeg[] = { 0xff, 0xd9 };
269
270 format = u_reduce_video_profile(context->templat.profile);
271 switch (format) {
272 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
273 if (bufHasStartcode(buf, 0x000001, 24))
274 break;
275
276 buffers[num_buffers] = (void *const)&start_code_h264;
277 sizes[num_buffers++] = sizeof(start_code_h264);
278 break;
279 case PIPE_VIDEO_FORMAT_HEVC:
280 if (bufHasStartcode(buf, 0x000001, 24))
281 break;
282
283 buffers[num_buffers] = (void *const)&start_code_h265;
284 sizes[num_buffers++] = sizeof(start_code_h265);
285 break;
286 case PIPE_VIDEO_FORMAT_VC1:
287 if (bufHasStartcode(buf, 0x0000010d, 32) ||
288 bufHasStartcode(buf, 0x0000010c, 32) ||
289 bufHasStartcode(buf, 0x0000010b, 32))
290 break;
291
292 if (context->decoder->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED) {
293 buffers[num_buffers] = (void *const)&start_code_vc1;
294 sizes[num_buffers++] = sizeof(start_code_vc1);
295 }
296 break;
297 case PIPE_VIDEO_FORMAT_MPEG4:
298 if (bufHasStartcode(buf, 0x000001, 24))
299 break;
300
301 vlVaDecoderFixMPEG4Startcode(context);
302 buffers[num_buffers] = (void *)context->mpeg4.start_code;
303 sizes[num_buffers++] = context->mpeg4.start_code_size;
304 break;
305 case PIPE_VIDEO_FORMAT_JPEG:
306 vlVaGetJpegSliceHeader(context);
307 buffers[num_buffers] = (void *)context->mjpeg.slice_header;
308 sizes[num_buffers++] = context->mjpeg.slice_header_size;
309 break;
310 case PIPE_VIDEO_FORMAT_VP9:
311 vlVaDecoderVP9BitstreamHeader(context, buf);
312 break;
313 default:
314 break;
315 }
316
317 buffers[num_buffers] = buf->data;
318 sizes[num_buffers] = buf->size;
319 ++num_buffers;
320
321 if (format == PIPE_VIDEO_FORMAT_JPEG) {
322 buffers[num_buffers] = (void *const)&eoi_jpeg;
323 sizes[num_buffers++] = sizeof(eoi_jpeg);
324 }
325
326 if (context->needs_begin_frame) {
327 context->decoder->begin_frame(context->decoder, context->target,
328 &context->desc.base);
329 context->needs_begin_frame = false;
330 }
331 context->decoder->decode_bitstream(context->decoder, context->target, &context->desc.base,
332 num_buffers, (const void * const*)buffers, sizes);
333 }
334
335 static VAStatus
336 handleVAEncMiscParameterTypeRateControl(vlVaContext *context, VAEncMiscParameterBuffer *misc)
337 {
338 VAStatus status = VA_STATUS_SUCCESS;
339
340 switch (u_reduce_video_profile(context->templat.profile)) {
341 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
342 status = vlVaHandleVAEncMiscParameterTypeRateControlH264(context, misc);
343 break;
344
345 case PIPE_VIDEO_FORMAT_HEVC:
346 status = vlVaHandleVAEncMiscParameterTypeRateControlHEVC(context, misc);
347 break;
348
349 default:
350 break;
351 }
352
353 return status;
354 }
355
356 static VAStatus
357 handleVAEncMiscParameterTypeFrameRate(vlVaContext *context, VAEncMiscParameterBuffer *misc)
358 {
359 VAStatus status = VA_STATUS_SUCCESS;
360
361 switch (u_reduce_video_profile(context->templat.profile)) {
362 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
363 status = vlVaHandleVAEncMiscParameterTypeFrameRateH264(context, misc);
364 break;
365
366 case PIPE_VIDEO_FORMAT_HEVC:
367 status = vlVaHandleVAEncMiscParameterTypeFrameRateHEVC(context, misc);
368 break;
369
370 default:
371 break;
372 }
373
374 return status;
375 }
376
377 static VAStatus
378 handleVAEncSequenceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
379 {
380 VAStatus status = VA_STATUS_SUCCESS;
381
382 switch (u_reduce_video_profile(context->templat.profile)) {
383 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
384 status = vlVaHandleVAEncSequenceParameterBufferTypeH264(drv, context, buf);
385 break;
386
387 case PIPE_VIDEO_FORMAT_HEVC:
388 status = vlVaHandleVAEncSequenceParameterBufferTypeHEVC(drv, context, buf);
389 break;
390
391 default:
392 break;
393 }
394
395 return status;
396 }
397
398 static VAStatus
399 handleVAEncMiscParameterBufferType(vlVaContext *context, vlVaBuffer *buf)
400 {
401 VAStatus vaStatus = VA_STATUS_SUCCESS;
402 VAEncMiscParameterBuffer *misc;
403 misc = buf->data;
404
405 switch (misc->type) {
406 case VAEncMiscParameterTypeRateControl:
407 vaStatus = handleVAEncMiscParameterTypeRateControl(context, misc);
408 break;
409
410 case VAEncMiscParameterTypeFrameRate:
411 vaStatus = handleVAEncMiscParameterTypeFrameRate(context, misc);
412 break;
413
414 default:
415 break;
416 }
417
418 return vaStatus;
419 }
420
421 static VAStatus
422 handleVAEncPictureParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
423 {
424 VAStatus status = VA_STATUS_SUCCESS;
425
426 switch (u_reduce_video_profile(context->templat.profile)) {
427 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
428 status = vlVaHandleVAEncPictureParameterBufferTypeH264(drv, context, buf);
429 break;
430
431 case PIPE_VIDEO_FORMAT_HEVC:
432 status = vlVaHandleVAEncPictureParameterBufferTypeHEVC(drv, context, buf);
433 break;
434
435 default:
436 break;
437 }
438
439 return status;
440 }
441
442 static VAStatus
443 handleVAEncSliceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
444 {
445 VAStatus status = VA_STATUS_SUCCESS;
446
447 switch (u_reduce_video_profile(context->templat.profile)) {
448 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
449 status = vlVaHandleVAEncSliceParameterBufferTypeH264(drv, context, buf);
450 break;
451
452 case PIPE_VIDEO_FORMAT_HEVC:
453 status = vlVaHandleVAEncSliceParameterBufferTypeHEVC(drv, context, buf);
454 break;
455
456 default:
457 break;
458 }
459
460 return status;
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 mtx_lock(&drv->mutex);
480 context = handle_table_get(drv->htab, context_id);
481 if (!context) {
482 mtx_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 mtx_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 case VAHuffmanTableBufferType:
530 vlVaHandleHuffmanTableBufferType(context, buf);
531 break;
532
533 default:
534 break;
535 }
536 }
537 mtx_unlock(&drv->mutex);
538
539 return vaStatus;
540 }
541
542 VAStatus
543 vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
544 {
545 vlVaDriver *drv;
546 vlVaContext *context;
547 vlVaBuffer *coded_buf;
548 vlVaSurface *surf;
549 void *feedback;
550 struct pipe_screen *screen;
551 bool supported;
552 bool realloc = false;
553 enum pipe_format format;
554
555 if (!ctx)
556 return VA_STATUS_ERROR_INVALID_CONTEXT;
557
558 drv = VL_VA_DRIVER(ctx);
559 if (!drv)
560 return VA_STATUS_ERROR_INVALID_CONTEXT;
561
562 mtx_lock(&drv->mutex);
563 context = handle_table_get(drv->htab, context_id);
564 mtx_unlock(&drv->mutex);
565 if (!context)
566 return VA_STATUS_ERROR_INVALID_CONTEXT;
567
568 if (!context->decoder) {
569 if (context->templat.profile != PIPE_VIDEO_PROFILE_UNKNOWN)
570 return VA_STATUS_ERROR_INVALID_CONTEXT;
571
572 /* VPP */
573 return VA_STATUS_SUCCESS;
574 }
575
576 mtx_lock(&drv->mutex);
577 surf = handle_table_get(drv->htab, context->target_id);
578 context->mpeg4.frame_num++;
579
580 screen = context->decoder->context->screen;
581 supported = screen->get_video_param(screen, context->decoder->profile,
582 context->decoder->entrypoint,
583 surf->buffer->interlaced ?
584 PIPE_VIDEO_CAP_SUPPORTS_INTERLACED :
585 PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE);
586
587 if (!supported) {
588 surf->templat.interlaced = screen->get_video_param(screen,
589 context->decoder->profile,
590 context->decoder->entrypoint,
591 PIPE_VIDEO_CAP_PREFERS_INTERLACED);
592 realloc = true;
593 }
594
595 format = screen->get_video_param(screen, context->decoder->profile,
596 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
597 PIPE_VIDEO_CAP_PREFERED_FORMAT);
598
599 if (surf->buffer->buffer_format != format &&
600 surf->buffer->buffer_format == PIPE_FORMAT_NV12) {
601 /* check originally as NV12 only */
602 surf->templat.buffer_format = format;
603 realloc = true;
604 }
605
606 if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_JPEG &&
607 surf->buffer->buffer_format == PIPE_FORMAT_NV12) {
608 if (context->mjpeg.sampling_factor == 0x211111 ||
609 context->mjpeg.sampling_factor == 0x221212) {
610 surf->templat.buffer_format = PIPE_FORMAT_YUYV;
611 realloc = true;
612 } else if (context->mjpeg.sampling_factor != 0x221111) {
613 /* Not NV12 either */
614 mtx_unlock(&drv->mutex);
615 return VA_STATUS_ERROR_INVALID_SURFACE;
616 }
617 }
618
619 if (realloc) {
620 struct pipe_video_buffer *old_buf = surf->buffer;
621
622 if (vlVaHandleSurfaceAllocate(drv, surf, &surf->templat) != VA_STATUS_SUCCESS) {
623 mtx_unlock(&drv->mutex);
624 return VA_STATUS_ERROR_ALLOCATION_FAILED;
625 }
626
627 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
628 if (old_buf->interlaced) {
629 struct u_rect src_rect, dst_rect;
630
631 dst_rect.x0 = src_rect.x0 = 0;
632 dst_rect.y0 = src_rect.y0 = 0;
633 dst_rect.x1 = src_rect.x1 = surf->templat.width;
634 dst_rect.y1 = src_rect.y1 = surf->templat.height;
635 vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor,
636 old_buf, surf->buffer,
637 &src_rect, &dst_rect, VL_COMPOSITOR_WEAVE);
638 } else {
639 /* Can't convert from progressive to interlaced yet */
640 mtx_unlock(&drv->mutex);
641 return VA_STATUS_ERROR_INVALID_SURFACE;
642 }
643 }
644
645 old_buf->destroy(old_buf);
646 context->target = surf->buffer;
647 }
648
649 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
650 coded_buf = context->coded_buf;
651 if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
652 getEncParamPresetH264(context);
653 context->desc.h264enc.frame_num_cnt++;
654 } else if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_HEVC)
655 getEncParamPresetH265(context);
656 context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
657 context->decoder->encode_bitstream(context->decoder, context->target,
658 coded_buf->derived_surface.resource, &feedback);
659 surf->feedback = feedback;
660 surf->coded_buf = coded_buf;
661 }
662
663 context->decoder->end_frame(context->decoder, context->target, &context->desc.base);
664 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE &&
665 u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
666 int idr_period = context->desc.h264enc.gop_size / context->gop_coeff;
667 int p_remain_in_idr = idr_period - context->desc.h264enc.frame_num;
668 surf->frame_num_cnt = context->desc.h264enc.frame_num_cnt;
669 surf->force_flushed = false;
670 if (context->first_single_submitted) {
671 context->decoder->flush(context->decoder);
672 context->first_single_submitted = false;
673 surf->force_flushed = true;
674 }
675 if (p_remain_in_idr == 1) {
676 if ((context->desc.h264enc.frame_num_cnt % 2) != 0) {
677 context->decoder->flush(context->decoder);
678 context->first_single_submitted = true;
679 }
680 else
681 context->first_single_submitted = false;
682 surf->force_flushed = true;
683 }
684 } else if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE &&
685 u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_HEVC)
686 context->desc.h265enc.frame_num++;
687 mtx_unlock(&drv->mutex);
688 return VA_STATUS_SUCCESS;
689 }