2 * Copyright 2011-2013 Maarten Lankhorst
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
28 #include <nvif/class.h>
30 #include "nouveau_screen.h"
31 #include "nouveau_context.h"
32 #include "nouveau_vp3_video.h"
34 #include "util/u_video.h"
35 #include "util/format/u_format.h"
36 #include "util/u_sampler.h"
38 static struct pipe_sampler_view
**
39 nouveau_vp3_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
41 struct nouveau_vp3_video_buffer
*buf
= (struct nouveau_vp3_video_buffer
*)buffer
;
42 return buf
->sampler_view_planes
;
45 static struct pipe_sampler_view
**
46 nouveau_vp3_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
48 struct nouveau_vp3_video_buffer
*buf
= (struct nouveau_vp3_video_buffer
*)buffer
;
49 return buf
->sampler_view_components
;
52 static struct pipe_surface
**
53 nouveau_vp3_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
55 struct nouveau_vp3_video_buffer
*buf
= (struct nouveau_vp3_video_buffer
*)buffer
;
60 nouveau_vp3_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
62 struct nouveau_vp3_video_buffer
*buf
= (struct nouveau_vp3_video_buffer
*)buffer
;
67 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
68 pipe_resource_reference(&buf
->resources
[i
], NULL
);
69 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
70 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
71 pipe_surface_reference(&buf
->surfaces
[i
* 2], NULL
);
72 pipe_surface_reference(&buf
->surfaces
[i
* 2 + 1], NULL
);
77 struct pipe_video_buffer
*
78 nouveau_vp3_video_buffer_create(struct pipe_context
*pipe
,
79 const struct pipe_video_buffer
*templat
,
82 struct nouveau_vp3_video_buffer
*buffer
;
83 struct pipe_resource templ
;
84 unsigned i
, j
, component
;
85 struct pipe_sampler_view sv_templ
;
86 struct pipe_surface surf_templ
;
88 if (getenv("XVMC_VL") || templat
->buffer_format
!= PIPE_FORMAT_NV12
)
89 return vl_video_buffer_create(pipe
, templat
);
91 assert(templat
->interlaced
);
92 assert(pipe_format_to_chroma_format(templat
->buffer_format
) == PIPE_VIDEO_CHROMA_FORMAT_420
);
94 buffer
= CALLOC_STRUCT(nouveau_vp3_video_buffer
);
98 buffer
->base
.buffer_format
= templat
->buffer_format
;
99 buffer
->base
.context
= pipe
;
100 buffer
->base
.destroy
= nouveau_vp3_video_buffer_destroy
;
101 buffer
->base
.width
= templat
->width
;
102 buffer
->base
.height
= templat
->height
;
103 buffer
->base
.get_sampler_view_planes
= nouveau_vp3_video_buffer_sampler_view_planes
;
104 buffer
->base
.get_sampler_view_components
= nouveau_vp3_video_buffer_sampler_view_components
;
105 buffer
->base
.get_surfaces
= nouveau_vp3_video_buffer_surfaces
;
106 buffer
->base
.interlaced
= true;
108 memset(&templ
, 0, sizeof(templ
));
109 templ
.target
= PIPE_TEXTURE_2D_ARRAY
;
111 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
112 templ
.format
= PIPE_FORMAT_R8_UNORM
;
113 templ
.width0
= buffer
->base
.width
;
114 templ
.height0
= (buffer
->base
.height
+ 1)/2;
116 templ
.array_size
= 2;
118 buffer
->resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
119 if (!buffer
->resources
[0])
122 templ
.format
= PIPE_FORMAT_R8G8_UNORM
;
123 buffer
->num_planes
= 2;
124 templ
.width0
= (templ
.width0
+ 1) / 2;
125 templ
.height0
= (templ
.height0
+ 1) / 2;
126 for (i
= 1; i
< buffer
->num_planes
; ++i
) {
127 buffer
->resources
[i
] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
128 if (!buffer
->resources
[i
])
132 memset(&sv_templ
, 0, sizeof(sv_templ
));
133 for (component
= 0, i
= 0; i
< buffer
->num_planes
; ++i
) {
134 struct pipe_resource
*res
= buffer
->resources
[i
];
135 unsigned nr_components
= util_format_get_nr_components(res
->format
);
137 u_sampler_view_default_template(&sv_templ
, res
, res
->format
);
138 buffer
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
139 if (!buffer
->sampler_view_planes
[i
])
142 for (j
= 0; j
< nr_components
; ++j
, ++component
) {
143 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_X
+ j
;
144 sv_templ
.swizzle_a
= PIPE_SWIZZLE_1
;
146 buffer
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
147 if (!buffer
->sampler_view_components
[component
])
152 memset(&surf_templ
, 0, sizeof(surf_templ
));
153 for (j
= 0; j
< buffer
->num_planes
; ++j
) {
154 surf_templ
.format
= buffer
->resources
[j
]->format
;
155 surf_templ
.u
.tex
.first_layer
= surf_templ
.u
.tex
.last_layer
= 0;
156 buffer
->surfaces
[j
* 2] = pipe
->create_surface(pipe
, buffer
->resources
[j
], &surf_templ
);
157 if (!buffer
->surfaces
[j
* 2])
160 surf_templ
.u
.tex
.first_layer
= surf_templ
.u
.tex
.last_layer
= 1;
161 buffer
->surfaces
[j
* 2 + 1] = pipe
->create_surface(pipe
, buffer
->resources
[j
], &surf_templ
);
162 if (!buffer
->surfaces
[j
* 2 + 1])
166 return &buffer
->base
;
169 nouveau_vp3_video_buffer_destroy(&buffer
->base
);
174 nouveau_vp3_decoder_flush(struct pipe_video_codec
*decoder
)
179 nouveau_vp3_decoder_begin_frame(struct pipe_video_codec
*decoder
,
180 struct pipe_video_buffer
*target
,
181 struct pipe_picture_desc
*picture
)
186 nouveau_vp3_decoder_end_frame(struct pipe_video_codec
*decoder
,
187 struct pipe_video_buffer
*target
,
188 struct pipe_picture_desc
*picture
)
193 nouveau_vp3_decoder_destroy(struct pipe_video_codec
*decoder
)
195 struct nouveau_vp3_decoder
*dec
= (struct nouveau_vp3_decoder
*)decoder
;
198 nouveau_bo_ref(NULL
, &dec
->ref_bo
);
199 nouveau_bo_ref(NULL
, &dec
->bitplane_bo
);
200 nouveau_bo_ref(NULL
, &dec
->inter_bo
[0]);
201 nouveau_bo_ref(NULL
, &dec
->inter_bo
[1]);
202 #if NOUVEAU_VP3_DEBUG_FENCE
203 nouveau_bo_ref(NULL
, &dec
->fence_bo
);
205 nouveau_bo_ref(NULL
, &dec
->fw_bo
);
207 for (i
= 0; i
< NOUVEAU_VP3_VIDEO_QDEPTH
; ++i
)
208 nouveau_bo_ref(NULL
, &dec
->bsp_bo
[i
]);
210 nouveau_object_del(&dec
->bsp
);
211 nouveau_object_del(&dec
->vp
);
212 nouveau_object_del(&dec
->ppp
);
214 if (dec
->channel
[0] != dec
->channel
[1]) {
215 for (i
= 0; i
< 3; ++i
) {
216 nouveau_pushbuf_del(&dec
->pushbuf
[i
]);
217 nouveau_object_del(&dec
->channel
[i
]);
220 nouveau_pushbuf_del(dec
->pushbuf
);
221 nouveau_object_del(dec
->channel
);
228 nouveau_vp3_decoder_init_common(struct pipe_video_codec
*dec
)
230 dec
->destroy
= nouveau_vp3_decoder_destroy
;
231 dec
->flush
= nouveau_vp3_decoder_flush
;
232 dec
->begin_frame
= nouveau_vp3_decoder_begin_frame
;
233 dec
->end_frame
= nouveau_vp3_decoder_end_frame
;
236 static void vp3_getpath(enum pipe_video_profile profile
, char *path
)
238 switch (u_reduce_video_profile(profile
)) {
239 case PIPE_VIDEO_FORMAT_MPEG12
: {
240 sprintf(path
, "/lib/firmware/nouveau/vuc-vp3-mpeg12-0");
243 case PIPE_VIDEO_FORMAT_VC1
: {
244 sprintf(path
, "/lib/firmware/nouveau/vuc-vp3-vc1-0");
247 case PIPE_VIDEO_FORMAT_MPEG4_AVC
: {
248 sprintf(path
, "/lib/firmware/nouveau/vuc-vp3-h264-0");
255 static void vp4_getpath(enum pipe_video_profile profile
, char *path
)
257 switch (u_reduce_video_profile(profile
)) {
258 case PIPE_VIDEO_FORMAT_MPEG12
: {
259 sprintf(path
, "/lib/firmware/nouveau/vuc-mpeg12-0");
262 case PIPE_VIDEO_FORMAT_MPEG4
: {
263 sprintf(path
, "/lib/firmware/nouveau/vuc-mpeg4-0");
266 case PIPE_VIDEO_FORMAT_VC1
: {
267 sprintf(path
, "/lib/firmware/nouveau/vuc-vc1-0");
270 case PIPE_VIDEO_FORMAT_MPEG4_AVC
: {
271 sprintf(path
, "/lib/firmware/nouveau/vuc-h264-0");
279 nouveau_vp3_load_firmware(struct nouveau_vp3_decoder
*dec
,
280 enum pipe_video_profile profile
,
286 uint32_t *end
, endval
;
288 if (chipset
>= 0xa3 && chipset
!= 0xaa && chipset
!= 0xac)
289 vp4_getpath(profile
, path
);
291 vp3_getpath(profile
, path
);
293 if (nouveau_bo_map(dec
->fw_bo
, NOUVEAU_BO_WR
, dec
->client
))
296 fd
= open(path
, O_RDONLY
| O_CLOEXEC
);
298 fprintf(stderr
, "opening firmware file %s failed: %m\n", path
);
301 r
= read(fd
, dec
->fw_bo
->map
, 0x4000);
305 fprintf(stderr
, "reading firmware file %s failed: %m\n", path
);
310 fprintf(stderr
, "firmware file %s too large!\n", path
);
315 fprintf(stderr
, "firmware file %s wrong size!\n", path
);
319 end
= dec
->fw_bo
->map
+ r
- 4;
321 while (endval
== *end
)
324 r
= (intptr_t)end
- (intptr_t)dec
->fw_bo
->map
+ 4;
326 switch (u_reduce_video_profile(profile
)) {
327 case PIPE_VIDEO_FORMAT_MPEG12
: {
328 assert((r
& 0xff) == 0xe0);
329 dec
->fw_sizes
= (0x2e0<<16) | (r
- 0x2e0);
332 case PIPE_VIDEO_FORMAT_MPEG4
: {
333 assert((r
& 0xff) == 0xe0);
334 dec
->fw_sizes
= (0x2e0<<16) | (r
- 0x2e0);
337 case PIPE_VIDEO_FORMAT_VC1
: {
338 assert((r
& 0xff) == 0xac);
339 dec
->fw_sizes
= (0x3ac<<16) | (r
- 0x3ac);
342 case PIPE_VIDEO_FORMAT_MPEG4_AVC
: {
343 assert((r
& 0xff) == 0x70);
344 dec
->fw_sizes
= (0x370<<16) | (r
- 0x370);
350 munmap(dec
->fw_bo
->map
, dec
->fw_bo
->size
);
351 dec
->fw_bo
->map
= NULL
;
355 static const struct nouveau_mclass
356 nouveau_decoder_msvld
[] = {
358 { IGT21A_MSVLD
, -1 },
366 firmware_present(struct pipe_screen
*pscreen
, enum pipe_video_profile profile
)
368 struct nouveau_screen
*screen
= nouveau_screen(pscreen
);
369 int chipset
= screen
->device
->chipset
;
370 int vp3
= chipset
< 0xa3 || chipset
== 0xaa || chipset
== 0xac;
371 int vp5
= chipset
>= 0xd0;
374 /* For all chipsets, try to create a BSP objects. Assume that if firmware
375 * is present for it, firmware is also present for VP/PPP */
376 if (!(screen
->firmware_info
.profiles_checked
& 1)) {
377 struct nouveau_object
*channel
= NULL
, *bsp
= NULL
;
378 struct nv04_fifo nv04_data
= {.vram
= 0xbeef0201, .gart
= 0xbeef0202};
379 struct nvc0_fifo nvc0_args
= {};
380 struct nve0_fifo nve0_args
= {.engine
= NVE0_FIFO_ENGINE_BSP
};
384 if (chipset
< 0xc0) {
386 size
= sizeof(nv04_data
);
387 } else if (chipset
< 0xe0) {
389 size
= sizeof(nvc0_args
);
392 size
= sizeof(nve0_args
);
395 /* kepler must have its own channel, so just do this for everyone */
396 nouveau_object_new(&screen
->device
->object
, 0,
397 NOUVEAU_FIFO_CHANNEL_CLASS
,
398 data
, size
, &channel
);
401 ret
= nouveau_object_mclass(channel
, nouveau_decoder_msvld
);
403 nouveau_object_new(channel
, 0, nouveau_decoder_msvld
[ret
].oclass
,
406 screen
->firmware_info
.profiles_present
|= 1;
407 nouveau_object_del(&bsp
);
408 nouveau_object_del(&channel
);
410 screen
->firmware_info
.profiles_checked
|= 1;
413 if (!(screen
->firmware_info
.profiles_present
& 1))
416 /* For vp3/vp4 chipsets, make sure that the relevant firmware is present */
417 if (!vp5
&& !(screen
->firmware_info
.profiles_checked
& (1 << profile
))) {
421 vp3_getpath(profile
, path
);
423 vp4_getpath(profile
, path
);
424 ret
= stat(path
, &s
);
425 if (!ret
&& s
.st_size
> 1000)
426 screen
->firmware_info
.profiles_present
|= (1 << profile
);
427 screen
->firmware_info
.profiles_checked
|= (1 << profile
);
430 return vp5
|| (screen
->firmware_info
.profiles_present
& (1 << profile
));
434 nouveau_vp3_screen_get_video_param(struct pipe_screen
*pscreen
,
435 enum pipe_video_profile profile
,
436 enum pipe_video_entrypoint entrypoint
,
437 enum pipe_video_cap param
)
439 int chipset
= nouveau_screen(pscreen
)->device
->chipset
;
440 int vp3
= chipset
< 0xa3 || chipset
== 0xaa || chipset
== 0xac;
441 int vp5
= chipset
>= 0xd0;
442 enum pipe_video_format codec
= u_reduce_video_profile(profile
);
444 case PIPE_VIDEO_CAP_SUPPORTED
:
445 /* VP3 does not support MPEG4, VP4+ do. */
446 return entrypoint
== PIPE_VIDEO_ENTRYPOINT_BITSTREAM
&&
447 profile
>= PIPE_VIDEO_PROFILE_MPEG1
&&
448 profile
< PIPE_VIDEO_PROFILE_HEVC_MAIN
&&
449 (!vp3
|| codec
!= PIPE_VIDEO_FORMAT_MPEG4
) &&
450 firmware_present(pscreen
, profile
);
451 case PIPE_VIDEO_CAP_NPOT_TEXTURES
:
453 case PIPE_VIDEO_CAP_MAX_WIDTH
:
454 case PIPE_VIDEO_CAP_MAX_HEIGHT
:
455 return vp5
? 4096 : 2048;
456 case PIPE_VIDEO_CAP_PREFERED_FORMAT
:
457 return PIPE_FORMAT_NV12
;
458 case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED
:
459 case PIPE_VIDEO_CAP_PREFERS_INTERLACED
:
461 case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE
:
463 case PIPE_VIDEO_CAP_MAX_LEVEL
:
465 case PIPE_VIDEO_PROFILE_MPEG1
:
467 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE
:
468 case PIPE_VIDEO_PROFILE_MPEG2_MAIN
:
470 case PIPE_VIDEO_PROFILE_MPEG4_SIMPLE
:
472 case PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE
:
474 case PIPE_VIDEO_PROFILE_VC1_SIMPLE
:
476 case PIPE_VIDEO_PROFILE_VC1_MAIN
:
478 case PIPE_VIDEO_PROFILE_VC1_ADVANCED
:
480 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE
:
481 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN
:
482 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
:
485 debug_printf("unknown video profile: %d\n", profile
);
489 debug_printf("unknown video param: %d\n", param
);
495 nouveau_vp3_screen_video_supported(struct pipe_screen
*screen
,
496 enum pipe_format format
,
497 enum pipe_video_profile profile
,
498 enum pipe_video_entrypoint entrypoint
)
500 if (profile
!= PIPE_VIDEO_PROFILE_UNKNOWN
)
501 return format
== PIPE_FORMAT_NV12
;
503 return vl_video_buffer_is_format_supported(screen
, format
, profile
, entrypoint
);