File: display.c

package info (click to toggle)
transcode 3%3A1.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,644 kB
  • sloc: ansic: 116,927; sh: 11,468; xml: 2,849; makefile: 1,891; perl: 1,492; pascal: 526; php: 191; python: 144; sed: 43
file content (737 lines) | stat: -rw-r--r-- 19,611 bytes parent folder | download | duplicates (2)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 *  display.c
 *
 *     Copyright (C) Charles 'Buck' Krasic - April 2000
 *     Copyright (C) Erik Walthinsen - April 2000
 *
 *  This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
 *  codec.
 *
 *  libdv 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, or (at your
 *  option) any later version.
 *
 *  libdv 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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The libdv homepage is http://libdv.sourceforge.net/.
 */

/* Most of this file is derived from patches 101018 and 101136 submitted by
 * Stefan Lucke <lucke@berlin.snafu.de> */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <libdv/dv_types.h>
#include "display.h"

#include "libtc/libtc.h"

#if HAVE_LIBXV
#include <sys/ipc.h>
#include <sys/shm.h>
#endif

// FIXME: Use autoconf for this!!
// MacOSX SDL uses native GUI frameworks, but this is an X11
// application setup.
#ifdef __APPLE__
#undef HAVE_SDL
#endif

static int      dv_display_SDL_init(dv_display_t *dv_dpy, char *w_name, char   *i_name           );
static int      dv_display_gdk_init(dv_display_t *dv_dpy, int  *argc,   char ***argv             );

#if HAVE_SDL
static void dv_center_window(SDL_Surface *screen);
#endif

#if HAVE_LIBXV

#define XV_FORMAT_MASK		0x03
#define XV_FORMAT_ASIS		0x00
#define XV_FORMAT_NORMAL	0x01
#define XV_FORMAT_WIDE		0x02

#define XV_SIZE_MASK		0x0c
#define XV_SIZE_NORMAL		0x04
#define XV_SIZE_QUARTER		0x08

#define XV_NOSAWINDOW		0x10	/* not use at the moment	*/

#define DV_FORMAT_UNKNOWN	-1
#define DV_FORMAT_NORMAL	0
#define DV_FORMAT_WIDE		1

static void dv_display_event (dv_display_t *dv_dpy);
static int dv_display_Xv_init (dv_display_t *dv_dpy, char *w_name,
				char   *i_name, int flags, int size);
#endif

dv_display_t *
dv_display_new(void)
{
  dv_display_t *result;

  result = (dv_display_t *)calloc(1,sizeof(dv_display_t));
  if(!result) goto no_mem;

 no_mem:
  return(result);
} /* dv_display_new */

void
dv_display_show(dv_display_t *dv_dpy) {
  switch(dv_dpy->lib) {
  case e_dv_dpy_Xv:
#if HAVE_LIBXV
    dv_display_event(dv_dpy);
    if (!dv_dpy->dontdraw) {
      XvShmPutImage(dv_dpy->dpy, dv_dpy->port,
		  dv_dpy->win, dv_dpy->gc,
		  dv_dpy->xv_image,
		  0, 0,					        /* sx, sy */
		  dv_dpy->swidth, dv_dpy->sheight,	        /* sw, sh */
		  dv_dpy->lxoff,  dv_dpy->lyoff,                /* dx, dy */
		  dv_dpy->lwidth, dv_dpy->lheight,	        /* dw, dh */
		  True);
      XFlush(dv_dpy->dpy);
    }
#endif /* HAVE_LIBXV */
    break;
  case e_dv_dpy_XShm:
    break;
  case e_dv_dpy_gtk:
    break;
  case e_dv_dpy_SDL:
#if HAVE_SDL
    {
      SDL_Event event;
      if (SDL_PollEvent(&event)) {
	if ( event.type == SDL_KEYDOWN ) {
	  switch(event.key.keysym.sym) {
	    case SDLK_ESCAPE:
	    case SDLK_q:
	      dv_dpy->dontdraw = 1;
	      break;
	    default:
	      break;
	  }
	}
      }

      if (!dv_dpy->dontdraw) {
	SDL_UnlockYUVOverlay(dv_dpy->overlay);
	SDL_DisplayYUVOverlay(dv_dpy->overlay, &dv_dpy->rect);
	SDL_LockYUVOverlay(dv_dpy->overlay);
      } else {
	SDL_Quit();
      }
    }
#endif
    break;
  default:
    break;
  } /* switch */
} /* dv_display_show */

