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_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_ScannerNotAvailableWithoutTuner
);
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_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_BogusNormParameter
, 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_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_BogusNormParameter
, norm
, "PAL");
263 static void parse_channels(tvi_handle_t
*tvh
)
265 char** channels
= tvh
->tv_param
->channels
;
267 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_ChannelNamesDetected
);
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_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_NoFreqForChannel
,
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_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_SelectedNorm
, norm
);
348 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
349 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_CannotSetNorm
);
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_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_SelectedNormId
, norm
);
362 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
363 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_CannotSetNorm
);
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_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_NoVideoInputPresent
);
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_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_UnknownImageFormat
,tvh
->tv_param
->outfmt
);
419 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
, &tvh
->tv_param
->outfmt
);
422 /* set some params got from cmdline */
423 funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_SET_INPUT
, &tvh
->tv_param
->input
);
425 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
427 #ifdef CONFIG_TV_V4L2
428 || (!strcmp(tvh
->tv_param
->driver
, "v4l2") && tvh
->tv_param
->normid
>= 0)
430 #ifdef CONFIG_TV_DSHOW
431 || (!strcmp(tvh
->tv_param
->driver
, "dshow") && tvh
->tv_param
->normid
>= 0)
434 tv_set_norm_i(tvh
, tvh
->tv_param
->normid
);
437 tv_set_norm(tvh
,tvh
->tv_param
->norm
);
439 #ifdef CONFIG_TV_V4L1
440 if ( tvh
->tv_param
->mjpeg
)
442 /* set width to expected value */
443 if (tvh
->tv_param
->width
== -1)
445 tvh
->tv_param
->width
= 704/tvh
->tv_param
->decimation
;
447 if (tvh
->tv_param
->height
== -1)
449 if ( tvh
->norm
!= TV_NORM_NTSC
)
450 tvh
->tv_param
->height
= 576/tvh
->tv_param
->decimation
;
452 tvh
->tv_param
->height
= 480/tvh
->tv_param
->decimation
;
454 mp_msg(MSGT_TV
, MSGL_INFO
,
455 MSGTR_TV_MJP_WidthHeight
, tvh
->tv_param
->width
, tvh
->tv_param
->height
);
459 /* limits on w&h are norm-dependent -- JM */
460 if (tvh
->tv_param
->width
!= -1 && tvh
->tv_param
->height
!= -1) {
461 // first tell the driver both width and height, some drivers do not support setting them independently.
463 dim
[0] = tvh
->tv_param
->width
; dim
[1] = tvh
->tv_param
->height
;
464 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH_HEIGHT
, dim
);
467 if (tvh
->tv_param
->width
!= -1)
469 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_WIDTH
, &tvh
->tv_param
->width
) == TVI_CONTROL_TRUE
)
470 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH
, &tvh
->tv_param
->width
);
473 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_UnableToSetWidth
, tvh
->tv_param
->width
);
474 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &tvh
->tv_param
->width
);
479 if (tvh
->tv_param
->height
!= -1)
481 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_HEIGHT
, &tvh
->tv_param
->height
) == TVI_CONTROL_TRUE
)
482 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HEIGHT
, &tvh
->tv_param
->height
);
485 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_UnableToSetHeight
, tvh
->tv_param
->height
);
486 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &tvh
->tv_param
->height
);
490 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
492 mp_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_NoTuner
);
496 /* select channel list */
497 for (i
= 0; chanlists
[i
].name
!= NULL
; i
++)
499 if (!strcasecmp(chanlists
[i
].name
, tvh
->tv_param
->chanlist
))
502 tvh
->chanlist_s
= chanlists
[i
].list
;
507 if (tvh
->chanlist
== -1)
508 mp_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_UnableFindChanlist
,
509 tvh
->tv_param
->chanlist
);
511 mp_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_SelectedChanlist
,
512 chanlists
[tvh
->chanlist
].name
, chanlists
[tvh
->chanlist
].count
);
514 if (tvh
->tv_param
->freq
&& tvh
->tv_param
->channel
)
516 mp_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_ChannelFreqParamConflict
);
520 /* Handle channel names */
521 if (tvh
->tv_param
->channels
) {
524 tv_channel_last_real
= malloc(5);
526 if (tv_channel_list
) {
529 if (tvh
->tv_param
->channel
)
531 if (isdigit(*tvh
->tv_param
->channel
))
532 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
533 channel
= atoi(tvh
->tv_param
->channel
);
536 /* if tvh->tv_param->channel does not begin with a digit
537 set the first channel that contains tvh->tv_param->channel in its name */
539 tv_channel_current
= tv_channel_list
;
540 while ( tv_channel_current
) {
541 if ( strstr(tv_channel_current
->name
, tvh
->tv_param
->channel
) )
543 tv_channel_current
= tv_channel_current
->next
;
545 if ( !tv_channel_current
) tv_channel_current
= tv_channel_list
;
552 tv_channel_current
= tv_channel_list
;
553 for (i
= 1; i
< channel
; i
++)
554 if (tv_channel_current
->next
)
555 tv_channel_current
= tv_channel_current
->next
;
558 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel3
, tv_channel_current
->number
,
559 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
560 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
561 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
562 tv_channel_last
= tv_channel_current
;
564 /* we need to set frequency */
565 if (tvh
->tv_param
->freq
)
567 unsigned long freq
= atof(tvh
->tv_param
->freq
)*16;
569 /* set freq in MHz */
570 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
572 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
573 mp_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_SelectedFrequency
,
574 freq
, (float)freq
/16);
577 if (tvh
->tv_param
->channel
) {
580 mp_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_RequestedChannel
, tvh
->tv_param
->channel
);
581 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
583 cl
= tvh
->chanlist_s
[i
];
584 // printf("count%d: name: %s, freq: %d\n",
585 // i, cl.name, cl.freq);
586 if (!strcasecmp(cl
.name
, tvh
->tv_param
->channel
))
588 strcpy(tv_channel_last_real
, cl
.name
);
590 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel2
,
591 cl
.name
, (float)cl
.freq
/1000);
592 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
599 /* grep frequency in chanlist */
604 tv_get_freq(tvh
, &i2
);
606 freq
= (int) (((float)(i2
/16))*1000)+250;
608 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
610 if (tvh
->chanlist_s
[i
].freq
== freq
)
619 /* also start device! */
623 static tvi_handle_t
*tv_begin(tv_param_t
* tv_param
)
627 if(tv_param
->driver
&& !strcmp(tv_param
->driver
,"help")){
628 mp_msg(MSGT_TV
,MSGL_INFO
,MSGTR_TV_AvailableDrivers
);
629 for(i
=0;tvi_driver_list
[i
];i
++){
630 mp_msg(MSGT_TV
,MSGL_INFO
," %s\t%s",tvi_driver_list
[i
]->short_name
,tvi_driver_list
[i
]->name
);
631 if(tvi_driver_list
[i
]->comment
)
632 mp_msg(MSGT_TV
,MSGL_INFO
," (%s)",tvi_driver_list
[i
]->comment
);
633 mp_msg(MSGT_TV
,MSGL_INFO
,"\n");
638 for(i
=0;tvi_driver_list
[i
];i
++){
639 if (!tv_param
->driver
|| !strcmp(tvi_driver_list
[i
]->short_name
, tv_param
->driver
)){
640 h
=tvi_driver_list
[i
]->tvi_init(tv_param
);
641 //Requested driver initialization failed
642 if (!h
&& tv_param
->driver
)
644 //Driver initialization failed during autodetection process.
648 h
->tv_param
=tv_param
;
649 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_DriverInfo
, tvi_driver_list
[i
]->short_name
,
650 tvi_driver_list
[i
]->name
,
651 tvi_driver_list
[i
]->author
,
652 tvi_driver_list
[i
]->comment
?tvi_driver_list
[i
]->comment
:"");
653 tv_param
->driver
=strdup(tvi_driver_list
[i
]->short_name
);
659 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_NoSuchDriver
, tv_param
->driver
);
661 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_DriverAutoDetectionFailed
);
665 static int tv_uninit(tvi_handle_t
*tvh
)
669 if (!tvh
->priv
) return 1;
670 res
=tvh
->functions
->uninit(tvh
->priv
);
678 static demuxer_t
* demux_open_tv(demuxer_t
*demuxer
)
681 sh_video_t
*sh_video
;
682 sh_audio_t
*sh_audio
= NULL
;
683 const tvi_functions_t
*funcs
;
686 if(!(tvh
=tv_begin(demuxer
->stream
->priv
))) return NULL
;
687 if (!tvh
->functions
->init(tvh
->priv
)) return NULL
;
689 tvh
->demuxer
= demuxer
;
690 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_VBI_INIT
,
691 &(tvh
->tv_param
->teletext
.device
));
692 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_GET_VBI_PTR
,
699 funcs
= tvh
->functions
;
702 sh_video
= new_sh_video(demuxer
, 0);
704 /* get IMAGE FORMAT */
705 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FORMAT
, &sh_video
->format
);
706 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
707 // sh_video->format = 0x0;
709 /* set FPS and FRAMETIME */
714 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FPS
, &tmp
) != TVI_CONTROL_TRUE
)
715 sh_video
->fps
= 25.0f
; /* on PAL */
716 else sh_video
->fps
= tmp
;
719 if (tvh
->tv_param
->fps
!= -1.0f
)
720 sh_video
->fps
= tvh
->tv_param
->fps
;
722 sh_video
->frametime
= 1.0f
/sh_video
->fps
;
724 /* If playback only mode, go to immediate mode, fail silently */
725 if(tvh
->tv_param
->immediate
== 1)
727 funcs
->control(tvh
->priv
, TVI_CONTROL_IMMEDIATE
, 0);
728 tvh
->tv_param
->noaudio
= 1;
731 /* disable TV audio if -nosound is present */
732 if (!demuxer
->audio
|| demuxer
->audio
->id
== -2) {
733 tvh
->tv_param
->noaudio
= 1;
737 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &sh_video
->disp_w
);
740 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &sh_video
->disp_h
);
742 demuxer
->video
->sh
= sh_video
;
743 sh_video
->ds
= demuxer
->video
;
744 demuxer
->video
->id
= 0;
745 demuxer
->seekable
= 0;
747 /* here comes audio init */
748 if (tvh
->tv_param
->noaudio
== 0 && funcs
->control(tvh
->priv
, TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
754 /* yeah, audio is present */
756 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_SET_SAMPLERATE
,
757 &tvh
->tv_param
->audiorate
);
759 if (funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_FORMAT
, &audio_format
) != TVI_CONTROL_TRUE
)
766 case AF_FORMAT_U16_LE
:
767 case AF_FORMAT_U16_BE
:
768 case AF_FORMAT_S16_LE
:
769 case AF_FORMAT_S16_BE
:
770 case AF_FORMAT_S32_LE
:
771 case AF_FORMAT_S32_BE
:
772 sh_audio_format
= 0x1; /* PCM */
774 case AF_FORMAT_IMA_ADPCM
:
775 case AF_FORMAT_MU_LAW
:
776 case AF_FORMAT_A_LAW
:
777 case AF_FORMAT_MPEG2
:
779 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_UnsupportedAudioType
,
780 af_fmt2str(audio_format
, buf
, 128), audio_format
);
784 sh_audio
= new_sh_audio(demuxer
, 0);
786 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLERATE
,
787 &sh_audio
->samplerate
);
788 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLESIZE
,
789 &sh_audio
->samplesize
);
790 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_CHANNELS
,
791 &sh_audio
->channels
);
793 sh_audio
->format
= sh_audio_format
;
794 sh_audio
->sample_format
= audio_format
;
796 sh_audio
->i_bps
= sh_audio
->o_bps
=
797 sh_audio
->samplerate
* sh_audio
->samplesize
*
800 // emulate WF for win32 codecs:
801 sh_audio
->wf
= malloc(sizeof(WAVEFORMATEX
));
802 sh_audio
->wf
->wFormatTag
= sh_audio
->format
;
803 sh_audio
->wf
->nChannels
= sh_audio
->channels
;
804 sh_audio
->wf
->wBitsPerSample
= sh_audio
->samplesize
* 8;
805 sh_audio
->wf
->nSamplesPerSec
= sh_audio
->samplerate
;
806 sh_audio
->wf
->nBlockAlign
= sh_audio
->samplesize
* sh_audio
->channels
;
807 sh_audio
->wf
->nAvgBytesPerSec
= sh_audio
->i_bps
;
809 mp_msg(MSGT_DECVIDEO
, MSGL_V
, MSGTR_TV_AudioFormat
,
810 sh_audio
->wf
->nChannels
, sh_audio
->wf
->wBitsPerSample
,
811 sh_audio
->wf
->nSamplesPerSec
);
813 demuxer
->audio
->sh
= sh_audio
;
814 sh_audio
->ds
= demuxer
->audio
;
815 demuxer
->audio
->id
= 0;
819 if(!(funcs
->start(tvh
->priv
))){
826 tv_set_color_options(tvh
, TV_COLOR_BRIGHTNESS
, tvh
->tv_param
->brightness
);
827 tv_set_color_options(tvh
, TV_COLOR_HUE
, tvh
->tv_param
->hue
);
828 tv_set_color_options(tvh
, TV_COLOR_SATURATION
, tvh
->tv_param
->saturation
);
829 tv_set_color_options(tvh
, TV_COLOR_CONTRAST
, tvh
->tv_param
->contrast
);
831 if(tvh
->tv_param
->gain
!=-1)
832 if(funcs
->control(tvh
->priv
,TVI_CONTROL_VID_SET_GAIN
,&tvh
->tv_param
->gain
)!=TVI_CONTROL_TRUE
)
833 mp_msg(MSGT_TV
,MSGL_WARN
,"Unable to set gain control!\n");
835 teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
836 &tvh
->tv_param
->teletext
);
841 static void demux_close_tv(demuxer_t
*demuxer
)
843 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demuxer
->priv
);
848 demuxer
->teletext
=NULL
;
851 /* utilities for mplayer (not mencoder!!) */
852 int tv_set_color_options(tvi_handle_t
*tvh
, int opt
, int value
)
854 const tvi_functions_t
*funcs
= tvh
->functions
;
858 case TV_COLOR_BRIGHTNESS
:
859 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_BRIGHTNESS
, &value
);
861 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HUE
, &value
);
862 case TV_COLOR_SATURATION
:
863 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_SATURATION
, &value
);
864 case TV_COLOR_CONTRAST
:
865 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_CONTRAST
, &value
);
867 mp_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_UnknownColorOption
, opt
);
870 return TVI_CONTROL_UNKNOWN
;
873 int tv_get_color_options(tvi_handle_t
*tvh
, int opt
, int* value
)
875 const tvi_functions_t
*funcs
= tvh
->functions
;
879 case TV_COLOR_BRIGHTNESS
:
880 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_BRIGHTNESS
, value
);
882 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HUE
, value
);
883 case TV_COLOR_SATURATION
:
884 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_SATURATION
, value
);
885 case TV_COLOR_CONTRAST
:
886 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_CONTRAST
, value
);
888 mp_msg(MSGT_TV
, MSGL_WARN
, MSGTR_TV_UnknownColorOption
, opt
);
891 return TVI_CONTROL_UNKNOWN
;
894 int tv_get_freq(tvi_handle_t
*tvh
, unsigned long *freq
)
896 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
898 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, freq
);
899 mp_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_CurrentFrequency
,
900 *freq
, (float)*freq
/16);
905 int tv_set_freq(tvi_handle_t
*tvh
, unsigned long freq
)
907 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
909 // unsigned long freq = atof(tvh->tv_param->freq)*16;
911 /* set freq in MHz */
912 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
914 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
915 mp_msg(MSGT_TV
, MSGL_V
, MSGTR_TV_CurrentFrequency
,
916 freq
, (float)freq
/16);
918 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
919 &tvh
->tv_param
->teletext
);
923 int tv_get_signal(tvi_handle_t
*tvh
)
926 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
||
927 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_SIGNAL
, &signal
)!=TVI_CONTROL_TRUE
)
933 /*****************************************************************
934 * \brief tune current frequency by step_interval value
935 * \parameter step_interval increment value in 1/16 MHz
936 * \note frequency is rounded to 1/16 MHz value
940 int tv_step_freq(tvi_handle_t
* tvh
, float step_interval
){
941 unsigned long frequency
;
943 tvh
->tv_param
->scan
=0;
944 tv_get_freq(tvh
,&frequency
);
945 frequency
+=step_interval
;
946 return tv_set_freq(tvh
,frequency
);
949 int tv_step_channel_real(tvi_handle_t
*tvh
, int direction
)
953 tvh
->tv_param
->scan
=0;
954 if (direction
== TV_CHANNEL_LOWER
)
956 if (tvh
->channel
-1 >= 0)
958 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
959 cl
= tvh
->chanlist_s
[--tvh
->channel
];
960 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel2
,
961 cl
.name
, (float)cl
.freq
/1000);
962 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
966 if (direction
== TV_CHANNEL_HIGHER
)
968 if (tvh
->channel
+1 < chanlists
[tvh
->chanlist
].count
)
970 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
971 cl
= tvh
->chanlist_s
[++tvh
->channel
];
972 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel2
,
973 cl
.name
, (float)cl
.freq
/1000);
974 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
980 int tv_step_channel(tvi_handle_t
*tvh
, int direction
) {
981 tvh
->tv_param
->scan
=0;
982 if (tv_channel_list
) {
983 if (direction
== TV_CHANNEL_HIGHER
) {
984 tv_channel_last
= tv_channel_current
;
985 if (tv_channel_current
->next
)
986 tv_channel_current
= tv_channel_current
->next
;
988 tv_channel_current
= tv_channel_list
;
990 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
991 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
992 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel3
,
993 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
995 if (direction
== TV_CHANNEL_LOWER
) {
996 tv_channel_last
= tv_channel_current
;
997 if (tv_channel_current
->prev
)
998 tv_channel_current
= tv_channel_current
->prev
;
1000 while (tv_channel_current
->next
)
1001 tv_channel_current
= tv_channel_current
->next
;
1002 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1003 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1004 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel3
,
1005 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1007 } else tv_step_channel_real(tvh
, direction
);
1011 int tv_set_channel_real(tvi_handle_t
*tvh
, char *channel
) {
1015 tvh
->tv_param
->scan
=0;
1016 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1017 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1019 cl
= tvh
->chanlist_s
[i
];
1020 // printf("count%d: name: %s, freq: %d\n",
1021 // i, cl.name, cl.freq);
1022 if (!strcasecmp(cl
.name
, channel
))
1025 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel2
,
1026 cl
.name
, (float)cl
.freq
/1000);
1027 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1034 int tv_set_channel(tvi_handle_t
*tvh
, char *channel
) {
1037 tvh
->tv_param
->scan
=0;
1038 if (tv_channel_list
) {
1039 tv_channel_last
= tv_channel_current
;
1040 channel_int
= atoi(channel
);
1041 tv_channel_current
= tv_channel_list
;
1042 for (i
= 1; i
< channel_int
; i
++)
1043 if (tv_channel_current
->next
)
1044 tv_channel_current
= tv_channel_current
->next
;
1045 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel3
, tv_channel_current
->number
,
1046 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1047 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1048 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1049 } else tv_set_channel_real(tvh
, channel
);
1053 int tv_last_channel(tvi_handle_t
*tvh
) {
1055 tvh
->tv_param
->scan
=0;
1056 if (tv_channel_list
) {
1059 tmp
= tv_channel_last
;
1060 tv_channel_last
= tv_channel_current
;
1061 tv_channel_current
= tmp
;
1063 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel3
, tv_channel_current
->number
,
1064 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1065 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1066 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1071 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1073 cl
= tvh
->chanlist_s
[i
];
1074 if (!strcasecmp(cl
.name
, tv_channel_last_real
))
1076 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1078 mp_msg(MSGT_TV
, MSGL_INFO
, MSGTR_TV_SelectedChannel2
,
1079 cl
.name
, (float)cl
.freq
/1000);
1080 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1088 int tv_step_norm(tvi_handle_t
*tvh
)
1091 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1092 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1094 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1095 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1096 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_TV_CannotSetNorm
);
1100 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
1101 &tvh
->tv_param
->teletext
);
1105 int tv_step_chanlist(tvi_handle_t
*tvh
)
1110 demuxer_desc_t demuxer_desc_tv
= {
1114 "Alex Beregszaszi, Charles R. Henrich",
1119 demux_tv_fill_buffer
,