Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / tv.c
blobcb1fe59feeac1c4ca202ccd5b7d493f6574e354b
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_v4l;
62 extern const tvi_info_t tvi_info_v4l2;
63 extern const tvi_info_t tvi_info_bsdbt848;
65 /** List of drivers in autodetection order */
66 static const tvi_info_t* tvi_driver_list[]={
67 #ifdef CONFIG_TV_V4L2
68 &tvi_info_v4l2,
69 #endif
70 #ifdef CONFIG_TV_V4L1
71 &tvi_info_v4l,
72 #endif
73 #ifdef CONFIG_TV_BSDBT848
74 &tvi_info_bsdbt848,
75 #endif
76 #ifdef CONFIG_TV_DSHOW
77 &tvi_info_dshow,
78 #endif
79 &tvi_info_dummy,
80 NULL
83 void tv_start_scan(tvi_handle_t *tvh, int start)
85 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
86 tvh->tv_param->scan=start?1:0;
89 static void tv_scan(tvi_handle_t *tvh)
91 unsigned int now;
92 struct CHANLIST cl;
93 tv_channels_t *tv_channel_tmp=NULL;
94 tv_channels_t *tv_channel_add=NULL;
95 tv_scan_t* scan;
96 int found=0, index=1;
98 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
99 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
101 mp_tmsg(MSGT_TV, MSGL_WARN, "Channel scanner is not available without tuner\n");
102 tvh->tv_param->scan=0;
103 return;
106 scan = tvh->scan;
107 now=GetTimer();
108 if (!scan) {
109 scan=calloc(1,sizeof(tv_scan_t));
110 tvh->scan=scan;
111 cl = tvh->chanlist_s[scan->channel_num];
112 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
113 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
115 if(scan->scan_timer>now)
116 return;
118 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
119 cl = tvh->chanlist_s[scan->channel_num];
120 tv_channel_tmp=tv_channel_list;
121 while (tv_channel_tmp) {
122 index++;
123 if (cl.freq==tv_channel_tmp->freq){
124 found=1;
125 break;
127 tv_channel_add=tv_channel_tmp;
128 tv_channel_tmp=tv_channel_tmp->next;
130 if (!found) {
131 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
132 scan->new_channels++;
133 tv_channel_tmp = malloc(sizeof(tv_channels_t));
134 tv_channel_tmp->index=index;
135 tv_channel_tmp->next=NULL;
136 tv_channel_tmp->prev=tv_channel_add;
137 tv_channel_tmp->freq=cl.freq;
138 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
139 strncpy(tv_channel_tmp->number, cl.name, 5);
140 tv_channel_tmp->number[4]='\0';
141 if (!tv_channel_list)
142 tv_channel_list=tv_channel_tmp;
143 else {
144 tv_channel_add->next=tv_channel_tmp;
145 tv_channel_list->prev=tv_channel_tmp;
147 }else
148 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
149 tv_channel_tmp->number,tv_channel_tmp->name);
151 scan->channel_num++;
152 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
153 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
154 tvh->tv_param->scan=0;
155 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
156 tv_channel_tmp=tv_channel_list;
157 if(tv_channel_tmp){
158 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
159 while(tv_channel_tmp){
160 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
161 if(tv_channel_tmp->next)
162 mp_msg(MSGT_TV,MSGL_INFO,",");
163 tv_channel_tmp=tv_channel_tmp->next;
165 mp_msg(MSGT_TV, MSGL_INFO, "\n");
167 if (!tv_channel_current) tv_channel_current=tv_channel_list;
168 if (tv_channel_current)
169 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
170 free(tvh->scan);
171 tvh->scan=NULL;
172 }else{
173 cl = tvh->chanlist_s[scan->channel_num];
174 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
175 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
179 /* ================== DEMUX_TV ===================== */
181 Return value:
182 0 = EOF(?) or no stream
183 1 = successfully read a packet
185 /* fill demux->video and demux->audio */
187 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
189 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
190 demux_packet_t* dp;
191 unsigned int len=0;
193 /* ================== ADD AUDIO PACKET =================== */
195 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
196 tvh->functions->control(tvh->priv,
197 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
199 len = tvh->functions->get_audio_framesize(tvh->priv);
201 dp=new_demux_packet(len);
202 dp->flags|=1; /* Keyframe */
203 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
204 ds_add_packet(demux->audio,dp);
207 /* ================== ADD VIDEO PACKET =================== */
209 if (ds==demux->video && tvh->functions->control(tvh->priv,
210 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
212 len = tvh->functions->get_video_framesize(tvh->priv);
213 dp=new_demux_packet(len);
214 dp->flags|=1; /* Keyframe */
215 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
216 ds_add_packet(demux->video,dp);
219 if (tvh->tv_param->scan) tv_scan(tvh);
220 return 1;
223 static int norm_from_string(tvi_handle_t *tvh, char* norm)
225 const tvi_functions_t *funcs = tvh->functions;
226 char str[20];
227 int ret;
229 strncpy(str, norm, sizeof(str)-1);
230 str[sizeof(str)-1] = '\0';
231 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
233 if(ret==TVI_CONTROL_TRUE)
234 return *(int *)str;
236 if(ret!=TVI_CONTROL_UNKNOWN)
238 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm,"default");
239 return 0;
242 if (!strcasecmp(norm, "pal"))
243 return TV_NORM_PAL;
244 else if (!strcasecmp(norm, "ntsc"))
245 return TV_NORM_NTSC;
246 else if (!strcasecmp(norm, "secam"))
247 return TV_NORM_SECAM;
248 else if (!strcasecmp(norm, "palnc"))
249 return TV_NORM_PALNC;
250 else if (!strcasecmp(norm, "palm"))
251 return TV_NORM_PALM;
252 else if (!strcasecmp(norm, "paln"))
253 return TV_NORM_PALN;
254 else if (!strcasecmp(norm, "ntscjp"))
255 return TV_NORM_NTSCJP;
256 else {
257 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
258 return TV_NORM_PAL;
262 static void parse_channels(tvi_handle_t *tvh)
264 char** channels = tvh->tv_param->channels;
266 mp_tmsg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
267 tv_channel_list = malloc(sizeof(tv_channels_t));
268 tv_channel_list->index=1;
269 tv_channel_list->next=NULL;
270 tv_channel_list->prev=NULL;
271 tv_channel_current = tv_channel_list;
272 tv_channel_current->norm = tvh->norm;
274 while (*channels) {
275 char* tmp = *(channels++);
276 char* sep = strchr(tmp,'-');
277 int i;
278 struct CHANLIST cl;
280 if (!sep) continue; // Wrong syntax, but mplayer should not crash
282 av_strlcpy(tv_channel_current->name, sep + 1,
283 sizeof(tv_channel_current->name));
284 sep[0] = '\0';
285 strncpy(tv_channel_current->number, tmp, 5);
286 tv_channel_current->number[4]='\0';
288 while ((sep=strchr(tv_channel_current->name, '_')))
289 sep[0] = ' ';
291 // if channel number is a number and larger than 1000 threat it as frequency
292 // tmp still contain pointer to null-terminated string with channel number here
293 if (atoi(tmp)>1000){
294 tv_channel_current->freq=atoi(tmp);
295 }else{
296 tv_channel_current->freq = 0;
297 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
298 cl = tvh->chanlist_s[i];
299 if (!strcasecmp(cl.name, tv_channel_current->number)) {
300 tv_channel_current->freq=cl.freq;
301 break;
305 if (tv_channel_current->freq == 0)
306 mp_tmsg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
307 tv_channel_current->number, tv_channel_current->name);
308 else {
309 sep = strchr(tv_channel_current->name, '-');
310 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
312 if ( sep ) {
313 i = atoi (sep+1);
314 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
315 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
316 sep[0] = '\0';
319 sep = strchr(tv_channel_current->name, '=');
320 if ( sep ) {
321 tv_channel_current->norm = norm_from_string(tvh, sep+1);
322 sep[0] = '\0';
326 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
327 tv_channel_current->number, tv_channel_current->name,
328 (float)tv_channel_current->freq/1000);*/
330 tv_channel_current->next = malloc(sizeof(tv_channels_t));
331 tv_channel_current->next->index = tv_channel_current->index + 1;
332 tv_channel_current->next->prev = tv_channel_current;
333 tv_channel_current->next->next = NULL;
334 tv_channel_current = tv_channel_current->next;
335 tv_channel_current->norm = tvh->norm;
337 if (tv_channel_current->prev)
338 tv_channel_current->prev->next = NULL;
339 free(tv_channel_current);
342 int tv_set_norm(tvi_handle_t *tvh, char* norm)
344 tvh->norm = norm_from_string(tvh, norm);
346 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm : %s\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");
349 return 0;
351 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
352 &tvh->tv_param->teletext);
353 return 1;
356 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
358 tvh->norm = norm;
360 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", norm);
361 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
362 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
363 return 0;
366 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
367 &tvh->tv_param->teletext);
368 return 1;
371 static int open_tv(tvi_handle_t *tvh)
373 int i;
374 const tvi_functions_t *funcs = tvh->functions;
375 int tv_fmt_list[] = {
376 IMGFMT_YV12,
377 IMGFMT_I420,
378 IMGFMT_UYVY,
379 IMGFMT_YUY2,
380 IMGFMT_RGB32,
381 IMGFMT_RGB24,
382 IMGFMT_RGB16,
383 IMGFMT_RGB15
386 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
388 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
389 return 0;
392 if (tvh->tv_param->outfmt == -1)
393 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
395 tvh->tv_param->outfmt = tv_fmt_list[i];
396 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
397 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
398 break;
400 else
402 switch(tvh->tv_param->outfmt)
404 case IMGFMT_YV12:
405 case IMGFMT_I420:
406 case IMGFMT_UYVY:
407 case IMGFMT_YUY2:
408 case IMGFMT_RGB32:
409 case IMGFMT_RGB24:
410 case IMGFMT_BGR32:
411 case IMGFMT_BGR24:
412 case IMGFMT_BGR16:
413 case IMGFMT_BGR15:
414 break;
415 default:
416 mp_tmsg(MSGT_TV, MSGL_ERR,
417 "==================================================================\n"\
418 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
419 " This may cause buggy playback or program crash! Bug reports will\n"\
420 " be ignored! You should try again with YV12 (which is the default\n"\
421 " colorspace) and read the documentation!\n"\
422 "==================================================================\n"
423 ,tvh->tv_param->outfmt);
425 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
428 /* set some params got from cmdline */
429 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
431 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
432 if (0
433 #ifdef CONFIG_TV_V4L2
434 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
435 #endif
436 #ifdef CONFIG_TV_DSHOW
437 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
438 #endif
440 tv_set_norm_i(tvh, tvh->tv_param->normid);
441 else
442 #endif
443 tv_set_norm(tvh,tvh->tv_param->norm);
445 #ifdef CONFIG_TV_V4L1
446 if ( tvh->tv_param->mjpeg )
448 /* set width to expected value */
449 if (tvh->tv_param->width == -1)
451 tvh->tv_param->width = 704/tvh->tv_param->decimation;
453 if (tvh->tv_param->height == -1)
455 if ( tvh->norm != TV_NORM_NTSC )
456 tvh->tv_param->height = 576/tvh->tv_param->decimation;
457 else
458 tvh->tv_param->height = 480/tvh->tv_param->decimation;
460 mp_tmsg(MSGT_TV, MSGL_INFO,
461 " MJP: width %d height %d\n", tvh->tv_param->width, tvh->tv_param->height);
463 #endif
465 /* limits on w&h are norm-dependent -- JM */
466 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
467 // first tell the driver both width and height, some drivers do not support setting them independently.
468 int dim[2];
469 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
470 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
472 /* set width */
473 if (tvh->tv_param->width != -1)
475 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
476 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
477 else
479 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tvh->tv_param->width);
480 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
484 /* set height */
485 if (tvh->tv_param->height != -1)
487 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
488 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
489 else
491 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tvh->tv_param->height);
492 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
496 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
498 mp_tmsg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
499 goto done;
502 /* select channel list */
503 for (i = 0; chanlists[i].name != NULL; i++)
505 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
507 tvh->chanlist = i;
508 tvh->chanlist_s = chanlists[i].list;
509 break;
513 if (tvh->chanlist == -1)
514 mp_tmsg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
515 tvh->tv_param->chanlist);
516 else
517 mp_tmsg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
518 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
520 if (tvh->tv_param->freq && tvh->tv_param->channel)
522 mp_tmsg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
523 goto done;
526 /* Handle channel names */
527 if (tvh->tv_param->channels) {
528 parse_channels(tvh);
529 } else
530 tv_channel_last_real = malloc(5);
532 if (tv_channel_list) {
533 int i;
534 int channel = 0;
535 if (tvh->tv_param->channel)
537 if (isdigit(*tvh->tv_param->channel))
538 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
539 channel = atoi(tvh->tv_param->channel);
540 else
542 /* if tvh->tv_param->channel does not begin with a digit
543 set the first channel that contains tvh->tv_param->channel in its name */
545 tv_channel_current = tv_channel_list;
546 while ( tv_channel_current ) {
547 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
548 break;
549 tv_channel_current = tv_channel_current->next;
551 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
554 else
555 channel = 1;
557 if ( channel ) {
558 tv_channel_current = tv_channel_list;
559 for (i = 1; i < channel; i++)
560 if (tv_channel_current->next)
561 tv_channel_current = tv_channel_current->next;
564 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
565 tv_channel_current->name, (float)tv_channel_current->freq/1000);
566 tv_set_norm_i(tvh, tv_channel_current->norm);
567 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
568 tv_channel_last = tv_channel_current;
569 } else {
570 /* we need to set frequency */
571 if (tvh->tv_param->freq)
573 unsigned long freq = atof(tvh->tv_param->freq)*16;
575 /* set freq in MHz */
576 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
578 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
579 mp_tmsg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
580 freq, (float)freq/16);
583 if (tvh->tv_param->channel) {
584 struct CHANLIST cl;
586 mp_tmsg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tvh->tv_param->channel);
587 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
589 cl = tvh->chanlist_s[i];
590 // printf("count%d: name: %s, freq: %d\n",
591 // i, cl.name, cl.freq);
592 if (!strcasecmp(cl.name, tvh->tv_param->channel))
594 strcpy(tv_channel_last_real, cl.name);
595 tvh->channel = i;
596 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
597 cl.name, (float)cl.freq/1000);
598 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
599 break;
605 /* grep frequency in chanlist */
607 unsigned long i2;
608 int freq;
610 tv_get_freq(tvh, &i2);
612 freq = (int) (((float)(i2/16))*1000)+250;
614 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
616 if (tvh->chanlist_s[i].freq == freq)
618 tvh->channel = i+1;
619 break;
624 done:
625 /* also start device! */
626 return 1;
629 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
631 int i;
632 tvi_handle_t* h;
633 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
634 mp_tmsg(MSGT_TV,MSGL_INFO,"Available drivers:\n");
635 for(i=0;tvi_driver_list[i];i++){
636 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
637 if(tvi_driver_list[i]->comment)
638 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
639 mp_msg(MSGT_TV,MSGL_INFO,"\n");
641 return NULL;
644 for(i=0;tvi_driver_list[i];i++){
645 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
646 h=tvi_driver_list[i]->tvi_init(tv_param);
647 //Requested driver initialization failed
648 if (!h && tv_param->driver)
649 return NULL;
650 //Driver initialization failed during autodetection process.
651 if (!h)
652 continue;
654 h->tv_param=tv_param;
655 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,
656 tvi_driver_list[i]->name,
657 tvi_driver_list[i]->author,
658 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
659 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
660 return h;
664 if(tv_param->driver)
665 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
666 else
667 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
668 return NULL;
671 static int tv_uninit(tvi_handle_t *tvh)
673 int res;
674 if(!tvh) return 1;
675 if (!tvh->priv) return 1;
676 res=tvh->functions->uninit(tvh->priv);
677 if(res) {
678 free(tvh->priv);
679 tvh->priv=NULL;
681 return res;
684 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
686 tvi_handle_t *tvh;
687 sh_video_t *sh_video;
688 sh_audio_t *sh_audio = NULL;
689 const tvi_functions_t *funcs;
691 demuxer->priv=NULL;
692 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
693 if (!tvh->functions->init(tvh->priv)) return NULL;
695 tvh->demuxer = demuxer;
696 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
697 &(tvh->tv_param->teletext.device));
698 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
699 &demuxer->teletext);
701 if (!open_tv(tvh)){
702 tv_uninit(tvh);
703 return NULL;
705 funcs = tvh->functions;
706 demuxer->priv=tvh;
708 sh_video = new_sh_video(demuxer, 0);
710 /* get IMAGE FORMAT */
711 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
712 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
713 // sh_video->format = 0x0;
715 /* set FPS and FRAMETIME */
717 if(!sh_video->fps)
719 float tmp;
720 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
721 sh_video->fps = 25.0f; /* on PAL */
722 else sh_video->fps = tmp;
725 if (tvh->tv_param->fps != -1.0f)
726 sh_video->fps = tvh->tv_param->fps;
728 sh_video->frametime = 1.0f/sh_video->fps;
730 /* If playback only mode, go to immediate mode, fail silently */
731 if(tvh->tv_param->immediate == 1)
733 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
734 tvh->tv_param->noaudio = 1;
737 /* disable TV audio if -nosound is present */
738 if (!demuxer->audio || demuxer->audio->id == -2) {
739 tvh->tv_param->noaudio = 1;
742 /* set width */
743 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
745 /* set height */
746 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
748 demuxer->video->sh = sh_video;
749 sh_video->ds = demuxer->video;
750 demuxer->video->id = 0;
751 demuxer->seekable = 0;
753 /* here comes audio init */
754 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
756 int audio_format;
757 int sh_audio_format;
758 char buf[128];
760 /* yeah, audio is present */
762 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
763 &tvh->tv_param->audiorate);
765 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
766 goto no_audio;
768 switch(audio_format)
770 case AF_FORMAT_U8:
771 case AF_FORMAT_S8:
772 case AF_FORMAT_U16_LE:
773 case AF_FORMAT_U16_BE:
774 case AF_FORMAT_S16_LE:
775 case AF_FORMAT_S16_BE:
776 case AF_FORMAT_S32_LE:
777 case AF_FORMAT_S32_BE:
778 sh_audio_format = 0x1; /* PCM */
779 break;
780 case AF_FORMAT_IMA_ADPCM:
781 case AF_FORMAT_MU_LAW:
782 case AF_FORMAT_A_LAW:
783 case AF_FORMAT_MPEG2:
784 default:
785 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
786 af_fmt2str(audio_format, buf, 128), audio_format);
787 goto no_audio;
790 sh_audio = new_sh_audio(demuxer, 0);
792 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
793 &sh_audio->samplerate);
794 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
795 &sh_audio->samplesize);
796 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
797 &sh_audio->channels);
799 sh_audio->format = sh_audio_format;
800 sh_audio->sample_format = audio_format;
802 sh_audio->i_bps = sh_audio->o_bps =
803 sh_audio->samplerate * sh_audio->samplesize *
804 sh_audio->channels;
806 // emulate WF for win32 codecs:
807 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
808 sh_audio->wf->wFormatTag = sh_audio->format;
809 sh_audio->wf->nChannels = sh_audio->channels;
810 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
811 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
812 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
813 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
815 mp_tmsg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
816 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
817 sh_audio->wf->nSamplesPerSec);
819 demuxer->audio->sh = sh_audio;
820 sh_audio->ds = demuxer->audio;
821 demuxer->audio->id = 0;
823 no_audio:
825 if(!(funcs->start(tvh->priv))){
826 // start failed :(
827 tv_uninit(tvh);
828 return NULL;
831 /* set color eq */
832 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
833 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
834 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
835 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
837 if(tvh->tv_param->gain!=-1)
838 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
839 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
841 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
842 &tvh->tv_param->teletext);
844 return demuxer;
847 static void demux_close_tv(demuxer_t *demuxer)
849 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
850 if (!tvh) return;
851 tv_uninit(tvh);
852 free(tvh);
853 demuxer->priv=NULL;
854 demuxer->teletext=NULL;
857 /* utilities for mplayer (not mencoder!!) */
858 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
860 const tvi_functions_t *funcs = tvh->functions;
862 switch(opt)
864 case TV_COLOR_BRIGHTNESS:
865 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
866 case TV_COLOR_HUE:
867 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
868 case TV_COLOR_SATURATION:
869 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
870 case TV_COLOR_CONTRAST:
871 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
872 default:
873 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
876 return TVI_CONTROL_UNKNOWN;
879 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
881 const tvi_functions_t *funcs = tvh->functions;
883 switch(opt)
885 case TV_COLOR_BRIGHTNESS:
886 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
887 case TV_COLOR_HUE:
888 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
889 case TV_COLOR_SATURATION:
890 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
891 case TV_COLOR_CONTRAST:
892 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
893 default:
894 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
897 return TVI_CONTROL_UNKNOWN;
900 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
902 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
904 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
905 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
906 *freq, (float)*freq/16);
908 return 1;
911 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
913 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
915 // unsigned long freq = atof(tvh->tv_param->freq)*16;
917 /* set freq in MHz */
918 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
920 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
921 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
922 freq, (float)freq/16);
924 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
925 &tvh->tv_param->teletext);
926 return 1;
929 int tv_get_signal(tvi_handle_t *tvh)
931 int signal=0;
932 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
933 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
934 return 0;
936 return signal;
939 /*****************************************************************
940 * \brief tune current frequency by step_interval value
941 * \parameter step_interval increment value in 1/16 MHz
942 * \note frequency is rounded to 1/16 MHz value
943 * \return 1
946 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
947 unsigned long frequency;
949 tvh->tv_param->scan=0;
950 tv_get_freq(tvh,&frequency);
951 frequency+=step_interval;
952 return tv_set_freq(tvh,frequency);
955 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
957 struct CHANLIST cl;
959 tvh->tv_param->scan=0;
960 if (direction == TV_CHANNEL_LOWER)
962 if (tvh->channel-1 >= 0)
964 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
965 cl = tvh->chanlist_s[--tvh->channel];
966 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
967 cl.name, (float)cl.freq/1000);
968 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
972 if (direction == TV_CHANNEL_HIGHER)
974 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
976 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
977 cl = tvh->chanlist_s[++tvh->channel];
978 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
979 cl.name, (float)cl.freq/1000);
980 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
983 return 1;
986 int tv_step_channel(tvi_handle_t *tvh, int direction) {
987 tvh->tv_param->scan=0;
988 if (tv_channel_list) {
989 if (direction == TV_CHANNEL_HIGHER) {
990 tv_channel_last = tv_channel_current;
991 if (tv_channel_current->next)
992 tv_channel_current = tv_channel_current->next;
993 else
994 tv_channel_current = tv_channel_list;
996 tv_set_norm_i(tvh, tv_channel_current->norm);
997 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
998 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
999 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1001 if (direction == TV_CHANNEL_LOWER) {
1002 tv_channel_last = tv_channel_current;
1003 if (tv_channel_current->prev)
1004 tv_channel_current = tv_channel_current->prev;
1005 else
1006 while (tv_channel_current->next)
1007 tv_channel_current = tv_channel_current->next;
1008 tv_set_norm_i(tvh, tv_channel_current->norm);
1009 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1010 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1011 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1013 } else tv_step_channel_real(tvh, direction);
1014 return 1;
1017 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1018 int i;
1019 struct CHANLIST cl;
1021 tvh->tv_param->scan=0;
1022 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1023 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1025 cl = tvh->chanlist_s[i];
1026 // printf("count%d: name: %s, freq: %d\n",
1027 // i, cl.name, cl.freq);
1028 if (!strcasecmp(cl.name, channel))
1030 tvh->channel = i;
1031 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1032 cl.name, (float)cl.freq/1000);
1033 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1034 break;
1037 return 1;
1040 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1041 int i, channel_int;
1043 tvh->tv_param->scan=0;
1044 if (tv_channel_list) {
1045 tv_channel_last = tv_channel_current;
1046 channel_int = atoi(channel);
1047 tv_channel_current = tv_channel_list;
1048 for (i = 1; i < channel_int; i++)
1049 if (tv_channel_current->next)
1050 tv_channel_current = tv_channel_current->next;
1051 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1052 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1053 tv_set_norm_i(tvh, tv_channel_current->norm);
1054 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1055 } else tv_set_channel_real(tvh, channel);
1056 return 1;
1059 int tv_last_channel(tvi_handle_t *tvh) {
1061 tvh->tv_param->scan=0;
1062 if (tv_channel_list) {
1063 tv_channels_t *tmp;
1065 tmp = tv_channel_last;
1066 tv_channel_last = tv_channel_current;
1067 tv_channel_current = tmp;
1069 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1070 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1071 tv_set_norm_i(tvh, tv_channel_current->norm);
1072 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1073 } else {
1074 int i;
1075 struct CHANLIST cl;
1077 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1079 cl = tvh->chanlist_s[i];
1080 if (!strcasecmp(cl.name, tv_channel_last_real))
1082 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1083 tvh->channel = i;
1084 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1085 cl.name, (float)cl.freq/1000);
1086 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1087 break;
1091 return 1;
1094 int tv_step_norm(tvi_handle_t *tvh)
1096 tvh->norm++;
1097 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1098 &tvh->norm) != TVI_CONTROL_TRUE) {
1099 tvh->norm = 0;
1100 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1101 &tvh->norm) != TVI_CONTROL_TRUE) {
1102 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
1103 return 0;
1106 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1107 &tvh->tv_param->teletext);
1108 return 1;
1111 int tv_step_chanlist(tvi_handle_t *tvh)
1113 return 1;
1116 demuxer_desc_t demuxer_desc_tv = {
1117 "Tv card demuxer",
1118 "tv",
1119 "TV",
1120 "Alex Beregszaszi, Charles R. Henrich",
1121 "?",
1122 DEMUXER_TYPE_TV,
1123 0, // no autodetect
1124 NULL,
1125 demux_tv_fill_buffer,
1126 demux_open_tv,
1127 demux_close_tv,
1128 NULL,
1129 NULL