Fix compilation after FFmpeg r21353.
[mplayer/glamo.git] / stream / tv.c
blob63497472c9777a767f0f7188b7dd06410dc8f669
1 /*
2 TV Interface for MPlayer
4 (C) Alex Beregszaszi
6 API idea based on libvo2
8 Feb 19, 2002: Significant rewrites by Charles R. Henrich (henrich@msu.edu)
9 to add support for audio, and bktr *BSD support.
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <sys/time.h>
20 #include "config.h"
23 #include "mp_msg.h"
24 #include "help_mp.h"
26 #include "stream.h"
27 #include "libmpdemux/demuxer.h"
28 #include "libmpdemux/stheader.h"
30 #include "libaf/af_format.h"
31 #include "libmpcodecs/img_format.h"
32 #include "libmpcodecs/dec_teletext.h"
33 #include "libavutil/avstring.h"
34 #include "osdep/timer.h"
36 #include "tv.h"
38 #include "frequencies.h"
40 tv_channels_t *tv_channel_list;
41 tv_channels_t *tv_channel_current, *tv_channel_last;
42 char *tv_channel_last_real;
44 /* enumerating drivers (like in stream.c) */
45 extern const tvi_info_t tvi_info_dummy;
46 extern const tvi_info_t tvi_info_dshow;
47 extern const tvi_info_t tvi_info_v4l;
48 extern const tvi_info_t tvi_info_v4l2;
49 extern const tvi_info_t tvi_info_bsdbt848;
51 /** List of drivers in autodetection order */
52 static const tvi_info_t* tvi_driver_list[]={
53 #ifdef CONFIG_TV_V4L2
54 &tvi_info_v4l2,
55 #endif
56 #ifdef CONFIG_TV_V4L1
57 &tvi_info_v4l,
58 #endif
59 #ifdef CONFIG_TV_BSDBT848
60 &tvi_info_bsdbt848,
61 #endif
62 #ifdef CONFIG_TV_DSHOW
63 &tvi_info_dshow,
64 #endif
65 &tvi_info_dummy,
66 NULL
69 void tv_start_scan(tvi_handle_t *tvh, int start)
71 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
72 tvh->tv_param->scan=start?1:0;
75 static void tv_scan(tvi_handle_t *tvh)
77 unsigned int now;
78 struct CHANLIST cl;
79 tv_channels_t *tv_channel_tmp=NULL;
80 tv_channels_t *tv_channel_add=NULL;
81 tv_scan_t* scan;
82 int found=0, index=1;
84 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
85 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
87 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ScannerNotAvailableWithoutTuner);
88 tvh->tv_param->scan=0;
89 return;
92 scan = tvh->scan;
93 now=GetTimer();
94 if (!scan) {
95 scan=calloc(1,sizeof(tv_scan_t));
96 tvh->scan=scan;
97 cl = tvh->chanlist_s[scan->channel_num];
98 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
99 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
101 if(scan->scan_timer>now)
102 return;
104 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
105 cl = tvh->chanlist_s[scan->channel_num];
106 tv_channel_tmp=tv_channel_list;
107 while (tv_channel_tmp) {
108 index++;
109 if (cl.freq==tv_channel_tmp->freq){
110 found=1;
111 break;
113 tv_channel_add=tv_channel_tmp;
114 tv_channel_tmp=tv_channel_tmp->next;
116 if (!found) {
117 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
118 scan->new_channels++;
119 tv_channel_tmp = malloc(sizeof(tv_channels_t));
120 tv_channel_tmp->index=index;
121 tv_channel_tmp->next=NULL;
122 tv_channel_tmp->prev=tv_channel_add;
123 tv_channel_tmp->freq=cl.freq;
124 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
125 strncpy(tv_channel_tmp->number, cl.name, 5);
126 tv_channel_tmp->number[4]='\0';
127 if (!tv_channel_list)
128 tv_channel_list=tv_channel_tmp;
129 else {
130 tv_channel_add->next=tv_channel_tmp;
131 tv_channel_list->prev=tv_channel_tmp;
133 }else
134 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
135 tv_channel_tmp->number,tv_channel_tmp->name);
137 scan->channel_num++;
138 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
139 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
140 tvh->tv_param->scan=0;
141 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
142 tv_channel_tmp=tv_channel_list;
143 if(tv_channel_tmp){
144 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
145 while(tv_channel_tmp){
146 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
147 if(tv_channel_tmp->next)
148 mp_msg(MSGT_TV,MSGL_INFO,",");
149 tv_channel_tmp=tv_channel_tmp->next;
151 mp_msg(MSGT_TV, MSGL_INFO, "\n");
153 if (!tv_channel_current) tv_channel_current=tv_channel_list;
154 if (tv_channel_current)
155 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
156 free(tvh->scan);
157 tvh->scan=NULL;
158 }else{
159 cl = tvh->chanlist_s[scan->channel_num];
160 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
161 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
165 /* ================== DEMUX_TV ===================== */
167 Return value:
168 0 = EOF(?) or no stream
169 1 = successfully read a packet
171 /* fill demux->video and demux->audio */
173 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
175 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
176 demux_packet_t* dp;
177 unsigned int len=0;
179 /* ================== ADD AUDIO PACKET =================== */
181 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
182 tvh->functions->control(tvh->priv,
183 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
185 len = tvh->functions->get_audio_framesize(tvh->priv);
187 dp=new_demux_packet(len);
188 dp->flags|=1; /* Keyframe */
189 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
190 ds_add_packet(demux->audio,dp);
193 /* ================== ADD VIDEO PACKET =================== */
195 if (ds==demux->video && tvh->functions->control(tvh->priv,
196 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
198 len = tvh->functions->get_video_framesize(tvh->priv);
199 dp=new_demux_packet(len);
200 dp->flags|=1; /* Keyframe */
201 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
202 ds_add_packet(demux->video,dp);
205 if (tvh->tv_param->scan) tv_scan(tvh);
206 return 1;
209 static int norm_from_string(tvi_handle_t *tvh, char* norm)
211 const tvi_functions_t *funcs = tvh->functions;
212 char str[20];
213 int ret;
215 strncpy(str, norm, sizeof(str)-1);
216 str[sizeof(str)-1] = '\0';
217 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
219 if(ret==TVI_CONTROL_TRUE)
220 return *(int *)str;
222 if(ret!=TVI_CONTROL_UNKNOWN)
224 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm,"default");
225 return 0;
228 if (!strcasecmp(norm, "pal"))
229 return TV_NORM_PAL;
230 else if (!strcasecmp(norm, "ntsc"))
231 return TV_NORM_NTSC;
232 else if (!strcasecmp(norm, "secam"))
233 return TV_NORM_SECAM;
234 else if (!strcasecmp(norm, "palnc"))
235 return TV_NORM_PALNC;
236 else if (!strcasecmp(norm, "palm"))
237 return TV_NORM_PALM;
238 else if (!strcasecmp(norm, "paln"))
239 return TV_NORM_PALN;
240 else if (!strcasecmp(norm, "ntscjp"))
241 return TV_NORM_NTSCJP;
242 else {
243 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_BogusNormParameter, norm, "PAL");
244 return TV_NORM_PAL;
248 static void parse_channels(tvi_handle_t *tvh)
250 char** channels = tvh->tv_param->channels;
252 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_ChannelNamesDetected);
253 tv_channel_list = malloc(sizeof(tv_channels_t));
254 tv_channel_list->index=1;
255 tv_channel_list->next=NULL;
256 tv_channel_list->prev=NULL;
257 tv_channel_current = tv_channel_list;
258 tv_channel_current->norm = tvh->norm;
260 while (*channels) {
261 char* tmp = *(channels++);
262 char* sep = strchr(tmp,'-');
263 int i;
264 struct CHANLIST cl;
266 if (!sep) continue; // Wrong syntax, but mplayer should not crash
268 av_strlcpy(tv_channel_current->name, sep + 1,
269 sizeof(tv_channel_current->name));
270 sep[0] = '\0';
271 strncpy(tv_channel_current->number, tmp, 5);
272 tv_channel_current->number[4]='\0';
274 while ((sep=strchr(tv_channel_current->name, '_')))
275 sep[0] = ' ';
277 // if channel number is a number and larger than 1000 threat it as frequency
278 // tmp still contain pointer to null-terminated string with channel number here
279 if (atoi(tmp)>1000){
280 tv_channel_current->freq=atoi(tmp);
281 }else{
282 tv_channel_current->freq = 0;
283 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
284 cl = tvh->chanlist_s[i];
285 if (!strcasecmp(cl.name, tv_channel_current->number)) {
286 tv_channel_current->freq=cl.freq;
287 break;
291 if (tv_channel_current->freq == 0)
292 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoFreqForChannel,
293 tv_channel_current->number, tv_channel_current->name);
294 else {
295 sep = strchr(tv_channel_current->name, '-');
296 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
298 if ( sep ) {
299 i = atoi (sep+1);
300 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
301 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
302 sep[0] = '\0';
305 sep = strchr(tv_channel_current->name, '=');
306 if ( sep ) {
307 tv_channel_current->norm = norm_from_string(tvh, sep+1);
308 sep[0] = '\0';
312 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
313 tv_channel_current->number, tv_channel_current->name,
314 (float)tv_channel_current->freq/1000);*/
316 tv_channel_current->next = malloc(sizeof(tv_channels_t));
317 tv_channel_current->next->index = tv_channel_current->index + 1;
318 tv_channel_current->next->prev = tv_channel_current;
319 tv_channel_current->next->next = NULL;
320 tv_channel_current = tv_channel_current->next;
321 tv_channel_current->norm = tvh->norm;
323 if (tv_channel_current->prev)
324 tv_channel_current->prev->next = NULL;
325 free(tv_channel_current);
328 int tv_set_norm(tvi_handle_t *tvh, char* norm)
330 tvh->norm = norm_from_string(tvh, norm);
332 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNorm, norm);
333 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
334 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
335 return 0;
337 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
338 &tvh->tv_param->teletext);
339 return 1;
342 int tv_set_norm_i(tvi_handle_t *tvh, int norm)
344 tvh->norm = norm;
346 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedNormId, norm);
347 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
348 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
349 return 0;
352 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
353 &tvh->tv_param->teletext);
354 return 1;
357 static int open_tv(tvi_handle_t *tvh)
359 int i;
360 const tvi_functions_t *funcs = tvh->functions;
361 int tv_fmt_list[] = {
362 IMGFMT_YV12,
363 IMGFMT_I420,
364 IMGFMT_UYVY,
365 IMGFMT_YUY2,
366 IMGFMT_RGB32,
367 IMGFMT_RGB24,
368 IMGFMT_RGB16,
369 IMGFMT_RGB15
372 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
374 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoVideoInputPresent);
375 return 0;
378 if (tvh->tv_param->outfmt == -1)
379 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
381 tvh->tv_param->outfmt = tv_fmt_list[i];
382 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
383 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
384 break;
386 else
388 switch(tvh->tv_param->outfmt)
390 case IMGFMT_YV12:
391 case IMGFMT_I420:
392 case IMGFMT_UYVY:
393 case IMGFMT_YUY2:
394 case IMGFMT_RGB32:
395 case IMGFMT_RGB24:
396 case IMGFMT_BGR32:
397 case IMGFMT_BGR24:
398 case IMGFMT_BGR16:
399 case IMGFMT_BGR15:
400 break;
401 default:
402 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnknownImageFormat,tvh->tv_param->outfmt);
404 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
407 /* set some params got from cmdline */
408 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
410 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
411 if (0
412 #ifdef CONFIG_TV_V4L2
413 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
414 #endif
415 #ifdef CONFIG_TV_DSHOW
416 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
417 #endif
419 tv_set_norm_i(tvh, tvh->tv_param->normid);
420 else
421 #endif
422 tv_set_norm(tvh,tvh->tv_param->norm);
424 #ifdef CONFIG_TV_V4L1
425 if ( tvh->tv_param->mjpeg )
427 /* set width to expected value */
428 if (tvh->tv_param->width == -1)
430 tvh->tv_param->width = 704/tvh->tv_param->decimation;
432 if (tvh->tv_param->height == -1)
434 if ( tvh->norm != TV_NORM_NTSC )
435 tvh->tv_param->height = 576/tvh->tv_param->decimation;
436 else
437 tvh->tv_param->height = 480/tvh->tv_param->decimation;
439 mp_msg(MSGT_TV, MSGL_INFO,
440 MSGTR_TV_MJP_WidthHeight, tvh->tv_param->width, tvh->tv_param->height);
442 #endif
444 /* limits on w&h are norm-dependent -- JM */
445 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
446 // first tell the driver both width and height, some drivers do not support setting them independently.
447 int dim[2];
448 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
449 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
451 /* set width */
452 if (tvh->tv_param->width != -1)
454 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
455 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
456 else
458 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetWidth, tvh->tv_param->width);
459 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
463 /* set height */
464 if (tvh->tv_param->height != -1)
466 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
467 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
468 else
470 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnableToSetHeight, tvh->tv_param->height);
471 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
475 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
477 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_NoTuner);
478 goto done;
481 /* select channel list */
482 for (i = 0; chanlists[i].name != NULL; i++)
484 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
486 tvh->chanlist = i;
487 tvh->chanlist_s = chanlists[i].list;
488 break;
492 if (tvh->chanlist == -1)
493 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnableFindChanlist,
494 tvh->tv_param->chanlist);
495 else
496 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedChanlist,
497 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
499 if (tvh->tv_param->freq && tvh->tv_param->channel)
501 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_ChannelFreqParamConflict);
502 goto done;
505 /* Handle channel names */
506 if (tvh->tv_param->channels) {
507 parse_channels(tvh);
508 } else
509 tv_channel_last_real = malloc(5);
511 if (tv_channel_list) {
512 int i;
513 int channel = 0;
514 if (tvh->tv_param->channel)
516 if (isdigit(*tvh->tv_param->channel))
517 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
518 channel = atoi(tvh->tv_param->channel);
519 else
521 /* if tvh->tv_param->channel does not begin with a digit
522 set the first channel that contains tvh->tv_param->channel in its name */
524 tv_channel_current = tv_channel_list;
525 while ( tv_channel_current ) {
526 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
527 break;
528 tv_channel_current = tv_channel_current->next;
530 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
533 else
534 channel = 1;
536 if ( channel ) {
537 tv_channel_current = tv_channel_list;
538 for (i = 1; i < channel; i++)
539 if (tv_channel_current->next)
540 tv_channel_current = tv_channel_current->next;
543 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
544 tv_channel_current->name, (float)tv_channel_current->freq/1000);
545 tv_set_norm_i(tvh, tv_channel_current->norm);
546 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
547 tv_channel_last = tv_channel_current;
548 } else {
549 /* we need to set frequency */
550 if (tvh->tv_param->freq)
552 unsigned long freq = atof(tvh->tv_param->freq)*16;
554 /* set freq in MHz */
555 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
557 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
558 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_SelectedFrequency,
559 freq, (float)freq/16);
562 if (tvh->tv_param->channel) {
563 struct CHANLIST cl;
565 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_RequestedChannel, tvh->tv_param->channel);
566 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
568 cl = tvh->chanlist_s[i];
569 // printf("count%d: name: %s, freq: %d\n",
570 // i, cl.name, cl.freq);
571 if (!strcasecmp(cl.name, tvh->tv_param->channel))
573 strcpy(tv_channel_last_real, cl.name);
574 tvh->channel = i;
575 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
576 cl.name, (float)cl.freq/1000);
577 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
578 break;
584 /* grep frequency in chanlist */
586 unsigned long i2;
587 int freq;
589 tv_get_freq(tvh, &i2);
591 freq = (int) (((float)(i2/16))*1000)+250;
593 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
595 if (tvh->chanlist_s[i].freq == freq)
597 tvh->channel = i+1;
598 break;
603 done:
604 /* also start device! */
605 return 1;
608 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
610 int i;
611 tvi_handle_t* h;
612 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
613 mp_msg(MSGT_TV,MSGL_INFO,MSGTR_TV_AvailableDrivers);
614 for(i=0;tvi_driver_list[i];i++){
615 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
616 if(tvi_driver_list[i]->comment)
617 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
618 mp_msg(MSGT_TV,MSGL_INFO,"\n");
620 return NULL;
623 for(i=0;tvi_driver_list[i];i++){
624 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
625 h=tvi_driver_list[i]->tvi_init(tv_param);
626 //Requested driver initialization failed
627 if (!h && tv_param->driver)
628 return NULL;
629 //Driver initialization failed during autodetection process.
630 if (!h)
631 continue;
633 h->tv_param=tv_param;
634 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_DriverInfo, tvi_driver_list[i]->short_name,
635 tvi_driver_list[i]->name,
636 tvi_driver_list[i]->author,
637 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
638 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
639 return h;
643 if(tv_param->driver)
644 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_NoSuchDriver, tv_param->driver);
645 else
646 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_DriverAutoDetectionFailed);
647 return NULL;
650 static int tv_uninit(tvi_handle_t *tvh)
652 int res;
653 if(!tvh) return 1;
654 if (!tvh->priv) return 1;
655 res=tvh->functions->uninit(tvh->priv);
656 if(res) {
657 free(tvh->priv);
658 tvh->priv=NULL;
660 return res;
663 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
665 tvi_handle_t *tvh;
666 sh_video_t *sh_video;
667 sh_audio_t *sh_audio = NULL;
668 const tvi_functions_t *funcs;
670 demuxer->priv=NULL;
671 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
672 if (!tvh->functions->init(tvh->priv)) return NULL;
674 tvh->demuxer = demuxer;
675 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
676 &(tvh->tv_param->teletext.device));
677 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
678 &demuxer->teletext);
680 if (!open_tv(tvh)){
681 tv_uninit(tvh);
682 return NULL;
684 funcs = tvh->functions;
685 demuxer->priv=tvh;
687 sh_video = new_sh_video(demuxer, 0);
689 /* get IMAGE FORMAT */
690 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
691 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
692 // sh_video->format = 0x0;
694 /* set FPS and FRAMETIME */
696 if(!sh_video->fps)
698 float tmp;
699 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
700 sh_video->fps = 25.0f; /* on PAL */
701 else sh_video->fps = tmp;
704 if (tvh->tv_param->fps != -1.0f)
705 sh_video->fps = tvh->tv_param->fps;
707 sh_video->frametime = 1.0f/sh_video->fps;
709 /* If playback only mode, go to immediate mode, fail silently */
710 if(tvh->tv_param->immediate == 1)
712 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
713 tvh->tv_param->noaudio = 1;
716 /* disable TV audio if -nosound is present */
717 if (!demuxer->audio || demuxer->audio->id == -2) {
718 tvh->tv_param->noaudio = 1;
721 /* set width */
722 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
724 /* set height */
725 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
727 demuxer->video->sh = sh_video;
728 sh_video->ds = demuxer->video;
729 demuxer->video->id = 0;
730 demuxer->seekable = 0;
732 /* here comes audio init */
733 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
735 int audio_format;
736 int sh_audio_format;
737 char buf[128];
739 /* yeah, audio is present */
741 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
742 &tvh->tv_param->audiorate);
744 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
745 goto no_audio;
747 switch(audio_format)
749 case AF_FORMAT_U8:
750 case AF_FORMAT_S8:
751 case AF_FORMAT_U16_LE:
752 case AF_FORMAT_U16_BE:
753 case AF_FORMAT_S16_LE:
754 case AF_FORMAT_S16_BE:
755 case AF_FORMAT_S32_LE:
756 case AF_FORMAT_S32_BE:
757 sh_audio_format = 0x1; /* PCM */
758 break;
759 case AF_FORMAT_IMA_ADPCM:
760 case AF_FORMAT_MU_LAW:
761 case AF_FORMAT_A_LAW:
762 case AF_FORMAT_MPEG2:
763 default:
764 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_UnsupportedAudioType,
765 af_fmt2str(audio_format, buf, 128), audio_format);
766 goto no_audio;
769 sh_audio = new_sh_audio(demuxer, 0);
771 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
772 &sh_audio->samplerate);
773 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
774 &sh_audio->samplesize);
775 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
776 &sh_audio->channels);
778 sh_audio->format = sh_audio_format;
779 sh_audio->sample_format = audio_format;
781 sh_audio->i_bps = sh_audio->o_bps =
782 sh_audio->samplerate * sh_audio->samplesize *
783 sh_audio->channels;
785 // emulate WF for win32 codecs:
786 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
787 sh_audio->wf->wFormatTag = sh_audio->format;
788 sh_audio->wf->nChannels = sh_audio->channels;
789 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
790 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
791 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
792 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
794 mp_msg(MSGT_DECVIDEO, MSGL_V, MSGTR_TV_AudioFormat,
795 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
796 sh_audio->wf->nSamplesPerSec);
798 demuxer->audio->sh = sh_audio;
799 sh_audio->ds = demuxer->audio;
800 demuxer->audio->id = 0;
802 no_audio:
804 if(!(funcs->start(tvh->priv))){
805 // start failed :(
806 tv_uninit(tvh);
807 return NULL;
810 /* set color eq */
811 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
812 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
813 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
814 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
816 if(tvh->tv_param->gain!=-1)
817 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
818 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
820 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
821 &tvh->tv_param->teletext);
823 return demuxer;
826 static void demux_close_tv(demuxer_t *demuxer)
828 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
829 if (!tvh) return;
830 tv_uninit(tvh);
831 free(tvh);
832 demuxer->priv=NULL;
833 demuxer->teletext=NULL;
836 /* utilities for mplayer (not mencoder!!) */
837 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
839 const tvi_functions_t *funcs = tvh->functions;
841 switch(opt)
843 case TV_COLOR_BRIGHTNESS:
844 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
845 case TV_COLOR_HUE:
846 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
847 case TV_COLOR_SATURATION:
848 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
849 case TV_COLOR_CONTRAST:
850 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
851 default:
852 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
855 return TVI_CONTROL_UNKNOWN;
858 int tv_get_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_GET_BRIGHTNESS, value);
866 case TV_COLOR_HUE:
867 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
868 case TV_COLOR_SATURATION:
869 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
870 case TV_COLOR_CONTRAST:
871 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
872 default:
873 mp_msg(MSGT_TV, MSGL_WARN, MSGTR_TV_UnknownColorOption, opt);
876 return TVI_CONTROL_UNKNOWN;
879 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
881 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
883 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
884 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
885 *freq, (float)*freq/16);
887 return 1;
890 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
892 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
894 // unsigned long freq = atof(tvh->tv_param->freq)*16;
896 /* set freq in MHz */
897 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
899 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
900 mp_msg(MSGT_TV, MSGL_V, MSGTR_TV_CurrentFrequency,
901 freq, (float)freq/16);
903 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
904 &tvh->tv_param->teletext);
905 return 1;
908 int tv_get_signal(tvi_handle_t *tvh)
910 int signal=0;
911 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
912 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
913 return 0;
915 return signal;
918 /*****************************************************************
919 * \brief tune current frequency by step_interval value
920 * \parameter step_interval increment value in 1/16 MHz
921 * \note frequency is rounded to 1/16 MHz value
922 * \return 1
925 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
926 unsigned long frequency;
928 tvh->tv_param->scan=0;
929 tv_get_freq(tvh,&frequency);
930 frequency+=step_interval;
931 return tv_set_freq(tvh,frequency);
934 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
936 struct CHANLIST cl;
938 tvh->tv_param->scan=0;
939 if (direction == TV_CHANNEL_LOWER)
941 if (tvh->channel-1 >= 0)
943 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
944 cl = tvh->chanlist_s[--tvh->channel];
945 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
946 cl.name, (float)cl.freq/1000);
947 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
951 if (direction == TV_CHANNEL_HIGHER)
953 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
955 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
956 cl = tvh->chanlist_s[++tvh->channel];
957 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
958 cl.name, (float)cl.freq/1000);
959 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
962 return 1;
965 int tv_step_channel(tvi_handle_t *tvh, int direction) {
966 tvh->tv_param->scan=0;
967 if (tv_channel_list) {
968 if (direction == TV_CHANNEL_HIGHER) {
969 tv_channel_last = tv_channel_current;
970 if (tv_channel_current->next)
971 tv_channel_current = tv_channel_current->next;
972 else
973 tv_channel_current = tv_channel_list;
975 tv_set_norm_i(tvh, tv_channel_current->norm);
976 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
977 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
978 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
980 if (direction == TV_CHANNEL_LOWER) {
981 tv_channel_last = tv_channel_current;
982 if (tv_channel_current->prev)
983 tv_channel_current = tv_channel_current->prev;
984 else
985 while (tv_channel_current->next)
986 tv_channel_current = tv_channel_current->next;
987 tv_set_norm_i(tvh, tv_channel_current->norm);
988 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
989 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3,
990 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
992 } else tv_step_channel_real(tvh, direction);
993 return 1;
996 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
997 int i;
998 struct CHANLIST cl;
1000 tvh->tv_param->scan=0;
1001 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1002 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1004 cl = tvh->chanlist_s[i];
1005 // printf("count%d: name: %s, freq: %d\n",
1006 // i, cl.name, cl.freq);
1007 if (!strcasecmp(cl.name, channel))
1009 tvh->channel = i;
1010 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1011 cl.name, (float)cl.freq/1000);
1012 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1013 break;
1016 return 1;
1019 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1020 int i, channel_int;
1022 tvh->tv_param->scan=0;
1023 if (tv_channel_list) {
1024 tv_channel_last = tv_channel_current;
1025 channel_int = atoi(channel);
1026 tv_channel_current = tv_channel_list;
1027 for (i = 1; i < channel_int; i++)
1028 if (tv_channel_current->next)
1029 tv_channel_current = tv_channel_current->next;
1030 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1031 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1032 tv_set_norm_i(tvh, tv_channel_current->norm);
1033 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1034 } else tv_set_channel_real(tvh, channel);
1035 return 1;
1038 int tv_last_channel(tvi_handle_t *tvh) {
1040 tvh->tv_param->scan=0;
1041 if (tv_channel_list) {
1042 tv_channels_t *tmp;
1044 tmp = tv_channel_last;
1045 tv_channel_last = tv_channel_current;
1046 tv_channel_current = tmp;
1048 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel3, tv_channel_current->number,
1049 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1050 tv_set_norm_i(tvh, tv_channel_current->norm);
1051 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1052 } else {
1053 int i;
1054 struct CHANLIST cl;
1056 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1058 cl = tvh->chanlist_s[i];
1059 if (!strcasecmp(cl.name, tv_channel_last_real))
1061 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1062 tvh->channel = i;
1063 mp_msg(MSGT_TV, MSGL_INFO, MSGTR_TV_SelectedChannel2,
1064 cl.name, (float)cl.freq/1000);
1065 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1066 break;
1070 return 1;
1073 int tv_step_norm(tvi_handle_t *tvh)
1075 tvh->norm++;
1076 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1077 &tvh->norm) != TVI_CONTROL_TRUE) {
1078 tvh->norm = 0;
1079 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1080 &tvh->norm) != TVI_CONTROL_TRUE) {
1081 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_TV_CannotSetNorm);
1082 return 0;
1085 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1086 &tvh->tv_param->teletext);
1087 return 1;
1090 int tv_step_chanlist(tvi_handle_t *tvh)
1092 return 1;
1095 demuxer_desc_t demuxer_desc_tv = {
1096 "Tv card demuxer",
1097 "tv",
1098 "TV",
1099 "Alex Beregszaszi, Charles R. Henrich",
1100 "?",
1101 DEMUXER_TYPE_TV,
1102 0, // no autodetect
1103 NULL,
1104 demux_tv_fill_buffer,
1105 demux_open_tv,
1106 demux_close_tv,
1107 NULL,
1108 NULL