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