libyuv接口YUY2ToI420的实际使用

YUY2ToI420

1.接口定义:
// Convert YUY2 to I420.
LIBYUV_API
int YUY2ToI420(const uint8* src_yuy2, int src_stride_yuy2,
uint8* dst_y, int dst_stride_y,
uint8* dst_u, int dst_stride_u,
uint8* dst_v, int dst_stride_v,
int width, int height);

2.实际使用
首先弄清楚两种图片格式的存储格式:
在这里插入图片描述
YUV格式在存储上存在两类布局:
Packed:把相邻的几个像素打包起来;比如把水平方向2个像素打包到一个DWORD
Plannar:方式相反;Y分量和UV分量完全分开来保存
YUY2和YV12是最常用的两个代表。
YUY2是packed方式的。水平方向两个像素打包到一个DWORD,并且UV采样率只有Y的一半,这符合人的视觉特征能有效的压缩数据,具体布局为[Y0, U0,Y1,V0]。 这种格式常见于MPEG1的解码器。
YV12则常见于H.264的解码器,它属于plannar方式。对于一个MxN大小的视频来说,数据布局为[Y:M x N] [V:M/2 x N/2] [U:M/2 x N/2]. 也就是说UV的采样率在水平和垂直方向上都只有Y的一半。

	i420_image = (uint8*)malloc(image->width * image->height * 1.5);
        uint8* i420_image_y_ptr = i420_image;
        uint8* i420_image_u_ptr = i420_image_y_ptr + (image->width * image->height);
        uint8* i420_image_v_ptr = i420_image_u_ptr + (int)(image->width * image->height * 0.25);

        libyuv::YUY2ToI420((const uint8*)image->data, image->bytes_per_line, //image->bytes_per_line为422一行的字节数
            i420_image_y_ptr, image->width,
            i420_image_u_ptr, (image->width >> 1),
            i420_image_v_ptr, (image->width >> 1),
            image->width, image->height);
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用 libyuv::YUY2ToNV12 进行图像转换的示例代码: ```cpp #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include "libyuv.h" int main(int argc, char* argv[]) { const char* src_filename = "input.yuy2"; const char* dst_filename = "output.nv12"; const int src_width = 640; const int src_height = 480; // 打开输入文件 FILE* src_file = fopen(src_filename, "rb"); if (!src_file) { std::cerr << "Could not open source file: " << src_filename << std::endl; return -1; } // 打开输出文件 FILE* dst_file = fopen(dst_filename, "wb"); if (!dst_file) { std::cerr << "Could not open destination file: " << dst_filename << std::endl; return -1; } // 准备输入帧 uint8_t* src_frame = (uint8_t*)malloc(src_width * src_height * 2); // 准备输出帧 uint8_t* dst_y = (uint8_t*)malloc(src_width * src_height); uint8_t* dst_uv = (uint8_t*)malloc(src_width * src_height / 2); // 读取输入帧 if (fread(src_frame, 1, src_width * src_height * 2, src_file) != src_width * src_height * 2) { std::cerr << "Could not read input frame" << std::endl; return -1; } // 进行图像转换 libyuv::YUY2ToNV12(src_frame, src_width * 2, dst_y, src_width, dst_uv, src_width, src_width, src_height); // 写入输出帧 if (fwrite(dst_y, 1, src_width * src_height, dst_file) != src_width * src_height) { std::cerr << "Could not write output frame" << std::endl; return -1; } if (fwrite(dst_uv, 1, src_width * src_height / 2, dst_file) != src_width * src_height / 2) { std::cerr << "Could not write output frame" << std::endl; return -1; } // 释放资源 fclose(src_file); fclose(dst_file); free(src_frame); free(dst_y); free(dst_uv); return 0; } ``` 代码中的 YUY2ToNV12 图像转换使用libyuv 库进行实现。在代码中,我们首先打开输入文件和输出文件,并准备输入帧和输出帧的内存空间。然后,我们读取输入帧,进行图像转换,最后将输出帧写入输出文件。最后,我们释放资源并返回。注意,由于 NV12 格式中的 UV 分量是交错存储的,因此在写入输出文件时需要分别写入 Y 分量和交错的 UV 分量。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值