很多時候我們希望通過回調函數給ffmpeg傳遞碼流,讓它幫我們完成解碼操作,要實現該功能需要定義AVIOContext和AVFormatContext對象,下面把核心代碼貼出來:
if (NULL != m_pull_data_func)
{
m_avio_ctx_input = avio_alloc_context(m_avio_ctx_buffer, m_avio_ctx_buffer_size,
0, m_pull_data_func_param, m_pull_data_func, NULL, NULL);
}
else
{
break;
}
if (!m_avio_ctx_input)
{
ret = AVERROR(ENOMEM);
break;
}
m_fmt_ctx->pb = m_avio_ctx_input;
m_fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
ret = avformat_open_input(&m_fmt_ctx, NULL, NULL, NULL);
if (ret < 0)
{
fprintf(stderr, "Could not open input\n");
break;
}
/* retrieve stream information */
if (avformat_find_stream_info(m_fmt_ctx, NULL) < 0)
{
fprintf(stderr, "Could not find stream information\n");
break;
}
m_pull_data_func是我們傳給ffmpeg的回調函數,ffmpeg會從該函數中獲取碼流,這裏說兩點影藏的坑,
1、m_pull_data_func必須按照ffmpeg的要求把它需要的數據返回(因爲ffmpeg要通過輸入的碼流判斷碼流格式)
2、m_pull_data_func的返回值也必須準確。