Video4Linux2 part 5a: colors and formats

V4L2+单色USB摄像头编程实践3:读取MJPG格式图像 阅读详情

This is the fifth article in the irregular LWN series on writing videodrivers for Linux. Those who have not yet read the introductory article maywant to start there.

Before any application can work with a video device, it must come to anunderstanding with the driver about how video data will be formatted. Thisnegotiation can be a rather complex process, resulting from the facts that(1) video hardware varies widely in the formats it can handle, and(2) performing format transformations in the kernel is frowned upon.So the application must be able to find out what formats are supported bythe hardware and set up a configuration which is workable for everybodyinvolved. This article will cover the basics of how formats are described;the next installment will get into the API implemented by V4L2 drivers tonegotiate formats with applications.

 

Colorspaces

A colorspace is, in broad terms, the coordinate system used todescribe colors. There are several of them defined by the V4L2specification, but only two are used in any broad way. They are:

 

  • V4L2_COLORSPACE_SRGB. The [red, green, blue] tuples familiar to many developers are covered under this colorspace. They provide a simple intensity value for each of the primary colors which, when mixed together, create the illusion of a wide range of colors. There are a number of ways of representing RGB values, as we will see below.

    This colorspace also covers the set of YUV and YCbCr representations. This representation derives from the need for early color television signals to be displayable on monochrome TV sets. So the Y (or "luminance") value is a simple brightness value; when displayed alone, it yields a grayscale image. The U and V (or Cb and Cr) "chrominance" values describe the blue and red components of the color; green can be derived by subtracting those components from the luminance. Conversion between YUV and RGB is not entirely straightforward, however; there are several formulas to choose from.

    Note that YUV and YCbCr are not exactly the same thing, though the terms are often used interchangeably.

     

  • V4L2_COLORSPACE_SMPTE170M is for analog color representations used in NTSC or PAL television signals. TV tuners will often produce data in this colorspace.

Quite a few other colorspaces exist; most of them are variants oftelevision-related standards. See this page from the V4L2specification for the full list.

 

Packed and planar

As we have seen, pixel values are expressed as tuples, usually consistingof RGB or YUV values. There are two commonly-used ways of organizing thosetuples into an image:

 

  • Packed formats store all of the values for one pixel together in memory.

     

  • Planar formats separate each component out into a separate array. Thus a planar YUV format will have all of the Y values stored contiguously in one array, the U values in another, and the V values in a third. The planes are usually stored contiguously in a single buffer, but it does not have to be that way.

Packed formats might be more commonly used, especially with RGB formats,but both types can be generated by hardware and requested by applications. If the video devicesupports both packed and planar formats, the driver should make them bothavailable to user space.

 

Fourcc codes

Color formats are described within the V4L2 API using the venerable"fourcc" code mechanism. These codes are 32-bit values, generated fromfour ASCII characters. As such, they have the advantages of being easilypassed around and being human-readable. When a color format code reads,for example, 'RGB4', there is no need to go look it up in atable.

Note that fourcc codes are used in a lot of different settings, some ofwhich predate Linux. The MPlayer application uses them internally. fourccrefers only to the coding mechanism, however, and says nothing about whichcodes are actually used - MPlayer has a translation function for convertingbetween its fourcc codes and those used by V4L2.

RGB formats

In the format descriptions shown below, bytes are always listed in memoryorder - least significant bytes first on a little-endian machine. Theleast significant bit of each byte is on the right; for each color field,the lighter-shaded bit is the most significant.

 

NamefourccByte 0Byte 1Byte2Byte 3
V4L2_PIX_FORMAT_RGB332RGB1
       
   
V4L2_PIX_FORMAT_RGB444R444
       
       
  
V4L2_PIX_FORMAT_RGB555RGB0
       
       
  
V4L2_PIX_FORMAT_RGB565RGBP
       
       
  
V4L2_PIX_FORMAT_RGB555XRGBQ
       
       
  
V4L2_PIX_FORMAT_RGB565XRGBR
       
       
  
V4L2_PIX_FORMAT_BGR24BGR3
       
       
       
 
V4L2_PIX_FORMAT_RGB24RGB3
       
       
       
 
V4L2_PIX_FORMAT_BGR32BGR4
       
       
       
       
V4L2_PIX_FORMAT_RGB32RGB4
       
       
       
       
V4L2_PIX_FORMAT_SBGGR8BA81
       
       
       
       
  
       
       
       
       

