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
,
101 pipe_format_to_chroma_format(buf
->buffer_format
),
103 vl_video_buffer_adjust_size(&width
, &height
, plane
,
104 pipe_format_to_chroma_format(buf
->buffer_format
),
107 box
->x
= region
->x
< 0 ? -x
: x
;
108 box
->y
= region
->y
< 0 ? -y
: y
;
110 box
->height
= height
;
113 static VAStatus
vlVaPostProcBlit(vlVaDriver
*drv
, vlVaContext
*context
,
114 const VARectangle
*src_region
,
115 const VARectangle
*dst_region
,
116 struct pipe_video_buffer
*src
,
117 struct pipe_video_buffer
*dst
,
118 enum vl_compositor_deinterlace deinterlace
)
120 struct pipe_surface
**src_surfaces
;
121 struct pipe_surface
**dst_surfaces
;
122 struct u_rect src_rect
;
123 struct u_rect dst_rect
;
128 if ((src
->buffer_format
== PIPE_FORMAT_B8G8R8A8_UNORM
||
129 src
->buffer_format
== PIPE_FORMAT_B8G8R8X8_UNORM
) &&
133 if ((src
->width
!= dst
->width
|| src
->height
!= dst
->height
) &&
134 (src
->interlaced
&& dst
->interlaced
))
137 src_surfaces
= src
->get_surfaces(src
);
138 if (!src_surfaces
|| !src_surfaces
[0])
139 return VA_STATUS_ERROR_INVALID_SURFACE
;
141 if (scale
|| (src
->interlaced
!= dst
->interlaced
&& dst
->interlaced
)) {
144 surf
= handle_table_get(drv
->htab
, context
->target_id
);
145 surf
->templat
.interlaced
= false;
148 if (vlVaHandleSurfaceAllocate(drv
, surf
, &surf
->templat
) != VA_STATUS_SUCCESS
)
149 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
151 dst
= context
->target
= surf
->buffer
;
154 dst_surfaces
= dst
->get_surfaces(dst
);
155 if (!dst_surfaces
|| !dst_surfaces
[0])
156 return VA_STATUS_ERROR_INVALID_SURFACE
;
158 src_rect
.x0
= src_region
->x
;
159 src_rect
.y0
= src_region
->y
;
160 src_rect
.x1
= src_region
->x
+ src_region
->width
;
161 src_rect
.y1
= src_region
->y
+ src_region
->height
;
163 dst_rect
.x0
= dst_region
->x
;
164 dst_rect
.y0
= dst_region
->y
;
165 dst_rect
.x1
= dst_region
->x
+ dst_region
->width
;
166 dst_rect
.y1
= dst_region
->y
+ dst_region
->height
;
169 vl_compositor_convert_rgb_to_yuv(&drv
->cstate
, &drv
->compositor
, 0,
170 ((struct vl_video_buffer
*)src
)->resources
[0],
171 dst
, &src_rect
, &dst_rect
);
173 return VA_STATUS_SUCCESS
;
176 if (src
->interlaced
!= dst
->interlaced
) {
177 vl_compositor_yuv_deint_full(&drv
->cstate
, &drv
->compositor
,
178 src
, dst
, &src_rect
, &dst_rect
,
181 return VA_STATUS_SUCCESS
;
184 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
185 struct pipe_surface
*from
= src_surfaces
[i
];
186 struct pipe_blit_info blit
;
188 if (src
->interlaced
) {
189 /* Not 100% accurate, but close enough */
190 switch (deinterlace
) {
191 case VL_COMPOSITOR_BOB_TOP
:
192 from
= src_surfaces
[i
& ~1];
194 case VL_COMPOSITOR_BOB_BOTTOM
:
195 from
= src_surfaces
[(i
& ~1) + 1];
202 if (!from
|| !dst_surfaces
[i
])
205 memset(&blit
, 0, sizeof(blit
));
206 blit
.src
.resource
= from
->texture
;
207 blit
.src
.format
= from
->format
;
209 blit
.src
.box
.z
= from
->u
.tex
.first_layer
;
210 blit
.src
.box
.depth
= 1;
211 vlVaGetBox(src
, i
, &blit
.src
.box
, src_region
);
213 blit
.dst
.resource
= dst_surfaces
[i
]->texture
;
214 blit
.dst
.format
= dst_surfaces
[i
]->format
;
216 blit
.dst
.box
.z
= dst_surfaces
[i
]->u
.tex
.first_layer
;
217 blit
.dst
.box
.depth
= 1;
218 vlVaGetBox(dst
, i
, &blit
.dst
.box
, dst_region
);
220 blit
.mask
= PIPE_MASK_RGBA
;
221 blit
.filter
= PIPE_TEX_MIPFILTER_LINEAR
;
223 if (drv
->pipe
->screen
->get_param(drv
->pipe
->screen
,
224 PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA
))
225 util_compute_blit(drv
->pipe
, &blit
, &context
->blit_cs
);
227 drv
->pipe
->blit(drv
->pipe
, &blit
);
230 // TODO: figure out why this is necessary for DMA-buf sharing
231 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
233 return VA_STATUS_SUCCESS
;
236 static struct pipe_video_buffer
*
237 vlVaApplyDeint(vlVaDriver
*drv
, vlVaContext
*context
,
238 VAProcPipelineParameterBuffer
*param
,
239 struct pipe_video_buffer
*current
,
242 vlVaSurface
*prevprev
, *prev
, *next
;
244 if (param
->num_forward_references
< 2 ||
245 param
->num_backward_references
< 1)
248 prevprev
= handle_table_get(drv
->htab
, param
->forward_references
[1]);
249 prev
= handle_table_get(drv
->htab
, param
->forward_references
[0]);
250 next
= handle_table_get(drv
->htab
, param
->backward_references
[0]);
252 if (!prevprev
|| !prev
|| !next
)
255 if (context
->deint
&& (context
->deint
->video_width
!= current
->width
||
256 context
->deint
->video_height
!= current
->height
)) {
257 vl_deint_filter_cleanup(context
->deint
);
258 FREE(context
->deint
);
259 context
->deint
= NULL
;
262 if (!context
->deint
) {
263 context
->deint
= MALLOC(sizeof(struct vl_deint_filter
));
264 if (!vl_deint_filter_init(context
->deint
, drv
->pipe
, current
->width
,
265 current
->height
, false, false)) {
266 FREE(context
->deint
);
267 context
->deint
= NULL
;
272 if (!vl_deint_filter_check_buffers(context
->deint
, prevprev
->buffer
,
273 prev
->buffer
, current
, next
->buffer
))
276 vl_deint_filter_render(context
->deint
, prevprev
->buffer
, prev
->buffer
,
277 current
, next
->buffer
, field
);
278 return context
->deint
->video_buffer
;
282 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
284 enum vl_compositor_deinterlace deinterlace
= VL_COMPOSITOR_WEAVE
;
285 VARectangle def_src_region
, def_dst_region
;
286 const VARectangle
*src_region
, *dst_region
;
287 VAProcPipelineParameterBuffer
*param
;
288 struct pipe_video_buffer
*src
;
289 vlVaSurface
*src_surface
, *dst_surface
;
292 if (!drv
|| !context
)
293 return VA_STATUS_ERROR_INVALID_CONTEXT
;
295 if (!buf
|| !buf
->data
)
296 return VA_STATUS_ERROR_INVALID_BUFFER
;
298 if (!context
->target
)
299 return VA_STATUS_ERROR_INVALID_SURFACE
;
303 src_surface
= handle_table_get(drv
->htab
, param
->surface
);
304 dst_surface
= handle_table_get(drv
->htab
, context
->target_id
);
306 if (!src_surface
|| !src_surface
->buffer
)
307 return VA_STATUS_ERROR_INVALID_SURFACE
;
309 src
= src_surface
->buffer
;
311 for (i
= 0; i
< param
->num_filters
; i
++) {
312 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, param
->filters
[i
]);
313 VAProcFilterParameterBufferBase
*filter
;
315 if (!buf
|| buf
->type
!= VAProcFilterParameterBufferType
)
316 return VA_STATUS_ERROR_INVALID_BUFFER
;
319 switch (filter
->type
) {
320 case VAProcFilterDeinterlacing
: {
321 VAProcFilterParameterBufferDeinterlacing
*deint
= buf
->data
;
322 switch (deint
->algorithm
) {
323 case VAProcDeinterlacingBob
:
324 if (deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD_FIRST
)
325 deinterlace
= VL_COMPOSITOR_BOB_BOTTOM
;
327 deinterlace
= VL_COMPOSITOR_BOB_TOP
;
330 case VAProcDeinterlacingWeave
:
331 deinterlace
= VL_COMPOSITOR_WEAVE
;
334 case VAProcDeinterlacingMotionAdaptive
:
335 src
= vlVaApplyDeint(drv
, context
, param
, src
,
336 !!(deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD_FIRST
));
340 return VA_STATUS_ERROR_UNIMPLEMENTED
;
347 return VA_STATUS_ERROR_UNIMPLEMENTED
;
351 src_region
= vlVaRegionDefault(param
->surface_region
, src_surface
, &def_src_region
);
352 dst_region
= vlVaRegionDefault(param
->output_region
, dst_surface
, &def_dst_region
);
354 if (context
->target
->buffer_format
!= PIPE_FORMAT_NV12
&&
355 context
->target
->buffer_format
!= PIPE_FORMAT_P010
&&
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
);