FFMPEG avcodec_decode_video2 got_picture_ptr不同的行为

时间:2015-11-13 16:09:18

标签: ffmpeg

目前正在将我们的FFMPEG库使用情况从相当旧版本(0.5)更新为2.8。作为更改的一部分,已将avcodec_decode_video替换为avcodec_decode_video2。但是,我注意到avcodec_decode_video2的功能与旧的avcodec_decode_video相比有很大不同。对于相同的数据包(相同的数据),'avcodec_decode_video2'给出了got_picture_ptr作为zeo而旧的'avcodec_decode_video'给出了非零值。在这里描述的示例中,我使用VideoCodec解码FLV文件:H264-MPEG-4 AVC(第10部分)和AudioCodec:MPEG AAC音频(我在FLV_Sample.Hex中附加了FLV文件的Hex版本的一部分{ {3}})。原始的flv文件太大了)。对于第一个AVPacket(从av_read_frame获得),来自'avcodec_decode_video2'的got_picture_ptr为零,但旧的'avcodec_decode_video'给出296(Am将所获得的整个AVPacket数据和从文件FFMPEG_Decoding_Packet_Info.txt {{3}中的两个函数获得的输出附加) })。继续,新的'avcodec_decode_video2'一直给出'零',直到第23个数据包给出1.所以它不像avcodec_decode_video2一直给零。我的主要困境是,我不确定这种行为差异是由于'avcodec_decode_video2'的变化还是我在使用解码器时所犯的任何错误。我已经放了一段代码,用于使用下面的解码器。任何建议都会有所帮助。

AVFormatContext *pFormatCtx;
AVCodecContext  *pCodecCtx;
AVCodec         *pCodec;
AVFrame         *pFrameRGB;

#if FFMPEG_2_8
avformat_open_input(&pFormatCtx, strFileName, NULL, NULL) ;
#else
av_open_input_file(&pFormatCtx, strFileName, NULL, 0, NULL) ;
#endif //FFMPEG_2_8

size_t videoStream=pFormatCtx->nb_streams;
bool streamFound = false ;
for(size_t i=0; i<pFormatCtx->nb_streams; i++)
{
    #if FFMPEG_2_8
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
    #else
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
    #endif //FFMPEG_2_8
    {
        videoStream = i;
        streamFound = true ; 
        break;
    }
}

if(streamFound)
{
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;

    // Find the decoder for the video stream
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL)
        return false; // Codec not found

    // Open codec
    #if FFMPEG_2_8
    if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
    #else
    if(avcodec_open(pCodecCtx, pCodec)<0)
    #endif //FFMPEG_2_8
    {
        return false; // Could not open codec
    }

    #if FFMPEG_2_8
    pFrameRGB=av_frame_alloc() ;
    #else
    pFrameRGB=avcodec_alloc_frame();
    #endif //FFMPEG_2_8
    if(pFrameRGB==NULL)
            return false; //No Memory

    while(true)
    {
        AVPacket packet ;

        if (av_read_frame(pFormatCtx, &packet) < 0)
        {
            break ;
        }


        int frameFinished;
        if (packet.stream_index == videoStream)
        {
            #if FFMPEG_2_8
            avcodec_decode_video2(pCodecCtx, pFrameRGB, &frameFinished, &packet);
            #else
            avcodec_decode_video(pCodecCtx, pFrameRGB, &frameFinished, packet.data, packet.size);
            #endif //FFMPEG_2_8
        }

        if(frameFinished !=0)
        {
            break ;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我确实有几乎相同的实现,正在研究最新的2.8.1版本。我不知道旧版本(0.5),但你对新版本的实现似乎没问题。

我想有一件事关于&#34; got_picture_ptr&#34;但不确定。 JFYI,H264中的解码顺序和显示顺序不同。可能是早期版本的FFMPEG用于以解码顺序给出图片,而不是显示顺序。在这种情况下,从第一个数据包开始,您将看到每个数据包解码的非零值。

在某些时候,ffmpeg会纠正这个以按显示顺序发出图片。因此,您可能无法从第一个数据包解码中观察到非零值。

我猜你的应用程序运行正常,无论这种差异如何。

相关问题