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"
31 #include "vl/vl_defines.h"
32 #include "vl/vl_video_buffer.h"
33 #include "vl/vl_deint_filter.h"
35 #include "va_private.h"
37 static const VARectangle
*
38 vlVaRegionDefault(const VARectangle
*region
, struct pipe_video_buffer
*buf
,
46 def
->width
= buf
->width
;
47 def
->height
= buf
->height
;
53 vlVaPostProcCompositor(vlVaDriver
*drv
, vlVaContext
*context
,
54 const VARectangle
*src_region
,
55 const VARectangle
*dst_region
,
56 struct pipe_video_buffer
*src
,
57 struct pipe_video_buffer
*dst
,
58 enum vl_compositor_deinterlace deinterlace
)
60 struct pipe_surface
**surfaces
;
61 struct u_rect src_rect
;
62 struct u_rect dst_rect
;
64 surfaces
= dst
->get_surfaces(dst
);
65 if (!surfaces
|| !surfaces
[0])
66 return VA_STATUS_ERROR_INVALID_SURFACE
;
68 src_rect
.x0
= src_region
->x
;
69 src_rect
.y0
= src_region
->y
;
70 src_rect
.x1
= src_region
->x
+ src_region
->width
;
71 src_rect
.y1
= src_region
->y
+ src_region
->height
;
73 dst_rect
.x0
= dst_region
->x
;
74 dst_rect
.y0
= dst_region
->y
;
75 dst_rect
.x1
= dst_region
->x
+ dst_region
->width
;
76 dst_rect
.y1
= dst_region
->y
+ dst_region
->height
;
78 vl_compositor_clear_layers(&drv
->cstate
);
79 vl_compositor_set_buffer_layer(&drv
->cstate
, &drv
->compositor
, 0, src
,
80 &src_rect
, NULL
, deinterlace
);
81 vl_compositor_set_layer_dst_area(&drv
->cstate
, 0, &dst_rect
);
82 vl_compositor_render(&drv
->cstate
, &drv
->compositor
, surfaces
[0], NULL
, false);
84 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
85 return VA_STATUS_SUCCESS
;
88 static void vlVaGetBox(struct pipe_video_buffer
*buf
, unsigned idx
,
89 struct pipe_box
*box
, const VARectangle
*region
)
91 unsigned plane
= buf
->interlaced
? idx
/ 2: idx
;
92 unsigned x
, y
, width
, height
;
96 width
= region
->width
;
97 height
= region
->height
;
99 vl_video_buffer_adjust_size(&x
, &y
, plane
, buf
->chroma_format
,
101 vl_video_buffer_adjust_size(&width
, &height
, plane
, buf
->chroma_format
,
104 box
->x
= region
->x
< 0 ? -x
: x
;
105 box
->y
= region
->y
< 0 ? -y
: y
;
107 box
->height
= height
;
110 static VAStatus
vlVaPostProcBlit(vlVaDriver
*drv
, vlVaContext
*context
,
111 const VARectangle
*src_region
,
112 const VARectangle
*dst_region
,
113 struct pipe_video_buffer
*src
,
114 struct pipe_video_buffer
*dst
,
115 enum vl_compositor_deinterlace deinterlace
)
117 struct pipe_surface
**src_surfaces
;
118 struct pipe_surface
**dst_surfaces
;
121 if (src
->interlaced
!= dst
->interlaced
)
122 return VA_STATUS_ERROR_INVALID_SURFACE
;
124 src_surfaces
= src
->get_surfaces(src
);
125 if (!src_surfaces
|| !src_surfaces
[0])
126 return VA_STATUS_ERROR_INVALID_SURFACE
;
128 dst_surfaces
= dst
->get_surfaces(dst
);
129 if (!dst_surfaces
|| !dst_surfaces
[0])
130 return VA_STATUS_ERROR_INVALID_SURFACE
;
132 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
133 struct pipe_surface
*from
= src_surfaces
[i
];
134 struct pipe_blit_info blit
;
136 if (src
->interlaced
) {
137 /* Not 100% accurate, but close enough */
138 switch (deinterlace
) {
139 case VL_COMPOSITOR_BOB_TOP
:
140 from
= src_surfaces
[i
& ~1];
142 case VL_COMPOSITOR_BOB_BOTTOM
:
143 from
= src_surfaces
[(i
& ~1) + 1];
150 if (!from
|| !dst_surfaces
[i
])
153 memset(&blit
, 0, sizeof(blit
));
154 blit
.src
.resource
= from
->texture
;
155 blit
.src
.format
= from
->format
;
157 blit
.src
.box
.z
= from
->u
.tex
.first_layer
;
158 blit
.src
.box
.depth
= 1;
159 vlVaGetBox(src
, i
, &blit
.src
.box
, src_region
);
161 blit
.dst
.resource
= dst_surfaces
[i
]->texture
;
162 blit
.dst
.format
= dst_surfaces
[i
]->format
;
164 blit
.dst
.box
.z
= dst_surfaces
[i
]->u
.tex
.first_layer
;
165 blit
.dst
.box
.depth
= 1;
166 vlVaGetBox(dst
, i
, &blit
.dst
.box
, dst_region
);
168 blit
.mask
= PIPE_MASK_RGBA
;
169 blit
.filter
= PIPE_TEX_MIPFILTER_LINEAR
;
171 drv
->pipe
->blit(drv
->pipe
, &blit
);
174 // TODO: figure out why this is necessary for DMA-buf sharing
175 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
177 return VA_STATUS_SUCCESS
;
180 static struct pipe_video_buffer
*
181 vlVaApplyDeint(vlVaDriver
*drv
, vlVaContext
*context
,
182 VAProcPipelineParameterBuffer
*param
,
183 struct pipe_video_buffer
*current
,
186 vlVaSurface
*prevprev
, *prev
, *next
;
188 if (param
->num_forward_references
< 2 ||
189 param
->num_backward_references
< 1)
192 prevprev
= handle_table_get(drv
->htab
, param
->forward_references
[1]);
193 prev
= handle_table_get(drv
->htab
, param
->forward_references
[0]);
194 next
= handle_table_get(drv
->htab
, param
->backward_references
[0]);
196 if (!prevprev
|| !prev
|| !next
)
199 if (context
->deint
&& (context
->deint
->video_width
!= current
->width
||
200 context
->deint
->video_height
!= current
->height
)) {
201 vl_deint_filter_cleanup(context
->deint
);
202 FREE(context
->deint
);
203 context
->deint
= NULL
;
206 if (!context
->deint
) {
207 context
->deint
= MALLOC(sizeof(struct vl_deint_filter
));
208 if (!vl_deint_filter_init(context
->deint
, drv
->pipe
, current
->width
,
209 current
->height
, false, false)) {
210 FREE(context
->deint
);
211 context
->deint
= NULL
;
216 if (!vl_deint_filter_check_buffers(context
->deint
, prevprev
->buffer
,
217 prev
->buffer
, current
, next
->buffer
))
220 vl_deint_filter_render(context
->deint
, prevprev
->buffer
, prev
->buffer
,
221 current
, next
->buffer
, field
);
222 return context
->deint
->video_buffer
;
226 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
228 enum vl_compositor_deinterlace deinterlace
= VL_COMPOSITOR_WEAVE
;
229 VARectangle def_src_region
, def_dst_region
;
230 const VARectangle
*src_region
, *dst_region
;
231 VAProcPipelineParameterBuffer
*param
;
232 struct pipe_video_buffer
*src
;
233 vlVaSurface
*src_surface
;
236 if (!drv
|| !context
)
237 return VA_STATUS_ERROR_INVALID_CONTEXT
;
239 if (!buf
|| !buf
->data
)
240 return VA_STATUS_ERROR_INVALID_BUFFER
;
242 if (!context
->target
)
243 return VA_STATUS_ERROR_INVALID_SURFACE
;
247 src_surface
= handle_table_get(drv
->htab
, param
->surface
);
248 if (!src_surface
|| !src_surface
->buffer
)
249 return VA_STATUS_ERROR_INVALID_SURFACE
;
251 src
= src_surface
->buffer
;
253 for (i
= 0; i
< param
->num_filters
; i
++) {
254 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, param
->filters
[i
]);
255 VAProcFilterParameterBufferBase
*filter
;
257 if (!buf
|| buf
->type
!= VAProcFilterParameterBufferType
)
258 return VA_STATUS_ERROR_INVALID_BUFFER
;
261 switch (filter
->type
) {
262 case VAProcFilterDeinterlacing
: {
263 VAProcFilterParameterBufferDeinterlacing
*deint
= buf
->data
;
264 switch (deint
->algorithm
) {
265 case VAProcDeinterlacingBob
:
266 if (deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD
)
267 deinterlace
= VL_COMPOSITOR_BOB_BOTTOM
;
269 deinterlace
= VL_COMPOSITOR_BOB_TOP
;
272 case VAProcDeinterlacingWeave
:
273 deinterlace
= VL_COMPOSITOR_WEAVE
;
276 case VAProcDeinterlacingMotionAdaptive
:
277 src
= vlVaApplyDeint(drv
, context
, param
, src
,
278 !!(deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD
));
282 return VA_STATUS_ERROR_UNIMPLEMENTED
;
289 return VA_STATUS_ERROR_UNIMPLEMENTED
;
293 src_region
= vlVaRegionDefault(param
->surface_region
, src_surface
->buffer
, &def_src_region
);
294 dst_region
= vlVaRegionDefault(param
->output_region
, context
->target
, &def_dst_region
);
296 if (context
->target
->buffer_format
!= PIPE_FORMAT_NV12
&&
297 context
->target
->buffer_format
!= PIPE_FORMAT_P016
)
298 return vlVaPostProcCompositor(drv
, context
, src_region
, dst_region
,
299 src
, context
->target
, deinterlace
);
301 return vlVaPostProcBlit(drv
, context
, src_region
, dst_region
,
302 src
, context
->target
, deinterlace
);