vl_mpeg12_decoder.c revision 311eb749a1ab7ffd417bc456345d63eba75e3fec
1/**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 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.
25 *
26 **************************************************************************/
27
28#include <math.h>
29#include <assert.h>
30
31#include <util/u_memory.h>
32#include <util/u_rect.h>
33#include <util/u_video.h>
34
35#include "vl_mpeg12_decoder.h"
36#include "vl_defines.h"
37
38#define SCALE_FACTOR_SNORM (32768.0f / 256.0f)
39#define SCALE_FACTOR_SSCALED (1.0f / 256.0f)
40
41struct format_config {
42   enum pipe_format zscan_source_format;
43   enum pipe_format idct_source_format;
44   enum pipe_format mc_source_format;
45
46   float idct_scale;
47   float mc_scale;
48};
49
50static const struct format_config bitstream_format_config[] = {
51   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },
52   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },
53   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },
54   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }
55};
56
57static const unsigned num_bitstream_format_configs =
58   sizeof(bitstream_format_config) / sizeof(struct format_config);
59
60static const struct format_config idct_format_config[] = {
61   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SSCALED },
62   { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R16G16B16A16_SSCALED, 1.0f, SCALE_FACTOR_SSCALED },
63   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_FLOAT, 1.0f, SCALE_FACTOR_SNORM },
64   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R16G16B16A16_SNORM, 1.0f, SCALE_FACTOR_SNORM }
65};
66
67static const unsigned num_idct_format_configs =
68   sizeof(idct_format_config) / sizeof(struct format_config);
69
70static const struct format_config mc_format_config[] = {
71   //{ PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SSCALED, 0.0f, SCALE_FACTOR_SSCALED },
72   { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_NONE, PIPE_FORMAT_R16_SNORM, 0.0f, SCALE_FACTOR_SNORM }
73};
74
75static const unsigned num_mc_format_configs =
76   sizeof(mc_format_config) / sizeof(struct format_config);
77
78static bool
79init_zscan_buffer(struct vl_mpeg12_buffer *buffer)
80{
81   enum pipe_format formats[3];
82
83   struct pipe_sampler_view **source;
84   struct pipe_surface **destination;
85
86   struct vl_mpeg12_decoder *dec;
87
88   unsigned i;
89
90   assert(buffer);
91
92   dec = (struct vl_mpeg12_decoder*)buffer->base.decoder;
93
94   formats[0] = formats[1] = formats[2] = dec->zscan_source_format;
95   buffer->zscan_source = vl_video_buffer_init(dec->base.context, dec->pipe,
96                                               dec->blocks_per_line * BLOCK_WIDTH * BLOCK_HEIGHT,
97                                               dec->max_blocks / dec->blocks_per_line,
98                                               1, PIPE_VIDEO_CHROMA_FORMAT_444,
99                                               formats, PIPE_USAGE_STATIC);
100   if (!buffer->zscan_source)
101      goto error_source;
102
103   source = buffer->zscan_source->get_sampler_view_planes(buffer->zscan_source);
104   if (!source)
105      goto error_sampler;
106
107   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
108      destination = dec->idct_source->get_surfaces(dec->idct_source);
109   else
110      destination = dec->mc_source->get_surfaces(dec->mc_source);
111
112   if (!destination)
113      goto error_surface;
114
115   for (i = 0; i < VL_MAX_PLANES; ++i)
116      if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c,
117                                &buffer->zscan[i], source[i], destination[i]))
118         goto error_plane;
119
120   return true;
121
122error_plane:
123   for (; i > 0; --i)
124      vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);
125
126error_surface:
127error_sampler:
128   buffer->zscan_source->destroy(buffer->zscan_source);
129
130error_source:
131   return false;
132}
133
134static void
135cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)
136{
137   unsigned i;
138
139   assert(buffer);
140
141   for (i = 0; i < VL_MAX_PLANES; ++i)
142      vl_zscan_cleanup_buffer(&buffer->zscan[i]);
143   buffer->zscan_source->destroy(buffer->zscan_source);
144}
145
146static bool
147init_idct_buffer(struct vl_mpeg12_buffer *buffer)
148{
149   struct pipe_sampler_view **idct_source_sv, **mc_source_sv;
150   struct pipe_surface **idct_surfaces;
151
152   struct vl_mpeg12_decoder *dec;
153
154   unsigned i;
155
156   assert(buffer);
157
158   dec = (struct vl_mpeg12_decoder*)buffer->base.decoder;
159
160   idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source);
161   if (!idct_source_sv)
162      goto error_source_sv;
163
164   mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
165   if (!mc_source_sv)
166      goto error_mc_source_sv;
167
168   idct_surfaces = dec->mc_source->get_surfaces(dec->mc_source);
169   if (!idct_surfaces)
170      goto error_surfaces;
171
172   for (i = 0; i < 3; ++i)
173      if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
174                               &buffer->idct[i], idct_source_sv[i],
175                               mc_source_sv[i], idct_surfaces[i]))
176         goto error_plane;
177
178   return true;
179
180error_plane:
181   for (; i > 0; --i)
182      vl_idct_cleanup_buffer(i == 1 ? &dec->idct_c : &dec->idct_y, &buffer->idct[i - 1]);
183
184error_surfaces:
185error_mc_source_sv:
186error_source_sv:
187   return false;
188}
189
190static void
191cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
192{
193   struct vl_mpeg12_decoder *dec;
194   assert(buf);
195
196   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
197   assert(dec);
198
199   vl_idct_cleanup_buffer(&dec->idct_y, &buf->idct[0]);
200   vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[1]);
201   vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[2]);
202}
203
204static bool
205init_mc_buffer(struct vl_mpeg12_buffer *buf)
206{
207   struct vl_mpeg12_decoder *dec;
208
209   assert(buf);
210
211   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
212   assert(dec);
213
214   if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))
215      goto error_mc_y;
216
217   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))
218      goto error_mc_cb;
219
220   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))
221      goto error_mc_cr;
222
223   return true;
224
225error_mc_cr:
226   vl_mc_cleanup_buffer(&buf->mc[1]);
227
228error_mc_cb:
229   vl_mc_cleanup_buffer(&buf->mc[0]);
230
231error_mc_y:
232   return false;
233}
234
235static void
236cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
237{
238   unsigned i;
239
240   assert(buf);
241
242   for (i = 0; i < VL_MAX_PLANES; ++i)
243      vl_mc_cleanup_buffer(&buf->mc[i]);
244}
245
246static void
247vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer)
248{
249   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
250   struct vl_mpeg12_decoder *dec;
251
252   assert(buf);
253
254   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
255   assert(dec);
256
257   cleanup_zscan_buffer(buf);
258
259   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
260      cleanup_idct_buffer(buf);
261
262   cleanup_mc_buffer(buf);
263
264   vl_vb_cleanup(&buf->vertex_stream);
265
266   FREE(buf);
267}
268
269static void
270vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer)
271{
272   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
273   struct vl_mpeg12_decoder *dec;
274
275   struct pipe_sampler_view **sampler_views;
276   unsigned i;
277
278   assert(buf);
279
280   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
281   assert(dec);
282
283   vl_vb_map(&buf->vertex_stream, dec->pipe);
284
285   sampler_views = buf->zscan_source->get_sampler_view_planes(buf->zscan_source);
286
287   assert(sampler_views);
288
289   for (i = 0; i < VL_MAX_PLANES; ++i) {
290      struct pipe_resource *tex = sampler_views[i]->texture;
291      struct pipe_box rect =
292      {
293         0, 0, 0,
294         tex->width0,
295         tex->height0,
296         1
297      };
298
299      buf->tex_transfer[i] = dec->pipe->get_transfer
300      (
301         dec->pipe, tex,
302         0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
303         &rect
304      );
305
306      buf->texels[i] = dec->pipe->transfer_map(dec->pipe, buf->tex_transfer[i]);
307   }
308
309   if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
310      struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES];
311      struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES];
312
313      for (i = 0; i < VL_MAX_PLANES; ++i)
314         ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i);
315
316      for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
317         mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i);
318
319      vl_mpg12_bs_set_buffers(&buf->bs, ycbcr_stream, buf->texels, mv_stream);
320   } else {
321      for (i = 0; i < VL_MAX_PLANES; ++i)
322         vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear);
323   }
324}
325
326static struct pipe_ycbcr_block *
327vl_mpeg12_buffer_get_ycbcr_stream(struct pipe_video_decode_buffer *buffer, int component)
328{
329   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
330
331   assert(buf);
332
333   return vl_vb_get_ycbcr_stream(&buf->vertex_stream, component);
334}
335
336static short *
337vl_mpeg12_buffer_get_ycbcr_buffer(struct pipe_video_decode_buffer *buffer, int component)
338{
339   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
340
341   assert(buf);
342   assert(component < VL_MAX_PLANES);
343
344   return buf->texels[component];
345}
346
347static unsigned
348vl_mpeg12_buffer_get_mv_stream_stride(struct pipe_video_decode_buffer *buffer)
349{
350   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
351
352   assert(buf);
353
354   return vl_vb_get_mv_stream_stride(&buf->vertex_stream);
355}
356
357static struct pipe_motionvector *
358vl_mpeg12_buffer_get_mv_stream(struct pipe_video_decode_buffer *buffer, int ref_frame)
359{
360   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
361
362   assert(buf);
363
364   return vl_vb_get_mv_stream(&buf->vertex_stream, ref_frame);
365}
366
367static void
368vl_mpeg12_buffer_decode_bitstream(struct pipe_video_decode_buffer *buffer,
369                                  unsigned num_bytes, const void *data,
370                                  struct pipe_mpeg12_picture_desc *picture,
371                                  unsigned num_ycbcr_blocks[3])
372{
373   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
374   struct vl_mpeg12_decoder *dec;
375   unsigned i;
376
377   assert(buf);
378
379   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
380   assert(dec);
381
382   for (i = 0; i < VL_MAX_PLANES; ++i)
383      vl_zscan_set_layout(&buf->zscan[i], picture->alternate_scan ? dec->zscan_alternate : dec->zscan_normal);
384
385   vl_mpg12_bs_decode(&buf->bs, num_bytes, data, picture, num_ycbcr_blocks);
386}
387
388static void
389vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer)
390{
391   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
392   struct vl_mpeg12_decoder *dec;
393   unsigned i;
394
395   assert(buf);
396
397   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
398   assert(dec);
399
400   vl_vb_unmap(&buf->vertex_stream, dec->pipe);
401
402   for (i = 0; i < VL_MAX_PLANES; ++i) {
403      dec->pipe->transfer_unmap(dec->pipe, buf->tex_transfer[i]);
404      dec->pipe->transfer_destroy(dec->pipe, buf->tex_transfer[i]);
405   }
406}
407
408static void
409vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
410{
411   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
412
413   assert(decoder);
414
415   /* Asserted in softpipe_delete_fs_state() for some reason */
416   dec->pipe->bind_vs_state(dec->pipe, NULL);
417   dec->pipe->bind_fs_state(dec->pipe, NULL);
418
419   dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
420   dec->pipe->delete_sampler_state(dec->pipe, dec->sampler_ycbcr);
421
422   vl_mc_cleanup(&dec->mc_y);
423   vl_mc_cleanup(&dec->mc_c);
424   dec->mc_source->destroy(dec->mc_source);
425
426   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
427      vl_idct_cleanup(&dec->idct_y);
428      vl_idct_cleanup(&dec->idct_c);
429      dec->idct_source->destroy(dec->idct_source);
430   }
431
432   vl_zscan_cleanup(&dec->zscan_y);
433   vl_zscan_cleanup(&dec->zscan_c);
434
435   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
436   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_mv);
437
438   pipe_resource_reference(&dec->quads.buffer, NULL);
439   pipe_resource_reference(&dec->pos.buffer, NULL);
440
441   pipe_sampler_view_reference(&dec->zscan_linear, NULL);
442   pipe_sampler_view_reference(&dec->zscan_normal, NULL);
443   pipe_sampler_view_reference(&dec->zscan_alternate, NULL);
444
445   FREE(dec);
446}
447
448static struct pipe_video_decode_buffer *
449vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
450{
451   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
452   struct vl_mpeg12_buffer *buffer;
453
454   assert(dec);
455
456   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
457   if (buffer == NULL)
458      return NULL;
459
460   buffer->base.decoder = decoder;
461   buffer->base.destroy = vl_mpeg12_buffer_destroy;
462   buffer->base.map = vl_mpeg12_buffer_map;
463   buffer->base.get_ycbcr_stream = vl_mpeg12_buffer_get_ycbcr_stream;
464   buffer->base.get_ycbcr_buffer = vl_mpeg12_buffer_get_ycbcr_buffer;
465   buffer->base.get_mv_stream_stride = vl_mpeg12_buffer_get_mv_stream_stride;
466   buffer->base.get_mv_stream = vl_mpeg12_buffer_get_mv_stream;
467   buffer->base.decode_bitstream = vl_mpeg12_buffer_decode_bitstream;
468   buffer->base.unmap = vl_mpeg12_buffer_unmap;
469
470   if (!vl_vb_init(&buffer->vertex_stream, dec->pipe,
471                   dec->base.width / MACROBLOCK_WIDTH,
472                   dec->base.height / MACROBLOCK_HEIGHT))
473      goto error_vertex_buffer;
474
475   if (!init_mc_buffer(buffer))
476      goto error_mc;
477
478   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
479      if (!init_idct_buffer(buffer))
480         goto error_idct;
481
482   if (!init_zscan_buffer(buffer))
483      goto error_zscan;
484
485   if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
486      vl_mpg12_bs_init(&buffer->bs,
487                       dec->base.width / MACROBLOCK_WIDTH,
488                       dec->base.height / MACROBLOCK_HEIGHT);
489
490   return &buffer->base;
491
492error_zscan:
493   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
494      cleanup_idct_buffer(buffer);
495
496error_idct:
497   cleanup_mc_buffer(buffer);
498
499error_mc:
500   vl_vb_cleanup(&buffer->vertex_stream);
501
502error_vertex_buffer:
503   FREE(buffer);
504   return NULL;
505}
506
507static void
508vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
509                               unsigned num_ycbcr_blocks[3],
510                               struct pipe_video_buffer *refs[2],
511                               struct pipe_video_buffer *dst)
512{
513   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
514   struct vl_mpeg12_decoder *dec;
515
516   struct pipe_sampler_view **sv[VL_MAX_REF_FRAMES], **mc_source_sv;
517   struct pipe_surface **surfaces;
518
519   struct pipe_vertex_buffer vb[3];
520
521   unsigned i, j, component;
522   unsigned nr_components;
523
524   assert(buf);
525
526   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
527   assert(dec);
528
529   for (i = 0; i < 2; ++i)
530      sv[i] = refs[i] ? refs[i]->get_sampler_view_planes(refs[i]) : NULL;
531
532   vb[0] = dec->quads;
533   vb[1] = dec->pos;
534
535   surfaces = dst->get_surfaces(dst);
536
537   dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv);
538   for (i = 0; i < VL_MAX_PLANES; ++i) {
539      if (!surfaces[i]) continue;
540
541      vl_mc_set_surface(&buf->mc[i], surfaces[i]);
542
543      for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
544         if (!sv[j]) continue;
545
546         vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);;
547         dec->pipe->set_vertex_buffers(dec->pipe, 3, vb);
548
549         vl_mc_render_ref(&buf->mc[i], sv[j][i]);
550      }
551   }
552
553   dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
554   for (i = 0; i < VL_MAX_PLANES; ++i) {
555      if (!num_ycbcr_blocks[i]) continue;
556
557      vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
558      dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
559
560      vl_zscan_render(&buf->zscan[i] , num_ycbcr_blocks[i]);
561
562      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
563         vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], num_ycbcr_blocks[i]);
564   }
565
566   mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
567   for (i = 0, component = 0; i < VL_MAX_PLANES; ++i) {
568      if (!surfaces[i]) continue;
569
570      nr_components = util_format_get_nr_components(surfaces[i]->texture->format);
571      for (j = 0; j < nr_components; ++j, ++component) {
572         if (!num_ycbcr_blocks[i]) continue;
573
574         vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, component);
575         dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
576
577         if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
578            vl_idct_prepare_stage2(component == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[component]);
579         else {
580            dec->pipe->set_fragment_sampler_views(dec->pipe, 1, &mc_source_sv[component]);
581            dec->pipe->bind_fragment_sampler_states(dec->pipe, 1, &dec->sampler_ycbcr);
582         }
583         vl_mc_render_ycbcr(&buf->mc[i], j, num_ycbcr_blocks[component]);
584      }
585   }
586}
587
588static bool
589init_pipe_state(struct vl_mpeg12_decoder *dec)
590{
591   struct pipe_depth_stencil_alpha_state dsa;
592   struct pipe_sampler_state sampler;
593   unsigned i;
594
595   assert(dec);
596
597   memset(&dsa, 0, sizeof dsa);
598   dsa.depth.enabled = 0;
599   dsa.depth.writemask = 0;
600   dsa.depth.func = PIPE_FUNC_ALWAYS;
601   for (i = 0; i < 2; ++i) {
602      dsa.stencil[i].enabled = 0;
603      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
604      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
605      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
606      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
607      dsa.stencil[i].valuemask = 0;
608      dsa.stencil[i].writemask = 0;
609   }
610   dsa.alpha.enabled = 0;
611   dsa.alpha.func = PIPE_FUNC_ALWAYS;
612   dsa.alpha.ref_value = 0;
613   dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
614   dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
615
616   memset(&sampler, 0, sizeof(sampler));
617   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
618   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
619   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
620   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
621   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
622   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
623   sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
624   sampler.compare_func = PIPE_FUNC_ALWAYS;
625   sampler.normalized_coords = 1;
626   dec->sampler_ycbcr = dec->pipe->create_sampler_state(dec->pipe, &sampler);
627   if (!dec->sampler_ycbcr)
628      return false;
629
630   return true;
631}
632
633static const struct format_config*
634find_format_config(struct vl_mpeg12_decoder *dec, const struct format_config configs[], unsigned num_configs)
635{
636   struct pipe_screen *screen;
637   unsigned i;
638
639   assert(dec);
640
641   screen = dec->pipe->screen;
642
643   for (i = 0; i < num_configs; ++i) {
644      if (!screen->is_format_supported(screen, configs[i].zscan_source_format, PIPE_TEXTURE_2D,
645                                       1, PIPE_BIND_SAMPLER_VIEW))
646         continue;
647
648      if (configs[i].idct_source_format != PIPE_FORMAT_NONE) {
649         if (!screen->is_format_supported(screen, configs[i].idct_source_format, PIPE_TEXTURE_2D,
650                                          1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
651            continue;
652
653         if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_3D,
654                                          1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
655            continue;
656      } else {
657         if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_2D,
658                                          1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
659            continue;
660      }
661      return &configs[i];
662   }
663
664   return NULL;
665}
666
667static bool
668init_zscan(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
669{
670   unsigned num_channels;
671
672   assert(dec);
673
674   dec->blocks_per_line = 4;
675   dec->max_blocks =
676      (dec->base.width * dec->base.height) /
677      (BLOCK_WIDTH * BLOCK_HEIGHT);
678
679   dec->zscan_source_format = format_config->zscan_source_format;
680   dec->zscan_linear = vl_zscan_layout(dec->pipe, vl_zscan_linear, dec->blocks_per_line);
681   dec->zscan_normal = vl_zscan_layout(dec->pipe, vl_zscan_normal, dec->blocks_per_line);
682   dec->zscan_alternate = vl_zscan_layout(dec->pipe, vl_zscan_alternate, dec->blocks_per_line);
683
684   num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
685
686   if (!vl_zscan_init(&dec->zscan_y, dec->pipe, dec->base.width, dec->base.height,
687                      dec->blocks_per_line, dec->max_blocks, num_channels))
688      return false;
689
690   if (!vl_zscan_init(&dec->zscan_c, dec->pipe, dec->chroma_width, dec->chroma_height,
691                      dec->blocks_per_line, dec->max_blocks, num_channels))
692      return false;
693
694   return true;
695}
696
697static bool
698init_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
699{
700   unsigned nr_of_idct_render_targets;
701   enum pipe_format formats[3];
702
703   struct pipe_sampler_view *matrix = NULL;
704
705   nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS);
706
707   // more than 4 render targets usually doesn't makes any seens
708   nr_of_idct_render_targets = MIN2(nr_of_idct_render_targets, 4);
709
710   formats[0] = formats[1] = formats[2] = format_config->idct_source_format;
711   dec->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe,
712                                           dec->base.width / 4, dec->base.height, 1,
713                                           dec->base.chroma_format,
714                                           formats, PIPE_USAGE_STATIC);
715   if (!dec->idct_source)
716      goto error_idct_source;
717
718   formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
719   dec->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe,
720                                         dec->base.width / nr_of_idct_render_targets,
721                                         dec->base.height / 4, nr_of_idct_render_targets,
722                                         dec->base.chroma_format,
723                                         formats, PIPE_USAGE_STATIC);
724
725   if (!dec->mc_source)
726      goto error_mc_source;
727
728   if (!(matrix = vl_idct_upload_matrix(dec->pipe, format_config->idct_scale)))
729      goto error_matrix;
730
731   if (!vl_idct_init(&dec->idct_y, dec->pipe, dec->base.width, dec->base.height,
732                     nr_of_idct_render_targets, matrix, matrix))
733      goto error_y;
734
735   if(!vl_idct_init(&dec->idct_c, dec->pipe, dec->chroma_width, dec->chroma_height,
736                    nr_of_idct_render_targets, matrix, matrix))
737      goto error_c;
738
739   pipe_sampler_view_reference(&matrix, NULL);
740
741   return true;
742
743error_c:
744   vl_idct_cleanup(&dec->idct_y);
745
746error_y:
747   pipe_sampler_view_reference(&matrix, NULL);
748
749error_matrix:
750   dec->mc_source->destroy(dec->mc_source);
751
752error_mc_source:
753   dec->idct_source->destroy(dec->idct_source);
754
755error_idct_source:
756   return false;
757}
758
759static bool
760init_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
761{
762   enum pipe_format formats[3];
763
764   formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
765   dec->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe,
766                                         dec->base.width, dec->base.height, 1,
767                                         dec->base.chroma_format,
768                                         formats, PIPE_USAGE_STATIC);
769
770   return dec->mc_source != NULL;
771}
772
773static void
774mc_vert_shader_callback(void *priv, struct vl_mc *mc,
775                        struct ureg_program *shader,
776                        unsigned first_output,
777                        struct ureg_dst tex)
778{
779   struct vl_mpeg12_decoder *dec = priv;
780   struct ureg_dst o_vtex;
781
782   assert(priv && mc);
783   assert(shader);
784
785   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
786      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
787      vl_idct_stage2_vert_shader(idct, shader, first_output, tex);
788   } else {
789      o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);
790      ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));
791   }
792}
793
794static void
795mc_frag_shader_callback(void *priv, struct vl_mc *mc,
796                        struct ureg_program *shader,
797                        unsigned first_input,
798                        struct ureg_dst dst)
799{
800   struct vl_mpeg12_decoder *dec = priv;
801   struct ureg_src src, sampler;
802
803   assert(priv && mc);
804   assert(shader);
805
806   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
807      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
808      vl_idct_stage2_frag_shader(idct, shader, first_input, dst);
809   } else {
810      src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);
811      sampler = ureg_DECL_sampler(shader, 0);
812      ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);
813   }
814}
815
816struct pipe_video_decoder *
817vl_create_mpeg12_decoder(struct pipe_video_context *context,
818                         struct pipe_context *pipe,
819                         enum pipe_video_profile profile,
820                         enum pipe_video_entrypoint entrypoint,
821                         enum pipe_video_chroma_format chroma_format,
822                         unsigned width, unsigned height)
823{
824   const struct format_config *format_config;
825   struct vl_mpeg12_decoder *dec;
826
827   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
828
829   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
830
831   if (!dec)
832      return NULL;
833
834   dec->base.context = context;
835   dec->base.profile = profile;
836   dec->base.entrypoint = entrypoint;
837   dec->base.chroma_format = chroma_format;
838   dec->base.width = width;
839   dec->base.height = height;
840
841   dec->base.destroy = vl_mpeg12_destroy;
842   dec->base.create_buffer = vl_mpeg12_create_buffer;
843   dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
844
845   dec->pipe = pipe;
846
847   dec->quads = vl_vb_upload_quads(dec->pipe);
848   dec->pos = vl_vb_upload_pos(
849      dec->pipe,
850      dec->base.width / MACROBLOCK_WIDTH,
851      dec->base.height / MACROBLOCK_HEIGHT
852   );
853
854   dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->pipe);
855   dec->ves_mv = vl_vb_get_ves_mv(dec->pipe);
856
857   /* TODO: Implement 422, 444 */
858   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
859
860   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
861      dec->chroma_width = dec->base.width / 2;
862      dec->chroma_height = dec->base.height / 2;
863   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
864      dec->chroma_width = dec->base.width;
865      dec->chroma_height = dec->base.height / 2;
866   } else {
867      dec->chroma_width = dec->base.width;
868      dec->chroma_height = dec->base.height;
869   }
870
871   switch (entrypoint) {
872   case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
873      format_config = find_format_config(dec, bitstream_format_config, num_bitstream_format_configs);
874      break;
875
876   case PIPE_VIDEO_ENTRYPOINT_IDCT:
877      format_config = find_format_config(dec, idct_format_config, num_idct_format_configs);
878      break;
879
880   case PIPE_VIDEO_ENTRYPOINT_MC:
881      format_config = find_format_config(dec, mc_format_config, num_mc_format_configs);
882      break;
883
884   default:
885      assert(0);
886      return NULL;
887   }
888
889   if (!format_config)
890      return NULL;
891
892   if (!init_zscan(dec, format_config))
893      goto error_zscan;
894
895   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
896      if (!init_idct(dec, format_config))
897         goto error_sources;
898   } else {
899      if (!init_mc_source_widthout_idct(dec, format_config))
900         goto error_sources;
901   }
902
903   if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, MACROBLOCK_HEIGHT, format_config->mc_scale,
904                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
905      goto error_mc_y;
906
907   // TODO
908   if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, BLOCK_HEIGHT, format_config->mc_scale,
909                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
910      goto error_mc_c;
911
912   if (!init_pipe_state(dec))
913      goto error_pipe_state;
914
915   return &dec->base;
916
917error_pipe_state:
918   vl_mc_cleanup(&dec->mc_c);
919
920error_mc_c:
921   vl_mc_cleanup(&dec->mc_y);
922
923error_mc_y:
924   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
925      vl_idct_cleanup(&dec->idct_y);
926      vl_idct_cleanup(&dec->idct_c);
927      dec->idct_source->destroy(dec->idct_source);
928   }
929   dec->mc_source->destroy(dec->mc_source);
930
931error_sources:
932   vl_zscan_cleanup(&dec->zscan_y);
933   vl_zscan_cleanup(&dec->zscan_c);
934
935error_zscan:
936   FREE(dec);
937   return NULL;
938}
939