Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / vl / vl_mpeg12_context.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 "util/u_inlines.h"
29 #include "util/u_memory.h"
30
31 #include "vl_mpeg12_context.h"
32 #include "vl_defines.h"
33 #include <pipe/p_shader_tokens.h>
34 #include <util/u_inlines.h>
35 #include <util/u_memory.h>
36 #include <util/u_keymap.h>
37 #include <util/u_rect.h>
38 #include <util/u_video.h>
39 #include <util/u_surface.h>
40
41 #define NUM_BUFFERS 2
42
43 static const unsigned const_empty_block_mask_420[3][2][2] = {
44 { { 0x20, 0x10 }, { 0x08, 0x04 } },
45 { { 0x02, 0x02 }, { 0x02, 0x02 } },
46 { { 0x01, 0x01 }, { 0x01, 0x01 } }
47 };
48
49 static void
50 flush_buffer(struct vl_mpeg12_context *ctx)
51 {
52 unsigned ne_start, ne_num, e_start, e_num;
53 assert(ctx);
54
55 if(ctx->cur_buffer != NULL) {
56
57 vl_vb_unmap(&ctx->cur_buffer->vertex_stream, ctx->pipe);
58 vl_idct_unmap_buffers(&ctx->idct_y, &ctx->cur_buffer->idct_y);
59 vl_idct_unmap_buffers(&ctx->idct_cr, &ctx->cur_buffer->idct_cr);
60 vl_idct_unmap_buffers(&ctx->idct_cb, &ctx->cur_buffer->idct_cb);
61 vl_vb_restart(&ctx->cur_buffer->vertex_stream,
62 &ne_start, &ne_num, &e_start, &e_num);
63
64 ctx->pipe->set_vertex_buffers(ctx->pipe, 2, ctx->cur_buffer->vertex_bufs.all);
65 ctx->pipe->bind_vertex_elements_state(ctx->pipe, ctx->vertex_elems_state);
66 vl_idct_flush(&ctx->idct_y, &ctx->cur_buffer->idct_y, ne_num);
67 vl_idct_flush(&ctx->idct_cr, &ctx->cur_buffer->idct_cr, ne_num);
68 vl_idct_flush(&ctx->idct_cb, &ctx->cur_buffer->idct_cb, ne_num);
69 vl_mpeg12_mc_renderer_flush(&ctx->mc_renderer, &ctx->cur_buffer->mc,
70 ne_start, ne_num, e_start, e_num);
71
72 ctx->cur_buffer = NULL;
73 }
74 }
75
76 static void
77 rotate_buffer(struct vl_mpeg12_context *ctx)
78 {
79 struct pipe_resource *y, *cr, *cb;
80 static unsigned key = 0;
81 struct vl_mpeg12_buffer *buffer;
82
83 assert(ctx);
84
85 flush_buffer(ctx);
86
87 buffer = (struct vl_mpeg12_buffer*)util_keymap_lookup(ctx->buffer_map, &key);
88 if (!buffer) {
89 boolean added_to_map;
90
91 buffer = CALLOC_STRUCT(vl_mpeg12_buffer);
92 if (buffer == NULL)
93 return;
94
95 buffer->vertex_bufs.individual.quad.stride = ctx->quads.stride;
96 buffer->vertex_bufs.individual.quad.buffer_offset = ctx->quads.buffer_offset;
97 pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, ctx->quads.buffer);
98
99 buffer->vertex_bufs.individual.stream = vl_vb_init(&buffer->vertex_stream, ctx->pipe,
100 ctx->vertex_buffer_size);
101 if (!(y = vl_idct_init_buffer(&ctx->idct_y, &buffer->idct_y))) {
102 FREE(buffer);
103 return;
104 }
105
106 if (!(cr = vl_idct_init_buffer(&ctx->idct_cr, &buffer->idct_cr))) {
107 FREE(buffer);
108 return;
109 }
110
111 if (!(cb = vl_idct_init_buffer(&ctx->idct_cb, &buffer->idct_cb))) {
112 FREE(buffer);
113 return;
114 }
115
116 if(!vl_mpeg12_mc_init_buffer(&ctx->mc_renderer, &buffer->mc, y, cr, cb)) {
117 FREE(buffer);
118 return;
119 }
120
121 added_to_map = util_keymap_insert(ctx->buffer_map, &key, buffer, ctx);
122 assert(added_to_map);
123 }
124 ++key;
125 key %= NUM_BUFFERS;
126 ctx->cur_buffer = buffer;
127
128 vl_vb_map(&ctx->cur_buffer->vertex_stream, ctx->pipe);
129 vl_idct_map_buffers(&ctx->idct_y, &ctx->cur_buffer->idct_y);
130 vl_idct_map_buffers(&ctx->idct_cr, &ctx->cur_buffer->idct_cr);
131 vl_idct_map_buffers(&ctx->idct_cb, &ctx->cur_buffer->idct_cb);
132 }
133
134 static void
135 delete_buffer(const struct keymap *map,
136 const void *key, void *data,
137 void *user)
138 {
139 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)user;
140 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)data;
141
142 assert(map);
143 assert(key);
144 assert(data);
145 assert(user);
146
147 vl_vb_cleanup(&buf->vertex_stream);
148 vl_idct_cleanup_buffer(&ctx->idct_y, &buf->idct_y);
149 vl_idct_cleanup_buffer(&ctx->idct_cb, &buf->idct_cb);
150 vl_idct_cleanup_buffer(&ctx->idct_cr, &buf->idct_cr);
151 vl_mpeg12_mc_cleanup_buffer(&ctx->mc_renderer, &buf->mc);
152 }
153
154 static void
155 upload_buffer(struct vl_mpeg12_context *ctx,
156 struct vl_mpeg12_buffer *buffer,
157 struct pipe_mpeg12_macroblock *mb)
158 {
159 short *blocks;
160 unsigned tb, x, y;
161
162 assert(ctx);
163 assert(buffer);
164 assert(mb);
165
166 blocks = mb->blocks;
167
168 for (y = 0; y < 2; ++y) {
169 for (x = 0; x < 2; ++x, ++tb) {
170 if (mb->cbp & (*ctx->empty_block_mask)[0][y][x]) {
171 vl_idct_add_block(&buffer->idct_y, mb->mbx * 2 + x, mb->mby * 2 + y, blocks);
172 blocks += BLOCK_WIDTH * BLOCK_HEIGHT;
173 }
174 }
175 }
176
177 /* TODO: Implement 422, 444 */
178 assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
179
180 for (tb = 1; tb < 3; ++tb) {
181 if (mb->cbp & (*ctx->empty_block_mask)[tb][0][0]) {
182 if(tb == 1)
183 vl_idct_add_block(&buffer->idct_cb, mb->mbx, mb->mby, blocks);
184 else
185 vl_idct_add_block(&buffer->idct_cr, mb->mbx, mb->mby, blocks);
186 blocks += BLOCK_WIDTH * BLOCK_HEIGHT;
187 }
188 }
189 }
190
191 static void
192 vl_mpeg12_destroy(struct pipe_video_context *vpipe)
193 {
194 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
195
196 assert(vpipe);
197
198 flush_buffer(ctx);
199
200 /* Asserted in softpipe_delete_fs_state() for some reason */
201 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
202 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
203
204 ctx->pipe->delete_blend_state(ctx->pipe, ctx->blend);
205 ctx->pipe->delete_rasterizer_state(ctx->pipe, ctx->rast);
206 ctx->pipe->delete_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
207
208 pipe_surface_reference(&ctx->decode_target, NULL);
209 vl_compositor_cleanup(&ctx->compositor);
210 util_delete_keymap(ctx->buffer_map, ctx);
211 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
212 vl_idct_cleanup(&ctx->idct_y);
213 vl_idct_cleanup(&ctx->idct_cr);
214 vl_idct_cleanup(&ctx->idct_cb);
215 ctx->pipe->delete_vertex_elements_state(ctx->pipe, ctx->vertex_elems_state);
216 pipe_resource_reference(&ctx->quads.buffer, NULL);
217 ctx->pipe->destroy(ctx->pipe);
218
219 FREE(ctx);
220 }
221
222 static int
223 vl_mpeg12_get_param(struct pipe_video_context *vpipe, int param)
224 {
225 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
226
227 assert(vpipe);
228
229 switch (param) {
230 case PIPE_CAP_NPOT_TEXTURES:
231 /* XXX: Temporary; not all paths are NPOT-tested */
232 #if 0
233 return ctx->pipe->screen->get_param(ctx->pipe->screen, param);
234 #endif
235 return FALSE;
236 case PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT:
237 return ctx->decode_format;
238 default:
239 {
240 debug_printf("vl_mpeg12_context: Unknown PIPE_CAP %d\n", param);
241 return 0;
242 }
243 }
244 }
245
246 static struct pipe_surface *
247 vl_mpeg12_create_surface(struct pipe_video_context *vpipe,
248 struct pipe_resource *resource,
249 const struct pipe_surface *templat)
250 {
251 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
252
253 assert(vpipe);
254
255 return ctx->pipe->create_surface(ctx->pipe, resource, templat);
256 }
257
258 static boolean
259 vl_mpeg12_is_format_supported(struct pipe_video_context *vpipe,
260 enum pipe_format format,
261 unsigned usage,
262 unsigned geom)
263 {
264 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
265
266 assert(vpipe);
267
268 return ctx->pipe->screen->is_format_supported(ctx->pipe->screen, format,
269 PIPE_TEXTURE_2D,
270 0, usage);
271 }
272
273 static void
274 vl_mpeg12_decode_macroblocks(struct pipe_video_context *vpipe,
275 struct pipe_surface *past,
276 struct pipe_surface *future,
277 unsigned num_macroblocks,
278 struct pipe_macroblock *macroblocks,
279 struct pipe_fence_handle **fence)
280 {
281 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
282 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
283 unsigned i;
284
285 assert(vpipe);
286 assert(num_macroblocks);
287 assert(macroblocks);
288 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
289 assert(ctx->decode_target);
290 assert(ctx->cur_buffer);
291
292 for ( i = 0; i < num_macroblocks; ++i ) {
293 vl_vb_add_block(&ctx->cur_buffer->vertex_stream, &mpeg12_macroblocks[i],
294 ctx->empty_block_mask);
295 upload_buffer(ctx, ctx->cur_buffer, &mpeg12_macroblocks[i]);
296 }
297
298 vl_mpeg12_mc_set_surfaces(&ctx->mc_renderer, &ctx->cur_buffer->mc,
299 ctx->decode_target, past, future, fence);
300 }
301
302 static void
303 vl_mpeg12_clear_render_target(struct pipe_video_context *vpipe,
304 struct pipe_surface *dst,
305 unsigned dstx, unsigned dsty,
306 const float *rgba,
307 unsigned width, unsigned height)
308 {
309 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
310
311 assert(vpipe);
312 assert(dst);
313
314 if (ctx->pipe->clear_render_target)
315 ctx->pipe->clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
316 else
317 util_clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
318 }
319
320 static void
321 vl_mpeg12_resource_copy_region(struct pipe_video_context *vpipe,
322 struct pipe_resource *dst,
323 unsigned dstx, unsigned dsty, unsigned dstz,
324 struct pipe_resource *src,
325 unsigned srcx, unsigned srcy, unsigned srcz,
326 unsigned width, unsigned height)
327 {
328 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
329
330 assert(vpipe);
331 assert(dst);
332
333 struct pipe_box box;
334 box.x = srcx;
335 box.y = srcy;
336 box.z = srcz;
337 box.width = width;
338 box.height = height;
339
340 if (ctx->pipe->resource_copy_region)
341 ctx->pipe->resource_copy_region(ctx->pipe, dst, 0,
342 dstx, dsty, dstz,
343 src, 0, &box);
344 else
345 util_resource_copy_region(ctx->pipe, dst, 0,
346 dstx, dsty, dstz,
347 src, 0, &box);
348 }
349
350 static struct pipe_transfer*
351 vl_mpeg12_get_transfer(struct pipe_video_context *vpipe,
352 struct pipe_resource *resource,
353 unsigned level,
354 unsigned usage, /* a combination of PIPE_TRANSFER_x */
355 const struct pipe_box *box)
356 {
357 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
358
359 assert(vpipe);
360 assert(resource);
361 assert(box);
362
363 return ctx->pipe->get_transfer(ctx->pipe, resource, level, usage, box);
364 }
365
366 static void
367 vl_mpeg12_transfer_destroy(struct pipe_video_context *vpipe,
368 struct pipe_transfer *transfer)
369 {
370 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
371
372 assert(vpipe);
373 assert(transfer);
374
375 ctx->pipe->transfer_destroy(ctx->pipe, transfer);
376 }
377
378 static void*
379 vl_mpeg12_transfer_map(struct pipe_video_context *vpipe,
380 struct pipe_transfer *transfer)
381 {
382 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
383
384 assert(vpipe);
385 assert(transfer);
386
387 return ctx->pipe->transfer_map(ctx->pipe, transfer);
388 }
389
390 static void
391 vl_mpeg12_transfer_flush_region(struct pipe_video_context *vpipe,
392 struct pipe_transfer *transfer,
393 const struct pipe_box *box)
394 {
395 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
396
397 assert(vpipe);
398 assert(transfer);
399 assert(box);
400
401 ctx->pipe->transfer_flush_region(ctx->pipe, transfer, box);
402 }
403
404 static void
405 vl_mpeg12_transfer_unmap(struct pipe_video_context *vpipe,
406 struct pipe_transfer *transfer)
407 {
408 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
409
410 assert(vpipe);
411 assert(transfer);
412
413 ctx->pipe->transfer_unmap(ctx->pipe, transfer);
414 }
415
416 static void
417 vl_mpeg12_transfer_inline_write(struct pipe_video_context *vpipe,
418 struct pipe_resource *resource,
419 unsigned level,
420 unsigned usage, /* a combination of PIPE_TRANSFER_x */
421 const struct pipe_box *box,
422 const void *data,
423 unsigned stride,
424 unsigned slice_stride)
425 {
426 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
427
428 assert(vpipe);
429 assert(resource);
430 assert(box);
431 assert(data);
432 assert(ctx->pipe->transfer_inline_write);
433
434 ctx->pipe->transfer_inline_write(ctx->pipe, resource, level, usage,
435 box, data, stride, slice_stride);
436 }
437
438 static void
439 vl_mpeg12_render_picture(struct pipe_video_context *vpipe,
440 struct pipe_surface *src_surface,
441 enum pipe_mpeg12_picture_type picture_type,
442 /*unsigned num_past_surfaces,
443 struct pipe_surface *past_surfaces,
444 unsigned num_future_surfaces,
445 struct pipe_surface *future_surfaces,*/
446 struct pipe_video_rect *src_area,
447 struct pipe_surface *dst_surface,
448 struct pipe_video_rect *dst_area,
449 struct pipe_fence_handle **fence)
450 {
451 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
452
453 assert(vpipe);
454 assert(src_surface);
455 assert(src_area);
456 assert(dst_surface);
457 assert(dst_area);
458
459 flush_buffer(ctx);
460
461 vl_compositor_render(&ctx->compositor, src_surface,
462 picture_type, src_area, dst_surface, dst_area, fence);
463 }
464
465 static void
466 vl_mpeg12_set_picture_background(struct pipe_video_context *vpipe,
467 struct pipe_surface *bg,
468 struct pipe_video_rect *bg_src_rect)
469 {
470 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
471
472 assert(vpipe);
473 assert(bg);
474 assert(bg_src_rect);
475
476 vl_compositor_set_background(&ctx->compositor, bg, bg_src_rect);
477 }
478
479 static void
480 vl_mpeg12_set_picture_layers(struct pipe_video_context *vpipe,
481 struct pipe_surface *layers[],
482 struct pipe_video_rect *src_rects[],
483 struct pipe_video_rect *dst_rects[],
484 unsigned num_layers)
485 {
486 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
487
488 assert(vpipe);
489 assert((layers && src_rects && dst_rects) ||
490 (!layers && !src_rects && !dst_rects));
491
492 vl_compositor_set_layers(&ctx->compositor, layers, src_rects, dst_rects, num_layers);
493 }
494
495 static void
496 vl_mpeg12_set_decode_target(struct pipe_video_context *vpipe,
497 struct pipe_surface *dt)
498 {
499 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
500
501 assert(vpipe);
502 assert(dt);
503
504 if (ctx->decode_target != dt || ctx->cur_buffer == NULL) {
505 rotate_buffer(ctx);
506
507 pipe_surface_reference(&ctx->decode_target, dt);
508 }
509 }
510
511 static void
512 vl_mpeg12_set_csc_matrix(struct pipe_video_context *vpipe, const float *mat)
513 {
514 struct vl_mpeg12_context *ctx = (struct vl_mpeg12_context*)vpipe;
515
516 assert(vpipe);
517
518 vl_compositor_set_csc_matrix(&ctx->compositor, mat);
519 }
520
521 static bool
522 init_pipe_state(struct vl_mpeg12_context *ctx)
523 {
524 struct pipe_rasterizer_state rast;
525 struct pipe_blend_state blend;
526 struct pipe_depth_stencil_alpha_state dsa;
527 unsigned i;
528
529 assert(ctx);
530
531 memset(&rast, 0, sizeof rast);
532 rast.flatshade = 1;
533 rast.flatshade_first = 0;
534 rast.light_twoside = 0;
535 rast.front_ccw = 1;
536 rast.cull_face = PIPE_FACE_NONE;
537 rast.fill_back = PIPE_POLYGON_MODE_FILL;
538 rast.fill_front = PIPE_POLYGON_MODE_FILL;
539 rast.offset_point = 0;
540 rast.offset_line = 0;
541 rast.scissor = 0;
542 rast.poly_smooth = 0;
543 rast.poly_stipple_enable = 0;
544 rast.sprite_coord_enable = 0;
545 rast.point_size_per_vertex = 0;
546 rast.multisample = 0;
547 rast.line_smooth = 0;
548 rast.line_stipple_enable = 0;
549 rast.line_stipple_factor = 0;
550 rast.line_stipple_pattern = 0;
551 rast.line_last_pixel = 0;
552 rast.line_width = 1;
553 rast.point_smooth = 0;
554 rast.point_quad_rasterization = 0;
555 rast.point_size_per_vertex = 1;
556 rast.offset_units = 1;
557 rast.offset_scale = 1;
558 rast.gl_rasterization_rules = 1;
559
560 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
561 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
562
563 memset(&blend, 0, sizeof blend);
564
565 blend.independent_blend_enable = 0;
566 blend.rt[0].blend_enable = 0;
567 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
568 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
569 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
570 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
571 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
572 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
573 blend.logicop_enable = 0;
574 blend.logicop_func = PIPE_LOGICOP_CLEAR;
575 /* Needed to allow color writes to FB, even if blending disabled */
576 blend.rt[0].colormask = PIPE_MASK_RGBA;
577 blend.dither = 0;
578 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
579 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
580
581 memset(&dsa, 0, sizeof dsa);
582 dsa.depth.enabled = 0;
583 dsa.depth.writemask = 0;
584 dsa.depth.func = PIPE_FUNC_ALWAYS;
585 for (i = 0; i < 2; ++i) {
586 dsa.stencil[i].enabled = 0;
587 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
588 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
589 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
590 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
591 dsa.stencil[i].valuemask = 0;
592 dsa.stencil[i].writemask = 0;
593 }
594 dsa.alpha.enabled = 0;
595 dsa.alpha.func = PIPE_FUNC_ALWAYS;
596 dsa.alpha.ref_value = 0;
597 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
598 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
599
600 return true;
601 }
602
603 struct pipe_video_context *
604 vl_create_mpeg12_context(struct pipe_context *pipe,
605 enum pipe_video_profile profile,
606 enum pipe_video_chroma_format chroma_format,
607 unsigned width, unsigned height,
608 bool pot_buffers,
609 enum pipe_format decode_format)
610 {
611 struct pipe_resource *idct_matrix;
612 unsigned buffer_width, buffer_height;
613 unsigned chroma_width, chroma_height, chroma_blocks_x, chroma_blocks_y;
614 struct vl_mpeg12_context *ctx;
615
616 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
617
618 ctx = CALLOC_STRUCT(vl_mpeg12_context);
619
620 if (!ctx)
621 return NULL;
622
623 /* TODO: Non-pot buffers untested, probably doesn't work without changes to texcoord generation, vert shader, etc */
624 assert(pot_buffers);
625
626 buffer_width = pot_buffers ? util_next_power_of_two(width) : width;
627 buffer_height = pot_buffers ? util_next_power_of_two(height) : height;
628
629 ctx->base.profile = profile;
630 ctx->base.chroma_format = chroma_format;
631 ctx->base.width = width;
632 ctx->base.height = height;
633
634 ctx->base.screen = pipe->screen;
635
636 ctx->base.destroy = vl_mpeg12_destroy;
637 ctx->base.get_param = vl_mpeg12_get_param;
638 ctx->base.is_format_supported = vl_mpeg12_is_format_supported;
639 ctx->base.create_surface = vl_mpeg12_create_surface;
640 ctx->base.decode_macroblocks = vl_mpeg12_decode_macroblocks;
641 ctx->base.render_picture = vl_mpeg12_render_picture;
642 ctx->base.clear_render_target = vl_mpeg12_clear_render_target;
643 ctx->base.resource_copy_region = vl_mpeg12_resource_copy_region;
644 ctx->base.get_transfer = vl_mpeg12_get_transfer;
645 ctx->base.transfer_destroy = vl_mpeg12_transfer_destroy;
646 ctx->base.transfer_map = vl_mpeg12_transfer_map;
647 ctx->base.transfer_flush_region = vl_mpeg12_transfer_flush_region;
648 ctx->base.transfer_unmap = vl_mpeg12_transfer_unmap;
649 if (pipe->transfer_inline_write)
650 ctx->base.transfer_inline_write = vl_mpeg12_transfer_inline_write;
651 ctx->base.set_picture_background = vl_mpeg12_set_picture_background;
652 ctx->base.set_picture_layers = vl_mpeg12_set_picture_layers;
653 ctx->base.set_decode_target = vl_mpeg12_set_decode_target;
654 ctx->base.set_csc_matrix = vl_mpeg12_set_csc_matrix;
655
656 ctx->pipe = pipe;
657 ctx->decode_format = decode_format;
658
659 ctx->quads = vl_vb_upload_quads(ctx->pipe, 2, 2);
660 ctx->vertex_buffer_size = width / MACROBLOCK_WIDTH * height / MACROBLOCK_HEIGHT;
661 ctx->vertex_elems_state = vl_vb_get_elems_state(ctx->pipe, true);
662
663 if (ctx->vertex_elems_state == NULL) {
664 ctx->pipe->destroy(ctx->pipe);
665 FREE(ctx);
666 return NULL;
667 }
668
669 /* TODO: Implement 422, 444 */
670 assert(chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
671 ctx->empty_block_mask = &const_empty_block_mask_420;
672
673 if (!(idct_matrix = vl_idct_upload_matrix(ctx->pipe)))
674 return false;
675
676 if (!vl_idct_init(&ctx->idct_y, ctx->pipe, buffer_width, buffer_height,
677 2, 2, TGSI_SWIZZLE_X, idct_matrix))
678 return false;
679
680 if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
681 chroma_width = buffer_width / 2;
682 chroma_height = buffer_height / 2;
683 chroma_blocks_x = 1;
684 chroma_blocks_y = 1;
685 } else if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
686 chroma_width = buffer_width;
687 chroma_height = buffer_height / 2;
688 chroma_blocks_x = 2;
689 chroma_blocks_y = 1;
690 } else {
691 chroma_width = buffer_width;
692 chroma_height = buffer_height;
693 chroma_blocks_x = 2;
694 chroma_blocks_y = 2;
695 }
696
697 if(!vl_idct_init(&ctx->idct_cr, ctx->pipe, chroma_width, chroma_height,
698 chroma_blocks_x, chroma_blocks_y, TGSI_SWIZZLE_Z, idct_matrix))
699 return false;
700
701 if(!vl_idct_init(&ctx->idct_cb, ctx->pipe, chroma_width, chroma_height,
702 chroma_blocks_x, chroma_blocks_y, TGSI_SWIZZLE_Y, idct_matrix))
703 return false;
704
705 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
706 buffer_width, buffer_height, chroma_format)) {
707 ctx->pipe->destroy(ctx->pipe);
708 FREE(ctx);
709 return NULL;
710 }
711
712 ctx->buffer_map = util_new_keymap(sizeof(unsigned), -1, delete_buffer);
713 if (!ctx->buffer_map) {
714 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
715 ctx->pipe->destroy(ctx->pipe);
716 FREE(ctx);
717 return NULL;
718 }
719
720 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
721 util_delete_keymap(ctx->buffer_map, ctx);
722 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
723 ctx->pipe->destroy(ctx->pipe);
724 FREE(ctx);
725 return NULL;
726 }
727
728 if (!init_pipe_state(ctx)) {
729 vl_compositor_cleanup(&ctx->compositor);
730 util_delete_keymap(ctx->buffer_map, ctx);
731 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
732 ctx->pipe->destroy(ctx->pipe);
733 FREE(ctx);
734 return NULL;
735 }
736
737 return &ctx->base;
738 }