1/* 2 * Copyright 2011 Maarten Lankhorst 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 */ 22 23#include "vl/vl_decoder.h" 24#include "vl/vl_video_buffer.h" 25 26#include "nouveau_screen.h" 27#include "nouveau_context.h" 28#include "nouveau_video.h" 29 30#include "nouveau/nouveau_buffer.h" 31#include "util/u_video.h" 32#include "util/u_format.h" 33#include "util/u_sampler.h" 34 35static int 36nouveau_vpe_init(struct nouveau_decoder *dec) { 37 int ret; 38 if (dec->cmds) 39 return 0; 40 ret = nouveau_bo_map(dec->cmd_bo, NOUVEAU_BO_RDWR, dec->client); 41 if (ret) { 42 debug_printf("Mapping cmd bo: %s\n", strerror(-ret)); 43 return ret; 44 } 45 ret = nouveau_bo_map(dec->data_bo, NOUVEAU_BO_RDWR, dec->client); 46 if (ret) { 47 debug_printf("Mapping data bo: %s\n", strerror(-ret)); 48 return ret; 49 } 50 dec->cmds = dec->cmd_bo->map; 51 dec->data = dec->data_bo->map; 52 return ret; 53} 54 55static void 56nouveau_vpe_synch(struct nouveau_decoder *dec) { 57 struct nouveau_pushbuf *push = dec->push; 58#if 0 59 if (dec->fence_map) { 60 BEGIN_NV04(push, NV84_MPEG(QUERY_COUNTER), 1); 61 PUSH_DATA (push, ++dec->fence_seq); 62 PUSH_KICK (push); 63 while (dec->fence_map[0] != dec->fence_seq) 64 usleep(1000); 65 } else 66#endif 67 PUSH_KICK(push); 68} 69 70static void 71nouveau_vpe_fini(struct nouveau_decoder *dec) { 72 struct nouveau_pushbuf *push = dec->push; 73 if (!dec->cmds) 74 return; 75 76 nouveau_pushbuf_space(push, 8, 2, 0); 77 nouveau_bufctx_reset(dec->bufctx, NV31_VIDEO_BIND_CMD); 78 79#define BCTX_ARGS dec->bufctx, NV31_VIDEO_BIND_CMD, NOUVEAU_BO_RD 80 81 BEGIN_NV04(push, NV31_MPEG(CMD_OFFSET), 2); 82 PUSH_MTHDl(push, NV31_MPEG(CMD_OFFSET), dec->cmd_bo, 0, BCTX_ARGS); 83 PUSH_DATA (push, dec->ofs * 4); 84 85 BEGIN_NV04(push, NV31_MPEG(DATA_OFFSET), 2); 86 PUSH_MTHDl(push, NV31_MPEG(DATA_OFFSET), dec->data_bo, 0, BCTX_ARGS); 87 PUSH_DATA (push, dec->data_pos * 4); 88 89#undef BCTX_ARGS 90 91 if (unlikely(nouveau_pushbuf_validate(dec->push))) 92 return; 93 94 BEGIN_NV04(push, NV31_MPEG(EXEC), 1); 95 PUSH_DATA (push, 1); 96 97 nouveau_vpe_synch(dec); 98 dec->ofs = dec->data_pos = dec->num_surfaces = 0; 99 dec->cmds = dec->data = NULL; 100 dec->current = dec->future = dec->past = 8; 101} 102 103static INLINE void 104nouveau_vpe_mb_dct_blocks(struct nouveau_decoder *dec, const struct pipe_mpeg12_macroblock *mb) 105{ 106 int cbb; 107 unsigned cbp = mb->coded_block_pattern; 108 short *db = mb->blocks; 109 for (cbb = 0x20; cbb > 0; cbb >>= 1) { 110 if (cbb & cbp) { 111 static const int lookup[64] = { 112 0, 1, 8,16, 9, 2, 3,10, 113 17,24,32,25,18,11, 4, 5, 114 12,19,26,33,40,48,41,34, 115 27,20,13, 6, 7,14,21,28, 116 35,42,49,56,57,50,43,36, 117 29,22,15,23,30,37,44,51, 118 58,59,52,45,38,31,39,46, 119 53,60,61,54,47,55,62,63 120 }; 121 int i, j = 0, found = 0; 122 for (i = 0; i < 64; ++i) { 123 if (!db[lookup[i]]) { j += 2; continue; } 124 dec->data[dec->data_pos++] = (db[lookup[i]] << 16) | j; 125 j = 0; 126 found = 1; 127 } 128 if (found) 129 dec->data[dec->data_pos - 1] |= 1; 130 else 131 dec->data[dec->data_pos++] = 1; 132 db += 64; 133 } else if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) { 134 dec->data[dec->data_pos++] = 1; 135 } 136 } 137} 138 139static INLINE void 140nouveau_vpe_mb_data_blocks(struct nouveau_decoder *dec, const struct pipe_mpeg12_macroblock *mb) 141{ 142 int cbb; 143 unsigned cbp = mb->coded_block_pattern; 144 short *db = mb->blocks; 145 for (cbb = 0x20; cbb > 0; cbb >>= 1) { 146 if (cbb & cbp) { 147 memcpy(&dec->data[dec->data_pos], db, 128); 148 dec->data_pos += 32; 149 db += 64; 150 } else if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) { 151 memset(&dec->data[dec->data_pos], 0, 128); 152 dec->data_pos += 32; 153 } 154 } 155} 156 157static INLINE void 158nouveau_vpe_mb_dct_header(struct nouveau_decoder *dec, 159 const struct pipe_mpeg12_macroblock *mb, 160 bool luma) 161{ 162 unsigned base_dct, cbp; 163 bool intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA; 164 unsigned x = mb->x * 16; 165 unsigned y = luma ? mb->y * 16 : mb->y * 8; 166 167 /* Setup the base dct header */ 168 base_dct = dec->current << NV17_MPEG_CMD_CHROMA_MB_HEADER_SURFACE__SHIFT; 169 base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_RUN_SINGLE; 170 171 if (!(mb->x & 1)) 172 base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_X_COORD_EVEN; 173 if (intra) 174 cbp = 0x3f; 175 else 176 cbp = mb->coded_block_pattern; 177 178 if (dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME) { 179 base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_TYPE_FRAME; 180 if (luma && mb->macroblock_modes.bits.dct_type == PIPE_MPEG12_DCT_TYPE_FIELD) 181 base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_FRAME_DCT_TYPE_FIELD; 182 } else { 183 if (dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FIELD_BOTTOM) 184 base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_FIELD_BOTTOM; 185 if (!intra) 186 y *= 2; 187 } 188 189 if (luma) { 190 base_dct |= NV17_MPEG_CMD_LUMA_MB_HEADER_OP_LUMA_MB_HEADER; 191 base_dct |= (cbp >> 2) << NV17_MPEG_CMD_LUMA_MB_HEADER_CBP__SHIFT; 192 } else { 193 base_dct |= NV17_MPEG_CMD_CHROMA_MB_HEADER_OP_CHROMA_MB_HEADER; 194 base_dct |= (cbp & 3) << NV17_MPEG_CMD_CHROMA_MB_HEADER_CBP__SHIFT; 195 } 196 nouveau_vpe_write(dec, base_dct); 197 nouveau_vpe_write(dec, NV17_MPEG_CMD_MB_COORDS_OP_MB_COORDS | 198 x | (y << NV17_MPEG_CMD_MB_COORDS_Y__SHIFT)); 199} 200 201static INLINE unsigned int 202nouveau_vpe_mb_mv_flags(bool luma, int mv_h, int mv_v, bool forward, bool first, bool vert) 203{ 204 unsigned mc_header = 0; 205 if (luma) 206 mc_header |= NV17_MPEG_CMD_LUMA_MV_HEADER_OP_LUMA_MV_HEADER; 207 else 208 mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_OP_CHROMA_MV_HEADER; 209 if (mv_h & 1) 210 mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_X_HALF; 211 if (mv_v & 1) 212 mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_Y_HALF; 213 if (!forward) 214 mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_DIRECTION_BACKWARD; 215 if (!first) 216 mc_header |= NV17_MPEG_CMD_CHROMA_MV_HEADER_IDX; 217 if (vert) 218 mc_header |= NV17_MPEG_CMD_LUMA_MV_HEADER_FIELD_BOTTOM; 219 return mc_header; 220} 221 222static unsigned pos(int pos, int mov, int max) { 223 int ret = pos + mov; 224 if (pos < 0) 225 return 0; 226 if (pos >= max) 227 return max-1; 228 return ret; 229} 230 231/* because we want -1 / 2 = -1 */ 232static int div_down(int val, int mult) { 233 val &= ~(mult - 1); 234 return val / mult; 235} 236 237static int div_up(int val, int mult) { 238 val += mult - 1; 239 return val / mult; 240} 241 242static INLINE void 243nouveau_vpe_mb_mv(struct nouveau_decoder *dec, unsigned mc_header, 244 bool luma, bool frame, bool forward, bool vert, 245 int x, int y, const short motions[2], 246 unsigned surface, bool first) 247{ 248 unsigned mc_vector; 249 int mv_horizontal = motions[0]; 250 int mv_vertical = motions[1]; 251 int mv2 = mc_header & NV17_MPEG_CMD_CHROMA_MV_HEADER_COUNT_2; 252 unsigned width = dec->base.width; 253 unsigned height = dec->base.height; 254 if (mv2) 255 mv_vertical = div_down(mv_vertical, 2); 256 assert(frame); // Untested for non-frames 257 if (!frame) 258 height *= 2; 259 260 mc_header |= surface << NV17_MPEG_CMD_CHROMA_MV_HEADER_SURFACE__SHIFT; 261 if (!luma) { 262 mv_vertical = div_up(mv_vertical, 2); 263 mv_horizontal = div_up(mv_horizontal, 2); 264 height /= 2; 265 } 266 mc_header |= nouveau_vpe_mb_mv_flags(luma, mv_horizontal, mv_vertical, forward, first, vert); 267 nouveau_vpe_write(dec, mc_header); 268 269 mc_vector = NV17_MPEG_CMD_MV_COORDS_OP_MV_COORDS; 270 if (luma) 271 mc_vector |= pos(x, div_down(mv_horizontal, 2), width); 272 else 273 mc_vector |= pos(x, mv_horizontal & ~1, width); 274 if (!mv2) 275 mc_vector |= pos(y, div_down(mv_vertical, 2), height) << NV17_MPEG_CMD_MV_COORDS_Y__SHIFT; 276 else 277 mc_vector |= pos(y, mv_vertical & ~1, height) << NV17_MPEG_CMD_MV_COORDS_Y__SHIFT; 278 nouveau_vpe_write(dec, mc_vector); 279} 280 281static void 282nouveau_vpe_mb_mv_header(struct nouveau_decoder *dec, 283 const struct pipe_mpeg12_macroblock *mb, 284 bool luma) 285{ 286 bool frame = dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME; 287 unsigned base; 288 bool forward, backward; 289 int y, y2, x = mb->x * 16; 290 if (luma) 291 y = mb->y * (frame ? 16 : 32); 292 else 293 y = mb->y * (frame ? 8 : 16); 294 if (frame) 295 y2 = y; 296 else 297 y2 = y + (luma ? 16 : 8); 298 299 forward = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_FORWARD; 300 backward = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD; 301 assert(!forward || dec->past < 8); 302 assert(!backward || dec->future < 8); 303 if (frame) { 304 switch (mb->macroblock_modes.bits.frame_motion_type) { 305 case PIPE_MPEG12_MO_TYPE_FRAME: goto mv1; 306 case PIPE_MPEG12_MO_TYPE_FIELD: goto mv2; 307 case PIPE_MPEG12_MO_TYPE_DUAL_PRIME: { 308 base = NV17_MPEG_CMD_CHROMA_MV_HEADER_COUNT_2; 309 if (forward) { 310 nouveau_vpe_mb_mv(dec, base, luma, frame, TRUE, FALSE, 311 x, y, mb->PMV[0][0], dec->past, TRUE); 312 nouveau_vpe_mb_mv(dec, base, luma, frame, TRUE, TRUE, 313 x, y2, mb->PMV[0][0], dec->past, FALSE); 314 } 315 if (backward && forward) { 316 nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, TRUE, 317 x, y, mb->PMV[1][0], dec->future, TRUE); 318 nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, FALSE, 319 x, y2, mb->PMV[1][1], dec->future, FALSE); 320 } else assert(!backward); 321 break; 322 } 323 default: assert(0); 324 } 325 } else { 326 switch (mb->macroblock_modes.bits.field_motion_type) { 327 case PIPE_MPEG12_MO_TYPE_FIELD: goto mv1; 328 case PIPE_MPEG12_MO_TYPE_16x8: goto mv2; 329 case PIPE_MPEG12_MO_TYPE_DUAL_PRIME: { 330 base = NV17_MPEG_CMD_CHROMA_MV_HEADER_MV_SPLIT_HALF_MB; 331 if (frame) 332 base |= NV17_MPEG_CMD_CHROMA_MV_HEADER_TYPE_FRAME; 333 if (forward) 334 nouveau_vpe_mb_mv(dec, base, luma, frame, TRUE, 335 dec->picture_structure != PIPE_MPEG12_PICTURE_STRUCTURE_FIELD_TOP, 336 x, y, mb->PMV[0][0], dec->past, TRUE); 337 if (backward && forward) 338 nouveau_vpe_mb_mv(dec, base, luma, frame, FALSE, 339 dec->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FIELD_TOP, 340 x, y, mb->PMV[0][1], dec->future, TRUE); 341 else assert(!backward); 342 break; 343 } 344 default: assert(0); 345 } 346 } 347 return; 348 349mv1: 350 base = NV17_MPEG_CMD_CHROMA_MV_HEADER_MV_SPLIT_HALF_MB; 351 if (frame) 352 base |= NV17_MPEG_CMD_CHROMA_MV_HEADER_TYPE_FRAME; 353 /* frame 16x16 */ 354 if (forward) 355 nouveau_vpe_mb_mv(dec, base, luma, frame, TRUE, FALSE, 356 x, y, mb->PMV[0][0], dec->past, TRUE); 357 if (backward) 358 nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, FALSE, 359 x, y, mb->PMV[0][1], dec->future, TRUE); 360 return; 361 362mv2: 363 base = NV17_MPEG_CMD_CHROMA_MV_HEADER_COUNT_2; 364 if (!frame) 365 base |= NV17_MPEG_CMD_CHROMA_MV_HEADER_MV_SPLIT_HALF_MB; 366 if (forward) { 367 nouveau_vpe_mb_mv(dec, base, luma, frame, TRUE, 368 mb->motion_vertical_field_select & PIPE_MPEG12_FS_FIRST_FORWARD, 369 x, y, mb->PMV[0][0], dec->past, TRUE); 370 nouveau_vpe_mb_mv(dec, base, luma, frame, TRUE, 371 mb->motion_vertical_field_select & PIPE_MPEG12_FS_SECOND_FORWARD, 372 x, y2, mb->PMV[1][0], dec->past, FALSE); 373 } 374 if (backward) { 375 nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, 376 mb->motion_vertical_field_select & PIPE_MPEG12_FS_FIRST_BACKWARD, 377 x, y, mb->PMV[0][1], dec->future, TRUE); 378 nouveau_vpe_mb_mv(dec, base, luma, frame, !forward, 379 mb->motion_vertical_field_select & PIPE_MPEG12_FS_SECOND_BACKWARD, 380 x, y2, mb->PMV[1][1], dec->future, FALSE); 381 } 382} 383 384static unsigned 385nouveau_decoder_surface_index(struct nouveau_decoder *dec, 386 struct pipe_video_buffer *buffer) 387{ 388 struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer; 389 struct nouveau_pushbuf *push = dec->push; 390 struct nouveau_bo *bo_y = nv04_resource(buf->resources[0])->bo; 391 struct nouveau_bo *bo_c = nv04_resource(buf->resources[1])->bo; 392 393 unsigned i; 394 395 if (!buf) 396 return 8; 397 for (i = 0; i < dec->num_surfaces; ++i) { 398 if (dec->surfaces[i] == buf) 399 return i; 400 } 401 assert(i < 8); 402 dec->surfaces[i] = buf; 403 dec->num_surfaces++; 404 405 nouveau_bufctx_reset(dec->bufctx, NV31_VIDEO_BIND_IMG(i)); 406 407#define BCTX_ARGS dec->bufctx, NV31_VIDEO_BIND_IMG(i), NOUVEAU_BO_RDWR 408 BEGIN_NV04(push, NV31_MPEG(IMAGE_Y_OFFSET(i)), 2); 409 PUSH_MTHDl(push, NV31_MPEG(IMAGE_Y_OFFSET(i)), bo_y, 0, BCTX_ARGS); 410 PUSH_MTHDl(push, NV31_MPEG(IMAGE_C_OFFSET(i)), bo_c, 0, BCTX_ARGS); 411#undef BCTX_ARGS 412 413 return i; 414} 415 416static void 417nouveau_decoder_begin_frame(struct pipe_video_decoder *decoder, 418 struct pipe_video_buffer *target, 419 struct pipe_picture_desc *picture) 420{ 421} 422 423static void 424nouveau_decoder_decode_macroblock(struct pipe_video_decoder *decoder, 425 struct pipe_video_buffer *target, 426 struct pipe_picture_desc *picture, 427 const struct pipe_macroblock *pipe_mb, 428 unsigned num_macroblocks) 429{ 430 struct nouveau_decoder *dec = (struct nouveau_decoder *)decoder; 431 struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc*)picture; 432 const struct pipe_mpeg12_macroblock *mb; 433 unsigned i; 434 assert(target->width == decoder->width); 435 assert(target->height == decoder->height); 436 437 dec->current = nouveau_decoder_surface_index(dec, target); 438 assert(dec->current < 8); 439 dec->picture_structure = desc->picture_structure; 440 if (desc->ref[1]) 441 dec->future = nouveau_decoder_surface_index(dec, desc->ref[1]); 442 if (desc->ref[0]) 443 dec->past = nouveau_decoder_surface_index(dec, desc->ref[0]); 444 445 if (nouveau_vpe_init(dec)) return; 446 mb = (const struct pipe_mpeg12_macroblock *)pipe_mb; 447 for (i = 0; i < num_macroblocks; ++i, mb++) { 448 if (mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) { 449 nouveau_vpe_mb_dct_header(dec, mb, TRUE); 450 nouveau_vpe_mb_dct_header(dec, mb, FALSE); 451 } else { 452 nouveau_vpe_mb_mv_header(dec, mb, TRUE); 453 nouveau_vpe_mb_dct_header(dec, mb, TRUE); 454 455 nouveau_vpe_mb_mv_header(dec, mb, FALSE); 456 nouveau_vpe_mb_dct_header(dec, mb, FALSE); 457 } 458 if (dec->base.entrypoint <= PIPE_VIDEO_ENTRYPOINT_IDCT) 459 nouveau_vpe_mb_dct_blocks(dec, mb); 460 else 461 nouveau_vpe_mb_data_blocks(dec, mb); 462 } 463} 464 465static void 466nouveau_decoder_end_frame(struct pipe_video_decoder *decoder, 467 struct pipe_video_buffer *target, 468 struct pipe_picture_desc *picture) 469{ 470} 471 472static void 473nouveau_decoder_flush(struct pipe_video_decoder *decoder) 474{ 475 struct nouveau_decoder *dec = (struct nouveau_decoder *)decoder; 476 if (dec->ofs) 477 nouveau_vpe_fini(dec); 478} 479 480static void 481nouveau_decoder_destroy(struct pipe_video_decoder *decoder) 482{ 483 struct nouveau_decoder *dec = (struct nouveau_decoder*)decoder; 484 485 if (dec->data_bo) 486 nouveau_bo_ref(NULL, &dec->data_bo); 487 if (dec->cmd_bo) 488 nouveau_bo_ref(NULL, &dec->cmd_bo); 489 if (dec->fence_bo) 490 nouveau_bo_ref(NULL, &dec->fence_bo); 491 492 nouveau_object_del(&dec->mpeg); 493 494 if (dec->bufctx) 495 nouveau_bufctx_del(&dec->bufctx); 496 if (dec->push) 497 nouveau_pushbuf_del(&dec->push); 498 if (dec->client) 499 nouveau_client_del(&dec->client); 500 if (dec->chan) 501 nouveau_object_del(&dec->chan); 502 503 FREE(dec); 504} 505 506static struct pipe_video_decoder * 507nouveau_create_decoder(struct pipe_context *context, 508 struct nouveau_screen *screen, 509 enum pipe_video_profile profile, 510 enum pipe_video_entrypoint entrypoint, 511 enum pipe_video_chroma_format chroma_format, 512 unsigned width, unsigned height, 513 unsigned max_references, bool expect_chunked_decode) 514{ 515 struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 }; 516 struct nouveau_object *mpeg = NULL; 517 struct nouveau_decoder *dec; 518 struct nouveau_pushbuf *push; 519 int ret; 520 bool is8274 = screen->device->chipset > 0x80; 521 522 debug_printf("Acceleration level: %s\n", entrypoint <= PIPE_VIDEO_ENTRYPOINT_BITSTREAM ? "bit": 523 entrypoint == PIPE_VIDEO_ENTRYPOINT_IDCT ? "IDCT" : "MC"); 524 525 if (getenv("XVMC_VL")) 526 goto vl; 527 if (u_reduce_video_profile(profile) != PIPE_VIDEO_CODEC_MPEG12) 528 goto vl; 529 if (screen->device->chipset >= 0x98 && screen->device->chipset != 0xa0) 530 goto vl; 531 532 dec = CALLOC_STRUCT(nouveau_decoder); 533 if (!dec) 534 return NULL; 535 536 ret = nouveau_object_new(&screen->device->object, 0, 537 NOUVEAU_FIFO_CHANNEL_CLASS, 538 &nv04_data, sizeof(nv04_data), &dec->chan); 539 if (ret) 540 goto fail; 541 ret = nouveau_client_new(screen->device, &dec->client); 542 if (ret) 543 goto fail; 544 ret = nouveau_pushbuf_new(dec->client, dec->chan, 2, 4096, 1, &dec->push); 545 if (ret) 546 goto fail; 547 ret = nouveau_bufctx_new(dec->client, NV31_VIDEO_BIND_COUNT, &dec->bufctx); 548 if (ret) 549 goto fail; 550 push = dec->push; 551 552 width = align(width, 64); 553 height = align(height, 64); 554 555 if (is8274) 556 ret = nouveau_object_new(dec->chan, 0xbeef8274, NV84_MPEG_CLASS, NULL, 0, 557 &mpeg); 558 else 559 ret = nouveau_object_new(dec->chan, 0xbeef3174, NV31_MPEG_CLASS, NULL, 0, 560 &mpeg); 561 if (ret < 0) { 562 debug_printf("Creation failed: %s (%i)\n", strerror(-ret), ret); 563 return NULL; 564 } 565 566 dec->mpeg = mpeg; 567 dec->base.context = context; 568 dec->base.profile = profile; 569 dec->base.entrypoint = entrypoint; 570 dec->base.chroma_format = chroma_format; 571 dec->base.width = width; 572 dec->base.height = height; 573 dec->base.max_references = max_references; 574 dec->base.destroy = nouveau_decoder_destroy; 575 dec->base.begin_frame = nouveau_decoder_begin_frame; 576 dec->base.decode_macroblock = nouveau_decoder_decode_macroblock; 577 dec->base.end_frame = nouveau_decoder_end_frame; 578 dec->base.flush = nouveau_decoder_flush; 579 dec->screen = screen; 580 581 ret = nouveau_bo_new(dec->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 582 0, 1024 * 1024, NULL, &dec->cmd_bo); 583 if (ret) 584 goto fail; 585 586 ret = nouveau_bo_new(dec->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 587 0, width * height * 6, NULL, &dec->data_bo); 588 if (ret) 589 goto fail; 590 591 /* we don't need the fence, the kernel sync's for us */ 592#if 0 593 ret = nouveau_bo_new(dec->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 594 0, 4096, NULL, &dec->fence_bo); 595 if (ret) 596 goto fail; 597 nouveau_bo_map(dec->fence_bo, NOUVEAU_BO_RDWR, NULL); 598 dec->fence_map = dec->fence_bo->map; 599 dec->fence_map[0] = 0; 600#endif 601 602 nouveau_pushbuf_bufctx(dec->push, dec->bufctx); 603 nouveau_pushbuf_space(push, 32, 4, 0); 604 605 BEGIN_NV04(push, SUBC_MPEG(NV01_SUBCHAN_OBJECT), 1); 606 PUSH_DATA (push, dec->mpeg->handle); 607 608 BEGIN_NV04(push, NV31_MPEG(DMA_CMD), 1); 609 PUSH_DATA (push, nv04_data.gart); 610 611 BEGIN_NV04(push, NV31_MPEG(DMA_DATA), 1); 612 PUSH_DATA (push, nv04_data.gart); 613 614 BEGIN_NV04(push, NV31_MPEG(DMA_IMAGE), 1); 615 PUSH_DATA (push, nv04_data.vram); 616 617 BEGIN_NV04(push, NV31_MPEG(PITCH), 2); 618 PUSH_DATA (push, width | NV31_MPEG_PITCH_UNK); 619 PUSH_DATA (push, (height << NV31_MPEG_SIZE_H__SHIFT) | width); 620 621 BEGIN_NV04(push, NV31_MPEG(FORMAT), 2); 622 PUSH_DATA (push, 0); 623 switch (entrypoint) { 624 case PIPE_VIDEO_ENTRYPOINT_BITSTREAM: PUSH_DATA (push, 0x100); break; 625 case PIPE_VIDEO_ENTRYPOINT_IDCT: PUSH_DATA (push, 1); break; 626 case PIPE_VIDEO_ENTRYPOINT_MC: PUSH_DATA (push, 0); break; 627 default: assert(0); 628 } 629 630 if (is8274) { 631 BEGIN_NV04(push, NV84_MPEG(DMA_QUERY), 1); 632 PUSH_DATA (push, nv04_data.vram); 633#if 0 634 BEGIN_NV04(push, NV84_MPEG(QUERY_OFFSET), 2); 635 PUSH_DATA (push, dec->fence_bo->offset); 636 PUSH_DATA (push, dec->fence_seq); 637#endif 638 } 639 640 ret = nouveau_vpe_init(dec); 641 if (ret) 642 goto fail; 643 nouveau_vpe_fini(dec); 644 return &dec->base; 645 646fail: 647 nouveau_decoder_destroy(&dec->base); 648 return NULL; 649 650vl: 651 debug_printf("Using g3dvl renderer\n"); 652 return vl_create_decoder(context, profile, entrypoint, 653 chroma_format, width, height, 654 max_references, expect_chunked_decode); 655} 656 657static struct pipe_sampler_view ** 658nouveau_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer) 659{ 660 struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer; 661 struct pipe_sampler_view sv_templ; 662 struct pipe_context *pipe; 663 unsigned i; 664 665 assert(buf); 666 667 pipe = buf->base.context; 668 669 for (i = 0; i < buf->num_planes; ++i ) { 670 if (!buf->sampler_view_planes[i]) { 671 memset(&sv_templ, 0, sizeof(sv_templ)); 672 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format); 673 674 if (util_format_get_nr_components(buf->resources[i]->format) == 1) 675 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_RED; 676 677 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ); 678 if (!buf->sampler_view_planes[i]) 679 goto error; 680 } 681 } 682 683 return buf->sampler_view_planes; 684 685error: 686 for (i = 0; i < buf->num_planes; ++i ) 687 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL); 688 689 return NULL; 690} 691 692static struct pipe_sampler_view ** 693nouveau_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer) 694{ 695 struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer; 696 struct pipe_sampler_view sv_templ; 697 struct pipe_context *pipe; 698 unsigned i, j, component; 699 700 assert(buf); 701 702 pipe = buf->base.context; 703 704 for (component = 0, i = 0; i < buf->num_planes; ++i ) { 705 unsigned nr_components = util_format_get_nr_components(buf->resources[i]->format); 706 707 for (j = 0; j < nr_components; ++j, ++component) { 708 assert(component < VL_NUM_COMPONENTS); 709 710 if (!buf->sampler_view_components[component]) { 711 memset(&sv_templ, 0, sizeof(sv_templ)); 712 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format); 713 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j; 714 sv_templ.swizzle_a = PIPE_SWIZZLE_ONE; 715 buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ); 716 if (!buf->sampler_view_components[component]) 717 goto error; 718 } 719 } 720 } 721 722 return buf->sampler_view_components; 723 724error: 725 for (i = 0; i < 3; ++i ) 726 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 727 728 return NULL; 729} 730 731static struct pipe_surface ** 732nouveau_video_buffer_surfaces(struct pipe_video_buffer *buffer) 733{ 734 struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer; 735 struct pipe_surface surf_templ; 736 struct pipe_context *pipe; 737 unsigned i; 738 739 assert(buf); 740 741 pipe = buf->base.context; 742 743 for (i = 0; i < buf->num_planes; ++i ) { 744 if (!buf->surfaces[i]) { 745 memset(&surf_templ, 0, sizeof(surf_templ)); 746 surf_templ.format = buf->resources[i]->format; 747 surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 748 buf->surfaces[i] = pipe->create_surface(pipe, buf->resources[i], &surf_templ); 749 if (!buf->surfaces[i]) 750 goto error; 751 } 752 } 753 754 return buf->surfaces; 755 756error: 757 for (i = 0; i < buf->num_planes; ++i ) 758 pipe_surface_reference(&buf->surfaces[i], NULL); 759 760 return NULL; 761} 762 763static void 764nouveau_video_buffer_destroy(struct pipe_video_buffer *buffer) 765{ 766 struct nouveau_video_buffer *buf = (struct nouveau_video_buffer *)buffer; 767 unsigned i; 768 769 assert(buf); 770 771 for (i = 0; i < buf->num_planes; ++i) { 772 pipe_surface_reference(&buf->surfaces[i], NULL); 773 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL); 774 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 775 pipe_resource_reference(&buf->resources[i], NULL); 776 } 777 for (;i < 3;++i) 778 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 779 780 FREE(buffer); 781} 782 783static struct pipe_video_buffer * 784nouveau_video_buffer_create(struct pipe_context *pipe, 785 struct nouveau_screen *screen, 786 const struct pipe_video_buffer *templat) 787{ 788 struct nouveau_video_buffer *buffer; 789 struct pipe_resource templ; 790 unsigned width, height; 791 792 /* Only do a linear surface when a hardware decoder is used 793 * hardware decoder is only supported on some chipsets 794 * and it only supports the NV12 format 795 */ 796 if (templat->buffer_format != PIPE_FORMAT_NV12 || getenv("XVMC_VL") || 797 (screen->device->chipset >= 0x98 && screen->device->chipset != 0xa0)) 798 return vl_video_buffer_create(pipe, templat); 799 800 assert(templat->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420); 801 width = align(templat->width, 64); 802 height = align(templat->height, 64); 803 804 buffer = CALLOC_STRUCT(nouveau_video_buffer); 805 if (!buffer) 806 return NULL; 807 808 buffer->base.context = pipe; 809 buffer->base.destroy = nouveau_video_buffer_destroy; 810 buffer->base.get_sampler_view_planes = nouveau_video_buffer_sampler_view_planes; 811 buffer->base.get_sampler_view_components = nouveau_video_buffer_sampler_view_components; 812 buffer->base.get_surfaces = nouveau_video_buffer_surfaces; 813 buffer->base.chroma_format = templat->chroma_format; 814 buffer->base.width = width; 815 buffer->base.height = height; 816 buffer->num_planes = 2; 817 818 memset(&templ, 0, sizeof(templ)); 819 templ.target = PIPE_TEXTURE_2D; 820 templ.format = PIPE_FORMAT_R8_UNORM; 821 templ.width0 = width; 822 templ.height0 = height; 823 templ.depth0 = 1; 824 templ.array_size = 1; 825 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 826 templ.usage = PIPE_USAGE_STATIC; 827 templ.flags = NOUVEAU_RESOURCE_FLAG_LINEAR; 828 829 buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ); 830 if (!buffer->resources[0]) 831 goto error; 832 templ.width0 /= 2; 833 templ.height0 /= 2; 834 templ.format = PIPE_FORMAT_R8G8_UNORM; 835 buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ); 836 if (!buffer->resources[1]) 837 goto error; 838 return &buffer->base; 839 840error: 841 nouveau_video_buffer_destroy(&buffer->base); 842 return NULL; 843} 844 845static int 846nouveau_screen_get_video_param(struct pipe_screen *pscreen, 847 enum pipe_video_profile profile, 848 enum pipe_video_cap param) 849{ 850 switch (param) { 851 case PIPE_VIDEO_CAP_SUPPORTED: 852 return vl_profile_supported(pscreen, profile); 853 case PIPE_VIDEO_CAP_NPOT_TEXTURES: 854 return 1; 855 case PIPE_VIDEO_CAP_MAX_WIDTH: 856 case PIPE_VIDEO_CAP_MAX_HEIGHT: 857 return vl_video_buffer_max_size(pscreen); 858 case PIPE_VIDEO_CAP_PREFERED_FORMAT: 859 return PIPE_FORMAT_NV12; 860 case PIPE_VIDEO_CAP_PREFERS_INTERLACED: 861 return false; 862 case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED: 863 return false; 864 case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE: 865 return true; 866 default: 867 debug_printf("unknown video param: %d\n", param); 868 return 0; 869 } 870} 871 872void 873nouveau_screen_init_vdec(struct nouveau_screen *screen) 874{ 875 screen->base.get_video_param = nouveau_screen_get_video_param; 876 screen->base.is_video_format_supported = vl_video_buffer_is_format_supported; 877} 878 879static struct pipe_video_decoder * 880nouveau_context_create_decoder(struct pipe_context *context, 881 enum pipe_video_profile profile, 882 enum pipe_video_entrypoint entrypoint, 883 enum pipe_video_chroma_format chroma_format, 884 unsigned width, unsigned height, 885 unsigned max_references, bool expect_chunked_decode) 886{ 887 struct nouveau_screen *screen = nouveau_context(context)->screen; 888 return nouveau_create_decoder(context, screen, profile, entrypoint, 889 chroma_format, width, height, 890 max_references, expect_chunked_decode); 891} 892 893static struct pipe_video_buffer * 894nouveau_context_video_buffer_create(struct pipe_context *pipe, 895 const struct pipe_video_buffer *templat) 896{ 897 struct nouveau_screen *screen = nouveau_context(pipe)->screen; 898 return nouveau_video_buffer_create(pipe, screen, templat); 899} 900 901void 902nouveau_context_init_vdec(struct nouveau_context *nv) 903{ 904 nv->pipe.create_video_decoder = nouveau_context_create_decoder; 905 nv->pipe.create_video_buffer = nouveau_context_video_buffer_create; 906} 907