vl_video_buffer.c 16.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**************************************************************************
 *
 * Copyright 2011 Christian König.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
Jose Fonseca's avatar
Jose Fonseca committed
21
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22
23
24
25
26
27
28
29
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <assert.h>

Kai Wasserbäch's avatar
Kai Wasserbäch committed
30
31
32
#include "pipe/p_screen.h"
#include "pipe/p_context.h"
#include "pipe/p_state.h"
33

34
#include "util/format/u_format.h"
Kai Wasserbäch's avatar
Kai Wasserbäch committed
35
36
37
#include "util/u_inlines.h"
#include "util/u_sampler.h"
#include "util/u_memory.h"
38

39
#include "vl_video_buffer.h"
40

Christian König's avatar
Christian König committed
41
42
43
44
45
46
47
48
49
50
51
52
const unsigned const_resource_plane_order_YUV[3] = {
   0,
   1,
   2
};

const unsigned const_resource_plane_order_YVU[3] = {
   0,
   2,
   1
};

53
54
55
void
vl_get_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format,
                            enum pipe_format out_format[VL_NUM_COMPONENTS])
56
{
57
58
   unsigned num_planes = util_format_get_num_planes(format);
   unsigned i;
59

60
61
62
63
   for (i = 0; i < num_planes; i++)
      out_format[i] = util_format_get_plane_format(format, i);
   for (; i < VL_NUM_COMPONENTS; i++)
      out_format[i] = PIPE_FORMAT_NONE;
64

65
66
67
68
   if (format == PIPE_FORMAT_YUYV)
      out_format[0] = PIPE_FORMAT_R8G8_R8B8_UNORM;
   else if (format == PIPE_FORMAT_UYVY)
      out_format[0] = PIPE_FORMAT_G8R8_B8R8_UNORM;
69
70
}

Christian König's avatar
Christian König committed
71
72
73
const unsigned *
vl_video_buffer_plane_order(enum pipe_format format)
{
74
   switch(format) {
Christian König's avatar
Christian König committed
75
76
77
78
   case PIPE_FORMAT_YV12:
      return const_resource_plane_order_YVU;

   case PIPE_FORMAT_NV12:
79
80
   case PIPE_FORMAT_R8G8B8A8_UNORM:
   case PIPE_FORMAT_B8G8R8A8_UNORM:
81
82
   case PIPE_FORMAT_YUYV:
   case PIPE_FORMAT_UYVY:
83
   case PIPE_FORMAT_P010:
84
   case PIPE_FORMAT_P016:
Christian König's avatar
Christian König committed
85
86
87
88
89
90
91
      return const_resource_plane_order_YUV;

   default:
      return NULL;
   }
}

92
93
94
95
96
97
98
99
100
101
102
103
static enum pipe_format
vl_video_buffer_surface_format(enum pipe_format format)
{
   const struct util_format_description *desc = util_format_description(format);

   /* a subsampled formats can't work as surface use RGBA instead */
   if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
      return PIPE_FORMAT_R8G8B8A8_UNORM;

   return format;
}

104
bool
105
106
vl_video_buffer_is_format_supported(struct pipe_screen *screen,
                                    enum pipe_format format,
107
108
                                    enum pipe_video_profile profile,
                                    enum pipe_video_entrypoint entrypoint)
109
{
110
   enum pipe_format resource_formats[VL_NUM_COMPONENTS];
111
112
   unsigned i;

113
   vl_get_video_buffer_formats(screen, format, resource_formats);
114

115
   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
116
117
118
      enum pipe_format format = resource_formats[i];

      if (format == PIPE_FORMAT_NONE)
119
120
         continue;

121
      /* we at least need to sample from it */
122
      if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))
123
124
125
         return false;

      format = vl_video_buffer_surface_format(format);
126
      if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET))
127
128
129
130
131
132
         return false;
   }

   return true;
}

133
134
135
unsigned
vl_video_buffer_max_size(struct pipe_screen *screen)
{
136
   return screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
137
138
}

139
140
void
vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
141
                                    struct pipe_video_codec *vcodec,
