v4l2-compliance: add v4l2_format support to cv4l-helpers.h
authorHans Verkuil <hans.verkuil@cisco.com>
Wed, 16 Jul 2014 07:07:18 +0000 (09:07 +0200)
committerHans Verkuil <hans.verkuil@cisco.com>
Wed, 16 Jul 2014 07:07:18 +0000 (09:07 +0200)
And use it in v4l2-compliance. This should simplify format handling
in the code.

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
utils/v4l2-compliance/cv4l-helpers.h
utils/v4l2-compliance/v4l-helpers.h
utils/v4l2-compliance/v4l2-test-buffers.cpp

index e2729a6..9fb9370 100644 (file)
@@ -3,6 +3,139 @@
 
 #include <v4l-helpers.h>
 
+class cv4l_fmt : public v4l2_format {
+public:
+       cv4l_fmt()
+       {
+               fd = NULL;
+               type = 0;
+               memset(&fmt, 0, sizeof(fmt));
+       }
+       cv4l_fmt(v4l_fd *_fd, unsigned _type = 0)
+       {
+               fd = _fd;
+               memset(&fmt, 0, sizeof(fmt));
+               if (_type == 0)
+                       type = v4l_buf_type_g_vid_cap(fd);
+               else
+                       type = _type;
+       }
+       cv4l_fmt(v4l_fd *_fd, const v4l2_format &_fmt)
+       {
+               fd = _fd;
+               type = _fmt.type;
+               fmt = _fmt.fmt;
+       }
+       cv4l_fmt(const cv4l_fmt &_fmt)
+       {
+               fd = _fmt.fd;
+               type = _fmt.type;
+               fmt = _fmt.fmt;
+       }
+       void init(v4l_fd *_fd, unsigned _type = 0)
+       {
+               fd = _fd;
+               memset(&fmt, 0, sizeof(fmt));
+               if (_type == 0)
+                       type = v4l_buf_type_g_vid_cap(fd);
+               else
+                       type = _type;
+       }
+       void init(v4l_fd *_fd, const v4l2_format &_fmt)
+       {
+               fd = _fd;
+               type = _fmt.type;
+               fmt = _fmt.fmt;
+       }
+       int g_fmt(unsigned _type = 0)
+       {
+               return v4l_g_fmt(fd, this, _type ? _type : type);
+       }
+       int try_fmt()
+       {
+               return v4l_try_fmt(fd, this);
+       }
+       int s_fmt()
+       {
+               return v4l_s_fmt(fd, this);
+       }
+       void s_width(__u32 width)
+       {
+               v4l_format_s_width(this, width);
+       }
+       __u32 g_width()
+       {
+               return v4l_format_g_width(this);
+       }
+       void s_height(__u32 height)
+       {
+               v4l_format_s_height(this, height);
+       }
+       __u32 g_height()
+       {
+               return v4l_format_g_height(this);
+       }
+       void s_pixelformat(__u32 pixelformat)
+       {
+               v4l_format_s_pixelformat(this, pixelformat);
+       }
+       __u32 g_pixelformat()
+       {
+               return v4l_format_g_pixelformat(this);
+       }
+       void s_field(unsigned field)
+       {
+               v4l_format_s_field(this, field);
+       }
+       unsigned g_field()
+       {
+               return v4l_format_g_field(this);
+       }
+       unsigned g_first_field(v4l2_std_id std)
+       {
+               return v4l_format_g_first_field(this, std);
+       }
+       unsigned g_flds_per_frm()
+       {
+               return v4l_format_g_flds_per_frm(this);
+       }
+       void s_colorspace(unsigned colorspace)
+       {
+               v4l_format_s_colorspace(this, colorspace);
+       }
+       unsigned g_colorspace()
+       {
+               return v4l_format_g_colorspace(this);
+       }
+       void s_num_planes(__u8 num_planes)
+       {
+               v4l_format_s_num_planes(this, num_planes);
+       }
+       __u8 g_num_planes()
+       {
+               return v4l_format_g_num_planes(this);
+       }
+       void s_bytesperline(unsigned plane, __u32 bytesperline)
+       {
+               v4l_format_s_bytesperline(this, plane, bytesperline);
+       }
+       __u32 g_bytesperline(unsigned plane)
+       {
+               return v4l_format_g_bytesperline(this, plane);
+       }
+       void s_sizeimage(unsigned plane, __u32 sizeimage)
+       {
+               v4l_format_s_sizeimage(this, plane, sizeimage);
+       }
+       __u32 g_sizeimage(unsigned plane)
+       {
+               return v4l_format_g_sizeimage(this, plane);
+       }
+
+protected:
+       v4l_fd *fd;
+};
+
 class cv4l_buffer;
 
 class cv4l_queue : v4l_queue {
index 6eb0989..66b87f3 100644 (file)
@@ -430,7 +430,29 @@ static inline unsigned v4l_format_g_field(const struct v4l2_format *fmt)
        }
 }
 
