Mercurial > mplayer.hg
annotate libmpdemux/tvi_v4l.c @ 4710:701976d7a7d1
fast header checker added
author | arpi |
---|---|
date | Thu, 14 Feb 2002 23:41:00 +0000 |
parents | 622a9ade4517 |
children | 942d497875b4 |
rev | line source |
---|---|
2802 | 1 /* |
3284 | 2 Video 4 Linux input |
2802 | 3 |
4 (C) Alex Beregszaszi <alex@naxine.org> | |
5 | |
6 Some ideas are based on xawtv/libng's grab-v4l.c written by | |
7 Gerd Knorr <kraxel@bytesex.org> | |
8 | |
9 CODE IS UNDER DEVELOPMENT, NO FEATURE REQUESTS PLEASE! | |
10 */ | |
11 | |
2790 | 12 #include "config.h" |
13 | |
3243 | 14 #if defined(USE_TV) && defined(HAVE_TV_V4L) |
2790 | 15 |
16 #include <stdio.h> | |
17 #include <errno.h> | |
18 #include <fcntl.h> | |
2802 | 19 #include <signal.h> |
2790 | 20 #include <sys/ioctl.h> |
21 #include <sys/types.h> | |
22 #include <linux/videodev.h> | |
3815 | 23 #include <linux/soundcard.h> |
2790 | 24 #include <unistd.h> |
25 #include <sys/mman.h> | |
2931 | 26 #include <stdlib.h> |
27 #include <string.h> | |
2790 | 28 |
2830 | 29 #include "mp_msg.h" |
30 #include "../libao2/afmt.h" | |
31 #include "../libvo/img_format.h" | |
32 #include "../libvo/fastmemcpy.h" | |
33 | |
2790 | 34 #include "tv.h" |
35 | |
36 static tvi_info_t info = { | |
3815 | 37 "Video 4 Linux input", |
2790 | 38 "v4l", |
2802 | 39 "Alex Beregszaszi <alex@naxine.org>", |
40 "under development" | |
41 }; | |
42 | |
3815 | 43 #define MAX_AUDIO_CHANNELS 10 |
44 | |
2790 | 45 typedef struct { |
2802 | 46 /* general */ |
2790 | 47 char *video_device; |
3815 | 48 int video_fd; |
2790 | 49 struct video_capability capability; |
50 struct video_channel *channels; | |
2841 | 51 int act_channel; |
2790 | 52 struct video_tuner tuner; |
2802 | 53 |
54 /* video */ | |
2790 | 55 struct video_picture picture; |
2802 | 56 int format; /* output format */ |
2790 | 57 int width; |
58 int height; | |
2802 | 59 int bytesperline; |
60 | |
61 struct video_mbuf mbuf; | |
62 unsigned char *mmap; | |
63 struct video_mmap *buf; | |
64 int nbuf; | |
65 int queue; | |
66 | |
67 /* audio */ | |
3815 | 68 int audio_id; |
69 char *audio_device; | |
70 struct video_audio audio[MAX_AUDIO_CHANNELS]; | |
71 int audio_fd; | |
72 int audio_channels[MAX_AUDIO_CHANNELS]; | |
73 int audio_format[MAX_AUDIO_CHANNELS]; | |
74 int audio_samplesize[MAX_AUDIO_CHANNELS]; | |
75 int audio_samplerate[MAX_AUDIO_CHANNELS]; | |
76 int audio_blocksize; | |
2790 | 77 } priv_t; |
78 | |
79 #include "tvi_def.h" | |
80 | |
2841 | 81 static const char *device_cap2name[] = { |
2790 | 82 "capture", "tuner", "teletext", "overlay", "chromakey", "clipping", |
2802 | 83 "frameram", "scales", "monochrome", "subcapture", "mpeg-decoder", |
84 "mpeg-encoder", "mjpeg-decoder", "mjpeg-encoder", NULL | |
85 }; | |
86 | |
2841 | 87 static const char *device_palette2name[] = { |
2802 | 88 "-", "grey", "hi240", "rgb16", "rgb24", "rgb32", "rgb15", "yuv422", |
89 "yuyv", "uyvy", "yuv420", "yuv411", "raw", "yuv422p", "yuv411p", | |
90 "yuv420p", "yuv410p", NULL | |
91 }; | |
92 #define PALETTE(x) ((x < sizeof(device_pal)/sizeof(char*)) ? device_pal[x] : "UNKNOWN") | |
93 | |
2841 | 94 static const char *audio_mode2name[] = { |
95 "unknown", "mono", "stereo", "language1", "language2", NULL | |
96 }; | |
97 | |
2802 | 98 static int palette2depth(int palette) |
99 { | |
2810 | 100 switch(palette) |
101 { | |
3220 | 102 /* component */ |
2810 | 103 case VIDEO_PALETTE_RGB555: |
104 return(15); | |
105 case VIDEO_PALETTE_RGB565: | |
106 return(16); | |
107 case VIDEO_PALETTE_RGB24: | |
108 return(24); | |
109 case VIDEO_PALETTE_RGB32: | |
110 return(32); | |
3220 | 111 /* planar */ |
112 case VIDEO_PALETTE_YUV411P: | |
2810 | 113 case VIDEO_PALETTE_YUV420P: |
3220 | 114 case VIDEO_PALETTE_YUV410P: |
2810 | 115 return(12); |
3220 | 116 /* packed */ |
3815 | 117 case VIDEO_PALETTE_YUV422P: |
2810 | 118 case VIDEO_PALETTE_YUV422: |
3220 | 119 case VIDEO_PALETTE_YUYV: |
2810 | 120 case VIDEO_PALETTE_UYVY: |
3220 | 121 case VIDEO_PALETTE_YUV420: |
122 case VIDEO_PALETTE_YUV411: | |
2810 | 123 return(16); |
124 } | |
125 return(-1); | |
2802 | 126 } |
127 | |
128 static int format2palette(int format) | |
129 { | |
2810 | 130 switch(format) |
131 { | |
132 case IMGFMT_RGB15: | |
133 return(VIDEO_PALETTE_RGB555); | |
134 case IMGFMT_RGB16: | |
135 return(VIDEO_PALETTE_RGB565); | |
136 case IMGFMT_RGB24: | |
137 return(VIDEO_PALETTE_RGB24); | |
138 case IMGFMT_RGB32: | |
139 return(VIDEO_PALETTE_RGB32); | |
140 case IMGFMT_YV12: | |
3703 | 141 case IMGFMT_I420: |
2810 | 142 return(VIDEO_PALETTE_YUV420P); |
143 case IMGFMT_UYVY: | |
144 return(VIDEO_PALETTE_YUV422); | |
3815 | 145 case IMGFMT_YUY2: |
146 return(VIDEO_PALETTE_YUYV); | |
2810 | 147 } |
148 return(-1); | |
2802 | 149 } |
150 | |
151 static int one = 1, zero = 0; | |
152 | |
2790 | 153 tvi_handle_t *tvi_init_v4l(char *device) |
154 { | |
155 tvi_handle_t *h; | |
156 priv_t *priv; | |
157 | |
158 h = new_handle(); | |
159 if (!h) | |
160 return(NULL); | |
161 | |
162 priv = h->priv; | |
163 | |
2802 | 164 /* set video device name */ |
2790 | 165 if (!device) |
3611 | 166 priv->video_device = strdup("/dev/video0"); |
2790 | 167 else |
3611 | 168 priv->video_device = strdup(device); |
169 | |
170 /* allocation failed */ | |
171 if (!priv->video_device) { | |
172 free_handle(h); | |
173 return(NULL); | |
2790 | 174 } |
175 | |
3815 | 176 /* set audio device name */ |
177 priv->audio_device = strdup("/dev/dsp"); | |
178 | |
2790 | 179 return(h); |
180 } | |
181 | |
3815 | 182 static int init(priv_t *priv) |
2790 | 183 { |
184 int i; | |
185 | |
3815 | 186 priv->video_fd = open(priv->video_device, O_RDWR); |
187 if (priv->video_fd == -1) | |
2790 | 188 { |
2818 | 189 mp_msg(MSGT_TV, MSGL_ERR, "unable to open '%s': %s\n", |
2802 | 190 priv->video_device, strerror(errno)); |
2790 | 191 goto err; |
192 } | |
193 | |
3815 | 194 mp_msg(MSGT_TV, MSGL_V, "Video fd: %d\n", priv->video_fd); |
2790 | 195 |
2802 | 196 /* get capabilities (priv->capability is needed!) */ |
3815 | 197 if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1) |
2790 | 198 { |
2818 | 199 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get capabilites failed: %s\n", strerror(errno)); |
2790 | 200 goto err; |
201 } | |
202 | |
3815 | 203 fcntl(priv->video_fd, F_SETFD, FD_CLOEXEC); |
2802 | 204 |
2818 | 205 mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.name); |
206 mp_msg(MSGT_TV, MSGL_INFO, " Capabilites: "); | |
2841 | 207 for (i = 0; device_cap2name[i] != NULL; i++) |
2790 | 208 if (priv->capability.type & (1 << i)) |
2841 | 209 mp_msg(MSGT_TV, MSGL_INFO, "%s ", device_cap2name[i]); |
2818 | 210 mp_msg(MSGT_TV, MSGL_INFO, "\n"); |
211 mp_msg(MSGT_TV, MSGL_INFO, " Device type: %d\n", priv->capability.type); | |
212 mp_msg(MSGT_TV, MSGL_INFO, " Supported sizes: %dx%d => %dx%d\n", | |
2790 | 213 priv->capability.minwidth, priv->capability.minheight, |
214 priv->capability.maxwidth, priv->capability.maxheight); | |
215 priv->width = priv->capability.minwidth; | |
216 priv->height = priv->capability.minheight; | |
2818 | 217 mp_msg(MSGT_TV, MSGL_INFO, " Inputs: %d\n", priv->capability.channels); |
2790 | 218 |
2819
2e58962dc9fe
cleaned up some warnings, and tv_param_on moved out from #ifdef USE_TV
alex
parents:
2818
diff
changeset
|
219 priv->channels = (struct video_channel *)malloc(sizeof(struct video_channel)*priv->capability.channels); |
3611 | 220 if (!priv->channels) |
221 goto malloc_failed; | |
2790 | 222 memset(priv->channels, 0, sizeof(struct video_channel)*priv->capability.channels); |
223 for (i = 0; i < priv->capability.channels; i++) | |
224 { | |
225 priv->channels[i].channel = i; | |
3815 | 226 if (ioctl(priv->video_fd, VIDIOCGCHAN, &priv->channels[i]) == -1) |
2841 | 227 { |
228 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get channel failed: %s\n", strerror(errno)); | |
229 break; | |
230 } | |
2818 | 231 mp_msg(MSGT_TV, MSGL_INFO, " %d: %s: %s%s%s%s (tuner:%d, norm:%d)\n", i, |
2790 | 232 priv->channels[i].name, |
233 (priv->channels[i].flags & VIDEO_VC_TUNER) ? "tuner " : "", | |
234 (priv->channels[i].flags & VIDEO_VC_AUDIO) ? "audio " : "", | |
235 (priv->channels[i].flags & VIDEO_TYPE_TV) ? "tv " : "", | |
2802 | 236 (priv->channels[i].flags & VIDEO_TYPE_CAMERA) ? "camera " : "", |
237 priv->channels[i].tuners, | |
238 priv->channels[i].norm); | |
239 } | |
240 | |
3815 | 241 /* audio chanlist */ |
2841 | 242 if (priv->capability.audios) |
243 { | |
244 mp_msg(MSGT_TV, MSGL_INFO, " Audio devices: %d\n", priv->capability.audios); | |
245 | |
246 for (i = 0; i < priv->capability.audios; i++) | |
247 { | |
3815 | 248 if (i >= MAX_AUDIO_CHANNELS) |
249 { | |
250 mp_msg(MSGT_TV, MSGL_ERR, "no space for more audio channels (incrase in source!) (%d > %d)\n", | |
251 i, MAX_AUDIO_CHANNELS); | |
252 i = priv->capability.audios; | |
253 break; | |
254 } | |
255 | |
3284 | 256 priv->audio[i].audio = i; |
3815 | 257 if (ioctl(priv->video_fd, VIDIOCGAUDIO, &priv->audio[i]) == -1) |
2841 | 258 { |
259 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get audio failed: %s\n", strerror(errno)); | |
260 break; | |
261 } | |
262 | |
3815 | 263 if (priv->audio[i].volume <= 0) |
264 priv->audio[i].volume = 100; | |
265 priv->audio[i].flags &= ~VIDEO_AUDIO_MUTE; | |
266 ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[i]); | |
267 | |
268 switch(priv->audio[i].mode) | |
269 { | |
270 case VIDEO_SOUND_MONO: | |
271 case VIDEO_SOUND_LANG1: | |
272 case VIDEO_SOUND_LANG2: | |
273 priv->audio_channels[i] = 1; | |
274 break; | |
275 case VIDEO_SOUND_STEREO: | |
276 priv->audio_channels[i] = 2; | |
277 break; | |
278 } | |
279 | |
280 priv->audio_format[i] = AFMT_S16_LE; | |
281 priv->audio_samplerate[i] = 44100; | |
282 priv->audio_samplesize[i] = /*76000*/priv->audio_channels[i]* | |
283 16*priv->audio_samplerate[i]/8; | |
284 | |
285 /* display stuff */ | |
3284 | 286 mp_msg(MSGT_TV, MSGL_V, " %d: %s: ", priv->audio[i].audio, |
287 priv->audio[i].name); | |
288 if (priv->audio[i].flags & VIDEO_AUDIO_MUTABLE) | |
2841 | 289 mp_msg(MSGT_TV, MSGL_V, "muted=%s ", |
3284 | 290 (priv->audio[i].flags & VIDEO_AUDIO_MUTE) ? "yes" : "no"); |
2841 | 291 mp_msg(MSGT_TV, MSGL_V, "volume=%d bass=%d treble=%d balance=%d mode=%s\n", |
3284 | 292 priv->audio[i].volume, priv->audio[i].bass, priv->audio[i].treble, |
293 priv->audio[i].balance, audio_mode2name[priv->audio[i].mode]); | |
3815 | 294 mp_msg(MSGT_TV, MSGL_V, " channels: %d, samplerate: %d, samplesize: %d, format: %s\n", |
295 priv->audio_channels[i], priv->audio_samplerate[i], priv->audio_samplesize[i], | |
296 audio_out_format_name(priv->audio_format[i])); | |
2841 | 297 } |
298 } | |
299 | |
2802 | 300 if (!(priv->capability.type & VID_TYPE_CAPTURE)) |
301 { | |
2818 | 302 mp_msg(MSGT_TV, MSGL_ERR, "Only grabbing supported (for overlay use another program)\n"); |
2802 | 303 goto err; |
304 } | |
305 | |
306 /* map grab buffer */ | |
3815 | 307 if (ioctl(priv->video_fd, VIDIOCGMBUF, &priv->mbuf) == -1) |
2802 | 308 { |
2818 | 309 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get mbuf failed: %s\n", strerror(errno)); |
2802 | 310 goto err; |
2790 | 311 } |
312 | |
2818 | 313 mp_msg(MSGT_TV, MSGL_V, "mbuf: size=%d, frames=%d\n", |
2802 | 314 priv->mbuf.size, priv->mbuf.frames); |
315 priv->mmap = mmap(0, priv->mbuf.size, PROT_READ|PROT_WRITE, | |
3815 | 316 MAP_SHARED, priv->video_fd, 0); |
2819
2e58962dc9fe
cleaned up some warnings, and tv_param_on moved out from #ifdef USE_TV
alex
parents:
2818
diff
changeset
|
317 if (priv->mmap == (unsigned char *)-1) |
2802 | 318 { |
3815 | 319 mp_msg(MSGT_TV, MSGL_ERR, "Unable to map memory for buffers: %s\n", strerror(errno)); |
2802 | 320 goto err; |
321 } | |
2818 | 322 mp_msg(MSGT_TV, MSGL_DBG2, "our buffer: %p\n", priv->mmap); |
2790 | 323 |
2802 | 324 /* num of buffers */ |
325 priv->nbuf = priv->mbuf.frames; | |
2790 | 326 |
2802 | 327 /* video buffers */ |
2819
2e58962dc9fe
cleaned up some warnings, and tv_param_on moved out from #ifdef USE_TV
alex
parents:
2818
diff
changeset
|
328 priv->buf = (struct video_mmap *)malloc(priv->nbuf * sizeof(struct video_mmap)); |
3611 | 329 if (!priv->buf) |
330 goto malloc_failed; | |
2802 | 331 memset(priv->buf, 0, priv->nbuf * sizeof(struct video_mmap)); |
2790 | 332 |
3815 | 333 /* audio init */ |
334 #if 0 | |
335 priv->audio_fd = open(priv->audio_device, O_RDONLY); | |
336 if (priv->audio_fd < 0) | |
337 { | |
338 mp_msg(MSGT_TV, MSGL_ERR, "unable to open '%s': %s\n", | |
339 priv->audio_device, strerror(errno)); | |
340 } | |
341 else | |
342 { | |
343 int ioctl_param; | |
344 | |
345 fcntl(priv->audio_fd, F_SETFL, O_NONBLOCK); | |
346 | |
347 #if 0 | |
348 ioctl_param = 0x7fff000d; /* 8k */ | |
349 printf("ioctl dsp setfragment: %d\n", | |
350 ioctl(priv->audio_fd, SNDCTL_DSP_SETFRAGMENT, &ioctl_param)); | |
351 #endif | |
352 | |
353 ioctl_param = 0 ; | |
354 printf("ioctl dsp getfmt: %d\n", | |
355 ioctl(priv->audio_fd, SNDCTL_DSP_GETFMTS, &ioctl_param)); | |
356 | |
357 printf("Supported formats: %x\n", ioctl_param); | |
358 if (!(ioctl_param & priv->audio_format[priv->audio_id])) | |
359 printf("notsupported format\n"); | |
360 | |
361 ioctl_param = priv->audio_format[priv->audio_id]; | |
362 printf("ioctl dsp setfmt: %d\n", | |
363 ioctl(priv->audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param)); | |
364 | |
365 // ioctl(priv->audio_fd, SNDCTL_DSP_GETISPACE, &ioctl_param); | |
366 // printf("getispace: %d\n", ioctl_param); | |
367 | |
368 if (priv->audio_channels[priv->audio_id] > 2) | |
369 { | |
370 ioctl_param = priv->audio_channels[priv->audio_id]; | |
371 printf("ioctl dsp channels: %d\n", | |
372 ioctl(priv->audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param)); | |
373 } | |
374 else | |
375 { | |
376 // if (priv->audio_channels[priv->audio_id] == 2) | |
377 // ioctl_param = 1; | |
378 // else | |
379 // ioctl_param = 0; | |
380 | |
381 ioctl_param = (priv->audio_channels[priv->audio_id] == 2); | |
382 printf("ioctl dsp stereo: %d (req: %d)\n", | |
383 ioctl(priv->audio_fd, SNDCTL_DSP_STEREO, &ioctl_param), | |
384 ioctl_param); | |
385 } | |
386 | |
387 ioctl_param = priv->audio_samplerate[priv->audio_id]; | |
388 printf("ioctl dsp speed: %d\n", | |
389 ioctl(priv->audio_fd, SNDCTL_DSP_SPEED, &ioctl_param)); | |
390 | |
391 #if 0 | |
392 ioctl_param = 0; | |
393 ioctl_param = ~PCM_ENABLE_INPUT; | |
394 printf("ioctl dsp trigger: %d\n", | |
395 ioctl(priv->audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param)); | |
396 ioctl_param = PCM_ENABLE_INPUT; | |
397 printf("ioctl dsp trigger: %d\n", | |
398 ioctl(priv->audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param)); | |
399 #endif | |
400 | |
401 printf("ioctl dsp trigger: %d\n", | |
402 ioctl(priv->audio_fd, SNDCTL_DSP_GETTRIGGER, &ioctl_param)); | |
403 printf("trigger: %x\n", ioctl_param); | |
404 ioctl_param = PCM_ENABLE_INPUT; | |
405 printf("ioctl dsp trigger: %d\n", | |
406 ioctl(priv->audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param)); | |
407 | |
408 printf("ioctl dsp getblocksize: %d\n", | |
409 ioctl(priv->audio_fd, SNDCTL_DSP_GETBLKSIZE, &priv->audio_blocksize)); | |
410 printf("blocksize: %d\n", priv->audio_blocksize); | |
411 } | |
412 #endif | |
2790 | 413 return(1); |
414 | |
3611 | 415 |
416 malloc_failed: | |
417 if (priv->channels) | |
418 free(priv->channels); | |
419 if (priv->buf) | |
420 free(priv->buf); | |
2790 | 421 err: |
3815 | 422 if (priv->video_fd != -1) |
423 close(priv->video_fd); | |
2790 | 424 return(0); |
425 } | |
426 | |
2802 | 427 static int uninit(priv_t *priv) |
2790 | 428 { |
3815 | 429 close(priv->video_fd); |
430 | |
431 priv->audio[priv->audio_id].volume = 0; | |
432 priv->audio[priv->audio_id].flags |= VIDEO_AUDIO_MUTE; | |
433 ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]); | |
434 close(priv->audio_fd); | |
2802 | 435 #warning "Implement uninit!" |
2931 | 436 |
437 return(1); | |
2790 | 438 } |
439 | |
2802 | 440 static int start(priv_t *priv) |
2790 | 441 { |
2802 | 442 int i; |
443 | |
444 | |
3815 | 445 if (ioctl(priv->video_fd, VIDIOCGPICT, &priv->picture) == -1) |
2790 | 446 { |
2818 | 447 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get picture failed: %s\n", strerror(errno)); |
2802 | 448 return(0); |
2790 | 449 } |
450 | |
2802 | 451 priv->picture.palette = format2palette(priv->format); |
452 priv->picture.depth = palette2depth(priv->picture.palette); | |
453 priv->bytesperline = priv->width * priv->picture.depth / 8; | |
3815 | 454 // if (IMGFMT_IS_BGR(priv->format) || IMGFMT_IS_RGB(priv->format)) |
455 // priv->bytesperline = priv->width * priv->picture.depth / 8; | |
456 // if ((priv->format == IMGFMT_YV12) || (priv->format == IMGFMT_I420) || (priv->format == IMGFMT_IYUV)) | |
457 // priv->bytesperline = priv->width * 3 / 2; | |
458 | |
459 printf("palette: %d, depth: %d, bytesperline: %d\n", | |
460 priv->picture.palette, priv->picture.depth, priv->bytesperline); | |
461 | |
2818 | 462 mp_msg(MSGT_TV, MSGL_INFO, "Picture values:\n"); |
463 mp_msg(MSGT_TV, MSGL_INFO, " Depth: %d, Palette: %d (Format: %s)\n", priv->picture.depth, | |
2802 | 464 priv->picture.palette, vo_format_name(priv->format)); |
2818 | 465 mp_msg(MSGT_TV, MSGL_INFO, " Brightness: %d, Hue: %d, Colour: %d, Contrast: %d\n", |
2802 | 466 priv->picture.brightness, priv->picture.hue, |
467 priv->picture.colour, priv->picture.contrast); | |
468 | |
469 | |
3815 | 470 if (ioctl(priv->video_fd, VIDIOCSPICT, &priv->picture) == -1) |
2790 | 471 { |
2818 | 472 mp_msg(MSGT_TV, MSGL_ERR, "ioctl set picture failed: %s\n", strerror(errno)); |
2802 | 473 return(0); |
2790 | 474 } |
475 | |
2802 | 476 priv->nbuf = priv->mbuf.frames; |
477 for (i=0; i < priv->nbuf; i++) | |
478 { | |
479 priv->buf[i].format = priv->picture.palette; | |
480 priv->buf[i].frame = i; | |
481 priv->buf[i].width = priv->width; | |
482 priv->buf[i].height = priv->height; | |
2818 | 483 mp_msg(MSGT_TV, MSGL_DBG2, "buffer: %d => %p\n", i, &priv->buf[i]); |
2802 | 484 } |
2837 | 485 |
2931 | 486 #if 0 |
487 { | |
488 struct video_play_mode pmode; | |
489 | |
490 pmode.mode = VID_PLAY_NORMAL; | |
491 pmode.p1 = 1; | |
492 pmode.p2 = 0; | |
3815 | 493 if (ioctl(priv->video_fd, VIDIOCSPLAYMODE, &pmode) == -1) |
2931 | 494 { |
495 mp_msg(MSGT_TV, MSGL_ERR, "ioctl set play mode failed: %s\n", strerror(errno)); | |
496 // return(0); | |
497 } | |
498 } | |
499 #endif | |
2837 | 500 |
501 #if 0 | |
502 { | |
503 struct video_window win; | |
504 | |
505 win.x = 0; | |
506 win.y = 0; | |
507 win.width = priv->width; | |
508 win.height = priv->height; | |
509 win.chromakey = -1; | |
510 win.flags = 0; | |
511 | |
3815 | 512 ioctl(priv->video_fd, VIDIOCSWIN, &win); |
2837 | 513 } |
514 | |
2802 | 515 /* start capture */ |
3815 | 516 if (ioctl(priv->video_fd, VIDIOCCAPTURE, &one) == -1) |
2802 | 517 { |
2818 | 518 mp_msg(MSGT_TV, MSGL_ERR, "ioctl capture failed: %s\n", strerror(errno)); |
2802 | 519 return(0); |
520 } | |
2837 | 521 #endif |
522 | |
523 return(1); | |
2790 | 524 } |
525 | |
526 static int control(priv_t *priv, int cmd, void *arg) | |
527 { | |
2818 | 528 mp_msg(MSGT_TV, MSGL_DBG2, "debug: control(priv=%p, cmd=%d, arg=%p)\n", |
2802 | 529 priv, cmd, arg); |
2790 | 530 switch(cmd) |
531 { | |
2802 | 532 /* ========== GENERIC controls =========== */ |
533 case TVI_CONTROL_IS_VIDEO: | |
534 { | |
535 if (priv->capability.type & VID_TYPE_CAPTURE) | |
536 return(TVI_CONTROL_TRUE); | |
537 return(TVI_CONTROL_FALSE); | |
538 } | |
539 case TVI_CONTROL_IS_AUDIO: | |
3815 | 540 return(TVI_CONTROL_FALSE); |
541 /* also disable audio for as it's not working! */ | |
2841 | 542 if (priv->channels[priv->act_channel].flags & VIDEO_VC_AUDIO) |
3815 | 543 { |
544 // printf("yeah, audio csennel!!"); | |
2841 | 545 return(TVI_CONTROL_TRUE); |
3815 | 546 } |
547 return(TVI_CONTROL_TRUE); | |
2802 | 548 case TVI_CONTROL_IS_TUNER: |
549 { | |
2841 | 550 // if (priv->capability.type & VID_TYPE_TUNER) |
551 if (priv->channels[priv->act_channel].flags & VIDEO_VC_TUNER) | |
2802 | 552 return(TVI_CONTROL_TRUE); |
553 return(TVI_CONTROL_FALSE); | |
554 } | |
555 | |
556 /* ========== VIDEO controls =========== */ | |
2790 | 557 case TVI_CONTROL_VID_GET_FORMAT: |
2802 | 558 { |
559 int output_fmt = -1; | |
560 | |
561 output_fmt = priv->format; | |
562 (int)*(void **)arg = output_fmt; | |
2818 | 563 mp_msg(MSGT_TV, MSGL_INFO, "Output format: %s\n", vo_format_name(output_fmt)); |
2802 | 564 return(TVI_CONTROL_TRUE); |
565 } | |
566 case TVI_CONTROL_VID_SET_FORMAT: | |
567 priv->format = (int)*(void **)arg; | |
2790 | 568 return(TVI_CONTROL_TRUE); |
569 case TVI_CONTROL_VID_GET_PLANES: | |
3220 | 570 (int)*(void **)arg = 1; /* FIXME, also not needed at this time */ |
2790 | 571 return(TVI_CONTROL_TRUE); |
572 case TVI_CONTROL_VID_GET_BITS: | |
2810 | 573 (int)*(void **)arg = palette2depth(format2palette(priv->format)); |
2790 | 574 return(TVI_CONTROL_TRUE); |
575 case TVI_CONTROL_VID_GET_WIDTH: | |
576 (int)*(void **)arg = priv->width; | |
577 return(TVI_CONTROL_TRUE); | |
578 case TVI_CONTROL_VID_CHK_WIDTH: | |
579 { | |
580 int req_width = (int)*(void **)arg; | |
581 | |
2818 | 582 mp_msg(MSGT_TV, MSGL_INFO, "Requested width: %d\n", req_width); |
2810 | 583 if ((req_width >= priv->capability.minwidth) && |
584 (req_width <= priv->capability.maxwidth)) | |
2790 | 585 return(TVI_CONTROL_TRUE); |
586 return(TVI_CONTROL_FALSE); | |
587 } | |
588 case TVI_CONTROL_VID_SET_WIDTH: | |
589 priv->width = (int)*(void **)arg; | |
590 return(TVI_CONTROL_TRUE); | |
591 case TVI_CONTROL_VID_GET_HEIGHT: | |
592 (int)*(void **)arg = priv->height; | |
593 return(TVI_CONTROL_TRUE); | |
594 case TVI_CONTROL_VID_CHK_HEIGHT: | |
595 { | |
596 int req_height = (int)*(void **)arg; | |
597 | |
2818 | 598 mp_msg(MSGT_TV, MSGL_INFO, "Requested height: %d\n", req_height); |
2810 | 599 if ((req_height >= priv->capability.minheight) && |
600 (req_height <= priv->capability.maxheight)) | |
2790 | 601 return(TVI_CONTROL_TRUE); |
602 return(TVI_CONTROL_FALSE); | |
603 } | |
604 case TVI_CONTROL_VID_SET_HEIGHT: | |
605 priv->height = (int)*(void **)arg; | |
606 return(TVI_CONTROL_TRUE); | |
2937 | 607 case TVI_CONTROL_VID_GET_PICTURE: |
3815 | 608 if (ioctl(priv->video_fd, VIDIOCGPICT, &priv->picture) == -1) |
2937 | 609 { |
610 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get picture failed: %s\n", strerror(errno)); | |
611 return(TVI_CONTROL_FALSE); | |
612 } | |
613 return(TVI_CONTROL_TRUE); | |
614 case TVI_CONTROL_VID_SET_PICTURE: | |
3815 | 615 if (ioctl(priv->video_fd, VIDIOCSPICT, &priv->picture) == -1) |
2937 | 616 { |
617 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get picture failed: %s\n", strerror(errno)); | |
618 return(TVI_CONTROL_FALSE); | |
619 } | |
620 return(TVI_CONTROL_TRUE); | |
621 case TVI_CONTROL_VID_SET_BRIGHTNESS: | |
622 priv->picture.brightness = (int)*(void **)arg; | |
623 control(priv, TVI_CONTROL_VID_SET_PICTURE, 0); | |
624 return(TVI_CONTROL_TRUE); | |
625 case TVI_CONTROL_VID_SET_HUE: | |
626 priv->picture.hue = (int)*(void **)arg; | |
627 control(priv, TVI_CONTROL_VID_SET_PICTURE, 0); | |
628 return(TVI_CONTROL_TRUE); | |
629 case TVI_CONTROL_VID_SET_SATURATION: | |
630 priv->picture.colour = (int)*(void **)arg; | |
631 control(priv, TVI_CONTROL_VID_SET_PICTURE, 0); | |
632 return(TVI_CONTROL_TRUE); | |
633 case TVI_CONTROL_VID_SET_CONTRAST: | |
634 priv->picture.contrast = (int)*(void **)arg; | |
635 control(priv, TVI_CONTROL_VID_SET_PICTURE, 0); | |
636 return(TVI_CONTROL_TRUE); | |
2790 | 637 |
2802 | 638 /* ========== TUNER controls =========== */ |
639 case TVI_CONTROL_TUN_GET_FREQ: | |
640 { | |
641 unsigned long freq; | |
642 | |
3815 | 643 if (ioctl(priv->video_fd, VIDIOCGFREQ, &freq) == -1) |
2802 | 644 { |
2818 | 645 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get freq failed: %s\n", strerror(errno)); |
2802 | 646 return(TVI_CONTROL_FALSE); |
647 } | |
648 | |
649 /* tuner uses khz not mhz ! */ | |
2837 | 650 // if (priv->tuner.flags & VIDEO_TUNER_LOW) |
651 // freq /= 1000; | |
2802 | 652 (unsigned long)*(void **)arg = freq; |
653 return(TVI_CONTROL_TRUE); | |
654 } | |
2790 | 655 case TVI_CONTROL_TUN_SET_FREQ: |
656 { | |
2802 | 657 /* argument is in MHz ! */ |
658 unsigned long freq = (unsigned long)*(void **)arg; | |
659 | |
2837 | 660 mp_msg(MSGT_TV, MSGL_V, "requested frequency: %.3f\n", (float)freq/16); |
2802 | 661 |
662 /* tuner uses khz not mhz ! */ | |
2837 | 663 // if (priv->tuner.flags & VIDEO_TUNER_LOW) |
664 // freq *= 1000; | |
665 // mp_msg(MSGT_TV, MSGL_V, " requesting from driver: freq=%.3f\n", (float)freq/16); | |
3815 | 666 if (ioctl(priv->video_fd, VIDIOCSFREQ, &freq) == -1) |
2802 | 667 { |
2818 | 668 mp_msg(MSGT_TV, MSGL_ERR, "ioctl set freq failed: %s\n", strerror(errno)); |
2802 | 669 return(TVI_CONTROL_FALSE); |
670 } | |
671 return(TVI_CONTROL_TRUE); | |
672 } | |
673 case TVI_CONTROL_TUN_GET_TUNER: | |
674 { | |
3815 | 675 if (ioctl(priv->video_fd, VIDIOCGTUNER, &priv->tuner) == -1) |
2802 | 676 { |
2818 | 677 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get tuner failed: %s\n", strerror(errno)); |
2802 | 678 return(TVI_CONTROL_FALSE); |
679 } | |
680 | |
2818 | 681 mp_msg(MSGT_TV, MSGL_INFO, "Tuner (%s) range: %lu -> %lu\n", priv->tuner.name, |
2802 | 682 priv->tuner.rangelow, priv->tuner.rangehigh); |
683 return(TVI_CONTROL_TRUE); | |
684 } | |
685 case TVI_CONTROL_TUN_SET_TUNER: | |
686 { | |
3815 | 687 if (ioctl(priv->video_fd, VIDIOCSTUNER, &priv->tuner) == -1) |
2802 | 688 { |
2818 | 689 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get tuner failed: %s\n", strerror(errno)); |
2802 | 690 return(TVI_CONTROL_FALSE); |
691 } | |
692 return(TVI_CONTROL_TRUE); | |
693 } | |
694 case TVI_CONTROL_TUN_SET_NORM: | |
695 { | |
696 int req_mode = (int)*(void **)arg; | |
697 | |
698 if ((!(priv->tuner.flags & VIDEO_TUNER_NORM)) || | |
699 ((req_mode == VIDEO_MODE_PAL) && !(priv->tuner.flags & VIDEO_TUNER_PAL)) || | |
700 ((req_mode == VIDEO_MODE_NTSC) && !(priv->tuner.flags & VIDEO_TUNER_NTSC)) || | |
701 ((req_mode == VIDEO_MODE_SECAM) && !(priv->tuner.flags & VIDEO_TUNER_SECAM))) | |
702 { | |
2818 | 703 mp_msg(MSGT_TV, MSGL_ERR, "Tuner isn't capable to set norm!\n"); |
2802 | 704 return(TVI_CONTROL_FALSE); |
705 } | |
706 | |
707 priv->tuner.mode = req_mode; | |
2790 | 708 |
3815 | 709 if (control(priv->video_fd, TVI_CONTROL_TUN_SET_TUNER, &priv->tuner) != TVI_CONTROL_TRUE) |
2802 | 710 return(TVI_CONTROL_FALSE); |
711 return(TVI_CONTROL_TRUE); | |
712 } | |
713 case TVI_CONTROL_TUN_GET_NORM: | |
714 { | |
715 (int)*(void **)arg = priv->tuner.mode; | |
716 | |
717 return(TVI_CONTROL_TRUE); | |
718 } | |
719 | |
720 /* ========== AUDIO controls =========== */ | |
721 case TVI_CONTROL_AUD_GET_FORMAT: | |
722 { | |
3815 | 723 (int)*(void **)arg = priv->audio_format[priv->audio_id]; |
2802 | 724 return(TVI_CONTROL_TRUE); |
725 } | |
726 case TVI_CONTROL_AUD_GET_CHANNELS: | |
727 { | |
3815 | 728 (int)*(void **)arg = priv->audio_channels[priv->audio_id]; |
2802 | 729 return(TVI_CONTROL_TRUE); |
730 } | |
731 case TVI_CONTROL_AUD_GET_SAMPLERATE: | |
732 { | |
3815 | 733 (int)*(void **)arg = priv->audio_samplerate[priv->audio_id]; |
2802 | 734 return(TVI_CONTROL_TRUE); |
735 } | |
736 case TVI_CONTROL_AUD_GET_SAMPLESIZE: | |
737 { | |
3815 | 738 (int)*(void **)arg = priv->audio_samplesize[priv->audio_id]; |
2802 | 739 return(TVI_CONTROL_TRUE); |
740 } | |
741 | |
742 /* ========== SPECIFIC controls =========== */ | |
743 case TVI_CONTROL_SPC_GET_INPUT: | |
744 { | |
745 int req_chan = (int)*(void **)arg; | |
746 int i; | |
747 | |
748 for (i = 0; i < priv->capability.channels; i++) | |
749 { | |
750 if (priv->channels[i].channel == req_chan) | |
751 break; | |
752 } | |
2841 | 753 |
754 priv->act_channel = i; | |
2802 | 755 |
3815 | 756 if (ioctl(priv->video_fd, VIDIOCGCHAN, &priv->channels[i]) == -1) |
2802 | 757 { |
2818 | 758 mp_msg(MSGT_TV, MSGL_ERR, "ioctl get channel failed: %s\n", strerror(errno)); |
2802 | 759 return(TVI_CONTROL_FALSE); |
760 } | |
761 return(TVI_CONTROL_TRUE); | |
762 } | |
763 | |
764 case TVI_CONTROL_SPC_SET_INPUT: | |
765 { | |
766 struct video_channel chan; | |
767 int req_chan = (int)*(void **)arg; | |
768 int i; | |
769 | |
770 if (req_chan >= priv->capability.channels) | |
771 { | |
2818 | 772 mp_msg(MSGT_TV, MSGL_ERR, "Invalid input requested: %d, valid: 0-%d\n", |
2802 | 773 req_chan, priv->capability.channels); |
774 return(TVI_CONTROL_FALSE); | |
775 } | |
776 | |
777 for (i = 0; i < priv->capability.channels; i++) | |
778 { | |
779 if (priv->channels[i].channel == req_chan) | |
780 chan = priv->channels[i]; | |
781 } | |
782 | |
3815 | 783 if (ioctl(priv->video_fd, VIDIOCSCHAN, &chan) == -1) |
2802 | 784 { |
2818 | 785 mp_msg(MSGT_TV, MSGL_ERR, "ioctl set chan failed: %s\n", strerror(errno)); |
2802 | 786 return(TVI_CONTROL_FALSE); |
787 } | |
2818 | 788 mp_msg(MSGT_TV, MSGL_INFO, "Using input '%s'\n", chan.name); |
2802 | 789 |
2841 | 790 priv->act_channel = i; |
791 | |
2802 | 792 /* update tuner state */ |
2841 | 793 // if (priv->capability.type & VID_TYPE_TUNER) |
794 if (priv->channels[priv->act_channel].flags & VIDEO_VC_TUNER) | |
2802 | 795 control(priv, TVI_CONTROL_TUN_GET_TUNER, 0); |
796 | |
797 /* update local channel list */ | |
798 control(priv, TVI_CONTROL_SPC_GET_INPUT, &req_chan); | |
799 return(TVI_CONTROL_TRUE); | |
2790 | 800 } |
801 } | |
802 | |
803 return(TVI_CONTROL_UNKNOWN); | |
804 } | |
805 | |
806 static int grab_video_frame(priv_t *priv, char *buffer, int len) | |
807 { | |
2802 | 808 int frame = priv->queue % priv->nbuf; |
2814 | 809 int nextframe = (priv->queue+1) % priv->nbuf; |
2802 | 810 |
3284 | 811 mp_dbg(MSGT_TV, MSGL_DBG2, "grab_video_frame(priv=%p, buffer=%p, len=%d)\n", |
2802 | 812 priv, buffer, len); |
813 | |
2931 | 814 mp_dbg(MSGT_TV, MSGL_DBG3, "buf: %p + frame: %d => %p\n", |
2814 | 815 priv->buf, nextframe, &priv->buf[nextframe]); |
3815 | 816 if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[nextframe]) == -1) |
2790 | 817 { |
2818 | 818 mp_msg(MSGT_TV, MSGL_ERR, "ioctl mcapture failed: %s\n", strerror(errno)); |
2802 | 819 return(0); |
2790 | 820 } |
3711 | 821 |
3815 | 822 while (ioctl(priv->video_fd, VIDIOCSYNC, &priv->buf[frame].frame) < 0 && |
3711 | 823 (errno == EAGAIN || errno == EINTR)); |
824 mp_dbg(MSGT_TV, MSGL_DBG3, "picture sync failed\n"); | |
825 | |
2802 | 826 priv->queue++; |
827 | |
2931 | 828 mp_dbg(MSGT_TV, MSGL_DBG3, "mmap: %p + offset: %d => %p\n", |
2802 | 829 priv->mmap, priv->mbuf.offsets[frame], |
830 priv->mmap+priv->mbuf.offsets[frame]); | |
2931 | 831 |
832 /* XXX also directrendering would be nicer! */ | |
833 /* 3 times copying the same picture to other buffer :( */ | |
834 | |
835 /* copy the actual frame */ | |
2802 | 836 memcpy(buffer, priv->mmap+priv->mbuf.offsets[frame], len); |
837 | |
838 return(len); | |
2790 | 839 } |
840 | |
841 static int get_video_framesize(priv_t *priv) | |
842 { | |
2931 | 843 return(priv->bytesperline * priv->height); |
2790 | 844 } |
845 | |
846 static int grab_audio_frame(priv_t *priv, char *buffer, int len) | |
847 { | |
3815 | 848 int in_len = 0; |
849 | |
850 /* DBG2 !! */ | |
851 mp_dbg(MSGT_TV, MSGL_V, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n", | |
852 priv, buffer, len); | |
853 | |
854 for (;;) | |
855 { | |
856 in_len = read(priv->audio_fd, buffer, len); | |
857 // printf("in_len: %d\n", in_len); | |
858 // fflush(NULL); | |
859 | |
860 if (in_len > 0) | |
861 break; | |
862 if (!((in_len == 0) || (in_len == -1 && (errno == EAGAIN || errno == EINTR)))) | |
863 { | |
864 in_len = 0; /* -EIO */ | |
865 break; | |
866 } | |
867 } | |
868 | |
869 return(in_len); | |
2790 | 870 } |
871 | |
872 static int get_audio_framesize(priv_t *priv) | |
873 { | |
3815 | 874 return(priv->audio_blocksize); |
875 // return(priv->audio_samplesize[priv->audio_id]); | |
2790 | 876 } |
877 | |
878 #endif /* USE_TV */ |