avcodec_decode_video 解码失败,got_picture返回0

来源:互联网 发布:sfp plus端口 编辑:程序博客网 时间:2024/05/21 18:36

//H264解码器初始化
bool H264DecodeInit(void)
{
        //
        int  numBytes;

        avcodec_init();
        /* register all the codecs */
        avcodec_register_all();

        codecContext= NULL;

        /* find the mpeg1 video decoder */
        codec = avcodec_find_decoder(CODEC_ID_H264);

        if (!codec) {
                fprintf(stderr, "codec not found\n");
        }

        codecContext= avcodec_alloc_context();

        codecContext->width=800;
        codecContext->height=600;
        codecContext->sample_fmt=SAMPLE_FMT_S16;

        codecContext->pix_fmt=PIX_FMT_YUV420P;

        m_pFrame= avcodec_alloc_frame();

        if (m_pFrame==NULL)
        {
                return false;
        }

        m_pFrameRGB = avcodec_alloc_frame();
        if(m_pFrameRGB == NULL)
        {
                return false;   
        }

        if(codec->capabilities&CODEC_CAP_TRUNCATED)
                codecContext->flags|= CODEC_FLAG_TRUNCATED;                /* we do not send complete frames */

        /* For some codecs, such as msmpeg4 and mpeg4, width and height
                MUST be initialized there because this information is not
        available in the bitstream. */

        /* open it */

        if (avcodec_open(codecContext, codec) < 0) {
                fprintf(stderr, "could not open codec\n");
        }

        numBytes=avpicture_get_size(PIX_FMT_RGB24, 800, 600);
        m_buffer=new uint8_t[numBytes];   

        // Assign appropriate parts of buffer to image planes in pFrameRGB
        avpicture_fill((AVPicture *)m_pFrameRGB, m_buffer, PIX_FMT_RGB24, 800 ,600);

        return true;
}



//H264 解码函数
void H264DecodeProcess(struct buffer_desc desc)
{
       
        int got_picture,len;

        len = avcodec_decode_video(codecContext, m_pFrame, &got_picture,
                (uint8_t *)desc.buffer, desc.valid_len);      //Rtp码流已经解包放在desc.buffer中,buffer的长度为desc.valid_len  调用这个函数got_picture=0;

        if (len != desc.valid_len) {
                printf("decode error\n");
        }
       
        img_convert((AVPicture *)m_pFrameRGB, PIX_FMT_BGR24,(AVPicture*)m_pFrame,codecContext->pix_fmt, codecContext->width,codecContext->height);

        //显示图像
       
        return;
}

如果我将Rtp码流解包后,直接丢给解码模块,实时解码,发现//Rtp码流已经解包放在desc.buffer中,buffer的长度为desc.valid_len  第一次调用avcodec_decode_video

这个函数got_picture=0;第2次调用avcodec_decode_video这个函数就可以得到正确的值,即我的每解码一帧要调用avcodec_decode_video,2次,很诧异,有路过的高手指点下。

我的码流中只有IDR帧和P帧,对Rtp包解码后,对sps和pps,idr,p帧前面都加了00 00 00 01开始标记