From 9eebd36d36b1c680f9673f3057c0639a99a7400b Mon Sep 17 00:00:00 2001
From: pansen <pansen626@sina.com>
Date: 星期六, 09 三月 2019 18:43:21 +0800
Subject: [PATCH] ????

---
 QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp |  128 +++++++++++++++++++++++-------------------
 1 files changed, 71 insertions(+), 57 deletions(-)

diff --git a/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp b/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
index 5106cbf..81d4fa7 100644
--- a/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
+++ b/QiaoJiaSystem/GB28181DecoderModel/FFmpegDecoderJPG.cpp
@@ -44,7 +44,6 @@
 }
 
 BASICGB28181::FFmpegDecoderJPG::~FFmpegDecoderJPG() {
-
     while (m_rtpQueue.count_queue()) {
         m_rtpQueue.popNotWait();
     }
@@ -65,10 +64,20 @@
         info->buffLen = datalen;
         info->camIdx = camIdx;
         memcpy(info->buff, data, datalen);
+    {
+//        FILE *fp11 = NULL;
+//        if (!fp11) {
+//        fp11 = fopen(camIdx.c_str(), "a+");
+//    }
+//        fwrite(data, sizeof(char), datalen, fp11);
+//        fclose(fp11);
+    }
+
 #ifdef TestCode
         DBG(" m_rtpQueue.push before ");
 #endif
-        m_rtpQueue.push(info);
+        m_rtpQueue.
+        push(info);
 #ifdef TestCode
         DBG(" m_rtpQueue.push after ");
 #endif
@@ -135,39 +144,41 @@
 void BASICGB28181::FFmpegDecoderJPG::BareFlowDecoderThd(FFmpegDecoderJPG *p_this) {
     DBG(p_this->m_camIdx << "  BareFlowDecoderThd ok ... gpuIdx is " << p_this->m_gpuIdx);
     p_this->m_running = true;
-    av_register_all();
-    avformat_network_init();
-    AVFormatContext *ic = avformat_alloc_context();
+//    av_register_all();
+//    avformat_network_init();
+    p_this->ic = avformat_alloc_context();
 
-    unsigned char *iobuffer = (unsigned char *) av_malloc(p_this->m_buf_size);
-    AVIOContext *avio = avio_alloc_context(iobuffer, p_this->m_buf_size, 0, p_this, p_this->read_data, NULL, NULL);
-    ic->pb = avio;
+    p_this->iobuffer = (unsigned char *) av_malloc(p_this->m_buf_size);
+    p_this->avio = avio_alloc_context(p_this->iobuffer, p_this->m_buf_size, 0, p_this, p_this->read_data, NULL, NULL);
+    p_this->ic->pb = p_this->avio;
 
-    int err = av_probe_input_buffer(ic->pb, &ic->iformat, nullptr, nullptr, 0, p_this->m_buf_size);
-    int err1 = avformat_open_input(&ic, "", NULL, NULL);
-    int err2 = avformat_find_stream_info(ic, nullptr);
+    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);
+//    int err2 = avformat_find_stream_info(ic, nullptr);
+    int err2 = avformat_find_stream_info(p_this->ic, NULL);
     int vi = -1;
-    for (int i = 0; i < ic->nb_streams; ++i) {
-        if (ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+    for (int i = 0; i < p_this->ic->nb_streams; ++i) {
+        if (p_this->ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
             vi = i;
             break;
         }
     }
-    AVStream *stream = ic->streams[vi];
-    p_this->video_st = stream;
-    AVCodecContext *ctx = avcodec_alloc_context3(nullptr);
-    int err3 = avcodec_parameters_to_context(ctx, stream->codecpar);
+    p_this->stream = p_this->ic->streams[vi];
+    p_this->video_st = p_this->stream;
+    p_this->ctx = avcodec_alloc_context3(nullptr);
+    int err3 = avcodec_parameters_to_context(p_this->ctx, p_this->stream->codecpar);
 
-    AVCodec *codec = avcodec_find_decoder(ctx->codec_id);
+    p_this->codec = avcodec_find_decoder(p_this->ctx->codec_id);
     //鏄惁鍚敤GPU
     if (p_this->m_gpuIdx >= 0) {
-        if (codec != NULL) {
+        if (p_this->codec != NULL) {
             char cuvidName[40] = {0};
-            sprintf(cuvidName, "%s_cuvid", codec->name);
-            if (!strcmp(codec->name, "h264") || !strcmp(codec->name, "h265") || !strcmp(codec->name, "hevc")) {
-                AVCodec *codec_cuvid = avcodec_find_decoder_by_name(cuvidName);
-                if (codec_cuvid != NULL) {
-                    codec = codec_cuvid;
+            sprintf(cuvidName, "%s_cuvid", p_this->codec->name);
+            if (!strcmp(p_this->codec->name, "h264") || !strcmp(p_this->codec->name, "h265") ||
+                !strcmp(p_this->codec->name, "hevc")) {
+                p_this->codec_cuvid = avcodec_find_decoder_by_name(cuvidName);
+                if (p_this->codec_cuvid != NULL) {
+                    p_this->codec = p_this->codec_cuvid;
                 } else {
 //                    return false;
                     ERR("codec_cuvid is NULL");
@@ -175,12 +186,11 @@
             }
         }
     }
-    int err4 = avcodec_open2(ctx, codec, nullptr);
+    int err4 = avcodec_open2(p_this->ctx, p_this->codec, nullptr);
 
-    AVPacket pkt;
-    av_init_packet(&pkt);
+    av_init_packet(&p_this->pkt);
 
-    AVFrame *frame = av_frame_alloc();
+    p_this->frame = av_frame_alloc();
     unsigned int usleepTime = (1000 / p_this->m_fps) - 12;
     usleepTime *= 1000;
     DBG(" before while <<usleepTime is " << usleepTime);
@@ -188,13 +198,16 @@
 #ifdef TestCode
         ClockTimer Test("while time");
 #endif
-        int err5 = av_read_frame(ic, &pkt);
+        av_packet_unref(&p_this->pkt);
+        p_this->pkt.data = NULL;
+        p_this->pkt.size = 0;
+
+        int err5 = av_read_frame(p_this->ic, &p_this->pkt);
         //# todo save package
         p_this->frame_number++;
 
-        int err6 = avcodec_send_packet(ctx, &pkt);
-        av_packet_unref(&pkt);
-        int err7 = avcodec_receive_frame(ctx, frame);
+        int err6 = avcodec_send_packet(p_this->ctx, &p_this->pkt);
+        int err7 = avcodec_receive_frame(p_this->ctx, p_this->frame);
         if ((err7 == AVERROR(EAGAIN)) || (err5 < 0) || (err6 < 0)) {
             ERR(" error << err7:" << err7 << "  err5: " << err5 << " err6: " << err6);
             usleep(40000);
@@ -202,11 +215,11 @@
         }
         //DBG("GotPicture "<<m_camId<<":"<<frame_number);
         //鏀惧湪姝ゅ鏄洜涓轰箣鍓嶆斁鍦ㄥ墠闈�,鏀跺埌鐨勫抚涓嶅畬鎴�
-        p_this->SaveToPacketVector(pkt);
+        p_this->SaveToPacketVector(p_this->pkt);
         p_this->CheckSave();
 
 //        BASICGB28181::avframe_to_cvmat(frame).copyTo(p_this->m_image);
-        p_this->m_image = std::move(BASICGB28181::avframe_to_cvmat(frame));
+        p_this->m_image = std::move(BASICGB28181::avframe_to_cvmat(p_this->frame));
 
 #ifdef TestCode
         {
@@ -262,7 +275,7 @@
 
     }
     DBG(" after while ");
-    av_frame_free(&frame);
+    av_frame_free(&p_this->frame);
 }
 
 bool BASICGB28181::FFmpegDecoderJPG::startThd(const std::string &camIdx, const int &fps, const int &gpuIdx) {
@@ -446,6 +459,7 @@
     m_frameIndex++;
     m_packetsVec.push_back({m_frameIndex, newPacket});
     if (newPacket.flags & AV_PKT_FLAG_KEY) {
+        DBG("newPacket.flags & AV_PKT_FLAG_KEY   ");
         m_last_I_FrameId = m_frameIndex;
     }
 }
@@ -466,27 +480,27 @@
     */
     m_pOutVideo_stream = avformat_new_stream(m_pOutFmtCtx, NULL);
     {
-//        AVCodecContext *c;
-//        c = m_pOutVideo_stream->codec;
-//        c->bit_rate = 400000;
-//        c->codec_id = video_st->codec->codec_id;
-//        c->codec_type = video_st->codec->codec_type;
-//        c->time_base.num = video_st->time_base.num;
-//        c->time_base.den = video_st->time_base.den;
-//        fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);
-//        c->width = video_st->codec->width;
-//        c->height = video_st->codec->height;
-//        c->pix_fmt = video_st->codec->pix_fmt;
-//        printf("%d %d %d", c->width, c->height, c->pix_fmt);
-//        c->flags = video_st->codec->flags;
-//        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
-//        c->me_range = video_st->codec->me_range;
-//        c->max_qdiff = video_st->codec->max_qdiff;
-//
-//        c->qmin = video_st->codec->qmin;
-//        c->qmax = video_st->codec->qmax;
-//
-//        c->qcompress = video_st->codec->qcompress;
+        AVCodecContext *c;
+        c = m_pOutVideo_stream->codec;
+        c->bit_rate = 400000;
+        c->codec_id = video_st->codec->codec_id;
+        c->codec_type = video_st->codec->codec_type;
+        c->time_base.num = video_st->time_base.num;
+        c->time_base.den = video_st->time_base.den;
+        fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);
+        c->width = video_st->codec->width;
+        c->height = video_st->codec->height;
+        c->pix_fmt = video_st->codec->pix_fmt;
+        printf("%d %d %d", c->width, c->height, c->pix_fmt);
+        c->flags = video_st->codec->flags;
+        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
+        c->me_range = video_st->codec->me_range;
+        c->max_qdiff = video_st->codec->max_qdiff;
+
+        c->qmin = video_st->codec->qmin;
+        c->qmax = video_st->codec->qmax;
+
+        c->qcompress = video_st->codec->qcompress;
     }
     ret = avio_open(&m_pOutFmtCtx->pb, filename, AVIO_FLAG_WRITE);
     if (ret < 0) {
@@ -494,7 +508,7 @@
         return -1;
     }
 
-    avformat_write_header(m_pOutFmtCtx, NULL);
+    DBG(" avformat_write_header   " << avformat_write_header(m_pOutFmtCtx, NULL));
 
     m_bstartWrite = true;
     m_bFirstKeyFrame = true;

--
Gitblit v1.8.0