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