summaryrefslogtreecommitdiff
path: root/sys/glsink/gstgl_pdrimage.c
blob: 3cc5c57f7be2af98c7551262ffd3b1b1d8ce1dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
/* gcc -ansi -pedantic on GNU/Linux causes warnings and errors
 * unless this is defined:
 * warning: #warning "Files using this header must be compiled with _SVID_SOURCE or _XOPEN_SOURCE"
 */
#ifndef _XOPEN_SOURCE
#  define _XOPEN_SOURCE 1
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#define GL_GLEXT_PROTOTYPES

// VERY dangerous:
#define GL_WRITE_PIXEL_DATA_RANGE_NV 0x8878

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <string.h>

#include "gstglsink.h"

typedef struct _GstGLImageConnection GstGLImageConnection;
struct _GstGLImageConnection {
  GstImageConnection conn;
  Display *dpy;
  gint w, h;
  gint bpp;
  
  int ytex_id;
  int uvtex_id;
  int septex_id; 
  unsigned char *m_memory;
  int m_bufslots[4];
};

#define TEX_XSIZE 1024
#define TEX_YSIZE 1024
#define YUVTEX_SIZE ((TEX_XSIZE * TEX_YSIZE) * 3 /2)

#define AGP_BUFSLOTS 4

typedef struct _GstNvImage GstNvImage;
struct _GstNvImage
{
  GstImageData data;
  int slot; // < AGP_BUFSLOTS: allocated from AGP mem, otherwise from CPU mem
  GstGLImageConnection *conn;
};

static GstGLImageInfo *		gst_gl_nvimage_info			(GstImageInfo *info);
static GstGLImageConnection *	gst_gl_nvimage_connection 		(GstImageConnection *conn);
static gboolean 		gst_gl_nvimage_check_xvideo	();

static GstCaps * 		gst_gl_nvimage_get_caps		(GstImageInfo *info); 
static GstImageConnection *	gst_gl_nvimage_set_caps		(GstImageInfo *info, GstCaps *caps);
static GstImageData *		gst_gl_nvimage_get_image		(GstImageInfo *info, GstImageConnection *conn);
static void 			gst_gl_nvimage_put_image		(GstImageInfo *info, GstImageData *image);
static void 			gst_gl_nvimage_free_image		(GstImageData *image);
static void 			gst_gl_nvimage_open_conn		(GstImageConnection *conn, GstImageInfo *info);
static void 			gst_gl_nvimage_close_conn		(GstImageConnection *conn, GstImageInfo *info);
static void 			gst_gl_nvimage_free_conn		(GstImageConnection *conn);

GstImagePlugin* get_gl_nvimage_plugin(void)
{
  static GstImagePlugin plugin = { gst_gl_nvimage_get_caps,
				   gst_gl_nvimage_set_caps,
				   gst_gl_nvimage_get_image,
				   gst_gl_nvimage_put_image,
    				   gst_gl_nvimage_free_image};

  return &plugin;
}


static GstGLImageInfo *
gst_gl_nvimage_info (GstImageInfo *info)
{
  if (info == NULL || info->id != GST_MAKE_FOURCC ('X', 'l', 'i', 'b'))
  {
    return NULL;
  }
  return (GstGLImageInfo *) info;
}

static GstGLImageConnection *
gst_gl_nvimage_connection (GstImageConnection *conn)
{
  if (conn == NULL || conn->free_conn != gst_gl_nvimage_free_conn)
    return NULL; 
  return (GstGLImageConnection *) conn;
}

gboolean
gst_gl_nvimage_check_xvideo ()
{
  int ver, rel, req, ev, err;
  
#if 0 
  if (display == NULL)
    return FALSE;
  if (Success == XvQueryExtension (display,&ver,&rel,&req,&ev,&err))
    return TRUE;
#endif

  return FALSE;
}

static GstCaps * 	
gst_gl_nvimage_get_caps (GstImageInfo *info)
{
  gint i;
  int adaptors;
  int formats;
  GstCaps *caps = NULL;
  GstGLImageInfo *xinfo = gst_gl_nvimage_info (info);
  
  /* we don't handle these image information */
  if (xinfo == NULL) return NULL;

  if (gst_gl_nvimage_check_xvideo () == FALSE)
  {
    g_warning("GL_NVImage: Server has no NVidia extension support\n"); 
    return NULL;
  }

  caps = gst_caps_append (caps, GST_CAPS_NEW (
					      "xvimage_caps",
					      "video/raw",
					      "format",  GST_PROPS_FOURCC (GST_MAKE_FOURCC ('Y', 'C', '1', '2')),
					      "width",   GST_PROPS_INT_RANGE (0, 1024),
					      "height",  GST_PROPS_INT_RANGE (0, 1024))
			  );
  return caps;
}

