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