| | |
| | | #include <libavutil/imgutils.h>
|
| | | #include <libavutil/opt.h>
|
| | | #include <libavformat/avformat.h>
|
| | | |
| | | #include <libyuv.h>
|
| | | }
|
| | |
|
| | | #include <libyuv.h>
|
| | |
|
| | | PL_H264Encoder_Config::PL_H264Encoder_Config() :
|
| | | inBufferSize(2*1024*1024), // 2MByte
|
| | | resetPTS(false),
|
| | | bytesBufferImageWidth(0), bytesBufferImageHeight(0),
|
| | | avc_bit_rate(1*1024*1024*8), //1Mbit
|
| | | avc_fps(25), avc_gop(25), avc_max_b_frames(0), avc_profile(FF_PROFILE_H264_MAIN), |
| | | av_opt_preset("superfast"), av_opt_tune("")
|
| | | avc_fps(25), avc_gop(25), avc_max_b_frames(0), avc_profile(FF_PROFILE_H264_BASELINE), |
| | | av_opt_preset("superfast"), av_opt_tune(""), avc_profile_str("")
|
| | | {
|
| | | // av_opt_tune: zerolatency
|
| | | }
|
| | |
| | |
|
| | | ~H264Encoder_Internal()
|
| | | {
|
| | | delete[] buffer;
|
| | | buffer = nullptr;
|
| | | }
|
| | |
|
| | | void reset()
|
| | |
| | | pAVFormatContext = nullptr;
|
| | |
|
| | | if (buffer != nullptr)
|
| | | {
|
| | | delete[] buffer;
|
| | | buffer = new uint8_t[config.inBufferSize];
|
| | | buffer = nullptr;
|
| | | }
|
| | | }
|
| | | };
|
| | |
|
| | |
| | | in->config = *config;
|
| | | }
|
| | |
|
| | | in->buffer = new uint8_t[in->config.inBufferSize];
|
| | | |
| | | return true;
|
| | | }
|
| | |
|
| | |
| | |
|
| | | if (!avCodec)
|
| | | {
|
| | | LOG_ERROR << "codec not found!"; |
| | | LOG_ERROR << "codec not found!" << std::endl; |
| | | return false;
|
| | | }
|
| | |
|
| | |
| | | in->pAVCodecContext->time_base.den = in->config.avc_fps;
|
| | | in->pAVCodecContext->gop_size = in->config.avc_gop;
|
| | | in->pAVCodecContext->max_b_frames = in->config.avc_max_b_frames;
|
| | | in->pAVCodecContext->profile = in->config.avc_profile;
|
| | | in->pAVCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
|
| | |
|
| | | if (!in->config.av_opt_preset.empty())
|
| | | av_opt_set(in->pAVCodecContext->priv_data, "preset", in->config.av_opt_preset.c_str(), 0);
|
| | | if (!in->config.av_opt_tune.empty())
|
| | | av_opt_set(in->pAVCodecContext->priv_data, "tune", in->config.av_opt_tune.c_str(), 0);
|
| | | if (!in->config.avc_profile_str.empty())
|
| | | av_opt_set(in->pAVCodecContext->priv_data, "profile", in->config.avc_profile_str.c_str(), 0);
|
| | | else
|
| | | in->pAVCodecContext->profile = in->config.avc_profile;
|
| | |
|
| | | if(avcodec_open2(in->pAVCodecContext, avCodec, NULL) >= 0)
|
| | | {
|
| | |
| | | in->pAVCodecContext->pix_fmt, 16);
|
| | | if (ret < 0)
|
| | | {
|
| | | LOG_ERROR << "av_image_alloc error";
|
| | | LOG_ERROR << "av_image_alloc error" << std::endl;
|
| | | return false;
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | LOG_ERROR << "avcodec_open2 error";
|
| | | LOG_ERROR << "avcodec_open2 error" << std::endl;
|
| | | return false;
|
| | | }
|
| | |
|
| | |
| | | int ret = avcodec_encode_video2(in->pAVCodecContext, &pAVPacket, in->pAVFrame, &gotPacket);
|
| | | if (ret < 0)
|
| | | {
|
| | | LOG_WARN << "avcodec_encode_video2 (1) error=" << ret;
|
| | | LOG_WARN << "avcodec_encode_video2 (1) error=" << ret << std::endl;
|
| | | return false;
|
| | | }
|
| | |
|
| | |
| | | {
|
| | | in->frameCount++;
|
| | | LOGP(DEBUG, "Succeed to encode (1) frame=%d, size=%d", in->frameCount, pAVPacket.size);
|
| | | memcpy(in->buffer, pAVPacket.data, pAVPacket.size);
|
| | | memcpy(in->buffer, pAVPacket.data, pAVPacket.size);//#todo check inBufferSize
|
| | | in->buffSize = pAVPacket.size;
|
| | | av_free_packet(&pAVPacket);
|
| | | }
|
| | |
| | | bool ret = initH264EncoderEnv(in);
|
| | | if (!ret)
|
| | | {
|
| | | LOG_ERROR << "initH264EncoderEnv error";
|
| | | LOG_ERROR << "initH264EncoderEnv error" << std::endl;
|
| | | return false;
|
| | | }
|
| | | else
|
| | |
| | |
|
| | | if (pm.type != PipeMaterial::PMT_FRAME)
|
| | | {
|
| | | LOG_ERROR << "Only support PMT_FRAME";
|
| | | LOG_ERROR << "Only support PMT_FRAME" << std::endl;
|
| | | return false;
|
| | | }
|
| | |
|
| | |
| | | ret = encodeH264(in, (uint8_t*)(frame->buffer), frame->pts);
|
| | | else
|
| | | {
|
| | | LOG_ERROR << "Only support MBFT_PTR_AVFRAME / MBFT_YUV420";
|
| | | LOG_ERROR << "Only support MBFT_PTR_AVFRAME / MBFT_YUV420" << std::endl;
|
| | | in->payError = true;
|
| | | return false;
|
| | | }
|
| | |
| | | in->lastFrame.buffSize = in->buffSize;
|
| | | in->lastFrame.width = frame->width;
|
| | | in->lastFrame.height = frame->height;
|
| | | in->lastFrame.pts = frame->pts;
|
| | | //#todo resetPts
|
| | |
|
| | | if (in->config.resetPTS)
|
| | | gettimeofday(&(in->lastFrame.pts),NULL);
|
| | | else
|
| | | in->lastFrame.pts = frame->pts;
|
| | | }
|
| | |
|
| | | return ret;
|