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