近期进行视频开发,用到了ffmpeg,视频处理中yuv格式是基础,输入输出必定会碰到。
yuv转rgb算法代码很多,但是反过来的很少,这里提供一个rgb转yuv的,是c语言代码,但可以轻松改为其他语言,如c#,java
int rgb32_to_i420(int width, int height, const char * src,
char * dst)
{
unsigned char * dst_y_even;
unsigned char * dst_y_odd;
unsigned char * dst_u;
unsigned char * dst_v;
const unsigned char *src_even;
const unsigned char *src_odd;
int i, j;
src_even = (const unsigned char *)src;
src_odd = src_even + width * 4;
// it's planar
dst_y_even = (unsigned char *) dst;
dst_y_odd = dst_y_even + width;
dst_u = dst_y_even + width * height;
dst_v = dst_u + ((width * height) >> 2);
// NB this doesn't work perfectly for u and v values of the
edges of the