1 /**************************************************************************
3 * Copyright 2011 Christian König.
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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 **************************************************************************/
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
34 #include "util/u_format.h"
35 #include "util/u_inlines.h"
36 #include "util/u_sampler.h"
37 #include "util/u_memory.h"
39 #include "vl_video_buffer.h"
41 const enum pipe_format const_resource_formats_YV12
[3] = {
47 const enum pipe_format const_resource_formats_NV12
[3] = {
49 PIPE_FORMAT_R8G8_UNORM
,
53 const enum pipe_format const_resource_formats_YUVA
[3] = {
54 PIPE_FORMAT_R8G8B8A8_UNORM
,
59 const enum pipe_format const_resource_formats_VUYA
[3] = {
60 PIPE_FORMAT_B8G8R8A8_UNORM
,
65 const enum pipe_format const_resource_formats_YUYV
[3] = {
66 PIPE_FORMAT_R8G8_R8B8_UNORM
,
71 const enum pipe_format const_resource_formats_UYVY
[3] = {
72 PIPE_FORMAT_G8R8_B8R8_UNORM
,
77 const unsigned const_resource_plane_order_YUV
[3] = {
83 const unsigned const_resource_plane_order_YVU
[3] = {
89 const enum pipe_format
*
90 vl_video_buffer_formats(struct pipe_screen
*screen
, enum pipe_format format
)
93 case PIPE_FORMAT_YV12
:
94 return const_resource_formats_YV12
;
96 case PIPE_FORMAT_NV12
:
97 return const_resource_formats_NV12
;
99 case PIPE_FORMAT_R8G8B8A8_UNORM
:
100 return const_resource_formats_YUVA
;
102 case PIPE_FORMAT_B8G8R8A8_UNORM
:
103 return const_resource_formats_VUYA
;
105 case PIPE_FORMAT_YUYV
:
106 return const_resource_formats_YUYV
;
108 case PIPE_FORMAT_UYVY
:
109 return const_resource_formats_UYVY
;
117 vl_video_buffer_plane_order(enum pipe_format format
)
120 case PIPE_FORMAT_YV12
:
121 return const_resource_plane_order_YVU
;
123 case PIPE_FORMAT_NV12
:
124 case PIPE_FORMAT_R8G8B8A8_UNORM
:
125 case PIPE_FORMAT_B8G8R8A8_UNORM
:
126 case PIPE_FORMAT_YUYV
:
127 case PIPE_FORMAT_UYVY
:
128 return const_resource_plane_order_YUV
;
135 static enum pipe_format
136 vl_video_buffer_surface_format(enum pipe_format format
)
138 const struct util_format_description
*desc
= util_format_description(format
);
140 /* a subsampled formats can't work as surface use RGBA instead */
141 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
142 return PIPE_FORMAT_R8G8B8A8_UNORM
;
148 vl_video_buffer_is_format_supported(struct pipe_screen
*screen
,
149 enum pipe_format format
,
150 enum pipe_video_profile profile
)
152 const enum pipe_format
*resource_formats
;
155 resource_formats
= vl_video_buffer_formats(screen
, format
);
156 if (!resource_formats
)
159 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
160 enum pipe_format format
= resource_formats
[i
];
162 if (format
== PIPE_FORMAT_NONE
)
165 /* we at least need to sample from it */
166 if (!screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
))
169 format
= vl_video_buffer_surface_format(format
);
170 if (!screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_RENDER_TARGET
))
178 vl_video_buffer_max_size(struct pipe_screen
*screen
)
180 uint32_t max_2d_texture_level
;
182 max_2d_texture_level
= screen
->get_param(screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
184 return 1 << (max_2d_texture_level
-1);
188 vl_video_buffer_set_associated_data(struct pipe_video_buffer
*vbuf
,
189 struct pipe_video_decoder
*vdec
,
190 void *associated_data
,
191 void (*destroy_associated_data
)(void *))
193 vbuf
->decoder
= vdec
;
195 if (vbuf
->associated_data
== associated_data
)
198 if (vbuf
->associated_data
)
199 vbuf
->destroy_associated_data(vbuf
->associated_data
);
201 vbuf
->associated_data
= associated_data
;
202 vbuf
->destroy_associated_data
= destroy_associated_data
;
206 vl_video_buffer_get_associated_data(struct pipe_video_buffer
*vbuf
,
207 struct pipe_video_decoder
*vdec
)
209 if (vbuf
->decoder
== vdec
)
210 return vbuf
->associated_data
;
216 vl_video_buffer_template(struct pipe_resource
*templ
,
217 const struct pipe_video_buffer
*tmpl
,
218 enum pipe_format resource_format
,
219 unsigned array_size
, unsigned usage
,
222 memset(templ
, 0, sizeof(*templ
));
223 templ
->target
= array_size
> 1 ? PIPE_TEXTURE_2D_ARRAY
: PIPE_TEXTURE_2D
;
224 templ
->format
= resource_format
;
225 templ
->width0
= tmpl
->width
;
226 templ
->height0
= tmpl
->height
;
228 templ
->array_size
= array_size
;
229 templ
->bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
230 templ
->usage
= usage
;
233 if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
236 } else if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
243 vl_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
245 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
250 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
251 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
252 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
253 pipe_resource_reference(&buf
->resources
[i
], NULL
);
256 for (i
= 0; i
< VL_NUM_COMPONENTS
* 2; ++i
)
257 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
259 vl_video_buffer_set_associated_data(buffer
, NULL
, NULL
, NULL
);
264 static struct pipe_sampler_view
**
265 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
267 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
268 struct pipe_sampler_view sv_templ
;
269 struct pipe_context
*pipe
;
274 pipe
= buf
->base
.context
;
276 for (i
= 0; i
< buf
->num_planes
; ++i
) {
277 if (!buf
->sampler_view_planes
[i
]) {
278 memset(&sv_templ
, 0, sizeof(sv_templ
));
279 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
281 if (util_format_get_nr_components(buf
->resources
[i
]->format
) == 1)
282 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= sv_templ
.swizzle_a
= PIPE_SWIZZLE_RED
;
284 buf
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
285 if (!buf
->sampler_view_planes
[i
])
290 return buf
->sampler_view_planes
;
293 for (i
= 0; i
< buf
->num_planes
; ++i
)
294 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
299 static struct pipe_sampler_view
**
300 vl_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
302 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
303 struct pipe_sampler_view sv_templ
;
304 struct pipe_context
*pipe
;
305 const enum pipe_format
*sampler_format
;
306 const unsigned *plane_order
;
307 unsigned i
, j
, component
;
311 pipe
= buf
->base
.context
;
313 sampler_format
= vl_video_buffer_formats(pipe
->screen
, buf
->base
.buffer_format
);
314 plane_order
= vl_video_buffer_plane_order(buf
->base
.buffer_format
);
316 for (component
= 0, i
= 0; i
< buf
->num_planes
; ++i
) {
317 struct pipe_resource
*res
= buf
->resources
[plane_order
[i
]];
318 const struct util_format_description
*desc
= util_format_description(res
->format
);
319 unsigned nr_components
= util_format_get_nr_components(res
->format
);
320 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
323 for (j
= 0; j
< nr_components
&& component
< VL_NUM_COMPONENTS
; ++j
, ++component
) {
324 if (buf
->sampler_view_components
[component
])
327 memset(&sv_templ
, 0, sizeof(sv_templ
));
328 u_sampler_view_default_template(&sv_templ
, res
, sampler_format
[plane_order
[i
]]);
329 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_RED
+ j
;
330 sv_templ
.swizzle_a
= PIPE_SWIZZLE_ONE
;
331 buf
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
332 if (!buf
->sampler_view_components
[component
])
336 assert(component
== VL_NUM_COMPONENTS
);
338 return buf
->sampler_view_components
;
341 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
342 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
347 static struct pipe_surface
**
348 vl_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
350 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
351 struct pipe_surface surf_templ
;
352 struct pipe_context
*pipe
;
353 unsigned i
, j
, array_size
, surf
;
357 pipe
= buf
->base
.context
;
359 array_size
= buffer
->interlaced
? 2 : 1;
360 for (i
= 0, surf
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
361 for (j
= 0; j
< array_size
; ++j
, ++surf
) {
362 assert(surf
< (VL_NUM_COMPONENTS
* 2));
364 if (!buf
->resources
[i
]) {
365 pipe_surface_reference(&buf
->surfaces
[surf
], NULL
);
369 if (!buf
->surfaces
[surf
]) {
370 memset(&surf_templ
, 0, sizeof(surf_templ
));
371 surf_templ
.format
= vl_video_buffer_surface_format(buf
->resources
[i
]->format
);
372 surf_templ
.u
.tex
.first_layer
= surf_templ
.u
.tex
.last_layer
= j
;
373 buf
->surfaces
[surf
] = pipe
->create_surface(pipe
, buf
->resources
[i
], &surf_templ
);
374 if (!buf
->surfaces
[surf
])
380 return buf
->surfaces
;
383 for (i
= 0; i
< (VL_NUM_COMPONENTS
* 2); ++i
)
384 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
389 struct pipe_video_buffer
*
390 vl_video_buffer_create(struct pipe_context
*pipe
,
391 const struct pipe_video_buffer
*tmpl
)
393 const enum pipe_format
*resource_formats
;
394 struct pipe_video_buffer templat
, *result
;
398 assert(tmpl
->width
> 0 && tmpl
->height
> 0);
400 pot_buffers
= !pipe
->screen
->get_video_param
403 PIPE_VIDEO_PROFILE_UNKNOWN
,
404 PIPE_VIDEO_CAP_NPOT_TEXTURES
407 resource_formats
= vl_video_buffer_formats(pipe
->screen
, tmpl
->buffer_format
);
408 if (!resource_formats
)
412 templat
.width
= pot_buffers
? util_next_power_of_two(tmpl
->width
)
413 : align(tmpl
->width
, VL_MACROBLOCK_WIDTH
);
414 templat
.height
= pot_buffers
? util_next_power_of_two(tmpl
->height
)
415 : align(tmpl
->height
, VL_MACROBLOCK_HEIGHT
);
417 if (tmpl
->interlaced
)
420 result
= vl_video_buffer_create_ex
422 pipe
, &templat
, resource_formats
,
423 tmpl
->interlaced
? 2 : 1, PIPE_USAGE_STATIC
427 if (result
&& tmpl
->interlaced
)
433 struct pipe_video_buffer
*
434 vl_video_buffer_create_ex(struct pipe_context
*pipe
,
435 const struct pipe_video_buffer
*tmpl
,
436 const enum pipe_format resource_formats
[VL_NUM_COMPONENTS
],
437 unsigned array_size
, unsigned usage
)
439 struct pipe_resource res_tmpl
;
440 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
];
445 memset(resources
, 0, sizeof resources
);
447 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[0], array_size
, usage
, 0);
448 resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
452 if (resource_formats
[1] == PIPE_FORMAT_NONE
) {
453 assert(resource_formats
[2] == PIPE_FORMAT_NONE
);
454 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
457 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[1], array_size
, usage
, 1);
458 resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
462 if (resource_formats
[2] == PIPE_FORMAT_NONE
)
463 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
465 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[2], array_size
, usage
, 2);
466 resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
470 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
473 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
474 pipe_resource_reference(&resources
[i
], NULL
);
479 struct pipe_video_buffer
*
480 vl_video_buffer_create_ex2(struct pipe_context
*pipe
,
481 const struct pipe_video_buffer
*tmpl
,
482 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
])
484 struct vl_video_buffer
*buffer
;
487 buffer
= CALLOC_STRUCT(vl_video_buffer
);
489 buffer
->base
= *tmpl
;
490 buffer
->base
.context
= pipe
;
491 buffer
->base
.destroy
= vl_video_buffer_destroy
;
492 buffer
->base
.get_sampler_view_planes
= vl_video_buffer_sampler_view_planes
;
493 buffer
->base
.get_sampler_view_components
= vl_video_buffer_sampler_view_components
;
494 buffer
->base
.get_surfaces
= vl_video_buffer_surfaces
;
495 buffer
->num_planes
= 0;
497 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
498 buffer
->resources
[i
] = resources
[i
];
500 buffer
->num_planes
++;
503 return &buffer
->base
;