1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 //#include <pipe/p_shader_tokens.h>
30 //#include "util/u_inlines.h"
32 //#include <util/u_inlines.h>
33 //#include <util/u_memory.h>
34 //#include <util/u_keymap.h>
36 //#include <util/u_video.h>
37 //#include <util/u_surface.h>
38 //#include <util/u_sampler.h>
40 #include <util/u_memory.h>
41 #include <util/u_rect.h>
42 #include <util/u_video.h>
44 #include "vl_mpeg12_decoder.h"
45 #include "vl_defines.h"
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 } }
54 map_buffers(struct vl_mpeg12_decoder
*ctx
, struct vl_mpeg12_buffer
*buffer
)
56 struct pipe_sampler_view
**sampler_views
;
57 struct pipe_resource
*tex
;
60 assert(ctx
&& buffer
);
62 sampler_views
= buffer
->idct_source
->get_sampler_views(buffer
->idct_source
);
63 assert(sampler_views
);
65 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
66 tex
= sampler_views
[i
]->texture
;
68 struct pipe_box rect
=
76 buffer
->tex_transfer
[i
] = ctx
->pipe
->get_transfer
79 0, PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
83 buffer
->texels
[i
] = ctx
->pipe
->transfer_map(ctx
->pipe
, buffer
->tex_transfer
[i
]);
88 upload_block(struct vl_mpeg12_buffer
*buffer
, unsigned plane
, unsigned x
, unsigned y
, short *block
)
98 tex_pitch
= buffer
->tex_transfer
[plane
]->stride
/ sizeof(short);
99 texels
= buffer
->texels
[plane
] + y
* tex_pitch
* BLOCK_HEIGHT
+ x
* BLOCK_WIDTH
;
101 for (i
= 0; i
< BLOCK_HEIGHT
; ++i
)
102 memcpy(texels
+ i
* tex_pitch
, block
+ i
* BLOCK_WIDTH
, BLOCK_WIDTH
* sizeof(short));
106 upload_buffer(struct vl_mpeg12_decoder
*ctx
,
107 struct vl_mpeg12_buffer
*buffer
,
108 struct pipe_mpeg12_macroblock
*mb
)
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
;
128 /* TODO: Implement 422, 444 */
129 assert(ctx
->base
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
);
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
;
140 unmap_buffers(struct vl_mpeg12_decoder
*ctx
, struct vl_mpeg12_buffer
*buffer
)
144 assert(ctx
&& buffer
);
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
]);
153 vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer
*buffer
)
155 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
156 struct vl_mpeg12_decoder
*dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
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]);
173 vl_mpeg12_buffer_map(struct pipe_video_decode_buffer
*buffer
)
175 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
176 struct vl_mpeg12_decoder
*dec
;
179 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
182 vl_vb_map(&buf
->vertex_stream
, dec
->pipe
);
183 map_buffers(dec
, buf
);
187 vl_mpeg12_buffer_add_macroblocks(struct pipe_video_decode_buffer
*buffer
,
188 unsigned num_macroblocks
,
189 struct pipe_macroblock
*macroblocks
)
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
;
198 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
201 assert(num_macroblocks
);
203 assert(macroblocks
->codec
== PIPE_VIDEO_CODEC_MPEG12
);
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
]);
212 vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer
*buffer
)
214 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
215 struct vl_mpeg12_decoder
*dec
;
218 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
221 vl_vb_unmap(&buf
->vertex_stream
, dec
->pipe
);
222 unmap_buffers(dec
, buf
);
226 vl_mpeg12_destroy(struct pipe_video_decoder
*decoder
)
228 struct vl_mpeg12_decoder
*dec
= (struct vl_mpeg12_decoder
*)decoder
;
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
);
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
);
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
);
251 static struct pipe_video_decode_buffer
*
252 vl_mpeg12_create_buffer(struct pipe_video_decoder
*decoder
)
254 const enum pipe_format idct_source_formats
[3] = {
255 PIPE_FORMAT_R16G16B16A16_SNORM
,
256 PIPE_FORMAT_R16G16B16A16_SNORM
,
257 PIPE_FORMAT_R16G16B16A16_SNORM
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
266 struct vl_mpeg12_decoder
*dec
= (struct vl_mpeg12_decoder
*)decoder
;
267 struct vl_mpeg12_buffer
*buffer
;
269 struct pipe_sampler_view
**idct_views
, **mc_views
;
270 struct pipe_surface
**idct_surfaces
;
274 buffer
= CALLOC_STRUCT(vl_mpeg12_buffer
);
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
;
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
);
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
;
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,
299 if (!buffer
->idct_source
)
300 goto error_idct_source
;
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,
307 if (!buffer
->idct_2_mc
)
308 goto error_idct_2_mc
;
310 idct_views
= buffer
->idct_source
->get_sampler_views(buffer
->idct_source
);
312 goto error_idct_views
;
314 idct_surfaces
= buffer
->idct_2_mc
->get_surfaces(buffer
->idct_2_mc
);
316 goto error_idct_surfaces
;
318 if (!vl_idct_init_buffer(&dec
->idct_y
, &buffer
->idct
[0],
319 idct_views
[0], idct_surfaces
[0]))
322 if (!vl_idct_init_buffer(&dec
->idct_c
, &buffer
->idct
[1],
323 idct_views
[1], idct_surfaces
[1]))
326 if (!vl_idct_init_buffer(&dec
->idct_c
, &buffer
->idct
[2],
327 idct_views
[2], idct_surfaces
[2]))
330 mc_views
= buffer
->idct_2_mc
->get_sampler_views(buffer
->idct_2_mc
);
334 if(!vl_mpeg12_mc_init_buffer(&dec
->mc
, &buffer
->mc
[0], mc_views
[0]))
337 if(!vl_mpeg12_mc_init_buffer(&dec
->mc
, &buffer
->mc
[1], mc_views
[1]))
340 if(!vl_mpeg12_mc_init_buffer(&dec
->mc
, &buffer
->mc
[2], mc_views
[2]))
343 return &buffer
->base
;
346 vl_mpeg12_mc_cleanup_buffer(&buffer
->mc
[1]);
349 vl_mpeg12_mc_cleanup_buffer(&buffer
->mc
[0]);
353 vl_idct_cleanup_buffer(&dec
->idct_c
, &buffer
->idct
[2]);
356 vl_idct_cleanup_buffer(&dec
->idct_c
, &buffer
->idct
[1]);
359 vl_idct_cleanup_buffer(&dec
->idct_y
, &buffer
->idct
[0]);
364 buffer
->idct_2_mc
->destroy(buffer
->idct_2_mc
);
367 buffer
->idct_source
->destroy(buffer
->idct_source
);
370 vl_vb_cleanup(&buffer
->vertex_stream
);
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
)
383 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
384 struct vl_mpeg12_decoder
*dec
;
386 struct pipe_sampler_view
**sv_past
;
387 struct pipe_sampler_view
**sv_future
;
388 struct pipe_surface
**surfaces
;
390 struct pipe_sampler_view
*sv_refs
[2];
391 unsigned ne_start
, ne_num
, e_start
, e_num
;
396 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
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
;
402 surfaces
= dst
->get_surfaces(dst
);
404 vl_vb_restart(&buf
->vertex_stream
, &ne_start
, &ne_num
, &e_start
, &e_num
);
406 dec
->pipe
->set_vertex_buffers(dec
->pipe
, 2, buf
->vertex_bufs
.all
);
407 dec
->pipe
->bind_blend_state(dec
->pipe
, dec
->blend
);
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
);
413 sv_refs
[0] = sv_past
? sv_past
[i
] : NULL
;
414 sv_refs
[1] = sv_future
? sv_future
[i
] : NULL
;
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
);
422 vl_mpeg12_decoder_clear_buffer(struct pipe_video_decode_buffer
*buffer
)
424 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
425 unsigned ne_start
, ne_num
, e_start
, e_num
;
429 vl_vb_restart(&buf
->vertex_stream
, &ne_start
, &ne_num
, &e_start
, &e_num
);
433 init_pipe_state(struct vl_mpeg12_decoder
*dec
)
435 struct pipe_rasterizer_state rast
;
436 struct pipe_blend_state blend
;
437 struct pipe_depth_stencil_alpha_state dsa
;
442 memset(&rast
, 0, sizeof rast
);
444 rast
.flatshade_first
= 0;
445 rast
.light_twoside
= 0;
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;
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;
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;
471 dec
->rast
= dec
->pipe
->create_rasterizer_state(dec
->pipe
, &rast
);
472 dec
->pipe
->bind_rasterizer_state(dec
->pipe
, dec
->rast
);
474 memset(&blend
, 0, sizeof blend
);
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
;
489 dec
->blend
= dec
->pipe
->create_blend_state(dec
->pipe
, &blend
);
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;
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
);
514 init_idct(struct vl_mpeg12_decoder
*dec
, unsigned buffer_width
, unsigned buffer_height
)
516 unsigned chroma_width
, chroma_height
, chroma_blocks_x
, chroma_blocks_y
;
517 struct pipe_sampler_view
*idct_matrix
;
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
;
523 if (!(idct_matrix
= vl_idct_upload_matrix(dec
->pipe
)))
524 goto error_idct_matrix
;
526 if (!vl_idct_init(&dec
->idct_y
, dec
->pipe
, buffer_width
, buffer_height
,
530 if (dec
->base
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
531 chroma_width
= buffer_width
/ 2;
532 chroma_height
= buffer_height
/ 2;
535 } else if (dec
->base
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
536 chroma_width
= buffer_width
;
537 chroma_height
= buffer_height
/ 2;
541 chroma_width
= buffer_width
;
542 chroma_height
= buffer_height
;
547 if(!vl_idct_init(&dec
->idct_c
, dec
->pipe
, chroma_width
, chroma_height
,
548 chroma_blocks_x
, chroma_blocks_y
, idct_matrix
))
551 pipe_sampler_view_reference(&idct_matrix
, NULL
);
555 vl_idct_cleanup(&dec
->idct_y
);
558 pipe_sampler_view_reference(&idct_matrix
, NULL
);
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
)
571 struct vl_mpeg12_decoder
*dec
;
574 assert(u_reduce_video_profile(profile
) == PIPE_VIDEO_CODEC_MPEG12
);
576 dec
= CALLOC_STRUCT(vl_mpeg12_decoder
);
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
;
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
;
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
);
598 dec
->base
.width
= align(width
, MACROBLOCK_WIDTH
);
599 dec
->base
.height
= align(height
, MACROBLOCK_HEIGHT
);
601 if (!init_idct(dec
, dec
->base
.width
, dec
->base
.height
))
604 if (!vl_mpeg12_mc_renderer_init(&dec
->mc
, dec
->pipe
, dec
->base
.width
, dec
->base
.height
))
607 if (!init_pipe_state(dec
))
608 goto error_pipe_state
;
613 vl_mpeg12_mc_renderer_cleanup(&dec
->mc
);
616 vl_idct_cleanup(&dec
->idct_y
);
617 vl_idct_cleanup(&dec
->idct_c
);