评论

收藏

[C++] 通过ffmpeg把RGBA数据保存为mp4

编程语言 编程语言 发布于:2021-07-11 10:49 | 阅读数:302 | 评论:0

#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
#include <stdint.h>
#include "libavcodec/jni.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libswscale/swscale.h"
#include "libavutil/opt.h"
#include "libavutil/log.h"
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1)
#define av_frame_alloc  avcodec_alloc_frame
#endif
struct SwsContext *pEncoderSwsCtx = NULL;
AVFormatContext *pEncoderFormatCtx = NULL;
AVCodecContext *pEncoderCtx = NULL;
AVFrame *pFrameYUV = NULL;
AVPacket *pEncoderPacket = NULL;
int64_t pts = 0;
bool ffmpeg_encode_init(const char *filename)
{
av_register_all();
avcodec_register_all();
AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;//AV_CODEC_ID_H264 only support AV_PIX_FMT_YUV420P
AVCodec *videoCodec = avcodec_find_encoder_by_name("nvenc_h264");//H264硬编码
if(videoCodec==NULL) {
videoCodec = avcodec_find_encoder_by_name("libx264rgb");
if (videoCodec != NULL) {
pix_fmt = AV_PIX_FMT_BGR24;//libx264rgb can support AV_PIX_FMT_BGR24
} else {
videoCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (videoCodec == NULL) {
LOGI("avcodec_find_encoder failed!");
return false;
}
}
}
LOGI("encoder: %s",videoCodec->name);
pEncoderCtx = avcodec_alloc_context3(videoCodec);//创建编解码器
if(!pEncoderCtx) {
LOGE("avcodec_alloc_context3 error!\n");
return false;
}
pEncoderCtx->width = nWidth;
pEncoderCtx->height = nHeight;
pEncoderCtx->bit_rate = 40000000;
pEncoderCtx->thread_count = 8;
int fps = 25;//把1秒钟分成fps个单位
pEncoderCtx->time_base = av_make_q(1, fps);
pEncoderCtx->framerate = av_make_q(fps, 1);
pEncoderCtx->gop_size = 50;//画面组大小,就是多少帧出现一个关键帧
pEncoderCtx->has_b_frames = 0;
pEncoderCtx->max_b_frames = 0;
pEncoderCtx->pix_fmt = pix_fmt;
pEncoderCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
av_opt_set(pEncoderCtx->priv_data,"preset","superfast",0);
av_opt_set(pEncoderCtx->priv_data,"tune","zerolatency",0);
if (avcodec_open2(pEncoderCtx, videoCodec, NULL) < 0) {//打开视频解码器
LOGE("Couldn't open Encodec.\n");
return false;
}
avformat_alloc_output_context2(&pEncoderFormatCtx, 0, 0, filename);
// add video stream
AVStream *pStream = avformat_new_stream(pEncoderFormatCtx, NULL);
pStream->id = 0;
pStream->codecpar->codec_tag = 0;
avcodec_parameters_from_context(pStream->codecpar,pEncoderCtx);
pEncoderSwsCtx = sws_getContext(pEncoderCtx->width, pEncoderCtx->height,
AV_PIX_FMT_RGBA, pEncoderCtx->width, pEncoderCtx->height,
pEncoderCtx->pix_fmt, SWS_FAST_BILINEAR,
NULL, NULL, NULL);
pFramePIX = av_frame_alloc();
uint8_t *out_buffer = (unsigned char *) av_malloc(
av_image_get_buffer_size(pEncoderCtx->pix_fmt, pEncoderCtx->width, pEncoderCtx->height, 1));
av_image_fill_arrays(pFramePIX->data, pFramePIX->linesize, out_buffer,
pEncoderCtx->pix_fmt, pEncoderCtx->width, pEncoderCtx->height, 1);
pEncoderPacket = (AVPacket *) av_malloc(sizeof(AVPacket));
if(avio_open(&pEncoderFormatCtx->pb,filename,AVIO_FLAG_WRITE)<0) {
LOGI("avio_open failed!");
return false;
}
if(avformat_write_header(pEncoderFormatCtx, NULL)<0) {
LOGI("avformat_write_header failed!");
return false;
}
return true;
}
bool ffmpeg_encode_frame(uint8_t *rgba)
{
if(rgba==NULL)
return false;
struct timeval tv;
gettimeofday(&tv,NULL);
long start = tv.tv_sec*1000 + tv.tv_usec/1000;
uint8_t *data[AV_NUM_DATA_POINTERS] = {0};
data[0] = rgba;
int linesize[AV_NUM_DATA_POINTERS] = {0};
linesize[0] = pEncoderCtx->width*4;
sws_scale(pEncoderSwsCtx, data, linesize, 0, pEncoderCtx->height, pFramePIX->data, pFramePIX->linesize);
pFramePIX->pts = pts;
pts+=90000/av_q2d(pEncoderCtx->framerate);
gettimeofday(&tv,NULL);
LOGI("sws_scale: %ld\r\n", tv.tv_sec*1000 + tv.tv_usec/1000-start);
start = tv.tv_sec*1000 + tv.tv_usec/1000;
if (avcodec_send_frame(pEncoderCtx, pFramePIX) == 0) {
gettimeofday(&tv,NULL);
LOGI("avcodec_send_frame: %ld\r\n", tv.tv_sec*1000 + tv.tv_usec/1000-start);
start = tv.tv_sec*1000 + tv.tv_usec/1000;
av_init_packet(pEncoderPacket);
if(avcodec_receive_packet(pEncoderCtx, pEncoderPacket)==0) {
gettimeofday(&tv,NULL);
LOGI("avcodec_receive_packet: %ld\r\n", tv.tv_sec*1000 + tv.tv_usec/1000-start);
start = tv.tv_sec*1000 + tv.tv_usec/1000;
av_interleaved_write_frame(pEncoderFormatCtx,pEncoderPacket);
gettimeofday(&tv,NULL);
LOGI("av_interleaved_write_frame: %ld\r\n", tv.tv_sec*1000 + tv.tv_usec/1000-start);
}
av_packet_unref(pEncoderPacket);
}
return true;
}
void ffmpeg_encode_release()
{
av_write_trailer(pEncoderFormatCtx);
avio_close(pEncoderFormatCtx->pb);
avformat_free_context(pEncoderFormatCtx);
sws_freeContext(pEncoderSwsCtx);
av_frame_free(&pFramePIX);
avcodec_close(pEncoderCtx);
avcodec_free_context(&pEncoderCtx);
av_packet_free(&pEncoderPacket);
avformat_close_input(&pEncoderFormatCtx);
}
  
关注下面的标签,发现更多相似文章