stream/tv: move free_handle() from header to tv.c
[mplayer.git] / stream / tv.c
blob55b7033adbf7e95d55cc6daef9ef80aa2f28bcc1
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_free_handle(tvi_handle_t *h)
85 if (h) {
86 if (h->priv)
87 free(h->priv);
88 if (h->scan)
89 free(h->scan);
90 free(h);
94 void tv_start_scan(tvi_handle_t *tvh, int start)
96 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
97 tvh->tv_param->scan=start?1:0;
100 static void tv_scan(tvi_handle_t *tvh)
102 unsigned int now;
103 struct CHANLIST cl;
104 tv_channels_t *tv_channel_tmp=NULL;
105 tv_channels_t *tv_channel_add=NULL;
106 tv_scan_t* scan;
107 int found=0, index=1;
109 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
110 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
112 mp_tmsg(MSGT_TV, MSGL_WARN, "Channel scanner is not available without tuner\n");
113 tvh->tv_param->scan=0;
114 return;
117 scan = tvh->scan;
118 now=GetTimer();
119 if (!scan) {
120 scan=calloc(1,sizeof(tv_scan_t));
121 tvh->scan=scan;
122 cl = tvh->chanlist_s[scan->channel_num];
123 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
124 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
126 if(scan->scan_timer>now)
127 return;
129 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
130 cl = tvh->chanlist_s[scan->channel_num];
131 tv_channel_tmp=tv_channel_list;
132 while (tv_channel_tmp) {
133 index++;
134 if (cl.freq==tv_channel_tmp->freq){
135 found=1;
136 break;
138 tv_channel_add=tv_channel_tmp;
139 tv_channel_tmp=tv_channel_tmp->next;
141 if (!found) {
142 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
143 scan->new_channels++;
144 tv_channel_tmp = malloc(sizeof(tv_channels_t));
145 tv_channel_tmp->index=index;
146 tv_channel_tmp->next=NULL;
147 tv_channel_tmp->prev=tv_channel_add;
148 tv_channel_tmp->freq=cl.freq;
149 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
150 strncpy(tv_channel_tmp->number, cl.name, 5);
151 tv_channel_tmp->number[4]='\0';
152 if (!tv_channel_list)
153 tv_channel_list=tv_channel_tmp;
154 else {
155 tv_channel_add->next=tv_channel_tmp;
156 tv_channel_list->prev=tv_channel_tmp;
158 }else
159 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
160 tv_channel_tmp->number,tv_channel_tmp->name);
162 scan->channel_num++;
163 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
164 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
165 tvh->tv_param->scan=0;
166 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
167 tv_channel_tmp=tv_channel_list;
168 if(tv_channel_tmp){
169 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
170 while(tv_channel_tmp){
171 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
172 if(tv_channel_tmp->next)
173 mp_msg(MSGT_TV,MSGL_INFO,",");
174 tv_channel_tmp=tv_channel_tmp->next;
176 mp_msg(MSGT_TV, MSGL_INFO, "\n");
178 if (!tv_channel_current) tv_channel_current=tv_channel_list;
179 if (tv_channel_current)
180 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
181 free(tvh->scan);
182 tvh->scan=NULL;
183 }else{
184 cl = tvh->chanlist_s[scan->channel_num];
185 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
186 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
190 /* ================== DEMUX_TV ===================== */
192 Return value:
193 0 = EOF(?) or no stream
194 1 = successfully read a packet
196 /* fill demux->video and demux->audio */
198 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
200 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
201 demux_packet_t* dp;
202 unsigned int len=0;
204 /* ================== ADD AUDIO PACKET =================== */
206 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
207 tvh->functions->control(tvh->priv,
208 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
210 len = tvh->functions->get_audio_framesize(tvh->priv);
212 dp=new_demux_packet(len);
213 dp->flags|=1; /* Keyframe */
214 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
215 ds_add_packet(demux->audio,dp);
218 /* ================== ADD VIDEO PACKET =================== */
220 if (ds==demux->video && tvh->functions->control(tvh->priv,
221 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
223 len = tvh->functions->get_video_framesize(tvh->priv);
224 dp=new_demux_packet(len);
225 dp->flags|=1; /* Keyframe */
226 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
227 ds_add_packet(demux->video,dp);
230 if (tvh->tv_param->scan) tv_scan(tvh);
231 return 1;
234 static int norm_from_string(tvi_handle_t *tvh, char* norm)
236 const tvi_functions_t *funcs = tvh->functions;
237 char str[20];
238 int ret;
240 strncpy(str, norm, sizeof(str)-1);
241 str[sizeof(str)-1] = '\0';
242 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
244 if(ret==TVI_CONTROL_TRUE)
245 return *(int *)str;
247 if(ret!=TVI_CONTROL_UNKNOWN)
249 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm,"default");
250 return 0;
253 if (!strcasecmp(norm, "pal"))
254 return TV_NORM_PAL;
255 else if (!strcasecmp(norm, "ntsc"))
256 return TV_NORM_NTSC;
257 else if (!strcasecmp(norm, "secam"))
258 return TV_NORM_SECAM;
259 else if (!strcasecmp(norm, "palnc"))
260 return TV_NORM_PALNC;
261 else if (!strcasecmp(norm, "palm"))
262 return TV_NORM_PALM;
263 else if (!strcasecmp(norm, "paln"))
264 return TV_NORM_PALN;
265 else if (!strcasecmp(norm, "ntscjp"))
266 return TV_NORM_NTSCJP;
267 else {
268 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
269 return TV_NORM_PAL;
273 static void parse_channels(tvi_handle_t *tvh)
275 char** channels = tvh->tv_param->channels;
277 mp_tmsg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
278 tv_channel_list = malloc(sizeof(tv_channels_t));
279 tv_channel_list->index=1;
280 tv_channel_list->next=NULL;
281 tv_channel_list->prev=NULL;
282 tv_channel_current = tv_channel_list;
283 tv_channel_current->norm = tvh->norm;
285 while (*channels) {
286 char* tmp = *(channels++);
287 char* sep = strchr(tmp,'-');
288 int i;
289 struct CHANLIST cl;
291 if (!sep) continue; // Wrong syntax, but mplayer should not crash
293 av_strlcpy(tv_channel_current->name, sep + 1,
294 sizeof(tv_channel_current->name));
295 sep[0] = '\0';
296 strncpy(tv_channel_current->number, tmp, 5);
297 tv_channel_current->number[4]='\0';
299 while ((sep=strchr(tv_channel_current->name, '_')))
300 sep[0] = ' ';
302 // if channel number is a number and larger than 1000 threat it as frequency
303 // tmp still contain pointer to null-terminated string with channel number here
304 if (atoi(tmp)>1000){
305 tv_channel_current->freq=atoi(tmp);
306 }else{
307 tv_channel_current->freq = 0;
308 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
309 cl = tvh->chanlist_s[i];
310 if (!strcasecmp(cl.name, tv_channel_current->number)) {
311 tv_channel_current->freq=cl.freq;
312 break;
316 if (tv_channel_current->freq == 0)
317 mp_tmsg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
318 tv_channel_current->number, tv_channel_current->name);
319 else {
320 sep = strchr(tv_channel_current->name, '-');
321 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
323 if ( sep ) {
324 i = atoi (sep+1);
325 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
326 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
327 sep[0] = '\0';
330 sep = strchr(tv_channel_current->name, '=');
331 if ( sep ) {
332 tv_channel_current->norm = norm_from_string(tvh, sep+1);
333 sep[0] = '\0';
337 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
338 tv_channel_current->number, tv_channel_current->name,
339 (float)tv_channel_current->freq/1000);*/
341 tv_channel_current->next = malloc(sizeof(tv_channels_t));
342 tv_channel_current->next->index = tv_channel_current->index + 1;
343 tv_channel_current->next->prev = tv_channel_current;
344 tv_channel_current->next->next = NULL;
345 tv_channel_current = tv_channel_current->next;
346 tv_channel_current->norm = tvh->norm;
348 if (tv_channel_current->prev)
349 tv_channel_current->prev->next = NULL;
350 free(tv_channel_current);
353 int tv_set_norm(tvi_handle_t *tvh, char* norm)
355 tvh->norm = norm_from_string(tvh, norm);
357 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm : %s\n", norm);
358 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
359 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
360 return 0;
362 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
363 &tvh->tv_param->teletext);
364 return 1;
367 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
369 tvh->norm = norm;
371 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", norm);
372 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
373 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
374 return 0;
377 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
378 &tvh->tv_param->teletext);
379 return 1;
382 static int open_tv(tvi_handle_t *tvh)
384 int i;
385 const tvi_functions_t *funcs = tvh->functions;
386 int tv_fmt_list[] = {
387 IMGFMT_YV12,
388 IMGFMT_I420,
389 IMGFMT_UYVY,
390 IMGFMT_YUY2,
391 IMGFMT_RGB32,
392 IMGFMT_RGB24,
393 IMGFMT_RGB16,
394 IMGFMT_RGB15
397 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
399 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
400 return 0;
403 if (tvh->tv_param->outfmt == -1)
404 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
406 tvh->tv_param->outfmt = tv_fmt_list[i];
407 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
408 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
409 break;
411 else
413 switch(tvh->tv_param->outfmt)
415 case IMGFMT_YV12:
416 case IMGFMT_I420:
417 case IMGFMT_UYVY:
418 case IMGFMT_YUY2:
419 case IMGFMT_RGB32:
420 case IMGFMT_RGB24:
421 case IMGFMT_BGR32:
422 case IMGFMT_BGR24:
423 case IMGFMT_BGR16:
424 case IMGFMT_BGR15:
425 break;
426 default:
427 mp_tmsg(MSGT_TV, MSGL_ERR,
428 "==================================================================\n"\
429 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
430 " This may cause buggy playback or program crash! Bug reports will\n"\
431 " be ignored! You should try again with YV12 (which is the default\n"\
432 " colorspace) and read the documentation!\n"\
433 "==================================================================\n"
434 ,tvh->tv_param->outfmt);
436 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
439 /* set some params got from cmdline */
440 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
442 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
443 if (0
444 #ifdef CONFIG_TV_V4L2
445 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
446 #endif
447 #ifdef CONFIG_TV_DSHOW
448 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
449 #endif
451 tv_set_norm_i(tvh, tvh->tv_param->normid);
452 else
453 #endif
454 tv_set_norm(tvh,tvh->tv_param->norm);
456 #ifdef CONFIG_TV_V4L1
457 if ( tvh->tv_param->mjpeg )
459 /* set width to expected value */
460 if (tvh->tv_param->width == -1)
462 tvh->tv_param->width = 704/tvh->tv_param->decimation;
464 if (tvh->tv_param->height == -1)
466 if ( tvh->norm != TV_NORM_NTSC )
467 tvh->tv_param->height = 576/tvh->tv_param->decimation;
468 else
469 tvh->tv_param->height = 480/tvh->tv_param->decimation;
471 mp_tmsg(MSGT_TV, MSGL_INFO,
472 " MJP: width %d height %d\n", tvh->tv_param->width, tvh->tv_param->height);
474 #endif
476 /* limits on w&h are norm-dependent -- JM */
477 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
478 // first tell the driver both width and height, some drivers do not support setting them independently.
479 int dim[2];
480 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
481 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
483 /* set width */
484 if (tvh->tv_param->width != -1)
486 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
487 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
488 else
490 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tvh->tv_param->width);
491 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
495 /* set height */
496 if (tvh->tv_param->height != -1)
498 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
499 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
500 else
502 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tvh->tv_param->height);
503 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
507 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
509 mp_tmsg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
510 goto done;
513 /* select channel list */
514 for (i = 0; chanlists[i].name != NULL; i++)
516 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
518 tvh->chanlist = i;
519 tvh->chanlist_s = chanlists[i].list;
520 break;
524 if (tvh->chanlist == -1)
525 mp_tmsg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
526 tvh->tv_param->chanlist);
527 else
528 mp_tmsg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
529 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
531 if (tvh->tv_param->freq && tvh->tv_param->channel)
533 mp_tmsg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
534 goto done;
537 /* Handle channel names */
538 if (tvh->tv_param->channels) {
539 parse_channels(tvh);
540 } else
541 tv_channel_last_real = malloc(5);
543 if (tv_channel_list) {
544 int i;
545 int channel = 0;
546 if (tvh->tv_param->channel)
548 if (isdigit(*tvh->tv_param->channel))
549 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
550 channel = atoi(tvh->tv_param->channel);
551 else
553 /* if tvh->tv_param->channel does not begin with a digit
554 set the first channel that contains tvh->tv_param->channel in its name */
556 tv_channel_current = tv_channel_list;
557 while ( tv_channel_current ) {
558 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
559 break;
560 tv_channel_current = tv_channel_current->next;
562 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
565 else
566 channel = 1;
568 if ( channel ) {
569 tv_channel_current = tv_channel_list;
570 for (i = 1; i < channel; i++)
571 if (tv_channel_current->next)
572 tv_channel_current = tv_channel_current->next;
575 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
576 tv_channel_current->name, (float)tv_channel_current->freq/1000);
577 tv_set_norm_i(tvh, tv_channel_current->norm);
578 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
579 tv_channel_last = tv_channel_current;
580 } else {
581 /* we need to set frequency */
582 if (tvh->tv_param->freq)
584 unsigned long freq = atof(tvh->tv_param->freq)*16;
586 /* set freq in MHz */
587 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
589 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
590 mp_tmsg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
591 freq, (float)freq/16);
594 if (tvh->tv_param->channel) {
595 struct CHANLIST cl;
597 mp_tmsg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tvh->tv_param->channel);
598 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
600 cl = tvh->chanlist_s[i];
601 // printf("count%d: name: %s, freq: %d\n",
602 // i, cl.name, cl.freq);
603 if (!strcasecmp(cl.name, tvh->tv_param->channel))
605 strcpy(tv_channel_last_real, cl.name);
606 tvh->channel = i;
607 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
608 cl.name, (float)cl.freq/1000);
609 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
610 break;
616 /* grep frequency in chanlist */
618 unsigned long i2;
619 int freq;
621 tv_get_freq(tvh, &i2);
623 freq = (int) (((float)(i2/16))*1000)+250;
625 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
627 if (tvh->chanlist_s[i].freq == freq)
629 tvh->channel = i+1;
630 break;
635 done:
636 /* also start device! */
637 return 1;
640 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
642 int i;
643 tvi_handle_t* h;
644 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
645 mp_tmsg(MSGT_TV,MSGL_INFO,"Available drivers:\n");
646 for(i=0;tvi_driver_list[i];i++){
647 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
648 if(tvi_driver_list[i]->comment)
649 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
650 mp_msg(MSGT_TV,MSGL_INFO,"\n");
652 return NULL;
655 for(i=0;tvi_driver_list[i];i++){
656 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
657 h=tvi_driver_list[i]->tvi_init(tv_param);
658 //Requested driver initialization failed
659 if (!h && tv_param->driver)
660 return NULL;
661 //Driver initialization failed during autodetection process.
662 if (!h)
663 continue;
665 h->tv_param=tv_param;
666 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,
667 tvi_driver_list[i]->name,
668 tvi_driver_list[i]->author,
669 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
670 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
671 return h;
675 if(tv_param->driver)
676 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
677 else
678 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
679 return NULL;
682 static int tv_uninit(tvi_handle_t *tvh)
684 int res;
685 if(!tvh) return 1;
686 if (!tvh->priv) return 1;
687 res=tvh->functions->uninit(tvh->priv);
688 if(res) {
689 free(tvh->priv);
690 tvh->priv=NULL;
692 return res;
695 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
697 tvi_handle_t *tvh;
698 sh_video_t *sh_video;
699 sh_audio_t *sh_audio = NULL;
700 const tvi_functions_t *funcs;
702 demuxer->priv=NULL;
703 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
704 if (!tvh->functions->init(tvh->priv)) return NULL;
706 tvh->demuxer = demuxer;
707 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
708 &(tvh->tv_param->teletext.device));
709 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
710 &demuxer->teletext);
712 if (!open_tv(tvh)){
713 tv_uninit(tvh);
714 return NULL;
716 funcs = tvh->functions;
717 demuxer->priv=tvh;
719 sh_video = new_sh_video(demuxer, 0);
721 /* get IMAGE FORMAT */
722 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
723 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
724 // sh_video->format = 0x0;
726 /* set FPS and FRAMETIME */
728 if(!sh_video->fps)
730 float tmp;
731 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
732 sh_video->fps = 25.0f; /* on PAL */
733 else sh_video->fps = tmp;
736 if (tvh->tv_param->fps != -1.0f)
737 sh_video->fps = tvh->tv_param->fps;
739 sh_video->frametime = 1.0f/sh_video->fps;
741 /* If playback only mode, go to immediate mode, fail silently */
742 if(tvh->tv_param->immediate == 1)
744 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
745 tvh->tv_param->noaudio = 1;
748 /* disable TV audio if -nosound is present */
749 if (!demuxer->audio || demuxer->audio->id == -2) {
750 tvh->tv_param->noaudio = 1;
753 /* set width */
754 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
756 /* set height */
757 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
759 demuxer->video->sh = sh_video;
760 sh_video->ds = demuxer->video;
761 demuxer->video->id = 0;
762 demuxer->seekable = 0;
764 /* here comes audio init */
765 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
767 int audio_format;
768 int sh_audio_format;
769 char buf[128];
771 /* yeah, audio is present */
773 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
774 &tvh->tv_param->audiorate);
776 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
777 goto no_audio;
779 switch(audio_format)
781 case AF_FORMAT_U8:
782 case AF_FORMAT_S8:
783 case AF_FORMAT_U16_LE:
784 case AF_FORMAT_U16_BE:
785 case AF_FORMAT_S16_LE:
786 case AF_FORMAT_S16_BE:
787 case AF_FORMAT_S32_LE:
788 case AF_FORMAT_S32_BE:
789 sh_audio_format = 0x1; /* PCM */
790 break;
791 case AF_FORMAT_IMA_ADPCM:
792 case AF_FORMAT_MU_LAW:
793 case AF_FORMAT_A_LAW:
794 case AF_FORMAT_MPEG2:
795 default:
796 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
797 af_fmt2str(audio_format, buf, 128), audio_format);
798 goto no_audio;
801 sh_audio = new_sh_audio(demuxer, 0);
803 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
804 &sh_audio->samplerate);
805 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
806 &sh_audio->samplesize);
807 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
808 &sh_audio->channels);
810 sh_audio->format = sh_audio_format;
811 sh_audio->sample_format = audio_format;
813 sh_audio->i_bps = sh_audio->o_bps =
814 sh_audio->samplerate * sh_audio->samplesize *
815 sh_audio->channels;
817 // emulate WF for win32 codecs:
818 sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
819 sh_audio->wf->wFormatTag = sh_audio->format;
820 sh_audio->wf->nChannels = sh_audio->channels;
821 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
822 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
823 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
824 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
826 mp_tmsg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
827 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
828 sh_audio->wf->nSamplesPerSec);
830 demuxer->audio->sh = sh_audio;
831 sh_audio->ds = demuxer->audio;
832 demuxer->audio->id = 0;
834 no_audio:
836 if(!(funcs->start(tvh->priv))){
837 // start failed :(
838 tv_uninit(tvh);
839 return NULL;
842 /* set color eq */
843 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
844 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
845 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
846 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
848 if(tvh->tv_param->gain!=-1)
849 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
850 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
852 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
853 &tvh->tv_param->teletext);
855 return demuxer;
858 static void demux_close_tv(demuxer_t *demuxer)
860 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
861 if (!tvh) return;
862 tv_uninit(tvh);
863 free(tvh);
864 demuxer->priv=NULL;
865 demuxer->teletext=NULL;
868 /* utilities for mplayer (not mencoder!!) */
869 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
871 const tvi_functions_t *funcs = tvh->functions;
873 switch(opt)
875 case TV_COLOR_BRIGHTNESS:
876 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
877 case TV_COLOR_HUE:
878 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
879 case TV_COLOR_SATURATION:
880 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
881 case TV_COLOR_CONTRAST:
882 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
883 default:
884 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
887 return TVI_CONTROL_UNKNOWN;
890 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
892 const tvi_functions_t *funcs = tvh->functions;
894 switch(opt)
896 case TV_COLOR_BRIGHTNESS:
897 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
898 case TV_COLOR_HUE:
899 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
900 case TV_COLOR_SATURATION:
901 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
902 case TV_COLOR_CONTRAST:
903 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
904 default:
905 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
908 return TVI_CONTROL_UNKNOWN;
911 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
913 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
915 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
916 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
917 *freq, (float)*freq/16);
919 return 1;
922 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
924 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
926 // unsigned long freq = atof(tvh->tv_param->freq)*16;
928 /* set freq in MHz */
929 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
931 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
932 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
933 freq, (float)freq/16);
935 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
936 &tvh->tv_param->teletext);
937 return 1;
940 int tv_get_signal(tvi_handle_t *tvh)
942 int signal=0;
943 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
944 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
945 return 0;
947 return signal;
950 /*****************************************************************
951 * \brief tune current frequency by step_interval value
952 * \parameter step_interval increment value in 1/16 MHz
953 * \note frequency is rounded to 1/16 MHz value
954 * \return 1
957 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
958 unsigned long frequency;
960 tvh->tv_param->scan=0;
961 tv_get_freq(tvh,&frequency);
962 frequency+=step_interval;
963 return tv_set_freq(tvh,frequency);
966 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
968 struct CHANLIST cl;
970 tvh->tv_param->scan=0;
971 if (direction == TV_CHANNEL_LOWER)
973 if (tvh->channel-1 >= 0)
975 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
976 cl = tvh->chanlist_s[--tvh->channel];
977 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
978 cl.name, (float)cl.freq/1000);
979 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
983 if (direction == TV_CHANNEL_HIGHER)
985 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
987 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
988 cl = tvh->chanlist_s[++tvh->channel];
989 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
990 cl.name, (float)cl.freq/1000);
991 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
994 return 1;
997 int tv_step_channel(tvi_handle_t *tvh, int direction) {
998 tvh->tv_param->scan=0;
999 if (tv_channel_list) {
1000 if (direction == TV_CHANNEL_HIGHER) {
1001 tv_channel_last = tv_channel_current;
1002 if (tv_channel_current->next)
1003 tv_channel_current = tv_channel_current->next;
1004 else
1005 tv_channel_current = tv_channel_list;
1007 tv_set_norm_i(tvh, tv_channel_current->norm);
1008 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1009 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1010 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1012 if (direction == TV_CHANNEL_LOWER) {
1013 tv_channel_last = tv_channel_current;
1014 if (tv_channel_current->prev)
1015 tv_channel_current = tv_channel_current->prev;
1016 else
1017 while (tv_channel_current->next)
1018 tv_channel_current = tv_channel_current->next;
1019 tv_set_norm_i(tvh, tv_channel_current->norm);
1020 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1021 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1022 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1024 } else tv_step_channel_real(tvh, direction);
1025 return 1;
1028 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1029 int i;
1030 struct CHANLIST cl;
1032 tvh->tv_param->scan=0;
1033 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1034 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1036 cl = tvh->chanlist_s[i];
1037 // printf("count%d: name: %s, freq: %d\n",
1038 // i, cl.name, cl.freq);
1039 if (!strcasecmp(cl.name, channel))
1041 tvh->channel = i;
1042 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1043 cl.name, (float)cl.freq/1000);
1044 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1045 break;
1048 return 1;
1051 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1052 int i, channel_int;
1054 tvh->tv_param->scan=0;
1055 if (tv_channel_list) {
1056 tv_channel_last = tv_channel_current;
1057 channel_int = atoi(channel);
1058 tv_channel_current = tv_channel_list;
1059 for (i = 1; i < channel_int; i++)
1060 if (tv_channel_current->next)
1061 tv_channel_current = tv_channel_current->next;
1062 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1063 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1064 tv_set_norm_i(tvh, tv_channel_current->norm);
1065 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1066 } else tv_set_channel_real(tvh, channel);
1067 return 1;
1070 int tv_last_channel(tvi_handle_t *tvh) {
1072 tvh->tv_param->scan=0;
1073 if (tv_channel_list) {
1074 tv_channels_t *tmp;
1076 tmp = tv_channel_last;
1077 tv_channel_last = tv_channel_current;
1078 tv_channel_current = tmp;
1080 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1081 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1082 tv_set_norm_i(tvh, tv_channel_current->norm);
1083 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1084 } else {
1085 int i;
1086 struct CHANLIST cl;
1088 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1090 cl = tvh->chanlist_s[i];
1091 if (!strcasecmp(cl.name, tv_channel_last_real))
1093 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1094 tvh->channel = i;
1095 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1096 cl.name, (float)cl.freq/1000);
1097 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1098 break;
1102 return 1;
1105 int tv_step_norm(tvi_handle_t *tvh)
1107 tvh->norm++;
1108 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1109 &tvh->norm) != TVI_CONTROL_TRUE) {
1110 tvh->norm = 0;
1111 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1112 &tvh->norm) != TVI_CONTROL_TRUE) {
1113 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
1114 return 0;
1117 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1118 &tvh->tv_param->teletext);
1119 return 1;
1122 int tv_step_chanlist(tvi_handle_t *tvh)
1124 return 1;
1127 demuxer_desc_t demuxer_desc_tv = {
1128 "Tv card demuxer",
1129 "tv",
1130 "TV",
1131 "Alex Beregszaszi, Charles R. Henrich",
1132 "?",
1133 DEMUXER_TYPE_TV,
1134 0, // no autodetect
1135 NULL,
1136 demux_tv_fill_buffer,
1137 demux_open_tv,
1138 demux_close_tv,
1139 NULL,
1140 NULL