summaryrefslogtreecommitdiff
path: root/client/player.cc
blob: fad7df4a371cff40d621bdfa2bb3d13ddd7b447a (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            player.cc
 *
 *  Mon Mar  6 20:14:25 CET 2006
 *  Copyright  2006 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of MIaV.
 *
 *  MIaV is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  MIaV is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MIaV; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "player.h"

static unsigned char yuv[720*576*4];
static int num = 0;

Player::Player(VideoWidget *w)
{
  widget = w;

  render.init(widget, 720, 576);//widget->getWidth(), widget->getHeight()

  connect(this, SIGNAL(timeout()), this, SLOT(show_frame()));

  for(int x = 1; x < 720; x++)
    for(int y = 1; y < 576; y++) {
      yuv[x + y * 576 * 1] = x % 720 * 255;
      yuv[x + y * 576 * 2] = y % 576 * 255;
      yuv[x + y * 576 * 3] = (unsigned char)x % y;
    }
}

Player::~Player()
{
  render.end();
}

void Player::show_frame()
{
  fprintf(stderr, "Frame!%d\n", num++);
  render.width = widget->getWidth();
  render.height = widget->getHeight();
  render.display(yuv, 720, 576);//widget->width(), widget->height());
}






#if 0

#define DBG(x) fprintf(stderr, x)

#include "info.h"
#include <QX11Info>

#define DV_FOURCC_YV12  0x32315659	/* 4:2:0 Planar mode: Y + V + U  (3 planes) */
#define DV_FOURCC_YUY2  0x32595559	/* 4:2:2 Packed mode: Y0+U0+Y1+V0 (1 plane) */

#include <X11/extensions/Xvlib.h>
#include <X11/extensions/XShm.h>


extern "C" {
//extern int 	XShmQueryExtension(Display*);
//extern int 	XShmGetEventBase(Display*);
extern XvImage  *XvShmCreateImage(Display*, XvPortID, int, char*, int, int, XShmSegmentInfo*);
}

Player::Player(VideoWidget *w)
{
  widget = w;

  //  dpy = XOpenDisplay(NULL); // Get the deafult display

  display = widget->x11Info().display();
  drawable = widget->winId();

  fprintf(stderr, "XCreateGC\n");
  gc = XCreateGC(display, drawable, 0, &values);

  /*
  switch(gc) {
  case BadAlloc:
    MIaV::info->error("The server failed to allocate the requested resource or server memory.");
    break;
  case BadDrawable:
    MIaV::info->error("A value for a Drawable argument does not name a defined Window or Pixmap.");
    break;

  case BadFont:
    MIaV::info->error("A value for a Font or GContext argument does not name a defined Font.");
    break;

  case BadMatch:
    MIaV::info->error("An InputOnly window is used as a Drawable.");
    break;

  case BadMatch:
    MIaV::info->error("Some argument or pair of arguments has the correct type and range but fails to match in some other way required by the request.");
    break;

  case BadPixmap:
    MIaV::info->error("A value for a Pixmap argument does not name a defined Pixmap.");
    break;
    
  case BadValue:
    MIaV::info->error("Some numeric value falls outside the range of values accepted by the request.  Unless a \
specific range is specified for an argument, the full range defined by the argument's type is \
accepted.  Any argument defined as a set of alternatives can generate this error.");
    break;
  }
  */
  //
  // Check version
  //
  unsigned int version, release;
  unsigned int request_base, event_base, error_base;
 
  fprintf(stderr, "XvQueryExtension\n");
  switch(XvQueryExtension(display, &version, &release, &request_base, &event_base, &error_base)) {
  case Success:DBG("[-]Returned if XvQueryExtension(3X) completed successfully.");break;
  case XvBadExtension:DBG("[E]Returned if the Xv video extension is not available for the named display.");break;
  case XvBadAlloc:DBG("[E]Returned if XvQueryExtension(3X) failed to allocate memory to process the request.");break;
  }


  //
  // Find port
  //
  unsigned int adaptors;
  XvAdaptorInfo *ai;

  fprintf(stderr, "XvQueryAdaptors\n");
  switch(XvQueryAdaptors(display, drawable, &adaptors, &ai)) {
  case Success:DBG("[-]Returned if XvQueryAdaptors(3X) completed successfully.");break;
  case XvBadExtension:DBG("[E]Returned if the Xv video extension is not available for the named display.");break;
  case XvBadAlloc:DBG("[E]Returned if XvQueryAdaptors(3X) failed to allocate memory to process the request.");break;
  }
  
  /*
   typedef struct {
      XvPortID base_id;
      unsigned long num_ports;
      char type;
      char *name;
      unsigned long num_formats;
      XvFormat *formats;
      unsigned long num_adaptors;
   } XvAdaptorInfo;
   */
  if(adaptors == 0) {
    MIaV::info->error("Unable to find any adapters.");
    return;
  }

  /* check adaptors */
  port = 0;
  for (unsigned int i = 0; i < adaptors; i++) {
    if ((ai[i].type & XvInputMask) &&
        (ai[i].type & XvImageMask) &&
        (ai[i].type & XvOutputMask) &&
        (port == 0)) {
      port = ai[i].base_id;
    }
  }

  fprintf(stderr, "XvGrabPort\n");
  if(port == 0) {
    MIaV::info->error("Unable to find suitable port.");
    return;
  } else {
    MIaV::info->info("Using port %d.", port);
  }

  fprintf(stderr, "Using XVideo port: %d\n", port);

  port=62;

  switch(XvGrabPort(display, port, CurrentTime)) {
  case Success:DBG("[-]Returned if XvGrabPort(3X) completed successfully.");break;
  case XvInvalidTime:DBG("[E]Returned if requested time is older than the current port time.");break;
  case XvAlreadyGrabbed:DBG("[E]Returned if the port is already grabbed by another client.");break;
  case XvBadExtension:DBG("[E]Returned if the Xv extension is unavailable.");break;
  case XvBadAlloc:DBG("[E]Returned if XvGrabPort(3X) failed to allocate memory to process the request.");break;
  }

}

Player::~Player()
{
  fprintf(stderr, "XFreeGC\n");
  XvUngrabPort(display, port, CurrentTime);
  XFreeGC(display, gc);
  //XCloseDisplay(dpy); // Close the Display
}

//extern XvImage  *XvShmCreateImage(Display*, XvPortID, int, char*, int, int, XShmSegmentInfo*);
void Player::run()
{
  while(1) {
    XShmSegmentInfo	yuv_shminfo;
    static char pixels[720*576*4][4];
    XvImage* xv_image = XvShmCreateImage(display, port,
                                         DV_FOURCC_YV12, //DV_FOURCC_YUY2
                                         pixels[0],
                                         widget->getWidth(), widget->getHeight(),// dv_dpy->width, dv_dpy->height,
                                         &yuv_shminfo);

    XvShmPutImage(display, port,
                  drawable, gc,
                  xv_image,
                  0, 0,
                  widget->getWidth(), widget->getHeight(),
                  widget->getX(), widget->getY(),
                  widget->getWidth(), widget->getHeight(),
                  True);
    //    XFlush(display);
    sleep(1);
  }
  /*
  int cnt = 100;
  while(cnt--) {
    sleep(1);
    fprintf(stderr, "XvPutStill(%d,%d,%d,%d)\n",widget->getX(),widget->getY(),widget->getWidth(), widget->getHeight());
    switch(XvPutStill(display, port, drawable, gc, 
                    widget->getX(),widget->getY(),widget->getWidth(), widget->getHeight(),
                    widget->getX(),widget->getY(),widget->getWidth(), widget->getHeight())) {
    //  case Success:
    // Yeah!
    //break;
  case XvBadExtension:
    MIaV::info->error("Returned if the Xv extension is unavailable.");
    break;
  case XvBadAlloc:
    MIaV::info->error("Returned if XvGetStill(3X) failed to allocate memory to process the request.");
    break;
  case XvBadPort:
    MIaV::info->error("Generated if the requested port does not exist.");
    break;
  case BadDrawable:
    MIaV::info->error("Generated if the requested drawable does not exist.");
    break;
  case BadGC: 
    MIaV::info->error("Generated if the requested graphics context does not exist.");
    break;
  case BadAlloc:
    MIaV::info->error("Generated if there were insufficient resources to process the request.");
    break;
  }

  //  fprintf(stderr, "XFlush\n");
  XFlush(display);
  }
  */


}



/*
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
*/
  /*
  XvImage* ximage=XCreateImage(display, visual, 32, ZPixmap, 0,
                               xw->virtualscreen, xw->width, xw->height, 32,
                               xw->width*xw->pixelsize);

  XShmSegmentInfo	yuv_shminfo;
  yuv_shminfo.shmid = shmget(IPC_PRIVATE, 
                             720*576*4, //yuv_image->data_size,
                             IPC_CREAT | 0777);
  //  yuv_shminfo.shmaddr = shmat(yuv_shminfo.shmid, 0, 0);
  yuv_shminfo.readOnly = False;

  char pixels[720*576*4][4];
  XvImage* xv_image = XvShmCreateImage(display, port,
                                       DV_FOURCC_YUY2, // DV_FOURCC_YV12
                                       pixels[0],
                                       720, 576,// dv_dpy->width, dv_dpy->height,
                                       &yuv_shminfo);
  int swidth = 720;
  int sheight = 576;
  int lxoff = 0;
  int lyoff = 0;
  int lwidth = swidth;
  int lheight = sheight;
 
  XvShmPutImage(display, port,
                drawable, gc,
                xv_image,
                0, 0,
                swidth, sheight,
                lxoff,  lyoff,
                lwidth, lheight,
                True);

  */

// From:
//  http://cvs.sourceforge.net/viewcvs.py/libmpeg2/MSSG/display_x11.c?rev=1.2
//#if 0
#ifdef HAVE_XV
xv_port = 0;
if (Success == XvQueryExtension(mydisplay,&ver,&rel,&req,&ev,&err)) {
  /* check for Xvideo support */
  if (Success != XvQueryAdaptors(mydisplay,DefaultRootWindow(mydisplay),
                                 &adaptors,&ai)) {
    fprintf(stderr,"Xv: XvQueryAdaptors failed");
    exit(1);
  }
  /* check adaptors */
  for (i = 0; i < adaptors; i++) {
    if ((ai[i].type & XvInputMask) &&
        (ai[i].type & XvImageMask) &&
        (xv_port == 0)) {
      xv_port = ai[i].base_id;
    }
  }
  /* check image formats */
  if (xv_port != 0) {
    fo = XvListImageFormats(mydisplay, xv_port, (int*)&formats);
    for(i = 0; i < formats; i++) {
      fprintf(stderr, "Xvideo image format: 0x%x (%4.4s) %s\n",
              fo[i].id,
              (char*)&fo[i].id,
              (fo[i].format == XvPacked) ? "packed" : "planar");
      if (0x32315659 == fo[i].id) {
        xv_format = fo[i].id;
        break;
      }
    }
    if (i == formats)
      /* no matching image format not */
      xv_port = 0;
  }
  if (xv_port != 0) {
    fprintf(stderr,"using Xvideo port %d for hw scaling\n",
            xv_port);
	   
    /* allocate XvImages.  FIXME: no error checking, without
     * mit-shm this will bomb... */
    xvimage1 = XvShmCreateImage(mydisplay, xv_port, xv_format, 0,
                                image_width, image_height,
                                &Shminfo1);
    Shminfo1.shmid    = shmget(IPC_PRIVATE, xvimage1->data_size,
                               IPC_CREAT | 0777);
    Shminfo1.shmaddr  = (char *) shmat(Shminfo1.shmid, 0, 0);
    Shminfo1.readOnly = False;
    xvimage1->data = Shminfo1.shmaddr;
    XShmAttach(mydisplay, &Shminfo1);
    XSync(mydisplay, False);
    shmctl(Shminfo1.shmid, IPC_RMID, 0);

    /* so we can do grayscale while testing... */
    memset(xvimage1->data,128,xvimage1->data_size);

    /* catch window resizes */
    XSelectInput(mydisplay, mywindow, StructureNotifyMask);
    win_width  = image_width;
    win_height = image_height;

    /* all done (I hope...) */
    X_already_started++;
    return;
  }
}
#endif/* HAVE_XV */
#endif/* 0 */