FFMPEG - avcodec_decode_video2 返回 "Invalid frame dimensions 0x0"

标签 ffmpeg video-streaming

我正在尝试使用 ffmpeg/doc/example/decoding__encoding_8c-source.html 在 ANDROID 上构建一个简单的 FFMPEG MPEG2 视频 PES 解码器。

我使用的是 FFMPEG 2.0 版!

我使用以下代码初始化ffmpeg系统:

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;
}

然后只有一个循环在调用此函数的解码器处连续发送 PES 数据包:

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;
}

但是当我运行代码时我得到: 调用 avcodec_decode_video2() 后,FFMPEG 出现“无效帧尺寸 0x0”错误。

看来我没有正确设置编解码器。 mpeg12dec.c 中 Mpeg1Context 中的 MpegEncContext 未正确设置。我需要做什么才能正确设置 MpegEncContext?

最佳答案

根据变量名称判断,您正在将 pes 数据包传递到decode_video 中。这是错误的。您必须传入原始 ES 数据包(无 pes header )。最简单的方法是使用 avformat 和 av_read_frame() 为您填充 AVPacket。如果您使用自己的格式解复用器,则必须深入到 Raw ES 层。您可能还需要设置 PTS DTS 值。

注意:我不太了解 mpeg2,但某些编解码器还要求您在编解码器上下文中配置额外数据值。

关于FFMPEG - avcodec_decode_video2 返回 "Invalid frame dimensions 0x0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048631/

相关文章:

google-app-engine - Google Application Engine 是高流量聊天网站的良好平台吗?

video - 如何在以 MPEG-TS 格式打包的 MPEG-2 流中查找关键帧

opencv - IP 摄像机的 URL 视频流

azure - 捕获视频并将其上传到 Azure Blob

audio - FFMPEG:音频和视频组合错误

nginx - 流式网络摄像头实时缓冲区 [东芝网络摄像头] [视频输入] 太满或接近太满

objective-c - 在 iPhone 上使用 FFMPeg 解码 WMA

ffmpeg - 移动文本drawtext过滤器的速度 - FFMPEG

objective-c - 如何使用 FFMPEG 混合不同格式的声音文件?

google-cloud-platform - 如何使用GCP设计可扩展的视频流架构?