mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / stream / tv.c
blob3ce7264691a0bb8312da64d4d39cbbe19c4459fb
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"
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "libmpdemux/stheader.h"
44 #include "libaf/af_format.h"
45 #include "libmpcodecs/img_format.h"
46 #include "libmpcodecs/dec_teletext.h"
47 #include "libavutil/avstring.h"
48 #include "osdep/timer.h"
50 #include "tv.h"
52 #include "frequencies.h"
54 tv_channels_t *tv_channel_list;
55 tv_channels_t *tv_channel_current, *tv_channel_last;
56 char *tv_channel_last_real;
58 /* enumerating drivers (like in stream.c) */
59 extern const tvi_info_t tvi_info_dummy;
60 extern const tvi_info_t tvi_info_dshow;
61 extern const tvi_info_t tvi_info_v4l2;
62 extern const tvi_info_t tvi_info_bsdbt848;
64 /** List of drivers in autodetection order */
65 static const tvi_info_t* tvi_driver_list[]={
66 #ifdef CONFIG_TV_V4L2
67 &tvi_info_v4l2,
68 #endif
69 #ifdef CONFIG_TV_BSDBT848
70 &tvi_info_bsdbt848,
71 #endif
72 #ifdef CONFIG_TV_DSHOW
73 &tvi_info_dshow,
74 #endif
75 &tvi_info_dummy,
76 NULL
79 tvi_handle_t *tv_new_handle(int size, const tvi_functions_t *functions)
81 tvi_handle_t *h = malloc(sizeof(*h));
83 if (!h)
84 return NULL;
86 h->priv = calloc(1, size);
88 if (!h->priv) {
89 free(h);
90 return NULL;
93 h->functions = functions;
94 h->seq = 0;
95 h->chanlist = -1;
96 h->chanlist_s = NULL;
97 h->norm = -1;
98 h->channel = -1;
99 h->scan = NULL;
101 return h;
104 void tv_free_handle(tvi_handle_t *h)
106 if (!h)
107 return;
108 free(h->priv);
109 free(h->scan);
110 free(h);
113 void tv_start_scan(tvi_handle_t *tvh, int start)
115 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
116 tvh->tv_param->scan=start?1:0;
119 static void tv_scan(tvi_handle_t *tvh)
121 unsigned int now;
122 struct CHANLIST cl;
123 tv_channels_t *tv_channel_tmp=NULL;
124 tv_channels_t *tv_channel_add=NULL;
125 tv_scan_t* scan;
126 int found=0, index=1;
128 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
129 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
131 mp_tmsg(MSGT_TV, MSGL_WARN, "Channel scanner is not available without tuner\n");
132 tvh->tv_param->scan=0;
133 return;
136 scan = tvh->scan;
137 now=GetTimer();
138 if (!scan) {
139 scan=calloc(1,sizeof(tv_scan_t));
140 tvh->scan=scan;
141 cl = tvh->chanlist_s[scan->channel_num];
142 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
143 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
145 if(scan->scan_timer>now)
146 return;
148 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
149 cl = tvh->chanlist_s[scan->channel_num];
150 tv_channel_tmp=tv_channel_list;
151 while (tv_channel_tmp) {
152 index++;
153 if (cl.freq==tv_channel_tmp->freq){
154 found=1;
155 break;
157 tv_channel_add=tv_channel_tmp;
158 tv_channel_tmp=tv_channel_tmp->next;
160 if (!found) {
161 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
162 scan->new_channels++;
163 tv_channel_tmp = malloc(sizeof(tv_channels_t));
164 tv_channel_tmp->index=index;
165 tv_channel_tmp->next=NULL;
166 tv_channel_tmp->prev=tv_channel_add;
167 tv_channel_tmp->freq=cl.freq;
168 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
169 strncpy(tv_channel_tmp->number, cl.name, 5);
170 tv_channel_tmp->number[4]='\0';
171 if (!tv_channel_list)
172 tv_channel_list=tv_channel_tmp;
173 else {
174 tv_channel_add->next=tv_channel_tmp;
175 tv_channel_list->prev=tv_channel_tmp;
177 }else
178 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
179 tv_channel_tmp->number,tv_channel_tmp->name);
181 scan->channel_num++;
182 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
183 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
184 tvh->tv_param->scan=0;
185 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
186 tv_channel_tmp=tv_channel_list;
187 if(tv_channel_tmp){
188 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
189 while(tv_channel_tmp){
190 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
191 if(tv_channel_tmp->next)
192 mp_msg(MSGT_TV,MSGL_INFO,",");
193 tv_channel_tmp=tv_channel_tmp->next;
195 mp_msg(MSGT_TV, MSGL_INFO, "\n");
197 if (!tv_channel_current) tv_channel_current=tv_channel_list;
198 if (tv_channel_current)
199 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
200 free(tvh->scan);
201 tvh->scan=NULL;
202 }else{
203 cl = tvh->chanlist_s[scan->channel_num];
204 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
205 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
209 /* ================== DEMUX_TV ===================== */
211 Return value:
212 0 = EOF(?) or no stream
213 1 = successfully read a packet
215 /* fill demux->video and demux->audio */
217 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
219 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
220 demux_packet_t* dp;
221 unsigned int len=0;
223 /* ================== ADD AUDIO PACKET =================== */
225 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
226 tvh->functions->control(tvh->priv,
227 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
229 len = tvh->functions->get_audio_framesize(tvh->priv);
231 dp=new_demux_packet(len);
232 dp->keyframe = true;
233 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
234 ds_add_packet(demux->audio,dp);
237 /* ================== ADD VIDEO PACKET =================== */
239 if (ds==demux->video && tvh->functions->control(tvh->priv,
240 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
242 len = tvh->functions->get_video_framesize(tvh->priv);
243 dp=new_demux_packet(len);
244 dp->keyframe = true;
245 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
246 ds_add_packet(demux->video,dp);
249 if (tvh->tv_param->scan) tv_scan(tvh);
250 return 1;
253 static int norm_from_string(tvi_handle_t *tvh, char* norm)
255 const tvi_functions_t *funcs = tvh->functions;
256 char str[20];
257 int ret;
259 strncpy(str, norm, sizeof(str)-1);
260 str[sizeof(str)-1] = '\0';
261 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
263 if (ret == TVI_CONTROL_TRUE) {
264 int *v = (int *)str;
265 return *v;
268 if(ret!=TVI_CONTROL_UNKNOWN)
270 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm,"default");
271 return 0;
274 if (!strcasecmp(norm, "pal"))
275 return TV_NORM_PAL;
276 else if (!strcasecmp(norm, "ntsc"))
277 return TV_NORM_NTSC;
278 else if (!strcasecmp(norm, "secam"))
279 return TV_NORM_SECAM;
280 else if (!strcasecmp(norm, "palnc"))
281 return TV_NORM_PALNC;
282 else if (!strcasecmp(norm, "palm"))
283 return TV_NORM_PALM;
284 else if (!strcasecmp(norm, "paln"))
285 return TV_NORM_PALN;
286 else if (!strcasecmp(norm, "ntscjp"))
287 return TV_NORM_NTSCJP;
288 else {
289 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
290 return TV_NORM_PAL;
294 static void parse_channels(tvi_handle_t *tvh)
296 char** channels = tvh->tv_param->channels;
298 mp_tmsg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
299 tv_channel_list = malloc(sizeof(tv_channels_t));
300 tv_channel_list->index=1;
301 tv_channel_list->next=NULL;
302 tv_channel_list->prev=NULL;
303 tv_channel_current = tv_channel_list;
304 tv_channel_current->norm = tvh->norm;
306 while (*channels) {
307 char* tmp = *(channels++);
308 char* sep = strchr(tmp,'-');
309 int i;
310 struct CHANLIST cl;
312 if (!sep) continue; // Wrong syntax, but mplayer should not crash
314 av_strlcpy(tv_channel_current->name, sep + 1,
315 sizeof(tv_channel_current->name));
316 sep[0] = '\0';
317 strncpy(tv_channel_current->number, tmp, 5);
318 tv_channel_current->number[4]='\0';
320 while ((sep=strchr(tv_channel_current->name, '_')))
321 sep[0] = ' ';
323 // if channel number is a number and larger than 1000 threat it as frequency
324 // tmp still contain pointer to null-terminated string with channel number here
325 if (atoi(tmp)>1000){
326 tv_channel_current->freq=atoi(tmp);
327 }else{
328 tv_channel_current->freq = 0;
329 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
330 cl = tvh->chanlist_s[i];
331 if (!strcasecmp(cl.name, tv_channel_current->number)) {
332 tv_channel_current->freq=cl.freq;
333 break;
337 if (tv_channel_current->freq == 0)
338 mp_tmsg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
339 tv_channel_current->number, tv_channel_current->name);
340 else {
341 sep = strchr(tv_channel_current->name, '-');
342 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
344 if ( sep ) {
345 i = atoi (sep+1);
346 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
347 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
348 sep[0] = '\0';
351 sep = strchr(tv_channel_current->name, '=');
352 if ( sep ) {
353 tv_channel_current->norm = norm_from_string(tvh, sep+1);
354 sep[0] = '\0';
358 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
359 tv_channel_current->number, tv_channel_current->name,
360 (float)tv_channel_current->freq/1000);*/
362 tv_channel_current->next = malloc(sizeof(tv_channels_t));
363 tv_channel_current->next->index = tv_channel_current->index + 1;
364 tv_channel_current->next->prev = tv_channel_current;
365 tv_channel_current->next->next = NULL;
366 tv_channel_current = tv_channel_current->next;
367 tv_channel_current->norm = tvh->norm;
369 if (tv_channel_current->prev)
370 tv_channel_current->prev->next = NULL;
371 free(tv_channel_current);
374 int tv_set_norm(tvi_handle_t *tvh, char* norm)
376 tvh->norm = norm_from_string(tvh, norm);
378 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm : %s\n", norm);
379 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
380 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
381 return 0;
383 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
384 &tvh->tv_param->teletext);
385 return 1;
388 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
390 tvh->norm = norm;
392 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", norm);
393 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
394 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
395 return 0;
398 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
399 &tvh->tv_param->teletext);
400 return 1;
403 static int open_tv(tvi_handle_t *tvh)
405 int i;
406 const tvi_functions_t *funcs = tvh->functions;
407 int tv_fmt_list[] = {
408 IMGFMT_YV12,
409 IMGFMT_I420,
410 IMGFMT_UYVY,
411 IMGFMT_YUY2,
412 IMGFMT_RGB32,
413 IMGFMT_RGB24,
414 IMGFMT_RGB16,
415 IMGFMT_RGB15
418 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
420 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
421 return 0;
424 if (tvh->tv_param->outfmt == -1)
425 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
427 tvh->tv_param->outfmt = tv_fmt_list[i];
428 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
429 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
430 break;
432 else
434 switch(tvh->tv_param->outfmt)
436 case IMGFMT_YV12:
437 case IMGFMT_I420:
438 case IMGFMT_UYVY:
439 case IMGFMT_YUY2:
440 case IMGFMT_RGB32:
441 case IMGFMT_RGB24:
442 case IMGFMT_BGR32:
443 case IMGFMT_BGR24:
444 case IMGFMT_BGR16:
445 case IMGFMT_BGR15:
446 break;
447 default:
448 mp_tmsg(MSGT_TV, MSGL_ERR,
449 "==================================================================\n"\
450 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
451 " This may cause buggy playback or program crash! Bug reports will\n"\
452 " be ignored! You should try again with YV12 (which is the default\n"\
453 " colorspace) and read the documentation!\n"\
454 "==================================================================\n"
455 ,tvh->tv_param->outfmt);
457 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
460 /* set some params got from cmdline */
461 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
463 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
464 if (0
465 #ifdef CONFIG_TV_V4L2
466 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
467 #endif
468 #ifdef CONFIG_TV_DSHOW
469 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
470 #endif
472 tv_set_norm_i(tvh, tvh->tv_param->normid);
473 else
474 #endif
475 tv_set_norm(tvh,tvh->tv_param->norm);
477 /* limits on w&h are norm-dependent -- JM */
478 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
479 // first tell the driver both width and height, some drivers do not support setting them independently.
480 int dim[2];
481 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
482 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
484 /* set width */
485 if (tvh->tv_param->width != -1)
487 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
488 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
489 else
491 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tvh->tv_param->width);
492 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
496 /* set height */
497 if (tvh->tv_param->height != -1)
499 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
500 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
501 else
503 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tvh->tv_param->height);
504 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
508 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
510 mp_tmsg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
511 goto done;
514 /* select channel list */
515 for (i = 0; chanlists[i].name != NULL; i++)
517 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
519 tvh->chanlist = i;
520 tvh->chanlist_s = chanlists[i].list;
521 break;
525 if (tvh->chanlist == -1)
526 mp_tmsg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
527 tvh->tv_param->chanlist);
528 else
529 mp_tmsg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
530 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
532 if (tvh->tv_param->freq && tvh->tv_param->channel)
534 mp_tmsg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
535 goto done;
538 /* Handle channel names */
539 if (tvh->tv_param->channels) {
540 parse_channels(tvh);
541 } else
542 tv_channel_last_real = malloc(5);
544 if (tv_channel_list) {
545 int i;
546 int channel = 0;
547 if (tvh->tv_param->channel)
549 if (isdigit(*tvh->tv_param->channel))
550 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
551 channel = atoi(tvh->tv_param->channel);
552 else
554 /* if tvh->tv_param->channel does not begin with a digit
555 set the first channel that contains tvh->tv_param->channel in its name */
557 tv_channel_current = tv_channel_list;
558 while ( tv_channel_current ) {
559 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
560 break;
561 tv_channel_current = tv_channel_current->next;
563 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
566 else
567 channel = 1;
569 if ( channel ) {
570 tv_channel_current = tv_channel_list;
571 for (i = 1; i < channel; i++)
572 if (tv_channel_current->next)
573 tv_channel_current = tv_channel_current->next;
576 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
577 tv_channel_current->name, (float)tv_channel_current->freq/1000);
578 tv_set_norm_i(tvh, tv_channel_current->norm);
579 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
580 tv_channel_last = tv_channel_current;
581 } else {
582 /* we need to set frequency */
583 if (tvh->tv_param->freq)
585 unsigned long freq = atof(tvh->tv_param->freq)*16;
587 /* set freq in MHz */
588 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
590 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
591 mp_tmsg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
592 freq, (float)freq/16);
595 if (tvh->tv_param->channel) {
596 struct CHANLIST cl;
598 mp_tmsg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tvh->tv_param->channel);
599 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
601 cl = tvh->chanlist_s[i];
602 // printf("count%d: name: %s, freq: %d\n",
603 // i, cl.name, cl.freq);
604 if (!strcasecmp(cl.name, tvh->tv_param->channel))
606 strcpy(tv_channel_last_real, cl.name);
607 tvh->channel = i;
608 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
609 cl.name, (float)cl.freq/1000);
610 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
611 break;
617 /* grep frequency in chanlist */
619 unsigned long i2;
620 int freq;
622 tv_get_freq(tvh, &i2);
624 freq = (int) (((float)(i2/16))*1000)+250;
626 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
628 if (tvh->chanlist_s[i].freq == freq)
630 tvh->channel = i+1;
631 break;
636 done:
637 /* also start device! */
638 return 1;
641 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
643 int i;
644 tvi_handle_t* h;
645 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
646 mp_tmsg(MSGT_TV,MSGL_INFO,"Available drivers:\n");
647 for(i=0;tvi_driver_list[i];i++){
648 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
649 if(tvi_driver_list[i]->comment)
650 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
651 mp_msg(MSGT_TV,MSGL_INFO,"\n");
653 return NULL;
656 for(i=0;tvi_driver_list[i];i++){
657 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
658 h=tvi_driver_list[i]->tvi_init(tv_param);
659 //Requested driver initialization failed
660 if (!h && tv_param->driver)
661 return NULL;
662 //Driver initialization failed during autodetection process.
663 if (!h)
664 continue;
666 h->tv_param=tv_param;
667 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected driver: %s\n name: %s\n author: %s\n comment: %s\n", tvi_driver_list[i]->short_name,
668 tvi_driver_list[i]->name,
669 tvi_driver_list[i]->author,
670 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
671 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
672 return h;
676 if(tv_param->driver)
677 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
678 else
679 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
680 return NULL;
683 static int tv_uninit(tvi_handle_t *tvh)
685 int res;
686 if(!tvh) return 1;
687 if (!tvh->priv) return 1;
688 res=tvh->functions->uninit(tvh->priv);
689 if(res) {
690 free(tvh->priv);
691 tvh->priv=NULL;
693 return res;
696 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
698 tvi_handle_t *tvh;
699 sh_video_t *sh_video;
700 sh_audio_t *sh_audio = NULL;
701 const tvi_functions_t *funcs;
703 demuxer->priv=NULL;
704 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
705 if (!tvh->functions->init(tvh->priv)) return NULL;
707 tvh->demuxer = demuxer;
708 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
709 &(tvh->tv_param->teletext.device));
710 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
711 &demuxer->teletext);
713 if (!open_tv(tvh)){
714 tv_uninit(tvh);
715 return NULL;
717 funcs = tvh->functions;
718 demuxer->priv=tvh;
720 sh_video = new_sh_video(demuxer, 0);
722 /* get IMAGE FORMAT */
723 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
724 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
725 // sh_video->format = 0x0;
727 /* set FPS and FRAMETIME */
729 if(!sh_video->fps)
731 float tmp;
732 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
733 sh_video->fps = 25.0f; /* on PAL */
734 else sh_video->fps = tmp;
737 if (tvh->tv_param->fps != -1.0f)
738 sh_video->fps = tvh->tv_param->fps;
740 sh_video->frametime = 1.0f/sh_video->fps;
742 /* If playback only mode, go to immediate mode, fail silently */
743 if(tvh->tv_param->immediate == 1)
745 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
746 tvh->tv_param->noaudio = 1;
749 /* disable TV audio if -nosound is present */
750 if (!demuxer->audio || demuxer->audio->id == -2) {
751 tvh->tv_param->noaudio = 1;
754 /* set width */
755 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
757 /* set height */
758 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
760 demuxer->video->sh = sh_video;
761 sh_video->ds = demuxer->video;
762 demuxer->video->id = 0;
763 demuxer->seekable = 0;
765 /* here comes audio init */
766 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
768 int audio_format;
769 int sh_audio_format;
770 char buf[128];
772 /* yeah, audio is present */
774 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
775 &tvh->tv_param->audiorate);
777 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
778 goto no_audio;
780 switch(audio_format)
782 case AF_FORMAT_U8:
783 case AF_FORMAT_S8:
784 case AF_FORMAT_U16_LE:
785 case AF_FORMAT_U16_BE:
786 case AF_FORMAT_S16_LE:
787 case AF_FORMAT_S16_BE:
788 case AF_FORMAT_S32_LE:
789 case AF_FORMAT_S32_BE:
790 sh_audio_format = 0x1; /* PCM */
791 break;
792 case AF_FORMAT_IMA_ADPCM:
793 case AF_FORMAT_MU_LAW:
794 case AF_FORMAT_A_LAW:
795 case AF_FORMAT_MPEG2:
796 default:
797 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
798 af_fmt2str(audio_format, buf, 128), audio_format);
799 goto no_audio;
802 sh_audio = new_sh_audio(demuxer, 0);
804 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
805 &sh_audio->samplerate);
806 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
807 &sh_audio->samplesize);
808 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
809 &sh_audio->channels);
811 sh_audio->format = sh_audio_format;
812 sh_audio->sample_format = audio_format;
814 sh_audio->i_bps = sh_audio->o_bps =
815 sh_audio->samplerate * sh_audio->samplesize *
816 sh_audio->channels;
818 // emulate WF for win32 codecs:
819 sh_audio->wf = malloc(sizeof(*sh_audio->wf));
820 sh_audio->wf->wFormatTag = sh_audio->format;
821 sh_audio->wf->nChannels = sh_audio->channels;
822 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
823 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
824 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
825 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
827 mp_tmsg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
828 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
829 sh_audio->wf->nSamplesPerSec);
831 demuxer->audio->sh = sh_audio;
832 sh_audio->ds = demuxer->audio;
833 demuxer->audio->id = 0;
835 no_audio:
837 if(!(funcs->start(tvh->priv))){
838 // start failed :(
839 tv_uninit(tvh);
840 return NULL;
843 /* set color eq */
844 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
845 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
846 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
847 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
849 if(tvh->tv_param->gain!=-1)
850 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
851 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
853 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
854 &tvh->tv_param->teletext);
856 return demuxer;
859 static void demux_close_tv(demuxer_t *demuxer)
861 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
862 if (!tvh) return;
863 tv_uninit(tvh);
864 free(tvh);
865 demuxer->priv=NULL;
866 demuxer->teletext=NULL;
869 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
871 const tvi_functions_t *funcs = tvh->functions;
873 switch(opt)
875 case TV_COLOR_BRIGHTNESS:
876 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
877 case TV_COLOR_HUE:
878 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
879 case TV_COLOR_SATURATION:
880 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
881 case TV_COLOR_CONTRAST:
882 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
883 default:
884 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
887 return TVI_CONTROL_UNKNOWN;
890 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
892 const tvi_functions_t *funcs = tvh->functions;
894 switch(opt)
896 case TV_COLOR_BRIGHTNESS:
897 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
898 case TV_COLOR_HUE:
899 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
900 case TV_COLOR_SATURATION:
901 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
902 case TV_COLOR_CONTRAST:
903 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
904 default:
905 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
908 return TVI_CONTROL_UNKNOWN;
911 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
913 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
915 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
916 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
917 *freq, (float)*freq/16);
919 return 1;
922 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
924 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
926 // unsigned long freq = atof(tvh->tv_param->freq)*16;
928 /* set freq in MHz */
929 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
931 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
932 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
933 freq, (float)freq/16);
935 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
936 &tvh->tv_param->teletext);
937 return 1;
940 int tv_get_signal(tvi_handle_t *tvh)
942 int signal=0;
943 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
944 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
945 return 0;
947 return signal;
950 /*****************************************************************
951 * \brief tune current frequency by step_interval value
952 * \parameter step_interval increment value in 1/16 MHz
953 * \note frequency is rounded to 1/16 MHz value
954 * \return 1
957 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
958 unsigned long frequency;
960 tvh->tv_param->scan=0;
961 tv_get_freq(tvh,&frequency);
962 frequency+=step_interval;
963 return tv_set_freq(tvh,frequency);
966 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
968 struct CHANLIST cl;
970 tvh->tv_param->scan=0;
971 if (direction == TV_CHANNEL_LOWER)
973 if (tvh->channel-1 >= 0)
975 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
976 cl = tvh->chanlist_s[--tvh->channel];
977 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
978 cl.name, (float)cl.freq/1000);
979 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
983 if (direction == TV_CHANNEL_HIGHER)
985 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
987 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
988 cl = tvh->chanlist_s[++tvh->channel];
989 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
990 cl.name, (float)cl.freq/1000);
991 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
994 return 1;
997 int tv_step_channel(tvi_handle_t *tvh, int direction) {
998 tvh->tv_param->scan=0;
999 if (tv_channel_list) {
1000 if (direction == TV_CHANNEL_HIGHER) {
1001 tv_channel_last = tv_channel_current;
1002 if (tv_channel_current->next)
1003 tv_channel_current = tv_channel_current->next;
1004 else
1005 tv_channel_current = tv_channel_list;
1007 tv_set_norm_i(tvh, tv_channel_current->norm);
1008 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1009 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1010 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1012 if (direction == TV_CHANNEL_LOWER) {
1013 tv_channel_last = tv_channel_current;
1014 if (tv_channel_current->prev)
1015 tv_channel_current = tv_channel_current->prev;
1016 else
1017 while (tv_channel_current->next)
1018 tv_channel_current = tv_channel_current->next;
1019 tv_set_norm_i(tvh, tv_channel_current->norm);
1020 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1021 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1022 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1024 } else tv_step_channel_real(tvh, direction);
1025 return 1;
1028 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1029 int i;
1030 struct CHANLIST cl;
1032 tvh->tv_param->scan=0;
1033 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1034 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1036 cl = tvh->chanlist_s[i];
1037 // printf("count%d: name: %s, freq: %d\n",
1038 // i, cl.name, cl.freq);
1039 if (!strcasecmp(cl.name, channel))
1041 tvh->channel = i;
1042 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1043 cl.name, (float)cl.freq/1000);
1044 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1045 break;
1048 return 1;
1051 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1052 int i, channel_int;
1054 tvh->tv_param->scan=0;
1055 if (tv_channel_list) {
1056 tv_channel_last = tv_channel_current;
1057 channel_int = atoi(channel);
1058 tv_channel_current = tv_channel_list;
1059 for (i = 1; i < channel_int; i++)
1060 if (tv_channel_current->next)
1061 tv_channel_current = tv_channel_current->next;
1062 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1063 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1064 tv_set_norm_i(tvh, tv_channel_current->norm);
1065 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1066 } else tv_set_channel_real(tvh, channel);
1067 return 1;
1070 int tv_last_channel(tvi_handle_t *tvh) {
1072 tvh->tv_param->scan=0;
1073 if (tv_channel_list) {
1074 tv_channels_t *tmp;
1076 tmp = tv_channel_last;
1077 tv_channel_last = tv_channel_current;
1078 tv_channel_current = tmp;
1080 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1081 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1082 tv_set_norm_i(tvh, tv_channel_current->norm);
1083 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1084 } else {
1085 int i;
1086 struct CHANLIST cl;
1088 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1090 cl = tvh->chanlist_s[i];
1091 if (!strcasecmp(cl.name, tv_channel_last_real))
1093 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1094 tvh->channel = i;
1095 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1096 cl.name, (float)cl.freq/1000);
1097 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1098 break;
1102 return 1;
1105 int tv_step_norm(tvi_handle_t *tvh)
1107 tvh->norm++;
1108 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1109 &tvh->norm) != TVI_CONTROL_TRUE) {
1110 tvh->norm = 0;
1111 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1112 &tvh->norm) != TVI_CONTROL_TRUE) {
1113 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
1114 return 0;
1117 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1118 &tvh->tv_param->teletext);
1119 return 1;
1122 int tv_step_chanlist(tvi_handle_t *tvh)
1124 return 1;
1127 demuxer_desc_t demuxer_desc_tv = {
1128 "Tv card demuxer",
1129 "tv",
1130 "TV",
1131 "Alex Beregszaszi, Charles R. Henrich",
1132 "?",
1133 DEMUXER_TYPE_TV,
1134 0, // no autodetect
1135 NULL,
1136 demux_tv_fill_buffer,
1137 demux_open_tv,
1138 demux_close_tv,
1139 NULL,
1140 NULL