static GstImageConnection *
gst_gl_nvimage_set_caps (GstImageInfo *info, GstCaps *caps)
{
  gint i, j = 0;
  int adaptors;
  int formats;
  GstGLImageConnection *conn;
  GstGLImageInfo *xinfo = gst_gl_nvimage_info (info);
  guint32 format;
  
  /* we don't handle these image information */
  if (xinfo == NULL) return NULL;
  
  conn = g_new0 (GstGLImageConnection, 1);
  conn->conn.open_conn = gst_gl_nvimage_open_conn;
  conn->conn.close_conn = gst_gl_nvimage_close_conn;
  conn->conn.free_conn = gst_gl_nvimage_free_conn;

  gst_caps_get (caps, 
		  "width",  &conn->w,
		  "height", &conn->h,
		  "format", &format,
                  NULL);

  // maybe I should a bit more checking here, e.g. maximum size smaller than maximum texture extents
  if (format != GST_MAKE_FOURCC ('R', 'G', 'B', ' ')) 
    { 
      GST_DEBUG (GST_CAT_PLUGIN_INFO, "GL_NVImage: Format is invalid !\n");
      return NULL;
    }
  if (0) //conn->port == (XvPortID) -1)
  {
    /* this happens if the plugin can't handle the caps, so no warning */
    g_free (conn);
    return NULL;
  }
  
  GST_DEBUG (GST_CAT_PLUGIN_INFO, "GL_NVImage: caps %p are ok, creating image", caps);
  return (GstImageConnection *) conn;
}

static GstImageData *
gst_gl_nvimage_get_image (GstImageInfo *info, GstImageConnection *conn)
{
  GstNvImage *image;
  GstGLImageInfo *xinfo = gst_gl_nvimage_info (info);  
  GstGLImageConnection *nvconn = gst_gl_nvimage_connection (conn);  
  int slot = 0;
  
  /* checks */
  if (xinfo == NULL) return NULL;
  if (nvconn == NULL) return NULL;

  // I should also check the current GLX context ! 
  // Ah, Don't have to, I am guarantueed to always be in the same thread

  image = g_new0(GstNvImage, 1);

  for (slot = 0; slot < AGP_BUFSLOTS; slot++)
    {
      if (!nvconn->m_bufslots[slot]) break;
    }

  image->data.size = nvconn->w * nvconn->h * 3/2;

  if (slot < AGP_BUFSLOTS) // found an AGP buffer slot
    {
      image->data.data = nvconn->m_memory + slot * YUVTEX_SIZE; 
      image->slot = slot;  // store for freeing
      nvconn->m_bufslots[slot] = 1; // it is now taken
    }
  else
    {
      g_warning("Allocating from main memory !");
      image->data.data = g_malloc(image->data.size);
      image->slot = AGP_BUFSLOTS; // no AGP slot
    }
  image->conn = nvconn;

  if (image->data.data == NULL)
  {
    g_warning ("GL_NvImage: data allocation failed!");
    g_free (image);
    return NULL;
  }

  return (GstImageData *) image;
}

static void 		
gst_gl_nvimage_put_image (GstImageInfo *info, GstImageData *image)
{
  GstNvImage *im = (GstNvImage *) image;
  GstGLImageInfo *xinfo = gst_gl_nvimage_info (info);  
  
  /* checks omitted for speed (and lazyness), do we need them? */
  g_assert (xinfo != NULL);
  
  /* Upload the texture here */
  g_warning("PUTTING IMAGE - BROOOKEN");

  // both upload the video, and redraw the screen
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  glEnable(GL_TEXTURE_2D);

  glPushMatrix();
  //glTranslatef(0,1,0);
  glRotatef(xinfo->rotX-250,1,0,0);
  glRotatef(xinfo->rotY,0,1,0);
  int zoom = xinfo->zoom;
  glScaled(zoom,zoom,zoom);
  //Draws the surface rectangle

  glBindTexture(GL_TEXTURE_2D, im->conn->ytex_id);
  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im->conn->w, im->conn->h, GL_RGBA, 
  		  GL_UNSIGNED_BYTE, im->data.data);  
  float xmax = (float)im->conn->w/TEX_XSIZE;
  float ymax = (float)im->conn->h/TEX_YSIZE;

  glColor4f(1,1,1,1);
  glBegin(GL_QUADS);

  glNormal3f(0, -1, 0);
  
  glTexCoord2f(xmax, 0);
  glVertex3f(4,0,-4);

  glTexCoord2f(0, 0);
  glVertex3f(-4,0,-4);

  glTexCoord2f(0, ymax);
  glVertex3f(-4,0,4);

  glTexCoord2f(xmax, ymax);
  glVertex3f(4,0,4);

  glEnd();

  glPopMatrix();

  glXSwapBuffers(xinfo->dpy, xinfo->win);
}

