video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2020-10-09 d64868c215e35088bfeda67aeb04db0800bf2844
csrc/ffmpeg/format/FormatOut.cpp
@@ -33,11 +33,16 @@
    ,format_name_("mp4")
    ,in_v_stream_(NULL)
    ,in_a_stream_(NULL)
    ,bsf_h264(NULL)
    ,bsf_hevc(NULL)
   {}
   FormatOut::~FormatOut()
   {
      clear();
        if (bsf_h264) av_bsf_free(&bsf_h264);
        if (bsf_hevc) av_bsf_free(&bsf_hevc);
   }
    void FormatOut::clear(){
@@ -293,7 +298,7 @@
            logIt("Failed to copy context from input to output stream codec context\n");
            return false;
        }
        out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0;
        out_stream->codecpar->codec_tag = 0;
        
        if(ctx_->oformat->flags & AVFMT_GLOBALHEADER)
            out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
@@ -349,7 +354,23 @@
            flag = writeHeader(&avdic);
            av_dict_free(&avdic);
        }
        if (v){
            if (v->codecpar->codec_id == AV_CODEC_ID_H264){
                char p[100] = {0};
                char *sub = av_fourcc_make_string(p, v->codecpar->codec_tag);
                if (strcmp(sub, "avc1") == 0){
                    const AVBitStreamFilter *f = av_bsf_get_by_name("h264_mp4toannexb");
                    if (f){
                        if (av_bsf_alloc(f, &bsf_h264) >= 0){
                            if (avcodec_parameters_copy(bsf_h264->par_in, v->codecpar) >= 0){
                                if (av_bsf_init(bsf_h264) < 0) bsf_h264 = NULL;
                            }
                        }
                    }
                }
            }
        }
        return flag;
    }
    
@@ -399,7 +420,7 @@
        int out_idx = -1;
        std::vector<AVStream*> in_streams{in_v_stream_, in_a_stream_};
        for (auto i : in_streams){
            if (i->index == pkt->stream_index){
            if (i && (i->index == pkt->stream_index)){
                if (i->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
                    out_idx = v_idx_;
                    in_stream = i;
@@ -434,6 +455,12 @@
            pkt->pts = pkt->dts = pkt->duration * time_stamp; 
        }
        // logIt("BEFORE in stream timebase %d:%d, out timebase %d:%d,
        //         pts: %lld, dts: %lld, duration: %lld",
        //     in_stream->time_base.num, in_stream->time_base.den,
        //     out_stream->time_base.num, out_stream->time_base.den,
        //     pkt->pts, pkt->dts, pkt->duration);
    }
    bool FormatOut::writeFrame(AVPacket *pkt, const int64_t &frame_cnt,
@@ -450,6 +477,19 @@
    bool FormatOut::writeFrame2(AVPacket *pkt, bool interleaved){
        
        // h264_mp4toanatax
        if (bsf_h264){
            if (av_bsf_send_packet(bsf_h264, pkt) < 0){
                logIt("bsf_h264 send packet failed");
                return true;
            }
            if (av_bsf_receive_packet(bsf_h264, pkt) < 0){
                logIt("bsf_h264 recv packet failed");
                return true;
            }
        }
        //
        int ret = 0;
        if(interleaved){
            ret = av_interleaved_write_frame(ctx_, pkt);