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