$OpenBSD: patch-libmpdemux_tvi_bsdbt848_c,v 1.5 2007/04/19 08:37:09 ajacoutot Exp $
--- libmpdemux/tvi_bsdbt848.c.orig	Sun Jun 11 20:35:46 2006
+++ libmpdemux/tvi_bsdbt848.c	Thu Apr 19 09:41:03 2007
@@ -40,9 +40,12 @@
 #include <string.h>
 
 #include <sys/param.h>
-#ifdef __NetBSD__
+#if defined(__NetBSD__) || defined(__OpenBSD__)
 #include <dev/ic/bt8xx.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
 #include <sys/audioio.h>
+#include <string.h>
 #elif defined(__DragonFly__)
 #include <dev/video/meteor/ioctl_meteor.h>
 #include <dev/video/bktr/ioctl_bt848.h>
@@ -54,15 +57,17 @@
 #include <machine/ioctl_bt848.h>
 #endif
 
+#if !defined(__OpenBSD__)
 #ifdef HAVE_SYS_SOUNDCARD_H
 #include <sys/soundcard.h>
 #else
-#ifdef HAVE_SOUNDCARD_H
+#if defined(HAVE_SOUNDCARD_H)
 #include <soundcard.h>
 #else
 #include <machine/soundcard.h>
 #endif
 #endif
+#endif
 
 #include "libaf/af_format.h"
 #include "libvo/img_format.h"
