[PATCH] libv4l1: move VIDIOCGAUDIO and VIDIOCSAUDIO to libv4l1

2010-06-07 Thread huzaifas
From: Huzaifa Sidhpurwala huzai...@redhat.com

move VIDIOCGAUDIO and VIDIOCSAUDIO to libv4l1

Signed-of-by: Huzaifa Sidhpurwala huzai...@redhat.com
---
 lib/libv4l1/libv4l1-priv.h |7 ++
 lib/libv4l1/libv4l1.c  |  160 
 2 files changed, 167 insertions(+), 0 deletions(-)

diff --git a/lib/libv4l1/libv4l1-priv.h b/lib/libv4l1/libv4l1-priv.h
index 11f4fd0..11ee57a 100644
--- a/lib/libv4l1/libv4l1-priv.h
+++ b/lib/libv4l1/libv4l1-priv.h
@@ -60,6 +60,13 @@ extern FILE *v4l1_log_file;
 #define min(a, b) (((a)  (b)) ? (a) : (b))
 #endif
 
+#define DIV_ROUND_CLOSEST(x, divisor)(  \
+{   \
+   typeof(divisor) __divisor = divisor;\
+   (((x) + ((__divisor) / 2)) / (__divisor));  \
+}   \
+)
+
 struct v4l1_dev_info {
int fd;
int flags;
diff --git a/lib/libv4l1/libv4l1.c b/lib/libv4l1/libv4l1.c
index 2981c40..263d564 100644
--- a/lib/libv4l1/libv4l1.c
+++ b/lib/libv4l1/libv4l1.c
@@ -233,6 +233,59 @@ static int v4l1_set_format(int index, unsigned int width,
return result;
 }
 
+static int set_v4l_control(int fd, int cid, int value)
+{
+   struct v4l2_queryctrl qctrl2;
+   struct v4l2_control ctrl2;
+   int result;
+
+   qctrl2.id = cid;
+   result = v4l2_ioctl(fd, VIDIOC_QUERYCTRL, qctrl2);
+   if (result  0)
+   return 0;
+   if (result == 0 
+   !(qctrl2.flags  V4L2_CTRL_FLAG_DISABLED) 
+   !(qctrl2.flags  V4L2_CTRL_FLAG_GRABBED)) {
+   if (value  0)
+   value = 0;
+   if (value  65535)
+   value = 65535;
+   if (value  qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
+   value = 65535;
+   ctrl2.id = qctrl2.id;
+   ctrl2.value =
+   (value * (qctrl2.maximum - qctrl2.minimum)
+   + 32767)
+   / 65535;
+   ctrl2.value += qctrl2.minimum;
+   result = v4l2_ioctl(fd, VIDIOC_S_CTRL, ctrl2);
+   }
+   return 0;
+}
+
+static int get_v4l_control(int fd, int cid)
+{
+   struct v4l2_queryctrl qctrl2;
+   struct v4l2_control ctrl2;
+   int result;
+
+   qctrl2.id = cid;
+   result = v4l2_ioctl(fd, VIDIOC_QUERYCTRL, qctrl2);
+   if (result  0)
+   return 0;
+   if (result == 0  !(qctrl2.flags  V4L2_CTRL_FLAG_DISABLED)) {
+   ctrl2.id = qctrl2.id;
+   result = v4l2_ioctl(fd, VIDIOC_G_CTRL, ctrl2);
+   if (result  0)
+   return 0;
+
+   return DIV_ROUND_CLOSEST((ctrl2.value-qctrl2.minimum) * 65535,
+   qctrl2.maximum - qctrl2.minimum);
+   }
+   return 0;
+}
+
+
 static void v4l1_find_min_and_max_size(int index, struct v4l2_format *fmt2)
 {
int i;
@@ -983,6 +1036,113 @@ int v4l1_ioctl(int fd, unsigned long int request, ...)
 
break;
}
+
+   case VIDIOCSAUDIO: {
+   struct video_audio *aud = arg;
+   struct v4l2_audio aud2 = { 0, };
+   struct v4l2_tuner tun2 = { 0, };
+
+   aud2.index = aud-audio;
+   result = v4l2_ioctl(fd, VIDIOC_S_AUDIO, aud2);
+   if (result  0)
+   break;
+
+   set_v4l_control(fd, V4L2_CID_AUDIO_VOLUME,
+   aud-volume);
+   set_v4l_control(fd, V4L2_CID_AUDIO_BASS,
+   aud-bass);
+   set_v4l_control(fd, V4L2_CID_AUDIO_TREBLE,
+   aud-treble);
+   set_v4l_control(fd, V4L2_CID_AUDIO_BALANCE,
+   aud-balance);
+   set_v4l_control(fd, V4L2_CID_AUDIO_MUTE,
+   !!(aud-flags  VIDEO_AUDIO_MUTE));
+
+   result = v4l2_ioctl(fd, VIDIOC_G_TUNER, tun2);
+   if (result  0)
+   break;
+   if (result == 0) {
+   switch (aud-mode) {
+   default:
+   case VIDEO_SOUND_MONO:
+   case VIDEO_SOUND_LANG1:
+   tun2.audmode = V4L2_TUNER_MODE_MONO;
+   break;
+   case VIDEO_SOUND_STEREO:
+   tun2.audmode = V4L2_TUNER_MODE_STEREO;
+   break;
+   case VIDEO_SOUND_LANG2:
+   tun2.audmode = V4L2_TUNER_MODE_LANG2;
+   break;
+   }
+   result = v4l2_ioctl(fd, VIDIOC_S_TUNER, tun2);
+   }
+   break;
+   }
+
+   case VIDIOCGAUDIO: {
+   int i;
+   struct video_audio *aud = arg;
+   struct v4l2_queryctrl 

Re: [PATCH] libv4l1: move VIDIOCGAUDIO and VIDIOCSAUDIO to libv4l1

2010-06-07 Thread Hans de Goede

Hi,

See comments inline.

On 06/07/2010 11:50 AM, huzai...@redhat.com wrote:

From: Huzaifa Sidhpurwalahuzai...@redhat.com

move VIDIOCGAUDIO and VIDIOCSAUDIO to libv4l1

Signed-of-by: Huzaifa Sidhpurwalahuzai...@redhat.com
---
  lib/libv4l1/libv4l1-priv.h |7 ++
  lib/libv4l1/libv4l1.c  |  160 
  2 files changed, 167 insertions(+), 0 deletions(-)

diff --git a/lib/libv4l1/libv4l1-priv.h b/lib/libv4l1/libv4l1-priv.h
index 11f4fd0..11ee57a 100644
--- a/lib/libv4l1/libv4l1-priv.h
+++ b/lib/libv4l1/libv4l1-priv.h
@@ -60,6 +60,13 @@ extern FILE *v4l1_log_file;
  #define min(a, b) (((a)  (b)) ? (a) : (b))
  #endif

+#define DIV_ROUND_CLOSEST(x, divisor)(  \
+{   \
+   typeof(divisor) __divisor = divisor;\
+   (((x) + ((__divisor) / 2)) / (__divisor));  \
+}   \
+)
+
  struct v4l1_dev_info {
int fd;
int flags;
diff --git a/lib/libv4l1/libv4l1.c b/lib/libv4l1/libv4l1.c
index 2981c40..263d564 100644
--- a/lib/libv4l1/libv4l1.c
+++ b/lib/libv4l1/libv4l1.c
@@ -233,6 +233,59 @@ static int v4l1_set_format(int index, unsigned int width,
return result;
  }

+static int set_v4l_control(int fd, int cid, int value)
+{
+   struct v4l2_queryctrl qctrl2;
+   struct v4l2_control ctrl2;
+   int result;
+
+   qctrl2.id = cid;
+   result = v4l2_ioctl(fd, VIDIOC_QUERYCTRL,qctrl2);
+   if (result  0)
+   return 0;
+   if (result == 0
+   !(qctrl2.flags  V4L2_CTRL_FLAG_DISABLED)
+   !(qctrl2.flags  V4L2_CTRL_FLAG_GRABBED)) {
+   if (value  0)
+   value = 0;
+   if (value  65535)
+   value = 65535;
+   if (value  qctrl2.type == V4L2_CTRL_TYPE_BOOLEAN)
+   value = 65535;
+   ctrl2.id = qctrl2.id;
+   ctrl2.value =
+   (value * (qctrl2.maximum - qctrl2.minimum)
+   + 32767)
+   / 65535;
+   ctrl2.value += qctrl2.minimum;
+   result = v4l2_ioctl(fd, VIDIOC_S_CTRL,ctrl2);
+   }
+   return 0;
+}
+
+static int get_v4l_control(int fd, int cid)
+{
+   struct v4l2_queryctrl qctrl2;
+   struct v4l2_control ctrl2;
+   int result;
+
+   qctrl2.id = cid;
+   result = v4l2_ioctl(fd, VIDIOC_QUERYCTRL,qctrl2);
+   if (result  0)
+   return 0;
+   if (result == 0  !(qctrl2.flags  V4L2_CTRL_FLAG_DISABLED)) {
+   ctrl2.id = qctrl2.id;
+   result = v4l2_ioctl(fd, VIDIOC_G_CTRL,ctrl2);
+   if (result  0)
+   return 0;
+
+   return DIV_ROUND_CLOSEST((ctrl2.value-qctrl2.minimum) * 65535,
+   qctrl2.maximum - qctrl2.minimum);
+   }
+   return 0;
+}
+
+


These 2 functions are already present in libv4l2, they are called
v4l2_set_control and v4l2_get_control resp.

Regards,

Hans



  static void v4l1_find_min_and_max_size(int index, struct v4l2_format *fmt2)
  {
int i;
@@ -983,6 +1036,113 @@ int v4l1_ioctl(int fd, unsigned long int request, ...)

break;
}
+
+   case VIDIOCSAUDIO: {
+   struct video_audio *aud = arg;
+   struct v4l2_audio aud2 = { 0, };
+   struct v4l2_tuner tun2 = { 0, };
+
+   aud2.index = aud-audio;
+   result = v4l2_ioctl(fd, VIDIOC_S_AUDIO,aud2);
+   if (result  0)
+   break;
+
+   set_v4l_control(fd, V4L2_CID_AUDIO_VOLUME,
+   aud-volume);
+   set_v4l_control(fd, V4L2_CID_AUDIO_BASS,
+   aud-bass);
+   set_v4l_control(fd, V4L2_CID_AUDIO_TREBLE,
+   aud-treble);
+   set_v4l_control(fd, V4L2_CID_AUDIO_BALANCE,
+   aud-balance);
+   set_v4l_control(fd, V4L2_CID_AUDIO_MUTE,
+   !!(aud-flags  VIDEO_AUDIO_MUTE));
+
+   result = v4l2_ioctl(fd, VIDIOC_G_TUNER,tun2);
+   if (result  0)
+   break;
+   if (result == 0) {
+   switch (aud-mode) {
+   default:
+   case VIDEO_SOUND_MONO:
+   case VIDEO_SOUND_LANG1:
+   tun2.audmode = V4L2_TUNER_MODE_MONO;
+   break;
+   case VIDEO_SOUND_STEREO:
+   tun2.audmode = V4L2_TUNER_MODE_STEREO;
+   break;
+   case VIDEO_SOUND_LANG2:
+   tun2.audmode = V4L2_TUNER_MODE_LANG2;
+   break;
+   }
+   result = v4l2_ioctl(fd,