vl_mpeg12_decoder.c revision b88fa924009b5cc572187d3ca6a395d5226aa1c3
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 unsigned const_empty_block_mask_420[3][2][2] = { 42 { { 0x20, 0x10 }, { 0x08, 0x04 } }, 43 { { 0x02, 0x02 }, { 0x02, 0x02 } }, 44 { { 0x01, 0x01 }, { 0x01, 0x01 } } 45}; 46 47static const enum pipe_format const_idct_source_formats[] = { 48 PIPE_FORMAT_R16G16B16A16_SNORM, 49 PIPE_FORMAT_R16G16B16A16_SSCALED 50}; 51 52static const unsigned num_idct_source_formats = 53 sizeof(const_idct_source_formats) / sizeof(enum pipe_format); 54 55static const enum pipe_format const_idct_intermediate_formats[] = { 56 PIPE_FORMAT_R16G16B16A16_FLOAT, 57 PIPE_FORMAT_R16G16B16A16_SNORM, 58 PIPE_FORMAT_R16G16B16A16_SSCALED, 59 PIPE_FORMAT_R32G32B32A32_FLOAT 60}; 61 62static const unsigned num_idct_intermediate_formats = 63 sizeof(const_idct_intermediate_formats) / sizeof(enum pipe_format); 64 65static const enum pipe_format const_mc_source_formats[] = { 66 PIPE_FORMAT_R16_SNORM, 67 PIPE_FORMAT_R16_SSCALED 68}; 69 70static const unsigned num_mc_source_formats = 71 sizeof(const_mc_source_formats) / sizeof(enum pipe_format); 72 73static void 74map_buffers(struct vl_mpeg12_decoder *ctx, struct vl_mpeg12_buffer *buffer) 75{ 76 struct pipe_sampler_view **sampler_views; 77 struct pipe_resource *tex; 78 unsigned i; 79 80 assert(ctx && buffer); 81 82 if (ctx->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 83 sampler_views = buffer->idct_source->get_sampler_views(buffer->idct_source); 84 else 85 sampler_views = buffer->mc_source->get_sampler_views(buffer->mc_source); 86 assert(sampler_views); 87 88 for (i = 0; i < VL_MAX_PLANES; ++i) { 89 tex = sampler_views[i]->texture; 90 91 struct pipe_box rect = 92 { 93 0, 0, 0, 94 tex->width0, 95 tex->height0, 96 1 97 }; 98 99 buffer->tex_transfer[i] = ctx->pipe->get_transfer 100 ( 101 ctx->pipe, tex, 102 0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 103 &rect 104 ); 105 106 buffer->texels[i] = ctx->pipe->transfer_map(ctx->pipe, buffer->tex_transfer[i]); 107 } 108} 109 110static void 111upload_block(struct vl_mpeg12_buffer *buffer, unsigned plane, unsigned x, unsigned y, short *block) 112{ 113 unsigned tex_pitch; 114 short *texels; 115 116 unsigned i; 117 118 assert(buffer); 119 assert(block); 120 121 tex_pitch = buffer->tex_transfer[plane]->stride / sizeof(short); 122 texels = buffer->texels[plane] + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH; 123 124 for (i = 0; i < BLOCK_HEIGHT; ++i) 125 memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short)); 126} 127 128static void 129upload_buffer(struct vl_mpeg12_decoder *ctx, 130 struct vl_mpeg12_buffer *buffer, 131 struct pipe_mpeg12_macroblock *mb) 132{ 133 short *blocks; 134 unsigned tb, x, y; 135 136 assert(ctx); 137 assert(buffer); 138 assert(mb); 139 140 blocks = mb->blocks; 141 142 for (y = 0; y < 2; ++y) { 143 for (x = 0; x < 2; ++x, ++tb) { 144 if (mb->cbp & (*ctx->empty_block_mask)[0][y][x]) { 145 upload_block(buffer, 0, mb->mbx * 2 + x, mb->mby * 2 + y, blocks); 146 blocks += BLOCK_WIDTH * BLOCK_HEIGHT; 147 } 148 } 149 } 150 151 /* TODO: Implement 422, 444 */ 152 assert(ctx->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420); 153 154 for (tb = 1; tb < 3; ++tb) { 155 if (mb->cbp & (*ctx->empty_block_mask)[tb][0][0]) { 156 upload_block(buffer, tb, mb->mbx, mb->mby, blocks); 157 blocks += BLOCK_WIDTH * BLOCK_HEIGHT; 158 } 159 } 160} 161 162static void 163unmap_buffers(struct vl_mpeg12_decoder *ctx, struct vl_mpeg12_buffer *buffer) 164{ 165 unsigned i; 166 167 assert(ctx && buffer); 168 169 for (i = 0; i < VL_MAX_PLANES; ++i) { 170 ctx->pipe->transfer_unmap(ctx->pipe, buffer->tex_transfer[i]); 171 ctx->pipe->transfer_destroy(ctx->pipe, buffer->tex_transfer[i]); 172 } 173} 174 175static void 176cleanup_idct_buffer(struct vl_mpeg12_buffer *buf) 177{ 178 struct vl_mpeg12_decoder *dec; 179 assert(buf); 180 181 dec = (struct vl_mpeg12_decoder*)buf->base.decoder; 182 assert(dec); 183 184 buf->idct_source->destroy(buf->idct_source); 185 buf->idct_intermediate->destroy(buf->idct_intermediate); 186 vl_idct_cleanup_buffer(&dec->idct_y, &buf->idct[0]); 187 vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[1]); 188 vl_idct_cleanup_buffer(&dec->idct_c, &buf->idct[2]); 189} 190 191static void 192vl_mpeg12_buffer_destroy(struct pipe_video_decode_buffer *buffer) 193{ 194 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 195 struct vl_mpeg12_decoder *dec; 196 assert(buf); 197 198 dec = (struct vl_mpeg12_decoder*)buf->base.decoder; 199 assert(dec); 200 201 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 202 cleanup_idct_buffer(buf); 203 204 buf->mc_source->destroy(buf->mc_source); 205 vl_vb_cleanup(&buf->vertex_stream); 206 vl_mpeg12_mc_cleanup_buffer(&buf->mc[0]); 207 vl_mpeg12_mc_cleanup_buffer(&buf->mc[1]); 208 vl_mpeg12_mc_cleanup_buffer(&buf->mc[2]); 209 210 FREE(buf); 211} 212 213static void 214vl_mpeg12_buffer_map(struct pipe_video_decode_buffer *buffer) 215{ 216 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 217 struct vl_mpeg12_decoder *dec; 218 assert(buf); 219 220 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 221 assert(dec); 222 223 vl_vb_map(&buf->vertex_stream, dec->pipe); 224 map_buffers(dec, buf); 225} 226 227static void 228vl_mpeg12_buffer_add_macroblocks(struct pipe_video_decode_buffer *buffer, 229 unsigned num_macroblocks, 230 struct pipe_macroblock *macroblocks) 231{ 232 struct pipe_mpeg12_macroblock *mb = (struct pipe_mpeg12_macroblock*)macroblocks; 233 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 234 struct vl_mpeg12_decoder *dec; 235 unsigned i; 236 237 assert(buf); 238 239 dec = (struct vl_mpeg12_decoder*)buf->base.decoder; 240 assert(dec); 241 242 assert(num_macroblocks); 243 assert(macroblocks); 244 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12); 245 246 for ( i = 0; i < num_macroblocks; ++i ) { 247 vl_vb_add_block(&buf->vertex_stream, &mb[i], dec->empty_block_mask); 248 upload_buffer(dec, buf, &mb[i]); 249 } 250} 251 252static void 253vl_mpeg12_buffer_unmap(struct pipe_video_decode_buffer *buffer) 254{ 255 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer*)buffer; 256 struct vl_mpeg12_decoder *dec; 257 assert(buf); 258 259 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 260 assert(dec); 261 262 vl_vb_unmap(&buf->vertex_stream, dec->pipe); 263 unmap_buffers(dec, buf); 264} 265 266static void 267vl_mpeg12_destroy(struct pipe_video_decoder *decoder) 268{ 269 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder; 270 271 assert(decoder); 272 273 /* Asserted in softpipe_delete_fs_state() for some reason */ 274 dec->pipe->bind_vs_state(dec->pipe, NULL); 275 dec->pipe->bind_fs_state(dec->pipe, NULL); 276 277 dec->pipe->delete_blend_state(dec->pipe, dec->blend); 278 dec->pipe->delete_depth_stencil_alpha_state(dec->pipe, dec->dsa); 279 280 vl_mpeg12_mc_renderer_cleanup(&dec->mc); 281 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 282 vl_idct_cleanup(&dec->idct_y); 283 vl_idct_cleanup(&dec->idct_c); 284 } 285 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves[0]); 286 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves[1]); 287 dec->pipe->delete_vertex_elements_state(dec->pipe, dec->ves[2]); 288 pipe_resource_reference(&dec->quads.buffer, NULL); 289 290 FREE(dec); 291} 292 293static bool 294init_idct_buffer(struct vl_mpeg12_buffer *buffer) 295{ 296 enum pipe_format formats[3]; 297 298 struct pipe_sampler_view **idct_source_sv, **idct_intermediate_sv; 299 struct pipe_surface **idct_surfaces; 300 301 struct vl_mpeg12_decoder *dec; 302 303 unsigned i; 304 305 assert(buffer); 306 307 dec = (struct vl_mpeg12_decoder*)buffer->base.decoder; 308 309 formats[0] = formats[1] = formats[2] = dec->idct_source_format; 310 buffer->idct_source = vl_video_buffer_init(dec->base.context, dec->pipe, 311 dec->base.width / 4, dec->base.height, 1, 312 dec->base.chroma_format, 313 formats, PIPE_USAGE_STREAM); 314 if (!buffer->idct_source) 315 goto error_source; 316 317 formats[0] = formats[1] = formats[2] = dec->idct_intermediate_format; 318 buffer->idct_intermediate = vl_video_buffer_init(dec->base.context, dec->pipe, 319 dec->base.width / dec->nr_of_idct_render_targets, 320 dec->base.height / 4, dec->nr_of_idct_render_targets, 321 dec->base.chroma_format, 322 formats, PIPE_USAGE_STATIC); 323 324 if (!buffer->idct_intermediate) 325 goto error_intermediate; 326 327 idct_source_sv = buffer->idct_source->get_sampler_views(buffer->idct_source); 328 if (!idct_source_sv) 329 goto error_source_sv; 330 331 idct_intermediate_sv = buffer->idct_intermediate->get_sampler_views(buffer->idct_intermediate); 332 if (!idct_intermediate_sv) 333 goto error_intermediate_sv; 334 335 idct_surfaces = buffer->mc_source->get_surfaces(buffer->mc_source); 336 if (!idct_surfaces) 337 goto error_surfaces; 338 339 for (i = 0; i < 3; ++i) 340 if (!vl_idct_init_buffer(i == 0 ? &dec->idct_y : &dec->idct_c, 341 &buffer->idct[i], idct_source_sv[i], 342 idct_intermediate_sv[i], idct_surfaces[i])) 343 goto error_plane; 344 345 return true; 346 347error_plane: 348 for (; i > 0; --i) 349 vl_idct_cleanup_buffer(i == 1 ? &dec->idct_c : &dec->idct_y, &buffer->idct[i - 1]); 350 351error_surfaces: 352error_intermediate_sv: 353error_source_sv: 354 buffer->idct_intermediate->destroy(buffer->idct_intermediate); 355 356error_intermediate: 357 buffer->idct_source->destroy(buffer->idct_source); 358 359error_source: 360 return false; 361} 362 363static struct pipe_video_decode_buffer * 364vl_mpeg12_create_buffer(struct pipe_video_decoder *decoder) 365{ 366 enum pipe_format formats[3]; 367 368 struct vl_mpeg12_decoder *dec = (struct vl_mpeg12_decoder*)decoder; 369 struct vl_mpeg12_buffer *buffer; 370 371 struct pipe_sampler_view **mc_source_sv; 372 373 assert(dec); 374 375 buffer = CALLOC_STRUCT(vl_mpeg12_buffer); 376 if (buffer == NULL) 377 return NULL; 378 379 buffer->base.decoder = decoder; 380 buffer->base.destroy = vl_mpeg12_buffer_destroy; 381 buffer->base.map = vl_mpeg12_buffer_map; 382 buffer->base.add_macroblocks = vl_mpeg12_buffer_add_macroblocks; 383 buffer->base.unmap = vl_mpeg12_buffer_unmap; 384 385 buffer->vertex_bufs.individual.quad.stride = dec->quads.stride; 386 buffer->vertex_bufs.individual.quad.buffer_offset = dec->quads.buffer_offset; 387 pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, dec->quads.buffer); 388 389 buffer->vertex_bufs.individual.stream = vl_vb_init(&buffer->vertex_stream, dec->pipe, 390 dec->base.width / MACROBLOCK_WIDTH * 391 dec->base.height / MACROBLOCK_HEIGHT); 392 if (!buffer->vertex_bufs.individual.stream.buffer) 393 goto error_vertex_stream; 394 395 formats[0] = formats[1] = formats[2] =dec->mc_source_format; 396 buffer->mc_source = vl_video_buffer_init(dec->base.context, dec->pipe, 397 dec->base.width, dec->base.height, 1, 398 dec->base.chroma_format, 399 formats, PIPE_USAGE_STATIC); 400 401 if (!buffer->mc_source) 402 goto error_mc_source; 403 404 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 405 if (!init_idct_buffer(buffer)) 406 goto error_idct; 407 408 mc_source_sv = buffer->mc_source->get_sampler_views(buffer->mc_source); 409 if (!mc_source_sv) 410 goto error_mc_source_sv; 411 412 if(!vl_mpeg12_mc_init_buffer(&dec->mc, &buffer->mc[0], mc_source_sv[0])) 413 goto error_mc_y; 414 415 if(!vl_mpeg12_mc_init_buffer(&dec->mc, &buffer->mc[1], mc_source_sv[1])) 416 goto error_mc_cb; 417 418 if(!vl_mpeg12_mc_init_buffer(&dec->mc, &buffer->mc[2], mc_source_sv[2])) 419 goto error_mc_cr; 420 421 return &buffer->base; 422 423error_mc_cr: 424 vl_mpeg12_mc_cleanup_buffer(&buffer->mc[1]); 425 426error_mc_cb: 427 vl_mpeg12_mc_cleanup_buffer(&buffer->mc[0]); 428 429error_mc_y: 430error_mc_source_sv: 431 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 432 cleanup_idct_buffer(buffer); 433 434error_idct: 435 buffer->mc_source->destroy(buffer->mc_source); 436 437error_mc_source: 438 vl_vb_cleanup(&buffer->vertex_stream); 439 440error_vertex_stream: 441 FREE(buffer); 442 return NULL; 443} 444 445static void 446vl_mpeg12_decoder_flush_buffer(struct pipe_video_decode_buffer *buffer, 447 struct pipe_video_buffer *refs[2], 448 struct pipe_video_buffer *dst, 449 struct pipe_fence_handle **fence) 450{ 451 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer; 452 struct vl_mpeg12_decoder *dec; 453 454 struct pipe_sampler_view **sv_past; 455 struct pipe_sampler_view **sv_future; 456 struct pipe_surface **surfaces; 457 458 unsigned ne_start, ne_num, e_start, e_num; 459 unsigned i; 460 461 assert(buf); 462 463 dec = (struct vl_mpeg12_decoder *)buf->base.decoder; 464 assert(dec); 465 466 sv_past = refs[0] ? refs[0]->get_sampler_views(refs[0]) : NULL; 467 sv_future = refs[1] ? refs[1]->get_sampler_views(refs[1]) : NULL; 468 469 surfaces = dst->get_surfaces(dst); 470 471 vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num); 472 473 dec->pipe->set_vertex_buffers(dec->pipe, 2, buf->vertex_bufs.all); 474 475 for (i = 0; i < VL_MAX_PLANES; ++i) { 476 struct pipe_sampler_view *sv_refs[2]; 477 478 dec->pipe->bind_blend_state(dec->pipe, dec->blend); 479 dec->pipe->bind_vertex_elements_state(dec->pipe, dec->ves[i]); 480 481 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 482 vl_idct_flush(i == 0 ? &dec->idct_y : &dec->idct_c, &buf->idct[i], ne_num); 483 484 sv_refs[0] = sv_past ? sv_past[i] : NULL; 485 sv_refs[1] = sv_future ? sv_future[i] : NULL; 486 487 vl_mpeg12_mc_renderer_flush(&dec->mc, &buf->mc[i], surfaces[i], sv_refs, 488 ne_start, ne_num, e_start, e_num, fence); 489 } 490} 491 492static void 493vl_mpeg12_decoder_clear_buffer(struct pipe_video_decode_buffer *buffer) 494{ 495 struct vl_mpeg12_buffer *buf = (struct vl_mpeg12_buffer *)buffer; 496 unsigned ne_start, ne_num, e_start, e_num; 497 498 assert(buf); 499 500 vl_vb_restart(&buf->vertex_stream, &ne_start, &ne_num, &e_start, &e_num); 501} 502 503static bool 504init_pipe_state(struct vl_mpeg12_decoder *dec) 505{ 506 struct pipe_blend_state blend; 507 struct pipe_depth_stencil_alpha_state dsa; 508 unsigned i; 509 510 assert(dec); 511 512 memset(&blend, 0, sizeof blend); 513 514 blend.independent_blend_enable = 0; 515 blend.rt[0].blend_enable = 0; 516 blend.rt[0].rgb_func = PIPE_BLEND_ADD; 517 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE; 518 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE; 519 blend.rt[0].alpha_func = PIPE_BLEND_ADD; 520 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE; 521 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE; 522 blend.logicop_enable = 0; 523 blend.logicop_func = PIPE_LOGICOP_CLEAR; 524 /* Needed to allow color writes to FB, even if blending disabled */ 525 blend.rt[0].colormask = PIPE_MASK_RGBA; 526 blend.dither = 0; 527 dec->blend = dec->pipe->create_blend_state(dec->pipe, &blend); 528 529 memset(&dsa, 0, sizeof dsa); 530 dsa.depth.enabled = 0; 531 dsa.depth.writemask = 0; 532 dsa.depth.func = PIPE_FUNC_ALWAYS; 533 for (i = 0; i < 2; ++i) { 534 dsa.stencil[i].enabled = 0; 535 dsa.stencil[i].func = PIPE_FUNC_ALWAYS; 536 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP; 537 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP; 538 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP; 539 dsa.stencil[i].valuemask = 0; 540 dsa.stencil[i].writemask = 0; 541 } 542 dsa.alpha.enabled = 0; 543 dsa.alpha.func = PIPE_FUNC_ALWAYS; 544 dsa.alpha.ref_value = 0; 545 dec->dsa = dec->pipe->create_depth_stencil_alpha_state(dec->pipe, &dsa); 546 dec->pipe->bind_depth_stencil_alpha_state(dec->pipe, dec->dsa); 547 548 return true; 549} 550 551static enum pipe_format 552find_first_supported_format(struct vl_mpeg12_decoder *dec, 553 const enum pipe_format formats[], 554 unsigned num_formats, 555 enum pipe_texture_target target) 556{ 557 struct pipe_screen *screen; 558 unsigned i; 559 560 assert(dec); 561 562 screen = dec->pipe->screen; 563 564 for (i = 0; i < num_formats; ++i) 565 if (screen->is_format_supported(dec->pipe->screen, formats[i], target, 1, 566 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET)) 567 return formats[i]; 568 569 return PIPE_FORMAT_NONE; 570} 571 572static bool 573init_idct(struct vl_mpeg12_decoder *dec, unsigned buffer_width, unsigned buffer_height) 574{ 575 unsigned chroma_width, chroma_height, chroma_blocks_x, chroma_blocks_y; 576 struct pipe_sampler_view *matrix, *transpose; 577 float matrix_scale, transpose_scale; 578 579 dec->nr_of_idct_render_targets = dec->pipe->screen->get_param(dec->pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS); 580 581 // more than 4 render targets usually doesn't makes any seens 582 dec->nr_of_idct_render_targets = MIN2(dec->nr_of_idct_render_targets, 4); 583 584 dec->idct_source_format = find_first_supported_format(dec, const_idct_source_formats, 585 num_idct_source_formats, PIPE_TEXTURE_2D); 586 587 if (dec->idct_source_format == PIPE_FORMAT_NONE) 588 return false; 589 590 dec->idct_intermediate_format = find_first_supported_format(dec, const_idct_intermediate_formats, 591 num_idct_intermediate_formats, PIPE_TEXTURE_3D); 592 593 if (dec->idct_intermediate_format == PIPE_FORMAT_NONE) 594 return false; 595 596 switch (dec->idct_source_format) { 597 case PIPE_FORMAT_R16G16B16A16_SSCALED: 598 matrix_scale = SCALE_FACTOR_SSCALED; 599 break; 600 601 case PIPE_FORMAT_R16G16B16A16_SNORM: 602 matrix_scale = SCALE_FACTOR_SNORM; 603 break; 604 605 default: 606 assert(0); 607 return false; 608 } 609 610 if (dec->idct_intermediate_format == PIPE_FORMAT_R16G16B16A16_FLOAT || 611 dec->idct_intermediate_format == PIPE_FORMAT_R32G32B32A32_FLOAT) 612 transpose_scale = 1.0f; 613 else 614 transpose_scale = matrix_scale = sqrt(matrix_scale); 615 616 if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED) 617 transpose_scale /= SCALE_FACTOR_SSCALED; 618 619 if (!(matrix = vl_idct_upload_matrix(dec->pipe, matrix_scale))) 620 goto error_matrix; 621 622 if (matrix_scale != transpose_scale) { 623 if (!(transpose = vl_idct_upload_matrix(dec->pipe, transpose_scale))) 624 goto error_transpose; 625 } else 626 pipe_sampler_view_reference(&transpose, matrix); 627 628 if (!vl_idct_init(&dec->idct_y, dec->pipe, buffer_width, buffer_height, 629 2, 2, dec->nr_of_idct_render_targets, matrix, transpose)) 630 goto error_y; 631 632 if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) { 633 chroma_width = buffer_width / 2; 634 chroma_height = buffer_height / 2; 635 chroma_blocks_x = 1; 636 chroma_blocks_y = 1; 637 } else if (dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) { 638 chroma_width = buffer_width; 639 chroma_height = buffer_height / 2; 640 chroma_blocks_x = 2; 641 chroma_blocks_y = 1; 642 } else { 643 chroma_width = buffer_width; 644 chroma_height = buffer_height; 645 chroma_blocks_x = 2; 646 chroma_blocks_y = 2; 647 } 648 649 if(!vl_idct_init(&dec->idct_c, dec->pipe, chroma_width, chroma_height, 650 chroma_blocks_x, chroma_blocks_y, 651 dec->nr_of_idct_render_targets, matrix, transpose)) 652 goto error_c; 653 654 pipe_sampler_view_reference(&matrix, NULL); 655 pipe_sampler_view_reference(&transpose, NULL); 656 return true; 657 658error_c: 659 vl_idct_cleanup(&dec->idct_y); 660 661error_y: 662 pipe_sampler_view_reference(&transpose, NULL); 663 664error_transpose: 665 pipe_sampler_view_reference(&matrix, NULL); 666 667error_matrix: 668 return false; 669} 670 671struct pipe_video_decoder * 672vl_create_mpeg12_decoder(struct pipe_video_context *context, 673 struct pipe_context *pipe, 674 enum pipe_video_profile profile, 675 enum pipe_video_entrypoint entrypoint, 676 enum pipe_video_chroma_format chroma_format, 677 unsigned width, unsigned height) 678{ 679 struct vl_mpeg12_decoder *dec; 680 float mc_scale; 681 unsigned i; 682 683 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12); 684 685 dec = CALLOC_STRUCT(vl_mpeg12_decoder); 686 687 if (!dec) 688 return NULL; 689 690 dec->base.context = context; 691 dec->base.profile = profile; 692 dec->base.entrypoint = entrypoint; 693 dec->base.chroma_format = chroma_format; 694 dec->base.width = width; 695 dec->base.height = height; 696 697 dec->base.destroy = vl_mpeg12_destroy; 698 dec->base.create_buffer = vl_mpeg12_create_buffer; 699 dec->base.flush_buffer = vl_mpeg12_decoder_flush_buffer; 700 dec->base.clear_buffer = vl_mpeg12_decoder_clear_buffer; 701 702 dec->pipe = pipe; 703 704 dec->quads = vl_vb_upload_quads(dec->pipe, 2, 2); 705 for (i = 0; i < VL_MAX_PLANES; ++i) 706 dec->ves[i] = vl_vb_get_elems_state(dec->pipe, i); 707 708 dec->base.width = align(width, MACROBLOCK_WIDTH); 709 dec->base.height = align(height, MACROBLOCK_HEIGHT); 710 711 /* TODO: Implement 422, 444 */ 712 assert(dec->base.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420); 713 dec->empty_block_mask = &const_empty_block_mask_420; 714 715 dec->mc_source_format = find_first_supported_format(dec, const_mc_source_formats, 716 num_mc_source_formats, PIPE_TEXTURE_3D); 717 718 if (dec->mc_source_format == PIPE_FORMAT_NONE) 719 return NULL; 720 721 if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 722 if (!init_idct(dec, dec->base.width, dec->base.height)) 723 goto error_idct; 724 if (dec->mc_source_format == PIPE_FORMAT_R16_SSCALED) 725 mc_scale = SCALE_FACTOR_SSCALED; 726 else 727 mc_scale = 1.0f; 728 } else { 729 switch (dec->mc_source_format) { 730 case PIPE_FORMAT_R16_SNORM: 731 mc_scale = SCALE_FACTOR_SNORM; 732 break; 733 734 case PIPE_FORMAT_R16_SSCALED: 735 mc_scale = SCALE_FACTOR_SSCALED; 736 break; 737 738 default: 739 assert(0); 740 return NULL; 741 } 742 } 743 744 if (!vl_mpeg12_mc_renderer_init(&dec->mc, dec->pipe, dec->base.width, dec->base.height, mc_scale)) 745 goto error_mc; 746 747 if (!init_pipe_state(dec)) 748 goto error_pipe_state; 749 750 return &dec->base; 751 752error_pipe_state: 753 vl_mpeg12_mc_renderer_cleanup(&dec->mc); 754 755error_mc: 756 if (entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) { 757 vl_idct_cleanup(&dec->idct_y); 758 vl_idct_cleanup(&dec->idct_c); 759 } 760 761error_idct: 762 FREE(dec); 763 return NULL; 764} 765