【发布时间】:2017-06-03 03:48:27
【问题描述】:
avformat_open_input 遇到了一个奇怪的问题,它失败了:
处理输入时发现无效数据
但这只发生在我尝试使用自定义 AVIOContext 读取文件时。
我的自定义代码如下(为清楚起见省略了错误检查):
auto fmtCtx = avformat_alloc_context();
auto ioBufferSize = 32768;
auto ioBuffer = (unsigned char *)av_malloc(ioBufferSize);
auto ioCtx = avio_alloc_context(ioBuffer,
ioBufferSize,
0,
reinterpret_cast<void *>(this),
&imageIORead,
NULL,
&imageIOSeek));
fmtCtx -> pb = ioCtx;
fmtCtx -> flags |= AVFMT_FLAG_CUSTOM_IO;
int err = avformat_open_input(&fmtCtx, NULL, NULL, NULL);
imageIOSeek 永远不会被调用,但会正确处理 whence 参数,包括 AVSEEK_SIZE 选项。我的文件数据已经加载到内存中,所以 imageIORead 很简单(在 EOF 时返回 0):
int imageIORead(void *opaque, uint8_t *buf, int buf_size) {
Image *d = (Image *)buf;
int rc = std::min(buf_size, static_cast<int>(d->data.size() - d->pos));
memcpy(buf, d->data.data() + d->pos, rc);
d->pos += rc;
return rc;
}
正在读取的数据是从磁盘上的文件加载的:
/tmp/25.jpeg
以下代码能够正确打开并提取图像:
auto fmtCtx = avformat_alloc_context();
int err = avformat_open_input(&fmtCtx, "/tmp/25.jpeg", NULL, NULL);
该项目使用libavformat 的缩小版本,仅包括我们需要的格式。我不认为这是问题的原因,因为在指定路径时文件可以打开并正确处理。我没有看到任何专门针对自定义 IO 上下文支持的 configure 选项。
这是有问题的图片:25.jpeg
【问题讨论】:
-
你解决过这个问题吗?
-
是的,我错过了一个配置选项。
-
@Tim 什么配置选项?请不要成为那些从不发布解决方案的人之一,让发现这个问题的人冷落。
标签: ffmpeg libavformat