2 TV Interface for MPlayer
6 API idea based on libvo2
8 Feb 19, 2002: Significant rewrites by Charles R. Henrich (henrich@msu.edu)
9 to add support for audio, and bktr *BSD support.
27 #include "libmpdemux/demuxer.h"
28 #include "libmpdemux/stheader.h"
30 #include "libaf/af_format.h"
31 #include "libmpcodecs/img_format.h"
32 #include "libmpcodecs/dec_teletext.h"
33 #include "libavutil/avstring.h"
34 #include "osdep/timer.h"
38 #include "frequencies.h"
40 tv_channels_t
*tv_channel_list
;
41 tv_channels_t
*tv_channel_current
, *tv_channel_last
;
42 char *tv_channel_last_real
;
44 /* enumerating drivers (like in stream.c) */
45 extern const tvi_info_t tvi_info_dummy
;
46 extern const tvi_info_t tvi_info_dshow
;
47 extern const tvi_info_t tvi_info_v4l
;
48 extern const tvi_info_t tvi_info_v4l2
;
49 extern const tvi_info_t tvi_info_bsdbt848
;
51 /** List of drivers in autodetection order */
52 static const tvi_info_t
* tvi_driver_list
[]={
59 #ifdef CONFIG_TV_BSDBT848
62 #ifdef CONFIG_TV_DSHOW
69 void tv_start_scan(tvi_handle_t
*tvh
, int start
)
71 mp_msg(MSGT_TV
,MSGL_INFO
,"start scan\n");
72 tvh
->tv_param
->scan
=start
?1:0;
75 static void tv_scan(tvi_handle_t
*tvh
)
79 tv_channels_t
*tv_channel_tmp
=NULL
;
80 tv_channels_t
*tv_channel_add
=NULL
;
84 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
85 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
87 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Channel scanner is not available without tuner\n");
88 tvh
->tv_param
->scan
=0;
95 scan
=calloc(1,sizeof(tv_scan_t
));
97 cl
= tvh
->chanlist_s
[scan
->channel_num
];
98 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
99 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
101 if(scan
->scan_timer
>now
)
104 if (tv_get_signal(tvh
)>tvh
->tv_param
->scan_threshold
) {
105 cl
= tvh
->chanlist_s
[scan
->channel_num
];
106 tv_channel_tmp
=tv_channel_list
;
107 while (tv_channel_tmp
) {
109 if (cl
.freq
==tv_channel_tmp
->freq
){
113 tv_channel_add
=tv_channel_tmp
;
114 tv_channel_tmp
=tv_channel_tmp
->next
;
117 mp_msg(MSGT_TV
, MSGL_INFO
, "Found new channel: %s (#%d). \n",cl
.name
,index
);
118 scan
->new_channels
++;
119 tv_channel_tmp
= malloc(sizeof(tv_channels_t
));
120 tv_channel_tmp
->index
=index
;
121 tv_channel_tmp
->next
=NULL
;
122 tv_channel_tmp
->prev
=tv_channel_add
;
123 tv_channel_tmp
->freq
=cl
.freq
;
124 snprintf(tv_channel_tmp
->name
,sizeof(tv_channel_tmp
->name
),"ch%d",index
);
125 strncpy(tv_channel_tmp
->number
, cl
.name
, 5);
126 tv_channel_tmp
->number
[4]='\0';
127 if (!tv_channel_list
)
128 tv_channel_list
=tv_channel_tmp
;
130 tv_channel_add
->next
=tv_channel_tmp
;
131 tv_channel_list
->prev
=tv_channel_tmp
;
134 mp_msg(MSGT_TV
, MSGL_INFO
, "Found existing channel: %s-%s.\n",
135 tv_channel_tmp
->number
,tv_channel_tmp
->name
);
138 scan
->scan_timer
=now
+1e6
*tvh
->tv_param
->scan_period
;
139 if (scan
->channel_num
>=chanlists
[tvh
->chanlist
].count
) {
140 tvh
->tv_param
->scan
=0;
141 mp_msg(MSGT_TV
, MSGL_INFO
, "TV scan end. Found %d new channels.\n", scan
->new_channels
);
142 tv_channel_tmp
=tv_channel_list
;
144 mp_msg(MSGT_TV
,MSGL_INFO
,"channels=");
145 while(tv_channel_tmp
){
146 mp_msg(MSGT_TV
,MSGL_INFO
,"%s-%s",tv_channel_tmp
->number
,tv_channel_tmp
->name
);
147 if(tv_channel_tmp
->next
)
148 mp_msg(MSGT_TV
,MSGL_INFO
,",");
149 tv_channel_tmp
=tv_channel_tmp
->next
;
151 mp_msg(MSGT_TV
, MSGL_INFO
, "\n");
153 if (!tv_channel_current
) tv_channel_current
=tv_channel_list
;
154 if (tv_channel_current
)
155 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
159 cl
= tvh
->chanlist_s
[scan
->channel_num
];
160 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
161 mp_msg(MSGT_TV
, MSGL_INFO
, "Trying: %s (%.2f). \n",cl
.name
,1e-3*cl
.freq
);
165 /* ================== DEMUX_TV ===================== */
168 0 = EOF(?) or no stream
169 1 = successfully read a packet
171 /* fill demux->video and demux->audio */
173 static int demux_tv_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
175 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demux
->priv
);
179 /* ================== ADD AUDIO PACKET =================== */
181 if (ds
==demux
->audio
&& tvh
->tv_param
->noaudio
== 0 &&
182 tvh
->functions
->control(tvh
->priv
,
183 TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
185 len
= tvh
->functions
->get_audio_framesize(tvh
->priv
);
187 dp
=new_demux_packet(len
);
188 dp
->flags
|=1; /* Keyframe */
189 dp
->pts
=tvh
->functions
->grab_audio_frame(tvh
->priv
, dp
->buffer
,len
);
190 ds_add_packet(demux
->audio
,dp
);
193 /* ================== ADD VIDEO PACKET =================== */
195 if (ds
==demux
->video
&& tvh
->functions
->control(tvh
->priv
,
196 TVI_CONTROL_IS_VIDEO
, 0) == TVI_CONTROL_TRUE
)
198 len
= tvh
->functions
->get_video_framesize(tvh
->priv
);
199 dp
=new_demux_packet(len
);
200 dp
->flags
|=1; /* Keyframe */
201 dp
->pts
=tvh
->functions
->grab_video_frame(tvh
->priv
, dp
->buffer
, len
);
202 ds_add_packet(demux
->video
,dp
);
205 if (tvh
->tv_param
->scan
) tv_scan(tvh
);
209 static int norm_from_string(tvi_handle_t
*tvh
, char* norm
)
211 const tvi_functions_t
*funcs
= tvh
->functions
;
215 strncpy(str
, norm
, sizeof(str
)-1);
216 str
[sizeof(str
)-1] = '\0';
217 ret
=funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_GET_NORMID
, str
);
219 if(ret
==TVI_CONTROL_TRUE
)
222 if(ret
!=TVI_CONTROL_UNKNOWN
)
224 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
,"default");
228 if (!strcasecmp(norm
, "pal"))
230 else if (!strcasecmp(norm
, "ntsc"))
232 else if (!strcasecmp(norm
, "secam"))
233 return TV_NORM_SECAM
;
234 else if (!strcasecmp(norm
, "palnc"))
235 return TV_NORM_PALNC
;
236 else if (!strcasecmp(norm
, "palm"))
238 else if (!strcasecmp(norm
, "paln"))
240 else if (!strcasecmp(norm
, "ntscjp"))
241 return TV_NORM_NTSCJP
;
243 mp_tmsg(MSGT_TV
, MSGL_WARN
, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm
, "PAL");
248 static void parse_channels(tvi_handle_t
*tvh
)
250 char** channels
= tvh
->tv_param
->channels
;
252 mp_tmsg(MSGT_TV
, MSGL_INFO
, "TV channel names detected.\n");
253 tv_channel_list
= malloc(sizeof(tv_channels_t
));
254 tv_channel_list
->index
=1;
255 tv_channel_list
->next
=NULL
;
256 tv_channel_list
->prev
=NULL
;
257 tv_channel_current
= tv_channel_list
;
258 tv_channel_current
->norm
= tvh
->norm
;
261 char* tmp
= *(channels
++);
262 char* sep
= strchr(tmp
,'-');
266 if (!sep
) continue; // Wrong syntax, but mplayer should not crash
268 av_strlcpy(tv_channel_current
->name
, sep
+ 1,
269 sizeof(tv_channel_current
->name
));
271 strncpy(tv_channel_current
->number
, tmp
, 5);
272 tv_channel_current
->number
[4]='\0';
274 while ((sep
=strchr(tv_channel_current
->name
, '_')))
277 // if channel number is a number and larger than 1000 threat it as frequency
278 // tmp still contain pointer to null-terminated string with channel number here
280 tv_channel_current
->freq
=atoi(tmp
);
282 tv_channel_current
->freq
= 0;
283 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++) {
284 cl
= tvh
->chanlist_s
[i
];
285 if (!strcasecmp(cl
.name
, tv_channel_current
->number
)) {
286 tv_channel_current
->freq
=cl
.freq
;
291 if (tv_channel_current
->freq
== 0)
292 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Couldn't find frequency for channel %s (%s)\n",
293 tv_channel_current
->number
, tv_channel_current
->name
);
295 sep
= strchr(tv_channel_current
->name
, '-');
296 if ( !sep
) sep
= strchr(tv_channel_current
->name
, '+');
300 if ( sep
[0] == '+' ) tv_channel_current
->freq
+= i
* 100;
301 if ( sep
[0] == '-' ) tv_channel_current
->freq
-= i
* 100;
305 sep
= strchr(tv_channel_current
->name
, '=');
307 tv_channel_current
->norm
= norm_from_string(tvh
, sep
+1);
312 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
313 tv_channel_current->number, tv_channel_current->name,
314 (float)tv_channel_current->freq/1000);*/
316 tv_channel_current
->next
= malloc(sizeof(tv_channels_t
));
317 tv_channel_current
->next
->index
= tv_channel_current
->index
+ 1;
318 tv_channel_current
->next
->prev
= tv_channel_current
;
319 tv_channel_current
->next
->next
= NULL
;
320 tv_channel_current
= tv_channel_current
->next
;
321 tv_channel_current
->norm
= tvh
->norm
;
323 if (tv_channel_current
->prev
)
324 tv_channel_current
->prev
->next
= NULL
;
325 free(tv_channel_current
);
328 int tv_set_norm(tvi_handle_t
*tvh
, char* norm
)
330 tvh
->norm
= norm_from_string(tvh
, norm
);
332 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm : %s\n", norm
);
333 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
, &tvh
->norm
) != TVI_CONTROL_TRUE
) {
334 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
337 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
338 &tvh
->tv_param
->teletext
);
342 static int tv_set_norm_i(tvi_handle_t
*tvh
, int norm
)
346 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected norm id: %d\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");
352 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
353 &tvh
->tv_param
->teletext
);
357 static int open_tv(tvi_handle_t
*tvh
)
360 const tvi_functions_t
*funcs
= tvh
->functions
;
361 int tv_fmt_list
[] = {
372 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_VIDEO
, 0) != TVI_CONTROL_TRUE
)
374 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: No video input present!\n");
378 if (tvh
->tv_param
->outfmt
== -1)
379 for (i
= 0; i
< sizeof (tv_fmt_list
) / sizeof (*tv_fmt_list
); i
++)
381 tvh
->tv_param
->outfmt
= tv_fmt_list
[i
];
382 if (funcs
->control (tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
,
383 &tvh
->tv_param
->outfmt
) == TVI_CONTROL_TRUE
)
388 switch(tvh
->tv_param
->outfmt
)
402 mp_tmsg(MSGT_TV
, MSGL_ERR
,
403 "==================================================================\n"\
404 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
405 " This may cause buggy playback or program crash! Bug reports will\n"\
406 " be ignored! You should try again with YV12 (which is the default\n"\
407 " colorspace) and read the documentation!\n"\
408 "==================================================================\n"
409 ,tvh
->tv_param
->outfmt
);
411 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_FORMAT
, &tvh
->tv_param
->outfmt
);
414 /* set some params got from cmdline */
415 funcs
->control(tvh
->priv
, TVI_CONTROL_SPC_SET_INPUT
, &tvh
->tv_param
->input
);
417 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
419 #ifdef CONFIG_TV_V4L2
420 || (!strcmp(tvh
->tv_param
->driver
, "v4l2") && tvh
->tv_param
->normid
>= 0)
422 #ifdef CONFIG_TV_DSHOW
423 || (!strcmp(tvh
->tv_param
->driver
, "dshow") && tvh
->tv_param
->normid
>= 0)
426 tv_set_norm_i(tvh
, tvh
->tv_param
->normid
);
429 tv_set_norm(tvh
,tvh
->tv_param
->norm
);
431 #ifdef CONFIG_TV_V4L1
432 if ( tvh
->tv_param
->mjpeg
)
434 /* set width to expected value */
435 if (tvh
->tv_param
->width
== -1)
437 tvh
->tv_param
->width
= 704/tvh
->tv_param
->decimation
;
439 if (tvh
->tv_param
->height
== -1)
441 if ( tvh
->norm
!= TV_NORM_NTSC
)
442 tvh
->tv_param
->height
= 576/tvh
->tv_param
->decimation
;
444 tvh
->tv_param
->height
= 480/tvh
->tv_param
->decimation
;
446 mp_tmsg(MSGT_TV
, MSGL_INFO
,
447 " MJP: width %d height %d\n", tvh
->tv_param
->width
, tvh
->tv_param
->height
);
451 /* limits on w&h are norm-dependent -- JM */
452 if (tvh
->tv_param
->width
!= -1 && tvh
->tv_param
->height
!= -1) {
453 // first tell the driver both width and height, some drivers do not support setting them independently.
455 dim
[0] = tvh
->tv_param
->width
; dim
[1] = tvh
->tv_param
->height
;
456 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH_HEIGHT
, dim
);
459 if (tvh
->tv_param
->width
!= -1)
461 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_WIDTH
, &tvh
->tv_param
->width
) == TVI_CONTROL_TRUE
)
462 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_WIDTH
, &tvh
->tv_param
->width
);
465 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested width: %d\n", tvh
->tv_param
->width
);
466 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &tvh
->tv_param
->width
);
471 if (tvh
->tv_param
->height
!= -1)
473 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_CHK_HEIGHT
, &tvh
->tv_param
->height
) == TVI_CONTROL_TRUE
)
474 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HEIGHT
, &tvh
->tv_param
->height
);
477 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Unable to set requested height: %d\n", tvh
->tv_param
->height
);
478 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &tvh
->tv_param
->height
);
482 if (funcs
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
)
484 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Selected input hasn't got a tuner!\n");
488 /* select channel list */
489 for (i
= 0; chanlists
[i
].name
!= NULL
; i
++)
491 if (!strcasecmp(chanlists
[i
].name
, tvh
->tv_param
->chanlist
))
494 tvh
->chanlist_s
= chanlists
[i
].list
;
499 if (tvh
->chanlist
== -1)
500 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unable to find selected channel list! (%s)\n",
501 tvh
->tv_param
->chanlist
);
503 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected channel list: %s (including %d channels)\n",
504 chanlists
[tvh
->chanlist
].name
, chanlists
[tvh
->chanlist
].count
);
506 if (tvh
->tv_param
->freq
&& tvh
->tv_param
->channel
)
508 mp_tmsg(MSGT_TV
, MSGL_WARN
, "You can't set frequency and channel simultaneously!\n");
512 /* Handle channel names */
513 if (tvh
->tv_param
->channels
) {
516 tv_channel_last_real
= malloc(5);
518 if (tv_channel_list
) {
521 if (tvh
->tv_param
->channel
)
523 if (isdigit(*tvh
->tv_param
->channel
))
524 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
525 channel
= atoi(tvh
->tv_param
->channel
);
528 /* if tvh->tv_param->channel does not begin with a digit
529 set the first channel that contains tvh->tv_param->channel in its name */
531 tv_channel_current
= tv_channel_list
;
532 while ( tv_channel_current
) {
533 if ( strstr(tv_channel_current
->name
, tvh
->tv_param
->channel
) )
535 tv_channel_current
= tv_channel_current
->next
;
537 if ( !tv_channel_current
) tv_channel_current
= tv_channel_list
;
544 tv_channel_current
= tv_channel_list
;
545 for (i
= 1; i
< channel
; i
++)
546 if (tv_channel_current
->next
)
547 tv_channel_current
= tv_channel_current
->next
;
550 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
551 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
552 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
553 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
554 tv_channel_last
= tv_channel_current
;
556 /* we need to set frequency */
557 if (tvh
->tv_param
->freq
)
559 unsigned long freq
= atof(tvh
->tv_param
->freq
)*16;
561 /* set freq in MHz */
562 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
564 funcs
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
565 mp_tmsg(MSGT_TV
, MSGL_V
, "Selected frequency: %lu (%.3f)\n",
566 freq
, (float)freq
/16);
569 if (tvh
->tv_param
->channel
) {
572 mp_tmsg(MSGT_TV
, MSGL_V
, "Requested channel: %s\n", tvh
->tv_param
->channel
);
573 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
575 cl
= tvh
->chanlist_s
[i
];
576 // printf("count%d: name: %s, freq: %d\n",
577 // i, cl.name, cl.freq);
578 if (!strcasecmp(cl
.name
, tvh
->tv_param
->channel
))
580 strcpy(tv_channel_last_real
, cl
.name
);
582 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
583 cl
.name
, (float)cl
.freq
/1000);
584 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
591 /* grep frequency in chanlist */
596 tv_get_freq(tvh
, &i2
);
598 freq
= (int) (((float)(i2
/16))*1000)+250;
600 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
602 if (tvh
->chanlist_s
[i
].freq
== freq
)
611 /* also start device! */
615 static tvi_handle_t
*tv_begin(tv_param_t
* tv_param
)
619 if(tv_param
->driver
&& !strcmp(tv_param
->driver
,"help")){
620 mp_tmsg(MSGT_TV
,MSGL_INFO
,"Available drivers:\n");
621 for(i
=0;tvi_driver_list
[i
];i
++){
622 mp_msg(MSGT_TV
,MSGL_INFO
," %s\t%s",tvi_driver_list
[i
]->short_name
,tvi_driver_list
[i
]->name
);
623 if(tvi_driver_list
[i
]->comment
)
624 mp_msg(MSGT_TV
,MSGL_INFO
," (%s)",tvi_driver_list
[i
]->comment
);
625 mp_msg(MSGT_TV
,MSGL_INFO
,"\n");
630 for(i
=0;tvi_driver_list
[i
];i
++){
631 if (!tv_param
->driver
|| !strcmp(tvi_driver_list
[i
]->short_name
, tv_param
->driver
)){
632 h
=tvi_driver_list
[i
]->tvi_init(tv_param
);
633 //Requested driver initialization failed
634 if (!h
&& tv_param
->driver
)
636 //Driver initialization failed during autodetection process.
640 h
->tv_param
=tv_param
;
641 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
,
642 tvi_driver_list
[i
]->name
,
643 tvi_driver_list
[i
]->author
,
644 tvi_driver_list
[i
]->comment
?tvi_driver_list
[i
]->comment
:"");
645 tv_param
->driver
=strdup(tvi_driver_list
[i
]->short_name
);
651 mp_tmsg(MSGT_TV
, MSGL_ERR
, "No such driver: %s\n", tv_param
->driver
);
653 mp_tmsg(MSGT_TV
, MSGL_ERR
, "TV driver autodetection failed.\n");
657 static int tv_uninit(tvi_handle_t
*tvh
)
661 if (!tvh
->priv
) return 1;
662 res
=tvh
->functions
->uninit(tvh
->priv
);
670 static demuxer_t
* demux_open_tv(demuxer_t
*demuxer
)
673 sh_video_t
*sh_video
;
674 sh_audio_t
*sh_audio
= NULL
;
675 const tvi_functions_t
*funcs
;
678 if(!(tvh
=tv_begin(demuxer
->stream
->priv
))) return NULL
;
679 if (!tvh
->functions
->init(tvh
->priv
)) return NULL
;
681 tvh
->demuxer
= demuxer
;
682 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_VBI_INIT
,
683 &(tvh
->tv_param
->teletext
.device
));
684 tvh
->functions
->control(tvh
->priv
,TVI_CONTROL_GET_VBI_PTR
,
691 funcs
= tvh
->functions
;
694 sh_video
= new_sh_video(demuxer
, 0);
696 /* get IMAGE FORMAT */
697 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FORMAT
, &sh_video
->format
);
698 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
699 // sh_video->format = 0x0;
701 /* set FPS and FRAMETIME */
706 if (funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_FPS
, &tmp
) != TVI_CONTROL_TRUE
)
707 sh_video
->fps
= 25.0f
; /* on PAL */
708 else sh_video
->fps
= tmp
;
711 if (tvh
->tv_param
->fps
!= -1.0f
)
712 sh_video
->fps
= tvh
->tv_param
->fps
;
714 sh_video
->frametime
= 1.0f
/sh_video
->fps
;
716 /* If playback only mode, go to immediate mode, fail silently */
717 if(tvh
->tv_param
->immediate
== 1)
719 funcs
->control(tvh
->priv
, TVI_CONTROL_IMMEDIATE
, 0);
720 tvh
->tv_param
->noaudio
= 1;
723 /* disable TV audio if -nosound is present */
724 if (!demuxer
->audio
|| demuxer
->audio
->id
== -2) {
725 tvh
->tv_param
->noaudio
= 1;
729 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_WIDTH
, &sh_video
->disp_w
);
732 funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HEIGHT
, &sh_video
->disp_h
);
734 demuxer
->video
->sh
= sh_video
;
735 sh_video
->ds
= demuxer
->video
;
736 demuxer
->video
->id
= 0;
737 demuxer
->seekable
= 0;
739 /* here comes audio init */
740 if (tvh
->tv_param
->noaudio
== 0 && funcs
->control(tvh
->priv
, TVI_CONTROL_IS_AUDIO
, 0) == TVI_CONTROL_TRUE
)
746 /* yeah, audio is present */
748 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_SET_SAMPLERATE
,
749 &tvh
->tv_param
->audiorate
);
751 if (funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_FORMAT
, &audio_format
) != TVI_CONTROL_TRUE
)
758 case AF_FORMAT_U16_LE
:
759 case AF_FORMAT_U16_BE
:
760 case AF_FORMAT_S16_LE
:
761 case AF_FORMAT_S16_BE
:
762 case AF_FORMAT_S32_LE
:
763 case AF_FORMAT_S32_BE
:
764 sh_audio_format
= 0x1; /* PCM */
766 case AF_FORMAT_IMA_ADPCM
:
767 case AF_FORMAT_MU_LAW
:
768 case AF_FORMAT_A_LAW
:
769 case AF_FORMAT_MPEG2
:
772 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Audio type '%s (%x)' unsupported!\n",
773 af_fmt2str(audio_format
, buf
, 128), audio_format
);
777 sh_audio
= new_sh_audio(demuxer
, 0);
779 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLERATE
,
780 &sh_audio
->samplerate
);
781 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_SAMPLESIZE
,
782 &sh_audio
->samplesize
);
783 funcs
->control(tvh
->priv
, TVI_CONTROL_AUD_GET_CHANNELS
,
784 &sh_audio
->channels
);
786 sh_audio
->format
= sh_audio_format
;
787 sh_audio
->sample_format
= audio_format
;
789 sh_audio
->i_bps
= sh_audio
->o_bps
=
790 sh_audio
->samplerate
* sh_audio
->samplesize
*
793 // emulate WF for win32 codecs:
794 sh_audio
->wf
= malloc(sizeof(WAVEFORMATEX
));
795 sh_audio
->wf
->wFormatTag
= sh_audio
->format
;
796 sh_audio
->wf
->nChannels
= sh_audio
->channels
;
797 sh_audio
->wf
->wBitsPerSample
= sh_audio
->samplesize
* 8;
798 sh_audio
->wf
->nSamplesPerSec
= sh_audio
->samplerate
;
799 sh_audio
->wf
->nBlockAlign
= sh_audio
->samplesize
* sh_audio
->channels
;
800 sh_audio
->wf
->nAvgBytesPerSec
= sh_audio
->i_bps
;
802 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, " TV audio: %d channels, %d bits, %d Hz\n",
803 sh_audio
->wf
->nChannels
, sh_audio
->wf
->wBitsPerSample
,
804 sh_audio
->wf
->nSamplesPerSec
);
806 demuxer
->audio
->sh
= sh_audio
;
807 sh_audio
->ds
= demuxer
->audio
;
808 demuxer
->audio
->id
= 0;
812 if(!(funcs
->start(tvh
->priv
))){
819 tv_set_color_options(tvh
, TV_COLOR_BRIGHTNESS
, tvh
->tv_param
->brightness
);
820 tv_set_color_options(tvh
, TV_COLOR_HUE
, tvh
->tv_param
->hue
);
821 tv_set_color_options(tvh
, TV_COLOR_SATURATION
, tvh
->tv_param
->saturation
);
822 tv_set_color_options(tvh
, TV_COLOR_CONTRAST
, tvh
->tv_param
->contrast
);
824 if(tvh
->tv_param
->gain
!=-1)
825 if(funcs
->control(tvh
->priv
,TVI_CONTROL_VID_SET_GAIN
,&tvh
->tv_param
->gain
)!=TVI_CONTROL_TRUE
)
826 mp_msg(MSGT_TV
,MSGL_WARN
,"Unable to set gain control!\n");
828 teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
829 &tvh
->tv_param
->teletext
);
834 static void demux_close_tv(demuxer_t
*demuxer
)
836 tvi_handle_t
*tvh
=(tvi_handle_t
*)(demuxer
->priv
);
841 demuxer
->teletext
=NULL
;
844 /* utilities for mplayer (not mencoder!!) */
845 int tv_set_color_options(tvi_handle_t
*tvh
, int opt
, int value
)
847 const tvi_functions_t
*funcs
= tvh
->functions
;
851 case TV_COLOR_BRIGHTNESS
:
852 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_BRIGHTNESS
, &value
);
854 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_HUE
, &value
);
855 case TV_COLOR_SATURATION
:
856 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_SATURATION
, &value
);
857 case TV_COLOR_CONTRAST
:
858 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_SET_CONTRAST
, &value
);
860 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
863 return TVI_CONTROL_UNKNOWN
;
866 int tv_get_color_options(tvi_handle_t
*tvh
, int opt
, int* value
)
868 const tvi_functions_t
*funcs
= tvh
->functions
;
872 case TV_COLOR_BRIGHTNESS
:
873 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_BRIGHTNESS
, value
);
875 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_HUE
, value
);
876 case TV_COLOR_SATURATION
:
877 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_SATURATION
, value
);
878 case TV_COLOR_CONTRAST
:
879 return funcs
->control(tvh
->priv
, TVI_CONTROL_VID_GET_CONTRAST
, value
);
881 mp_tmsg(MSGT_TV
, MSGL_WARN
, "Unknown color option (%d) specified!\n", opt
);
884 return TVI_CONTROL_UNKNOWN
;
887 int tv_get_freq(tvi_handle_t
*tvh
, unsigned long *freq
)
889 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
891 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, freq
);
892 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
893 *freq
, (float)*freq
/16);
898 int tv_set_freq(tvi_handle_t
*tvh
, unsigned long freq
)
900 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) == TVI_CONTROL_TRUE
)
902 // unsigned long freq = atof(tvh->tv_param->freq)*16;
904 /* set freq in MHz */
905 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_FREQ
, &freq
);
907 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_FREQ
, &freq
);
908 mp_tmsg(MSGT_TV
, MSGL_V
, "Current frequency: %lu (%.3f)\n",
909 freq
, (float)freq
/16);
911 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
912 &tvh
->tv_param
->teletext
);
916 int tv_get_signal(tvi_handle_t
*tvh
)
919 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_IS_TUNER
, 0) != TVI_CONTROL_TRUE
||
920 tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_GET_SIGNAL
, &signal
)!=TVI_CONTROL_TRUE
)
926 /*****************************************************************
927 * \brief tune current frequency by step_interval value
928 * \parameter step_interval increment value in 1/16 MHz
929 * \note frequency is rounded to 1/16 MHz value
933 int tv_step_freq(tvi_handle_t
* tvh
, float step_interval
){
934 unsigned long frequency
;
936 tvh
->tv_param
->scan
=0;
937 tv_get_freq(tvh
,&frequency
);
938 frequency
+=step_interval
;
939 return tv_set_freq(tvh
,frequency
);
942 int tv_step_channel_real(tvi_handle_t
*tvh
, int direction
)
946 tvh
->tv_param
->scan
=0;
947 if (direction
== TV_CHANNEL_LOWER
)
949 if (tvh
->channel
-1 >= 0)
951 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
952 cl
= tvh
->chanlist_s
[--tvh
->channel
];
953 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
954 cl
.name
, (float)cl
.freq
/1000);
955 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
959 if (direction
== TV_CHANNEL_HIGHER
)
961 if (tvh
->channel
+1 < chanlists
[tvh
->chanlist
].count
)
963 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
964 cl
= tvh
->chanlist_s
[++tvh
->channel
];
965 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
966 cl
.name
, (float)cl
.freq
/1000);
967 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
973 int tv_step_channel(tvi_handle_t
*tvh
, int direction
) {
974 tvh
->tv_param
->scan
=0;
975 if (tv_channel_list
) {
976 if (direction
== TV_CHANNEL_HIGHER
) {
977 tv_channel_last
= tv_channel_current
;
978 if (tv_channel_current
->next
)
979 tv_channel_current
= tv_channel_current
->next
;
981 tv_channel_current
= tv_channel_list
;
983 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
984 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
985 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
986 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
988 if (direction
== TV_CHANNEL_LOWER
) {
989 tv_channel_last
= tv_channel_current
;
990 if (tv_channel_current
->prev
)
991 tv_channel_current
= tv_channel_current
->prev
;
993 while (tv_channel_current
->next
)
994 tv_channel_current
= tv_channel_current
->next
;
995 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
996 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
997 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n",
998 tv_channel_current
->number
, tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1000 } else tv_step_channel_real(tvh
, direction
);
1004 int tv_set_channel_real(tvi_handle_t
*tvh
, char *channel
) {
1008 tvh
->tv_param
->scan
=0;
1009 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1010 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1012 cl
= tvh
->chanlist_s
[i
];
1013 // printf("count%d: name: %s, freq: %d\n",
1014 // i, cl.name, cl.freq);
1015 if (!strcasecmp(cl
.name
, channel
))
1018 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1019 cl
.name
, (float)cl
.freq
/1000);
1020 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1027 int tv_set_channel(tvi_handle_t
*tvh
, char *channel
) {
1030 tvh
->tv_param
->scan
=0;
1031 if (tv_channel_list
) {
1032 tv_channel_last
= tv_channel_current
;
1033 channel_int
= atoi(channel
);
1034 tv_channel_current
= tv_channel_list
;
1035 for (i
= 1; i
< channel_int
; i
++)
1036 if (tv_channel_current
->next
)
1037 tv_channel_current
= tv_channel_current
->next
;
1038 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1039 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1040 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1041 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1042 } else tv_set_channel_real(tvh
, channel
);
1046 int tv_last_channel(tvi_handle_t
*tvh
) {
1048 tvh
->tv_param
->scan
=0;
1049 if (tv_channel_list
) {
1052 tmp
= tv_channel_last
;
1053 tv_channel_last
= tv_channel_current
;
1054 tv_channel_current
= tmp
;
1056 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current
->number
,
1057 tv_channel_current
->name
, (float)tv_channel_current
->freq
/1000);
1058 tv_set_norm_i(tvh
, tv_channel_current
->norm
);
1059 tv_set_freq(tvh
, (unsigned long)(((float)tv_channel_current
->freq
/1000)*16));
1064 for (i
= 0; i
< chanlists
[tvh
->chanlist
].count
; i
++)
1066 cl
= tvh
->chanlist_s
[i
];
1067 if (!strcasecmp(cl
.name
, tv_channel_last_real
))
1069 strcpy(tv_channel_last_real
, tvh
->chanlist_s
[tvh
->channel
].name
);
1071 mp_tmsg(MSGT_TV
, MSGL_INFO
, "Selected channel: %s (freq: %.3f)\n",
1072 cl
.name
, (float)cl
.freq
/1000);
1073 tv_set_freq(tvh
, (unsigned long)(((float)cl
.freq
/1000)*16));
1081 int tv_step_norm(tvi_handle_t
*tvh
)
1084 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1085 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1087 if (tvh
->functions
->control(tvh
->priv
, TVI_CONTROL_TUN_SET_NORM
,
1088 &tvh
->norm
) != TVI_CONTROL_TRUE
) {
1089 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Error: Cannot set norm!\n");
1093 teletext_control(tvh
->demuxer
->teletext
,TV_VBI_CONTROL_RESET
,
1094 &tvh
->tv_param
->teletext
);
1098 int tv_step_chanlist(tvi_handle_t
*tvh
)
1103 demuxer_desc_t demuxer_desc_tv
= {
1107 "Alex Beregszaszi, Charles R. Henrich",
1112 demux_tv_fill_buffer
,