vl_mpeg12_decoder.c revision 7f04fe5338d0846ec9a6003033da5357d2785c8b
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
351static struct pipe_ycbcr_block *
352vl_mpeg12_buffer_get_ycbcr_stream(struct pipe_video_decode_buffer *buffer, int component)
353{
354   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
355
356   assert(buf);
357
358   return vl_vb_get_ycbcr_stream(&buf->vertex_stream, component);
359}
360
361static short *
362vl_mpeg12_buffer_get_ycbcr_buffer(struct pipe_video_decode_buffer *buffer, int component)
363{
364   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
365
366   assert(buf);
367   assert(component < VL_MAX_PLANES);
368
369   return buf->texels[component];
370}
371
372static unsigned
373vl_mpeg12_buffer_get_mv_stream_stride(struct pipe_video_decode_buffer *buffer)
374{
375   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
376
377   assert(buf);
378
379   return vl_vb_get_mv_stream_stride(&buf->vertex_stream);
380}
381
382static struct pipe_motionvector *
383vl_mpeg12_buffer_get_mv_stream(struct pipe_video_decode_buffer *buffer, int ref_frame)
384{
385   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
386
387   assert(buf);
388
389   return vl_vb_get_mv_stream(&buf->vertex_stream, ref_frame);
390}
391
392static void
393vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer)
394{
395   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
396   struct vl_mpeg12_decoder *dec;
397   unsigned i;
398
399   assert(buf);
400
401   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
402   assert(dec);
403
404   vl_vb_unmap(&buf->vertex_stream, dec->pipe);
405
406   for (i = 0; i < VL_MAX_PLANES; ++i) {
407      dec->pipe->transfer_unmap(dec->pipe, buf->tex_transfer[i]);
408      dec->pipe->transfer_destroy(dec->pipe, buf->tex_transfer[i]);
409   }
410}
411
412static void
413vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
414{
415   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
416
417   assert(decoder);
418
419   /* Asserted in softpipe_delete_fs_state() for some reason */
420   dec->pipe->bind_vs_state(dec->pipe, NULL);
421   dec->pipe->bind_fs_state(dec->pipe, NULL);
422
423   dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
424   dec->pipe->delete_sampler_state(dec->pipe, dec->sampler_ycbcr);
425
426   vl_mc_cleanup(&dec->mc_y);
427   vl_mc_cleanup(&dec->mc_c);
428
429   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
430      vl_idct_cleanup(&dec->idct_y);
431      vl_idct_cleanup(&dec->idct_c);
432   }
433
434   vl_zscan_cleanup(&dec->zscan_y);
435   vl_zscan_cleanup(&dec->zscan_c);
436
437   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
438   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_mv);
439
440   pipe_resource_reference(&dec->quads.buffer, NULL);
441   pipe_resource_reference(&dec->pos.buffer, NULL);
442
443   FREE(dec);
444}
445
446static struct pipe_video_decode_buffer *
447vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
448{
449   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
450   struct vl_mpeg12_buffer *buffer;
451
452   assert(dec);
453
454   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
455   if (buffer == NULL)
456      return NULL;
457
458   buffer->base.decoder = decoder;
459   buffer->base.destroy = vl_mpeg12_buffer_destroy;
460   buffer->base.map = vl_mpeg12_buffer_map;
461   buffer->base.get_ycbcr_stream = vl_mpeg12_buffer_get_ycbcr_stream;
462   buffer->base.get_ycbcr_buffer = vl_mpeg12_buffer_get_ycbcr_buffer;
463   buffer->base.get_mv_stream_stride = vl_mpeg12_buffer_get_mv_stream_stride;
464   buffer->base.get_mv_stream = vl_mpeg12_buffer_get_mv_stream;
465   buffer->base.unmap = vl_mpeg12_buffer_unmap;
466
467   if (!vl_vb_init(&buffer->vertex_stream, dec->pipe,
468                   dec->base.width / MACROBLOCK_WIDTH,
469                   dec->base.height / MACROBLOCK_HEIGHT))
470      goto error_vertex_buffer;
471
472   if (!init_mc_buffer(buffer))
473      goto error_mc;
474
475   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
476      if (!init_idct_buffer(buffer))
477         goto error_idct;
478
479   if (!init_zscan_buffer(buffer))
480      goto error_zscan;
481
482   return &buffer->base;
483
484error_zscan:
485   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
486      cleanup_idct_buffer(buffer);
487
488error_idct:
489   cleanup_mc_buffer(buffer);
490
491error_mc:
492   vl_vb_cleanup(&buffer->vertex_stream);
493
494error_vertex_buffer:
495   FREE(buffer);
496   return NULL;
497}
498
499static void
500vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
501                               unsigned num_ycbcr_blocks[3],
502                               struct pipe_video_buffer *refs[2],
503                               struct pipe_video_buffer *dst)
504{
505   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
506   struct vl_mpeg12_decoder *dec;
507
508   struct pipe_sampler_view **sv[VL_MAX_REF_FRAMES], **mc_source_sv;
509   struct pipe_surface **surfaces;
510
511   struct pipe_vertex_buffer vb[3];
512
513   unsigned i, j, component;
514   unsigned nr_components;
515
516   assert(buf);
517
518   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
519   assert(dec);
520
521   for (i = 0; i < 2; ++i)
522      sv[i] = refs[i] ? refs[i]->get_sampler_view_planes(refs[i]) : NULL;
523
524   vb[0] = dec->quads;
525   vb[1] = dec->pos;
526
527   surfaces = dst->get_surfaces(dst);
528
529   dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv);
530   for (i = 0; i < VL_MAX_PLANES; ++i) {
531      if (!surfaces[i]) continue;
532
533      vl_mc_set_surface(&buf->mc[i], surfaces[i]);
534
535      for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
536         if (!sv[j]) continue;
537
538         vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);;
539         dec->pipe->set_vertex_buffers(dec->pipe, 3, vb);
540
541         vl_mc_render_ref(&buf->mc[i], sv[j][i]);
542      }
543   }
544
545   dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
546   for (i = 0; i < VL_MAX_PLANES; ++i) {
547      if (!num_ycbcr_blocks[i]) continue;
548
549      vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
550      dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
551
552      vl_zscan_render(&buf->zscan[i] , num_ycbcr_blocks[i]);
553
554      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
555         vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], num_ycbcr_blocks[i]);
556   }
557
558   mc_source_sv = buf->mc_source->get_sampler_view_planes(buf->mc_source);
559   for (i = 0, component = 0; i < VL_MAX_PLANES; ++i) {
560      if (!surfaces[i]) continue;
561
562      nr_components = util_format_get_nr_components(surfaces[i]->texture->format);
563      for (j = 0; j < nr_components; ++j, ++component) {
564         if (!num_ycbcr_blocks[i]) continue;
565
566         vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, component);
567         dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
568
569         if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
570            vl_idct_prepare_stage2(component == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[component]);
571         else {
572            dec->pipe->set_fragment_sampler_views(dec->pipe, 1, &mc_source_sv[component]);
573            dec->pipe->bind_fragment_sampler_states(dec->pipe, 1, &dec->sampler_ycbcr);
574         }
575         vl_mc_render_ycbcr(&buf->mc[i], j, num_ycbcr_blocks[component]);
576      }
577   }
578}
579
580static bool
581init_pipe_state(struct vl_mpeg12_decoder *dec)
582{
583   struct pipe_depth_stencil_alpha_state dsa;
584   struct pipe_sampler_state sampler;
585   unsigned i;
586
587   assert(dec);
588
589   memset(&dsa, 0, sizeof dsa);
590   dsa.depth.enabled = 0;
591   dsa.depth.writemask = 0;
592   dsa.depth.func = PIPE_FUNC_ALWAYS;
593   for (i = 0; i < 2; ++i) {
594      dsa.stencil[i].enabled = 0;
595      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
596      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
597      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
598      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
599      dsa.stencil[i].valuemask = 0;
600      dsa.stencil[i].writemask = 0;
601   }
602   dsa.alpha.enabled = 0;
603   dsa.alpha.func = PIPE_FUNC_ALWAYS;
604   dsa.alpha.ref_value = 0;
605   dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
606   dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
607
608   memset(&sampler, 0, sizeof(sampler));
609   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
610   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
611   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
612   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
613   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
614   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
615   sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
616   sampler.compare_func = PIPE_FUNC_ALWAYS;
617   sampler.normalized_coords = 1;
618   dec->sampler_ycbcr = dec->pipe->create_sampler_state(dec->pipe, &sampler);
619   if (!dec->sampler_ycbcr)
620      return false;
621
622   return true;
623}
624
625static enum pipe_format
626find_first_supported_format(struct vl_mpeg12_decoder *dec,
627                            const enum pipe_format formats[],
628                            unsigned num_formats,
629                            enum pipe_texture_target target)
630{
631   struct pipe_screen *screen;
632   unsigned i;
633
634   assert(dec);
635
636   screen = dec->pipe->screen;
637
638   for (i = 0; i < num_formats; ++i)
639      if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1,
640                                      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
641         return formats[i];
642
643   return PIPE_FORMAT_NONE;
644}
645
646static bool
647init_zscan(struct vl_mpeg12_decoder *dec)
648{
649   struct pipe_sampler_view *layout;
650
651   unsigned num_channels;
652
653   assert(dec);
654
655   dec->blocks_per_line = 4;
656   dec->max_blocks =
657      (dec->base.width * dec->base.height) /
658      (BLOCK_WIDTH * BLOCK_HEIGHT);
659
660   dec->zscan_source_format = find_first_supported_format(dec, const_zscan_source_formats,
661                                                          num_zscan_source_formats, PIPE_TEXTURE_2D);
662
663   if (dec->zscan_source_format == PIPE_FORMAT_NONE)
664      return false;
665
666   layout = vl_zscan_linear(dec->pipe, dec->blocks_per_line);
667
668   num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
669
670   if (!vl_zscan_init(&dec->zscan_y, dec->pipe, dec->base.width, dec->base.height,
671                      dec->blocks_per_line, dec->max_blocks, num_channels))
672      return false;
673
674   vl_zscan_set_layout(&dec->zscan_y, layout);
675
676   if (!vl_zscan_init(&dec->zscan_c, dec->pipe, dec->chroma_width, dec->chroma_height,
677                      dec->blocks_per_line, dec->max_blocks, num_channels))
678      return false;
679
680   vl_zscan_set_layout(&dec->zscan_c, layout);
681
682   return true;
683}
684
685static bool
686init_idct(struct vl_mpeg12_decoder *dec)
687{
688   struct pipe_sampler_view *matrix, *transpose = NULL;
689   float matrix_scale, transpose_scale;
690
691   dec->nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS);
692
693   // more than 4 render targets usually doesn't makes any seens
694   dec->nr_of_idct_render_targets = MIN2(dec->nr_of_idct_render_targets, 4);
695
696   dec->idct_source_format = find_first_supported_format(dec, const_idct_source_formats,
697                                                         num_idct_source_formats, PIPE_TEXTURE_2D);
698
699   if (dec->idct_source_format == PIPE_FORMAT_NONE)
700      return false;
701
702   dec->mc_source_format = find_first_supported_format(dec, const_idct_intermediate_formats,
703                                                       num_idct_intermediate_formats, PIPE_TEXTURE_3D);
704
705   if (dec->mc_source_format == PIPE_FORMAT_NONE)
706      return false;
707
708   switch (dec->idct_source_format) {
709   case PIPE_FORMAT_R16G16B16A16_SSCALED:
710      matrix_scale = SCALE_FACTOR_SSCALED;
711      break;
712
713   case PIPE_FORMAT_R16G16B16A16_SNORM:
714      matrix_scale = SCALE_FACTOR_SNORM;
715      break;
716
717   default:
718      assert(0);
719      return false;
720   }
721
722   if (dec->mc_source_format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
723       dec->mc_source_format == PIPE_FORMAT_R32G32B32A32_FLOAT)
724      transpose_scale = 1.0f;
725   else
726      transpose_scale = matrix_scale = sqrt(matrix_scale);
727
728   if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
729      transpose_scale /= SCALE_FACTOR_SSCALED;
730
731   if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale)))
732      goto error_matrix;
733
734   if (matrix_scale != transpose_scale) {
735      if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale)))
736         goto error_transpose;
737   } else
738      pipe_sampler_view_reference(&transpose, matrix);
739
740   if (!vl_idct_init(&dec->idct_y, dec->pipe, dec->base.width, dec->base.height,
741                     dec->nr_of_idct_render_targets, matrix, transpose))
742      goto error_y;
743
744   if(!vl_idct_init(&dec->idct_c, dec->pipe, dec->chroma_width, dec->chroma_height,
745                    dec->nr_of_idct_render_targets, matrix, transpose))
746      goto error_c;
747
748   pipe_sampler_view_reference(&matrix, NULL);
749   pipe_sampler_view_reference(&transpose, NULL);
750   return true;
751
752error_c:
753   vl_idct_cleanup(&dec->idct_y);
754
755error_y:
756   pipe_sampler_view_reference(&transpose, NULL);
757
758error_transpose:
759   pipe_sampler_view_reference(&matrix, NULL);
760
761error_matrix:
762   return false;
763}
764
765static void
766mc_vert_shader_callback(void *priv, struct vl_mc *mc,
767                        struct ureg_program *shader,
768                        unsigned first_output,
769                        struct ureg_dst tex)
770{
771   struct vl_mpeg12_decoder *dec = priv;
772   struct ureg_dst o_vtex;
773
774   assert(priv && mc);
775   assert(shader);
776
777   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
778      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
779      vl_idct_stage2_vert_shader(idct, shader, first_output, tex);
780   } else {
781      o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);
782      ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));
783   }
784}
785
786static void
787mc_frag_shader_callback(void *priv, struct vl_mc *mc,
788                        struct ureg_program *shader,
789                        unsigned first_input,
790                        struct ureg_dst dst)
791{
792   struct vl_mpeg12_decoder *dec = priv;
793   struct ureg_src src, sampler;
794
795   assert(priv && mc);
796   assert(shader);
797
798   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
799      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
800      vl_idct_stage2_frag_shader(idct, shader, first_input, dst);
801   } else {
802      src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);
803      sampler = ureg_DECL_sampler(shader, 0);
804      ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);
805   }
806}
807
808struct pipe_video_decoder *
809vl_create_mpeg12_decoder(struct pipe_video_context *context,
810                         struct pipe_context *pipe,
811                         enum pipe_video_profile profile,
812                         enum pipe_video_entrypoint entrypoint,
813                         enum pipe_video_chroma_format chroma_format,
814                         unsigned width, unsigned height)
815{
816   struct vl_mpeg12_decoder *dec;
817   float mc_scale;
818
819   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
820
821   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
822
823   if (!dec)
824      return NULL;
825
826   dec->base.context = context;
827   dec->base.profile = profile;
828   dec->base.entrypoint = entrypoint;
829   dec->base.chroma_format = chroma_format;
830   dec->base.width = width;
831   dec->base.height = height;
832
833   dec->base.destroy = vl_mpeg12_destroy;
834   dec->base.create_buffer = vl_mpeg12_create_buffer;
835   dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
836
837   dec->base.width = align(width, MACROBLOCK_WIDTH);
838   dec->base.height = align(height, MACROBLOCK_HEIGHT);
839
840   dec->pipe = pipe;
841
842   dec->quads = vl_vb_upload_quads(dec->pipe);
843   dec->pos = vl_vb_upload_pos(
844      dec->pipe,
845      dec->base.width / MACROBLOCK_WIDTH,
846      dec->base.height / MACROBLOCK_HEIGHT
847   );
848
849   dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->pipe);
850   dec->ves_mv = vl_vb_get_ves_mv(dec->pipe);
851
852   /* TODO: Implement 422, 444 */
853   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
854
855   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
856      dec->chroma_width = dec->base.width / 2;
857      dec->chroma_height = dec->base.height / 2;
858   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
859      dec->chroma_width = dec->base.width;
860      dec->chroma_height = dec->base.height / 2;
861   } else {
862      dec->chroma_width = dec->base.width;
863      dec->chroma_height = dec->base.height;
864   }
865
866   if (!init_zscan(dec))
867      goto error_zscan;
868
869   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
870      if (!init_idct(dec))
871         goto error_idct;
872      if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
873         mc_scale = SCALE_FACTOR_SSCALED;
874      else
875         mc_scale = 1.0f;
876   } else {
877      dec->mc_source_format = find_first_supported_format(dec, const_mc_source_formats,
878                                                          num_mc_source_formats, PIPE_TEXTURE_3D);
879
880      if (dec->mc_source_format == PIPE_FORMAT_NONE)
881         return NULL;
882
883      switch (dec->mc_source_format) {
884      case PIPE_FORMAT_R16_SNORM:
885         mc_scale = SCALE_FACTOR_SNORM;
886         break;
887
888      case PIPE_FORMAT_R16_SSCALED:
889         mc_scale = SCALE_FACTOR_SSCALED;
890         break;
891
892      default:
893         assert(0);
894         return NULL;
895      }
896   }
897
898   if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, MACROBLOCK_HEIGHT, mc_scale,
899                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
900      goto error_mc_y;
901
902   // TODO
903   if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, BLOCK_HEIGHT, mc_scale,
904                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
905      goto error_mc_c;
906
907   if (!init_pipe_state(dec))
908      goto error_pipe_state;
909
910   return &dec->base;
911
912error_pipe_state:
913   vl_mc_cleanup(&dec->mc_c);
914
915error_mc_c:
916   vl_mc_cleanup(&dec->mc_y);
917
918error_mc_y:
919   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
920      vl_idct_cleanup(&dec->idct_y);
921      vl_idct_cleanup(&dec->idct_c);
922   }
923
924error_idct:
925   vl_zscan_cleanup(&dec->zscan_y);
926   vl_zscan_cleanup(&dec->zscan_c);
927
928error_zscan:
929   FREE(dec);
930   return NULL;
931}
932