demux_ts: cleanup
[mplayer/glamo.git] / stream / tv.c
blob925109e77802a9c1612f317c3c3c063fcb91f063
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 return;
112 free(h->priv);
113 free(h->scan);
114 free(h);
117 void tv_start_scan(tvi_handle_t *tvh, int start)
119 mp_msg(MSGT_TV,MSGL_INFO,"start scan\n");
120 tvh->tv_param->scan=start?1:0;
123 static void tv_scan(tvi_handle_t *tvh)
125 unsigned int now;
126 struct CHANLIST cl;
127 tv_channels_t *tv_channel_tmp=NULL;
128 tv_channels_t *tv_channel_add=NULL;
129 tv_scan_t* scan;
130 int found=0, index=1;
132 //Channel scanner without tuner is useless and causes crash due to uninitialized chanlist_s
133 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
135 mp_tmsg(MSGT_TV, MSGL_WARN, "Channel scanner is not available without tuner\n");
136 tvh->tv_param->scan=0;
137 return;
140 scan = tvh->scan;
141 now=GetTimer();
142 if (!scan) {
143 scan=calloc(1,sizeof(tv_scan_t));
144 tvh->scan=scan;
145 cl = tvh->chanlist_s[scan->channel_num];
146 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
147 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
149 if(scan->scan_timer>now)
150 return;
152 if (tv_get_signal(tvh)>tvh->tv_param->scan_threshold) {
153 cl = tvh->chanlist_s[scan->channel_num];
154 tv_channel_tmp=tv_channel_list;
155 while (tv_channel_tmp) {
156 index++;
157 if (cl.freq==tv_channel_tmp->freq){
158 found=1;
159 break;
161 tv_channel_add=tv_channel_tmp;
162 tv_channel_tmp=tv_channel_tmp->next;
164 if (!found) {
165 mp_msg(MSGT_TV, MSGL_INFO, "Found new channel: %s (#%d). \n",cl.name,index);
166 scan->new_channels++;
167 tv_channel_tmp = malloc(sizeof(tv_channels_t));
168 tv_channel_tmp->index=index;
169 tv_channel_tmp->next=NULL;
170 tv_channel_tmp->prev=tv_channel_add;
171 tv_channel_tmp->freq=cl.freq;
172 snprintf(tv_channel_tmp->name,sizeof(tv_channel_tmp->name),"ch%d",index);
173 strncpy(tv_channel_tmp->number, cl.name, 5);
174 tv_channel_tmp->number[4]='\0';
175 if (!tv_channel_list)
176 tv_channel_list=tv_channel_tmp;
177 else {
178 tv_channel_add->next=tv_channel_tmp;
179 tv_channel_list->prev=tv_channel_tmp;
181 }else
182 mp_msg(MSGT_TV, MSGL_INFO, "Found existing channel: %s-%s.\n",
183 tv_channel_tmp->number,tv_channel_tmp->name);
185 scan->channel_num++;
186 scan->scan_timer=now+1e6*tvh->tv_param->scan_period;
187 if (scan->channel_num>=chanlists[tvh->chanlist].count) {
188 tvh->tv_param->scan=0;
189 mp_msg(MSGT_TV, MSGL_INFO, "TV scan end. Found %d new channels.\n", scan->new_channels);
190 tv_channel_tmp=tv_channel_list;
191 if(tv_channel_tmp){
192 mp_msg(MSGT_TV,MSGL_INFO,"channels=");
193 while(tv_channel_tmp){
194 mp_msg(MSGT_TV,MSGL_INFO,"%s-%s",tv_channel_tmp->number,tv_channel_tmp->name);
195 if(tv_channel_tmp->next)
196 mp_msg(MSGT_TV,MSGL_INFO,",");
197 tv_channel_tmp=tv_channel_tmp->next;
199 mp_msg(MSGT_TV, MSGL_INFO, "\n");
201 if (!tv_channel_current) tv_channel_current=tv_channel_list;
202 if (tv_channel_current)
203 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
204 free(tvh->scan);
205 tvh->scan=NULL;
206 }else{
207 cl = tvh->chanlist_s[scan->channel_num];
208 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
209 mp_msg(MSGT_TV, MSGL_INFO, "Trying: %s (%.2f). \n",cl.name,1e-3*cl.freq);
213 /* ================== DEMUX_TV ===================== */
215 Return value:
216 0 = EOF(?) or no stream
217 1 = successfully read a packet
219 /* fill demux->video and demux->audio */
221 static int demux_tv_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
223 tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
224 demux_packet_t* dp;
225 unsigned int len=0;
227 /* ================== ADD AUDIO PACKET =================== */
229 if (ds==demux->audio && tvh->tv_param->noaudio == 0 &&
230 tvh->functions->control(tvh->priv,
231 TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
233 len = tvh->functions->get_audio_framesize(tvh->priv);
235 dp=new_demux_packet(len);
236 dp->flags|=1; /* Keyframe */
237 dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
238 ds_add_packet(demux->audio,dp);
241 /* ================== ADD VIDEO PACKET =================== */
243 if (ds==demux->video && tvh->functions->control(tvh->priv,
244 TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
246 len = tvh->functions->get_video_framesize(tvh->priv);
247 dp=new_demux_packet(len);
248 dp->flags|=1; /* Keyframe */
249 dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
250 ds_add_packet(demux->video,dp);
253 if (tvh->tv_param->scan) tv_scan(tvh);
254 return 1;
257 static int norm_from_string(tvi_handle_t *tvh, char* norm)
259 const tvi_functions_t *funcs = tvh->functions;
260 char str[20];
261 int ret;
263 strncpy(str, norm, sizeof(str)-1);
264 str[sizeof(str)-1] = '\0';
265 ret=funcs->control(tvh->priv, TVI_CONTROL_SPC_GET_NORMID, str);
267 if(ret==TVI_CONTROL_TRUE)
268 return *(int *)str;
270 if(ret!=TVI_CONTROL_UNKNOWN)
272 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm,"default");
273 return 0;
276 if (!strcasecmp(norm, "pal"))
277 return TV_NORM_PAL;
278 else if (!strcasecmp(norm, "ntsc"))
279 return TV_NORM_NTSC;
280 else if (!strcasecmp(norm, "secam"))
281 return TV_NORM_SECAM;
282 else if (!strcasecmp(norm, "palnc"))
283 return TV_NORM_PALNC;
284 else if (!strcasecmp(norm, "palm"))
285 return TV_NORM_PALM;
286 else if (!strcasecmp(norm, "paln"))
287 return TV_NORM_PALN;
288 else if (!strcasecmp(norm, "ntscjp"))
289 return TV_NORM_NTSCJP;
290 else {
291 mp_tmsg(MSGT_TV, MSGL_WARN, "tv.c: norm_from_string(%s): Bogus norm parameter, setting %s.\n", norm, "PAL");
292 return TV_NORM_PAL;
296 static void parse_channels(tvi_handle_t *tvh)
298 char** channels = tvh->tv_param->channels;
300 mp_tmsg(MSGT_TV, MSGL_INFO, "TV channel names detected.\n");
301 tv_channel_list = malloc(sizeof(tv_channels_t));
302 tv_channel_list->index=1;
303 tv_channel_list->next=NULL;
304 tv_channel_list->prev=NULL;
305 tv_channel_current = tv_channel_list;
306 tv_channel_current->norm = tvh->norm;
308 while (*channels) {
309 char* tmp = *(channels++);
310 char* sep = strchr(tmp,'-');
311 int i;
312 struct CHANLIST cl;
314 if (!sep) continue; // Wrong syntax, but mplayer should not crash
316 av_strlcpy(tv_channel_current->name, sep + 1,
317 sizeof(tv_channel_current->name));
318 sep[0] = '\0';
319 strncpy(tv_channel_current->number, tmp, 5);
320 tv_channel_current->number[4]='\0';
322 while ((sep=strchr(tv_channel_current->name, '_')))
323 sep[0] = ' ';
325 // if channel number is a number and larger than 1000 threat it as frequency
326 // tmp still contain pointer to null-terminated string with channel number here
327 if (atoi(tmp)>1000){
328 tv_channel_current->freq=atoi(tmp);
329 }else{
330 tv_channel_current->freq = 0;
331 for (i = 0; i < chanlists[tvh->chanlist].count; i++) {
332 cl = tvh->chanlist_s[i];
333 if (!strcasecmp(cl.name, tv_channel_current->number)) {
334 tv_channel_current->freq=cl.freq;
335 break;
339 if (tv_channel_current->freq == 0)
340 mp_tmsg(MSGT_TV, MSGL_ERR, "Couldn't find frequency for channel %s (%s)\n",
341 tv_channel_current->number, tv_channel_current->name);
342 else {
343 sep = strchr(tv_channel_current->name, '-');
344 if ( !sep ) sep = strchr(tv_channel_current->name, '+');
346 if ( sep ) {
347 i = atoi (sep+1);
348 if ( sep[0] == '+' ) tv_channel_current->freq += i * 100;
349 if ( sep[0] == '-' ) tv_channel_current->freq -= i * 100;
350 sep[0] = '\0';
353 sep = strchr(tv_channel_current->name, '=');
354 if ( sep ) {
355 tv_channel_current->norm = norm_from_string(tvh, sep+1);
356 sep[0] = '\0';
360 /*mp_msg(MSGT_TV, MSGL_INFO, "-- Detected channel %s - %s (%5.3f)\n",
361 tv_channel_current->number, tv_channel_current->name,
362 (float)tv_channel_current->freq/1000);*/
364 tv_channel_current->next = malloc(sizeof(tv_channels_t));
365 tv_channel_current->next->index = tv_channel_current->index + 1;
366 tv_channel_current->next->prev = tv_channel_current;
367 tv_channel_current->next->next = NULL;
368 tv_channel_current = tv_channel_current->next;
369 tv_channel_current->norm = tvh->norm;
371 if (tv_channel_current->prev)
372 tv_channel_current->prev->next = NULL;
373 free(tv_channel_current);
376 int tv_set_norm(tvi_handle_t *tvh, char* norm)
378 tvh->norm = norm_from_string(tvh, norm);
380 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm : %s\n", norm);
381 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
382 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
383 return 0;
385 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
386 &tvh->tv_param->teletext);
387 return 1;
390 static int tv_set_norm_i(tvi_handle_t *tvh, int norm)
392 tvh->norm = norm;
394 mp_tmsg(MSGT_TV, MSGL_V, "Selected norm id: %d\n", norm);
395 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM, &tvh->norm) != TVI_CONTROL_TRUE) {
396 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
397 return 0;
400 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
401 &tvh->tv_param->teletext);
402 return 1;
405 static int open_tv(tvi_handle_t *tvh)
407 int i;
408 const tvi_functions_t *funcs = tvh->functions;
409 int tv_fmt_list[] = {
410 IMGFMT_YV12,
411 IMGFMT_I420,
412 IMGFMT_UYVY,
413 IMGFMT_YUY2,
414 IMGFMT_RGB32,
415 IMGFMT_RGB24,
416 IMGFMT_RGB16,
417 IMGFMT_RGB15
420 if (funcs->control(tvh->priv, TVI_CONTROL_IS_VIDEO, 0) != TVI_CONTROL_TRUE)
422 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: No video input present!\n");
423 return 0;
426 if (tvh->tv_param->outfmt == -1)
427 for (i = 0; i < sizeof (tv_fmt_list) / sizeof (*tv_fmt_list); i++)
429 tvh->tv_param->outfmt = tv_fmt_list[i];
430 if (funcs->control (tvh->priv, TVI_CONTROL_VID_SET_FORMAT,
431 &tvh->tv_param->outfmt) == TVI_CONTROL_TRUE)
432 break;
434 else
436 switch(tvh->tv_param->outfmt)
438 case IMGFMT_YV12:
439 case IMGFMT_I420:
440 case IMGFMT_UYVY:
441 case IMGFMT_YUY2:
442 case IMGFMT_RGB32:
443 case IMGFMT_RGB24:
444 case IMGFMT_BGR32:
445 case IMGFMT_BGR24:
446 case IMGFMT_BGR16:
447 case IMGFMT_BGR15:
448 break;
449 default:
450 mp_tmsg(MSGT_TV, MSGL_ERR,
451 "==================================================================\n"\
452 " WARNING: UNTESTED OR UNKNOWN OUTPUT IMAGE FORMAT REQUESTED (0x%x)\n"\
453 " This may cause buggy playback or program crash! Bug reports will\n"\
454 " be ignored! You should try again with YV12 (which is the default\n"\
455 " colorspace) and read the documentation!\n"\
456 "==================================================================\n"
457 ,tvh->tv_param->outfmt);
459 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_FORMAT, &tvh->tv_param->outfmt);
462 /* set some params got from cmdline */
463 funcs->control(tvh->priv, TVI_CONTROL_SPC_SET_INPUT, &tvh->tv_param->input);
465 #if defined(CONFIG_TV_V4L2) || defined(CONFIG_TV_DSHOW)
466 if (0
467 #ifdef CONFIG_TV_V4L2
468 || (!strcmp(tvh->tv_param->driver, "v4l2") && tvh->tv_param->normid >= 0)
469 #endif
470 #ifdef CONFIG_TV_DSHOW
471 || (!strcmp(tvh->tv_param->driver, "dshow") && tvh->tv_param->normid >= 0)
472 #endif
474 tv_set_norm_i(tvh, tvh->tv_param->normid);
475 else
476 #endif
477 tv_set_norm(tvh,tvh->tv_param->norm);
479 #ifdef CONFIG_TV_V4L1
480 if ( tvh->tv_param->mjpeg )
482 /* set width to expected value */
483 if (tvh->tv_param->width == -1)
485 tvh->tv_param->width = 704/tvh->tv_param->decimation;
487 if (tvh->tv_param->height == -1)
489 if ( tvh->norm != TV_NORM_NTSC )
490 tvh->tv_param->height = 576/tvh->tv_param->decimation;
491 else
492 tvh->tv_param->height = 480/tvh->tv_param->decimation;
494 mp_tmsg(MSGT_TV, MSGL_INFO,
495 " MJP: width %d height %d\n", tvh->tv_param->width, tvh->tv_param->height);
497 #endif
499 /* limits on w&h are norm-dependent -- JM */
500 if (tvh->tv_param->width != -1 && tvh->tv_param->height != -1) {
501 // first tell the driver both width and height, some drivers do not support setting them independently.
502 int dim[2];
503 dim[0] = tvh->tv_param->width; dim[1] = tvh->tv_param->height;
504 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH_HEIGHT, dim);
506 /* set width */
507 if (tvh->tv_param->width != -1)
509 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_WIDTH, &tvh->tv_param->width) == TVI_CONTROL_TRUE)
510 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_WIDTH, &tvh->tv_param->width);
511 else
513 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested width: %d\n", tvh->tv_param->width);
514 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &tvh->tv_param->width);
518 /* set height */
519 if (tvh->tv_param->height != -1)
521 if (funcs->control(tvh->priv, TVI_CONTROL_VID_CHK_HEIGHT, &tvh->tv_param->height) == TVI_CONTROL_TRUE)
522 funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HEIGHT, &tvh->tv_param->height);
523 else
525 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to set requested height: %d\n", tvh->tv_param->height);
526 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &tvh->tv_param->height);
530 if (funcs->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE)
532 mp_tmsg(MSGT_TV, MSGL_WARN, "Selected input hasn't got a tuner!\n");
533 goto done;
536 /* select channel list */
537 for (i = 0; chanlists[i].name != NULL; i++)
539 if (!strcasecmp(chanlists[i].name, tvh->tv_param->chanlist))
541 tvh->chanlist = i;
542 tvh->chanlist_s = chanlists[i].list;
543 break;
547 if (tvh->chanlist == -1)
548 mp_tmsg(MSGT_TV, MSGL_WARN, "Unable to find selected channel list! (%s)\n",
549 tvh->tv_param->chanlist);
550 else
551 mp_tmsg(MSGT_TV, MSGL_V, "Selected channel list: %s (including %d channels)\n",
552 chanlists[tvh->chanlist].name, chanlists[tvh->chanlist].count);
554 if (tvh->tv_param->freq && tvh->tv_param->channel)
556 mp_tmsg(MSGT_TV, MSGL_WARN, "You can't set frequency and channel simultaneously!\n");
557 goto done;
560 /* Handle channel names */
561 if (tvh->tv_param->channels) {
562 parse_channels(tvh);
563 } else
564 tv_channel_last_real = malloc(5);
566 if (tv_channel_list) {
567 int i;
568 int channel = 0;
569 if (tvh->tv_param->channel)
571 if (isdigit(*tvh->tv_param->channel))
572 /* if tvh->tv_param->channel begins with a digit interpret it as a number */
573 channel = atoi(tvh->tv_param->channel);
574 else
576 /* if tvh->tv_param->channel does not begin with a digit
577 set the first channel that contains tvh->tv_param->channel in its name */
579 tv_channel_current = tv_channel_list;
580 while ( tv_channel_current ) {
581 if ( strstr(tv_channel_current->name, tvh->tv_param->channel) )
582 break;
583 tv_channel_current = tv_channel_current->next;
585 if ( !tv_channel_current ) tv_channel_current = tv_channel_list;
588 else
589 channel = 1;
591 if ( channel ) {
592 tv_channel_current = tv_channel_list;
593 for (i = 1; i < channel; i++)
594 if (tv_channel_current->next)
595 tv_channel_current = tv_channel_current->next;
598 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
599 tv_channel_current->name, (float)tv_channel_current->freq/1000);
600 tv_set_norm_i(tvh, tv_channel_current->norm);
601 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
602 tv_channel_last = tv_channel_current;
603 } else {
604 /* we need to set frequency */
605 if (tvh->tv_param->freq)
607 unsigned long freq = atof(tvh->tv_param->freq)*16;
609 /* set freq in MHz */
610 funcs->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
612 funcs->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
613 mp_tmsg(MSGT_TV, MSGL_V, "Selected frequency: %lu (%.3f)\n",
614 freq, (float)freq/16);
617 if (tvh->tv_param->channel) {
618 struct CHANLIST cl;
620 mp_tmsg(MSGT_TV, MSGL_V, "Requested channel: %s\n", tvh->tv_param->channel);
621 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
623 cl = tvh->chanlist_s[i];
624 // printf("count%d: name: %s, freq: %d\n",
625 // i, cl.name, cl.freq);
626 if (!strcasecmp(cl.name, tvh->tv_param->channel))
628 strcpy(tv_channel_last_real, cl.name);
629 tvh->channel = i;
630 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
631 cl.name, (float)cl.freq/1000);
632 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
633 break;
639 /* grep frequency in chanlist */
641 unsigned long i2;
642 int freq;
644 tv_get_freq(tvh, &i2);
646 freq = (int) (((float)(i2/16))*1000)+250;
648 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
650 if (tvh->chanlist_s[i].freq == freq)
652 tvh->channel = i+1;
653 break;
658 done:
659 /* also start device! */
660 return 1;
663 static tvi_handle_t *tv_begin(tv_param_t* tv_param)
665 int i;
666 tvi_handle_t* h;
667 if(tv_param->driver && !strcmp(tv_param->driver,"help")){
668 mp_tmsg(MSGT_TV,MSGL_INFO,"Available drivers:\n");
669 for(i=0;tvi_driver_list[i];i++){
670 mp_msg(MSGT_TV,MSGL_INFO," %s\t%s",tvi_driver_list[i]->short_name,tvi_driver_list[i]->name);
671 if(tvi_driver_list[i]->comment)
672 mp_msg(MSGT_TV,MSGL_INFO," (%s)",tvi_driver_list[i]->comment);
673 mp_msg(MSGT_TV,MSGL_INFO,"\n");
675 return NULL;
678 for(i=0;tvi_driver_list[i];i++){
679 if (!tv_param->driver || !strcmp(tvi_driver_list[i]->short_name, tv_param->driver)){
680 h=tvi_driver_list[i]->tvi_init(tv_param);
681 //Requested driver initialization failed
682 if (!h && tv_param->driver)
683 return NULL;
684 //Driver initialization failed during autodetection process.
685 if (!h)
686 continue;
688 h->tv_param=tv_param;
689 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,
690 tvi_driver_list[i]->name,
691 tvi_driver_list[i]->author,
692 tvi_driver_list[i]->comment?tvi_driver_list[i]->comment:"");
693 tv_param->driver=strdup(tvi_driver_list[i]->short_name);
694 return h;
698 if(tv_param->driver)
699 mp_tmsg(MSGT_TV, MSGL_ERR, "No such driver: %s\n", tv_param->driver);
700 else
701 mp_tmsg(MSGT_TV, MSGL_ERR, "TV driver autodetection failed.\n");
702 return NULL;
705 static int tv_uninit(tvi_handle_t *tvh)
707 int res;
708 if(!tvh) return 1;
709 if (!tvh->priv) return 1;
710 res=tvh->functions->uninit(tvh->priv);
711 if(res) {
712 free(tvh->priv);
713 tvh->priv=NULL;
715 return res;
718 static demuxer_t* demux_open_tv(demuxer_t *demuxer)
720 tvi_handle_t *tvh;
721 sh_video_t *sh_video;
722 sh_audio_t *sh_audio = NULL;
723 const tvi_functions_t *funcs;
725 demuxer->priv=NULL;
726 if(!(tvh=tv_begin(demuxer->stream->priv))) return NULL;
727 if (!tvh->functions->init(tvh->priv)) return NULL;
729 tvh->demuxer = demuxer;
730 tvh->functions->control(tvh->priv,TVI_CONTROL_VBI_INIT,
731 &(tvh->tv_param->teletext.device));
732 tvh->functions->control(tvh->priv,TVI_CONTROL_GET_VBI_PTR,
733 &demuxer->teletext);
735 if (!open_tv(tvh)){
736 tv_uninit(tvh);
737 return NULL;
739 funcs = tvh->functions;
740 demuxer->priv=tvh;
742 sh_video = new_sh_video(demuxer, 0);
744 /* get IMAGE FORMAT */
745 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &sh_video->format);
746 // if (IMGFMT_IS_RGB(sh_video->format) || IMGFMT_IS_BGR(sh_video->format))
747 // sh_video->format = 0x0;
749 /* set FPS and FRAMETIME */
751 if(!sh_video->fps)
753 float tmp;
754 if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
755 sh_video->fps = 25.0f; /* on PAL */
756 else sh_video->fps = tmp;
759 if (tvh->tv_param->fps != -1.0f)
760 sh_video->fps = tvh->tv_param->fps;
762 sh_video->frametime = 1.0f/sh_video->fps;
764 /* If playback only mode, go to immediate mode, fail silently */
765 if(tvh->tv_param->immediate == 1)
767 funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
768 tvh->tv_param->noaudio = 1;
771 /* disable TV audio if -nosound is present */
772 if (!demuxer->audio || demuxer->audio->id == -2) {
773 tvh->tv_param->noaudio = 1;
776 /* set width */
777 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w);
779 /* set height */
780 funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h);
782 demuxer->video->sh = sh_video;
783 sh_video->ds = demuxer->video;
784 demuxer->video->id = 0;
785 demuxer->seekable = 0;
787 /* here comes audio init */
788 if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
790 int audio_format;
791 int sh_audio_format;
792 char buf[128];
794 /* yeah, audio is present */
796 funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
797 &tvh->tv_param->audiorate);
799 if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
800 goto no_audio;
802 switch(audio_format)
804 case AF_FORMAT_U8:
805 case AF_FORMAT_S8:
806 case AF_FORMAT_U16_LE:
807 case AF_FORMAT_U16_BE:
808 case AF_FORMAT_S16_LE:
809 case AF_FORMAT_S16_BE:
810 case AF_FORMAT_S32_LE:
811 case AF_FORMAT_S32_BE:
812 sh_audio_format = 0x1; /* PCM */
813 break;
814 case AF_FORMAT_IMA_ADPCM:
815 case AF_FORMAT_MU_LAW:
816 case AF_FORMAT_A_LAW:
817 case AF_FORMAT_MPEG2:
818 default:
819 mp_tmsg(MSGT_TV, MSGL_ERR, "Audio type '%s (%x)' unsupported!\n",
820 af_fmt2str(audio_format, buf, 128), audio_format);
821 goto no_audio;
824 sh_audio = new_sh_audio(demuxer, 0);
826 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
827 &sh_audio->samplerate);
828 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLESIZE,
829 &sh_audio->samplesize);
830 funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
831 &sh_audio->channels);
833 sh_audio->format = sh_audio_format;
834 sh_audio->sample_format = audio_format;
836 sh_audio->i_bps = sh_audio->o_bps =
837 sh_audio->samplerate * sh_audio->samplesize *
838 sh_audio->channels;
840 // emulate WF for win32 codecs:
841 sh_audio->wf = malloc(sizeof(*sh_audio->wf));
842 sh_audio->wf->wFormatTag = sh_audio->format;
843 sh_audio->wf->nChannels = sh_audio->channels;
844 sh_audio->wf->wBitsPerSample = sh_audio->samplesize * 8;
845 sh_audio->wf->nSamplesPerSec = sh_audio->samplerate;
846 sh_audio->wf->nBlockAlign = sh_audio->samplesize * sh_audio->channels;
847 sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps;
849 mp_tmsg(MSGT_DECVIDEO, MSGL_V, " TV audio: %d channels, %d bits, %d Hz\n",
850 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
851 sh_audio->wf->nSamplesPerSec);
853 demuxer->audio->sh = sh_audio;
854 sh_audio->ds = demuxer->audio;
855 demuxer->audio->id = 0;
857 no_audio:
859 if(!(funcs->start(tvh->priv))){
860 // start failed :(
861 tv_uninit(tvh);
862 return NULL;
865 /* set color eq */
866 tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
867 tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
868 tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
869 tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
871 if(tvh->tv_param->gain!=-1)
872 if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
873 mp_msg(MSGT_TV,MSGL_WARN,"Unable to set gain control!\n");
875 teletext_control(demuxer->teletext,TV_VBI_CONTROL_RESET,
876 &tvh->tv_param->teletext);
878 return demuxer;
881 static void demux_close_tv(demuxer_t *demuxer)
883 tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
884 if (!tvh) return;
885 tv_uninit(tvh);
886 free(tvh);
887 demuxer->priv=NULL;
888 demuxer->teletext=NULL;
891 /* utilities for mplayer (not mencoder!!) */
892 int tv_set_color_options(tvi_handle_t *tvh, int opt, int value)
894 const tvi_functions_t *funcs = tvh->functions;
896 switch(opt)
898 case TV_COLOR_BRIGHTNESS:
899 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_BRIGHTNESS, &value);
900 case TV_COLOR_HUE:
901 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_HUE, &value);
902 case TV_COLOR_SATURATION:
903 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_SATURATION, &value);
904 case TV_COLOR_CONTRAST:
905 return funcs->control(tvh->priv, TVI_CONTROL_VID_SET_CONTRAST, &value);
906 default:
907 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
910 return TVI_CONTROL_UNKNOWN;
913 int tv_get_color_options(tvi_handle_t *tvh, int opt, int* value)
915 const tvi_functions_t *funcs = tvh->functions;
917 switch(opt)
919 case TV_COLOR_BRIGHTNESS:
920 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_BRIGHTNESS, value);
921 case TV_COLOR_HUE:
922 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HUE, value);
923 case TV_COLOR_SATURATION:
924 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_SATURATION, value);
925 case TV_COLOR_CONTRAST:
926 return funcs->control(tvh->priv, TVI_CONTROL_VID_GET_CONTRAST, value);
927 default:
928 mp_tmsg(MSGT_TV, MSGL_WARN, "Unknown color option (%d) specified!\n", opt);
931 return TVI_CONTROL_UNKNOWN;
934 int tv_get_freq(tvi_handle_t *tvh, unsigned long *freq)
936 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
938 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, freq);
939 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
940 *freq, (float)*freq/16);
942 return 1;
945 int tv_set_freq(tvi_handle_t *tvh, unsigned long freq)
947 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) == TVI_CONTROL_TRUE)
949 // unsigned long freq = atof(tvh->tv_param->freq)*16;
951 /* set freq in MHz */
952 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_FREQ, &freq);
954 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_FREQ, &freq);
955 mp_tmsg(MSGT_TV, MSGL_V, "Current frequency: %lu (%.3f)\n",
956 freq, (float)freq/16);
958 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
959 &tvh->tv_param->teletext);
960 return 1;
963 int tv_get_signal(tvi_handle_t *tvh)
965 int signal=0;
966 if (tvh->functions->control(tvh->priv, TVI_CONTROL_IS_TUNER, 0) != TVI_CONTROL_TRUE ||
967 tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_GET_SIGNAL, &signal)!=TVI_CONTROL_TRUE)
968 return 0;
970 return signal;
973 /*****************************************************************
974 * \brief tune current frequency by step_interval value
975 * \parameter step_interval increment value in 1/16 MHz
976 * \note frequency is rounded to 1/16 MHz value
977 * \return 1
980 int tv_step_freq(tvi_handle_t* tvh, float step_interval){
981 unsigned long frequency;
983 tvh->tv_param->scan=0;
984 tv_get_freq(tvh,&frequency);
985 frequency+=step_interval;
986 return tv_set_freq(tvh,frequency);
989 int tv_step_channel_real(tvi_handle_t *tvh, int direction)
991 struct CHANLIST cl;
993 tvh->tv_param->scan=0;
994 if (direction == TV_CHANNEL_LOWER)
996 if (tvh->channel-1 >= 0)
998 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
999 cl = tvh->chanlist_s[--tvh->channel];
1000 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1001 cl.name, (float)cl.freq/1000);
1002 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1006 if (direction == TV_CHANNEL_HIGHER)
1008 if (tvh->channel+1 < chanlists[tvh->chanlist].count)
1010 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1011 cl = tvh->chanlist_s[++tvh->channel];
1012 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1013 cl.name, (float)cl.freq/1000);
1014 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1017 return 1;
1020 int tv_step_channel(tvi_handle_t *tvh, int direction) {
1021 tvh->tv_param->scan=0;
1022 if (tv_channel_list) {
1023 if (direction == TV_CHANNEL_HIGHER) {
1024 tv_channel_last = tv_channel_current;
1025 if (tv_channel_current->next)
1026 tv_channel_current = tv_channel_current->next;
1027 else
1028 tv_channel_current = tv_channel_list;
1030 tv_set_norm_i(tvh, tv_channel_current->norm);
1031 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1032 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1033 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1035 if (direction == TV_CHANNEL_LOWER) {
1036 tv_channel_last = tv_channel_current;
1037 if (tv_channel_current->prev)
1038 tv_channel_current = tv_channel_current->prev;
1039 else
1040 while (tv_channel_current->next)
1041 tv_channel_current = tv_channel_current->next;
1042 tv_set_norm_i(tvh, tv_channel_current->norm);
1043 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1044 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n",
1045 tv_channel_current->number, tv_channel_current->name, (float)tv_channel_current->freq/1000);
1047 } else tv_step_channel_real(tvh, direction);
1048 return 1;
1051 int tv_set_channel_real(tvi_handle_t *tvh, char *channel) {
1052 int i;
1053 struct CHANLIST cl;
1055 tvh->tv_param->scan=0;
1056 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1057 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1059 cl = tvh->chanlist_s[i];
1060 // printf("count%d: name: %s, freq: %d\n",
1061 // i, cl.name, cl.freq);
1062 if (!strcasecmp(cl.name, channel))
1064 tvh->channel = i;
1065 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1066 cl.name, (float)cl.freq/1000);
1067 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1068 break;
1071 return 1;
1074 int tv_set_channel(tvi_handle_t *tvh, char *channel) {
1075 int i, channel_int;
1077 tvh->tv_param->scan=0;
1078 if (tv_channel_list) {
1079 tv_channel_last = tv_channel_current;
1080 channel_int = atoi(channel);
1081 tv_channel_current = tv_channel_list;
1082 for (i = 1; i < channel_int; i++)
1083 if (tv_channel_current->next)
1084 tv_channel_current = tv_channel_current->next;
1085 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1086 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1087 tv_set_norm_i(tvh, tv_channel_current->norm);
1088 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1089 } else tv_set_channel_real(tvh, channel);
1090 return 1;
1093 int tv_last_channel(tvi_handle_t *tvh) {
1095 tvh->tv_param->scan=0;
1096 if (tv_channel_list) {
1097 tv_channels_t *tmp;
1099 tmp = tv_channel_last;
1100 tv_channel_last = tv_channel_current;
1101 tv_channel_current = tmp;
1103 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s - %s (freq: %.3f)\n", tv_channel_current->number,
1104 tv_channel_current->name, (float)tv_channel_current->freq/1000);
1105 tv_set_norm_i(tvh, tv_channel_current->norm);
1106 tv_set_freq(tvh, (unsigned long)(((float)tv_channel_current->freq/1000)*16));
1107 } else {
1108 int i;
1109 struct CHANLIST cl;
1111 for (i = 0; i < chanlists[tvh->chanlist].count; i++)
1113 cl = tvh->chanlist_s[i];
1114 if (!strcasecmp(cl.name, tv_channel_last_real))
1116 strcpy(tv_channel_last_real, tvh->chanlist_s[tvh->channel].name);
1117 tvh->channel = i;
1118 mp_tmsg(MSGT_TV, MSGL_INFO, "Selected channel: %s (freq: %.3f)\n",
1119 cl.name, (float)cl.freq/1000);
1120 tv_set_freq(tvh, (unsigned long)(((float)cl.freq/1000)*16));
1121 break;
1125 return 1;
1128 int tv_step_norm(tvi_handle_t *tvh)
1130 tvh->norm++;
1131 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1132 &tvh->norm) != TVI_CONTROL_TRUE) {
1133 tvh->norm = 0;
1134 if (tvh->functions->control(tvh->priv, TVI_CONTROL_TUN_SET_NORM,
1135 &tvh->norm) != TVI_CONTROL_TRUE) {
1136 mp_tmsg(MSGT_TV, MSGL_ERR, "Error: Cannot set norm!\n");
1137 return 0;
1140 teletext_control(tvh->demuxer->teletext,TV_VBI_CONTROL_RESET,
1141 &tvh->tv_param->teletext);
1142 return 1;
1145 int tv_step_chanlist(tvi_handle_t *tvh)
1147 return 1;
1150 demuxer_desc_t demuxer_desc_tv = {
1151 "Tv card demuxer",
1152 "tv",
1153 "TV",
1154 "Alex Beregszaszi, Charles R. Henrich",
1155 "?",
1156 DEMUXER_TYPE_TV,
1157 0, // no autodetect
1158 NULL,
1159 demux_tv_fill_buffer,
1160 demux_open_tv,
1161 demux_close_tv,
1162 NULL,
1163 NULL