Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG - avcodec_decode_video2 return "Invalid frame dimensions 0x0"

I am trying to build a simple FFMPEG MPEG2 video PES decoder on ANDROID using the ffmpeg/doc/example/decoding__encoding_8c-source.html.

I am using FFMPEG version 2.0!

I initialize the ffmpeg system with the following code:

int VideoPlayer::_setupFFmpeg()
{
    int rc;
    AVCodec *codec;

    av_register_all();
    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG2VIDEO);
    if(NULL == codec){
        LOGE("_setupFFmpeg. No codec!");
        return -1;
    }
    LOGI("found: %p. name: %s", codec, codec->name);

    _video_dec_ctx = avcodec_alloc_context3(codec);
    if(NULL == _video_dec_ctx){
        LOGE("_setupFFmpeg. Could not allocate codec context space!");
        return -1;
    }
    _video_dec_ctx->debug = FF_DEBUG_PICT_INFO;
    if(codec->capabilities & CODEC_CAP_TRUNCATED) _video_dec_ctx->flags |= CODEC_FLAG_TRUNCATED;

    rc = avcodec_open2(_video_dec_ctx, codec, NULL);
    if(rc < 0) {
        LOGE("_setupFFmpeg. Could not open the codec: %s!", _video_dec_ctx->codec->name);
        return -1;
    }

    av_init_packet(&_avpkt);

    if(NULL == _video_frame) _video_frame = avcodec_alloc_frame();
    if(NULL == _video_frame){
        LOGE("_setupFFmpeg. Could not allocate memory for the video frame!");
        return -1;
    }

    LOGI("_setupFFmpeg(exit)");
    return 0;
}

And then just having a loop that is continuously sending PES packets at the decoder calling this function:

int VideoPlayer::_playVideoPacket(PesPacket *packet)
{
    int len, frameReady;
    _avpkt.data = packet->buf;
    _avpkt.size = packet->info.bufferSize;
    while(_avpkt.size){
        len = avcodec_decode_video2(_video_dec_ctx, _video_frame, &frameReady, &_avpkt);
        if(len < 0){
            LOGE("FFW_decodeVideo");
            return len;
        }
        if(frameReady){
            //Draw the picture...
        }
        _avpkt.size -= len;
        _avpkt.data += len;
    }
    return 1;
}

But when I run the code I get: "Invalid frame dimensions 0x0" error from FFMPEG after calling avcodec_decode_video2().

It seems that I am not setting up the codec correctly. The MpegEncContext in Mpeg1Context in mpeg12dec.c is not setup correctly. What do I have to do to setup MpegEncContext correctly?

like image 674
user3004612 Avatar asked Nov 18 '13 13:11

user3004612


1 Answers

Judging by your variable names, you are passing a pes packet into decode_video. This is wrong. You must pass in a raw ES packet (no pes header) The easiest way to do this is use avformat and av_read_frame() to fill AVPacket for you. If you are using your own format demuxer, you must get down to the Raw ES layer. You may also need to set the PTS DTS values.

Note: I do not know mpeg2 very well, but some codecas require you to configure the extradata values in the codec context also.

like image 194
szatmary Avatar answered Oct 17 '22 13:10

szatmary