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
vl_get_video_buffer_formats(struct pipe_screen * screen,enum pipe_format format,enum pipe_format out_format[VL_NUM_COMPONENTS])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 *
vl_video_buffer_plane_order(enum pipe_format format)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_NV21:
80 case PIPE_FORMAT_Y8_U8_V8_444_UNORM:
81 case PIPE_FORMAT_Y8_U8_V8_440_UNORM:
82 case PIPE_FORMAT_R8G8B8A8_UNORM:
83 case PIPE_FORMAT_R8G8B8X8_UNORM:
84 case PIPE_FORMAT_B8G8R8A8_UNORM:
85 case PIPE_FORMAT_B8G8R8X8_UNORM:
86 case PIPE_FORMAT_R10G10B10A2_UNORM:
87 case PIPE_FORMAT_R10G10B10X2_UNORM:
88 case PIPE_FORMAT_B10G10R10A2_UNORM:
89 case PIPE_FORMAT_B10G10R10X2_UNORM:
90 case PIPE_FORMAT_YUYV:
91 case PIPE_FORMAT_UYVY:
92 case PIPE_FORMAT_P010:
93 case PIPE_FORMAT_P012:
94 case PIPE_FORMAT_P016:
95 case PIPE_FORMAT_Y8_400_UNORM:
96 case PIPE_FORMAT_IYUV:
97 return const_resource_plane_order_YUV;
98
99 default:
100 assert(0);
101 return NULL;
102 }
103 }
104
105 static enum pipe_format
vl_video_buffer_surface_format(enum pipe_format format)106 vl_video_buffer_surface_format(enum pipe_format format)
107 {
108 const struct util_format_description *desc = util_format_description(format);
109
110 /* a subsampled formats can't work as surface use RGBA instead */
111 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
112 return PIPE_FORMAT_R8G8B8A8_UNORM;
113
114 return format;
115 }
116
117 bool
vl_video_buffer_is_format_supported(struct pipe_screen * screen,enum pipe_format format,enum pipe_video_profile profile,enum pipe_video_entrypoint entrypoint)118 vl_video_buffer_is_format_supported(struct pipe_screen *screen,
119 enum pipe_format format,
120 enum pipe_video_profile profile,
121 enum pipe_video_entrypoint entrypoint)
122 {
123 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
124 unsigned i;
125
126 if (entrypoint == PIPE_VIDEO_ENTRYPOINT_PROCESSING && format == PIPE_FORMAT_R8_G8_B8_UNORM)
127 return false;
128
129 vl_get_video_buffer_formats(screen, format, resource_formats);
130
131 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
132 enum pipe_format fmt = resource_formats[i];
133
134 if (fmt == PIPE_FORMAT_NONE)
135 continue;
136
137 /* we at least need to sample from it */
138 if (!screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))
139 continue;
140
141 fmt = vl_video_buffer_surface_format(fmt);
142 if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET))
143 return true;
144 }
145
146 return false;
147 }
148
149 unsigned
vl_video_buffer_max_size(struct pipe_screen * screen)150 vl_video_buffer_max_size(struct pipe_screen *screen)
151 {
152 return screen->caps.max_texture_2d_size;
153 }
154
155 void
vl_video_buffer_set_associated_data(struct pipe_video_buffer * vbuf,struct pipe_video_codec * vcodec,void * associated_data,void (* destroy_associated_data)(void *))156 vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
157 struct pipe_video_codec *vcodec,
158 void *associated_data,
159 void (*destroy_associated_data)(void *))
160 {
161 vbuf->codec = vcodec;
162
163 if (vbuf->associated_data == associated_data)
164 return;
165
166 if (vbuf->associated_data)
167 vbuf->destroy_associated_data(vbuf->associated_data);
168
169 vbuf->associated_data = associated_data;
170 vbuf->destroy_associated_data = destroy_associated_data;
171 }
172
173 void *
vl_video_buffer_get_associated_data(struct pipe_video_buffer * vbuf,struct pipe_video_codec * vcodec)174 vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
175 struct pipe_video_codec *vcodec)
176 {
177 if (vbuf->codec == vcodec)
178 return vbuf->associated_data;
179 else
180 return NULL;
181 }
182
183 void
vl_video_buffer_template(struct pipe_resource * templ,const struct pipe_video_buffer * tmpl,enum pipe_format resource_format,unsigned depth,unsigned array_size,unsigned usage,unsigned plane,enum pipe_video_chroma_format chroma_format)184 vl_video_buffer_template(struct pipe_resource *templ,
185 const struct pipe_video_buffer *tmpl,
186 enum pipe_format resource_format,
187 unsigned depth, unsigned array_size,
188 unsigned usage, unsigned plane,
189 enum pipe_video_chroma_format chroma_format)
190 {
191 unsigned height = tmpl->height;
192
193 memset(templ, 0, sizeof(*templ));
194 if (depth > 1)
195 templ->target = PIPE_TEXTURE_3D;
196 else if (array_size > 1)
197 templ->target = PIPE_TEXTURE_2D_ARRAY;
198 else
199 templ->target = PIPE_TEXTURE_2D;
200 templ->format = resource_format;
201 templ->width0 = tmpl->width;
202 templ->depth0 = depth;
203 templ->array_size = array_size;
204 templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
205 templ->usage = usage;
206 templ->flags = tmpl->flags;
207
208 vl_video_buffer_adjust_size(&templ->width0, &height, plane,
209 chroma_format, false);
210 templ->height0 = height;
211 }
212
213 void
vl_video_buffer_destroy(struct pipe_video_buffer * buffer)214 vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
215 {
216 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
217 unsigned i;
218
219 assert(buf);
220
221 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
222 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
223 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
224 pipe_resource_reference(&buf->resources[i], NULL);
225 }
226
227 for (i = 0; i < VL_MAX_SURFACES; ++i)
228 pipe_surface_reference(&buf->surfaces[i], NULL);
229
230 vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
231
232 FREE(buffer);
233 }
234
235 static void
vl_video_buffer_resources(struct pipe_video_buffer * buffer,struct pipe_resource ** resources)236 vl_video_buffer_resources(struct pipe_video_buffer *buffer,
237 struct pipe_resource **resources)
238 {
239 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
240 unsigned num_planes = util_format_get_num_planes(buffer->buffer_format);
241 unsigned i;
242
243 assert(buf);
244
245 for (i = 0; i < num_planes; ++i) {
246 resources[i] = buf->resources[i];
247 }
248 }
249
250 static struct pipe_sampler_view **
vl_video_buffer_sampler_view_planes(struct pipe_video_buffer * buffer)251 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
252 {
253 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
254 unsigned num_planes = util_format_get_num_planes(buffer->buffer_format);
255 struct pipe_sampler_view sv_templ;
256 struct pipe_context *pipe;
257 unsigned i;
258
259 assert(buf);
260
261 pipe = buf->base.context;
262
263 for (i = 0; i < num_planes; ++i ) {
264 if (!buf->sampler_view_planes[i]) {
265 memset(&sv_templ, 0, sizeof(sv_templ));
266 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
267
268 if (util_format_get_nr_components(buf->resources[i]->format) == 1)
269 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;
270
271 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
272 if (!buf->sampler_view_planes[i])
273 goto error;
274 }
275 }
276
277 return buf->sampler_view_planes;
278
279 error:
280 for (i = 0; i < num_planes; ++i )
281 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
282
283 return NULL;
284 }
285
286 static struct pipe_sampler_view **
vl_video_buffer_sampler_view_components(struct pipe_video_buffer * buffer)287 vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
288 {
289 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
290 struct pipe_sampler_view sv_templ;
291 struct pipe_context *pipe;
292 enum pipe_format sampler_format[VL_NUM_COMPONENTS];
293 const unsigned *plane_order;
294 unsigned i, j, component, num_planes;
295
296 assert(buf);
297
298 pipe = buf->base.context;
299
300 vl_get_video_buffer_formats(pipe->screen, buf->base.buffer_format, sampler_format);
301 plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);
302 num_planes = util_format_get_num_planes(buf->base.buffer_format);
303
304 for (component = 0, i = 0; i < num_planes; ++i) {
305 struct pipe_resource *res = buf->resources[plane_order[i]];
306 const struct util_format_description *desc = util_format_description(res->format);
307 unsigned nr_components = util_format_get_nr_components(res->format);
308 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
309 nr_components = 3;
310
311 for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {
312 unsigned pipe_swizzle;
313
314 if (buf->sampler_view_components[component])
315 continue;
316
317 memset(&sv_templ, 0, sizeof(sv_templ));
318 u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);
319 pipe_swizzle = (buf->base.buffer_format == PIPE_FORMAT_YUYV || buf->base.buffer_format == PIPE_FORMAT_UYVY) ?
320 (PIPE_SWIZZLE_X + j + 1) % 3 :
321 (PIPE_SWIZZLE_X + j);
322 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = pipe_swizzle;
323 sv_templ.swizzle_a = PIPE_SWIZZLE_1;
324
325 buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
326 if (!buf->sampler_view_components[component])
327 goto error;
328 }
329 }
330
331 assert(component != 0);
332
333 for (i = component; i < VL_NUM_COMPONENTS; ++i)
334 pipe_sampler_view_reference(&buf->sampler_view_components[i], buf->sampler_view_components[component - 1]);
335
336 return buf->sampler_view_components;
337
338 error:
339 for (i = 0; i < VL_NUM_COMPONENTS; ++i )
340 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
341
342 return NULL;
343 }
344
345 static struct pipe_surface **
vl_video_buffer_surfaces(struct pipe_video_buffer * buffer)346 vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
347 {
348 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
349 struct pipe_surface surf_templ;
350 struct pipe_context *pipe;
351 unsigned i, j, array_size, surf;
352
353 assert(buf);
354
355 pipe = buf->base.context;
356
357 array_size = buffer->interlaced ? 2 : 1;
358 for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {
359 for (j = 0; j < array_size; ++j, ++surf) {
360 assert(surf < VL_MAX_SURFACES);
361
362 if (!buf->resources[i]) {
363 pipe_surface_reference(&buf->surfaces[surf], NULL);
364 continue;
365 }
366
367 if (!buf->surfaces[surf]) {
368 memset(&surf_templ, 0, sizeof(surf_templ));
369 surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);
370 surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;
371 buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
372 if (!buf->surfaces[surf])
373 goto error;
374 }
375 }
376 }
377
378 return buf->surfaces;
379
380 error:
381 for (i = 0; i < VL_MAX_SURFACES; ++i )
382 pipe_surface_reference(&buf->surfaces[i], NULL);
383
384 return NULL;
385 }
386
387 struct pipe_video_buffer *
vl_video_buffer_create(struct pipe_context * pipe,const struct pipe_video_buffer * tmpl)388 vl_video_buffer_create(struct pipe_context *pipe,
389 const struct pipe_video_buffer *tmpl)
390 {
391 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
392 struct pipe_video_buffer templat, *result;
393 bool pot_buffers;
394
395 assert(pipe);
396 assert(tmpl->width > 0 && tmpl->height > 0);
397
398 pot_buffers = !pipe->screen->get_video_param
399 (
400 pipe->screen,
401 PIPE_VIDEO_PROFILE_UNKNOWN,
402 PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
403 PIPE_VIDEO_CAP_NPOT_TEXTURES
404 );
405
406 vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);
407
408 templat = *tmpl;
409 templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
410 : align(tmpl->width, VL_MACROBLOCK_WIDTH);
411 templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
412 : align(tmpl->height, VL_MACROBLOCK_HEIGHT);
413
414 if (tmpl->interlaced)
415 templat.height /= 2;
416
417 result = vl_video_buffer_create_ex
418 (
419 pipe, &templat, resource_formats,
420 1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT,
421 pipe_format_to_chroma_format(templat.buffer_format)
422 );
423
424
425 if (result && tmpl->interlaced)
426 result->height *= 2;
427
428 return result;
429 }
430
431 struct pipe_video_buffer *
vl_video_buffer_create_ex(struct pipe_context * pipe,const struct pipe_video_buffer * tmpl,const enum pipe_format resource_formats[VL_NUM_COMPONENTS],unsigned depth,unsigned array_size,unsigned usage,enum pipe_video_chroma_format chroma_format)432 vl_video_buffer_create_ex(struct pipe_context *pipe,
433 const struct pipe_video_buffer *tmpl,
434 const enum pipe_format resource_formats[VL_NUM_COMPONENTS],
435 unsigned depth, unsigned array_size, unsigned usage,
436 enum pipe_video_chroma_format chroma_format)
437 {
438 struct pipe_resource res_tmpl;
439 struct pipe_resource *resources[VL_NUM_COMPONENTS];
440 unsigned i;
441
442 assert(pipe);
443
444 memset(resources, 0, sizeof resources);
445
446 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size,
447 usage, 0, chroma_format);
448 resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
449 if (!resources[0])
450 goto error;
451
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);
455 }
456
457 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size,
458 usage, 1, chroma_format);
459 resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
460 if (!resources[1])
461 goto error;
462
463 if (resource_formats[2] == PIPE_FORMAT_NONE)
464 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
465
466 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size,
467 usage, 2, chroma_format);
468 resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
469 if (!resources[2])
470 goto error;
471
472 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
473
474 error:
475 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
476 pipe_resource_reference(&resources[i], NULL);
477
478 return NULL;
479 }
480
481 struct pipe_video_buffer *
vl_video_buffer_create_ex2(struct pipe_context * pipe,const struct pipe_video_buffer * tmpl,struct pipe_resource * resources[VL_NUM_COMPONENTS])482 vl_video_buffer_create_ex2(struct pipe_context *pipe,
483 const struct pipe_video_buffer *tmpl,
484 struct pipe_resource *resources[VL_NUM_COMPONENTS])
485 {
486 struct vl_video_buffer *buffer;
487 unsigned i, num_planes;
488
489 buffer = CALLOC_STRUCT(vl_video_buffer);
490 if (!buffer)
491 return NULL;
492
493 num_planes = util_format_get_num_planes(tmpl->buffer_format);
494
495 buffer->base = *tmpl;
496 buffer->base.context = pipe;
497 buffer->base.destroy = vl_video_buffer_destroy;
498 buffer->base.get_resources = vl_video_buffer_resources;
499 buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
500 buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
501 buffer->base.get_surfaces = vl_video_buffer_surfaces;
502
503 for (i = 0; i < num_planes; ++i)
504 buffer->resources[i] = resources[i];
505
506 /* Ignore auxiliary planes. */
507 for (; i < VL_NUM_COMPONENTS; ++i) {
508 struct pipe_resource *res = resources[i];
509 pipe_resource_reference(&res, NULL);
510 }
511
512 return &buffer->base;
513 }
514
515 /* Create pipe_video_buffer by using resource_create with planar formats. */
516 struct pipe_video_buffer *
vl_video_buffer_create_as_resource(struct pipe_context * pipe,const struct pipe_video_buffer * tmpl,const uint64_t * modifiers,int modifiers_count)517 vl_video_buffer_create_as_resource(struct pipe_context *pipe,
518 const struct pipe_video_buffer *tmpl,
519 const uint64_t *modifiers,
520 int modifiers_count)
521 {
522 struct pipe_resource templ, *resources[VL_NUM_COMPONENTS] = {0};
523 unsigned array_size = tmpl->interlaced ? 2 : 1;
524
525 memset(&templ, 0, sizeof(templ));
526 templ.target = array_size > 1 ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
527 templ.width0 = align(tmpl->width, VL_MACROBLOCK_WIDTH);
528 templ.height0 = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
529 templ.depth0 = 1;
530 templ.array_size = array_size;
531 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
532 templ.usage = PIPE_USAGE_DEFAULT;
533 templ.flags = tmpl->flags;
534
535 if (tmpl->buffer_format == PIPE_FORMAT_YUYV)
536 templ.format = PIPE_FORMAT_R8G8_R8B8_UNORM;
537 else if (tmpl->buffer_format == PIPE_FORMAT_UYVY)
538 templ.format = PIPE_FORMAT_G8R8_B8R8_UNORM;
539 else
540 templ.format = tmpl->buffer_format;
541
542 if (modifiers)
543 resources[0] = pipe->screen->resource_create_with_modifiers(pipe->screen,
544 &templ, modifiers,
545 modifiers_count);
546 else
547 resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
548 if (!resources[0])
549 return NULL;
550
551 if (resources[0]->next) {
552 pipe_resource_reference(&resources[1], resources[0]->next);
553 if (resources[1]->next)
554 pipe_resource_reference(&resources[2], resources[1]->next);
555 }
556
557 struct pipe_video_buffer vidtemplate = *tmpl;
558 vidtemplate.width = templ.width0;
559 vidtemplate.height = templ.height0 * array_size;
560 vidtemplate.contiguous_planes = true;
561 return vl_video_buffer_create_ex2(pipe, &vidtemplate, resources);
562 }
563