Make sure that -sid works even if the number of subtitles initially detected
[mplayer/glamo.git] / stream / tv.c
blob44d16a42fee19ae7c0fb418d039937c4b0b41b2b
1 /*
2 TV Interface for MPlayer
4 (C) Alex Beregszaszi
6 API idea based on libvo2
8 Feb 19, 2002: Significant rewrites by Charles R. Henrich (henrich@msu.edu)
9 to add support for audio, and bktr *BSD support.
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <sys/time.h>
20 #include "config.h"
22 int tv_param_on = 0;
24 #include "mp_msg.h"
25 #include "help_mp.h"
27 #include "stream.h"
28 #include "libmpdemux/demuxer.h"
29 #include "libmpdemux/stheader.h"
31 #include "libaf/af_format.h"
32 #include "libmpcodecs/img_format.h"
33 #include "libvo/fastmemcpy.h"
35 #include "tv.h"
37 #include "frequencies.h"
39 /* some default values */
40 int tv_param_audiorate = 44100;
41 int tv_param_noaudio = 0;
42 int tv_param_immediate = 0;
43 char *tv_param_freq = NULL;
44 char *tv_param_channel = NULL;
45 char *tv_param_norm = "pal";
46 #ifdef HAVE_TV_V4L2
47 int tv_param_normid = -1;
48 #endif
49 char *tv_param_chanlist = "europe-east";
50 char *tv_param_device = NULL;
51 char *tv_param_driver = "dummy";
52 int tv_param_width = -1;
53 int tv_param_height = -1;
54 int tv_param_input = 0; /* used in v4l and bttv */
55 int tv_param_outfmt = -1;
56 float tv_param_fps = -1.0;
57 char **tv_param_channels = NULL;
58 int tv_param_audio_id = 0;
59 #if defined(HAVE_TV_V4L)
60 int tv_param_amode = -1;
61 int tv_param_volume = -1;
62 int tv_param_bass = -1;
63 int tv_param_treble = -1;
64 int tv_param_balance = -1;
65 int tv_param_forcechan = -1;
66 int tv_param_force_audio = 0;
67 int tv_param_buffer_size = -1;
68 int tv_param_mjpeg = 0;
69 int tv_param_decimation = 2;
70 int tv_param_quality = 90;
71 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
72 int tv_param_alsa = 0;
73 #endif
74 char* tv_param_adevice = NULL;
75 #endif
76 int tv_param_brightness = 0;
77 int tv_param_contrast = 0;
78 int tv_param_hue = 0;
79 int tv_param_saturation = 0;
80 tv_channels_t *tv_channel_list;
81 tv_channels_t *tv_channel_current, *tv_channel_last;
82 char *tv_channel_last_real;
84 /* ================== DEMUX_TV ===================== */
86 Return value:
87 0 = EOF(?) or no stream
88 1 = successfully read a packet
90 /* fill demux->video and demux->audio */
92 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
94 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
95 demux_packet_t* dp;
96 unsigned int len=0;
98 /* ================== ADD AUDIO PACKET =================== */
100 if (ds==demux->audio && tv_param_noaudio == 0 &&
101 tvh->functions->control(tvh->priv,
102 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
104 len = tvh->functions->get_audio_framesize(tvh->priv);
106 dp=new_demux_packet(len);
107 dp->flags|=1; /* Keyframe */
108 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
109 ds_add_packet(demux->audio,dp);
112 /* ================== ADD VIDEO PACKET =================== */
114 if (ds==demux->video && tvh->functions->control(tvh->priv,
115 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
117 len = tvh->functions->get_video_framesize(tvh->priv);
118 dp=new_demux_packet(len);
119 dp->flags|=1; /* Keyframe */
120 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
121 ds_add_packet(demux->video,dp);
124 return 1;
127 static int norm_from_string(tvi_handle_t *tvh, char* norm)
129 #ifdef HAVE_TV_V4L2
130 if (strcmp(tv_param_driver, "v4l2") != 0) {
131 #endif
132 if (!strcasecmp(norm, "pal"))
133 return TV_NORM_PAL;
134 else if (!strcasecmp(norm, "ntsc"))
135 return TV_NORM_NTSC;
136 else if (!strcasecmp(norm, "secam"))
137 return TV_NORM_SECAM;
138 else if (!strcasecmp(norm, "palnc"))
139 return TV_NORM_PALNC;
140 else if (!strcasecmp(norm, "palm"))
141 return TV_NORM_PALM;
142 else if (!strcasecmp(norm, "paln"))
143 return TV_NORM_PALN;
144 else if (!strcasecmp(norm, "ntscjp"))
145 return TV_NORM_NTSCJP;
146 else {
147 mp_msg(MSGT_TV, MSGL_V, "tv.c: norm_from_string(%s): Bogus norm parameter, setting PAL.\n", norm);
148 return TV_NORM_PAL;
150 #ifdef HAVE_TV_V4L2
151 } else {
152 tvi_functions_t *funcs = tvh->functions;
153 char str[8];
154 strncpy(str, norm, sizeof(str)-1);
155 str[sizeof(str)-1] = '\0';
156 if (funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str) != TVI_CONTROL_TRUE)
157 return 0;
158 return *(int *)str;
160 #endif
163 static int open_tv(tvi_handle_t *tvh)
165 int i;
166 tvi_functions_t *funcs = tvh->functions;
167 int tv_fmt_list[] = {
168 IMGFMT_YV12,
169 IMGFMT_I420,
170 IMGFMT_UYVY,
171 IMGFMT_YUY2,
172 IMGFMT_RGB32,
173 IMGFMT_RGB24,
174 IMGFMT_RGB16,
175 IMGFMT_RGB15
178 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
180 mp_msg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
181 return 0;
184 if (tv_param_outfmt == -1)
185 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
187 tv_param_outfmt = tv_fmt_list[i];
188 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
189 &tv_param_outfmt) == TVI_CONTROL_TRUE)
190 break;
192 else
194 switch(tv_param_outfmt)
196 case IMGFMT_YV12:
197 case IMGFMT_I420:
198 case IMGFMT_UYVY:
199 case IMGFMT_YUY2:
200 case IMGFMT_RGB32:
201 case IMGFMT_RGB24:
202 case IMGFMT_BGR32:
203 case IMGFMT_BGR24:
204 case IMGFMT_BGR16:
205 case IMGFMT_BGR15:
206 break;
207 default:
208 mp_msg(MSGT_TV, MSGL_ERR, "==================================================================\n");
209 mp_msg(MSGT_TV, MSGL_ERR, " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n", tv_param_outfmt);
210 mp_msg(MSGT_TV, MSGL_ERR, " This may cause buggy playback or program crash! Bug reports will\n");
211 mp_msg(MSGT_TV, MSGL_ERR, " be ignored! You should try again with YV12 (which is the default\n");
212 mp_msg(MSGT_TV, MSGL_ERR, " colorspace) and read the documentation!\n");
213 mp_msg(MSGT_TV, MSGL_ERR, "==================================================================\n");
215 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tv_param_outfmt);
218 /* set some params got from cmdline */
219 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tv_param_input);
221 #ifdef HAVE_TV_V4L2
222 if (!strcmp(tv_param_driver, "v4l2") && tv_param_normid >= 0) {
223 mp_msg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", tv_param_normid);
224 if (funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tv_param_normid) != TVI_CONTROL_TRUE) {
225 mp_msg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
227 } else {
228 #endif
229 /* select video norm */
230 tvh->norm = norm_from_string(tvh, tv_param_norm);
232 mp_msg(MSGT_TV, MSGL_V, "Selected norm: %s\n", tv_param_norm);
233 if (funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
234 mp_msg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
236 #ifdef HAVE_TV_V4L2
238 #endif
240 #ifdef HAVE_TV_V4L1
241 if ( tv_param_mjpeg )
243 /* set width to expected value */
244 if (tv_param_width == -1)
246 tv_param_width = 704/tv_param_decimation;
248 if (tv_param_height == -1)
250 if ( tvh->norm != TV_NORM_NTSC )
251 tv_param_height = 576/tv_param_decimation;
252 else
253 tv_param_height = 480/tv_param_decimation;
255 mp_msg(MSGT_TV, MSGL_INFO,
256 " MJP: width %d height %d\n", tv_param_width, tv_param_height);
258 #endif
260 /* limits on w&h are norm-dependent -- JM */
261 /* set width */
262 if (tv_param_width != -1)
264 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tv_param_width) == TVI_CONTROL_TRUE)
265 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tv_param_width);
266 else
268 mp_msg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tv_param_width);
269 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tv_param_width);
273 /* set height */
274 if (tv_param_height != -1)
276 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tv_param_height) == TVI_CONTROL_TRUE)
277 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tv_param_height);
278 else
280 mp_msg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tv_param_height);
281 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tv_param_height);
285 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
287 mp_msg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
288 goto done;
291 /* select channel list */
292 for (i = 0; chanlists[i].name != NULL; i++)
294 if (!strcasecmp(chanlists[i].name, tv_param_chanlist))
296 tvh->chanlist = i;
297 tvh->chanlist_s = chanlists[i].list;
298 break;
302 if (tvh->chanlist == -1)
303 mp_msg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
304 tv_param_chanlist);
305 else
306 mp_msg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
307 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
309 if (tv_param_freq && tv_param_channel)
311 mp_msg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
312 goto done;
315 /* Handle channel names */
316 if (tv_param_channels) {
317 char** channels = tv_param_channels;
318 mp_msg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
319 tv_channel_list = malloc(sizeof(tv_channels_t));
320 tv_channel_list->index=1;
321 tv_channel_list->next=NULL;
322 tv_channel_list->prev=NULL;
323 tv_channel_current = tv_channel_list;
325 while (*channels) {
326 char* tmp = *(channels++);
327 char* sep = strchr(tmp,'-');
328 int i;
329 struct CHANLIST cl;
331 if (!sep) continue; // Wrong syntax, but mplayer should not crash
333 strlcpy(tv_channel_current->name, sep + 1,
334 sizeof(tv_channel_current->name));
335 sep[0] = '\0';
336 strncpy(tv_channel_current->number, tmp, 5);
338 while ((sep=strchr(tv_channel_current->name, '_')))
339 sep[0] = ' ';
341 tv_channel_current->freq = 0;
342 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
343 cl = tvh->chanlist_s[i];
344 if (!strcasecmp(cl.name, tv_channel_current->number)) {
345 tv_channel_current->freq=cl.freq;
346 break;
349 if (tv_channel_current->freq == 0)
350 mp_msg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
351 tv_channel_current->number, tv_channel_current->name);
352 else {
353 sep = strchr(tv_channel_current->name, '-');
354 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
356 if ( sep ) {
357 i = atoi (sep+1);
358 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
359 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
360 sep[0] = '\0';
364 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
365 tv_channel_current->number, tv_channel_current->name,
366 (float)tv_channel_current->freq/1000);*/
368 tv_channel_current->next = malloc(sizeof(tv_channels_t));
369 tv_channel_current->next->index = tv_channel_current->index + 1;
370 tv_channel_current->next->prev = tv_channel_current;
371 tv_channel_current->next->next = NULL;
372 tv_channel_current = tv_channel_current->next;
374 if (tv_channel_current->prev)
375 tv_channel_current->prev->next = NULL;
376 free(tv_channel_current);
377 } else
378 tv_channel_last_real = malloc(5);
380 if (tv_channel_list) {
381 int i;
382 int channel = 0;
383 if (tv_param_channel)
385 if (isdigit(*tv_param_channel))
386 /* if tv_param_channel begins with a digit interpret it as a number */
387 channel = atoi(tv_param_channel);
388 else
390 /* if tv_param_channel does not begin with a digit
391 set the first channel that contains tv_param_channel in its name */
393 tv_channel_current = tv_channel_list;
394 while ( tv_channel_current ) {
395 if ( strstr(tv_channel_current->name, tv_param_channel) )
396 break;
397 tv_channel_current = tv_channel_current->next;
399 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
402 else
403 channel = 1;
405 if ( channel ) {
406 tv_channel_current = tv_channel_list;
407 for (i = 1; i < channel; i++)
408 if (tv_channel_current->next)
409 tv_channel_current = tv_channel_current->next;
412 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
413 tv_channel_current->name, (float)tv_channel_current->freq/1000);
414 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
415 tv_channel_last = tv_channel_current;
416 } else {
417 /* we need to set frequency */
418 if (tv_param_freq)
420 unsigned long freq = atof(tv_param_freq)*16;
422 /* set freq in MHz */
423 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
425 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
426 mp_msg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
427 freq, (float)freq/16);
430 if (tv_param_channel) {
431 struct CHANLIST cl;
433 mp_msg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tv_param_channel);
434 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
436 cl = tvh->chanlist_s[i];
437 // printf("count%d: name: %s, freq: %d\n",
438 // i, cl.name, cl.freq);
439 if (!strcasecmp(cl.name, tv_param_channel))
441 strcpy(tv_channel_last_real, cl.name);
442 tvh->channel = i;
443 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
444 cl.name, (float)cl.freq/1000);
445 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
446 break;
452 /* grep frequency in chanlist */
454 unsigned long i2;
455 int freq;
457 tv_get_freq(tvh, &i2);
459 freq = (int) (((float)(i2/16))*1000)+250;
461 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
463 if (tvh->chanlist_s[i].freq == freq)
465 tvh->channel = i+1;
466 break;
471 done:
472 /* also start device! */
473 return 1;
476 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
478 tvi_handle_t *tvh;
479 sh_video_t *sh_video;
480 sh_audio_t *sh_audio = NULL;
481 tvi_functions_t *funcs;
483 demuxer->priv=NULL;
484 if(!(tvh=tv_begin())) return NULL;
485 if (!tv_init(tvh)) return NULL;
486 if (!open_tv(tvh)){
487 tv_uninit(tvh);
488 return NULL;
490 funcs = tvh->functions;
491 demuxer->priv=tvh;
493 sh_video = new_sh_video(demuxer, 0);
495 /* get IMAGE FORMAT */
496 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
497 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
498 // sh_video->format = 0x0;
500 /* set FPS and FRAMETIME */
502 if(!sh_video->fps)
504 float tmp;
505 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
506 sh_video->fps = 25.0f; /* on PAL */
507 else sh_video->fps = tmp;
510 if (tv_param_fps != -1.0f)
511 sh_video->fps = tv_param_fps;
513 sh_video->frametime = 1.0f/sh_video->fps;
515 /* If playback only mode, go to immediate mode, fail silently */
516 if(tv_param_immediate == 1)
518 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
519 tv_param_noaudio = 1;
522 /* disable TV audio if -nosound is present */
523 if (!demuxer->audio || demuxer->audio->id == -2) {
524 tv_param_noaudio = 1;
527 /* set width */
528 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
530 /* set height */
531 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
533 demuxer->video->sh = sh_video;
534 sh_video->ds = demuxer->video;
535 demuxer->video->id = 0;
536 demuxer->seekable = 0;
538 /* here comes audio init */
539 if (tv_param_noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
541 int audio_format;
542 int sh_audio_format;
543 char buf[128];
545 /* yeah, audio is present */
547 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
548 &tv_param_audiorate);
550 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
551 goto no_audio;
553 switch(audio_format)
555 case AF_FORMAT_U8:
556 case AF_FORMAT_S8:
557 case AF_FORMAT_U16_LE:
558 case AF_FORMAT_U16_BE:
559 case AF_FORMAT_S16_LE:
560 case AF_FORMAT_S16_BE:
561 case AF_FORMAT_S32_LE:
562 case AF_FORMAT_S32_BE:
563 sh_audio_format = 0x1; /* PCM */
564 break;
565 case AF_FORMAT_IMA_ADPCM:
566 case AF_FORMAT_MU_LAW:
567 case AF_FORMAT_A_LAW:
568 case AF_FORMAT_MPEG2:
569 case AF_FORMAT_AC3:
570 default:
571 mp_msg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
572 af_fmt2str(audio_format, buf, 128), audio_format);
573 goto no_audio;
576 sh_audio = new_sh_audio(demuxer, 0);
578 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
579 &sh_audio->samplerate);
580 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
581 &sh_audio->samplesize);
582 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
583 &sh_audio->channels);
585 sh_audio->format = sh_audio_format;
586 sh_audio->sample_format = audio_format;
588 sh_audio->i_bps = sh_audio->o_bps =
589 sh_audio->samplerate * sh_audio->samplesize *
590 sh_audio->channels;
592 // emulate WF for win32 codecs:
593 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
594 sh_audio->wf->wFormatTag = sh_audio->format;
595 sh_audio->wf->nChannels = sh_audio->channels;
596 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
597 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
598 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
599 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
601 mp_msg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
602 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
603 sh_audio->wf->nSamplesPerSec);
605 demuxer->audio->sh = sh_audio;
606 sh_audio->ds = demuxer->audio;
607 demuxer->audio->id = 0;
609 no_audio:
611 if(!(funcs->start(tvh->priv))){
612 // start failed :(
613 tv_uninit(tvh);
614 return NULL;
617 /* set color eq */
618 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tv_param_brightness);
619 tv_set_color_options(tvh, TV_COLOR_HUE, tv_param_hue);
620 tv_set_color_options(tvh, TV_COLOR_SATURATION, tv_param_saturation);
621 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tv_param_contrast);
623 return demuxer;
626 static void demux_close_tv(demuxer_t *demuxer)
628 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
629 if (!tvh) return;
630 tvh->functions->uninit(tvh->priv);
631 demuxer->priv=NULL;
634 /* ================== STREAM_TV ===================== */
635 tvi_handle_t *tvi_init_dummy(char *device);
636 tvi_handle_t *tvi_init_v4l(char *device, char *adevice);
637 tvi_handle_t *tvi_init_v4l2(char *device, char *adevice);
638 tvi_handle_t *tvi_init_bsdbt848(char *device);
640 tvi_handle_t *tv_begin(void)
642 if (!strcmp(tv_param_driver, "dummy"))
643 return tvi_init_dummy(tv_param_device);
644 #ifdef HAVE_TV_V4L1
645 if (!strcmp(tv_param_driver, "v4l"))
646 return tvi_init_v4l(tv_param_device, tv_param_adevice);
647 #endif
648 #ifdef HAVE_TV_V4L2
649 if (!strcmp(tv_param_driver, "v4l2"))
650 return tvi_init_v4l2(tv_param_device, tv_param_adevice);
651 #endif
652 #ifdef HAVE_TV_BSDBT848
653 if (!strcmp(tv_param_driver, "bsdbt848"))
654 return tvi_init_bsdbt848(tv_param_device);
655 #endif
657 mp_msg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param_driver);
658 return(NULL);
661 int tv_init(tvi_handle_t *tvh)
663 mp_msg(MSGT_TV, MSGL_INFO, "Selected driver: %s\n", tvh->info->short_name);
664 mp_msg(MSGT_TV, MSGL_INFO, " name: %s\n", tvh->info->name);
665 mp_msg(MSGT_TV, MSGL_INFO, " author: %s\n", tvh->info->author);
666 if (tvh->info->comment)
667 mp_msg(MSGT_TV, MSGL_INFO, " comment: %s\n", tvh->info->comment);
669 return(tvh->functions->init(tvh->priv));
672 int tv_uninit(tvi_handle_t *tvh)
674 int res;
675 if(!tvh) return 1;
676 if (!tvh->priv) return 1;
677 res=tvh->functions->uninit(tvh->priv);
678 if(res) tvh->priv=NULL;
679 return res;
682 /* utilities for mplayer (not mencoder!!) */
683 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
685 tvi_functions_t *funcs = tvh->functions;
687 switch(opt)
689 case TV_COLOR_BRIGHTNESS:
690 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
691 case TV_COLOR_HUE:
692 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
693 case TV_COLOR_SATURATION:
694 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
695 case TV_COLOR_CONTRAST:
696 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
697 default:
698 mp_msg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
701 return(TVI_CONTROL_UNKNOWN);
704 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
706 tvi_functions_t *funcs = tvh->functions;
708 switch(opt)
710 case TV_COLOR_BRIGHTNESS:
711 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
712 case TV_COLOR_HUE:
713 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
714 case TV_COLOR_SATURATION:
715 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
716 case TV_COLOR_CONTRAST:
717 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
718 default:
719 mp_msg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
722 return(TVI_CONTROL_UNKNOWN);
725 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
727 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
729 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
730 mp_msg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
731 *freq, (float)*freq/16);
733 return(1);
736 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
738 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
740 // unsigned long freq = atof(tv_param_freq)*16;
742 /* set freq in MHz */
743 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
745 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
746 mp_msg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
747 freq, (float)freq/16);
749 return(1);
752 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
754 struct CHANLIST cl;
756 if (direction == TV_CHANNEL_LOWER)
758 if (tvh->channel-1 >= 0)
760 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
761 cl = tvh->chanlist_s[--tvh->channel];
762 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
763 cl.name, (float)cl.freq/1000);
764 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
768 if (direction == TV_CHANNEL_HIGHER)
770 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
772 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
773 cl = tvh->chanlist_s[++tvh->channel];
774 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
775 cl.name, (float)cl.freq/1000);
776 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
779 return(1);
782 int tv_step_channel(tvi_handle_t *tvh, int direction) {
783 if (tv_channel_list) {
784 if (direction == TV_CHANNEL_HIGHER) {
785 tv_channel_last = tv_channel_current;
786 if (tv_channel_current->next)
787 tv_channel_current = tv_channel_current->next;
788 else
789 tv_channel_current = tv_channel_list;
790 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
791 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
792 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
794 if (direction == TV_CHANNEL_LOWER) {
795 tv_channel_last = tv_channel_current;
796 if (tv_channel_current->prev)
797 tv_channel_current = tv_channel_current->prev;
798 else
799 while (tv_channel_current->next)
800 tv_channel_current = tv_channel_current->next;
801 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
802 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
803 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
805 } else tv_step_channel_real(tvh, direction);
806 return(1);
809 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
810 int i;
811 struct CHANLIST cl;
813 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
814 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
816 cl = tvh->chanlist_s[i];
817 // printf("count%d: name: %s, freq: %d\n",
818 // i, cl.name, cl.freq);
819 if (!strcasecmp(cl.name, channel))
821 tvh->channel = i;
822 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
823 cl.name, (float)cl.freq/1000);
824 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
825 break;
828 return(1);
831 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
832 int i, channel_int;
834 if (tv_channel_list) {
835 tv_channel_last = tv_channel_current;
836 channel_int = atoi(channel);
837 tv_channel_current = tv_channel_list;
838 for (i = 1; i < channel_int; i++)
839 if (tv_channel_current->next)
840 tv_channel_current = tv_channel_current->next;
841 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
842 tv_channel_current->name, (float)tv_channel_current->freq/1000);
843 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
844 } else tv_set_channel_real(tvh, channel);
845 return(1);
848 int tv_last_channel(tvi_handle_t *tvh) {
850 if (tv_channel_list) {
851 tv_channels_t *tmp;
853 tmp = tv_channel_last;
854 tv_channel_last = tv_channel_current;
855 tv_channel_current = tmp;
857 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
858 tv_channel_current->name, (float)tv_channel_current->freq/1000);
859 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
860 } else {
861 int i;
862 struct CHANLIST cl;
864 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
866 cl = tvh->chanlist_s[i];
867 if (!strcasecmp(cl.name, tv_channel_last_real))
869 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
870 tvh->channel = i;
871 mp_msg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
872 cl.name, (float)cl.freq/1000);
873 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
874 break;
878 return(1);
881 int tv_step_norm(tvi_handle_t *tvh)
883 tvh->norm++;
884 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
885 &tvh->norm) != TVI_CONTROL_TRUE) {
886 tvh->norm = 0;
887 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
888 &tvh->norm) != TVI_CONTROL_TRUE) {
889 mp_msg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
890 return 0;
893 return(1);
896 int tv_step_chanlist(tvi_handle_t *tvh)
898 return(1);
901 int tv_set_norm(tvi_handle_t *tvh, char* norm)
903 tvh->norm = norm_from_string(tvh, norm);
905 mp_msg(MSGT_TV, MSGL_V, "Selected norm: %s\n", tv_param_norm);
906 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
907 mp_msg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
908 return 0;
910 return(1);
913 demuxer_desc_t demuxer_desc_tv = {
914 "Tv card demuxer",
915 "tv",
916 "TV",
917 "Alex Beregszaszi, Charles R. Henrich",
918 "?",
919 DEMUXER_TYPE_TV,
920 0, // no autodetect
921 NULL,
922 demux_tv_fill_buffer,
923 demux_open_tv,
924 demux_close_tv,
925 NULL,
926 NULL