@@ -129,7 +134,7 @@ typedef struct {
     int tunerfd;
     int tunerready;
     u_long tunerfreq;
-    struct bktr_chnlset cset;
+    int tunertype;
 
 /* Other */
 
@@ -140,9 +145,34 @@ typedef struct {
 
 #include "tvi_def.h"
 
+static int chnlsets[17] = {
+    CHNLSET_NABCST,    /* us-bcast */
+    CHNLSET_CABLEIRC,  /* us-cable */
+    CHNLSET_CABLEHRC,  /* us-cable-hrc */
+    CHNLSET_JPNBCST,   /* japan-bcast */
+    CHNLSET_JPNCABLE,  /* japan-cable */
+    CHNLSET_WEUROPE,   /* europe-west */
+    CHNLSET_XUSSR,     /* europe-east ? */
+    CHNLSET_WEUROPE,   /* italy ? */
+    CHNLSET_AUSTRALIA, /* newzealand ? */
+    CHNLSET_AUSTRALIA, /* australia */
+    CHNLSET_WEUROPE,   /* ireland ? */
+    CHNLSET_FRANCE,    /* france */
+    CHNLSET_XUSSR,     /* china-bcast ? */
+    CHNLSET_WEUROPE,   /* southafrica ? */
+    CHNLSET_WEUROPE,   /* argentina ? */
+    CHNLSET_XUSSR,     /* russia */
+    NULL,              /* NULL */
+};
+
+static int sources[6] = { METEOR_INPUT_DEV1, METEOR_INPUT_DEV0, METEOR_INPUT_DEV2,
+    METEOR_INPUT_DEV3, METEOR_INPUT_DEV_RGB, METEOR_INPUT_DEV_SVIDEO };
+
+
 static priv_t *G_private=NULL;
 
 static int getinput(int innumber);
+static int getsource(int insrc);
 
 static void processframe(int signal)
 {
@@ -212,6 +242,19 @@ static int control(priv_t *priv, int cmd, void *arg)
         return(TVI_CONTROL_TRUE);        
         }
 
+    case TVI_CONTROL_TUN_SET_CHANLIST:
+	{
+	priv->tunertype = chnlsets[(int)*(void **)arg];
+	
+	if(ioctl(priv->tunerfd, TVTUNER_SETTYPE, &priv->tunertype) < 0)
+	    {
+	    perror("SETTYPE:ioctl");
+	    return(0);
+	    }
+
+	return(TVI_CONTROL_TRUE);
+	}
+	
     case TVI_CONTROL_TUN_GET_TUNER:
     case TVI_CONTROL_TUN_SET_TUNER:
 
@@ -219,23 +262,24 @@ static int control(priv_t *priv, int cmd, void *arg)
 
     case TVI_CONTROL_SPC_GET_INPUT:
         {
-        if(ioctl(priv->btfd, METEORGINPUT, &priv->input) < 0)
+        if(ioctl(priv->btfd, METEORGINPUT, &priv->source) < 0)
             {
             perror("GINPUT:ioctl");
             return(TVI_CONTROL_FALSE);
             }
 
+	priv->input = getsource(priv->source);
         (int)*(void **)arg = priv->input;
         return(TVI_CONTROL_TRUE);
         }
     
     case TVI_CONTROL_SPC_SET_INPUT:
         {
-        priv->input = getinput((int)*(void **)arg);
+        priv->source = getinput((int)*(void **)arg);
 
-        if(ioctl(priv->btfd, METEORSINPUT, &priv->input) < 0) 
+        if(ioctl(priv->btfd, METEORSINPUT, &priv->source) < 0) 
             {
-            perror("tunerfreq:ioctl");
+            perror("SINPUT:ioctl");
             return(0);
             }
 
@@ -261,8 +305,15 @@ static int control(priv_t *priv, int cmd, void *arg)
     case TVI_CONTROL_AUD_SET_SAMPLERATE:
         {
         int dspspeed = (int)*(void **)arg;
+#if defined(__OpenBSD__)
+           audio_info_t audio_if;
+           AUDIO_INITINFO(&audio_if);
+           audio_if.record.sample_rate = dspspeed;
+           if (ioctl(priv->dspfd, AUDIO_SETINFO, &audio_if) == -1)
+#else
 
-           if(ioctl(priv->dspfd, SNDCTL_DSP_SPEED, &dspspeed) == -1) 
+           if(ioctl(priv->dspfd, SNDCTL_DSP_SPEED, &dspspeed) == -1)
+#endif
             {
             perror("invalidaudiorate");
             return(TVI_CONTROL_FALSE);
@@ -473,7 +524,12 @@ static int init(priv_t *priv)
 int marg;
 int count;
 u_short tmp_fps;
+int tuner_audio;
+#if defined(__OpenBSD__)
+audio_info_t audio_if;
+#endif
 
+
 G_private = priv; /* Oooh, sick */
 
 /* Video Configuration */
@@ -485,7 +541,7 @@ priv->iformat = METEOR_FMT_PAL;
 priv->maxheight = PAL_HEIGHT;
 priv->maxwidth = PAL_WIDTH;
 priv->maxfps = PAL_FPS;
-priv->source = METEOR_INPUT_DEV0;
+priv->source = METEOR_INPUT_DEV1;
 priv->fps = priv->maxfps;
 
 priv->starttime=0;
@@ -572,10 +628,26 @@ if(priv->tunerfd < 0)
     priv->tunerready = FALSE;
     }
 
+if(ioctl(priv->tunerfd, BT848_GAUDIO, &tuner_audio) < 0)
+    {
+    perror("tuner get audio");
+    priv->tunerready = FALSE;
+    }
+if(tuner_audio & AUDIO_MUTE)
+    {
+    tuner_audio = AUDIO_UNMUTE;
+    if(ioctl(priv->tunerfd, BT848_SAUDIO, &tuner_audio) < 0)
+        {
+        perror("tuner unmute");
+        priv->tunerready = FALSE;
+        }
+    }
+
+
 /* Audio Configuration */
 
 priv->dspready = TRUE;
-#ifdef __NetBSD__
+#if defined(__NetBSD__) || defined(__OpenBSD__)
 priv->dspdev = strdup("/dev/sound");
 #else
 priv->dspdev = strdup("/dev/dsp");
@@ -583,7 +655,11 @@ priv->dspdev = strdup("/dev/dsp");
 priv->dspsamplesize = 16;
 priv->dspstereo = 1;
 priv->dspspeed = 44100;
+#if defined (__OpenBSD__)
+priv->dspfmt = AUDIO_ENCODING_SLINEAR_LE;
+#else
 priv->dspfmt = AFMT_S16_LE;
+#endif
 priv->dspbytesread = 0;
 priv->dsprate = priv->dspspeed * priv->dspsamplesize/8*(priv->dspstereo+1);
 priv->dspframesize = priv->dspspeed*priv->dspsamplesize/8/priv->fps * 
@@ -597,6 +673,31 @@ if((priv->dspfd = open (priv->dspdev, O_RDONLY, 0)) < 
 
 marg = (256 << 16) | 12;
 
+#if defined(__OpenBSD__)
+
+    AUDIO_INITINFO(&audio_if);
+
+    audio_if.record.precision = priv->dspsamplesize;
+    audio_if.record.channels = priv->dspstereo+1;
+    audio_if.record.sample_rate = priv->dspspeed;
+    audio_if.record.encoding = priv->dspfmt;
+
+    audio_if.mode = AUMODE_RECORD;
+
+    audio_if.play.open = 0;
+    audio_if.play.pause = 1;
+    audio_if.record.open = 1;
+    audio_if.record.pause = 0;
+
+    if (ioctl(priv->dspfd, AUDIO_SETINFO, &audio_if) < 0)
+        {
+        perror ("AUDIO_SETINFO:ioctl");
+        close(priv->dspfd);
+        priv->dspready = FALSE;
+        }
+
+#else
+
 if (ioctl(priv->dspfd, SNDCTL_DSP_SETFRAGMENT, &marg ) < 0 ) 
     {
     perror("setfrag");
@@ -614,6 +715,8 @@ if((priv->dspready == TRUE) &&
     priv->dspready = FALSE;
     }
 
+#endif
+
 return(1);
 }
 
@@ -623,6 +726,7 @@ static int start(priv_t *priv)
 int tmp;
 struct timeval curtime;
 int marg;
+int tuner_audio;
 
 fprintf(stderr,"START\n");
 if(priv->videoready == FALSE) return(0);
@@ -658,6 +762,7 @@ return(1);
 static int uninit(priv_t *priv)
 {
 int marg;
+int tuner_audio;
 
 if(priv->videoready == FALSE) return(0);
 
@@ -680,6 +785,15 @@ if(ioctl(priv->btfd, METEORCAPTUR, &marg) < 0 ) 
 close(priv->btfd);
 close(priv->dspfd);
 
+tuner_audio = AUDIO_MUTE;
+if(ioctl(priv->tunerfd, BT848_SAUDIO, &tuner_audio) < 0)
+    {
+    perror("tuner mute");
+    priv->tunerready = FALSE;
+    }
+
+close(priv->tunerfd);
+
 priv->dspfd = -1;
 priv->btfd = -1;
 
@@ -802,13 +916,13 @@ return(priv->dspbytesread * 1.0 / priv->dsprate);
 static int get_audio_framesize(priv_t *priv)
 {
 int bytesavail;
-#ifdef __NetBSD__
+#if defined(__NetBSD__) || defined(__OpenBSD__)
 struct audio_info auinf;
 #endif
 
 if(priv->dspready == FALSE) return 0;
 
-#ifdef __NetBSD__
+#if defined(__NetBSD__) || defined(__OpenBSD__)
 if(ioctl(priv->dspfd, AUDIO_GETINFO, &auinf) < 0) 
     {
     perror("AUDIO_GETINFO");
@@ -827,24 +941,28 @@ if(ioctl(priv->dspfd, FIONREAD, &bytesavail) < 0) 
 /* When mencoder wants audio data, it wants data..
    it won't go do anything else until it gets it :( */
 
-if(bytesavail == 0) return FRAGSIZE;
+// if(bytesavail == 0) return FRAGSIZE;
 
 return(bytesavail);
 }
 
 static int getinput(int innumber)
 {
-switch(innumber)
-    {
-    case 0: return METEOR_INPUT_DEV0;     /* RCA   */
-    case 1: return METEOR_INPUT_DEV1;     /* Tuner */
-    case 2: return METEOR_INPUT_DEV2;     /* In 1  */
-    case 3: return METEOR_INPUT_DEV3;     /* In 2  */
-    case 4: return METEOR_INPUT_DEV_RGB;     /* RGB   */
-    case 5: return METEOR_INPUT_DEV_SVIDEO; /* SVid  */
-    }
-
+if (sources[innumber])
+    return sources[innumber];
+    
 return 0;
 }
+
+static int getsource(int insrc)
+{
+    int i;
+
+    for (i = 0; sources[i]; i++)
+        if (sources[i] == insrc)
+            return i;
+
+    return 0;
+ }
 
 #endif /* USE_TV */
