【发布时间】:2017-04-26 16:02:36
【问题描述】:
我目前正在尝试在 SFML 中实现 FFmpeg,因此我可以读取更广泛的音频文件,但在打开 m4a 文件时出现错误 [mov,mp4,m4a,3gp,3g2,mj2 @ #] moov atom not found。现在这只发生在我使用自定义 IOContext 读取文件而不是从 URL 打开它时。 This page 这里说我不应该使用流来打开 m4a 文件,但是 IOContext 是否被视为流?因为我无法将它作为 URL 打开,因为这就是 SFML 的工作原理。
// Explanation of InputStream class
class InputStream {
int64_t getSize()
int64_t read(void* data, int64_t size);
int64_t seek(int64_t position);
int64_t tell(); // Gets the stream position
};
// Used for IOContext
int read(void* opaque, uint8_t* buf, int buf_size) {
sf::InputStream* stream = (sf::InputStream*)opaque;
return (int)stream->read(buf, buf_size);
}
// Used for IOContext
int64_t seek(void* opaque, int64_t offset, int whence) {
sf::InputStream* stream = (sf::InputStream*)opaque;
switch (whence) {
case SEEK_SET:
break;
case SEEK_CUR:
offset += stream->tell();
break;
case SEEK_END:
offset = stream->getSize() - offset;
}
return (int64_t)stream->seek(offset);
}
bool open(sf::InputStream& stream) {
AVFormatContext* m_formatContext = NULL;
AVIOContext* m_ioContext = NULL;
uint8_t* m_ioContextBuffer = NULL;
size_t m_ioContextBufferSize = 0;
av_register_all();
avformat_network_init();
m_formatContext = avformat_alloc_context();
m_ioContextBuffer = (uint8_t*)av_malloc(m_ioContextBufferSize);
if (!m_ioContextBuffer) {
close();
return false;
}
m_ioContext = avio_alloc_context(
m_ioContextBuffer, m_ioContextBufferSize,
0, &stream, &::read, NULL, &::seek
);
if (!m_ioContext) {
close();
return false;
}
m_formatContext = avformat_alloc_context();
m_formatContext->pb = m_ioContext;
if (avformat_open_input(&m_formatContext, NULL, NULL, NULL) != 0) {
// FAILS HERE
close();
return false;
}
//...
return true;
}
【问题讨论】: