1 /**************************************************************************
3 * Copyright 2015 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "util/u_handle_table.h"
29 #include "util/u_memory.h"
30 #include "util/u_compute.h"
32 #include "vl/vl_defines.h"
33 #include "vl/vl_video_buffer.h"
34 #include "vl/vl_deint_filter.h"
36 #include "va_private.h"
38 static const VARectangle
*
39 vlVaRegionDefault(const VARectangle
*region
, vlVaSurface
*surf
,
47 def
->width
= surf
->templat
.width
;
48 def
->height
= surf
->templat
.height
;
54 vlVaPostProcCompositor(vlVaDriver
*drv
, vlVaContext
*context
,
55 const VARectangle
*src_region
,
56 const VARectangle
*dst_region
,
57 struct pipe_video_buffer
*src
,
58 struct pipe_video_buffer
*dst
,
59 enum vl_compositor_deinterlace deinterlace
)
61 struct pipe_surface
**surfaces
;
62 struct u_rect src_rect
;
63 struct u_rect dst_rect
;
65 surfaces
= dst
->get_surfaces(dst
);
66 if (!surfaces
|| !surfaces
[0])
67 return VA_STATUS_ERROR_INVALID_SURFACE
;
69 src_rect
.x0
= src_region
->x
;
70 src_rect
.y0
= src_region
->y
;
71 src_rect
.x1
= src_region
->x
+ src_region
->width
;
72 src_rect
.y1
= src_region
->y
+ src_region
->height
;
74 dst_rect
.x0
= dst_region
->x
;
75 dst_rect
.y0
= dst_region
->y
;
76 dst_rect
.x1
= dst_region
->x
+ dst_region
->width
;
77 dst_rect
.y1
= dst_region
->y
+ dst_region
->height
;
79 vl_compositor_clear_layers(&drv
->cstate
);
80 vl_compositor_set_buffer_layer(&drv
->cstate
, &drv
->compositor
, 0, src
,
81 &src_rect
, NULL
, deinterlace
);
82 vl_compositor_set_layer_dst_area(&drv
->cstate
, 0, &dst_rect
);
83 vl_compositor_render(&drv
->cstate
, &drv
->compositor
, surfaces
[0], NULL
, false);
85 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
86 return VA_STATUS_SUCCESS
;
89 static void vlVaGetBox(struct pipe_video_buffer
*buf
, unsigned idx
,
90 struct pipe_box
*box
, const VARectangle
*region
)
92 unsigned plane
= buf
->interlaced
? idx
/ 2: idx
;
93 unsigned x
, y
, width
, height
;
97 width
= region
->width
;
98 height
= region
->height
;
100 vl_video_buffer_adjust_size(&x
, &y
, plane
, buf
->chroma_format
,
102 vl_video_buffer_adjust_size(&width
, &height
, plane
, buf
->chroma_format
,
105 box
->x
= region
->x
< 0 ? -x
: x
;
106 box
->y
= region
->y
< 0 ? -y
: y
;
108 box
->height
= height
;
111 static VAStatus
vlVaPostProcBlit(vlVaDriver
*drv
, vlVaContext
*context
,
112 const VARectangle
*src_region
,
113 const VARectangle
*dst_region
,
114 struct pipe_video_buffer
*src
,
115 struct pipe_video_buffer
*dst
,
116 enum vl_compositor_deinterlace deinterlace
)
118 struct pipe_surface
**src_surfaces
;
119 struct pipe_surface
**dst_surfaces
;
120 struct u_rect src_rect
;
121 struct u_rect dst_rect
;
126 if ((src
->buffer_format
== PIPE_FORMAT_B8G8R8A8_UNORM
||
127 src
->buffer_format
== PIPE_FORMAT_B8G8R8X8_UNORM
) &&
131 if (src
->interlaced
!= dst
->interlaced
&& dst
->interlaced
&& !grab
)
132 return VA_STATUS_ERROR_INVALID_SURFACE
;
134 if ((src
->width
!= dst
->width
|| src
->height
!= dst
->height
) &&
135 (src
->interlaced
&& dst
->interlaced
))
138 src_surfaces
= src
->get_surfaces(src
);
139 if (!src_surfaces
|| !src_surfaces
[0])
140 return VA_STATUS_ERROR_INVALID_SURFACE
;
142 if (scale
|| (grab
&& dst
->interlaced
)) {
145 surf
= handle_table_get(drv
->htab
, context
->target_id
);
146 surf
->templat
.interlaced
= false;
149 if (vlVaHandleSurfaceAllocate(drv
, surf
, &surf
->templat
) != VA_STATUS_SUCCESS
)
150 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
152 dst
= context
->target
= surf
->buffer
;
155 dst_surfaces
= dst
->get_surfaces(dst
);
156 if (!dst_surfaces
|| !dst_surfaces
[0])
157 return VA_STATUS_ERROR_INVALID_SURFACE
;
159 src_rect
.x0
= src_region
->x
;
160 src_rect
.y0
= src_region
->y
;
161 src_rect
.x1
= src_region
->x
+ src_region
->width
;
162 src_rect
.y1
= src_region
->y
+ src_region
->height
;
164 dst_rect
.x0
= dst_region
->x
;
165 dst_rect
.y0
= dst_region
->y
;
166 dst_rect
.x1
= dst_region
->x
+ dst_region
->width
;
167 dst_rect
.y1
= dst_region
->y
+ dst_region
->height
;
170 vl_compositor_convert_rgb_to_yuv(&drv
->cstate
, &drv
->compositor
, 0,
171 ((struct vl_video_buffer
*)src
)->resources
[0],
172 dst
, &src_rect
, &dst_rect
);
174 return VA_STATUS_SUCCESS
;
177 if (src
->interlaced
!= dst
->interlaced
) {
178 vl_compositor_yuv_deint_full(&drv
->cstate
, &drv
->compositor
,
179 src
, dst
, &src_rect
, &dst_rect
,
182 return VA_STATUS_SUCCESS
;
185 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
186 struct pipe_surface
*from
= src_surfaces
[i
];
187 struct pipe_blit_info blit
;
189 if (src
->interlaced
) {
190 /* Not 100% accurate, but close enough */
191 switch (deinterlace
) {
192 case VL_COMPOSITOR_BOB_TOP
:
193 from
= src_surfaces
[i
& ~1];
195 case VL_COMPOSITOR_BOB_BOTTOM
:
196 from
= src_surfaces
[(i
& ~1) + 1];
203 if (!from
|| !dst_surfaces
[i
])
206 memset(&blit
, 0, sizeof(blit
));
207 blit
.src
.resource
= from
->texture
;
208 blit
.src
.format
= from
->format
;
210 blit
.src
.box
.z
= from
->u
.tex
.first_layer
;
211 blit
.src
.box
.depth
= 1;
212 vlVaGetBox(src
, i
, &blit
.src
.box
, src_region
);
214 blit
.dst
.resource
= dst_surfaces
[i
]->texture
;
215 blit
.dst
.format
= dst_surfaces
[i
]->format
;
217 blit
.dst
.box
.z
= dst_surfaces
[i
]->u
.tex
.first_layer
;
218 blit
.dst
.box
.depth
= 1;
219 vlVaGetBox(dst
, i
, &blit
.dst
.box
, dst_region
);
221 blit
.mask
= PIPE_MASK_RGBA
;
222 blit
.filter
= PIPE_TEX_MIPFILTER_LINEAR
;
224 if (drv
->pipe
->screen
->get_param(drv
->pipe
->screen
,
225 PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA
))
226 util_compute_blit(drv
->pipe
, &blit
, &context
->blit_cs
);
228 drv
->pipe
->blit(drv
->pipe
, &blit
);
231 // TODO: figure out why this is necessary for DMA-buf sharing
232 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
234 return VA_STATUS_SUCCESS
;
237 static struct pipe_video_buffer
*
238 vlVaApplyDeint(vlVaDriver
*drv
, vlVaContext
*context
,
239 VAProcPipelineParameterBuffer
*param
,
240 struct pipe_video_buffer
*current
,
243 vlVaSurface
*prevprev
, *prev
, *next
;
245 if (param
->num_forward_references
< 2 ||
246 param
->num_backward_references
< 1)
249 prevprev
= handle_table_get(drv
->htab
, param
->forward_references
[1]);
250 prev
= handle_table_get(drv
->htab
, param
->forward_references
[0]);
251 next
= handle_table_get(drv
->htab
, param
->backward_references
[0]);
253 if (!prevprev
|| !prev
|| !next
)
256 if (context
->deint
&& (context
->deint
->video_width
!= current
->width
||
257 context
->deint
->video_height
!= current
->height
)) {
258 vl_deint_filter_cleanup(context
->deint
);
259 FREE(context
->deint
);
260 context
->deint
= NULL
;
263 if (!context
->deint
) {
264 context
->deint
= MALLOC(sizeof(struct vl_deint_filter
));
265 if (!vl_deint_filter_init(context
->deint
, drv
->pipe
, current
->width
,
266 current
->height
, false, false)) {
267 FREE(context
->deint
);
268 context
->deint
= NULL
;
273 if (!vl_deint_filter_check_buffers(context
->deint
, prevprev
->buffer
,
274 prev
->buffer
, current
, next
->buffer
))
277 vl_deint_filter_render(context
->deint
, prevprev
->buffer
, prev
->buffer
,
278 current
, next
->buffer
, field
);
279 return context
->deint
->video_buffer
;
283 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
285 enum vl_compositor_deinterlace deinterlace
= VL_COMPOSITOR_WEAVE
;
286 VARectangle def_src_region
, def_dst_region
;
287 const VARectangle
*src_region
, *dst_region
;
288 VAProcPipelineParameterBuffer
*param
;
289 struct pipe_video_buffer
*src
;
290 vlVaSurface
*src_surface
, *dst_surface
;
293 if (!drv
|| !context
)
294 return VA_STATUS_ERROR_INVALID_CONTEXT
;
296 if (!buf
|| !buf
->data
)
297 return VA_STATUS_ERROR_INVALID_BUFFER
;
299 if (!context
->target
)
300 return VA_STATUS_ERROR_INVALID_SURFACE
;
304 src_surface
= handle_table_get(drv
->htab
, param
->surface
);
305 dst_surface
= handle_table_get(drv
->htab
, context
->target_id
);
307 if (!src_surface
|| !src_surface
->buffer
)
308 return VA_STATUS_ERROR_INVALID_SURFACE
;
310 src
= src_surface
->buffer
;
312 for (i
= 0; i
< param
->num_filters
; i
++) {
313 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, param
->filters
[i
]);
314 VAProcFilterParameterBufferBase
*filter
;
316 if (!buf
|| buf
->type
!= VAProcFilterParameterBufferType
)
317 return VA_STATUS_ERROR_INVALID_BUFFER
;
320 switch (filter
->type
) {
321 case VAProcFilterDeinterlacing
: {
322 VAProcFilterParameterBufferDeinterlacing
*deint
= buf
->data
;
323 switch (deint
->algorithm
) {
324 case VAProcDeinterlacingBob
:
325 if (deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD
)
326 deinterlace
= VL_COMPOSITOR_BOB_BOTTOM
;
328 deinterlace
= VL_COMPOSITOR_BOB_TOP
;
331 case VAProcDeinterlacingWeave
:
332 deinterlace
= VL_COMPOSITOR_WEAVE
;
335 case VAProcDeinterlacingMotionAdaptive
:
336 src
= vlVaApplyDeint(drv
, context
, param
, src
,
337 !!(deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD
));
341 return VA_STATUS_ERROR_UNIMPLEMENTED
;
348 return VA_STATUS_ERROR_UNIMPLEMENTED
;
352 src_region
= vlVaRegionDefault(param
->surface_region
, src_surface
, &def_src_region
);
353 dst_region
= vlVaRegionDefault(param
->output_region
, dst_surface
, &def_dst_region
);
355 if (context
->target
->buffer_format
!= PIPE_FORMAT_NV12
&&
356 context
->target
->buffer_format
!= PIPE_FORMAT_P016
)
357 return vlVaPostProcCompositor(drv
, context
, src_region
, dst_region
,
358 src
, context
->target
, deinterlace
);
360 return vlVaPostProcBlit(drv
, context
, src_region
, dst_region
,
361 src
, context
->target
, deinterlace
);