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