When formats with empty space (shown in gray, above) are used, applicationsmay use that space for an alpha (transparency) value.

The final format above is the "Bayer" format, which is generally somethingvery close to the real data from the sensor found in most cameras. Thereare green values for every pixel, but blue and red only for every otherpixel. Essentially, green carries the more important intensityinformation, with red and blue being interpolated across the pixels wherethey are missing. This is a pattern we will see again with the YUV formats.

 

YUV formats

The packed YUV formats will be shown first. The key for reading this tableis:

 

  •         
    = Y (intensity)
  •         
    = U (Cb)
  •         
    = V (Cr)

 

NamefourccByte 0Byte 1Byte2Byte 3
V4L2_PIX_FORMAT_GREYGREY
       
   
V4L2_PIX_FORMAT_YUYVYUYV
       
       
       
       
V4L2_PIX_FORMAT_UYVYUYVY
       
       
       
       
V4L2_PIX_FORMAT_Y41PY41P
       
       
       
       
  
       
       
       
       
  
       
       
       
       

There are several planar YUV formats in use as well. Drawing them all outdoes not help much, so we'll go with one example. The commonly-used"YUV 4:2:2" format (V4L2_PIX_FMT_YUV422, fourcc422P) uses three separate arrays. A 4x4 image would berepresented like this:

 

Y plane:
       
       
       
       
 
       
       
       
       
 
       
       
       
       
 
       
       
       
       
 
U plane:
       
       
       
       
 
       
       
       
       
 
V plane:
       
       
       
       
 
       
       
       
       

As with the Bayer format, YUV 4:2:2 has one U and one V value for everyother Y value; displaying the image requires interpolating across themissing values. The other planar YUVformats are:

 

  • V4L2_PIX_FMT_YUV420: the YUV 4:2:0 format, with one U and one V value for every four Y values. U and V must be interpolated in both the horizontal and vertical directions. The planes are stored in Y-U-V order, as with the example above.

     

  • V4L2_PIX_FMT_YVU420: like YUV 4:2:0, except that the positions of the U and V arrays are swapped.

     

  • V4L2_PIX_FMT_YUV410: A single U and V value for each sixteen Y values. The arrays are in the order Y-U-V.

     

  • V4L2_PIX_FMT_YVU410: A single U and V value for each sixteen Y values. The arrays are in the order Y-V-U.

A few other YUV formats exist, but they are rarely used; see this page for thefull list.

 

Other formats

A couple of formats which might be useful for some drivers are:

 

  • V4L2_PIX_FMT_JPEG: a vaguely-defined JPEG stream; a littlemore information can be found here.

     

  • V4L2_PIX_FMT_MPEG: an MPEG stream. There are a few variantson the MPEG stream format; controlling these streams will be discussed in afuture installment.

There are a number of other, miscellaneous formats, some of themproprietary; thispage has a list of them.

 

Describing formats