142
143
144
                                    void *associated_data,
                                    void (*destroy_associated_data)(void *))
{
145
   vbuf->codec = vcodec;
146
147
148
149
150
151
152
153
154
155
156
157
158

   if (vbuf->associated_data == associated_data)
      return;

   if (vbuf->associated_data)
      vbuf->destroy_associated_data(vbuf->associated_data);

   vbuf->associated_data = associated_data;
   vbuf->destroy_associated_data = destroy_associated_data;
}

void *
vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
159
                                    struct pipe_video_codec *vcodec)
160
{
161
   if (vbuf->codec == vcodec)
162
163
164
165
166
      return vbuf->associated_data;
   else
      return NULL;
}

167
void
168
169
170
vl_video_buffer_template(struct pipe_resource *templ,
                         const struct pipe_video_buffer *tmpl,
                         enum pipe_format resource_format,
171
                         unsigned depth, unsigned array_size,
172
173
                         unsigned usage, unsigned plane,
                         enum pipe_video_chroma_format chroma_format)
174
{
175
176
   unsigned height = tmpl->height;

177
   memset(templ, 0, sizeof(*templ));
178
179
180
181
182
183
   if (depth > 1)
      templ->target = PIPE_TEXTURE_3D;
   else if (array_size > 1)
      templ->target = PIPE_TEXTURE_2D_ARRAY;
   else
      templ->target = PIPE_TEXTURE_2D;
184
185
   templ->format = resource_format;
   templ->width0 = tmpl->width;
186
   templ->depth0 = depth;
187
   templ->array_size = array_size;
188
   templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
189
190
   templ->usage = usage;

191
   vl_video_buffer_adjust_size(&templ->width0, &height, plane,
192
                               chroma_format, false);
193
   templ->height0 = height;
194
195
}

196
197
198
199
200
201
202
203
static void
vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
{
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
   unsigned i;

   assert(buf);

204
   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
205
206
      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
207
208
      pipe_resource_reference(&buf->resources[i], NULL);
   }
209

210
   for (i = 0; i < VL_MAX_SURFACES; ++i)
211
212
      pipe_surface_reference(&buf->surfaces[i], NULL);

213
   vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
214
215

   FREE(buffer);
216
217
218
}

static struct pipe_sampler_view **
219
vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
220
{
221
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
222
   unsigned num_planes = util_format_get_num_planes(buffer->buffer_format);
223
224
225
226
   struct pipe_sampler_view sv_templ;
   struct pipe_context *pipe;
   unsigned i;

227
   assert(buf);
228

229
   pipe = buf->base.context;
230

231
   for (i = 0; i < num_planes; ++i ) {
232
      if (!buf->sampler_view_planes[i]) {
233
         memset(&sv_templ, 0, sizeof(sv_templ));
234
         u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
235
236

         if (util_format_get_nr_components(buf->resources[i]->format) == 1)
237
            sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;
238
239
240

         buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
         if (!buf->sampler_view_planes[i])
241
242
243
244
            goto error;
      }
   }

245
   return buf->sampler_view_planes;
246
247

error:
248
   for (i = 0; i < num_planes; ++i )
249
250
251
252
253
254
255
256
257
258
259
      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);

   return NULL;
}

static struct pipe_sampler_view **
vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
{
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
   struct pipe_sampler_view sv_templ;
   struct pipe_context *pipe;
260
   enum pipe_format sampler_format[VL_NUM_COMPONENTS];
Christian König's avatar
Christian König committed
261
   const unsigned *plane_order;
262
263
264
265
   unsigned i, j, component;

   assert(buf);

266
   pipe = buf->base.context;
267

268
   vl_get_video_buffer_formats(pipe->screen, buf->base.buffer_format, sampler_format);
Christian König's avatar
Christian König committed
269
270
   plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);

