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