void
dv_display_exit(dv_display_t *dv_dpy) {
  if(!dv_dpy)
    return;

  switch(dv_dpy->lib) {
  case e_dv_dpy_Xv:
#if HAVE_LIBXV

    XvStopVideo(dv_dpy->dpy, dv_dpy->port, dv_dpy->win);

    if(dv_dpy->shminfo.shmaddr)
      shmdt(dv_dpy->shminfo.shmaddr);

    if(dv_dpy->shminfo.shmid > 0)
      shmctl(dv_dpy->shminfo.shmid, IPC_RMID, 0);

    if(dv_dpy->xv_image)
      free(dv_dpy->xv_image);
      dv_dpy->xv_image = NULL;

#endif /* HAVE_LIBXV */
    break;
  case e_dv_dpy_gtk:
    break;
  case e_dv_dpy_XShm:
    break;
  case e_dv_dpy_SDL:
#if HAVE_SDL
    SDL_Quit();
#endif /* HAVE_SDL */
    break;
  } /* switch */

  free(dv_dpy);
  dv_dpy = NULL;
} /* dv_display_exit */

static int
dv_display_gdk_init(dv_display_t *dv_dpy, int *argc, char ***argv) {

  return FALSE;
} /* dv_display_gdk_init */

#if HAVE_LIBXV

static int xv_pause=0;

/* ----------------------------------------------------------------------------
 */
static void
dv_display_event (dv_display_t *dv_dpy)
{
    int	old_pic_format;
    KeySym keysym;
    char buf[16];


  while (XCheckTypedWindowEvent (dv_dpy->dpy, dv_dpy->win,
				 ConfigureNotify, &dv_dpy->event) ||
  XCheckTypedWindowEvent (dv_dpy->dpy, dv_dpy->win,
				 KeyPress, &dv_dpy->event)) {
    switch (dv_dpy->event.type) {
      case ConfigureNotify:
	dv_dpy->dwidth = dv_dpy->event.xconfigure.width;
	dv_dpy->dheight = dv_dpy->event.xconfigure.height;
        /* --------------------------------------------------------------------
         * set current picture format to unknown, so that .._check_format
         * does some work.
         */
        old_pic_format = dv_dpy->pic_format;
        dv_dpy->pic_format = DV_FORMAT_UNKNOWN;
        dv_display_check_format (dv_dpy, old_pic_format);
	break;
      case KeyPress:

	XLookupString (&dv_dpy->event.xkey, buf, 16, &keysym, NULL);

	switch(keysym) {

	case XK_Escape:
	  dv_dpy->dontdraw = 1;
	  xv_pause=0;
	  XvStopVideo(dv_dpy->dpy, dv_dpy->port, dv_dpy->win);
	  XDestroyWindow(dv_dpy->dpy, dv_dpy->win);
	  break;

	case XK_Q:
	case XK_q:
	  xv_pause=0;
	  dv_dpy->dontdraw = (dv_dpy->dontdraw) ? 0:1;
	  break;

	case XK_space:
	  xv_pause = (xv_pause)?0:1;
	  while(xv_pause) {
	    dv_display_event(dv_dpy);
	    usleep(10000);
	  }

	default:
		break;
	}
	break;
      default:
	break;
    } /* switch */
  } /* while */
} /* dv_display_event */

#endif /* HAVE_LIBXV */

/* ----------------------------------------------------------------------------
 */
void
dv_display_set_norm (dv_display_t *dv_dpy, dv_system_t norm)
{
#if HAVE_LIBXV
  dv_dpy->sheight = (norm == e_dv_system_625_50) ? 576: 480;
#endif /* HAVE_LIBXV */
} /* dv_display_set_norm */

/* ----------------------------------------------------------------------------
 */
