vl_mpeg12_decoder.c revision 0121aae967d3d1366cccc8946cf89ad22818365e
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 <math.h> 29#include <assert.h> 30 31#include <util/u_memory.h> 32#include <util/u_rect.h> 33#include <util/u_video.h> 34 35#include "vl_mpeg12_decoder.h" 36#include "vl_defines.h" 37 38#define SCALE_FACTOR_SNORM (32768.0f / 256.0f) 39#define SCALE_FACTOR_SSCALED (1.0f / 256.0f) 40 41static const enum pipe_format const_zscan_source_formats[] = { 42 PIPE_FORMAT_R16_SNORM, 43 PIPE_FORMAT_R16_SSCALED 44}; 45 46static const unsigned num_zscan_source_formats = 47 sizeof(const_zscan_source_formats) / sizeof(enum pipe_format); 48 49static const enum pipe_format const_idct_source_formats[] = { 50 PIPE_FORMAT_R16G16B16A16_SNORM, 51 PIPE_FORMAT_R16G16B16A16_SSCALED 52}; 53 54static const unsigned num_idct_source_formats = 55 sizeof(const_idct_source_formats) / sizeof(enum pipe_format); 56 57static const enum pipe_format const_idct_intermediate_formats[] = { 58 PIPE_FORMAT_R16G16B16A16_FLOAT, 59 PIPE_FORMAT_R16G16B16A16_SNORM, 60 PIPE_FORMAT_R16G16B16A16_SSCALED, 61 PIPE_FORMAT_R32G32B32A32_FLOAT 62}; 63 64static const unsigned num_idct_intermediate_formats = 65 sizeof(const_idct_intermediate_formats) / sizeof(enum pipe_format); 66 67static const enum pipe_format const_mc_source_formats[] = { 68 PIPE_FORMAT_R16_SNORM, 69 PIPE_FORMAT_R16_SSCALED 70}; 71 72static const unsigned num_mc_source_formats = 73 sizeof(const_mc_source_formats) / sizeof(enum pipe_format); 74 75static bool 76init_zscan_buffer(struct vl_mpeg12_buffer *buffer) 77{ 78 enum pipe_format formats[3]; 79 80 struct pipe_sampler_view **source; 81 struct pipe_surface **destination; 82 83 struct vl_mpeg12_decoder *dec; 84 85 unsigned i; 86 87 assert(buffer); 88 89 dec = (struct vl_mpeg12_decoder*)buffer->base.decoder; 90 91 formats[0] = formats[1] = formats[2] = dec->zscan_source_format; 92 buffer->zscan_source = vl_video_buffer_init(dec->base.context, dec->pipe, 93 dec->blocks_per_line * BLOCK_WIDTH * BLOCK_HEIGHT, 94 dec->max_blocks / dec->blocks_per_line, 95 1, PIPE_VIDEO_CHROMA_FORMAT_444, 96 formats, PIPE_USAGE_STATIC); 97 if (!buffer->zscan_source) 98 goto error_source; 99 100 source = buffer->zscan_source->get_sampler_view_planes(buffer->zscan_source); 101 if (!source) 102 goto error_sampler; 103 104 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 105 destination = dec->idct_source->get_surfaces(dec->idct_source); 106 else 107 destination = dec->mc_source->get_surfaces(dec->mc_source); 108 109 if (!destination) 110 goto error_surface; 111 112 for (i = 0; i < VL_MAX_PLANES; ++i) 113 if (!vl_zscan_init_buffer(i == 0 ? &dec->zscan_y : &dec->zscan_c, 114 &buffer->zscan[i], source[i], destination[i])) 115 goto error_plane; 116 117 return true; 118 119error_plane: 120 for (; i > 0; --i) 121 vl_zscan_cleanup_buffer(&buffer->zscan[i - 1]); 122 123error_surface: 124error_sampler: 125 buffer->zscan_source->destroy(buffer->zscan_source); 126 127error_source: 128 return false; 129} 130 131static void 132cleanup_zscan_buffer(struct vl_mpeg12_buffer *buffer) 133{ 134 unsigned i; 135 136 assert(buffer); 137 138 for (i = 0; i < VL_MAX_PLANES; ++i) 139 vl_zscan_cleanup_buffer(&buffer->zscan[i]); 140 buffer->zscan_source->destroy(buffer->zscan_source); 141} 142 143static bool 144init_idct_buffer(struct vl_mpeg12_buffer *buffer) 145{ 146 struct pipe_sampler_view **idct_source_sv, **mc_source_sv; 147 struct pipe_surface **idct_surfaces; 148 149 struct vl_mpeg12_decoder *dec; 150 151 unsigned i; 152 153 assert(buffer); 154 155 dec = (struct vl_mpeg12_decoder*)buffer->base.decoder; 156 157 idct_source_sv = dec->idct_source->get_sampler_view_planes(dec->idct_source); 158 if (!idct_source_sv) 159 goto error_source_sv; 160 161 mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source); 162 if (!mc_source_sv) 163 goto error_mc_source_sv; 164 165 idct_surfaces = dec->mc_source->get_surfaces(dec->mc_source); 166 if (!idct_surfaces) 167 goto error_surfaces; 168 169 for (i = 0; i < 3; ++i) 170 if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c, 171 &buffer->idct[i], idct_source_sv[i], 172 mc_source_sv[i], idct_surfaces[i])) 173 goto error_plane; 174 175 return true; 176 177error_plane: 178 for (; i > 0; --i) 179 vl_idct_cleanup_buffer(i == 1 ? &dec->idct_c : &dec->idct_y, &buffer->idct[i - 1]); 180 181error_surfaces: 182error_mc_source_sv: 183error_source_sv: 184 return false; 185} 186 187static void 188cleanup_idct_buffer(struct vl_mpeg12_buffer *buf) 189{ 190 struct vl_mpeg12_decoder *dec; 191 assert(buf); 192 193 dec = (struct vl_mpeg12_decoder*)buf->base.decoder; 194 assert(dec); 195 196 vl_idct_cleanup_buffer(&dec->idct_y, &buf->idct[0]); 197 vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[1]); 198 vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[2]); 199} 200 201static bool 202init_mc_buffer(struct vl_mpeg12_buffer *buf) 203{ 204 struct vl_mpeg12_decoder *dec; 205 206 assert(buf); 207 208 dec = (struct vl_mpeg12_decoder*)buf->base.decoder; 209 assert(dec); 210 211 if(!vl_mc_init_buffer(&dec->mc_y, &buf->mc[0])) 212 goto error_mc_y; 213 214 if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[1])) 215 goto error_mc_cb; 216 217 if(!vl_mc_init_buffer(&dec->mc_c, &buf->mc[2])) 218 goto error_mc_cr; 219 220 return true; 221 222error_mc_cr: 223 vl_mc_cleanup_buffer(&buf->mc[1]); 224 225error_mc_cb: 226 vl_mc_cleanup_buffer(&buf->mc[0]); 227 228error_mc_y: 229 return false; 230} 231 232static void 233cleanup_mc_buffer(struct vl_mpeg12_buffer *buf) 234{ 235 unsigned i; 236 237 assert(buf); 238 239 for (i = 0; i < VL_MAX_PLANES; ++i) 240 vl_mc_cleanup_buffer(&buf->mc[i]); 241} 242 243static void 244vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer) 245{ 246 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 247 struct vl_mpeg12_decoder *dec; 248 249 assert(buf); 250 251 dec = (struct vl_mpeg12_decoder*)buf->base.decoder; 252 assert(dec); 253 254 cleanup_zscan_buffer(buf); 255 256 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 257 cleanup_idct_buffer(buf); 258 259 cleanup_mc_buffer(buf); 260 261 vl_vb_cleanup(&buf->vertex_stream); 262 263 FREE(buf); 264} 265 266static void 267vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer) 268{ 269 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 270 struct vl_mpeg12_decoder *dec; 271 272 struct pipe_sampler_view **sampler_views; 273 unsigned i; 274 275 assert(buf); 276 277 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 278 assert(dec); 279 280 vl_vb_map(&buf->vertex_stream, dec->pipe); 281 282 sampler_views = buf->zscan_source->get_sampler_view_planes(buf->zscan_source); 283 284 assert(sampler_views); 285 286 for (i = 0; i < VL_MAX_PLANES; ++i) { 287 struct pipe_resource *tex = sampler_views[i]->texture; 288 struct pipe_box rect = 289 { 290 0, 0, 0, 291 tex->width0, 292 tex->height0, 293 1 294 }; 295 296 buf->tex_transfer[i] = dec->pipe->get_transfer 297 ( 298 dec->pipe, tex, 299 0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 300 &rect 301 ); 302 303 buf->texels[i] = dec->pipe->transfer_map(dec->pipe, buf->tex_transfer[i]); 304 } 305 306 if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) { 307 struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES]; 308 struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES]; 309 310 for (i = 0; i < VL_MAX_PLANES; ++i) 311 ycbcr_stream[i] = vl_vb_get_ycbcr_stream(&buf->vertex_stream, i); 312 313 for (i = 0; i < VL_MAX_REF_FRAMES; ++i) 314 mv_stream[i] = vl_vb_get_mv_stream(&buf->vertex_stream, i); 315 316 vl_mpg12_bs_set_buffers(&buf->bs, ycbcr_stream, buf->texels, mv_stream); 317 } else { 318 for (i = 0; i < VL_MAX_PLANES; ++i) 319 vl_zscan_set_layout(&buf->zscan[i], dec->zscan_linear); 320 } 321} 322 323static struct pipe_ycbcr_block * 324vl_mpeg12_buffer_get_ycbcr_stream(struct pipe_video_decode_buffer *buffer, int component) 325{ 326 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 327 328 assert(buf); 329 330 return vl_vb_get_ycbcr_stream(&buf->vertex_stream, component); 331} 332 333static short * 334vl_mpeg12_buffer_get_ycbcr_buffer(struct pipe_video_decode_buffer *buffer, int component) 335{ 336 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 337 338 assert(buf); 339 assert(component < VL_MAX_PLANES); 340 341 return buf->texels[component]; 342} 343 344static unsigned 345vl_mpeg12_buffer_get_mv_stream_stride(struct pipe_video_decode_buffer *buffer) 346{ 347 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 348 349 assert(buf); 350 351 return vl_vb_get_mv_stream_stride(&buf->vertex_stream); 352} 353 354static struct pipe_motionvector * 355vl_mpeg12_buffer_get_mv_stream(struct pipe_video_decode_buffer *buffer, int ref_frame) 356{ 357 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 358 359 assert(buf); 360 361 return vl_vb_get_mv_stream(&buf->vertex_stream, ref_frame); 362} 363 364static void 365vl_mpeg12_buffer_decode_bitstream(struct pipe_video_decode_buffer *buffer, 366 unsigned num_bytes, const void *data, 367 struct pipe_mpeg12_picture_desc *picture, 368 unsigned num_ycbcr_blocks[3]) 369{ 370 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 371 struct vl_mpeg12_decoder *dec; 372 unsigned i; 373 374 assert(buf); 375 376 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 377 assert(dec); 378 379 for (i = 0; i < VL_MAX_PLANES; ++i) 380 vl_zscan_set_layout(&buf->zscan[i], picture->alternate_scan ? dec->zscan_alternate : dec->zscan_normal); 381 382 vl_mpg12_bs_decode(&buf->bs, num_bytes, data, picture, num_ycbcr_blocks); 383} 384 385static void 386vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer) 387{ 388 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 389 struct vl_mpeg12_decoder *dec; 390 unsigned i; 391 392 assert(buf); 393 394 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 395 assert(dec); 396 397 vl_vb_unmap(&buf->vertex_stream, dec->pipe); 398 399 for (i = 0; i < VL_MAX_PLANES; ++i) { 400 dec->pipe->transfer_unmap(dec->pipe, buf->tex_transfer[i]); 401 dec->pipe->transfer_destroy(dec->pipe, buf->tex_transfer[i]); 402 } 403} 404 405static void 406vl_mpeg12_destroy(struct pipe_video_decoder *decoder) 407{ 408 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder; 409 410 assert(decoder); 411 412 /* Asserted in softpipe_delete_fs_state() for some reason */ 413 dec->pipe->bind_vs_state(dec->pipe, NULL); 414 dec->pipe->bind_fs_state(dec->pipe, NULL); 415 416 dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa); 417 dec->pipe->delete_sampler_state(dec->pipe, dec->sampler_ycbcr); 418 419 vl_mc_cleanup(&dec->mc_y); 420 vl_mc_cleanup(&dec->mc_c); 421 dec->mc_source->destroy(dec->mc_source); 422 423 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 424 vl_idct_cleanup(&dec->idct_y); 425 vl_idct_cleanup(&dec->idct_c); 426 dec->idct_source->destroy(dec->idct_source); 427 } 428 429 vl_zscan_cleanup(&dec->zscan_y); 430 vl_zscan_cleanup(&dec->zscan_c); 431 432 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_ycbcr); 433 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves_mv); 434 435 pipe_resource_reference(&dec->quads.buffer, NULL); 436 pipe_resource_reference(&dec->pos.buffer, NULL); 437 438 pipe_sampler_view_reference(&dec->zscan_linear, NULL); 439 pipe_sampler_view_reference(&dec->zscan_normal, NULL); 440 pipe_sampler_view_reference(&dec->zscan_alternate, NULL); 441 442 FREE(dec); 443} 444 445static struct pipe_video_decode_buffer * 446vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder) 447{ 448 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder; 449 struct vl_mpeg12_buffer *buffer; 450 451 assert(dec); 452 453 buffer = CALLOC_STRUCT(vl_mpeg12_buffer); 454 if (buffer == NULL) 455 return NULL; 456 457 buffer->base.decoder = decoder; 458 buffer->base.destroy = vl_mpeg12_buffer_destroy; 459 buffer->base.map = vl_mpeg12_buffer_map; 460 buffer->base.get_ycbcr_stream = vl_mpeg12_buffer_get_ycbcr_stream; 461 buffer->base.get_ycbcr_buffer = vl_mpeg12_buffer_get_ycbcr_buffer; 462 buffer->base.get_mv_stream_stride = vl_mpeg12_buffer_get_mv_stream_stride; 463 buffer->base.get_mv_stream = vl_mpeg12_buffer_get_mv_stream; 464 buffer->base.decode_bitstream = vl_mpeg12_buffer_decode_bitstream; 465 buffer->base.unmap = vl_mpeg12_buffer_unmap; 466 467 if (!vl_vb_init(&buffer->vertex_stream, dec->pipe, 468 dec->base.width / MACROBLOCK_WIDTH, 469 dec->base.height / MACROBLOCK_HEIGHT)) 470 goto error_vertex_buffer; 471 472 if (!init_mc_buffer(buffer)) 473 goto error_mc; 474 475 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 476 if (!init_idct_buffer(buffer)) 477 goto error_idct; 478 479 if (!init_zscan_buffer(buffer)) 480 goto error_zscan; 481 482 if (dec->base.entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) 483 vl_mpg12_bs_init(&buffer->bs, 484 dec->base.width / MACROBLOCK_WIDTH, 485 dec->base.height / MACROBLOCK_HEIGHT); 486 487 return &buffer->base; 488 489error_zscan: 490 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 491 cleanup_idct_buffer(buffer); 492 493error_idct: 494 cleanup_mc_buffer(buffer); 495 496error_mc: 497 vl_vb_cleanup(&buffer->vertex_stream); 498 499error_vertex_buffer: 500 FREE(buffer); 501 return NULL; 502} 503 504static void 505vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer, 506 unsigned num_ycbcr_blocks[3], 507 struct pipe_video_buffer *refs[2], 508 struct pipe_video_buffer *dst) 509{ 510 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer; 511 struct vl_mpeg12_decoder *dec; 512 513 struct pipe_sampler_view **sv[VL_MAX_REF_FRAMES], **mc_source_sv; 514 struct pipe_surface **surfaces; 515 516 struct pipe_vertex_buffer vb[3]; 517 518 unsigned i, j, component; 519 unsigned nr_components; 520 521 assert(buf); 522 523 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 524 assert(dec); 525 526 for (i = 0; i < 2; ++i) 527 sv[i] = refs[i] ? refs[i]->get_sampler_view_planes(refs[i]) : NULL; 528 529 vb[0] = dec->quads; 530 vb[1] = dec->pos; 531 532 surfaces = dst->get_surfaces(dst); 533 534 dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_mv); 535 for (i = 0; i < VL_MAX_PLANES; ++i) { 536 if (!surfaces[i]) continue; 537 538 vl_mc_set_surface(&buf->mc[i], surfaces[i]); 539 540 for (j = 0; j < VL_MAX_REF_FRAMES; ++j) { 541 if (!sv[j]) continue; 542 543 vb[2] = vl_vb_get_mv(&buf->vertex_stream, j);; 544 dec->pipe->set_vertex_buffers(dec->pipe, 3, vb); 545 546 vl_mc_render_ref(&buf->mc[i], sv[j][i]); 547 } 548 } 549 550 dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves_ycbcr); 551 for (i = 0; i < VL_MAX_PLANES; ++i) { 552 if (!num_ycbcr_blocks[i]) continue; 553 554 vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, i); 555 dec->pipe->set_vertex_buffers(dec->pipe, 2, vb); 556 557 vl_zscan_render(&buf->zscan[i] , num_ycbcr_blocks[i]); 558 559 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 560 vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], num_ycbcr_blocks[i]); 561 } 562 563 mc_source_sv = dec->mc_source->get_sampler_view_planes(dec->mc_source); 564 for (i = 0, component = 0; i < VL_MAX_PLANES; ++i) { 565 if (!surfaces[i]) continue; 566 567 nr_components = util_format_get_nr_components(surfaces[i]->texture->format); 568 for (j = 0; j < nr_components; ++j, ++component) { 569 if (!num_ycbcr_blocks[i]) continue; 570 571 vb[1] = vl_vb_get_ycbcr(&buf->vertex_stream, component); 572 dec->pipe->set_vertex_buffers(dec->pipe, 2, vb); 573 574 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 575 vl_idct_prepare_stage2(component == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[component]); 576 else { 577 dec->pipe->set_fragment_sampler_views(dec->pipe, 1, &mc_source_sv[component]); 578 dec->pipe->bind_fragment_sampler_states(dec->pipe, 1, &dec->sampler_ycbcr); 579 } 580 vl_mc_render_ycbcr(&buf->mc[i], j, num_ycbcr_blocks[component]); 581 } 582 } 583} 584 585static bool 586init_pipe_state(struct vl_mpeg12_decoder *dec) 587{ 588 struct pipe_depth_stencil_alpha_state dsa; 589 struct pipe_sampler_state sampler; 590 unsigned i; 591 592 assert(dec); 593 594 memset(&dsa, 0, sizeof dsa); 595 dsa.depth.enabled = 0; 596 dsa.depth.writemask = 0; 597 dsa.depth.func = PIPE_FUNC_ALWAYS; 598 for (i = 0; i < 2; ++i) { 599 dsa.stencil[i].enabled = 0; 600 dsa.stencil[i].func = PIPE_FUNC_ALWAYS; 601 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP; 602 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP; 603 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP; 604 dsa.stencil[i].valuemask = 0; 605 dsa.stencil[i].writemask = 0; 606 } 607 dsa.alpha.enabled = 0; 608 dsa.alpha.func = PIPE_FUNC_ALWAYS; 609 dsa.alpha.ref_value = 0; 610 dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa); 611 dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa); 612 613 memset(&sampler, 0, sizeof(sampler)); 614 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 615 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 616 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER; 617 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 618 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 619 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 620 sampler.compare_mode = PIPE_TEX_COMPARE_NONE; 621 sampler.compare_func = PIPE_FUNC_ALWAYS; 622 sampler.normalized_coords = 1; 623 dec->sampler_ycbcr = dec->pipe->create_sampler_state(dec->pipe, &sampler); 624 if (!dec->sampler_ycbcr) 625 return false; 626 627 return true; 628} 629 630static enum pipe_format 631find_first_supported_format(struct vl_mpeg12_decoder *dec, 632 const enum pipe_format formats[], 633 unsigned num_formats, 634 enum pipe_texture_target target) 635{ 636 struct pipe_screen *screen; 637 unsigned i; 638 639 assert(dec); 640 641 screen = dec->pipe->screen; 642 643 for (i = 0; i < num_formats; ++i) 644 if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1, 645 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET)) 646 return formats[i]; 647 648 return PIPE_FORMAT_NONE; 649} 650 651static bool 652init_zscan(struct vl_mpeg12_decoder *dec) 653{ 654 unsigned num_channels; 655 656 assert(dec); 657 658 dec->blocks_per_line = 4; 659 dec->max_blocks = 660 (dec->base.width * dec->base.height) / 661 (BLOCK_WIDTH * BLOCK_HEIGHT); 662 663 dec->zscan_source_format = find_first_supported_format(dec, const_zscan_source_formats, 664 num_zscan_source_formats, PIPE_TEXTURE_2D); 665 666 if (dec->zscan_source_format == PIPE_FORMAT_NONE) 667 return false; 668 669 dec->zscan_linear = vl_zscan_layout(dec->pipe, vl_zscan_linear, dec->blocks_per_line); 670 dec->zscan_normal = vl_zscan_layout(dec->pipe, vl_zscan_normal, dec->blocks_per_line); 671 dec->zscan_alternate = vl_zscan_layout(dec->pipe, vl_zscan_alternate, dec->blocks_per_line); 672 673 num_channels = dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT ? 4 : 1; 674 675 if (!vl_zscan_init(&dec->zscan_y, dec->pipe, dec->base.width, dec->base.height, 676 dec->blocks_per_line, dec->max_blocks, num_channels)) 677 return false; 678 679 if (!vl_zscan_init(&dec->zscan_c, dec->pipe, dec->chroma_width, dec->chroma_height, 680 dec->blocks_per_line, dec->max_blocks, num_channels)) 681 return false; 682 683 return true; 684} 685 686static bool 687init_idct(struct vl_mpeg12_decoder *dec, float *mc_scale) 688{ 689 unsigned nr_of_idct_render_targets; 690 enum pipe_format formats[3]; 691 692 struct pipe_sampler_view *matrix, *transpose = NULL; 693 float matrix_scale, transpose_scale; 694 695 nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS); 696 697 // more than 4 render targets usually doesn't makes any seens 698 nr_of_idct_render_targets = MIN2(nr_of_idct_render_targets, 4); 699 700 formats[0] = formats[1] = formats[2] = find_first_supported_format(dec, const_idct_source_formats, 701 num_idct_source_formats, PIPE_TEXTURE_2D); 702 703 switch (formats[0]) { 704 case PIPE_FORMAT_NONE: 705 goto error_idct_format; 706 707 case PIPE_FORMAT_R16G16B16A16_SSCALED: 708 matrix_scale = SCALE_FACTOR_SSCALED; 709 break; 710 711 case PIPE_FORMAT_R16G16B16A16_SNORM: 712 matrix_scale = SCALE_FACTOR_SNORM; 713 break; 714 715 default: 716 assert(0); 717 return false; 718 } 719 720 dec->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe, 721 dec->base.width / 4, dec->base.height, 1, 722 dec->base.chroma_format, 723 formats, PIPE_USAGE_STATIC); 724 if (!dec->idct_source) 725 goto error_idct_source; 726 727 formats[0] = formats[1] = formats[2] = find_first_supported_format(dec, const_idct_intermediate_formats, 728 num_idct_intermediate_formats, PIPE_TEXTURE_3D); 729 730 switch (formats[0]) { 731 case PIPE_FORMAT_NONE: 732 goto error_mc_format; 733 734 case PIPE_FORMAT_R16G16B16A16_FLOAT: 735 case PIPE_FORMAT_R32G32B32A32_FLOAT: 736 transpose_scale = 1.0f; 737 *mc_scale = 1.0f; 738 break; 739 740 case PIPE_FORMAT_R16_SSCALED: 741 transpose_scale = matrix_scale = sqrt(matrix_scale); 742 transpose_scale /= SCALE_FACTOR_SSCALED; 743 *mc_scale = SCALE_FACTOR_SSCALED; 744 break; 745 746 default: 747 transpose_scale = matrix_scale = sqrt(matrix_scale); 748 *mc_scale = 1.0f; 749 break; 750 } 751 752 dec->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe, 753 dec->base.width / nr_of_idct_render_targets, 754 dec->base.height / 4, nr_of_idct_render_targets, 755 dec->base.chroma_format, 756 formats, PIPE_USAGE_STATIC); 757 758 if (!dec->mc_source) 759 goto error_mc_source; 760 761 if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale))) 762 goto error_matrix; 763 764 if (matrix_scale != transpose_scale) { 765 if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale))) 766 goto error_transpose; 767 } else 768 pipe_sampler_view_reference(&transpose, matrix); 769 770 if (!vl_idct_init(&dec->idct_y, dec->pipe, dec->base.width, dec->base.height, 771 nr_of_idct_render_targets, matrix, transpose)) 772 goto error_y; 773 774 if(!vl_idct_init(&dec->idct_c, dec->pipe, dec->chroma_width, dec->chroma_height, 775 nr_of_idct_render_targets, matrix, transpose)) 776 goto error_c; 777 778 pipe_sampler_view_reference(&matrix, NULL); 779 pipe_sampler_view_reference(&transpose, NULL); 780 781 return true; 782 783error_c: 784 vl_idct_cleanup(&dec->idct_y); 785 786error_y: 787 pipe_sampler_view_reference(&transpose, NULL); 788 789error_transpose: 790 pipe_sampler_view_reference(&matrix, NULL); 791 792error_matrix: 793 dec->mc_source->destroy(dec->mc_source); 794 795error_mc_source: 796error_mc_format: 797 dec->idct_source->destroy(dec->idct_source); 798 799error_idct_source: 800error_idct_format: 801 return false; 802} 803 804static bool 805init_mc_source_widthout_idct(struct vl_mpeg12_decoder *dec, float *mc_scale) 806{ 807 enum pipe_format formats[3]; 808 809 formats[0] = formats[1] = formats[2] = find_first_supported_format(dec, const_mc_source_formats, 810 num_mc_source_formats, PIPE_TEXTURE_2D); 811 812 switch (formats[0]) { 813 case PIPE_FORMAT_NONE: 814 return false; 815 816 case PIPE_FORMAT_R16_SNORM: 817 *mc_scale = SCALE_FACTOR_SNORM; 818 break; 819 820 case PIPE_FORMAT_R16_SSCALED: 821 *mc_scale = SCALE_FACTOR_SSCALED; 822 break; 823 824 default: 825 assert(0); 826 return false; 827 } 828 829 dec->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe, 830 dec->base.width, dec->base.height, 1, 831 dec->base.chroma_format, 832 formats, PIPE_USAGE_STATIC); 833 834 return dec->mc_source; 835} 836 837static void 838mc_vert_shader_callback(void *priv, struct vl_mc *mc, 839 struct ureg_program *shader, 840 unsigned first_output, 841 struct ureg_dst tex) 842{ 843 struct vl_mpeg12_decoder *dec = priv; 844 struct ureg_dst o_vtex; 845 846 assert(priv && mc); 847 assert(shader); 848 849 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 850 struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c; 851 vl_idct_stage2_vert_shader(idct, shader, first_output, tex); 852 } else { 853 o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output); 854 ureg_MOV(shader, ureg_writemask(o_vtex, TGSI_WRITEMASK_XY), ureg_src(tex)); 855 } 856} 857 858static void 859mc_frag_shader_callback(void *priv, struct vl_mc *mc, 860 struct ureg_program *shader, 861 unsigned first_input, 862 struct ureg_dst dst) 863{ 864 struct vl_mpeg12_decoder *dec = priv; 865 struct ureg_src src, sampler; 866 867 assert(priv && mc); 868 assert(shader); 869 870 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 871 struct vl_idct *idct = mc == &dec->mc_y ? &dec->idct_y : &dec->idct_c; 872 vl_idct_stage2_frag_shader(idct, shader, first_input, dst); 873 } else { 874 src = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, first_input, TGSI_INTERPOLATE_LINEAR); 875 sampler = ureg_DECL_sampler(shader, 0); 876 ureg_TEX(shader, dst, TGSI_TEXTURE_2D, src, sampler); 877 } 878} 879 880struct pipe_video_decoder * 881vl_create_mpeg12_decoder(struct pipe_video_context *context, 882 struct pipe_context *pipe, 883 enum pipe_video_profile profile, 884 enum pipe_video_entrypoint entrypoint, 885 enum pipe_video_chroma_format chroma_format, 886 unsigned width, unsigned height) 887{ 888 struct vl_mpeg12_decoder *dec; 889 float mc_scale; 890 891 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12); 892 893 dec = CALLOC_STRUCT(vl_mpeg12_decoder); 894 895 if (!dec) 896 return NULL; 897 898 dec->base.context = context; 899 dec->base.profile = profile; 900 dec->base.entrypoint = entrypoint; 901 dec->base.chroma_format = chroma_format; 902 dec->base.width = width; 903 dec->base.height = height; 904 905 dec->base.destroy = vl_mpeg12_destroy; 906 dec->base.create_buffer = vl_mpeg12_create_buffer; 907 dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer; 908 909 dec->pipe = pipe; 910 911 dec->quads = vl_vb_upload_quads(dec->pipe); 912 dec->pos = vl_vb_upload_pos( 913 dec->pipe, 914 dec->base.width / MACROBLOCK_WIDTH, 915 dec->base.height / MACROBLOCK_HEIGHT 916 ); 917 918 dec->ves_ycbcr = vl_vb_get_ves_ycbcr(dec->pipe); 919 dec->ves_mv = vl_vb_get_ves_mv(dec->pipe); 920 921 /* TODO: Implement 422, 444 */ 922 assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420); 923 924 if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) { 925 dec->chroma_width = dec->base.width / 2; 926 dec->chroma_height = dec->base.height / 2; 927 } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) { 928 dec->chroma_width = dec->base.width; 929 dec->chroma_height = dec->base.height / 2; 930 } else { 931 dec->chroma_width = dec->base.width; 932 dec->chroma_height = dec->base.height; 933 } 934 935 if (!init_zscan(dec)) 936 goto error_zscan; 937 938 if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 939 if (!init_idct(dec, &mc_scale)) 940 goto error_sources; 941 } else { 942 if (!init_mc_source_widthout_idct(dec, &mc_scale)) 943 goto error_sources; 944 } 945 946 if (!vl_mc_init(&dec->mc_y, dec->pipe, dec->base.width, dec->base.height, MACROBLOCK_HEIGHT, mc_scale, 947 mc_vert_shader_callback, mc_frag_shader_callback, dec)) 948 goto error_mc_y; 949 950 // TODO 951 if (!vl_mc_init(&dec->mc_c, dec->pipe, dec->base.width, dec->base.height, BLOCK_HEIGHT, mc_scale, 952 mc_vert_shader_callback, mc_frag_shader_callback, dec)) 953 goto error_mc_c; 954 955 if (!init_pipe_state(dec)) 956 goto error_pipe_state; 957 958 return &dec->base; 959 960error_pipe_state: 961 vl_mc_cleanup(&dec->mc_c); 962 963error_mc_c: 964 vl_mc_cleanup(&dec->mc_y); 965 966error_mc_y: 967 if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 968 vl_idct_cleanup(&dec->idct_y); 969 vl_idct_cleanup(&dec->idct_c); 970 dec->idct_source->destroy(dec->idct_source); 971 } 972 dec->mc_source->destroy(dec->mc_source); 973 974error_sources: 975 vl_zscan_cleanup(&dec->zscan_y); 976 vl_zscan_cleanup(&dec->zscan_c); 977 978error_zscan: 979 FREE(dec); 980 return NULL; 981} 982