緊接上一章內容,將視頻文件添加一個紅色方框後文件轉成了YUV數據,這一節就再處理下YUV數據,編碼成H.264文件。總體流程也比較簡單,源碼以下:markdown
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
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 = fopen("/Users/liuwei/Desktop/new_test.yuv", "rb"); //Input raw YUV data
int in_w = 568, in_h = 320; //new_test.yuv的寬高
int framenum = 259; //Frames to encode,這個值是我將本來的視頻文件test.mp4解碼爲YUV文件時獲得的數據
const char *out_file = "/Users/liuwei/Desktop/new_test.h264";
//Method1.
pFormatCtx = avformat_alloc_context();
//Guess Format根據輸出文件後綴獲取已註冊輸出格式列表中的輸出格式,若是沒有匹配的,則返回NULL。
fmt = av_guess_format(NULL, out_file, NULL);
if (fmt == NULL){
return -1;
}
pFormatCtx->oformat = fmt;
//打開輸出文件
if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
printf("Failed to open output file! \n");
return -1;
}
//建立流
video_st = avformat_new_stream(pFormatCtx, 0);
if (video_st == NULL) {
return -1;
}
// 根據視頻編解碼器獲取對應的編碼器
pCodec = avcodec_find_encoder(fmt->video_codec);
if (!pCodec) {
printf("Can not find encoder! \n");
return -1;
}
//
pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
//設置參數
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
pCodecCtx->width = in_w;
pCodecCtx->height = in_h;
pCodecCtx->bit_rate = 400000; // 碼率
pCodecCtx->gop_size = 250; // 每250幀產生一個關鍵幀,new_test.yuv只有259幀,表示最後只有2個I幀
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); //壓縮速度慢,保證視頻質量
av_dict_set(¶m, "tune", "zerolatency", 0);//零延遲
}
//H.265
if (pCodecCtx->codec_id == AV_CODEC_ID_H265) {
av_dict_set(¶m, "preset", "ultrafast", 0);
av_dict_set(¶m, "tune", "zero-latency", 0);
}
//Show some Information
av_dump_format(pFormatCtx, 0, out_file, 1);
if (avcodec_open2(pCodecCtx, pCodec, ¶m) < 0) {
printf("Failed to open encoder! \n");
return -1;
}
pFrame = av_frame_alloc();
//返回使用給定參數存儲圖像所需的數據量的大小
picture_size = av_image_get_buffer_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 4);
picture_buf = (uint8_t *) av_malloc(picture_size);
//分配內存
av_image_fill_arrays(pFrame->data, pFrame->linesize, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width,
pCodecCtx->height, 1);
//Write File Header
avformat_write_header(pFormatCtx, NULL);
av_new_packet(&pkt, picture_size);
y_size = pCodecCtx->width * pCodecCtx->height;
for (int i = 0; i < framenum; i++) {
//從文件中讀取YUV數據
if (fread(picture_buf, 1, y_size * 3 / 2, in_file) <= 0) {
printf("Failed to read raw data! \n");
return -1;
} else if (feof(in_file)) {
break;
}
pFrame->data[0] = picture_buf; // Y
pFrame->data[1] = picture_buf + y_size; // U
pFrame->data[2] = picture_buf + y_size * 5 / 4; // V
//PTS
pFrame->pts = i * (video_st->time_base.den) / ((video_st->time_base.num) * 25);
//編碼
avcodec_send_frame(pCodecCtx, pFrame);
int ret = avcodec_receive_packet(pCodecCtx, &pkt);
if (ret == 0) {
printf("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, pkt.size);
framecnt++;
pkt.stream_index = video_st->index;
av_write_frame(pFormatCtx, &pkt);
}
av_packet_unref(&pkt);
}
//Write file trailer
av_write_trailer(pFormatCtx);
//Clean
avcodec_free_context(&pCodecCtx);
av_free(pFrame);
av_free(picture_buf);
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
fclose(in_file);
return 0;
}
複製代碼
壓縮效果很是明顯,84M的YUV視頻壓縮成H264後只有492KB。ide