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