Debian Codegraph @

src/gallium/drivers/nouveau/nv50/nv84_video.c

raw
1 /*
2 * Copyright 2013 Ilia Mirkin
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 <sys/mman.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27
28 #include "util/format/u_format.h"
29 #include "util/u_sampler.h"
30 #include "util/u_surface.h"
31 #include "util/vl_zscan_data.h"
32
33 #include "nv50/nv84_video.h"
34
35 static int
36 nv84_copy_firmware(const char *path, void *dest, ssize_t len)
37 {
38 int fd = open(path, O_RDONLY | O_CLOEXEC);
39 ssize_t r;
40 if (fd < 0) {
41 fprintf(stderr, "opening firmware file %s failed: %m\n", path);
42 return 1;
43 }
44 r = read(fd, dest, len);
45 close(fd);
46
47 if (r != len) {
48 fprintf(stderr, "reading firmware file %s failed: %m\n", path);
49 return 1;
50 }
51
52 return 0;
53 }
54
55 static int
56 filesize(const char *path)
57 {
58 int ret;
59 struct stat statbuf;
60
61 ret = stat(path, &statbuf);
62 if (ret)
63 return ret;
64 return statbuf.st_size;
65 }
66
67 static struct nouveau_bo *
68 nv84_load_firmwares(struct nouveau_device *dev, struct nv84_decoder *dec,
69 const char *fw1, const char *fw2)
70 {
71 int ret, size1, size2 = 0;
72 struct nouveau_bo *fw;
73 struct nouveau_screen *screen = nouveau_screen(dec->base.context->screen);
74
75 size1 = filesize(fw1);
76 if (fw2)
77 size2 = filesize(fw2);
78 if (size1 < 0 || size2 < 0)
79 return NULL;
80
81 dec->vp_fw2_offset = align(size1, 0x100);
82
83 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 0, dec->vp_fw2_offset + size2, NULL, &fw);
84 if (ret)
85 return NULL;
86 ret = BO_MAP(screen, fw, NOUVEAU_BO_WR, dec->client);
87 if (ret)
88 goto error;
89
90 ret = nv84_copy_firmware(fw1, fw->map, size1);
91 if (fw2 && !ret)
92 ret = nv84_copy_firmware(fw2, fw->map + dec->vp_fw2_offset, size2);
93 munmap(fw->map, fw->size);
94 fw->map = NULL;
95 if (!ret)
96 return fw;
97 error:
98 nouveau_bo_ref(NULL, &fw);
99 return NULL;
100 }
101
102 static struct nouveau_bo *
103 nv84_load_bsp_firmware(struct nouveau_device *dev, struct nv84_decoder *dec)
104 {
105 return nv84_load_firmwares(
106 dev, dec, "/lib/firmware/nouveau/nv84_bsp-h264", NULL);
107 }
108
109 static struct nouveau_bo *
110 nv84_load_vp_firmware(struct nouveau_device *dev, struct nv84_decoder *dec)
111 {
112 return nv84_load_firmwares(
113 dev, dec,
114 "/lib/firmware/nouveau/nv84_vp-h264-1",
115 "/lib/firmware/nouveau/nv84_vp-h264-2");
116 }
117
118 static struct nouveau_bo *
119 nv84_load_vp_firmware_mpeg(struct nouveau_device *dev, struct nv84_decoder *dec)
120 {
121 return nv84_load_firmwares(
122 dev, dec, "/lib/firmware/nouveau/nv84_vp-mpeg12", NULL);
123 }
124
125 static void
126 nv84_decoder_decode_bitstream_h264(struct pipe_video_codec *decoder,
127 struct pipe_video_buffer *video_target,
128 struct pipe_picture_desc *picture,
129 unsigned num_buffers,
130 const void *const *data,
131 const unsigned *num_bytes)
132 {
133 struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
134 struct nv84_video_buffer *target = (struct nv84_video_buffer *)video_target;
135
136 struct pipe_h264_picture_desc *desc = (struct pipe_h264_picture_desc *)picture;
137
138 assert(target->base.buffer_format == PIPE_FORMAT_NV12);
139
140 nv84_decoder_bsp(dec, desc, num_buffers, data, num_bytes, target);
141 nv84_decoder_vp_h264(dec, desc, target);
142 }
143
144 static void
145 nv84_decoder_flush(struct pipe_video_codec *decoder)
146 {
147 }
148
149 static void
150 nv84_decoder_begin_frame_h264(struct pipe_video_codec *decoder,
151 struct pipe_video_buffer *target,
152 struct pipe_picture_desc *picture)
153 {
154 }
155
156 static int
157 nv84_decoder_end_frame_h264(struct pipe_video_codec *decoder,
158 struct pipe_video_buffer *target,
159 struct pipe_picture_desc *picture)
160 {
161 return 0;
162 }
163
164 static void
165 nv84_decoder_decode_bitstream_mpeg12(struct pipe_video_codec *decoder,
166 struct pipe_video_buffer *video_target,
167 struct pipe_picture_desc *picture,
168 unsigned num_buffers,
169 const void *const *data,
170 const unsigned *num_bytes)
171 {
172 struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
173
174 assert(video_target->buffer_format == PIPE_FORMAT_NV12);
175
176 vl_mpg12_bs_decode(dec->mpeg12_bs,
177 video_target,
178 (struct pipe_mpeg12_picture_desc *)picture,
179 num_buffers,
180 data,
181 num_bytes);
182 }
183
184 static void
185 nv84_decoder_begin_frame_mpeg12(struct pipe_video_codec *decoder,
186 struct pipe_video_buffer *target,
187 struct pipe_picture_desc *picture)
188 {
189 struct nouveau_screen *screen = nouveau_screen(decoder->context->screen);
190 struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
191 struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
192 int i;
193
194 BO_WAIT(screen, dec->mpeg12_bo, NOUVEAU_BO_RDWR, dec->client);
195 dec->mpeg12_mb_info = dec->mpeg12_bo->map + 0x100;
196 dec->mpeg12_data = dec->mpeg12_bo->map + 0x100 +
197 align(0x20 * mb(dec->base.width) * mb(dec->base.height), 0x100);
198 if (desc->intra_matrix) {
199 dec->zscan = desc->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
200 for (i = 0; i < 64; i++) {
201 dec->mpeg12_intra_matrix[i] = desc->intra_matrix[dec->zscan[i]];
202 dec->mpeg12_non_intra_matrix[i] = desc->non_intra_matrix[dec->zscan[i]];
203 }
204 dec->mpeg12_intra_matrix[0] = 1 << (7 - desc->intra_dc_precision);
205 }
206 }
207
208 static int
209 nv84_decoder_end_frame_mpeg12(struct pipe_video_codec *decoder,
210 struct pipe_video_buffer *target,
211 struct pipe_picture_desc *picture)
212 {
213 nv84_decoder_vp_mpeg12(
214 (struct nv84_decoder *)decoder,
215 (struct pipe_mpeg12_picture_desc *)picture,
216 (struct nv84_video_buffer *)target);
217 return 0;
218 }
219
220 static void
221 nv84_decoder_decode_macroblock(struct pipe_video_codec *decoder,
222 struct pipe_video_buffer *target,
223 struct pipe_picture_desc *picture,
224 const struct pipe_macroblock *macroblocks,
225 unsigned num_macroblocks)
226 {
227 const struct pipe_mpeg12_macroblock *mb = (const struct pipe_mpeg12_macroblock *)macroblocks;
228 for (int i = 0; i < num_macroblocks; i++, mb++) {
229 nv84_decoder_vp_mpeg12_mb(
230 (struct nv84_decoder *)decoder,
231 (struct pipe_mpeg12_picture_desc *)picture,
232 mb);
233 }
234 }
235
236 static void
237 nv84_decoder_destroy(struct pipe_video_codec *decoder)
238 {
239 struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
240
241 nouveau_bo_ref(NULL, &dec->bsp_fw);
242 nouveau_bo_ref(NULL, &dec->bsp_data);
243 nouveau_bo_ref(NULL, &dec->vp_fw);
244 nouveau_bo_ref(NULL, &dec->vp_data);
245 nouveau_bo_ref(NULL, &dec->mbring);
246 nouveau_bo_ref(NULL, &dec->vpring);
247 nouveau_bo_ref(NULL, &dec->bitstream);
248 nouveau_bo_ref(NULL, &dec->vp_params);
249 nouveau_bo_ref(NULL, &dec->fence);
250
251 nouveau_object_del(&dec->bsp);
252 nouveau_object_del(&dec->vp);
253
254 nouveau_bufctx_del(&dec->bsp_bufctx);
255 nouveau_pushbuf_destroy(&dec->bsp_pushbuf);
256 nouveau_object_del(&dec->bsp_channel);
257
258 nouveau_bufctx_del(&dec->vp_bufctx);
259 nouveau_pushbuf_destroy(&dec->vp_pushbuf);
260 nouveau_object_del(&dec->vp_channel);
261
262 nouveau_client_del(&dec->client);
263
264 FREE(dec->mpeg12_bs);
265 FREE(dec);
266 }
267
268 struct pipe_video_codec *
269 nv84_create_decoder(struct pipe_context *context,
270 const struct pipe_video_codec *templ)
271 {
272 struct nv50_context *nv50 = (struct nv50_context *)context;
273 struct nouveau_screen *screen = &nv50->screen->base;
274 struct nv84_decoder *dec;
275 struct nouveau_pushbuf *bsp_push, *vp_push;
276 struct nv50_surface surf;
277 struct nv50_miptree mip;
278 union pipe_color_union color;
279 struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
280 int ret, i;
281 int is_h264 = u_reduce_video_profile(templ->profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC;
282 int is_mpeg12 = u_reduce_video_profile(templ->profile) == PIPE_VIDEO_FORMAT_MPEG12;
283
284 if ((is_h264 && templ->entrypoint != PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
285 (is_mpeg12 && templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_IDCT)) {
286 debug_printf("%x\n", templ->entrypoint);
287 return NULL;
288 }
289
290 if (!is_h264 && !is_mpeg12) {
291 debug_printf("invalid profile: %x\n", templ->profile);
292 return NULL;
293 }
294
295 dec = CALLOC_STRUCT(nv84_decoder);
296 if (!dec)
297 return NULL;
298
299 dec->base = *templ;
300 dec->base.context = context;
301 dec->base.destroy = nv84_decoder_destroy;
302 dec->base.flush = nv84_decoder_flush;
303 if (is_h264) {
304 dec->base.decode_bitstream = nv84_decoder_decode_bitstream_h264;
305 dec->base.begin_frame = nv84_decoder_begin_frame_h264;
306 dec->base.end_frame = nv84_decoder_end_frame_h264;
307
308 dec->frame_mbs = mb(dec->base.width) * mb_half(dec->base.height) * 2;
309 dec->frame_size = dec->frame_mbs << 8;
310 dec->vpring_deblock = align(0x30 * dec->frame_mbs, 0x100);
311 dec->vpring_residual = 0x2000 + MAX2(0x32000, 0x600 * dec->frame_mbs);
312 dec->vpring_ctrl = MAX2(0x10000, align(0x1080 + 0x144 * dec->frame_mbs, 0x100));
313 } else if (is_mpeg12) {
314 dec->base.decode_macroblock = nv84_decoder_decode_macroblock;
315 dec->base.begin_frame = nv84_decoder_begin_frame_mpeg12;
316 dec->base.end_frame = nv84_decoder_end_frame_mpeg12;
317
318 if (templ->entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
319 dec->mpeg12_bs = CALLOC_STRUCT(vl_mpg12_bs);
320 if (!dec->mpeg12_bs)
321 goto fail;
322 vl_mpg12_bs_init(dec->mpeg12_bs, &dec->base);
323 dec->base.decode_bitstream = nv84_decoder_decode_bitstream_mpeg12;
324 }
325 } else {
326 goto fail;
327 }
328
329 ret = nouveau_client_new(screen->device, &dec->client);
330 if (ret)
331 goto fail;
332
333 if (is_h264) {
334 ret = nouveau_object_new(&screen->device->object, 0,
335 NOUVEAU_FIFO_CHANNEL_CLASS,
336 &nv04_data, sizeof(nv04_data), &dec->bsp_channel);
337 if (ret)
338 goto fail;
339
340 ret = nouveau_pushbuf_create(screen, &nv50->base, dec->client, dec->bsp_channel,
341 4, 32 * 1024, &dec->bsp_pushbuf);
342 if (ret)
343 goto fail;
344
345 ret = nouveau_bufctx_new(dec->client, 1, &dec->bsp_bufctx);
346 if (ret)
347 goto fail;
348 }
349
350 ret = nouveau_object_new(&screen->device->object, 0,
351 NOUVEAU_FIFO_CHANNEL_CLASS,
352 &nv04_data, sizeof(nv04_data), &dec->vp_channel);
353 if (ret)
354 goto fail;
355 ret = nouveau_pushbuf_create(screen, &nv50->base, dec->client, dec->vp_channel,
356 4, 32 * 1024, &dec->vp_pushbuf);
357 if (ret)
358 goto fail;
359
360 ret = nouveau_bufctx_new(dec->client, 1, &dec->vp_bufctx);
361 if (ret)
362 goto fail;
363
364 bsp_push = dec->bsp_pushbuf;
365 vp_push = dec->vp_pushbuf;
366
367 if (is_h264) {
368 dec->bsp_fw = nv84_load_bsp_firmware(screen->device, dec);
369 dec->vp_fw = nv84_load_vp_firmware(screen->device, dec);
370 if (!dec->bsp_fw || !dec->vp_fw)
371 goto fail;
372 }
373 if (is_mpeg12) {
374 dec->vp_fw = nv84_load_vp_firmware_mpeg(screen->device, dec);
375 if (!dec->vp_fw)
376 goto fail;
377 }
378
379 if (is_h264) {
380 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP,
381 0, 0x40000, NULL, &dec->bsp_data);
382 if (ret)
383 goto fail;
384 }
385 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP,
386 0, 0x40000, NULL, &dec->vp_data);
387 if (ret)
388 goto fail;
389 if (is_h264) {
390 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP,
391 0,
392 2 * (dec->vpring_deblock +
393 dec->vpring_residual +
394 dec->vpring_ctrl +
395 0x1000),
396 NULL, &dec->vpring);
397 if (ret)
398 goto fail;
399 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP,
400 0,
401 (templ->max_references + 1) * dec->frame_mbs * 0x40 +
402 dec->frame_size + 0x2000,
403 NULL, &dec->mbring);
404 if (ret)
405 goto fail;
406 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_GART,
407 0, 2 * (0x700 + MAX2(0x40000, 0x800 + 0x180 * dec->frame_mbs)),
408 NULL, &dec->bitstream);
409 if (ret)
410 goto fail;
411 ret = BO_MAP(screen, dec->bitstream, NOUVEAU_BO_WR, dec->client);
412 if (ret)
413 goto fail;
414 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_GART,
415 0, 0x2000, NULL, &dec->vp_params);
416 if (ret)
417 goto fail;
418 ret = BO_MAP(screen, dec->vp_params, NOUVEAU_BO_WR, dec->client);
419 if (ret)
420 goto fail;
421 }
422 if (is_mpeg12) {
423 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_GART,
424 0,
425 align(0x20 * mb(templ->width) * mb(templ->height), 0x100) +
426 (6 * 64 * 8) * mb(templ->width) * mb(templ->height) + 0x100,
427 NULL, &dec->mpeg12_bo);
428 if (ret)
429 goto fail;
430 ret = BO_MAP(screen, dec->mpeg12_bo, NOUVEAU_BO_WR, dec->client);
431 if (ret)
432 goto fail;
433 }
434
435 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM,
436 0, 0x1000, NULL, &dec->fence);
437 if (ret)
438 goto fail;
439 ret = BO_MAP(screen, dec->fence, NOUVEAU_BO_WR, dec->client);
440 if (ret)
441 goto fail;
442 *(uint32_t *)dec->fence->map = 0;
443
444 if (is_h264) {
445 nouveau_pushbuf_bufctx(bsp_push, dec->bsp_bufctx);
446 nouveau_bufctx_refn(dec->bsp_bufctx, 0,
447 dec->bsp_fw, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
448 nouveau_bufctx_refn(dec->bsp_bufctx, 0,
449 dec->bsp_data, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
450 }
451
452 nouveau_pushbuf_bufctx(vp_push, dec->vp_bufctx);
453 nouveau_bufctx_refn(dec->vp_bufctx, 0, dec->vp_fw,
454 NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
455 nouveau_bufctx_refn(dec->vp_bufctx, 0, dec->vp_data,
456 NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
457
458 if (is_h264 && !ret)
459 ret = nouveau_object_new(dec->bsp_channel, 0xbeef74b0, 0x74b0,
460 NULL, 0, &dec->bsp);
461
462 if (!ret)
463 ret = nouveau_object_new(dec->vp_channel, 0xbeef7476, 0x7476,
464 NULL, 0, &dec->vp);
465
466 if (ret)
467 goto fail;
468
469
470 if (is_h264) {
471 /* Zero out some parts of mbring/vpring. there's gotta be some cleaner way
472 * of doing this... perhaps makes sense to just copy the relevant logic
473 * here. */
474 color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
475 surf.offset = dec->frame_size;
476 surf.width = 64;
477 surf.height = (templ->max_references + 1) * dec->frame_mbs / 4;
478 surf.depth = 1;
479 surf.base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
480 surf.base.level = 0;
481 surf.base.texture = &mip.base.base;
482 mip.level[0].tile_mode = 0;
483 mip.level[0].pitch = surf.width * 4;
484 mip.base.domain = NOUVEAU_BO_VRAM;
485 mip.base.bo = dec->mbring;
486 mip.base.address = dec->mbring->offset;
487 context->clear_render_target(context, &surf.base, &color, 0, 0, 64, 4760, false);
488 surf.offset = dec->vpring->size / 2 - 0x1000;
489 surf.width = 1024;
490 surf.height = 1;
491 mip.level[0].pitch = surf.width * 4;
492 mip.base.bo = dec->vpring;
493 mip.base.address = dec->vpring->offset;
494 context->clear_render_target(context, &surf.base, &color, 0, 0, 1024, 1, false);
495 surf.offset = dec->vpring->size - 0x1000;
496 context->clear_render_target(context, &surf.base, &color, 0, 0, 1024, 1, false);
497
498 PUSH_SPACE(nv50->base.pushbuf, 5);
499 PUSH_REF1(nv50->base.pushbuf, dec->fence, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
500 /* The clear_render_target is done via 3D engine, so use it to write to a
501 * sempahore to indicate that it's done.
502 */
503 BEGIN_NV04(nv50->base.pushbuf, NV50_3D(QUERY_ADDRESS_HIGH), 4);
504 PUSH_DATAh(nv50->base.pushbuf, dec->fence->offset);
505 PUSH_DATA (nv50->base.pushbuf, dec->fence->offset);
506 PUSH_DATA (nv50->base.pushbuf, 1);
507 PUSH_DATA (nv50->base.pushbuf, 0xf010);
508 PUSH_KICK (nv50->base.pushbuf);
509
510 PUSH_SPACE(bsp_push, 2 + 12 + 2 + 4 + 3);
511
512 BEGIN_NV04(bsp_push, SUBC_BSP(NV01_SUBCHAN_OBJECT), 1);
513 PUSH_DATA (bsp_push, dec->bsp->handle);
514
515 BEGIN_NV04(bsp_push, SUBC_BSP(0x180), 11);
516 for (i = 0; i < 11; i++)
517 PUSH_DATA(bsp_push, nv04_data.vram);
518 BEGIN_NV04(bsp_push, SUBC_BSP(0x1b8), 1);
519 PUSH_DATA (bsp_push, nv04_data.vram);
520
521 BEGIN_NV04(bsp_push, SUBC_BSP(0x600), 3);
522 PUSH_DATAh(bsp_push, dec->bsp_fw->offset);
523 PUSH_DATA (bsp_push, dec->bsp_fw->offset);
524 PUSH_DATA (bsp_push, dec->bsp_fw->size);
525
526 BEGIN_NV04(bsp_push, SUBC_BSP(0x628), 2);
527 PUSH_DATA (bsp_push, dec->bsp_data->offset >> 8);
528 PUSH_DATA (bsp_push, dec->bsp_data->size);
529 PUSH_KICK (bsp_push);
530 }
531
532 PUSH_SPACE(vp_push, 2 + 12 + 2 + 4 + 3);
533
534 BEGIN_NV04(vp_push, SUBC_VP(NV01_SUBCHAN_OBJECT), 1);
535 PUSH_DATA (vp_push, dec->vp->handle);
536
537 BEGIN_NV04(vp_push, SUBC_VP(0x180), 11);
538 for (i = 0; i < 11; i++)
539 PUSH_DATA(vp_push, nv04_data.vram);
540
541 BEGIN_NV04(vp_push, SUBC_VP(0x1b8), 1);
542 PUSH_DATA (vp_push, nv04_data.vram);
543
544 BEGIN_NV04(vp_push, SUBC_VP(0x600), 3);
545 PUSH_DATAh(vp_push, dec->vp_fw->offset);
546 PUSH_DATA (vp_push, dec->vp_fw->offset);
547 PUSH_DATA (vp_push, dec->vp_fw->size);
548
549 BEGIN_NV04(vp_push, SUBC_VP(0x628), 2);
550 PUSH_DATA (vp_push, dec->vp_data->offset >> 8);
551 PUSH_DATA (vp_push, dec->vp_data->size);
552 PUSH_KICK (vp_push);
553
554 return &dec->base;
555 fail:
556 nv84_decoder_destroy(&dec->base);
557 return NULL;
558 }
559
560 static void
561 nv84_video_buffer_resources(struct pipe_video_buffer *buffer,
562 struct pipe_resource **resources)
563 {
564 struct nv84_video_buffer *buf = (struct nv84_video_buffer *)buffer;
565 unsigned num_planes = util_format_get_num_planes(buffer->buffer_format);
566 unsigned i;
567
568 assert(buf);
569
570 for (i = 0; i < num_planes; ++i) {
571 resources[i] = buf->resources[i];
572 }
573 }
574
575 static struct pipe_sampler_view **
576 nv84_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
577 {
578 struct nv84_video_buffer *buf = (struct nv84_video_buffer *)buffer;
579 return buf->sampler_view_planes;
580 }
581
582 static struct pipe_sampler_view **
583 nv84_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
584 {
585 struct nv84_video_buffer *buf = (struct nv84_video_buffer *)buffer;
586 return buf->sampler_view_components;
587 }
588
589 static struct pipe_surface *
590 nv84_video_buffer_surfaces(struct pipe_video_buffer *buffer)
591 {
592 struct nv84_video_buffer *buf = (struct nv84_video_buffer *)buffer;
593 return buf->surfaces;
594 }
595
596 static void
597 nv84_video_buffer_destroy(struct pipe_video_buffer *buffer)
598 {
599 struct nv84_video_buffer *buf = (struct nv84_video_buffer *)buffer;
600 unsigned i;
601
602 assert(buf);
603
604 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
605 pipe_resource_reference(&buf->resources[i], NULL);
606 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
607 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
608 }
609
610 nouveau_bo_ref(NULL, &buf->interlaced);
611 nouveau_bo_ref(NULL, &buf->full);
612
613 FREE(buffer);
614 }
615
616 struct pipe_video_buffer *
617 nv84_video_buffer_create(struct pipe_context *pipe,
618 const struct pipe_video_buffer *template)
619 {
620 struct nv84_video_buffer *buffer;
621 struct pipe_resource templ;
622 unsigned i, j, component;
623 struct pipe_sampler_view sv_templ;
624 struct pipe_surface surf_templ;
625 struct nv50_miptree *mt0, *mt1;
626 struct nouveau_screen *screen = &((struct nv50_context *)pipe)->screen->base;
627 union nouveau_bo_config cfg;
628 unsigned bo_size;
629
630 if (template->buffer_format != PIPE_FORMAT_NV12)
631 return vl_video_buffer_create(pipe, template);
632
633 if (!template->interlaced) {
634 debug_printf("Require interlaced video buffers\n");
635 return NULL;
636 }
637 if (pipe_format_to_chroma_format(template->buffer_format) != PIPE_VIDEO_CHROMA_FORMAT_420) {
638 debug_printf("Must use 4:2:0 format\n");
639 return NULL;
640 }
641
642 /*
643 * Note that there are always going to be exactly two planes, one for Y,
644 * and one for UV. These are also the resources. VP expects these to be
645 * adjacent, so they need to belong to the same BO.
646 */
647
648 buffer = CALLOC_STRUCT(nv84_video_buffer);
649 if (!buffer) return NULL;
650
651 buffer->mvidx = -1;
652
653 buffer->base.buffer_format = template->buffer_format;
654 buffer->base.context = pipe;
655 buffer->base.destroy = nv84_video_buffer_destroy;
656 buffer->base.width = template->width;
657 buffer->base.height = template->height;
658 buffer->base.get_resources = nv84_video_buffer_resources;
659 buffer->base.get_sampler_view_planes = nv84_video_buffer_sampler_view_planes;
660 buffer->base.get_sampler_view_components = nv84_video_buffer_sampler_view_components;
661 buffer->base.get_surfaces = nv84_video_buffer_surfaces;
662 buffer->base.interlaced = true;
663
664 memset(&templ, 0, sizeof(templ));
665 templ.target = PIPE_TEXTURE_2D_ARRAY;
666 templ.depth0 = 1;
667 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
668 templ.format = PIPE_FORMAT_R8_UNORM;
669 templ.width0 = align(template->width, 2);
670 templ.height0 = align(template->height, 4) / 2;
671 templ.flags = NV50_RESOURCE_FLAG_VIDEO | NV50_RESOURCE_FLAG_NOALLOC;
672 templ.array_size = 2;
673
674 cfg.nv50.tile_mode = 0x20;
675 cfg.nv50.memtype = 0x70;
676
677 buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
678 if (!buffer->resources[0])
679 goto error;
680
681 templ.format = PIPE_FORMAT_R8G8_UNORM;
682 templ.width0 /= 2;
683 templ.height0 /= 2;
684 buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
685 if (!buffer->resources[1])
686 goto error;
687
688 mt0 = nv50_miptree(buffer->resources[0]);
689 mt1 = nv50_miptree(buffer->resources[1]);
690
691 bo_size = mt0->total_size + mt1->total_size;
692 if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
693 bo_size, &cfg, &buffer->interlaced))
694 goto error;
695 /* XXX Change reference frame management so that this is only allocated in
696 * the decoder when necessary. */
697 if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
698 bo_size, &cfg, &buffer->full))
699 goto error;
700
701 nouveau_bo_ref(buffer->interlaced, &mt0->base.bo);
702 mt0->base.domain = NOUVEAU_BO_VRAM;
703 mt0->base.address = buffer->interlaced->offset;
704
705 nouveau_bo_ref(buffer->interlaced, &mt1->base.bo);
706 mt1->base.domain = NOUVEAU_BO_VRAM;
707 mt1->base.offset = mt0->total_size;
708 mt1->base.address = buffer->interlaced->offset + mt0->total_size;
709
710 memset(&sv_templ, 0, sizeof(sv_templ));
711 for (component = 0, i = 0; i < 2; ++i ) {
712 struct pipe_resource *res = buffer->resources[i];
713 unsigned nr_components = util_format_get_nr_components(res->format);
714
715 u_sampler_view_default_template(&sv_templ, res, res->format);
716 buffer->sampler_view_planes[i] =
717 pipe->create_sampler_view(pipe, res, &sv_templ);
718 if (!buffer->sampler_view_planes[i])
719 goto error;
720
721 for (j = 0; j < nr_components; ++j, ++component) {
722 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b =
723 PIPE_SWIZZLE_X + j;
724 sv_templ.swizzle_a = PIPE_SWIZZLE_1;
725
726 buffer->sampler_view_components[component] =
727 pipe->create_sampler_view(pipe, res, &sv_templ);
728 if (!buffer->sampler_view_components[component])
729 goto error;
730 }
731 }
732
733 memset(&surf_templ, 0, sizeof(surf_templ));
734 for (j = 0; j < 2; ++j) {
735 u_surface_default_template(&surf_templ, buffer->resources[j]);
736 surf_templ.first_layer = surf_templ.last_layer = 0;
737 buffer->surfaces[j * 2] = surf_templ;
738
739 surf_templ.first_layer = surf_templ.last_layer = 1;
740 buffer->surfaces[j * 2 + 1] = surf_templ;
741 }
742
743 return &buffer->base;
744
745 error:
746 nv84_video_buffer_destroy(&buffer->base);
747 return NULL;
748 }
749
750 #define FIRMWARE_BSP_KERN 0x01
751 #define FIRMWARE_VP_KERN 0x02
752 #define FIRMWARE_BSP_H264 0x04
753 #define FIRMWARE_VP_MPEG2 0x08
754 #define FIRMWARE_VP_H264_1 0x10
755 #define FIRMWARE_VP_H264_2 0x20
756 #define FIRMWARE_PRESENT(val, fw) (val & FIRMWARE_ ## fw)
757
758 static int
759 firmware_present(struct pipe_screen *pscreen, enum pipe_video_format codec)
760 {
761 struct nouveau_screen *screen = nouveau_screen(pscreen);
762 struct nouveau_object *obj = NULL;
763 struct stat s;
764 int checked = screen->firmware_info.profiles_checked;
765 int present, ret;
766
767 if (!FIRMWARE_PRESENT(checked, VP_KERN)) {
768 ret = nouveau_object_new(screen->channel, 0, 0x7476, NULL, 0, &obj);
769 if (!ret)
770 screen->firmware_info.profiles_present |= FIRMWARE_VP_KERN;
771 nouveau_object_del(&obj);
772 screen->firmware_info.profiles_checked |= FIRMWARE_VP_KERN;
773 }
774
775 if (codec == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
776 if (!FIRMWARE_PRESENT(checked, BSP_KERN)) {
777 ret = nouveau_object_new(screen->channel, 0, 0x74b0, NULL, 0, &obj);
778 if (!ret)
779 screen->firmware_info.profiles_present |= FIRMWARE_BSP_KERN;
780 nouveau_object_del(&obj);
781 screen->firmware_info.profiles_checked |= FIRMWARE_BSP_KERN;
782 }
783
784 if (!FIRMWARE_PRESENT(checked, VP_H264_1)) {
785 ret = stat("/lib/firmware/nouveau/nv84_vp-h264-1", &s);
786 if (!ret && s.st_size > 1000)
787 screen->firmware_info.profiles_present |= FIRMWARE_VP_H264_1;
788 screen->firmware_info.profiles_checked |= FIRMWARE_VP_H264_1;
789 }
790
791 /* should probably check the others, but assume that 1 means all */
792
793 present = screen->firmware_info.profiles_present;
794 return FIRMWARE_PRESENT(present, VP_KERN) &&
795 FIRMWARE_PRESENT(present, BSP_KERN) &&
796 FIRMWARE_PRESENT(present, VP_H264_1);
797 } else {
798 if (!FIRMWARE_PRESENT(checked, VP_MPEG2)) {
799 ret = stat("/lib/firmware/nouveau/nv84_vp-mpeg12", &s);
800 if (!ret && s.st_size > 1000)
801 screen->firmware_info.profiles_present |= FIRMWARE_VP_MPEG2;
802 screen->firmware_info.profiles_checked |= FIRMWARE_VP_MPEG2;
803 }
804 present = screen->firmware_info.profiles_present;
805 return FIRMWARE_PRESENT(present, VP_KERN) &&
806 FIRMWARE_PRESENT(present, VP_MPEG2);
807 }
808 }
809
810 int
811 nv84_screen_get_video_param(struct pipe_screen *pscreen,
812 enum pipe_video_profile profile,
813 enum pipe_video_entrypoint entrypoint,
814 enum pipe_video_cap param)
815 {
816 enum pipe_video_format codec;
817
818 switch (param) {
819 case PIPE_VIDEO_CAP_SUPPORTED:
820 codec = u_reduce_video_profile(profile);
821 return (codec == PIPE_VIDEO_FORMAT_MPEG4_AVC ||
822 codec == PIPE_VIDEO_FORMAT_MPEG12) &&
823 firmware_present(pscreen, codec);
824 case PIPE_VIDEO_CAP_NPOT_TEXTURES:
825 return 1;
826 case PIPE_VIDEO_CAP_MAX_WIDTH:
827 case PIPE_VIDEO_CAP_MAX_HEIGHT:
828 return 2048;
829 case PIPE_VIDEO_CAP_PREFERRED_FORMAT:
830 return PIPE_FORMAT_NV12;
831 case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
832 return false;
833 case PIPE_VIDEO_CAP_MAX_LEVEL:
834 switch (profile) {
835 case PIPE_VIDEO_PROFILE_MPEG1:
836 return 0;
837 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
838 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
839 return 3;
840 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
841 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
842 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
843 return 41;
844 default:
845 debug_printf("unknown video profile: %d\n", profile);
846 return 0;
847 }
848 case PIPE_VIDEO_CAP_MAX_MACROBLOCKS:
849 return 8192; /* vc-1 actually has 8190, but this is not supported */
850 default:
851 debug_printf("unknown video param: %d\n", param);
852 return 0;
853 }
854 }
855
856 bool
857 nv84_screen_video_supported(struct pipe_screen *screen,
858 enum pipe_format format,
859 enum pipe_video_profile profile,
860 enum pipe_video_entrypoint entrypoint)
861 {
862 if (profile != PIPE_VIDEO_PROFILE_UNKNOWN)
863 return format == PIPE_FORMAT_NV12;
864
865 return vl_video_buffer_is_format_supported(screen, format, profile, entrypoint);
866 }