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