YUV图像数据格式

1. YUV,分为三个分量,“Y”表示明亮度(Luminance或Luma),也就是灰度值;而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素的颜色。YUV格式主要有YUV444, YUV422, YUV420, YV12, NV12, NV21。需要强调的是如何根据其采样格式来从码流中还原每个像素点的YUV值,因为只有正确地还原了每个像素点的YUV值,才能通过YUV与RGB的转换公式提取出每个像素点的RGB值并显示出来。

  • YUV 4:4:4采样,每一个Y对应一组UV分量,一个YUV占8+8+8 = 24bits 3个字节;
  • YUV 4:2:2采样,每两个Y共用一组UV分量,一个YUV占8+4+4 = 16bits 2个字节;
  • YUV 4:2:0采样,每四个Y共用一组UV分量,一个YUV占8+2+2 = 12bits 1.5个字节;

2. YUV格式有两大类:planar,packed:

  • 对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V;
  • 对于packed的YUV格式,每个像素点的Y,U,V是连续交*存储的

3. YUV采样

最常见的YUV420PYUV420SP都是基于4:2:0采样的,所以如果图片的宽为width,高为heigth,在内存中占的空间为width * height * 3 / 2,其中前width * height的空间存放Y分量,接着width * height / 4存放U分量,最后width * height / 4存放V分量。

 YUV420P有YU12和YV12两种格式。YU12在android平台下也叫作I420格式。

YU12: YYYYYYYY UUVV    =>    YUV420P
YV12: YYYYYYYY VVUU    =>    YUV420P

YUV420SP有NV21和NV12两种格式。

NV12: YYYYYYYY UVUV    =>YUV420SP
NV21: YYYYYYYY VUVU    =>YUV420SP

4. 如果直接采集到的视频数据是RGB24的格式,RGB24一帧的大小size=width×heigth×3 Bit,RGB32的size=width×heigth×4,如果是YU12(即YUV标准格式4:2:0)的数据量是 size=width×heigth×1.5 Bit。 在采集到RGB24数据后,需要对这个格式的数据进行第一次压缩。即将图像的颜色空间由RGB2YUV。因为,X264在进行编码的时候需要标准的YUV(4:2:0)。但是这里需要注意的是,虽然YV12也是(4:2:0),但是YV12和YU12的却是不同的,在存储空间上面有些区别。如下:

YUV420P-YV12 : 亮度(行×列) + U(行×列/4) + V(行×列/4)

YUV420P-YU12 : 亮度(行×列) + V(行×列/4) + U(行×列/4)

YV12和YU12区别在于UV的顺序不同。

经过第一次数据压缩后RGB24->YUV(I420)数据量将减少一半。同样,如果是RGB24->YUV(YV12),也是减少一半。但是,虽然都是一半,如果是YV12的话效果就有很大损失???????。然后,经过X264编码后,数据量将大大减少。将编码后的数据打包,通过RTMP(基于TCP)或SRT(基于UDP)实时传送。到达目的地后,将数据取出,进行解码。完成解码后,数据仍然是YUV格式的,所以,还需要一次转换,windows或linux驱动才可以处理,就是YUV2RGB24。

5. YUV与RGB转换

