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.
42 #include "libmpdemux/demuxer.h"
43 #include "libmpdemux/stheader.h"
45 #include "libaf/af_format.h"
46 #include "libmpcodecs/img_format.h"
47 #include "libmpcodecs/dec_teletext.h"
48 #include "libavutil/avstring.h"
49 #include "osdep/timer.h"
53 #include "frequencies.h"
55 tv_channels_t
*tv_channel_list
;
56 tv_channels_t
*tv_channel_current
, *tv_channel_last
;
57 char *tv_channel_last_real
;
59 /* enumerating drivers (like in stream.c) */
60 extern const tvi_info_t tvi_info_dummy
;
61 extern const tvi_info_t tvi_info_dshow
;
62 extern const tvi_info_t tvi_info_v4l
;
63 extern const tvi_info_t tvi_info_v4l2
;
64 extern const tvi_info_t tvi_info_bsdbt848
;
66 /** List of drivers in autodetection order */
67 static const tvi_info_t
* tvi_driver_list
[]={
74 #ifdef CONFIG_TV_BSDBT848
77 #ifdef CONFIG_TV_DSHOW
84 void tv_start_scan(tvi_handle_t
*tvh
, int start
)
86 mp_msg(MSGT_TV
,MSGL_INFO
,"start scan\n");
87 tvh
->tv_param
->scan
=start
?1:0;
90 static void tv_scan(tvi_handle_t
*tvh
)
94 tv_channels_t
*tv_channel_tmp
=NULL
;
95 tv_channels_t
*tv_channel_add
=NULL
;
99 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
100 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
102 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Channel scanner is not available without tuner\n");
103 tvh
->tv_param
->scan
=0;
110 scan
=calloc(1,sizeof(tv_scan_t
));
112 cl
= tvh
->chanlist_s
[scan
->channel_num
];
113 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
114 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
116 if(scan
->scan_timer
>now
)
119 if (tv_get_signal(tvh
)>tvh
->tv_param
->scan_threshold
) {
120 cl
= tvh
->chanlist_s
[scan
->channel_num
];
121 tv_channel_tmp
=tv_channel_list
;
122 while (tv_channel_tmp
) {
124 if (cl
.freq
==tv_channel_tmp
->freq
){
128 tv_channel_add
=tv_channel_tmp
;
129 tv_channel_tmp
=tv_channel_tmp
->next
;
132 mp_msg(MSGT_TV
, MSGL_INFO
, "Found new channel: %s (#%d). \n",cl
.name
,index
);
133 scan
->new_channels
++;
134 tv_channel_tmp
= malloc(sizeof(tv_channels_t
));
135 tv_channel_tmp
->index
=index
;
136 tv_channel_tmp
->next
=NULL
;
137 tv_channel_tmp
->prev
=tv_channel_add
;
138 tv_channel_tmp
->freq
=cl
.freq
;
139 snprintf(tv_channel_tmp
->name
,sizeof(tv_channel_tmp
->name
),"ch%d",index
);
140 strncpy(tv_channel_tmp
->number
, cl
.name
, 5);
141 tv_channel_tmp
->number
[4]='\0';
142 if (!tv_channel_list
)
143 tv_channel_list
=tv_channel_tmp
;
145 tv_channel_add
->next
=tv_channel_tmp
;
146 tv_channel_list
->prev
=tv_channel_tmp
;
149 mp_msg(MSGT_TV
, MSGL_INFO
, "Found existing channel: %s-%s.\n",
150 tv_channel_tmp
->number
,tv_channel_tmp
->name
);
153 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
154 if (scan
->channel_num
>=chanlists
[tvh
->chanlist
].count
) {
155 tvh
->tv_param
->scan
=0;
156 mp_msg(MSGT_TV
, MSGL_INFO
, "TV scan end. Found %d new channels.\n", scan
->new_channels
);
157 tv_channel_tmp
=tv_channel_list
;
159 mp_msg(MSGT_TV
,MSGL_INFO
,"channels=");
160 while(tv_channel_tmp
){
161 mp_msg(MSGT_TV
,MSGL_INFO
,"%s-%s",tv_channel_tmp
->number
,tv_channel_tmp
->name
);
162 if(tv_channel_tmp
->next
)
163 mp_msg(MSGT_TV
,MSGL_INFO
,",");
164 tv_channel_tmp
=tv_channel_tmp
->next
;
166 mp_msg(MSGT_TV
, MSGL_INFO
, "\n");
168 if (!tv_channel_current
) tv_channel_current
=tv_channel_list
;
169 if (tv_channel_current
)
170 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
174 cl
= tvh
->chanlist_s
[scan
->channel_num
];
175 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
176 mp_msg(MSGT_TV
, MSGL_INFO
, "Trying: %s (%.2f). \n",cl
.name
,1e-3*cl
.freq
);
180 /* ================== DEMUX_TV ===================== */
183 0 = EOF(?) or no stream
184 1 = successfully read a packet
186 /* fill demux->video and demux->audio */
188 static int demux_tv_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
190 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demux
->priv
);
194 /* ================== ADD AUDIO PACKET =================== */
196 if (ds
==demux
->audio
&& tvh
->tv_param
->noaudio
== 0 &&
197 tvh
->functions
->control(tvh
->priv
,
198 TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
200 len
= tvh
->functions
->get_audio_framesize(tvh
->priv
);
202 dp
=new_demux_packet(len
);
203 dp
->flags
|=1; /* Keyframe */
204 dp
->pts
=tvh
->functions
->grab_audio_frame(tvh
->priv
, dp
->buffer
,len
);
205 ds_add_packet(demux
->audio
,dp
);
208 /* ================== ADD VIDEO PACKET =================== */
210 if (ds
==demux
->video
&& tvh
->functions
->control(tvh
->priv
,
211 TVI_CONTROL_IS_VIDEO
, 0) == TVI_CONTROL_TRUE
)
213 len
= tvh
->functions
->get_video_framesize(tvh
->priv
);
214 dp
=new_demux_packet(len
);
215 dp
->flags
|=1; /* Keyframe */
216 dp
->pts
=tvh
->functions
->grab_video_frame(tvh
->priv
, dp
->buffer
, len
);
217 ds_add_packet(demux
->video
,dp
);
220 if (tvh
->tv_param
->scan
) tv_scan(tvh
);
224 static int norm_from_string(tvi_handle_t
*tvh
, char* norm
)
226 const tvi_functions_t
*funcs
= tvh
->functions
;
230 strncpy(str
, norm
, sizeof(str
)-1);
231 str
[sizeof(str
)-1] = '\0';
232 ret
=funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_GET_NORMID
, str
);
234 if(ret
==TVI_CONTROL_TRUE
)
237 if(ret
!=TVI_CONTROL_UNKNOWN
)
239 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
,"default");
243 if (!strcasecmp(norm
, "pal"))
245 else if (!strcasecmp(norm
, "ntsc"))
247 else if (!strcasecmp(norm
, "secam"))
248 return TV_NORM_SECAM
;
249 else if (!strcasecmp(norm
, "palnc"))
250 return TV_NORM_PALNC
;
251 else if (!strcasecmp(norm
, "palm"))
253 else if (!strcasecmp(norm
, "paln"))
255 else if (!strcasecmp(norm
, "ntscjp"))
256 return TV_NORM_NTSCJP
;
258 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
, "PAL");
263 static void parse_channels(tvi_handle_t
*tvh
)
265 char** channels
= tvh
->tv_param
->channels
;
267 mp_tmsg(MSGT_TV
, MSGL_INFO
, "TV channel names detected.\n");
268 tv_channel_list
= malloc(sizeof(tv_channels_t
));
269 tv_channel_list
->index
=1;
270 tv_channel_list
->next
=NULL
;
271 tv_channel_list
->prev
=NULL
;
272 tv_channel_current
= tv_channel_list
;
273 tv_channel_current
->norm
= tvh
->norm
;
276 char* tmp
= *(channels
++);
277 char* sep
= strchr(tmp
,'-');
281 if (!sep
) continue; // Wrong syntax, but mplayer should not crash
283 av_strlcpy(tv_channel_current
->name
, sep
+ 1,
284 sizeof(tv_channel_current
->name
));
286 strncpy(tv_channel_current
->number
, tmp
, 5);
287 tv_channel_current
->number
[4]='\0';
289 while ((sep
=strchr(tv_channel_current
->name
, '_')))
292 // if channel number is a number and larger than 1000 threat it as frequency
293 // tmp still contain pointer to null-terminated string with channel number here
295 tv_channel_current
->freq
=atoi(tmp
);
297 tv_channel_current
->freq
= 0;
298 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++) {
299 cl
= tvh
->chanlist_s
[i
];
300 if (!strcasecmp(cl
.name
, tv_channel_current
->number
)) {
301 tv_channel_current
->freq
=cl
.freq
;
306 if (tv_channel_current
->freq
== 0)
307 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Couldn't find frequency for channel %s (%s)\n",
308 tv_channel_current
->number
, tv_channel_current
->name
);
310 sep
= strchr(tv_channel_current
->name
, '-');
311 if ( !sep
) sep
= strchr(tv_channel_current
->name
, '+');
315 if ( sep
[0] == '+' ) tv_channel_current
->freq
+= i
* 100;
316 if ( sep
[0] == '-' ) tv_channel_current
->freq
-= i
* 100;
320 sep
= strchr(tv_channel_current
->name
, '=');
322 tv_channel_current
->norm
= norm_from_string(tvh
, sep
+1);
327 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
328 tv_channel_current->number, tv_channel_current->name,
329 (float)tv_channel_current->freq/1000);*/
331 tv_channel_current
->next
= malloc(sizeof(tv_channels_t
));
332 tv_channel_current
->next
->index
= tv_channel_current
->index
+ 1;
333 tv_channel_current
->next
->prev
= tv_channel_current
;
334 tv_channel_current
->next
->next
= NULL
;
335 tv_channel_current
= tv_channel_current
->next
;
336 tv_channel_current
->norm
= tvh
->norm
;
338 if (tv_channel_current
->prev
)
339 tv_channel_current
->prev
->next
= NULL
;
340 free(tv_channel_current
);
343 int tv_set_norm(tvi_handle_t
*tvh
, char* norm
)
345 tvh
->norm
= norm_from_string(tvh
, norm
);
347 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm : %s\n", norm
);
348 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
349 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
352 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
353 &tvh
->tv_param
->teletext
);
357 static int tv_set_norm_i(tvi_handle_t
*tvh
, int norm
)
361 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm id: %d\n", norm
);
362 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
363 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
367 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
368 &tvh
->tv_param
->teletext
);
372 static int open_tv(tvi_handle_t
*tvh
)
375 const tvi_functions_t
*funcs
= tvh
->functions
;
376 int tv_fmt_list
[] = {
387 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_VIDEO
, 0) != TVI_CONTROL_TRUE
)
389 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: No video input present!\n");
393 if (tvh
->tv_param
->outfmt
== -1)
394 for (i
= 0; i
< sizeof (tv_fmt_list
) / sizeof (*tv_fmt_list
); i
++)
396 tvh
->tv_param
->outfmt
= tv_fmt_list
[i
];
397 if (funcs
->control (tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
,
398 &tvh
->tv_param
->outfmt
) == TVI_CONTROL_TRUE
)
403 switch(tvh
->tv_param
->outfmt
)
417 mp_tmsg(MSGT_TV
, MSGL_ERR
,
418 "==================================================================\n"\
419 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
420 " This may cause buggy playback or program crash! Bug reports will\n"\
421 " be ignored! You should try again with YV12 (which is the default\n"\
422 " colorspace) and read the documentation!\n"\
423 "==================================================================\n"
424 ,tvh
->tv_param
->outfmt
);
426 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
, &tvh
->tv_param
->outfmt
);
429 /* set some params got from cmdline */
430 funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_SET_INPUT
, &tvh
->tv_param
->input
);
432 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
434 #ifdef CONFIG_TV_V4L2
435 || (!strcmp(tvh
->tv_param
->driver
, "v4l2") && tvh
->tv_param
->normid
>= 0)
437 #ifdef CONFIG_TV_DSHOW
438 || (!strcmp(tvh
->tv_param
->driver
, "dshow") && tvh
->tv_param
->normid
>= 0)
441 tv_set_norm_i(tvh
, tvh
->tv_param
->normid
);
444 tv_set_norm(tvh
,tvh
->tv_param
->norm
);
446 #ifdef CONFIG_TV_V4L1
447 if ( tvh
->tv_param
->mjpeg
)
449 /* set width to expected value */
450 if (tvh
->tv_param
->width
== -1)
452 tvh
->tv_param
->width
= 704/tvh
->tv_param
->decimation
;
454 if (tvh
->tv_param
->height
== -1)
456 if ( tvh
->norm
!= TV_NORM_NTSC
)
457 tvh
->tv_param
->height
= 576/tvh
->tv_param
->decimation
;
459 tvh
->tv_param
->height
= 480/tvh
->tv_param
->decimation
;
461 mp_tmsg(MSGT_TV
, MSGL_INFO
,
462 " MJP: width %d height %d\n", tvh
->tv_param
->width
, tvh
->tv_param
->height
);
466 /* limits on w&h are norm-dependent -- JM */
467 if (tvh
->tv_param
->width
!= -1 && tvh
->tv_param
->height
!= -1) {
468 // first tell the driver both width and height, some drivers do not support setting them independently.
470 dim
[0] = tvh
->tv_param
->width
; dim
[1] = tvh
->tv_param
->height
;
471 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH_HEIGHT
, dim
);
474 if (tvh
->tv_param
->width
!= -1)
476 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_WIDTH
, &tvh
->tv_param
->width
) == TVI_CONTROL_TRUE
)
477 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH
, &tvh
->tv_param
->width
);
480 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested width: %d\n", tvh
->tv_param
->width
);
481 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &tvh
->tv_param
->width
);
486 if (tvh
->tv_param
->height
!= -1)
488 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_HEIGHT
, &tvh
->tv_param
->height
) == TVI_CONTROL_TRUE
)
489 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HEIGHT
, &tvh
->tv_param
->height
);
492 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested height: %d\n", tvh
->tv_param
->height
);
493 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &tvh
->tv_param
->height
);
497 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
499 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Selected input hasn't got a tuner!\n");
503 /* select channel list */
504 for (i
= 0; chanlists
[i
].name
!= NULL
; i
++)
506 if (!strcasecmp(chanlists
[i
].name
, tvh
->tv_param
->chanlist
))
509 tvh
->chanlist_s
= chanlists
[i
].list
;
514 if (tvh
->chanlist
== -1)
515 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unable to find selected channel list! (%s)\n",
516 tvh
->tv_param
->chanlist
);
518 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected channel list: %s (including %d channels)\n",
519 chanlists
[tvh
->chanlist
].name
, chanlists
[tvh
->chanlist
].count
);
521 if (tvh
->tv_param
->freq
&& tvh
->tv_param
->channel
)
523 mp_tmsg(MSGT_TV
, MSGL_WARN
, "You can't set frequency and channel simultaneously!\n");
527 /* Handle channel names */
528 if (tvh
->tv_param
->channels
) {
531 tv_channel_last_real
= malloc(5);
533 if (tv_channel_list
) {
536 if (tvh
->tv_param
->channel
)
538 if (isdigit(*tvh
->tv_param
->channel
))
539 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
540 channel
= atoi(tvh
->tv_param
->channel
);
543 /* if tvh->tv_param->channel does not begin with a digit
544 set the first channel that contains tvh->tv_param->channel in its name */
546 tv_channel_current
= tv_channel_list
;
547 while ( tv_channel_current
) {
548 if ( strstr(tv_channel_current
->name
, tvh
->tv_param
->channel
) )
550 tv_channel_current
= tv_channel_current
->next
;
552 if ( !tv_channel_current
) tv_channel_current
= tv_channel_list
;
559 tv_channel_current
= tv_channel_list
;
560 for (i
= 1; i
< channel
; i
++)
561 if (tv_channel_current
->next
)
562 tv_channel_current
= tv_channel_current
->next
;
565 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
566 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
567 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
568 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
569 tv_channel_last
= tv_channel_current
;
571 /* we need to set frequency */
572 if (tvh
->tv_param
->freq
)
574 unsigned long freq
= atof(tvh
->tv_param
->freq
)*16;
576 /* set freq in MHz */
577 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
579 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
580 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected frequency: %lu (%.3f)\n",
581 freq
, (float)freq
/16);
584 if (tvh
->tv_param
->channel
) {
587 mp_tmsg(MSGT_TV
, MSGL_V
, "Requested channel: %s\n", tvh
->tv_param
->channel
);
588 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
590 cl
= tvh
->chanlist_s
[i
];
591 // printf("count%d: name: %s, freq: %d\n",
592 // i, cl.name, cl.freq);
593 if (!strcasecmp(cl
.name
, tvh
->tv_param
->channel
))
595 strcpy(tv_channel_last_real
, cl
.name
);
597 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
598 cl
.name
, (float)cl
.freq
/1000);
599 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
606 /* grep frequency in chanlist */
611 tv_get_freq(tvh
, &i2
);
613 freq
= (int) (((float)(i2
/16))*1000)+250;
615 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
617 if (tvh
->chanlist_s
[i
].freq
== freq
)
626 /* also start device! */
630 static tvi_handle_t
*tv_begin(tv_param_t
* tv_param
)
634 if(tv_param
->driver
&& !strcmp(tv_param
->driver
,"help")){
635 mp_tmsg(MSGT_TV
,MSGL_INFO
,"Available drivers:\n");
636 for(i
=0;tvi_driver_list
[i
];i
++){
637 mp_msg(MSGT_TV
,MSGL_INFO
," %s\t%s",tvi_driver_list
[i
]->short_name
,tvi_driver_list
[i
]->name
);
638 if(tvi_driver_list
[i
]->comment
)
639 mp_msg(MSGT_TV
,MSGL_INFO
," (%s)",tvi_driver_list
[i
]->comment
);
640 mp_msg(MSGT_TV
,MSGL_INFO
,"\n");
645 for(i
=0;tvi_driver_list
[i
];i
++){
646 if (!tv_param
->driver
|| !strcmp(tvi_driver_list
[i
]->short_name
, tv_param
->driver
)){
647 h
=tvi_driver_list
[i
]->tvi_init(tv_param
);
648 //Requested driver initialization failed
649 if (!h
&& tv_param
->driver
)
651 //Driver initialization failed during autodetection process.
655 h
->tv_param
=tv_param
;
656 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
,
657 tvi_driver_list
[i
]->name
,
658 tvi_driver_list
[i
]->author
,
659 tvi_driver_list
[i
]->comment
?tvi_driver_list
[i
]->comment
:"");
660 tv_param
->driver
=strdup(tvi_driver_list
[i
]->short_name
);
666 mp_tmsg(MSGT_TV
, MSGL_ERR
, "No such driver: %s\n", tv_param
->driver
);
668 mp_tmsg(MSGT_TV
, MSGL_ERR
, "TV driver autodetection failed.\n");
672 static int tv_uninit(tvi_handle_t
*tvh
)
676 if (!tvh
->priv
) return 1;
677 res
=tvh
->functions
->uninit(tvh
->priv
);
685 static demuxer_t
* demux_open_tv(demuxer_t
*demuxer
)
688 sh_video_t
*sh_video
;
689 sh_audio_t
*sh_audio
= NULL
;
690 const tvi_functions_t
*funcs
;
693 if(!(tvh
=tv_begin(demuxer
->stream
->priv
))) return NULL
;
694 if (!tvh
->functions
->init(tvh
->priv
)) return NULL
;
696 tvh
->demuxer
= demuxer
;
697 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_VBI_INIT
,
698 &(tvh
->tv_param
->teletext
.device
));
699 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_GET_VBI_PTR
,
706 funcs
= tvh
->functions
;
709 sh_video
= new_sh_video(demuxer
, 0);
711 /* get IMAGE FORMAT */
712 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FORMAT
, &sh_video
->format
);
713 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
714 // sh_video->format = 0x0;
716 /* set FPS and FRAMETIME */
721 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FPS
, &tmp
) != TVI_CONTROL_TRUE
)
722 sh_video
->fps
= 25.0f
; /* on PAL */
723 else sh_video
->fps
= tmp
;
726 if (tvh
->tv_param
->fps
!= -1.0f
)
727 sh_video
->fps
= tvh
->tv_param
->fps
;
729 sh_video
->frametime
= 1.0f
/sh_video
->fps
;
731 /* If playback only mode, go to immediate mode, fail silently */
732 if(tvh
->tv_param
->immediate
== 1)
734 funcs
->control(tvh
->priv
, TVI_CONTROL_IMMEDIATE
, 0);
735 tvh
->tv_param
->noaudio
= 1;
738 /* disable TV audio if -nosound is present */
739 if (!demuxer
->audio
|| demuxer
->audio
->id
== -2) {
740 tvh
->tv_param
->noaudio
= 1;
744 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &sh_video
->disp_w
);
747 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &sh_video
->disp_h
);
749 demuxer
->video
->sh
= sh_video
;
750 sh_video
->ds
= demuxer
->video
;
751 demuxer
->video
->id
= 0;
752 demuxer
->seekable
= 0;
754 /* here comes audio init */
755 if (tvh
->tv_param
->noaudio
== 0 && funcs
->control(tvh
->priv
, TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
761 /* yeah, audio is present */
763 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_SET_SAMPLERATE
,
764 &tvh
->tv_param
->audiorate
);
766 if (funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_FORMAT
, &audio_format
) != TVI_CONTROL_TRUE
)
773 case AF_FORMAT_U16_LE
:
774 case AF_FORMAT_U16_BE
:
775 case AF_FORMAT_S16_LE
:
776 case AF_FORMAT_S16_BE
:
777 case AF_FORMAT_S32_LE
:
778 case AF_FORMAT_S32_BE
:
779 sh_audio_format
= 0x1; /* PCM */
781 case AF_FORMAT_IMA_ADPCM
:
782 case AF_FORMAT_MU_LAW
:
783 case AF_FORMAT_A_LAW
:
784 case AF_FORMAT_MPEG2
:
786 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Audio type '%s (%x)' unsupported!\n",
787 af_fmt2str(audio_format
, buf
, 128), audio_format
);
791 sh_audio
= new_sh_audio(demuxer
, 0);
793 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLERATE
,
794 &sh_audio
->samplerate
);
795 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLESIZE
,
796 &sh_audio
->samplesize
);
797 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_CHANNELS
,
798 &sh_audio
->channels
);
800 sh_audio
->format
= sh_audio_format
;
801 sh_audio
->sample_format
= audio_format
;
803 sh_audio
->i_bps
= sh_audio
->o_bps
=
804 sh_audio
->samplerate
* sh_audio
->samplesize
*
807 // emulate WF for win32 codecs:
808 sh_audio
->wf
= malloc(sizeof(WAVEFORMATEX
));
809 sh_audio
->wf
->wFormatTag
= sh_audio
->format
;
810 sh_audio
->wf
->nChannels
= sh_audio
->channels
;
811 sh_audio
->wf
->wBitsPerSample
= sh_audio
->samplesize
* 8;
812 sh_audio
->wf
->nSamplesPerSec
= sh_audio
->samplerate
;
813 sh_audio
->wf
->nBlockAlign
= sh_audio
->samplesize
* sh_audio
->channels
;
814 sh_audio
->wf
->nAvgBytesPerSec
= sh_audio
->i_bps
;
816 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, " TV audio: %d channels, %d bits, %d Hz\n",
817 sh_audio
->wf
->nChannels
, sh_audio
->wf
->wBitsPerSample
,
818 sh_audio
->wf
->nSamplesPerSec
);
820 demuxer
->audio
->sh
= sh_audio
;
821 sh_audio
->ds
= demuxer
->audio
;
822 demuxer
->audio
->id
= 0;
826 if(!(funcs
->start(tvh
->priv
))){
833 tv_set_color_options(tvh
, TV_COLOR_BRIGHTNESS
, tvh
->tv_param
->brightness
);
834 tv_set_color_options(tvh
, TV_COLOR_HUE
, tvh
->tv_param
->hue
);
835 tv_set_color_options(tvh
, TV_COLOR_SATURATION
, tvh
->tv_param
->saturation
);
836 tv_set_color_options(tvh
, TV_COLOR_CONTRAST
, tvh
->tv_param
->contrast
);
838 if(tvh
->tv_param
->gain
!=-1)
839 if(funcs
->control(tvh
->priv
,TVI_CONTROL_VID_SET_GAIN
,&tvh
->tv_param
->gain
)!=TVI_CONTROL_TRUE
)
840 mp_msg(MSGT_TV
,MSGL_WARN
,"Unable to set gain control!\n");
842 teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
843 &tvh
->tv_param
->teletext
);
848 static void demux_close_tv(demuxer_t
*demuxer
)
850 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demuxer
->priv
);
855 demuxer
->teletext
=NULL
;
858 /* utilities for mplayer (not mencoder!!) */
859 int tv_set_color_options(tvi_handle_t
*tvh
, int opt
, int value
)
861 const tvi_functions_t
*funcs
= tvh
->functions
;
865 case TV_COLOR_BRIGHTNESS
:
866 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_BRIGHTNESS
, &value
);
868 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HUE
, &value
);
869 case TV_COLOR_SATURATION
:
870 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_SATURATION
, &value
);
871 case TV_COLOR_CONTRAST
:
872 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_CONTRAST
, &value
);
874 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
877 return TVI_CONTROL_UNKNOWN
;
880 int tv_get_color_options(tvi_handle_t
*tvh
, int opt
, int* value
)
882 const tvi_functions_t
*funcs
= tvh
->functions
;
886 case TV_COLOR_BRIGHTNESS
:
887 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_BRIGHTNESS
, value
);
889 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HUE
, value
);
890 case TV_COLOR_SATURATION
:
891 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_SATURATION
, value
);
892 case TV_COLOR_CONTRAST
:
893 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_CONTRAST
, value
);
895 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
898 return TVI_CONTROL_UNKNOWN
;
901 int tv_get_freq(tvi_handle_t
*tvh
, unsigned long *freq
)
903 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
905 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, freq
);
906 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
907 *freq
, (float)*freq
/16);
912 int tv_set_freq(tvi_handle_t
*tvh
, unsigned long freq
)
914 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
916 // unsigned long freq = atof(tvh->tv_param->freq)*16;
918 /* set freq in MHz */
919 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
921 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
922 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
923 freq
, (float)freq
/16);
925 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
926 &tvh
->tv_param
->teletext
);
930 int tv_get_signal(tvi_handle_t
*tvh
)
933 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
||
934 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_SIGNAL
, &signal
)!=TVI_CONTROL_TRUE
)
940 /*****************************************************************
941 * \brief tune current frequency by step_interval value
942 * \parameter step_interval increment value in 1/16 MHz
943 * \note frequency is rounded to 1/16 MHz value
947 int tv_step_freq(tvi_handle_t
* tvh
, float step_interval
){
948 unsigned long frequency
;
950 tvh
->tv_param
->scan
=0;
951 tv_get_freq(tvh
,&frequency
);
952 frequency
+=step_interval
;
953 return tv_set_freq(tvh
,frequency
);
956 int tv_step_channel_real(tvi_handle_t
*tvh
, int direction
)
960 tvh
->tv_param
->scan
=0;
961 if (direction
== TV_CHANNEL_LOWER
)
963 if (tvh
->channel
-1 >= 0)
965 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
966 cl
= tvh
->chanlist_s
[--tvh
->channel
];
967 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
968 cl
.name
, (float)cl
.freq
/1000);
969 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
973 if (direction
== TV_CHANNEL_HIGHER
)
975 if (tvh
->channel
+1 < chanlists
[tvh
->chanlist
].count
)
977 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
978 cl
= tvh
->chanlist_s
[++tvh
->channel
];
979 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
980 cl
.name
, (float)cl
.freq
/1000);
981 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
987 int tv_step_channel(tvi_handle_t
*tvh
, int direction
) {
988 tvh
->tv_param
->scan
=0;
989 if (tv_channel_list
) {
990 if (direction
== TV_CHANNEL_HIGHER
) {
991 tv_channel_last
= tv_channel_current
;
992 if (tv_channel_current
->next
)
993 tv_channel_current
= tv_channel_current
->next
;
995 tv_channel_current
= tv_channel_list
;
997 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
998 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
999 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
1000 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1002 if (direction
== TV_CHANNEL_LOWER
) {
1003 tv_channel_last
= tv_channel_current
;
1004 if (tv_channel_current
->prev
)
1005 tv_channel_current
= tv_channel_current
->prev
;
1007 while (tv_channel_current
->next
)
1008 tv_channel_current
= tv_channel_current
->next
;
1009 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1010 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1011 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
1012 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1014 } else tv_step_channel_real(tvh
, direction
);
1018 int tv_set_channel_real(tvi_handle_t
*tvh
, char *channel
) {
1022 tvh
->tv_param
->scan
=0;
1023 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1024 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1026 cl
= tvh
->chanlist_s
[i
];
1027 // printf("count%d: name: %s, freq: %d\n",
1028 // i, cl.name, cl.freq);
1029 if (!strcasecmp(cl
.name
, channel
))
1032 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1033 cl
.name
, (float)cl
.freq
/1000);
1034 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1041 int tv_set_channel(tvi_handle_t
*tvh
, char *channel
) {
1044 tvh
->tv_param
->scan
=0;
1045 if (tv_channel_list
) {
1046 tv_channel_last
= tv_channel_current
;
1047 channel_int
= atoi(channel
);
1048 tv_channel_current
= tv_channel_list
;
1049 for (i
= 1; i
< channel_int
; i
++)
1050 if (tv_channel_current
->next
)
1051 tv_channel_current
= tv_channel_current
->next
;
1052 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1053 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1054 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1055 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1056 } else tv_set_channel_real(tvh
, channel
);
1060 int tv_last_channel(tvi_handle_t
*tvh
) {
1062 tvh
->tv_param
->scan
=0;
1063 if (tv_channel_list
) {
1066 tmp
= tv_channel_last
;
1067 tv_channel_last
= tv_channel_current
;
1068 tv_channel_current
= tmp
;
1070 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1071 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1072 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1073 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1078 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1080 cl
= tvh
->chanlist_s
[i
];
1081 if (!strcasecmp(cl
.name
, tv_channel_last_real
))
1083 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1085 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1086 cl
.name
, (float)cl
.freq
/1000);
1087 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1095 int tv_step_norm(tvi_handle_t
*tvh
)
1098 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1099 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1101 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1102 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1103 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
1107 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
1108 &tvh
->tv_param
->teletext
);
1112 int tv_step_chanlist(tvi_handle_t
*tvh
)
1117 demuxer_desc_t demuxer_desc_tv
= {
1121 "Alex Beregszaszi, Charles R. Henrich",
1126 demux_tv_fill_buffer
,