Simplify: use &= instead of a = b & a;
[mplayer/greg.git] / stream / tv.c
blob7e7ba263fad61aaf1982a7abd8a761cde52129f0
1 /*
2 TV Interface for MPlayer
4 (C) Alex Beregszaszi
6 API idea based on libvo2
8 Feb 19, 2002: Significant rewrites by Charles R. Henrich (henrich@msu.edu)
9 to add support for audio, and bktr *BSD support.
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <sys/time.h>
20 #include "config.h"
23 #include "mp_msg.h"
24 #include "help_mp.h"
26 #include "stream.h"
27 #include "libmpdemux/demuxer.h"
28 #include "libmpdemux/stheader.h"
30 #include "libaf/af_format.h"
31 #include "libmpcodecs/img_format.h"
32 #include "libmpcodecs/dec_teletext.h"
33 #include "libavutil/avstring.h"
34 #include "osdep/timer.h"
36 #include "tv.h"
38 #include "frequencies.h"
40 tv_channels_t *tv_channel_list;
41 tv_channels_t *tv_channel_current, *tv_channel_last;
42 char *tv_channel_last_real;
44 /* enumerating drivers (like in stream.c) */
45 extern const tvi_info_t tvi_info_dummy;
46 extern const tvi_info_t tvi_info_dshow;
47 extern const tvi_info_t tvi_info_v4l;
48 extern const tvi_info_t tvi_info_v4l2;
49 extern const tvi_info_t tvi_info_bsdbt848;
51 /** List of drivers in autodetection order */
52 static const tvi_info_t* tvi_driver_list[]={
53 #ifdef CONFIG_TV_V4L2
54 &tvi_info_v4l2,
55 #endif
56 #ifdef CONFIG_TV_V4L1
57 &tvi_info_v4l,
58 #endif
59 #ifdef CONFIG_TV_BSDBT848
60 &tvi_info_bsdbt848,
61 #endif
62 #ifdef CONFIG_TV_DSHOW
63 &tvi_info_dshow,
64 #endif
65 &tvi_info_dummy,
66 NULL
69 void tv_start_scan(tvi_handle_t *tvh, int start)
71 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
72 tvh->tv_param->scan=start?1:0;
75 static void tv_scan(tvi_handle_t *tvh)
77 unsigned int now;
78 struct CHANLIST cl;
79 tv_channels_t *tv_channel_tmp=NULL;
80 tv_channels_t *tv_channel_add=NULL;
81 tv_scan_t* scan;
82 int found=0, index=1;
84 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
85 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
87 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ScannerNotAvailableWithoutTuner);
88 tvh->tv_param->scan=0;
89 return;
92 scan = tvh->scan;
93 now=GetTimer();
94 if (!scan) {
95 scan=calloc(1,sizeof(tv_scan_t));
96 tvh->scan=scan;
97 cl = tvh->chanlist_s[scan->channel_num];
98 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
99 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
101 if(scan->scan_timer>now)
102 return;
104 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
105 cl = tvh->chanlist_s[scan->channel_num];
106 tv_channel_tmp=tv_channel_list;
107 while (tv_channel_tmp) {
108 index++;
109 if (cl.freq==tv_channel_tmp->freq){
110 found=1;
111 break;
113 tv_channel_add=tv_channel_tmp;
114 tv_channel_tmp=tv_channel_tmp->next;
116 if (!found) {
117 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
118 scan->new_channels++;
119 tv_channel_tmp = malloc(sizeof(tv_channels_t));
120 tv_channel_tmp->index=index;
121 tv_channel_tmp->next=NULL;
122 tv_channel_tmp->prev=tv_channel_add;
123 tv_channel_tmp->freq=cl.freq;
124 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
125 strncpy(tv_channel_tmp->number, cl.name, 5);
126 tv_channel_tmp->number[4]='\0';
127 if (!tv_channel_list)
128 tv_channel_list=tv_channel_tmp;
129 else {
130 tv_channel_add->next=tv_channel_tmp;
131 tv_channel_list->prev=tv_channel_tmp;
133 }else
134 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
135 tv_channel_tmp->number,tv_channel_tmp->name);
137 scan->channel_num++;
138 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
139 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
140 tvh->tv_param->scan=0;
141 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
142 tv_channel_tmp=tv_channel_list;
143 if(tv_channel_tmp){
144 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
145 while(tv_channel_tmp){
146 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
147 if(tv_channel_tmp->next)
148 mp_msg(MSGT_TV,MSGL_INFO,",");
149 tv_channel_tmp=tv_channel_tmp->next;
151 mp_msg(MSGT_TV, MSGL_INFO, "\n");
153 if (!tv_channel_current) tv_channel_current=tv_channel_list;
154 if (tv_channel_current)
155 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
156 free(tvh->scan);
157 tvh->scan=NULL;
158 }else{
159 cl = tvh->chanlist_s[scan->channel_num];
160 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
161 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
165 /* ================== DEMUX_TV ===================== */
167 Return value:
168 0 = EOF(?) or no stream
169 1 = successfully read a packet
171 /* fill demux->video and demux->audio */
173 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
175 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
176 demux_packet_t* dp;
177 unsigned int len=0;
179 /* ================== ADD AUDIO PACKET =================== */
181 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
182 tvh->functions->control(tvh->priv,
183 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
185 len = tvh->functions->get_audio_framesize(tvh->priv);
187 dp=new_demux_packet(len);
188 dp->flags|=1; /* Keyframe */
189 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
190 ds_add_packet(demux->audio,dp);
193 /* ================== ADD VIDEO PACKET =================== */
195 if (ds==demux->video && tvh->functions->control(tvh->priv,
196 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
198 len = tvh->functions->get_video_framesize(tvh->priv);
199 dp=new_demux_packet(len);
200 dp->flags|=1; /* Keyframe */
201 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
202 ds_add_packet(demux->video,dp);
205 if (tvh->tv_param->scan) tv_scan(tvh);
206 return 1;
209 static int norm_from_string(tvi_handle_t *tvh, char* norm)
211 const tvi_functions_t *funcs = tvh->functions;
212 char str[20];
213 int ret;
215 strncpy(str, norm, sizeof(str)-1);
216 str[sizeof(str)-1] = '\0';
217 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
219 if(ret==TVI_CONTROL_TRUE)
220 return *(int *)str;
222 if(ret!=TVI_CONTROL_UNKNOWN)
224 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm,"default");
225 return 0;
228 if (!strcasecmp(norm, "pal"))
229 return TV_NORM_PAL;
230 else if (!strcasecmp(norm, "ntsc"))
231 return TV_NORM_NTSC;
232 else if (!strcasecmp(norm, "secam"))
233 return TV_NORM_SECAM;
234 else if (!strcasecmp(norm, "palnc"))
235 return TV_NORM_PALNC;
236 else if (!strcasecmp(norm, "palm"))
237 return TV_NORM_PALM;
238 else if (!strcasecmp(norm, "paln"))
239 return TV_NORM_PALN;
240 else if (!strcasecmp(norm, "ntscjp"))
241 return TV_NORM_NTSCJP;
242 else {
243 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm, "PAL");
244 return TV_NORM_PAL;
248 static void parse_channels(tvi_handle_t *tvh)
250 char** channels = tvh->tv_param->channels;
252 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_ChannelNamesDetected);
253 tv_channel_list = malloc(sizeof(tv_channels_t));
254 tv_channel_list->index=1;
255 tv_channel_list->next=NULL;
256 tv_channel_list->prev=NULL;
257 tv_channel_current = tv_channel_list;
258 tv_channel_current->norm = tvh->norm;
260 while (*channels) {
261 char* tmp = *(channels++);
262 char* sep = strchr(tmp,'-');
263 int i;
264 struct CHANLIST cl;
266 if (!sep) continue; // Wrong syntax, but mplayer should not crash
268 av_strlcpy(tv_channel_current->name, sep + 1,
269 sizeof(tv_channel_current->name));
270 sep[0] = '\0';
271 strncpy(tv_channel_current->number, tmp, 5);
272 tv_channel_current->number[4]='\0';
274 while ((sep=strchr(tv_channel_current->name, '_')))
275 sep[0] = ' ';
277 // if channel number is a number and larger than 1000 threat it as frequency
278 // tmp still contain pointer to null-terminated string with channel number here
279 if (atoi(tmp)>1000){
280 tv_channel_current->freq=atoi(tmp);
281 }else{
282 tv_channel_current->freq = 0;
283 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
284 cl = tvh->chanlist_s[i];
285 if (!strcasecmp(cl.name, tv_channel_current->number)) {
286 tv_channel_current->freq=cl.freq;
287 break;
291 if (tv_channel_current->freq == 0)
292 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoFreqForChannel,
293 tv_channel_current->number, tv_channel_current->name);
294 else {
295 sep = strchr(tv_channel_current->name, '-');
296 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
298 if ( sep ) {
299 i = atoi (sep+1);
300 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
301 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
302 sep[0] = '\0';
305 sep = strchr(tv_channel_current->name, '=');
306 if ( sep ) {
307 tv_channel_current->norm = norm_from_string(tvh, sep+1);
308 sep[0] = '\0';
312 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
313 tv_channel_current->number, tv_channel_current->name,
314 (float)tv_channel_current->freq/1000);*/
316 tv_channel_current->next = malloc(sizeof(tv_channels_t));
317 tv_channel_current->next->index = tv_channel_current->index + 1;
318 tv_channel_current->next->prev = tv_channel_current;
319 tv_channel_current->next->next = NULL;
320 tv_channel_current = tv_channel_current->next;
321 tv_channel_current->norm = tvh->norm;
323 if (tv_channel_current->prev)
324 tv_channel_current->prev->next = NULL;
325 free(tv_channel_current);
328 int tv_set_norm(tvi_handle_t *tvh, char* norm)
330 tvh->norm = norm_from_string(tvh, norm);
332 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNorm, norm);
333 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
334 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
335 return 0;
337 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
338 return 1;
341 int tv_set_norm_i(tvi_handle_t *tvh, int norm)
343 tvh->norm = norm;
345 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNormId, norm);
346 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
347 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
348 return 0;
351 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
352 return 1;
355 static int open_tv(tvi_handle_t *tvh)
357 int i;
358 const tvi_functions_t *funcs = tvh->functions;
359 int tv_fmt_list[] = {
360 IMGFMT_YV12,
361 IMGFMT_I420,
362 IMGFMT_UYVY,
363 IMGFMT_YUY2,
364 IMGFMT_RGB32,
365 IMGFMT_RGB24,
366 IMGFMT_RGB16,
367 IMGFMT_RGB15
370 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
372 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoVideoInputPresent);
373 return 0;
376 if (tvh->tv_param->outfmt == -1)
377 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
379 tvh->tv_param->outfmt = tv_fmt_list[i];
380 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
381 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
382 break;
384 else
386 switch(tvh->tv_param->outfmt)
388 case IMGFMT_YV12:
389 case IMGFMT_I420:
390 case IMGFMT_UYVY:
391 case IMGFMT_YUY2:
392 case IMGFMT_RGB32:
393 case IMGFMT_RGB24:
394 case IMGFMT_BGR32:
395 case IMGFMT_BGR24:
396 case IMGFMT_BGR16:
397 case IMGFMT_BGR15:
398 break;
399 default:
400 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnknownImageFormat,tvh->tv_param->outfmt);
402 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
405 /* set some params got from cmdline */
406 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
408 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
409 if (0
410 #ifdef CONFIG_TV_V4L2
411 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
412 #endif
413 #ifdef CONFIG_TV_DSHOW
414 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
415 #endif
417 tv_set_norm_i(tvh, tvh->tv_param->normid);
418 else
419 #endif
420 tv_set_norm(tvh,tvh->tv_param->norm);
422 #ifdef CONFIG_TV_V4L1
423 if ( tvh->tv_param->mjpeg )
425 /* set width to expected value */
426 if (tvh->tv_param->width == -1)
428 tvh->tv_param->width = 704/tvh->tv_param->decimation;
430 if (tvh->tv_param->height == -1)
432 if ( tvh->norm != TV_NORM_NTSC )
433 tvh->tv_param->height = 576/tvh->tv_param->decimation;
434 else
435 tvh->tv_param->height = 480/tvh->tv_param->decimation;
437 mp_msg(MSGT_TV, MSGL_INFO,
438 MSGTR_TV_MJP_WidthHeight, tvh->tv_param->width, tvh->tv_param->height);
440 #endif
442 /* limits on w&h are norm-dependent -- JM */
443 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
444 // first tell the driver both width and height, some drivers do not support setting them independently.
445 int dim[2];
446 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
447 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
449 /* set width */
450 if (tvh->tv_param->width != -1)
452 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
453 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
454 else
456 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetWidth, tvh->tv_param->width);
457 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
461 /* set height */
462 if (tvh->tv_param->height != -1)
464 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
465 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
466 else
468 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetHeight, tvh->tv_param->height);
469 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
473 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
475 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_NoTuner);
476 goto done;
479 /* select channel list */
480 for (i = 0; chanlists[i].name != NULL; i++)
482 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
484 tvh->chanlist = i;
485 tvh->chanlist_s = chanlists[i].list;
486 break;
490 if (tvh->chanlist == -1)
491 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnableFindChanlist,
492 tvh->tv_param->chanlist);
493 else
494 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedChanlist,
495 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
497 if (tvh->tv_param->freq && tvh->tv_param->channel)
499 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ChannelFreqParamConflict);
500 goto done;
503 /* Handle channel names */
504 if (tvh->tv_param->channels) {
505 parse_channels(tvh);
506 } else
507 tv_channel_last_real = malloc(5);
509 if (tv_channel_list) {
510 int i;
511 int channel = 0;
512 if (tvh->tv_param->channel)
514 if (isdigit(*tvh->tv_param->channel))
515 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
516 channel = atoi(tvh->tv_param->channel);
517 else
519 /* if tvh->tv_param->channel does not begin with a digit
520 set the first channel that contains tvh->tv_param->channel in its name */
522 tv_channel_current = tv_channel_list;
523 while ( tv_channel_current ) {
524 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
525 break;
526 tv_channel_current = tv_channel_current->next;
528 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
531 else
532 channel = 1;
534 if ( channel ) {
535 tv_channel_current = tv_channel_list;
536 for (i = 1; i < channel; i++)
537 if (tv_channel_current->next)
538 tv_channel_current = tv_channel_current->next;
541 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
542 tv_channel_current->name, (float)tv_channel_current->freq/1000);
543 tv_set_norm_i(tvh, tv_channel_current->norm);
544 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
545 tv_channel_last = tv_channel_current;
546 } else {
547 /* we need to set frequency */
548 if (tvh->tv_param->freq)
550 unsigned long freq = atof(tvh->tv_param->freq)*16;
552 /* set freq in MHz */
553 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
555 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
556 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedFrequency,
557 freq, (float)freq/16);
560 if (tvh->tv_param->channel) {
561 struct CHANLIST cl;
563 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_RequestedChannel, tvh->tv_param->channel);
564 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
566 cl = tvh->chanlist_s[i];
567 // printf("count%d: name: %s, freq: %d\n",
568 // i, cl.name, cl.freq);
569 if (!strcasecmp(cl.name, tvh->tv_param->channel))
571 strcpy(tv_channel_last_real, cl.name);
572 tvh->channel = i;
573 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
574 cl.name, (float)cl.freq/1000);
575 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
576 break;
582 /* grep frequency in chanlist */
584 unsigned long i2;
585 int freq;
587 tv_get_freq(tvh, &i2);
589 freq = (int) (((float)(i2/16))*1000)+250;
591 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
593 if (tvh->chanlist_s[i].freq == freq)
595 tvh->channel = i+1;
596 break;
601 done:
602 /* also start device! */
603 return 1;
606 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
608 int i;
609 tvi_handle_t* h;
610 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
611 mp_msg(MSGT_TV,MSGL_INFO,MSGTR_TV_AvailableDrivers);
612 for(i=0;tvi_driver_list[i];i++){
613 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
614 if(tvi_driver_list[i]->comment)
615 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
616 mp_msg(MSGT_TV,MSGL_INFO,"\n");
618 return NULL;
621 for(i=0;tvi_driver_list[i];i++){
622 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
623 h=tvi_driver_list[i]->tvi_init(tv_param);
624 //Requested driver initialization failed
625 if (!h && tv_param->driver)
626 return NULL;
627 //Driver initialization failed during autodetection process.
628 if (!h)
629 continue;
631 h->tv_param=tv_param;
632 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_DriverInfo, tvi_driver_list[i]->short_name,
633 tvi_driver_list[i]->name,
634 tvi_driver_list[i]->author,
635 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
636 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
637 return h;
641 if(tv_param->driver)
642 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoSuchDriver, tv_param->driver);
643 else
644 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_DriverAutoDetectionFailed);
645 return NULL;
648 static int tv_uninit(tvi_handle_t *tvh)
650 int res;
651 if(!tvh) return 1;
652 if (!tvh->priv) return 1;
653 res=tvh->functions->uninit(tvh->priv);
654 if(res) {
655 free(tvh->priv);
656 tvh->priv=NULL;
658 return res;
661 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
663 tvi_handle_t *tvh;
664 sh_video_t *sh_video;
665 sh_audio_t *sh_audio = NULL;
666 const tvi_functions_t *funcs;
668 demuxer->priv=NULL;
669 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
670 if (!tvh->functions->init(tvh->priv)) return NULL;
672 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,&(tvh->tv_param->tdevice));
674 if (!open_tv(tvh)){
675 tv_uninit(tvh);
676 return NULL;
678 funcs = tvh->functions;
679 demuxer->priv=tvh;
681 sh_video = new_sh_video(demuxer, 0);
683 /* get IMAGE FORMAT */
684 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
685 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
686 // sh_video->format = 0x0;
688 /* set FPS and FRAMETIME */
690 if(!sh_video->fps)
692 float tmp;
693 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
694 sh_video->fps = 25.0f; /* on PAL */
695 else sh_video->fps = tmp;
698 if (tvh->tv_param->fps != -1.0f)
699 sh_video->fps = tvh->tv_param->fps;
701 sh_video->frametime = 1.0f/sh_video->fps;
703 /* If playback only mode, go to immediate mode, fail silently */
704 if(tvh->tv_param->immediate == 1)
706 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
707 tvh->tv_param->noaudio = 1;
710 /* disable TV audio if -nosound is present */
711 if (!demuxer->audio || demuxer->audio->id == -2) {
712 tvh->tv_param->noaudio = 1;
715 /* set width */
716 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
718 /* set height */
719 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
721 demuxer->video->sh = sh_video;
722 sh_video->ds = demuxer->video;
723 demuxer->video->id = 0;
724 demuxer->seekable = 0;
726 /* here comes audio init */
727 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
729 int audio_format;
730 int sh_audio_format;
731 char buf[128];
733 /* yeah, audio is present */
735 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
736 &tvh->tv_param->audiorate);
738 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
739 goto no_audio;
741 switch(audio_format)
743 case AF_FORMAT_U8:
744 case AF_FORMAT_S8:
745 case AF_FORMAT_U16_LE:
746 case AF_FORMAT_U16_BE:
747 case AF_FORMAT_S16_LE:
748 case AF_FORMAT_S16_BE:
749 case AF_FORMAT_S32_LE:
750 case AF_FORMAT_S32_BE:
751 sh_audio_format = 0x1; /* PCM */
752 break;
753 case AF_FORMAT_IMA_ADPCM:
754 case AF_FORMAT_MU_LAW:
755 case AF_FORMAT_A_LAW:
756 case AF_FORMAT_MPEG2:
757 case AF_FORMAT_AC3:
758 default:
759 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnsupportedAudioType,
760 af_fmt2str(audio_format, buf, 128), audio_format);
761 goto no_audio;
764 sh_audio = new_sh_audio(demuxer, 0);
766 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
767 &sh_audio->samplerate);
768 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
769 &sh_audio->samplesize);
770 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
771 &sh_audio->channels);
773 sh_audio->format = sh_audio_format;
774 sh_audio->sample_format = audio_format;
776 sh_audio->i_bps = sh_audio->o_bps =
777 sh_audio->samplerate * sh_audio->samplesize *
778 sh_audio->channels;
780 // emulate WF for win32 codecs:
781 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
782 sh_audio->wf->wFormatTag = sh_audio->format;
783 sh_audio->wf->nChannels = sh_audio->channels;
784 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
785 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
786 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
787 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
789 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_TV_AudioFormat,
790 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
791 sh_audio->wf->nSamplesPerSec);
793 demuxer->audio->sh = sh_audio;
794 sh_audio->ds = demuxer->audio;
795 demuxer->audio->id = 0;
797 no_audio:
799 if(!(funcs->start(tvh->priv))){
800 // start failed :(
801 tv_uninit(tvh);
802 return NULL;
805 /* set color eq */
806 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
807 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
808 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
809 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
811 if(tvh->tv_param->gain!=-1)
812 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
813 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
815 funcs->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
817 return demuxer;
820 static void demux_close_tv(demuxer_t *demuxer)
822 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
823 if (!tvh) return;
824 tv_uninit(tvh);
825 free(tvh);
826 demuxer->priv=NULL;
829 /* utilities for mplayer (not mencoder!!) */
830 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
832 const tvi_functions_t *funcs = tvh->functions;
834 switch(opt)
836 case TV_COLOR_BRIGHTNESS:
837 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
838 case TV_COLOR_HUE:
839 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
840 case TV_COLOR_SATURATION:
841 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
842 case TV_COLOR_CONTRAST:
843 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
844 default:
845 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
848 return TVI_CONTROL_UNKNOWN;
851 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
853 const tvi_functions_t *funcs = tvh->functions;
855 switch(opt)
857 case TV_COLOR_BRIGHTNESS:
858 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
859 case TV_COLOR_HUE:
860 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
861 case TV_COLOR_SATURATION:
862 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
863 case TV_COLOR_CONTRAST:
864 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
865 default:
866 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
869 return TVI_CONTROL_UNKNOWN;
872 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
874 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
876 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
877 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
878 *freq, (float)*freq/16);
880 return 1;
883 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
885 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
887 // unsigned long freq = atof(tvh->tv_param->freq)*16;
889 /* set freq in MHz */
890 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
892 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
893 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
894 freq, (float)freq/16);
896 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
897 return 1;
900 int tv_get_signal(tvi_handle_t *tvh)
902 int signal=0;
903 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
904 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
905 return 0;
907 return signal;
910 /*****************************************************************
911 * \brief tune current frequency by step_interval value
912 * \parameter step_interval increment value in 1/16 MHz
913 * \note frequency is rounded to 1/16 MHz value
914 * \return 1
917 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
918 unsigned long frequency;
920 tvh->tv_param->scan=0;
921 tv_get_freq(tvh,&frequency);
922 frequency+=step_interval;
923 return tv_set_freq(tvh,frequency);
926 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
928 struct CHANLIST cl;
930 tvh->tv_param->scan=0;
931 if (direction == TV_CHANNEL_LOWER)
933 if (tvh->channel-1 >= 0)
935 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
936 cl = tvh->chanlist_s[--tvh->channel];
937 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
938 cl.name, (float)cl.freq/1000);
939 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
943 if (direction == TV_CHANNEL_HIGHER)
945 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
947 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
948 cl = tvh->chanlist_s[++tvh->channel];
949 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
950 cl.name, (float)cl.freq/1000);
951 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
954 return 1;
957 int tv_step_channel(tvi_handle_t *tvh, int direction) {
958 tvh->tv_param->scan=0;
959 if (tv_channel_list) {
960 if (direction == TV_CHANNEL_HIGHER) {
961 tv_channel_last = tv_channel_current;
962 if (tv_channel_current->next)
963 tv_channel_current = tv_channel_current->next;
964 else
965 tv_channel_current = tv_channel_list;
967 tv_set_norm_i(tvh, tv_channel_current->norm);
968 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
969 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
970 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
972 if (direction == TV_CHANNEL_LOWER) {
973 tv_channel_last = tv_channel_current;
974 if (tv_channel_current->prev)
975 tv_channel_current = tv_channel_current->prev;
976 else
977 while (tv_channel_current->next)
978 tv_channel_current = tv_channel_current->next;
979 tv_set_norm_i(tvh, tv_channel_current->norm);
980 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
981 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
982 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
984 } else tv_step_channel_real(tvh, direction);
985 return 1;
988 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
989 int i;
990 struct CHANLIST cl;
992 tvh->tv_param->scan=0;
993 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
994 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
996 cl = tvh->chanlist_s[i];
997 // printf("count%d: name: %s, freq: %d\n",
998 // i, cl.name, cl.freq);
999 if (!strcasecmp(cl.name, channel))
1001 tvh->channel = i;
1002 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1003 cl.name, (float)cl.freq/1000);
1004 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1005 break;
1008 return 1;
1011 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1012 int i, channel_int;
1014 tvh->tv_param->scan=0;
1015 if (tv_channel_list) {
1016 tv_channel_last = tv_channel_current;
1017 channel_int = atoi(channel);
1018 tv_channel_current = tv_channel_list;
1019 for (i = 1; i < channel_int; i++)
1020 if (tv_channel_current->next)
1021 tv_channel_current = tv_channel_current->next;
1022 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1023 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1024 tv_set_norm_i(tvh, tv_channel_current->norm);
1025 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1026 } else tv_set_channel_real(tvh, channel);
1027 return 1;
1030 int tv_last_channel(tvi_handle_t *tvh) {
1032 tvh->tv_param->scan=0;
1033 if (tv_channel_list) {
1034 tv_channels_t *tmp;
1036 tmp = tv_channel_last;
1037 tv_channel_last = tv_channel_current;
1038 tv_channel_current = tmp;
1040 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1041 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1042 tv_set_norm_i(tvh, tv_channel_current->norm);
1043 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1044 } else {
1045 int i;
1046 struct CHANLIST cl;
1048 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1050 cl = tvh->chanlist_s[i];
1051 if (!strcasecmp(cl.name, tv_channel_last_real))
1053 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1054 tvh->channel = i;
1055 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1056 cl.name, (float)cl.freq/1000);
1057 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1058 break;
1062 return 1;
1065 int tv_step_norm(tvi_handle_t *tvh)
1067 tvh->norm++;
1068 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1069 &tvh->norm) != TVI_CONTROL_TRUE) {
1070 tvh->norm = 0;
1071 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1072 &tvh->norm) != TVI_CONTROL_TRUE) {
1073 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1074 return 0;
1077 tvh->functions->control(tvh->priv,TV_VBI_CONTROL_RESET,tvh->tv_param);
1078 return 1;
1081 int tv_step_chanlist(tvi_handle_t *tvh)
1083 return 1;
1086 demuxer_desc_t demuxer_desc_tv = {
1087 "Tv card demuxer",
1088 "tv",
1089 "TV",
1090 "Alex Beregszaszi, Charles R. Henrich",
1091 "?",
1092 DEMUXER_TYPE_TV,
1093 0, // no autodetect
1094 NULL,
1095 demux_tv_fill_buffer,
1096 demux_open_tv,
1097 demux_close_tv,
1098 NULL,
1099 NULL