103b705cfSriastradh/***************************************************************************
203b705cfSriastradh
303b705cfSriastradhCopyright 2000 Intel Corporation.  All Rights Reserved.
403b705cfSriastradh
503b705cfSriastradhPermission is hereby granted, free of charge, to any person obtaining a
603b705cfSriastradhcopy of this software and associated documentation files (the
703b705cfSriastradh"Software"), to deal in the Software without restriction, including
803b705cfSriastradhwithout limitation the rights to use, copy, modify, merge, publish,
903b705cfSriastradhdistribute, sub license, and/or sell copies of the Software, and to
1003b705cfSriastradhpermit persons to whom the Software is furnished to do so, subject to
1103b705cfSriastradhthe following conditions:
1203b705cfSriastradh
1303b705cfSriastradhThe above copyright notice and this permission notice (including the
1403b705cfSriastradhnext paragraph) shall be included in all copies or substantial portions
1503b705cfSriastradhof the Software.
1603b705cfSriastradh
1703b705cfSriastradhTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1803b705cfSriastradhOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1903b705cfSriastradhMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2003b705cfSriastradhIN NO EVENT SHALL INTEL, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2103b705cfSriastradhDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2203b705cfSriastradhOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
2303b705cfSriastradhTHE USE OR OTHER DEALINGS IN THE SOFTWARE.
2403b705cfSriastradh
2503b705cfSriastradh**************************************************************************/
2603b705cfSriastradh
2703b705cfSriastradh#ifndef SNA_VIDEO_H
2803b705cfSriastradh#define SNA_VIDEO_H
2903b705cfSriastradh
3003b705cfSriastradh#include <xf86_OSproc.h>
3103b705cfSriastradh#include <xf86xv.h>
3203b705cfSriastradh#include <fourcc.h>
3303b705cfSriastradh
3403b705cfSriastradh#if defined(XvMCExtension) && defined(ENABLE_XVMC)
3503b705cfSriastradh#define SNA_XVMC 1
3603b705cfSriastradh#endif
3703b705cfSriastradh
3803b705cfSriastradh#define FOURCC_XVMC (('C' << 24) + ('M' << 16) + ('V' << 8) + 'X')
3903b705cfSriastradh#define FOURCC_RGB565 ((16 << 24) + ('B' << 16) + ('G' << 8) + 'R')
4003b705cfSriastradh#define FOURCC_RGB888 ((24 << 24) + ('B' << 16) + ('G' << 8) + 'R')
41a7f02474Smrg#ifndef FOURCC_NV12 /* xorg-server 21.x's fourcc.h uses raw number. */
42fe8aea9eSmrg#define FOURCC_NV12 (('2' << 24) + ('1' << 16) + ('V' << 8) + 'N')
43a7f02474Smrg#endif
44fe8aea9eSmrg#define FOURCC_AYUV (('V' << 24) + ('U' << 16) + ('Y' << 8) + 'A')
4503b705cfSriastradh
4603b705cfSriastradh/*
4703b705cfSriastradh * Below, a dummy picture type that is used in XvPutImage
4803b705cfSriastradh * only to do an overlay update.
4903b705cfSriastradh * Introduced for the XvMC client lib.
5003b705cfSriastradh * Defined to have a zero data size.
5103b705cfSriastradh */
5203b705cfSriastradh#define XVMC_YUV { \
5303b705cfSriastradh	FOURCC_XVMC, XvYUV, LSBFirst, \
5403b705cfSriastradh	{'X', 'V', 'M', 'C', 0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71}, \
5503b705cfSriastradh	12, XvPlanar, 3, 0, 0, 0, 0, 8, 8, 8, 1, 2, 2, 1, 2, 2, \
5603b705cfSriastradh	{'Y', 'V', 'U', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \
5703b705cfSriastradh	XvTopToBottom \
5803b705cfSriastradh}
5903b705cfSriastradh
6003b705cfSriastradh#define XVMC_RGB565 { \
6103b705cfSriastradh	FOURCC_RGB565, XvRGB, LSBFirst, \
6203b705cfSriastradh	{'P', 'A', 'S', 'S', 'T', 'H', 'R', 'O', 'U', 'G', 'H', 'R', 'G', 'B', '1', '6'}, \
6303b705cfSriastradh	16, XvPacked, 1, 16, 0x1f<<11, 0x3f<<5, 0x1f<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
6403b705cfSriastradh	{'B', 'G', 'R', 'X', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \
6503b705cfSriastradh	XvTopToBottom \
6603b705cfSriastradh}
6703b705cfSriastradh
6803b705cfSriastradh#define XVMC_RGB888 { \
6903b705cfSriastradh	FOURCC_RGB888, XvRGB, LSBFirst, \
7003b705cfSriastradh	{'P', 'A', 'S', 'S', 'T', 'H', 'R', 'O', 'U', 'G', 'H', 'R', 'G', 'B', '2', '4'}, \
7103b705cfSriastradh	32, XvPacked, 1, 24, 0xff<<16, 0xff<<8, 0xff<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
7203b705cfSriastradh	{'B', 'G', 'R', 'X', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \
7303b705cfSriastradh	XvTopToBottom \
7403b705cfSriastradh}
7503b705cfSriastradh
76fe8aea9eSmrg/* no standard define for this */
77a7f02474Smrg#ifndef XVIMAGE_NV12 /* xorg-server 21.x's fourcc.h uses raw number. */
78fe8aea9eSmrg#define XVIMAGE_NV12 { \
79fe8aea9eSmrg	FOURCC_NV12, XvYUV, LSBFirst,				\
80fe8aea9eSmrg	{'N','V','1','2', 0x00,0x00,0x00,0x10,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71}, \
81fe8aea9eSmrg	12, XvPlanar, 2, 0, 0, 0, 0, 8, 8, 8, 1, 2, 2, 1, 2, 2, \
82fe8aea9eSmrg	{'Y','U','V', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \
83fe8aea9eSmrg	XvTopToBottom \
84fe8aea9eSmrg}
85a7f02474Smrg#endif
86fe8aea9eSmrg
87fe8aea9eSmrg#define XVIMAGE_AYUV { \
88fe8aea9eSmrg	FOURCC_AYUV, XvYUV, LSBFirst, \
89fe8aea9eSmrg	{'A', 'Y', 'U', 'V', 0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71}, \
90fe8aea9eSmrg	32, XvPacked, 1, 0, 0, 0, 0, 8, 8, 8, 1, 1, 1, 1, 1, 1, \
91fe8aea9eSmrg	{'A', 'Y', 'U', 'V', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \
92fe8aea9eSmrg	XvTopToBottom \
93fe8aea9eSmrg}
94fe8aea9eSmrg
9503b705cfSriastradhstruct sna_video {
9603b705cfSriastradh	struct sna *sna;
9703b705cfSriastradh
98fe8aea9eSmrg	int idx; /* XXX expose struct plane instead? */
99fe8aea9eSmrg
10003b705cfSriastradh	int brightness;
10103b705cfSriastradh	int contrast;
10203b705cfSriastradh	int saturation;
10303b705cfSriastradh	xf86CrtcPtr desired_crtc;
10403b705cfSriastradh
10503b705cfSriastradh	uint32_t gamma0;
10603b705cfSriastradh	uint32_t gamma1;
10703b705cfSriastradh	uint32_t gamma2;
10803b705cfSriastradh	uint32_t gamma3;
10903b705cfSriastradh	uint32_t gamma4;
11003b705cfSriastradh	uint32_t gamma5;
11103b705cfSriastradh
11242542f5fSchristos	unsigned color_key;
11342542f5fSchristos	unsigned color_key_changed;
11442542f5fSchristos	bool has_color_key;
11503b705cfSriastradh
116fe8aea9eSmrg	unsigned colorspace;
117fe8aea9eSmrg	unsigned colorspace_changed;
118fe8aea9eSmrg
11903b705cfSriastradh	/** YUV data buffers */
12003b705cfSriastradh	struct kgem_bo *old_buf[2];
12103b705cfSriastradh	struct kgem_bo *buf;
12242542f5fSchristos	int width, height, format;
12303b705cfSriastradh
12403b705cfSriastradh	int alignment;
12503b705cfSriastradh	bool tiled;
12603b705cfSriastradh	bool textured;
12742542f5fSchristos
12842542f5fSchristos	struct kgem_bo *bo[4];
12942542f5fSchristos	RegionRec clip;
13003b705cfSriastradh
13103b705cfSriastradh	int SyncToVblank;	/* -1: auto, 0: off, 1: on */
13203b705cfSriastradh	int AlwaysOnTop;
13303b705cfSriastradh};
13403b705cfSriastradh
13503b705cfSriastradhstruct sna_video_frame {
13603b705cfSriastradh	struct kgem_bo *bo;
13703b705cfSriastradh	uint32_t id;
13803b705cfSriastradh	uint32_t size;
13903b705cfSriastradh	uint32_t UBufOffset;
14003b705cfSriastradh	uint32_t VBufOffset;
14142542f5fSchristos	Rotation rotation;
14203b705cfSriastradh
14303b705cfSriastradh	uint16_t width, height;
14403b705cfSriastradh	uint16_t pitch[2];
14503b705cfSriastradh
14603b705cfSriastradh	/* extents */
14703b705cfSriastradh	BoxRec image;
14803b705cfSriastradh	BoxRec src;
14903b705cfSriastradh};
15003b705cfSriastradh
15103b705cfSriastradhstatic inline XvScreenPtr to_xv(ScreenPtr screen)
15203b705cfSriastradh{
15303b705cfSriastradh	return dixLookupPrivate(&screen->devPrivates, XvGetScreenKey());
15403b705cfSriastradh}
15503b705cfSriastradh
15603b705cfSriastradhvoid sna_video_init(struct sna *sna, ScreenPtr screen);
15703b705cfSriastradhvoid sna_video_overlay_setup(struct sna *sna, ScreenPtr screen);
15803b705cfSriastradhvoid sna_video_sprite_setup(struct sna *sna, ScreenPtr screen);
15903b705cfSriastradhvoid sna_video_textured_setup(struct sna *sna, ScreenPtr screen);
16003b705cfSriastradhvoid sna_video_destroy_window(WindowPtr win);
16142542f5fSchristosvoid sna_video_close(struct sna *sna);
16203b705cfSriastradh
16303b705cfSriastradhXvAdaptorPtr sna_xv_adaptor_alloc(struct sna *sna);
16403b705cfSriastradhint sna_xv_fixup_formats(ScreenPtr screen,
16503b705cfSriastradh			 XvFormatPtr formats,
16603b705cfSriastradh			 int num_formats);
16703b705cfSriastradhint sna_xv_alloc_port(unsigned long port, XvPortPtr in, XvPortPtr *out);
16803b705cfSriastradhint sna_xv_free_port(XvPortPtr port);
16903b705cfSriastradh
17003b705cfSriastradhstatic inline int xvmc_passthrough(int id)
17103b705cfSriastradh{
17203b705cfSriastradh	switch (id) {
17303b705cfSriastradh	case FOURCC_XVMC:
17403b705cfSriastradh	case FOURCC_RGB565:
17503b705cfSriastradh	case FOURCC_RGB888:
17603b705cfSriastradh		return true;
17703b705cfSriastradh	default:
17803b705cfSriastradh		return false;
17903b705cfSriastradh	}
18003b705cfSriastradh}
18103b705cfSriastradh
18203b705cfSriastradhstatic inline int is_planar_fourcc(int id)
18303b705cfSriastradh{
18403b705cfSriastradh	switch (id) {
18503b705cfSriastradh	case FOURCC_YV12:
18603b705cfSriastradh	case FOURCC_I420:
18703b705cfSriastradh	case FOURCC_XVMC:
188fe8aea9eSmrg	case FOURCC_NV12:
189fe8aea9eSmrg		return 1;
190fe8aea9eSmrg	default:
191fe8aea9eSmrg		return 0;
192fe8aea9eSmrg	}
193fe8aea9eSmrg}
194fe8aea9eSmrg
195fe8aea9eSmrgstatic inline int is_nv12_fourcc(int id)
196fe8aea9eSmrg{
197fe8aea9eSmrg	switch (id) {
198fe8aea9eSmrg	case FOURCC_NV12:
199fe8aea9eSmrg		return 1;
200fe8aea9eSmrg	default:
201fe8aea9eSmrg		return 0;
202fe8aea9eSmrg	}
203fe8aea9eSmrg}
204fe8aea9eSmrg
205fe8aea9eSmrgstatic inline int is_ayuv_fourcc(int id)
206fe8aea9eSmrg{
207fe8aea9eSmrg	switch (id) {
208fe8aea9eSmrg	case FOURCC_AYUV:
20903b705cfSriastradh		return 1;
21003b705cfSriastradh	default:
21103b705cfSriastradh		return 0;
21203b705cfSriastradh	}
21303b705cfSriastradh}
21403b705cfSriastradh
21503b705cfSriastradhbool
21642542f5fSchristossna_video_clip_helper(struct sna_video *video,
21703b705cfSriastradh		      struct sna_video_frame *frame,
21842542f5fSchristos		      xf86CrtcPtr *crtc_ret,
21903b705cfSriastradh		      BoxPtr dst,
22003b705cfSriastradh		      short src_x, short src_y,
22103b705cfSriastradh		      short drw_x, short drw_y,
22203b705cfSriastradh		      short src_w, short src_h,
22303b705cfSriastradh		      short drw_w, short drw_h,
22403b705cfSriastradh		      RegionPtr reg);
22503b705cfSriastradh
22603b705cfSriastradhvoid
22703b705cfSriastradhsna_video_frame_init(struct sna_video *video,
22803b705cfSriastradh		     int id, short width, short height,
22903b705cfSriastradh		     struct sna_video_frame *frame);
23003b705cfSriastradh
23142542f5fSchristosvoid
23242542f5fSchristossna_video_frame_set_rotation(struct sna_video *video,
23342542f5fSchristos			     struct sna_video_frame *frame,
23442542f5fSchristos			     Rotation rotation);
23542542f5fSchristos
23603b705cfSriastradhstruct kgem_bo *
23703b705cfSriastradhsna_video_buffer(struct sna_video *video,
23803b705cfSriastradh		 struct sna_video_frame *frame);
23903b705cfSriastradh
24003b705cfSriastradhbool
24103b705cfSriastradhsna_video_copy_data(struct sna_video *video,
24203b705cfSriastradh		    struct sna_video_frame *frame,
24303b705cfSriastradh		    const uint8_t *buf);
244fe8aea9eSmrgvoid
245fe8aea9eSmrgsna_video_fill_colorkey(struct sna_video *video,
246fe8aea9eSmrg			const RegionRec *clip);
24703b705cfSriastradh
24803b705cfSriastradhvoid sna_video_buffer_fini(struct sna_video *video);
24903b705cfSriastradh
25003b705cfSriastradhvoid sna_video_free_buffers(struct sna_video *video);
25103b705cfSriastradh
25203b705cfSriastradhstatic inline XvPortPtr
25303b705cfSriastradhsna_window_get_port(WindowPtr window)
25403b705cfSriastradh{
25503b705cfSriastradh	return ((void **)__get_private(window, sna_window_key))[2];
25603b705cfSriastradh}
25703b705cfSriastradh
25803b705cfSriastradhstatic inline void
25903b705cfSriastradhsna_window_set_port(WindowPtr window, XvPortPtr port)
26003b705cfSriastradh{
26103b705cfSriastradh	((void **)__get_private(window, sna_window_key))[2] = port;
26203b705cfSriastradh}
26303b705cfSriastradh
264fe8aea9eSmrgstatic inline int offset_and_clip(int x, int dx)
265fe8aea9eSmrg{
266fe8aea9eSmrg	x += dx;
267fe8aea9eSmrg	if (x <= 0)
268fe8aea9eSmrg		return 0;
269fe8aea9eSmrg	if (x >= MAXSHORT)
270fe8aea9eSmrg		return MAXSHORT;
271fe8aea9eSmrg	return x;
272fe8aea9eSmrg}
273fe8aea9eSmrg
274fe8aea9eSmrgstatic inline void init_video_region(RegionRec *region,
275fe8aea9eSmrg				     DrawablePtr draw,
276fe8aea9eSmrg				     int drw_x, int drw_y,
277fe8aea9eSmrg				     int drw_w, int drw_h)
278fe8aea9eSmrg{
279fe8aea9eSmrg	region->extents.x1 = offset_and_clip(draw->x, drw_x);
280fe8aea9eSmrg	region->extents.y1 = offset_and_clip(draw->y, drw_y);
281fe8aea9eSmrg	region->extents.x2 = offset_and_clip(draw->x, drw_x + drw_w);
282fe8aea9eSmrg	region->extents.y2 = offset_and_clip(draw->y, drw_y + drw_h);
283fe8aea9eSmrg	region->data = NULL;
284fe8aea9eSmrg}
285fe8aea9eSmrg
28603b705cfSriastradh#endif /* SNA_VIDEO_H */
287