编码YUYV为H.264
YUYV格式与YUV444,YUV422,YUV420最大的差别就是前者Y,U,V三个分量交叉存储,后者Y,U,V三个分量分别集中存储。
即YUYV:
YUYVYUYVYUYV
YUYVYUYVYUYV
YUYVYUYVYUYV
YUYVYUYVYUYV
…
YUV444:
YYYYYYYYYYYY
YYYYYYYYYYYY
UUUUUUUUUUU
UUUUUUUUUUU
VVVVVVVVVVVV
VVVVVVVVVVVV
由此也可以看出,YUYV一帧大小等于Width * Height * 2,YUV444等于Width * Height * 3;
YUYV轮流读取到对应plane[];
//序列为YU YV YU YV,一个yuv422帧的长度 width * height * 2 个字节
for (k = 0; k < y_size2; k+=4){
*(y + y_index) = *(in+k+0);
y_index++;
*(u + u_index) = *(in+k+1);
u_index++;
*(y + y_index) = *(in+k+2);
y_index++;
*(v + v_index) = (in+k+3);
v_index++;
}
参考雷神的H264编码,针对YUYV格式信号做了简单修改。
最简单的视频编码器:基于libx264(编码YUV为H.264)
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"
#include "x264.h"
int main()
{
int ret;
int y_size;
int i,j,k;
//FILE* fp_src = fopen("../cuc_ieschool_640x360_yuv444p.yuv", "rb");
//FILE* fp_src = fopen("../cuc_ieschool_640x360_yuv420p.yuv", "rb");
FILE* fp_src = fopen("out.yuv","r");
FILE* fp_dst = fopen("out.h264", "wb");
//Encode 30 frame
//if set 0, encode all frame
int frame_num=0;
int csp=X264_CSP_I422;
int width=640,height=480;
int iNal = 0;
x264_nal_t* pNals = NULL;
x264_t* pHandle = NULL;
x264_picture_t* pPic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t));
x264_picture_t* pPic_out = (x264_picture_t*)malloc(sizeof(x264_picture_t));
x264_param_t* pParam = (x264_param_t*)malloc(sizeof(x264_param_t));
uint8_t *y;
uint8_t *u;
uint8_t *v;
uint8_t *in;
//Check
if(fp_src==NULL||fp_dst==NULL){
printf("Error open files.\n");
return -1;
}
x264_param_default(pParam);
pParam->i_width = width;
pParam->i_height = height;
pParam->i_fps_den = 1;
pParam->i_fps_num = 15;
pParam->i_timebase_den = pParam->i_fps_num;
pParam->i_timebase_num = pParam->i_fps_den;
/*
pParam->i_log_level = X264_LOG_DEBUG;
pParam->i_threads = X264_SYNC_LOOKAHEAD_AUTO;
pParam->i_frame_total = 0;
pParam->i_keyint_max = 10;
pParam->i_bframe = 5;
pParam->b_open_gop = 0;
pParam->i_bframe_pyramid = 0;
pParam->rc.i_qp_constant=0;
pParam->rc.i_qp_max=0;
pParam->rc.i_qp_min=0;
pParam->i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
*/
pParam->i_csp=csp;
x264_param_apply_profile(pParam, x264_profile_names[5]);
pHandle = x264_encoder_open(pParam);
x264_picture_init(pPic_out);
x264_picture_alloc(pPic_in, csp, pParam->i_width, pParam->i_height);
//ret = x264_encoder_headers(pHandle, &pNals, &iNal);
y_size = pParam->i_width * pParam->i_height;
//detect frame number
if(frame_num==0){
fseek(fp_src,0,SEEK_END);
switch(csp){
case X264_CSP_I444:frame_num=ftell(fp_src)/(y_size*3);break;
case X264_CSP_I422:frame_num=ftell(fp_src)/(y_size*2);break;
case X264_CSP_I420:frame_num=ftell(fp_src)/(y_size*3/2);break;
default:printf("Colorspace Not Support.\n");return -1;
}
fseek(fp_src,0,SEEK_SET);
}
//Loop to Encode
for( i=0;i<frame_num;i++){
int y_index = 0, u_index = 0, v_index = 0;
in = (uint8_t *)malloc(sizeof(uint8_t)*y_size*2);
y = (uint8_t *)malloc(sizeof(uint8_t)*y_size);
u = (uint8_t *)malloc(sizeof(uint8_t)*y_size);
v = (uint8_t *)malloc(sizeof(uint8_t)*y_size);
fread(in,y_size*2,1,fp_src);
//序列为YU YV YU YV,一个yuv422帧的长度 width * height * 2 个字节
for (k = 0; k < y_size*2; k+=4){
*(y + y_index) = *(in+k+0);
y_index++;
*(u + u_index) = *(in+k+1);
u_index++;
*(y + y_index) = *(in+k+2);
y_index++;
*(v + v_index) = *(in+k+3);
v_index++;
}
pPic_in->img.plane[0] = y;
pPic_in->img.plane[1] = u;
pPic_in->img.plane[2] = v;
pPic_in->i_pts = i;
ret = x264_encoder_encode(pHandle, &pNals, &iNal, pPic_in, pPic_out);
if (ret< 0){
printf("Error.\n");
return -1;
}
printf("Succeed encode frame: %5d\n",i);
for ( j = 0; j < iNal; ++j){
fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);
}
free(in);
}
i=0;
//flush encoder
while(1){
ret = x264_encoder_encode(pHandle, &pNals, &iNal, NULL, pPic_out);
if(ret==0){
break;
}
printf("Flush 1 frame.\n");
for (j = 0; j < iNal; ++j){
fwrite(pNals[j].p_payload, 1, pNals[j].i_payload, fp_dst);
}
i++;
}
x264_picture_clean(pPic_in);
x264_encoder_close(pHandle);
pHandle = NULL;
free(pPic_in);
free(pPic_out);
free(pParam);
fclose(fp_src);
fclose(fp_dst);
return 0;
}
测试结果如下:
测试软件为 H264BSAnalyzer
原YUYV视频如下: