Merge svn changes up to r30475
[mplayer/glamo.git] / stream / tv.c
blobcb44790bb65b4f206a1a6f3014c93d21fe29941b
1 /*
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.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sys/time.h>
35 #include "config.h"
38 #include "mp_msg.h"
39 #include "help_mp.h"
41 #include "stream.h"
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"
51 #include "tv.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[]={
68 #ifdef CONFIG_TV_V4L2
69 &tvi_info_v4l2,
70 #endif
71 #ifdef CONFIG_TV_V4L1
72 &tvi_info_v4l,
73 #endif
74 #ifdef CONFIG_TV_BSDBT848
75 &tvi_info_bsdbt848,
76 #endif
77 #ifdef CONFIG_TV_DSHOW
78 &tvi_info_dshow,
79 #endif
80 &tvi_info_dummy,
81 NULL
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)
92 unsigned int now;
93 struct CHANLIST cl;
94 tv_channels_t *tv_channel_tmp=NULL;
95 tv_channels_t *tv_channel_add=NULL;
96 tv_scan_t* scan;
97 int found=0, index=1;
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;
104 return;
107 scan = tvh->scan;
108 now=GetTimer();
109 if (!scan) {
110 scan=calloc(1,sizeof(tv_scan_t));
111 tvh->scan=scan;
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)
117 return;
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) {
123 index++;
124 if (cl.freq==tv_channel_tmp->freq){
125 found=1;
126 break;
128 tv_channel_add=tv_channel_tmp;
129 tv_channel_tmp=tv_channel_tmp->next;
131 if (!found) {
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;
144 else {
145 tv_channel_add->next=tv_channel_tmp;
146 tv_channel_list->prev=tv_channel_tmp;
148 }else
149 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
150 tv_channel_tmp->number,tv_channel_tmp->name);
152 scan->channel_num++;
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;
158 if(tv_channel_tmp){
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));
171 free(tvh->scan);
172 tvh->scan=NULL;
173 }else{
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 ===================== */
182 Return value:
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);
191 demux_packet_t* dp;
192 unsigned int len=0;
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);
221 return 1;
224 static int norm_from_string(tvi_handle_t *tvh, char* norm)
226 const tvi_functions_t *funcs = tvh->functions;
227 char str[20];
228 int ret;
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)
235 return *(int *)str;
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");
240 return 0;
243 if (!strcasecmp(norm, "pal"))
244 return TV_NORM_PAL;
245 else if (!strcasecmp(norm, "ntsc"))
246 return TV_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"))
252 return TV_NORM_PALM;
253 else if (!strcasecmp(norm, "paln"))
254 return TV_NORM_PALN;
255 else if (!strcasecmp(norm, "ntscjp"))
256 return TV_NORM_NTSCJP;
257 else {
258 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
259 return TV_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;
275 while (*channels) {
276 char* tmp = *(channels++);
277 char* sep = strchr(tmp,'-');
278 int i;
279 struct CHANLIST cl;
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));
285 sep[0] = '\0';
286 strncpy(tv_channel_current->number, tmp, 5);
287 tv_channel_current->number[4]='\0';
289 while ((sep=strchr(tv_channel_current->name, '_')))
290 sep[0] = ' ';
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
294 if (atoi(tmp)>1000){
295 tv_channel_current->freq=atoi(tmp);
296 }else{
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;
302 break;
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);
309 else {
310 sep = strchr(tv_channel_current->name, '-');
311 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
313 if ( sep ) {
314 i = atoi (sep+1);
315 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
316 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
317 sep[0] = '\0';
320 sep = strchr(tv_channel_current->name, '=');
321 if ( sep ) {
322 tv_channel_current->norm = norm_from_string(tvh, sep+1);
323 sep[0] = '\0';
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");
350 return 0;
352 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
353 &tvh->tv_param->teletext);
354 return 1;
357 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
359 tvh->norm = 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");
364 return 0;
367 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
368 &tvh->tv_param->teletext);
369 return 1;
372 static int open_tv(tvi_handle_t *tvh)
374 int i;
375 const tvi_functions_t *funcs = tvh->functions;
376 int tv_fmt_list[] = {
377 IMGFMT_YV12,
378 IMGFMT_I420,
379 IMGFMT_UYVY,
380 IMGFMT_YUY2,
381 IMGFMT_RGB32,
382 IMGFMT_RGB24,
383 IMGFMT_RGB16,
384 IMGFMT_RGB15
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");
390 return 0;
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)
399 break;
401 else
403 switch(tvh->tv_param->outfmt)
405 case IMGFMT_YV12:
406 case IMGFMT_I420:
407 case IMGFMT_UYVY:
408 case IMGFMT_YUY2:
409 case IMGFMT_RGB32:
410 case IMGFMT_RGB24:
411 case IMGFMT_BGR32:
412 case IMGFMT_BGR24:
413 case IMGFMT_BGR16:
414 case IMGFMT_BGR15:
415 break;
416 default:
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)
433 if (0
434 #ifdef CONFIG_TV_V4L2
435 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
436 #endif
437 #ifdef CONFIG_TV_DSHOW
438 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
439 #endif
441 tv_set_norm_i(tvh, tvh->tv_param->normid);
442 else
443 #endif
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;
458 else
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);
464 #endif
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.
469 int dim[2];
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);
473 /* set width */
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);
478 else
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);
485 /* set height */
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);
490 else
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");
500 goto done;
503 /* select channel list */
504 for (i = 0; chanlists[i].name != NULL; i++)
506 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
508 tvh->chanlist = i;
509 tvh->chanlist_s = chanlists[i].list;
510 break;
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);
517 else
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");
524 goto done;
527 /* Handle channel names */
528 if (tvh->tv_param->channels) {
529 parse_channels(tvh);
530 } else
531 tv_channel_last_real = malloc(5);
533 if (tv_channel_list) {
534 int i;
535 int channel = 0;
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);
541 else
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) )
549 break;
550 tv_channel_current = tv_channel_current->next;
552 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
555 else
556 channel = 1;
558 if ( channel ) {
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;
570 } else {
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) {
585 struct CHANLIST cl;
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);
596 tvh->channel = i;
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));
600 break;
606 /* grep frequency in chanlist */
608 unsigned long i2;
609 int freq;
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)
619 tvh->channel = i+1;
620 break;
625 done:
626 /* also start device! */
627 return 1;
630 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
632 int i;
633 tvi_handle_t* h;
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");
642 return NULL;
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)
650 return NULL;
651 //Driver initialization failed during autodetection process.
652 if (!h)
653 continue;
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);
661 return h;
665 if(tv_param->driver)
666 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
667 else
668 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
669 return NULL;
672 static int tv_uninit(tvi_handle_t *tvh)
674 int res;
675 if(!tvh) return 1;
676 if (!tvh->priv) return 1;
677 res=tvh->functions->uninit(tvh->priv);
678 if(res) {
679 free(tvh->priv);
680 tvh->priv=NULL;
682 return res;
685 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
687 tvi_handle_t *tvh;
688 sh_video_t *sh_video;
689 sh_audio_t *sh_audio = NULL;
690 const tvi_functions_t *funcs;
692 demuxer->priv=NULL;
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,
700 &demuxer->teletext);
702 if (!open_tv(tvh)){
703 tv_uninit(tvh);
704 return NULL;
706 funcs = tvh->functions;
707 demuxer->priv=tvh;
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 */
718 if(!sh_video->fps)
720 float tmp;
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;
743 /* set width */
744 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
746 /* set height */
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)
757 int audio_format;
758 int sh_audio_format;
759 char buf[128];
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)
767 goto no_audio;
769 switch(audio_format)
771 case AF_FORMAT_U8:
772 case AF_FORMAT_S8:
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 */
780 break;
781 case AF_FORMAT_IMA_ADPCM:
782 case AF_FORMAT_MU_LAW:
783 case AF_FORMAT_A_LAW:
784 case AF_FORMAT_MPEG2:
785 default:
786 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
787 af_fmt2str(audio_format, buf, 128), audio_format);
788 goto no_audio;
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 *
805 sh_audio->channels;
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;
824 no_audio:
826 if(!(funcs->start(tvh->priv))){
827 // start failed :(
828 tv_uninit(tvh);
829 return NULL;
832 /* set color eq */
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);
845 return demuxer;
848 static void demux_close_tv(demuxer_t *demuxer)
850 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
851 if (!tvh) return;
852 tv_uninit(tvh);
853 free(tvh);
854 demuxer->priv=NULL;
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;
863 switch(opt)
865 case TV_COLOR_BRIGHTNESS:
866 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
867 case TV_COLOR_HUE:
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);
873 default:
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;
884 switch(opt)
886 case TV_COLOR_BRIGHTNESS:
887 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
888 case TV_COLOR_HUE:
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);
894 default:
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);
909 return 1;
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);
927 return 1;
930 int tv_get_signal(tvi_handle_t *tvh)
932 int signal=0;
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)
935 return 0;
937 return signal;
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
944 * \return 1
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)
958 struct CHANLIST cl;
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));
984 return 1;
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;
994 else
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;
1006 else
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);
1015 return 1;
1018 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1019 int i;
1020 struct CHANLIST cl;
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))
1031 tvh->channel = i;
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));
1035 break;
1038 return 1;
1041 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1042 int i, channel_int;
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);
1057 return 1;
1060 int tv_last_channel(tvi_handle_t *tvh) {
1062 tvh->tv_param->scan=0;
1063 if (tv_channel_list) {
1064 tv_channels_t *tmp;
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));
1074 } else {
1075 int i;
1076 struct CHANLIST cl;
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);
1084 tvh->channel = i;
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));
1088 break;
1092 return 1;
1095 int tv_step_norm(tvi_handle_t *tvh)
1097 tvh->norm++;
1098 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1099 &tvh->norm) != TVI_CONTROL_TRUE) {
1100 tvh->norm = 0;
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");
1104 return 0;
1107 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1108 &tvh->tv_param->teletext);
1109 return 1;
1112 int tv_step_chanlist(tvi_handle_t *tvh)
1114 return 1;
1117 demuxer_desc_t demuxer_desc_tv = {
1118 "Tv card demuxer",
1119 "tv",
1120 "TV",
1121 "Alex Beregszaszi, Charles R. Henrich",
1122 "?",
1123 DEMUXER_TYPE_TV,
1124 0, // no autodetect
1125 NULL,
1126 demux_tv_fill_buffer,
1127 demux_open_tv,
1128 demux_close_tv,
1129 NULL,
1130 NULL