summaryrefslogtreecommitdiff
path: root/v4l2-apps/lib
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@infradead.org>2006-11-16 12:02:28 -0200
committerMauro Carvalho Chehab <mchehab@infradead.org>2006-11-16 12:02:28 -0200
commitf90bff01fa3468160e9403fb488c1f319c30c6dd (patch)
tree8bc246501930c82b13484a96b57976c128d091a9 /v4l2-apps/lib
parentcbbc1373bd674a772030df86c05fdb5cadffa95c (diff)
downloadmediapointer-dvb-s2-f90bff01fa3468160e9403fb488c1f319c30c6dd.tar.gz
mediapointer-dvb-s2-f90bff01fa3468160e9403fb488c1f319c30c6dd.tar.bz2
Start adding v4l2_mmap_bufs to prepare for stream
From: Mauro Carvalho Chehab <mchehab@infradead.org> This method requests buffers and maps it into a mmaped memory. Still under testing. Still need other stuff to make stream to work. Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'v4l2-apps/lib')
-rw-r--r--v4l2-apps/lib/v4l2_driver.c176
-rw-r--r--v4l2-apps/lib/v4l2_driver.h21
2 files changed, 189 insertions, 8 deletions
diff --git a/v4l2-apps/lib/v4l2_driver.c b/v4l2-apps/lib/v4l2_driver.c
index 1b0a61109..e79ffb0b5 100644
--- a/v4l2-apps/lib/v4l2_driver.c
+++ b/v4l2-apps/lib/v4l2_driver.c
@@ -10,9 +10,10 @@
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
-
*/
+/* FIXME: Add checks at calloc for out-of-memory errors */
+
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
@@ -20,6 +21,7 @@
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
+#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -51,6 +53,74 @@ static void free_list(struct drv_list **list_ptr)
}
/****************************************************************************
+ Auxiliary Arrays to aid debug messages
+ ****************************************************************************/
+char *v4l2_field_names[] = {
+ [V4L2_FIELD_ANY] = "any",
+ [V4L2_FIELD_NONE] = "none",
+ [V4L2_FIELD_TOP] = "top",
+ [V4L2_FIELD_BOTTOM] = "bottom",
+ [V4L2_FIELD_INTERLACED] = "interlaced",
+ [V4L2_FIELD_SEQ_TB] = "seq-tb",
+ [V4L2_FIELD_SEQ_BT] = "seq-bt",
+ [V4L2_FIELD_ALTERNATE] = "alternate",
+};
+
+char *v4l2_type_names[] = {
+ [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
+ [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
+ [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
+ [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
+ [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
+ [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
+ [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "slicec-vbi-out",
+};
+
+static char *v4l2_memory_names[] = {
+ [V4L2_MEMORY_MMAP] = "mmap",
+ [V4L2_MEMORY_USERPTR] = "userptr",
+ [V4L2_MEMORY_OVERLAY] = "overlay",
+};
+
+#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*arr))
+#define prt_names(a,arr) (((a)<ARRAY_SIZE(arr))?arr[a]:"unknown")
+
+char *prt_caps(uint32_t caps)
+{
+ static char s[4096]="";
+
+ if (V4L2_CAP_VIDEO_CAPTURE & caps)
+ strcat (s,"CAPTURE ");
+ if (V4L2_CAP_VIDEO_OUTPUT & caps)
+ strcat (s,"OUTPUT ");
+ if (V4L2_CAP_VIDEO_OVERLAY & caps)
+ strcat (s,"OVERLAY ");
+ if (V4L2_CAP_VBI_CAPTURE & caps)
+ strcat (s,"VBI_CAPTURE ");
+ if (V4L2_CAP_VBI_OUTPUT & caps)
+ strcat (s,"VBI_OUTPUT ");
+ if (V4L2_CAP_SLICED_VBI_CAPTURE & caps)
+ strcat (s,"SLICED_VBI_CAPTURE ");
+ if (V4L2_CAP_SLICED_VBI_OUTPUT & caps)
+ strcat (s,"SLICED_VBI_OUTPUT ");
+ if (V4L2_CAP_RDS_CAPTURE & caps)
+ strcat (s,"RDS_CAPTURE ");
+ if (V4L2_CAP_TUNER & caps)
+ strcat (s,"TUNER ");
+ if (V4L2_CAP_AUDIO & caps)
+ strcat (s,"AUDIO ");
+ if (V4L2_CAP_RADIO & caps)
+ strcat (s,"RADIO ");
+ if (V4L2_CAP_READWRITE & caps)
+ strcat (s,"READWRITE ");
+ if (V4L2_CAP_ASYNCIO & caps)
+ strcat (s,"ASYNCIO ");
+ if (V4L2_CAP_STREAMING & caps)
+ strcat (s,"STREAMING ");
+
+ return s;
+}
+/****************************************************************************
Open/Close V4L2 devices
****************************************************************************/
int v4l2_open (char *device, int debug, struct v4l2_driver *drv)
@@ -69,12 +139,14 @@ int v4l2_open (char *device, int debug, struct v4l2_driver *drv)
ret=ioctl(drv->fd,VIDIOC_QUERYCAP,(void *) &drv->cap);
if (ret>=0 && drv->debug) {
printf ("driver=%s, card=%s, bus=%s, version=%d.%d.%d, "
- "capabilities=0x%08x\n",
+ "capabilities=%s\n",
drv->cap.driver,drv->cap.card,drv->cap.bus_info,
(drv->cap.version >> 16) & 0xff,
(drv->cap.version >> 8) & 0xff,
drv->cap.version & 0xff,
- drv->cap.capabilities);
+ prt_caps(drv->cap.capabilities));
+
+
}
return ret;
}
@@ -324,6 +396,7 @@ int v4l2_gettryset_fmt_cap (struct v4l2_driver *drv, enum v4l2_direction dir,
if (ret < 0) {
perror("VIDIOC_S_FMT failed\n");
}
+ drv->sizeimage=pix->sizeimage;
}
if (pix->pixelformat != pixelformat) {
@@ -390,3 +463,100 @@ int v4l2_get_parm (struct v4l2_driver *drv)
return ret;
}
+
+/****************************************************************************
+ Queue Control
+ ****************************************************************************/
+
+int v4l2_mmap_bufs(struct v4l2_driver *drv, unsigned int num_buffers)
+{
+ uint32_t i;
+
+ if (drv->sizeimage==0) {
+ fprintf(stderr,"Image size is zero! Can't proceed\n");
+ return -1;
+ }
+ /* Requests the specified number of buffers */
+ drv->reqbuf.count = num_buffers;
+ drv->reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ drv->reqbuf.memory = V4L2_MEMORY_MMAP;
+
+ if (ioctl(drv->fd,VIDIOC_REQBUFS,&drv->reqbuf)<0) {
+ perror("reqbufs");
+ return errno;
+ }
+
+ if (drv->debug)
+ printf ("REQBUFS: count=%d, type=%s, memory=%s\n",
+ drv->reqbuf.count,
+ prt_names(drv->reqbuf.type,v4l2_type_names),
+ prt_names(drv->reqbuf.memory,v4l2_memory_names));
+
+ /* Frees previous allocations, if required */
+ if (drv->v4l2_bufs)
+ free(drv->v4l2_bufs);
+ if (drv->bufs)
+ free(drv->bufs);
+
+ /* Allocates the required number of buffers */
+ drv->v4l2_bufs=calloc(drv->reqbuf.count, sizeof(drv->v4l2_bufs));
+ drv->bufs=calloc(drv->reqbuf.count, drv->sizeimage);
+
+ for (i = 0; i < drv->reqbuf.count; i++) {
+ struct v4l2_buffer *p=drv->v4l2_bufs[i];
+ struct v4l2_timecode *tc;
+
+ /* Requests kernel buffers to be mmapped */
+ p=calloc(1,sizeof(*p));
+ p->index = i;
+ p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ p->memory = V4L2_MEMORY_MMAP;
+ if (ioctl(drv->fd,VIDIOC_QUERYBUF,p)<0) {
+ int ret=errno;
+ perror("querybuf");
+
+ free(drv->v4l2_bufs);
+ free(drv->bufs);
+
+ drv->v4l2_bufs=NULL;
+ drv->bufs=NULL;
+ return ret;
+ }
+
+ if (drv->debug) {
+ printf ("QUERYBUF: %02ld:%02d:%02d.%08ld index=%d, type=%s, "
+ "bytesused=%d, flags=0x%08x, "
+ "field=%s, sequence=%d, memory=%s, offset/userptr=0x%08lx\n",
+ (p->timestamp.tv_sec/3600),
+ (int)(p->timestamp.tv_sec/60)%60,
+ (int)(p->timestamp.tv_sec%60),
+ p->timestamp.tv_usec,
+ p->index,
+ prt_names(p->type,v4l2_type_names),
+ p->bytesused,p->flags,
+ prt_names(p->field,v4l2_field_names),
+ p->sequence,
+ prt_names(p->memory,v4l2_memory_names),
+ p->m.userptr);
+ tc=&p->timecode;
+ printf ("TIMECODE: %02d:%02d:%02d type=%d, "
+ "flags=0x%08x, frames=%d, userbits=0x%08x\n",
+ tc->hours,tc->minutes,tc->seconds,
+ tc->type, tc->flags, tc->frames, *(uint32_t *) tc->userbits);
+ }
+
+ drv->bufs = mmap(NULL, drv->sizeimage, PROT_READ | PROT_WRITE,
+ MAP_SHARED, drv->fd, p->m.offset);
+ if (MAP_FAILED == drv->bufs) {
+ perror("mmap");
+
+// free(drv->v4l2_bufs);
+// free(drv->bufs);
+
+// drv->v4l2_bufs=NULL;
+// drv->bufs=NULL;
+ return errno;
+ }
+ }
+ return 0;
+}
diff --git a/v4l2-apps/lib/v4l2_driver.h b/v4l2-apps/lib/v4l2_driver.h
index aac00834f..f429f01cd 100644
--- a/v4l2-apps/lib/v4l2_driver.h
+++ b/v4l2-apps/lib/v4l2_driver.h
@@ -22,15 +22,25 @@ struct drv_list {
};
struct v4l2_driver {
- int fd; /* Driver descriptor */
+ int fd; /* Driver descriptor */
- int debug;
+ int debug;
- struct v4l2_capability cap;
+ /* V4L2 structs */
+ struct v4l2_capability cap;
+ struct v4l2_streamparm parm;
- struct v4l2_streamparm parm;
+ /* Several lists to be used to store enumbered values */
+ struct drv_list *stds,*inputs,*fmt_caps;
- struct drv_list *stds,*inputs,*fmt_caps;
+ /* Stream control */
+ struct v4l2_requestbuffers reqbuf;
+ struct v4l2_buffer **v4l2_bufs;
+ uint8_t **bufs;
+ uint32_t sizeimage;
+
+ /* Queue control */
+ uint32_t waitq, currq;
};
enum v4l2_direction {
@@ -53,4 +63,5 @@ int v4l2_setget_input (struct v4l2_driver *drv, enum v4l2_direction dir, struct
int v4l2_gettryset_fmt_cap (struct v4l2_driver *drv, enum v4l2_direction dir,
struct v4l2_format *fmt,uint32_t width, uint32_t height,
uint32_t pixelformat, enum v4l2_field field);
+int v4l2_mmap_bufs(struct v4l2_driver *drv, unsigned int num_buffers);