32398e4ff0800b11388323e7b2f45ff8e52fbbd0
[mesa.git] / src / gallium / drivers / softpipe / sp_video_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 "sp_video_context.h"
32 #include <pipe/p_shader_tokens.h>
33 #include <util/u_inlines.h>
34 #include <util/u_memory.h>
35 #include <util/u_keymap.h>
36 #include <util/u_rect.h>
37 #include <util/u_video.h>
38 #include <util/u_surface.h>
39 #include "sp_public.h"
40 #include "sp_texture.h"
41
42 #define MACROBLOCK_WIDTH 16
43 #define MACROBLOCK_HEIGHT 16
44 #define BLOCK_WIDTH 8
45 #define BLOCK_HEIGHT 8
46
47 #define NUM_BUFFERS 2
48
49 static void
50 flush_buffer(struct sp_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 sp_mpeg12_context *ctx)
78 {
79 struct pipe_resource *y, *cr, *cb;
80 static unsigned key = 0;
81 struct sp_mpeg12_buffer *buffer;
82
83 assert(ctx);
84
85 flush_buffer(ctx);
86
87 buffer = (struct sp_mpeg12_buffer*)util_keymap_lookup(ctx->buffer_map, &key);
88 if (!buffer) {
89 boolean added_to_map;
90
91 buffer = CALLOC_STRUCT(sp_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 sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)user;
140 struct sp_mpeg12_buffer *buf = (struct sp_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 sp_mpeg12_context *ctx,
156 struct sp_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->mc_renderer.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->mc_renderer.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 sp_mpeg12_destroy(struct pipe_video_context *vpipe)
193 {
194 struct sp_mpeg12_context *ctx = (struct sp_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 sp_mpeg12_get_param(struct pipe_video_context *vpipe, int param)
224 {
225 struct sp_mpeg12_context *ctx = (struct sp_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("Softpipe: Unknown PIPE_CAP %d\n", param);
241 return 0;
242 }
243 }
244 }
245
246 static struct pipe_surface *
247 sp_mpeg12_create_surface(struct pipe_video_context *vpipe,
248 struct pipe_resource *resource,
249 const struct pipe_surface *templat)
250 {
251 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
252
253 assert(vpipe);
254
255 return ctx->pipe->create_surface(ctx->pipe, resource, templat);
256 }
257
258 static boolean
259 sp_mpeg12_is_format_supported(struct pipe_video_context *vpipe,
260 enum pipe_format format,
261 unsigned usage,
262 unsigned geom)
263 {
264 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
265
266 assert(vpipe);
267
268 /* XXX: Temporary; not all paths are NPOT-tested */
269 if (geom & PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO)
270 return FALSE;
271
272
273 return ctx->pipe->screen->is_format_supported(ctx->pipe->screen, format, PIPE_TEXTURE_2D,
274 0, usage, geom);
275 }
276
277 static void
278 sp_mpeg12_decode_macroblocks(struct pipe_video_context *vpipe,
279 struct pipe_surface *past,
280 struct pipe_surface *future,
281 unsigned num_macroblocks,
282 struct pipe_macroblock *macroblocks,
283 struct pipe_fence_handle **fence)
284 {
285 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
286 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
287 unsigned i;
288
289 assert(vpipe);
290 assert(num_macroblocks);
291 assert(macroblocks);
292 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
293 assert(ctx->decode_target);
294 assert(ctx->cur_buffer);
295
296 for ( i = 0; i < num_macroblocks; ++i ) {
297 vl_vb_add_block(&ctx->cur_buffer->vertex_stream, &mpeg12_macroblocks[i],
298 ctx->mc_renderer.empty_block_mask);
299 upload_buffer(ctx, ctx->cur_buffer, &mpeg12_macroblocks[i]);
300 }
301
302 vl_mpeg12_mc_set_surfaces(&ctx->mc_renderer, &ctx->cur_buffer->mc,
303 ctx->decode_target, past, future, fence);
304 }
305
306 static void
307 sp_mpeg12_clear_render_target(struct pipe_video_context *vpipe,
308 struct pipe_surface *dst,
309 unsigned dstx, unsigned dsty,
310 unsigned width, unsigned height)
311 {
312 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
313 float rgba[4] = { 0, 0, 0, 0 };
314
315 assert(vpipe);
316 assert(dst);
317
318 if (ctx->pipe->clear_render_target)
319 ctx->pipe->clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
320 else
321 util_clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
322 }
323
324 static void
325 sp_mpeg12_resource_copy_region(struct pipe_video_context *vpipe,
326 struct pipe_surface *dst,
327 unsigned dstx, unsigned dsty,
328 struct pipe_surface *src,
329 unsigned srcx, unsigned srcy,
330 unsigned width, unsigned height)
331 {
332 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
333
334 assert(vpipe);
335 assert(dst);
336
337 struct pipe_box box;
338 box.x = srcx;
339 box.y = srcy;
340 box.z = 0;
341 box.width = width;
342 box.height = height;
343
344 if (ctx->pipe->resource_copy_region)
345 ctx->pipe->resource_copy_region(ctx->pipe, dst->texture, dst->u.tex.level,
346 dstx, dsty, dst->u.tex.first_layer,
347 src->texture, src->u.tex.level, &box);
348 else
349 util_resource_copy_region(ctx->pipe, dst->texture, dst->u.tex.level,
350 dstx, dsty, dst->u.tex.first_layer,
351 src->texture, src->u.tex.level, &box);
352 }
353
354 static struct pipe_transfer*
355 sp_mpeg12_get_transfer(struct pipe_video_context *vpipe,
356 struct pipe_resource *resource,
357 unsigned level,
358 unsigned usage, /* a combination of PIPE_TRANSFER_x */
359 const struct pipe_box *box)
360 {
361 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
362
363 assert(vpipe);
364 assert(resource);
365 assert(box);
366
367 return ctx->pipe->get_transfer(ctx->pipe, resource, level, usage, box);
368 }
369
370 static void
371 sp_mpeg12_transfer_destroy(struct pipe_video_context *vpipe,
372 struct pipe_transfer *transfer)
373 {
374 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
375
376 assert(vpipe);
377 assert(transfer);
378
379 ctx->pipe->transfer_destroy(ctx->pipe, transfer);
380 }
381
382 static void*
383 sp_mpeg12_transfer_map(struct pipe_video_context *vpipe,
384 struct pipe_transfer *transfer)
385 {
386 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
387
388 assert(vpipe);
389 assert(transfer);
390
391 return ctx->pipe->transfer_map(ctx->pipe, transfer);
392 }
393
394 static void
395 sp_mpeg12_transfer_flush_region(struct pipe_video_context *vpipe,
396 struct pipe_transfer *transfer,
397 const struct pipe_box *box)
398 {
399 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
400
401 assert(vpipe);
402 assert(transfer);
403 assert(box);
404
405 ctx->pipe->transfer_flush_region(ctx->pipe, transfer, box);
406 }
407
408 static void
409 sp_mpeg12_transfer_unmap(struct pipe_video_context *vpipe,
410 struct pipe_transfer *transfer)
411 {
412 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
413
414 assert(vpipe);
415 assert(transfer);
416
417 ctx->pipe->transfer_unmap(ctx->pipe, transfer);
418 }
419
420 static void
421 sp_mpeg12_transfer_inline_write(struct pipe_video_context *vpipe,
422 struct pipe_resource *resource,
423 unsigned level,
424 unsigned usage, /* a combination of PIPE_TRANSFER_x */
425 const struct pipe_box *box,
426 const void *data,
427 unsigned stride,
428 unsigned slice_stride)
429 {
430 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
431
432 assert(vpipe);
433 assert(resource);
434 assert(box);
435 assert(data);
436 assert(ctx->pipe->transfer_inline_write);
437
438 ctx->pipe->transfer_inline_write(ctx->pipe, resource, level, usage,
439 box, data, stride, slice_stride);
440 }
441
442 static void
443 sp_mpeg12_render_picture(struct pipe_video_context *vpipe,
444 struct pipe_surface *src_surface,
445 enum pipe_mpeg12_picture_type picture_type,
446 /*unsigned num_past_surfaces,
447 struct pipe_surface *past_surfaces,
448 unsigned num_future_surfaces,
449 struct pipe_surface *future_surfaces,*/
450 struct pipe_video_rect *src_area,
451 struct pipe_surface *dst_surface,
452 struct pipe_video_rect *dst_area,
453 struct pipe_fence_handle **fence)
454 {
455 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
456
457 assert(vpipe);
458 assert(src_surface);
459 assert(src_area);
460 assert(dst_surface);
461 assert(dst_area);
462
463 flush_buffer(ctx);
464
465 vl_compositor_render(&ctx->compositor, src_surface,
466 picture_type, src_area, dst_surface, dst_area, fence);
467 }
468
469 static void
470 sp_mpeg12_set_picture_background(struct pipe_video_context *vpipe,
471 struct pipe_surface *bg,
472 struct pipe_video_rect *bg_src_rect)
473 {
474 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
475
476 assert(vpipe);
477 assert(bg);
478 assert(bg_src_rect);
479
480 vl_compositor_set_background(&ctx->compositor, bg, bg_src_rect);
481 }
482
483 static void
484 sp_mpeg12_set_picture_layers(struct pipe_video_context *vpipe,
485 struct pipe_surface *layers[],
486 struct pipe_video_rect *src_rects[],
487 struct pipe_video_rect *dst_rects[],
488 unsigned num_layers)
489 {
490 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
491
492 assert(vpipe);
493 assert((layers && src_rects && dst_rects) ||
494 (!layers && !src_rects && !dst_rects));
495
496 vl_compositor_set_layers(&ctx->compositor, layers, src_rects, dst_rects, num_layers);
497 }
498
499 static void
500 sp_mpeg12_set_decode_target(struct pipe_video_context *vpipe,
501 struct pipe_surface *dt)
502 {
503 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
504
505 assert(vpipe);
506 assert(dt);
507
508 if (ctx->decode_target != dt || ctx->cur_buffer == NULL) {
509 rotate_buffer(ctx);
510
511 pipe_surface_reference(&ctx->decode_target, dt);
512 }
513 }
514
515 static void
516 sp_mpeg12_set_csc_matrix(struct pipe_video_context *vpipe, const float *mat)
517 {
518 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
519
520 assert(vpipe);
521
522 vl_compositor_set_csc_matrix(&ctx->compositor, mat);
523 }
524
525 static bool
526 init_pipe_state(struct sp_mpeg12_context *ctx)
527 {
528 struct pipe_rasterizer_state rast;
529 struct pipe_blend_state blend;
530 struct pipe_depth_stencil_alpha_state dsa;
531 unsigned i;
532
533 assert(ctx);
534
535 memset(&rast, 0, sizeof rast);
536 rast.flatshade = 1;
537 rast.flatshade_first = 0;
538 rast.light_twoside = 0;
539 rast.front_ccw = 1;
540 rast.cull_face = PIPE_FACE_NONE;
541 rast.fill_back = PIPE_POLYGON_MODE_FILL;
542 rast.fill_front = PIPE_POLYGON_MODE_FILL;
543 rast.offset_point = 0;
544 rast.offset_line = 0;
545 rast.scissor = 0;
546 rast.poly_smooth = 0;
547 rast.poly_stipple_enable = 0;
548 rast.sprite_coord_enable = 0;
549 rast.point_size_per_vertex = 0;
550 rast.multisample = 0;
551 rast.line_smooth = 0;
552 rast.line_stipple_enable = 0;
553 rast.line_stipple_factor = 0;
554 rast.line_stipple_pattern = 0;
555 rast.line_last_pixel = 0;
556 rast.line_width = 1;
557 rast.point_smooth = 0;
558 rast.point_quad_rasterization = 0;
559 rast.point_size_per_vertex = 1;
560 rast.offset_units = 1;
561 rast.offset_scale = 1;
562 rast.gl_rasterization_rules = 1;
563
564 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
565 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
566
567 memset(&blend, 0, sizeof blend);
568
569 blend.independent_blend_enable = 0;
570 blend.rt[0].blend_enable = 0;
571 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
572 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
573 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
574 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
575 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
576 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
577 blend.logicop_enable = 0;
578 blend.logicop_func = PIPE_LOGICOP_CLEAR;
579 /* Needed to allow color writes to FB, even if blending disabled */
580 blend.rt[0].colormask = PIPE_MASK_RGBA;
581 blend.dither = 0;
582 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
583 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
584
585 memset(&dsa, 0, sizeof dsa);
586 dsa.depth.enabled = 0;
587 dsa.depth.writemask = 0;
588 dsa.depth.func = PIPE_FUNC_ALWAYS;
589 for (i = 0; i < 2; ++i) {
590 dsa.stencil[i].enabled = 0;
591 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
592 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
593 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
594 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
595 dsa.stencil[i].valuemask = 0;
596 dsa.stencil[i].writemask = 0;
597 }
598 dsa.alpha.enabled = 0;
599 dsa.alpha.func = PIPE_FUNC_ALWAYS;
600 dsa.alpha.ref_value = 0;
601 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
602 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
603
604 return true;
605 }
606
607 static struct pipe_video_context *
608 sp_mpeg12_create(struct pipe_context *pipe, enum pipe_video_profile profile,
609 enum pipe_video_chroma_format chroma_format,
610 unsigned width, unsigned height,
611 bool pot_buffers,
612 enum pipe_format decode_format)
613 {
614 struct pipe_resource *idct_matrix;
615 unsigned buffer_width, buffer_height;
616 unsigned chroma_width, chroma_height, chroma_blocks_x, chroma_blocks_y;
617 struct sp_mpeg12_context *ctx;
618
619 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
620
621 ctx = CALLOC_STRUCT(sp_mpeg12_context);
622
623 if (!ctx)
624 return NULL;
625
626 /* TODO: Non-pot buffers untested, probably doesn't work without changes to texcoord generation, vert shader, etc */
627 assert(pot_buffers);
628
629 buffer_width = pot_buffers ? util_next_power_of_two(width) : width;
630 buffer_height = pot_buffers ? util_next_power_of_two(height) : height;
631
632 ctx->base.profile = profile;
633 ctx->base.chroma_format = chroma_format;
634 ctx->base.width = width;
635 ctx->base.height = height;
636
637 ctx->base.screen = pipe->screen;
638
639 ctx->base.destroy = sp_mpeg12_destroy;
640 ctx->base.get_param = sp_mpeg12_get_param;
641 ctx->base.is_format_supported = sp_mpeg12_is_format_supported;
642 ctx->base.create_surface = sp_mpeg12_create_surface;
643 ctx->base.decode_macroblocks = sp_mpeg12_decode_macroblocks;
644 ctx->base.render_picture = sp_mpeg12_render_picture;
645 ctx->base.clear_render_target = sp_mpeg12_clear_render_target;
646 ctx->base.resource_copy_region = sp_mpeg12_resource_copy_region;
647 ctx->base.get_transfer = sp_mpeg12_get_transfer;
648 ctx->base.transfer_destroy = sp_mpeg12_transfer_destroy;
649 ctx->base.transfer_map = sp_mpeg12_transfer_map;
650 ctx->base.transfer_flush_region = sp_mpeg12_transfer_flush_region;
651 ctx->base.transfer_unmap = sp_mpeg12_transfer_unmap;
652 if (pipe->transfer_inline_write)
653 ctx->base.transfer_inline_write = sp_mpeg12_transfer_inline_write;
654 ctx->base.set_picture_background = sp_mpeg12_set_picture_background;
655 ctx->base.set_picture_layers = sp_mpeg12_set_picture_layers;
656 ctx->base.set_decode_target = sp_mpeg12_set_decode_target;
657 ctx->base.set_csc_matrix = sp_mpeg12_set_csc_matrix;
658
659 ctx->pipe = pipe;
660 ctx->decode_format = decode_format;
661
662 ctx->quads = vl_vb_upload_quads(ctx->pipe, 2, 2);
663 ctx->vertex_buffer_size = width / MACROBLOCK_WIDTH * height / MACROBLOCK_HEIGHT;
664 ctx->vertex_elems_state = vl_vb_get_elems_state(ctx->pipe, true);
665
666 if (ctx->vertex_elems_state == NULL) {
667 ctx->pipe->destroy(ctx->pipe);
668 FREE(ctx);
669 return NULL;
670 }
671
672 if (!(idct_matrix = vl_idct_upload_matrix(ctx->pipe)))
673 return false;
674
675 if (!vl_idct_init(&ctx->idct_y, ctx->pipe, buffer_width, buffer_height,
676 2, 2, TGSI_SWIZZLE_X, idct_matrix))
677 return false;
678
679 if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
680 chroma_width = buffer_width / 2;
681 chroma_height = buffer_height / 2;
682 chroma_blocks_x = 1;
683 chroma_blocks_y = 1;
684 } else if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
685 chroma_width = buffer_width;
686 chroma_height = buffer_height / 2;
687 chroma_blocks_x = 2;
688 chroma_blocks_y = 1;
689 } else {
690 chroma_width = buffer_width;
691 chroma_height = buffer_height;
692 chroma_blocks_x = 2;
693 chroma_blocks_y = 2;
694 }
695
696 if(!vl_idct_init(&ctx->idct_cr, ctx->pipe, chroma_width, chroma_height,
697 chroma_blocks_x, chroma_blocks_y, TGSI_SWIZZLE_Y, idct_matrix))
698 return false;
699
700 if(!vl_idct_init(&ctx->idct_cb, ctx->pipe, chroma_width, chroma_height,
701 chroma_blocks_x, chroma_blocks_y, TGSI_SWIZZLE_Z, idct_matrix))
702 return false;
703
704 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
705 buffer_width, buffer_height, chroma_format)) {
706 ctx->pipe->destroy(ctx->pipe);
707 FREE(ctx);
708 return NULL;
709 }
710
711 ctx->buffer_map = util_new_keymap(sizeof(unsigned), -1, delete_buffer);
712 if (!ctx->buffer_map) {
713 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
714 ctx->pipe->destroy(ctx->pipe);
715 FREE(ctx);
716 return NULL;
717 }
718
719 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
720 util_delete_keymap(ctx->buffer_map, ctx);
721 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
722 ctx->pipe->destroy(ctx->pipe);
723 FREE(ctx);
724 return NULL;
725 }
726
727 if (!init_pipe_state(ctx)) {
728 vl_compositor_cleanup(&ctx->compositor);
729 util_delete_keymap(ctx->buffer_map, ctx);
730 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
731 ctx->pipe->destroy(ctx->pipe);
732 FREE(ctx);
733 return NULL;
734 }
735
736 return &ctx->base;
737 }
738
739 struct pipe_video_context *
740 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
741 enum pipe_video_chroma_format chroma_format,
742 unsigned width, unsigned height, void *priv)
743 {
744 struct pipe_context *pipe;
745
746 assert(screen);
747 assert(width && height);
748
749 pipe = screen->context_create(screen, NULL);
750 if (!pipe)
751 return NULL;
752
753 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture with softpipe */
754 return sp_video_create_ex(pipe, profile,
755 chroma_format,
756 width, height,
757 true,
758 PIPE_FORMAT_XYUV);
759 }
760
761 struct pipe_video_context *
762 sp_video_create_ex(struct pipe_context *pipe, enum pipe_video_profile profile,
763 enum pipe_video_chroma_format chroma_format,
764 unsigned width, unsigned height,
765 bool pot_buffers,
766 enum pipe_format decode_format)
767 {
768 assert(pipe);
769 assert(width && height);
770
771 switch (u_reduce_video_profile(profile)) {
772 case PIPE_VIDEO_CODEC_MPEG12:
773 return sp_mpeg12_create(pipe, profile,
774 chroma_format,
775 width, height,
776 pot_buffers,
777 decode_format);
778 default:
779 return NULL;
780 }
781 }