ffmpeg自定义IO

  • 所谓的自定义IO就是实现复用结构体AVFormatContextpb字段,这个字段的结果类型是AVIOContext,可以通过avio_alloc_context函数构造一个此结构体
  • 如果是要读文件需要实现函数指针read_packetseek,如果是要写文件需要实现函数指针write_packetseek
  • bufferbuffer_size必须是有效的,如果是写文件,在写之前数据存储在此buffer中。如果是读文件,会预先读取到此buffer中。所以此buffer的大小应当大于我们一次性读写的大小。
  • opaque是用户自定义数据结构,read_packetwrite_packetseek都会透传此数据结构给回用户。
  • seek函数指针中的whence参数除了支持正常的SEEK_SETSEEK_CURSEEK_END还支持FFMpeg自定义的类型AVSEEK_SIZEAVSEEK_FORCE
AVIOContext *avio_alloc_context(
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
    AVIOContext *s = av_malloc(sizeof(AVIOContext));
    if (!s)
        return NULL;
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
                  read_packet, write_packet, seek);
    return s;
}
  • 关于buffer之间的关系
   /*
     * The following shows the relationship between buffer, buf_ptr,
     * buf_ptr_max, buf_end, buf_size, and pos, when reading and when writing
     * (since AVIOContext is used for both):
     *
     **********************************************************************************
     *                                   READING
     **********************************************************************************
     *
     *                            |              buffer_size              |
     *                            |---------------------------------------|
     *                            |                                       |
     *
     *                         buffer          buf_ptr       buf_end
     *                            +---------------+-----------------------+
     *                            |/ / / / / / / /|/ / / / / / /|         |
     *  read buffer:              |/ / consumed / | to be read /|         |
     *                            |/ / / / / / / /|/ / / / / / /|         |
     *                            +---------------+-----------------------+
     *
     *                                                         pos
     *              +-------------------------------------------+-----------------+
     *  input file: |                                           |                 |
     *              +-------------------------------------------+-----------------+
     *
     *
     **********************************************************************************
     *                                   WRITING
     **********************************************************************************
     *
     *                             |          buffer_size                 |
     *                             |--------------------------------------|
     *                             |                                      |
     *
     *                                                buf_ptr_max
     *                          buffer                 (buf_ptr)       buf_end
     *                             +-----------------------+--------------+
     *                             |/ / / / / / / / / / / /|              |
     *  write buffer:              | / / to be flushed / / |              |
     *                             |/ / / / / / / / / / / /|              |
     *                             +-----------------------+--------------+
     *                               buf_ptr can be in this
     *                               due to a backward seek
     *
     *                            pos
     *               +-------------+----------------------------------------------+
     *  output file: |             |                                              |
     *               +-------------+----------------------------------------------+
     *
     */
  • 简单的例子
bool InitAVFormatContext()
{
    constexpr int32_t buffer_size = 16384; // 16 KByte
    uint8_t* buffer = new[buffer_size];

    AVFormatContext* fmt = avformat_alloc_context();
    fmt.pb = avio_alloc_context(buffer, buffer_size, 0, nullptr, [](void *opaque, uint8_t *buf, int buf_size){
        // TODO
        return -1;
    }, [](void *opaque, uint8_t *buf, int buf_size){
        // TODO
        return -1;
    }, [](void *opaque, int64_t offset, int whence){
        // TODO
        return -1;
    });

    int32_t ret = avformat_open_input(&ctx->format_ctx, nullptr, nullptr, nullptr);
    if (ret < 0) return false;
    return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值