以前的笔记转存到这里。
// x264t.cpp : Defines the entry point for the console application.
//
#include "stdint.h"
#ifndef _DEBUG
#pragma comment(lib, "libx264.lib")
#else
#pragma comment(lib, "libx264d.lib")
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "x264.h"
typedef char bool;
#define false 0;
#define true 1;
bool x264_encode_frame(x264_t *h,x264_param_t *param,
x264_picture_t* pic_in,
uint8_t* data_out,int* data_out_len,
bool* key_frame);
int main(int argc, char* argv[])
{
char *in_file = argv[1];
char *out_file = argv[2];
FILE *fp_in;
FILE *fp_out;
int width, height, frame_size, plane0_size,plane1_size,plane2_size;
x264_param_t param;
x264_t *h;
x264_picture_t pic;
uint8_t *buffer;
int actual_read_size;
bool key_frame;
if(argc >= 5)
{
sscanf(argv[3],"%dx%d",&width,&height);
plane0_size = plane1_size = plane2_size = height*width;
if(strcmp(argv[4],"444") == 0)
{
param.i_csp = X264_CSP_I444;
x264_picture_alloc( &pic, X264_CSP_I444, width, height );
}
else if(strcmp(argv[4],"422") == 0)
{
param.i_csp = X264_CSP_I422;
plane1_size = plane2_size = plane0_size/2;
x264_picture_alloc( &pic, X264_CSP_I422, width, height );
}
else /*if(strcmp(argv[4],"420") == 0)*/
{
param.i_csp = X264_CSP_I420;
plane1_size = plane2_size = plane0_size/4;
x264_picture_alloc( &pic, X264_CSP_I420, width, height );
}
frame_size = plane0_size + plane1_size + plane2_size;;
buffer = (uint8_t*)malloc(frame_size);
x264_param_default( ¶m );
//设置编码参数
param.i_threads = 8;
param.i_width = width;
param.i_height = height;
param.i_bframe=3;
param.i_frame_reference=3;
param.rc.i_rc_method=X264_RC_CQP ;
param.i_csp = X264_CSP_I420;
param.b_visualize=1;
param.i_log_level=X264_LOG_NONE;
param.pf_log = printf;
if( ( h = x264_encoder_open(¶m ) ) == NULL )
{
return -1;
}
if((fp_in =fopen(in_file,"rb")) == NULL)
{
puts("打开输入文件失败");
return -1;
}
if((fp_out =fopen(out_file,"wb")) == NULL)
{
puts("打开输出文件失败");
return -1;
}
do
{
actual_read_size = fread(buffer,frame_size,1,fp_in);
if(actual_read_size == 1)
{
actual_read_size= frame_size;
memcpy(pic.img.plane[0], buffer, plane0_size);
memcpy(pic.img.plane[1], buffer + plane0_size, plane1_size);
memcpy(pic.img.plane[2], buffer + plane0_size + plane1_size, plane2_size);
pic.i_pts = (int64_t)param.i_frame_total * param.i_fps_den;
x264_encode_frame(h,¶m,&pic,buffer,&actual_read_size,&key_frame);
fwrite(buffer,actual_read_size,1,fp_out);
flushall();
}
else
{
fseek( fp_in, 0, SEEK_SET );
}
param.i_frame_total ++;
}while(1);
x264_picture_clean( &pic );
x264_encoder_close( h );
free(buffer);
fclose(fp_out);
fclose(fp_in);
}
else
{
printf("%s","参数错误!");
}
return 0;
}
bool x264_encode_frame(x264_t *h, x264_param_t *param, x264_picture_t* pic_in, uint8_t* data_out,int* data_out_len, bool* key_frame )
{
x264_picture_t pic_out;
x264_nal_t *nal;
int i_nal,i;
int i_size = 0;
int data_out_l = *data_out_len;
*data_out_len = 0;
if( x264_encoder_encode( h, &nal, &i_nal, pic_in, &pic_out ) < 0 )
{
return false;
}
for(i = 0; i < i_nal; i++ )
{
i_size = 0;
if( ( i_size = x264_nal_encode( data_out + *data_out_len, &data_out_l, 1, &nal[i] ) ) > 0 )
{
*data_out_len += i_size;
data_out_l -= i_size;
}
}
if(pic_out.i_type==X264_TYPE_IDR)
*key_frame = true;
return true;
}