pans
2016-12-30 eb68a1e3ad5fb24b4c674b8f2fd529b8302c7d97
RtspFace/PL_H264Encoder.cpp
@@ -5,8 +5,9 @@
{
   #include <libavcodec/avcodec.h>
   #include <libavutil/frame.h>
   #include <libavformat/avformat.h>
   #include <libavutil/imgutils.h>
   #include <libavutil/opt.h>
   #include <libavformat/avformat.h>
   
   #include <libyuv.h>
}
@@ -106,10 +107,11 @@
    in->pAVCodecContext->time_base.den=25;
    in->pAVCodecContext->gop_size = 25;
    in->pAVCodecContext->max_b_frames = 0;
   //in->pAVCodecContext->profile = FF_PROFILE_H264_MAIN;
    in->pAVCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
   
   //av_opt_set(c->priv_data, "preset", "superfast", 0);
   //av_opt_set(c->priv_data, "tune", "zerolatency", 0);
   av_opt_set(in->pAVCodecContext->priv_data, "preset", "superfast", 0);
   //av_opt_set(in->pAVCodecContext->priv_data, "tune", "zerolatency", 0);
   if(avcodec_open2(in->pAVCodecContext, avCodec, NULL) >= 0)
   {
@@ -261,6 +263,17 @@
   return !(in->payError);
}
bool encodeH264(H264Encoder_Internal* in, uint8_t* buffer, timeval pts)
{
   AVFrame avFrame;
   avFrame.width = 1920;//#todo
   avFrame.height = 1080;
   avFrame.data[0] = buffer;
   avFrame.data[1] = buffer + 1920*1080;
   avFrame.data[2] = buffer + 1920*1080 + 1920*1080/4;
   return encodeH264(in, &avFrame, pts);
}
bool PL_H264Encoder::pay(const PipeMaterial& pm)
{
   H264Encoder_Internal* in = (H264Encoder_Internal*)internal;
@@ -289,13 +302,20 @@
      return false;
   
   MB_Frame* frame = (MB_Frame*)pm.buffer;
   if (frame->type != MB_Frame::MBFT_PTR_AVFRAME)
   bool ret;
   if (frame->type == MB_Frame::MBFT_PTR_AVFRAME)
      ret = encodeH264(in, (AVFrame*)(frame->buffer), frame->pts);
   else if (frame->type == MB_Frame::MBFT_YUV420)
      ret = encodeH264(in, (uint8_t*)(frame->buffer), frame->pts);
   else
   {
      printf("PL_H264Encoder::pay only support MBFT_PTR_AVFRAME\n");
      printf("PL_H264Encoder::pay only support MBFT_PTR_AVFRAME / MBFT_YUV420\n");
      in->payError = true;
      return false;
   }
   bool ret = encodeH264(in, (AVFrame*)(frame->buffer), frame->pts);
   in->payError = !ret;
   
   if (ret)
@@ -303,6 +323,8 @@
      in->lastFrame.type = MB_Frame::MBFT_H264_NALU;
      in->lastFrame.buffer = in->buffer;
      in->lastFrame.buffSize = in->buffSize;
      in->lastFrame.width = frame->width;
      in->lastFrame.height = frame->height;
      in->lastFrame.pts = frame->pts;
   }
   
@@ -316,9 +338,8 @@
   if (!in->payError)
   {
      pm.type = PipeMaterial::PMT_FRAME;
      pm.buffer = (uint8_t*)(&(in->lastFrame));
      pm.buffSize = sizeof(in->lastFrame);
      pm.former = this;
      pm.buffer = &(in->lastFrame);
      pm.buffSize = 0;
   }
   pm.former = this;
   return !in->payError;