2 * TV Interface for MPlayer
4 * API idea based on libvo2
6 * Copyright (C) 2001 Alex Beregszaszi
8 * Feb 19, 2002: Significant rewrites by Charles R. Henrich (henrich@msu.edu)
9 * to add support for audio, and bktr *BSD support.
11 * This file is part of MPlayer.
13 * MPlayer is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * MPlayer is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
41 #include "libmpdemux/demuxer.h"
42 #include "libmpdemux/stheader.h"
44 #include "libaf/af_format.h"
45 #include "libmpcodecs/img_format.h"
46 #include "libmpcodecs/dec_teletext.h"
47 #include "libavutil/avstring.h"
48 #include "osdep/timer.h"
52 #include "frequencies.h"
54 tv_channels_t
*tv_channel_list
;
55 tv_channels_t
*tv_channel_current
, *tv_channel_last
;
56 char *tv_channel_last_real
;
58 /* enumerating drivers (like in stream.c) */
59 extern const tvi_info_t tvi_info_dummy
;
60 extern const tvi_info_t tvi_info_dshow
;
61 extern const tvi_info_t tvi_info_v4l
;
62 extern const tvi_info_t tvi_info_v4l2
;
63 extern const tvi_info_t tvi_info_bsdbt848
;
65 /** List of drivers in autodetection order */
66 static const tvi_info_t
* tvi_driver_list
[]={
73 #ifdef CONFIG_TV_BSDBT848
76 #ifdef CONFIG_TV_DSHOW
83 void tv_start_scan(tvi_handle_t
*tvh
, int start
)
85 mp_msg(MSGT_TV
,MSGL_INFO
,"start scan\n");
86 tvh
->tv_param
->scan
=start
?1:0;
89 static void tv_scan(tvi_handle_t
*tvh
)
93 tv_channels_t
*tv_channel_tmp
=NULL
;
94 tv_channels_t
*tv_channel_add
=NULL
;
98 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
99 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
101 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Channel scanner is not available without tuner\n");
102 tvh
->tv_param
->scan
=0;
109 scan
=calloc(1,sizeof(tv_scan_t
));
111 cl
= tvh
->chanlist_s
[scan
->channel_num
];
112 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
113 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
115 if(scan
->scan_timer
>now
)
118 if (tv_get_signal(tvh
)>tvh
->tv_param
->scan_threshold
) {
119 cl
= tvh
->chanlist_s
[scan
->channel_num
];
120 tv_channel_tmp
=tv_channel_list
;
121 while (tv_channel_tmp
) {
123 if (cl
.freq
==tv_channel_tmp
->freq
){
127 tv_channel_add
=tv_channel_tmp
;
128 tv_channel_tmp
=tv_channel_tmp
->next
;
131 mp_msg(MSGT_TV
, MSGL_INFO
, "Found new channel: %s (#%d). \n",cl
.name
,index
);
132 scan
->new_channels
++;
133 tv_channel_tmp
= malloc(sizeof(tv_channels_t
));
134 tv_channel_tmp
->index
=index
;
135 tv_channel_tmp
->next
=NULL
;
136 tv_channel_tmp
->prev
=tv_channel_add
;
137 tv_channel_tmp
->freq
=cl
.freq
;
138 snprintf(tv_channel_tmp
->name
,sizeof(tv_channel_tmp
->name
),"ch%d",index
);
139 strncpy(tv_channel_tmp
->number
, cl
.name
, 5);
140 tv_channel_tmp
->number
[4]='\0';
141 if (!tv_channel_list
)
142 tv_channel_list
=tv_channel_tmp
;
144 tv_channel_add
->next
=tv_channel_tmp
;
145 tv_channel_list
->prev
=tv_channel_tmp
;
148 mp_msg(MSGT_TV
, MSGL_INFO
, "Found existing channel: %s-%s.\n",
149 tv_channel_tmp
->number
,tv_channel_tmp
->name
);
152 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
153 if (scan
->channel_num
>=chanlists
[tvh
->chanlist
].count
) {
154 tvh
->tv_param
->scan
=0;
155 mp_msg(MSGT_TV
, MSGL_INFO
, "TV scan end. Found %d new channels.\n", scan
->new_channels
);
156 tv_channel_tmp
=tv_channel_list
;
158 mp_msg(MSGT_TV
,MSGL_INFO
,"channels=");
159 while(tv_channel_tmp
){
160 mp_msg(MSGT_TV
,MSGL_INFO
,"%s-%s",tv_channel_tmp
->number
,tv_channel_tmp
->name
);
161 if(tv_channel_tmp
->next
)
162 mp_msg(MSGT_TV
,MSGL_INFO
,",");
163 tv_channel_tmp
=tv_channel_tmp
->next
;
165 mp_msg(MSGT_TV
, MSGL_INFO
, "\n");
167 if (!tv_channel_current
) tv_channel_current
=tv_channel_list
;
168 if (tv_channel_current
)
169 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
173 cl
= tvh
->chanlist_s
[scan
->channel_num
];
174 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
175 mp_msg(MSGT_TV
, MSGL_INFO
, "Trying: %s (%.2f). \n",cl
.name
,1e-3*cl
.freq
);
179 /* ================== DEMUX_TV ===================== */
182 0 = EOF(?) or no stream
183 1 = successfully read a packet
185 /* fill demux->video and demux->audio */
187 static int demux_tv_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
189 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demux
->priv
);
193 /* ================== ADD AUDIO PACKET =================== */
195 if (ds
==demux
->audio
&& tvh
->tv_param
->noaudio
== 0 &&
196 tvh
->functions
->control(tvh
->priv
,
197 TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
199 len
= tvh
->functions
->get_audio_framesize(tvh
->priv
);
201 dp
=new_demux_packet(len
);
202 dp
->flags
|=1; /* Keyframe */
203 dp
->pts
=tvh
->functions
->grab_audio_frame(tvh
->priv
, dp
->buffer
,len
);
204 ds_add_packet(demux
->audio
,dp
);
207 /* ================== ADD VIDEO PACKET =================== */
209 if (ds
==demux
->video
&& tvh
->functions
->control(tvh
->priv
,
210 TVI_CONTROL_IS_VIDEO
, 0) == TVI_CONTROL_TRUE
)
212 len
= tvh
->functions
->get_video_framesize(tvh
->priv
);
213 dp
=new_demux_packet(len
);
214 dp
->flags
|=1; /* Keyframe */
215 dp
->pts
=tvh
->functions
->grab_video_frame(tvh
->priv
, dp
->buffer
, len
);
216 ds_add_packet(demux
->video
,dp
);
219 if (tvh
->tv_param
->scan
) tv_scan(tvh
);
223 static int norm_from_string(tvi_handle_t
*tvh
, char* norm
)
225 const tvi_functions_t
*funcs
= tvh
->functions
;
229 strncpy(str
, norm
, sizeof(str
)-1);
230 str
[sizeof(str
)-1] = '\0';
231 ret
=funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_GET_NORMID
, str
);
233 if(ret
==TVI_CONTROL_TRUE
)
236 if(ret
!=TVI_CONTROL_UNKNOWN
)
238 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
,"default");
242 if (!strcasecmp(norm
, "pal"))
244 else if (!strcasecmp(norm
, "ntsc"))
246 else if (!strcasecmp(norm
, "secam"))
247 return TV_NORM_SECAM
;
248 else if (!strcasecmp(norm
, "palnc"))
249 return TV_NORM_PALNC
;
250 else if (!strcasecmp(norm
, "palm"))
252 else if (!strcasecmp(norm
, "paln"))
254 else if (!strcasecmp(norm
, "ntscjp"))
255 return TV_NORM_NTSCJP
;
257 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
, "PAL");
262 static void parse_channels(tvi_handle_t
*tvh
)
264 char** channels
= tvh
->tv_param
->channels
;
266 mp_tmsg(MSGT_TV
, MSGL_INFO
, "TV channel names detected.\n");
267 tv_channel_list
= malloc(sizeof(tv_channels_t
));
268 tv_channel_list
->index
=1;
269 tv_channel_list
->next
=NULL
;
270 tv_channel_list
->prev
=NULL
;
271 tv_channel_current
= tv_channel_list
;
272 tv_channel_current
->norm
= tvh
->norm
;
275 char* tmp
= *(channels
++);
276 char* sep
= strchr(tmp
,'-');
280 if (!sep
) continue; // Wrong syntax, but mplayer should not crash
282 av_strlcpy(tv_channel_current
->name
, sep
+ 1,
283 sizeof(tv_channel_current
->name
));
285 strncpy(tv_channel_current
->number
, tmp
, 5);
286 tv_channel_current
->number
[4]='\0';
288 while ((sep
=strchr(tv_channel_current
->name
, '_')))
291 // if channel number is a number and larger than 1000 threat it as frequency
292 // tmp still contain pointer to null-terminated string with channel number here
294 tv_channel_current
->freq
=atoi(tmp
);
296 tv_channel_current
->freq
= 0;
297 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++) {
298 cl
= tvh
->chanlist_s
[i
];
299 if (!strcasecmp(cl
.name
, tv_channel_current
->number
)) {
300 tv_channel_current
->freq
=cl
.freq
;
305 if (tv_channel_current
->freq
== 0)
306 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Couldn't find frequency for channel %s (%s)\n",
307 tv_channel_current
->number
, tv_channel_current
->name
);
309 sep
= strchr(tv_channel_current
->name
, '-');
310 if ( !sep
) sep
= strchr(tv_channel_current
->name
, '+');
314 if ( sep
[0] == '+' ) tv_channel_current
->freq
+= i
* 100;
315 if ( sep
[0] == '-' ) tv_channel_current
->freq
-= i
* 100;
319 sep
= strchr(tv_channel_current
->name
, '=');
321 tv_channel_current
->norm
= norm_from_string(tvh
, sep
+1);
326 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
327 tv_channel_current->number, tv_channel_current->name,
328 (float)tv_channel_current->freq/1000);*/
330 tv_channel_current
->next
= malloc(sizeof(tv_channels_t
));
331 tv_channel_current
->next
->index
= tv_channel_current
->index
+ 1;
332 tv_channel_current
->next
->prev
= tv_channel_current
;
333 tv_channel_current
->next
->next
= NULL
;
334 tv_channel_current
= tv_channel_current
->next
;
335 tv_channel_current
->norm
= tvh
->norm
;
337 if (tv_channel_current
->prev
)
338 tv_channel_current
->prev
->next
= NULL
;
339 free(tv_channel_current
);
342 int tv_set_norm(tvi_handle_t
*tvh
, char* norm
)
344 tvh
->norm
= norm_from_string(tvh
, norm
);
346 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm : %s\n", norm
);
347 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
348 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
351 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
352 &tvh
->tv_param
->teletext
);
356 static int tv_set_norm_i(tvi_handle_t
*tvh
, int norm
)
360 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm id: %d\n", norm
);
361 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
362 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
366 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
367 &tvh
->tv_param
->teletext
);
371 static int open_tv(tvi_handle_t
*tvh
)
374 const tvi_functions_t
*funcs
= tvh
->functions
;
375 int tv_fmt_list
[] = {
386 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_VIDEO
, 0) != TVI_CONTROL_TRUE
)
388 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: No video input present!\n");
392 if (tvh
->tv_param
->outfmt
== -1)
393 for (i
= 0; i
< sizeof (tv_fmt_list
) / sizeof (*tv_fmt_list
); i
++)
395 tvh
->tv_param
->outfmt
= tv_fmt_list
[i
];
396 if (funcs
->control (tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
,
397 &tvh
->tv_param
->outfmt
) == TVI_CONTROL_TRUE
)
402 switch(tvh
->tv_param
->outfmt
)
416 mp_tmsg(MSGT_TV
, MSGL_ERR
,
417 "==================================================================\n"\
418 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
419 " This may cause buggy playback or program crash! Bug reports will\n"\
420 " be ignored! You should try again with YV12 (which is the default\n"\
421 " colorspace) and read the documentation!\n"\
422 "==================================================================\n"
423 ,tvh
->tv_param
->outfmt
);
425 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
, &tvh
->tv_param
->outfmt
);
428 /* set some params got from cmdline */
429 funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_SET_INPUT
, &tvh
->tv_param
->input
);
431 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
433 #ifdef CONFIG_TV_V4L2
434 || (!strcmp(tvh
->tv_param
->driver
, "v4l2") && tvh
->tv_param
->normid
>= 0)
436 #ifdef CONFIG_TV_DSHOW
437 || (!strcmp(tvh
->tv_param
->driver
, "dshow") && tvh
->tv_param
->normid
>= 0)
440 tv_set_norm_i(tvh
, tvh
->tv_param
->normid
);
443 tv_set_norm(tvh
,tvh
->tv_param
->norm
);
445 #ifdef CONFIG_TV_V4L1
446 if ( tvh
->tv_param
->mjpeg
)
448 /* set width to expected value */
449 if (tvh
->tv_param
->width
== -1)
451 tvh
->tv_param
->width
= 704/tvh
->tv_param
->decimation
;
453 if (tvh
->tv_param
->height
== -1)
455 if ( tvh
->norm
!= TV_NORM_NTSC
)
456 tvh
->tv_param
->height
= 576/tvh
->tv_param
->decimation
;
458 tvh
->tv_param
->height
= 480/tvh
->tv_param
->decimation
;
460 mp_tmsg(MSGT_TV
, MSGL_INFO
,
461 " MJP: width %d height %d\n", tvh
->tv_param
->width
, tvh
->tv_param
->height
);
465 /* limits on w&h are norm-dependent -- JM */
466 if (tvh
->tv_param
->width
!= -1 && tvh
->tv_param
->height
!= -1) {
467 // first tell the driver both width and height, some drivers do not support setting them independently.
469 dim
[0] = tvh
->tv_param
->width
; dim
[1] = tvh
->tv_param
->height
;
470 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH_HEIGHT
, dim
);
473 if (tvh
->tv_param
->width
!= -1)
475 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_WIDTH
, &tvh
->tv_param
->width
) == TVI_CONTROL_TRUE
)
476 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH
, &tvh
->tv_param
->width
);
479 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested width: %d\n", tvh
->tv_param
->width
);
480 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &tvh
->tv_param
->width
);
485 if (tvh
->tv_param
->height
!= -1)
487 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_HEIGHT
, &tvh
->tv_param
->height
) == TVI_CONTROL_TRUE
)
488 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HEIGHT
, &tvh
->tv_param
->height
);
491 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested height: %d\n", tvh
->tv_param
->height
);
492 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &tvh
->tv_param
->height
);
496 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
498 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Selected input hasn't got a tuner!\n");
502 /* select channel list */
503 for (i
= 0; chanlists
[i
].name
!= NULL
; i
++)
505 if (!strcasecmp(chanlists
[i
].name
, tvh
->tv_param
->chanlist
))
508 tvh
->chanlist_s
= chanlists
[i
].list
;
513 if (tvh
->chanlist
== -1)
514 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unable to find selected channel list! (%s)\n",
515 tvh
->tv_param
->chanlist
);
517 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected channel list: %s (including %d channels)\n",
518 chanlists
[tvh
->chanlist
].name
, chanlists
[tvh
->chanlist
].count
);
520 if (tvh
->tv_param
->freq
&& tvh
->tv_param
->channel
)
522 mp_tmsg(MSGT_TV
, MSGL_WARN
, "You can't set frequency and channel simultaneously!\n");
526 /* Handle channel names */
527 if (tvh
->tv_param
->channels
) {
530 tv_channel_last_real
= malloc(5);
532 if (tv_channel_list
) {
535 if (tvh
->tv_param
->channel
)
537 if (isdigit(*tvh
->tv_param
->channel
))
538 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
539 channel
= atoi(tvh
->tv_param
->channel
);
542 /* if tvh->tv_param->channel does not begin with a digit
543 set the first channel that contains tvh->tv_param->channel in its name */
545 tv_channel_current
= tv_channel_list
;
546 while ( tv_channel_current
) {
547 if ( strstr(tv_channel_current
->name
, tvh
->tv_param
->channel
) )
549 tv_channel_current
= tv_channel_current
->next
;
551 if ( !tv_channel_current
) tv_channel_current
= tv_channel_list
;
558 tv_channel_current
= tv_channel_list
;
559 for (i
= 1; i
< channel
; i
++)
560 if (tv_channel_current
->next
)
561 tv_channel_current
= tv_channel_current
->next
;
564 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
565 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
566 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
567 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
568 tv_channel_last
= tv_channel_current
;
570 /* we need to set frequency */
571 if (tvh
->tv_param
->freq
)
573 unsigned long freq
= atof(tvh
->tv_param
->freq
)*16;
575 /* set freq in MHz */
576 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
578 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
579 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected frequency: %lu (%.3f)\n",
580 freq
, (float)freq
/16);
583 if (tvh
->tv_param
->channel
) {
586 mp_tmsg(MSGT_TV
, MSGL_V
, "Requested channel: %s\n", tvh
->tv_param
->channel
);
587 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
589 cl
= tvh
->chanlist_s
[i
];
590 // printf("count%d: name: %s, freq: %d\n",
591 // i, cl.name, cl.freq);
592 if (!strcasecmp(cl
.name
, tvh
->tv_param
->channel
))
594 strcpy(tv_channel_last_real
, cl
.name
);
596 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
597 cl
.name
, (float)cl
.freq
/1000);
598 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
605 /* grep frequency in chanlist */
610 tv_get_freq(tvh
, &i2
);
612 freq
= (int) (((float)(i2
/16))*1000)+250;
614 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
616 if (tvh
->chanlist_s
[i
].freq
== freq
)
625 /* also start device! */
629 static tvi_handle_t
*tv_begin(tv_param_t
* tv_param
)
633 if(tv_param
->driver
&& !strcmp(tv_param
->driver
,"help")){
634 mp_tmsg(MSGT_TV
,MSGL_INFO
,"Available drivers:\n");
635 for(i
=0;tvi_driver_list
[i
];i
++){
636 mp_msg(MSGT_TV
,MSGL_INFO
," %s\t%s",tvi_driver_list
[i
]->short_name
,tvi_driver_list
[i
]->name
);
637 if(tvi_driver_list
[i
]->comment
)
638 mp_msg(MSGT_TV
,MSGL_INFO
," (%s)",tvi_driver_list
[i
]->comment
);
639 mp_msg(MSGT_TV
,MSGL_INFO
,"\n");
644 for(i
=0;tvi_driver_list
[i
];i
++){
645 if (!tv_param
->driver
|| !strcmp(tvi_driver_list
[i
]->short_name
, tv_param
->driver
)){
646 h
=tvi_driver_list
[i
]->tvi_init(tv_param
);
647 //Requested driver initialization failed
648 if (!h
&& tv_param
->driver
)
650 //Driver initialization failed during autodetection process.
654 h
->tv_param
=tv_param
;
655 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected driver: %s\n name: %s\n author: %s\n comment: %s\n", tvi_driver_list
[i
]->short_name
,
656 tvi_driver_list
[i
]->name
,
657 tvi_driver_list
[i
]->author
,
658 tvi_driver_list
[i
]->comment
?tvi_driver_list
[i
]->comment
:"");
659 tv_param
->driver
=strdup(tvi_driver_list
[i
]->short_name
);
665 mp_tmsg(MSGT_TV
, MSGL_ERR
, "No such driver: %s\n", tv_param
->driver
);
667 mp_tmsg(MSGT_TV
, MSGL_ERR
, "TV driver autodetection failed.\n");
671 static int tv_uninit(tvi_handle_t
*tvh
)
675 if (!tvh
->priv
) return 1;
676 res
=tvh
->functions
->uninit(tvh
->priv
);
684 static demuxer_t
* demux_open_tv(demuxer_t
*demuxer
)
687 sh_video_t
*sh_video
;
688 sh_audio_t
*sh_audio
= NULL
;
689 const tvi_functions_t
*funcs
;
692 if(!(tvh
=tv_begin(demuxer
->stream
->priv
))) return NULL
;
693 if (!tvh
->functions
->init(tvh
->priv
)) return NULL
;
695 tvh
->demuxer
= demuxer
;
696 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_VBI_INIT
,
697 &(tvh
->tv_param
->teletext
.device
));
698 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_GET_VBI_PTR
,
705 funcs
= tvh
->functions
;
708 sh_video
= new_sh_video(demuxer
, 0);
710 /* get IMAGE FORMAT */
711 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FORMAT
, &sh_video
->format
);
712 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
713 // sh_video->format = 0x0;
715 /* set FPS and FRAMETIME */
720 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FPS
, &tmp
) != TVI_CONTROL_TRUE
)
721 sh_video
->fps
= 25.0f
; /* on PAL */
722 else sh_video
->fps
= tmp
;
725 if (tvh
->tv_param
->fps
!= -1.0f
)
726 sh_video
->fps
= tvh
->tv_param
->fps
;
728 sh_video
->frametime
= 1.0f
/sh_video
->fps
;
730 /* If playback only mode, go to immediate mode, fail silently */
731 if(tvh
->tv_param
->immediate
== 1)
733 funcs
->control(tvh
->priv
, TVI_CONTROL_IMMEDIATE
, 0);
734 tvh
->tv_param
->noaudio
= 1;
737 /* disable TV audio if -nosound is present */
738 if (!demuxer
->audio
|| demuxer
->audio
->id
== -2) {
739 tvh
->tv_param
->noaudio
= 1;
743 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &sh_video
->disp_w
);
746 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &sh_video
->disp_h
);
748 demuxer
->video
->sh
= sh_video
;
749 sh_video
->ds
= demuxer
->video
;
750 demuxer
->video
->id
= 0;
751 demuxer
->seekable
= 0;
753 /* here comes audio init */
754 if (tvh
->tv_param
->noaudio
== 0 && funcs
->control(tvh
->priv
, TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
760 /* yeah, audio is present */
762 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_SET_SAMPLERATE
,
763 &tvh
->tv_param
->audiorate
);
765 if (funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_FORMAT
, &audio_format
) != TVI_CONTROL_TRUE
)
772 case AF_FORMAT_U16_LE
:
773 case AF_FORMAT_U16_BE
:
774 case AF_FORMAT_S16_LE
:
775 case AF_FORMAT_S16_BE
:
776 case AF_FORMAT_S32_LE
:
777 case AF_FORMAT_S32_BE
:
778 sh_audio_format
= 0x1; /* PCM */
780 case AF_FORMAT_IMA_ADPCM
:
781 case AF_FORMAT_MU_LAW
:
782 case AF_FORMAT_A_LAW
:
783 case AF_FORMAT_MPEG2
:
785 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Audio type '%s (%x)' unsupported!\n",
786 af_fmt2str(audio_format
, buf
, 128), audio_format
);
790 sh_audio
= new_sh_audio(demuxer
, 0);
792 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLERATE
,
793 &sh_audio
->samplerate
);
794 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLESIZE
,
795 &sh_audio
->samplesize
);
796 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_CHANNELS
,
797 &sh_audio
->channels
);
799 sh_audio
->format
= sh_audio_format
;
800 sh_audio
->sample_format
= audio_format
;
802 sh_audio
->i_bps
= sh_audio
->o_bps
=
803 sh_audio
->samplerate
* sh_audio
->samplesize
*
806 // emulate WF for win32 codecs:
807 sh_audio
->wf
= malloc(sizeof(WAVEFORMATEX
));
808 sh_audio
->wf
->wFormatTag
= sh_audio
->format
;
809 sh_audio
->wf
->nChannels
= sh_audio
->channels
;
810 sh_audio
->wf
->wBitsPerSample
= sh_audio
->samplesize
* 8;
811 sh_audio
->wf
->nSamplesPerSec
= sh_audio
->samplerate
;
812 sh_audio
->wf
->nBlockAlign
= sh_audio
->samplesize
* sh_audio
->channels
;
813 sh_audio
->wf
->nAvgBytesPerSec
= sh_audio
->i_bps
;
815 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, " TV audio: %d channels, %d bits, %d Hz\n",
816 sh_audio
->wf
->nChannels
, sh_audio
->wf
->wBitsPerSample
,
817 sh_audio
->wf
->nSamplesPerSec
);
819 demuxer
->audio
->sh
= sh_audio
;
820 sh_audio
->ds
= demuxer
->audio
;
821 demuxer
->audio
->id
= 0;
825 if(!(funcs
->start(tvh
->priv
))){
832 tv_set_color_options(tvh
, TV_COLOR_BRIGHTNESS
, tvh
->tv_param
->brightness
);
833 tv_set_color_options(tvh
, TV_COLOR_HUE
, tvh
->tv_param
->hue
);
834 tv_set_color_options(tvh
, TV_COLOR_SATURATION
, tvh
->tv_param
->saturation
);
835 tv_set_color_options(tvh
, TV_COLOR_CONTRAST
, tvh
->tv_param
->contrast
);
837 if(tvh
->tv_param
->gain
!=-1)
838 if(funcs
->control(tvh
->priv
,TVI_CONTROL_VID_SET_GAIN
,&tvh
->tv_param
->gain
)!=TVI_CONTROL_TRUE
)
839 mp_msg(MSGT_TV
,MSGL_WARN
,"Unable to set gain control!\n");
841 teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
842 &tvh
->tv_param
->teletext
);
847 static void demux_close_tv(demuxer_t
*demuxer
)
849 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demuxer
->priv
);
854 demuxer
->teletext
=NULL
;
857 /* utilities for mplayer (not mencoder!!) */
858 int tv_set_color_options(tvi_handle_t
*tvh
, int opt
, int value
)
860 const tvi_functions_t
*funcs
= tvh
->functions
;
864 case TV_COLOR_BRIGHTNESS
:
865 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_BRIGHTNESS
, &value
);
867 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HUE
, &value
);
868 case TV_COLOR_SATURATION
:
869 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_SATURATION
, &value
);
870 case TV_COLOR_CONTRAST
:
871 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_CONTRAST
, &value
);
873 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
876 return TVI_CONTROL_UNKNOWN
;
879 int tv_get_color_options(tvi_handle_t
*tvh
, int opt
, int* value
)
881 const tvi_functions_t
*funcs
= tvh
->functions
;
885 case TV_COLOR_BRIGHTNESS
:
886 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_BRIGHTNESS
, value
);
888 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HUE
, value
);
889 case TV_COLOR_SATURATION
:
890 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_SATURATION
, value
);
891 case TV_COLOR_CONTRAST
:
892 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_CONTRAST
, value
);
894 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
897 return TVI_CONTROL_UNKNOWN
;
900 int tv_get_freq(tvi_handle_t
*tvh
, unsigned long *freq
)
902 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
904 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, freq
);
905 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
906 *freq
, (float)*freq
/16);
911 int tv_set_freq(tvi_handle_t
*tvh
, unsigned long freq
)
913 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
915 // unsigned long freq = atof(tvh->tv_param->freq)*16;
917 /* set freq in MHz */
918 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
920 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
921 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
922 freq
, (float)freq
/16);
924 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
925 &tvh
->tv_param
->teletext
);
929 int tv_get_signal(tvi_handle_t
*tvh
)
932 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
||
933 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_SIGNAL
, &signal
)!=TVI_CONTROL_TRUE
)
939 /*****************************************************************
940 * \brief tune current frequency by step_interval value
941 * \parameter step_interval increment value in 1/16 MHz
942 * \note frequency is rounded to 1/16 MHz value
946 int tv_step_freq(tvi_handle_t
* tvh
, float step_interval
){
947 unsigned long frequency
;
949 tvh
->tv_param
->scan
=0;
950 tv_get_freq(tvh
,&frequency
);
951 frequency
+=step_interval
;
952 return tv_set_freq(tvh
,frequency
);
955 int tv_step_channel_real(tvi_handle_t
*tvh
, int direction
)
959 tvh
->tv_param
->scan
=0;
960 if (direction
== TV_CHANNEL_LOWER
)
962 if (tvh
->channel
-1 >= 0)
964 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
965 cl
= tvh
->chanlist_s
[--tvh
->channel
];
966 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
967 cl
.name
, (float)cl
.freq
/1000);
968 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
972 if (direction
== TV_CHANNEL_HIGHER
)
974 if (tvh
->channel
+1 < chanlists
[tvh
->chanlist
].count
)
976 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
977 cl
= tvh
->chanlist_s
[++tvh
->channel
];
978 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
979 cl
.name
, (float)cl
.freq
/1000);
980 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
986 int tv_step_channel(tvi_handle_t
*tvh
, int direction
) {
987 tvh
->tv_param
->scan
=0;
988 if (tv_channel_list
) {
989 if (direction
== TV_CHANNEL_HIGHER
) {
990 tv_channel_last
= tv_channel_current
;
991 if (tv_channel_current
->next
)
992 tv_channel_current
= tv_channel_current
->next
;
994 tv_channel_current
= tv_channel_list
;
996 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
997 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
998 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
999 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1001 if (direction
== TV_CHANNEL_LOWER
) {
1002 tv_channel_last
= tv_channel_current
;
1003 if (tv_channel_current
->prev
)
1004 tv_channel_current
= tv_channel_current
->prev
;
1006 while (tv_channel_current
->next
)
1007 tv_channel_current
= tv_channel_current
->next
;
1008 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1009 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1010 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
1011 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1013 } else tv_step_channel_real(tvh
, direction
);
1017 int tv_set_channel_real(tvi_handle_t
*tvh
, char *channel
) {
1021 tvh
->tv_param
->scan
=0;
1022 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1023 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1025 cl
= tvh
->chanlist_s
[i
];
1026 // printf("count%d: name: %s, freq: %d\n",
1027 // i, cl.name, cl.freq);
1028 if (!strcasecmp(cl
.name
, channel
))
1031 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1032 cl
.name
, (float)cl
.freq
/1000);
1033 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1040 int tv_set_channel(tvi_handle_t
*tvh
, char *channel
) {
1043 tvh
->tv_param
->scan
=0;
1044 if (tv_channel_list
) {
1045 tv_channel_last
= tv_channel_current
;
1046 channel_int
= atoi(channel
);
1047 tv_channel_current
= tv_channel_list
;
1048 for (i
= 1; i
< channel_int
; i
++)
1049 if (tv_channel_current
->next
)
1050 tv_channel_current
= tv_channel_current
->next
;
1051 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1052 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1053 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1054 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1055 } else tv_set_channel_real(tvh
, channel
);
1059 int tv_last_channel(tvi_handle_t
*tvh
) {
1061 tvh
->tv_param
->scan
=0;
1062 if (tv_channel_list
) {
1065 tmp
= tv_channel_last
;
1066 tv_channel_last
= tv_channel_current
;
1067 tv_channel_current
= tmp
;
1069 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1070 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1071 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1072 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1077 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1079 cl
= tvh
->chanlist_s
[i
];
1080 if (!strcasecmp(cl
.name
, tv_channel_last_real
))
1082 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1084 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1085 cl
.name
, (float)cl
.freq
/1000);
1086 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1094 int tv_step_norm(tvi_handle_t
*tvh
)
1097 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1098 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1100 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1101 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1102 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
1106 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
1107 &tvh
->tv_param
->teletext
);
1111 int tv_step_chanlist(tvi_handle_t
*tvh
)
1116 demuxer_desc_t demuxer_desc_tv
= {
1120 "Alex Beregszaszi, Charles R. Henrich",
1125 demux_tv_fill_buffer
,