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