1 /**************************************************************************
2 *
3 * Copyright 2015 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "util/u_handle_table.h"
29 #include "util/u_memory.h"
30
31 #include "vl/vl_defines.h"
32 #include "vl/vl_video_buffer.h"
33 #include "vl/vl_deint_filter.h"
34
35 #include "va_private.h"
36
37 static const VARectangle *
vlVaRegionDefault(const VARectangle * region,vlVaSurface * surf,VARectangle * def)38 vlVaRegionDefault(const VARectangle *region, vlVaSurface *surf,
39 VARectangle *def)
40 {
41 if (region)
42 return region;
43
44 def->x = 0;
45 def->y = 0;
46 def->width = surf->templat.width;
47 def->height = surf->templat.height;
48
49 return def;
50 }
51
52 static VAStatus
vlVaPostProcCompositor(vlVaDriver * drv,vlVaContext * context,const VARectangle * src_region,const VARectangle * dst_region,struct pipe_video_buffer * src,struct pipe_video_buffer * dst,enum vl_compositor_deinterlace deinterlace)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)
59 {
60 struct pipe_surface **surfaces;
61 struct u_rect src_rect;
62 struct u_rect dst_rect;
63
64 surfaces = dst->get_surfaces(dst);
65 if (!surfaces || !surfaces[0])
66 return VA_STATUS_ERROR_INVALID_SURFACE;
67
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;
72
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;
77
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);
83
84 drv->pipe->flush(drv->pipe, NULL, 0);
85 return VA_STATUS_SUCCESS;
86 }
87
vlVaGetBox(struct pipe_video_buffer * buf,unsigned idx,struct pipe_box * box,const VARectangle * region)88 static void vlVaGetBox(struct pipe_video_buffer *buf, unsigned idx,
89 struct pipe_box *box, const VARectangle *region)
90 {
91 unsigned plane = buf->interlaced ? idx / 2: idx;
92 unsigned x, y, width, height;
93
94 x = abs(region->x);
95 y = abs(region->y);
96 width = region->width;
97 height = region->height;
98
99 vl_video_buffer_adjust_size(&x, &y, plane, buf->chroma_format,
100 buf->interlaced);
101 vl_video_buffer_adjust_size(&width, &height, plane, buf->chroma_format,
102 buf->interlaced);
103
104 box->x = region->x < 0 ? -x : x;
105 box->y = region->y < 0 ? -y : y;
106 box->width = width;
107 box->height = height;
108 }
109
vlVaPostProcBlit(vlVaDriver * drv,vlVaContext * context,const VARectangle * src_region,const VARectangle * dst_region,struct pipe_video_buffer * src,struct pipe_video_buffer * dst,enum vl_compositor_deinterlace deinterlace)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)
116 {
117 struct pipe_surface **src_surfaces;
118 struct pipe_surface **dst_surfaces;
119 struct u_rect src_rect;
120 struct u_rect dst_rect;
121 bool scale = false;
122 bool grab = false;
123 unsigned i;
124
125 if ((src->buffer_format == PIPE_FORMAT_B8G8R8A8_UNORM ||
126 src->buffer_format == PIPE_FORMAT_B8G8R8X8_UNORM) &&
127 !src->interlaced)
128 grab = true;
129
130 if (src->interlaced != dst->interlaced && dst->interlaced && !grab)
131 return VA_STATUS_ERROR_INVALID_SURFACE;
132
133 if ((src->width != dst->width || src->height != dst->height) &&
134 (src->interlaced && dst->interlaced))
135 scale = true;
136
137 src_surfaces = src->get_surfaces(src);
138 if (!src_surfaces || !src_surfaces[0])
139 return VA_STATUS_ERROR_INVALID_SURFACE;
140
141 if (scale || (grab && dst->interlaced)) {
142 vlVaSurface *surf;
143
144 surf = handle_table_get(drv->htab, context->target_id);
145 surf->templat.interlaced = false;
146 dst->destroy(dst);
147
148 if (vlVaHandleSurfaceAllocate(drv, surf, &surf->templat) != VA_STATUS_SUCCESS)
149 return VA_STATUS_ERROR_ALLOCATION_FAILED;
150
151 dst = context->target = surf->buffer;
152 }
153
154 dst_surfaces = dst->get_surfaces(dst);
155 if (!dst_surfaces || !dst_surfaces[0])
156 return VA_STATUS_ERROR_INVALID_SURFACE;
157
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;
162
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;
167
168 if (grab) {
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);
172
173 return VA_STATUS_SUCCESS;
174 }
175
176 if (src->interlaced != dst->interlaced) {
177 vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor,
178 src, dst, &src_rect, &dst_rect,
179 deinterlace);
180
181 return VA_STATUS_SUCCESS;
182 }
183
184 for (i = 0; i < VL_MAX_SURFACES; ++i) {
185 struct pipe_surface *from = src_surfaces[i];
186 struct pipe_blit_info blit;
187
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];
193 break;
194 case VL_COMPOSITOR_BOB_BOTTOM:
195 from = src_surfaces[(i & ~1) + 1];
196 break;
197 default:
198 break;
199 }
200 }
201
202 if (!from || !dst_surfaces[i])
203 continue;
204
205 memset(&blit, 0, sizeof(blit));
206 blit.src.resource = from->texture;
207 blit.src.format = from->format;
208 blit.src.level = 0;
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);
212
213 blit.dst.resource = dst_surfaces[i]->texture;
214 blit.dst.format = dst_surfaces[i]->format;
215 blit.dst.level = 0;
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);
219
220 blit.mask = PIPE_MASK_RGBA;
221 blit.filter = PIPE_TEX_MIPFILTER_LINEAR;
222
223 drv->pipe->blit(drv->pipe, &blit);
224 }
225
226 // TODO: figure out why this is necessary for DMA-buf sharing
227 drv->pipe->flush(drv->pipe, NULL, 0);
228
229 return VA_STATUS_SUCCESS;
230 }
231
232 static struct pipe_video_buffer *
vlVaApplyDeint(vlVaDriver * drv,vlVaContext * context,VAProcPipelineParameterBuffer * param,struct pipe_video_buffer * current,unsigned field)233 vlVaApplyDeint(vlVaDriver *drv, vlVaContext *context,
234 VAProcPipelineParameterBuffer *param,
235 struct pipe_video_buffer *current,
236 unsigned field)
237 {
238 vlVaSurface *prevprev, *prev, *next;
239
240 if (param->num_forward_references < 2 ||
241 param->num_backward_references < 1)
242 return current;
243
244 prevprev = handle_table_get(drv->htab, param->forward_references[1]);
245 prev = handle_table_get(drv->htab, param->forward_references[0]);
246 next = handle_table_get(drv->htab, param->backward_references[0]);
247
248 if (!prevprev || !prev || !next)
249 return current;
250
251 if (context->deint && (context->deint->video_width != current->width ||
252 context->deint->video_height != current->height)) {
253 vl_deint_filter_cleanup(context->deint);
254 FREE(context->deint);
255 context->deint = NULL;
256 }
257
258 if (!context->deint) {
259 context->deint = MALLOC(sizeof(struct vl_deint_filter));
260 if (!vl_deint_filter_init(context->deint, drv->pipe, current->width,
261 current->height, false, false)) {
262 FREE(context->deint);
263 context->deint = NULL;
264 return current;
265 }
266 }
267
268 if (!vl_deint_filter_check_buffers(context->deint, prevprev->buffer,
269 prev->buffer, current, next->buffer))
270 return current;
271
272 vl_deint_filter_render(context->deint, prevprev->buffer, prev->buffer,
273 current, next->buffer, field);
274 return context->deint->video_buffer;
275 }
276
277 VAStatus
vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)278 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
279 {
280 enum vl_compositor_deinterlace deinterlace = VL_COMPOSITOR_WEAVE;
281 VARectangle def_src_region, def_dst_region;
282 const VARectangle *src_region, *dst_region;
283 VAProcPipelineParameterBuffer *param;
284 struct pipe_video_buffer *src;
285 vlVaSurface *src_surface, *dst_surface;
286 unsigned i;
287
288 if (!drv || !context)
289 return VA_STATUS_ERROR_INVALID_CONTEXT;
290
291 if (!buf || !buf->data)
292 return VA_STATUS_ERROR_INVALID_BUFFER;
293
294 if (!context->target)
295 return VA_STATUS_ERROR_INVALID_SURFACE;
296
297 param = buf->data;
298
299 src_surface = handle_table_get(drv->htab, param->surface);
300 dst_surface = handle_table_get(drv->htab, context->target_id);
301
302 if (!src_surface || !src_surface->buffer)
303 return VA_STATUS_ERROR_INVALID_SURFACE;
304
305 src = src_surface->buffer;
306
307 for (i = 0; i < param->num_filters; i++) {
308 vlVaBuffer *buf = handle_table_get(drv->htab, param->filters[i]);
309 VAProcFilterParameterBufferBase *filter;
310
311 if (!buf || buf->type != VAProcFilterParameterBufferType)
312 return VA_STATUS_ERROR_INVALID_BUFFER;
313
314 filter = buf->data;
315 switch (filter->type) {
316 case VAProcFilterDeinterlacing: {
317 VAProcFilterParameterBufferDeinterlacing *deint = buf->data;
318 switch (deint->algorithm) {
319 case VAProcDeinterlacingBob:
320 if (deint->flags & VA_DEINTERLACING_BOTTOM_FIELD)
321 deinterlace = VL_COMPOSITOR_BOB_BOTTOM;
322 else
323 deinterlace = VL_COMPOSITOR_BOB_TOP;
324 break;
325
326 case VAProcDeinterlacingWeave:
327 deinterlace = VL_COMPOSITOR_WEAVE;
328 break;
329
330 case VAProcDeinterlacingMotionAdaptive:
331 src = vlVaApplyDeint(drv, context, param, src,
332 !!(deint->flags & VA_DEINTERLACING_BOTTOM_FIELD));
333 break;
334
335 default:
336 return VA_STATUS_ERROR_UNIMPLEMENTED;
337 }
338
339 break;
340 }
341
342 default:
343 return VA_STATUS_ERROR_UNIMPLEMENTED;
344 }
345 }
346
347 src_region = vlVaRegionDefault(param->surface_region, src_surface, &def_src_region);
348 dst_region = vlVaRegionDefault(param->output_region, dst_surface, &def_dst_region);
349
350 if (context->target->buffer_format != PIPE_FORMAT_NV12 &&
351 context->target->buffer_format != PIPE_FORMAT_P016)
352 return vlVaPostProcCompositor(drv, context, src_region, dst_region,
353 src, context->target, deinterlace);
354 else
355 return vlVaPostProcBlit(drv, context, src_region, dst_region,
356 src, context->target, deinterlace);
357 }
358