069481de207a97a6258a9793065d91c540566030
[mesa.git] / src / gallium / drivers / nouveau / nv50 / nv98_video.c
1 /*
2 * Copyright 2011-2013 Maarten Lankhorst, 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 "nv50/nv98_video.h"
24
25 #include "util/u_sampler.h"
26 #include "util/u_format.h"
27
28 static void
29 nv98_decoder_decode_bitstream(struct pipe_video_codec *decoder,
30 struct pipe_video_buffer *video_target,
31 struct pipe_picture_desc *picture,
32 unsigned num_buffers,
33 const void *const *data,
34 const unsigned *num_bytes)
35 {
36 struct nouveau_vp3_decoder *dec = (struct nouveau_vp3_decoder *)decoder;
37 struct nouveau_vp3_video_buffer *target = (struct nouveau_vp3_video_buffer *)video_target;
38 uint32_t comm_seq = ++dec->fence_seq;
39 union pipe_desc desc;
40
41 unsigned vp_caps, is_ref, ret;
42 struct nouveau_vp3_video_buffer *refs[16] = {};
43
44 desc.base = picture;
45
46 assert(target->base.buffer_format == PIPE_FORMAT_NV12);
47
48 ret = nv98_decoder_bsp(dec, desc, target, comm_seq,
49 num_buffers, data, num_bytes,
50 &vp_caps, &is_ref, refs);
51
52 /* did we decode bitstream correctly? */
53 assert(ret == 2);
54
55 nv98_decoder_vp(dec, desc, target, comm_seq, vp_caps, is_ref, refs);
56 nv98_decoder_ppp(dec, desc, target, comm_seq);
57 }
58
59 struct pipe_video_codec *
60 nv98_create_decoder(struct pipe_context *context,
61 const struct pipe_video_codec *templ)
62 {
63 struct nouveau_screen *screen = &((struct nv50_context *)context)->screen->base;
64 struct nouveau_vp3_decoder *dec;
65 struct nouveau_pushbuf **push;
66 struct nv04_fifo nv04_data = {.vram = 0xbeef0201, .gart = 0xbeef0202};
67 union nouveau_bo_config cfg;
68
69 cfg.nv50.tile_mode = 0x20;
70 cfg.nv50.memtype = 0x70;
71
72 int ret, i;
73 uint32_t codec = 1, ppp_codec = 3;
74 uint32_t timeout;
75 u32 tmp_size = 0;
76
77 if (getenv("XVMC_VL"))
78 return vl_create_decoder(context, templ);
79
80 if (templ->entrypoint != PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
81 debug_printf("%x\n", templ->entrypoint);
82 return NULL;
83 }
84
85 dec = CALLOC_STRUCT(nouveau_vp3_decoder);
86 if (!dec)
87 return NULL;
88 dec->client = screen->client;
89 dec->base = *templ;
90 nouveau_vp3_decoder_init_common(&dec->base);
91
92 dec->bsp_idx = 5;
93 dec->vp_idx = 6;
94 dec->ppp_idx = 7;
95
96 ret = nouveau_object_new(&screen->device->object, 0,
97 NOUVEAU_FIFO_CHANNEL_CLASS,
98 &nv04_data, sizeof(nv04_data), &dec->channel[0]);
99
100 if (!ret)
101 ret = nouveau_pushbuf_new(screen->client, dec->channel[0], 4,
102 32 * 1024, true, &dec->pushbuf[0]);
103
104 for (i = 1; i < 3; ++i) {
105 dec->channel[i] = dec->channel[0];
106 dec->pushbuf[i] = dec->pushbuf[0];
107 }
108 push = dec->pushbuf;
109
110 if (!ret)
111 ret = nouveau_object_new(dec->channel[0], 0x390b1, 0x85b1, NULL, 0, &dec->bsp);
112 if (!ret)
113 ret = nouveau_object_new(dec->channel[1], 0x190b2, 0x85b2, NULL, 0, &dec->vp);
114 if (!ret)
115 ret = nouveau_object_new(dec->channel[2], 0x290b3, 0x85b3, NULL, 0, &dec->ppp);
116 if (ret)
117 goto fail;
118
119 BEGIN_NV04(push[0], SUBC_BSP(NV01_SUBCHAN_OBJECT), 1);
120 PUSH_DATA (push[0], dec->bsp->handle);
121
122 BEGIN_NV04(push[0], SUBC_BSP(0x180), 5);
123 for (i = 0; i < 5; i++)
124 PUSH_DATA (push[0], nv04_data.vram);
125
126 BEGIN_NV04(push[1], SUBC_VP(NV01_SUBCHAN_OBJECT), 1);
127 PUSH_DATA (push[1], dec->vp->handle);
128
129 BEGIN_NV04(push[1], SUBC_VP(0x180), 6);
130 for (i = 0; i < 6; i++)
131 PUSH_DATA (push[1], nv04_data.vram);
132
133 BEGIN_NV04(push[2], SUBC_PPP(NV01_SUBCHAN_OBJECT), 1);
134 PUSH_DATA (push[2], dec->ppp->handle);
135
136 BEGIN_NV04(push[2], SUBC_PPP(0x180), 5);
137 for (i = 0; i < 5; i++)
138 PUSH_DATA (push[2], nv04_data.vram);
139
140 dec->base.context = context;
141 dec->base.decode_bitstream = nv98_decoder_decode_bitstream;
142
143 for (i = 0; i < NOUVEAU_VP3_VIDEO_QDEPTH && !ret; ++i)
144 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM,
145 0, 1 << 20, NULL, &dec->bsp_bo[i]);
146 if (!ret)
147 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM,
148 0x100, 4 << 20, NULL, &dec->inter_bo[0]);
149 if (!ret)
150 nouveau_bo_ref(dec->inter_bo[0], &dec->inter_bo[1]);
151 if (ret)
152 goto fail;
153
154 switch (u_reduce_video_profile(templ->profile)) {
155 case PIPE_VIDEO_FORMAT_MPEG12: {
156 codec = 1;
157 assert(templ->max_references <= 2);
158 break;
159 }
160 case PIPE_VIDEO_FORMAT_MPEG4: {
161 codec = 4;
162 tmp_size = mb(templ->height)*16 * mb(templ->width)*16;
163 assert(templ->max_references <= 2);
164 break;
165 }
166 case PIPE_VIDEO_FORMAT_VC1: {
167 ppp_codec = codec = 2;
168 tmp_size = mb(templ->height)*16 * mb(templ->width)*16;
169 assert(templ->max_references <= 2);
170 break;
171 }
172 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
173 codec = 3;
174 dec->tmp_stride = 16 * mb_half(templ->width) * nouveau_vp3_video_align(templ->height) * 3 / 2;
175 tmp_size = dec->tmp_stride * (templ->max_references + 1);
176 assert(templ->max_references <= 16);
177 break;
178 }
179 default:
180 fprintf(stderr, "invalid codec\n");
181 goto fail;
182 }
183
184 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, 0,
185 0x4000, NULL, &dec->fw_bo);
186 if (ret)
187 goto fail;
188
189 ret = nouveau_vp3_load_firmware(dec, templ->profile, screen->device->chipset);
190 if (ret)
191 goto fw_fail;
192
193 if (codec != 3) {
194 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, 0,
195 0x400, NULL, &dec->bitplane_bo);
196 if (ret)
197 goto fail;
198 }
199
200 dec->ref_stride = mb(templ->width)*16 * (mb_half(templ->height)*32 + nouveau_vp3_video_align(templ->height)/2);
201 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM, 0,
202 dec->ref_stride * (templ->max_references+2) + tmp_size,
203 &cfg, &dec->ref_bo);
204 if (ret)
205 goto fail;
206
207 timeout = 0;
208
209 BEGIN_NV04(push[0], SUBC_BSP(0x200), 2);
210 PUSH_DATA (push[0], codec);
211 PUSH_DATA (push[0], timeout);
212
213 BEGIN_NV04(push[1], SUBC_VP(0x200), 2);
214 PUSH_DATA (push[1], codec);
215 PUSH_DATA (push[1], timeout);
216
217 BEGIN_NV04(push[2], SUBC_PPP(0x200), 2);
218 PUSH_DATA (push[2], ppp_codec);
219 PUSH_DATA (push[2], timeout);
220
221 ++dec->fence_seq;
222
223 #if NOUVEAU_VP3_DEBUG_FENCE
224 ret = nouveau_bo_new(screen->device, NOUVEAU_BO_GART|NOUVEAU_BO_MAP,
225 0, 0x1000, NULL, &dec->fence_bo);
226 if (ret)
227 goto fail;
228
229 nouveau_bo_map(dec->fence_bo, NOUVEAU_BO_RDWR, screen->client);
230 dec->fence_map = dec->fence_bo->map;
231 dec->fence_map[0] = dec->fence_map[4] = dec->fence_map[8] = 0;
232 dec->comm = (struct comm *)(dec->fence_map + (COMM_OFFSET/sizeof(*dec->fence_map)));
233
234 /* So lets test if the fence is working? */
235 nouveau_pushbuf_space(push[0], 6, 1, 0);
236 PUSH_REFN (push[0], dec->fence_bo, NOUVEAU_BO_GART|NOUVEAU_BO_RDWR);
237 BEGIN_NV04(push[0], SUBC_BSP(0x240), 3);
238 PUSH_DATAh(push[0], dec->fence_bo->offset);
239 PUSH_DATA (push[0], dec->fence_bo->offset);
240 PUSH_DATA (push[0], dec->fence_seq);
241
242 BEGIN_NV04(push[0], SUBC_BSP(0x304), 1);
243 PUSH_DATA (push[0], 0);
244 PUSH_KICK (push[0]);
245
246 nouveau_pushbuf_space(push[1], 6, 1, 0);
247 PUSH_REFN (push[1], dec->fence_bo, NOUVEAU_BO_GART|NOUVEAU_BO_RDWR);
248 BEGIN_NV04(push[1], SUBC_VP(0x240), 3);
249 PUSH_DATAh(push[1], (dec->fence_bo->offset + 0x10));
250 PUSH_DATA (push[1], (dec->fence_bo->offset + 0x10));
251 PUSH_DATA (push[1], dec->fence_seq);
252
253 BEGIN_NV04(push[1], SUBC_VP(0x304), 1);
254 PUSH_DATA (push[1], 0);
255 PUSH_KICK (push[1]);
256
257 nouveau_pushbuf_space(push[2], 6, 1, 0);
258 PUSH_REFN (push[2], dec->fence_bo, NOUVEAU_BO_GART|NOUVEAU_BO_RDWR);
259 BEGIN_NV04(push[2], SUBC_PPP(0x240), 3);
260 PUSH_DATAh(push[2], (dec->fence_bo->offset + 0x20));
261 PUSH_DATA (push[2], (dec->fence_bo->offset + 0x20));
262 PUSH_DATA (push[2], dec->fence_seq);
263
264 BEGIN_NV04(push[2], SUBC_PPP(0x304), 1);
265 PUSH_DATA (push[2], 0);
266 PUSH_KICK (push[2]);
267
268 usleep(100);
269 while (dec->fence_seq > dec->fence_map[0] ||
270 dec->fence_seq > dec->fence_map[4] ||
271 dec->fence_seq > dec->fence_map[8]) {
272 debug_printf("%u: %u %u %u\n", dec->fence_seq, dec->fence_map[0], dec->fence_map[4], dec->fence_map[8]);
273 usleep(100);
274 }
275 debug_printf("%u: %u %u %u\n", dec->fence_seq, dec->fence_map[0], dec->fence_map[4], dec->fence_map[8]);
276 #endif
277
278 return &dec->base;
279
280 fw_fail:
281 debug_printf("Cannot create decoder without firmware..\n");
282 dec->base.destroy(&dec->base);
283 return NULL;
284
285 fail:
286 debug_printf("Creation failed: %s (%i)\n", strerror(-ret), ret);
287 dec->base.destroy(&dec->base);
288 return NULL;
289 }
290
291 struct pipe_video_buffer *
292 nv98_video_buffer_create(struct pipe_context *pipe,
293 const struct pipe_video_buffer *templat)
294 {
295 return nouveau_vp3_video_buffer_create(
296 pipe, templat, NV50_RESOURCE_FLAG_VIDEO);
297 }