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