加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – FFMPEG:编码后直接解码数据包

发布时间:2020-12-16 07:30:04 所属栏目:百科 来源:网络整理
导读:使用FFMPEG API,我尝试将x264视频编码为具有0帧延迟的MP4文件,并且还实时地在屏幕上显示当前编码的帧(具有编码工件).对文件的编码有效,但到目前为止,我没有在将文件写入文件后立即解码.我尝试将从avcodec_encode_video()返回的数据包数据直接输入avcodec_dec
使用FFMPEG API,我尝试将x264视频编码为具有0帧延迟的MP4文件,并且还实时地在屏幕上显示当前编码的帧(具有编码工件).对文件的编码有效,但到目前为止,我没有在将文件写入文件后立即解码.我尝试将从avcodec_encode_video()返回的数据包数据直接输入avcodec_decode_video2(),但函数返回-1,cmd输出显示:

[h264 @ 00000000025F0710] non-existing PPS 0 referenced
[h264 @ 00000000025F0710] decode_slice_header error
[h264 @ 00000000025F0710] no frame

这是我用于编码的一些代码:

AVPacket FFMpegEncoder2::write_video_frame(AVFrame* pic,int &numBytes)
{
    int out_size,ret;

    AVPacket pkt;


    /* encode the image */
    out_size = avcodec_encode_video(m_cctx,m_outbuf,m_outbufSize,pic);
    /* If size is zero,it means the image was buffered. */
    assert(out_size>0) //0 frame delay

    av_init_packet(&pkt);

    if (m_cctx->coded_frame->pts != AV_NOPTS_VALUE)
          pkt.pts = av_rescale_q(m_cctx->coded_frame->pts,m_cctx->time_base,m_video_st->time_base);
    if (m_cctx->coded_frame->key_frame)
          pkt.flags |= AV_PKT_FLAG_KEY;
    pkt.stream_index = m_video_st->index;
    pkt.data         = m_outbuf;
    pkt.size         = out_size;

    /* Write the compressed frame to the media file. */
    ret = av_interleaved_write_frame(m_fctx,&pkt);

    if (ret != 0) {
        fprintf(stderr,"Error while writing video framen");
        exit(1);
    }
    numBytes = out_size;
    return pkt;
}

然后我把这个返回的数据包送到解码器:

const AVFrame* FFMpegDecoder2::decode(AVPacket* packet){
    AVPacket pkt;
    av_init_packet(&pkt);
    pkt.size = packet->size;
    pkt.data = packet->data;

    int len=0;
    int got_picture=0;


    while (pkt.size > 0) {
            len = avcodec_decode_video2(m_cctx,m_frame,&got_picture,&pkt);
            if (len < 0) {
                fprintf(stderr,"Error while decoding frame %dn",m_f);
                exit(1);
            }
            if (got_picture) {
                assert(pkt.size==len);
                m_f++;
            }
            pkt.size -= len;
            pkt.data += len;
        }
    assert(got_picture);
    return m_frame;
}

但如上所述,avcodec_decode_video2()返回-1

我究竟做错了什么?我是否需要先以某种方式将一些头数据输入解码器?

//编辑:

如果我设定

m_formatCtx->oformat->flags &= ~AVFMT_GLOBALHEADER;
m_codecctx->flags &= ~CODEC_FLAG_GLOBAL_HEADER;

然后我可以无错误地解码返回的数据包,但写入的mp4文件将是黑色的.

//编辑:这是我设置解码器的方式:

FFMpegDecoder2::FFMpegDecoder2(CodecID id)
    : m_codec(NULL),m_cctx(NULL)
{


    /* Initialize libavcodec,and register all codecs and formats. */
    avcodec_register_all();

    m_codec = avcodec_find_decoder(id);
    if (!m_codec) {
        fprintf(stderr,"codec not foundn");
        exit(1);
    }

    m_cctx = avcodec_alloc_context3(m_codec);
    m_cctx->codec = m_codec;
    m_cctx->pix_fmt = PIX_FMT_YUV420P;

    avcodec_open2(m_cctx,m_codec,NULL);

    //alloc frame
    m_frame = avcodec_alloc_frame();
}

这是内存窗口显示的第一个数据包(没有全部复制.第一个数据包的大小是7859):

