Commit e01a039762b37c1c1012682a5d03031109655610

Authored by Hu Chunming
1 parent bf661eb0

代码优化;

src/decoder/dvpp/DvppDecoder.cpp
... ... @@ -245,8 +245,6 @@ void DvppDecoder::close(){
245 245 if(m_read_thread != 0){
246 246 pthread_join(m_read_thread,0);
247 247 }
248   -
249   - m_recoderManager.close();
250 248 }
251 249  
252 250 void DvppDecoder::setPostDecArg(const void* postDecArg){
... ... @@ -442,6 +440,8 @@ void DvppDecoder::read_thread() {
442 440 decode_finished_cbk(m_finishedDecArg);
443 441 }
444 442  
  443 + m_recoderManager.close();
  444 +
445 445 LOG_INFO("[{}]- read thread exit.", m_dec_name);
446 446 m_bFinished = true;
447 447 release_ffmpeg();
... ...
src/decoder/dvpp/FFRecoder.cpp
... ... @@ -24,7 +24,6 @@ FFRecoder::FFRecoder()
24 24  
25 25 FFRecoder::~FFRecoder()
26 26 {
27   - uninit();
28 27 }
29 28  
30 29  
... ... @@ -125,7 +124,7 @@ bool FFRecoder::init(AVStream* stream, AVCodecContext* avctx, const char* outfil
125 124  
126 125 codec_ctx_ = (AVCodecContext*)av_malloc(sizeof(AVCodecContext));
127 126 avcodec_copy_context(codec_ctx_, avctx);
128   - codec_ctx_->time_base = stream->time_base;
  127 + codec_ctx_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
129 128 m_inStream = stream;
130 129  
131 130 // [2] 创建输出上下文
... ... @@ -133,11 +132,19 @@ bool FFRecoder::init(AVStream* stream, AVCodecContext* avctx, const char* outfil
133 132  
134 133 // [3] 添加输出视频流
135 134 out_stream_ = avformat_new_stream(fmt_ctx_, nullptr);
  135 +
136 136 out_stream_->id = 0;
137 137 out_stream_->codecpar->codec_tag = 0;
138 138 avcodec_parameters_from_context(out_stream_->codecpar, codec_ctx_);
139   - // out_stream_->time_base = { 1,30 };
  139 + // out_stream_->time_base = { 1,25 };
140 140 out_stream_->time_base = stream->time_base;
  141 + out_stream_->r_frame_rate = stream->r_frame_rate;
  142 + out_stream_->avg_frame_rate = stream->r_frame_rate;
  143 +
  144 + codec_ctx_->time_base = out_stream_->time_base;
  145 +
  146 + av_opt_set(out_stream_->codec->priv_data, "preset", "ultrafast", 0);
  147 + av_opt_set(out_stream_->codec->priv_data, "tune", "zerolatency", 0);
141 148  
142 149 av_dump_format(fmt_ctx_, out_stream_->id, outfile_name, 1);
143 150  
... ... @@ -154,6 +161,18 @@ bool FFRecoder::init(AVStream* stream, AVCodecContext* avctx, const char* outfil
154 161 return true;
155 162 }
156 163  
  164 +void FFRecoder::release() {
  165 + av_write_trailer(fmt_ctx_);
  166 +
  167 + avcodec_close(fmt_ctx_->streams[0]->codec);
  168 + av_freep(&fmt_ctx_->streams[0]->codec);
  169 + av_freep(&fmt_ctx_->streams[0]);
  170 +
  171 + avio_close(fmt_ctx_->pb);
  172 + av_free(fmt_ctx_);
  173 + fmt_ctx_ = nullptr;
  174 +}
  175 +
157 176 void FFRecoder::uninit()
158 177 {
159 178 //if (out_buffer) {
... ... @@ -250,8 +269,7 @@ bool FFRecoder::write_pkt(AVPacket *pkt) {
250 269 // update_pts(pkt);
251 270 // pkt->stream_index = out_stream_->index;
252 271  
253   - // if(pkt->pts==AV_NOPTS_VALUE)
254   - {
  272 + if(pkt->pts==AV_NOPTS_VALUE) {
255 273 // printf("frame_index:%d\n", frame_index);
256 274 //Write PTS
257 275 AVRational time_base1 = codec_ctx_->time_base;
... ... @@ -262,15 +280,15 @@ bool FFRecoder::write_pkt(AVPacket *pkt) {
262 280 pkt->dts = pkt->pts;
263 281 pkt->duration = (double)calc_duration / (double)(av_q2d(time_base1)*AV_TIME_BASE);
264 282 frame_index++;
265   - }
  283 + }
266 284 // Convert PTS/DTS
267 285 pkt->pts = av_rescale_q_rnd(pkt->pts, codec_ctx_->time_base, out_stream_->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
268 286 pkt->dts = av_rescale_q_rnd(pkt->dts, codec_ctx_->time_base, out_stream_->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
269 287 pkt->duration = av_rescale_q(pkt->duration, codec_ctx_->time_base, out_stream_->time_base);
  288 +
270 289 pkt->pos = -1;
271   -
272 290 pkt->stream_index = out_stream_->index;
273   -
  291 + fmt_ctx_->duration += pkt->duration;
274 292  
275 293 // 将数据写入到输出流
276 294 int ret = av_interleaved_write_frame(fmt_ctx_, pkt);
... ... @@ -336,6 +354,11 @@ bool FFRecoder::flush()
336 354 return write_frame(nullptr);
337 355 }
338 356  
  357 +bool FFRecoder::flush_pkt()
  358 +{
  359 + return av_interleaved_write_frame(fmt_ctx_, nullptr);
  360 +}
  361 +
339 362 bool FFRecoder::bgr_to_yuv420p(const uint8_t* const buf_bgr, uint8_t* const buf_420p)
340 363 {
341 364 // 分配转换上下文
... ...
src/decoder/dvpp/FFRecoder.h
... ... @@ -26,6 +26,8 @@ public:
26 26 // AVPacket 方式
27 27 bool init(AVStream* stream, AVCodecContext* avctx, const char* outfile_name);
28 28 bool write_pkt(AVPacket *pkt);
  29 + bool flush_pkt();
  30 + void release();
29 31  
30 32 private:
31 33 bool bgr_to_yuv420p(const uint8_t* const buf_bgr, uint8_t* const buf_420p);
... ...
src/decoder/dvpp/FFRecoderTaskManager.cpp
... ... @@ -393,7 +393,7 @@ void FFRecoderTaskManager::recode_thread2() {
393 393 end_frame_nb = (*it_save)->frame_nb;
394 394 }
395 395  
396   - // ffrecoder.flush();
  396 + ffrecoder.flush_pkt();
397 397 ffrecoder.uninit();
398 398  
399 399 // 发送mq消息
... ...
src/decoder/test_recoder.cpp
... ... @@ -64,7 +64,7 @@ int main(){
64 64  
65 65 MgrDecConfig config;
66 66 config.name = task_id;
67   - config.cfg.uri = "rtsp://122.97.218.170:8604/openUrl/LBBYTra?params=eyJwcm90b2NhbCI6InJ0c3AiLCJjbGllbnRUeXBlIjoib3Blbl9hcGkiLCJleHByaWVUaW1lIjotMSwicHJvdG9jb2wiOiJydHNwIiwiZXhwaXJlVGltZSI6MzAwLCJlbmFibGVNR0MiOnRydWUsImV4cGFuZCI6InN0YW5kYXJkPXJ0c3Amc3RyZWFtZm9ybT1ydHAiLCJhIjoiOTgzYjRjMmUxMThlNGU1OTlkYThmMTI3NTkyMGViODV8MXwwfDEiLCJ0IjoxfQ==";
  67 + config.cfg.uri = "rtsp://admin:ad123456@192.168.60.165:554/cam/realmonitor?channel=1&subtype=0";
68 68 config.cfg.post_decoded_cbk = post_decod_cbk;
69 69 config.cfg.decode_finished_cbk = decode_finished_cbk;
70 70 config.cfg.force_tcp = true; // rtsp用tcp
... ...