一、像素格式简介
像素格式(pixel format)指像素色彩按分量的大小和排列。这种格式以每个像素所使用的总位数以及用于存储像素色彩的红、绿、蓝和 alpha 分量的位数指定。在音视频领域,常用的像素格式包括RGB格式和YUV格式,本文对RGB格式进行简介。
二、RGB格式简介
三原色光模式(RGB color model,又称RGB表色系统、RGB颜色模型、红绿蓝颜色模型,是一种加色模型,将红(Red)、绿(Green)、蓝(Blue)三原色的色光以不同的比例相加混色,以合成产生各种色彩光。
RGB颜色模型的主要目的是在电子系统中检测,表示和显示图像,比如电视和电脑,利用大脑强制视觉生理模糊化(失焦),将红绿蓝三原色子像素合成为一色彩像素,产生感知色彩(其实此真彩色并非加色法所产生的合成色彩,原因为该三原色光从来没有重叠在一起,只是人类为了“想”看到色彩,大脑强制眼睛失焦而形成。情况其实就有点像看那些 autostereograms 的立体图时,大脑与眼睛扭曲才能看到“想”看的立体影像的情况)。 红绿蓝三色模型在传统摄影中也有应用。在电子时代之前,基于人类对颜色的感知,RGB颜色模型已经有了坚实的理论支撑。
三、RGB24格式简介
RGB24是表明图像以RGB三原色,每个像素点3个字节表示的一种图像存储格式,即每像素24位编码的RGB值:使用三个8位无符号整数(0到255)表示红色、绿色和蓝色的强度。这是当前主流的标准表示方法,用于真彩色和JPEG或者TIFF等图像文件格式里的通用颜色交换。它可以产生一千六百万种颜色组合,比如:
(0, 0, 0)是黑色;(255, 255, 255)是白色;(255, 0, 0)是红色;(0, 255, 0)是绿色;(0, 0, 255)是蓝色;(255, 255, 0)是黄色;(0, 255, 255)是青色或水色;(255, 0, 255)是洋红色。
四、RGB24格式视频大小计算
由于RGB24格式每像素占3个字节。所以一张rgb图片的大小 = 图片的宽 * 图片的高 *3 byte。
一个rgb视频的大小 = 里面一张rgb图片的大小 * 该视频文件的总帧数。
下面我们用FFmpeg(我用的FFmpeg版本是5.0.3)来验证上述的公式。
首先查看FFmpeg将视频文件转为rgb文件的命令,执行命令:
ffmpeg -h full
打印ffmpeg的所有帮助信息
可以看到有一栏写道:-pix_fmts show available pixel formats
Print help / information / capabilities:
-L show license
-h topic show help
-? topic show help
-help topic show help
--help topic show help
-version show version
-buildconf show build configuration
-formats show available formats
-muxers show available muxers
-demuxers show available demuxers
-devices show available devices
-codecs show available codecs
-decoders show available decoders
-encoders show available encoders
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formats
-dispositions show available stream dispositions
-colors show available color names
-sources device list sources of the input device
-sinks device list sinks of the output device
-hwaccels show available HW acceleration methods
意思是FFmpeg命令中可以通过-pix_fmts 选项显示可用的像素格式
所以执行命令:
ffmpeg -pix_fmts
得到输出如下:
Pixel formats:
I.... = Supported Input format for conversion
.O... = Supported Output format for conversion
..H.. = Hardware accelerated format
...P. = Paletted format
....B = Bitstream format
FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL BIT_DEPTHS
-----
IO... yuv420p 3 12 8-8-8
IO... yuyv422 3 16 8-8-8
IO... rgb24 3 24 8-8-8
IO... bgr24 3 24 8-8-8
IO... yuv422p 3 16 8-8-8
IO... yuv444p 3 24 8-8-8
IO... yuv410p 3 9 8-8-8
IO... yuv411p 3 12 8-8-8
IO... gray 1 8 8
IO..B monow 1 1 1
IO..B monob 1 1 1
I..P. pal8 1 8 8
IO... yuvj420p 3 12 8-8-8
IO... yuvj422p 3 16 8-8-8
IO... yuvj444p 3 24 8-8-8
IO... uyvy422 3 16 8-8-8
..... uyyvyy411 3 12 8-8-8
IO... bgr8 3 8 3-3-2
.O..B bgr4 3 4 1-2-1
IO... bgr4_byte 3 4 1-2-1
IO... rgb8 3 8 2-3-3
.O..B rgb4 3 4 1-2-1
IO... rgb4_byte 3 4 1-2-1
IO... nv12 3 12 8-8-8
IO... nv21 3 12 8-8-8
IO... argb 4 32 8-8-8-8
IO... rgba 4 32 8-8-8-8
IO... abgr 4 32 8-8-8-8
IO... bgra 4 32 8-8-8-8
IO... gray16be 1 16 16
IO... gray16le 1 16 16
IO... yuv440p 3 16 8-8-8
IO... yuvj440p 3 16 8-8-8
IO... yuva420p 4 20 8-8-8-8
IO... rgb48be 3 48 16-16-16
IO... rgb48le 3 48 16-16-16
IO... rgb565be 3 16 5-6-5
IO... rgb565le 3 16 5-6-5
IO... rgb555be 3 15 5-5-5
IO... rgb555le 3 15 5-5-5
IO... bgr565be 3 16 5-6-5
IO... bgr565le 3 16 5-6-5
IO... bgr555be 3 15 5-5-5
IO... bgr555le 3 15 5-5-5
..H.. vaapi 0 0 0
IO... yuv420p16le 3 24 16-16-16
IO... yuv420p16be 3 24 16-16-16
IO... yuv422p16le 3 32 16-16-16
IO... yuv422p16be 3 32 16-16-16
IO... yuv444p16le 3 48 16-16-16
IO... yuv444p16be 3 48 16-16-16
..H.. dxva2_vld 0 0 0
IO... rgb444le 3 12 4-4-4
IO... rgb444be 3 12 4-4-4
IO... bgr444le 3 12 4-4-4
IO... bgr444be 3 12 4-4-4
IO... ya8 2 16 8-8
IO... bgr48be 3 48 16-16-16
IO... bgr48le 3 48 16-16-16
IO... yuv420p9be 3 13 9-9-9
IO... yuv420p9le 3 13 9-9-9
IO... yuv420p10be 3 15 10-10-10
IO... yuv420p10le 3 15 10-10-10
IO... yuv422p10be 3 20 10-10-10
IO... yuv422p10le 3 20 10-10-10
IO... yuv444p9be 3 27 9-9-9
IO... yuv444p9le 3 27 9-9-9
IO... yuv444p10be 3 30 10-10-10
IO... yuv444p10le 3 30 10-10-10
IO... yuv422p9be 3 18 9-9-9
IO... yuv422p9le 3 18 9-9-9
IO... gbrp 3 24 8-8-8
IO... gbrp9be 3 27 9-9-9
IO... gbrp9le 3 27 9-9-9
IO... gbrp10be 3 30 10-10-10
IO... gbrp10le 3 30 10-10-10
IO... gbrp16be 3 48 16-16-16
IO... gbrp16le 3 48 16-16-16
IO... yuva422p 4 24 8-8-8-8
IO... yuva444p 4 32 8-8-8-8
IO... yuva420p9be 4 22 9-9-9-9
IO... yuva420p9le 4 22 9-9-9-9
IO... yuva422p9be 4 27 9-9-9-9
IO... yuva422p9le 4 27 9-9-9-9
IO... yuva444p9be 4 36 9-9-9-9
IO... yuva444p9le 4 36 9-9-9-9
IO... yuva420p10be 4 25 10-10-10-10
IO... yuva420p10le 4 25 10-10-10-10
IO... yuva422p10be 4 30 10-10-10-10
IO... yuva422p10le 4 30 10-10-10-10
IO... yuva444p10be 4 40 10-10-10-10
IO... yuva444p10le 4 40 10-10-10-10
IO... yuva420p16be 4 40 16-16-16-16
IO... yuva420p16le 4 40 16-16-16-16
IO... yuva422p16be 4 48 16-16-16-16
IO... yuva422p16le 4 48 16-16-16-16
IO... yuva444p16be 4 64 16-16-16-16
IO... yuva444p16le 4 64 16-16-16-16
..H.. vdpau 0 0 0
IO... xyz12le 3 36 12-12-12
IO... xyz12be 3 36 12-12-12
..... nv16 3 16 8-8-8
..... nv20le 3 20 10-10-10
..... nv20be 3 20 10-10-10
IO... rgba64be 4 64 16-16-16-16
IO... rgba64le 4 64 16-16-16-16
IO... bgra64be 4 64 16-16-16-16
IO... bgra64le 4 64 16-16-16-16
IO... yvyu422 3 16 8-8-8
IO... ya16be 2 32 16-16
IO... ya16le 2 32 16-16
IO... gbrap 4 32 8-8-8-8
IO... gbrap16be 4 64 16-16-16-16
IO... gbrap16le 4 64 16-16-16-16
..H.. qsv 0 0 0
..H.. mmal 0 0 0
..H.. d3d11va_vld 0 0 0
..H.. cuda 0 0 0
IO... 0rgb 3 24 8-8-8
IO... rgb0 3 24 8-8-8
IO... 0bgr 3 24 8-8-8
IO... bgr0 3 24 8-8-8
IO... yuv420p12be 3 18 12-12-12
IO... yuv420p12le 3 18 12-12-12
IO... yuv420p14be 3 21 14-14-14
IO... yuv420p14le 3 21 14-14-14
IO... yuv422p12be 3 24 12-12-12
IO... yuv422p12le 3 24 12-12-12
IO... yuv422p14be 3 28 14-14-14
IO... yuv422p14le 3 28 14-14-14
IO... yuv444p12be 3 36 12-12-12
IO... yuv444p12le 3 36 12-12-12
IO... yuv444p14be 3 42 14-14-14
IO... yuv444p14le 3 42 14-14-14
IO... gbrp12be 3 36 12-12-12
IO... gbrp12le 3 36 12-12-12
IO... gbrp14be 3 42 14-14-14
IO... gbrp14le 3 42 14-14-14
IO... yuvj411p 3 12 8-8-8
I.... bayer_bggr8 3 8 2-4-2
I.... bayer_rggb8 3 8 2-4-2
I.... bayer_gbrg8 3 8 2-4-2
I.... bayer_grbg8 3 8 2-4-2
I.... bayer_bggr16le 3 16 4-8-4
I.... bayer_bggr16be 3 16 4-8-4
I.... bayer_rggb16le 3 16 4-8-4
I.... bayer_rggb16be 3 16 4-8-4
I.... bayer_gbrg16le 3 16 4-8-4
I.... bayer_gbrg16be 3 16 4-8-4
I.... bayer_grbg16le 3 16 4-8-4
I.... bayer_grbg16be 3 16 4-8-4
..H.. xvmc 0 0 0
IO... yuv440p10le 3 20 10-10-10
IO... yuv440p10be 3 20 10-10-10
IO... yuv440p12le 3 24 12-12-12
IO... yuv440p12be 3 24 12-12-12
IO... ayuv64le 4 64 16-16-16-16
..... ayuv64be 4 64 16-16-16-16
..H.. videotoolbox_vld 0 0 0
IO... p010le 3 15 10-10-10
IO... p010be 3 15 10-10-10
IO... gbrap12be 4 48 12-12-12-12
IO... gbrap12le 4 48 12-12-12-12
IO... gbrap10be 4 40 10-10-10-10
IO... gbrap10le 4 40 10-10-10-10
..H.. mediacodec 0 0 0
IO... gray12be 1 12 12
IO... gray12le 1 12 12
IO... gray10be 1 10 10
IO... gray10le 1 10 10
IO... p016le 3 24 16-16-16
IO... p016be 3 24 16-16-16
..H.. d3d11 0 0 0
IO... gray9be 1 9 9
IO... gray9le 1 9 9
IO... gbrpf32be 3 96 32-32-32
IO... gbrpf32le 3 96 32-32-32
IO... gbrapf32be 4 128 32-32-32-32
IO... gbrapf32le 4 128 32-32-32-32
..H.. drm_prime 0 0 0
..H.. opencl 0 0 0
IO... gray14be 1 14 14
IO... gray14le 1 14 14
IO... grayf32be 1 32 32
IO... grayf32le 1 32 32
IO... yuva422p12be 4 36 12-12-12-12
IO... yuva422p12le 4 36 12-12-12-12
IO... yuva444p12be 4 48 12-12-12-12
IO... yuva444p12le 4 48 12-12-12-12
IO... nv24 3 24 8-8-8
IO... nv42 3 24 8-8-8
..H.. vulkan 0 0 0
..... y210be 3 20 10-10-10
I.... y210le 3 20 10-10-10
IO... x2rgb10le 3 30 10-10-10
..... x2rgb10be 3 30 10-10-10
IO... x2bgr10le 3 30 10-10-10
..... x2bgr10be 3 30 10-10-10
IO... p210be 3 20 10-10-10
IO... p210le 3 20 10-10-10
IO... p410be 3 30 10-10-10
IO... p410le 3 30 10-10-10
IO... p216be 3 32 16-16-16
IO... p216le 3 32 16-16-16
IO... p416be 3 48 16-16-16
IO... p416le 3 48 16-16-16
可以看到ffmpeg是支持rgb24的像素格式转换的。
我们用下面的这个“视频素材_天空中的云.mp4”视频作为演示:
该视频的大小为4.57 MB (4,794,032 字节)
分辨率为:1280 * 720
通过命令:
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 视频素材_天空中的云.mp4
可以得到该视频总共有387帧
将“视频素材_天空中的云.mp4”转换为像素格式为rgb24的文件:output.rgb
ffmpeg -i 视频素材_天空中的云.mp4 -pix_fmt rgb24 output.rgb
该文件即包含原视频“视频素材_天空中的云.mp4” 里面解压并像素格式转换为rgb24后的所有数据
可以看到它的大小为0.99 GB = 1,069,977,600 byte = 1280 * 720 * 3 * 387 byte。所以可以证明“RGB24格式每像素占3个字节” 以及相关公式是正确的。
五、播放RGB24格式视频
常用的播放器无法播放裸rgb24格式的视频,但可以通过YUView 来播放,该播放器支持播放裸RGB 文件。
打开YUView,"File" -> "Open File"
设置视频宽度和高度,选择RGB Format为RGB 8bit(因为rgb24每个像素占8位),点击播放按钮就能播放了