void
dv_display_check_format(dv_display_t *dv_dpy, int pic_format)
{
#if HAVE_LIBXV
  /*  return immediate if ther is no format change or no format
   * specific flag was set upon initialisation
   */
  if (pic_format == dv_dpy->pic_format ||
      !(dv_dpy->flags & XV_FORMAT_MASK))
    return;

  /* --------------------------------------------------------------------
   * check if there are some aspect ratio constraints
   */
  if (dv_dpy->flags & XV_FORMAT_NORMAL) {
    if (pic_format == DV_FORMAT_NORMAL) {
      dv_dpy->lxoff = dv_dpy->lyoff = 0;
      dv_dpy->lwidth = dv_dpy->dwidth;
      dv_dpy->lheight = dv_dpy->dheight;
    } else if (pic_format == DV_FORMAT_WIDE) {
      dv_dpy->lxoff = 0;
      dv_dpy->lyoff = dv_dpy->dheight / 8;
      dv_dpy->lwidth = dv_dpy->dwidth;
      dv_dpy->lheight = (dv_dpy->dheight * 3) / 4;
    }
  } else if (dv_dpy->flags & XV_FORMAT_WIDE) {
    if (pic_format == DV_FORMAT_NORMAL) {
      dv_dpy->lxoff = dv_dpy->dwidth / 8;
      dv_dpy->lyoff = 0;
      dv_dpy->lwidth = (dv_dpy->dwidth * 3) / 4;
      dv_dpy->lheight = dv_dpy->dheight;
    } else if (pic_format == DV_FORMAT_WIDE) {
      dv_dpy->lxoff = dv_dpy->lyoff = 0;
      dv_dpy->lwidth = dv_dpy->dwidth;
      dv_dpy->lheight = dv_dpy->dheight;
    }
  } else {
    dv_dpy->lwidth = dv_dpy->dwidth;
    dv_dpy->lheight = dv_dpy->dheight;
  }
  dv_dpy->pic_format = pic_format;
#endif /* HAVE_LIBXV */
} /* dv_display_check_format */

#if HAVE_LIBXV
/* ----------------------------------------------------------------------------
 */
