当前位置:网站首页>C code production YUV420 planar format file
C code production YUV420 planar format file
2022-07-03 07:15:00 【Hui's technical notes】
YUV420 knowledge
For all YUV420 Images , Their Y The values are arranged exactly the same , Because only Y The image of is a grayscale image .
YUV420 According to the storage form , It is divided into YUV420sp(semi-Planar Half plane ) And YUV420p(planar Plane ), Their data format is UV The arrangement is different .420p It is to put U perhaps V After storage , Re storage V perhaps U, in other words UV They are continuous , and 420sp yes UV Alternate storage .
I420(YU12) and YV12 Belong to YUV420p Format , take Y、U、V Pack the components separately , Store... In sequence .NV12 And NV21 similar ,U and V staggered , Belong to YUV420sp.
YUV 4:2:0 sampling , Every four Y Share a group UV component . therefore , Work out a YUV420 The size of storage in memory , It's below. Y,U,V The sum of the sizes of the components , namely :Y+U+V => width * height * 3/2
Y = width * hight
U = Y / 4
V = Y / 4
Icon :1 A pixel YUV Storage , Each letter represents a ,
I420: YYYYYYYY UUVV => YUV420P
YV12: YYYYYYYY VVUU => YUV420P
NV12: YYYYYYYY UVUV => UV420SP
NV21: YYYYYYYY VUVU => YUV420SP
In the name ,“YV” Represents the plane order :Y, then V( then U).12 Refers to pixel depth : about YV12, Each pixel 12 position .NV12 Medium 12 It also means per pixel 12 position .
Grey white YUV
The following code produces a 1080x720 Of YUV file , The format is YUV420 planar, about planar Of YUV Format , First, store all pixels in a row Y, And then it stores all the pixels U, And then all the pixels V.
therefore ,data Its size is width * height * 3 / 2, First write width * height Size data , Color bit 0xb4, To write width * height / 2 The size of as uv The data of , Here's the choice UV The color is the same , So I wrote together . The document written in this way has been used ubuntu Upper YUVView Tool view is grayish white .
int width = 1080;
int height = 720;
int size = width * height * 3 / 2;
uint8_t *data = (uint8_t *)malloc(size);
memset(data, 0xb4, size);
fwrite(data, width * height, 1, fp); // write y data
memset(data + width*height, 0x80, size);
fwrite(data, width*height/2, 1, fp); //write uv data
fclose(fp);
Cyan YUV
The size of the cyan file definition is 8x8 The file of , Because discovery passed YUVView Tools , Zoom in 8x8 Of YUV After the image , You can see clearly YUV The distribution of components in the picture .
Apply first in the following code data Memory , Then set different values , Finally, write by size .
int width = 8;
int height = 8;
int size = width * height * 3 / 2;
uint8_t *data = (uint8_t *)malloc(size);
// set Y to 131
memset(data, 0x83/* 131 */, width*height);
// set U to 156
memset(data + width*height, 0x9c/* 156 */, width*height/4);
// set V to 44
memset(data + width*height + width*height/4, 0x2c/* 44 */, width*height/4);
FILE* fp = nullptr;
fp = fopen("/sdcard/h264.yuv", "wb");
// write Y
fwrite(data, width * height, 1, fp);
// write UV
fwrite(data+ width*height, width*height/2, 1, fp);
fclose(fp);
adopt ubuntu On YUView Tools , Zoom in to 64 times , You can see that YUV Distribution of ,YUV420 One of the four points UV value :

Replace the code with an array
width = 8;
height = 8;
int size = width * height * 3 / 2;
uint8_t *data = (uint8_t *)malloc(size);
memset(data, 0, size);
FILE* fp = nullptr;
fp = fopen("/sdcard/h264.yuv", "wb");
// set Y to 131
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
data[i*width + j] = 131;
}
}
// set U to 156
int offset = width * height;
for(int i = 0; i < height/4; i++) {
for(int j = 0; j < width; j++) {
data[offset + i*width + j] = 156;
}
}
// set V to 45
offset = width*height + width*height/4;
for(int i = 0; i < height/4; i++) {
for(int j = 0; j < width; j++) {
data[offset + i*width + j] = 45;
}
}
fwrite(data, width * height, 1, fp);
//write UV
fwrite(data+ width*height, width*height/2, 1, fp);
fclose(fp);
YUView see , And the one in front V Values are not the same :

The color table reference in the previous code
| Nominal range | white | yellow | Cyan | green | Red | Blue | black | |
|---|---|---|---|---|---|---|---|---|
| Y | 16~235 | 180 | 162 | 131 | 112 | 65 | 35 | 16 |
| Cb | 16~240 | 128 | 44 | 156 | 72 | 100 | 212 | 128 |
| Cr | 16~240 | 128 | 142 | 44 | 58 | 212 | 114 | 128 |
边栏推荐
- Practice of enterprise ab/testing platform
- gstreamer ffmpeg avdec解码数据流向分析
- Laravel Web Framework
- Final, override, polymorphism, abstraction, interface
- Troubleshooting of high CPU load but low CPU usage
- Recursion, Fibonacci sequence
- 【无标题】
- Advanced API (byte stream & buffer stream)
- Advanced API (serialization & deserialization)
- 7.2刷题两个
猜你喜欢
![[set theory] partition (partition | partition example | partition and equivalence relationship)](/img/f0/c3c82de52d563f3b81d731ba74e3a2.jpg)
[set theory] partition (partition | partition example | partition and equivalence relationship)

【已解决】Unknown error 1146

10000小時定律不會讓你成為編程大師,但至少是個好的起點

Selenium key knowledge explanation

Notes on the core knowledge of Domain Driven Design DDD

在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储

7.2刷题两个

Recursion, Fibonacci sequence

Interview questions about producers and consumers (important)

MySQL installation
随机推荐
The essence of interview
【已解决】Unknown error 1146
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
[set theory] partition (partition | partition example | partition and equivalence relationship)
【无标题】
Redis command
万卷书 - 价值投资者指南 [The Education of a Value Investor]
mongodb
Troubleshooting of high CPU load but low CPU usage
Class and object summary
Advanced APL (realize group chat room)
MySQL syntax (basic)
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
Abstract learning
Win 2008 R2 crashed at the final installation stage
树莓派更新工具链
Setting up the development environment of dataworks custom function
PHP install the spool extension
[solved] unknown error 1146