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 upload_buffer(struct vl_mpeg12_decoder
*ctx
,
55 struct vl_mpeg12_buffer
*buffer
,
56 struct pipe_mpeg12_macroblock
*mb
)
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
;
76 /* TODO: Implement 422, 444 */
77 assert(ctx
->base
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
);
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
;
88 vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer
*buffer
)
90 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
91 struct vl_mpeg12_decoder
*dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
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]);
108 vl_mpeg12_buffer_map(struct pipe_video_decode_buffer
*buffer
)
110 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
111 struct vl_mpeg12_decoder
*dec
;
114 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
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]);
124 vl_mpeg12_buffer_add_macroblocks(struct pipe_video_decode_buffer
*buffer
,
125 unsigned num_macroblocks
,
126 struct pipe_macroblock
*macroblocks
)
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
;
135 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
138 assert(num_macroblocks
);
140 assert(macroblocks
->codec
== PIPE_VIDEO_CODEC_MPEG12
);
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
]);
149 vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer
*buffer
)
151 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
152 struct vl_mpeg12_decoder
*dec
;
155 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
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]);
165 vl_mpeg12_destroy(struct pipe_video_decoder
*decoder
)
167 struct vl_mpeg12_decoder
*dec
= (struct vl_mpeg12_decoder
*)decoder
;
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
);
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
);
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
);
190 static struct pipe_video_decode_buffer
*
191 vl_mpeg12_create_buffer(struct pipe_video_decoder
*decoder
)
193 const enum pipe_format idct_source_formats
[3] = {
194 PIPE_FORMAT_R16G16B16A16_SNORM
,
195 PIPE_FORMAT_R16G16B16A16_SNORM
,
196 PIPE_FORMAT_R16G16B16A16_SNORM
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
205 struct vl_mpeg12_decoder
*dec
= (struct vl_mpeg12_decoder
*)decoder
;
206 struct vl_mpeg12_buffer
*buffer
;
208 struct pipe_sampler_view
**idct_views
, **mc_views
;
209 struct pipe_surface
**idct_surfaces
;
213 buffer
= CALLOC_STRUCT(vl_mpeg12_buffer
);
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
;
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
);
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
;
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,
238 if (!buffer
->idct_source
)
239 goto error_idct_source
;
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,
246 if (!buffer
->idct_2_mc
)
247 goto error_idct_2_mc
;
249 idct_views
= buffer
->idct_source
->get_sampler_views(buffer
->idct_source
);
251 goto error_idct_views
;
253 idct_surfaces
= buffer
->idct_2_mc
->get_surfaces(buffer
->idct_2_mc
);
255 goto error_idct_surfaces
;
257 if (!vl_idct_init_buffer(&dec
->idct_y
, &buffer
->idct
[0],
258 idct_views
[0], idct_surfaces
[0]))
261 if (!vl_idct_init_buffer(&dec
->idct_c
, &buffer
->idct
[1],
262 idct_views
[1], idct_surfaces
[1]))
265 if (!vl_idct_init_buffer(&dec
->idct_c
, &buffer
->idct
[2],
266 idct_views
[2], idct_surfaces
[2]))
269 mc_views
= buffer
->idct_2_mc
->get_sampler_views(buffer
->idct_2_mc
);
273 if(!vl_mpeg12_mc_init_buffer(&dec
->mc
, &buffer
->mc
[0], mc_views
[0]))
276 if(!vl_mpeg12_mc_init_buffer(&dec
->mc
, &buffer
->mc
[1], mc_views
[1]))
279 if(!vl_mpeg12_mc_init_buffer(&dec
->mc
, &buffer
->mc
[2], mc_views
[2]))
282 return &buffer
->base
;
285 vl_mpeg12_mc_cleanup_buffer(&buffer
->mc
[1]);
288 vl_mpeg12_mc_cleanup_buffer(&buffer
->mc
[0]);
292 vl_idct_cleanup_buffer(&dec
->idct_c
, &buffer
->idct
[2]);
295 vl_idct_cleanup_buffer(&dec
->idct_c
, &buffer
->idct
[1]);
298 vl_idct_cleanup_buffer(&dec
->idct_y
, &buffer
->idct
[0]);
303 buffer
->idct_2_mc
->destroy(buffer
->idct_2_mc
);
306 buffer
->idct_source
->destroy(buffer
->idct_source
);
309 vl_vb_cleanup(&buffer
->vertex_stream
);
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
)
322 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
323 struct vl_mpeg12_decoder
*dec
;
325 struct pipe_sampler_view
**sv_past
;
326 struct pipe_sampler_view
**sv_future
;
327 struct pipe_surface
**surfaces
;
329 struct pipe_sampler_view
*sv_refs
[2];
330 unsigned ne_start
, ne_num
, e_start
, e_num
;
335 dec
= (struct vl_mpeg12_decoder
*)buf
->base
.decoder
;
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
;
341 surfaces
= dst
->get_surfaces(dst
);
343 vl_vb_restart(&buf
->vertex_stream
, &ne_start
, &ne_num
, &e_start
, &e_num
);
345 dec
->pipe
->set_vertex_buffers(dec
->pipe
, 2, buf
->vertex_bufs
.all
);
346 dec
->pipe
->bind_blend_state(dec
->pipe
, dec
->blend
);
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
);
352 sv_refs
[0] = sv_past
? sv_past
[i
] : NULL
;
353 sv_refs
[1] = sv_future
? sv_future
[i
] : NULL
;
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
);
361 vl_mpeg12_decoder_clear_buffer(struct pipe_video_decode_buffer
*buffer
)
363 struct vl_mpeg12_buffer
*buf
= (struct vl_mpeg12_buffer
*)buffer
;
364 unsigned ne_start
, ne_num
, e_start
, e_num
;
368 vl_vb_restart(&buf
->vertex_stream
, &ne_start
, &ne_num
, &e_start
, &e_num
);
372 init_pipe_state(struct vl_mpeg12_decoder
*dec
)
374 struct pipe_rasterizer_state rast
;
375 struct pipe_blend_state blend
;
376 struct pipe_depth_stencil_alpha_state dsa
;
381 memset(&rast
, 0, sizeof rast
);
383 rast
.flatshade_first
= 0;
384 rast
.light_twoside
= 0;
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;
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;
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;
410 dec
->rast
= dec
->pipe
->create_rasterizer_state(dec
->pipe
, &rast
);
411 dec
->pipe
->bind_rasterizer_state(dec
->pipe
, dec
->rast
);
413 memset(&blend
, 0, sizeof blend
);
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
;
428 dec
->blend
= dec
->pipe
->create_blend_state(dec
->pipe
, &blend
);
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;
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
);
453 init_idct(struct vl_mpeg12_decoder
*dec
, unsigned buffer_width
, unsigned buffer_height
)
455 unsigned chroma_width
, chroma_height
, chroma_blocks_x
, chroma_blocks_y
;
456 struct pipe_sampler_view
*idct_matrix
;
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
;
462 if (!(idct_matrix
= vl_idct_upload_matrix(dec
->pipe
)))
463 goto error_idct_matrix
;
465 if (!vl_idct_init(&dec
->idct_y
, dec
->pipe
, buffer_width
, buffer_height
,
469 if (dec
->base
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
470 chroma_width
= buffer_width
/ 2;
471 chroma_height
= buffer_height
/ 2;
474 } else if (dec
->base
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
475 chroma_width
= buffer_width
;
476 chroma_height
= buffer_height
/ 2;
480 chroma_width
= buffer_width
;
481 chroma_height
= buffer_height
;
486 if(!vl_idct_init(&dec
->idct_c
, dec
->pipe
, chroma_width
, chroma_height
,
487 chroma_blocks_x
, chroma_blocks_y
, idct_matrix
))
490 pipe_sampler_view_reference(&idct_matrix
, NULL
);
494 vl_idct_cleanup(&dec
->idct_y
);
497 pipe_sampler_view_reference(&idct_matrix
, NULL
);
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
)
510 struct vl_mpeg12_decoder
*dec
;
513 assert(u_reduce_video_profile(profile
) == PIPE_VIDEO_CODEC_MPEG12
);
515 dec
= CALLOC_STRUCT(vl_mpeg12_decoder
);
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
;
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
;
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
);
537 dec
->base
.width
= align(width
, MACROBLOCK_WIDTH
);
538 dec
->base
.height
= align(height
, MACROBLOCK_HEIGHT
);
540 if (!init_idct(dec
, dec
->base
.width
, dec
->base
.height
))
543 if (!vl_mpeg12_mc_renderer_init(&dec
->mc
, dec
->pipe
, dec
->base
.width
, dec
->base
.height
))
546 if (!init_pipe_state(dec
))
547 goto error_pipe_state
;
552 vl_mpeg12_mc_renderer_cleanup(&dec
->mc
);
555 vl_idct_cleanup(&dec
->idct_y
);
556 vl_idct_cleanup(&dec
->idct_c
);