Veethree D7 SDK
Loading...
Searching...
No Matches
Video Examples

Details about video inputs

More information about the analog video inputs can be found at Video inputs on the D7 Platform,

Using GStreamer and QML

This example is adapted from GStreamer's at https://gitlab.freedesktop.org/gstreamer/gstreamer/-/tree/main/subprojects/gst-plugins-good/tests/examples/qt6/qmlsink?ref_type=heads, and as such its original copyright notice has been kept.

The differences are:

  • using CMake instead of qmake
  • using QQuickWindow::NoStage instead of QQuickWindow::BeforeSynchronizingStage for starting the video stream
  • playing back the video input rather than the test pattern

It uses the gstreamer 'good' plugin for qml6glsink.

Note

The application can take either --pal or --ntsc as an optional command line argument. It defaults to --pal if none are specified.

This will set the expected video standard on the video device /dev/video2 and configure the GStreamer pipeline accordingly.

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
gstreamer-qmlsink
VERSION 0.1
LANGUAGES CXX)
# Project name
set(APPNAME gstreamer-qmlsink)
# Find QT and gstreamer packages
find_package(Qt6 REQUIRED COMPONENTS Widgets Quick)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0 IMPORTED_TARGET)
pkg_check_modules(GSTREAMER_VIDEO REQUIRED gstreamer-video-1.0 IMPORTED_TARGET)
add_compile_definitions(GST_USE_UNSTABLE_API)
qt_standard_project_setup(REQUIRES 6.8)
qt_add_executable(${APPNAME} main.cpp)
qt_add_qml_module(
${APPNAME}
URI
gstreamer_qmlsink
VERSION
1.0
QML_FILES
main.qml
)
target_link_libraries(${APPNAME} PRIVATE Qt6::Widgets Qt6::Quick PkgConfig::GSTREAMER PkgConfig::GSTREAMER_VIDEO)
# Needed because on the host, there won't be the same video device as on the target
if (UNIX AND CMAKE_CROSSCOMPILING)
target_compile_definitions(${APPNAME} PUBLIC TARGET_BUILD=1)
endif()
include(GNUInstallDirs)
install(TARGETS ${APPNAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
main.cpp
// SPDX-License-Identifier: BSD-2-Clause
//
// Copyright (C) 2022, Matthew Waters <matthew@centricular.com>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// a) Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// b) Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQuickItem>
#include <QRunnable>
#include <QString>
#include <gst/gst.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <error.h>
#ifdef TARGET_BUILD
#define VIDEO_DEVICE "/dev/video2"
#define VIDEO_SOURCE "v4l2src"
#define VIDEO_FORMAT "NV12"
constexpr auto QQUICKWINDOW_STAGE = QQuickWindow::NoStage;
#else
#define VIDEO_SOURCE "videotestsrc"
#define VIDEO_FORMAT "YV12"
constexpr auto QQUICKWINDOW_STAGE = QQuickWindow::BeforeSynchronizingStage;
#endif
class SetPlaying : public QRunnable
{
public:
SetPlaying(GstElement *);
~SetPlaying();
void run ();
private:
GstElement * pipeline_;
};
SetPlaying::SetPlaying (GstElement * pipeline)
{
this->pipeline_ = pipeline ? static_cast<GstElement *> (gst_object_ref (pipeline)) : NULL;
}
SetPlaying::~SetPlaying ()
{
if (this->pipeline_)
gst_object_unref (this->pipeline_);
}
void
SetPlaying::run ()
{
if (this->pipeline_)
gst_element_set_state (this->pipeline_, GST_STATE_PLAYING);
}
#ifdef TARGET_BUILD
bool
setStandard(const v4l2_std_id &standard)
{
int fd = open(VIDEO_DEVICE, O_RDWR | O_CLOEXEC);
if (fd < 0) {
perror("failed to open " VIDEO_DEVICE);
return false;
}
if (ioctl(fd, VIDIOC_S_STD, &standard) != 0) {
close(fd);
perror("failed to set video standard");
return false;
}
close(fd);
return true;
}
#endif
int main(int argc, char *argv[])
{
int ret;
gst_init (&argc, &argv);
{
QGuiApplication app(argc, argv);
const char* palResolution = "width=720,height=576,framerate=25/1";
const char* ntscResolution = "width=720,height=480,framerate=30000/1001";
// Default to PAL
const char* videoResolution = palResolution;
[[maybe_unused]] v4l2_std_id videoStandard = V4L2_STD_PAL;
if (argc >= 2 && QString(argv[1]) == "--ntsc")
{
videoResolution = ntscResolution;
videoStandard = V4L2_STD_NTSC;
}
#ifdef TARGET_BUILD
if (!setStandard(videoStandard))
{
return 1;
}
#endif
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
GstElement *pipeline = gst_pipeline_new (NULL);
GstElement *src = gst_element_factory_make (VIDEO_SOURCE, NULL);
GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL);
GstCaps *caps = gst_caps_from_string (QString("video/x-raw,format=%1,%2")
.arg(VIDEO_FORMAT)
.arg(videoResolution)
.toStdString().c_str());
g_object_set (capsfilter, "caps", caps, NULL);
gst_clear_caps (&caps);
GstElement *glupload = gst_element_factory_make ("glupload", NULL);
/* Example for cropping the input to hide the black bars and half lines from analog input
*/
GstElement *crop = gst_element_factory_make ("videocrop", NULL);
g_object_set(crop, "left", 20, "right", 20, "top", 20, "bottom", 20, NULL);
/* the plugin must be loaded before loading the qml file to register the
* GstGLVideoItem qml item */
GstElement *sink = gst_element_factory_make ("qml6glsink", NULL);
g_assert (src && glupload && crop && sink);
#ifdef TARGET_BUILD
g_object_set(src, "device", VIDEO_DEVICE, NULL);
#endif
gst_bin_add_many (GST_BIN (pipeline), src, capsfilter, crop, glupload, sink, NULL);
gst_element_link_many (src, capsfilter, crop, glupload, sink, NULL);
QQmlApplicationEngine engine;
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreationFailed, &app,
[]() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/gstreamer_qmlsink/main.qml")));
QQuickItem *videoItem;
QQuickWindow *rootObject;
/* find and set the videoItem on the sink */
rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
g_assert (videoItem);
g_object_set(sink, "widget", videoItem, NULL);
rootObject->scheduleRenderJob (new SetPlaying (pipeline),
QQUICKWINDOW_STAGE);
ret = app.exec();
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
}
gst_deinit ();
return ret;
}
int open(const std::string &path, int flags)
Wrapper implementation that deals with C++ linter (clang-tidy)
int ioctl(Args &&... args)
Wrapper implementation that deals with C++ linter (clang-tidy)
Definition syscall_wrappers.h:86
@ close
close operation failed
Definition io_error_codes.h:44
main.qml
// SPDX-License-Identifier: BSD-2-Clause
//
// Copyright (C) 2022, Matthew Waters <matthew@centricular.com>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// a) Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// b) Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
import QtQuick 6.0
import QtQuick.Controls 6.0
import QtQuick.Dialogs 6.0
import QtQuick.Window 6.0
import org.freedesktop.gstreamer.Qt6GLVideoItem 1.0
ApplicationWindow {
id: window
visible: true
width: 1280
height: 768
color: "black"
Item {
anchors.fill: parent
GstGLQt6VideoItem {
id: video
objectName: "videoItem"
anchors.centerIn: parent
width: parent.width
height: parent.height
}
}
}

Capture a video frame using V4L2

v4lcap.c originally from the NXP Community has been modified to support the ADV7280A-M Video Decoder in the D7.

On the D7, run:

# v4l2-ctl -d /dev/video2 --set-standard NTSC
# v4l2-ctl -d /dev/video2 --set-input 0
# ./v4lcap
video node: /dev/video2
frame count: 1
frame width: 720
frame height: 480
plane bytesused: 691200
Raw capture completed frame-1.raw

Copy the raw frame to the PC. Then using e.g. ImageMagick, convert from YUYV to png:

# dd if=frame-1.raw conv=swab | convert -sampling-factor 4:2:2 -size 720x480 -depth 8 uyvy:- frame-1.png
# open frame-1.png

As ImageMagick supports UYVY, rather than YUYV, dd conv=swab swaps each pair of bytes.

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(v4lcap)
add_executable(v4lcap)
target_sources(v4lcap
PRIVATE
v4lcap.c
)
v4lcap.c
/*
* V4L2 video capture example
*
* This program can be used and distributed without restrictions.
*
* This program is provided with the V4L2 API
* see http://linuxtv.org/docs.php for more information
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h> /* getopt_long() */
#include <errno.h>
#include <fcntl.h> /* low-level i/o */
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/videodev2.h>
#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define NUM_PLANES 1
enum vb2_memory {
VB2_MEMORY_UNKNOWN = 0,
VB2_MEMORY_MMAP = 1,
};
struct buffer {
void *start[NUM_PLANES];
size_t length[NUM_PLANES];
};
static char dev_name[20];
static char convertcmd[100];
static int fd = -1;
struct buffer *buffers;
static unsigned int n_buffers;
static int out_buf;
static int frame_count = 1;
static int frame_number = 0;
static int img_width = 720;
static int img_height = 480;
static int status;
static int xioctl(int fh, int request, void *arg)
{
int r;
int i = 0;
do {
r = ioctl(fh, request, arg);
} while (-1 == r && EINTR == errno);
return r;
}
static void process_image(const void *p, int size)
{
char filename[15];
frame_number++;
sprintf(filename, "frame-%d.raw", frame_number);
FILE *fp = fopen(filename, "wb");
fwrite(p, size, 1, fp);
fflush(fp);
fclose(fp);
}
static int read_frame(void)
{
struct v4l2_buffer buf;
struct v4l2_plane mplanes[1];
unsigned int i;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = VB2_MEMORY_MMAP;
buf.m.planes = mplanes;
buf.length = NUM_PLANES;
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
case EAGAIN:
return 0;
default:
fprintf(stderr, "VIDIOC_DQBUF: error - %d\n", errno);
exit(EXIT_FAILURE);
}
}
assert(buf.index < n_buffers);
printf("plane bytesused: %u\n", buf.m.planes->bytesused);
process_image(buffers[buf.index].start[0], buf.m.planes->bytesused);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) {
fprintf(stderr, "VIDIOC_QBUF error: %d\n", errno);
exit(EXIT_FAILURE);
}
return 1;
}
static void stop_capturing(void)
{
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type)) {
fprintf(stderr, "VIDIOC_STREAMOFF: error - %d\n", errno);
exit(EXIT_FAILURE);
}
}
static void mainloop(void)
{
unsigned int count;
count = frame_count;
while (count-- > 0) {
for (;;) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 4;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r) {
if (EINTR == errno)
continue;
fprintf(stderr, "select error\n");
exit(EXIT_FAILURE);
}
if (0 == r) {
fprintf(stderr, "select timeout\n");
stop_capturing();
exit(EXIT_FAILURE);
}
if (read_frame())
break;
/* EAGAIN - continue select loop. */
}
}
}
static void start_capturing(void)
{
unsigned int i;
enum v4l2_buf_type type;
for (i = 0; i < n_buffers; ++i) {
struct v4l2_buffer buf;
struct v4l2_plane mplanes[1];
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = VB2_MEMORY_MMAP;
buf.index = i;
buf.m.planes = mplanes;
buf.length = NUM_PLANES;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) {
fprintf(stderr, "VIDIOC_QBUF error: %d\n", errno);
exit(EXIT_FAILURE);
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) {
fprintf(stderr, "VIDIOC_STREAMON error: %d\n", errno);
exit(EXIT_FAILURE);
}
}
static void uninit_device(void)
{
unsigned int i, j;
for (i = 0; i < n_buffers; ++i)
for (j = 0; j < NUM_PLANES; j++)
munmap(buffers[i].start[j], buffers[i].length[j]);
free(buffers);
}
static void init_mmap(void)
{
struct v4l2_requestbuffers req;
CLEAR(req);
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
req.memory = VB2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
fprintf(stderr, "VIDIOC_REQBUFS: error - %d\n", errno);
exit(EXIT_FAILURE);
}
if (req.count < 2) {
fprintf(stderr, "Insufficient buffer memory on %s\n", dev_name);
exit(EXIT_FAILURE);
}
buffers = calloc(req.count, sizeof(*buffers));
if (!buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
struct v4l2_buffer buf;
struct v4l2_plane mplanes[NUM_PLANES];
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = VB2_MEMORY_MMAP;
buf.index = n_buffers;
buf.m.planes = mplanes;
buf.length = NUM_PLANES;
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf)) {
fprintf(stderr, "VIDIOC_QUERYBUF: error - %d\n", errno);
exit(EXIT_FAILURE);
}
for (int j = 0; j < NUM_PLANES; j++) {
buffers[n_buffers].length[j] = buf.m.planes[j].length;
buffers[n_buffers].start[j] = mmap(NULL,
buf.m.planes[j].length,
PROT_READ | PROT_WRITE, /* recommended */
MAP_SHARED, /* recommended */
fd,
buf.m.planes[j].m.mem_offset);
if (MAP_FAILED == buffers[n_buffers].start[j]) {
fprintf(stderr, "mmap: error - %d\n", errno);
exit(EXIT_FAILURE);
}
}
}
}
static void init_device(void)
{
struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
struct v4l2_streamparm parm;
unsigned int min;
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
fprintf(stderr, "VIDIOC_QUERYCAP: error - %d\n", errno);
exit(EXIT_FAILURE);
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) {
fprintf(stderr, "%s is no video capture device\n", dev_name);
exit(EXIT_FAILURE);
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
fprintf(stderr, "%s does not support streaming i/o\n", dev_name);
exit(EXIT_FAILURE);
}
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
fmt.fmt.pix_mp.width = img_width; //replace
fmt.fmt.pix_mp.height = img_height; //replace
fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix_mp.field = V4L2_FIELD_NONE;
fmt.fmt.pix_mp.num_planes = 1;
fmt.fmt.pix_mp.plane_fmt[0].bytesperline = img_width * 2;
fmt.fmt.pix_mp.plane_fmt[0].sizeimage = img_width * 2 * img_height;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) {
fprintf(stderr, "VIDIOC_S_FMT: error: %d\n", errno);
exit(EXIT_FAILURE);
}
init_mmap();
}
static void close_device(void)
{
if (-1 == close(fd)) {
fprintf(stderr, "Failed to close device\n");
exit(EXIT_FAILURE);
}
fd = -1;
}
static void open_device(void)
{
fd = open(dev_name, O_RDWR /* required */ /* | O_NONBLOCK*/, 0);
if (fd == -1) {
fprintf(stderr, "Cannot open '%s': %d\n", dev_name, errno);
exit(EXIT_FAILURE);
}
}
static void usage(FILE *fp, int argc, char **argv)
{
fprintf(fp,
"Usage: %s [options]\n\n"
"Options:\n"
"-d | --device name Video device name [%s]\n"
"-h | --help Print this message\n"
"-c | --count Number of frames to grab [%i]\n"
"-w | --width Frame width\n"
"-v | --height Frame height\n"
"",
argv[0],
dev_name,
frame_count);
}
int main(int argc, char **argv)
{
int opt;
strcpy(dev_name, "/dev/video2");
while ((opt = getopt(argc, argv, ":d:c:w:hv:")) != -1) {
switch (opt) {
case 'h':
usage(stdout, argc, argv);
exit(0);
case 'd':
strcpy(dev_name, optarg);
break;
case 'o':
out_buf++;
break;
case 'c':
frame_count = atoi(optarg);
break;
case 'w':
img_width = atoi(optarg);
break;
case 'v':
img_height = atoi(optarg);
break;
case '?':
printf("unknown option: %c\n", optopt);
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
break;
}
}
printf("\nvideo node: %s\n", dev_name);
printf("frame count: %d\n", frame_count);
printf("frame width: %d\n", img_width);
printf("frame height: %d\n\n", img_height);
open_device();
init_device();
start_capturing();
mainloop();
stop_capturing();
uninit_device();
close_device();
printf("\nRaw capture completed frame-%d.raw \n", frame_count);
fprintf(stderr, "\n");
return 0;
}