Get rid of nonsensical limits on -geometry x, y,w and h values, they only
[mplayer/glamo.git] / stream / tv.c
blob7e8ae530d46feff0a0a13c55aeb20e9b4e791f11
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 "libavutil/avstring.h"
33 #include "osdep/timer.h"
35 #include "tv.h"
37 #include "frequencies.h"
39 tv_channels_t *tv_channel_list;
40 tv_channels_t *tv_channel_current, *tv_channel_last;
41 char *tv_channel_last_real;
43 /* enumerating drivers (like in stream.c) */
44 extern const tvi_info_t tvi_info_dummy;
45 extern const tvi_info_t tvi_info_dshow;
46 extern const tvi_info_t tvi_info_v4l;
47 extern const tvi_info_t tvi_info_v4l2;
48 extern const tvi_info_t tvi_info_bsdbt848;
50 /** List of drivers in autodetection order */
51 static const tvi_info_t* tvi_driver_list[]={
52 #ifdef CONFIG_TV_V4L2
53 &tvi_info_v4l2,
54 #endif
55 #ifdef CONFIG_TV_V4L1
56 &tvi_info_v4l,
57 #endif
58 #ifdef CONFIG_TV_BSDBT848
59 &tvi_info_bsdbt848,
60 #endif
61 #ifdef CONFIG_TV_DSHOW
62 &tvi_info_dshow,
63 #endif
64 &tvi_info_dummy,
65 NULL
68 void tv_start_scan(tvi_handle_t *tvh, int start)
70 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
71 tvh->tv_param->scan=start?1:0;
74 static void tv_scan(tvi_handle_t *tvh)
76 unsigned int now;
77 struct CHANLIST cl;
78 tv_channels_t *tv_channel_tmp=NULL;
79 tv_channels_t *tv_channel_add=NULL;
80 tv_scan_t* scan;
81 int found=0, index=1;
83 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
84 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
86 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ScannerNotAvailableWithoutTuner);
87 tvh->tv_param->scan=0;
88 return;
91 scan = tvh->scan;
92 now=GetTimer();
93 if (!scan) {
94 scan=calloc(1,sizeof(tv_scan_t));
95 tvh->scan=scan;
96 cl = tvh->chanlist_s[scan->channel_num];
97 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
98 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
100 if(scan->scan_timer>now)
101 return;
103 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
104 cl = tvh->chanlist_s[scan->channel_num];
105 tv_channel_tmp=tv_channel_list;
106 while (tv_channel_tmp) {
107 index++;
108 if (cl.freq==tv_channel_tmp->freq){
109 found=1;
110 break;
112 tv_channel_add=tv_channel_tmp;
113 tv_channel_tmp=tv_channel_tmp->next;
115 if (!found) {
116 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
117 scan->new_channels++;
118 tv_channel_tmp = malloc(sizeof(tv_channels_t));
119 tv_channel_tmp->index=index;
120 tv_channel_tmp->next=NULL;
121 tv_channel_tmp->prev=tv_channel_add;
122 tv_channel_tmp->freq=cl.freq;
123 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
124 strncpy(tv_channel_tmp->number, cl.name, 5);
125 tv_channel_tmp->number[4]='\0';
126 if (!tv_channel_list)
127 tv_channel_list=tv_channel_tmp;
128 else {
129 tv_channel_add->next=tv_channel_tmp;
130 tv_channel_list->prev=tv_channel_tmp;
132 }else
133 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
134 tv_channel_tmp->number,tv_channel_tmp->name);
136 scan->channel_num++;
137 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
138 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
139 tvh->tv_param->scan=0;
140 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
141 tv_channel_tmp=tv_channel_list;
142 if(tv_channel_tmp){
143 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
144 while(tv_channel_tmp){
145 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
146 if(tv_channel_tmp->next)
147 mp_msg(MSGT_TV,MSGL_INFO,",");
148 tv_channel_tmp=tv_channel_tmp->next;
150 mp_msg(MSGT_TV, MSGL_INFO, "\n");
152 if (!tv_channel_current) tv_channel_current=tv_channel_list;
153 if (tv_channel_current)
154 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
155 free(tvh->scan);
156 tvh->scan=NULL;
157 }else{
158 cl = tvh->chanlist_s[scan->channel_num];
159 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
160 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
164 /* ================== DEMUX_TV ===================== */
166 Return value:
167 0 = EOF(?) or no stream
168 1 = successfully read a packet
170 /* fill demux->video and demux->audio */
172 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
174 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
175 demux_packet_t* dp;
176 unsigned int len=0;
178 /* ================== ADD AUDIO PACKET =================== */
180 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
181 tvh->functions->control(tvh->priv,
182 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
184 len = tvh->functions->get_audio_framesize(tvh->priv);
186 dp=new_demux_packet(len);
187 dp->flags|=1; /* Keyframe */
188 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
189 ds_add_packet(demux->audio,dp);
192 /* ================== ADD VIDEO PACKET =================== */
194 if (ds==demux->video && tvh->functions->control(tvh->priv,
195 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
197 len = tvh->functions->get_video_framesize(tvh->priv);
198 dp=new_demux_packet(len);
199 dp->flags|=1; /* Keyframe */
200 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
201 ds_add_packet(demux->video,dp);
204 if (tvh->tv_param->scan) tv_scan(tvh);
205 return 1;
208 static int norm_from_string(tvi_handle_t *tvh, char* norm)
210 const tvi_functions_t *funcs = tvh->functions;
211 char str[20];
212 int ret;
214 strncpy(str, norm, sizeof(str)-1);
215 str[sizeof(str)-1] = '\0';
216 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
218 if(ret==TVI_CONTROL_TRUE)
219 return *(int *)str;
221 if(ret!=TVI_CONTROL_UNKNOWN)
223 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm,"default");
224 return 0;
227 if (!strcasecmp(norm, "pal"))
228 return TV_NORM_PAL;
229 else if (!strcasecmp(norm, "ntsc"))
230 return TV_NORM_NTSC;
231 else if (!strcasecmp(norm, "secam"))
232 return TV_NORM_SECAM;
233 else if (!strcasecmp(norm, "palnc"))
234 return TV_NORM_PALNC;
235 else if (!strcasecmp(norm, "palm"))
236 return TV_NORM_PALM;
237 else if (!strcasecmp(norm, "paln"))
238 return TV_NORM_PALN;
239 else if (!strcasecmp(norm, "ntscjp"))
240 return TV_NORM_NTSCJP;
241 else {
242 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm, "PAL");
243 return TV_NORM_PAL;
247 static void parse_channels(tvi_handle_t *tvh)
249 char** channels = tvh->tv_param->channels;
251 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_ChannelNamesDetected);
252 tv_channel_list = malloc(sizeof(tv_channels_t));
253 tv_channel_list->index=1;
254 tv_channel_list->next=NULL;
255 tv_channel_list->prev=NULL;
256 tv_channel_current = tv_channel_list;
257 tv_channel_current->norm = tvh->norm;
259 while (*channels) {
260 char* tmp = *(channels++);
261 char* sep = strchr(tmp,'-');
262 int i;
263 struct CHANLIST cl;
265 if (!sep) continue; // Wrong syntax, but mplayer should not crash
267 av_strlcpy(tv_channel_current->name, sep + 1,
268 sizeof(tv_channel_current->name));
269 sep[0] = '\0';
270 strncpy(tv_channel_current->number, tmp, 5);
271 tv_channel_current->number[4]='\0';
273 while ((sep=strchr(tv_channel_current->name, '_')))
274 sep[0] = ' ';
276 // if channel number is a number and larger than 1000 threat it as frequency
277 // tmp still contain pointer to null-terminated string with channel number here
278 if (atoi(tmp)>1000){
279 tv_channel_current->freq=atoi(tmp);
280 }else{
281 tv_channel_current->freq = 0;
282 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
283 cl = tvh->chanlist_s[i];
284 if (!strcasecmp(cl.name, tv_channel_current->number)) {
285 tv_channel_current->freq=cl.freq;
286 break;
290 if (tv_channel_current->freq == 0)
291 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoFreqForChannel,
292 tv_channel_current->number, tv_channel_current->name);
293 else {
294 sep = strchr(tv_channel_current->name, '-');
295 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
297 if ( sep ) {
298 i = atoi (sep+1);
299 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
300 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
301 sep[0] = '\0';
304 sep = strchr(tv_channel_current->name, '=');
305 if ( sep ) {
306 tv_channel_current->norm = norm_from_string(tvh, sep+1);
307 sep[0] = '\0';
311 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
312 tv_channel_current->number, tv_channel_current->name,
313 (float)tv_channel_current->freq/1000);*/
315 tv_channel_current->next = malloc(sizeof(tv_channels_t));
316 tv_channel_current->next->index = tv_channel_current->index + 1;
317 tv_channel_current->next->prev = tv_channel_current;
318 tv_channel_current->next->next = NULL;
319 tv_channel_current = tv_channel_current->next;
320 tv_channel_current->norm = tvh->norm;
322 if (tv_channel_current->prev)
323 tv_channel_current->prev->next = NULL;
324 free(tv_channel_current);
327 int tv_set_norm(tvi_handle_t *tvh, char* norm)
329 tvh->norm = norm_from_string(tvh, norm);
331 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNorm, norm);
332 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
333 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
334 return 0;
336 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
337 return 1;
340 int tv_set_norm_i(tvi_handle_t *tvh, int norm)
342 tvh->norm = norm;
344 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNormId, norm);
345 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
346 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
347 return 0;
350 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
351 return(1);
354 static int open_tv(tvi_handle_t *tvh)
356 int i;
357 const tvi_functions_t *funcs = tvh->functions;
358 int tv_fmt_list[] = {
359 IMGFMT_YV12,
360 IMGFMT_I420,
361 IMGFMT_UYVY,
362 IMGFMT_YUY2,
363 IMGFMT_RGB32,
364 IMGFMT_RGB24,
365 IMGFMT_RGB16,
366 IMGFMT_RGB15
369 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
371 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoVideoInputPresent);
372 return 0;
375 if (tvh->tv_param->outfmt == -1)
376 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
378 tvh->tv_param->outfmt = tv_fmt_list[i];
379 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
380 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
381 break;
383 else
385 switch(tvh->tv_param->outfmt)
387 case IMGFMT_YV12:
388 case IMGFMT_I420:
389 case IMGFMT_UYVY:
390 case IMGFMT_YUY2:
391 case IMGFMT_RGB32:
392 case IMGFMT_RGB24:
393 case IMGFMT_BGR32:
394 case IMGFMT_BGR24:
395 case IMGFMT_BGR16:
396 case IMGFMT_BGR15:
397 break;
398 default:
399 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnknownImageFormat,tvh->tv_param->outfmt);
401 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
404 /* set some params got from cmdline */
405 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
407 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
408 if (0
409 #ifdef CONFIG_TV_V4L2
410 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
411 #endif
412 #ifdef CONFIG_TV_DSHOW
413 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
414 #endif
416 tv_set_norm_i(tvh, tvh->tv_param->normid);
417 else
418 #endif
419 tv_set_norm(tvh,tvh->tv_param->norm);
421 #ifdef CONFIG_TV_V4L1
422 if ( tvh->tv_param->mjpeg )
424 /* set width to expected value */
425 if (tvh->tv_param->width == -1)
427 tvh->tv_param->width = 704/tvh->tv_param->decimation;
429 if (tvh->tv_param->height == -1)
431 if ( tvh->norm != TV_NORM_NTSC )
432 tvh->tv_param->height = 576/tvh->tv_param->decimation;
433 else
434 tvh->tv_param->height = 480/tvh->tv_param->decimation;
436 mp_msg(MSGT_TV, MSGL_INFO,
437 MSGTR_TV_MJP_WidthHeight, tvh->tv_param->width, tvh->tv_param->height);
439 #endif
441 /* limits on w&h are norm-dependent -- JM */
442 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
443 // first tell the driver both width and height, some drivers do not support setting them independently.
444 int dim[2];
445 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
446 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
448 /* set width */
449 if (tvh->tv_param->width != -1)
451 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
452 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
453 else
455 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetWidth, tvh->tv_param->width);
456 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
460 /* set height */
461 if (tvh->tv_param->height != -1)
463 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
464 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
465 else
467 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetHeight, tvh->tv_param->height);
468 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
472 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
474 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_NoTuner);
475 goto done;
478 /* select channel list */
479 for (i = 0; chanlists[i].name != NULL; i++)
481 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
483 tvh->chanlist = i;
484 tvh->chanlist_s = chanlists[i].list;
485 break;
489 if (tvh->chanlist == -1)
490 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnableFindChanlist,
491 tvh->tv_param->chanlist);
492 else
493 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedChanlist,
494 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
496 if (tvh->tv_param->freq && tvh->tv_param->channel)
498 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ChannelFreqParamConflict);
499 goto done;
502 /* Handle channel names */
503 if (tvh->tv_param->channels) {
504 parse_channels(tvh);
505 } else
506 tv_channel_last_real = malloc(5);
508 if (tv_channel_list) {
509 int i;
510 int channel = 0;
511 if (tvh->tv_param->channel)
513 if (isdigit(*tvh->tv_param->channel))
514 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
515 channel = atoi(tvh->tv_param->channel);
516 else
518 /* if tvh->tv_param->channel does not begin with a digit
519 set the first channel that contains tvh->tv_param->channel in its name */
521 tv_channel_current = tv_channel_list;
522 while ( tv_channel_current ) {
523 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
524 break;
525 tv_channel_current = tv_channel_current->next;
527 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
530 else
531 channel = 1;
533 if ( channel ) {
534 tv_channel_current = tv_channel_list;
535 for (i = 1; i < channel; i++)
536 if (tv_channel_current->next)
537 tv_channel_current = tv_channel_current->next;
540 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
541 tv_channel_current->name, (float)tv_channel_current->freq/1000);
542 tv_set_norm_i(tvh, tv_channel_current->norm);
543 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
544 tv_channel_last = tv_channel_current;
545 } else {
546 /* we need to set frequency */
547 if (tvh->tv_param->freq)
549 unsigned long freq = atof(tvh->tv_param->freq)*16;
551 /* set freq in MHz */
552 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
554 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
555 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedFrequency,
556 freq, (float)freq/16);
559 if (tvh->tv_param->channel) {
560 struct CHANLIST cl;
562 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_RequestedChannel, tvh->tv_param->channel);
563 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
565 cl = tvh->chanlist_s[i];
566 // printf("count%d: name: %s, freq: %d\n",
567 // i, cl.name, cl.freq);
568 if (!strcasecmp(cl.name, tvh->tv_param->channel))
570 strcpy(tv_channel_last_real, cl.name);
571 tvh->channel = i;
572 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
573 cl.name, (float)cl.freq/1000);
574 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
575 break;
581 /* grep frequency in chanlist */
583 unsigned long i2;
584 int freq;
586 tv_get_freq(tvh, &i2);
588 freq = (int) (((float)(i2/16))*1000)+250;
590 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
592 if (tvh->chanlist_s[i].freq == freq)
594 tvh->channel = i+1;
595 break;
600 done:
601 /* also start device! */
602 return 1;
605 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
607 int i;
608 tvi_handle_t* h;
609 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
610 mp_msg(MSGT_TV,MSGL_INFO,MSGTR_TV_AvailableDrivers);
611 for(i=0;tvi_driver_list[i];i++){
612 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
613 if(tvi_driver_list[i]->comment)
614 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
615 mp_msg(MSGT_TV,MSGL_INFO,"\n");
617 return NULL;
620 for(i=0;tvi_driver_list[i];i++){
621 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
622 h=tvi_driver_list[i]->tvi_init(tv_param);
623 //Requested driver initialization failed
624 if (!h && tv_param->driver)
625 return NULL;
626 //Driver initialization failed during autodetection process.
627 if (!h)
628 continue;
630 h->tv_param=tv_param;
631 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_DriverInfo, tvi_driver_list[i]->short_name,
632 tvi_driver_list[i]->name,
633 tvi_driver_list[i]->author,
634 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
635 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
636 return h;
640 if(tv_param->driver)
641 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoSuchDriver, tv_param->driver);
642 else
643 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_DriverAutoDetectionFailed);
644 return NULL;
647 static int tv_uninit(tvi_handle_t *tvh)
649 int res;
650 if(!tvh) return 1;
651 if (!tvh->priv) return 1;
652 res=tvh->functions->uninit(tvh->priv);
653 if(res) {
654 free(tvh->priv);
655 tvh->priv=NULL;
657 return res;
660 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
662 tvi_handle_t *tvh;
663 sh_video_t *sh_video;
664 sh_audio_t *sh_audio = NULL;
665 const tvi_functions_t *funcs;
667 demuxer->priv=NULL;
668 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
669 if (!tvh->functions->init(tvh->priv)) return NULL;
671 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,&(tvh->tv_param->tdevice));
673 if (!open_tv(tvh)){
674 tv_uninit(tvh);
675 return NULL;
677 funcs = tvh->functions;
678 demuxer->priv=tvh;
680 sh_video = new_sh_video(demuxer, 0);
682 /* get IMAGE FORMAT */
683 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
684 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
685 // sh_video->format = 0x0;
687 /* set FPS and FRAMETIME */
689 if(!sh_video->fps)
691 float tmp;
692 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
693 sh_video->fps = 25.0f; /* on PAL */
694 else sh_video->fps = tmp;
697 if (tvh->tv_param->fps != -1.0f)
698 sh_video->fps = tvh->tv_param->fps;
700 sh_video->frametime = 1.0f/sh_video->fps;
702 /* If playback only mode, go to immediate mode, fail silently */
703 if(tvh->tv_param->immediate == 1)
705 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
706 tvh->tv_param->noaudio = 1;
709 /* disable TV audio if -nosound is present */
710 if (!demuxer->audio || demuxer->audio->id == -2) {
711 tvh->tv_param->noaudio = 1;
714 /* set width */
715 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
717 /* set height */
718 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
720 demuxer->video->sh = sh_video;
721 sh_video->ds = demuxer->video;
722 demuxer->video->id = 0;
723 demuxer->seekable = 0;
725 /* here comes audio init */
726 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
728 int audio_format;
729 int sh_audio_format;
730 char buf[128];
732 /* yeah, audio is present */
734 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
735 &tvh->tv_param->audiorate);
737 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
738 goto no_audio;
740 switch(audio_format)
742 case AF_FORMAT_U8:
743 case AF_FORMAT_S8:
744 case AF_FORMAT_U16_LE:
745 case AF_FORMAT_U16_BE:
746 case AF_FORMAT_S16_LE:
747 case AF_FORMAT_S16_BE:
748 case AF_FORMAT_S32_LE:
749 case AF_FORMAT_S32_BE:
750 sh_audio_format = 0x1; /* PCM */
751 break;
752 case AF_FORMAT_IMA_ADPCM:
753 case AF_FORMAT_MU_LAW:
754 case AF_FORMAT_A_LAW:
755 case AF_FORMAT_MPEG2:
756 case AF_FORMAT_AC3:
757 default:
758 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnsupportedAudioType,
759 af_fmt2str(audio_format, buf, 128), audio_format);
760 goto no_audio;
763 sh_audio = new_sh_audio(demuxer, 0);
765 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
766 &sh_audio->samplerate);
767 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
768 &sh_audio->samplesize);
769 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
770 &sh_audio->channels);
772 sh_audio->format = sh_audio_format;
773 sh_audio->sample_format = audio_format;
775 sh_audio->i_bps = sh_audio->o_bps =
776 sh_audio->samplerate * sh_audio->samplesize *
777 sh_audio->channels;
779 // emulate WF for win32 codecs:
780 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
781 sh_audio->wf->wFormatTag = sh_audio->format;
782 sh_audio->wf->nChannels = sh_audio->channels;
783 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
784 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
785 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
786 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
788 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_TV_AudioFormat,
789 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
790 sh_audio->wf->nSamplesPerSec);
792 demuxer->audio->sh = sh_audio;
793 sh_audio->ds = demuxer->audio;
794 demuxer->audio->id = 0;
796 no_audio:
798 if(!(funcs->start(tvh->priv))){
799 // start failed :(
800 tv_uninit(tvh);
801 return NULL;
804 /* set color eq */
805 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
806 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
807 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
808 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
810 if(tvh->tv_param->gain!=-1)
811 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
812 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
814 funcs->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
816 return demuxer;
819 static void demux_close_tv(demuxer_t *demuxer)
821 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
822 if (!tvh) return;
823 tv_uninit(tvh);
824 free(tvh);
825 demuxer->priv=NULL;
828 /* utilities for mplayer (not mencoder!!) */
829 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
831 const tvi_functions_t *funcs = tvh->functions;
833 switch(opt)
835 case TV_COLOR_BRIGHTNESS:
836 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
837 case TV_COLOR_HUE:
838 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
839 case TV_COLOR_SATURATION:
840 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
841 case TV_COLOR_CONTRAST:
842 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
843 default:
844 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
847 return TVI_CONTROL_UNKNOWN;
850 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
852 const tvi_functions_t *funcs = tvh->functions;
854 switch(opt)
856 case TV_COLOR_BRIGHTNESS:
857 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
858 case TV_COLOR_HUE:
859 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
860 case TV_COLOR_SATURATION:
861 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
862 case TV_COLOR_CONTRAST:
863 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
864 default:
865 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
868 return TVI_CONTROL_UNKNOWN;
871 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
873 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
875 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
876 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
877 *freq, (float)*freq/16);
879 return 1;
882 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
884 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
886 // unsigned long freq = atof(tvh->tv_param->freq)*16;
888 /* set freq in MHz */
889 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
891 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
892 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
893 freq, (float)freq/16);
895 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
896 return 1;
899 int tv_get_signal(tvi_handle_t *tvh)
901 int signal=0;
902 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
903 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
904 return 0;
906 return signal;
909 /*****************************************************************
910 * \brief tune current frequency by step_interval value
911 * \parameter step_interval increment value in 1/16 MHz
912 * \note frequency is rounded to 1/16 MHz value
913 * \return 1
916 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
917 unsigned long frequency;
919 tvh->tv_param->scan=0;
920 tv_get_freq(tvh,&frequency);
921 frequency+=step_interval;
922 return tv_set_freq(tvh,frequency);
925 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
927 struct CHANLIST cl;
929 tvh->tv_param->scan=0;
930 if (direction == TV_CHANNEL_LOWER)
932 if (tvh->channel-1 >= 0)
934 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
935 cl = tvh->chanlist_s[--tvh->channel];
936 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
937 cl.name, (float)cl.freq/1000);
938 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
942 if (direction == TV_CHANNEL_HIGHER)
944 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
946 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
947 cl = tvh->chanlist_s[++tvh->channel];
948 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
949 cl.name, (float)cl.freq/1000);
950 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
953 return 1;
956 int tv_step_channel(tvi_handle_t *tvh, int direction) {
957 tvh->tv_param->scan=0;
958 if (tv_channel_list) {
959 if (direction == TV_CHANNEL_HIGHER) {
960 tv_channel_last = tv_channel_current;
961 if (tv_channel_current->next)
962 tv_channel_current = tv_channel_current->next;
963 else
964 tv_channel_current = tv_channel_list;
966 tv_set_norm_i(tvh, tv_channel_current->norm);
967 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
968 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
969 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
971 if (direction == TV_CHANNEL_LOWER) {
972 tv_channel_last = tv_channel_current;
973 if (tv_channel_current->prev)
974 tv_channel_current = tv_channel_current->prev;
975 else
976 while (tv_channel_current->next)
977 tv_channel_current = tv_channel_current->next;
978 tv_set_norm_i(tvh, tv_channel_current->norm);
979 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
980 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
981 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
983 } else tv_step_channel_real(tvh, direction);
984 return 1;
987 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
988 int i;
989 struct CHANLIST cl;
991 tvh->tv_param->scan=0;
992 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
993 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
995 cl = tvh->chanlist_s[i];
996 // printf("count%d: name: %s, freq: %d\n",
997 // i, cl.name, cl.freq);
998 if (!strcasecmp(cl.name, channel))
1000 tvh->channel = i;
1001 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1002 cl.name, (float)cl.freq/1000);
1003 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1004 break;
1007 return 1;
1010 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1011 int i, channel_int;
1013 tvh->tv_param->scan=0;
1014 if (tv_channel_list) {
1015 tv_channel_last = tv_channel_current;
1016 channel_int = atoi(channel);
1017 tv_channel_current = tv_channel_list;
1018 for (i = 1; i < channel_int; i++)
1019 if (tv_channel_current->next)
1020 tv_channel_current = tv_channel_current->next;
1021 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1022 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1023 tv_set_norm_i(tvh, tv_channel_current->norm);
1024 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1025 } else tv_set_channel_real(tvh, channel);
1026 return 1;
1029 int tv_last_channel(tvi_handle_t *tvh) {
1031 tvh->tv_param->scan=0;
1032 if (tv_channel_list) {
1033 tv_channels_t *tmp;
1035 tmp = tv_channel_last;
1036 tv_channel_last = tv_channel_current;
1037 tv_channel_current = tmp;
1039 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1040 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1041 tv_set_norm_i(tvh, tv_channel_current->norm);
1042 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1043 } else {
1044 int i;
1045 struct CHANLIST cl;
1047 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1049 cl = tvh->chanlist_s[i];
1050 if (!strcasecmp(cl.name, tv_channel_last_real))
1052 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1053 tvh->channel = i;
1054 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1055 cl.name, (float)cl.freq/1000);
1056 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1057 break;
1061 return 1;
1064 int tv_step_norm(tvi_handle_t *tvh)
1066 tvh->norm++;
1067 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1068 &tvh->norm) != TVI_CONTROL_TRUE) {
1069 tvh->norm = 0;
1070 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1071 &tvh->norm) != TVI_CONTROL_TRUE) {
1072 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1073 return 0;
1076 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
1077 return 1;
1080 int tv_step_chanlist(tvi_handle_t *tvh)
1082 return 1;
1085 demuxer_desc_t demuxer_desc_tv = {
1086 "Tv card demuxer",
1087 "tv",
1088 "TV",
1089 "Alex Beregszaszi, Charles R. Henrich",
1090 "?",
1091 DEMUXER_TYPE_TV,
1092 0, // no autodetect
1093 NULL,
1094 demux_tv_fill_buffer,
1095 demux_open_tv,
1096 demux_close_tv,
1097 NULL,
1098 NULL