vl_mpeg12_decoder.c revision fcdf50f74befad8d89eb3f9cdfd88b82d1daa98c
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_16_TO_9 (32768.0f / 256.0f)
39
40static const unsigned const_empty_block_mask_420[3][2][2] = {
41        { { 0x20, 0x10 },  { 0x08, 0x04 } },
42        { { 0x02, 0x02 },  { 0x02, 0x02 } },
43        { { 0x01, 0x01 },  { 0x01, 0x01 } }
44};
45
46static void
47map_buffers(struct vl_mpeg12_decoder *ctx, struct vl_mpeg12_buffer *buffer)
48{
49   struct pipe_sampler_view **sampler_views;
50   struct pipe_resource *tex;
51   unsigned i;
52
53   assert(ctx && buffer);
54
55   if (ctx->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
56      sampler_views = buffer->idct_source->get_sampler_views(buffer->idct_source);
57   else
58      sampler_views = buffer->mc_source->get_sampler_views(buffer->mc_source);
59   assert(sampler_views);
60
61   for (i = 0; i < VL_MAX_PLANES; ++i) {
62      tex = sampler_views[i]->texture;
63
64      struct pipe_box rect =
65      {
66         0, 0, 0,
67         tex->width0,
68         tex->height0,
69         1
70      };
71
72      buffer->tex_transfer[i] = ctx->pipe->get_transfer
73      (
74         ctx->pipe, tex,
75         0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
76         &rect
77      );
78
79      buffer->texels[i] = ctx->pipe->transfer_map(ctx->pipe, buffer->tex_transfer[i]);
80   }
81}
82
83static void
84upload_block(struct vl_mpeg12_buffer *buffer, unsigned plane, unsigned x, unsigned y, short *block)
85{
86   unsigned tex_pitch;
87   short *texels;
88
89   unsigned i;
90
91   assert(buffer);
92   assert(block);
93
94   tex_pitch = buffer->tex_transfer[plane]->stride / sizeof(short);
95   texels = buffer->texels[plane] + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH;
96
97   for (i = 0; i < BLOCK_HEIGHT; ++i)
98      memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short));
99}
100
101static void
102upload_buffer(struct vl_mpeg12_decoder *ctx,
103              struct vl_mpeg12_buffer *buffer,
104              struct pipe_mpeg12_macroblock *mb)
105{
106   short *blocks;
107   unsigned tb, x, y;
108
109   assert(ctx);
110   assert(buffer);
111   assert(mb);
112
113   blocks = mb->blocks;
114
115   for (y = 0; y < 2; ++y) {
116      for (x = 0; x < 2; ++x, ++tb) {
117         if (mb->cbp & (*ctx->empty_block_mask)[0][y][x]) {
118            upload_block(buffer, 0, mb->mbx * 2 + x, mb->mby * 2 + y, blocks);
119            blocks += BLOCK_WIDTH * BLOCK_HEIGHT;
120         }
121      }
122   }
123
124   /* TODO: Implement 422, 444 */
125   assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
126
127   for (tb = 1; tb < 3; ++tb) {
128      if (mb->cbp & (*ctx->empty_block_mask)[tb][0][0]) {
129         upload_block(buffer, tb, mb->mbx, mb->mby, blocks);
130         blocks += BLOCK_WIDTH * BLOCK_HEIGHT;
131      }
132   }
133}
134
135static void
136unmap_buffers(struct vl_mpeg12_decoder *ctx, struct vl_mpeg12_buffer *buffer)
137{
138   unsigned i;
139
140   assert(ctx && buffer);
141
142   for (i = 0; i < VL_MAX_PLANES; ++i) {
143      ctx->pipe->transfer_unmap(ctx->pipe, buffer->tex_transfer[i]);
144      ctx->pipe->transfer_destroy(ctx->pipe, buffer->tex_transfer[i]);
145   }
146}
147
148static void
149vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer)
150{
151   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
152   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)buf->base.decoder;
153   assert(buf && dec);
154
155   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
156      buf->idct_source->destroy(buf->idct_source);
157      vl_idct_cleanup_buffer(&dec->idct_y, &buf->idct[0]);
158      vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[1]);
159      vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[2]);
160   }
161   buf->mc_source->destroy(buf->mc_source);
162   vl_vb_cleanup(&buf->vertex_stream);
163   vl_mpeg12_mc_cleanup_buffer(&buf->mc[0]);
164   vl_mpeg12_mc_cleanup_buffer(&buf->mc[1]);
165   vl_mpeg12_mc_cleanup_buffer(&buf->mc[2]);
166
167   FREE(buf);
168}
169
170static void
171vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer)
172{
173   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
174   struct vl_mpeg12_decoder *dec;
175   assert(buf);
176
177   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
178   assert(dec);
179
180   vl_vb_map(&buf->vertex_stream, dec->pipe);
181   map_buffers(dec, buf);
182}
183
184static void
185vl_mpeg12_buffer_add_macroblocks(struct pipe_video_decode_buffer *buffer,
186                                 unsigned num_macroblocks,
187                                 struct pipe_macroblock *macroblocks)
188{
189   struct pipe_mpeg12_macroblock *mb = (struct pipe_mpeg12_macroblock*)macroblocks;
190   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
191   struct vl_mpeg12_decoder *dec;
192   unsigned i;
193
194   assert(buf);
195
196   dec =  (struct vl_mpeg12_decoder*)buf->base.decoder;
197   assert(dec);
198
199   assert(num_macroblocks);
200   assert(macroblocks);
201   assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
202
203   for ( i = 0; i < num_macroblocks; ++i ) {
204      vl_vb_add_block(&buf->vertex_stream, &mb[i], dec->empty_block_mask);
205      upload_buffer(dec, buf, &mb[i]);
206   }
207}
208
209static void
210vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer)
211{
212   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer;
213   struct vl_mpeg12_decoder *dec;
214   assert(buf);
215
216   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
217   assert(dec);
218
219   vl_vb_unmap(&buf->vertex_stream, dec->pipe);
220   unmap_buffers(dec, buf);
221}
222
223static void
224vl_mpeg12_destroy(struct pipe_video_decoder *decoder)
225{
226   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
227
228   assert(decoder);
229
230   /* Asserted in softpipe_delete_fs_state() for some reason */
231   dec->pipe->bind_vs_state(dec->pipe, NULL);
232   dec->pipe->bind_fs_state(dec->pipe, NULL);
233
234   dec->pipe->delete_blend_state(dec->pipe, dec->blend);
235   dec->pipe->delete_rasterizer_state(dec->pipe, dec->rast);
236   dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa);
237
238   vl_mpeg12_mc_renderer_cleanup(&dec->mc);
239   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
240      vl_idct_cleanup(&dec->idct_y);
241      vl_idct_cleanup(&dec->idct_c);
242   }
243   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves[0]);
244   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves[1]);
245   dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves[2]);
246   pipe_resource_reference(&dec->quads.buffer, NULL);
247
248   FREE(dec);
249}
250
251static struct pipe_video_decode_buffer *
252vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder)
253{
254   const enum pipe_format idct_source_formats[3] = {
255      PIPE_FORMAT_R16G16B16A16_SNORM,
256      PIPE_FORMAT_R16G16B16A16_SNORM,
257      PIPE_FORMAT_R16G16B16A16_SNORM
258   };
259
260   const enum pipe_format mc_source_formats[3] = {
261      PIPE_FORMAT_R16_SNORM,
262      PIPE_FORMAT_R16_SNORM,
263      PIPE_FORMAT_R16_SNORM
264   };
265
266   struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder;
267   struct vl_mpeg12_buffer *buffer;
268
269   struct pipe_sampler_view **idct_views, **mc_views;
270   struct pipe_surface **idct_surfaces;
271
272   assert(dec);
273
274   buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
275   if (buffer == NULL)
276      return NULL;
277
278   buffer->base.decoder = decoder;
279   buffer->base.destroy = vl_mpeg12_buffer_destroy;
280   buffer->base.map = vl_mpeg12_buffer_map;
281   buffer->base.add_macroblocks = vl_mpeg12_buffer_add_macroblocks;
282   buffer->base.unmap = vl_mpeg12_buffer_unmap;
283
284   buffer->vertex_bufs.individual.quad.stride = dec->quads.stride;
285   buffer->vertex_bufs.individual.quad.buffer_offset = dec->quads.buffer_offset;
286   pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, dec->quads.buffer);
287
288   buffer->vertex_bufs.individual.stream = vl_vb_init(&buffer->vertex_stream, dec->pipe,
289                                                      dec->base.width / MACROBLOCK_WIDTH *
290                                                      dec->base.height / MACROBLOCK_HEIGHT);
291   if (!buffer->vertex_bufs.individual.stream.buffer)
292      goto error_vertex_stream;
293
294   buffer->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe,
295                                            dec->base.width, dec->base.height, 1,
296                                            dec->base.chroma_format, 3,
297                                            mc_source_formats,
298                                            PIPE_USAGE_STATIC);
299
300   if (!buffer->mc_source)
301      goto error_mc_source;
302
303   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
304      buffer->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe,
305                                                 dec->base.width / 4, dec->base.height, 1,
306                                                 dec->base.chroma_format, 3,
307                                                 idct_source_formats,
308                                                 PIPE_USAGE_STREAM);
309      if (!buffer->idct_source)
310         goto error_idct_source;
311
312
313      idct_views = buffer->idct_source->get_sampler_views(buffer->idct_source);
314      if (!idct_views)
315         goto error_idct_views;
316
317      idct_surfaces = buffer->mc_source->get_surfaces(buffer->mc_source);
318      if (!idct_surfaces)
319         goto error_idct_surfaces;
320
321      if (!vl_idct_init_buffer(&dec->idct_y, &buffer->idct[0],
322                               idct_views[0], idct_surfaces[0]))
323         goto error_idct_y;
324
325      if (!vl_idct_init_buffer(&dec->idct_c, &buffer->idct[1],
326                               idct_views[1], idct_surfaces[1]))
327         goto error_idct_cb;
328
329      if (!vl_idct_init_buffer(&dec->idct_c, &buffer->idct[2],
330                               idct_views[2], idct_surfaces[2]))
331         goto error_idct_cr;
332   }
333
334   mc_views = buffer->mc_source->get_sampler_views(buffer->mc_source);
335   if (!mc_views)
336      goto error_mc_views;
337
338   if(!vl_mpeg12_mc_init_buffer(&dec->mc, &buffer->mc[0], mc_views[0]))
339      goto error_mc_y;
340
341   if(!vl_mpeg12_mc_init_buffer(&dec->mc, &buffer->mc[1], mc_views[1]))
342      goto error_mc_cb;
343
344   if(!vl_mpeg12_mc_init_buffer(&dec->mc, &buffer->mc[2], mc_views[2]))
345      goto error_mc_cr;
346
347   return &buffer->base;
348
349error_mc_cr:
350   vl_mpeg12_mc_cleanup_buffer(&buffer->mc[1]);
351
352error_mc_cb:
353   vl_mpeg12_mc_cleanup_buffer(&buffer->mc[0]);
354
355error_mc_y:
356error_mc_views:
357   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
358      vl_idct_cleanup_buffer(&dec->idct_c, &buffer->idct[2]);
359
360error_idct_cr:
361   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
362      vl_idct_cleanup_buffer(&dec->idct_c, &buffer->idct[1]);
363
364error_idct_cb:
365   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
366      vl_idct_cleanup_buffer(&dec->idct_y, &buffer->idct[0]);
367
368error_idct_y:
369error_idct_surfaces:
370error_idct_views:
371   if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
372      buffer->idct_source->destroy(buffer->idct_source);
373
374error_idct_source:
375   buffer->mc_source->destroy(buffer->mc_source);
376
377error_mc_source:
378   vl_vb_cleanup(&buffer->vertex_stream);
379
380error_vertex_stream:
381   FREE(buffer);
382   return NULL;
383}
384
385static void
386vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer,
387                               struct pipe_video_buffer *refs[2],
388                               struct pipe_video_buffer *dst,
389                               struct pipe_fence_handle **fence)
390{
391   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
392   struct vl_mpeg12_decoder *dec;
393
394   struct pipe_sampler_view **sv_past;
395   struct pipe_sampler_view **sv_future;
396   struct pipe_surface **surfaces;
397
398   struct pipe_sampler_view *sv_refs[2];
399   unsigned ne_start, ne_num, e_start, e_num;
400   unsigned i;
401
402   assert(buf);
403
404   dec = (struct vl_mpeg12_decoder *)buf->base.decoder;
405   assert(dec);
406
407   sv_past = refs[0] ? refs[0]->get_sampler_views(refs[0]) : NULL;
408   sv_future = refs[1] ? refs[1]->get_sampler_views(refs[1]) : NULL;
409
410   surfaces = dst->get_surfaces(dst);
411
412   vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num);
413
414   dec->pipe->set_vertex_buffers(dec->pipe, 2, buf->vertex_bufs.all);
415   dec->pipe->bind_blend_state(dec->pipe, dec->blend);
416
417   for (i = 0; i < VL_MAX_PLANES; ++i) {
418      dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves[i]);
419
420      if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
421         vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], ne_num);
422
423      sv_refs[0] = sv_past ? sv_past[i] : NULL;
424      sv_refs[1] = sv_future ? sv_future[i] : NULL;
425
426      vl_mpeg12_mc_renderer_flush(&dec->mc, &buf->mc[i], surfaces[i], sv_refs,
427                                  ne_start, ne_num, e_start, e_num, fence);
428   }
429}
430
431static void
432vl_mpeg12_decoder_clear_buffer(struct pipe_video_decode_buffer *buffer)
433{
434   struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer;
435   unsigned ne_start, ne_num, e_start, e_num;
436
437   assert(buf);
438
439   vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num);
440}
441
442static bool
443init_pipe_state(struct vl_mpeg12_decoder *dec)
444{
445   struct pipe_rasterizer_state rast;
446   struct pipe_blend_state blend;
447   struct pipe_depth_stencil_alpha_state dsa;
448   unsigned i;
449
450   assert(dec);
451
452   memset(&rast, 0, sizeof rast);
453   rast.flatshade = 1;
454   rast.flatshade_first = 0;
455   rast.light_twoside = 0;
456   rast.front_ccw = 1;
457   rast.cull_face = PIPE_FACE_NONE;
458   rast.fill_back = PIPE_POLYGON_MODE_FILL;
459   rast.fill_front = PIPE_POLYGON_MODE_FILL;
460   rast.offset_point = 0;
461   rast.offset_line = 0;
462   rast.scissor = 0;
463   rast.poly_smooth = 0;
464   rast.poly_stipple_enable = 0;
465   rast.sprite_coord_enable = 0;
466   rast.point_size_per_vertex = 0;
467   rast.multisample = 0;
468   rast.line_smooth = 0;
469   rast.line_stipple_enable = 0;
470   rast.line_stipple_factor = 0;
471   rast.line_stipple_pattern = 0;
472   rast.line_last_pixel = 0;
473   rast.line_width = 1;
474   rast.point_smooth = 0;
475   rast.point_quad_rasterization = 0;
476   rast.point_size_per_vertex = 1;
477   rast.offset_units = 1;
478   rast.offset_scale = 1;
479   rast.gl_rasterization_rules = 1;
480
481   dec->rast = dec->pipe->create_rasterizer_state(dec->pipe, &rast);
482   dec->pipe->bind_rasterizer_state(dec->pipe, dec->rast);
483
484   memset(&blend, 0, sizeof blend);
485
486   blend.independent_blend_enable = 0;
487   blend.rt[0].blend_enable = 0;
488   blend.rt[0].rgb_func = PIPE_BLEND_ADD;
489   blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
490   blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
491   blend.rt[0].alpha_func = PIPE_BLEND_ADD;
492   blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
493   blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
494   blend.logicop_enable = 0;
495   blend.logicop_func = PIPE_LOGICOP_CLEAR;
496   /* Needed to allow color writes to FB, even if blending disabled */
497   blend.rt[0].colormask = PIPE_MASK_RGBA;
498   blend.dither = 0;
499   dec->blend = dec->pipe->create_blend_state(dec->pipe, &blend);
500
501   memset(&dsa, 0, sizeof dsa);
502   dsa.depth.enabled = 0;
503   dsa.depth.writemask = 0;
504   dsa.depth.func = PIPE_FUNC_ALWAYS;
505   for (i = 0; i < 2; ++i) {
506      dsa.stencil[i].enabled = 0;
507      dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
508      dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
509      dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
510      dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
511      dsa.stencil[i].valuemask = 0;
512      dsa.stencil[i].writemask = 0;
513   }
514   dsa.alpha.enabled = 0;
515   dsa.alpha.func = PIPE_FUNC_ALWAYS;
516   dsa.alpha.ref_value = 0;
517   dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa);
518   dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa);
519
520   return true;
521}
522
523static bool
524init_idct(struct vl_mpeg12_decoder *dec, unsigned buffer_width, unsigned buffer_height)
525{
526   unsigned chroma_width, chroma_height, chroma_blocks_x, chroma_blocks_y;
527   struct pipe_sampler_view *idct_matrix;
528
529   if (!(idct_matrix = vl_idct_upload_matrix(dec->pipe, sqrt(SCALE_FACTOR_16_TO_9))))
530      goto error_idct_matrix;
531
532   if (!vl_idct_init(&dec->idct_y, dec->pipe, buffer_width, buffer_height,
533                     2, 2, idct_matrix))
534      goto error_idct_y;
535
536   if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
537      chroma_width = buffer_width / 2;
538      chroma_height = buffer_height / 2;
539      chroma_blocks_x = 1;
540      chroma_blocks_y = 1;
541   } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
542      chroma_width = buffer_width;
543      chroma_height = buffer_height / 2;
544      chroma_blocks_x = 2;
545      chroma_blocks_y = 1;
546   } else {
547      chroma_width = buffer_width;
548      chroma_height = buffer_height;
549      chroma_blocks_x = 2;
550      chroma_blocks_y = 2;
551   }
552
553   if(!vl_idct_init(&dec->idct_c, dec->pipe, chroma_width, chroma_height,
554                    chroma_blocks_x, chroma_blocks_y, idct_matrix))
555      goto error_idct_c;
556
557   pipe_sampler_view_reference(&idct_matrix, NULL);
558   return true;
559
560error_idct_c:
561   vl_idct_cleanup(&dec->idct_y);
562
563error_idct_y:
564   pipe_sampler_view_reference(&idct_matrix, NULL);
565
566error_idct_matrix:
567   return false;
568}
569
570struct pipe_video_decoder *
571vl_create_mpeg12_decoder(struct pipe_video_context *context,
572                         struct pipe_context *pipe,
573                         enum pipe_video_profile profile,
574                         enum pipe_video_entrypoint entrypoint,
575                         enum pipe_video_chroma_format chroma_format,
576                         unsigned width, unsigned height)
577{
578   struct vl_mpeg12_decoder *dec;
579   unsigned i;
580
581   assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
582
583   dec = CALLOC_STRUCT(vl_mpeg12_decoder);
584
585   if (!dec)
586      return NULL;
587
588   dec->base.context = context;
589   dec->base.profile = profile;
590   dec->base.entrypoint = entrypoint;
591   dec->base.chroma_format = chroma_format;
592   dec->base.width = width;
593   dec->base.height = height;
594
595   dec->base.destroy = vl_mpeg12_destroy;
596   dec->base.create_buffer = vl_mpeg12_create_buffer;
597   dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer;
598   dec->base.clear_buffer = vl_mpeg12_decoder_clear_buffer;
599
600   dec->pipe = pipe;
601
602   dec->quads = vl_vb_upload_quads(dec->pipe, 2, 2);
603   for (i = 0; i < VL_MAX_PLANES; ++i)
604      dec->ves[i] = vl_vb_get_elems_state(dec->pipe, i);
605
606   dec->base.width = align(width, MACROBLOCK_WIDTH);
607   dec->base.height = align(height, MACROBLOCK_HEIGHT);
608
609   /* TODO: Implement 422, 444 */
610   assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
611   dec->empty_block_mask = &const_empty_block_mask_420;
612
613   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT)
614      if (!init_idct(dec, dec->base.width, dec->base.height))
615         goto error_idct;
616
617   if (!vl_mpeg12_mc_renderer_init(&dec->mc, dec->pipe, dec->base.width, dec->base.height,
618                                   entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 1.0f : SCALE_FACTOR_16_TO_9))
619      goto error_mc;
620
621   if (!init_pipe_state(dec))
622      goto error_pipe_state;
623
624   return &dec->base;
625
626error_pipe_state:
627   vl_mpeg12_mc_renderer_cleanup(&dec->mc);
628
629error_mc:
630   if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) {
631      vl_idct_cleanup(&dec->idct_y);
632      vl_idct_cleanup(&dec->idct_c);
633   }
634
635error_idct:
636   FREE(dec);
637   return NULL;
638}
639