Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从基带(ffmpeg)编码帧时,avcodec_encode_video2出现分段错误_C_Image Processing_Ffmpeg_Segmentation Fault_Video Encoding - Fatal编程技术网

从基带(ffmpeg)编码帧时,avcodec_encode_video2出现分段错误

从基带(ffmpeg)编码帧时,avcodec_encode_video2出现分段错误,c,image-processing,ffmpeg,segmentation-fault,video-encoding,C,Image Processing,Ffmpeg,Segmentation Fault,Video Encoding,我最近在尝试从基带编码帧时遇到了一个问题。分段错误发生在avcodec_encode_video2调用上,但我不知道原因 这是我用于初始化AvcodeContext的函数: void create_codec_context( Header *hdr, AVCodecContext *cc, AVStream *st, AVCodec *codec) { switch (hdr->stream_info) { case (Li

我最近在尝试从基带编码帧时遇到了一个问题。分段错误发生在avcodec_encode_video2调用上,但我不知道原因

这是我用于初始化AvcodeContext的函数:

void
create_codec_context(
    Header *hdr,
    AVCodecContext *cc,
    AVStream *st,
    AVCodec *codec)
{
    switch (hdr->stream_info)
    {
        case (Line1080i60):
            cc->time_base.num = 1;
            cc->time_base.den = 30;
            break;
        case (Line1080i5994):
            cc->time_base.num = 1001;
        cc->time_base.den = 30000;
        break;
        case (Line1080i50):
            cc->time_base.num = 1;
            cc->time_base.den = 25;
            break;
        case (Line720p60):
            cc->time_base.num = 1;
            cc->time_base.den = 60;
            break;
        case (Line1080p60):
            cc->time_base.num = 1;
            cc->time_base.den = 60;
            break;
        case (Line720p5994):
            cc->time_base.num = 1001;
            cc->time_base.den = 60000;
            break;
        case (Line1080p5994):
            cc->time_base.num = 1001;
            cc->time_base.den = 60000;
            break;
        case (Line720p50):
            cc->time_base.num = 1;
            cc->time_base.den = 50;
        case (Line1080p50):
            cc->time_base.num = 1;
            cc->time_base.den = 50;
            break;
        default:
            syslog(LOG_ERR, "Unknown local socket header stream info (VidStd): %d", hdr->stream_info);
            exit(EXIT_FAILURE);
            break;
    }

    cc->width = hdr->width;
    cc->height = hdr->height;

    st->r_frame_rate.num = cc->time_base.den;
    st->r_frame_rate.den = cc->time_base.num;

    cc->codec_type = AVMEDIA_TYPE_VIDEO;
    cc->codec_id = codec->id;
    cc->bit_rate = (int64_t)cc->width * (cc->height >> !!hdr->interlaced) * 2 * 8 * cc->time_base.den / cc->time_base.num;
    cc->pix_fmt = hdr->pix_fmt;

    st->time_base.num = 1;
    st->time_base.den = 90000;

    if (cc->codec_id == AV_CODEC_ID_RAWVIDEO)
    {
        cc->codec_tag = get_raw_pix_fmt_tag(cc->pix_fmt);
        if (cc->codec_tag == 0)
        {
            syslog(LOG_ERR, "Failed to get codec tag for raw video pix fmt: %d", cc->pix_fmt);
            exit(EXIT_FAILURE);
        }
    }

    cc->max_b_frames = 0;
    cc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
}
这是我对每一帧的处理循环:

size_t
process_bb_data(
    uint8_t *src,
    uint8_t *dst)
{
    Header *src_hdr = (Header *)src;
    uint8_t *src_y = src + sizeof(Header);
    uint8_t *src_u = src_y + (src_hdr->Y_linesize * src_hdr->height);
    uint8_t *src_v = src_u + (src_hdr->U_linesize * src_hdr->height);

    Header *dst_hdr = (Header *)dst;
    uint8_t *dst_y = dst + sizeof(Header);
    uint8_t *dst_u = dst_y + (dst_hdr->Y_linesize * dst_hdr->height);
    uint8_t *dst_v = dst_u + (dst_hdr->U_linesize * dst_hdr->height);

    memcpy(dst_hdr, src_hdr, sizeof(Header));

    memcpy(dst_y, src_y, dst_hdr->Y_linesize * dst_hdr->height);
    memcpy(dst_u, src_u, dst_hdr->U_linesize * dst_hdr->height);
    memcpy(dst_v, src_v, dst_hdr->V_linesize * dst_hdr->height);

    av_register_all();
    avcodec_register_all();

    AVCodec *codec = avcodec_find_encoder_by_name("mpeg2video");
    AVCodecContext *cc = avcodec_alloc_context3(codec);
    avcodec_get_context_defaults3(cc, codec)

    AVFormatContext *fmt_ctx = avformat_alloc_context();
    AVStream *st = avformat_new_stream(fmt_ctx, NULL);

    /* My function from above */
    create_codec_context(dst_hdr, cc, st, codec);

    AVFrame *frame = av_frame_alloc();
    frame->width = dst_hdr->width;
    frame->height = dst_hdr->height;
    frame->format = dst_hdr->pix_fmt;
    frame->data[0] = dst_y;
    frame->data[1] = dst_u;
    frame->data[2] = dst_v;
    frame->linesize[0] = dst_hdr->Y_linesize;
    frame->linesize[1] = dst_hdr->U_linesize;
    frame->linesize[2] = dst_hdr->V_linesize;
    int ret;
    int got_packet = 0;

    AVPacket pkt = {0};
    av_init_packet(&pkt);
    pkt.data = NULL; //dst_y;
    pkt.size = 0; //(dst_hdr->Y_linesize + dst_hdr->U_linesize + dst_hdr->V_linesize) * dst_hdr->height;

    /* SEGMENTATION FAULT HERE */
    ret = avcodec_encode_video2(cc, &pkt, frame, &got_packet);

    if (got_packet)
    {
        ret = av_write_frame(fmt_ctx, &pkt);
        av_packet_unref(&pkt);
    }
    else
    {
        ret = 0;
    }

    if (ret < 0)
    {
        exit(EXIT_FAILURE);
    }

    return (sizeof(Header) + dst_hdr->data_size);
}
size\t
处理bb_数据(
uint8_t*src,
uint8_t*dst)
{
页眉*src_hdr=(页眉*)src;
uint8_t*src_y=src+sizeof(表头);
uint8_t*src_=src_y+(src_hdr->y_线条尺寸*src_hdr->height);
uint8_t*src_v=src_+(src_hdr->u_线条尺寸*src_hdr->height);
标题*dst_hdr=(标题*)dst;
uint8_t*dst_y=dst+sizeof(表头);
uint8_t*dst_=dst_y+(dst_hdr->y_线条尺寸*dst_hdr->height);
uint8_t*dst_v=dst_+(dst_hdr->u_线条尺寸*dst_hdr->高度);
memcpy(dst_hdr、src_hdr、sizeof(Header));
memcpy(dst_y、src_y、dst_hdr->y_线条尺寸*dst_hdr->高度);
memcpy(dst_、src_、dst_hdr->u_线条尺寸*dst_hdr->高度);
memcpy(dst_v、src_v、dst_hdr->v_线条尺寸*dst_hdr->高度);
av_寄存器_all();
avcodec_寄存器_all();
AVCodec*codec=AVCodec按名称查找编码器(“mpeg2video”);
AVCodecContext*cc=avcodec\u alloc\u context3(编解码器);
avcodec_get_context_defaults3(抄送,编解码器)
AVFormatContext*fmt_ctx=avformat_alloc_context();
AVStream*st=avformat\U new\U stream(fmt\U ctx,NULL);
/*我的功能来自上面*/
创建编解码器上下文(dst、hdr、cc、st、编解码器);
AVFrame*frame=av_frame_alloc();
帧->宽度=dst_hdr->宽度;
帧->高度=dst_hdr->高度;
帧->格式=dst\U hdr->pix\U fmt;
帧->数据[0]=dst_y;
帧->数据[1]=dst_;
帧->数据[2]=dst_v;
帧->线宽[0]=dst\U hdr->Y\U线宽;
帧->线宽[1]=dst\U hdr->U\U线宽;
帧->线宽[2]=dst_hdr->V_线宽;
int ret;
int got_packet=0;
AVPacket pkt={0};
av_初始_数据包(&pkt);
pkt.data=NULL;//dst_y;
pkt.size=0;//(dst_hdr->Y_线条尺寸+dst_hdr->U线条尺寸+dst_hdr->V_线条尺寸)*dst_hdr->高度;
/*这里有分段错误*/
ret=avcodec_encode_video2(cc、pkt、帧和got_数据包);
如果(得到_数据包)
{
ret=av写入帧(fmt、ctx和pkt);
av_数据包_unref(&pkt);
}
其他的
{
ret=0;
}
如果(ret<0)
{
退出(退出失败);
}
返回(sizeof(Header)+dst_hdr->data_size);
}

我是不是错过了一些非常愚蠢的事情?

哇,刚刚发现了问题。。。女士们先生们,不要忘记您的avcodec\u open2(抄送,编解码器,空)通话