mirror of
git://nv-tegra.nvidia.com/tegra/v4l2-src/v4l2_libs.git
synced 2025-12-22 09:21:28 +03:00
f07563fa1628a75c578152387638b1f217f730be - libv4lconvert/Makefile 6ac3c887c56c7d4345499cb5b7bfac9579a57d16 - libv4lconvert/cpia1.c 2d385802ec3690e78a961ce95431e98eb3f5fc6a - libv4lconvert/rgbyuv.c 43843cbdb81f3efee5873f5ed25ae1eb7745a9e5 - libv4lconvert/libv4lconvert-priv.h 002783c808dc565478c18bd27373792028264bb6 - libv4lconvert/ov518-decomp.c 4d49e6e897046e66e41c3563fec2eb2562abf813 - libv4lconvert/jpeg_memsrcdest.c a38fdb4c79518e972678745c93cd62881d72972b - libv4lconvert/Makefile.dGPU e9f783fb933606224252dfd3093afa36490f6f4c - libv4lconvert/Makefile.in 52900601a24ee9ffea87ca7fe6651e912e20235e - libv4lconvert/libv4lsyscall-priv.h d37f854500cbd759ad93ae89c6aa75c00b000931 - libv4lconvert/bayer.c 2cd1b994bb7999d0c08c19b1a2efd11707250852 - libv4lconvert/helper.c adac14b8826d05ddab33d595a803e92eaf84b797 - libv4lconvert/Makefile.am 6e806290f6e426c881322d362f189fa7b9e2f917 - libv4lconvert/sn9c10x.c ba44b6ac00a643e6df484f3172022e1b2628c0cc - libv4lconvert/jpgl.c fa06343de31dcf6d3d5862cc278396272fa838c4 - libv4lconvert/se401.c 4bd081bf25b63f5463d582ef36f77ba5e3711d0d - libv4lconvert/sq905c.c 940250beb5898bf254e7cd1ef9d7950a8db10af6 - libv4lconvert/mr97310a.c 5e177c3a7c157c25c4afcae031e79830237510f6 - libv4lconvert/libv4lconvert.c 1f362080c319355070cf409cfccf28d8c68d024e - libv4lconvert/sn9c2028-decomp.c 703ce22dc98d834490c7aa23ae73b9113e386221 - libv4lconvert/sn9c20x.c 018e503969b2e06a0ea639fc08f03a7476cc15b5 - libv4lconvert/jpeg_memsrcdest.h fb3344cfa8df97688332ee4fd3b17968437e8ad5 - libv4lconvert/helper-funcs.h d378cbdd377027e7528af47362a94ccb1ae15da3 - libv4lconvert/libv4lconvert.export 61710972d4e9bfcd00490c563d1bb5bde5480c9e - libv4lconvert/tinyjpeg.c 231571db9f8caa6fdc68138102029127a6b93812 - libv4lconvert/crop.c d62448a06539ecf6e3daae7faa299b6da8878054 - libv4lconvert/pac207.c 72b65c9f0f3c87dcae86a2afbea65a76f2c6bafd - libv4lconvert/tinyjpeg-internal.h a016f896e0e9f0cd8af7447b52651ed14c5a5b29 - libv4lconvert/jpeg.c 608a4c341a5df9daea647fbddc415cdaf27b0482 - libv4lconvert/spca561-decompress.c b03b32eae024bf59b6ceaf6b6d342383cd0b0673 - libv4lconvert/jl2005bcd.c 803c4d0b9364050eda163452b8792e62e221ab6d - libv4lconvert/tinyjpeg.h 2df34af8f9d747fb0e5c7c71954c788aff1c483f - libv4lconvert/stv0680.c 3e8e6c1fb85e3c4b58c4e9b2b0a223ddc793edcb - libv4lconvert/libv4lconvert.pc.in 02aa2e7d12b72fe4ce79cdf4009da61f35091e94 - libv4lconvert/flip.c 05038ef9efab175322a12d3d6620dba6298aa2a9 - libv4lconvert/spca501.c b2c19c2eac71d39d3fb883cdc159a69c2afa8fd6 - libv4lconvert/ov511-decomp.c 1d9c446cd8a232da87bd79acebc93e018ec72499 - libv4lconvert/jidctflt.c ed215eca170d07a6838be3796aad933adf89ece2 - libv4lconvert/hm12.c 193e9d2c997e21ce8ca02f25050fb8d2d62b6bfd - libv4lconvert/processing/autogain.c c894d4a9b9e0f95be9e7bade4f7f734ae0c6996c - libv4lconvert/processing/libv4lprocessing.c 4158e9c3d21cf0a064b99bdf8571df4f3ba6d5bb - libv4lconvert/processing/whitebalance.c be34baf0d2ce1374c841494ba27ccc352c444e98 - libv4lconvert/processing/gamma.c b48edcb4036ee5f4e77e9cb49dd1b52b1e7f2427 - libv4lconvert/processing/libv4lprocessing.h 967d27c0e09849338a69dc4b5647d1824f2cd2fb - libv4lconvert/processing/libv4lprocessing-priv.h 9f382ff1f2895b926f3596e0a8ae7637b5e6a2ac - libv4lconvert/control/libv4lcontrol.c 8c523bd3838004a4fa00430a38cbc6dbaa47286f - libv4lconvert/control/libv4lcontrol.h 4ce5e891af7857a3504d1350ef13015997dde62a - libv4lconvert/control/libv4lcontrol-priv.h bda0c1296aad2b3b178a76f38e26c20ce3b81233 - include/libv4l1.h a95818658d95c1602f2422e5f1fb54b9ca70536e - include/libv4l2rds.h 2d1932763a064ef1176450b1bb4dd242aa3c77bb - include/libv4lconvert.h 3517bc15e30852ad01ad9966ec7111a16a2263df - include/libv4l2.h 944cd9bc32420e73da3a7d3cb1d0ce707263c9f0 - include/libv4l1-videodev.h 7d9d199f4e6f4ba6f0d669ee5decd74355507137 - include/libv4l-plugin.h cfeaf0c16571e0c64bce50bcb8a6cd8d8c976187 - include/config.h d7d4fd219e36edadc17837bff78eb8323e2a115e - include/libdvbv5/libdvb-version.h 33071176eda60242f39927e572ac6b1c1bab32e2 - include/libdvbv5/desc_event_extended.h 29c2cd2554e4f4285ab8709e6eace38046d8ecdc - include/libdvbv5/desc_t2_delivery.h b42819db457bc6a23324faf259c0db1104741d99 - include/libdvbv5/dvb-scan.h bae10c2fa28bb522e9fc0bb5e4b0128ab6f90e65 - include/libdvbv5/desc_partial_reception.h f797ddac8e1adc6b29750f076df0f3a23cf630eb - include/libdvbv5/dvb-v5-std.h 48079dcae511070a8eeb55b004bd8ac06735ecc7 - include/libdvbv5/desc_ca.h f4eb8b615b7c5a245c76e3f448e6cd848d8cf765 - include/libdvbv5/desc_cable_delivery.h 170a50b570691975e0ff22faffeb0131fc47c7c8 - include/libdvbv5/desc_logical_channel.h c8f11e89456fc078c607d424e887babe72cd3684 - include/libdvbv5/nit.h d39a24d0d0a0fbe732881dd2734b10d842d88de9 - include/libdvbv5/header.h badc03677ddbcfa1d1188ddaaa4c272fccfe4a21 - include/libdvbv5/desc_atsc_service_location.h d3bad046037cad4a95778555021a1a1a6b3040d9 - include/libdvbv5/cat.h a614739513f62affb1059df81c064203d9fe2fcd - include/libdvbv5/desc_ca_identifier.h 6d70f699eb912c974dc06a19f9bdcb7dd0c27932 - include/libdvbv5/desc_service.h 0eda1161b34ca39b8528bc53d05fb0fc7d457f80 - include/libdvbv5/desc_frequency_list.h 22f6b663d50de467539201ecfdb363e9252c88bd - include/libdvbv5/atsc_eit.h 1c1292f060664abf6c12273a41b2bf6f704dd19b - include/libdvbv5/desc_sat.h 22de6de5e65530aa1ab38a2373db128e81983a4c - include/libdvbv5/desc_network_name.h 91178a5945e63c81051b57b7b2842139947a3b39 - include/libdvbv5/desc_extension.h 9ca776ad159cf84b2f68101bf279472741829047 - include/libdvbv5/descriptors.h fa7e8d5e996310e1dd47d5467780d533c8a93ad8 - include/libdvbv5/dvb-dev.h fed6115825c5b23e1e31ff80aab997f0a7b6c3fc - include/libdvbv5/sdt.h 4b892acd2ce77c060b50cf67f5faf9dc9b76edbb - include/libdvbv5/dvb-frontend.h 8b9f9e00f7bcdce58aef1d8d7234567af82e49b8 - include/libdvbv5/dvb-file.h b8a077b8e3c6e337223fc37685d217a81df112f1 - include/libdvbv5/dvb-demux.h 01a0afbe1a4f41cf58f9ce47d65b31e3df7c8680 - include/libdvbv5/pat.h 4a49c8b46947be16356d6355aa36d72d7baa946c - include/libdvbv5/vct.h c83200debc98ff8fa82030d36371a2caa230281a - include/libdvbv5/desc_language.h 21cea15c5d4faff2726f18bdf5b83e2210d472c4 - include/libdvbv5/mpeg_pes.h 825f75350da9fb148acc7d36af513da977105169 - include/libdvbv5/desc_isdbt_delivery.h 8f60bf43907b1eac3c1c2f031cf68a9bdebb3662 - include/libdvbv5/desc_ts_info.h 89cbb75f3e0952e4dfb145fdea776b45e05b7e76 - include/libdvbv5/desc_hierarchy.h 5c80a7ee23d1da2c91f60e79f7292248694b4f65 - include/libdvbv5/crc32.h 93b7b27ccc89e1ad4279eb12560e1dfc3d2ff1eb - include/libdvbv5/dvb-fe.h aeeb75989de4b2ecafa1b2fc5ed30787999eec1b - include/libdvbv5/mpeg_es.h dc684983365ffef353993faad0b105bcb87f4218 - include/libdvbv5/desc_event_short.h 5e3be50d1292109d59c80f920ba1b7a4bcbbdf56 - include/libdvbv5/eit.h f95a0206952db4cce561455accad66a37f9a4104 - include/libdvbv5/mgt.h 88f8c78bbaf22b345e2ccb68e9b787e649dac2c1 - include/libdvbv5/libdvb-version.h.in 74a66644980a66adeba21ecc40ebefa2b4480e25 - include/libdvbv5/pmt.h 7671f125c6f61eef85f55eb314097435b3772431 - include/libdvbv5/dvb-sat.h 26fd40a10548cdb9bd4aafdda4486f9d39fd5c9e - include/libdvbv5/countries.h 65db3beaf2ec19a43a870b2759e6aecb15a95c7b - include/libdvbv5/atsc_header.h 149f7eb3c71b8f65127d3c4f2143080b115697e5 - include/libdvbv5/desc_terrestrial_delivery.h 708af2b77b2c94ee9cf593b31e20471c80ea556b - include/libdvbv5/mpeg_ts.h ba205468e43ca16edf1ebec59dce9822044c1064 - include/libdvbv5/desc_registration_id.h e70b24d8d1f3a248735c2d724d3b92c49b7aaba5 - include/libdvbv5/dvb-log.h 2d0557eadd4cff02ab52e26778d35b6c34f09158 - libv4l2/libv4l2-priv.h 487af43db163be6d3604371b9a504c1df2a9bf6c - libv4l2/v4l2-plugin-android.c 0ac38b46fa1659db4c05660205f212389295e7ff - libv4l2/Makefile 8ac3789333a6cd18e7f35d3b66a8a4d0293feb78 - libv4l2/Makefile.dGPU fdd6e5e27aa8b41d5da77cb3b9be3fc3b8a44763 - libv4l2/libv4l2.export 1d37e5ea9231e7e0eea7d1a02938c6d1ae8ba791 - libv4l2/Makefile.in 51892a60925c37c865fe6eb9c923a552df180ebf - libv4l2/v4l2-plugin.c d4c7daee644a35244ac8a5e1009a3aeb1d4fd3da - libv4l2/Makefile.am 87856c7113d150dceb254b5d548f942e7fcaf8f1 - libv4l2/libv4l2.c d11ec5b8ce8390a72fd61457d7b8667378311191 - libv4l2/log.c cbcee4426c19c168c6f49d04af3a0b2e30c0b681 - libv4l2/libv4l2.pc.in 55bb16d05817a3ecf076174175b2ca5b56e729d8 - libv4l2/v4l2convert.c Change-Id: I92b00c7d65c1689c9d6ff337a6f2ae790e88a7cb
2000 lines
58 KiB
C
2000 lines
58 KiB
C
/*
|
|
# (C) 2008-2011 Hans de Goede <hdegoede@redhat.com>
|
|
#
|
|
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# 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.
|
|
#
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
|
|
*/
|
|
|
|
#ifdef ANDROID
|
|
#include <android-config.h>
|
|
#else
|
|
#include <config.h>
|
|
#endif
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include "libv4lconvert.h"
|
|
#include "libv4lconvert-priv.h"
|
|
#include "libv4lsyscall-priv.h"
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
|
|
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
|
|
|
|
static inline void set_bit(int nr, volatile unsigned long *addr)
|
|
{
|
|
unsigned long mask = BIT_MASK(nr);
|
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
|
|
|
*p |= mask;
|
|
}
|
|
|
|
static inline void clear_bit(int nr, volatile unsigned long *addr)
|
|
{
|
|
unsigned long mask = BIT_MASK(nr);
|
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
|
|
|
*p &= ~mask;
|
|
}
|
|
|
|
static inline int test_bit(int nr, const volatile unsigned long *addr)
|
|
{
|
|
return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
|
|
}
|
|
|
|
static void *dev_init(int fd)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
static void dev_close(void *dev_ops_priv)
|
|
{
|
|
}
|
|
|
|
static int dev_ioctl(void *dev_ops_priv, int fd, unsigned long cmd, void *arg)
|
|
{
|
|
return SYS_IOCTL(fd, cmd, arg);
|
|
}
|
|
|
|
static ssize_t dev_read(void *dev_ops_priv, int fd, void *buf, size_t len)
|
|
{
|
|
return SYS_READ(fd, buf, len);
|
|
}
|
|
|
|
static ssize_t dev_write(void *dev_ops_priv, int fd, const void *buf,
|
|
size_t len)
|
|
{
|
|
return SYS_WRITE(fd, buf, len);
|
|
}
|
|
|
|
static const struct libv4l_dev_ops default_dev_ops = {
|
|
.init = dev_init,
|
|
.close = dev_close,
|
|
.ioctl = dev_ioctl,
|
|
.read = dev_read,
|
|
.write = dev_write,
|
|
};
|
|
|
|
const struct libv4l_dev_ops *v4lconvert_get_default_dev_ops()
|
|
{
|
|
return &default_dev_ops;
|
|
}
|
|
|
|
static void v4lconvert_get_framesizes(struct v4lconvert_data *data,
|
|
unsigned int pixelformat, int index);
|
|
|
|
/*
|
|
* Notes:
|
|
* 1) for proper functioning of v4lconvert_enum_fmt the first entries in
|
|
* supported_src_pixfmts must match with the entries in
|
|
* supported_dst_pixfmts.
|
|
* 2) The field needs_conversion should be zero, *except* for device-specific
|
|
* formats, where it doesn't make sense for applications to have their
|
|
* own decoders.
|
|
*/
|
|
#define SUPPORTED_DST_PIXFMTS \
|
|
/* fourcc bpp rgb yuv needs */ \
|
|
/* rank rank conversion */ \
|
|
{ V4L2_PIX_FMT_RGB24, 24, 1, 5, 0 }, \
|
|
{ V4L2_PIX_FMT_BGR24, 24, 1, 5, 0 }, \
|
|
{ V4L2_PIX_FMT_YUV420, 12, 6, 1, 0 }, \
|
|
{ V4L2_PIX_FMT_YVU420, 12, 6, 1, 0 }
|
|
|
|
static const struct v4lconvert_pixfmt supported_src_pixfmts[] = {
|
|
SUPPORTED_DST_PIXFMTS,
|
|
/* packed rgb formats */
|
|
{ V4L2_PIX_FMT_RGB565, 16, 4, 6, 0 },
|
|
{ V4L2_PIX_FMT_BGR32, 32, 4, 6, 0 },
|
|
{ V4L2_PIX_FMT_RGB32, 32, 4, 6, 0 },
|
|
{ V4L2_PIX_FMT_XBGR32, 32, 4, 6, 0 },
|
|
{ V4L2_PIX_FMT_XRGB32, 32, 4, 6, 0 },
|
|
{ V4L2_PIX_FMT_ABGR32, 32, 4, 6, 0 },
|
|
{ V4L2_PIX_FMT_ARGB32, 32, 4, 6, 0 },
|
|
/* yuv 4:2:2 formats */
|
|
{ V4L2_PIX_FMT_YUYV, 16, 5, 4, 0 },
|
|
{ V4L2_PIX_FMT_YVYU, 16, 5, 4, 0 },
|
|
{ V4L2_PIX_FMT_UYVY, 16, 5, 4, 0 },
|
|
{ V4L2_PIX_FMT_NV16, 16, 5, 4, 1 },
|
|
{ V4L2_PIX_FMT_NV61, 16, 5, 4, 1 },
|
|
/* yuv 4:2:0 formats */
|
|
{ V4L2_PIX_FMT_SPCA501, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_SPCA505, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_SPCA508, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_CIT_YYVYUY, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_KONICA420, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_SN9C20X_I420, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_M420, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_HM12, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_NV12, 12, 6, 3, 1 },
|
|
{ V4L2_PIX_FMT_CPIA1, 0, 6, 3, 1 },
|
|
/* JPEG and variants */
|
|
{ V4L2_PIX_FMT_MJPEG, 0, 7, 7, 0 },
|
|
{ V4L2_PIX_FMT_JPEG, 0, 7, 7, 0 },
|
|
{ V4L2_PIX_FMT_PJPG, 0, 7, 7, 1 },
|
|
{ V4L2_PIX_FMT_JPGL, 0, 7, 7, 1 },
|
|
#ifdef HAVE_LIBV4LCONVERT_HELPERS
|
|
{ V4L2_PIX_FMT_OV511, 0, 7, 7, 1 },
|
|
{ V4L2_PIX_FMT_OV518, 0, 7, 7, 1 },
|
|
#endif
|
|
/* uncompressed bayer */
|
|
{ V4L2_PIX_FMT_SBGGR8, 8, 8, 8, 0 },
|
|
{ V4L2_PIX_FMT_SGBRG8, 8, 8, 8, 0 },
|
|
{ V4L2_PIX_FMT_SGRBG8, 8, 8, 8, 0 },
|
|
{ V4L2_PIX_FMT_SRGGB8, 8, 8, 8, 0 },
|
|
{ V4L2_PIX_FMT_STV0680, 8, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SBGGR10P, 10, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SGBRG10P, 10, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SGRBG10P, 10, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SRGGB10P, 10, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SBGGR10, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SGBRG10, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SGRBG10, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SRGGB10, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SBGGR16, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SGBRG16, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SGRBG16, 16, 8, 8, 1 },
|
|
{ V4L2_PIX_FMT_SRGGB16, 16, 8, 8, 1 },
|
|
/* compressed bayer */
|
|
{ V4L2_PIX_FMT_SPCA561, 0, 9, 9, 1 },
|
|
{ V4L2_PIX_FMT_SN9C10X, 0, 9, 9, 1 },
|
|
{ V4L2_PIX_FMT_SN9C2028, 0, 9, 9, 1 },
|
|
{ V4L2_PIX_FMT_PAC207, 0, 9, 9, 1 },
|
|
{ V4L2_PIX_FMT_MR97310A, 0, 9, 9, 1 },
|
|
#ifdef HAVE_JPEG
|
|
{ V4L2_PIX_FMT_JL2005BCD, 0, 9, 9, 1 },
|
|
#endif
|
|
{ V4L2_PIX_FMT_SQ905C, 0, 9, 9, 1 },
|
|
/* special */
|
|
{ V4L2_PIX_FMT_SE401, 0, 8, 9, 1 },
|
|
/* grey formats */
|
|
{ V4L2_PIX_FMT_GREY, 8, 20, 20, 0 },
|
|
{ V4L2_PIX_FMT_Y4, 8, 20, 20, 0 },
|
|
{ V4L2_PIX_FMT_Y6, 8, 20, 20, 0 },
|
|
{ V4L2_PIX_FMT_Y10BPACK, 10, 20, 20, 0 },
|
|
{ V4L2_PIX_FMT_Y16, 16, 20, 20, 0 },
|
|
{ V4L2_PIX_FMT_Y16_BE, 16, 20, 20, 0 },
|
|
/* hsv formats */
|
|
{ V4L2_PIX_FMT_HSV32, 32, 5, 4, 0 },
|
|
{ V4L2_PIX_FMT_HSV24, 24, 5, 4, 0 },
|
|
};
|
|
|
|
static const struct v4lconvert_pixfmt supported_dst_pixfmts[] = {
|
|
SUPPORTED_DST_PIXFMTS
|
|
};
|
|
|
|
/* List of well known resolutions which we can get by cropping somewhat larger
|
|
resolutions */
|
|
static const unsigned int v4lconvert_crop_res[][2] = {
|
|
/* low res VGA resolutions, can be made by software cropping SIF resolutions
|
|
for cam/drivers which do not support this in hardware */
|
|
{ 320, 240 },
|
|
{ 160, 120 },
|
|
/* Some CIF cams (with vv6410 sensor) have slightly larger then usual CIF
|
|
resolutions, make regular CIF resolutions available on these by sw crop */
|
|
{ 352, 288 },
|
|
{ 176, 144 },
|
|
};
|
|
|
|
struct v4lconvert_data *v4lconvert_create(int fd)
|
|
{
|
|
return v4lconvert_create_with_dev_ops(fd, NULL, &default_dev_ops);
|
|
}
|
|
|
|
struct v4lconvert_data *v4lconvert_create_with_dev_ops(int fd, void *dev_ops_priv,
|
|
const struct libv4l_dev_ops *dev_ops)
|
|
{
|
|
int i, j;
|
|
struct v4lconvert_data *data = calloc(1, sizeof(struct v4lconvert_data));
|
|
struct v4l2_capability cap;
|
|
/*
|
|
* This keeps tracks of device-specific formats for which apps most
|
|
* likely don't know. If all a driver can offer are proprietary
|
|
* formats, a conversion is needed anyway. We can thus safely
|
|
* add software processing controls without much concern about a
|
|
* performance impact.
|
|
*/
|
|
int always_needs_conversion = 1;
|
|
|
|
if (!data) {
|
|
fprintf(stderr, "libv4lconvert: error: out of memory!\n");
|
|
return NULL;
|
|
}
|
|
|
|
data->fd = fd;
|
|
data->dev_ops = dev_ops;
|
|
data->dev_ops_priv = dev_ops_priv;
|
|
data->decompress_pid = -1;
|
|
data->fps = 30;
|
|
|
|
/* Check supported formats */
|
|
for (i = 0; ; i++) {
|
|
struct v4l2_fmtdesc fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
|
|
|
|
fmt.index = i;
|
|
|
|
if (data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_ENUM_FMT, &fmt))
|
|
break;
|
|
|
|
for (j = 0; j < ARRAY_SIZE(supported_src_pixfmts); j++)
|
|
if (fmt.pixelformat == supported_src_pixfmts[j].fmt)
|
|
break;
|
|
|
|
if (j < ARRAY_SIZE(supported_src_pixfmts)) {
|
|
set_bit(j, data->supported_src_formats);
|
|
v4lconvert_get_framesizes(data, fmt.pixelformat, j);
|
|
if (!supported_src_pixfmts[j].needs_conversion)
|
|
always_needs_conversion = 0;
|
|
} else
|
|
always_needs_conversion = 0;
|
|
}
|
|
|
|
data->no_formats = i;
|
|
|
|
/* Check if this cam has any special flags */
|
|
if (data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_QUERYCAP, &cap) == 0) {
|
|
if (!strcmp((char *)cap.driver, "uvcvideo"))
|
|
data->flags |= V4LCONVERT_IS_UVC;
|
|
|
|
if (cap.capabilities & V4L2_CAP_DEVICE_CAPS)
|
|
cap.capabilities = cap.device_caps;
|
|
if ((cap.capabilities & 0xff) & ~V4L2_CAP_VIDEO_CAPTURE)
|
|
always_needs_conversion = 0;
|
|
}
|
|
|
|
data->control = v4lcontrol_create(fd, dev_ops_priv, dev_ops,
|
|
always_needs_conversion);
|
|
if (!data->control) {
|
|
free(data);
|
|
return NULL;
|
|
}
|
|
data->bandwidth = v4lcontrol_get_bandwidth(data->control);
|
|
data->control_flags = v4lcontrol_get_flags(data->control);
|
|
if (data->control_flags & V4LCONTROL_FORCE_TINYJPEG)
|
|
data->flags |= V4LCONVERT_USE_TINYJPEG;
|
|
|
|
data->processing = v4lprocessing_create(fd, data->control);
|
|
if (!data->processing) {
|
|
v4lcontrol_destroy(data->control);
|
|
free(data);
|
|
return NULL;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
void v4lconvert_destroy(struct v4lconvert_data *data)
|
|
{
|
|
if (!data)
|
|
return;
|
|
|
|
v4lprocessing_destroy(data->processing);
|
|
v4lcontrol_destroy(data->control);
|
|
if (data->tinyjpeg) {
|
|
unsigned char *comps[3] = { NULL, NULL, NULL };
|
|
|
|
tinyjpeg_set_components(data->tinyjpeg, comps, 3);
|
|
tinyjpeg_free(data->tinyjpeg);
|
|
}
|
|
#ifdef HAVE_JPEG
|
|
if (data->cinfo_initialized)
|
|
jpeg_destroy_decompress(&data->cinfo);
|
|
#endif // HAVE_JPEG
|
|
#ifdef HAVE_LIBV4LCONVERT_HELPERS
|
|
v4lconvert_helper_cleanup(data);
|
|
#endif
|
|
free(data->convert1_buf);
|
|
free(data->convert2_buf);
|
|
free(data->rotate90_buf);
|
|
free(data->flip_buf);
|
|
free(data->convert_pixfmt_buf);
|
|
free(data->previous_frame);
|
|
free(data);
|
|
}
|
|
|
|
int v4lconvert_supported_dst_format(unsigned int pixelformat)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(supported_dst_pixfmts); i++)
|
|
if (supported_dst_pixfmts[i].fmt == pixelformat)
|
|
break;
|
|
|
|
return i != ARRAY_SIZE(supported_dst_pixfmts);
|
|
}
|
|
|
|
int v4lconvert_supported_dst_fmt_only(struct v4lconvert_data *data)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0 ; i < ARRAY_SIZE(data->supported_src_formats); i++)
|
|
if (data->supported_src_formats[i])
|
|
break;
|
|
if (i == ARRAY_SIZE(data->supported_src_formats))
|
|
return 0;
|
|
|
|
return v4lcontrol_needs_conversion(data->control);
|
|
}
|
|
|
|
/* See libv4lconvert.h for description of in / out parameters */
|
|
int v4lconvert_enum_fmt(struct v4lconvert_data *data, struct v4l2_fmtdesc *fmt)
|
|
{
|
|
int i, no_faked_fmts = 0;
|
|
unsigned int faked_fmts[ARRAY_SIZE(supported_dst_pixfmts)];
|
|
|
|
if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
|
|
(!v4lconvert_supported_dst_fmt_only(data) &&
|
|
fmt->index < data->no_formats))
|
|
return data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_ENUM_FMT, fmt);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(supported_dst_pixfmts); i++)
|
|
if (v4lconvert_supported_dst_fmt_only(data) ||
|
|
!test_bit(i, data->supported_src_formats)) {
|
|
faked_fmts[no_faked_fmts] = supported_dst_pixfmts[i].fmt;
|
|
no_faked_fmts++;
|
|
}
|
|
|
|
if (!v4lconvert_supported_dst_fmt_only(data))
|
|
i = fmt->index - data->no_formats;
|
|
else
|
|
i = fmt->index;
|
|
|
|
if (i >= no_faked_fmts) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
fmt->flags = V4L2_FMT_FLAG_EMULATED;
|
|
fmt->pixelformat = faked_fmts[i];
|
|
fmt->description[0] = faked_fmts[i] & 0xff;
|
|
fmt->description[1] = (faked_fmts[i] >> 8) & 0xff;
|
|
fmt->description[2] = (faked_fmts[i] >> 16) & 0xff;
|
|
fmt->description[3] = faked_fmts[i] >> 24;
|
|
fmt->description[4] = '\0';
|
|
memset(fmt->reserved, 0, sizeof(fmt->reserved));
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* This function returns a value to rank (sort) source format by preference
|
|
when multiple source formats are available for a certain resolution, the
|
|
source format for which this function returns the lowest value wins.
|
|
|
|
This function uses the rgb_rank resp. yuv_rank values as a base when
|
|
converting to rgb32 resp. yuv420. The initial ranks range from 1 - 10,
|
|
the initial rank purely expresses the CPU cost of doing the conversion, the
|
|
ranking algorithm will give a penalty of 10 points if
|
|
(width * height * fps * bpp / 8) > bandwidth
|
|
thus disqualifying a src format which causes the bandwidth to be exceeded,
|
|
except when all of them cause this.
|
|
|
|
Note grey scale formats start at 20 rather than 1-10, because we want to
|
|
never autoselect them, unless they are the only choice */
|
|
static int v4lconvert_get_rank(struct v4lconvert_data *data,
|
|
int src_index, int src_width, int src_height,
|
|
unsigned int dest_pixelformat)
|
|
{
|
|
int needed, rank = 0;
|
|
|
|
switch (dest_pixelformat) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
rank = supported_src_pixfmts[src_index].rgb_rank;
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
rank = supported_src_pixfmts[src_index].yuv_rank;
|
|
break;
|
|
}
|
|
|
|
/* So that if both rgb32 and bgr32 are supported, or both yuv420 and
|
|
yvu420 the right one wins */
|
|
if (supported_src_pixfmts[src_index].fmt == dest_pixelformat)
|
|
rank--;
|
|
|
|
/* check bandwidth needed */
|
|
needed = src_width * src_height * data->fps *
|
|
supported_src_pixfmts[src_index].bpp / 8;
|
|
if (data->bandwidth && needed > data->bandwidth)
|
|
rank += 10;
|
|
#if 0
|
|
printf("ranked: %c%c%c%c for %dx%d @ %d fps, needed: %d, bandwidth: %d, rank: %d\n",
|
|
supported_src_pixfmts[src_index].fmt & 0xff,
|
|
(supported_src_pixfmts[src_index].fmt >> 8) & 0xff,
|
|
(supported_src_pixfmts[src_index].fmt >> 16) & 0xff,
|
|
supported_src_pixfmts[src_index].fmt >> 24, src_width,
|
|
src_height, data->fps, needed, data->bandwidth, rank);
|
|
#endif
|
|
return rank;
|
|
}
|
|
|
|
/* Find out what format to use based on the (cached) results of enum
|
|
framesizes instead of doing a zillion try_fmt calls. This function
|
|
currently is intended for use with UVC cams only. This is esp.
|
|
important for UVC based cams as doing try_fmt there actually causes I/O */
|
|
static int v4lconvert_do_try_format_uvc(struct v4lconvert_data *data,
|
|
struct v4l2_format *dest_fmt, struct v4l2_format *src_fmt)
|
|
{
|
|
unsigned int i, rank;
|
|
unsigned int closest_fmt_size_diff = -1;
|
|
int best_framesize = 0;/* Just use the first format if no small enough one */
|
|
int best_format = 0;
|
|
unsigned int best_rank = 100;
|
|
|
|
for (i = 0; i < data->no_framesizes; i++) {
|
|
if (data->framesizes[i].discrete.width <= dest_fmt->fmt.pix.width &&
|
|
data->framesizes[i].discrete.height <= dest_fmt->fmt.pix.height) {
|
|
int size_x_diff = dest_fmt->fmt.pix.width -
|
|
data->framesizes[i].discrete.width;
|
|
int size_y_diff = dest_fmt->fmt.pix.height -
|
|
data->framesizes[i].discrete.height;
|
|
unsigned int size_diff = size_x_diff * size_x_diff +
|
|
size_y_diff * size_y_diff;
|
|
|
|
if (size_diff < closest_fmt_size_diff) {
|
|
closest_fmt_size_diff = size_diff;
|
|
best_framesize = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(supported_src_pixfmts); i++) {
|
|
/* is this format supported? */
|
|
if (!(data->framesize_supported_src_formats[best_framesize] &
|
|
(1ULL << i)))
|
|
continue;
|
|
|
|
/* Note the hardcoded use of discrete is based on this function
|
|
only getting called for uvc devices */
|
|
rank = v4lconvert_get_rank(data, i,
|
|
data->framesizes[best_framesize].discrete.width,
|
|
data->framesizes[best_framesize].discrete.height,
|
|
dest_fmt->fmt.pix.pixelformat);
|
|
if (rank < best_rank) {
|
|
best_rank = rank;
|
|
best_format = supported_src_pixfmts[i].fmt;
|
|
}
|
|
}
|
|
|
|
dest_fmt->fmt.pix.width = data->framesizes[best_framesize].discrete.width;
|
|
dest_fmt->fmt.pix.height = data->framesizes[best_framesize].discrete.height;
|
|
dest_fmt->fmt.pix.field = V4L2_FIELD_NONE; /* UVC has no fields */
|
|
/* Not pretty, the pwc driver doesn't fill these in try_fmt either though,
|
|
so we should be able to get away with this. */
|
|
dest_fmt->fmt.pix.bytesperline = 0;
|
|
dest_fmt->fmt.pix.sizeimage = 0;
|
|
dest_fmt->fmt.pix.colorspace = V4L2_COLORSPACE_DEFAULT;
|
|
dest_fmt->fmt.pix.priv = 0;
|
|
|
|
*src_fmt = *dest_fmt;
|
|
src_fmt->fmt.pix.pixelformat = best_format;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int v4lconvert_do_try_format(struct v4lconvert_data *data,
|
|
struct v4l2_format *dest_fmt, struct v4l2_format *src_fmt)
|
|
{
|
|
int i, size_x_diff, size_y_diff, rank, best_rank = 0;
|
|
unsigned int size_diff, closest_fmt_size_diff = -1;
|
|
unsigned int desired_pixfmt = dest_fmt->fmt.pix.pixelformat;
|
|
struct v4l2_format try_fmt, closest_fmt = { .type = 0 };
|
|
|
|
if (data->flags & V4LCONVERT_IS_UVC)
|
|
return v4lconvert_do_try_format_uvc(data, dest_fmt, src_fmt);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(supported_src_pixfmts); i++) {
|
|
/* is this format supported? */
|
|
if (!test_bit(i, data->supported_src_formats))
|
|
continue;
|
|
|
|
try_fmt = *dest_fmt;
|
|
try_fmt.fmt.pix.pixelformat = supported_src_pixfmts[i].fmt;
|
|
if (data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_TRY_FMT, &try_fmt))
|
|
continue;
|
|
|
|
if (try_fmt.fmt.pix.pixelformat !=
|
|
supported_src_pixfmts[i].fmt)
|
|
continue;
|
|
|
|
/* Did we get a better match than before? */
|
|
size_x_diff = (int)try_fmt.fmt.pix.width -
|
|
(int)dest_fmt->fmt.pix.width;
|
|
size_y_diff = (int)try_fmt.fmt.pix.height -
|
|
(int)dest_fmt->fmt.pix.height;
|
|
size_diff = size_x_diff * size_x_diff +
|
|
size_y_diff * size_y_diff;
|
|
|
|
rank = v4lconvert_get_rank(data, i,
|
|
try_fmt.fmt.pix.width,
|
|
try_fmt.fmt.pix.height,
|
|
desired_pixfmt);
|
|
if (size_diff < closest_fmt_size_diff ||
|
|
(size_diff == closest_fmt_size_diff && rank < best_rank)) {
|
|
closest_fmt = try_fmt;
|
|
closest_fmt_size_diff = size_diff;
|
|
best_rank = rank;
|
|
}
|
|
}
|
|
|
|
if (closest_fmt.type == 0)
|
|
return -1;
|
|
|
|
*dest_fmt = closest_fmt;
|
|
if (closest_fmt.fmt.pix.pixelformat != desired_pixfmt)
|
|
dest_fmt->fmt.pix.pixelformat = desired_pixfmt;
|
|
*src_fmt = closest_fmt;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void v4lconvert_fixup_fmt(struct v4l2_format *fmt)
|
|
{
|
|
switch (fmt->fmt.pix.pixelformat) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
fmt->fmt.pix.bytesperline = fmt->fmt.pix.width * 3;
|
|
fmt->fmt.pix.sizeimage = fmt->fmt.pix.width * fmt->fmt.pix.height * 3;
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
fmt->fmt.pix.bytesperline = fmt->fmt.pix.width;
|
|
fmt->fmt.pix.sizeimage = fmt->fmt.pix.width * fmt->fmt.pix.height * 3 / 2;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* See libv4lconvert.h for description of in / out parameters */
|
|
int v4lconvert_try_format(struct v4lconvert_data *data,
|
|
struct v4l2_format *dest_fmt, struct v4l2_format *src_fmt)
|
|
{
|
|
int i, result;
|
|
unsigned int desired_width = dest_fmt->fmt.pix.width;
|
|
unsigned int desired_height = dest_fmt->fmt.pix.height;
|
|
struct v4l2_format try_src, try_dest, try2_src, try2_dest;
|
|
|
|
if (dest_fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
|
|
v4lconvert_supported_dst_fmt_only(data) &&
|
|
!v4lconvert_supported_dst_format(dest_fmt->fmt.pix.pixelformat))
|
|
dest_fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
|
|
|
|
try_dest = *dest_fmt;
|
|
|
|
/* Can we do conversion to the requested format & type? */
|
|
if (!v4lconvert_supported_dst_format(dest_fmt->fmt.pix.pixelformat) ||
|
|
dest_fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
|
|
v4lconvert_do_try_format(data, &try_dest, &try_src)) {
|
|
result = data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_TRY_FMT, dest_fmt);
|
|
if (src_fmt)
|
|
*src_fmt = *dest_fmt;
|
|
return result;
|
|
}
|
|
|
|
/* In case of a non exact resolution match, try again with a slightly larger
|
|
resolution as some weird devices are not able to crop of the number of
|
|
extra (border) pixels most sensors have compared to standard resolutions,
|
|
which we will then just crop off in software */
|
|
if (try_dest.fmt.pix.width != desired_width ||
|
|
try_dest.fmt.pix.height != desired_height) {
|
|
try2_dest = *dest_fmt;
|
|
try2_dest.fmt.pix.width = desired_width + 7;
|
|
try2_dest.fmt.pix.height = desired_height + 1;
|
|
result = v4lconvert_do_try_format(data, &try2_dest, &try2_src);
|
|
if (result == 0 &&
|
|
try2_dest.fmt.pix.width >= desired_width &&
|
|
try2_dest.fmt.pix.width <= desired_width + 7 &&
|
|
try2_dest.fmt.pix.height >= desired_height &&
|
|
try2_dest.fmt.pix.height <= desired_height + 1) {
|
|
/* Success! */
|
|
try2_dest.fmt.pix.width = desired_width;
|
|
try2_dest.fmt.pix.height = desired_height;
|
|
try_dest = try2_dest;
|
|
try_src = try2_src;
|
|
}
|
|
}
|
|
|
|
/* In case of a non exact resolution match, see if this is a well known
|
|
resolution some apps are hardcoded too and try to give the app what it
|
|
asked for by cropping a slightly larger resolution or adding a small
|
|
black border to a slightly smaller resolution */
|
|
if (try_dest.fmt.pix.width != desired_width ||
|
|
try_dest.fmt.pix.height != desired_height) {
|
|
for (i = 0; i < ARRAY_SIZE(v4lconvert_crop_res); i++) {
|
|
if (v4lconvert_crop_res[i][0] == desired_width &&
|
|
v4lconvert_crop_res[i][1] == desired_height) {
|
|
try2_dest = *dest_fmt;
|
|
|
|
/* Note these are chosen so that cropping to vga res just works for
|
|
vv6410 sensor cams, which have 356x292 and 180x148 */
|
|
try2_dest.fmt.pix.width = desired_width * 113 / 100;
|
|
try2_dest.fmt.pix.height = desired_height * 124 / 100;
|
|
result = v4lconvert_do_try_format(data, &try2_dest, &try2_src);
|
|
if (result == 0 &&
|
|
(/* Add a small black border of max 16 pixels */
|
|
(try2_dest.fmt.pix.width >= desired_width - 16 &&
|
|
try2_dest.fmt.pix.width <= desired_width &&
|
|
try2_dest.fmt.pix.height >= desired_height - 16 &&
|
|
try2_dest.fmt.pix.height <= desired_height) ||
|
|
/* Standard cropping to max 80% of actual width / height */
|
|
(try2_dest.fmt.pix.width >= desired_width &&
|
|
try2_dest.fmt.pix.width <= desired_width * 5 / 4 &&
|
|
try2_dest.fmt.pix.height >= desired_height &&
|
|
try2_dest.fmt.pix.height <= desired_height * 5 / 4) ||
|
|
/* Downscale 2x + cropping to max 80% of actual width / height */
|
|
(try2_dest.fmt.pix.width >= desired_width * 2 &&
|
|
try2_dest.fmt.pix.width <= desired_width * 5 / 2 &&
|
|
try2_dest.fmt.pix.height >= desired_height * 2 &&
|
|
try2_dest.fmt.pix.height <= desired_height * 5 / 2))) {
|
|
/* Success! */
|
|
try2_dest.fmt.pix.width = desired_width;
|
|
try2_dest.fmt.pix.height = desired_height;
|
|
try_dest = try2_dest;
|
|
try_src = try2_src;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Some applications / libs (*cough* gstreamer *cough*) will not work
|
|
correctly with planar YUV formats when the width is not a multiple of 8
|
|
or the height is not a multiple of 2. With RGB formats these apps require
|
|
the width to be a multiple of 4. We apply the same rounding to all
|
|
formats to not end up with 2 close but different resolutions. */
|
|
try_dest.fmt.pix.width &= ~7;
|
|
try_dest.fmt.pix.height &= ~1;
|
|
|
|
/* Are we converting / cropping ? */
|
|
if (try_src.fmt.pix.width != try_dest.fmt.pix.width ||
|
|
try_src.fmt.pix.height != try_dest.fmt.pix.height ||
|
|
try_src.fmt.pix.pixelformat != try_dest.fmt.pix.pixelformat)
|
|
v4lconvert_fixup_fmt(&try_dest);
|
|
|
|
*dest_fmt = try_dest;
|
|
if (src_fmt)
|
|
*src_fmt = try_src;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Is conversion necessary ? */
|
|
int v4lconvert_needs_conversion(struct v4lconvert_data *data,
|
|
const struct v4l2_format *src_fmt, /* in */
|
|
const struct v4l2_format *dest_fmt) /* in */
|
|
{
|
|
if (src_fmt->fmt.pix.width != dest_fmt->fmt.pix.width ||
|
|
src_fmt->fmt.pix.height != dest_fmt->fmt.pix.height ||
|
|
src_fmt->fmt.pix.pixelformat != dest_fmt->fmt.pix.pixelformat ||
|
|
(v4lcontrol_needs_conversion(data->control) &&
|
|
v4lconvert_supported_dst_format(dest_fmt->fmt.pix.pixelformat)))
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int v4lconvert_processing_needs_double_conversion(
|
|
unsigned int src_pix_fmt, unsigned int dest_pix_fmt)
|
|
{
|
|
switch (src_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
case V4L2_PIX_FMT_SPCA561:
|
|
case V4L2_PIX_FMT_SN9C10X:
|
|
case V4L2_PIX_FMT_PAC207:
|
|
case V4L2_PIX_FMT_MR97310A:
|
|
#ifdef HAVE_JPEG
|
|
case V4L2_PIX_FMT_JL2005BCD:
|
|
#endif
|
|
case V4L2_PIX_FMT_SN9C2028:
|
|
case V4L2_PIX_FMT_SQ905C:
|
|
case V4L2_PIX_FMT_SBGGR8:
|
|
case V4L2_PIX_FMT_SGBRG8:
|
|
case V4L2_PIX_FMT_SGRBG8:
|
|
case V4L2_PIX_FMT_SRGGB8:
|
|
case V4L2_PIX_FMT_SBGGR10P:
|
|
case V4L2_PIX_FMT_SGBRG10P:
|
|
case V4L2_PIX_FMT_SGRBG10P:
|
|
case V4L2_PIX_FMT_SRGGB10P:
|
|
case V4L2_PIX_FMT_SBGGR10:
|
|
case V4L2_PIX_FMT_SGBRG10:
|
|
case V4L2_PIX_FMT_SGRBG10:
|
|
case V4L2_PIX_FMT_SRGGB10:
|
|
case V4L2_PIX_FMT_SBGGR16:
|
|
case V4L2_PIX_FMT_SGBRG16:
|
|
case V4L2_PIX_FMT_SGRBG16:
|
|
case V4L2_PIX_FMT_SRGGB16:
|
|
case V4L2_PIX_FMT_STV0680:
|
|
return 0;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
unsigned char *v4lconvert_alloc_buffer(int needed,
|
|
unsigned char **buf, int *buf_size)
|
|
{
|
|
if (*buf_size < needed) {
|
|
free(*buf);
|
|
*buf = malloc(needed);
|
|
if (*buf == NULL) {
|
|
*buf_size = 0;
|
|
return NULL;
|
|
}
|
|
*buf_size = needed;
|
|
}
|
|
return *buf;
|
|
}
|
|
|
|
int v4lconvert_oom_error(struct v4lconvert_data *data)
|
|
{
|
|
V4LCONVERT_ERR("could not allocate memory\n");
|
|
errno = ENOMEM;
|
|
return -1;
|
|
}
|
|
|
|
static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
|
|
unsigned char *src, unsigned int src_size, unsigned char *dest, int dest_size,
|
|
struct v4l2_format *fmt, unsigned int dest_pix_fmt)
|
|
{
|
|
int result = 0;
|
|
unsigned int src_pix_fmt = fmt->fmt.pix.pixelformat;
|
|
unsigned int width = fmt->fmt.pix.width;
|
|
unsigned int height = fmt->fmt.pix.height;
|
|
unsigned int bytesperline = fmt->fmt.pix.bytesperline;
|
|
|
|
switch (src_pix_fmt) {
|
|
/* JPG and variants */
|
|
case V4L2_PIX_FMT_MJPEG:
|
|
case V4L2_PIX_FMT_JPEG:
|
|
#ifdef HAVE_JPEG
|
|
if (data->flags & V4LCONVERT_USE_TINYJPEG) {
|
|
#endif // HAVE_JPEG
|
|
result = v4lconvert_decode_jpeg_tinyjpeg(data,
|
|
src, src_size, dest,
|
|
fmt, dest_pix_fmt, 0);
|
|
#ifdef HAVE_JPEG
|
|
} else {
|
|
result = v4lconvert_decode_jpeg_libjpeg(data,
|
|
src, src_size, dest,
|
|
fmt, dest_pix_fmt);
|
|
if (result == -1 && errno == EOPNOTSUPP) {
|
|
/* Fall back to tinyjpeg */
|
|
jpeg_destroy_decompress(&data->cinfo);
|
|
data->cinfo_initialized = 0;
|
|
data->flags |= V4LCONVERT_USE_TINYJPEG;
|
|
result = v4lconvert_decode_jpeg_tinyjpeg(data,
|
|
src, src_size, dest,
|
|
fmt, dest_pix_fmt, 0);
|
|
}
|
|
}
|
|
#endif // HAVE_JPEG
|
|
break;
|
|
case V4L2_PIX_FMT_PJPG:
|
|
result = v4lconvert_decode_jpeg_tinyjpeg(data, src, src_size,
|
|
dest, fmt, dest_pix_fmt,
|
|
TINYJPEG_FLAGS_PIXART_JPEG);
|
|
break;
|
|
case V4L2_PIX_FMT_JPGL:
|
|
result = v4lconvert_decode_jpgl(src, src_size, dest_pix_fmt,
|
|
dest, width, height);
|
|
break;
|
|
|
|
/* Custom cam specific YUV formats */
|
|
case V4L2_PIX_FMT_SPCA501:
|
|
case V4L2_PIX_FMT_SPCA505:
|
|
case V4L2_PIX_FMT_SPCA508:
|
|
case V4L2_PIX_FMT_CIT_YYVYUY:
|
|
case V4L2_PIX_FMT_KONICA420:
|
|
case V4L2_PIX_FMT_M420:
|
|
case V4L2_PIX_FMT_SN9C20X_I420:
|
|
case V4L2_PIX_FMT_CPIA1:
|
|
case V4L2_PIX_FMT_OV511:
|
|
case V4L2_PIX_FMT_OV518: {
|
|
unsigned char *d;
|
|
int d_size;
|
|
int yvu = 0;
|
|
|
|
if (dest_pix_fmt != V4L2_PIX_FMT_YUV420 &&
|
|
dest_pix_fmt != V4L2_PIX_FMT_YVU420) {
|
|
d = v4lconvert_alloc_buffer(width * height * 3 / 2,
|
|
&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
|
|
if (!d)
|
|
return v4lconvert_oom_error(data);
|
|
d_size = width * height * 3 / 2;
|
|
} else {
|
|
d = dest;
|
|
d_size = dest_size;
|
|
}
|
|
|
|
if (dest_pix_fmt == V4L2_PIX_FMT_YVU420)
|
|
yvu = 1;
|
|
|
|
switch (src_pix_fmt) {
|
|
case V4L2_PIX_FMT_SPCA501:
|
|
v4lconvert_spca501_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_SPCA505:
|
|
v4lconvert_spca505_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_SPCA508:
|
|
v4lconvert_spca508_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_CIT_YYVYUY:
|
|
v4lconvert_cit_yyvyuy_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_KONICA420:
|
|
v4lconvert_konica_yuv420_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_M420:
|
|
v4lconvert_m420_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_SN9C20X_I420:
|
|
v4lconvert_sn9c20x_to_yuv420(src, d, width, height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_CPIA1:
|
|
if (v4lconvert_cpia1_to_yuv420(data, src, src_size, d,
|
|
width, height, yvu)) {
|
|
/* Corrupt frame, better get another one */
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
break;
|
|
#ifdef HAVE_LIBV4LCONVERT_HELPERS
|
|
case V4L2_PIX_FMT_OV511:
|
|
if (v4lconvert_helper_decompress(data, LIBV4LCONVERT_PRIV_DIR "/ov511-decomp",
|
|
src, src_size, d, d_size, width, height, yvu)) {
|
|
/* Corrupt frame, better get another one */
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
break;
|
|
case V4L2_PIX_FMT_OV518:
|
|
if (v4lconvert_helper_decompress(data, LIBV4LCONVERT_PRIV_DIR "/ov518-decomp",
|
|
src, src_size, d, d_size, width, height, yvu)) {
|
|
/* Corrupt frame, better get another one */
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
break;
|
|
#endif
|
|
}
|
|
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_yuv420_to_rgb24(data->convert_pixfmt_buf, dest, width,
|
|
height, yvu);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_yuv420_to_bgr24(data->convert_pixfmt_buf, dest, width,
|
|
height, yvu);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* Conexant cx2341x raw video macroblock format */
|
|
case V4L2_PIX_FMT_HM12:
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_hm12_to_rgb24(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_hm12_to_bgr24(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_hm12_to_yuv420(src, dest, width, height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_hm12_to_yuv420(src, dest, width, height, 1);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
/* NV12 formats */
|
|
case V4L2_PIX_FMT_NV12:
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_nv12_to_rgb24(src, dest, width, height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_nv12_to_rgb24(src, dest, width, height, 1);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_nv12_to_yuv420(src, dest, width, height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_nv12_to_yuv420(src, dest, width, height, 1);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
/* compressed bayer formats */
|
|
case V4L2_PIX_FMT_SPCA561:
|
|
case V4L2_PIX_FMT_SN9C10X:
|
|
case V4L2_PIX_FMT_PAC207:
|
|
case V4L2_PIX_FMT_MR97310A:
|
|
#ifdef HAVE_JPEG
|
|
case V4L2_PIX_FMT_JL2005BCD:
|
|
#endif
|
|
case V4L2_PIX_FMT_SN9C2028:
|
|
case V4L2_PIX_FMT_SQ905C:
|
|
case V4L2_PIX_FMT_STV0680: { /* Not compressed but needs some shuffling */
|
|
unsigned char *tmpbuf;
|
|
struct v4l2_format tmpfmt = *fmt;
|
|
|
|
tmpbuf = v4lconvert_alloc_buffer(width * height,
|
|
&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
|
|
if (!tmpbuf)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
switch (src_pix_fmt) {
|
|
case V4L2_PIX_FMT_SPCA561:
|
|
v4lconvert_decode_spca561(src, tmpbuf, width, height);
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SGBRG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SN9C10X:
|
|
v4lconvert_decode_sn9c10x(src, tmpbuf, width, height);
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
case V4L2_PIX_FMT_PAC207:
|
|
if (v4lconvert_decode_pac207(data, src, src_size, tmpbuf,
|
|
width, height)) {
|
|
/* Corrupt frame, better get another one */
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
case V4L2_PIX_FMT_MR97310A:
|
|
if (v4lconvert_decode_mr97310a(data, src, src_size, tmpbuf,
|
|
width, height)) {
|
|
/* Corrupt frame, better get another one */
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
#ifdef HAVE_JPEG
|
|
case V4L2_PIX_FMT_JL2005BCD:
|
|
if (v4lconvert_decode_jl2005bcd(data, src, src_size,
|
|
tmpbuf,
|
|
width, height)) {
|
|
/* Corrupt frame, better get another one */
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
|
|
break;
|
|
#endif
|
|
case V4L2_PIX_FMT_SN9C2028:
|
|
v4lconvert_decode_sn9c2028(src, tmpbuf, width, height);
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
case V4L2_PIX_FMT_SQ905C:
|
|
v4lconvert_decode_sq905c(src, tmpbuf, width, height);
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
|
|
break;
|
|
case V4L2_PIX_FMT_STV0680:
|
|
v4lconvert_decode_stv0680(src, tmpbuf, width, height);
|
|
tmpfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SRGGB8;
|
|
break;
|
|
}
|
|
/* Do processing on the tmp buffer, because doing it on bayer data is
|
|
cheaper, and bayer == rgb and our dest_fmt may be yuv */
|
|
tmpfmt.fmt.pix.bytesperline = width;
|
|
tmpfmt.fmt.pix.sizeimage = width * height;
|
|
v4lprocessing_processing(data->processing, tmpbuf, &tmpfmt);
|
|
/* Deliberate fall through to raw bayer fmt code! */
|
|
src_pix_fmt = tmpfmt.fmt.pix.pixelformat;
|
|
src = tmpbuf;
|
|
src_size = width * height;
|
|
/* fall through */
|
|
}
|
|
|
|
/* Raw bayer formats */
|
|
case V4L2_PIX_FMT_SBGGR10P:
|
|
case V4L2_PIX_FMT_SGBRG10P:
|
|
case V4L2_PIX_FMT_SGRBG10P:
|
|
case V4L2_PIX_FMT_SRGGB10P: {
|
|
int b10format = 1;
|
|
|
|
switch (src_pix_fmt) {
|
|
case V4L2_PIX_FMT_SBGGR10P:
|
|
src_pix_fmt = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
case V4L2_PIX_FMT_SGBRG10P:
|
|
src_pix_fmt = V4L2_PIX_FMT_SGBRG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SGRBG10P:
|
|
src_pix_fmt = V4L2_PIX_FMT_SGRBG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SRGGB10P:
|
|
src_pix_fmt = V4L2_PIX_FMT_SRGGB8;
|
|
break;
|
|
default:
|
|
b10format = 0;
|
|
break;
|
|
}
|
|
|
|
if (b10format) {
|
|
if (src_size < ((width * height * 10)/8)) {
|
|
V4LCONVERT_ERR
|
|
("short raw bayer10 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
break;
|
|
}
|
|
v4lconvert_bayer10p_to_bayer8(src, src, width, height);
|
|
bytesperline = width;
|
|
}
|
|
}
|
|
|
|
case V4L2_PIX_FMT_SBGGR10:
|
|
case V4L2_PIX_FMT_SGBRG10:
|
|
case V4L2_PIX_FMT_SGRBG10:
|
|
case V4L2_PIX_FMT_SRGGB10: {
|
|
int b10format = 1;
|
|
|
|
switch (src_pix_fmt) {
|
|
case V4L2_PIX_FMT_SBGGR10:
|
|
src_pix_fmt = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
case V4L2_PIX_FMT_SGBRG10:
|
|
src_pix_fmt = V4L2_PIX_FMT_SGBRG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SGRBG10:
|
|
src_pix_fmt = V4L2_PIX_FMT_SGRBG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SRGGB10:
|
|
src_pix_fmt = V4L2_PIX_FMT_SRGGB8;
|
|
break;
|
|
default:
|
|
b10format = 0;
|
|
break;
|
|
}
|
|
|
|
if (b10format) {
|
|
if (src_size < (width * height * 2)) {
|
|
V4LCONVERT_ERR
|
|
("short raw bayer10 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
break;
|
|
}
|
|
v4lconvert_bayer10_to_bayer8(src, src, width, height);
|
|
bytesperline = width;
|
|
}
|
|
}
|
|
|
|
case V4L2_PIX_FMT_SBGGR16:
|
|
case V4L2_PIX_FMT_SGBRG16:
|
|
case V4L2_PIX_FMT_SGRBG16:
|
|
case V4L2_PIX_FMT_SRGGB16: {
|
|
int b16format = 1;
|
|
|
|
switch (src_pix_fmt) {
|
|
case V4L2_PIX_FMT_SBGGR16:
|
|
src_pix_fmt = V4L2_PIX_FMT_SBGGR8;
|
|
break;
|
|
case V4L2_PIX_FMT_SGBRG16:
|
|
src_pix_fmt = V4L2_PIX_FMT_SGBRG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SGRBG16:
|
|
src_pix_fmt = V4L2_PIX_FMT_SGRBG8;
|
|
break;
|
|
case V4L2_PIX_FMT_SRGGB16:
|
|
src_pix_fmt = V4L2_PIX_FMT_SRGGB8;
|
|
break;
|
|
default:
|
|
b16format = 0;
|
|
break;
|
|
}
|
|
|
|
if (b16format) {
|
|
if (src_size < ((width * height * 2))) {
|
|
V4LCONVERT_ERR
|
|
("short raw bayer16 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
break;
|
|
}
|
|
v4lconvert_bayer16_to_bayer8(src, src, width, height);
|
|
bytesperline = width;
|
|
}
|
|
}
|
|
|
|
/* Fall-through*/
|
|
case V4L2_PIX_FMT_SBGGR8:
|
|
case V4L2_PIX_FMT_SGBRG8:
|
|
case V4L2_PIX_FMT_SGRBG8:
|
|
case V4L2_PIX_FMT_SRGGB8:
|
|
if (src_size < (width * height)) {
|
|
V4LCONVERT_ERR("short raw bayer data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_bayer_to_rgb24(src, dest, width, height, bytesperline, src_pix_fmt);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_bayer_to_bgr24(src, dest, width, height, bytesperline, src_pix_fmt);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_bayer_to_yuv420(src, dest, width, height, bytesperline, src_pix_fmt, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_bayer_to_yuv420(src, dest, width, height, bytesperline, src_pix_fmt, 1);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_SE401: {
|
|
unsigned char *d = NULL;
|
|
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
d = dest;
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
d = v4lconvert_alloc_buffer(width * height * 3,
|
|
&data->convert_pixfmt_buf,
|
|
&data->convert_pixfmt_buf_size);
|
|
if (!d)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
|
|
v4lconvert_fixup_fmt(fmt);
|
|
break;
|
|
default:
|
|
V4LCONVERT_ERR("Unknown destination format in conversion\n");
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
result = v4lconvert_se401_to_rgb24(data, src, src_size, d,
|
|
width, height);
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_swap_rgb(d, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 0, 3);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 1, 3);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case V4L2_PIX_FMT_Y16:
|
|
case V4L2_PIX_FMT_Y16_BE:
|
|
if (src_size < (width * height * 2)) {
|
|
V4LCONVERT_ERR("short y16 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_y16_to_rgb24(src, dest, width, height,
|
|
src_pix_fmt == V4L2_PIX_FMT_Y16);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_y16_to_yuv420(src, dest, fmt,
|
|
src_pix_fmt == V4L2_PIX_FMT_Y16);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_GREY:
|
|
case V4L2_PIX_FMT_Y4:
|
|
case V4L2_PIX_FMT_Y6:
|
|
if (src_size < (width * height)) {
|
|
V4LCONVERT_ERR("short grey data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_grey_to_rgb24(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_grey_to_yuv420(src, dest, fmt);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_Y10BPACK:
|
|
if (src_size < (width * height * 10 / 8)) {
|
|
V4LCONVERT_ERR("short y10b data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
result = v4lconvert_y10b_to_rgb24(data, src, dest,
|
|
width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
result = v4lconvert_y10b_to_yuv420(data, src, dest,
|
|
width, height);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_RGB565:
|
|
if (src_size < (width * height * 2)) {
|
|
V4LCONVERT_ERR("short rgb565 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_rgb565_to_rgb24(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_rgb565_to_bgr24(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_rgb565_to_yuv420(src, dest, fmt, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_rgb565_to_yuv420(src, dest, fmt, 1);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_RGB24:
|
|
if (src_size < (width * height * 3)) {
|
|
V4LCONVERT_ERR("short rgb24 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
memcpy(dest, src, width * height * 3);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_swap_rgb(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 3);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 3);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_BGR24:
|
|
if (src_size < (width * height * 3)) {
|
|
V4LCONVERT_ERR("short bgr24 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_swap_rgb(src, dest, width, height);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
memcpy(dest, src, width * height * 3);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0, 3);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1, 3);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_RGB32:
|
|
case V4L2_PIX_FMT_XRGB32:
|
|
case V4L2_PIX_FMT_ARGB32:
|
|
if (src_size < (width * height * 4)) {
|
|
V4LCONVERT_ERR("short rgb32 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
src++;
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_rgb32_to_rgb24(src, dest, width, height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_rgb32_to_rgb24(src, dest, width, height, 1);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 4);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 4);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_BGR32:
|
|
case V4L2_PIX_FMT_XBGR32:
|
|
case V4L2_PIX_FMT_ABGR32:
|
|
if (src_size < (width * height * 4)) {
|
|
V4LCONVERT_ERR("short bgr32 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_rgb32_to_rgb24(src, dest, width, height, 1);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_rgb32_to_rgb24(src, dest, width, height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0, 4);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1, 4);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_YUV420:
|
|
if (src_size < (width * height * 3 / 2)) {
|
|
V4LCONVERT_ERR("short yuv420 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_yuv420_to_rgb24(src, dest, width,
|
|
height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_yuv420_to_bgr24(src, dest, width,
|
|
height, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
memcpy(dest, src, width * height * 3 / 2);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_swap_uv(src, dest, fmt);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_YVU420:
|
|
if (src_size < (width * height * 3 / 2)) {
|
|
V4LCONVERT_ERR("short yvu420 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_yuv420_to_rgb24(src, dest, width,
|
|
height, 1);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_yuv420_to_bgr24(src, dest, width,
|
|
height, 1);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_swap_uv(src, dest, fmt);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
memcpy(dest, src, width * height * 3 / 2);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_NV16: {
|
|
unsigned char *tmpbuf;
|
|
|
|
tmpbuf = v4lconvert_alloc_buffer(width * height * 2,
|
|
&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
|
|
if (!tmpbuf)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
v4lconvert_nv16_to_yuyv(src, tmpbuf, width, height);
|
|
src_pix_fmt = V4L2_PIX_FMT_YUYV;
|
|
src = tmpbuf;
|
|
bytesperline = bytesperline * 2;
|
|
/* fall through */
|
|
}
|
|
case V4L2_PIX_FMT_YUYV:
|
|
if (src_size < (width * height * 2)) {
|
|
V4LCONVERT_ERR("short yuyv data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_yuyv_to_rgb24(src, dest, width, height, bytesperline);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_yuyv_to_bgr24(src, dest, width, height, bytesperline);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_yuyv_to_yuv420(src, dest, width, height, bytesperline, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_yuyv_to_yuv420(src, dest, width, height, bytesperline, 1);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_NV61: {
|
|
unsigned char *tmpbuf;
|
|
|
|
tmpbuf = v4lconvert_alloc_buffer(width * height * 2,
|
|
&data->convert_pixfmt_buf, &data->convert_pixfmt_buf_size);
|
|
if (!tmpbuf)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
/* Note NV61 is NV16 with U and V swapped so this becomes yvyu. */
|
|
v4lconvert_nv16_to_yuyv(src, tmpbuf, width, height);
|
|
src_pix_fmt = V4L2_PIX_FMT_YVYU;
|
|
src = tmpbuf;
|
|
bytesperline = bytesperline * 2;
|
|
/* fall through */
|
|
}
|
|
case V4L2_PIX_FMT_YVYU:
|
|
if (src_size < (width * height * 2)) {
|
|
V4LCONVERT_ERR("short yvyu data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_yvyu_to_rgb24(src, dest, width, height, bytesperline);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_yvyu_to_bgr24(src, dest, width, height, bytesperline);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
/* Note we use yuyv_to_yuv420 not v4lconvert_yvyu_to_yuv420,
|
|
with the last argument reversed to make it have as we want */
|
|
v4lconvert_yuyv_to_yuv420(src, dest, width, height, bytesperline, 1);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_yuyv_to_yuv420(src, dest, width, height, bytesperline, 0);
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_UYVY:
|
|
if (src_size < (width * height * 2)) {
|
|
V4LCONVERT_ERR("short uyvy data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_uyvy_to_rgb24(src, dest, width, height, bytesperline);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_uyvy_to_bgr24(src, dest, width, height, bytesperline);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_uyvy_to_yuv420(src, dest, width, height, bytesperline, 0);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_uyvy_to_yuv420(src, dest, width, height, bytesperline, 1);
|
|
break;
|
|
}
|
|
break;
|
|
case V4L2_PIX_FMT_HSV24:
|
|
if (src_size < (width * height * 3)) {
|
|
V4LCONVERT_ERR("short hsv24 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 0,
|
|
24, fmt->fmt.pix.hsv_enc);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 1,
|
|
24, fmt->fmt.pix.hsv_enc);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 0,
|
|
24, fmt->fmt.pix.hsv_enc);
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 3);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 0,
|
|
24, fmt->fmt.pix.hsv_enc);
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 3);
|
|
break;
|
|
}
|
|
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_HSV32:
|
|
if (src_size < (width * height * 4)) {
|
|
V4LCONVERT_ERR("short hsv32 data frame\n");
|
|
errno = EPIPE;
|
|
result = -1;
|
|
}
|
|
switch (dest_pix_fmt) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 0,
|
|
32, fmt->fmt.pix.hsv_enc);
|
|
break;
|
|
case V4L2_PIX_FMT_BGR24:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 1,
|
|
32, fmt->fmt.pix.hsv_enc);
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 0,
|
|
32, fmt->fmt.pix.hsv_enc);
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 3);
|
|
break;
|
|
case V4L2_PIX_FMT_YVU420:
|
|
v4lconvert_hsv_to_rgb24(src, dest, width, height, 0,
|
|
32, fmt->fmt.pix.hsv_enc);
|
|
v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 3);
|
|
break;
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
V4LCONVERT_ERR("Unknown src format in conversion\n");
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
fmt->fmt.pix.pixelformat = dest_pix_fmt;
|
|
v4lconvert_fixup_fmt(fmt);
|
|
|
|
return result;
|
|
}
|
|
|
|
int v4lconvert_convert(struct v4lconvert_data *data,
|
|
const struct v4l2_format *src_fmt, /* in */
|
|
const struct v4l2_format *dest_fmt, /* in */
|
|
unsigned char *src, int src_size, unsigned char *dest, int dest_size)
|
|
{
|
|
int res, dest_needed, temp_needed, processing, convert = 0;
|
|
int rotate90, vflip, hflip, crop;
|
|
unsigned char *convert1_dest = dest;
|
|
int convert1_dest_size = dest_size;
|
|
unsigned char *convert2_src = src, *convert2_dest = dest;
|
|
int convert2_dest_size = dest_size;
|
|
unsigned char *rotate90_src = src, *rotate90_dest = dest;
|
|
unsigned char *flip_src = src, *flip_dest = dest;
|
|
unsigned char *crop_src = src;
|
|
struct v4l2_format my_src_fmt = *src_fmt;
|
|
struct v4l2_format my_dest_fmt = *dest_fmt;
|
|
|
|
processing = v4lprocessing_pre_processing(data->processing);
|
|
rotate90 = data->control_flags & V4LCONTROL_ROTATED_90_JPEG;
|
|
hflip = v4lcontrol_get_ctrl(data->control, V4LCONTROL_HFLIP);
|
|
vflip = v4lcontrol_get_ctrl(data->control, V4LCONTROL_VFLIP);
|
|
crop = my_dest_fmt.fmt.pix.width != my_src_fmt.fmt.pix.width ||
|
|
my_dest_fmt.fmt.pix.height != my_src_fmt.fmt.pix.height;
|
|
|
|
if (/* If no conversion/processing is needed */
|
|
(src_fmt->fmt.pix.pixelformat == dest_fmt->fmt.pix.pixelformat &&
|
|
!processing && !rotate90 && !hflip && !vflip && !crop) ||
|
|
/* or if we should do processing/rotating/flipping but the app tries to
|
|
use the native cam format, we just return an unprocessed frame copy */
|
|
!v4lconvert_supported_dst_format(dest_fmt->fmt.pix.pixelformat)) {
|
|
int to_copy = MIN(dest_size, src_size);
|
|
memcpy(dest, src, to_copy);
|
|
return to_copy;
|
|
}
|
|
|
|
/* sanity check, is the dest buffer large enough? */
|
|
switch (my_dest_fmt.fmt.pix.pixelformat) {
|
|
case V4L2_PIX_FMT_RGB24:
|
|
case V4L2_PIX_FMT_BGR24:
|
|
dest_needed = my_dest_fmt.fmt.pix.width * my_dest_fmt.fmt.pix.height * 3;
|
|
temp_needed = my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3;
|
|
break;
|
|
case V4L2_PIX_FMT_YUV420:
|
|
case V4L2_PIX_FMT_YVU420:
|
|
dest_needed =
|
|
my_dest_fmt.fmt.pix.width * my_dest_fmt.fmt.pix.height * 3 / 2;
|
|
temp_needed =
|
|
my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3 / 2;
|
|
break;
|
|
default:
|
|
V4LCONVERT_ERR("Unknown dest format in conversion\n");
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
if (dest_size < dest_needed) {
|
|
V4LCONVERT_ERR("destination buffer too small (%d < %d)\n",
|
|
dest_size, dest_needed);
|
|
errno = EFAULT;
|
|
return -1;
|
|
}
|
|
|
|
|
|
/* Sometimes we need foo -> rgb -> bar as video processing (whitebalance,
|
|
etc.) can only be done on rgb data */
|
|
if (processing && v4lconvert_processing_needs_double_conversion(
|
|
my_src_fmt.fmt.pix.pixelformat,
|
|
my_dest_fmt.fmt.pix.pixelformat))
|
|
convert = 2;
|
|
else if (my_dest_fmt.fmt.pix.pixelformat !=
|
|
my_src_fmt.fmt.pix.pixelformat ||
|
|
/* Special case if we do not need to do conversion, but we
|
|
are not doing any other step involving copying either,
|
|
force going through convert_pixfmt to copy the data from
|
|
source to dest */
|
|
(!rotate90 && !hflip && !vflip && !crop))
|
|
convert = 1;
|
|
|
|
/* convert_pixfmt (only if convert == 2) -> processing -> convert_pixfmt ->
|
|
rotate -> flip -> crop, all steps are optional */
|
|
if (convert == 2) {
|
|
convert1_dest = v4lconvert_alloc_buffer(
|
|
my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3,
|
|
&data->convert1_buf, &data->convert1_buf_size);
|
|
if (!convert1_dest)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
convert1_dest_size =
|
|
my_src_fmt.fmt.pix.width * my_src_fmt.fmt.pix.height * 3;
|
|
convert2_src = convert1_dest;
|
|
}
|
|
|
|
if (convert && (rotate90 || hflip || vflip || crop)) {
|
|
convert2_dest = v4lconvert_alloc_buffer(temp_needed,
|
|
&data->convert2_buf, &data->convert2_buf_size);
|
|
if (!convert2_dest)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
convert2_dest_size = temp_needed;
|
|
rotate90_src = flip_src = crop_src = convert2_dest;
|
|
}
|
|
|
|
if (rotate90 && (hflip || vflip || crop)) {
|
|
rotate90_dest = v4lconvert_alloc_buffer(temp_needed,
|
|
&data->rotate90_buf, &data->rotate90_buf_size);
|
|
if (!rotate90_dest)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
flip_src = crop_src = rotate90_dest;
|
|
}
|
|
|
|
if ((vflip || hflip) && crop) {
|
|
flip_dest = v4lconvert_alloc_buffer(temp_needed, &data->flip_buf,
|
|
&data->flip_buf_size);
|
|
if (!flip_dest)
|
|
return v4lconvert_oom_error(data);
|
|
|
|
crop_src = flip_dest;
|
|
}
|
|
|
|
/* Done setting sources / dest and allocating intermediate buffers,
|
|
real conversion / processing / ... starts here. */
|
|
if (convert == 2) {
|
|
res = v4lconvert_convert_pixfmt(data, src, src_size,
|
|
convert1_dest, convert1_dest_size,
|
|
&my_src_fmt,
|
|
V4L2_PIX_FMT_RGB24);
|
|
if (res)
|
|
return res;
|
|
|
|
src_size = my_src_fmt.fmt.pix.sizeimage;
|
|
}
|
|
|
|
if (processing)
|
|
v4lprocessing_processing(data->processing, convert2_src, &my_src_fmt);
|
|
|
|
if (convert) {
|
|
res = v4lconvert_convert_pixfmt(data, convert2_src, src_size,
|
|
convert2_dest, convert2_dest_size,
|
|
&my_src_fmt,
|
|
my_dest_fmt.fmt.pix.pixelformat);
|
|
if (res)
|
|
return res;
|
|
|
|
src_size = my_src_fmt.fmt.pix.sizeimage;
|
|
|
|
/* We call processing here again in case the source format was not
|
|
rgb, but the dest is. v4lprocessing checks it self it only actually
|
|
does the processing once per frame. */
|
|
if (processing)
|
|
v4lprocessing_processing(data->processing, convert2_dest, &my_src_fmt);
|
|
}
|
|
|
|
if (rotate90)
|
|
v4lconvert_rotate90(rotate90_src, rotate90_dest, &my_src_fmt);
|
|
|
|
if (hflip || vflip)
|
|
v4lconvert_flip(flip_src, flip_dest, &my_src_fmt, hflip, vflip);
|
|
|
|
if (crop)
|
|
v4lconvert_crop(crop_src, dest, &my_src_fmt, &my_dest_fmt);
|
|
|
|
return dest_needed;
|
|
}
|
|
|
|
const char *v4lconvert_get_error_message(struct v4lconvert_data *data)
|
|
{
|
|
return data->error_msg;
|
|
}
|
|
|
|
static void v4lconvert_get_framesizes(struct v4lconvert_data *data,
|
|
unsigned int pixelformat, int index)
|
|
{
|
|
unsigned int i, j, match;
|
|
struct v4l2_frmsizeenum frmsize = { .pixel_format = pixelformat };
|
|
|
|
for (i = 0; ; i++) {
|
|
frmsize.index = i;
|
|
if (data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_ENUM_FRAMESIZES, &frmsize))
|
|
break;
|
|
|
|
/* We got a framesize, check we don't have the same one already */
|
|
match = 0;
|
|
for (j = 0; j < data->no_framesizes; j++) {
|
|
if (frmsize.type != data->framesizes[j].type)
|
|
continue;
|
|
|
|
switch (frmsize.type) {
|
|
case V4L2_FRMSIZE_TYPE_DISCRETE:
|
|
if (!memcmp(&frmsize.discrete, &data->framesizes[j].discrete,
|
|
sizeof(frmsize.discrete)))
|
|
match = 1;
|
|
break;
|
|
case V4L2_FRMSIZE_TYPE_CONTINUOUS:
|
|
case V4L2_FRMSIZE_TYPE_STEPWISE:
|
|
if (!memcmp(&frmsize.stepwise, &data->framesizes[j].stepwise,
|
|
sizeof(frmsize.stepwise)))
|
|
match = 1;
|
|
break;
|
|
}
|
|
if (match)
|
|
break;
|
|
}
|
|
/* Add this framesize if it is not already in our list */
|
|
if (!match) {
|
|
if (data->no_framesizes == V4LCONVERT_MAX_FRAMESIZES) {
|
|
fprintf(stderr, "libv4lconvert: warning more framesizes than I can handle!\n");
|
|
return;
|
|
}
|
|
data->framesizes[data->no_framesizes].type = frmsize.type;
|
|
data->framesize_supported_src_formats[data->no_framesizes] = 1ULL << index;
|
|
|
|
switch (frmsize.type) {
|
|
case V4L2_FRMSIZE_TYPE_DISCRETE:
|
|
data->framesizes[data->no_framesizes].discrete = frmsize.discrete;
|
|
break;
|
|
case V4L2_FRMSIZE_TYPE_CONTINUOUS:
|
|
case V4L2_FRMSIZE_TYPE_STEPWISE:
|
|
data->framesizes[data->no_framesizes].stepwise = frmsize.stepwise;
|
|
break;
|
|
}
|
|
data->no_framesizes++;
|
|
} else {
|
|
data->framesize_supported_src_formats[j] |= 1ULL << index;
|
|
}
|
|
}
|
|
}
|
|
|
|
int v4lconvert_enum_framesizes(struct v4lconvert_data *data,
|
|
struct v4l2_frmsizeenum *frmsize)
|
|
{
|
|
if (!v4lconvert_supported_dst_format(frmsize->pixel_format)) {
|
|
if (v4lconvert_supported_dst_fmt_only(data)) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
return data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_ENUM_FRAMESIZES, frmsize);
|
|
}
|
|
|
|
if (frmsize->index >= data->no_framesizes) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
frmsize->type = data->framesizes[frmsize->index].type;
|
|
memset(frmsize->reserved, 0, sizeof(frmsize->reserved));
|
|
switch (frmsize->type) {
|
|
case V4L2_FRMSIZE_TYPE_DISCRETE:
|
|
frmsize->discrete = data->framesizes[frmsize->index].discrete;
|
|
/* Apply the same rounding algorithm as v4lconvert_try_format */
|
|
frmsize->discrete.width &= ~7;
|
|
frmsize->discrete.height &= ~1;
|
|
break;
|
|
case V4L2_FRMSIZE_TYPE_CONTINUOUS:
|
|
case V4L2_FRMSIZE_TYPE_STEPWISE:
|
|
frmsize->stepwise = data->framesizes[frmsize->index].stepwise;
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int v4lconvert_enum_frameintervals(struct v4lconvert_data *data,
|
|
struct v4l2_frmivalenum *frmival)
|
|
{
|
|
int res;
|
|
struct v4l2_format src_fmt, dest_fmt;
|
|
|
|
if (!v4lconvert_supported_dst_format(frmival->pixel_format)) {
|
|
if (v4lconvert_supported_dst_fmt_only(data)) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
res = data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_ENUM_FRAMEINTERVALS, frmival);
|
|
if (res)
|
|
V4LCONVERT_ERR("%s\n", strerror(errno));
|
|
return res;
|
|
}
|
|
|
|
/* Check which format we will be using to convert to frmival->pixel_format */
|
|
memset(&dest_fmt, 0, sizeof(dest_fmt));
|
|
dest_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
dest_fmt.fmt.pix.pixelformat = frmival->pixel_format;
|
|
dest_fmt.fmt.pix.width = frmival->width;
|
|
dest_fmt.fmt.pix.height = frmival->height;
|
|
res = v4lconvert_try_format(data, &dest_fmt, &src_fmt);
|
|
if (res) {
|
|
V4LCONVERT_ERR("trying format: %s\n", strerror(errno));
|
|
return res;
|
|
}
|
|
|
|
/* Check the requested format is supported exactly as requested */
|
|
if (dest_fmt.fmt.pix.pixelformat != frmival->pixel_format ||
|
|
dest_fmt.fmt.pix.width != frmival->width ||
|
|
dest_fmt.fmt.pix.height != frmival->height) {
|
|
int frmival_pixformat = frmival->pixel_format;
|
|
int dest_pixformat = dest_fmt.fmt.pix.pixelformat;
|
|
|
|
V4LCONVERT_ERR("Could not find matching framesize for: %c%c%c%c %dx%d "
|
|
"closest match: %c%c%c%c %dx%d\n",
|
|
frmival_pixformat & 0xff,
|
|
(frmival_pixformat >> 8) & 0xff,
|
|
(frmival_pixformat >> 16) & 0xff,
|
|
frmival_pixformat >> 24,
|
|
frmival->width, frmival->height,
|
|
dest_pixformat & 0xff,
|
|
(dest_pixformat >> 8) & 0xff,
|
|
(dest_pixformat >> 16) & 0xff,
|
|
dest_pixformat >> 24,
|
|
dest_fmt.fmt.pix.width , dest_fmt.fmt.pix.height);
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
/* Enumerate the frameintervals of the source format we will be using */
|
|
frmival->pixel_format = src_fmt.fmt.pix.pixelformat;
|
|
frmival->width = src_fmt.fmt.pix.width;
|
|
frmival->height = src_fmt.fmt.pix.height;
|
|
res = data->dev_ops->ioctl(data->dev_ops_priv, data->fd,
|
|
VIDIOC_ENUM_FRAMEINTERVALS, frmival);
|
|
if (res) {
|
|
int dest_pixfmt = dest_fmt.fmt.pix.pixelformat;
|
|
int src_pixfmt = src_fmt.fmt.pix.pixelformat;
|
|
|
|
V4LCONVERT_ERR("Could not enum frameival index: %d for: %c%c%c%c %dx%d "
|
|
"using src: %c%c%c%c %dx%d, error: %s\n",
|
|
frmival->index,
|
|
dest_pixfmt & 0xff,
|
|
(dest_pixfmt >> 8) & 0xff,
|
|
(dest_pixfmt >> 16) & 0xff,
|
|
dest_pixfmt >> 24,
|
|
dest_fmt.fmt.pix.width , dest_fmt.fmt.pix.height,
|
|
src_pixfmt & 0xff,
|
|
(src_pixfmt >> 8) & 0xff,
|
|
(src_pixfmt >> 16) & 0xff,
|
|
src_pixfmt >> 24,
|
|
src_fmt.fmt.pix.width, src_fmt.fmt.pix.height, strerror(errno));
|
|
}
|
|
|
|
/* Restore the requested format in the frmival struct */
|
|
frmival->pixel_format = dest_fmt.fmt.pix.pixelformat;
|
|
frmival->width = dest_fmt.fmt.pix.width;
|
|
frmival->height = dest_fmt.fmt.pix.height;
|
|
|
|
return res;
|
|
}
|
|
|
|
int v4lconvert_vidioc_queryctrl(struct v4lconvert_data *data, void *arg)
|
|
{
|
|
return v4lcontrol_vidioc_queryctrl(data->control, arg);
|
|
}
|
|
|
|
int v4lconvert_vidioc_g_ctrl(struct v4lconvert_data *data, void *arg)
|
|
{
|
|
return v4lcontrol_vidioc_g_ctrl(data->control, arg);
|
|
}
|
|
|
|
int v4lconvert_vidioc_s_ctrl(struct v4lconvert_data *data, void *arg)
|
|
{
|
|
return v4lcontrol_vidioc_s_ctrl(data->control, arg);
|
|
}
|
|
|
|
int v4lconvert_vidioc_g_ext_ctrls(struct v4lconvert_data *data, void *arg)
|
|
{
|
|
return v4lcontrol_vidioc_g_ext_ctrls(data->control, arg);
|
|
}
|
|
|
|
int v4lconvert_vidioc_try_ext_ctrls(struct v4lconvert_data *data, void *arg)
|
|
{
|
|
return v4lcontrol_vidioc_try_ext_ctrls(data->control, arg);
|
|
}
|
|
|
|
int v4lconvert_vidioc_s_ext_ctrls(struct v4lconvert_data *data, void *arg)
|
|
{
|
|
return v4lcontrol_vidioc_s_ext_ctrls(data->control, arg);
|
|
}
|
|
|
|
int v4lconvert_get_fps(struct v4lconvert_data *data)
|
|
{
|
|
return data->fps;
|
|
}
|
|
|
|
void v4lconvert_set_fps(struct v4lconvert_data *data, int fps)
|
|
{
|
|
data->fps = fps;
|
|
}
|