YUV格式数据

本文深入探讨了Android中常见的原始视频格式YUV,包括420和422系列的不同变种,如420p、420sp、422p等,以及NV12和NV21的区别。通过详细的存储方式示例,阐述了YUV各格式的像素排列和数据存储特点,有助于理解和处理Android视频数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

YUV格式有两大类:平面(planar)和紧凑(packed)。
对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。
对于packed的YUV格式,每个像素点的Y,U,V是连续交*存储的。以下缩写p表示“紧凑”,sp表示“半紧凑”。

以分辨率8*4为例,数据存储方式如下。

1、yuyv(yuv422)【YUY2】:
YUYVYUYV
YUYVYUYV
YUYVYUYV
YUYVYUYV
YUYVYUYV
YUYVYUYV
YUYVYUYV
YUYVYUYV


2、yvyu(yuv422):
YVYUYVYU
YVYUYVYU
YVYUYVYU
YVYUYVYU
YVYUYVYU
YVYUYVYU
YVYUYVYU
YVYUYVYU


uyvy、vyuy略


3、yuv422p(yuv422):
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
UUUUUUUU
UUUUUUUU
VVVVVVVV
VVVVVVVV


4、yuv422sp(yuv422):
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
UVUVUVUV
UVUVUVUV
UVUVUVUV
UVUVUVUV


5、yuv420p(yuv420)【I420】:
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
UUUUUUUU
VVVVVVVV


6、YV12(yuv420):
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
VVVVVVVV
UUUUUUUU


7、yuv420sp(yuv420):
YYYYYYYY
YYYYYYYY
YYYYYYYY
YYYYYYYY
UVUVUVUV
UVUVUVUV
 

一些常用格式介绍:
1、YUV444

(1)YUV444p:YYYYYYYYY VVVVVVVVV UUUUUUUU

2、YUV422

(1)YUV422p:

         YV16:                              YYYYYYYY VVVV UUUU       (V前U后)

        YUV422p或I422或YU16: YYYYYYYY UUUU VVVV       (U前V后)

(2)YUV422sp:

          NV61:YYYYYYYY VUVUVUVU      (V前U后)

          NV16:YYYYYYYY UVUVUVUV      (U前V后)

(2)YUVY:YUYV YUYV YUYV YUYV

(3)UYVY:UYVY UYVY UYVY UYVY

3、YUV420

(1)YUV420p:

YV12:YYYYYYYY VV UU

I420:YYYYYYYY UU VV

(2)YUV420sp:

NV12:YYYYYYYY UVUV

NV21:YYYYYYYY VUVU
 

一篇好文章:Android原始视频格式YUV,NV12,NV21,YV12,YU12(I420)

Android原始视频格式YUV,NV12,NV21,YV12,YU12(I420)_lidongxiu0714-CSDN博客

要保存 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、付费专栏及课程。

余额充值