-static inline void v4l_format_s_pix_colorspace(struct v4l2_format *fmt,
+static inline unsigned v4l_format_g_first_field(const struct v4l2_format *fmt,
+                                               v4l2_std_id std)
+{
+       unsigned field = v4l_format_g_field(fmt);
+
+       if (field != V4L2_FIELD_ALTERNATE)
+               return field;
+       if (std & V4L2_STD_525_60)
+               return V4L2_FIELD_BOTTOM;
+       return V4L2_FIELD_TOP;
+}
+
+static inline unsigned v4l_format_g_flds_per_frm(const struct v4l2_format *fmt)
+{
+       unsigned field = v4l_format_g_field(fmt);
+
+       if (field == V4L2_FIELD_ALTERNATE ||
+           field == V4L2_FIELD_TOP || field == V4L2_FIELD_BOTTOM)
+               return 2;
+       return 1;
+}
+
+static inline void v4l_format_s_colorspace(struct v4l2_format *fmt,
                                               unsigned colorspace)
 {
        switch (fmt->type) {
@@ -446,7 +468,7 @@ static inline void v4l_format_s_pix_colorspace(struct v4l2_format *fmt,
 }
 
 static inline unsigned
-v4l_format_g_pix_colorspace(const struct v4l2_format *fmt)
+v4l_format_g_colorspace(const struct v4l2_format *fmt)
 {
        switch (fmt->type) {
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
index e7afe66..d4fbda7 100644 (file)
@@ -33,7 +33,7 @@
 #include <map>
 #include "v4l2-compliance.h"
 
-static struct v4l2_format cur_fmt;
+static struct cv4l_fmt cur_fmt;
 
 static const unsigned valid_output_flags =
        V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK |
@@ -307,7 +307,7 @@ int buffer::check(unsigned type, unsigned memory, unsigned index,
                if (is_video()) {
                        fail_on_test(g_field() == V4L2_FIELD_ALTERNATE);
                        fail_on_test(g_field() == V4L2_FIELD_ANY);
-                       if (v4l_format_g_field(&cur_fmt) == V4L2_FIELD_ALTERNATE) {
+                       if (cur_fmt.g_field() == V4L2_FIELD_ALTERNATE) {
                                fail_on_test(g_field() != V4L2_FIELD_BOTTOM &&
                                                g_field() != V4L2_FIELD_TOP);
                                fail_on_test(g_field() == seq.last_field);
@@ -317,7 +317,7 @@ int buffer::check(unsigned type, unsigned memory, unsigned index,
                                else
                                        fail_on_test((int)g_sequence() != seq.last_seq + 1);
                        } else {
-                               fail_on_test(g_field() != v4l_format_g_field(&cur_fmt));
+                               fail_on_test(g_field() != cur_fmt.g_field());
                                fail_on_test((int)g_sequence() != seq.last_seq + 1);
                        }
                } else {
@@ -331,7 +331,6 @@ int buffer::check(unsigned type, unsigned memory, unsigned index,
                        fail_on_test(!g_timestamp().tv_sec && !g_timestamp().tv_usec);
                } else {
                        fail_on_test(g_timestamp().tv_sec || g_timestamp().tv_usec);
-                       fail_on_test(g_flags() & V4L2_BUF_FLAG_TIMECODE);
                }
                if (!is_output() || mode == Unqueued)
                        fail_on_test(frame_types);
@@ -706,10 +705,10 @@ static int setupM2M(struct node *node, queue &q)
                fail_on_test(buf.qbuf(q));
        }
        if (q.is_video()) {
-               v4l2_format fmt;
+               cv4l_fmt fmt(&node->vfd, q.g_type());
 
-               v4l_g_fmt(&node->vfd, &fmt, q.g_type());
-               last_m2m_seq.last_field = v4l_format_g_field(&fmt);
+               fmt.g_fmt();
+               last_m2m_seq.last_field = fmt.g_field();
        }
        fail_on_test(q.streamon());
        return 0;
@@ -864,26 +863,27 @@ int testMmap(struct node *node, unsigned frame_count)
                // Good check for whether all the internal vb2 calls are in
                // balance.
                fail_on_test(q.reqbufs(q.g_buffers()));
-               v4l_g_fmt(&node->vfd, &cur_fmt, q.g_type());
+               cur_fmt.init(&node->vfd, q.g_type());
+               cur_fmt.g_fmt();
 
                ret = q.create_bufs(0);
                fail_on_test(ret != ENOTTY && ret != 0);
                if (ret == ENOTTY)
                        have_createbufs = false;
                if (have_createbufs) {
-                       v4l2_format fmt = cur_fmt;
+                       cv4l_fmt fmt(cur_fmt);
 
                        if (node->is_video) {
-                               last_seq.last_field = v4l_format_g_field(&cur_fmt);
-                               v4l_format_s_height(&fmt, v4l_format_g_height(&fmt) / 2);
-                               for (unsigned p = 0; p < v4l_format_g_num_planes(&fmt); p++)
-                                       v4l_format_s_sizeimage(&fmt, p, v4l_format_g_sizeimage(&fmt, p) / 2);
+                               last_seq.last_field = cur_fmt.g_field();
+                               fmt.s_height(fmt.g_height() / 2);
+                               for (unsigned p = 0; p < fmt.g_num_planes(); p++)
+                                       fmt.s_sizeimage(p, fmt.g_sizeimage(p) / 2);
                                ret = q.create_bufs(1, &fmt);
                                fail_on_test(ret != EINVAL);
                                fail_on_test(testQueryBuf(node, cur_fmt.type, q.g_buffers()));
                                fmt = cur_fmt;
-                               for (unsigned p = 0; p < v4l_format_g_num_planes(&fmt); p++)
-                                       v4l_format_s_sizeimage(&fmt, p, v4l_format_g_sizeimage(&fmt, p) * 2);
+                               for (unsigned p = 0; p < fmt.g_num_planes(); p++)
+                                       fmt.s_sizeimage(p, fmt.g_sizeimage(p) * 2);
                        }
                        fail_on_test(q.create_bufs(1, &fmt));
                }
@@ -994,7 +994,7 @@ int testUserPtr(struct node *node, unsigned frame_count)
                fail_on_test(q.streamoff());
                last_seq.init();
                if (node->is_video)
-                       last_seq.last_field = v4l_format_g_field(&cur_fmt);
+                       last_seq.last_field = cur_fmt.g_field();
 
                fail_on_test(setupUserPtr(node, q));
 
@@ -1099,7 +1099,7 @@ int testDmaBuf(struct node *expbuf_node, struct node *node, unsigned frame_count
                fail_on_test(q.streamoff());
                last_seq.init();
                if (node->is_video)
-                       last_seq.last_field = v4l_format_g_field(&cur_fmt);
+                       last_seq.last_field = cur_fmt.g_field();
 
                fail_on_test(setupDmaBuf(expbuf_node, node, q, exp_q));