使用ffmpeg库读取视频文件时,无法检测到错误/损坏的视频帧

时间:2016-03-08 16:04:04

标签: c video ffmpeg libavcodec libavformat

我最近开始使用ffmpeg C库来分析文件中视频帧的完整性。我和quotemeta开始(像很多人一样),然后我从他们那里做了自己的研究。但是我遇到了一个问题:在读取数据损坏的视频文件时,我无法检测到损坏的数据包或帧。例如,下面有这个代码(它确实有效,但我对解放资源并不太担心):

extern "C" {
#include <libavformat\avformat.h>
#include <libavcodec\avcodec.h>
#include <libavutil\frame.h>
}
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")

int main(int argc, char *argv[]) {

av_register_all();

AVFormatContext* pFormatContext = 0;

if (avformat_open_input(&pFormatContext, "errorVideo.mpg", NULL, NULL) < 0) {
    //Error opening file
    exit(1);
    }

if (avformat_find_stream_info(pFormatContext, NULL) < 0) {
    //Error finding stream...
    exit(1);
    }

//Finding first video stream:
unsigned int iStream;
for (iStream = 0; iStream < pFormatContext->nb_streams && pFormatContext->streams[iStream]->codec->codec_type != AVMEDIA_TYPE_VIDEO;    iStream++);
if (iStream >= pFormatContext->nb_streams) {
    //Error finding video stream.
    exit(1);
    }

AVCodecContext* pOrigCodecCtx = pFormatContext->streams[iStream]->codec;
AVCodec* pCodec = avcodec_find_decoder(pOrigCodecCtx->codec_id);
if (!pCodec) {
    //Codec no supported
    exit(1);
    }

AVCodecContext* pNewCodecCtx = avcodec_alloc_context3(pCodec);
if (!pNewCodecCtx || avcodec_copy_context(pNewCodecCtx, pOrigCodecCtx) != 0) {
    //Error allocating or copying context
    exit(1);
    }

if (avcodec_open2(pNewCodecCtx, pCodec, NULL) < 0) {
    //Error initializing codec
    exit(1);
    }

AVPacket packet;
int gotFrame;
while (av_read_frame(pFormatContext, &packet) >= 0) {

    if (packet.stream_index == iStream) {
        AVFrame* pFrame = av_frame_alloc();

        if (avcodec_decode_video2(pNewCodecCtx, pFrame, &gotFrame, &packet) < 0) {
            //Error decoding frame.
            exit(1);
            }

        if (gotFrame) {
            //do something with frame...
            }

        av_frame_unref(pFrame);
        }

    av_free_packet(&packet);
    }
if (pFormatContext->pb->error != 0) {
    //Error reading packet
    exit(1);
    }
}

当我尝试读取数据损坏的视频时,我从ffmpeg库中获得输出,表明存在无效数据,例如:

[mpeg2video @ 0000000000B68A40] invalid cbp -1 at 19 6
[mpeg2video @ 0000000000B68A40] Warning MVs not available
[mpeg2video @ 0000000000B68A40] concealing 45 DC, 45 AC, 45 MV errors in P frame
[mpeg2video @ 0000000000B68A40] invalid mb type in P Frame at 21 16
[mpeg2video @ 0000000000B68A40] Warning MVs not available
[mpeg2video @ 0000000000B68A40] concealing 45 DC, 45 AC, 45 MV errors in P frame
[mpeg2video @ 0000000000B68A40] slice mismatch
[mpeg2video @ 0000000000B68A40] 00 motion_type at 25 8
[mpeg2video @ 0000000000B68A40] Warning MVs not available
[mpeg2video @ 0000000000B68A40] concealing 90 DC, 90 AC, 90 MV errors in B frame

但是我无法从我的代码中检测到所有那些失败的帧: av_read_frame avcodec_decode_video2 函数都不会返回错误代码!我做错了什么?

0 个答案:

没有答案