Created
July 28, 2020 03:41
-
-
Save prabindh/1587d4b6cde284d30b082df2e4fb7df1 to your computer and use it in GitHub Desktop.
calling ffmpeg encoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define OUTPUT_W 1920 | |
#define OUTPUT_H 1080 | |
#define OUTPUT_FPS 25 | |
int main() | |
{ | |
int ret = 0; | |
bool bUseHW = false; | |
bool bNv = false; | |
Encoder* pEnc = new Encoder(bUseHW, bNv, OUTPUT_W, OUTPUT_H, OUTPUT_FPS); | |
format = AV_PIX_FMT_YUV420P; | |
size = avpicture_get_size(format, OUTPUT_W, OUTPUT_H); | |
picture = av_frame_alloc(); | |
picture_buffer = (uint8_t*)(av_malloc(size)); | |
ret = avpicture_fill((AVPicture *)picture, picture_buffer, format, | |
OUTPUT_W, OUTPUT_H); | |
picture = av_frame_alloc(); | |
if (!picture) { | |
fprintf(stderr, "Could not allocate video frame\n"); | |
exit(1); | |
} | |
picture->format = format; | |
picture->width = OUTPUT_W; | |
picture->height = OUTPUT_H; | |
ret = av_frame_get_buffer(picture, 0); | |
if (ret < 0) { | |
fprintf(stderr, "Could not allocate the video frame data\n"); | |
exit(1); | |
} | |
for (int i = 0; i < 600; i++) | |
{ | |
prepare_dummy_420P(OUTPUT_W, OUTPUT_H, i, picture); | |
pEnc->addFrame(picture); | |
} | |
pEnc->flush(); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//
#include "encoder.h"
extern "C" {
}
Encoder::Encoder(const bool hwAccel, bool bNv, int w, int h, int fps)
: m_hardwareAcceleration(hwAccel)
{
m_width = w;
m_height = h;
m_fps = fps;
setup(bNv);
}
void Encoder::addFrame(AVFrame* frame)
{
AVFrame* frameToEncode = frame;
if (m_hardwareAcceleration) {
filterFrame(frame, m_hwFrame);
assert(m_hwFrame->format == AV_PIX_FMT_VAAPI);
frameToEncode = m_hwFrame;
}
}
void Encoder::flush()
{
encodeFrame(nullptr);
av_write_trailer(m_muxer);
}
void Encoder::setup(bool bNv)
{
AVOutputFormat * outFmt = av_guess_format("mp4", NULL, NULL);
assert(avformat_alloc_output_context2(&m_muxer, outFmt, nullptr, nullptr) == 0);
}
void Encoder::setupEncoder(bool bNv)
{
const char* encoderName = m_hardwareAcceleration ? "h264_vaapi" : "libx264";
AVCodec* videoCodec = nullptr;
AVRational timeBase, frameRate;
#if 0
m_encoder->hw_device_ctx = nullptr;
AVBufferRef* bufferRef = av_buffersink_get_hw_frames_ctx(m_bufferSink);
m_encoder->hw_frames_ctx = av_buffer_ref(bufferRef);
#else
{
AVBufferRef *hw_frames_ref;
AVHWFramesContext *frames_ctx = NULL;
#endif
}
}
void Encoder::initFilters(bool bNv)
{
AVFilterInOut* inputs = nullptr;
AVFilterInOut* outputs = nullptr;
m_filterGraph = avfilter_graph_alloc();
}
void Encoder::initInputFilters(AVFilterInOut* inputs, bool bNv)
{
assert(inputs != nullptr);
assert(inputs->next == nullptr);
}
void Encoder::initOutputFilters(AVFilterInOut* outputs, bool bNv)
{
assert(outputs != nullptr);
assert(outputs->next == nullptr);
}
void Encoder::filterFrame(AVFrame* inFrame, AVFrame* outFrame)
{
assert(av_buffersrc_add_frame_flags(m_bufferSrc, inFrame, AV_BUFFERSRC_FLAG_KEEP_REF) == 0);
assert(av_buffersink_get_frame(m_bufferSink, outFrame) == 0);
}
void Encoder::encodeFrame(AVFrame* frame)
{
assert(avcodec_send_frame(m_encoder, frame) == 0);
}
// Results in [h264_nvenc @ 00007FFF05513D40] Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format