static int
dv_display_Xv_init(dv_display_t *dv_dpy, char *w_name, char *i_name,
                   int flags, int size) {
  int		scn_id,
                ad_cnt, fmt_cnt,
                got_port, got_fmt,
                i, k;
  XGCValues	values;
  XSizeHints	hints;
  XWMHints	wmhints;
  XTextProperty	x_wname, x_iname;

  XvAdaptorInfo	*ad_info;
  XvImageFormatValues	*fmt_info;

  if(!(dv_dpy->dpy = XOpenDisplay(NULL))) {
    return 0;
  } /* if */

  dv_dpy->rwin = DefaultRootWindow(dv_dpy->dpy);
  scn_id = DefaultScreen(dv_dpy->dpy);

  /*
   * So let's first check for an available adaptor and port
   */
  if(Success == XvQueryAdaptors(dv_dpy->dpy, dv_dpy->rwin, &ad_cnt, &ad_info)) {

    for(i = 0, got_port = False; i < ad_cnt; ++i) {
      tc_log_msg(__FILE__,
		 "Xv: %s: ports %ld - %ld",
		 ad_info[i].name,
		 ad_info[i].base_id,
		 ad_info[i].base_id +
		 ad_info[i].num_ports - 1);

      if (dv_dpy->arg_xv_port != 0 &&
	      (dv_dpy->arg_xv_port < ad_info[i].base_id ||
	       dv_dpy->arg_xv_port >= ad_info[i].base_id+ad_info[i].num_ports)) {
	  tc_log_msg(__FILE__,
		    "Xv: %s: skipping (looking for port %i)",
		    ad_info[i].name,
		    dv_dpy->arg_xv_port);
	  continue;
      }

      if (!(ad_info[i].type & XvImageMask)) {
	tc_log_warn(__FILE__,
		    "Xv: %s: XvImage NOT in capabilty list (%s%s%s%s%s )",
		    ad_info[i].name,
		    (ad_info[i].type & XvInputMask) ? " XvInput"  : "",
		    (ad_info[i]. type & XvOutputMask) ? " XvOutput" : "",
		    (ad_info[i]. type & XvVideoMask)  ?  " XvVideo"  : "",
		    (ad_info[i]. type & XvStillMask)  ?  " XvStill"  : "",
		    (ad_info[i]. type & XvImageMask)  ?  " XvImage"  : "");
	continue;
      } /* if */
      fmt_info = XvListImageFormats(dv_dpy->dpy, ad_info[i].base_id,&fmt_cnt);
      if (!fmt_info || fmt_cnt == 0) {
	tc_log_warn(__FILE__, "Xv: %s: NO supported formats", ad_info[i].name);
	continue;
      } /* if */
      for(got_fmt = False, k = 0; k < fmt_cnt; ++k) {
	if (dv_dpy->format == fmt_info[k].id) {
	  got_fmt = True;
	  break;
	} /* if */
      } /* for */
      if (!got_fmt) {
	char tmpbuf[1000];
	*tmpbuf = 0;
	for (k = 0; k < fmt_cnt; ++k) {
	  tc_snprintf(tmpbuf+strlen(tmpbuf), sizeof(tmpbuf)-strlen(tmpbuf),
		      "%s%#08x[%s]", k>0 ? " " : "", fmt_info[k].id,
		      fmt_info[k].guid);
	}
	tc_log_warn(__FILE__,
		    "Xv: %s: format %#08x is NOT in format list (%s)",
		    ad_info[i].name,
		    dv_dpy->format,
		    tmpbuf);
	continue;
      } /* if */

      for(dv_dpy->port = ad_info[i].base_id, k = 0;
	  k < ad_info[i].num_ports;
	  ++k, ++(dv_dpy->port)) {
	if (dv_dpy->arg_xv_port != 0 && dv_dpy->arg_xv_port != dv_dpy->port) continue;
	if(!XvGrabPort(dv_dpy->dpy, dv_dpy->port, CurrentTime)) {
	  tc_log_msg(__FILE__, "Xv: grabbed port %ld",
		     dv_dpy->port);
	  got_port = True;
	  break;
	} /* if */
      } /* for */
      if(got_port)
	break;
    } /* for */

  } else {
    /* Xv extension probably not present */
    return 0;
  } /* else */

  if(!ad_cnt) {
    tc_log_warn(__FILE__, "Xv: (ERROR) no adaptor found!");
    return 0;
  }
  if(!got_port) {
    tc_log_warn(__FILE__, "Xv: (ERROR) could not grab any port!");
    return 0;
  }

  /* --------------------------------------------------------------------------
   * default settings which allow arbitraray resizing of the window
   */
  hints.flags = PSize | PMaxSize | PMinSize;
  hints.min_width = dv_dpy->width / 16;
  hints.min_height = dv_dpy->height / 16;

  /* --------------------------------------------------------------------------
   * maximum dimensions for Xv support are about 2048x2048
   */
  hints.max_width = 2048;
  hints.max_height = 2048;

  wmhints.input = True;
  wmhints.flags = InputHint;

  XStringListToTextProperty(&w_name, 1 ,&x_wname);
  XStringListToTextProperty(&i_name, 1 ,&x_iname);

  /*
   * default settings: source, destination and logical widht/height
   * are set to our well known dimensions.
   */
  dv_dpy->lwidth = dv_dpy->dwidth = dv_dpy->swidth = dv_dpy->width;
  dv_dpy->lheight = dv_dpy->dheight = dv_dpy->sheight = dv_dpy->height;
  dv_dpy->lxoff = dv_dpy->lyoff = 0;
  dv_dpy-> flags = flags;

  if (flags & XV_FORMAT_MASK) {
    dv_dpy->lwidth = dv_dpy->dwidth = 768;
    dv_dpy->lheight = dv_dpy->dheight = 576;
    dv_dpy->pic_format = DV_FORMAT_UNKNOWN;
    if (flags & XV_FORMAT_WIDE) {
      dv_dpy->lwidth = dv_dpy->dwidth = 1024;
    }
  }
  if (size) {
    dv_dpy->lwidth  = (int)(((double)dv_dpy->lwidth  * (double)size)/100.0);
    dv_dpy->lheight = (int)(((double)dv_dpy->lheight * (double)size)/100.0);
    dv_dpy->dwidth  = (int)(((double)dv_dpy->dwidth  * (double)size)/100.0);
    dv_dpy->dheight = (int)(((double)dv_dpy->dheight * (double)size)/100.0);
  }
  if (flags & XV_FORMAT_MASK) {
    hints.flags |= PAspect;
    if (flags & XV_FORMAT_WIDE) {
      hints.min_aspect.x = hints.max_aspect.x = 1024;
    } else {
      hints.min_aspect.x = hints.max_aspect.x = 768;
    }
    hints.min_aspect.y = hints.max_aspect.y = 576;
  }

  if (!(flags & XV_NOSAWINDOW)) {
    dv_dpy->win = XCreateSimpleWindow(dv_dpy->dpy,
				       dv_dpy->rwin,
				       0, 0,
				       dv_dpy->dwidth, dv_dpy->dheight,
				       0,
				       XWhitePixel(dv_dpy->dpy, scn_id),
				       XBlackPixel(dv_dpy->dpy, scn_id));
  } else {
  }
  XSetWMProperties(dv_dpy->dpy, dv_dpy->win,
		    &x_wname, &x_iname,
		    NULL, 0,
		    &hints, &wmhints, NULL);

  XSelectInput(dv_dpy->dpy, dv_dpy->win, ExposureMask | StructureNotifyMask | KeyPressMask);
  XMapRaised(dv_dpy->dpy, dv_dpy->win);
  XNextEvent(dv_dpy->dpy, &dv_dpy->event);

  dv_dpy->gc = XCreateGC(dv_dpy->dpy, dv_dpy->win, 0, &values);

  /*
   * Now we do shared memory allocation etc..
   */
  dv_dpy->xv_image = XvShmCreateImage(dv_dpy->dpy, dv_dpy->port,
					 dv_dpy->format, dv_dpy->pixels[0],
				      	 dv_dpy->width, dv_dpy->height,
					 &dv_dpy->shminfo);

  dv_dpy->shminfo.shmid = shmget(IPC_PRIVATE,
				     dv_dpy->len,
				     IPC_CREAT | 0777);

  dv_dpy->xv_image->data = dv_dpy->pixels[0] = dv_dpy->shminfo.shmaddr =
    shmat(dv_dpy->shminfo.shmid, 0, 0);

  XShmAttach(dv_dpy->dpy, &dv_dpy->shminfo);
  XSync(dv_dpy->dpy, False);

  return 1;
} /* dv_display_Xv_init */
#endif /* HAVE_LIBXV */


