看了雷神的博客《最简单的基于FFMPEG的视频编码器(YUV编码为H.264)》,它介绍的是使用FFMPEG将YUV420 数据编码成H.264数据。在它的博客中,没有介绍到YUYV数据格式的编码,但是我们在实际应用中,有时候摄像头只能输出YUYV数据格式,这样他的工程就不能使用了。经过一通折腾,终于在他的基础上实现了使用FFMPEG将YUYV编码成H264数据。
因为YUYV数据是属于YUV422格式,而FFMPEG的编码器又不支持AV_PIX_FMT_YUYV422 编码格式。因此我们需要做的是将YUYV数据格式转换为YUV422P数据格式,然后再进行编码。因为YUYV转YUV422P比较简单,因此我这里没有调用FFMPEG的转换器而是直接转换了。这里需要注意一下,我使用的FFMPEG版本是ffmpeg-3.2.4 ,因为版本的更新,有些接口可能改变了,也正是因为这样,在调试的时候出现了不少的问题。代码如下:
/*=============================================================================
# FileName: ffmpeg_encoder.c
# Desc: an example of ffmpeg fileter
# Author: licaibiao
# LastChange: 2017-03-18
=============================================================================*/
#include
#define __STDC_CONSTANT_MACROS
#include
#include
#include
//#define ENCODE_YUV
#ifdef ENCODE_YUV
#define INPUT_FILE "yuv_512_288.yuv"
#define INPUT_WIDTH 512
#define INPUT_HEIGHT 288
#else
#define INPUT_FILE "yuyv_640_480.yuyv"
#define INPUT_WIDTH 640
#define INPUT_HEIGHT 480
#endif
#define OUTPUT_FILE "ds.h264"
#define FRAMENUM 300
int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
CODEC_CAP_DELAY))
return 0;
while (1) {
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)
break;
if (!got_frame){
ret=0;
break;
}
printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
/* mux encoded frame */
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
return ret;
}
int main(int argc, char* argv[])
{
AVFormatContext* pFormatCtx;
AVOutputFormat* fmt;
AVStream* video_st;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
AVPacket pkt;
uint8_t* picture_buf;
AVFrame* pFrame;
int picture_size;
int y_size;
int framecnt=0;
FILE *in_file ; //Input raw YUV data
int in_w=INPUT_WIDTH, in_h=INPUT_HEIGHT; //Input data's width and height
int framenum=FRAMENUM; //Frames to encode
const char* out_file = OUTPUT_FILE;
int i,j,k;
int num;
int index_y, index_u, index_v;
uint8_t *y_, *u_, *v_, *in;
int got_picture =0;
int ret;
in_file = fopen(INPUT_FILE, "rb"); //Input raw YUV data
av_register_all();
//Method1.
pFormatCtx = avformat_alloc_context();
//Guess Format
fmt = av_guess_format(NULL, out_file, NULL);
pFormatCtx->oformat = fmt;
//Method 2.
/* 初始化输出码流的AVFormatContext */
//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
//fmt = pFormatCtx->oformat;
//Open output URL
if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){
printf("Failed to open output file! \n");
return -1;
}
/* 创建输出码流的AVStream */
video_st = avformat_new_stream(pFormatCtx, 0);
//video_st->time_base.num = 1;
//video_st->time_base.den = 25;
if (video_st==NULL){
return -1;
}
//Param that must set
pCodecCtx = video_st->codec;
//pCodecCtx->codec_id =AV_CODEC_ID_HEVC;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
#ifdef ENCODE_YUV
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
#else
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV422P;
#endif
pCodecCtx->width = in_w;
pCodecCtx->height = in_h;
pCodecCtx->bit_rate = 2000000;
pCodecCtx->gop_size=10;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;
//H264
//pCodecCtx->me_range = 16;
//pCodecCtx->max_qdiff = 4;
//pCodecCtx->qcompress = 0.6;
pCodecCtx->qmin = 10;
pCodecCtx->qmax = 51;
//Optional Param
pCodecCtx->max_b_frames=3;
// Set Option
AVDictionary *param = 0;
//H.264
if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
av_dict_set(¶m, "preset", "slow", 0);
/* 这个可以让libav不缓存视频帧 */
av_dict_set(¶m, "tune", "zerolatency", 0);
//av_dict_set(¶m, "profile", "main", 0);
}
//Show some Information
av_dump_format(pFormatCtx, 0, out_file, 1);
/* 查找编码器 */
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec){
printf("Can not find encoder! \n");
return -1;
}
/* 打开编码器 */
if (avcodec_open2(pCodecCtx, pCodec,¶m) < 0){
printf("Failed to open encoder! \n");
return -1;
}
pFrame = av_frame_alloc();
picture_size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
picture_buf = (uint8_t *)av_malloc(picture_size);
#ifdef ENCODE_YUV
avpicture_fill((AVPicture *)pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
#else
pFrame->format = pCodecCtx->pix_fmt;
pFrame->width = pCodecCtx->width;
pFrame->height = pCodecCtx->height;
av_image_alloc(pFrame->data, pFrame->linesize, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 32);
#endif
//Write File Header
/* 写文件头(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS) */
avformat_write_header(pFormatCtx,NULL);
/* Allocate the payload of a packet and initialize its fields with default values. */
av_new_packet(&pkt,picture_size);
y_size = pCodecCtx->width * pCodecCtx->height;
for (j=0; j< framenum; j++){
//Read raw YUV data
#ifdef ENCODE_YUV
if (fread(picture_buf, 1, y_size*3/2, in_file) <= 0){
#else
if (fread(picture_buf, 1, y_size*2, in_file) <= 0){
#endif
printf("Failed to read raw data! \n");
return -1;
}else if(feof(in_file)){
break;
}
#ifdef ENCODE_YUV
pFrame->data[0] = picture_buf; // Y
pFrame->data[1] = picture_buf + y_size; // U
pFrame->data[2] = picture_buf + y_size*5/4; // V
#else
num = y_size * 2 - 4;
index_y = 0;
index_u = 0;
index_v = 0;
y_ = pFrame->data[0];
u_ = pFrame->data[1];
v_ = pFrame->data[2];
in = picture_buf;
for(i=0; ipts=i;
pFrame->pts=j*(video_st->time_base.den)/((video_st->time_base.num)*25);
//Encode
/* 编码一帧视频。即将AVFrame(存储YUV像素数据)编码为AVPacket(存储H.264等格式的码流数据) */
int ret = avcodec_encode_video2(pCodecCtx, &pkt,pFrame, &got_picture);
if(ret < 0){
printf("Failed to encode! \n");
return -1;
}
if (got_picture==1){
//printf("Succeed to encode frame: %5d\tsize:%5d\n",framecnt,pkt.size);
framecnt++;
pkt.stream_index = video_st->index;
/* 将编码后的视频码流写入文件 */
ret = av_write_frame(pFormatCtx, &pkt);
av_free_packet(&pkt);
}
}
//Flush Encoder
/* 输入的像素数据读取完成后调用此函数。用于输出编码器中剩余的AVPacket */
ret = flush_encoder(pFormatCtx,0);
if (ret < 0) {
printf("Flushing encoder failed\n");
return -1;
}
//Write file trailer
/* 写文件尾(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS) */
av_write_trailer(pFormatCtx);
//Clean
if (video_st){
avcodec_close(video_st->codec);
av_free(pFrame);
av_free(picture_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
fclose(in_file);
return 0;
}
Makefile文件书写如下:
OUT_APP = test
INCLUDE_PATH = /usr/local/include/
INCLUDE = -I$(INCLUDE_PATH)libavutil/ -I$(INCLUDE_PATH)libavdevice/ \
-I$(INCLUDE_PATH)libavcodec/ -I$(INCLUDE_PATH)libswresample \
-I$(INCLUDE_PATH)libavfilter/ -I$(INCLUDE_PATH)libavformat \
-I$(INCLUDE_PATH)libswscale/
FFMPEG_LIBS = -lavformat -lavutil -lavdevice -lavcodec -lswresample -lavfilter -lswscale
SDL_LIBS =
LIBS = $(FFMPEG_LIBS)$(SDL_LIBS)
COMPILE_OPTS = $(INCLUDE)
C = c
OBJ = o
C_COMPILER = cc
C_FLAGS = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS)
LINK = cc -o
LINK_OPTS = -lz -lm -lpthread
LINK_OBJ = test.o
.$(C).$(OBJ):
$(C_COMPILER) -c -g $(C_FLAGS) $<
$(OUT_APP): $(LINK_OBJ)
$(LINK)$@ $(LINK_OBJ) $(LIBS) $(LINK_OPTS)
clean:
rm -rf *.$(OBJ) $(OUT_APP) core *.core *~ *.h264 yuyv.yuyv
编译运行结果如下:
licaibiao@ubuntu:~/test/FFMPEG/encoder$ ls
Makefile test test.c test.o yuv_512_288.yuv yuyv_640_480.yuyv
licaibiao@ubuntu:~/test/FFMPEG/encoder$ ./test
Output #0, h264, to 'ds.h264':
Stream #0:0: Unknown: none
[libx264 @ 0xbe7140] using cpu capabilities: none!
[libx264 @ 0xbe7140] profile High 4:2:2, level 3.0, 4:2:2 8-bit
[h264 @ 0xbde240] Using AVStream.codec.time_base as a timebase hint to the muxer is deprecated. Set AVStream.time_base instead.
[h264 @ 0xbde240] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
Flush Encoder: Succeed to encode 1 frame! size: 3131
Flush Encoder: Succeed to encode 1 frame! size: 5618
Flush Encoder: Succeed to encode 1 frame! size:16635
[libx264 @ 0xbe7140] frame I:30 Avg QP:18.56 size: 31571
[libx264 @ 0xbe7140] frame P:61 Avg QP:19.89 size: 15100
[libx264 @ 0xbe7140] frame B:209 Avg QP:21.10 size: 5299
[libx264 @ 0xbe7140] consecutive B-frames: 0.7% 19.3% 0.0% 80.0%
[libx264 @ 0xbe7140] mb I I16..4: 14.0% 40.4% 45.6%
[libx264 @ 0xbe7140] mb P I16..4: 8.7% 15.4% 9.8% P16..4: 36.2% 15.1% 5.2% 0.0% 0.0% skip: 9.6%
[libx264 @ 0xbe7140] mb B I16..4: 1.0% 4.0% 1.7% B16..8: 27.6% 9.3% 1.6% direct:10.3% skip:44.5% L0:41.6% L1:42.2% BI:16.2%
[libx264 @ 0xbe7140] final ratefactor: 17.64
[libx264 @ 0xbe7140] 8x8 transform intra:46.1% inter:53.3%
[libx264 @ 0xbe7140] direct mvs spatial:99.5% temporal:0.5%
[libx264 @ 0xbe7140] coded y,uvDC,uvAC intra: 74.5% 88.9% 70.6% inter: 14.6% 34.0% 5.8%
[libx264 @ 0xbe7140] i16 v,h,dc,p: 48% 9% 2% 42%
[libx264 @ 0xbe7140] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 12% 13% 6% 11% 11% 13% 10% 12% 11%
[libx264 @ 0xbe7140] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 12% 21% 6% 9% 14% 13% 10% 9% 7%
[libx264 @ 0xbe7140] i8c dc,h,v,p: 41% 24% 22% 13%
[libx264 @ 0xbe7140] Weighted P-Frames: Y:41.0% UV:19.7%
[libx264 @ 0xbe7140] kb/s:1983.80
licaibiao@ubuntu:~/test/FFMPEG/encoder$ ll
total 413032
drwxrwxr-x 2 licaibiao licaibiao 4096 Mar 18 04:24 ./
drwxrwxr-x 10 licaibiao licaibiao 4096 Mar 17 03:46 ../
-rw-rw-r-- 1 licaibiao licaibiao 2975705 Mar 18 04:24 ds.h264
-rwxrwxr-x 1 licaibiao licaibiao 800 Mar 18 01:40 Makefile*
-rwxrwxr-x 1 licaibiao licaibiao 61800 Mar 18 04:01 test*
-rw-rw-r-- 1 licaibiao licaibiao 7131 Mar 18 04:22 test.c
-rw-rw-r-- 1 licaibiao licaibiao 95184 Mar 18 04:01 test.o
-rw-r--r-- 1 licaibiao licaibiao 112582656 Mar 16 18:35 yuv_512_288.yuv
-rw-r--r-- 1 licaibiao licaibiao 307200000 Mar 17 04:25 yuyv_640_480.yuyv
licaibiao@ubuntu:~/test/FFMPEG/encoder$
生成了ds.h264文件,使用VLC播放器播放:
pFrame->format = pCodecCtx->pix_fmt;
pFrame->width = pCodecCtx->width;
pFrame->height = pCodecCtx->height;
av_image_alloc(pFrame->data, pFrame->linesize, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 32);
在我的例子中,有下面的代码,它的作用就是讲YUYV数据格式转换成YUV422P格式然后在填充到编码器中去,具体的装换算法在以前的博客中已经有介绍。
for(i=0; i