| | |
| | | #include <libavutil/avassert.h> |
| | | #include <libavutil/imgutils.h> |
| | | #include <libswscale/swscale.h> |
| | | #include <libavutil/intreadwrite.h> |
| | | } |
| | | |
| | | #include "../log/log.hpp" |
| | |
| | | ,read_io_buff_size_(32768) |
| | | ,handle_gb28181(NULL) |
| | | ,fps_(25.0) |
| | | ,bsf_h264(NULL) |
| | | ,bsf_hevc(NULL) |
| | | {} |
| | | |
| | | FormatIn::FormatIn(const VideoProp &prop, bool hw/*=true*/) |
| | |
| | | ,read_io_buff_size_(32768) |
| | | ,handle_gb28181(NULL) |
| | | ,fps_(25.0) |
| | | ,bsf_h264(NULL) |
| | | ,bsf_hevc(NULL) |
| | | { |
| | | prop_ = new VideoProp; |
| | | *prop_ = prop; |
| | |
| | | io_ctx_ = NULL; |
| | | } |
| | | |
| | | if (bsf_h264) av_bsf_free(&bsf_h264); |
| | | if (bsf_hevc) av_bsf_free(&bsf_hevc); |
| | | } |
| | | |
| | | //////////////////////////////////////////////////////////////////////// |
| | |
| | | fps_ = av_q2d(in->avg_frame_rate); |
| | | } |
| | | logIt("in stream video fps %f, time_base: %d : %d, size: %dx%d", fps_, in->time_base.num, in->time_base.den, in->codecpar->width, in->codecpar->height); |
| | | |
| | | if (IsHEVC()){ |
| | | |
| | | }else if (isAVC1()) { |
| | | 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, in->codecpar) >= 0){ |
| | | if (av_bsf_init(bsf_h264) < 0) bsf_h264 = NULL; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (type == AVMEDIA_TYPE_AUDIO){ |
| | | auto in = ctx_->streams[i]; |
| | |
| | | int FormatIn::readPacket(AVPacket *pkt_out){ |
| | | |
| | | auto flag = av_read_frame(ctx_, pkt_out); |
| | | if (flag < 0) return flag; |
| | | |
| | | AVBSFContext *bsf = NULL; |
| | | |
| | | if (IsHEVC()){ |
| | | if (pkt_out->size >= 5 && |
| | | AV_RB32(pkt_out->data) != 0x0000001 && |
| | | AV_RB24(pkt_out->data) != 0x000001 && |
| | | !bsf_hevc){ |
| | | |
| | | const AVBitStreamFilter *f = av_bsf_get_by_name("hevc_mp4toannexb"); |
| | | if (f){ |
| | | if (av_bsf_alloc(f, &bsf_hevc) >= 0){ |
| | | if (avcodec_parameters_copy(bsf_hevc->par_in, ctx_->streams[vs_idx_]->codecpar) >= 0){ |
| | | if (av_bsf_init(bsf_hevc) < 0) bsf_hevc = NULL; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | bsf = bsf_hevc; |
| | | |
| | | }else { |
| | | bsf = bsf_h264; |
| | | } |
| | | |
| | | if (bsf){ |
| | | if (av_bsf_send_packet(bsf, pkt_out) < 0){ |
| | | logIt("bsf_%s send packet failed", IsHEVC() ? "hevc" : "h264"); |
| | | return -1; |
| | | } |
| | | return av_bsf_receive_packet(bsf, pkt_out); |
| | | } |
| | | |
| | | return flag; |
| | | } |
| | | |
| | |
| | | struct AVFrame; |
| | | struct AVCodec; |
| | | struct AVIOContext; |
| | | struct AVBSFContext; |
| | | |
| | | typedef int(* read_packet)(void *opaque,uint8_t *buf, int buf_size); |
| | | |
| | |
| | | int vs_idx_; |
| | | int as_idx_; |
| | | |
| | | AVBSFContext *bsf_h264, *bsf_hevc; |
| | | |
| | | VideoProp *prop_; |
| | | bool hw_accl_; |
| | | double fps_; |
| | |
| | | ,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(){ |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | bool FormatOut::writeFrameInternal(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; |
| | | } |
| | | int ret; |
| | | while((ret = av_bsf_receive_packet(bsf_h264, pkt)) == 0){ |
| | | if (!write_frame(ctx_, pkt, interleaved)) return false; |
| | | } |
| | | |
| | | if (ret == AVERROR(EAGAIN)) return true; |
| | | } |
| | | |
| | | return write_frame(ctx_, pkt, interleaved); |
| | | } |
| | |
| | | struct AVFrame; |
| | | struct AVPacket; |
| | | struct AVDictionary; |
| | | struct AVBSFContext; |
| | | |
| | | namespace ffwrapper{ |
| | | class VideoProp; |
| | |
| | | double fps_; |
| | | std::string format_name_; |
| | | |
| | | AVBSFContext *bsf_h264, *bsf_hevc; |
| | | // rec |
| | | AVStream *in_v_stream_; |
| | | AVStream *in_a_stream_; |