271
   for (component = 0, i = 0; i < buf->num_planes; ++i ) {
Christian König's avatar
Christian König committed
272
      struct pipe_resource *res = buf->resources[plane_order[i]];
273
      const struct util_format_description *desc = util_format_description(res->format);
Christian König's avatar
Christian König committed
274
      unsigned nr_components = util_format_get_nr_components(res->format);
275
276
      if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
         nr_components = 3;
277

278
279
280
      for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {
         if (buf->sampler_view_components[component])
            continue;
281

282
283
         memset(&sv_templ, 0, sizeof(sv_templ));
         u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);
284
285
         sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_X + j;
         sv_templ.swizzle_a = PIPE_SWIZZLE_1;
286
287
288
         buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
         if (!buf->sampler_view_components[component])
            goto error;
289
290
      }
   }
291
   assert(component == VL_NUM_COMPONENTS);
292
293
294
295

   return buf->sampler_view_components;

error:
296
   for (i = 0; i < VL_NUM_COMPONENTS; ++i )
297
      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
298
299
300
301

   return NULL;
}

302
303
static struct pipe_surface **
vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
304
{
305
   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
306
307
   struct pipe_surface surf_templ;
   struct pipe_context *pipe;
308
   unsigned i, j, array_size, surf;
309

310
   assert(buf);
311

312
   pipe = buf->base.context;
313

314
   array_size = buffer->interlaced ? 2 : 1;
315
   for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {
316
      for (j = 0; j < array_size; ++j, ++surf) {
317
         assert(surf < VL_MAX_SURFACES);
318

319
         if (!buf->resources[i]) {
320
321
322
323
            pipe_surface_reference(&buf->surfaces[surf], NULL);
            continue;
         }

324
325
         if (!buf->surfaces[surf]) {
            memset(&surf_templ, 0, sizeof(surf_templ));
326
327
328
            surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);
            surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;
            buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
329
            if (!buf->surfaces[surf])
330
331
               goto error;
         }
332
333
334
      }
   }

335
   return buf->surfaces;
336
337

error:
338
   for (i = 0; i < VL_MAX_SURFACES; ++i )
339
      pipe_surface_reference(&buf->surfaces[i], NULL);
340
341
342
343

   return NULL;
}

344
struct pipe_video_buffer *
345
vl_video_buffer_create(struct pipe_context *pipe,
346
                       const struct pipe_video_buffer *tmpl)
347
{
348
   enum pipe_format resource_formats[VL_NUM_COMPONENTS];
349
   struct pipe_video_buffer templat, *result;
350
351
352
   bool pot_buffers;

   assert(pipe);
353
   assert(tmpl->width > 0 && tmpl->height > 0);
354
355
356
357
358

   pot_buffers = !pipe->screen->get_video_param
   (
      pipe->screen,
      PIPE_VIDEO_PROFILE_UNKNOWN,
359
      PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
360
361
362
      PIPE_VIDEO_CAP_NPOT_TEXTURES
   );

363
   vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);
364

365
366
   templat = *tmpl;
   templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
367
                 : align(tmpl->width, VL_MACROBLOCK_WIDTH);
368
   templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
369
                  : align(tmpl->height, VL_MACROBLOCK_HEIGHT);
370

371
372
373
   if (tmpl->interlaced)
      templat.height /= 2;

374
   result = vl_video_buffer_create_ex
375
   (
376
      pipe, &templat, resource_formats,
377
378
      1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT,
      pipe_format_to_chroma_format(templat.buffer_format)
379
   );
380
381
382
383
384
385


   if (result && tmpl->interlaced)
      result->height *= 2;

   return result;
386
387
388
389
}

struct pipe_video_buffer *
vl_video_buffer_create_ex(struct pipe_context *pipe,
390
                          const struct pipe_video_buffer *tmpl,
391
                          const enum pipe_format resource_formats[VL_NUM_COMPONENTS],
392
393
                          unsigned depth, unsigned array_size, unsigned usage,
                          enum pipe_video_chroma_format chroma_format)
394
{
395
   struct pipe_resource res_tmpl;
396
   struct pipe_resource *resources[VL_NUM_COMPONENTS];
397
398
   unsigned i;

399
   assert(pipe);
400

401
402
   memset(resources, 0, sizeof resources);

403
404
   vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size,
                            usage, 0, chroma_format);
