vl_mpeg12_decoder.c revision 104ac0066394f8246d18c833bca4bcce271b5eef
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, **idct_intermediate_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->idct_intermediate_format;
168   buffer->idct_intermediate = 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->idct_intermediate)
175      goto error_intermediate;
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   idct_intermediate_sv = buffer->idct_intermediate->get_sampler_view_planes(buffer->idct_intermediate);
182   if (!idct_intermediate_sv)
183      goto error_intermediate_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                               idct_intermediate_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_intermediate_sv:
203error_source_sv:
204   buffer->idct_intermediate->destroy(buffer->idct_intermediate);
205
206error_intermediate:
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   buf->idct_intermediate->destroy(buf->idct_intermediate);
227}
228
229static bool
230init_mc_buffer(struct vl_mpeg12_buffer *buf)
231{
232   struct vl_mpeg12_decoder *dec;
233   enum pipe_format formats[3];
234
235   assert(buf);
236
237   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
238   assert(dec);
239
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   if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))
250      goto error_mc_y;
251
252   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))
253      goto error_mc_cb;
254
255   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))
256      goto error_mc_cr;
257
258   return true;
259
260error_mc_cr:
261   vl_mc_cleanup_buffer(&buf->mc[1]);
262
263error_mc_cb:
264   vl_mc_cleanup_buffer(&buf->mc[0]);
265
266error_mc_y:
267   buf->mc_source->destroy(buf->mc_source);
268
269error_mc_source:
270   return false;
271}
272
273static void
274cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
275{
276   unsigned i;
277
278   assert(buf);
279
280   for (i = 0; i < VL_MAX_PLANES; ++i)
281      vl_mc_cleanup_buffer(&buf->mc[i]);
282
283   buf->mc_source->destroy(buf->mc_source);
284}
285
286static void
287vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer)
288{
289   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
290   struct vl_mpeg12_decoder *dec;
291
292   assert(buf);
293
294   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
295   assert(dec);
296
297   cleanup_zscan_buffer(buf);
298
299   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
300      cleanup_idct_buffer(buf);
301
302   cleanup_mc_buffer(buf);
303
304   vl_vb_cleanup(&buf->vertex_stream);
305
306   FREE(buf);
307}
308
309static void
310vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer)
311{
312   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
313   struct vl_mpeg12_decoder *dec;
314
315   struct pipe_sampler_view **sampler_views;
316   unsigned i;
317
318   assert(buf);
319
320   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
321   assert(dec);
322
323   vl_vb_map(&buf->vertex_stream, dec->pipe);
324
325   sampler_views = buf->zscan_source->get_sampler_view_planes(buf->zscan_source);
326
327   assert(sampler_views);
328
329   for (i = 0; i < VL_MAX_PLANES; ++i) {
330      struct pipe_resource *tex = sampler_views[i]->texture;
331      struct pipe_box rect =
332      {
333         0, 0, 0,
334         tex->width0,
335         tex->height0,
336         1
337      };
338
339      buf->tex_transfer[i] = dec->pipe->get_transfer
340      (
341         dec->pipe, tex,
342         0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
343         &rect
344      );
345
346      buf->texels[i] = dec->pipe->transfer_map(dec->pipe, buf->tex_transfer[i]);
347   }
348}
349
350static struct pipe_ycbcr_block *
351vl_mpeg12_buffer_get_ycbcr_stream(struct pipe_video_decode_buffer *buffer, int component)
352{
353   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
354
355   assert(buf);
356
357   return vl_vb_get_ycbcr_stream(&buf->vertex_stream, component);
358}
359
360static short *
361vl_mpeg12_buffer_get_ycbcr_buffer(struct pipe_video_decode_buffer *buffer, int component)
362{
363   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
364
365   assert(buf);
366   assert(component < VL_MAX_PLANES);
367
368   return buf->texels[component];
369}
370
371static unsigned
372vl_mpeg12_buffer_get_mv_stream_stride(struct pipe_video_decode_buffer *buffer)
373{
374   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
375
376   assert(buf);
377
378   return vl_vb_get_mv_stream_stride(&buf->vertex_stream);
379}
380
381static struct pipe_motionvector *
382vl_mpeg12_buffer_get_mv_stream(struct pipe_video_decode_buffer *buffer, int ref_frame)
383{
384   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
385
386   assert(buf);
387
388   return vl_vb_get_mv_stream(&buf->vertex_stream, ref_frame);
389}
390
391static void
392vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer)
393{
394   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
395   struct vl_mpeg12_decoder *dec;
396   unsigned i;
397
398   assert(buf);
399
400   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
401   assert(dec);
402
403   vl_vb_unmap(&buf->vertex_stream, dec->pipe);
404
405   for (i = 0; i < VL_MAX_PLANES; ++i) {
406      dec->pipe->transfer_unmap(dec->pipe, buf->tex_transfer[i]);
407      dec->pipe->transfer_destroy(dec->pipe, buf->tex_transfer[i]);
408   }
409}
410
411static void
412vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
413{
414   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
415
416   assert(decoder);
417
418   /* Asserted in softpipe_delete_fs_state() for some reason */
419   dec->pipe->bind_vs_state(dec->pipe, NULL);
420   dec->pipe->bind_fs_state(dec->pipe, NULL);
421
422   dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
423
424   vl_mc_cleanup(&dec->mc_y);
425   vl_mc_cleanup(&dec->mc_c);
426
427   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
428      vl_idct_cleanup(&dec->idct_y);
429      vl_idct_cleanup(&dec->idct_c);
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   FREE(dec);
442}
443
444static struct pipe_video_decode_buffer *
445vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
446{
447   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
448   struct vl_mpeg12_buffer *buffer;
449
450   assert(dec);
451
452   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
453   if (buffer == NULL)
454      return NULL;
455
456   buffer->base.decoder = decoder;
457   buffer->base.destroy = vl_mpeg12_buffer_destroy;
458   buffer->base.map = vl_mpeg12_buffer_map;
459   buffer->base.get_ycbcr_stream = vl_mpeg12_buffer_get_ycbcr_stream;
460   buffer->base.get_ycbcr_buffer = vl_mpeg12_buffer_get_ycbcr_buffer;
461   buffer->base.get_mv_stream_stride = vl_mpeg12_buffer_get_mv_stream_stride;
462   buffer->base.get_mv_stream = vl_mpeg12_buffer_get_mv_stream;
463   buffer->base.unmap = vl_mpeg12_buffer_unmap;
464
465   if (!vl_vb_init(&buffer->vertex_stream, dec->pipe,
466                   dec->base.width / MACROBLOCK_WIDTH,
467                   dec->base.height / MACROBLOCK_HEIGHT))
468      goto error_vertex_buffer;
469
470   if (!init_mc_buffer(buffer))
471      goto error_mc;
472
473   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
474      if (!init_idct_buffer(buffer))
475         goto error_idct;
476
477   if (!init_zscan_buffer(buffer))
478      goto error_zscan;
479
480   return &buffer->base;
481
482error_zscan:
483   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
484      cleanup_idct_buffer(buffer);
485
486error_idct:
487   cleanup_mc_buffer(buffer);
488
489error_mc:
490   vl_vb_cleanup(&buffer->vertex_stream);
491
492error_vertex_buffer:
493   FREE(buffer);
494   return NULL;
495}
496
497static void
498vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
499                               unsigned num_ycbcr_blocks[3],
500                               struct pipe_video_buffer *refs[2],
501                               struct pipe_video_buffer *dst)
502{
503   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
504   struct vl_mpeg12_decoder *dec;
505
506   struct pipe_sampler_view **sv[VL_MAX_REF_FRAMES], **mc_source_sv;
507   struct pipe_surface **surfaces;
508
509   struct pipe_vertex_buffer vb[3];
510
511   unsigned i, j, component;
512   unsigned nr_components;
513
514   assert(buf);
515
516   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
517   assert(dec);
518
519   for (i = 0; i < 2; ++i)
520      sv[i] = refs[i] ? refs[i]->get_sampler_view_planes(refs[i]) : NULL;
521
522   vb[0] = dec->quads;
523   vb[1] = dec->pos;
524
525   surfaces = dst->get_surfaces(dst);
526
527   dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv);
528   for (i = 0; i < VL_MAX_PLANES; ++i) {
529      if (!surfaces[i]) continue;
530
531      vl_mc_set_surface(&buf->mc[i], surfaces[i]);
532
533      for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
534         if (!sv[j]) continue;
535
536         vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);;
537         dec->pipe->set_vertex_buffers(dec->pipe, 3, vb);
538
539         vl_mc_render_ref(&buf->mc[i], sv[j][i]);
540      }
541   }
542
543   dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_ycbcr);
544   for (i = 0; i < VL_MAX_PLANES; ++i) {
545      if (!num_ycbcr_blocks[i]) continue;
546
547      vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
548      dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
549
550      vl_zscan_render(&buf->zscan[i] , num_ycbcr_blocks[i]);
551
552      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
553         vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], num_ycbcr_blocks[i]);
554   }
555
556   mc_source_sv = buf->mc_source->get_sampler_view_planes(buf->mc_source);
557   for (i = 0, component = 0; i < VL_MAX_PLANES; ++i) {
558      if (!surfaces[i]) continue;
559
560      nr_components = util_format_get_nr_components(surfaces[i]->texture->format);
561      for (j = 0; j < nr_components; ++j, ++component) {
562         if (!num_ycbcr_blocks[i]) continue;
563
564         vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, component);
565         dec->pipe->set_vertex_buffers(dec->pipe, 2, vb);
566         vl_mc_render_ycbcr(&buf->mc[i], mc_source_sv[component], j, num_ycbcr_blocks[component]);
567      }
568   }
569}
570
571static bool
572init_pipe_state(struct vl_mpeg12_decoder *dec)
573{
574   struct pipe_depth_stencil_alpha_state dsa;
575   unsigned i;
576
577   assert(dec);
578
579   memset(&dsa, 0, sizeof dsa);
580   dsa.depth.enabled = 0;
581   dsa.depth.writemask = 0;
582   dsa.depth.func = PIPE_FUNC_ALWAYS;
583   for (i = 0; i < 2; ++i) {
584      dsa.stencil[i].enabled = 0;
585      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
586      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
587      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
588      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
589      dsa.stencil[i].valuemask = 0;
590      dsa.stencil[i].writemask = 0;
591   }
592   dsa.alpha.enabled = 0;
593   dsa.alpha.func = PIPE_FUNC_ALWAYS;
594   dsa.alpha.ref_value = 0;
595   dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
596   dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
597
598   return true;
599}
600
601static enum pipe_format
602find_first_supported_format(struct vl_mpeg12_decoder *dec,
603                            const enum pipe_format formats[],
604                            unsigned num_formats,
605                            enum pipe_texture_target target)
606{
607   struct pipe_screen *screen;
608   unsigned i;
609
610   assert(dec);
611
612   screen = dec->pipe->screen;
613
614   for (i = 0; i < num_formats; ++i)
615      if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1,
616                                      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
617         return formats[i];
618
619   return PIPE_FORMAT_NONE;
620}
621
622static bool
623init_zscan(struct vl_mpeg12_decoder *dec)
624{
625   struct pipe_sampler_view *layout;
626
627   unsigned num_channels;
628
629   assert(dec);
630
631   dec->blocks_per_line = 4;
632   dec->max_blocks =
633      (dec->base.width * dec->base.height) /
634      (BLOCK_WIDTH * BLOCK_HEIGHT);
635
636   dec->zscan_source_format = find_first_supported_format(dec, const_zscan_source_formats,
637                                                          num_zscan_source_formats, PIPE_TEXTURE_2D);
638
639   if (dec->zscan_source_format == PIPE_FORMAT_NONE)
640      return false;
641
642   layout = vl_zscan_linear(dec->pipe, dec->blocks_per_line);
643
644   num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
645
646   if (!vl_zscan_init(&dec->zscan_y, dec->pipe, dec->base.width, dec->base.height,
647                      dec->blocks_per_line, dec->max_blocks, num_channels))
648      return false;
649
650   vl_zscan_set_layout(&dec->zscan_y, layout);
651
652   if (!vl_zscan_init(&dec->zscan_c, dec->pipe, dec->chroma_width, dec->chroma_height,
653                      dec->blocks_per_line, dec->max_blocks, num_channels))
654      return false;
655
656   vl_zscan_set_layout(&dec->zscan_c, layout);
657
658   return true;
659}
660
661static bool
662init_idct(struct vl_mpeg12_decoder *dec)
663{
664   struct pipe_sampler_view *matrix, *transpose = NULL;
665   float matrix_scale, transpose_scale;
666
667   dec->nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS);
668
669   // more than 4 render targets usually doesn't makes any seens
670   dec->nr_of_idct_render_targets = MIN2(dec->nr_of_idct_render_targets, 4);
671
672   dec->idct_source_format = find_first_supported_format(dec, const_idct_source_formats,
673                                                         num_idct_source_formats, PIPE_TEXTURE_2D);
674
675   if (dec->idct_source_format == PIPE_FORMAT_NONE)
676      return false;
677
678   dec->idct_intermediate_format = find_first_supported_format(dec, const_idct_intermediate_formats,
679                                                               num_idct_intermediate_formats, PIPE_TEXTURE_3D);
680
681   if (dec->idct_intermediate_format == PIPE_FORMAT_NONE)
682      return false;
683
684   switch (dec->idct_source_format) {
685   case PIPE_FORMAT_R16G16B16A16_SSCALED:
686      matrix_scale = SCALE_FACTOR_SSCALED;
687      break;
688
689   case PIPE_FORMAT_R16G16B16A16_SNORM:
690      matrix_scale = SCALE_FACTOR_SNORM;
691      break;
692
693   default:
694      assert(0);
695      return false;
696   }
697
698   if (dec->idct_intermediate_format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
699       dec->idct_intermediate_format == PIPE_FORMAT_R32G32B32A32_FLOAT)
700      transpose_scale = 1.0f;
701   else
702      transpose_scale = matrix_scale = sqrt(matrix_scale);
703
704   if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
705      transpose_scale /= SCALE_FACTOR_SSCALED;
706
707   if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale)))
708      goto error_matrix;
709
710   if (matrix_scale != transpose_scale) {
711      if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale)))
712         goto error_transpose;
713   } else
714      pipe_sampler_view_reference(&transpose, matrix);
715
716   if (!vl_idct_init(&dec->idct_y, dec->pipe, dec->base.width, dec->base.height,
717                     dec->nr_of_idct_render_targets, matrix, transpose))
718      goto error_y;
719
720   if(!vl_idct_init(&dec->idct_c, dec->pipe, dec->chroma_width, dec->chroma_height,
721                    dec->nr_of_idct_render_targets, matrix, transpose))
722      goto error_c;
723
724   pipe_sampler_view_reference(&matrix, NULL);
725   pipe_sampler_view_reference(&transpose, NULL);
726   return true;
727
728error_c:
729   vl_idct_cleanup(&dec->idct_y);
730
731error_y:
732   pipe_sampler_view_reference(&transpose, NULL);
733
734error_transpose:
735   pipe_sampler_view_reference(&matrix, NULL);
736
737error_matrix:
738   return false;
739}
740
741struct pipe_video_decoder *
742vl_create_mpeg12_decoder(struct pipe_video_context *context,
743                         struct pipe_context *pipe,
744                         enum pipe_video_profile profile,
745                         enum pipe_video_entrypoint entrypoint,
746                         enum pipe_video_chroma_format chroma_format,
747                         unsigned width, unsigned height)
748{
749   struct vl_mpeg12_decoder *dec;
750   float mc_scale;
751
752   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
753
754   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
755
756   if (!dec)
757      return NULL;
758
759   dec->base.context = context;
760   dec->base.profile = profile;
761   dec->base.entrypoint = entrypoint;
762   dec->base.chroma_format = chroma_format;
763   dec->base.width = width;
764   dec->base.height = height;
765
766   dec->base.destroy = vl_mpeg12_destroy;
767   dec->base.create_buffer = vl_mpeg12_create_buffer;
768   dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
769
770   dec->base.width = align(width, MACROBLOCK_WIDTH);
771   dec->base.height = align(height, MACROBLOCK_HEIGHT);
772
773   dec->pipe = pipe;
774
775   dec->quads = vl_vb_upload_quads(dec->pipe);
776   dec->pos = vl_vb_upload_pos(
777      dec->pipe,
778      dec->base.width / MACROBLOCK_WIDTH,
779      dec->base.height / MACROBLOCK_HEIGHT
780   );
781
782   dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->pipe);
783   dec->ves_mv = vl_vb_get_ves_mv(dec->pipe);
784
785   /* TODO: Implement 422, 444 */
786   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
787
788   dec->mc_source_format = find_first_supported_format(dec, const_mc_source_formats,
789                                                       num_mc_source_formats, PIPE_TEXTURE_3D);
790
791   if (dec->mc_source_format == PIPE_FORMAT_NONE)
792      return NULL;
793
794   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
795      dec->chroma_width = dec->base.width / 2;
796      dec->chroma_height = dec->base.height / 2;
797   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
798      dec->chroma_width = dec->base.width;
799      dec->chroma_height = dec->base.height / 2;
800   } else {
801      dec->chroma_width = dec->base.width;
802      dec->chroma_height = dec->base.height;
803   }
804
805   if (!init_zscan(dec))
806      goto error_zscan;
807
808   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
809      if (!init_idct(dec))
810         goto error_idct;
811      if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
812         mc_scale = SCALE_FACTOR_SSCALED;
813      else
814         mc_scale = 1.0f;
815   } else {
816      switch (dec->mc_source_format) {
817      case PIPE_FORMAT_R16_SNORM:
818         mc_scale = SCALE_FACTOR_SNORM;
819         break;
820
821      case PIPE_FORMAT_R16_SSCALED:
822         mc_scale = SCALE_FACTOR_SSCALED;
823         break;
824
825      default:
826         assert(0);
827         return NULL;
828      }
829   }
830
831   if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, MACROBLOCK_HEIGHT, mc_scale))
832      goto error_mc_y;
833
834   // TODO
835   if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, BLOCK_HEIGHT, mc_scale))
836      goto error_mc_c;
837
838   if (!init_pipe_state(dec))
839      goto error_pipe_state;
840
841   return &dec->base;
842
843error_pipe_state:
844   vl_mc_cleanup(&dec->mc_c);
845
846error_mc_c:
847   vl_mc_cleanup(&dec->mc_y);
848
849error_mc_y:
850   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
851      vl_idct_cleanup(&dec->idct_y);
852      vl_idct_cleanup(&dec->idct_c);
853   }
854
855error_idct:
856   vl_zscan_cleanup(&dec->zscan_y);
857   vl_zscan_cleanup(&dec->zscan_c);
858
859error_zscan:
860   FREE(dec);
861   return NULL;
862}
863