Merge remote branch 'mplayer/master'
[mplayer/glamo.git] / stream / tv.c
blob85a79267b6de9fa7086b5188d75a88fa7402495e
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"
23 #include "mp_msg.h"
24 #include "help_mp.h"
26 #include "stream.h"
27 #include "libmpdemux/demuxer.h"
28 #include "libmpdemux/stheader.h"
30 #include "libaf/af_format.h"
31 #include "libmpcodecs/img_format.h"
32 #include "libmpcodecs/dec_teletext.h"
33 #include "libavutil/avstring.h"
34 #include "osdep/timer.h"
36 #include "tv.h"
38 #include "frequencies.h"
40 tv_channels_t *tv_channel_list;
41 tv_channels_t *tv_channel_current, *tv_channel_last;
42 char *tv_channel_last_real;
44 /* enumerating drivers (like in stream.c) */
45 extern const tvi_info_t tvi_info_dummy;
46 extern const tvi_info_t tvi_info_dshow;
47 extern const tvi_info_t tvi_info_v4l;
48 extern const tvi_info_t tvi_info_v4l2;
49 extern const tvi_info_t tvi_info_bsdbt848;
51 /** List of drivers in autodetection order */
52 static const tvi_info_t* tvi_driver_list[]={
53 #ifdef CONFIG_TV_V4L2
54 &tvi_info_v4l2,
55 #endif
56 #ifdef CONFIG_TV_V4L1
57 &tvi_info_v4l,
58 #endif
59 #ifdef CONFIG_TV_BSDBT848
60 &tvi_info_bsdbt848,
61 #endif
62 #ifdef CONFIG_TV_DSHOW
63 &tvi_info_dshow,
64 #endif
65 &tvi_info_dummy,
66 NULL
69 void tv_start_scan(tvi_handle_t *tvh, int start)
71 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
72 tvh->tv_param->scan=start?1:0;
75 static void tv_scan(tvi_handle_t *tvh)
77 unsigned int now;
78 struct CHANLIST cl;
79 tv_channels_t *tv_channel_tmp=NULL;
80 tv_channels_t *tv_channel_add=NULL;
81 tv_scan_t* scan;
82 int found=0, index=1;
84 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
85 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
87 mp_tmsg(MSGT_TV, MSGL_WARN, "Channel scanner is not available without tuner\n");
88 tvh->tv_param->scan=0;
89 return;
92 scan = tvh->scan;
93 now=GetTimer();
94 if (!scan) {
95 scan=calloc(1,sizeof(tv_scan_t));
96 tvh->scan=scan;
97 cl = tvh->chanlist_s[scan->channel_num];
98 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
99 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
101 if(scan->scan_timer>now)
102 return;
104 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
105 cl = tvh->chanlist_s[scan->channel_num];
106 tv_channel_tmp=tv_channel_list;
107 while (tv_channel_tmp) {
108 index++;
109 if (cl.freq==tv_channel_tmp->freq){
110 found=1;
111 break;
113 tv_channel_add=tv_channel_tmp;
114 tv_channel_tmp=tv_channel_tmp->next;
116 if (!found) {
117 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
118 scan->new_channels++;
119 tv_channel_tmp = malloc(sizeof(tv_channels_t));
120 tv_channel_tmp->index=index;
121 tv_channel_tmp->next=NULL;
122 tv_channel_tmp->prev=tv_channel_add;
123 tv_channel_tmp->freq=cl.freq;
124 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
125 strncpy(tv_channel_tmp->number, cl.name, 5);
126 tv_channel_tmp->number[4]='\0';
127 if (!tv_channel_list)
128 tv_channel_list=tv_channel_tmp;
129 else {
130 tv_channel_add->next=tv_channel_tmp;
131 tv_channel_list->prev=tv_channel_tmp;
133 }else
134 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
135 tv_channel_tmp->number,tv_channel_tmp->name);
137 scan->channel_num++;
138 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
139 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
140 tvh->tv_param->scan=0;
141 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
142 tv_channel_tmp=tv_channel_list;
143 if(tv_channel_tmp){
144 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
145 while(tv_channel_tmp){
146 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
147 if(tv_channel_tmp->next)
148 mp_msg(MSGT_TV,MSGL_INFO,",");
149 tv_channel_tmp=tv_channel_tmp->next;
151 mp_msg(MSGT_TV, MSGL_INFO, "\n");
153 if (!tv_channel_current) tv_channel_current=tv_channel_list;
154 if (tv_channel_current)
155 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
156 free(tvh->scan);
157 tvh->scan=NULL;
158 }else{
159 cl = tvh->chanlist_s[scan->channel_num];
160 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
161 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
165 /* ================== DEMUX_TV ===================== */
167 Return value:
168 0 = EOF(?) or no stream
169 1 = successfully read a packet
171 /* fill demux->video and demux->audio */
173 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
175 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
176 demux_packet_t* dp;
177 unsigned int len=0;
179 /* ================== ADD AUDIO PACKET =================== */
181 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
182 tvh->functions->control(tvh->priv,
183 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
185 len = tvh->functions->get_audio_framesize(tvh->priv);
187 dp=new_demux_packet(len);
188 dp->flags|=1; /* Keyframe */
189 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
190 ds_add_packet(demux->audio,dp);
193 /* ================== ADD VIDEO PACKET =================== */
195 if (ds==demux->video && tvh->functions->control(tvh->priv,
196 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
198 len = tvh->functions->get_video_framesize(tvh->priv);
199 dp=new_demux_packet(len);
200 dp->flags|=1; /* Keyframe */
201 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
202 ds_add_packet(demux->video,dp);
205 if (tvh->tv_param->scan) tv_scan(tvh);
206 return 1;
209 static int norm_from_string(tvi_handle_t *tvh, char* norm)
211 const tvi_functions_t *funcs = tvh->functions;
212 char str[20];
213 int ret;
215 strncpy(str, norm, sizeof(str)-1);
216 str[sizeof(str)-1] = '\0';
217 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
219 if(ret==TVI_CONTROL_TRUE)
220 return *(int *)str;
222 if(ret!=TVI_CONTROL_UNKNOWN)
224 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm,"default");
225 return 0;
228 if (!strcasecmp(norm, "pal"))
229 return TV_NORM_PAL;
230 else if (!strcasecmp(norm, "ntsc"))
231 return TV_NORM_NTSC;
232 else if (!strcasecmp(norm, "secam"))
233 return TV_NORM_SECAM;
234 else if (!strcasecmp(norm, "palnc"))
235 return TV_NORM_PALNC;
236 else if (!strcasecmp(norm, "palm"))
237 return TV_NORM_PALM;
238 else if (!strcasecmp(norm, "paln"))
239 return TV_NORM_PALN;
240 else if (!strcasecmp(norm, "ntscjp"))
241 return TV_NORM_NTSCJP;
242 else {
243 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
244 return TV_NORM_PAL;
248 static void parse_channels(tvi_handle_t *tvh)
250 char** channels = tvh->tv_param->channels;
252 mp_tmsg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
253 tv_channel_list = malloc(sizeof(tv_channels_t));
254 tv_channel_list->index=1;
255 tv_channel_list->next=NULL;
256 tv_channel_list->prev=NULL;
257 tv_channel_current = tv_channel_list;
258 tv_channel_current->norm = tvh->norm;
260 while (*channels) {
261 char* tmp = *(channels++);
262 char* sep = strchr(tmp,'-');
263 int i;
264 struct CHANLIST cl;
266 if (!sep) continue; // Wrong syntax, but mplayer should not crash
268 av_strlcpy(tv_channel_current->name, sep + 1,
269 sizeof(tv_channel_current->name));
270 sep[0] = '\0';
271 strncpy(tv_channel_current->number, tmp, 5);
272 tv_channel_current->number[4]='\0';
274 while ((sep=strchr(tv_channel_current->name, '_')))
275 sep[0] = ' ';
277 // if channel number is a number and larger than 1000 threat it as frequency
278 // tmp still contain pointer to null-terminated string with channel number here
279 if (atoi(tmp)>1000){
280 tv_channel_current->freq=atoi(tmp);
281 }else{
282 tv_channel_current->freq = 0;
283 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
284 cl = tvh->chanlist_s[i];
285 if (!strcasecmp(cl.name, tv_channel_current->number)) {
286 tv_channel_current->freq=cl.freq;
287 break;
291 if (tv_channel_current->freq == 0)
292 mp_tmsg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
293 tv_channel_current->number, tv_channel_current->name);
294 else {
295 sep = strchr(tv_channel_current->name, '-');
296 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
298 if ( sep ) {
299 i = atoi (sep+1);
300 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
301 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
302 sep[0] = '\0';
305 sep = strchr(tv_channel_current->name, '=');
306 if ( sep ) {
307 tv_channel_current->norm = norm_from_string(tvh, sep+1);
308 sep[0] = '\0';
312 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
313 tv_channel_current->number, tv_channel_current->name,
314 (float)tv_channel_current->freq/1000);*/
316 tv_channel_current->next = malloc(sizeof(tv_channels_t));
317 tv_channel_current->next->index = tv_channel_current->index + 1;
318 tv_channel_current->next->prev = tv_channel_current;
319 tv_channel_current->next->next = NULL;
320 tv_channel_current = tv_channel_current->next;
321 tv_channel_current->norm = tvh->norm;
323 if (tv_channel_current->prev)
324 tv_channel_current->prev->next = NULL;
325 free(tv_channel_current);
328 int tv_set_norm(tvi_handle_t *tvh, char* norm)
330 tvh->norm = norm_from_string(tvh, norm);
332 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm : %s\n", norm);
333 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
334 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
335 return 0;
337 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
338 &tvh->tv_param->teletext);
339 return 1;
342 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
344 tvh->norm = norm;
346 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", norm);
347 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
348 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
349 return 0;
352 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
353 &tvh->tv_param->teletext);
354 return 1;
357 static int open_tv(tvi_handle_t *tvh)
359 int i;
360 const tvi_functions_t *funcs = tvh->functions;
361 int tv_fmt_list[] = {
362 IMGFMT_YV12,
363 IMGFMT_I420,
364 IMGFMT_UYVY,
365 IMGFMT_YUY2,
366 IMGFMT_RGB32,
367 IMGFMT_RGB24,
368 IMGFMT_RGB16,
369 IMGFMT_RGB15
372 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
374 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
375 return 0;
378 if (tvh->tv_param->outfmt == -1)
379 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
381 tvh->tv_param->outfmt = tv_fmt_list[i];
382 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
383 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
384 break;
386 else
388 switch(tvh->tv_param->outfmt)
390 case IMGFMT_YV12:
391 case IMGFMT_I420:
392 case IMGFMT_UYVY:
393 case IMGFMT_YUY2:
394 case IMGFMT_RGB32:
395 case IMGFMT_RGB24:
396 case IMGFMT_BGR32:
397 case IMGFMT_BGR24:
398 case IMGFMT_BGR16:
399 case IMGFMT_BGR15:
400 break;
401 default:
402 mp_tmsg(MSGT_TV, MSGL_ERR,
403 "==================================================================\n"\
404 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
405 " This may cause buggy playback or program crash! Bug reports will\n"\
406 " be ignored! You should try again with YV12 (which is the default\n"\
407 " colorspace) and read the documentation!\n"\
408 "==================================================================\n"
409 ,tvh->tv_param->outfmt);
411 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
414 /* set some params got from cmdline */
415 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
417 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
418 if (0
419 #ifdef CONFIG_TV_V4L2
420 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
421 #endif
422 #ifdef CONFIG_TV_DSHOW
423 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
424 #endif
426 tv_set_norm_i(tvh, tvh->tv_param->normid);
427 else
428 #endif
429 tv_set_norm(tvh,tvh->tv_param->norm);
431 #ifdef CONFIG_TV_V4L1
432 if ( tvh->tv_param->mjpeg )
434 /* set width to expected value */
435 if (tvh->tv_param->width == -1)
437 tvh->tv_param->width = 704/tvh->tv_param->decimation;
439 if (tvh->tv_param->height == -1)
441 if ( tvh->norm != TV_NORM_NTSC )
442 tvh->tv_param->height = 576/tvh->tv_param->decimation;
443 else
444 tvh->tv_param->height = 480/tvh->tv_param->decimation;
446 mp_tmsg(MSGT_TV, MSGL_INFO,
447 " MJP: width %d height %d\n", tvh->tv_param->width, tvh->tv_param->height);
449 #endif
451 /* limits on w&h are norm-dependent -- JM */
452 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
453 // first tell the driver both width and height, some drivers do not support setting them independently.
454 int dim[2];
455 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
456 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
458 /* set width */
459 if (tvh->tv_param->width != -1)
461 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
462 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
463 else
465 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tvh->tv_param->width);
466 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
470 /* set height */
471 if (tvh->tv_param->height != -1)
473 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
474 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
475 else
477 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tvh->tv_param->height);
478 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
482 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
484 mp_tmsg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
485 goto done;
488 /* select channel list */
489 for (i = 0; chanlists[i].name != NULL; i++)
491 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
493 tvh->chanlist = i;
494 tvh->chanlist_s = chanlists[i].list;
495 break;
499 if (tvh->chanlist == -1)
500 mp_tmsg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
501 tvh->tv_param->chanlist);
502 else
503 mp_tmsg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
504 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
506 if (tvh->tv_param->freq && tvh->tv_param->channel)
508 mp_tmsg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
509 goto done;
512 /* Handle channel names */
513 if (tvh->tv_param->channels) {
514 parse_channels(tvh);
515 } else
516 tv_channel_last_real = malloc(5);
518 if (tv_channel_list) {
519 int i;
520 int channel = 0;
521 if (tvh->tv_param->channel)
523 if (isdigit(*tvh->tv_param->channel))
524 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
525 channel = atoi(tvh->tv_param->channel);
526 else
528 /* if tvh->tv_param->channel does not begin with a digit
529 set the first channel that contains tvh->tv_param->channel in its name */
531 tv_channel_current = tv_channel_list;
532 while ( tv_channel_current ) {
533 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
534 break;
535 tv_channel_current = tv_channel_current->next;
537 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
540 else
541 channel = 1;
543 if ( channel ) {
544 tv_channel_current = tv_channel_list;
545 for (i = 1; i < channel; i++)
546 if (tv_channel_current->next)
547 tv_channel_current = tv_channel_current->next;
550 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
551 tv_channel_current->name, (float)tv_channel_current->freq/1000);
552 tv_set_norm_i(tvh, tv_channel_current->norm);
553 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
554 tv_channel_last = tv_channel_current;
555 } else {
556 /* we need to set frequency */
557 if (tvh->tv_param->freq)
559 unsigned long freq = atof(tvh->tv_param->freq)*16;
561 /* set freq in MHz */
562 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
564 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
565 mp_tmsg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
566 freq, (float)freq/16);
569 if (tvh->tv_param->channel) {
570 struct CHANLIST cl;
572 mp_tmsg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tvh->tv_param->channel);
573 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
575 cl = tvh->chanlist_s[i];
576 // printf("count%d: name: %s, freq: %d\n",
577 // i, cl.name, cl.freq);
578 if (!strcasecmp(cl.name, tvh->tv_param->channel))
580 strcpy(tv_channel_last_real, cl.name);
581 tvh->channel = i;
582 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
583 cl.name, (float)cl.freq/1000);
584 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
585 break;
591 /* grep frequency in chanlist */
593 unsigned long i2;
594 int freq;
596 tv_get_freq(tvh, &i2);
598 freq = (int) (((float)(i2/16))*1000)+250;
600 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
602 if (tvh->chanlist_s[i].freq == freq)
604 tvh->channel = i+1;
605 break;
610 done:
611 /* also start device! */
612 return 1;
615 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
617 int i;
618 tvi_handle_t* h;
619 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
620 mp_tmsg(MSGT_TV,MSGL_INFO,"Available drivers:\n");
621 for(i=0;tvi_driver_list[i];i++){
622 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
623 if(tvi_driver_list[i]->comment)
624 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
625 mp_msg(MSGT_TV,MSGL_INFO,"\n");
627 return NULL;
630 for(i=0;tvi_driver_list[i];i++){
631 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
632 h=tvi_driver_list[i]->tvi_init(tv_param);
633 //Requested driver initialization failed
634 if (!h && tv_param->driver)
635 return NULL;
636 //Driver initialization failed during autodetection process.
637 if (!h)
638 continue;
640 h->tv_param=tv_param;
641 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected driver: %s\n name: %s\n author: %s\n comment: %s\n", tvi_driver_list[i]->short_name,
642 tvi_driver_list[i]->name,
643 tvi_driver_list[i]->author,
644 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
645 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
646 return h;
650 if(tv_param->driver)
651 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
652 else
653 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
654 return NULL;
657 static int tv_uninit(tvi_handle_t *tvh)
659 int res;
660 if(!tvh) return 1;
661 if (!tvh->priv) return 1;
662 res=tvh->functions->uninit(tvh->priv);
663 if(res) {
664 free(tvh->priv);
665 tvh->priv=NULL;
667 return res;
670 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
672 tvi_handle_t *tvh;
673 sh_video_t *sh_video;
674 sh_audio_t *sh_audio = NULL;
675 const tvi_functions_t *funcs;
677 demuxer->priv=NULL;
678 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
679 if (!tvh->functions->init(tvh->priv)) return NULL;
681 tvh->demuxer = demuxer;
682 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
683 &(tvh->tv_param->teletext.device));
684 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
685 &demuxer->teletext);
687 if (!open_tv(tvh)){
688 tv_uninit(tvh);
689 return NULL;
691 funcs = tvh->functions;
692 demuxer->priv=tvh;
694 sh_video = new_sh_video(demuxer, 0);
696 /* get IMAGE FORMAT */
697 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
698 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
699 // sh_video->format = 0x0;
701 /* set FPS and FRAMETIME */
703 if(!sh_video->fps)
705 float tmp;
706 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
707 sh_video->fps = 25.0f; /* on PAL */
708 else sh_video->fps = tmp;
711 if (tvh->tv_param->fps != -1.0f)
712 sh_video->fps = tvh->tv_param->fps;
714 sh_video->frametime = 1.0f/sh_video->fps;
716 /* If playback only mode, go to immediate mode, fail silently */
717 if(tvh->tv_param->immediate == 1)
719 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
720 tvh->tv_param->noaudio = 1;
723 /* disable TV audio if -nosound is present */
724 if (!demuxer->audio || demuxer->audio->id == -2) {
725 tvh->tv_param->noaudio = 1;
728 /* set width */
729 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
731 /* set height */
732 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
734 demuxer->video->sh = sh_video;
735 sh_video->ds = demuxer->video;
736 demuxer->video->id = 0;
737 demuxer->seekable = 0;
739 /* here comes audio init */
740 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
742 int audio_format;
743 int sh_audio_format;
744 char buf[128];
746 /* yeah, audio is present */
748 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
749 &tvh->tv_param->audiorate);
751 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
752 goto no_audio;
754 switch(audio_format)
756 case AF_FORMAT_U8:
757 case AF_FORMAT_S8:
758 case AF_FORMAT_U16_LE:
759 case AF_FORMAT_U16_BE:
760 case AF_FORMAT_S16_LE:
761 case AF_FORMAT_S16_BE:
762 case AF_FORMAT_S32_LE:
763 case AF_FORMAT_S32_BE:
764 sh_audio_format = 0x1; /* PCM */
765 break;
766 case AF_FORMAT_IMA_ADPCM:
767 case AF_FORMAT_MU_LAW:
768 case AF_FORMAT_A_LAW:
769 case AF_FORMAT_MPEG2:
770 case AF_FORMAT_AC3:
771 default:
772 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
773 af_fmt2str(audio_format, buf, 128), audio_format);
774 goto no_audio;
777 sh_audio = new_sh_audio(demuxer, 0);
779 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
780 &sh_audio->samplerate);
781 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
782 &sh_audio->samplesize);
783 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
784 &sh_audio->channels);
786 sh_audio->format = sh_audio_format;
787 sh_audio->sample_format = audio_format;
789 sh_audio->i_bps = sh_audio->o_bps =
790 sh_audio->samplerate * sh_audio->samplesize *
791 sh_audio->channels;
793 // emulate WF for win32 codecs:
794 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
795 sh_audio->wf->wFormatTag = sh_audio->format;
796 sh_audio->wf->nChannels = sh_audio->channels;
797 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
798 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
799 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
800 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
802 mp_tmsg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
803 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
804 sh_audio->wf->nSamplesPerSec);
806 demuxer->audio->sh = sh_audio;
807 sh_audio->ds = demuxer->audio;
808 demuxer->audio->id = 0;
810 no_audio:
812 if(!(funcs->start(tvh->priv))){
813 // start failed :(
814 tv_uninit(tvh);
815 return NULL;
818 /* set color eq */
819 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
820 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
821 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
822 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
824 if(tvh->tv_param->gain!=-1)
825 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
826 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
828 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
829 &tvh->tv_param->teletext);
831 return demuxer;
834 static void demux_close_tv(demuxer_t *demuxer)
836 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
837 if (!tvh) return;
838 tv_uninit(tvh);
839 free(tvh);
840 demuxer->priv=NULL;
841 demuxer->teletext=NULL;
844 /* utilities for mplayer (not mencoder!!) */
845 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
847 const tvi_functions_t *funcs = tvh->functions;
849 switch(opt)
851 case TV_COLOR_BRIGHTNESS:
852 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
853 case TV_COLOR_HUE:
854 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
855 case TV_COLOR_SATURATION:
856 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
857 case TV_COLOR_CONTRAST:
858 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
859 default:
860 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
863 return TVI_CONTROL_UNKNOWN;
866 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
868 const tvi_functions_t *funcs = tvh->functions;
870 switch(opt)
872 case TV_COLOR_BRIGHTNESS:
873 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
874 case TV_COLOR_HUE:
875 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
876 case TV_COLOR_SATURATION:
877 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
878 case TV_COLOR_CONTRAST:
879 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
880 default:
881 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
884 return TVI_CONTROL_UNKNOWN;
887 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
889 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
891 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
892 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
893 *freq, (float)*freq/16);
895 return 1;
898 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
900 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
902 // unsigned long freq = atof(tvh->tv_param->freq)*16;
904 /* set freq in MHz */
905 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
907 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
908 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
909 freq, (float)freq/16);
911 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
912 &tvh->tv_param->teletext);
913 return 1;
916 int tv_get_signal(tvi_handle_t *tvh)
918 int signal=0;
919 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
920 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
921 return 0;
923 return signal;
926 /*****************************************************************
927 * \brief tune current frequency by step_interval value
928 * \parameter step_interval increment value in 1/16 MHz
929 * \note frequency is rounded to 1/16 MHz value
930 * \return 1
933 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
934 unsigned long frequency;
936 tvh->tv_param->scan=0;
937 tv_get_freq(tvh,&frequency);
938 frequency+=step_interval;
939 return tv_set_freq(tvh,frequency);
942 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
944 struct CHANLIST cl;
946 tvh->tv_param->scan=0;
947 if (direction == TV_CHANNEL_LOWER)
949 if (tvh->channel-1 >= 0)
951 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
952 cl = tvh->chanlist_s[--tvh->channel];
953 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
954 cl.name, (float)cl.freq/1000);
955 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
959 if (direction == TV_CHANNEL_HIGHER)
961 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
963 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
964 cl = tvh->chanlist_s[++tvh->channel];
965 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
966 cl.name, (float)cl.freq/1000);
967 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
970 return 1;
973 int tv_step_channel(tvi_handle_t *tvh, int direction) {
974 tvh->tv_param->scan=0;
975 if (tv_channel_list) {
976 if (direction == TV_CHANNEL_HIGHER) {
977 tv_channel_last = tv_channel_current;
978 if (tv_channel_current->next)
979 tv_channel_current = tv_channel_current->next;
980 else
981 tv_channel_current = tv_channel_list;
983 tv_set_norm_i(tvh, tv_channel_current->norm);
984 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
985 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
986 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
988 if (direction == TV_CHANNEL_LOWER) {
989 tv_channel_last = tv_channel_current;
990 if (tv_channel_current->prev)
991 tv_channel_current = tv_channel_current->prev;
992 else
993 while (tv_channel_current->next)
994 tv_channel_current = tv_channel_current->next;
995 tv_set_norm_i(tvh, tv_channel_current->norm);
996 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
997 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
998 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1000 } else tv_step_channel_real(tvh, direction);
1001 return 1;
1004 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1005 int i;
1006 struct CHANLIST cl;
1008 tvh->tv_param->scan=0;
1009 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1010 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1012 cl = tvh->chanlist_s[i];
1013 // printf("count%d: name: %s, freq: %d\n",
1014 // i, cl.name, cl.freq);
1015 if (!strcasecmp(cl.name, channel))
1017 tvh->channel = i;
1018 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1019 cl.name, (float)cl.freq/1000);
1020 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1021 break;
1024 return 1;
1027 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1028 int i, channel_int;
1030 tvh->tv_param->scan=0;
1031 if (tv_channel_list) {
1032 tv_channel_last = tv_channel_current;
1033 channel_int = atoi(channel);
1034 tv_channel_current = tv_channel_list;
1035 for (i = 1; i < channel_int; i++)
1036 if (tv_channel_current->next)
1037 tv_channel_current = tv_channel_current->next;
1038 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1039 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1040 tv_set_norm_i(tvh, tv_channel_current->norm);
1041 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1042 } else tv_set_channel_real(tvh, channel);
1043 return 1;
1046 int tv_last_channel(tvi_handle_t *tvh) {
1048 tvh->tv_param->scan=0;
1049 if (tv_channel_list) {
1050 tv_channels_t *tmp;
1052 tmp = tv_channel_last;
1053 tv_channel_last = tv_channel_current;
1054 tv_channel_current = tmp;
1056 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1057 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1058 tv_set_norm_i(tvh, tv_channel_current->norm);
1059 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1060 } else {
1061 int i;
1062 struct CHANLIST cl;
1064 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1066 cl = tvh->chanlist_s[i];
1067 if (!strcasecmp(cl.name, tv_channel_last_real))
1069 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1070 tvh->channel = i;
1071 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1072 cl.name, (float)cl.freq/1000);
1073 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1074 break;
1078 return 1;
1081 int tv_step_norm(tvi_handle_t *tvh)
1083 tvh->norm++;
1084 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1085 &tvh->norm) != TVI_CONTROL_TRUE) {
1086 tvh->norm = 0;
1087 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1088 &tvh->norm) != TVI_CONTROL_TRUE) {
1089 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
1090 return 0;
1093 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1094 &tvh->tv_param->teletext);
1095 return 1;
1098 int tv_step_chanlist(tvi_handle_t *tvh)
1100 return 1;
1103 demuxer_desc_t demuxer_desc_tv = {
1104 "Tv card demuxer",
1105 "tv",
1106 "TV",
1107 "Alex Beregszaszi, Charles R. Henrich",
1108 "?",
1109 DEMUXER_TYPE_TV,
1110 0, // no autodetect
1111 NULL,
1112 demux_tv_fill_buffer,
1113 demux_open_tv,
1114 demux_close_tv,
1115 NULL,
1116 NULL