Comment out the correct #endif directive.
[mplayer/greg.git] / stream / tv.c
blobd40b88b32f1733eb48503e3733d820bb22323404
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 tvi_info_t tvi_info_dummy;
45 #ifdef HAVE_TV_DSHOW
46 extern tvi_info_dshow;
47 #endif
48 #ifdef HAVE_TV_V4L1
49 extern tvi_info_t tvi_info_v4l;
50 #endif
51 #ifdef HAVE_TV_V4L2
52 extern tvi_info_t tvi_info_v4l2;
53 #endif
54 #ifdef HAVE_TV_BSDBT848
55 extern tvi_info_t tvi_info_bsdbt848;
56 #endif
58 /** List of drivers in autodetection order */
59 static const tvi_info_t* tvi_driver_list[]={
60 #ifdef HAVE_TV_V4L2
61 &tvi_info_v4l2,
62 #endif
63 #ifdef HAVE_TV_V4L1
64 &tvi_info_v4l,
65 #endif
66 #ifdef HAVE_TV_BSDBT848
67 &tvi_info_bsdbt848,
68 #endif
69 #ifdef HAVE_TV_DSHOW
70 &tvi_info_dshow,
71 #endif
72 &tvi_info_dummy,
73 NULL
76 void tv_start_scan(tvi_handle_t *tvh, int start)
78 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
79 tvh->tv_param->scan=start?1:0;
82 static void tv_scan(tvi_handle_t *tvh)
84 unsigned int now;
85 struct CHANLIST cl;
86 tv_channels_t *tv_channel_tmp=NULL;
87 tv_channels_t *tv_channel_add=NULL;
88 tv_scan_t* scan;
89 int found=0, index=1;
91 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
92 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
94 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ScannerNotAvailableWithoutTuner);
95 tvh->tv_param->scan=0;
96 return;
99 scan = tvh->scan;
100 now=GetTimer();
101 if (!scan) {
102 scan=calloc(1,sizeof(tv_scan_t));
103 tvh->scan=scan;
104 cl = tvh->chanlist_s[scan->channel_num];
105 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
106 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
108 if(scan->scan_timer>now)
109 return;
111 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
112 cl = tvh->chanlist_s[scan->channel_num];
113 tv_channel_tmp=tv_channel_list;
114 while (tv_channel_tmp) {
115 index++;
116 if (cl.freq==tv_channel_tmp->freq){
117 found=1;
118 break;
120 tv_channel_add=tv_channel_tmp;
121 tv_channel_tmp=tv_channel_tmp->next;
123 if (!found) {
124 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
125 scan->new_channels++;
126 tv_channel_tmp = malloc(sizeof(tv_channels_t));
127 tv_channel_tmp->index=index;
128 tv_channel_tmp->next=NULL;
129 tv_channel_tmp->prev=tv_channel_add;
130 tv_channel_tmp->freq=cl.freq;
131 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
132 strncpy(tv_channel_tmp->number, cl.name, 5);
133 tv_channel_tmp->number[4]='\0';
134 if (!tv_channel_list)
135 tv_channel_list=tv_channel_tmp;
136 else {
137 tv_channel_add->next=tv_channel_tmp;
138 tv_channel_list->prev=tv_channel_tmp;
140 }else
141 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
142 tv_channel_tmp->number,tv_channel_tmp->name);
144 scan->channel_num++;
145 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
146 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
147 tvh->tv_param->scan=0;
148 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
149 tv_channel_tmp=tv_channel_list;
150 if(tv_channel_tmp){
151 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
152 while(tv_channel_tmp){
153 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
154 if(tv_channel_tmp->next)
155 mp_msg(MSGT_TV,MSGL_INFO,",");
156 tv_channel_tmp=tv_channel_tmp->next;
158 mp_msg(MSGT_TV, MSGL_INFO, "\n");
160 if (!tv_channel_current) tv_channel_current=tv_channel_list;
161 if (tv_channel_current)
162 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
163 free(tvh->scan);
164 tvh->scan=NULL;
165 }else{
166 cl = tvh->chanlist_s[scan->channel_num];
167 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
168 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
172 /* ================== DEMUX_TV ===================== */
174 Return value:
175 0 = EOF(?) or no stream
176 1 = successfully read a packet
178 /* fill demux->video and demux->audio */
180 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
182 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
183 demux_packet_t* dp;
184 unsigned int len=0;
186 /* ================== ADD AUDIO PACKET =================== */
188 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
189 tvh->functions->control(tvh->priv,
190 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
192 len = tvh->functions->get_audio_framesize(tvh->priv);
194 dp=new_demux_packet(len);
195 dp->flags|=1; /* Keyframe */
196 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
197 ds_add_packet(demux->audio,dp);
200 /* ================== ADD VIDEO PACKET =================== */
202 if (ds==demux->video && tvh->functions->control(tvh->priv,
203 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
205 len = tvh->functions->get_video_framesize(tvh->priv);
206 dp=new_demux_packet(len);
207 dp->flags|=1; /* Keyframe */
208 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
209 ds_add_packet(demux->video,dp);
212 if (tvh->tv_param->scan) tv_scan(tvh);
213 return 1;
216 static int norm_from_string(tvi_handle_t *tvh, char* norm)
218 tvi_functions_t *funcs = tvh->functions;
219 char str[20];
220 int ret;
222 strncpy(str, norm, sizeof(str)-1);
223 str[sizeof(str)-1] = '\0';
224 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
226 if(ret==TVI_CONTROL_TRUE)
227 return *(int *)str;
229 if(ret!=TVI_CONTROL_UNKNOWN)
231 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm,"default");
232 return 0;
235 if (!strcasecmp(norm, "pal"))
236 return TV_NORM_PAL;
237 else if (!strcasecmp(norm, "ntsc"))
238 return TV_NORM_NTSC;
239 else if (!strcasecmp(norm, "secam"))
240 return TV_NORM_SECAM;
241 else if (!strcasecmp(norm, "palnc"))
242 return TV_NORM_PALNC;
243 else if (!strcasecmp(norm, "palm"))
244 return TV_NORM_PALM;
245 else if (!strcasecmp(norm, "paln"))
246 return TV_NORM_PALN;
247 else if (!strcasecmp(norm, "ntscjp"))
248 return TV_NORM_NTSCJP;
249 else {
250 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm, "PAL");
251 return TV_NORM_PAL;
255 static void parse_channels(tvi_handle_t *tvh)
257 char** channels = tvh->tv_param->channels;
259 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_ChannelNamesDetected);
260 tv_channel_list = malloc(sizeof(tv_channels_t));
261 tv_channel_list->index=1;
262 tv_channel_list->next=NULL;
263 tv_channel_list->prev=NULL;
264 tv_channel_current = tv_channel_list;
266 while (*channels) {
267 char* tmp = *(channels++);
268 char* sep = strchr(tmp,'-');
269 int i;
270 struct CHANLIST cl;
272 if (!sep) continue; // Wrong syntax, but mplayer should not crash
274 av_strlcpy(tv_channel_current->name, sep + 1,
275 sizeof(tv_channel_current->name));
276 sep[0] = '\0';
277 strncpy(tv_channel_current->number, tmp, 5);
278 tv_channel_current->number[4]='\0';
280 while ((sep=strchr(tv_channel_current->name, '_')))
281 sep[0] = ' ';
283 // if channel number is a number and larger than 1000 threat it as frequency
284 // tmp still contain pointer to null-terminated string with channel number here
285 if (atoi(tmp)>1000){
286 tv_channel_current->freq=atoi(tmp);
287 }else{
288 tv_channel_current->freq = 0;
289 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
290 cl = tvh->chanlist_s[i];
291 if (!strcasecmp(cl.name, tv_channel_current->number)) {
292 tv_channel_current->freq=cl.freq;
293 break;
297 if (tv_channel_current->freq == 0)
298 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoFreqForChannel,
299 tv_channel_current->number, tv_channel_current->name);
300 else {
301 sep = strchr(tv_channel_current->name, '-');
302 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
304 if ( sep ) {
305 i = atoi (sep+1);
306 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
307 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
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;
322 if (tv_channel_current->prev)
323 tv_channel_current->prev->next = NULL;
324 free(tv_channel_current);
326 static int open_tv(tvi_handle_t *tvh)
328 int i;
329 tvi_functions_t *funcs = tvh->functions;
330 int tv_fmt_list[] = {
331 IMGFMT_YV12,
332 IMGFMT_I420,
333 IMGFMT_UYVY,
334 IMGFMT_YUY2,
335 IMGFMT_RGB32,
336 IMGFMT_RGB24,
337 IMGFMT_RGB16,
338 IMGFMT_RGB15
341 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
343 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoVideoInputPresent);
344 return 0;
347 if (tvh->tv_param->outfmt == -1)
348 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
350 tvh->tv_param->outfmt = tv_fmt_list[i];
351 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
352 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
353 break;
355 else
357 switch(tvh->tv_param->outfmt)
359 case IMGFMT_YV12:
360 case IMGFMT_I420:
361 case IMGFMT_UYVY:
362 case IMGFMT_YUY2:
363 case IMGFMT_RGB32:
364 case IMGFMT_RGB24:
365 case IMGFMT_BGR32:
366 case IMGFMT_BGR24:
367 case IMGFMT_BGR16:
368 case IMGFMT_BGR15:
369 break;
370 default:
371 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnknownImageFormat,tvh->tv_param->outfmt);
373 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
376 /* set some params got from cmdline */
377 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
379 #if defined(HAVE_TV_V4L2) || defined(HAVE_TV_DSHOW)
380 if (0
381 #ifdef HAVE_TV_V4L2
382 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
383 #endif
384 #ifdef HAVE_TV_DSHOW
385 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
386 #endif
388 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNormId, tvh->tv_param->normid);
389 if (funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->tv_param->normid) != TVI_CONTROL_TRUE) {
390 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
392 } else
393 #endif
394 tv_set_norm(tvh,tvh->tv_param->norm);
396 #ifdef HAVE_TV_V4L1
397 if ( tvh->tv_param->mjpeg )
399 /* set width to expected value */
400 if (tvh->tv_param->width == -1)
402 tvh->tv_param->width = 704/tvh->tv_param->decimation;
404 if (tvh->tv_param->height == -1)
406 if ( tvh->norm != TV_NORM_NTSC )
407 tvh->tv_param->height = 576/tvh->tv_param->decimation;
408 else
409 tvh->tv_param->height = 480/tvh->tv_param->decimation;
411 mp_msg(MSGT_TV, MSGL_INFO,
412 MSGTR_TV_MJP_WidthHeight, tvh->tv_param->width, tvh->tv_param->height);
414 #endif
416 /* limits on w&h are norm-dependent -- JM */
417 /* set width */
418 if (tvh->tv_param->width != -1)
420 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
421 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
422 else
424 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetWidth, tvh->tv_param->width);
425 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
429 /* set height */
430 if (tvh->tv_param->height != -1)
432 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
433 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
434 else
436 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetHeight, tvh->tv_param->height);
437 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
441 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
443 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_NoTuner);
444 goto done;
447 /* select channel list */
448 for (i = 0; chanlists[i].name != NULL; i++)
450 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
452 tvh->chanlist = i;
453 tvh->chanlist_s = chanlists[i].list;
454 break;
458 if (tvh->chanlist == -1)
459 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnableFindChanlist,
460 tvh->tv_param->chanlist);
461 else
462 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedChanlist,
463 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
465 if (tvh->tv_param->freq && tvh->tv_param->channel)
467 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ChannelFreqParamConflict);
468 goto done;
471 /* Handle channel names */
472 if (tvh->tv_param->channels) {
473 parse_channels(tvh);
474 } else
475 tv_channel_last_real = malloc(5);
477 if (tv_channel_list) {
478 int i;
479 int channel = 0;
480 if (tvh->tv_param->channel)
482 if (isdigit(*tvh->tv_param->channel))
483 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
484 channel = atoi(tvh->tv_param->channel);
485 else
487 /* if tvh->tv_param->channel does not begin with a digit
488 set the first channel that contains tvh->tv_param->channel in its name */
490 tv_channel_current = tv_channel_list;
491 while ( tv_channel_current ) {
492 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
493 break;
494 tv_channel_current = tv_channel_current->next;
496 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
499 else
500 channel = 1;
502 if ( channel ) {
503 tv_channel_current = tv_channel_list;
504 for (i = 1; i < channel; i++)
505 if (tv_channel_current->next)
506 tv_channel_current = tv_channel_current->next;
509 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
510 tv_channel_current->name, (float)tv_channel_current->freq/1000);
511 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
512 tv_channel_last = tv_channel_current;
513 } else {
514 /* we need to set frequency */
515 if (tvh->tv_param->freq)
517 unsigned long freq = atof(tvh->tv_param->freq)*16;
519 /* set freq in MHz */
520 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
522 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
523 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedFrequency,
524 freq, (float)freq/16);
527 if (tvh->tv_param->channel) {
528 struct CHANLIST cl;
530 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_RequestedChannel, tvh->tv_param->channel);
531 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
533 cl = tvh->chanlist_s[i];
534 // printf("count%d: name: %s, freq: %d\n",
535 // i, cl.name, cl.freq);
536 if (!strcasecmp(cl.name, tvh->tv_param->channel))
538 strcpy(tv_channel_last_real, cl.name);
539 tvh->channel = i;
540 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
541 cl.name, (float)cl.freq/1000);
542 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
543 break;
549 /* grep frequency in chanlist */
551 unsigned long i2;
552 int freq;
554 tv_get_freq(tvh, &i2);
556 freq = (int) (((float)(i2/16))*1000)+250;
558 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
560 if (tvh->chanlist_s[i].freq == freq)
562 tvh->channel = i+1;
563 break;
568 done:
569 /* also start device! */
570 return 1;
573 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
575 int i;
576 tvi_handle_t* h;
577 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
578 mp_msg(MSGT_TV,MSGL_INFO,MSGTR_TV_AvailableDrivers);
579 for(i=0;tvi_driver_list[i];i++){
580 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
581 if(tvi_driver_list[i]->comment)
582 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
583 mp_msg(MSGT_TV,MSGL_INFO,"\n");
585 return NULL;
588 for(i=0;tvi_driver_list[i];i++){
589 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
590 h=tvi_driver_list[i]->tvi_init(tv_param);
591 //Requested driver initialization failed
592 if (!h && tv_param->driver)
593 return NULL;
594 //Driver initialization failed during autodetection process.
595 if (!h)
596 continue;
598 h->tv_param=tv_param;
599 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_DriverInfo, tvi_driver_list[i]->short_name,
600 tvi_driver_list[i]->name,
601 tvi_driver_list[i]->author,
602 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
603 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
604 return h;
608 if(tv_param->driver)
609 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoSuchDriver, tv_param->driver);
610 else
611 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_DriverAutoDetectionFailed);
612 return(NULL);
615 static int tv_uninit(tvi_handle_t *tvh)
617 int res;
618 if(!tvh) return 1;
619 if (!tvh->priv) return 1;
620 res=tvh->functions->uninit(tvh->priv);
621 if(res) {
622 free(tvh->priv);
623 tvh->priv=NULL;
625 return res;
628 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
630 tvi_handle_t *tvh;
631 sh_video_t *sh_video;
632 sh_audio_t *sh_audio = NULL;
633 tvi_functions_t *funcs;
635 demuxer->priv=NULL;
636 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
637 if (!tvh->functions->init(tvh->priv)) return NULL;
639 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,&(tvh->tv_param->tdevice));
641 if (!open_tv(tvh)){
642 tv_uninit(tvh);
643 return NULL;
645 funcs = tvh->functions;
646 demuxer->priv=tvh;
648 sh_video = new_sh_video(demuxer, 0);
650 /* get IMAGE FORMAT */
651 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
652 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
653 // sh_video->format = 0x0;
655 /* set FPS and FRAMETIME */
657 if(!sh_video->fps)
659 float tmp;
660 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
661 sh_video->fps = 25.0f; /* on PAL */
662 else sh_video->fps = tmp;
665 if (tvh->tv_param->fps != -1.0f)
666 sh_video->fps = tvh->tv_param->fps;
668 sh_video->frametime = 1.0f/sh_video->fps;
670 /* If playback only mode, go to immediate mode, fail silently */
671 if(tvh->tv_param->immediate == 1)
673 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
674 tvh->tv_param->noaudio = 1;
677 /* disable TV audio if -nosound is present */
678 if (!demuxer->audio || demuxer->audio->id == -2) {
679 tvh->tv_param->noaudio = 1;
682 /* set width */
683 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
685 /* set height */
686 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
688 demuxer->video->sh = sh_video;
689 sh_video->ds = demuxer->video;
690 demuxer->video->id = 0;
691 demuxer->seekable = 0;
693 /* here comes audio init */
694 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
696 int audio_format;
697 int sh_audio_format;
698 char buf[128];
700 /* yeah, audio is present */
702 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
703 &tvh->tv_param->audiorate);
705 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
706 goto no_audio;
708 switch(audio_format)
710 case AF_FORMAT_U8:
711 case AF_FORMAT_S8:
712 case AF_FORMAT_U16_LE:
713 case AF_FORMAT_U16_BE:
714 case AF_FORMAT_S16_LE:
715 case AF_FORMAT_S16_BE:
716 case AF_FORMAT_S32_LE:
717 case AF_FORMAT_S32_BE:
718 sh_audio_format = 0x1; /* PCM */
719 break;
720 case AF_FORMAT_IMA_ADPCM:
721 case AF_FORMAT_MU_LAW:
722 case AF_FORMAT_A_LAW:
723 case AF_FORMAT_MPEG2:
724 case AF_FORMAT_AC3:
725 default:
726 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnsupportedAudioType,
727 af_fmt2str(audio_format, buf, 128), audio_format);
728 goto no_audio;
731 sh_audio = new_sh_audio(demuxer, 0);
733 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
734 &sh_audio->samplerate);
735 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
736 &sh_audio->samplesize);
737 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
738 &sh_audio->channels);
740 sh_audio->format = sh_audio_format;
741 sh_audio->sample_format = audio_format;
743 sh_audio->i_bps = sh_audio->o_bps =
744 sh_audio->samplerate * sh_audio->samplesize *
745 sh_audio->channels;
747 // emulate WF for win32 codecs:
748 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
749 sh_audio->wf->wFormatTag = sh_audio->format;
750 sh_audio->wf->nChannels = sh_audio->channels;
751 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
752 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
753 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
754 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
756 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_TV_AudioFormat,
757 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
758 sh_audio->wf->nSamplesPerSec);
760 demuxer->audio->sh = sh_audio;
761 sh_audio->ds = demuxer->audio;
762 demuxer->audio->id = 0;
764 no_audio:
766 if(!(funcs->start(tvh->priv))){
767 // start failed :(
768 tv_uninit(tvh);
769 return NULL;
772 /* set color eq */
773 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
774 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
775 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
776 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
778 if(tvh->tv_param->gain!=-1)
779 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
780 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
782 funcs->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
784 return demuxer;
787 static void demux_close_tv(demuxer_t *demuxer)
789 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
790 if (!tvh) return;
791 tv_uninit(tvh);
792 free(tvh);
793 demuxer->priv=NULL;
796 /* utilities for mplayer (not mencoder!!) */
797 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
799 tvi_functions_t *funcs = tvh->functions;
801 switch(opt)
803 case TV_COLOR_BRIGHTNESS:
804 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
805 case TV_COLOR_HUE:
806 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
807 case TV_COLOR_SATURATION:
808 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
809 case TV_COLOR_CONTRAST:
810 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
811 default:
812 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
815 return(TVI_CONTROL_UNKNOWN);
818 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
820 tvi_functions_t *funcs = tvh->functions;
822 switch(opt)
824 case TV_COLOR_BRIGHTNESS:
825 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
826 case TV_COLOR_HUE:
827 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
828 case TV_COLOR_SATURATION:
829 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
830 case TV_COLOR_CONTRAST:
831 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
832 default:
833 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
836 return(TVI_CONTROL_UNKNOWN);
839 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
841 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
843 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
844 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
845 *freq, (float)*freq/16);
847 return(1);
850 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
852 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
854 // unsigned long freq = atof(tvh->tv_param->freq)*16;
856 /* set freq in MHz */
857 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
859 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
860 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
861 freq, (float)freq/16);
863 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
864 return(1);
867 int tv_get_signal(tvi_handle_t *tvh)
869 int signal=0;
870 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
871 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
872 return 0;
874 return signal;
877 /*****************************************************************
878 * \brief tune current frequency by step_interval value
879 * \parameter step_interval increment value in 1/16 MHz
880 * \note frequency is rounded to 1/16 MHz value
881 * \return 1
884 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
885 unsigned long frequency;
887 tvh->tv_param->scan=0;
888 tv_get_freq(tvh,&frequency);
889 frequency+=step_interval;
890 return tv_set_freq(tvh,frequency);
893 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
895 struct CHANLIST cl;
897 tvh->tv_param->scan=0;
898 if (direction == TV_CHANNEL_LOWER)
900 if (tvh->channel-1 >= 0)
902 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
903 cl = tvh->chanlist_s[--tvh->channel];
904 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
905 cl.name, (float)cl.freq/1000);
906 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
910 if (direction == TV_CHANNEL_HIGHER)
912 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
914 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
915 cl = tvh->chanlist_s[++tvh->channel];
916 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
917 cl.name, (float)cl.freq/1000);
918 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
921 return(1);
924 int tv_step_channel(tvi_handle_t *tvh, int direction) {
925 tvh->tv_param->scan=0;
926 if (tv_channel_list) {
927 if (direction == TV_CHANNEL_HIGHER) {
928 tv_channel_last = tv_channel_current;
929 if (tv_channel_current->next)
930 tv_channel_current = tv_channel_current->next;
931 else
932 tv_channel_current = tv_channel_list;
933 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
934 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
935 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
937 if (direction == TV_CHANNEL_LOWER) {
938 tv_channel_last = tv_channel_current;
939 if (tv_channel_current->prev)
940 tv_channel_current = tv_channel_current->prev;
941 else
942 while (tv_channel_current->next)
943 tv_channel_current = tv_channel_current->next;
944 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
945 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
946 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
948 } else tv_step_channel_real(tvh, direction);
949 return(1);
952 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
953 int i;
954 struct CHANLIST cl;
956 tvh->tv_param->scan=0;
957 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
958 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
960 cl = tvh->chanlist_s[i];
961 // printf("count%d: name: %s, freq: %d\n",
962 // i, cl.name, cl.freq);
963 if (!strcasecmp(cl.name, channel))
965 tvh->channel = i;
966 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
967 cl.name, (float)cl.freq/1000);
968 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
969 break;
972 return(1);
975 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
976 int i, channel_int;
978 tvh->tv_param->scan=0;
979 if (tv_channel_list) {
980 tv_channel_last = tv_channel_current;
981 channel_int = atoi(channel);
982 tv_channel_current = tv_channel_list;
983 for (i = 1; i < channel_int; i++)
984 if (tv_channel_current->next)
985 tv_channel_current = tv_channel_current->next;
986 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
987 tv_channel_current->name, (float)tv_channel_current->freq/1000);
988 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
989 } else tv_set_channel_real(tvh, channel);
990 return(1);
993 int tv_last_channel(tvi_handle_t *tvh) {
995 tvh->tv_param->scan=0;
996 if (tv_channel_list) {
997 tv_channels_t *tmp;
999 tmp = tv_channel_last;
1000 tv_channel_last = tv_channel_current;
1001 tv_channel_current = tmp;
1003 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1004 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1005 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1006 } else {
1007 int i;
1008 struct CHANLIST cl;
1010 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1012 cl = tvh->chanlist_s[i];
1013 if (!strcasecmp(cl.name, tv_channel_last_real))
1015 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1016 tvh->channel = i;
1017 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1018 cl.name, (float)cl.freq/1000);
1019 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1020 break;
1024 return(1);
1027 int tv_step_norm(tvi_handle_t *tvh)
1029 tvh->norm++;
1030 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1031 &tvh->norm) != TVI_CONTROL_TRUE) {
1032 tvh->norm = 0;
1033 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1034 &tvh->norm) != TVI_CONTROL_TRUE) {
1035 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1036 return 0;
1039 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
1040 return(1);
1043 int tv_step_chanlist(tvi_handle_t *tvh)
1045 return(1);
1048 int tv_set_norm(tvi_handle_t *tvh, char* norm)
1050 tvh->norm = norm_from_string(tvh, norm);
1052 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNorm, norm);
1053 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
1054 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1055 return 0;
1057 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
1058 return(1);
1061 demuxer_desc_t demuxer_desc_tv = {
1062 "Tv card demuxer",
1063 "tv",
1064 "TV",
1065 "Alex Beregszaszi, Charles R. Henrich",
1066 "?",
1067 DEMUXER_TYPE_TV,
1068 0, // no autodetect
1069 NULL,
1070 demux_tv_fill_buffer,
1071 demux_open_tv,
1072 demux_close_tv,
1073 NULL,
1074 NULL