revert commits 26437-26439 the right way[tm]
[mplayer/glamo.git] / stream / tv.c
blob17bedccce3b21b5d4b455c068ffda445756676a4
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 HAVE_TV_V4L2
53 &tvi_info_v4l2,
54 #endif
55 #ifdef HAVE_TV_V4L1
56 &tvi_info_v4l,
57 #endif
58 #ifdef HAVE_TV_BSDBT848
59 &tvi_info_bsdbt848,
60 #endif
61 #ifdef HAVE_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;
258 while (*channels) {
259 char* tmp = *(channels++);
260 char* sep = strchr(tmp,'-');
261 int i;
262 struct CHANLIST cl;
264 if (!sep) continue; // Wrong syntax, but mplayer should not crash
266 av_strlcpy(tv_channel_current->name, sep + 1,
267 sizeof(tv_channel_current->name));
268 sep[0] = '\0';
269 strncpy(tv_channel_current->number, tmp, 5);
270 tv_channel_current->number[4]='\0';
272 while ((sep=strchr(tv_channel_current->name, '_')))
273 sep[0] = ' ';
275 // if channel number is a number and larger than 1000 threat it as frequency
276 // tmp still contain pointer to null-terminated string with channel number here
277 if (atoi(tmp)>1000){
278 tv_channel_current->freq=atoi(tmp);
279 }else{
280 tv_channel_current->freq = 0;
281 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
282 cl = tvh->chanlist_s[i];
283 if (!strcasecmp(cl.name, tv_channel_current->number)) {
284 tv_channel_current->freq=cl.freq;
285 break;
289 if (tv_channel_current->freq == 0)
290 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoFreqForChannel,
291 tv_channel_current->number, tv_channel_current->name);
292 else {
293 sep = strchr(tv_channel_current->name, '-');
294 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
296 if ( sep ) {
297 i = atoi (sep+1);
298 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
299 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
300 sep[0] = '\0';
304 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
305 tv_channel_current->number, tv_channel_current->name,
306 (float)tv_channel_current->freq/1000);*/
308 tv_channel_current->next = malloc(sizeof(tv_channels_t));
309 tv_channel_current->next->index = tv_channel_current->index + 1;
310 tv_channel_current->next->prev = tv_channel_current;
311 tv_channel_current->next->next = NULL;
312 tv_channel_current = tv_channel_current->next;
314 if (tv_channel_current->prev)
315 tv_channel_current->prev->next = NULL;
316 free(tv_channel_current);
318 static int open_tv(tvi_handle_t *tvh)
320 int i;
321 const tvi_functions_t *funcs = tvh->functions;
322 int tv_fmt_list[] = {
323 IMGFMT_YV12,
324 IMGFMT_I420,
325 IMGFMT_UYVY,
326 IMGFMT_YUY2,
327 IMGFMT_RGB32,
328 IMGFMT_RGB24,
329 IMGFMT_RGB16,
330 IMGFMT_RGB15
333 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
335 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoVideoInputPresent);
336 return 0;
339 if (tvh->tv_param->outfmt == -1)
340 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
342 tvh->tv_param->outfmt = tv_fmt_list[i];
343 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
344 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
345 break;
347 else
349 switch(tvh->tv_param->outfmt)
351 case IMGFMT_YV12:
352 case IMGFMT_I420:
353 case IMGFMT_UYVY:
354 case IMGFMT_YUY2:
355 case IMGFMT_RGB32:
356 case IMGFMT_RGB24:
357 case IMGFMT_BGR32:
358 case IMGFMT_BGR24:
359 case IMGFMT_BGR16:
360 case IMGFMT_BGR15:
361 break;
362 default:
363 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnknownImageFormat,tvh->tv_param->outfmt);
365 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
368 /* set some params got from cmdline */
369 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
371 #if defined(HAVE_TV_V4L2) || defined(HAVE_TV_DSHOW)
372 if (0
373 #ifdef HAVE_TV_V4L2
374 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
375 #endif
376 #ifdef HAVE_TV_DSHOW
377 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
378 #endif
380 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNormId, tvh->tv_param->normid);
381 if (funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->tv_param->normid) != TVI_CONTROL_TRUE) {
382 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
384 } else
385 #endif
386 tv_set_norm(tvh,tvh->tv_param->norm);
388 #ifdef HAVE_TV_V4L1
389 if ( tvh->tv_param->mjpeg )
391 /* set width to expected value */
392 if (tvh->tv_param->width == -1)
394 tvh->tv_param->width = 704/tvh->tv_param->decimation;
396 if (tvh->tv_param->height == -1)
398 if ( tvh->norm != TV_NORM_NTSC )
399 tvh->tv_param->height = 576/tvh->tv_param->decimation;
400 else
401 tvh->tv_param->height = 480/tvh->tv_param->decimation;
403 mp_msg(MSGT_TV, MSGL_INFO,
404 MSGTR_TV_MJP_WidthHeight, tvh->tv_param->width, tvh->tv_param->height);
406 #endif
408 /* limits on w&h are norm-dependent -- JM */
409 /* set width */
410 if (tvh->tv_param->width != -1)
412 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
413 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
414 else
416 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetWidth, tvh->tv_param->width);
417 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
421 /* set height */
422 if (tvh->tv_param->height != -1)
424 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
425 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
426 else
428 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetHeight, tvh->tv_param->height);
429 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
433 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
435 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_NoTuner);
436 goto done;
439 /* select channel list */
440 for (i = 0; chanlists[i].name != NULL; i++)
442 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
444 tvh->chanlist = i;
445 tvh->chanlist_s = chanlists[i].list;
446 break;
450 if (tvh->chanlist == -1)
451 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnableFindChanlist,
452 tvh->tv_param->chanlist);
453 else
454 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedChanlist,
455 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
457 if (tvh->tv_param->freq && tvh->tv_param->channel)
459 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ChannelFreqParamConflict);
460 goto done;
463 /* Handle channel names */
464 if (tvh->tv_param->channels) {
465 parse_channels(tvh);
466 } else
467 tv_channel_last_real = malloc(5);
469 if (tv_channel_list) {
470 int i;
471 int channel = 0;
472 if (tvh->tv_param->channel)
474 if (isdigit(*tvh->tv_param->channel))
475 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
476 channel = atoi(tvh->tv_param->channel);
477 else
479 /* if tvh->tv_param->channel does not begin with a digit
480 set the first channel that contains tvh->tv_param->channel in its name */
482 tv_channel_current = tv_channel_list;
483 while ( tv_channel_current ) {
484 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
485 break;
486 tv_channel_current = tv_channel_current->next;
488 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
491 else
492 channel = 1;
494 if ( channel ) {
495 tv_channel_current = tv_channel_list;
496 for (i = 1; i < channel; i++)
497 if (tv_channel_current->next)
498 tv_channel_current = tv_channel_current->next;
501 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
502 tv_channel_current->name, (float)tv_channel_current->freq/1000);
503 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
504 tv_channel_last = tv_channel_current;
505 } else {
506 /* we need to set frequency */
507 if (tvh->tv_param->freq)
509 unsigned long freq = atof(tvh->tv_param->freq)*16;
511 /* set freq in MHz */
512 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
514 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
515 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedFrequency,
516 freq, (float)freq/16);
519 if (tvh->tv_param->channel) {
520 struct CHANLIST cl;
522 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_RequestedChannel, tvh->tv_param->channel);
523 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
525 cl = tvh->chanlist_s[i];
526 // printf("count%d: name: %s, freq: %d\n",
527 // i, cl.name, cl.freq);
528 if (!strcasecmp(cl.name, tvh->tv_param->channel))
530 strcpy(tv_channel_last_real, cl.name);
531 tvh->channel = i;
532 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
533 cl.name, (float)cl.freq/1000);
534 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
535 break;
541 /* grep frequency in chanlist */
543 unsigned long i2;
544 int freq;
546 tv_get_freq(tvh, &i2);
548 freq = (int) (((float)(i2/16))*1000)+250;
550 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
552 if (tvh->chanlist_s[i].freq == freq)
554 tvh->channel = i+1;
555 break;
560 done:
561 /* also start device! */
562 return 1;
565 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
567 int i;
568 tvi_handle_t* h;
569 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
570 mp_msg(MSGT_TV,MSGL_INFO,MSGTR_TV_AvailableDrivers);
571 for(i=0;tvi_driver_list[i];i++){
572 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
573 if(tvi_driver_list[i]->comment)
574 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
575 mp_msg(MSGT_TV,MSGL_INFO,"\n");
577 return NULL;
580 for(i=0;tvi_driver_list[i];i++){
581 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
582 h=tvi_driver_list[i]->tvi_init(tv_param);
583 //Requested driver initialization failed
584 if (!h && tv_param->driver)
585 return NULL;
586 //Driver initialization failed during autodetection process.
587 if (!h)
588 continue;
590 h->tv_param=tv_param;
591 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_DriverInfo, tvi_driver_list[i]->short_name,
592 tvi_driver_list[i]->name,
593 tvi_driver_list[i]->author,
594 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
595 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
596 return h;
600 if(tv_param->driver)
601 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoSuchDriver, tv_param->driver);
602 else
603 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_DriverAutoDetectionFailed);
604 return(NULL);
607 static int tv_uninit(tvi_handle_t *tvh)
609 int res;
610 if(!tvh) return 1;
611 if (!tvh->priv) return 1;
612 res=tvh->functions->uninit(tvh->priv);
613 if(res) {
614 free(tvh->priv);
615 tvh->priv=NULL;
617 return res;
620 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
622 tvi_handle_t *tvh;
623 sh_video_t *sh_video;
624 sh_audio_t *sh_audio = NULL;
625 const tvi_functions_t *funcs;
627 demuxer->priv=NULL;
628 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
629 if (!tvh->functions->init(tvh->priv)) return NULL;
631 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,&(tvh->tv_param->tdevice));
633 if (!open_tv(tvh)){
634 tv_uninit(tvh);
635 return NULL;
637 funcs = tvh->functions;
638 demuxer->priv=tvh;
640 sh_video = new_sh_video(demuxer, 0);
642 /* get IMAGE FORMAT */
643 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
644 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
645 // sh_video->format = 0x0;
647 /* set FPS and FRAMETIME */
649 if(!sh_video->fps)
651 float tmp;
652 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
653 sh_video->fps = 25.0f; /* on PAL */
654 else sh_video->fps = tmp;
657 if (tvh->tv_param->fps != -1.0f)
658 sh_video->fps = tvh->tv_param->fps;
660 sh_video->frametime = 1.0f/sh_video->fps;
662 /* If playback only mode, go to immediate mode, fail silently */
663 if(tvh->tv_param->immediate == 1)
665 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
666 tvh->tv_param->noaudio = 1;
669 /* disable TV audio if -nosound is present */
670 if (!demuxer->audio || demuxer->audio->id == -2) {
671 tvh->tv_param->noaudio = 1;
674 /* set width */
675 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
677 /* set height */
678 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
680 demuxer->video->sh = sh_video;
681 sh_video->ds = demuxer->video;
682 demuxer->video->id = 0;
683 demuxer->seekable = 0;
685 /* here comes audio init */
686 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
688 int audio_format;
689 int sh_audio_format;
690 char buf[128];
692 /* yeah, audio is present */
694 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
695 &tvh->tv_param->audiorate);
697 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
698 goto no_audio;
700 switch(audio_format)
702 case AF_FORMAT_U8:
703 case AF_FORMAT_S8:
704 case AF_FORMAT_U16_LE:
705 case AF_FORMAT_U16_BE:
706 case AF_FORMAT_S16_LE:
707 case AF_FORMAT_S16_BE:
708 case AF_FORMAT_S32_LE:
709 case AF_FORMAT_S32_BE:
710 sh_audio_format = 0x1; /* PCM */
711 break;
712 case AF_FORMAT_IMA_ADPCM:
713 case AF_FORMAT_MU_LAW:
714 case AF_FORMAT_A_LAW:
715 case AF_FORMAT_MPEG2:
716 case AF_FORMAT_AC3:
717 default:
718 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnsupportedAudioType,
719 af_fmt2str(audio_format, buf, 128), audio_format);
720 goto no_audio;
723 sh_audio = new_sh_audio(demuxer, 0);
725 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
726 &sh_audio->samplerate);
727 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
728 &sh_audio->samplesize);
729 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
730 &sh_audio->channels);
732 sh_audio->format = sh_audio_format;
733 sh_audio->sample_format = audio_format;
735 sh_audio->i_bps = sh_audio->o_bps =
736 sh_audio->samplerate * sh_audio->samplesize *
737 sh_audio->channels;
739 // emulate WF for win32 codecs:
740 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
741 sh_audio->wf->wFormatTag = sh_audio->format;
742 sh_audio->wf->nChannels = sh_audio->channels;
743 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
744 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
745 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
746 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
748 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_TV_AudioFormat,
749 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
750 sh_audio->wf->nSamplesPerSec);
752 demuxer->audio->sh = sh_audio;
753 sh_audio->ds = demuxer->audio;
754 demuxer->audio->id = 0;
756 no_audio:
758 if(!(funcs->start(tvh->priv))){
759 // start failed :(
760 tv_uninit(tvh);
761 return NULL;
764 /* set color eq */
765 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
766 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
767 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
768 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
770 if(tvh->tv_param->gain!=-1)
771 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
772 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
774 funcs->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
776 return demuxer;
779 static void demux_close_tv(demuxer_t *demuxer)
781 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
782 if (!tvh) return;
783 tv_uninit(tvh);
784 free(tvh);
785 demuxer->priv=NULL;
788 /* utilities for mplayer (not mencoder!!) */
789 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
791 const tvi_functions_t *funcs = tvh->functions;
793 switch(opt)
795 case TV_COLOR_BRIGHTNESS:
796 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
797 case TV_COLOR_HUE:
798 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
799 case TV_COLOR_SATURATION:
800 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
801 case TV_COLOR_CONTRAST:
802 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
803 default:
804 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
807 return(TVI_CONTROL_UNKNOWN);
810 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
812 const tvi_functions_t *funcs = tvh->functions;
814 switch(opt)
816 case TV_COLOR_BRIGHTNESS:
817 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
818 case TV_COLOR_HUE:
819 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
820 case TV_COLOR_SATURATION:
821 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
822 case TV_COLOR_CONTRAST:
823 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
824 default:
825 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
828 return(TVI_CONTROL_UNKNOWN);
831 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
833 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
835 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
836 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
837 *freq, (float)*freq/16);
839 return(1);
842 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
844 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
846 // unsigned long freq = atof(tvh->tv_param->freq)*16;
848 /* set freq in MHz */
849 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
851 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
852 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
853 freq, (float)freq/16);
855 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
856 return(1);
859 int tv_get_signal(tvi_handle_t *tvh)
861 int signal=0;
862 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
863 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
864 return 0;
866 return signal;
869 /*****************************************************************
870 * \brief tune current frequency by step_interval value
871 * \parameter step_interval increment value in 1/16 MHz
872 * \note frequency is rounded to 1/16 MHz value
873 * \return 1
876 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
877 unsigned long frequency;
879 tvh->tv_param->scan=0;
880 tv_get_freq(tvh,&frequency);
881 frequency+=step_interval;
882 return tv_set_freq(tvh,frequency);
885 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
887 struct CHANLIST cl;
889 tvh->tv_param->scan=0;
890 if (direction == TV_CHANNEL_LOWER)
892 if (tvh->channel-1 >= 0)
894 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
895 cl = tvh->chanlist_s[--tvh->channel];
896 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
897 cl.name, (float)cl.freq/1000);
898 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
902 if (direction == TV_CHANNEL_HIGHER)
904 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
906 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
907 cl = tvh->chanlist_s[++tvh->channel];
908 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
909 cl.name, (float)cl.freq/1000);
910 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
913 return(1);
916 int tv_step_channel(tvi_handle_t *tvh, int direction) {
917 tvh->tv_param->scan=0;
918 if (tv_channel_list) {
919 if (direction == TV_CHANNEL_HIGHER) {
920 tv_channel_last = tv_channel_current;
921 if (tv_channel_current->next)
922 tv_channel_current = tv_channel_current->next;
923 else
924 tv_channel_current = tv_channel_list;
925 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
926 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
927 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
929 if (direction == TV_CHANNEL_LOWER) {
930 tv_channel_last = tv_channel_current;
931 if (tv_channel_current->prev)
932 tv_channel_current = tv_channel_current->prev;
933 else
934 while (tv_channel_current->next)
935 tv_channel_current = tv_channel_current->next;
936 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
937 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
938 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
940 } else tv_step_channel_real(tvh, direction);
941 return(1);
944 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
945 int i;
946 struct CHANLIST cl;
948 tvh->tv_param->scan=0;
949 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
950 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
952 cl = tvh->chanlist_s[i];
953 // printf("count%d: name: %s, freq: %d\n",
954 // i, cl.name, cl.freq);
955 if (!strcasecmp(cl.name, channel))
957 tvh->channel = i;
958 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
959 cl.name, (float)cl.freq/1000);
960 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
961 break;
964 return(1);
967 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
968 int i, channel_int;
970 tvh->tv_param->scan=0;
971 if (tv_channel_list) {
972 tv_channel_last = tv_channel_current;
973 channel_int = atoi(channel);
974 tv_channel_current = tv_channel_list;
975 for (i = 1; i < channel_int; i++)
976 if (tv_channel_current->next)
977 tv_channel_current = tv_channel_current->next;
978 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
979 tv_channel_current->name, (float)tv_channel_current->freq/1000);
980 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
981 } else tv_set_channel_real(tvh, channel);
982 return(1);
985 int tv_last_channel(tvi_handle_t *tvh) {
987 tvh->tv_param->scan=0;
988 if (tv_channel_list) {
989 tv_channels_t *tmp;
991 tmp = tv_channel_last;
992 tv_channel_last = tv_channel_current;
993 tv_channel_current = tmp;
995 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
996 tv_channel_current->name, (float)tv_channel_current->freq/1000);
997 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
998 } else {
999 int i;
1000 struct CHANLIST cl;
1002 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1004 cl = tvh->chanlist_s[i];
1005 if (!strcasecmp(cl.name, tv_channel_last_real))
1007 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1008 tvh->channel = i;
1009 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1010 cl.name, (float)cl.freq/1000);
1011 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1012 break;
1016 return(1);
1019 int tv_step_norm(tvi_handle_t *tvh)
1021 tvh->norm++;
1022 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1023 &tvh->norm) != TVI_CONTROL_TRUE) {
1024 tvh->norm = 0;
1025 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1026 &tvh->norm) != TVI_CONTROL_TRUE) {
1027 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1028 return 0;
1031 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
1032 return(1);
1035 int tv_step_chanlist(tvi_handle_t *tvh)
1037 return(1);
1040 int tv_set_norm(tvi_handle_t *tvh, char* norm)
1042 tvh->norm = norm_from_string(tvh, norm);
1044 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNorm, norm);
1045 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
1046 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1047 return 0;
1049 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
1050 return(1);
1053 demuxer_desc_t demuxer_desc_tv = {
1054 "Tv card demuxer",
1055 "tv",
1056 "TV",
1057 "Alex Beregszaszi, Charles R. Henrich",
1058 "?",
1059 DEMUXER_TYPE_TV,
1060 0, // no autodetect
1061 NULL,
1062 demux_tv_fill_buffer,
1063 demux_open_tv,
1064 demux_close_tv,
1065 NULL,
1066 NULL