m_config.[ch]: mark some function parameters const
[mplayer.git] / stream / tv.c
blobdaa6b7dd7fb316f9ffbabc3b88ad63f821809e1a
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 tvi_handle_t *tv_new_handle(int size, const tvi_functions_t *functions)
85 tvi_handle_t *h = malloc(sizeof(*h));
87 if (!h)
88 return NULL;
90 h->priv = calloc(1, size);
92 if (!h->priv) {
93 free(h);
94 return NULL;
97 h->functions = functions;
98 h->seq = 0;
99 h->chanlist = -1;
100 h->chanlist_s = NULL;
101 h->norm = -1;
102 h->channel = -1;
103 h->scan = NULL;
105 return h;
108 void tv_free_handle(tvi_handle_t *h)
110 if (h) {
111 if (h->priv)
112 free(h->priv);
113 if (h->scan)
114 free(h->scan);
115 free(h);
119 void tv_start_scan(tvi_handle_t *tvh, int start)
121 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
122 tvh->tv_param->scan=start?1:0;
125 static void tv_scan(tvi_handle_t *tvh)
127 unsigned int now;
128 struct CHANLIST cl;
129 tv_channels_t *tv_channel_tmp=NULL;
130 tv_channels_t *tv_channel_add=NULL;
131 tv_scan_t* scan;
132 int found=0, index=1;
134 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
135 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
137 mp_tmsg(MSGT_TV, MSGL_WARN, "Channel scanner is not available without tuner\n");
138 tvh->tv_param->scan=0;
139 return;
142 scan = tvh->scan;
143 now=GetTimer();
144 if (!scan) {
145 scan=calloc(1,sizeof(tv_scan_t));
146 tvh->scan=scan;
147 cl = tvh->chanlist_s[scan->channel_num];
148 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
149 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
151 if(scan->scan_timer>now)
152 return;
154 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
155 cl = tvh->chanlist_s[scan->channel_num];
156 tv_channel_tmp=tv_channel_list;
157 while (tv_channel_tmp) {
158 index++;
159 if (cl.freq==tv_channel_tmp->freq){
160 found=1;
161 break;
163 tv_channel_add=tv_channel_tmp;
164 tv_channel_tmp=tv_channel_tmp->next;
166 if (!found) {
167 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
168 scan->new_channels++;
169 tv_channel_tmp = malloc(sizeof(tv_channels_t));
170 tv_channel_tmp->index=index;
171 tv_channel_tmp->next=NULL;
172 tv_channel_tmp->prev=tv_channel_add;
173 tv_channel_tmp->freq=cl.freq;
174 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
175 strncpy(tv_channel_tmp->number, cl.name, 5);
176 tv_channel_tmp->number[4]='\0';
177 if (!tv_channel_list)
178 tv_channel_list=tv_channel_tmp;
179 else {
180 tv_channel_add->next=tv_channel_tmp;
181 tv_channel_list->prev=tv_channel_tmp;
183 }else
184 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
185 tv_channel_tmp->number,tv_channel_tmp->name);
187 scan->channel_num++;
188 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
189 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
190 tvh->tv_param->scan=0;
191 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
192 tv_channel_tmp=tv_channel_list;
193 if(tv_channel_tmp){
194 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
195 while(tv_channel_tmp){
196 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
197 if(tv_channel_tmp->next)
198 mp_msg(MSGT_TV,MSGL_INFO,",");
199 tv_channel_tmp=tv_channel_tmp->next;
201 mp_msg(MSGT_TV, MSGL_INFO, "\n");
203 if (!tv_channel_current) tv_channel_current=tv_channel_list;
204 if (tv_channel_current)
205 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
206 free(tvh->scan);
207 tvh->scan=NULL;
208 }else{
209 cl = tvh->chanlist_s[scan->channel_num];
210 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
211 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
215 /* ================== DEMUX_TV ===================== */
217 Return value:
218 0 = EOF(?) or no stream
219 1 = successfully read a packet
221 /* fill demux->video and demux->audio */
223 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
225 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
226 demux_packet_t* dp;
227 unsigned int len=0;
229 /* ================== ADD AUDIO PACKET =================== */
231 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
232 tvh->functions->control(tvh->priv,
233 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
235 len = tvh->functions->get_audio_framesize(tvh->priv);
237 dp=new_demux_packet(len);
238 dp->flags|=1; /* Keyframe */
239 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
240 ds_add_packet(demux->audio,dp);
243 /* ================== ADD VIDEO PACKET =================== */
245 if (ds==demux->video && tvh->functions->control(tvh->priv,
246 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
248 len = tvh->functions->get_video_framesize(tvh->priv);
249 dp=new_demux_packet(len);
250 dp->flags|=1; /* Keyframe */
251 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
252 ds_add_packet(demux->video,dp);
255 if (tvh->tv_param->scan) tv_scan(tvh);
256 return 1;
259 static int norm_from_string(tvi_handle_t *tvh, char* norm)
261 const tvi_functions_t *funcs = tvh->functions;
262 char str[20];
263 int ret;
265 strncpy(str, norm, sizeof(str)-1);
266 str[sizeof(str)-1] = '\0';
267 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
269 if(ret==TVI_CONTROL_TRUE)
270 return *(int *)str;
272 if(ret!=TVI_CONTROL_UNKNOWN)
274 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm,"default");
275 return 0;
278 if (!strcasecmp(norm, "pal"))
279 return TV_NORM_PAL;
280 else if (!strcasecmp(norm, "ntsc"))
281 return TV_NORM_NTSC;
282 else if (!strcasecmp(norm, "secam"))
283 return TV_NORM_SECAM;
284 else if (!strcasecmp(norm, "palnc"))
285 return TV_NORM_PALNC;
286 else if (!strcasecmp(norm, "palm"))
287 return TV_NORM_PALM;
288 else if (!strcasecmp(norm, "paln"))
289 return TV_NORM_PALN;
290 else if (!strcasecmp(norm, "ntscjp"))
291 return TV_NORM_NTSCJP;
292 else {
293 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
294 return TV_NORM_PAL;
298 static void parse_channels(tvi_handle_t *tvh)
300 char** channels = tvh->tv_param->channels;
302 mp_tmsg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
303 tv_channel_list = malloc(sizeof(tv_channels_t));
304 tv_channel_list->index=1;
305 tv_channel_list->next=NULL;
306 tv_channel_list->prev=NULL;
307 tv_channel_current = tv_channel_list;
308 tv_channel_current->norm = tvh->norm;
310 while (*channels) {
311 char* tmp = *(channels++);
312 char* sep = strchr(tmp,'-');
313 int i;
314 struct CHANLIST cl;
316 if (!sep) continue; // Wrong syntax, but mplayer should not crash
318 av_strlcpy(tv_channel_current->name, sep + 1,
319 sizeof(tv_channel_current->name));
320 sep[0] = '\0';
321 strncpy(tv_channel_current->number, tmp, 5);
322 tv_channel_current->number[4]='\0';
324 while ((sep=strchr(tv_channel_current->name, '_')))
325 sep[0] = ' ';
327 // if channel number is a number and larger than 1000 threat it as frequency
328 // tmp still contain pointer to null-terminated string with channel number here
329 if (atoi(tmp)>1000){
330 tv_channel_current->freq=atoi(tmp);
331 }else{
332 tv_channel_current->freq = 0;
333 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
334 cl = tvh->chanlist_s[i];
335 if (!strcasecmp(cl.name, tv_channel_current->number)) {
336 tv_channel_current->freq=cl.freq;
337 break;
341 if (tv_channel_current->freq == 0)
342 mp_tmsg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
343 tv_channel_current->number, tv_channel_current->name);
344 else {
345 sep = strchr(tv_channel_current->name, '-');
346 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
348 if ( sep ) {
349 i = atoi (sep+1);
350 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
351 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
352 sep[0] = '\0';
355 sep = strchr(tv_channel_current->name, '=');
356 if ( sep ) {
357 tv_channel_current->norm = norm_from_string(tvh, sep+1);
358 sep[0] = '\0';
362 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
363 tv_channel_current->number, tv_channel_current->name,
364 (float)tv_channel_current->freq/1000);*/
366 tv_channel_current->next = malloc(sizeof(tv_channels_t));
367 tv_channel_current->next->index = tv_channel_current->index + 1;
368 tv_channel_current->next->prev = tv_channel_current;
369 tv_channel_current->next->next = NULL;
370 tv_channel_current = tv_channel_current->next;
371 tv_channel_current->norm = tvh->norm;
373 if (tv_channel_current->prev)
374 tv_channel_current->prev->next = NULL;
375 free(tv_channel_current);
378 int tv_set_norm(tvi_handle_t *tvh, char* norm)
380 tvh->norm = norm_from_string(tvh, norm);
382 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm : %s\n", norm);
383 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
384 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
385 return 0;
387 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
388 &tvh->tv_param->teletext);
389 return 1;
392 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
394 tvh->norm = norm;
396 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", norm);
397 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
398 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
399 return 0;
402 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
403 &tvh->tv_param->teletext);
404 return 1;
407 static int open_tv(tvi_handle_t *tvh)
409 int i;
410 const tvi_functions_t *funcs = tvh->functions;
411 int tv_fmt_list[] = {
412 IMGFMT_YV12,
413 IMGFMT_I420,
414 IMGFMT_UYVY,
415 IMGFMT_YUY2,
416 IMGFMT_RGB32,
417 IMGFMT_RGB24,
418 IMGFMT_RGB16,
419 IMGFMT_RGB15
422 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
424 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
425 return 0;
428 if (tvh->tv_param->outfmt == -1)
429 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
431 tvh->tv_param->outfmt = tv_fmt_list[i];
432 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
433 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
434 break;
436 else
438 switch(tvh->tv_param->outfmt)
440 case IMGFMT_YV12:
441 case IMGFMT_I420:
442 case IMGFMT_UYVY:
443 case IMGFMT_YUY2:
444 case IMGFMT_RGB32:
445 case IMGFMT_RGB24:
446 case IMGFMT_BGR32:
447 case IMGFMT_BGR24:
448 case IMGFMT_BGR16:
449 case IMGFMT_BGR15:
450 break;
451 default:
452 mp_tmsg(MSGT_TV, MSGL_ERR,
453 "==================================================================\n"\
454 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
455 " This may cause buggy playback or program crash! Bug reports will\n"\
456 " be ignored! You should try again with YV12 (which is the default\n"\
457 " colorspace) and read the documentation!\n"\
458 "==================================================================\n"
459 ,tvh->tv_param->outfmt);
461 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
464 /* set some params got from cmdline */
465 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
467 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
468 if (0
469 #ifdef CONFIG_TV_V4L2
470 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
471 #endif
472 #ifdef CONFIG_TV_DSHOW
473 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
474 #endif
476 tv_set_norm_i(tvh, tvh->tv_param->normid);
477 else
478 #endif
479 tv_set_norm(tvh,tvh->tv_param->norm);
481 #ifdef CONFIG_TV_V4L1
482 if ( tvh->tv_param->mjpeg )
484 /* set width to expected value */
485 if (tvh->tv_param->width == -1)
487 tvh->tv_param->width = 704/tvh->tv_param->decimation;
489 if (tvh->tv_param->height == -1)
491 if ( tvh->norm != TV_NORM_NTSC )
492 tvh->tv_param->height = 576/tvh->tv_param->decimation;
493 else
494 tvh->tv_param->height = 480/tvh->tv_param->decimation;
496 mp_tmsg(MSGT_TV, MSGL_INFO,
497 " MJP: width %d height %d\n", tvh->tv_param->width, tvh->tv_param->height);
499 #endif
501 /* limits on w&h are norm-dependent -- JM */
502 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
503 // first tell the driver both width and height, some drivers do not support setting them independently.
504 int dim[2];
505 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
506 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
508 /* set width */
509 if (tvh->tv_param->width != -1)
511 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
512 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
513 else
515 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tvh->tv_param->width);
516 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
520 /* set height */
521 if (tvh->tv_param->height != -1)
523 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
524 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
525 else
527 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tvh->tv_param->height);
528 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
532 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
534 mp_tmsg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
535 goto done;
538 /* select channel list */
539 for (i = 0; chanlists[i].name != NULL; i++)
541 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
543 tvh->chanlist = i;
544 tvh->chanlist_s = chanlists[i].list;
545 break;
549 if (tvh->chanlist == -1)
550 mp_tmsg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
551 tvh->tv_param->chanlist);
552 else
553 mp_tmsg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
554 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
556 if (tvh->tv_param->freq && tvh->tv_param->channel)
558 mp_tmsg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
559 goto done;
562 /* Handle channel names */
563 if (tvh->tv_param->channels) {
564 parse_channels(tvh);
565 } else
566 tv_channel_last_real = malloc(5);
568 if (tv_channel_list) {
569 int i;
570 int channel = 0;
571 if (tvh->tv_param->channel)
573 if (isdigit(*tvh->tv_param->channel))
574 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
575 channel = atoi(tvh->tv_param->channel);
576 else
578 /* if tvh->tv_param->channel does not begin with a digit
579 set the first channel that contains tvh->tv_param->channel in its name */
581 tv_channel_current = tv_channel_list;
582 while ( tv_channel_current ) {
583 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
584 break;
585 tv_channel_current = tv_channel_current->next;
587 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
590 else
591 channel = 1;
593 if ( channel ) {
594 tv_channel_current = tv_channel_list;
595 for (i = 1; i < channel; i++)
596 if (tv_channel_current->next)
597 tv_channel_current = tv_channel_current->next;
600 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
601 tv_channel_current->name, (float)tv_channel_current->freq/1000);
602 tv_set_norm_i(tvh, tv_channel_current->norm);
603 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
604 tv_channel_last = tv_channel_current;
605 } else {
606 /* we need to set frequency */
607 if (tvh->tv_param->freq)
609 unsigned long freq = atof(tvh->tv_param->freq)*16;
611 /* set freq in MHz */
612 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
614 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
615 mp_tmsg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
616 freq, (float)freq/16);
619 if (tvh->tv_param->channel) {
620 struct CHANLIST cl;
622 mp_tmsg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tvh->tv_param->channel);
623 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
625 cl = tvh->chanlist_s[i];
626 // printf("count%d: name: %s, freq: %d\n",
627 // i, cl.name, cl.freq);
628 if (!strcasecmp(cl.name, tvh->tv_param->channel))
630 strcpy(tv_channel_last_real, cl.name);
631 tvh->channel = i;
632 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
633 cl.name, (float)cl.freq/1000);
634 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
635 break;
641 /* grep frequency in chanlist */
643 unsigned long i2;
644 int freq;
646 tv_get_freq(tvh, &i2);
648 freq = (int) (((float)(i2/16))*1000)+250;
650 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
652 if (tvh->chanlist_s[i].freq == freq)
654 tvh->channel = i+1;
655 break;
660 done:
661 /* also start device! */
662 return 1;
665 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
667 int i;
668 tvi_handle_t* h;
669 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
670 mp_tmsg(MSGT_TV,MSGL_INFO,"Available drivers:\n");
671 for(i=0;tvi_driver_list[i];i++){
672 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
673 if(tvi_driver_list[i]->comment)
674 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
675 mp_msg(MSGT_TV,MSGL_INFO,"\n");
677 return NULL;
680 for(i=0;tvi_driver_list[i];i++){
681 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
682 h=tvi_driver_list[i]->tvi_init(tv_param);
683 //Requested driver initialization failed
684 if (!h && tv_param->driver)
685 return NULL;
686 //Driver initialization failed during autodetection process.
687 if (!h)
688 continue;
690 h->tv_param=tv_param;
691 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,
692 tvi_driver_list[i]->name,
693 tvi_driver_list[i]->author,
694 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
695 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
696 return h;
700 if(tv_param->driver)
701 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
702 else
703 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
704 return NULL;
707 static int tv_uninit(tvi_handle_t *tvh)
709 int res;
710 if(!tvh) return 1;
711 if (!tvh->priv) return 1;
712 res=tvh->functions->uninit(tvh->priv);
713 if(res) {
714 free(tvh->priv);
715 tvh->priv=NULL;
717 return res;
720 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
722 tvi_handle_t *tvh;
723 sh_video_t *sh_video;
724 sh_audio_t *sh_audio = NULL;
725 const tvi_functions_t *funcs;
727 demuxer->priv=NULL;
728 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
729 if (!tvh->functions->init(tvh->priv)) return NULL;
731 tvh->demuxer = demuxer;
732 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
733 &(tvh->tv_param->teletext.device));
734 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
735 &demuxer->teletext);
737 if (!open_tv(tvh)){
738 tv_uninit(tvh);
739 return NULL;
741 funcs = tvh->functions;
742 demuxer->priv=tvh;
744 sh_video = new_sh_video(demuxer, 0);
746 /* get IMAGE FORMAT */
747 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
748 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
749 // sh_video->format = 0x0;
751 /* set FPS and FRAMETIME */
753 if(!sh_video->fps)
755 float tmp;
756 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
757 sh_video->fps = 25.0f; /* on PAL */
758 else sh_video->fps = tmp;
761 if (tvh->tv_param->fps != -1.0f)
762 sh_video->fps = tvh->tv_param->fps;
764 sh_video->frametime = 1.0f/sh_video->fps;
766 /* If playback only mode, go to immediate mode, fail silently */
767 if(tvh->tv_param->immediate == 1)
769 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
770 tvh->tv_param->noaudio = 1;
773 /* disable TV audio if -nosound is present */
774 if (!demuxer->audio || demuxer->audio->id == -2) {
775 tvh->tv_param->noaudio = 1;
778 /* set width */
779 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
781 /* set height */
782 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
784 demuxer->video->sh = sh_video;
785 sh_video->ds = demuxer->video;
786 demuxer->video->id = 0;
787 demuxer->seekable = 0;
789 /* here comes audio init */
790 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
792 int audio_format;
793 int sh_audio_format;
794 char buf[128];
796 /* yeah, audio is present */
798 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
799 &tvh->tv_param->audiorate);
801 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
802 goto no_audio;
804 switch(audio_format)
806 case AF_FORMAT_U8:
807 case AF_FORMAT_S8:
808 case AF_FORMAT_U16_LE:
809 case AF_FORMAT_U16_BE:
810 case AF_FORMAT_S16_LE:
811 case AF_FORMAT_S16_BE:
812 case AF_FORMAT_S32_LE:
813 case AF_FORMAT_S32_BE:
814 sh_audio_format = 0x1; /* PCM */
815 break;
816 case AF_FORMAT_IMA_ADPCM:
817 case AF_FORMAT_MU_LAW:
818 case AF_FORMAT_A_LAW:
819 case AF_FORMAT_MPEG2:
820 default:
821 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
822 af_fmt2str(audio_format, buf, 128), audio_format);
823 goto no_audio;
826 sh_audio = new_sh_audio(demuxer, 0);
828 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
829 &sh_audio->samplerate);
830 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
831 &sh_audio->samplesize);
832 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
833 &sh_audio->channels);
835 sh_audio->format = sh_audio_format;
836 sh_audio->sample_format = audio_format;
838 sh_audio->i_bps = sh_audio->o_bps =
839 sh_audio->samplerate * sh_audio->samplesize *
840 sh_audio->channels;
842 // emulate WF for win32 codecs:
843 sh_audio->wf = malloc(sizeof(*sh_audio->wf));
844 sh_audio->wf->wFormatTag = sh_audio->format;
845 sh_audio->wf->nChannels = sh_audio->channels;
846 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
847 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
848 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
849 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
851 mp_tmsg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
852 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
853 sh_audio->wf->nSamplesPerSec);
855 demuxer->audio->sh = sh_audio;
856 sh_audio->ds = demuxer->audio;
857 demuxer->audio->id = 0;
859 no_audio:
861 if(!(funcs->start(tvh->priv))){
862 // start failed :(
863 tv_uninit(tvh);
864 return NULL;
867 /* set color eq */
868 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
869 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
870 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
871 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
873 if(tvh->tv_param->gain!=-1)
874 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
875 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
877 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
878 &tvh->tv_param->teletext);
880 return demuxer;
883 static void demux_close_tv(demuxer_t *demuxer)
885 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
886 if (!tvh) return;
887 tv_uninit(tvh);
888 free(tvh);
889 demuxer->priv=NULL;
890 demuxer->teletext=NULL;
893 /* utilities for mplayer (not mencoder!!) */
894 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
896 const tvi_functions_t *funcs = tvh->functions;
898 switch(opt)
900 case TV_COLOR_BRIGHTNESS:
901 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
902 case TV_COLOR_HUE:
903 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
904 case TV_COLOR_SATURATION:
905 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
906 case TV_COLOR_CONTRAST:
907 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
908 default:
909 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
912 return TVI_CONTROL_UNKNOWN;
915 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
917 const tvi_functions_t *funcs = tvh->functions;
919 switch(opt)
921 case TV_COLOR_BRIGHTNESS:
922 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
923 case TV_COLOR_HUE:
924 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
925 case TV_COLOR_SATURATION:
926 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
927 case TV_COLOR_CONTRAST:
928 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
929 default:
930 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
933 return TVI_CONTROL_UNKNOWN;
936 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
938 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
940 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
941 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
942 *freq, (float)*freq/16);
944 return 1;
947 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
949 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
951 // unsigned long freq = atof(tvh->tv_param->freq)*16;
953 /* set freq in MHz */
954 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
956 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
957 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
958 freq, (float)freq/16);
960 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
961 &tvh->tv_param->teletext);
962 return 1;
965 int tv_get_signal(tvi_handle_t *tvh)
967 int signal=0;
968 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
969 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
970 return 0;
972 return signal;
975 /*****************************************************************
976 * \brief tune current frequency by step_interval value
977 * \parameter step_interval increment value in 1/16 MHz
978 * \note frequency is rounded to 1/16 MHz value
979 * \return 1
982 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
983 unsigned long frequency;
985 tvh->tv_param->scan=0;
986 tv_get_freq(tvh,&frequency);
987 frequency+=step_interval;
988 return tv_set_freq(tvh,frequency);
991 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
993 struct CHANLIST cl;
995 tvh->tv_param->scan=0;
996 if (direction == TV_CHANNEL_LOWER)
998 if (tvh->channel-1 >= 0)
1000 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1001 cl = tvh->chanlist_s[--tvh->channel];
1002 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1003 cl.name, (float)cl.freq/1000);
1004 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1008 if (direction == TV_CHANNEL_HIGHER)
1010 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
1012 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1013 cl = tvh->chanlist_s[++tvh->channel];
1014 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1015 cl.name, (float)cl.freq/1000);
1016 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1019 return 1;
1022 int tv_step_channel(tvi_handle_t *tvh, int direction) {
1023 tvh->tv_param->scan=0;
1024 if (tv_channel_list) {
1025 if (direction == TV_CHANNEL_HIGHER) {
1026 tv_channel_last = tv_channel_current;
1027 if (tv_channel_current->next)
1028 tv_channel_current = tv_channel_current->next;
1029 else
1030 tv_channel_current = tv_channel_list;
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 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1035 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1037 if (direction == TV_CHANNEL_LOWER) {
1038 tv_channel_last = tv_channel_current;
1039 if (tv_channel_current->prev)
1040 tv_channel_current = tv_channel_current->prev;
1041 else
1042 while (tv_channel_current->next)
1043 tv_channel_current = tv_channel_current->next;
1044 tv_set_norm_i(tvh, tv_channel_current->norm);
1045 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1046 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1047 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1049 } else tv_step_channel_real(tvh, direction);
1050 return 1;
1053 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1054 int i;
1055 struct CHANLIST cl;
1057 tvh->tv_param->scan=0;
1058 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1059 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1061 cl = tvh->chanlist_s[i];
1062 // printf("count%d: name: %s, freq: %d\n",
1063 // i, cl.name, cl.freq);
1064 if (!strcasecmp(cl.name, channel))
1066 tvh->channel = i;
1067 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1068 cl.name, (float)cl.freq/1000);
1069 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1070 break;
1073 return 1;
1076 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1077 int i, channel_int;
1079 tvh->tv_param->scan=0;
1080 if (tv_channel_list) {
1081 tv_channel_last = tv_channel_current;
1082 channel_int = atoi(channel);
1083 tv_channel_current = tv_channel_list;
1084 for (i = 1; i < channel_int; i++)
1085 if (tv_channel_current->next)
1086 tv_channel_current = tv_channel_current->next;
1087 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1088 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1089 tv_set_norm_i(tvh, tv_channel_current->norm);
1090 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1091 } else tv_set_channel_real(tvh, channel);
1092 return 1;
1095 int tv_last_channel(tvi_handle_t *tvh) {
1097 tvh->tv_param->scan=0;
1098 if (tv_channel_list) {
1099 tv_channels_t *tmp;
1101 tmp = tv_channel_last;
1102 tv_channel_last = tv_channel_current;
1103 tv_channel_current = tmp;
1105 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1106 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1107 tv_set_norm_i(tvh, tv_channel_current->norm);
1108 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1109 } else {
1110 int i;
1111 struct CHANLIST cl;
1113 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1115 cl = tvh->chanlist_s[i];
1116 if (!strcasecmp(cl.name, tv_channel_last_real))
1118 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1119 tvh->channel = i;
1120 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1121 cl.name, (float)cl.freq/1000);
1122 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1123 break;
1127 return 1;
1130 int tv_step_norm(tvi_handle_t *tvh)
1132 tvh->norm++;
1133 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1134 &tvh->norm) != TVI_CONTROL_TRUE) {
1135 tvh->norm = 0;
1136 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1137 &tvh->norm) != TVI_CONTROL_TRUE) {
1138 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
1139 return 0;
1142 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1143 &tvh->tv_param->teletext);
1144 return 1;
1147 int tv_step_chanlist(tvi_handle_t *tvh)
1149 return 1;
1152 demuxer_desc_t demuxer_desc_tv = {
1153 "Tv card demuxer",
1154 "tv",
1155 "TV",
1156 "Alex Beregszaszi, Charles R. Henrich",
1157 "?",
1158 DEMUXER_TYPE_TV,
1159 0, // no autodetect
1160 NULL,
1161 demux_tv_fill_buffer,
1162 demux_open_tv,
1163 demux_close_tv,
1164 NULL,
1165 NULL