17ec681f3Smrg/************************************************************************** 27ec681f3Smrg * 37ec681f3Smrg * Copyright 2015 Advanced Micro Devices, Inc. 47ec681f3Smrg * All Rights Reserved. 57ec681f3Smrg * 67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 77ec681f3Smrg * copy of this software and associated documentation files (the 87ec681f3Smrg * "Software"), to deal in the Software without restriction, including 97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish, 107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to 117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to 127ec681f3Smrg * the following conditions: 137ec681f3Smrg * 147ec681f3Smrg * The above copyright notice and this permission notice (including the 157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions 167ec681f3Smrg * of the Software. 177ec681f3Smrg * 187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 217ec681f3Smrg * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR 227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 257ec681f3Smrg * 267ec681f3Smrg **************************************************************************/ 277ec681f3Smrg 287ec681f3Smrg#include "util/u_handle_table.h" 297ec681f3Smrg#include "util/u_memory.h" 307ec681f3Smrg#include "util/u_compute.h" 317ec681f3Smrg 327ec681f3Smrg#include "vl/vl_defines.h" 337ec681f3Smrg#include "vl/vl_video_buffer.h" 347ec681f3Smrg#include "vl/vl_deint_filter.h" 357ec681f3Smrg 367ec681f3Smrg#include "va_private.h" 377ec681f3Smrg 387ec681f3Smrgstatic const VARectangle * 397ec681f3SmrgvlVaRegionDefault(const VARectangle *region, vlVaSurface *surf, 407ec681f3Smrg VARectangle *def) 417ec681f3Smrg{ 427ec681f3Smrg if (region) 437ec681f3Smrg return region; 447ec681f3Smrg 457ec681f3Smrg def->x = 0; 467ec681f3Smrg def->y = 0; 477ec681f3Smrg def->width = surf->templat.width; 487ec681f3Smrg def->height = surf->templat.height; 497ec681f3Smrg 507ec681f3Smrg return def; 517ec681f3Smrg} 527ec681f3Smrg 537ec681f3Smrgstatic VAStatus 547ec681f3SmrgvlVaPostProcCompositor(vlVaDriver *drv, vlVaContext *context, 557ec681f3Smrg const VARectangle *src_region, 567ec681f3Smrg const VARectangle *dst_region, 577ec681f3Smrg struct pipe_video_buffer *src, 587ec681f3Smrg struct pipe_video_buffer *dst, 597ec681f3Smrg enum vl_compositor_deinterlace deinterlace) 607ec681f3Smrg{ 617ec681f3Smrg struct pipe_surface **surfaces; 627ec681f3Smrg struct u_rect src_rect; 637ec681f3Smrg struct u_rect dst_rect; 647ec681f3Smrg 657ec681f3Smrg surfaces = dst->get_surfaces(dst); 667ec681f3Smrg if (!surfaces || !surfaces[0]) 677ec681f3Smrg return VA_STATUS_ERROR_INVALID_SURFACE; 687ec681f3Smrg 697ec681f3Smrg src_rect.x0 = src_region->x; 707ec681f3Smrg src_rect.y0 = src_region->y; 717ec681f3Smrg src_rect.x1 = src_region->x + src_region->width; 727ec681f3Smrg src_rect.y1 = src_region->y + src_region->height; 737ec681f3Smrg 747ec681f3Smrg dst_rect.x0 = dst_region->x; 757ec681f3Smrg dst_rect.y0 = dst_region->y; 767ec681f3Smrg dst_rect.x1 = dst_region->x + dst_region->width; 777ec681f3Smrg dst_rect.y1 = dst_region->y + dst_region->height; 787ec681f3Smrg 797ec681f3Smrg vl_compositor_clear_layers(&drv->cstate); 807ec681f3Smrg vl_compositor_set_buffer_layer(&drv->cstate, &drv->compositor, 0, src, 817ec681f3Smrg &src_rect, NULL, deinterlace); 827ec681f3Smrg vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dst_rect); 837ec681f3Smrg vl_compositor_render(&drv->cstate, &drv->compositor, surfaces[0], NULL, false); 847ec681f3Smrg 857ec681f3Smrg drv->pipe->flush(drv->pipe, NULL, 0); 867ec681f3Smrg return VA_STATUS_SUCCESS; 877ec681f3Smrg} 887ec681f3Smrg 897ec681f3Smrgstatic void vlVaGetBox(struct pipe_video_buffer *buf, unsigned idx, 907ec681f3Smrg struct pipe_box *box, const VARectangle *region) 917ec681f3Smrg{ 927ec681f3Smrg unsigned plane = buf->interlaced ? idx / 2: idx; 937ec681f3Smrg unsigned x, y, width, height; 947ec681f3Smrg 957ec681f3Smrg x = abs(region->x); 967ec681f3Smrg y = abs(region->y); 977ec681f3Smrg width = region->width; 987ec681f3Smrg height = region->height; 997ec681f3Smrg 1007ec681f3Smrg vl_video_buffer_adjust_size(&x, &y, plane, 1017ec681f3Smrg pipe_format_to_chroma_format(buf->buffer_format), 1027ec681f3Smrg buf->interlaced); 1037ec681f3Smrg vl_video_buffer_adjust_size(&width, &height, plane, 1047ec681f3Smrg pipe_format_to_chroma_format(buf->buffer_format), 1057ec681f3Smrg buf->interlaced); 1067ec681f3Smrg 1077ec681f3Smrg box->x = region->x < 0 ? -x : x; 1087ec681f3Smrg box->y = region->y < 0 ? -y : y; 1097ec681f3Smrg box->width = width; 1107ec681f3Smrg box->height = height; 1117ec681f3Smrg} 1127ec681f3Smrg 1137ec681f3Smrgstatic VAStatus vlVaPostProcBlit(vlVaDriver *drv, vlVaContext *context, 1147ec681f3Smrg const VARectangle *src_region, 1157ec681f3Smrg const VARectangle *dst_region, 1167ec681f3Smrg struct pipe_video_buffer *src, 1177ec681f3Smrg struct pipe_video_buffer *dst, 1187ec681f3Smrg enum vl_compositor_deinterlace deinterlace) 1197ec681f3Smrg{ 1207ec681f3Smrg struct pipe_surface **src_surfaces; 1217ec681f3Smrg struct pipe_surface **dst_surfaces; 1227ec681f3Smrg struct u_rect src_rect; 1237ec681f3Smrg struct u_rect dst_rect; 1247ec681f3Smrg bool scale = false; 1257ec681f3Smrg bool grab = false; 1267ec681f3Smrg unsigned i; 1277ec681f3Smrg 1287ec681f3Smrg if ((src->buffer_format == PIPE_FORMAT_B8G8R8A8_UNORM || 1297ec681f3Smrg src->buffer_format == PIPE_FORMAT_B8G8R8X8_UNORM) && 1307ec681f3Smrg !src->interlaced) 1317ec681f3Smrg grab = true; 1327ec681f3Smrg 1337ec681f3Smrg if ((src->width != dst->width || src->height != dst->height) && 1347ec681f3Smrg (src->interlaced && dst->interlaced)) 1357ec681f3Smrg scale = true; 1367ec681f3Smrg 1377ec681f3Smrg src_surfaces = src->get_surfaces(src); 1387ec681f3Smrg if (!src_surfaces || !src_surfaces[0]) 1397ec681f3Smrg return VA_STATUS_ERROR_INVALID_SURFACE; 1407ec681f3Smrg 1417ec681f3Smrg if (scale || (src->interlaced != dst->interlaced && dst->interlaced)) { 1427ec681f3Smrg vlVaSurface *surf; 1437ec681f3Smrg 1447ec681f3Smrg surf = handle_table_get(drv->htab, context->target_id); 1457ec681f3Smrg surf->templat.interlaced = false; 1467ec681f3Smrg dst->destroy(dst); 1477ec681f3Smrg 1487ec681f3Smrg if (vlVaHandleSurfaceAllocate(drv, surf, &surf->templat, NULL, 0) != VA_STATUS_SUCCESS) 1497ec681f3Smrg return VA_STATUS_ERROR_ALLOCATION_FAILED; 1507ec681f3Smrg 1517ec681f3Smrg dst = context->target = surf->buffer; 1527ec681f3Smrg } 1537ec681f3Smrg 1547ec681f3Smrg dst_surfaces = dst->get_surfaces(dst); 1557ec681f3Smrg if (!dst_surfaces || !dst_surfaces[0]) 1567ec681f3Smrg return VA_STATUS_ERROR_INVALID_SURFACE; 1577ec681f3Smrg 1587ec681f3Smrg src_rect.x0 = src_region->x; 1597ec681f3Smrg src_rect.y0 = src_region->y; 1607ec681f3Smrg src_rect.x1 = src_region->x + src_region->width; 1617ec681f3Smrg src_rect.y1 = src_region->y + src_region->height; 1627ec681f3Smrg 1637ec681f3Smrg dst_rect.x0 = dst_region->x; 1647ec681f3Smrg dst_rect.y0 = dst_region->y; 1657ec681f3Smrg dst_rect.x1 = dst_region->x + dst_region->width; 1667ec681f3Smrg dst_rect.y1 = dst_region->y + dst_region->height; 1677ec681f3Smrg 1687ec681f3Smrg if (grab) { 1697ec681f3Smrg vl_compositor_convert_rgb_to_yuv(&drv->cstate, &drv->compositor, 0, 1707ec681f3Smrg ((struct vl_video_buffer *)src)->resources[0], 1717ec681f3Smrg dst, &src_rect, &dst_rect); 1727ec681f3Smrg 1737ec681f3Smrg return VA_STATUS_SUCCESS; 1747ec681f3Smrg } 1757ec681f3Smrg 1767ec681f3Smrg if (src->interlaced != dst->interlaced) { 1777ec681f3Smrg vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor, 1787ec681f3Smrg src, dst, &src_rect, &dst_rect, 1797ec681f3Smrg deinterlace); 1807ec681f3Smrg 1817ec681f3Smrg return VA_STATUS_SUCCESS; 1827ec681f3Smrg } 1837ec681f3Smrg 1847ec681f3Smrg for (i = 0; i < VL_MAX_SURFACES; ++i) { 1857ec681f3Smrg struct pipe_surface *from = src_surfaces[i]; 1867ec681f3Smrg struct pipe_blit_info blit; 1877ec681f3Smrg 1887ec681f3Smrg if (src->interlaced) { 1897ec681f3Smrg /* Not 100% accurate, but close enough */ 1907ec681f3Smrg switch (deinterlace) { 1917ec681f3Smrg case VL_COMPOSITOR_BOB_TOP: 1927ec681f3Smrg from = src_surfaces[i & ~1]; 1937ec681f3Smrg break; 1947ec681f3Smrg case VL_COMPOSITOR_BOB_BOTTOM: 1957ec681f3Smrg from = src_surfaces[(i & ~1) + 1]; 1967ec681f3Smrg break; 1977ec681f3Smrg default: 1987ec681f3Smrg break; 1997ec681f3Smrg } 2007ec681f3Smrg } 2017ec681f3Smrg 2027ec681f3Smrg if (!from || !dst_surfaces[i]) 2037ec681f3Smrg continue; 2047ec681f3Smrg 2057ec681f3Smrg memset(&blit, 0, sizeof(blit)); 2067ec681f3Smrg blit.src.resource = from->texture; 2077ec681f3Smrg blit.src.format = from->format; 2087ec681f3Smrg blit.src.level = 0; 2097ec681f3Smrg blit.src.box.z = from->u.tex.first_layer; 2107ec681f3Smrg blit.src.box.depth = 1; 2117ec681f3Smrg vlVaGetBox(src, i, &blit.src.box, src_region); 2127ec681f3Smrg 2137ec681f3Smrg blit.dst.resource = dst_surfaces[i]->texture; 2147ec681f3Smrg blit.dst.format = dst_surfaces[i]->format; 2157ec681f3Smrg blit.dst.level = 0; 2167ec681f3Smrg blit.dst.box.z = dst_surfaces[i]->u.tex.first_layer; 2177ec681f3Smrg blit.dst.box.depth = 1; 2187ec681f3Smrg vlVaGetBox(dst, i, &blit.dst.box, dst_region); 2197ec681f3Smrg 2207ec681f3Smrg blit.mask = PIPE_MASK_RGBA; 2217ec681f3Smrg blit.filter = PIPE_TEX_MIPFILTER_LINEAR; 2227ec681f3Smrg 2237ec681f3Smrg if (drv->pipe->screen->get_param(drv->pipe->screen, 2247ec681f3Smrg PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA)) 2257ec681f3Smrg util_compute_blit(drv->pipe, &blit, &context->blit_cs, !drv->compositor.deinterlace); 2267ec681f3Smrg else 2277ec681f3Smrg drv->pipe->blit(drv->pipe, &blit); 2287ec681f3Smrg } 2297ec681f3Smrg 2307ec681f3Smrg // TODO: figure out why this is necessary for DMA-buf sharing 2317ec681f3Smrg drv->pipe->flush(drv->pipe, NULL, 0); 2327ec681f3Smrg 2337ec681f3Smrg return VA_STATUS_SUCCESS; 2347ec681f3Smrg} 2357ec681f3Smrg 2367ec681f3Smrgstatic struct pipe_video_buffer * 2377ec681f3SmrgvlVaApplyDeint(vlVaDriver *drv, vlVaContext *context, 2387ec681f3Smrg VAProcPipelineParameterBuffer *param, 2397ec681f3Smrg struct pipe_video_buffer *current, 2407ec681f3Smrg unsigned field) 2417ec681f3Smrg{ 2427ec681f3Smrg vlVaSurface *prevprev, *prev, *next; 2437ec681f3Smrg 2447ec681f3Smrg if (param->num_forward_references < 2 || 2457ec681f3Smrg param->num_backward_references < 1) 2467ec681f3Smrg return current; 2477ec681f3Smrg 2487ec681f3Smrg prevprev = handle_table_get(drv->htab, param->forward_references[1]); 2497ec681f3Smrg prev = handle_table_get(drv->htab, param->forward_references[0]); 2507ec681f3Smrg next = handle_table_get(drv->htab, param->backward_references[0]); 2517ec681f3Smrg 2527ec681f3Smrg if (!prevprev || !prev || !next) 2537ec681f3Smrg return current; 2547ec681f3Smrg 2557ec681f3Smrg if (context->deint && (context->deint->video_width != current->width || 2567ec681f3Smrg context->deint->video_height != current->height)) { 2577ec681f3Smrg vl_deint_filter_cleanup(context->deint); 2587ec681f3Smrg FREE(context->deint); 2597ec681f3Smrg context->deint = NULL; 2607ec681f3Smrg } 2617ec681f3Smrg 2627ec681f3Smrg if (!context->deint) { 2637ec681f3Smrg context->deint = MALLOC(sizeof(struct vl_deint_filter)); 2647ec681f3Smrg if (!vl_deint_filter_init(context->deint, drv->pipe, current->width, 2657ec681f3Smrg current->height, false, false)) { 2667ec681f3Smrg FREE(context->deint); 2677ec681f3Smrg context->deint = NULL; 2687ec681f3Smrg return current; 2697ec681f3Smrg } 2707ec681f3Smrg } 2717ec681f3Smrg 2727ec681f3Smrg if (!vl_deint_filter_check_buffers(context->deint, prevprev->buffer, 2737ec681f3Smrg prev->buffer, current, next->buffer)) 2747ec681f3Smrg return current; 2757ec681f3Smrg 2767ec681f3Smrg vl_deint_filter_render(context->deint, prevprev->buffer, prev->buffer, 2777ec681f3Smrg current, next->buffer, field); 2787ec681f3Smrg return context->deint->video_buffer; 2797ec681f3Smrg} 2807ec681f3Smrg 2817ec681f3SmrgVAStatus 2827ec681f3SmrgvlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf) 2837ec681f3Smrg{ 2847ec681f3Smrg enum vl_compositor_deinterlace deinterlace = VL_COMPOSITOR_NONE; 2857ec681f3Smrg VARectangle def_src_region, def_dst_region; 2867ec681f3Smrg const VARectangle *src_region, *dst_region; 2877ec681f3Smrg VAProcPipelineParameterBuffer *param; 2887ec681f3Smrg struct pipe_video_buffer *src, *dst; 2897ec681f3Smrg vlVaSurface *src_surface, *dst_surface; 2907ec681f3Smrg unsigned i; 2917ec681f3Smrg 2927ec681f3Smrg if (!drv || !context) 2937ec681f3Smrg return VA_STATUS_ERROR_INVALID_CONTEXT; 2947ec681f3Smrg 2957ec681f3Smrg if (!buf || !buf->data) 2967ec681f3Smrg return VA_STATUS_ERROR_INVALID_BUFFER; 2977ec681f3Smrg 2987ec681f3Smrg if (!context->target) 2997ec681f3Smrg return VA_STATUS_ERROR_INVALID_SURFACE; 3007ec681f3Smrg 3017ec681f3Smrg param = buf->data; 3027ec681f3Smrg 3037ec681f3Smrg src_surface = handle_table_get(drv->htab, param->surface); 3047ec681f3Smrg dst_surface = handle_table_get(drv->htab, context->target_id); 3057ec681f3Smrg 3067ec681f3Smrg if (!src_surface || !src_surface->buffer) 3077ec681f3Smrg return VA_STATUS_ERROR_INVALID_SURFACE; 3087ec681f3Smrg 3097ec681f3Smrg src = src_surface->buffer; 3107ec681f3Smrg dst = dst_surface->buffer; 3117ec681f3Smrg 3127ec681f3Smrg /* convert the destination buffer to progressive if we're deinterlacing 3137ec681f3Smrg otherwise we might end up deinterlacing twice */ 3147ec681f3Smrg if (param->num_filters && dst->interlaced) { 3157ec681f3Smrg vlVaSurface *surf; 3167ec681f3Smrg surf = dst_surface; 3177ec681f3Smrg surf->templat.interlaced = false; 3187ec681f3Smrg dst->destroy(dst); 3197ec681f3Smrg 3207ec681f3Smrg if (vlVaHandleSurfaceAllocate(drv, surf, &surf->templat, NULL, 0) != VA_STATUS_SUCCESS) 3217ec681f3Smrg return VA_STATUS_ERROR_ALLOCATION_FAILED; 3227ec681f3Smrg 3237ec681f3Smrg dst = context->target = surf->buffer; 3247ec681f3Smrg } 3257ec681f3Smrg 3267ec681f3Smrg for (i = 0; i < param->num_filters; i++) { 3277ec681f3Smrg vlVaBuffer *buf = handle_table_get(drv->htab, param->filters[i]); 3287ec681f3Smrg VAProcFilterParameterBufferBase *filter; 3297ec681f3Smrg 3307ec681f3Smrg if (!buf || buf->type != VAProcFilterParameterBufferType) 3317ec681f3Smrg return VA_STATUS_ERROR_INVALID_BUFFER; 3327ec681f3Smrg 3337ec681f3Smrg filter = buf->data; 3347ec681f3Smrg switch (filter->type) { 3357ec681f3Smrg case VAProcFilterDeinterlacing: { 3367ec681f3Smrg VAProcFilterParameterBufferDeinterlacing *deint = buf->data; 3377ec681f3Smrg switch (deint->algorithm) { 3387ec681f3Smrg case VAProcDeinterlacingBob: 3397ec681f3Smrg if (deint->flags & VA_DEINTERLACING_BOTTOM_FIELD) 3407ec681f3Smrg deinterlace = VL_COMPOSITOR_BOB_BOTTOM; 3417ec681f3Smrg else 3427ec681f3Smrg deinterlace = VL_COMPOSITOR_BOB_TOP; 3437ec681f3Smrg break; 3447ec681f3Smrg 3457ec681f3Smrg case VAProcDeinterlacingWeave: 3467ec681f3Smrg deinterlace = VL_COMPOSITOR_WEAVE; 3477ec681f3Smrg break; 3487ec681f3Smrg 3497ec681f3Smrg case VAProcDeinterlacingMotionAdaptive: 3507ec681f3Smrg src = vlVaApplyDeint(drv, context, param, src, 3517ec681f3Smrg !!(deint->flags & VA_DEINTERLACING_BOTTOM_FIELD)); 3527ec681f3Smrg deinterlace = VL_COMPOSITOR_MOTION_ADAPTIVE; 3537ec681f3Smrg break; 3547ec681f3Smrg 3557ec681f3Smrg default: 3567ec681f3Smrg return VA_STATUS_ERROR_UNIMPLEMENTED; 3577ec681f3Smrg } 3587ec681f3Smrg drv->compositor.deinterlace = deinterlace; 3597ec681f3Smrg break; 3607ec681f3Smrg } 3617ec681f3Smrg 3627ec681f3Smrg default: 3637ec681f3Smrg return VA_STATUS_ERROR_UNIMPLEMENTED; 3647ec681f3Smrg } 3657ec681f3Smrg } 3667ec681f3Smrg 3677ec681f3Smrg src_region = vlVaRegionDefault(param->surface_region, src_surface, &def_src_region); 3687ec681f3Smrg dst_region = vlVaRegionDefault(param->output_region, dst_surface, &def_dst_region); 3697ec681f3Smrg 3707ec681f3Smrg if (context->target->buffer_format != PIPE_FORMAT_NV12 && 3717ec681f3Smrg context->target->buffer_format != PIPE_FORMAT_P010 && 3727ec681f3Smrg context->target->buffer_format != PIPE_FORMAT_P016) 3737ec681f3Smrg return vlVaPostProcCompositor(drv, context, src_region, dst_region, 3747ec681f3Smrg src, context->target, deinterlace); 3757ec681f3Smrg else 3767ec681f3Smrg return vlVaPostProcBlit(drv, context, src_region, dst_region, 3777ec681f3Smrg src, context->target, deinterlace); 3787ec681f3Smrg} 379