1.实现打开和关闭输入文件和输出文件的操作
点击查看代码
//io_data.cpp
static FILE* input_file= nullptr;
static FILE* output_file= nullptr;
int32_t open_input_output_files(const char* input_name,const char* output_name){
if(strlen(input_name)==0||strlen(output_name)==0){
cout<<"Error:empty input or output file name."<<endl;
return -1;
}
close_input_output_files();
input_file=fopen(input_name,"rb");//rb:读取一个二进制文件,该文件必须存在
if(input_file==nullptr){
cerr<<"Error:failed to open input file."<<endl;
return -1;
}
output_file=fopen(output_name,"wb");//wb:打开或新建一个二进制文件,只允许写
if(output_file== nullptr){
cout<<"Error:failed to open output file."<<endl;
return -1;
}
return 0;
}
void close_input_output_files(){
if(input_file!= nullptr){
fclose(input_file);
input_file= nullptr;
}
if(output_file!= nullptr){
fclose(output_file);
output_file= nullptr;
}
}
点击查看代码
typedef struct AVCodec{
const char *name;//简要名称
const char *longname;//完整名称
enum AVMediaType type;//媒体类型
enum AVCodecID id;
enum AVPixelFormat *pix_fmts;//像素格式,一般为yuv420p
const AVProfile *profiles;//编码档次
}
点击查看代码
typedef struct AVCodecContext{
void *priv_data;//私有参数
int64_t bit_rate;//码率
int width,height;
enum AVPixelFormat pix_fmt;
int max_b_frames;//最大的b帧数量
}
点击查看代码
typedef struct AVFrame{
#define AV_NUM_DATA_POINTERS 8
uint8_t *data[AV_NUM_DATA_POINTERS];//图像数据缓存区
int linesize[AV_NUM_DATA_POINTERS];//存储区的宽度
int width,height;
int format;
}
点击查看代码
typedef struct AVPacket{
int64_t pts;//显示时间戳
int64_t dts;//解码时间戳
uint8_t *data;//码流数据
int size;
int stream_index;//所从属的stream序号
}
点击查看代码
//video_encoder_core.cpp
static const AVCodec* codec= nullptr;
static AVCodecContext* codec_ctx= nullptr;
static AVFrame* frame= nullptr;
static AVPacket* pkt= nullptr;
int32_t init_video_encoder(const char* codec_name){
if(strlen(codec_name)==0){
cerr<<"Error:empty codec name."<<endl;
return -1;
}
//查找编码器
codec=avcodec_find_encoder_by_name(codec_name);
if(!codec){
cerr<<"Error:could not find codec with codec name:"<<string(codec_name)<<endl;
return -1;
}
//创建编码器上下文结构的实例
codec_ctx= avcodec_alloc_context3(codec);
if(!codec_ctx){
cerr<<"Error:could not allocate video codec context."<<endl;
return -1;
}
//配置编码参数
codec_ctx->profile=FF_PROFILE_H264_HIGH;
codec_ctx->bit_rate=2000000;
codec_ctx->width=1920;
codec_ctx->height=1080;
codec_ctx->gop_size=10;//关键帧间距
codec_ctx->time_base=(AVRational){1,25};//num:分子,den:分母
codec_ctx->framerate=(AVRational){25,1};
codec_ctx->max_b_frames=3;
codec_ctx->pix_fmt=AV_PIX_FMT_YUV420P;
if(codec->id==AV_CODEC_ID_H264){
av_opt_set(codec_ctx->priv_data,"preset","slow",0);
av_opt_set(codec_ctx->priv_data,"tune","zerolatency",0);
}
//使用指定的codec初始化编码器上下文结构,并分配内存
int32_t result=avcodec_open2(codec_ctx,codec, nullptr);
if(result<0){
cerr<<"Error:could not open codec"<<endl;
return -1;
}
pkt=av_packet_alloc();
if(!pkt){
cerr<<"Error:could not allocate AVPacket."<<endl;
return -1;
}
frame=av_frame_alloc();
if(!frame){
cerr<<"Error:could not allocate AVFrame."<<endl;
return -1;
}
frame->width=codec_ctx->width;
frame->height=codec_ctx->height;
frame->format=codec_ctx->pix_fmt;
result= av_frame_get_buffer(frame,0);//给AVFrame结构中的音视频数据分配空间
if(result<0){
cerr<<"Error:could not get AVFrame buffer."<<endl;
return -1;
}
return 0;
}
读取图像数据和写出码流数据:
点击查看代码
//io_data.cpp
int32_t read_yuv_to_frame(AVFrame* frame){
int32_t frame_width=frame->width;
int32_t frame_height=frame->height;
int32_t luma_stride=frame->linesize[0];
int32_t chroma_stride=frame->linesize[1];
int32_t frame_size=frame_width*frame_height*3/2;
int32_t read_size=0;
if(frame_width==luma_stride){
//如果width等于stride,则说明frame中不存在padding字节,可整体读取
read_size+=fread(frame->data[0],1,frame_width*frame_height,input_file);
read_size+=fread(frame->data[1],1,frame_width*frame_height/4,input_file);
read_size+=fread(frame->data[2],1,frame_width*frame_height/4,input_file);
}
else{
//如果width不等于stride,则说明frame中存在padding字节
//对三个分量应该逐行读取
for(size_t i=0;i<frame_height;i++){
read_size+=fread(frame->data[0]+i*luma_stride,1,frame_width,input_file);
}
for(size_t uv=1;uv<=2;uv++){
for(size_t i=0;i<frame_height/2;i++){
read_size+=fread(frame->data[uv]+i*chroma_stride,1,frame_width/2,input_file);
}
}
}
if(read_size!=frame_size){
cerr<<"Error:Read data error,frame_size:"<<frame_size<<",read_size:"<<read_size<<endl;
return -1;
}
return 0;
}
void write_pkt_to_file(AVPacket* pkt){
fwrite(pkt->data,1,pkt->size,output_file);
}
点击查看代码
//video_encoder_core.cpp
static int32_t encode_frame(bool flushing){
int32_t result=0;
if(!flushing){
cout<<"Send frame to encoder with pts:"<<frame->pts<<endl;
}
result=avcodec_send_frame(codec_ctx,flushing? nullptr:frame);
if(result<0){
cerr<<"Error:avcodec_send_frame failed."<<endl;
return result;
}
while(result>=0){
result= avcodec_receive_packet(codec_ctx,pkt);
if(result==AVERROR(EAGAIN)||result==AVERROR_EOF){//尚未完成对新一帧的编码,要传入后续帧或编码器已完全输出内部缓存的码流
return 1;
}
else if(result<0){
cerr<<"Error:avcodec_receive_packet failed."<<endl;
return result;
}
if(flushing){
cout<<"Flushing:";
}
cout<<"Got encoded package with dts:"<<pkt->dts<<",pts:"<<pkt->pts<<", "<<endl;
write_pkt_to_file(pkt);
}
return 0;
}
点击查看代码
//video_encoder_core.cpp
int32_t encoding(int32_t frame_cnt){
int result=0;
for(size_t i=0;i<frame_cnt;i++){
result= av_frame_make_writable(frame);//确保AVFrame是可写的
if(result<0){
cerr<<"Error:could not av_frame_make_writable."<<endl;
return result;
}
result= read_yuv_to_frame(frame);
if(result<0){
cerr<<"Error:read_yuv_to_frame failed."<<endl;
return result;
}
frame->pts=i;
result= encode_frame(false);
if(result<0){
cerr<<"Error:encode_frame failed."<<endl;
return result;
}
}
result= encode_frame(true);
if(result<0){
cerr<<"Error:flushing failed."<<endl;
return result;
}
return 0;
}
点击查看代码
//video_encoder_core.cpp
void destroy_video_encoder(){
avcodec_free_context(&codec_ctx);
av_frame_free(&frame);
av_packet_free(&pkt);
}
点击查看代码
int main(){
const char* input_file_name= "../input.yuv";
const char* output_file_name= "../output.h264";
const char* codec_name= "libx264";
int32_t result= open_input_output_files(input_file_name,output_file_name);
if(result<0){
return result;
}
result=init_video_encoder(codec_name);
if(result<0){
return result;
}
result=encoding(250);
if(result<0){
return result;
}
destroy_video_encoder();
close_input_output_files();
return 0;
}