[g3dvl] start handling motion_vertical_field_select
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / surface.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29 #include <X11/Xlibint.h>
30 #include <vl_winsys.h>
31 #include <pipe/p_video_context.h>
32 #include <pipe/p_video_state.h>
33 #include <pipe/p_state.h>
34 #include <util/u_inlines.h>
35 #include <util/u_memory.h>
36 #include <util/u_math.h>
37 #include "xvmc_private.h"
38
39 static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
40 {
41 if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
42 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
43 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
44 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
45 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
46 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
47 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
48 return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
49
50 assert(0);
51
52 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized mb type 0x%08X.\n", xvmc_mb_type);
53
54 return -1;
55 }
56
57 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
58 {
59 switch (xvmc_pic) {
60 case XVMC_TOP_FIELD:
61 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
62 case XVMC_BOTTOM_FIELD:
63 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
64 case XVMC_FRAME_PICTURE:
65 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
66 default:
67 assert(0);
68 }
69
70 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic);
71
72 return -1;
73 }
74
75 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, unsigned int xvmc_picture_structure)
76 {
77 switch (xvmc_motion_type) {
78 case XVMC_PREDICTION_FRAME:
79 if (xvmc_picture_structure == XVMC_FRAME_PICTURE)
80 return PIPE_MPEG12_MOTION_TYPE_FRAME;
81 else
82 return PIPE_MPEG12_MOTION_TYPE_16x8;
83 break;
84 case XVMC_PREDICTION_FIELD:
85 return PIPE_MPEG12_MOTION_TYPE_FIELD;
86 case XVMC_PREDICTION_DUAL_PRIME:
87 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
88 default:
89 assert(0);
90 }
91
92 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized motion type 0x%08X (with picture structure 0x%08X).\n", xvmc_motion_type, xvmc_picture_structure);
93
94 return -1;
95 }
96
97 #if 0
98 static bool
99 CreateOrResizeBackBuffer(struct vl_context *vctx, unsigned int width, unsigned int height,
100 struct pipe_surface **backbuffer)
101 {
102 struct pipe_video_context *vpipe;
103 struct pipe_resource template;
104 struct pipe_resource *tex;
105
106 assert(vctx);
107
108 vpipe = vctx->vpipe;
109
110 if (*backbuffer) {
111 if ((*backbuffer)->width != width || (*backbuffer)->height != height)
112 pipe_surface_reference(backbuffer, NULL);
113 else
114 return true;
115 }
116
117 memset(&template, 0, sizeof(struct pipe_resource));
118 template.target = PIPE_TEXTURE_2D;
119 template.format = vctx->vscreen->format;
120 template.last_level = 0;
121 template.width0 = width;
122 template.height0 = height;
123 template.depth0 = 1;
124 template.usage = PIPE_USAGE_DEFAULT;
125 template.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_BLIT_SOURCE;
126 template.flags = 0;
127
128 tex = vpipe->screen->resource_create(vpipe->screen, &template);
129 if (!tex)
130 return false;
131
132 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
133 template.bind);
134 pipe_resource_reference(&tex, NULL);
135
136 if (!*backbuffer)
137 return false;
138
139 /* Clear the backbuffer in case the video doesn't cover the whole window */
140 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
141 vpipe->surface_fill(vpipe, *backbuffer, 0, 0, width, height, 0);
142
143 return true;
144 }
145 #endif
146
147 static void
148 MacroBlocksToPipe(struct pipe_screen *screen,
149 unsigned int xvmc_picture_structure,
150 const XvMCMacroBlockArray *xvmc_macroblocks,
151 const XvMCBlockArray *xvmc_blocks,
152 unsigned int first_macroblock,
153 unsigned int num_macroblocks,
154 struct pipe_mpeg12_macroblock *pipe_macroblocks)
155 {
156 unsigned int i, j, k, l;
157 XvMCMacroBlock *xvmc_mb;
158
159 assert(xvmc_macroblocks);
160 assert(xvmc_blocks);
161 assert(pipe_macroblocks);
162 assert(num_macroblocks);
163
164 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
165
166 for (i = 0; i < num_macroblocks; ++i) {
167 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
168 pipe_macroblocks->mbx = xvmc_mb->x;
169 pipe_macroblocks->mby = xvmc_mb->y;
170 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
171 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
172 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_picture_structure);
173 /* Get rid of Valgrind 'undefined' warnings */
174 else
175 pipe_macroblocks->mo_type = -1;
176 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
177 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
178
179 for (j = 0; j < 2; ++j)
180 for (k = 0; k < 2; ++k)
181 for (l = 0; l < 2; ++l)
182 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
183
184 pipe_macroblocks->mvfs[0][0] = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_FIRST_FORWARD;
185 pipe_macroblocks->mvfs[0][1] = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_FIRST_BACKWARD;
186 pipe_macroblocks->mvfs[1][0] = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_SECOND_FORWARD;
187 pipe_macroblocks->mvfs[1][1] = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_SECOND_BACKWARD;
188
189 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
190 pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
191
192 ++pipe_macroblocks;
193 ++xvmc_mb;
194 }
195 }
196
197 PUBLIC
198 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
199 {
200 XvMCContextPrivate *context_priv;
201 struct pipe_video_context *vpipe;
202 XvMCSurfacePrivate *surface_priv;
203 struct pipe_resource template;
204 struct pipe_resource *vsfc_tex;
205 struct pipe_surface *vsfc;
206
207 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface);
208
209 assert(dpy);
210
211 if (!context)
212 return XvMCBadContext;
213 if (!surface)
214 return XvMCBadSurface;
215
216 context_priv = context->privData;
217 vpipe = context_priv->vctx->vpipe;
218
219 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
220 if (!surface_priv)
221 return BadAlloc;
222
223 memset(&template, 0, sizeof(struct pipe_resource));
224 template.target = PIPE_TEXTURE_2D;
225 template.format = (enum pipe_format)vpipe->get_param(vpipe, PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT);
226 template.last_level = 0;
227 if (vpipe->is_format_supported(vpipe, template.format,
228 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
229 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO)) {
230 template.width0 = context->width;
231 template.height0 = context->height;
232 }
233 else {
234 assert(vpipe->is_format_supported(vpipe, template.format,
235 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
236 PIPE_TEXTURE_GEOM_NON_SQUARE));
237 template.width0 = util_next_power_of_two(context->width);
238 template.height0 = util_next_power_of_two(context->height);
239 }
240 template.depth0 = 1;
241 template.usage = PIPE_USAGE_DEFAULT;
242 template.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
243 template.flags = 0;
244 vsfc_tex = vpipe->screen->resource_create(vpipe->screen, &template);
245 if (!vsfc_tex) {
246 FREE(surface_priv);
247 return BadAlloc;
248 }
249
250 vsfc = vpipe->screen->get_tex_surface(vpipe->screen, vsfc_tex, 0, 0, 0,
251 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
252 pipe_resource_reference(&vsfc_tex, NULL);
253 if (!vsfc) {
254 FREE(surface_priv);
255 return BadAlloc;
256 }
257
258 surface_priv->pipe_vsfc = vsfc;
259 surface_priv->context = context;
260
261 surface->surface_id = XAllocID(dpy);
262 surface->context_id = context->context_id;
263 surface->surface_type_id = context->surface_type_id;
264 surface->width = context->width;
265 surface->height = context->height;
266 surface->privData = surface_priv;
267
268 SyncHandle();
269
270 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p created.\n", surface);
271
272 return Success;
273 }
274
275 PUBLIC
276 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
277 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
278 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
279 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
280 )
281 {
282 struct pipe_video_context *vpipe;
283 struct pipe_surface *t_vsfc;
284 struct pipe_surface *p_vsfc;
285 struct pipe_surface *f_vsfc;
286 XvMCContextPrivate *context_priv;
287 XvMCSurfacePrivate *target_surface_priv;
288 XvMCSurfacePrivate *past_surface_priv;
289 XvMCSurfacePrivate *future_surface_priv;
290 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
291
292 XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p.\n", target_surface);
293
294 assert(dpy);
295
296 if (!context || !context->privData)
297 return XvMCBadContext;
298 if (!target_surface || !target_surface->privData)
299 return XvMCBadSurface;
300
301 if (picture_structure != XVMC_TOP_FIELD &&
302 picture_structure != XVMC_BOTTOM_FIELD &&
303 picture_structure != XVMC_FRAME_PICTURE)
304 return BadValue;
305 /* Bkwd pred equivalent to fwd (past && !future) */
306 if (future_surface && !past_surface)
307 return BadMatch;
308
309 assert(context->context_id == target_surface->context_id);
310 assert(!past_surface || context->context_id == past_surface->context_id);
311 assert(!future_surface || context->context_id == future_surface->context_id);
312
313 assert(macroblocks);
314 assert(blocks);
315
316 assert(macroblocks->context_id == context->context_id);
317 assert(blocks->context_id == context->context_id);
318
319 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
320
321 target_surface_priv = target_surface->privData;
322 past_surface_priv = past_surface ? past_surface->privData : NULL;
323 future_surface_priv = future_surface ? future_surface->privData : NULL;
324
325 assert(target_surface_priv->context == context);
326 assert(!past_surface || past_surface_priv->context == context);
327 assert(!future_surface || future_surface_priv->context == context);
328
329 context_priv = context->privData;
330 vpipe = context_priv->vctx->vpipe;
331
332 t_vsfc = target_surface_priv->pipe_vsfc;
333 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
334 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
335
336 MacroBlocksToPipe(vpipe->screen, picture_structure, macroblocks, blocks, first_macroblock,
337 num_macroblocks, pipe_macroblocks);
338
339 vpipe->set_decode_target(vpipe, t_vsfc);
340 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
341 &pipe_macroblocks->base, &target_surface_priv->render_fence);
342
343 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface);
344
345 return Success;
346 }
347
348 PUBLIC
349 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
350 {
351 assert(dpy);
352
353 if (!surface)
354 return XvMCBadSurface;
355
356 return Success;
357 }
358
359 PUBLIC
360 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
361 {
362 assert(dpy);
363
364 if (!surface)
365 return XvMCBadSurface;
366
367 return Success;
368 }
369
370 PUBLIC
371 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
372 short srcx, short srcy, unsigned short srcw, unsigned short srch,
373 short destx, short desty, unsigned short destw, unsigned short desth,
374 int flags)
375 {
376 struct pipe_video_context *vpipe;
377 XvMCSurfacePrivate *surface_priv;
378 XvMCContextPrivate *context_priv;
379 XvMCSubpicturePrivate *subpicture_priv;
380 XvMCContext *context;
381 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
382 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
383 struct pipe_surface *drawable_surface;
384
385 XVMC_MSG(XVMC_TRACE, "[XvMC] Displaying surface %p.\n", surface);
386
387 assert(dpy);
388
389 if (!surface || !surface->privData)
390 return XvMCBadSurface;
391
392 surface_priv = surface->privData;
393 context = surface_priv->context;
394 context_priv = context->privData;
395
396 drawable_surface = vl_drawable_surface_get(context_priv->vctx->vscreen, drawable);
397 if (!drawable_surface)
398 return BadDrawable;
399
400 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
401 assert(srcx + srcw - 1 < surface->width);
402 assert(srcy + srch - 1 < surface->height);
403 /*
404 * Some apps (mplayer) hit these asserts because they call
405 * this function after the window has been resized by the WM
406 * but before they've handled the corresponding XEvent and
407 * know about the new dimensions. The output should be clipped
408 * until the app updates destw and desth.
409 */
410 /*
411 assert(destx + destw - 1 < drawable_surface->width);
412 assert(desty + desth - 1 < drawable_surface->height);
413 */
414
415 subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL;
416 vpipe = context_priv->vctx->vpipe;
417
418 #if 0
419 if (!CreateOrResizeBackBuffer(context_priv->vctx, width, height, &context_priv->backbuffer))
420 return BadAlloc;
421 #endif
422
423 if (subpicture_priv) {
424 struct pipe_video_rect src_rect = {surface_priv->subx, surface_priv->suby, surface_priv->subw, surface_priv->subh};
425 struct pipe_video_rect dst_rect = {surface_priv->surfx, surface_priv->surfy, surface_priv->surfw, surface_priv->surfh};
426 struct pipe_video_rect *src_rects[1] = {&src_rect};
427 struct pipe_video_rect *dst_rects[1] = {&dst_rect};
428
429 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture);
430
431 assert(subpicture_priv->surface == surface);
432 vpipe->set_picture_layers(vpipe, &subpicture_priv->sfc, src_rects, dst_rects, 1);
433
434 surface_priv->subpicture = NULL;
435 subpicture_priv->surface = NULL;
436 }
437 else
438 vpipe->set_picture_layers(vpipe, NULL, NULL, NULL, 0);
439
440 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
441 drawable_surface, &dst_rect, &surface_priv->disp_fence);
442
443 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface);
444
445 vpipe->screen->flush_frontbuffer
446 (
447 vpipe->screen,
448 drawable_surface,
449 vl_contextprivate_get(context_priv->vctx, drawable_surface)
450 );
451
452 pipe_surface_reference(&drawable_surface, NULL);
453
454 XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface);
455
456 return Success;
457 }
458
459 PUBLIC
460 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
461 {
462 assert(dpy);
463
464 if (!surface)
465 return XvMCBadSurface;
466
467 assert(status);
468
469 *status = 0;
470
471 return Success;
472 }
473
474 PUBLIC
475 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
476 {
477 XvMCSurfacePrivate *surface_priv;
478
479 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying surface %p.\n", surface);
480
481 assert(dpy);
482
483 if (!surface || !surface->privData)
484 return XvMCBadSurface;
485
486 surface_priv = surface->privData;
487 pipe_surface_reference(&surface_priv->pipe_vsfc, NULL);
488 FREE(surface_priv);
489 surface->privData = NULL;
490
491 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p destroyed.\n", surface);
492
493 return Success;
494 }
495
496 PUBLIC
497 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
498 {
499 assert(dpy);
500
501 if (!surface || !surface->privData)
502 return XvMCBadSurface;
503
504 /* No op, only for overlaid rendering */
505
506 return Success;
507 }