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_v4l2
;
62 extern const tvi_info_t tvi_info_bsdbt848
;
64 /** List of drivers in autodetection order */
65 static const tvi_info_t
* tvi_driver_list
[]={
69 #ifdef CONFIG_TV_BSDBT848
72 #ifdef CONFIG_TV_DSHOW
79 tvi_handle_t
*tv_new_handle(int size
, const tvi_functions_t
*functions
)
81 tvi_handle_t
*h
= malloc(sizeof(*h
));
86 h
->priv
= calloc(1, size
);
93 h
->functions
= functions
;
104 void tv_free_handle(tvi_handle_t
*h
)
113 void tv_start_scan(tvi_handle_t
*tvh
, int start
)
115 mp_msg(MSGT_TV
,MSGL_INFO
,"start scan\n");
116 tvh
->tv_param
->scan
=start
?1:0;
119 static void tv_scan(tvi_handle_t
*tvh
)
123 tv_channels_t
*tv_channel_tmp
=NULL
;
124 tv_channels_t
*tv_channel_add
=NULL
;
126 int found
=0, index
=1;
128 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
129 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
131 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Channel scanner is not available without tuner\n");
132 tvh
->tv_param
->scan
=0;
139 scan
=calloc(1,sizeof(tv_scan_t
));
141 cl
= tvh
->chanlist_s
[scan
->channel_num
];
142 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
143 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
145 if(scan
->scan_timer
>now
)
148 if (tv_get_signal(tvh
)>tvh
->tv_param
->scan_threshold
) {
149 cl
= tvh
->chanlist_s
[scan
->channel_num
];
150 tv_channel_tmp
=tv_channel_list
;
151 while (tv_channel_tmp
) {
153 if (cl
.freq
==tv_channel_tmp
->freq
){
157 tv_channel_add
=tv_channel_tmp
;
158 tv_channel_tmp
=tv_channel_tmp
->next
;
161 mp_msg(MSGT_TV
, MSGL_INFO
, "Found new channel: %s (#%d). \n",cl
.name
,index
);
162 scan
->new_channels
++;
163 tv_channel_tmp
= malloc(sizeof(tv_channels_t
));
164 tv_channel_tmp
->index
=index
;
165 tv_channel_tmp
->next
=NULL
;
166 tv_channel_tmp
->prev
=tv_channel_add
;
167 tv_channel_tmp
->freq
=cl
.freq
;
168 snprintf(tv_channel_tmp
->name
,sizeof(tv_channel_tmp
->name
),"ch%d",index
);
169 strncpy(tv_channel_tmp
->number
, cl
.name
, 5);
170 tv_channel_tmp
->number
[4]='\0';
171 if (!tv_channel_list
)
172 tv_channel_list
=tv_channel_tmp
;
174 tv_channel_add
->next
=tv_channel_tmp
;
175 tv_channel_list
->prev
=tv_channel_tmp
;
178 mp_msg(MSGT_TV
, MSGL_INFO
, "Found existing channel: %s-%s.\n",
179 tv_channel_tmp
->number
,tv_channel_tmp
->name
);
182 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
183 if (scan
->channel_num
>=chanlists
[tvh
->chanlist
].count
) {
184 tvh
->tv_param
->scan
=0;
185 mp_msg(MSGT_TV
, MSGL_INFO
, "TV scan end. Found %d new channels.\n", scan
->new_channels
);
186 tv_channel_tmp
=tv_channel_list
;
188 mp_msg(MSGT_TV
,MSGL_INFO
,"channels=");
189 while(tv_channel_tmp
){
190 mp_msg(MSGT_TV
,MSGL_INFO
,"%s-%s",tv_channel_tmp
->number
,tv_channel_tmp
->name
);
191 if(tv_channel_tmp
->next
)
192 mp_msg(MSGT_TV
,MSGL_INFO
,",");
193 tv_channel_tmp
=tv_channel_tmp
->next
;
195 mp_msg(MSGT_TV
, MSGL_INFO
, "\n");
197 if (!tv_channel_current
) tv_channel_current
=tv_channel_list
;
198 if (tv_channel_current
)
199 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
203 cl
= tvh
->chanlist_s
[scan
->channel_num
];
204 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
205 mp_msg(MSGT_TV
, MSGL_INFO
, "Trying: %s (%.2f). \n",cl
.name
,1e-3*cl
.freq
);
209 /* ================== DEMUX_TV ===================== */
212 0 = EOF(?) or no stream
213 1 = successfully read a packet
215 /* fill demux->video and demux->audio */
217 static int demux_tv_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
219 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demux
->priv
);
223 /* ================== ADD AUDIO PACKET =================== */
225 if (ds
==demux
->audio
&& tvh
->tv_param
->noaudio
== 0 &&
226 tvh
->functions
->control(tvh
->priv
,
227 TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
229 len
= tvh
->functions
->get_audio_framesize(tvh
->priv
);
231 dp
=new_demux_packet(len
);
233 dp
->pts
=tvh
->functions
->grab_audio_frame(tvh
->priv
, dp
->buffer
,len
);
234 ds_add_packet(demux
->audio
,dp
);
237 /* ================== ADD VIDEO PACKET =================== */
239 if (ds
==demux
->video
&& tvh
->functions
->control(tvh
->priv
,
240 TVI_CONTROL_IS_VIDEO
, 0) == TVI_CONTROL_TRUE
)
242 len
= tvh
->functions
->get_video_framesize(tvh
->priv
);
243 dp
=new_demux_packet(len
);
245 dp
->pts
=tvh
->functions
->grab_video_frame(tvh
->priv
, dp
->buffer
, len
);
246 ds_add_packet(demux
->video
,dp
);
249 if (tvh
->tv_param
->scan
) tv_scan(tvh
);
253 static int norm_from_string(tvi_handle_t
*tvh
, char* norm
)
255 const tvi_functions_t
*funcs
= tvh
->functions
;
259 strncpy(str
, norm
, sizeof(str
)-1);
260 str
[sizeof(str
)-1] = '\0';
261 ret
=funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_GET_NORMID
, str
);
263 if (ret
== TVI_CONTROL_TRUE
) {
268 if(ret
!=TVI_CONTROL_UNKNOWN
)
270 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
,"default");
274 if (!strcasecmp(norm
, "pal"))
276 else if (!strcasecmp(norm
, "ntsc"))
278 else if (!strcasecmp(norm
, "secam"))
279 return TV_NORM_SECAM
;
280 else if (!strcasecmp(norm
, "palnc"))
281 return TV_NORM_PALNC
;
282 else if (!strcasecmp(norm
, "palm"))
284 else if (!strcasecmp(norm
, "paln"))
286 else if (!strcasecmp(norm
, "ntscjp"))
287 return TV_NORM_NTSCJP
;
289 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
, "PAL");
294 static void parse_channels(tvi_handle_t
*tvh
)
296 char** channels
= tvh
->tv_param
->channels
;
298 mp_tmsg(MSGT_TV
, MSGL_INFO
, "TV channel names detected.\n");
299 tv_channel_list
= malloc(sizeof(tv_channels_t
));
300 tv_channel_list
->index
=1;
301 tv_channel_list
->next
=NULL
;
302 tv_channel_list
->prev
=NULL
;
303 tv_channel_current
= tv_channel_list
;
304 tv_channel_current
->norm
= tvh
->norm
;
307 char* tmp
= *(channels
++);
308 char* sep
= strchr(tmp
,'-');
312 if (!sep
) continue; // Wrong syntax, but mplayer should not crash
314 av_strlcpy(tv_channel_current
->name
, sep
+ 1,
315 sizeof(tv_channel_current
->name
));
317 strncpy(tv_channel_current
->number
, tmp
, 5);
318 tv_channel_current
->number
[4]='\0';
320 while ((sep
=strchr(tv_channel_current
->name
, '_')))
323 // if channel number is a number and larger than 1000 threat it as frequency
324 // tmp still contain pointer to null-terminated string with channel number here
326 tv_channel_current
->freq
=atoi(tmp
);
328 tv_channel_current
->freq
= 0;
329 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++) {
330 cl
= tvh
->chanlist_s
[i
];
331 if (!strcasecmp(cl
.name
, tv_channel_current
->number
)) {
332 tv_channel_current
->freq
=cl
.freq
;
337 if (tv_channel_current
->freq
== 0)
338 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Couldn't find frequency for channel %s (%s)\n",
339 tv_channel_current
->number
, tv_channel_current
->name
);
341 sep
= strchr(tv_channel_current
->name
, '-');
342 if ( !sep
) sep
= strchr(tv_channel_current
->name
, '+');
346 if ( sep
[0] == '+' ) tv_channel_current
->freq
+= i
* 100;
347 if ( sep
[0] == '-' ) tv_channel_current
->freq
-= i
* 100;
351 sep
= strchr(tv_channel_current
->name
, '=');
353 tv_channel_current
->norm
= norm_from_string(tvh
, sep
+1);
358 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
359 tv_channel_current->number, tv_channel_current->name,
360 (float)tv_channel_current->freq/1000);*/
362 tv_channel_current
->next
= malloc(sizeof(tv_channels_t
));
363 tv_channel_current
->next
->index
= tv_channel_current
->index
+ 1;
364 tv_channel_current
->next
->prev
= tv_channel_current
;
365 tv_channel_current
->next
->next
= NULL
;
366 tv_channel_current
= tv_channel_current
->next
;
367 tv_channel_current
->norm
= tvh
->norm
;
369 if (tv_channel_current
->prev
)
370 tv_channel_current
->prev
->next
= NULL
;
371 free(tv_channel_current
);
374 int tv_set_norm(tvi_handle_t
*tvh
, char* norm
)
376 tvh
->norm
= norm_from_string(tvh
, norm
);
378 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm : %s\n", norm
);
379 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
380 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
383 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
384 &tvh
->tv_param
->teletext
);
388 static int tv_set_norm_i(tvi_handle_t
*tvh
, int norm
)
392 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm id: %d\n", norm
);
393 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
394 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
398 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
399 &tvh
->tv_param
->teletext
);
403 static int open_tv(tvi_handle_t
*tvh
)
406 const tvi_functions_t
*funcs
= tvh
->functions
;
407 int tv_fmt_list
[] = {
418 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_VIDEO
, 0) != TVI_CONTROL_TRUE
)
420 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: No video input present!\n");
424 if (tvh
->tv_param
->outfmt
== -1)
425 for (i
= 0; i
< sizeof (tv_fmt_list
) / sizeof (*tv_fmt_list
); i
++)
427 tvh
->tv_param
->outfmt
= tv_fmt_list
[i
];
428 if (funcs
->control (tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
,
429 &tvh
->tv_param
->outfmt
) == TVI_CONTROL_TRUE
)
434 switch(tvh
->tv_param
->outfmt
)
448 mp_tmsg(MSGT_TV
, MSGL_ERR
,
449 "==================================================================\n"\
450 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
451 " This may cause buggy playback or program crash! Bug reports will\n"\
452 " be ignored! You should try again with YV12 (which is the default\n"\
453 " colorspace) and read the documentation!\n"\
454 "==================================================================\n"
455 ,tvh
->tv_param
->outfmt
);
457 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
, &tvh
->tv_param
->outfmt
);
460 /* set some params got from cmdline */
461 funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_SET_INPUT
, &tvh
->tv_param
->input
);
463 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
465 #ifdef CONFIG_TV_V4L2
466 || (!strcmp(tvh
->tv_param
->driver
, "v4l2") && tvh
->tv_param
->normid
>= 0)
468 #ifdef CONFIG_TV_DSHOW
469 || (!strcmp(tvh
->tv_param
->driver
, "dshow") && tvh
->tv_param
->normid
>= 0)
472 tv_set_norm_i(tvh
, tvh
->tv_param
->normid
);
475 tv_set_norm(tvh
,tvh
->tv_param
->norm
);
477 /* limits on w&h are norm-dependent -- JM */
478 if (tvh
->tv_param
->width
!= -1 && tvh
->tv_param
->height
!= -1) {
479 // first tell the driver both width and height, some drivers do not support setting them independently.
481 dim
[0] = tvh
->tv_param
->width
; dim
[1] = tvh
->tv_param
->height
;
482 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH_HEIGHT
, dim
);
485 if (tvh
->tv_param
->width
!= -1)
487 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_WIDTH
, &tvh
->tv_param
->width
) == TVI_CONTROL_TRUE
)
488 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH
, &tvh
->tv_param
->width
);
491 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested width: %d\n", tvh
->tv_param
->width
);
492 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &tvh
->tv_param
->width
);
497 if (tvh
->tv_param
->height
!= -1)
499 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_HEIGHT
, &tvh
->tv_param
->height
) == TVI_CONTROL_TRUE
)
500 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HEIGHT
, &tvh
->tv_param
->height
);
503 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested height: %d\n", tvh
->tv_param
->height
);
504 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &tvh
->tv_param
->height
);
508 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
510 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Selected input hasn't got a tuner!\n");
514 /* select channel list */
515 for (i
= 0; chanlists
[i
].name
!= NULL
; i
++)
517 if (!strcasecmp(chanlists
[i
].name
, tvh
->tv_param
->chanlist
))
520 tvh
->chanlist_s
= chanlists
[i
].list
;
525 if (tvh
->chanlist
== -1)
526 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unable to find selected channel list! (%s)\n",
527 tvh
->tv_param
->chanlist
);
529 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected channel list: %s (including %d channels)\n",
530 chanlists
[tvh
->chanlist
].name
, chanlists
[tvh
->chanlist
].count
);
532 if (tvh
->tv_param
->freq
&& tvh
->tv_param
->channel
)
534 mp_tmsg(MSGT_TV
, MSGL_WARN
, "You can't set frequency and channel simultaneously!\n");
538 /* Handle channel names */
539 if (tvh
->tv_param
->channels
) {
542 tv_channel_last_real
= malloc(5);
544 if (tv_channel_list
) {
547 if (tvh
->tv_param
->channel
)
549 if (isdigit(*tvh
->tv_param
->channel
))
550 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
551 channel
= atoi(tvh
->tv_param
->channel
);
554 /* if tvh->tv_param->channel does not begin with a digit
555 set the first channel that contains tvh->tv_param->channel in its name */
557 tv_channel_current
= tv_channel_list
;
558 while ( tv_channel_current
) {
559 if ( strstr(tv_channel_current
->name
, tvh
->tv_param
->channel
) )
561 tv_channel_current
= tv_channel_current
->next
;
563 if ( !tv_channel_current
) tv_channel_current
= tv_channel_list
;
570 tv_channel_current
= tv_channel_list
;
571 for (i
= 1; i
< channel
; i
++)
572 if (tv_channel_current
->next
)
573 tv_channel_current
= tv_channel_current
->next
;
576 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
577 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
578 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
579 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
580 tv_channel_last
= tv_channel_current
;
582 /* we need to set frequency */
583 if (tvh
->tv_param
->freq
)
585 unsigned long freq
= atof(tvh
->tv_param
->freq
)*16;
587 /* set freq in MHz */
588 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
590 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
591 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected frequency: %lu (%.3f)\n",
592 freq
, (float)freq
/16);
595 if (tvh
->tv_param
->channel
) {
598 mp_tmsg(MSGT_TV
, MSGL_V
, "Requested channel: %s\n", tvh
->tv_param
->channel
);
599 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
601 cl
= tvh
->chanlist_s
[i
];
602 // printf("count%d: name: %s, freq: %d\n",
603 // i, cl.name, cl.freq);
604 if (!strcasecmp(cl
.name
, tvh
->tv_param
->channel
))
606 strcpy(tv_channel_last_real
, cl
.name
);
608 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
609 cl
.name
, (float)cl
.freq
/1000);
610 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
617 /* grep frequency in chanlist */
622 tv_get_freq(tvh
, &i2
);
624 freq
= (int) (((float)(i2
/16))*1000)+250;
626 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
628 if (tvh
->chanlist_s
[i
].freq
== freq
)
637 /* also start device! */
641 static tvi_handle_t
*tv_begin(tv_param_t
* tv_param
)
645 if(tv_param
->driver
&& !strcmp(tv_param
->driver
,"help")){
646 mp_tmsg(MSGT_TV
,MSGL_INFO
,"Available drivers:\n");
647 for(i
=0;tvi_driver_list
[i
];i
++){
648 mp_msg(MSGT_TV
,MSGL_INFO
," %s\t%s",tvi_driver_list
[i
]->short_name
,tvi_driver_list
[i
]->name
);
649 if(tvi_driver_list
[i
]->comment
)
650 mp_msg(MSGT_TV
,MSGL_INFO
," (%s)",tvi_driver_list
[i
]->comment
);
651 mp_msg(MSGT_TV
,MSGL_INFO
,"\n");
656 for(i
=0;tvi_driver_list
[i
];i
++){
657 if (!tv_param
->driver
|| !strcmp(tvi_driver_list
[i
]->short_name
, tv_param
->driver
)){
658 h
=tvi_driver_list
[i
]->tvi_init(tv_param
);
659 //Requested driver initialization failed
660 if (!h
&& tv_param
->driver
)
662 //Driver initialization failed during autodetection process.
666 h
->tv_param
=tv_param
;
667 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
,
668 tvi_driver_list
[i
]->name
,
669 tvi_driver_list
[i
]->author
,
670 tvi_driver_list
[i
]->comment
?tvi_driver_list
[i
]->comment
:"");
671 tv_param
->driver
=strdup(tvi_driver_list
[i
]->short_name
);
677 mp_tmsg(MSGT_TV
, MSGL_ERR
, "No such driver: %s\n", tv_param
->driver
);
679 mp_tmsg(MSGT_TV
, MSGL_ERR
, "TV driver autodetection failed.\n");
683 static int tv_uninit(tvi_handle_t
*tvh
)
687 if (!tvh
->priv
) return 1;
688 res
=tvh
->functions
->uninit(tvh
->priv
);
696 static demuxer_t
* demux_open_tv(demuxer_t
*demuxer
)
699 sh_video_t
*sh_video
;
700 sh_audio_t
*sh_audio
= NULL
;
701 const tvi_functions_t
*funcs
;
704 if(!(tvh
=tv_begin(demuxer
->stream
->priv
))) return NULL
;
705 if (!tvh
->functions
->init(tvh
->priv
)) return NULL
;
707 tvh
->demuxer
= demuxer
;
708 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_VBI_INIT
,
709 &(tvh
->tv_param
->teletext
.device
));
710 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_GET_VBI_PTR
,
717 funcs
= tvh
->functions
;
720 sh_video
= new_sh_video(demuxer
, 0);
722 /* get IMAGE FORMAT */
723 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FORMAT
, &sh_video
->format
);
724 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
725 // sh_video->format = 0x0;
727 /* set FPS and FRAMETIME */
732 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FPS
, &tmp
) != TVI_CONTROL_TRUE
)
733 sh_video
->fps
= 25.0f
; /* on PAL */
734 else sh_video
->fps
= tmp
;
737 if (tvh
->tv_param
->fps
!= -1.0f
)
738 sh_video
->fps
= tvh
->tv_param
->fps
;
740 sh_video
->frametime
= 1.0f
/sh_video
->fps
;
742 /* If playback only mode, go to immediate mode, fail silently */
743 if(tvh
->tv_param
->immediate
== 1)
745 funcs
->control(tvh
->priv
, TVI_CONTROL_IMMEDIATE
, 0);
746 tvh
->tv_param
->noaudio
= 1;
749 /* disable TV audio if -nosound is present */
750 if (!demuxer
->audio
|| demuxer
->audio
->id
== -2) {
751 tvh
->tv_param
->noaudio
= 1;
755 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &sh_video
->disp_w
);
758 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &sh_video
->disp_h
);
760 demuxer
->video
->sh
= sh_video
;
761 sh_video
->ds
= demuxer
->video
;
762 demuxer
->video
->id
= 0;
763 demuxer
->seekable
= 0;
765 /* here comes audio init */
766 if (tvh
->tv_param
->noaudio
== 0 && funcs
->control(tvh
->priv
, TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
772 /* yeah, audio is present */
774 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_SET_SAMPLERATE
,
775 &tvh
->tv_param
->audiorate
);
777 if (funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_FORMAT
, &audio_format
) != TVI_CONTROL_TRUE
)
784 case AF_FORMAT_U16_LE
:
785 case AF_FORMAT_U16_BE
:
786 case AF_FORMAT_S16_LE
:
787 case AF_FORMAT_S16_BE
:
788 case AF_FORMAT_S32_LE
:
789 case AF_FORMAT_S32_BE
:
790 sh_audio_format
= 0x1; /* PCM */
792 case AF_FORMAT_IMA_ADPCM
:
793 case AF_FORMAT_MU_LAW
:
794 case AF_FORMAT_A_LAW
:
795 case AF_FORMAT_MPEG2
:
797 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Audio type '%s (%x)' unsupported!\n",
798 af_fmt2str(audio_format
, buf
, 128), audio_format
);
802 sh_audio
= new_sh_audio(demuxer
, 0);
804 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLERATE
,
805 &sh_audio
->samplerate
);
806 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLESIZE
,
807 &sh_audio
->samplesize
);
808 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_CHANNELS
,
809 &sh_audio
->channels
);
811 sh_audio
->format
= sh_audio_format
;
812 sh_audio
->sample_format
= audio_format
;
814 sh_audio
->i_bps
= sh_audio
->o_bps
=
815 sh_audio
->samplerate
* sh_audio
->samplesize
*
818 // emulate WF for win32 codecs:
819 sh_audio
->wf
= malloc(sizeof(*sh_audio
->wf
));
820 sh_audio
->wf
->wFormatTag
= sh_audio
->format
;
821 sh_audio
->wf
->nChannels
= sh_audio
->channels
;
822 sh_audio
->wf
->wBitsPerSample
= sh_audio
->samplesize
* 8;
823 sh_audio
->wf
->nSamplesPerSec
= sh_audio
->samplerate
;
824 sh_audio
->wf
->nBlockAlign
= sh_audio
->samplesize
* sh_audio
->channels
;
825 sh_audio
->wf
->nAvgBytesPerSec
= sh_audio
->i_bps
;
827 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, " TV audio: %d channels, %d bits, %d Hz\n",
828 sh_audio
->wf
->nChannels
, sh_audio
->wf
->wBitsPerSample
,
829 sh_audio
->wf
->nSamplesPerSec
);
831 demuxer
->audio
->sh
= sh_audio
;
832 sh_audio
->ds
= demuxer
->audio
;
833 demuxer
->audio
->id
= 0;
837 if(!(funcs
->start(tvh
->priv
))){
844 tv_set_color_options(tvh
, TV_COLOR_BRIGHTNESS
, tvh
->tv_param
->brightness
);
845 tv_set_color_options(tvh
, TV_COLOR_HUE
, tvh
->tv_param
->hue
);
846 tv_set_color_options(tvh
, TV_COLOR_SATURATION
, tvh
->tv_param
->saturation
);
847 tv_set_color_options(tvh
, TV_COLOR_CONTRAST
, tvh
->tv_param
->contrast
);
849 if(tvh
->tv_param
->gain
!=-1)
850 if(funcs
->control(tvh
->priv
,TVI_CONTROL_VID_SET_GAIN
,&tvh
->tv_param
->gain
)!=TVI_CONTROL_TRUE
)
851 mp_msg(MSGT_TV
,MSGL_WARN
,"Unable to set gain control!\n");
853 teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
854 &tvh
->tv_param
->teletext
);
859 static void demux_close_tv(demuxer_t
*demuxer
)
861 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demuxer
->priv
);
866 demuxer
->teletext
=NULL
;
869 int tv_set_color_options(tvi_handle_t
*tvh
, int opt
, int value
)
871 const tvi_functions_t
*funcs
= tvh
->functions
;
875 case TV_COLOR_BRIGHTNESS
:
876 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_BRIGHTNESS
, &value
);
878 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HUE
, &value
);
879 case TV_COLOR_SATURATION
:
880 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_SATURATION
, &value
);
881 case TV_COLOR_CONTRAST
:
882 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_CONTRAST
, &value
);
884 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
887 return TVI_CONTROL_UNKNOWN
;
890 int tv_get_color_options(tvi_handle_t
*tvh
, int opt
, int* value
)
892 const tvi_functions_t
*funcs
= tvh
->functions
;
896 case TV_COLOR_BRIGHTNESS
:
897 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_BRIGHTNESS
, value
);
899 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HUE
, value
);
900 case TV_COLOR_SATURATION
:
901 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_SATURATION
, value
);
902 case TV_COLOR_CONTRAST
:
903 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_CONTRAST
, value
);
905 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
908 return TVI_CONTROL_UNKNOWN
;
911 int tv_get_freq(tvi_handle_t
*tvh
, unsigned long *freq
)
913 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
915 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, freq
);
916 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
917 *freq
, (float)*freq
/16);
922 int tv_set_freq(tvi_handle_t
*tvh
, unsigned long freq
)
924 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
926 // unsigned long freq = atof(tvh->tv_param->freq)*16;
928 /* set freq in MHz */
929 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
931 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
932 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
933 freq
, (float)freq
/16);
935 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
936 &tvh
->tv_param
->teletext
);
940 int tv_get_signal(tvi_handle_t
*tvh
)
943 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
||
944 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_SIGNAL
, &signal
)!=TVI_CONTROL_TRUE
)
950 /*****************************************************************
951 * \brief tune current frequency by step_interval value
952 * \parameter step_interval increment value in 1/16 MHz
953 * \note frequency is rounded to 1/16 MHz value
957 int tv_step_freq(tvi_handle_t
* tvh
, float step_interval
){
958 unsigned long frequency
;
960 tvh
->tv_param
->scan
=0;
961 tv_get_freq(tvh
,&frequency
);
962 frequency
+=step_interval
;
963 return tv_set_freq(tvh
,frequency
);
966 int tv_step_channel_real(tvi_handle_t
*tvh
, int direction
)
970 tvh
->tv_param
->scan
=0;
971 if (direction
== TV_CHANNEL_LOWER
)
973 if (tvh
->channel
-1 >= 0)
975 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
976 cl
= tvh
->chanlist_s
[--tvh
->channel
];
977 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
978 cl
.name
, (float)cl
.freq
/1000);
979 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
983 if (direction
== TV_CHANNEL_HIGHER
)
985 if (tvh
->channel
+1 < chanlists
[tvh
->chanlist
].count
)
987 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
988 cl
= tvh
->chanlist_s
[++tvh
->channel
];
989 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
990 cl
.name
, (float)cl
.freq
/1000);
991 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
997 int tv_step_channel(tvi_handle_t
*tvh
, int direction
) {
998 tvh
->tv_param
->scan
=0;
999 if (tv_channel_list
) {
1000 if (direction
== TV_CHANNEL_HIGHER
) {
1001 tv_channel_last
= tv_channel_current
;
1002 if (tv_channel_current
->next
)
1003 tv_channel_current
= tv_channel_current
->next
;
1005 tv_channel_current
= tv_channel_list
;
1007 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1008 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1009 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
1010 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1012 if (direction
== TV_CHANNEL_LOWER
) {
1013 tv_channel_last
= tv_channel_current
;
1014 if (tv_channel_current
->prev
)
1015 tv_channel_current
= tv_channel_current
->prev
;
1017 while (tv_channel_current
->next
)
1018 tv_channel_current
= tv_channel_current
->next
;
1019 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1020 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1021 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
1022 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1024 } else tv_step_channel_real(tvh
, direction
);
1028 int tv_set_channel_real(tvi_handle_t
*tvh
, char *channel
) {
1032 tvh
->tv_param
->scan
=0;
1033 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1034 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1036 cl
= tvh
->chanlist_s
[i
];
1037 // printf("count%d: name: %s, freq: %d\n",
1038 // i, cl.name, cl.freq);
1039 if (!strcasecmp(cl
.name
, channel
))
1042 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1043 cl
.name
, (float)cl
.freq
/1000);
1044 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1051 int tv_set_channel(tvi_handle_t
*tvh
, char *channel
) {
1054 tvh
->tv_param
->scan
=0;
1055 if (tv_channel_list
) {
1056 tv_channel_last
= tv_channel_current
;
1057 channel_int
= atoi(channel
);
1058 tv_channel_current
= tv_channel_list
;
1059 for (i
= 1; i
< channel_int
; i
++)
1060 if (tv_channel_current
->next
)
1061 tv_channel_current
= tv_channel_current
->next
;
1062 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1063 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1064 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1065 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1066 } else tv_set_channel_real(tvh
, channel
);
1070 int tv_last_channel(tvi_handle_t
*tvh
) {
1072 tvh
->tv_param
->scan
=0;
1073 if (tv_channel_list
) {
1076 tmp
= tv_channel_last
;
1077 tv_channel_last
= tv_channel_current
;
1078 tv_channel_current
= tmp
;
1080 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1081 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1082 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1083 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1088 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1090 cl
= tvh
->chanlist_s
[i
];
1091 if (!strcasecmp(cl
.name
, tv_channel_last_real
))
1093 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1095 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1096 cl
.name
, (float)cl
.freq
/1000);
1097 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1105 int tv_step_norm(tvi_handle_t
*tvh
)
1108 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1109 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1111 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1112 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1113 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
1117 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
1118 &tvh
->tv_param
->teletext
);
1122 int tv_step_chanlist(tvi_handle_t
*tvh
)
1127 demuxer_desc_t demuxer_desc_tv
= {
1131 "Alex Beregszaszi, Charles R. Henrich",
1136 demux_tv_fill_buffer
,