static void 		
gst_gl_nvimage_free_image (GstImageData *image)
{
  GstNvImage *im = (GstNvImage *) image;
  g_return_if_fail (im != NULL);
  GstGLImageConnection *nvconn = im->conn;  

  if (im->slot < AGP_BUFSLOTS)
    {
      nvconn->m_bufslots[im->slot] = 0;      
    } 
  else
    g_free(im->data.data);

  g_free (im);
}

static void
gst_gl_nvimage_open_conn (GstImageConnection *conn, GstImageInfo *info)
{
  GstGLImageInfo *xinfo = gst_gl_nvimage_info (info);  
  GstGLImageConnection *xconn = gst_gl_nvimage_connection (conn);

  unsigned char data_sep[2][2] = {{0, 255}, {0, 255}}; 
  int slot;

  g_warning("Opening NVidia Connection");
  xconn->m_memory = (unsigned char*)glXAllocateMemoryNV(AGP_BUFSLOTS*YUVTEX_SIZE, 0, 1.0, 1.0);
  
  if (!xconn->m_memory)
    {
      printf("Unable to acquire graphics card mem... will acquire in normal memory.\n");
      for (slot = 0; slot < AGP_BUFSLOTS; slot++)
	xconn->m_bufslots[slot] = 1;
    }
  else
    {
      // maybe this fast writable memory, awfully slow to read from, though
      glPixelDataRangeNV(GL_WRITE_PIXEL_DATA_RANGE_NV, AGP_BUFSLOTS*YUVTEX_SIZE, xconn->m_memory);
      glEnableClientState(GL_WRITE_PIXEL_DATA_RANGE_NV);

      for (slot = 0; slot < AGP_BUFSLOTS; slot++)
	xconn->m_bufslots[slot] = 0;
    }

  glGenTextures(1, &xconn->ytex_id);
  glBindTexture(GL_TEXTURE_2D, xconn->ytex_id);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8_ALPHA8, TEX_XSIZE, TEX_YSIZE, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);

  glActiveTextureARB(GL_TEXTURE1_ARB);
  glGenTextures(1, &xconn->uvtex_id);
  glBindTexture(GL_TEXTURE_2D, xconn->uvtex_id);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8_ALPHA8, TEX_XSIZE/2, TEX_YSIZE/2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);

  glActiveTextureARB(GL_TEXTURE2_ARB);
  glGenTextures(1, &xconn->septex_id);
  glBindTexture(GL_TEXTURE_2D, xconn->septex_id);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, 2, 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data_sep);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);

  glFlushPixelDataRangeNV(GL_WRITE_PIXEL_DATA_RANGE_NV);
  //glEnable(GL_TEXTURE_2D);
  glActiveTextureARB(GL_TEXTURE0_ARB);
  glEnable(GL_TEXTURE_2D);
  glActiveTextureARB(GL_TEXTURE1_ARB);
  glEnable(GL_TEXTURE_2D);
  glActiveTextureARB(GL_TEXTURE2_ARB);
  glEnable(GL_TEXTURE_2D);
  glActiveTextureARB(GL_TEXTURE0_ARB);
}

static void
gst_gl_nvimage_close_conn (GstImageConnection *conn, GstImageInfo *info)
{
  GstGLImageConnection *xconn = gst_gl_nvimage_connection (conn);
  GstGLImageInfo *xinfo = gst_gl_nvimage_info (info);  

  // anything needed in here ? Oh, maybe drawing de-init, or something
  glDeleteTextures(1, &xconn->ytex_id);
  glDeleteTextures(1, &xconn->uvtex_id);
  glDeleteTextures(1, &xconn->septex_id);
}

static void 			
gst_gl_nvimage_free_conn (GstImageConnection *conn)
{
  GstGLImageConnection *nvconn = gst_gl_nvimage_connection (conn);  

  g_free (nvconn);
}