#if HAVE_SDL

static void
dv_center_window(SDL_Surface *screen)
{
    SDL_SysWMinfo info;

    SDL_VERSION(&info.version);
    if ( SDL_GetWMInfo(&info) > 0 ) {
        int x, y;
        int w, h;
        if ( info.subsystem == SDL_SYSWM_X11 ) {
            info.info.x11.lock_func();
            w = DisplayWidth(info.info.x11.display,
                             DefaultScreen(info.info.x11.display));
            h = DisplayHeight(info.info.x11.display,
                             DefaultScreen(info.info.x11.display));
            x = (w - screen->w)/2;
            y = (h - screen->h)/2;
            XMoveWindow(info.info.x11.display, info.info.x11.wmwindow, x, y);
            info.info.x11.unlock_func();
        } /* if */
    } /* if  */
} /* dv_center_window */

static int
dv_display_SDL_init(dv_display_t *dv_dpy, char *w_name, char *i_name) {
  const SDL_VideoInfo *video_info;
  int video_bpp;

  if(SDL_Init(SDL_INIT_VIDEO) < 0) goto no_sdl;
  /* Get the "native" video mode */
  video_info = SDL_GetVideoInfo();
  switch (video_info->vfmt->BitsPerPixel) {
  case 16:
  case 32:
    video_bpp = video_info->vfmt->BitsPerPixel;
    break;
  default:
    video_bpp = 16;
    break;
  } /* switch  */
  dv_dpy->sdl_screen = SDL_SetVideoMode(dv_dpy->width,dv_dpy->height,
					video_bpp,SDL_HWSURFACE);
  SDL_WM_SetCaption(w_name, i_name);
  dv_dpy->overlay = SDL_CreateYUVOverlay(dv_dpy->width, dv_dpy->height, dv_dpy->format,
					 dv_dpy->sdl_screen);
  if((!dv_dpy->overlay || (!dv_dpy->overlay->hw_overlay) ||  /* we only want HW overlays */
      SDL_LockYUVOverlay(dv_dpy->overlay)<0)) {
    goto no_overlay;
  } /* if */
  dv_center_window(dv_dpy->sdl_screen);
  dv_dpy->rect.x = 0;
  dv_dpy->rect.y = 0;
  dv_dpy->rect.w = dv_dpy->overlay->w;
  dv_dpy->rect.h = dv_dpy->overlay->h;
  dv_dpy->pixels[0] = dv_dpy->overlay->pixels[0];
  dv_dpy->pixels[1] = dv_dpy->overlay->pixels[1];
  dv_dpy->pixels[2] = dv_dpy->overlay->pixels[2];
  dv_dpy->pitches[0] = dv_dpy->overlay->pitches[0];
  dv_dpy->pitches[1] = dv_dpy->overlay->pitches[1];
  dv_dpy->pitches[2] = dv_dpy->overlay->pitches[2];
  return(True);

 no_overlay:
  if(dv_dpy->overlay)
    SDL_FreeYUVOverlay(dv_dpy->overlay);
  SDL_Quit();
 no_sdl:
  return(False);

} /* dv_display_SDL_init */

