| 1 |
/* |
| 2 |
* Copyright 2011-2013 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 "nvc0/nvc0_video.h" |
| 24 |
|
| 25 |
#include "util/u_sampler.h" |
| 26 |
#include "util/format/u_format.h" |
| 27 |
|
| 28 |
static void |
| 29 |
nvc0_decoder_begin_frame(struct pipe_video_codec *decoder, |
| 30 |
struct pipe_video_buffer *target, |
| 31 |
struct pipe_picture_desc *picture) |
| 32 |
{ |
| 33 |
struct nouveau_vp3_decoder *dec = (struct nouveau_vp3_decoder *)decoder; |
| 34 |
uint32_t comm_seq = ++dec->fence_seq; |
| 35 |
ASSERTED unsigned ret = 0; /* used in debug checks */ |
| 36 |
|
| 37 |
assert(dec); |
| 38 |
assert(target); |
| 39 |
assert(target->buffer_format == PIPE_FORMAT_NV12); |
| 40 |
|
| 41 |
ret = nvc0_decoder_bsp_begin(dec, comm_seq); |
| 42 |
|
| 43 |
assert(ret == 2); |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
nvc0_decoder_decode_bitstream(struct pipe_video_codec *decoder, |
| 48 |
struct pipe_video_buffer *video_target, |
| 49 |
struct pipe_picture_desc *picture, |
| 50 |
unsigned num_buffers, |
| 51 |
const void *const *data, |
| 52 |
const unsigned *num_bytes) |
| 53 |
{ |
| 54 |
struct nouveau_vp3_decoder *dec = (struct nouveau_vp3_decoder *)decoder; |
| 55 |
uint32_t comm_seq = dec->fence_seq; |
| 56 |
ASSERTED unsigned ret = 0; /* used in debug checks */ |
| 57 |
|
| 58 |
assert(decoder); |
| 59 |
|
| 60 |
ret = nvc0_decoder_bsp_next(dec, comm_seq, num_buffers, data, num_bytes); |
| 61 |
|
| 62 |
assert(ret == 2); |
| 63 |
} |
| 64 |
|
| 65 |
static int |
| 66 |
nvc0_decoder_end_frame(struct pipe_video_codec *decoder, |
| 67 |
struct pipe_video_buffer *video_target, |
| 68 |
struct pipe_picture_desc *picture) |
| 69 |
{ |
| 70 |
struct nouveau_vp3_decoder *dec = (struct nouveau_vp3_decoder *)decoder; |
| 71 |
struct nouveau_vp3_video_buffer *target = (struct nouveau_vp3_video_buffer *)video_target; |
| 72 |
uint32_t comm_seq = dec->fence_seq; |
| 73 |
union pipe_desc desc; |
| 74 |
|
| 75 |
unsigned vp_caps, is_ref; |
| 76 |
ASSERTED unsigned ret; /* used in debug checks */ |
| 77 |
struct nouveau_vp3_video_buffer *refs[16] = {}; |
| 78 |
|
| 79 |
desc.base = picture; |
| 80 |
|
| 81 |
ret = nvc0_decoder_bsp_end(dec, desc, target, comm_seq, &vp_caps, &is_ref, refs); |
| 82 |
|
| 83 |
/* did we decode bitstream correctly? */ |
| 84 |
assert(ret == 2); |
| 85 |
|
| 86 |
nvc0_decoder_vp(dec, desc, target, comm_seq, vp_caps, is_ref, refs); |
| 87 |
nvc0_decoder_ppp(dec, desc, target, comm_seq); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
struct pipe_video_codec * |
| 92 |
nvc0_create_decoder(struct pipe_context *context, |
| 93 |
const struct pipe_video_codec *templ) |
| 94 |
{ |
| 95 |
struct nvc0_context *nvc0 = nvc0_context(context); |
| 96 |
struct nouveau_screen *screen = &nvc0->screen->base; |
| 97 |
struct nouveau_vp3_decoder *dec; |
| 98 |
struct nouveau_pushbuf **push; |
| 99 |
union nouveau_bo_config cfg; |
| 100 |
bool kepler = screen->device->chipset >= 0xe0; |
| 101 |
|
| 102 |
cfg.nvc0.tile_mode = 0x10; |
| 103 |
cfg.nvc0.memtype = 0xfe; |
| 104 |
|
| 105 |
int ret, i; |
| 106 |
uint32_t codec = 1, ppp_codec = 3; |
| 107 |
uint32_t timeout; |
| 108 |
u32 tmp_size = 0; |
| 109 |
|
| 110 |
if (templ->entrypoint != PIPE_VIDEO_ENTRYPOINT_BITSTREAM) { |
| 111 |
debug_printf("%x\n", templ->entrypoint); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
dec = CALLOC_STRUCT(nouveau_vp3_decoder); |
| 116 |
if (!dec) |
| 117 |
return NULL; |
| 118 |
dec->client = nvc0->base.client; |
| 119 |
dec->base = *templ; |
| 120 |
nouveau_vp3_decoder_init_common(&dec->base); |
| 121 |
|
| 122 |
if (!kepler) { |
| 123 |
dec->bsp_idx = 5; |
| 124 |
dec->vp_idx = 6; |
| 125 |
dec->ppp_idx = 7; |
| 126 |
} else { |
| 127 |
dec->bsp_idx = 2; |
| 128 |
dec->vp_idx = 2; |
| 129 |
dec->ppp_idx = 2; |
| 130 |
} |
| 131 |
|
| 132 |
for (i = 0; i < 3; ++i) |
| 133 |
if (i && !kepler) { |
| 134 |
dec->channel[i] = dec->channel[0]; |
| 135 |
dec->pushbuf[i] = dec->pushbuf[0]; |
| 136 |
} else { |
| 137 |
void *data; |
| 138 |
u32 size; |
| 139 |
struct nvc0_fifo nvc0_args = {}; |
| 140 |
struct nve0_fifo nve0_args = {}; |
| 141 |
|
| 142 |
if (!kepler) { |
| 143 |
size = sizeof(nvc0_args); |
| 144 |
data = &nvc0_args; |
| 145 |
} else { |
| 146 |
unsigned engine[] = { |
| 147 |
NOUVEAU_FIFO_ENGINE_BSP, |
| 148 |
NOUVEAU_FIFO_ENGINE_VP, |
| 149 |
NOUVEAU_FIFO_ENGINE_PPP |
| 150 |
}; |
| 151 |
|
| 152 |
nve0_args.engine = engine[i]; |
| 153 |
size = sizeof(nve0_args); |
| 154 |
data = &nve0_args; |
| 155 |
} |
| 156 |
|
| 157 |
ret = nouveau_object_new(&screen->device->object, 0, |
| 158 |
NOUVEAU_FIFO_CHANNEL_CLASS, |
| 159 |
data, size, &dec->channel[i]); |
| 160 |
|
| 161 |
if (!ret) |
| 162 |
ret = nouveau_pushbuf_create(screen, &nvc0->base, nvc0->base.client, dec->channel[i], |
| 163 |
4, 32 * 1024, &dec->pushbuf[i]); |
| 164 |
if (ret) |
| 165 |
break; |
| 166 |
} |
| 167 |
push = dec->pushbuf; |
| 168 |
|
| 169 |
if (!kepler) { |
| 170 |
if (!ret) |
| 171 |
ret = nouveau_object_new(dec->channel[0], 0x390b1, 0x90b1, NULL, 0, &dec->bsp); |
| 172 |
if (!ret) |
| 173 |
ret = nouveau_object_new(dec->channel[1], 0x190b2, 0x90b2, NULL, 0, &dec->vp); |
| 174 |
if (!ret) |
| 175 |
ret = nouveau_object_new(dec->channel[2], 0x290b3, 0x90b3, NULL, 0, &dec->ppp); |
| 176 |
} else { |
| 177 |
if (!ret) |
| 178 |
ret = nouveau_object_new(dec->channel[0], 0x95b1, 0x95b1, NULL, 0, &dec->bsp); |
| 179 |
if (!ret) |
| 180 |
ret = nouveau_object_new(dec->channel[1], 0x95b2, 0x95b2, NULL, 0, &dec->vp); |
| 181 |
if (!ret) |
| 182 |
ret = nouveau_object_new(dec->channel[2], 0x90b3, 0x90b3, NULL, 0, &dec->ppp); |
| 183 |
} |
| 184 |
if (ret) |
| 185 |
goto fail; |
| 186 |
|
| 187 |
BEGIN_NVC0(push[0], SUBC_BSP(NV01_SUBCHAN_OBJECT), 1); |
| 188 |
PUSH_DATA (push[0], dec->bsp->handle); |
| 189 |
|
| 190 |
BEGIN_NVC0(push[1], SUBC_VP(NV01_SUBCHAN_OBJECT), 1); |
| 191 |
PUSH_DATA (push[1], dec->vp->handle); |
| 192 |
|
| 193 |
BEGIN_NVC0(push[2], SUBC_PPP(NV01_SUBCHAN_OBJECT), 1); |
| 194 |
PUSH_DATA (push[2], dec->ppp->handle); |
| 195 |
|
| 196 |
dec->base.context = context; |
| 197 |
dec->base.begin_frame = nvc0_decoder_begin_frame; |
| 198 |
dec->base.decode_bitstream = nvc0_decoder_decode_bitstream; |
| 199 |
dec->base.end_frame = nvc0_decoder_end_frame; |
| 200 |
|
| 201 |
for (i = 0; i < NOUVEAU_VP3_VIDEO_QDEPTH && !ret; ++i) |
| 202 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, |
| 203 |
0, 1 << 20, &cfg, &dec->bsp_bo[i]); |
| 204 |
if (!ret) { |
| 205 |
/* total fudge factor... just has to be bigger for higher bitrates? */ |
| 206 |
unsigned inter_size = align(templ->width * templ->height * 2, 4 << 20); |
| 207 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, |
| 208 |
0x100, inter_size, &cfg, &dec->inter_bo[0]); |
| 209 |
} |
| 210 |
if (!ret) { |
| 211 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, |
| 212 |
0x100, dec->inter_bo[0]->size, &cfg, |
| 213 |
&dec->inter_bo[1]); |
| 214 |
} |
| 215 |
if (ret) |
| 216 |
goto fail; |
| 217 |
switch (u_reduce_video_profile(templ->profile)) { |
| 218 |
case PIPE_VIDEO_FORMAT_MPEG12: { |
| 219 |
codec = 1; |
| 220 |
assert(templ->max_references <= 2); |
| 221 |
break; |
| 222 |
} |
| 223 |
case PIPE_VIDEO_FORMAT_VC1: { |
| 224 |
ppp_codec = codec = 2; |
| 225 |
tmp_size = mb(templ->height)*16 * mb(templ->width)*16; |
| 226 |
assert(templ->max_references <= 2); |
| 227 |
break; |
| 228 |
} |
| 229 |
case PIPE_VIDEO_FORMAT_MPEG4_AVC: { |
| 230 |
codec = 3; |
| 231 |
dec->tmp_stride = 16 * mb_half(templ->width) * nouveau_vp3_video_align(templ->height) * 3 / 2; |
| 232 |
tmp_size = dec->tmp_stride * (templ->max_references + 1); |
| 233 |
assert(templ->max_references <= 16); |
| 234 |
break; |
| 235 |
} |
| 236 |
default: |
| 237 |
fprintf(stderr, "invalid codec\n"); |
| 238 |
goto fail; |
| 239 |
} |
| 240 |
|
| 241 |
if (screen->device->chipset < 0xd0) { |
| 242 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, 0, |
| 243 |
0x4000, &cfg, &dec->fw_bo); |
| 244 |
if (ret) |
| 245 |
goto fail; |
| 246 |
|
| 247 |
ret = nouveau_vp3_load_firmware(dec, templ->profile, screen->device->chipset); |
| 248 |
if (ret) |
| 249 |
goto fw_fail; |
| 250 |
} |
| 251 |
|
| 252 |
if (codec != 3) { |
| 253 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, 0, |
| 254 |
0x400, &cfg, &dec->bitplane_bo); |
| 255 |
if (ret) |
| 256 |
goto fail; |
| 257 |
} |
| 258 |
|
| 259 |
dec->ref_stride = mb(templ->width)*16 * (mb_half(templ->height)*32 + nouveau_vp3_video_align(templ->height)/2); |
| 260 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, 0, |
| 261 |
dec->ref_stride * (templ->max_references+2) + tmp_size, |
| 262 |
&cfg, &dec->ref_bo); |
| 263 |
if (ret) |
| 264 |
goto fail; |
| 265 |
|
| 266 |
timeout = 0; |
| 267 |
|
| 268 |
BEGIN_NVC0(push[0], SUBC_BSP(0x200), 2); |
| 269 |
PUSH_DATA (push[0], codec); |
| 270 |
PUSH_DATA (push[0], timeout); |
| 271 |
|
| 272 |
BEGIN_NVC0(push[1], SUBC_VP(0x200), 2); |
| 273 |
PUSH_DATA (push[1], codec); |
| 274 |
PUSH_DATA (push[1], timeout); |
| 275 |
|
| 276 |
BEGIN_NVC0(push[2], SUBC_PPP(0x200), 2); |
| 277 |
PUSH_DATA (push[2], ppp_codec); |
| 278 |
PUSH_DATA (push[2], timeout); |
| 279 |
|
| 280 |
++dec->fence_seq; |
| 281 |
|
| 282 |
#if NOUVEAU_VP3_DEBUG_FENCE |
| 283 |
ret = nouveau_bo_new(screen->device, NOUVEAU_BO_GART|NOUVEAU_BO_MAP, |
| 284 |
0, 0x1000, NULL, &dec->fence_bo); |
| 285 |
if (ret) |
| 286 |
goto fail; |
| 287 |
|
| 288 |
BO_MAP(screen, dec->fence_bo, NOUVEAU_BO_RDWR, screen->client); |
| 289 |
dec->fence_map = dec->fence_bo->map; |
| 290 |
dec->fence_map[0] = dec->fence_map[4] = dec->fence_map[8] = 0; |
| 291 |
dec->comm = (struct comm *)(dec->fence_map + (COMM_OFFSET/sizeof(*dec->fence_map))); |
| 292 |
|
| 293 |
/* So lets test if the fence is working? */ |
| 294 |
PUSH_SPACE_EX(push[0], 16, 1, 0); |
| 295 |
PUSH_REF1 (push[0], dec->fence_bo, NOUVEAU_BO_GART|NOUVEAU_BO_RDWR); |
| 296 |
BEGIN_NVC0(push[0], SUBC_BSP(0x240), 3); |
| 297 |
PUSH_DATAh(push[0], dec->fence_bo->offset); |
| 298 |
PUSH_DATA (push[0], dec->fence_bo->offset); |
| 299 |
PUSH_DATA (push[0], dec->fence_seq); |
| 300 |
|
| 301 |
BEGIN_NVC0(push[0], SUBC_BSP(0x304), 1); |
| 302 |
PUSH_DATA (push[0], 0); |
| 303 |
PUSH_KICK (push[0]); |
| 304 |
|
| 305 |
PUSH_SPACE_EX(push[1], 16, 1, 0); |
| 306 |
PUSH_REF1 (push[1], dec->fence_bo, NOUVEAU_BO_GART|NOUVEAU_BO_RDWR); |
| 307 |
BEGIN_NVC0(push[1], SUBC_VP(0x240), 3); |
| 308 |
PUSH_DATAh(push[1], (dec->fence_bo->offset + 0x10)); |
| 309 |
PUSH_DATA (push[1], (dec->fence_bo->offset + 0x10)); |
| 310 |
PUSH_DATA (push[1], dec->fence_seq); |
| 311 |
|
| 312 |
BEGIN_NVC0(push[1], SUBC_VP(0x304), 1); |
| 313 |
PUSH_DATA (push[1], 0); |
| 314 |
PUSH_KICK (push[1]); |
| 315 |
|
| 316 |
PUSH_SPACE_EX(push[2], 16, 1, 0); |
| 317 |
PUSH_REF1 (push[2], dec->fence_bo, NOUVEAU_BO_GART|NOUVEAU_BO_RDWR); |
| 318 |
BEGIN_NVC0(push[2], SUBC_PPP(0x240), 3); |
| 319 |
PUSH_DATAh(push[2], (dec->fence_bo->offset + 0x20)); |
| 320 |
PUSH_DATA (push[2], (dec->fence_bo->offset + 0x20)); |
| 321 |
PUSH_DATA (push[2], dec->fence_seq); |
| 322 |
|
| 323 |
BEGIN_NVC0(push[2], SUBC_PPP(0x304), 1); |
| 324 |
PUSH_DATA (push[2], 0); |
| 325 |
PUSH_KICK (push[2]); |
| 326 |
|
| 327 |
usleep(100); |
| 328 |
while (dec->fence_seq > dec->fence_map[0] || |
| 329 |
dec->fence_seq > dec->fence_map[4] || |
| 330 |
dec->fence_seq > dec->fence_map[8]) { |
| 331 |
debug_printf("%u: %u %u %u\n", dec->fence_seq, dec->fence_map[0], dec->fence_map[4], dec->fence_map[8]); |
| 332 |
usleep(100); |
| 333 |
} |
| 334 |
debug_printf("%u: %u %u %u\n", dec->fence_seq, dec->fence_map[0], dec->fence_map[4], dec->fence_map[8]); |
| 335 |
#endif |
| 336 |
|
| 337 |
return &dec->base; |
| 338 |
|
| 339 |
fw_fail: |
| 340 |
debug_printf("Cannot create decoder without firmware..\n"); |
| 341 |
dec->base.destroy(&dec->base); |
| 342 |
return NULL; |
| 343 |
|
| 344 |
fail: |
| 345 |
debug_printf("Creation failed: %s (%i)\n", strerror(-ret), ret); |
| 346 |
dec->base.destroy(&dec->base); |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
|
| 350 |
struct pipe_video_buffer * |
| 351 |
nvc0_video_buffer_create(struct pipe_context *pipe, |
| 352 |
const struct pipe_video_buffer *templat) |
| 353 |
{ |
| 354 |
return nouveau_vp3_video_buffer_create( |
| 355 |
pipe, templat, NVC0_RESOURCE_FLAG_VIDEO); |
| 356 |
} |