vl_mpeg12_decoder.c revision 5294ac62236bf05e1eaaca3399e539c28c0ccc4c
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_blend_state(dec->pipe, dec->blend);
280   dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
281
282   vl_mc_cleanup(&dec->mc_y);
283   vl_mc_cleanup(&dec->mc_c);
284
285   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
286      vl_idct_cleanup(&dec->idct_y);
287      vl_idct_cleanup(&dec->idct_c);
288   }
289
290   for (i = 0; i < VL_MAX_PLANES; ++i)
291      dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_eb[i]);
292
293   for (i = 0; i < 2; ++i)
294      dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_mv[i]);
295
296   pipe_resource_reference(&dec->quads.buffer, NULL);
297
298   FREE(dec);
299}
300
301static bool
302init_idct_buffer(struct vl_mpeg12_buffer *buffer)
303{
304   enum pipe_format formats[3];
305
306   struct pipe_sampler_view **idct_source_sv, **idct_intermediate_sv;
307   struct pipe_surface **idct_surfaces;
308
309   struct vl_mpeg12_decoder *dec;
310
311   unsigned i;
312
313   assert(buffer);
314
315   dec = (struct vl_mpeg12_decoder*)buffer->base.decoder;
316
317   formats[0] = formats[1] = formats[2] = dec->idct_source_format;
318   buffer->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe,
319                                              dec->base.width / 4, dec->base.height, 1,
320                                              dec->base.chroma_format,
321                                              formats, PIPE_USAGE_STREAM);
322   if (!buffer->idct_source)
323      goto error_source;
324
325   formats[0] = formats[1] = formats[2] = dec->idct_intermediate_format;
326   buffer->idct_intermediate = vl_video_buffer_init(dec->base.context, dec->pipe,
327                                                    dec->base.width / dec->nr_of_idct_render_targets,
328                                                    dec->base.height / 4, dec->nr_of_idct_render_targets,
329                                                    dec->base.chroma_format,
330                                                    formats, PIPE_USAGE_STATIC);
331
332   if (!buffer->idct_intermediate)
333      goto error_intermediate;
334
335   idct_source_sv = buffer->idct_source->get_sampler_views(buffer->idct_source);
336   if (!idct_source_sv)
337      goto error_source_sv;
338
339   idct_intermediate_sv = buffer->idct_intermediate->get_sampler_views(buffer->idct_intermediate);
340   if (!idct_intermediate_sv)
341      goto error_intermediate_sv;
342
343   idct_surfaces = buffer->mc_source->get_surfaces(buffer->mc_source);
344   if (!idct_surfaces)
345      goto error_surfaces;
346
347   for (i = 0; i < 3; ++i)
348      if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
349                               &buffer->idct[i], idct_source_sv[i],
350                               idct_intermediate_sv[i], idct_surfaces[i]))
351         goto error_plane;
352
353   return true;
354
355error_plane:
356   for (; i > 0; --i)
357      vl_idct_cleanup_buffer(i == 1 ? &dec->idct_c : &dec->idct_y, &buffer->idct[i - 1]);
358
359error_surfaces:
360error_intermediate_sv:
361error_source_sv:
362   buffer->idct_intermediate->destroy(buffer->idct_intermediate);
363
364error_intermediate:
365   buffer->idct_source->destroy(buffer->idct_source);
366
367error_source:
368   return false;
369}
370
371static struct pipe_video_decode_buffer *
372vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
373{
374   enum pipe_format formats[3];
375
376   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
377   struct vl_mpeg12_buffer *buffer;
378
379   struct pipe_sampler_view **mc_source_sv;
380
381   assert(dec);
382
383   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
384   if (buffer == NULL)
385      return NULL;
386
387   buffer->base.decoder = decoder;
388   buffer->base.destroy = vl_mpeg12_buffer_destroy;
389   buffer->base.map = vl_mpeg12_buffer_map;
390   buffer->base.add_macroblocks = vl_mpeg12_buffer_add_macroblocks;
391   buffer->base.unmap = vl_mpeg12_buffer_unmap;
392
393   buffer->vertex_bufs.individual.quad.stride = dec->quads.stride;
394   buffer->vertex_bufs.individual.quad.buffer_offset = dec->quads.buffer_offset;
395   pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, dec->quads.buffer);
396
397   buffer->vertex_bufs.individual.stream = vl_vb_init(&buffer->vertex_stream, dec->pipe,
398                                                      dec->base.width / MACROBLOCK_WIDTH *
399                                                      dec->base.height / MACROBLOCK_HEIGHT);
400   if (!buffer->vertex_bufs.individual.stream.buffer)
401      goto error_vertex_stream;
402
403   formats[0] = formats[1] = formats[2] =dec->mc_source_format;
404   buffer->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe,
405                                            dec->base.width, dec->base.height, 1,
406                                            dec->base.chroma_format,
407                                            formats, PIPE_USAGE_STATIC);
408
409   if (!buffer->mc_source)
410      goto error_mc_source;
411
412   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
413      if (!init_idct_buffer(buffer))
414         goto error_idct;
415
416   mc_source_sv = buffer->mc_source->get_sampler_views(buffer->mc_source);
417   if (!mc_source_sv)
418      goto error_mc_source_sv;
419
420   if(!vl_mc_init_buffer(&dec->mc_y, &buffer->mc[0], mc_source_sv[0]))
421      goto error_mc_y;
422
423   if(!vl_mc_init_buffer(&dec->mc_c, &buffer->mc[1], mc_source_sv[1]))
424      goto error_mc_cb;
425
426   if(!vl_mc_init_buffer(&dec->mc_c, &buffer->mc[2], mc_source_sv[2]))
427      goto error_mc_cr;
428
429   return &buffer->base;
430
431error_mc_cr:
432   vl_mc_cleanup_buffer(&buffer->mc[1]);
433
434error_mc_cb:
435   vl_mc_cleanup_buffer(&buffer->mc[0]);
436
437error_mc_y:
438error_mc_source_sv:
439   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
440      cleanup_idct_buffer(buffer);
441
442error_idct:
443   buffer->mc_source->destroy(buffer->mc_source);
444
445error_mc_source:
446   vl_vb_cleanup(&buffer->vertex_stream);
447
448error_vertex_stream:
449   FREE(buffer);
450   return NULL;
451}
452
453static void
454vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
455                               struct pipe_video_buffer *refs[2],
456                               struct pipe_video_buffer *dst,
457                               struct pipe_fence_handle **fence)
458{
459   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
460   struct vl_mpeg12_decoder *dec;
461
462   struct pipe_sampler_view **sv[2];
463   struct pipe_surface **surfaces;
464
465   unsigned ne_start, ne_num, e_start, e_num;
466   unsigned i, j;
467
468   assert(buf);
469
470   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
471   assert(dec);
472
473   for (i = 0; i < 2; ++i)
474      sv[i] = refs[i] ? refs[i]->get_sampler_views(refs[i]) : NULL;
475
476   surfaces = dst->get_surfaces(dst);
477
478   vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num);
479
480   dec->pipe->set_vertex_buffers(dec->pipe, 2, buf->vertex_bufs.all);
481
482   for (i = 0; i < VL_MAX_PLANES; ++i) {
483      vl_mc_set_surface(&buf->mc[i], surfaces[i]);
484
485      for (j = 0; j < 2; ++j) {
486         if (sv[j] == NULL) continue;
487
488         dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv[j]);
489         vl_mc_render_ref(&buf->mc[i], sv[j][i], ne_start, ne_num, e_start, e_num);
490      }
491
492      dec->pipe->bind_blend_state(dec->pipe, dec->blend);
493      dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_eb[i]);
494
495      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
496         vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], ne_num);
497
498      vl_mc_render_ycbcr(&buf->mc[i], ne_start, ne_num);
499
500   }
501   dec->pipe->flush(dec->pipe, fence);
502}
503
504static void
505vl_mpeg12_decoder_clear_buffer(struct pipe_video_decode_buffer *buffer)
506{
507   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
508   unsigned ne_start, ne_num, e_start, e_num;
509
510   assert(buf);
511
512   vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num);
513}
514
515static bool
516init_pipe_state(struct vl_mpeg12_decoder *dec)
517{
518   struct pipe_blend_state blend;
519   struct pipe_depth_stencil_alpha_state dsa;
520   unsigned i;
521
522   assert(dec);
523
524   memset(&blend, 0, sizeof blend);
525
526   blend.independent_blend_enable = 0;
527   blend.rt[0].blend_enable = 0;
528   blend.rt[0].rgb_func = PIPE_BLEND_ADD;
529   blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
530   blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
531   blend.rt[0].alpha_func = PIPE_BLEND_ADD;
532   blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
533   blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
534   blend.logicop_enable = 0;
535   blend.logicop_func = PIPE_LOGICOP_CLEAR;
536   /* Needed to allow color writes to FB, even if blending disabled */
537   blend.rt[0].colormask = PIPE_MASK_RGBA;
538   blend.dither = 0;
539   dec->blend = dec->pipe->create_blend_state(dec->pipe, &blend);
540
541   memset(&dsa, 0, sizeof dsa);
542   dsa.depth.enabled = 0;
543   dsa.depth.writemask = 0;
544   dsa.depth.func = PIPE_FUNC_ALWAYS;
545   for (i = 0; i < 2; ++i) {
546      dsa.stencil[i].enabled = 0;
547      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
548      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
549      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
550      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
551      dsa.stencil[i].valuemask = 0;
552      dsa.stencil[i].writemask = 0;
553   }
554   dsa.alpha.enabled = 0;
555   dsa.alpha.func = PIPE_FUNC_ALWAYS;
556   dsa.alpha.ref_value = 0;
557   dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
558   dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
559
560   return true;
561}
562
563static enum pipe_format
564find_first_supported_format(struct vl_mpeg12_decoder *dec,
565                            const enum pipe_format formats[],
566                            unsigned num_formats,
567                            enum pipe_texture_target target)
568{
569   struct pipe_screen *screen;
570   unsigned i;
571
572   assert(dec);
573
574   screen = dec->pipe->screen;
575
576   for (i = 0; i < num_formats; ++i)
577      if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1,
578                                      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
579         return formats[i];
580
581   return PIPE_FORMAT_NONE;
582}
583
584static bool
585init_idct(struct vl_mpeg12_decoder *dec, unsigned buffer_width, unsigned buffer_height)
586{
587   unsigned chroma_width, chroma_height, chroma_blocks_x, chroma_blocks_y;
588   struct pipe_sampler_view *matrix, *transpose;
589   float matrix_scale, transpose_scale;
590
591   dec->nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS);
592
593   // more than 4 render targets usually doesn't makes any seens
594   dec->nr_of_idct_render_targets = MIN2(dec->nr_of_idct_render_targets, 4);
595
596   dec->idct_source_format = find_first_supported_format(dec, const_idct_source_formats,
597                                                         num_idct_source_formats, PIPE_TEXTURE_2D);
598
599   if (dec->idct_source_format == PIPE_FORMAT_NONE)
600      return false;
601
602   dec->idct_intermediate_format = find_first_supported_format(dec, const_idct_intermediate_formats,
603                                                               num_idct_intermediate_formats, PIPE_TEXTURE_3D);
604
605   if (dec->idct_intermediate_format == PIPE_FORMAT_NONE)
606      return false;
607
608   switch (dec->idct_source_format) {
609   case PIPE_FORMAT_R16G16B16A16_SSCALED:
610      matrix_scale = SCALE_FACTOR_SSCALED;
611      break;
612
613   case PIPE_FORMAT_R16G16B16A16_SNORM:
614      matrix_scale = SCALE_FACTOR_SNORM;
615      break;
616
617   default:
618      assert(0);
619      return false;
620   }
621
622   if (dec->idct_intermediate_format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
623       dec->idct_intermediate_format == PIPE_FORMAT_R32G32B32A32_FLOAT)
624      transpose_scale = 1.0f;
625   else
626      transpose_scale = matrix_scale = sqrt(matrix_scale);
627
628   if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
629      transpose_scale /= SCALE_FACTOR_SSCALED;
630
631   if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale)))
632      goto error_matrix;
633
634   if (matrix_scale != transpose_scale) {
635      if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale)))
636         goto error_transpose;
637   } else
638      pipe_sampler_view_reference(&transpose, matrix);
639
640   if (!vl_idct_init(&dec->idct_y, dec->pipe, buffer_width, buffer_height,
641                     2, 2, dec->nr_of_idct_render_targets, matrix, transpose))
642      goto error_y;
643
644   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
645      chroma_width = buffer_width / 2;
646      chroma_height = buffer_height / 2;
647      chroma_blocks_x = 1;
648      chroma_blocks_y = 1;
649   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
650      chroma_width = buffer_width;
651      chroma_height = buffer_height / 2;
652      chroma_blocks_x = 2;
653      chroma_blocks_y = 1;
654   } else {
655      chroma_width = buffer_width;
656      chroma_height = buffer_height;
657      chroma_blocks_x = 2;
658      chroma_blocks_y = 2;
659   }
660
661   if(!vl_idct_init(&dec->idct_c, dec->pipe, chroma_width, chroma_height,
662                    chroma_blocks_x, chroma_blocks_y,
663                    dec->nr_of_idct_render_targets, matrix, transpose))
664      goto error_c;
665
666   pipe_sampler_view_reference(&matrix, NULL);
667   pipe_sampler_view_reference(&transpose, NULL);
668   return true;
669
670error_c:
671   vl_idct_cleanup(&dec->idct_y);
672
673error_y:
674   pipe_sampler_view_reference(&transpose, NULL);
675
676error_transpose:
677   pipe_sampler_view_reference(&matrix, NULL);
678
679error_matrix:
680   return false;
681}
682
683struct pipe_video_decoder *
684vl_create_mpeg12_decoder(struct pipe_video_context *context,
685                         struct pipe_context *pipe,
686                         enum pipe_video_profile profile,
687                         enum pipe_video_entrypoint entrypoint,
688                         enum pipe_video_chroma_format chroma_format,
689                         unsigned width, unsigned height)
690{
691   struct vl_mpeg12_decoder *dec;
692   float mc_scale;
693   unsigned i;
694
695   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
696
697   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
698
699   if (!dec)
700      return NULL;
701
702   dec->base.context = context;
703   dec->base.profile = profile;
704   dec->base.entrypoint = entrypoint;
705   dec->base.chroma_format = chroma_format;
706   dec->base.width = width;
707   dec->base.height = height;
708
709   dec->base.destroy = vl_mpeg12_destroy;
710   dec->base.create_buffer = vl_mpeg12_create_buffer;
711   dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
712   dec->base.clear_buffer = vl_mpeg12_decoder_clear_buffer;
713
714   dec->pipe = pipe;
715
716   dec->quads = vl_vb_upload_quads(dec->pipe, 2, 2);
717   for (i = 0; i < VL_MAX_PLANES; ++i)
718      dec->ves_eb[i] = vl_vb_get_ves_eb(dec->pipe, i);
719
720   for (i = 0; i < 2; ++i)
721      dec->ves_mv[i] = vl_vb_get_ves_mv(dec->pipe, i);
722
723   dec->base.width = align(width, MACROBLOCK_WIDTH);
724   dec->base.height = align(height, MACROBLOCK_HEIGHT);
725
726   /* TODO: Implement 422, 444 */
727   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
728   dec->empty_block_mask = &const_empty_block_mask_420;
729
730   dec->mc_source_format = find_first_supported_format(dec, const_mc_source_formats,
731                                                       num_mc_source_formats, PIPE_TEXTURE_3D);
732
733   if (dec->mc_source_format == PIPE_FORMAT_NONE)
734      return NULL;
735
736   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
737      if (!init_idct(dec, dec->base.width, dec->base.height))
738         goto error_idct;
739      if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED)
740         mc_scale = SCALE_FACTOR_SSCALED;
741      else
742         mc_scale = 1.0f;
743   } else {
744      switch (dec->mc_source_format) {
745      case PIPE_FORMAT_R16_SNORM:
746         mc_scale = SCALE_FACTOR_SNORM;
747         break;
748
749      case PIPE_FORMAT_R16_SSCALED:
750         mc_scale = SCALE_FACTOR_SSCALED;
751         break;
752
753      default:
754         assert(0);
755         return NULL;
756      }
757   }
758
759   if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, mc_scale))
760      goto error_mc_y;
761
762   if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, mc_scale))
763      goto error_mc_c;
764
765   if (!init_pipe_state(dec))
766      goto error_pipe_state;
767
768   return &dec->base;
769
770error_pipe_state:
771   vl_mc_cleanup(&dec->mc_c);
772
773error_mc_c:
774   vl_mc_cleanup(&dec->mc_y);
775
776error_mc_y:
777   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
778      vl_idct_cleanup(&dec->idct_y);
779      vl_idct_cleanup(&dec->idct_c);
780   }
781
782error_idct:
783   FREE(dec);
784   return NULL;
785}
786