video - 使用 FFMPEG 解码 mp4/mkv 失败

标签 video ffmpeg decode mp4

我正在使用基于最新 FFmpeg git 源代码树的项目,并链接到 Zeranoe 在 https://ffmpeg.zeranoe.com/builds/ 发布的共享 DLL。

播放代码有效并循环。它播放 h265 文件(原始)、mpeg、avi 和 mpg 文件。然而,一旦 mp4 或 mkv 容器被指定为输入文件,无论里面有什么,编解码器都会转储大量错误。不管是 HEVC 还是 h264。

[h264 @ 00000000xyz] No start code is found
[h264 @ 00000000xyz] Error splitting the input into NAL units.

为了让一切变得非常奇怪,ffplay.exe 可以很好地播放这些文件。

我意识到我可能可以通过先将文件转换为原始格式来解决这个问题,但我希望能够读取和解析 mp4 文件。由于我使用的是 Zeraneo 的预构建库,我的猜测是在构建过程中没有启用某些东西,但我希望 ffplay 也会失败。我是否需要在 format_context 或 codec_context 中设置标志,或提供某种过滤器标识符?

播放良好的电影来自http://bbb3d.renderfarming.net/download.html , http://www.w6rz.net/http://www.sample-videos.com/

这些工作:

big_buck_bunny_480p_surround-fix.avi
bigbuckbunny_480x272.h265

作为 ffmpeg 的完全菜鸟,请帮助我了解问题所在以及如何修复它。如果预构建库是罪魁祸首,那么第二个问题是是否有人有方便的 cmake 设置来为 Windows X64 和 x32 调试和发布目标构建它。

这里是初始化ffmpeg的源码,供阅读

avdevice_register_all();
avfilter_register_all();
av_register_all();
avformat_network_init();

格式解析如下:

m_FormatContext = avformat_alloc_context();
if (avformat_open_input(&m_FormatContext, file.GetPath().ToString().c_str(), NULL, NULL) != 0) 
{
    //std::cout << "failed to open input" << std::endl;
    success = false;
}
// find stream info
if (success)
{
    if (avformat_find_stream_info(m_FormatContext, NULL) < 0) 
    {
        //std::cout << "failed to get stream info" << std::endl;
        success = false;
    }
}

流打开如下:

m_VideoStream = avstream;
m_FormatContext = formatContext;
if (m_VideoStream)
{
    m_StreamIndex = m_VideoStream->stream_identifier;
    AVCodecParameters *codecpar = m_VideoStream->codecpar;      
    if (codecpar)
    {
        AVCodecID codec_id = codecpar->codec_id;
        m_Decoder = avcodec_find_decoder(codec_id);
        if (m_Decoder)
        {
            m_CodecContext = avcodec_alloc_context3(m_Decoder);
            if (m_CodecContext)
            {
                m_CodecContext->width = codecpar->width;                    
                m_CodecContext->height = codecpar->height;
                m_VideoSize = i3(codecpar->width, codecpar->height,1);
                success = 0 == avcodec_open2(m_CodecContext, m_Decoder, NULL);
                if (success)
                {
                    if(m_CodecContext)
                    {
                        int size = av_image_get_buffer_size(format, m_CodecContext->width, m_CodecContext->height, 1);      
                        if (size > 0)
                        {
                            av_frame = av_frame_alloc();
                            gl_frame = av_frame_alloc();        
                            uint8_t *internal_buffer = (uint8_t *)av_malloc(size * sizeof(uint8_t));
                            av_image_fill_arrays((uint8_t**)((AVPicture *)gl_frame->data), (int*) ((AVPicture *)gl_frame->linesize), internal_buffer, format, m_CodecContext->width, m_CodecContext->height,1);
                            m_Packet = (AVPacket *)av_malloc(sizeof(AVPacket));
                        }
                    }
                }
                if (!success) 
                {
                    avcodec_close(m_CodecContext);
                    avcodec_free_context(&m_CodecContext);
                    m_CodecContext = NULL;
                    m_Decoder = NULL;
                    m_VideoStream = NULL;
                }
            }
            else
            {
                m_Decoder = NULL;
                m_VideoStream = NULL;
            }
        }
    }
}

单线程解码:

do 
{
    if (av_read_frame(m_FormatContext, m_Packet) < 0) 
    {
        av_packet_unref(m_Packet);
        m_AllPacketsSent = true;
    }
    else
    {
        if (m_Packet->stream_index == m_StreamIndex) 
        {                   
            avcodec_send_packet(m_CodecContext, m_Packet);
        }
    }

    int frame_finished = avcodec_receive_frame(m_CodecContext, av_frame);
    if (frame_finished == 0) 
    {
        if (!conv_ctx) 
        {
            conv_ctx = sws_getContext(m_CodecContext->width, 
                m_CodecContext->height, m_CodecContext->pix_fmt, 
                m_CodecContext->width, m_CodecContext->height, format, SWS_BICUBIC, NULL, NULL, NULL);
        }

        sws_scale(conv_ctx, av_frame->data, av_frame->linesize, 0, m_CodecContext->height, gl_frame->data, gl_frame->linesize);

        switch(format)
        {
            case AV_PIX_FMT_BGR32_1:
            case AV_PIX_FMT_RGB32_1:
            case AV_PIX_FMT_0BGR32:
            case AV_PIX_FMT_0RGB32:
            case AV_PIX_FMT_BGR32:  
            case AV_PIX_FMT_RGB32:              
            {
                m_CodecContext->bits_per_raw_sample = 32; break;                    
            }
            default:
            {
                FWASSERT(format == AV_PIX_FMT_RGB32, "The format changed, update the bits per raw sample!"); break;
            }
        }


        size_t bufferSize = m_CodecContext->width * m_CodecContext->height * m_CodecContext->bits_per_raw_sample / 8;
        m_Buffer.Realloc(bufferSize, false,  gl_frame->data[0]);
        m_VideoSize = i3(m_CodecContext->width, m_CodecContext->height,1);
        result = true;
        // sends the image buffer straight to the locked texture here..
        // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, codec_ctx->width, codec_ctx->height, GL_RGB, GL_UNSIGNED_BYTE, gl_frame->data[0]);
    }

    av_packet_unref(m_Packet);
} while (m_Packet->stream_index != m_StreamIndex);

m_FrameDecoded = result;

感谢任何见解!

最佳答案

而不是在这里隐含地提供宽度和高度:

m_CodecContext->width = codecpar->width;                    
m_CodecContext->height = codecpar->height;

你应该调用avcodec_parameters_to_context() .

关于video - 使用 FFMPEG 解码 mp4/mkv 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39105571/

相关文章:

algorithm - 编码/解码 AMR

c# - FFMPEG 转换的视频在 HTML5 中不起作用

html - 全屏居中 html5 视频直播

c++ - 使用 videowriter opencv 的视频大小为 0 字节

php - 在 lampp 服务器中从 php 运行 ffmpeg 时找不到 GLIBCXX_3.4.9

android - 如何使用 FFmpeg 在几秒钟之间剪切视频?

python - 带有 print json 的 Ffprobe 不打印任何内容

java - 解码 html 响应

audio - FFMPEG 和 MP3。如何解码

javascript - 如何在 Angular2 中嵌入视频?