派生自 development/c++

pansen
2019-03-07 d3b7bbe7102cd089680a828f5d8f6402c8cf6342
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
@@ -128,6 +137,10 @@
    return bufsize;
}
/***
 * 解码线程
 * @param p_this
 */
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;
@@ -188,25 +201,25 @@
        //# todo save package
        p_this->frame_number++;
        //DBG("GotPicture "<<m_camId<<":"<<frame_number);
        //放在此处是因为之前放在前面,收到的帧不完成
        p_this->SaveToPacketVector(pkt);
        p_this->CheckSave();
        int err6 = avcodec_send_packet(ctx, &pkt);
        av_packet_unref(&pkt);
//        av_packet_unref(&pkt);
        int err7 = avcodec_receive_frame(ctx, frame);
        if ((err7 == AVERROR(EAGAIN)) || (err5 < 0) || (err6 < 0)) {
            ERR(" error << err7:" << err7 << "  err5: " << err5 << " err6: " << err6);
            usleep(40000);
            continue;
        }
        //DBG("GotPicture "<<m_camId<<":"<<frame_number);
        //放在此处是因为之前放在前面,收到的帧不完成
        p_this->SaveToPacketVector(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));
#ifdef TestCode
        {
//            TestCode
//            TestCode 测试代码保存图片到本地
            ClockTimer cl("TestCode");
            std::string strNewTime2 = AppUtil::getTimeUSecString();
            cv::putText(p_this->m_image, strNewTime2, cv::Point(408, 540), cv::HersheyFonts::FONT_HERSHEY_PLAIN,
@@ -228,7 +241,7 @@
#ifdef TestCode
        DBG("emitSigal(\"DecoderImageOK\") begin");
#endif
        //触发信号
        gSignalLock.emitSigal(p_this->m_camIdx + "DecoderImageOK");
//#ifdef TestCode
//        DBG("emitSigal(\"DecoderImageOK\") after");
@@ -278,7 +291,7 @@
bool BASICGB28181::FFmpegDecoderJPG::stopThd() {
    TryCath(
        DBG(m_camIdx << "  FFmpegDecoderJPG stopThd ... ");
        DBG(m_camIdx << "  FFmpegDecoderJPG stopThd ... " << m_camIdx);
        m_running = false;
    );
    return true;
@@ -380,9 +393,11 @@
        int64_t firstKeyFrameDts = m_packetsVec[0].m_packet.dts;
        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);
                av_write_frame(m_pOutFmtCtx, &item.m_packet);
            } else {
                DBG("item.m_frameId > lastFrameId   " << item.m_frameId << "  " << lastFrameId);
                break;
            }
        }
@@ -440,6 +455,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;
    }
}
@@ -460,27 +476,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) {
@@ -488,7 +504,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;