aboutsummaryrefslogtreecommitdiffstats
path: root/xinebrowser_util.c
blob: fc4397e9018fb73fd285bd7e2e51860c16f42646 (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
/* mb-desktop-xine - a desktop media playing module.

   Copyright 2004 Matthew Allum

   This program 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.

   This program 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.
*/

#include "xinebrowser.h"

static int trapped_error_code = 0;
static int (*old_error_handler) (Display *d, XErrorEvent *e);

int
error_handler(Display     *display,
              XErrorEvent *error)
{
  trapped_error_code = error->error_code;
  return 0;
}

void
trap_errors(void)
{
  trapped_error_code = 0;
  old_error_handler = XSetErrorHandler(error_handler);
}
 
int
untrap_errors(void)
{
  XSetErrorHandler(old_error_handler);
  return trapped_error_code;
}

Bool 
file_exists(char *filename)
{
  struct stat st;
  if (stat(filename, &st)) return False;
  return True;
}

int
xinebrowser_cd_get_disc_type(char *device)
{
  int fd;           /* file descriptor for CD-ROM device */
  int status;       /* return status for system calls */
  int i, have_cd = 0, had_cd = 0, cd_type = CD_TYPE_UNKNOWN;


  /* Should check permissions  */
  fd = open(device, O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    fprintf (stderr, "mbxine: open failed for `%s': %s\n",
	     device, strerror (errno));
    return CD_TYPE_ERROR;
  }

  status = ioctl (fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
  if (status < 0) 
    {
      perror(" CDROM_DRIVE_STATUS");
      close(fd);
      return CD_TYPE_ERROR;
    } 

  if (status != CDS_DISC_OK) /* CDS_TRAY_OPEN CDS_DRIVE_NOT_READY */
    {
      return CD_TYPE_NO_DISC;
    }

  cd_type = CD_TYPE_UNKNOWN;

  status = ioctl (fd, CDROM_DISC_STATUS);

  switch (status) 
    {
    case CDS_AUDIO:
      cd_type = CD_TYPE_AUDIO;
      break;
    case CDS_DATA_1:
    case CDS_DATA_2:
    case CDS_XA_2_1:
    case CDS_XA_2_2:
      cd_type = CD_TYPE_DATA;
      break;
    case CDS_NO_INFO:
    case CDS_MIXED:
    default:
      cd_type = CD_TYPE_UNKNOWN;
    }

  if (cd_type == CD_TYPE_UNKNOWN || cd_type == CD_TYPE_DATA)
    {
      /* See if its a DVD */
      int foo;
      dvd_struct dvd;
      
      dvd.type = DVD_STRUCT_COPYRIGHT;
      dvd.copyright.layer_num = 0;
      
      status = ioctl( fd, DVD_READ_STRUCT, &dvd );

      /* 
	 dvd.copyright.cpst == 0 if encrypted

      */      
      if (status >= 0)
	{
	  cd_type = CD_TYPE_DVD;
	} 
    }

  close(fd);
  return cd_type;

}


/* Code taken from:
 * transcode Copyright (C) Thomas Oestreich - June 2001
 * enix      enix.berlios.de
 */

void 
yuy2toyv12 (unsigned char *y, 
	    unsigned char *u, 
	    unsigned char *v, 
	    unsigned char *input,
	    int            width, 
	    int            height) 
{
  int i, j, w2;
  
  w2 = width / 2;

  for (i = 0; i < height; i += 2) 
    {
      for (j = 0; j < w2; j++) {
	/* packed YUV 422 is: Y[i] U[i] Y[i+1] V[i] */
	*(y++) = *(input++);
	*(u++) = *(input++);
	*(y++) = *(input++);
	*(v++) = *(input++);
      }
      
      /* down sampling */
      for (j = 0; j < w2; j++) {
	/* skip every second line for U and V */
	*(y++) = *(input++);
	input++;
	*(y++) = *(input++);
	input++;
      }
    }
}

#define clip_8_bit(val)				\
{						\
	if (val < 0)				\
		val = 0;			\
	else					\
		if (val > 255) val = 255;	\
}

unsigned char* 
yv12torgb (unsigned char *src_y, 
	   unsigned char *src_u, 
	   unsigned char *src_v,
	   int width, 
	   int height) 
{
	  int     i, j;

	  int     y, u, v;
	  int     r, g, b;

	  int     sub_i_uv;
	  int     sub_j_uv;

	  int     uv_width, uv_height;

	  unsigned char *rgb;

	  uv_width  = width / 2;
	  uv_height = height / 2;

	  rgb = (unsigned char *) malloc (width * height * 3);
	  if (!rgb)
		  return NULL;

	  for (i = 0; i < height; ++i) {
		  /* calculate u & v rows */
		  sub_i_uv = ((i * uv_height) / height);

		  for (j = 0; j < width; ++j) {
			  /* calculate u & v columns */
			  sub_j_uv = ((j * uv_width) / width);

	  /***************************************************
	   *  Colour conversion from
	   *    http://www.inforamp.net/~poynton/notes/colour_and_gamma/ColorFAQ.html#RTFToC30
	   *
	   *  Thanks to Billy Biggs <vektor@dumbterm.net>
	   *  for the pointer and the following conversion.
	   *
	   *   R' = [ 1.1644         0    1.5960 ]   ([ Y' ]   [  16 ])
	   *   G' = [ 1.1644   -0.3918   -0.8130 ] * ([ Cb ] - [ 128 ])
	   *   B' = [ 1.1644    2.0172         0 ]   ([ Cr ]   [ 128 ])
	   *
	   *  Where in xine the above values are represented as
	   *   Y' == image->y
	   *   Cb == image->u
	   *   Cr == image->v
	   *
	   ***************************************************/

			  y = src_y[(i * width) + j] - 16;
			  u = src_u[(sub_i_uv * uv_width) + sub_j_uv] - 128;
			  v = src_v[(sub_i_uv * uv_width) + sub_j_uv] - 128;

			  r = (1.1644 * y) + (1.5960 * v);
			  g = (1.1644 * y) - (0.3918 * u) - (0.8130 * v);
			  b = (1.1644 * y) + (2.0172 * u);

			  clip_8_bit (r);
			  clip_8_bit (g);
			  clip_8_bit (b);

			  rgb[(i * width + j) * 3 + 0] = r;
			  rgb[(i * width + j) * 3 + 1] = g;
			  rgb[(i * width + j) * 3 + 2] = b;
		  }
	  }

	  return rgb;
}