Y      =  (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
Cr = V =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128

B = 1.164(Y - 16) + 2.018(U - 128)
G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
R = 1.164(Y - 16) + 1.596(V - 128)

根据上述转换公式,当U和V分量都为128时,图像只保留Y分量信息会变为灰度图。

6. 使用ffmpeg库将jpg格式图像转为yuv格式图片:

ffmpeg -i input.jpg -s 510x510 -pix_fmt yuv420p output.yuv

使用ffplay来播放yuv格式的文件:

ffplay -f rawvideo -video_size 510x510 output.yuv

# 查看yuv格式图片
ffplay -pixel_format nv21 -video_size 510*510 output.yuv
ffplay -pixel_format yuyv420 -video_size * *

# 查看实时视频流
ffplay /dev/video0

7. 代码示例:

1. 功能:(1)分离YUV文件各个分量 (2)改变UV分量值将YUV图像转为灰度图

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void SplitYuv420p_y_AndSavetoYUVFile(char *inputPath, char *outputPath, int width, int height) 
{

    FILE *inFile = fopen(inputPath, "rb+");
    FILE *outFile = fopen(outputPath, "wb+");

    unsigned char *data = (unsigned char *) malloc(width * height * 3 / 2);

    fread(data, 1, width * height * 3 / 2, inFile); //read y
    //Y
    fwrite(data, 1, width * height, outFile); //write y

    unsigned char *buffer = (unsigned char *) malloc(width * height / 4);
    memset(buffer, 0x80, width * height / 4);
    //U
    fwrite(buffer, 1, width * height / 4, outFile); //write u
    //V
    fwrite(buffer, 1, width * height / 4, outFile); //write v

    free(buffer);
    free(data);
    fclose(inFile);
    fclose(outFile);
}


void SplitYuv420(char *inputPath, int width, int height)
{

    FILE *fp_yuv = fopen(inputPath, "rb+");
    FILE *fp_y = fopen("output_420_y.raw", "wb+");
    FILE *fp_u = fopen("output_420_u.raw", "wb+");
    FILE *fp_v = fopen("output_420_v.raw", "wb+");

    unsigned char *data = (unsigned char *) malloc(width * height * 3 / 2);

    fread(data, 1, width * height * 3 / 2, fp_yuv);
    //Y
    fwrite(data, 1, width * height, fp_y);
    //U
    fwrite(data + width * height, 1, width * height / 4, fp_u);
    //V
    fwrite(data + width * height * 5 / 4, 1, width * height / 4, fp_v);

    //release resources
    free(data);

    fclose(fp_yuv);
    fclose(fp_y);
    fclose(fp_u);
    fclose(fp_v);
}

int main(int argc, char **argv)
{
    char yuvpath[128] = "/home/chang/test/yuv_test/input.yuv";
    char outpath[128] = "/home/chang/test/yuv_test/output.yuv";
    
    SplitYuv420(yuvpath, 510, 510);
    SplitYuv420p_y_AndSavetoYUVFile(yuvpath, outpath, 510, 510);

    return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要保存 YUV 格式数据,可以使用 FFmpeg 库提供的 `AVFrame` 结构体和 `av_write_frame` 函数。下面是保存 YUV 格式数据的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavformat/avformat.h> int main(int argc, char *argv[]) { int ret; AVFormatContext *fmt_ctx = NULL; AVOutputFormat *ofmt = NULL; AVStream *video_st = NULL; AVCodecContext *codec_ctx = NULL; AVFrame *frame = NULL; uint8_t *frame_data = NULL; int frame_size; int width = 640, height = 480; // 打开输出文件 if ((ret = avformat_alloc_output_context2(&fmt_ctx, NULL, NULL, "output.yuv")) < 0) { fprintf(stderr, "Error allocating output context: %s\n", av_err2str(ret)); return 1; } ofmt = fmt_ctx->oformat; // 添加视频流 video_st = avformat_new_stream(fmt_ctx, NULL); if (!video_st) { fprintf(stderr, "Error creating video stream\n"); return 1; } codec_ctx = video_st->codec; codec_ctx->codec_id = AV_CODEC_ID_RAWVIDEO; codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; codec_ctx->width = width; codec_ctx->height = height; codec_ctx->time_base = (AVRational){1, 25}; if ((ret = avcodec_parameters_to_context(codec_ctx, video_st->codecpar)) < 0) { fprintf(stderr, "Error copying codec parameters to context: %s\n", av_err2str(ret)); return 1; } // 打开视频编码器 if ((ret = avcodec_open2(codec_ctx, NULL, NULL)) < 0) { fprintf(stderr, "Error opening video encoder: %s\n", av_err2str(ret)); return 1; } // 创建帧缓冲区 frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Error allocating frame\n"); return 1; } frame->width = width; frame->height = height; frame->format = codec_ctx->pix_fmt; if ((ret = av_frame_get_buffer(frame, 0)) < 0) { fprintf(stderr, "Error allocating frame buffer: %s\n", av_err2str(ret)); return 1; } frame_data = frame->data[0]; frame_size = av_image_get_buffer_size(codec_ctx->pix_fmt, width, height, 1); // 打开输出文件 if (!(ofmt->flags & AVFMT_NOFILE)) { if ((ret = avio_open(&fmt_ctx->pb, "output.yuv", AVIO_FLAG_WRITE)) < 0) { fprintf(stderr, "Error opening output file: %s\n", av_err2str(ret)); return 1; } } // 写入视频帧 for (int i = 0; i < 25; i++) { // 生成测试图像 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int r = rand() % 256; int g = rand() % 256; int b = rand() % 256; uint8_t *yuv = frame_data + y * frame->linesize[0] + x * 3 / 2; yuv[0] = 0.299 * r + 0.587 * g + 0.114 * b; yuv[1] = -0.14713 * r - 0.28886 * g + 0.436 * b; yuv[2] = 0.615 * r - 0.51498 * g - 0.10001 * b; } } // 编码并写入帧 frame->pts = i; AVPacket pkt = {0}; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; if ((ret = avcodec_send_frame(codec_ctx, frame)) < 0) { fprintf(stderr, "Error sending frame to encoder: %s\n", av_err2str(ret)); return 1; } while (ret >= 0) { ret = avcodec_receive_packet(codec_ctx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error encoding frame: %s\n", av_err2str(ret)); return 1; } pkt.stream_index = video_st->index; av_write_frame(fmt_ctx, &pkt); av_packet_unref(&pkt); } } // 清理工作 av_write_trailer(fmt_ctx); if (codec_ctx) avcodec_close(codec_ctx); if (frame) av_frame_free(&frame); if (fmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) avio_closep(&fmt_ctx->pb); avformat_free_context(fmt_ctx); return 0; } ``` 这段代码首先创建一个 YUV 格式的视频流,然后生成一些随机数据作为测试图像,并将每帧图像编码成 YUV 格式的数据,最终将所有帧数据写入到文件中。注意,这里使用了随机数据生成测试图像,实际应用中需要根据实际情况生成真实的图像数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值