xuxiuxi
2017-05-11 109ffe9a777658936a38d0c146579a67c60a0d17
RtspFace/PL_H264Decoder.cpp
@@ -1,5 +1,6 @@
#include "PL_H264Decoder.h"
#include "MaterialBuffer.h"
#include "logger.h"
#include <H264VideoRTPSource.hh> // for SPropRecord
#include <libbase64.h>
@@ -21,14 +22,16 @@
   AVCodecContext* pAVCodecContext;
   AVFrame* pAVFrame;//#todo delete
   MB_Frame lastFrame;
   PL_H264Decoder_Config config;
   
   H264Decoder_Internal() : 
      //buffSize(0), buffSizeMax(sizeof(buffer)), 
      fmtp_set_to_context(false), 
      payError(true), 
      pAVCodecContext(nullptr), pAVFrame(nullptr), 
      lastFrame()
      lastFrame(), config()
   {
   }
   
@@ -42,8 +45,14 @@
      fmtp_set_to_context = false;
      payError = true;
      
      pAVCodecContext = nullptr;
      pAVFrame = nullptr;
      MB_Frame _lastFrame;
      lastFrame = _lastFrame;
      PL_H264Decoder_Config _config;
      config = _config;
   }
};
@@ -66,6 +75,12 @@
{
   H264Decoder_Internal* in = (H264Decoder_Internal*)internal;
   in->reset();
   if (args)
   {
      PL_H264Decoder_Config* config = (PL_H264Decoder_Config*)args;
      in->config = *config;
   }
   
   return true;
}
@@ -121,19 +136,20 @@
   if (!avCodec)   
   {  
      printf("codec not found!\n");
      LOG_WARN << "codec not found!" << std::endl;
      return false;  
   }  
   in->pAVCodecContext = avcodec_alloc_context3(avCodec);
   in->pAVCodecContext->time_base.num = 1;
   in->pAVCodecContext->frame_number = 1;
   in->pAVCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
   in->pAVCodecContext->bit_rate = 0;
   in->pAVCodecContext->time_base.den = 25;
   in->pAVCodecContext->width = 1920;//#todo get from pm
   in->pAVCodecContext->height = 1080;
   // this is only reference for codec
   //in->pAVCodecContext->frame_number = 1;
   //in->pAVCodecContext->bit_rate = 0;
   //in->pAVCodecContext->time_base.num = 1;
   //in->pAVCodecContext->time_base.den = 25;
   //in->pAVCodecContext->width = 1920;
   //in->pAVCodecContext->height = 1080;
   if (in->pAVCodecContext->extradata == NULL)  
    {  
@@ -169,7 +185,7 @@
   if (av_packet_from_data(&packet, buffer, buffSize) != 0)
   {  
      printf("av_packet_from_data error\n");
      LOG_WARN << "av_packet_from_data error" << std::endl;
      return false;
   }
   
@@ -184,7 +200,8 @@
   }
   else
   {
      printf("incomplete frame\n");
      //#todo  sps sps changing
      LOG_WARN << "incomplete frame" << std::endl;
      return false;
   }
}
@@ -200,46 +217,64 @@
      if (manager == NULL)
         return false;
      
      std::string fmtp(manager->get_global_param(PLGP_RTSP_FMTP));
      std::string fmtp(manager->get_param(PLGP_RTSP_FMTP));
      if (fmtp.empty())
         return false;
      
      size_t numSPropRecords = 0;
      SPropRecord *p_record = parseSPropParameterSets(fmtp.c_str(), numSPropRecords);
      if (numSPropRecords < 2)
         return false;//#todo log
      {
         LOG_WARN << "numSPropRecords < 2" << std::endl;
         return false;
      }
      SPropRecord &sps = p_record[0];  
      SPropRecord &pps = p_record[1];
      bool ret = initH264DecoderEnv(in, sps.sPropBytes, sps.sPropLength, pps.sPropBytes, pps.sPropLength);
      if (!ret)
         return false; // #todo log
      {
         LOG_ERROR << "initH264DecoderEnv error" << std::endl;
         return false;
      }
      else
         in->fmtp_set_to_context = true;
   }
   
   if (pm.buffer == nullptr || pm.buffSize <= 0)
   if (pm.buffer == nullptr)
      return false;
   
   bool ret = false;
   if (pm.type == PipeMaterial::PMT_BYTES)
   {
      if (pm.buffSize <= 0)
         return false;
      timeval pts = {0};
      ret = decodeH264(in, pm.buffer, pm.buffSize, pts);
      ret = decodeH264(in, (uint8_t*)pm.buffer, pm.buffSize, pts);
   }
   else if (pm.type == PipeMaterial::PMT_FRAME)
   {
      MB_Frame* frame = (MB_Frame*)pm.buffer;
      
      ret = decodeH264(in, frame->buffer, frame->buffSize, frame->pts);
      if (frame->buffSize <= 0)
         return false;
      ret = decodeH264(in, (uint8_t*)frame->buffer, frame->buffSize, frame->pts);
      if (ret)
      {
         in->lastFrame.type = MB_Frame::MBFT_PTR_AVFRAME;
         in->lastFrame.buffer = (uint8_t*)(in->pAVFrame);
         in->lastFrame.buffSize = sizeof(in->pAVFrame);
         //in->lastFrame.pts = frame->pts;//#todo
         gettimeofday(&(in->lastFrame.pts),NULL);
         in->lastFrame.width = in->pAVFrame->width;
         in->lastFrame.height = in->pAVFrame->height;
         if (in->config.resetPTS)
            gettimeofday(&(in->lastFrame.pts),NULL);
         else
            in->lastFrame.pts = frame->pts;
      }
   }
   
@@ -254,8 +289,8 @@
   if (!in->payError)
   {
      pm.type = PipeMaterial::PMT_FRAME;
      pm.buffer = (uint8_t*)(&(in->lastFrame));
      pm.buffSize = sizeof(uint8_t*);
      pm.buffer = &(in->lastFrame);
      pm.buffSize = 0;
   }
   pm.former = this;
   return !in->payError;