0x0000000002E66670  00 00 01 06 05 ff ff 55 dc 45 e9 bd e6 d9 48 b7 96 2c d8 20 d9 23 ee ef 78 32 36 34 20 2d 20 63 6f 72 65 20 31 32 30 20 72 32 31 34 36 20 62  .....??UüEé.?ùH·–,? ù#??x264 - core 120 r2146 b
0x0000000002E6669F  63 64 34 31 64 62 20 2d 20 48 2e 32 36 34 2f 4d 50 45 47 2d 34 20 41 56 43 20 63 6f 64 65 63 20 2d 20 43 6f 70 79 6c 65 66 74 20 32 30 30 33  cd41db - H.264/MPEG-4 AVC codec - Copyleft 2003
0x0000000002E666CE  2d 32 30 31 31 20 2d 20 68 74 74 70 3a 2f 2f 77 77 77 2e 76 69 64 65 6f 6c 61 6e 2e 6f 72 67 2f 78 32 36 34 2e 68 74 6d 6c 20 2d 20 6f 70 74  -2011 - http://www.videolan.org/x264.html - opt
0x0000000002E666FD  69 6f 6e 73 3a 20 63 61 62 61 63 3d 30 20 72 65 66 3d 33 20 64 65 62 6c 6f 63 6b 3d 31 3a 30 3a 30 20 61 6e 61 6c 79 73 65 3d 30 78 33 3a 30  ions: cabac=0 ref=3 deblock=1:0:0 analyse=0x3:0
0x0000000002E6672C  78 31 31 33 20 6d 65 3d 68 65 78 20 73 75 62 6d 65 3d 34 20 70 73 79 3d 31 20 70 73 79 5f 72 64 3d 31 2e 30 30 3a 30 2e 30 30 20 6d 69 78 65  x113 me=hex subme=4 psy=1 psy_rd=1.00:0.00 mixe
0x0000000002E6675B  64 5f 72 65 66 3d 31 20 6d 65 5f 72 61 6e 67 65 3d 31 36 20 63 68 72 6f 6d 61 5f 6d 65 3d 31 20 74 72 65 6c 6c 69 73 3d 30 20 38 78 38 64 63  d_ref=1 me_range=16 chroma_me=1 trellis=0 8x8dc
0x0000000002E6678A  74 3d 31 20 63 71 6d 3d 30 20 64 65 61 64 7a 6f 6e 65 3d 32 31 2c 31 31 20 66 61 73 74 5f 70 73 6b 69 70 3d 31 20 63 68 72 6f 6d 61 5f 71 70  t=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp
0x0000000002E667B9  5f 6f 66 66 73 65 74 3d 30 20 74 68 72 65 61 64 73 3d 31 20 73 6c 69 63 65 64 5f 74 68 72 65 61 64 73 3d 30 20 6e 72 3d 30 20 64 65 63 69 6d  _offset=0 threads=1 sliced_threads=0 nr=0 decim
0x0000000002E667E8  61 74 65 3d 31 20 69 6e 74 65 72 6c 61 63 65 64 3d 30 20 62 6c 75 72 61 79 5f 63 6f 6d 70 61 74 3d 30 20 63 6f 6e 73 74 72 61 69 6e 65 64 5f  ate=1 interlaced=0 bluray_compat=0 constrained_
0x0000000002E66817  69 6e 74 72 61 3d 30 20 62 66 72 61 6d 65 73 3d 30 20 77 65 69 67 68 74 70 3d 32 20 6b 65 79 69 6e 74 3d 32 35 20 6b 65 79 69 6e 74 5f 6d 69  intra=0 bframes=0 weightp=2 keyint=25 keyint_mi
0x0000000002E66846  6e 3d 32 20 73 63 65 6e 65 63 75 74 3d 34 30 20 69 6e 74 72 61 5f 72 65 66 72 65 73 68 3d 30 20 72 63 3d 61 62 72 20 6d 62 74 72 65 65 3d 30  n=2 scenecut=40 intra_refresh=0 rc=abr mbtree=0
0x0000000002E66875  20 62 69 74 72 61 74 65 3d 34 30 30 20 72 61 74 65 74 6f 6c 3d 31 2e 30 20 71 63 6f 6d 70 3d 30 2e 36 30 20 71 70 6d 69 6e 3d 30 20 71 70 6d   bitrate=400 ratetol=1.0 qcomp=0.60 qpmin=0 qpm
0x0000000002E668A4  61 78 3d 36 39 20 71 70 73 74 65 70 3d 34 20 69 70 5f 72 61 74 69 6f 3d 31 2e 34 30 20 61 71 3d 31 3a 31 2e 30 30 00 80 00 00 00 01 65 88 84  ax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00.                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读