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