vl_mpeg12_decoder.c revision 0a2310b375068694d5700395aededc3fe68a0f3a
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 unsigned const_empty_block_mask_420[3][2][2] = {
42   { { 0x20, 0x10 },  { 0x08, 0x04 } },
43   { { 0x02, 0x02 },  { 0x02, 0x02 } },
44   { { 0x01, 0x01 },  { 0x01, 0x01 } }
45};
46
47static const enum pipe_format const_idct_source_formats[] = {
48   PIPE_FORMAT_R16G16B16A16_SNORM,
49   PIPE_FORMAT_R16G16B16A16_SSCALED
50};
51
52static const unsigned num_idct_source_formats =
53   sizeof(const_idct_source_formats) / sizeof(enum pipe_format);
54
55static const enum pipe_format const_idct_intermediate_formats[] = {
56   PIPE_FORMAT_R16G16B16A16_FLOAT,
57   PIPE_FORMAT_R16G16B16A16_SNORM,
58   PIPE_FORMAT_R16G16B16A16_SSCALED,
59   PIPE_FORMAT_R32G32B32A32_FLOAT
60};
61
62static const unsigned num_idct_intermediate_formats =
63   sizeof(const_idct_intermediate_formats) / sizeof(enum pipe_format);
64
65static const enum pipe_format const_mc_source_formats[] = {
66   PIPE_FORMAT_R16_SNORM,
67   PIPE_FORMAT_R16_SSCALED
68};
69
70static const unsigned num_mc_source_formats =
71   sizeof(const_mc_source_formats) / sizeof(enum pipe_format);
72
73static void
74map_buffers(struct vl_mpeg12_decoder *ctx, struct vl_mpeg12_buffer *buffer)
75{
76   struct pipe_sampler_view **sampler_views;
77   struct pipe_resource *tex;
78   unsigned i;
79
80   assert(ctx && buffer);
81
82   if (ctx->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
83      sampler_views = buffer->idct_source->get_sampler_views(buffer->idct_source);
84   else
85      sampler_views = buffer->mc_source->get_sampler_views(buffer->mc_source);
86   assert(sampler_views);
87
88   for (i = 0; i < VL_MAX_PLANES; ++i) {
89      tex = sampler_views[i]->texture;
90
91      struct pipe_box rect =
92      {
93         0, 0, 0,
94         tex->width0,
95         tex->height0,
96         1
97      };
98
99      buffer->tex_transfer[i] = ctx->pipe->get_transfer
100      (
101         ctx->pipe, tex,
102         0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
103         &rect
104      );
105
106      buffer->texels[i] = ctx->pipe->transfer_map(ctx->pipe, buffer->tex_transfer[i]);
107   }
108}
109
110static void
111upload_block(struct vl_mpeg12_buffer *buffer, unsigned plane, unsigned x, unsigned y, short *block)
112{
113   unsigned tex_pitch;
114   short *texels;
115
116   unsigned i;
117
118   assert(buffer);
119   assert(block);
120
121   tex_pitch = buffer->tex_transfer[plane]->stride / sizeof(short);
122   texels = buffer->texels[plane] + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH;
123
124   for (i = 0; i < BLOCK_HEIGHT; ++i)
125      memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short));
126}
127
128static void
129upload_buffer(struct vl_mpeg12_decoder *ctx,
130              struct vl_mpeg12_buffer *buffer,
131              struct pipe_mpeg12_macroblock *mb)
132{
133   short *blocks;
134   unsigned tb, x, y;
135
136   assert(ctx);
137   assert(buffer);
138   assert(mb);
139
140   blocks = mb->blocks;
141
142   for (y = 0; y < 2; ++y) {
143      for (x = 0; x < 2; ++x, ++tb) {
144         if (mb->cbp & (*ctx->empty_block_mask)[0][y][x]) {
145            upload_block(buffer, 0, mb->mbx * 2 + x, mb->mby * 2 + y, blocks);
146            blocks += BLOCK_WIDTH * BLOCK_HEIGHT;
147         }
148      }
149   }
150
151   /* TODO: Implement 422, 444 */
152   assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
153
154   for (tb = 1; tb < 3; ++tb) {
155      if (mb->cbp & (*ctx->empty_block_mask)[tb][0][0]) {
156         upload_block(buffer, tb, mb->mbx, mb->mby, blocks);
157         blocks += BLOCK_WIDTH * BLOCK_HEIGHT;
158      }
159   }
160}
161
162static void
163unmap_buffers(struct vl_mpeg12_decoder *ctx, struct vl_mpeg12_buffer *buffer)
164{
165   unsigned i;
166
167   assert(ctx && buffer);
168
169   for (i = 0; i < VL_MAX_PLANES; ++i) {
170      ctx->pipe->transfer_unmap(ctx->pipe, buffer->tex_transfer[i]);
171      ctx->pipe->transfer_destroy(ctx->pipe, buffer->tex_transfer[i]);
172   }
173}
174
175static void
176cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
177{
178   struct vl_mpeg12_decoder *dec;
179   assert(buf);
180
181   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
182   assert(dec);
183
184   buf->idct_source->destroy(buf->idct_source);
185   buf->idct_intermediate->destroy(buf->idct_intermediate);
186   vl_idct_cleanup_buffer(&dec->idct_y, &buf->idct[0]);
187   vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[1]);
188   vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[2]);
189}
190
191static void
192vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer)
193{
194   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
195   struct vl_mpeg12_decoder *dec;
196   unsigned i;
197
198   assert(buf);
199
200   dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
201   assert(dec);
202
203   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
204      cleanup_idct_buffer(buf);
205
206   buf->mc_source->destroy(buf->mc_source);
207   vl_vb_cleanup(&buf->vertex_stream);
208   for (i = 0; i < VL_MAX_PLANES; ++i)
209      vl_mc_cleanup_buffer(&buf->mc[i]);
210
211   FREE(buf);
212}
213
214static void
215vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer)
216{
217   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
218   struct vl_mpeg12_decoder *dec;
219   assert(buf);
220
221   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
222   assert(dec);
223
224   vl_vb_map(&buf->vertex_stream, dec->pipe);
225   map_buffers(dec, buf);
226}
227
228static void
229vl_mpeg12_buffer_add_macroblocks(struct pipe_video_decode_buffer *buffer,
230                                 unsigned num_macroblocks,
231                                 struct pipe_macroblock *macroblocks)
232{
233   struct pipe_mpeg12_macroblock *mb = (struct pipe_mpeg12_macroblock*)macroblocks;
234   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
235   struct vl_mpeg12_decoder *dec;
236   unsigned i;
237
238   assert(buf);
239
240   dec =  (struct vl_mpeg12_decoder*)buf->base.decoder;
241   assert(dec);
242
243   assert(num_macroblocks);
244   assert(macroblocks);
245   assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
246
247   for ( i = 0; i < num_macroblocks; ++i ) {
248      vl_vb_add_block(&buf->vertex_stream, &mb[i], dec->empty_block_mask);
249      upload_buffer(dec, buf, &mb[i]);
250   }
251}
252
253static void
254vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer)
255{
256   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
257   struct vl_mpeg12_decoder *dec;
258   assert(buf);
259
260   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
261   assert(dec);
262
263   vl_vb_unmap(&buf->vertex_stream, dec->pipe);
264   unmap_buffers(dec, buf);
265}
266
267static void
268vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
269{
270   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
271   unsigned i;
272
273   assert(decoder);
274
275   /* Asserted in softpipe_delete_fs_state() for some reason */
276   dec->pipe->bind_vs_state(dec->pipe, NULL);
277   dec->pipe->bind_fs_state(dec->pipe, NULL);
278
279   dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
280
281   vl_mc_cleanup(&dec->mc_y);
282   vl_mc_cleanup(&dec->mc_c);
283
284   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
285      vl_idct_cleanup(&dec->idct_y);
286      vl_idct_cleanup(&dec->idct_c);
287   }
288
289   for (i = 0; i < VL_MAX_PLANES; ++i)
290      dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_eb[i]);
291
292   for (i = 0; i < 2; ++i)
293      dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_mv[i]);
294
295   pipe_resource_reference(&dec->quads.buffer, NULL);
296
297   FREE(dec);
298}
299
300static bool
301init_idct_buffer(struct vl_mpeg12_buffer *buffer)
302{
303   enum pipe_format formats[3];
304
305   struct pipe_sampler_view **idct_source_sv, **idct_intermediate_sv;
306   struct pipe_surface **idct_surfaces;
307
308   struct vl_mpeg12_decoder *dec;
309
310   unsigned i;
311
312   assert(buffer);
313
314   dec = (struct vl_mpeg12_decoder*)buffer->base.decoder;
315
316   formats[0] = formats[1] = formats[2] = dec->idct_source_format;
317   buffer->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe,
318                                              dec->base.width / 4, dec->base.height, 1,
319                                              dec->base.chroma_format,
320                                              formats, PIPE_USAGE_STREAM);
321   if (!buffer->idct_source)
322      goto error_source;
323
324   formats[0] = formats[1] = formats[2] = dec->idct_intermediate_format;
325   buffer->idct_intermediate = vl_video_buffer_init(dec->base.context, dec->pipe,
326                                                    dec->base.width / dec->nr_of_idct_render_targets,
327                                                    dec->base.height / 4, dec->nr_of_idct_render_targets,
328                                                    dec->base.chroma_format,
329                                                    formats, PIPE_USAGE_STATIC);
330
331   if (!buffer->idct_intermediate)
332      goto error_intermediate;
333
334   idct_source_sv = buffer->idct_source->get_sampler_views(buffer->idct_source);
335   if (!idct_source_sv)
336      goto error_source_sv;
337
338   idct_intermediate_sv = buffer->idct_intermediate->get_sampler_views(buffer->idct_intermediate);
339   if (!idct_intermediate_sv)
340      goto error_intermediate_sv;
341
342   idct_surfaces = buffer->mc_source->get_surfaces(buffer->mc_source);
343   if (!idct_surfaces)
344      goto error_surfaces;
345
346   for (i = 0; i < 3; ++i)
347      if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
348                               &buffer->idct[i], idct_source_sv[i],
349                               idct_intermediate_sv[i], idct_surfaces[i]))
350         goto error_plane;
351
352   return true;
353
354error_plane:
355   for (; i > 0; --i)
356      vl_idct_cleanup_buffer(i == 1 ? &dec->idct_c : &dec->idct_y, &buffer->idct[i - 1]);
357
358error_surfaces:
359error_intermediate_sv:
360error_source_sv:
361   buffer->idct_intermediate->destroy(buffer->idct_intermediate);
362
363error_intermediate:
364   buffer->idct_source->destroy(buffer->idct_source);
365
366error_source:
367   return false;
368}
369
370static struct pipe_video_decode_buffer *
371vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
372{
373   enum pipe_format formats[3];
374
375   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
376   struct vl_mpeg12_buffer *buffer;
377
378   struct pipe_sampler_view **mc_source_sv;
379
380   assert(dec);
381
382   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
383   if (buffer == NULL)
384      return NULL;
385
386   buffer->base.decoder = decoder;
387   buffer->base.destroy = vl_mpeg12_buffer_destroy;
388   buffer->base.map = vl_mpeg12_buffer_map;
389   buffer->base.add_macroblocks = vl_mpeg12_buffer_add_macroblocks;
390   buffer->base.unmap = vl_mpeg12_buffer_unmap;
391
392   buffer->vertex_bufs.individual.quad.stride = dec->quads.stride;
393   buffer->vertex_bufs.individual.quad.buffer_offset = dec->quads.buffer_offset;
394   pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, dec->quads.buffer);
395
396   buffer->vertex_bufs.individual.stream = vl_vb_init(&buffer->vertex_stream, dec->pipe,
397                                                      dec->base.width / MACROBLOCK_WIDTH *
398                                                      dec->base.height / MACROBLOCK_HEIGHT);
399   if (!buffer->vertex_bufs.individual.stream.buffer)
400      goto error_vertex_stream;
401
402   formats[0] = formats[1] = formats[2] =dec->mc_source_format;
403   buffer->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe,
404                                            dec->base.width, dec->base.height, 1,
405                                            dec->base.chroma_format,
406                                            formats, PIPE_USAGE_STATIC);
407
408   if (!buffer->mc_source)
409      goto error_mc_source;
410
411   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
412      if (!init_idct_buffer(buffer))
413         goto error_idct;
414
415   mc_source_sv = buffer->mc_source->get_sampler_views(buffer->mc_source);
416   if (!mc_source_sv)
417      goto error_mc_source_sv;
418
419   if(!vl_mc_init_buffer(&dec->mc_y, &buffer->mc[0], mc_source_sv[0]))
420      goto error_mc_y;
421
422   if(!vl_mc_init_buffer(&dec->mc_c, &buffer->mc[1], mc_source_sv[1]))
423      goto error_mc_cb;
424
425   if(!vl_mc_init_buffer(&dec->mc_c, &buffer->mc[2], mc_source_sv[2]))
426      goto error_mc_cr;
427
428   return &buffer->base;
429
430error_mc_cr:
431   vl_mc_cleanup_buffer(&buffer->mc[1]);
432
433error_mc_cb:
434   vl_mc_cleanup_buffer(&buffer->mc[0]);
435
436error_mc_y:
437error_mc_source_sv:
438   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
439      cleanup_idct_buffer(buffer);
440
441error_idct:
442   buffer->mc_source->destroy(buffer->mc_source);
443
444error_mc_source:
445   vl_vb_cleanup(&buffer->vertex_stream);
446
447error_vertex_stream:
448   FREE(buffer);
449   return NULL;
450}
451
452static void
453vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
454                               struct pipe_video_buffer *refs[2],
455                               struct pipe_video_buffer *dst,
456                               struct pipe_fence_handle **fence)
457{
458   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
459   struct vl_mpeg12_decoder *dec;
460
461   struct pipe_sampler_view **sv[2];
462   struct pipe_surface **surfaces;
463
464   unsigned ne_start, ne_num, e_start, e_num;
465   unsigned i, j;
466
467   assert(buf);
468
469   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
470   assert(dec);
471
472   for (i = 0; i < 2; ++i)
473      sv[i] = refs[i] ? refs[i]->get_sampler_views(refs[i]) : NULL;
474
475   surfaces = dst->get_surfaces(dst);
476
477   vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num);
478
479   dec->pipe->set_vertex_buffers(dec->pipe, 2, buf->vertex_bufs.all);
480
481   for (i = 0; i < VL_MAX_PLANES; ++i) {
482      vl_mc_set_surface(&buf->mc[i], surfaces[i]);
483
484      for (j = 0; j < 2; ++j) {
485         if (sv[j] == NULL) continue;
486
487         dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv[j]);
488         vl_mc_render_ref(&buf->mc[i], sv[j][i], ne_start, ne_num, e_start, e_num);
489      }
490
491      dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_eb[i]);
492
493      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
494         vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], ne_num);
495
496      vl_mc_render_ycbcr(&buf->mc[i], ne_start, ne_num);
497
498   }
499   dec->pipe->flush(dec->pipe, fence);
500}
501
502static void
503vl_mpeg12_decoder_clear_buffer(struct pipe_video_decode_buffer *buffer)
504{
505   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
506   unsigned ne_start, ne_num, e_start, e_num;
507
508   assert(buf);
509
510   vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num);
511}
512
513static bool
514init_pipe_state(struct vl_mpeg12_decoder *dec)
515{
516   struct pipe_depth_stencil_alpha_state dsa;
517   unsigned i;
518
519   assert(dec);
520
521   memset(&dsa, 0, sizeof dsa);
522   dsa.depth.enabled = 0;
523   dsa.depth.writemask = 0;
524   dsa.depth.func = PIPE_FUNC_ALWAYS;
525   for (i = 0; i < 2; ++i) {
526      dsa.stencil[i].enabled = 0;
527      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
528      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
529      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
530      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
531      dsa.stencil[i].valuemask = 0;
532      dsa.stencil[i].writemask = 0;
533   }
534   dsa.alpha.enabled = 0;
535   dsa.alpha.func = PIPE_FUNC_ALWAYS;
536   dsa.alpha.ref_value = 0;
537   dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
538   dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
539
540   return true;
541}
542
543static enum pipe_format
544find_first_supported_format(struct vl_mpeg12_decoder *dec,
545                            const enum pipe_format formats[],
546                            unsigned num_formats,
547                            enum pipe_texture_target target)
548{
549   struct pipe_screen *screen;
550   unsigned i;
551
552   assert(dec);
553
554   screen = dec->pipe->screen;
555
556   for (i = 0; i < num_formats; ++i)
557      if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1,
558                                      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
559         return formats[i];
560
561   return PIPE_FORMAT_NONE;
562}
563
564static bool
565init_idct(struct vl_mpeg12_decoder *dec, unsigned buffer_width, unsigned buffer_height)
566{
567   unsigned chroma_width, chroma_height, chroma_blocks_x, chroma_blocks_y;
568   struct pipe_sampler_view *matrix, *transpose;
569   float matrix_scale, transpose_scale;
570
571   dec->nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS);
572
573   // more than 4 render targets usually doesn't makes any seens
574   dec->nr_of_idct_render_targets = MIN2(dec->nr_of_idct_render_targets, 4);
575
576   dec->idct_source_format = find_first_supported_format(dec, const_idct_source_formats,
577                                                         num_idct_source_formats, PIPE_TEXTURE_2D);
578
579   if (dec->idct_source_format == PIPE_FORMAT_NONE)
580      return false;
581
582   dec->idct_intermediate_format = find_first_supported_format(dec, const_idct_intermediate_formats,
583                                                               num_idct_intermediate_formats, PIPE_TEXTURE_3D);
584
585   if (dec->idct_intermediate_format == PIPE_FORMAT_NONE)
586      return false;
587
588   switch (dec->idct_source_format) {
589   case PIPE_FORMAT_R16G16B16A16_SSCALED:
590      matrix_scale = SCALE_FACTOR_SSCALED;
591      break;
592
593   case PIPE_FORMAT_R16G16B16A16_SNORM:
594      matrix_scale = SCALE_FACTOR_SNORM;
595      break;
596
597   default:
598      assert(0);
599      return false;
600   }
601
602   if (dec->idct_intermediate_format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
603       dec->idct_intermediate_format == PIPE_FORMAT_R32G32B32A32_FLOAT)
604      transpose_scale = 1.0f;
605   else
606      transpose_scale = matrix_scale = sqrt(matrix_scale);
607
608   if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
609      transpose_scale /= SCALE_FACTOR_SSCALED;
610
611   if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale)))
612      goto error_matrix;
613
614   if (matrix_scale != transpose_scale) {
615      if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale)))
616         goto error_transpose;
617   } else
618      pipe_sampler_view_reference(&transpose, matrix);
619
620   if (!vl_idct_init(&dec->idct_y, dec->pipe, buffer_width, buffer_height,
621                     2, 2, dec->nr_of_idct_render_targets, matrix, transpose))
622      goto error_y;
623
624   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
625      chroma_width = buffer_width / 2;
626      chroma_height = buffer_height / 2;
627      chroma_blocks_x = 1;
628      chroma_blocks_y = 1;
629   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
630      chroma_width = buffer_width;
631      chroma_height = buffer_height / 2;
632      chroma_blocks_x = 2;
633      chroma_blocks_y = 1;
634   } else {
635      chroma_width = buffer_width;
636      chroma_height = buffer_height;
637      chroma_blocks_x = 2;
638      chroma_blocks_y = 2;
639   }
640
641   if(!vl_idct_init(&dec->idct_c, dec->pipe, chroma_width, chroma_height,
642                    chroma_blocks_x, chroma_blocks_y,
643                    dec->nr_of_idct_render_targets, matrix, transpose))
644      goto error_c;
645
646   pipe_sampler_view_reference(&matrix, NULL);
647   pipe_sampler_view_reference(&transpose, NULL);
648   return true;
649
650error_c:
651   vl_idct_cleanup(&dec->idct_y);
652
653error_y:
654   pipe_sampler_view_reference(&transpose, NULL);
655
656error_transpose:
657   pipe_sampler_view_reference(&matrix, NULL);
658
659error_matrix:
660   return false;
661}
662
663struct pipe_video_decoder *
664vl_create_mpeg12_decoder(struct pipe_video_context *context,
665                         struct pipe_context *pipe,
666                         enum pipe_video_profile profile,
667                         enum pipe_video_entrypoint entrypoint,
668                         enum pipe_video_chroma_format chroma_format,
669                         unsigned width, unsigned height)
670{
671   struct vl_mpeg12_decoder *dec;
672   float mc_scale;
673   unsigned i;
674
675   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
676
677   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
678
679   if (!dec)
680      return NULL;
681
682   dec->base.context = context;
683   dec->base.profile = profile;
684   dec->base.entrypoint = entrypoint;
685   dec->base.chroma_format = chroma_format;
686   dec->base.width = width;
687   dec->base.height = height;
688
689   dec->base.destroy = vl_mpeg12_destroy;
690   dec->base.create_buffer = vl_mpeg12_create_buffer;
691   dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
692   dec->base.clear_buffer = vl_mpeg12_decoder_clear_buffer;
693
694   dec->pipe = pipe;
695
696   dec->quads = vl_vb_upload_quads(dec->pipe, 2, 2);
697   for (i = 0; i < VL_MAX_PLANES; ++i)
698      dec->ves_eb[i] = vl_vb_get_ves_eb(dec->pipe, i);
699
700   for (i = 0; i < 2; ++i)
701      dec->ves_mv[i] = vl_vb_get_ves_mv(dec->pipe, i);
702
703   dec->base.width = align(width, MACROBLOCK_WIDTH);
704   dec->base.height = align(height, MACROBLOCK_HEIGHT);
705
706   /* TODO: Implement 422, 444 */
707   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
708   dec->empty_block_mask = &const_empty_block_mask_420;
709
710   dec->mc_source_format = find_first_supported_format(dec, const_mc_source_formats,
711                                                       num_mc_source_formats, PIPE_TEXTURE_3D);
712
713   if (dec->mc_source_format == PIPE_FORMAT_NONE)
714      return NULL;
715
716   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
717      if (!init_idct(dec, dec->base.width, dec->base.height))
718         goto error_idct;
719      if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
720         mc_scale = SCALE_FACTOR_SSCALED;
721      else
722         mc_scale = 1.0f;
723   } else {
724      switch (dec->mc_source_format) {
725      case PIPE_FORMAT_R16_SNORM:
726         mc_scale = SCALE_FACTOR_SNORM;
727         break;
728
729      case PIPE_FORMAT_R16_SSCALED:
730         mc_scale = SCALE_FACTOR_SSCALED;
731         break;
732
733      default:
734         assert(0);
735         return NULL;
736      }
737   }
738
739   if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, MACROBLOCK_HEIGHT, mc_scale))
740      goto error_mc_y;
741
742   // TODO
743   if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, BLOCK_HEIGHT, mc_scale))
744      goto error_mc_c;
745
746   if (!init_pipe_state(dec))
747      goto error_pipe_state;
748
749   return &dec->base;
750
751error_pipe_state:
752   vl_mc_cleanup(&dec->mc_c);
753
754error_mc_c:
755   vl_mc_cleanup(&dec->mc_y);
756
757error_mc_y:
758   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
759      vl_idct_cleanup(&dec->idct_y);
760      vl_idct_cleanup(&dec->idct_c);
761   }
762
763error_idct:
764   FREE(dec);
765   return NULL;
766}
767