405
406
   resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
   if (!resources[0])
407
408
      goto error;

409
410
   if (resource_formats[1] == PIPE_FORMAT_NONE) {
      assert(resource_formats[2] == PIPE_FORMAT_NONE);
411
      return vl_video_buffer_create_ex2(pipe, tmpl, resources);
412
   }
413

414
415
   vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size,
                            usage, 1, chroma_format);
416
417
   resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
   if (!resources[1])
418
419
      goto error;

420
   if (resource_formats[2] == PIPE_FORMAT_NONE)
421
      return vl_video_buffer_create_ex2(pipe, tmpl, resources);
422

423
424
   vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size,
                            usage, 2, chroma_format);
425
426
   resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
   if (!resources[2])
427
428
      goto error;

429
   return vl_video_buffer_create_ex2(pipe, tmpl, resources);
430
431

error:
432
   for (i = 0; i < VL_NUM_COMPONENTS; ++i)
433
      pipe_resource_reference(&resources[i], NULL);
434
435

   return NULL;
436
}
437
438
439
440

struct pipe_video_buffer *
vl_video_buffer_create_ex2(struct pipe_context *pipe,
                           const struct pipe_video_buffer *tmpl,
441
                           struct pipe_resource *resources[VL_NUM_COMPONENTS])
442
443
444
445
446
{
   struct vl_video_buffer *buffer;
   unsigned i;

   buffer = CALLOC_STRUCT(vl_video_buffer);
447
448
   if (!buffer)
      return NULL;
449
450
451
452
453
454
455
456
457

   buffer->base = *tmpl;
   buffer->base.context = pipe;
   buffer->base.destroy = vl_video_buffer_destroy;
   buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
   buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
   buffer->base.get_surfaces = vl_video_buffer_surfaces;
   buffer->num_planes = 0;

458
   for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
459
460
461
462
463
464
465
      buffer->resources[i] = resources[i];
      if (resources[i])
         buffer->num_planes++;
   }

   return &buffer->base;
}
466
467
468
469

/* Create pipe_video_buffer by using resource_create with planar formats. */
struct pipe_video_buffer *
vl_video_buffer_create_as_resource(struct pipe_context *pipe,
470
471
472
                                   const struct pipe_video_buffer *tmpl,
                                   const uint64_t *modifiers,
                                   int modifiers_count)
473
{
474
   struct pipe_resource templ, *resources[VL_NUM_COMPONENTS] = {0};
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
   unsigned array_size =  tmpl->interlaced ? 2 : 1;

   memset(&templ, 0, sizeof(templ));
   templ.target = array_size > 1 ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
   templ.width0 = align(tmpl->width, VL_MACROBLOCK_WIDTH);
   templ.height0 = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
   templ.depth0 = 1;
   templ.array_size = array_size;
   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
   templ.usage = PIPE_USAGE_DEFAULT;

   if (tmpl->buffer_format == PIPE_FORMAT_YUYV)
      templ.format = PIPE_FORMAT_R8G8_R8B8_UNORM;
   else if (tmpl->buffer_format == PIPE_FORMAT_UYVY)
      templ.format = PIPE_FORMAT_G8R8_B8R8_UNORM;
   else
      templ.format = tmpl->buffer_format;

493
494
495
496
497
498
   if (modifiers)
      resources[0] = pipe->screen->resource_create_with_modifiers(pipe->screen,
                                                                  &templ, modifiers,
                                                                  modifiers_count);
   else
      resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
499
500
501
502
503
504
505
506
507
508
509
510
511
512
   if (!resources[0])
      return NULL;

   if (resources[0]->next) {
      pipe_resource_reference(&resources[1], resources[0]->next);
      if (resources[1]->next)
         pipe_resource_reference(&resources[2], resources[1]->next);
   }

   struct pipe_video_buffer vidtemplate = *tmpl;
   vidtemplate.width = templ.width0;
   vidtemplate.height = templ.height0 * array_size;
   return vl_video_buffer_create_ex2(pipe, &vidtemplate, resources);
}