st/va/postproc: use progressive target buffer for scaling
[mesa.git] / src / gallium / state_trackers / va / postproc.c
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 *
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
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
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
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 unsigned i;
123
124 if (src->interlaced != dst->interlaced && dst->interlaced)
125 return VA_STATUS_ERROR_INVALID_SURFACE;
126
127 if ((src->width != dst->width || src->height != dst->height) &&
128 (src->interlaced && dst->interlaced))
129 scale = true;
130
131 src_surfaces = src->get_surfaces(src);
132 if (!src_surfaces || !src_surfaces[0])
133 return VA_STATUS_ERROR_INVALID_SURFACE;
134
135 if (scale) {
136 vlVaSurface *surf;
137
138 surf = handle_table_get(drv->htab, context->target_id);
139 surf->templat.interlaced = false;
140 dst->destroy(dst);
141
142 if (vlVaHandleSurfaceAllocate(drv, surf, &surf->templat) != VA_STATUS_SUCCESS)
143 return VA_STATUS_ERROR_ALLOCATION_FAILED;
144
145 dst = context->target = surf->buffer;
146 }
147
148 dst_surfaces = dst->get_surfaces(dst);
149 if (!dst_surfaces || !dst_surfaces[0])
150 return VA_STATUS_ERROR_INVALID_SURFACE;
151
152 src_rect.x0 = src_region->x;
153 src_rect.y0 = src_region->y;
154 src_rect.x1 = src_region->x + src_region->width;
155 src_rect.y1 = src_region->y + src_region->height;
156
157 dst_rect.x0 = dst_region->x;
158 dst_rect.y0 = dst_region->y;
159 dst_rect.x1 = dst_region->x + dst_region->width;
160 dst_rect.y1 = dst_region->y + dst_region->height;
161
162 if (src->interlaced != dst->interlaced) {
163 vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor,
164 src, dst, &src_rect, &dst_rect,
165 deinterlace);
166
167 return VA_STATUS_SUCCESS;
168 }
169
170 for (i = 0; i < VL_MAX_SURFACES; ++i) {
171 struct pipe_surface *from = src_surfaces[i];
172 struct pipe_blit_info blit;
173
174 if (src->interlaced) {
175 /* Not 100% accurate, but close enough */
176 switch (deinterlace) {
177 case VL_COMPOSITOR_BOB_TOP:
178 from = src_surfaces[i & ~1];
179 break;
180 case VL_COMPOSITOR_BOB_BOTTOM:
181 from = src_surfaces[(i & ~1) + 1];
182 break;
183 default:
184 break;
185 }
186 }
187
188 if (!from || !dst_surfaces[i])
189 continue;
190
191 memset(&blit, 0, sizeof(blit));
192 blit.src.resource = from->texture;
193 blit.src.format = from->format;
194 blit.src.level = 0;
195 blit.src.box.z = from->u.tex.first_layer;
196 blit.src.box.depth = 1;
197 vlVaGetBox(src, i, &blit.src.box, src_region);
198
199 blit.dst.resource = dst_surfaces[i]->texture;
200 blit.dst.format = dst_surfaces[i]->format;
201 blit.dst.level = 0;
202 blit.dst.box.z = dst_surfaces[i]->u.tex.first_layer;
203 blit.dst.box.depth = 1;
204 vlVaGetBox(dst, i, &blit.dst.box, dst_region);
205
206 blit.mask = PIPE_MASK_RGBA;
207 blit.filter = PIPE_TEX_MIPFILTER_LINEAR;
208
209 drv->pipe->blit(drv->pipe, &blit);
210 }
211
212 // TODO: figure out why this is necessary for DMA-buf sharing
213 drv->pipe->flush(drv->pipe, NULL, 0);
214
215 return VA_STATUS_SUCCESS;
216 }
217
218 static struct pipe_video_buffer *
219 vlVaApplyDeint(vlVaDriver *drv, vlVaContext *context,
220 VAProcPipelineParameterBuffer *param,
221 struct pipe_video_buffer *current,
222 unsigned field)
223 {
224 vlVaSurface *prevprev, *prev, *next;
225
226 if (param->num_forward_references < 2 ||
227 param->num_backward_references < 1)
228 return current;
229
230 prevprev = handle_table_get(drv->htab, param->forward_references[1]);
231 prev = handle_table_get(drv->htab, param->forward_references[0]);
232 next = handle_table_get(drv->htab, param->backward_references[0]);
233
234 if (!prevprev || !prev || !next)
235 return current;
236
237 if (context->deint && (context->deint->video_width != current->width ||
238 context->deint->video_height != current->height)) {
239 vl_deint_filter_cleanup(context->deint);
240 FREE(context->deint);
241 context->deint = NULL;
242 }
243
244 if (!context->deint) {
245 context->deint = MALLOC(sizeof(struct vl_deint_filter));
246 if (!vl_deint_filter_init(context->deint, drv->pipe, current->width,
247 current->height, false, false)) {
248 FREE(context->deint);
249 context->deint = NULL;
250 return current;
251 }
252 }
253
254 if (!vl_deint_filter_check_buffers(context->deint, prevprev->buffer,
255 prev->buffer, current, next->buffer))
256 return current;
257
258 vl_deint_filter_render(context->deint, prevprev->buffer, prev->buffer,
259 current, next->buffer, field);
260 return context->deint->video_buffer;
261 }
262
263 VAStatus
264 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
265 {
266 enum vl_compositor_deinterlace deinterlace = VL_COMPOSITOR_WEAVE;
267 VARectangle def_src_region, def_dst_region;
268 const VARectangle *src_region, *dst_region;
269 VAProcPipelineParameterBuffer *param;
270 struct pipe_video_buffer *src;
271 vlVaSurface *src_surface, *dst_surface;
272 unsigned i;
273
274 if (!drv || !context)
275 return VA_STATUS_ERROR_INVALID_CONTEXT;
276
277 if (!buf || !buf->data)
278 return VA_STATUS_ERROR_INVALID_BUFFER;
279
280 if (!context->target)
281 return VA_STATUS_ERROR_INVALID_SURFACE;
282
283 param = buf->data;
284
285 src_surface = handle_table_get(drv->htab, param->surface);
286 dst_surface = handle_table_get(drv->htab, context->target_id);
287
288 if (!src_surface || !src_surface->buffer)
289 return VA_STATUS_ERROR_INVALID_SURFACE;
290
291 src = src_surface->buffer;
292
293 for (i = 0; i < param->num_filters; i++) {
294 vlVaBuffer *buf = handle_table_get(drv->htab, param->filters[i]);
295 VAProcFilterParameterBufferBase *filter;
296
297 if (!buf || buf->type != VAProcFilterParameterBufferType)
298 return VA_STATUS_ERROR_INVALID_BUFFER;
299
300 filter = buf->data;
301 switch (filter->type) {
302 case VAProcFilterDeinterlacing: {
303 VAProcFilterParameterBufferDeinterlacing *deint = buf->data;
304 switch (deint->algorithm) {
305 case VAProcDeinterlacingBob:
306 if (deint->flags & VA_DEINTERLACING_BOTTOM_FIELD)
307 deinterlace = VL_COMPOSITOR_BOB_BOTTOM;
308 else
309 deinterlace = VL_COMPOSITOR_BOB_TOP;
310 break;
311
312 case VAProcDeinterlacingWeave:
313 deinterlace = VL_COMPOSITOR_WEAVE;
314 break;
315
316 case VAProcDeinterlacingMotionAdaptive:
317 src = vlVaApplyDeint(drv, context, param, src,
318 !!(deint->flags & VA_DEINTERLACING_BOTTOM_FIELD));
319 break;
320
321 default:
322 return VA_STATUS_ERROR_UNIMPLEMENTED;
323 }
324
325 break;
326 }
327
328 default:
329 return VA_STATUS_ERROR_UNIMPLEMENTED;
330 }
331 }
332
333 src_region = vlVaRegionDefault(param->surface_region, src_surface, &def_src_region);
334 dst_region = vlVaRegionDefault(param->output_region, dst_surface, &def_dst_region);
335
336 if (context->target->buffer_format != PIPE_FORMAT_NV12 &&
337 context->target->buffer_format != PIPE_FORMAT_P016)
338 return vlVaPostProcCompositor(drv, context, src_region, dst_region,
339 src, context->target, deinterlace);
340 else
341 return vlVaPostProcBlit(drv, context, src_region, dst_region,
342 src, context->target, deinterlace);
343 }