#else

static int
dv_display_SDL_init(dv_display_t *dv_dpy, char *w_name, char *i_name) {
  tc_log_warn(__FILE__,"playdv was compiled without SDL support");
  return(FALSE);
} /* dv_display_SDL_init */

#endif /* HAVE_SDL */

int
dv_display_init(dv_display_t *dv_dpy, int *argc, char ***argv, int width, int height,
		dv_sample_t sampling, char *w_name, char *i_name) {

  dv_dpy->width = width;
  dv_dpy->height = height;

  dv_dpy->dontdraw = 0;

  switch(sampling) {

  case e_dv_sample_420:
    dv_dpy->format = DV_FOURCC_YV12;
    dv_dpy->len = (dv_dpy->width * dv_dpy->height * 3) / 2;
    break;

  default:
    /* Not possible */
    break;
  } /* switch */

  switch(dv_dpy->arg_display) {
  case 0:
    /* Autoselect */
#if HAVE_LIBXV
    /* Try to use Xv first, then SDL */
    if(dv_display_Xv_init(dv_dpy, w_name, i_name,
			  dv_dpy->arg_aspect_val,
			  dv_dpy->arg_size_val)) {
      goto Xv_ok;
    } else
#endif /* HAVE_LIBXV */
    if(dv_display_SDL_init(dv_dpy, w_name, i_name)) {
      goto SDL_ok;
    } else {
      goto use_gtk;
    } /* else */
    break;
  case 1:
    /* Gtk */
    goto use_gtk;
    break;
  case 2:
#if HAVE_LIBXV
    /* Xv */
    if(dv_display_Xv_init(dv_dpy, w_name, i_name,
			  dv_dpy->arg_aspect_val,
			  dv_dpy->arg_size_val)) {
      goto Xv_ok;
    } else {
      tc_log_error(__FILE__, "Attempt to display via Xv failed");
      goto fail;
    }
#else /* HAVE_LIBXV */
    tc_log_error(__FILE__, "Attempt to display via Xv failed");
    goto fail;
#endif /* HAVE_LIBXV */
    break;
  case 3:
    /* SDL */
    if(dv_display_SDL_init(dv_dpy, w_name, i_name)) {
      goto SDL_ok;
    } else {
      tc_log_error(__FILE__, "Attempt to display via SDL failed");
      goto fail;
    }
    break;
  default:
    break;
  } /* switch */

#if HAVE_LIBXV
 Xv_ok:
  tc_log_info(__FILE__, " Using Xv for display");
  dv_dpy->lib = e_dv_dpy_Xv;
  goto yuv_ok;
#endif /* HAVE_LIBXV */

 SDL_ok:
  tc_log_info(__FILE__, " Using SDL for display");
  dv_dpy->lib = e_dv_dpy_SDL;
  goto yuv_ok;

 yuv_ok:

  dv_dpy->color_space = e_dv_color_yuv;

  switch(dv_dpy->format) {
  case DV_FOURCC_YUY2:
    dv_dpy->pitches[0] = width * 2;
    break;
  case DV_FOURCC_YV12:
    dv_dpy->pixels[1] = dv_dpy->pixels[0] + (width * height);
    dv_dpy->pixels[2] = dv_dpy->pixels[1] + (width * height / 4);
    dv_dpy->pitches[0] = width;
    dv_dpy->pitches[1] = width / 2;
    dv_dpy->pitches[2] = width / 2;
    break;
  } /* switch */

  goto ok;

 use_gtk:

  /* Try to use GDK since we couldn't get a HW YUV surface */
  dv_dpy->color_space = e_dv_color_rgb;
  dv_dpy->lib = e_dv_dpy_gtk;
  dv_dpy->len = dv_dpy->width * dv_dpy->height * 3;
  if(!dv_display_gdk_init(dv_dpy, argc, argv)) {
    tc_log_error(__FILE__, "Attempt to use gtk for display failed");
    goto fail;
  } /* if  */
  dv_dpy->pitches[0] = width * 3;
  tc_log_info(__FILE__, " Using gtk for display");

 ok:
  return(TRUE);

 fail:
  tc_log_error(__FILE__, " Unable to establish a display method");
  return(FALSE);
} /* dv_display_init */