gpt4 book ai didi

c++ - 带有自定义流对象的 ffmpeg avformat_open_input

转载 作者:行者123 更新时间:2023-11-28 01:49:23 28 4
gpt4 key购买 nike

我目前正在尝试使用 ffmpeg 的库为 SFML 编写自定义 SoundFileReader。我唯一可以与 avcodec 和 avformat 一起使用的是下面的类 InputStream,它是 SFML 的一部分。我查看了 avformat_open_inputAVIOContext 并了解了如何将自定义流与缓冲区一起使用,但我如何创建一个 AVIOContext 与自定义 read、seek 和 lseek 函数?

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

最佳答案

您需要编写一组回调函数,然后将指针传递给这些回调并将不透明参数传递给 avio_alloc_context。请注意,这些回调不能抛出。

/// <summary>
/// Reads up to buffer_capacity_bytes_count bytes into supplied buffer.
/// Basically should work like ::read C method.
/// </summary>
/// <param name="opaque">
/// Opaque pointer to reader instance. Passing nullptr is not allowed.
/// </param>
/// <param name="p_buffer">
/// Pointer to data buffer. Passing nullptr is not allowed.
/// </param>
/// <param name="buffer_capacity_bytes_count">
/// Size of the buffer pointed to by p_buffer, in bytes.
/// Passing value less than or equal to 0 is not allowed.
/// </param>
/// <returns>
/// Non negative values containing amount of bytes actually read. 0 if EOF has been reached.
/// -1 if an error occurred.
/// </returns>
static auto
Read(void * const opaque, uint8_t * const p_buffer, int const buffer_capacity_bytes_count) noexcept
{
int result{-1};
if(opaque && p_buffer && (0 <= buffer_capacity_bytes_count))
{
auto & stream{*reinterpret_cast< InputStream * >(opaque)};
try
{
auto const read_result{stream.read(p_buffer, buffer_capacity_bytes_count)};
if((0 <= read_result) && (read_result <= buffer_capacity_bytes_count))
{
result = read_result;
}
}
catch(...)
{
// print error or something
}
}
return(result);
}

/// <summary>
/// Changes file pointer position or retrieves file size.
/// Basically should work like ::lseek and ::fstat C methods.
/// </summary>
/// <param name="opaque">
/// Opaque pointer to reader instance. Passing nullptr is not allowed.
/// </param>
/// <param name="pos">
/// Target offset. When retrieving file size this should be 0.
/// </param>
/// <param name="whence">
/// Flag indicating operation. Valid values are SEEK_SET, SEEK_CUR, SEEK_END (as in C library),
/// AVSEEK_SIZE and optional AVSEEK_FORCE bit.
/// </param>
/// <returns>
/// Non-negative values containing offset of the file pointer or file size in bytes.
/// Negative values if an error occurred.
/// </returns>
static auto
Seek(void * const opaque, int64_t const pos, int const whence) noexcept
{
int64_t result{AVERROR(EBADF)};
if(opaque)
{
auto & stream{*reinterpret_cast< InputStream * >(opaque)};
try
{
auto const action{whence & (SEEK_SET | SEEK_CUR | SEEK_END | AVSEEK_SIZE)};
auto const forced{0 != (whence & AVSEEK_FORCE)}; // can be ignored
switch(action)
{
case SEEK_SET:
case SEEK_CUR:
case SEEK_END:
{
// TODO perform seek...
break;
}
case AVSEEK_SIZE:
{
result = stream.getSize();
break;
}
}
}
catch(...)
{
// print error or something
}
}
return(result);
}

...

InputStream stream;
auto const opaque{reinterpret_cast< void * >(::std::addressof(stream))};
auto p_io_context
{
::avio_alloc_context
(
static_cast< unsigned char * >(p_buffer) // ownership is not transferred
, buffer_size
, 0 // 0 if openning for reading, 1 if openning for writing
, opaque
, &Read
, nullptr // write callback function, not used if we open for reading
, &Seek
)
};

关于c++ - 带有自定义流对象的 ffmpeg avformat_open_input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com