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"
30 #include "vl/vl_defines.h"
31 #include "vl/vl_video_buffer.h"
33 #include "va_private.h"
35 static const VARectangle
*
36 vlVaRegionDefault(const VARectangle
*region
, struct pipe_video_buffer
*buf
,
44 def
->width
= buf
->width
;
45 def
->height
= buf
->height
;
51 vlVaPostProcCompositor(vlVaDriver
*drv
, vlVaContext
*context
,
52 const VARectangle
*src_region
,
53 const VARectangle
*dst_region
,
54 struct pipe_video_buffer
*src
,
55 struct pipe_video_buffer
*dst
,
56 enum vl_compositor_deinterlace deinterlace
)
58 struct pipe_surface
**surfaces
;
59 struct u_rect src_rect
;
60 struct u_rect dst_rect
;
62 surfaces
= dst
->get_surfaces(dst
);
63 if (!surfaces
|| !surfaces
[0])
64 return VA_STATUS_ERROR_INVALID_SURFACE
;
66 src_rect
.x0
= src_region
->x
;
67 src_rect
.y0
= src_region
->y
;
68 src_rect
.x1
= src_region
->x
+ src_region
->width
;
69 src_rect
.y1
= src_region
->y
+ src_region
->height
;
71 dst_rect
.x0
= dst_region
->x
;
72 dst_rect
.y0
= dst_region
->y
;
73 dst_rect
.x1
= dst_region
->x
+ dst_region
->width
;
74 dst_rect
.y1
= dst_region
->y
+ dst_region
->height
;
76 vl_compositor_clear_layers(&drv
->cstate
);
77 vl_compositor_set_buffer_layer(&drv
->cstate
, &drv
->compositor
, 0, src
,
78 &src_rect
, NULL
, deinterlace
);
79 vl_compositor_set_layer_dst_area(&drv
->cstate
, 0, &dst_rect
);
80 vl_compositor_render(&drv
->cstate
, &drv
->compositor
, surfaces
[0], NULL
, false);
82 return VA_STATUS_SUCCESS
;
85 static void vlVaGetBox(struct pipe_video_buffer
*buf
, unsigned idx
,
86 struct pipe_box
*box
, const VARectangle
*region
)
88 unsigned plane
= buf
->interlaced
? idx
/ 2: idx
;
89 unsigned x
, y
, width
, height
;
93 width
= region
->width
;
94 height
= region
->height
;
96 vl_video_buffer_adjust_size(&x
, &y
, plane
, buf
->chroma_format
,
98 vl_video_buffer_adjust_size(&width
, &height
, plane
, buf
->chroma_format
,
101 box
->x
= region
->x
< 0 ? -x
: x
;
102 box
->y
= region
->y
< 0 ? -y
: y
;
104 box
->height
= height
;
107 static VAStatus
vlVaPostProcBlit(vlVaDriver
*drv
, vlVaContext
*context
,
108 const VARectangle
*src_region
,
109 const VARectangle
*dst_region
,
110 struct pipe_video_buffer
*src
,
111 struct pipe_video_buffer
*dst
,
112 enum vl_compositor_deinterlace deinterlace
)
114 struct pipe_surface
**src_surfaces
;
115 struct pipe_surface
**dst_surfaces
;
118 if (src
->interlaced
!= dst
->interlaced
)
119 return VA_STATUS_ERROR_INVALID_SURFACE
;
121 src_surfaces
= src
->get_surfaces(src
);
122 if (!src_surfaces
|| !src_surfaces
[0])
123 return VA_STATUS_ERROR_INVALID_SURFACE
;
125 dst_surfaces
= dst
->get_surfaces(dst
);
126 if (!dst_surfaces
|| !dst_surfaces
[0])
127 return VA_STATUS_ERROR_INVALID_SURFACE
;
129 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
130 struct pipe_surface
*from
= src_surfaces
[i
];
131 struct pipe_blit_info blit
;
133 if (src
->interlaced
) {
134 /* Not 100% accurate, but close enough */
135 switch (deinterlace
) {
136 case VL_COMPOSITOR_BOB_TOP
:
137 from
= src_surfaces
[i
& ~1];
139 case VL_COMPOSITOR_BOB_BOTTOM
:
140 from
= src_surfaces
[(i
& ~1) + 1];
147 if (!from
|| !dst_surfaces
[i
])
150 memset(&blit
, 0, sizeof(blit
));
151 blit
.src
.resource
= from
->texture
;
152 blit
.src
.format
= from
->format
;
154 blit
.src
.box
.z
= from
->u
.tex
.first_layer
;
155 blit
.src
.box
.depth
= 1;
156 vlVaGetBox(src
, i
, &blit
.src
.box
, src_region
);
158 blit
.dst
.resource
= dst_surfaces
[i
]->texture
;
159 blit
.dst
.format
= dst_surfaces
[i
]->format
;
161 blit
.dst
.box
.z
= dst_surfaces
[i
]->u
.tex
.first_layer
;
162 blit
.dst
.box
.depth
= 1;
163 vlVaGetBox(dst
, i
, &blit
.dst
.box
, dst_region
);
165 blit
.mask
= PIPE_MASK_RGBA
;
166 blit
.filter
= PIPE_TEX_MIPFILTER_LINEAR
;
168 drv
->pipe
->blit(drv
->pipe
, &blit
);
171 // TODO: figure out why this is necessary for DMA-buf sharing
172 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
174 return VA_STATUS_SUCCESS
;
178 vlVaHandleVAProcPipelineParameterBufferType(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
180 enum vl_compositor_deinterlace deinterlace
= VL_COMPOSITOR_WEAVE
;
181 VARectangle def_src_region
, def_dst_region
;
182 const VARectangle
*src_region
, *dst_region
;
183 VAProcPipelineParameterBuffer
*param
;
184 vlVaSurface
*src_surface
;
187 if (!drv
|| !context
)
188 return VA_STATUS_ERROR_INVALID_CONTEXT
;
190 if (!buf
|| !buf
->data
)
191 return VA_STATUS_ERROR_INVALID_BUFFER
;
193 if (!context
->target
)
194 return VA_STATUS_ERROR_INVALID_SURFACE
;
198 src_surface
= handle_table_get(drv
->htab
, param
->surface
);
199 if (!src_surface
|| !src_surface
->buffer
)
200 return VA_STATUS_ERROR_INVALID_SURFACE
;
202 for (i
= 0; i
< param
->num_filters
; i
++) {
203 vlVaBuffer
*buf
= handle_table_get(drv
->htab
, param
->filters
[i
]);
204 VAProcFilterParameterBufferBase
*filter
;
206 if (!buf
|| buf
->type
!= VAProcFilterParameterBufferType
)
207 return VA_STATUS_ERROR_INVALID_BUFFER
;
210 switch (filter
->type
) {
211 case VAProcFilterDeinterlacing
: {
212 VAProcFilterParameterBufferDeinterlacing
*deint
= buf
->data
;
213 switch (deint
->algorithm
) {
214 case VAProcDeinterlacingBob
:
215 if (deint
->flags
& VA_DEINTERLACING_BOTTOM_FIELD
)
216 deinterlace
= VL_COMPOSITOR_BOB_BOTTOM
;
218 deinterlace
= VL_COMPOSITOR_BOB_TOP
;
221 case VAProcDeinterlacingWeave
:
222 deinterlace
= VL_COMPOSITOR_WEAVE
;
226 return VA_STATUS_ERROR_UNIMPLEMENTED
;
233 return VA_STATUS_ERROR_UNIMPLEMENTED
;
237 src_region
= vlVaRegionDefault(param
->surface_region
, src_surface
->buffer
, &def_src_region
);
238 dst_region
= vlVaRegionDefault(param
->output_region
, context
->target
, &def_dst_region
);
240 if (context
->target
->buffer_format
!= PIPE_FORMAT_NV12
)
241 return vlVaPostProcCompositor(drv
, context
, src_region
, dst_region
,
242 src_surface
->buffer
, context
->target
,
245 return vlVaPostProcBlit(drv
, context
, src_region
, dst_region
,
246 src_surface
->buffer
, context
->target
,