Now that we have an understanding of color formats, we can take a look athow the V4L2 API describes image formats in general. The key structurehere is struct v4l2_pix_format (defined in<linux/videodev2.h>, which contains these fields:

 

  • __u32 width: the width of the image in pixels.

     

  • __u32 height: the height of the image in pixels.

     

  • __u32 pixelformat: the fourcc code describing the image format.

     

  • enum v4l2_field field: many image sources will interlace the data - transferring all of the even scan lines first, followed by the odd lines. Real camera devices normally do not do interlacing. The V4L2 API allows the application to work with interlaced fields in a surprising number of ways. Common values include V4L2_FIELD_NONE (fields are not interlaced), V4l2_FIELD_TOP (top field only), or V4L2_FIELD_ANY (don't care). See this page for a full list.

     

  • __u32 bytesperline: the number of bytes between two adjacent scan lines. It includes any padding the device may require. For planar formats, this value describes the largest (Y) plane.

     

  • __u32 sizeimage: the size of the buffer required to hold the full image.

     

  • enum v4l2_colorspace colorspace: the colorspace being used.

All together, these parameters describe a buffer of video data in areasonably complete manner. An application can fill out av4l2_pix_format structure asking for just about any sort of formatthat a user-space developer can imagine. On the driver side, however,things have to be restrained to the formats the hardware can work with. Soevery V4L2 application must go through a negotiation process with thedriver in an attempt to arrive at an image format that is both supported bythe hardware and adequate for the application's needs. The nextinstallment in this series will describe how this negotiation works from the device driver's point of view.

 

V4L2结构体手册,Android进阶之光 Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!上面分享的字节跳动公司2021年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。Android学习PDF+学习视频+面试文档+知识点笔记【Android高级架构视频学习资源】r;__u32 id; 阅读详情

相关推荐

v4l2及gstreamer使用指南

两个命令工具分别侧重不同功能,v4l2主要用于视频采集;gstreamer则像个管家,整合所有组件完成某项功能。

平面到立体 6816

v4l2文档第五A--颜色与格式

颜色与格式这是不定期发布的关于写视频驱动程序的LWN系统文章的第五篇.没有看过介绍篇的,也许想从这里开始. 应用在可以使视频设备工作之前,它必须与驱动达成了解,知道视频数据是何种格式的。这种协商将是一个非常复杂的过程,其原因有二:1、视频硬件所支持的视频格互不相同。2、在内核的格式转换是令人难以接受的。所以应用在找出一种硬件支持的格式,并做出一种大家都可以接受的配置。这篇文章将会讲述格式

pengjin1985的专栏 1734

CORS(跨域资源共享)漏洞解决方法

最近,测试环境上的项目被360测试人员检测出来有一个CORS漏洞,以下记录下漏洞问题与解决方法。 一、低危漏洞:CORS漏洞问题 测试人员访问某个url,将请求头中的Origin字段修改为任意值,结果仍然能获得正确的响应报文,就说明有CORS漏洞。 当CORS的设置不正确时,就会带来安全问题;当响应头中的Access-Control-Allow-Origin设置为null或*时,表示信任任何域,这时候就可能引入安全问题。 修复方法是合理配置CORS,判断Origin是否合法;具体说就是不让在nginx或t

BHSZZY的博客 3万+

V4L2文档翻译(九)

RGB格式 RGB组合格式 名字 RGB组合格式 描述 此格式用来匹配PC图形帧缓存。每个像素占据8,16,24或32个位,他们都是组合像素格式,其意为在内存中所有像素数据都是相邻排列的。当使用这些格式之一时,驱动应该上报颜色空间为V4L2_COLORSPACE_SRGB。 表2.6 组合RGB图像格式 定义 码 Byte 0 Byte 1 Byte

Feed Technology 9599

6-v4l2——color and format颜色 格式

v4l2文档第五A--颜色与格式    颜色与格式这是不定期发布的关于写视频驱动程序的LWN系统文章的第五篇.没有看过介绍篇的,也许想从这里 开始。   应用在可以使视频设备工作之前,它必须与驱动达成了解,知道视频数据是何种格式的。这种协商将是一个非常复杂的过程,其原因有二:1、视频硬件所支持的视 频格互不相同。2、在内核的格式转换是令人难以接受的。所以应用在找出一种硬件支持的格式,并做出一种

当我荒废时间的时候,会有多少人在拼命 2066

内核关于色域空间的枚举

内核关于色域空间的枚举。

qq_30883899的博客 1426

V4L2结构体手册

bionic/libc/kernel/uapi/linux/videodev2.h /**************************************************************************** **************************************************************************** *** *** This header was automatically generated from ...

Android系统攻城狮 1万+

基于V4L2编程详解(二)

基于V4L2编程详解(二)   输入  视频捕获的应用首先要通过VIDIOC_ENUMINPUT 命令来枚举所有可用的输入。在V4L2层,这个调用会转换成调用一个驱动中对应的回调函数: -------------------------------------------------------------------------------------------------------

Coding never stop! 917

V4L2(二)虚拟摄像头驱动vivi深入分析

前面一篇文章中,简单分析了 V4L2 大框架,本文借助内核中的虚拟摄像头驱动 vivi 来分析一个完整的摄像头驱动程序。vivi 相对于后面要分析的 usb 摄像头驱动程序,它没有真正的硬件相关层的操作,也就是说抛开了复杂的 usb 层的相关知识,便于理解 V4L2 驱动框架,侧重于驱动和应用的交互。   前面我们提到,V4L2 的核心是 v4l2-dev.c 它向上提供统一的文件操作接口 v4

漫不经心 9205

移植ov5640摄像头到imx6ull开发板(一)

ov5640摄像头驱动是基于v4l2框架进行编写,这样应用层才能提供对应的接口函数。ov5640摄像头驱动中的主要工作:1.初始化Linux内核提供有关v4l2驱动框架的结构体2.通过i2c协议读写寄存器,配置摄像头3.提供应用层ioctl对应的函数以上便是我对ov5640摄像头驱动代码的一些理解和分析,如果存在错误,希望大家指正。

qq_55389904的博客 6802

V4L2(三)编写虚拟摄像头驱动

内核版本:linux-3.5 开发板:tiny4412 概述前面简单分析了内核中虚拟摄像头驱动 vivi 的框架与实现,本文参考 vivi 来写一个虚拟摄像头驱动,查询、设置视频格式相对简单,难点在于 vb2_buf 的处理过程。数据采集流程分析 在我的程序中,大概的数据采集流程如上图所示,启动视频采集之后,创建了一个内核线程,内核线程每30ms 唤醒一次,每一次唤醒都会尝试用 queue_

漫不经心 5327

v4l2文档之——color and format

v4l2文档第五A--颜色与格式    颜色与格式这是不定期发布的关于写视频驱动程序的LWN系统文章的第五篇.没有看过介绍篇的,也许想从这里 开始。   应用在可以使视频设备工作之前,它必须与驱动达成了解,知道视频数据是何种格式的。这种协商将是一个非常复杂的过程,其原因有二:1、视频硬件所支持的视 频格互不相同。2、在内核的格式转换是令人难以接受的。所以应用在找出一种硬件支持的格式,并做出一种

1466

YUV Colorspace

本文转自:http://softpixel.com/~cwright/programming/colorspace/yuv/ YUV Colorspace YUV colorspace is a bit unusual. The Y component determines the brightness of the color (referred to as luminance or l

蓝色污点的专栏 2005

操作系统(1):揭秘操作系统:从内核到Linux的演进之路

本文系统梳理了操作系统的基本概念、功能、发展历程与结构模型。操作系统是管理软硬件资源的核心系统软件,具备进程、存储、设备、文件管理及用户接口等五大功能,位于硬件与应用之间。其发展历经人工处理、批处理、分时与实时系统等阶段,推动了并发、并行技术进步。主流结构包括单体、模块化、分层式与微内核,现代系统如Linux采用宏内核架构,支持多任务与高可靠性。通过对比图示与实例分析,深入理解操作系统在资源管理、性能优化与系统安全中的关键作用。

2503_91800161的博客 523

Linux 文件系统管理与挂载实战

九条命令串起一条主线——

xbzb的博客 155

【项目实现】 Linux自定义shell实现

前面学了这么多知识,我们可以写一个简易的自定义myshell来复习一波,接下来来跟随小编的视角来看看吧。let's go!!!!!!!!Part1. Shell 核心原理前置知识Shell 本质就是运行在用户态的命令行解释程序,它的核心工作流程固定:打印提示符,读取用户输入一行命令;解析命令字符串,切割成 argv 参数数组;识别特殊语法:重定向> >> <;识别别名 alias,替换原始命令;

2501_93971468的博客 21

Linux文件查看与编辑:cat、less、tail、vim快速入门

零基础入门 Linux 文件查看与编辑:一文吃透 cat、less、tail、vim 四大命令。从真实排障场景出发,讲解语法、参数、实战示例与预期输出,附工具选型对比表、tail -f 与 tail -F 的区别(inode 原理)、vim 模式速查与最佳实践

m0_66083353的博客 476

Linux dmesg 工业边缘实战:内核日志过滤、持久化与故障定位

本文围绕 Linux dmesg 在工业边缘节点上的应用,介绍内核日志基础用法、时间/级别/子系统过滤、硬件/OOM/网络/USB/panic 排障、journald 与 syslog 持久化、实时监测和 sysctl 配合,适合网关与边缘设备日常运维。

ZenovaEdgeOS的博客 313
上一篇: Video4Linux2 part 4: inputs and outputs
下一篇: Video4Linux2 part 5b: format negotiation
kickxxx
博客等级 码龄16年 586粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值