Blame view

src/decoder/gb28181/FFDecoder.cpp0 3.77 KB
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  #include "FFDecoder.h"
  
  #include "./rtp/demuxer.h"
  
  #include "common_header.h"
  #include "../nvdec/FFCuContextManager.h"
  
  static AVPixelFormat get_hw_format(AVCodecContext *avctx, const AVPixelFormat *pix_fmts){
  	return AV_PIX_FMT_CUDA;
  }
  
  FFDecoder::FFDecoder(/* args */)
  {
  }
  
  FFDecoder::~FFDecoder()
  {
  }
  
  FrameData* FFDecoder::Decode(int videoType, char* data, int len, int isKey, uint64_t pts) {
  
      AVPacket* pkt = av_packet_alloc();
  	av_init_packet(pkt);
  
      pkt->size = len;
      pkt->data = (uint8_t*)data;
  
      // ffmpeg 解码
      FrameData* frame_data = ff_decode(videoType, pkt);
  
      av_packet_free(&pkt);
      pkt = nullptr;
  
      frame_data.bKeyframe = (isKey != 0);
      frame_data.pts = pts;
  
      return frame_data;
  }
  
  FrameData* FFDecoder::ff_decode(int videoType, AVPacket* pkt) {
      if (m_pAVCodecCtx == nullptr) {
          LOG_INFO("frame data is zero --{}", m_dec_name);
  		if (VIDEO_TYPE_H264 == videoType) {
              if (m_devid >= 0){
                  m_pAVCodec = avcodec_find_decoder_by_name("h264_cuvid");
              }
              else{
  				m_pAVCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
  			}
  			LOG_INFO("m_avCodecName is H264");		
  		}
  		else if (VIDEO_TYPE_H265 == videoType)
  		{
              if (m_devid >= 0){
                  m_pAVCodec = avcodec_find_decoder_by_name("hevc_cuvid");
              }
              else{
                  m_pAVCodec = avcodec_find_decoder(AV_CODEC_ID_H265);
              }
  			LOG_INFO("m_avCodecName is H265");
  		}
  		else{
  			LOG_INFO("m_avCodecName is unknown, videoType is {}", videoType);
  		}
  
  		if (!m_pAVCodec)
  		{
  			LOG_ERROR("frameCallback frame decode error, ERROR_DECODER_NOT_FOUND");
  			return nullptr;return;
  		}
  
          m_pAVCodecCtx = avcodec_alloc_context3(m_pAVCodec);
  
          if (m_devid >= 0) {
              char gpui[8] = { 0 };
              sprintf(gpui, "%d", m_devid);
              av_dict_set(&gpu_options, "gpu", gpui, 0);
  
              m_pAVCodecCtx->get_format = get_hw_format;
  
              FFCuContextManager* pCtxMgr = FFCuContextManager::getInstance();
              m_pAVCodecCtx->hw_device_ctx = av_buffer_ref(pCtxMgr->getCuCtx(gpui));
              if (nullptr == m_pAVCodecCtx->hw_device_ctx){
                  // TODO 这里应该抛出错误
                  return nullptr;
              }
          }
  
          if (avcodec_open2(m_pAVCodecCtx, m_pAVCodec, &gpu_options) < 0)
  			return nullptr;
  	}
  
      //开始解码
      int ret = avcodec_send_packet(m_pAVCodecCtx, pkt);
      if (ret < 0) {
          //send_exception(RunMessageType::E2002, e_msg);
          LOG_ERROR("Real stream视频解码失败,请检查视频设备{}: avcodec_send_packet failed. ret={}", m_dec_name, ret);
          return;
      }
   
      if (frameW < 1) {
          frameW = m_pAVCodecCtx->width;
          frameH = m_pAVCodecCtx->height;
          if (frameW <= 0 || frameH <= 0) {
              LOG_ERROR("[{}] frame W or H is error! ({},{})", m_dec_name, frameW, frameH);
              return nullptr;
          }	
      }
  	// m_fps = m_pAVCodecCtx->pkt_timebase.den == 0 ? 25.0 : av_q2d(m_pAVCodecCtx->pkt_timebase);
      m_fps = av_q2d(m_pAVCodecCtx->framerate);
  	// LOG_DEBUG("frameW {}--frameH {}", frameW, frameH);
  
      FrameData* pData = new FrameData();
      ret = avcodec_receive_frame(m_pAVCodecCtx, pData->frame);
      if ((ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) || ret < 0){
          LOG_ERROR("{} - Failed to receive frame: {}", m_dec_name, ret);
          delete pData;
          pData = nullptr;
          return nullptr;
      }
      
      AVFrame* gpuFrame = pData->frame;
      if (gpuFrame->width != frameW || gpuFrame->height != frameH){
          LOG_INFO("AVFrame is inconsistent: width is {}, height is {}; original frameW is {}, frameH is {}--{}", gpuFrame->width, gpuFrame->height, frameW, frameH , m_dec_name);
          delete pData;
          pData = nullptr;
          return nullptr;
      }
  
      return pData;
  }