vl_mpeg12_decoder.c revision d3770d6229d95e9beb67358ae2b2c8824ed3ae58
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 const unsigned const_empty_block_mask_420[3][2][2] = {
79   { { 0x20, 0x10 },  { 0x08, 0x04 } },
80   { { 0x02, 0x02 },  { 0x02, 0x02 } },
81   { { 0x01, 0x01 },  { 0x01, 0x01 } }
82};
83
84static bool
85init_zscan_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
86{
87   enum pipe_format formats[3];
88
89   struct pipe_sampler_view **source;
90   struct pipe_surface **destination;
91
92   unsigned i;
93
94   assert(dec && buffer);
95
96   formats[0] = formats[1] = formats[2] = dec->zscan_source_format;
97   buffer->zscan_source = vl_video_buffer_create_ex
98   (
99      dec->base.context,
100      dec->blocks_per_line * BLOCK_WIDTH * BLOCK_HEIGHT,
101      align(dec->num_blocks, dec->blocks_per_line) / dec->blocks_per_line,
102      1, PIPE_VIDEO_CHROMA_FORMAT_444, formats, PIPE_USAGE_STATIC
103   );
104
105   if (!buffer->zscan_source)
106      goto error_source;
107
108   source = buffer->zscan_source->get_sampler_view_planes(buffer->zscan_source);
109   if (!source)
110      goto error_sampler;
111
112   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
113      destination = dec->idct_source->get_surfaces(dec->idct_source);
114   else
115      destination = dec->mc_source->get_surfaces(dec->mc_source);
116
117   if (!destination)
118      goto error_surface;
119
120   for (i = 0; i < VL_MAX_PLANES; ++i)
121      if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c,
122                                &buffer->zscan[i], source[i], destination[i]))
123         goto error_plane;
124
125   return true;
126
127error_plane:
128   for (; i > 0; --i)
129      vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]);
130
131error_surface:
132error_sampler:
133   buffer->zscan_source->destroy(buffer->zscan_source);
134
135error_source:
136   return false;
137}
138
139static void
140cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer)
141{
142   unsigned i;
143
144   assert(buffer);
145
146   for (i = 0; i < VL_MAX_PLANES; ++i)
147      vl_zscan_cleanup_buffer(&buffer->zscan[i]);
148   buffer->zscan_source->destroy(buffer->zscan_source);
149}
150
151static bool
152init_idct_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buffer)
153{
154   struct pipe_sampler_view **idct_source_sv, **mc_source_sv;
155
156   unsigned i;
157
158   assert(dec && buffer);
159
160   idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source);
161   if (!idct_source_sv)
162      goto error_source_sv;
163
164   mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
165   if (!mc_source_sv)
166      goto error_mc_source_sv;
167
168   for (i = 0; i < 3; ++i)
169      if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c,
170                               &buffer->idct[i], idct_source_sv[i],
171                               mc_source_sv[i]))
172         goto error_plane;
173
174   return true;
175
176error_plane:
177   for (; i > 0; --i)
178      vl_idct_cleanup_buffer(&buffer->idct[i - 1]);
179
180error_mc_source_sv:
181error_source_sv:
182   return false;
183}
184
185static void
186cleanup_idct_buffer(struct vl_mpeg12_buffer *buf)
187{
188   unsigned i;
189
190   assert(buf);
191
192   for (i = 0; i < 3; ++i)
193      vl_idct_cleanup_buffer(&buf->idct[0]);
194}
195
196static bool
197init_mc_buffer(struct vl_mpeg12_decoder *dec, struct vl_mpeg12_buffer *buf)
198{
199   assert(dec && buf);
200
201   if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0]))
202      goto error_mc_y;
203
204   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1]))
205      goto error_mc_cb;
206
207   if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2]))
208      goto error_mc_cr;
209
210   return true;
211
212error_mc_cr:
213   vl_mc_cleanup_buffer(&buf->mc[1]);
214
215error_mc_cb:
216   vl_mc_cleanup_buffer(&buf->mc[0]);
217
218error_mc_y:
219   return false;
220}
221
222static void
223cleanup_mc_buffer(struct vl_mpeg12_buffer *buf)
224{
225   unsigned i;
226
227   assert(buf);
228
229   for (i = 0; i < VL_MAX_PLANES; ++i)
230      vl_mc_cleanup_buffer(&buf->mc[i]);
231}
232
233static inline void
234MacroBlockTypeToPipeWeights(const struct pipe_mpeg12_macroblock *mb, unsigned weights[2])
235{
236   assert(mb);
237
238   switch (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
239   case PIPE_MPEG12_MB_TYPE_MOTION_FORWARD:
240      weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
241      weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
242      break;
243
244   case (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD):
245      weights[0] = PIPE_VIDEO_MV_WEIGHT_HALF;
246      weights[1] = PIPE_VIDEO_MV_WEIGHT_HALF;
247      break;
248
249   case PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD:
250      weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
251      weights[1] = PIPE_VIDEO_MV_WEIGHT_MAX;
252      break;
253
254   default:
255      if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_PATTERN) {
256         /* patern without a motion vector, just copy the old frame content */
257         weights[0] = PIPE_VIDEO_MV_WEIGHT_MAX;
258         weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
259      } else {
260         weights[0] = PIPE_VIDEO_MV_WEIGHT_MIN;
261         weights[1] = PIPE_VIDEO_MV_WEIGHT_MIN;
262      }
263      break;
264   }
265}
266
267static inline struct vl_motionvector
268MotionVectorToPipe(const struct pipe_mpeg12_macroblock *mb, unsigned vector,
269                   unsigned field_select_mask, unsigned weight)
270{
271   struct vl_motionvector mv;
272
273   assert(mb);
274
275   if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
276      switch (mb->macroblock_modes.bits.frame_motion_type) {
277      case PIPE_MPEG12_MO_TYPE_FRAME:
278         mv.top.x = mb->PMV[0][vector][0];
279         mv.top.y = mb->PMV[0][vector][1];
280         mv.top.field_select = PIPE_VIDEO_FRAME;
281         mv.top.weight = weight;
282
283         mv.bottom.x = mb->PMV[0][vector][0];
284         mv.bottom.y = mb->PMV[0][vector][1];
285         mv.bottom.weight = weight;
286         mv.bottom.field_select = PIPE_VIDEO_FRAME;
287         break;
288
289      case PIPE_MPEG12_MO_TYPE_FIELD:
290         mv.top.x = mb->PMV[0][vector][0];
291         mv.top.y = mb->PMV[0][vector][1];
292         mv.top.field_select = (mb->motion_vertical_field_select & field_select_mask) ?
293            PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
294         mv.top.weight = weight;
295
296         mv.bottom.x = mb->PMV[1][vector][0];
297         mv.bottom.y = mb->PMV[1][vector][1];
298         mv.bottom.field_select = (mb->motion_vertical_field_select & (field_select_mask << 2)) ?
299            PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
300         mv.bottom.weight = weight;
301         break;
302
303      default: // TODO: Support DUALPRIME and 16x8
304         break;
305      }
306   } else {
307      mv.top.x = mv.top.y = 0;
308      mv.top.field_select = PIPE_VIDEO_FRAME;
309      mv.top.weight = weight;
310
311      mv.bottom.x = mv.bottom.y = 0;
312      mv.bottom.field_select = PIPE_VIDEO_FRAME;
313      mv.bottom.weight = weight;
314   }
315   return mv;
316}
317
318static inline void
319UploadYcbcrBlocks(struct vl_mpeg12_decoder *dec,
320                  struct vl_mpeg12_buffer *buf,
321                  const struct pipe_mpeg12_macroblock *mb)
322{
323   unsigned intra;
324   unsigned tb, x, y, luma_blocks;
325   short *blocks;
326
327   assert(dec && buf);
328   assert(mb);
329
330   if (!mb->coded_block_pattern)
331      return;
332
333   blocks = mb->blocks;
334   intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA ? 1 : 0;
335
336   for (y = 0, luma_blocks = 0; y < 2; ++y) {
337      for (x = 0; x < 2; ++x, ++tb) {
338         if (mb->coded_block_pattern & const_empty_block_mask_420[0][y][x]) {
339
340            struct vl_ycbcr_block *stream = buf->ycbcr_stream[0];
341            stream->x = mb->x * 2 + x;
342            stream->y = mb->y * 2 + y;
343            stream->intra = intra;
344            stream->coding = mb->macroblock_modes.bits.dct_type;
345
346            buf->num_ycbcr_blocks[0]++;
347            buf->ycbcr_stream[0]++;
348
349            luma_blocks++;
350         }
351      }
352   }
353
354   if (luma_blocks > 0) {
355      memcpy(buf->texels[0], blocks, 64 * sizeof(short) * luma_blocks);
356      buf->texels[0] += 64 * luma_blocks;
357      blocks += 64 * luma_blocks;
358   }
359
360   /* TODO: Implement 422, 444 */
361   //assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
362
363   for (tb = 1; tb < 3; ++tb) {
364      if (mb->coded_block_pattern & const_empty_block_mask_420[tb][0][0]) {
365
366         struct vl_ycbcr_block *stream = buf->ycbcr_stream[tb];
367         stream->x = mb->x;
368         stream->y = mb->y;
369         stream->intra = intra;
370         stream->coding = 0;
371
372         buf->num_ycbcr_blocks[tb]++;
373         buf->ycbcr_stream[tb]++;
374
375         memcpy(buf->texels[tb], blocks, 64 * sizeof(short));
376         buf->texels[tb] += 64;
377         blocks += 64;
378      }
379   }
380}
381
382static void
383vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
384{
385   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
386
387   assert(decoder);
388
389   /* Asserted in softpipe_delete_fs_state() for some reason */
390   dec->base.context->bind_vs_state(dec->base.context, NULL);
391   dec->base.context->bind_fs_state(dec->base.context, NULL);
392
393   dec->base.context->delete_depth_stencil_alpha_state(dec->base.context, dec->dsa);
394   dec->base.context->delete_sampler_state(dec->base.context, dec->sampler_ycbcr);
395
396   vl_mc_cleanup(&dec->mc_y);
397   vl_mc_cleanup(&dec->mc_c);
398   dec->mc_source->destroy(dec->mc_source);
399
400   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
401      vl_idct_cleanup(&dec->idct_y);
402      vl_idct_cleanup(&dec->idct_c);
403      dec->idct_source->destroy(dec->idct_source);
404   }
405
406   vl_zscan_cleanup(&dec->zscan_y);
407   vl_zscan_cleanup(&dec->zscan_c);
408
409   dec->base.context->delete_vertex_elements_state(dec->base.context, dec->ves_ycbcr);
410   dec->base.context->delete_vertex_elements_state(dec->base.context, dec->ves_mv);
411
412   pipe_resource_reference(&dec->quads.buffer, NULL);
413   pipe_resource_reference(&dec->pos.buffer, NULL);
414   pipe_resource_reference(&dec->block_num.buffer, NULL);
415
416   pipe_sampler_view_reference(&dec->zscan_linear, NULL);
417   pipe_sampler_view_reference(&dec->zscan_normal, NULL);
418   pipe_sampler_view_reference(&dec->zscan_alternate, NULL);
419
420   FREE(dec);
421}
422
423static void *
424vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
425{
426   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
427   struct vl_mpeg12_buffer *buffer;
428
429   assert(dec);
430
431   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
432   if (buffer == NULL)
433      return NULL;
434
435   if (!vl_vb_init(&buffer->vertex_stream, dec->base.context,
436                   dec->base.width / MACROBLOCK_WIDTH,
437                   dec->base.height / MACROBLOCK_HEIGHT))
438      goto error_vertex_buffer;
439
440   if (!init_mc_buffer(dec, buffer))
441      goto error_mc;
442
443   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
444      if (!init_idct_buffer(dec, buffer))
445         goto error_idct;
446
447   if (!init_zscan_buffer(dec, buffer))
448      goto error_zscan;
449
450   if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
451      vl_mpg12_bs_init(&buffer->bs,
452                       dec->base.width / MACROBLOCK_WIDTH,
453                       dec->base.height / MACROBLOCK_HEIGHT);
454
455   return buffer;
456
457error_zscan:
458   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
459      cleanup_idct_buffer(buffer);
460
461error_idct:
462   cleanup_mc_buffer(buffer);
463
464error_mc:
465   vl_vb_cleanup(&buffer->vertex_stream);
466
467error_vertex_buffer:
468   FREE(buffer);
469   return NULL;
470}
471
472static void
473vl_mpeg12_destroy_buffer(struct pipe_video_decoder *decoder, void *buffer)
474{
475   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
476   struct vl_mpeg12_buffer *buf = buffer;
477
478   assert(dec && buf);
479
480   cleanup_zscan_buffer(buf);
481
482   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
483      cleanup_idct_buffer(buf);
484
485   cleanup_mc_buffer(buf);
486
487   vl_vb_cleanup(&buf->vertex_stream);
488
489   FREE(buf);
490}
491
492static void
493vl_mpeg12_set_decode_buffer(struct pipe_video_decoder *decoder, void *buffer)
494{
495   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
496
497   assert(dec && buffer);
498
499   dec->current_buffer = buffer;
500}
501
502static void
503vl_mpeg12_set_picture_parameters(struct pipe_video_decoder *decoder,
504                                 struct pipe_picture_desc *picture)
505{
506   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
507   struct pipe_mpeg12_picture_desc *pic = (struct pipe_mpeg12_picture_desc *)picture;
508
509   assert(dec && pic);
510
511   dec->picture_desc = *pic;
512}
513
514static void
515vl_mpeg12_set_quant_matrix(struct pipe_video_decoder *decoder,
516                           const uint8_t intra_matrix[64],
517                           const uint8_t non_intra_matrix[64])
518{
519   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
520
521   assert(dec);
522
523   memcpy(dec->intra_matrix, intra_matrix, 64);
524   memcpy(dec->non_intra_matrix, non_intra_matrix, 64);
525}
526
527static void
528vl_mpeg12_set_decode_target(struct pipe_video_decoder *decoder,
529                            struct pipe_video_buffer *target)
530{
531   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
532   struct pipe_surface **surfaces;
533   unsigned i;
534
535   assert(dec);
536
537   surfaces = target->get_surfaces(target);
538   for (i = 0; i < VL_MAX_PLANES; ++i)
539      pipe_surface_reference(&dec->target_surfaces[i], surfaces[i]);
540}
541
542static void
543vl_mpeg12_set_reference_frames(struct pipe_video_decoder *decoder,
544                               struct pipe_video_buffer **ref_frames,
545                               unsigned num_ref_frames)
546{
547   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
548   struct pipe_sampler_view **sv;
549   unsigned i,j;
550
551   assert(dec);
552   assert(num_ref_frames <= VL_MAX_REF_FRAMES);
553
554   for (i = 0; i < num_ref_frames; ++i) {
555      sv = ref_frames[i]->get_sampler_view_planes(ref_frames[i]);
556      for (j = 0; j < VL_MAX_PLANES; ++j)
557         pipe_sampler_view_reference(&dec->ref_frames[i][j], sv[j]);
558   }
559
560   for (; i < VL_MAX_REF_FRAMES; ++i)
561      for (j = 0; j < VL_MAX_PLANES; ++j)
562         pipe_sampler_view_reference(&dec->ref_frames[i][j], NULL);
563}
564
565static void
566vl_mpeg12_begin_frame(struct pipe_video_decoder *decoder)
567{
568   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
569
570   struct vl_mpeg12_buffer *buf;
571   struct pipe_sampler_view **sampler_views;
572   unsigned i;
573
574   assert(dec);
575
576   buf = dec->current_buffer;
577   assert(buf);
578
579   for (i = 0; i < VL_MAX_PLANES; ++i) {
580      vl_zscan_upload_quant(&buf->zscan[i], dec->intra_matrix, true);
581      vl_zscan_upload_quant(&buf->zscan[i], dec->non_intra_matrix, false);
582   }
583
584   vl_vb_map(&buf->vertex_stream, dec->base.context);
585
586   sampler_views = buf->zscan_source->get_sampler_view_planes(buf->zscan_source);
587
588   assert(sampler_views);
589
590   for (i = 0; i < VL_MAX_PLANES; ++i) {
591      struct pipe_resource *tex = sampler_views[i]->texture;
592      struct pipe_box rect =
593      {
594         0, 0, 0,
595         tex->width0,
596         tex->height0,
597         1
598      };
599
600      buf->tex_transfer[i] = dec->base.context->get_transfer
601      (
602         dec->base.context, tex,
603         0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
604         &rect
605      );
606
607      buf->texels[i] = dec->base.context->transfer_map(dec->base.context, buf->tex_transfer[i]);
608
609      buf->num_ycbcr_blocks[i] = 0;
610   }
611
612   for (i = 0; i < VL_MAX_PLANES; ++i)
613      buf->ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i);
614
615   for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
616      buf->mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i);
617
618   if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
619      vl_mpg12_bs_set_buffers(&buf->bs, buf->ycbcr_stream, buf->texels, buf->mv_stream);
620
621   } else {
622
623      for (i = 0; i < VL_MAX_PLANES; ++i)
624         vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear);
625   }
626}
627
628static void
629vl_mpeg12_decode_macroblock(struct pipe_video_decoder *decoder,
630                            const struct pipe_macroblock *macroblocks,
631                            unsigned num_macroblocks)
632{
633   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
634   const struct pipe_mpeg12_macroblock *mb = (const struct pipe_mpeg12_macroblock *)macroblocks;
635   struct vl_mpeg12_buffer *buf;
636
637   unsigned i, j, mv_weights[2];
638
639   assert(dec && dec->current_buffer);
640   assert(macroblocks && macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
641
642   buf = dec->current_buffer;
643   assert(buf);
644
645   for (; num_macroblocks > 0; --num_macroblocks) {
646      unsigned mb_addr = mb->y * dec->width_in_macroblocks + mb->x;
647
648      if (mb->macroblock_type & (PIPE_MPEG12_MB_TYPE_PATTERN | PIPE_MPEG12_MB_TYPE_INTRA))
649         UploadYcbcrBlocks(dec, buf, mb);
650
651      MacroBlockTypeToPipeWeights(mb, mv_weights);
652
653      for (i = 0; i < 2; ++i) {
654          if (!dec->ref_frames[i][0]) continue;
655
656         buf->mv_stream[i][mb_addr] = MotionVectorToPipe
657         (
658            mb, i,
659            i ? PIPE_MPEG12_FS_FIRST_BACKWARD : PIPE_MPEG12_FS_FIRST_FORWARD,
660            mv_weights[i]
661         );
662      }
663
664      /* see section 7.6.6 of the spec */
665      if (mb->num_skipped_macroblocks > 0) {
666         struct vl_motionvector skipped_mv[2];
667
668         if (dec->ref_frames[0][0] && !dec->ref_frames[1][0]) {
669            skipped_mv[0].top.x = skipped_mv[0].top.y = 0;
670            skipped_mv[0].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
671         } else {
672           skipped_mv[0] = buf->mv_stream[0][mb_addr];
673           skipped_mv[1] = buf->mv_stream[1][mb_addr];
674         }
675         skipped_mv[0].top.field_select = PIPE_VIDEO_FRAME;
676         skipped_mv[1].top.field_select = PIPE_VIDEO_FRAME;
677
678         skipped_mv[0].bottom = skipped_mv[0].top;
679         skipped_mv[1].bottom = skipped_mv[1].top;
680
681         ++mb_addr;
682         for (i = 0; i < mb->num_skipped_macroblocks; ++i, ++mb_addr) {
683            for (j = 0; j < 2; ++j) {
684               if (!dec->ref_frames[j][0]) continue;
685               buf->mv_stream[j][mb_addr] = skipped_mv[j];
686
687            }
688         }
689      }
690
691      ++mb;
692   }
693}
694
695static void
696vl_mpeg12_decode_bitstream(struct pipe_video_decoder *decoder,
697                           unsigned num_bytes, const void *data)
698{
699   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
700   struct vl_mpeg12_buffer *buf;
701
702   unsigned i;
703
704   assert(dec && dec->current_buffer);
705
706   buf = dec->current_buffer;
707   assert(buf);
708
709   for (i = 0; i < VL_MAX_PLANES; ++i)
710      vl_zscan_set_layout(&buf->zscan[i], dec->picture_desc.alternate_scan ?
711                          dec->zscan_alternate : dec->zscan_normal);
712
713   vl_mpg12_bs_decode(&buf->bs, num_bytes, data, &dec->picture_desc, buf->num_ycbcr_blocks);
714}
715
716static void
717vl_mpeg12_end_frame(struct pipe_video_decoder *decoder)
718{
719   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder *)decoder;
720   struct pipe_sampler_view **mc_source_sv;
721   struct pipe_vertex_buffer vb[3];
722   struct vl_mpeg12_buffer *buf;
723
724   unsigned i, j, component;
725   unsigned nr_components;
726
727   assert(dec && dec->current_buffer);
728
729   buf = dec->current_buffer;
730
731   vl_vb_unmap(&buf->vertex_stream, dec->base.context);
732
733   for (i = 0; i < VL_MAX_PLANES; ++i) {
734      dec->base.context->transfer_unmap(dec->base.context, buf->tex_transfer[i]);
735      dec->base.context->transfer_destroy(dec->base.context, buf->tex_transfer[i]);
736   }
737
738   vb[0] = dec->quads;
739   vb[1] = dec->pos;
740
741   dec->base.context->bind_vertex_elements_state(dec->base.context, dec->ves_mv);
742   for (i = 0; i < VL_MAX_PLANES; ++i) {
743      if (!dec->target_surfaces[i]) continue;
744
745      vl_mc_set_surface(&buf->mc[i], dec->target_surfaces[i]);
746
747      for (j = 0; j < VL_MAX_REF_FRAMES; ++j) {
748         if (!dec->ref_frames[j][i]) continue;
749
750         vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);;
751         dec->base.context->set_vertex_buffers(dec->base.context, 3, vb);
752
753         vl_mc_render_ref(&buf->mc[i], dec->ref_frames[j][i]);
754      }
755   }
756
757   vb[2] = dec->block_num;
758
759   dec->base.context->bind_vertex_elements_state(dec->base.context, dec->ves_ycbcr);
760   for (i = 0; i < VL_MAX_PLANES; ++i) {
761      if (!buf->num_ycbcr_blocks[i]) continue;
762
763      vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i);
764      dec->base.context->set_vertex_buffers(dec->base.context, 3, vb);
765
766      vl_zscan_render(&buf->zscan[i] , buf->num_ycbcr_blocks[i]);
767
768      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
769         vl_idct_flush(&buf->idct[i], buf->num_ycbcr_blocks[i]);
770   }
771
772   mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source);
773   for (i = 0, component = 0; i < VL_MAX_PLANES; ++i) {
774      if (!dec->target_surfaces[i]) continue;
775
776      nr_components = util_format_get_nr_components(dec->target_surfaces[i]->texture->format);
777      for (j = 0; j < nr_components; ++j, ++component) {
778         if (!buf->num_ycbcr_blocks[i]) continue;
779
780         vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, component);
781         dec->base.context->set_vertex_buffers(dec->base.context, 3, vb);
782
783         if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
784            vl_idct_prepare_stage2(&buf->idct[component]);
785         else {
786            dec->base.context->set_fragment_sampler_views(dec->base.context, 1, &mc_source_sv[component]);
787            dec->base.context->bind_fragment_sampler_states(dec->base.context, 1, &dec->sampler_ycbcr);
788         }
789         vl_mc_render_ycbcr(&buf->mc[i], j, buf->num_ycbcr_blocks[component]);
790      }
791   }
792}
793
794static void
795vl_mpeg12_flush(struct pipe_video_decoder *decoder)
796{
797   assert(decoder);
798
799   //Noop, for shaders it is much faster to flush everything in end_frame
800}
801
802static bool
803init_pipe_state(struct vl_mpeg12_decoder *dec)
804{
805   struct pipe_depth_stencil_alpha_state dsa;
806   struct pipe_sampler_state sampler;
807   unsigned i;
808
809   assert(dec);
810
811   memset(&dsa, 0, sizeof dsa);
812   dsa.depth.enabled = 0;
813   dsa.depth.writemask = 0;
814   dsa.depth.func = PIPE_FUNC_ALWAYS;
815   for (i = 0; i < 2; ++i) {
816      dsa.stencil[i].enabled = 0;
817      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
818      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
819      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
820      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
821      dsa.stencil[i].valuemask = 0;
822      dsa.stencil[i].writemask = 0;
823   }
824   dsa.alpha.enabled = 0;
825   dsa.alpha.func = PIPE_FUNC_ALWAYS;
826   dsa.alpha.ref_value = 0;
827   dec->dsa = dec->base.context->create_depth_stencil_alpha_state(dec->base.context, &dsa);
828   dec->base.context->bind_depth_stencil_alpha_state(dec->base.context, dec->dsa);
829
830   memset(&sampler, 0, sizeof(sampler));
831   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
832   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
833   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
834   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
835   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
836   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
837   sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
838   sampler.compare_func = PIPE_FUNC_ALWAYS;
839   sampler.normalized_coords = 1;
840   dec->sampler_ycbcr = dec->base.context->create_sampler_state(dec->base.context, &sampler);
841   if (!dec->sampler_ycbcr)
842      return false;
843
844   return true;
845}
846
847static const struct format_config*
848find_format_config(struct vl_mpeg12_decoder *dec, const struct format_config configs[], unsigned num_configs)
849{
850   struct pipe_screen *screen;
851   unsigned i;
852
853   assert(dec);
854
855   screen = dec->base.context->screen;
856
857   for (i = 0; i < num_configs; ++i) {
858      if (!screen->is_format_supported(screen, configs[i].zscan_source_format, PIPE_TEXTURE_2D,
859                                       1, PIPE_BIND_SAMPLER_VIEW))
860         continue;
861
862      if (configs[i].idct_source_format != PIPE_FORMAT_NONE) {
863         if (!screen->is_format_supported(screen, configs[i].idct_source_format, PIPE_TEXTURE_2D,
864                                          1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
865            continue;
866
867         if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_3D,
868                                          1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
869            continue;
870      } else {
871         if (!screen->is_format_supported(screen, configs[i].mc_source_format, PIPE_TEXTURE_2D,
872                                          1, PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET))
873            continue;
874      }
875      return &configs[i];
876   }
877
878   return NULL;
879}
880
881static bool
882init_zscan(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
883{
884   unsigned num_channels;
885
886   assert(dec);
887
888   dec->zscan_source_format = format_config->zscan_source_format;
889   dec->zscan_linear = vl_zscan_layout(dec->base.context, vl_zscan_linear, dec->blocks_per_line);
890   dec->zscan_normal = vl_zscan_layout(dec->base.context, vl_zscan_normal, dec->blocks_per_line);
891   dec->zscan_alternate = vl_zscan_layout(dec->base.context, vl_zscan_alternate, dec->blocks_per_line);
892
893   num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1;
894
895   if (!vl_zscan_init(&dec->zscan_y, dec->base.context, dec->base.width, dec->base.height,
896                      dec->blocks_per_line, dec->num_blocks, num_channels))
897      return false;
898
899   if (!vl_zscan_init(&dec->zscan_c, dec->base.context, dec->chroma_width, dec->chroma_height,
900                      dec->blocks_per_line, dec->num_blocks, num_channels))
901      return false;
902
903   return true;
904}
905
906static bool
907init_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
908{
909   unsigned nr_of_idct_render_targets, max_inst;
910   enum pipe_format formats[3];
911
912   struct pipe_sampler_view *matrix = NULL;
913
914   nr_of_idct_render_targets = dec->base.context->screen->get_param
915   (
916      dec->base.context->screen, PIPE_CAP_MAX_RENDER_TARGETS
917   );
918
919   max_inst = dec->base.context->screen->get_shader_param
920   (
921      dec->base.context->screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_MAX_INSTRUCTIONS
922   );
923
924   // Just assume we need 32 inst per render target, not 100% true, but should work in most cases
925   if (nr_of_idct_render_targets >= 4 && max_inst >= 32*4)
926      // more than 4 render targets usually doesn't makes any seens
927      nr_of_idct_render_targets = 4;
928   else
929      nr_of_idct_render_targets = 1;
930
931   formats[0] = formats[1] = formats[2] = format_config->idct_source_format;
932   dec->idct_source = vl_video_buffer_create_ex
933   (
934      dec->base.context, dec->base.width / 4, dec->base.height, 1,
935      dec->base.chroma_format, formats, PIPE_USAGE_STATIC
936   );
937
938   if (!dec->idct_source)
939      goto error_idct_source;
940
941   formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
942   dec->mc_source = vl_video_buffer_create_ex
943   (
944      dec->base.context, dec->base.width / nr_of_idct_render_targets,
945      dec->base.height / 4, nr_of_idct_render_targets,
946      dec->base.chroma_format, formats, PIPE_USAGE_STATIC
947   );
948
949   if (!dec->mc_source)
950      goto error_mc_source;
951
952   if (!(matrix = vl_idct_upload_matrix(dec->base.context, format_config->idct_scale)))
953      goto error_matrix;
954
955   if (!vl_idct_init(&dec->idct_y, dec->base.context, dec->base.width, dec->base.height,
956                     nr_of_idct_render_targets, matrix, matrix))
957      goto error_y;
958
959   if(!vl_idct_init(&dec->idct_c, dec->base.context, dec->chroma_width, dec->chroma_height,
960                    nr_of_idct_render_targets, matrix, matrix))
961      goto error_c;
962
963   pipe_sampler_view_reference(&matrix, NULL);
964
965   return true;
966
967error_c:
968   vl_idct_cleanup(&dec->idct_y);
969
970error_y:
971   pipe_sampler_view_reference(&matrix, NULL);
972
973error_matrix:
974   dec->mc_source->destroy(dec->mc_source);
975
976error_mc_source:
977   dec->idct_source->destroy(dec->idct_source);
978
979error_idct_source:
980   return false;
981}
982
983static bool
984init_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, const struct format_config* format_config)
985{
986   enum pipe_format formats[3];
987
988   formats[0] = formats[1] = formats[2] = format_config->mc_source_format;
989   dec->mc_source = vl_video_buffer_create_ex
990   (
991      dec->base.context, dec->base.width, dec->base.height, 1,
992      dec->base.chroma_format, formats, PIPE_USAGE_STATIC
993   );
994
995   return dec->mc_source != NULL;
996}
997
998static void
999mc_vert_shader_callback(void *priv, struct vl_mc *mc,
1000                        struct ureg_program *shader,
1001                        unsigned first_output,
1002                        struct ureg_dst tex)
1003{
1004   struct vl_mpeg12_decoder *dec = priv;
1005   struct ureg_dst o_vtex;
1006
1007   assert(priv && mc);
1008   assert(shader);
1009
1010   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1011      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1012      vl_idct_stage2_vert_shader(idct, shader, first_output, tex);
1013   } else {
1014      o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output);
1015      ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex));
1016   }
1017}
1018
1019static void
1020mc_frag_shader_callback(void *priv, struct vl_mc *mc,
1021                        struct ureg_program *shader,
1022                        unsigned first_input,
1023                        struct ureg_dst dst)
1024{
1025   struct vl_mpeg12_decoder *dec = priv;
1026   struct ureg_src src, sampler;
1027
1028   assert(priv && mc);
1029   assert(shader);
1030
1031   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1032      struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c;
1033      vl_idct_stage2_frag_shader(idct, shader, first_input, dst);
1034   } else {
1035      src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR);
1036      sampler = ureg_DECL_sampler(shader, 0);
1037      ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler);
1038   }
1039}
1040
1041struct pipe_video_decoder *
1042vl_create_mpeg12_decoder(struct pipe_context *context,
1043                         enum pipe_video_profile profile,
1044                         enum pipe_video_entrypoint entrypoint,
1045                         enum pipe_video_chroma_format chroma_format,
1046                         unsigned width, unsigned height)
1047{
1048   const unsigned block_size_pixels = BLOCK_WIDTH * BLOCK_HEIGHT;
1049   const struct format_config *format_config;
1050   struct vl_mpeg12_decoder *dec;
1051
1052   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
1053
1054   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
1055
1056   if (!dec)
1057      return NULL;
1058
1059   dec->base.context = context;
1060   dec->base.profile = profile;
1061   dec->base.entrypoint = entrypoint;
1062   dec->base.chroma_format = chroma_format;
1063   dec->base.width = width;
1064   dec->base.height = height;
1065
1066   dec->base.destroy = vl_mpeg12_destroy;
1067   dec->base.create_buffer = vl_mpeg12_create_buffer;
1068   dec->base.destroy_buffer = vl_mpeg12_destroy_buffer;
1069   dec->base.set_decode_buffer = vl_mpeg12_set_decode_buffer;
1070   dec->base.set_picture_parameters = vl_mpeg12_set_picture_parameters;
1071   dec->base.set_quant_matrix = vl_mpeg12_set_quant_matrix;
1072   dec->base.set_decode_target = vl_mpeg12_set_decode_target;
1073   dec->base.set_reference_frames = vl_mpeg12_set_reference_frames;
1074   dec->base.begin_frame = vl_mpeg12_begin_frame;
1075   dec->base.decode_macroblock = vl_mpeg12_decode_macroblock;
1076   dec->base.decode_bitstream = vl_mpeg12_decode_bitstream;
1077   dec->base.end_frame = vl_mpeg12_end_frame;
1078   dec->base.flush = vl_mpeg12_flush;
1079
1080   dec->blocks_per_line = MAX2(util_next_power_of_two(dec->base.width) / block_size_pixels, 4);
1081   dec->num_blocks = (dec->base.width * dec->base.height) / block_size_pixels;
1082   dec->width_in_macroblocks = align(dec->base.width, MACROBLOCK_WIDTH) / MACROBLOCK_WIDTH;
1083
1084   dec->quads = vl_vb_upload_quads(dec->base.context);
1085   dec->pos = vl_vb_upload_pos(
1086      dec->base.context,
1087      dec->base.width / MACROBLOCK_WIDTH,
1088      dec->base.height / MACROBLOCK_HEIGHT
1089   );
1090   dec->block_num = vl_vb_upload_block_num(dec->base.context, dec->num_blocks);
1091
1092   dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->base.context);
1093   dec->ves_mv = vl_vb_get_ves_mv(dec->base.context);
1094
1095   /* TODO: Implement 422, 444 */
1096   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
1097
1098   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
1099      dec->chroma_width = dec->base.width / 2;
1100      dec->chroma_height = dec->base.height / 2;
1101   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
1102      dec->chroma_width = dec->base.width;
1103      dec->chroma_height = dec->base.height / 2;
1104   } else {
1105      dec->chroma_width = dec->base.width;
1106      dec->chroma_height = dec->base.height;
1107   }
1108
1109   switch (entrypoint) {
1110   case PIPE_VIDEO_ENTRYPOINT_BITSTREAM:
1111      format_config = find_format_config(dec, bitstream_format_config, num_bitstream_format_configs);
1112      break;
1113
1114   case PIPE_VIDEO_ENTRYPOINT_IDCT:
1115      format_config = find_format_config(dec, idct_format_config, num_idct_format_configs);
1116      break;
1117
1118   case PIPE_VIDEO_ENTRYPOINT_MC:
1119      format_config = find_format_config(dec, mc_format_config, num_mc_format_configs);
1120      break;
1121
1122   default:
1123      assert(0);
1124      return NULL;
1125   }
1126
1127   if (!format_config)
1128      return NULL;
1129
1130   if (!init_zscan(dec, format_config))
1131      goto error_zscan;
1132
1133   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1134      if (!init_idct(dec, format_config))
1135         goto error_sources;
1136   } else {
1137      if (!init_mc_source_widthout_idct(dec, format_config))
1138         goto error_sources;
1139   }
1140
1141   if (!vl_mc_init(&dec->mc_y, dec->base.context, dec->base.width, dec->base.height,
1142                   MACROBLOCK_HEIGHT, format_config->mc_scale,
1143                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
1144      goto error_mc_y;
1145
1146   // TODO
1147   if (!vl_mc_init(&dec->mc_c, dec->base.context, dec->base.width, dec->base.height,
1148                   BLOCK_HEIGHT, format_config->mc_scale,
1149                   mc_vert_shader_callback, mc_frag_shader_callback, dec))
1150      goto error_mc_c;
1151
1152   if (!init_pipe_state(dec))
1153      goto error_pipe_state;
1154
1155   return &dec->base;
1156
1157error_pipe_state:
1158   vl_mc_cleanup(&dec->mc_c);
1159
1160error_mc_c:
1161   vl_mc_cleanup(&dec->mc_y);
1162
1163error_mc_y:
1164   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
1165      vl_idct_cleanup(&dec->idct_y);
1166      vl_idct_cleanup(&dec->idct_c);
1167      dec->idct_source->destroy(dec->idct_source);
1168   }
1169   dec->mc_source->destroy(dec->mc_source);
1170
1171error_sources:
1172   vl_zscan_cleanup(&dec->zscan_y);
1173   vl_zscan_cleanup(&dec->zscan_c);
1174
1175error_zscan:
1176   FREE(dec);
1177   return NULL;
1178}
1179