From d979bf53d93dc3feb6697a519b12a0bc2756d18f Mon Sep 17 00:00:00 2001
From: xuxiuxi <554325746@qq.com>
Date: 星期四, 14 三月 2019 10:38:46 +0800
Subject: [PATCH] Merge branch 'yw.1.2.fixbug' of http://192.168.1.226:10010/r/development/c++ into yw.1.2.fixbug

---
 QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp |   75 +++++++++++++++++++++++++++----------
 1 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp b/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
index 81d4fa7..2d5455d 100644
--- a/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
+++ b/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
@@ -153,9 +153,18 @@
     p_this->ic->pb = p_this->avio;
 
     int err = av_probe_input_buffer(p_this->ic->pb, &p_this->ic->iformat, nullptr, nullptr, 0, p_this->m_buf_size);
-    int err1 = avformat_open_input(&p_this->ic, "", NULL, NULL);
+    if(err){
+        ERR("av_probe_input_buffer" << err);
+    }
+    err = avformat_open_input(&p_this->ic, "", NULL, NULL);
+    if(err){
+        ERR("avformat_open_input" << err);
+    }
 //    int err2 = avformat_find_stream_info(ic, nullptr);
-    int err2 = avformat_find_stream_info(p_this->ic, NULL);
+    err = avformat_find_stream_info(p_this->ic, NULL);
+    if(err){
+        ERR("avformat_find_stream_info" << err);
+    }
     int vi = -1;
     for (int i = 0; i < p_this->ic->nb_streams; ++i) {
         if (p_this->ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
@@ -379,7 +388,7 @@
             delete iter->m_packet.data;
             iter = m_packetsVec.erase(iter);
             while (!(iter->m_packet.flags & AV_PKT_FLAG_KEY)) {
-//                INFO("DropFrame: " << iter->m_frameId);
+                INFO("DropFrame: " << iter->m_frameId);
                 delete iter->m_packet.data;
                 iter = m_packetsVec.erase(iter);
             }
@@ -390,15 +399,17 @@
 
 bool BASICGB28181::FFmpegDecoderJPG::SaveVideo(std::string path, int64_t lastFrameId) {
     std::lock_guard<std::mutex> lock(g_mutex);
-    INFO("SaveVideo: " << path);
+    INFO("SaveVideo: " << path << "m_packetsVec.size : " <<m_packetsVec.size());
     if (!m_packetsVec.empty()) {
         startWrite(path.c_str());
         int64_t firstKeyFramePts = m_packetsVec[0].m_packet.pts;
         int64_t firstKeyFrameDts = m_packetsVec[0].m_packet.dts;
+        unsigned long int frame_index = 0;
         for (const auto &item:m_packetsVec) {
             if (item.m_frameId < lastFrameId) {
                 DBG("item.m_frameId < lastFrameId   " << item.m_frameId << "  " << lastFrameId);
-                conversion(const_cast<AVPacket *> (&item.m_packet), firstKeyFramePts, firstKeyFrameDts, video_st);
+                conversion(const_cast<AVPacket *> (&item.m_packet), firstKeyFramePts, firstKeyFrameDts, video_st, frame_index);
+                frame_index++;
                 av_write_frame(m_pOutFmtCtx, &item.m_packet);
             } else {
                 DBG("item.m_frameId > lastFrameId   " << item.m_frameId << "  " << lastFrameId);
@@ -521,11 +532,12 @@
 int BASICGB28181::FFmpegDecoderJPG::stopWrite() {
     if (m_pOutFmtCtx == nullptr) return -1;
     av_write_trailer(m_pOutFmtCtx);
-    avio_close(m_pOutFmtCtx->pb);
+
     avcodec_close(m_pOutFmtCtx->streams[0]->codec);
     av_freep(&m_pOutFmtCtx->streams[0]->codec);
     av_freep(&m_pOutFmtCtx->streams[0]);
 
+    avio_close(m_pOutFmtCtx->pb);
     av_free(m_pOutFmtCtx);
     m_pOutFmtCtx = nullptr;
     m_bstartWrite = false;
@@ -534,23 +546,44 @@
 
 
 void BASICGB28181::FFmpegDecoderJPG::conversion(void *packet, const long int &firstKeyPts, const long int &firstKeyDts,
-                                                void *inVideoStream) {
+                                                void *inVideoStream, unsigned long int frame_index) {
     if ((packet != nullptr) && (inVideoStream != nullptr)) {
         AVStream *inStream = (AVStream *) inVideoStream;
         AVPacket *pkg = static_cast<AVPacket *>(packet);
-//            static int a = 0;
-//            pkg->dts = a++;
-//            pkg->pts = a;
-        pkg->pts -= firstKeyPts;
-        pkg->dts -= firstKeyDts;
-        pkg->pts = av_rescale_q_rnd(pkg->pts, inStream->time_base,
-                                    m_pOutVideo_stream->time_base,
-                                    (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
-        pkg->dts = av_rescale_q_rnd(pkg->dts, inStream->time_base,
-                                    m_pOutVideo_stream->time_base,
-                                    (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
-        pkg->duration = av_rescale_q(pkg->duration, inStream->time_base,
-                                     m_pOutVideo_stream->time_base);
-        pkg->pos = -1;
+//        static unsigned  long int frame_index = 0
+//            pkg->dts = ++frame_index;
+//            pkg->pts = frame_index;
+
+            //todo
+//        if(pkg->pts==AV_NOPTS_VALUE){
+            DBG("frame_index==%d\n" << frame_index);
+            //Write PTS
+            AVRational time_base1=inStream->time_base;
+            //Duration between 2 frames (us)
+            int64_t calc_duration=(double)AV_TIME_BASE/av_q2d(inStream->r_frame_rate);
+            //Parameters
+            pkg->pts=(double)(frame_index*calc_duration)/(double)(av_q2d(time_base1)*AV_TIME_BASE);
+            pkg->dts=pkg->pts;
+            pkg->duration=(double)calc_duration/(double)(av_q2d(time_base1)*AV_TIME_BASE);
+//            frame_index++;
+//        }
+//        else{
+//            DBG("pts" << pkg->pts << "firstKeyPts" << firstKeyPts);
+//            DBG("dts" << pkg->dts << "firstKeyDts" << firstKeyDts);
+//            pkg->pts -= firstKeyPts;
+//            pkg->dts -= firstKeyDts;
+//            pkg->pts = av_rescale_q_rnd(pkg->pts, inStream->time_base,
+//                                        m_pOutVideo_stream->time_base,
+//                                        (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
+//            pkg->dts = av_rescale_q_rnd(pkg->dts, inStream->time_base,
+//                                        m_pOutVideo_stream->time_base,
+//                                        (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
+//            pkg->duration = av_rescale_q(pkg->duration, inStream->time_base,
+//                                         m_pOutVideo_stream->time_base);
+//            pkg->pos = -1;
+//        }
+
+        DBG("pts:" << pkg->pts);
+        DBG("dts:" << pkg->dts);
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0