ed331c94ad07e3382909c41e6011856664f4504c
[mesa.git] / src / gallium / auxiliary / vl / vl_video_buffer.c
1 /**************************************************************************
2 *
3 * Copyright 2011 Christian König.
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 VMWARE 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.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33
34 #include "util/format/u_format.h"
35 #include "util/u_inlines.h"
36 #include "util/u_sampler.h"
37 #include "util/u_memory.h"
38
39 #include "vl_video_buffer.h"
40
41 const unsigned const_resource_plane_order_YUV[3] = {
42 0,
43 1,
44 2
45 };
46
47 const unsigned const_resource_plane_order_YVU[3] = {
48 0,
49 2,
50 1
51 };
52
53 void
54 vl_get_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format,
55 enum pipe_format out_format[VL_NUM_COMPONENTS])
56 {
57 unsigned num_planes = util_format_get_num_planes(format);
58 unsigned i;
59
60 for (i = 0; i < num_planes; i++)
61 out_format[i] = util_format_get_plane_format(format, i);
62 for (; i < VL_NUM_COMPONENTS; i++)
63 out_format[i] = PIPE_FORMAT_NONE;
64
65 if (format == PIPE_FORMAT_YUYV)
66 out_format[0] = PIPE_FORMAT_R8G8_R8B8_UNORM;
67 else if (format == PIPE_FORMAT_UYVY)
68 out_format[0] = PIPE_FORMAT_G8R8_B8R8_UNORM;
69 }
70
71 const unsigned *
72 vl_video_buffer_plane_order(enum pipe_format format)
73 {
74 switch(format) {
75 case PIPE_FORMAT_YV12:
76 return const_resource_plane_order_YVU;
77
78 case PIPE_FORMAT_NV12:
79 case PIPE_FORMAT_R8G8B8A8_UNORM:
80 case PIPE_FORMAT_B8G8R8A8_UNORM:
81 case PIPE_FORMAT_YUYV:
82 case PIPE_FORMAT_UYVY:
83 case PIPE_FORMAT_P016:
84 return const_resource_plane_order_YUV;
85
86 default:
87 return NULL;
88 }
89 }
90
91 static enum pipe_format
92 vl_video_buffer_surface_format(enum pipe_format format)
93 {
94 const struct util_format_description *desc = util_format_description(format);
95
96 /* a subsampled formats can't work as surface use RGBA instead */
97 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
98 return PIPE_FORMAT_R8G8B8A8_UNORM;
99
100 return format;
101 }
102
103 bool
104 vl_video_buffer_is_format_supported(struct pipe_screen *screen,
105 enum pipe_format format,
106 enum pipe_video_profile profile,
107 enum pipe_video_entrypoint entrypoint)
108 {
109 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
110 unsigned i;
111
112 vl_get_video_buffer_formats(screen, format, resource_formats);
113
114 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
115 enum pipe_format format = resource_formats[i];
116
117 if (format == PIPE_FORMAT_NONE)
118 continue;
119
120 /* we at least need to sample from it */
121 if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))
122 return false;
123
124 format = vl_video_buffer_surface_format(format);
125 if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET))
126 return false;
127 }
128
129 return true;
130 }
131
132 unsigned
133 vl_video_buffer_max_size(struct pipe_screen *screen)
134 {
135 return screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
136 }
137
138 void
139 vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
140 struct pipe_video_codec *vcodec,
141 void *associated_data,
142 void (*destroy_associated_data)(void *))
143 {
144 vbuf->codec = vcodec;
145
146 if (vbuf->associated_data == associated_data)
147 return;
148
149 if (vbuf->associated_data)
150 vbuf->destroy_associated_data(vbuf->associated_data);
151
152 vbuf->associated_data = associated_data;
153 vbuf->destroy_associated_data = destroy_associated_data;
154 }
155
156 void *
157 vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
158 struct pipe_video_codec *vcodec)
159 {
160 if (vbuf->codec == vcodec)
161 return vbuf->associated_data;
162 else
163 return NULL;
164 }
165
166 void
167 vl_video_buffer_template(struct pipe_resource *templ,
168 const struct pipe_video_buffer *tmpl,
169 enum pipe_format resource_format,
170 unsigned depth, unsigned array_size,
171 unsigned usage, unsigned plane)
172 {
173 unsigned height = tmpl->height;
174
175 memset(templ, 0, sizeof(*templ));
176 if (depth > 1)
177 templ->target = PIPE_TEXTURE_3D;
178 else if (array_size > 1)
179 templ->target = PIPE_TEXTURE_2D_ARRAY;
180 else
181 templ->target = PIPE_TEXTURE_2D;
182 templ->format = resource_format;
183 templ->width0 = tmpl->width;
184 templ->depth0 = depth;
185 templ->array_size = array_size;
186 templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
187 templ->usage = usage;
188
189 vl_video_buffer_adjust_size(&templ->width0, &height, plane,
190 tmpl->chroma_format, false);
191 templ->height0 = height;
192 }
193
194 static void
195 vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
196 {
197 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
198 unsigned i;
199
200 assert(buf);
201
202 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
203 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
204 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
205 pipe_resource_reference(&buf->resources[i], NULL);
206 }
207
208 for (i = 0; i < VL_MAX_SURFACES; ++i)
209 pipe_surface_reference(&buf->surfaces[i], NULL);
210
211 vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
212
213 FREE(buffer);
214 }
215
216 static struct pipe_sampler_view **
217 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
218 {
219 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
220 struct pipe_sampler_view sv_templ;
221 struct pipe_context *pipe;
222 unsigned i;
223
224 assert(buf);
225
226 pipe = buf->base.context;
227
228 for (i = 0; i < buf->num_planes; ++i ) {
229 if (!buf->sampler_view_planes[i]) {
230 memset(&sv_templ, 0, sizeof(sv_templ));
231 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
232
233 if (util_format_get_nr_components(buf->resources[i]->format) == 1)
234 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;
235
236 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
237 if (!buf->sampler_view_planes[i])
238 goto error;
239 }
240 }
241
242 return buf->sampler_view_planes;
243
244 error:
245 for (i = 0; i < buf->num_planes; ++i )
246 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
247
248 return NULL;
249 }
250
251 static struct pipe_sampler_view **
252 vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
253 {
254 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
255 struct pipe_sampler_view sv_templ;
256 struct pipe_context *pipe;
257 enum pipe_format sampler_format[VL_NUM_COMPONENTS];
258 const unsigned *plane_order;
259 unsigned i, j, component;
260
261 assert(buf);
262
263 pipe = buf->base.context;
264
265 vl_get_video_buffer_formats(pipe->screen, buf->base.buffer_format, sampler_format);
266 plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);
267
268 for (component = 0, i = 0; i < buf->num_planes; ++i ) {
269 struct pipe_resource *res = buf->resources[plane_order[i]];
270 const struct util_format_description *desc = util_format_description(res->format);
271 unsigned nr_components = util_format_get_nr_components(res->format);
272 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
273 nr_components = 3;
274
275 for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {
276 if (buf->sampler_view_components[component])
277 continue;
278
279 memset(&sv_templ, 0, sizeof(sv_templ));
280 u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);
281 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_X + j;
282 sv_templ.swizzle_a = PIPE_SWIZZLE_1;
283 buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
284 if (!buf->sampler_view_components[component])
285 goto error;
286 }
287 }
288 assert(component == VL_NUM_COMPONENTS);
289
290 return buf->sampler_view_components;
291
292 error:
293 for (i = 0; i < VL_NUM_COMPONENTS; ++i )
294 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
295
296 return NULL;
297 }
298
299 static struct pipe_surface **
300 vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
301 {
302 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
303 struct pipe_surface surf_templ;
304 struct pipe_context *pipe;
305 unsigned i, j, array_size, surf;
306
307 assert(buf);
308
309 pipe = buf->base.context;
310
311 array_size = buffer->interlaced ? 2 : 1;
312 for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {
313 for (j = 0; j < array_size; ++j, ++surf) {
314 assert(surf < VL_MAX_SURFACES);
315
316 if (!buf->resources[i]) {
317 pipe_surface_reference(&buf->surfaces[surf], NULL);
318 continue;
319 }
320
321 if (!buf->surfaces[surf]) {
322 memset(&surf_templ, 0, sizeof(surf_templ));
323 surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);
324 surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;
325 buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
326 if (!buf->surfaces[surf])
327 goto error;
328 }
329 }
330 }
331
332 return buf->surfaces;
333
334 error:
335 for (i = 0; i < VL_MAX_SURFACES; ++i )
336 pipe_surface_reference(&buf->surfaces[i], NULL);
337
338 return NULL;
339 }
340
341 struct pipe_video_buffer *
342 vl_video_buffer_create(struct pipe_context *pipe,
343 const struct pipe_video_buffer *tmpl)
344 {
345 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
346 struct pipe_video_buffer templat, *result;
347 bool pot_buffers;
348
349 assert(pipe);
350 assert(tmpl->width > 0 && tmpl->height > 0);
351
352 pot_buffers = !pipe->screen->get_video_param
353 (
354 pipe->screen,
355 PIPE_VIDEO_PROFILE_UNKNOWN,
356 PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
357 PIPE_VIDEO_CAP_NPOT_TEXTURES
358 );
359
360 vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);
361
362 templat = *tmpl;
363 templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
364 : align(tmpl->width, VL_MACROBLOCK_WIDTH);
365 templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
366 : align(tmpl->height, VL_MACROBLOCK_HEIGHT);
367
368 if (tmpl->interlaced)
369 templat.height /= 2;
370
371 result = vl_video_buffer_create_ex
372 (
373 pipe, &templat, resource_formats,
374 1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT
375 );
376
377
378 if (result && tmpl->interlaced)
379 result->height *= 2;
380
381 return result;
382 }
383
384 struct pipe_video_buffer *
385 vl_video_buffer_create_ex(struct pipe_context *pipe,
386 const struct pipe_video_buffer *tmpl,
387 const enum pipe_format resource_formats[VL_NUM_COMPONENTS],
388 unsigned depth, unsigned array_size, unsigned usage)
389 {
390 struct pipe_resource res_tmpl;
391 struct pipe_resource *resources[VL_NUM_COMPONENTS];
392 unsigned i;
393
394 assert(pipe);
395
396 memset(resources, 0, sizeof resources);
397
398 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size, usage, 0);
399 resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
400 if (!resources[0])
401 goto error;
402
403 if (resource_formats[1] == PIPE_FORMAT_NONE) {
404 assert(resource_formats[2] == PIPE_FORMAT_NONE);
405 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
406 }
407
408 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size, usage, 1);
409 resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
410 if (!resources[1])
411 goto error;
412
413 if (resource_formats[2] == PIPE_FORMAT_NONE)
414 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
415
416 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size, usage, 2);
417 resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
418 if (!resources[2])
419 goto error;
420
421 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
422
423 error:
424 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
425 pipe_resource_reference(&resources[i], NULL);
426
427 return NULL;
428 }
429
430 struct pipe_video_buffer *
431 vl_video_buffer_create_ex2(struct pipe_context *pipe,
432 const struct pipe_video_buffer *tmpl,
433 struct pipe_resource *resources[VL_NUM_COMPONENTS])
434 {
435 struct vl_video_buffer *buffer;
436 unsigned i;
437
438 buffer = CALLOC_STRUCT(vl_video_buffer);
439 if (!buffer)
440 return NULL;
441
442 buffer->base = *tmpl;
443 buffer->base.context = pipe;
444 buffer->base.destroy = vl_video_buffer_destroy;
445 buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
446 buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
447 buffer->base.get_surfaces = vl_video_buffer_surfaces;
448 buffer->num_planes = 0;
449
450 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
451 buffer->resources[i] = resources[i];
452 if (resources[i])
453 buffer->num_planes++;
454 }
455
456 return &buffer->base;
457 }
458
459 /* Create pipe_video_buffer by using resource_create with planar formats. */
460 struct pipe_video_buffer *
461 vl_video_buffer_create_as_resource(struct pipe_context *pipe,
462 const struct pipe_video_buffer *tmpl)
463 {
464 struct pipe_resource templ, *resources[VL_NUM_COMPONENTS] = {};
465 unsigned array_size = tmpl->interlaced ? 2 : 1;
466
467 memset(&templ, 0, sizeof(templ));
468 templ.target = array_size > 1 ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
469 templ.width0 = align(tmpl->width, VL_MACROBLOCK_WIDTH);
470 templ.height0 = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
471 templ.depth0 = 1;
472 templ.array_size = array_size;
473 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
474 templ.usage = PIPE_USAGE_DEFAULT;
475
476 if (tmpl->buffer_format == PIPE_FORMAT_YUYV)
477 templ.format = PIPE_FORMAT_R8G8_R8B8_UNORM;
478 else if (tmpl->buffer_format == PIPE_FORMAT_UYVY)
479 templ.format = PIPE_FORMAT_G8R8_B8R8_UNORM;
480 else
481 templ.format = tmpl->buffer_format;
482
483 resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
484 if (!resources[0])
485 return NULL;
486
487 if (resources[0]->next) {
488 pipe_resource_reference(&resources[1], resources[0]->next);
489 if (resources[1]->next)
490 pipe_resource_reference(&resources[2], resources[1]->next);
491 }
492
493 struct pipe_video_buffer vidtemplate = *tmpl;
494 vidtemplate.width = templ.width0;
495 vidtemplate.height = templ.height0 * array_size;
496 return vl_video_buffer_create_ex2(pipe, &vidtemplate, resources);
497 }