avio_alloc_context 读内存

作者: 此昵称已被狗抢占 | 来源:发表于2018-03-27 14:53 被阅读0次
// 不要用第四个参数传自定的数据,当av_read_frame的时候会出问题,无限循环
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);

/* 
avio_alloc_context开头会读取部分数据探测流的信息,不会全部读取,除非设置的缓存过大
av_read_frame会在读帧的时候调用avio_alloc_context中的read_packet方法取流数据,每隔avio_ctx_buffer_size调用一次,直至读完
*/
/*正确方式*/
struct buffer_data
{
    uint8_t *ptr; /* 文件中对应位置指针 */
    size_t size;  ///< size left in the buffer /* 文件当前指针到末尾 */
};

// 重点,自定的buffer数据要在外面这里定义
struct buffer_data bd = {0};

//用来将内存buffer的数据拷贝到buf
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{

    buf_size = FFMIN(buf_size, bd.size);

    if (!buf_size)
        return AVERROR_EOF;
    printf("ptr:%p size:%zu bz%zu\n", bd.ptr, bd.size, buf_size);

    /* copy internal buffer data to buf */
    memcpy(buf, bd.ptr, buf_size);
    bd.ptr += buf_size;
    bd.size -= buf_size;

    return buf_size;
}

/* 打开前端传来的视频buffer */
int open_input_buffer(uint8_t *buf, int len)
{
    unsigned char *avio_ctx_buffer = NULL;
    size_t avio_ctx_buffer_size = 32768;
    
    AVInputFormat* in_fmt = av_find_input_format("h265");

    bd.ptr = buf;  /* will be grown as needed by the realloc above */
    bd.size = len; /* no data at this point */

    fmt_ctx = avformat_alloc_context();

    avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);
    
    /* 读内存数据 */
    avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);

    fmt_ctx->pb = avio_ctx;
    fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;

    /* 打开内存缓存文件, and allocate format context */
    if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
    {
        fprintf(stderr, "Could not open input\n");
        return -1;
    }
    return 0;
}

/*错误方式*/

struct buffer_data
{
    uint8_t *ptr; /* 文件中对应位置指针 */
    size_t size;  ///< size left in the buffer /* 文件当前指针到末尾 */
};


//用来将内存buffer的数据拷贝到buf
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
    struct buffer_data *bd = (struct buffer_data *)opaque;
    buf_size = FFMIN(buf_size, bd->size);

    if (!buf_size)
        return AVERROR_EOF;
    printf("ptr:%p size:%zu bz%zu\n", bd->ptr, bd->size, buf_size);

    /* copy internal buffer data to buf */
    memcpy(buf, bd->ptr, buf_size);
    bd->ptr += buf_size;
    bd->size -= buf_size;

    return buf_size;
}



/* 打开前端传来的视频buffer */
int open_input_buffer(uint8_t *buf, int len)
{
    unsigned char *avio_ctx_buffer = NULL;
    size_t avio_ctx_buffer_size = 32768;
    struct buffer_data bd = { 0 };

    AVInputFormat* in_fmt = av_find_input_format("h265");

    bd.ptr = buf;  /* will be grown as needed by the realloc above */
    bd.size = len; /* no data at this point */

    fmt_ctx = avformat_alloc_context();

    avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);

    /* 读内存数据 */
    //使用了第四个参数传buffer,虽然可以获得流的信息,但是读帧的时候会无限循环
    avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, read_packet, NULL, NULL); 

    fmt_ctx->pb = avio_ctx;
    fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;

    /* 打开内存缓存文件, and allocate format context */
    if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
    {
        fprintf(stderr, "Could not open input\n");
        return -1;
    }
    return 0;
}

相关文章

  • avio_alloc_context 读内存

  • 汇编 assembly

    汇编 CPU & 内存 控制线内存是读还是写 地址线从内存地址中找出内存的值 数据线从内存中取出数据交给CPU,传...

  • JS引擎之内存管理

    内存的生命周期 分配你所需要的内存 使用分配到的内存(读、写) 不需要时将其释放\归还 内存空间 JS内存空间分为...

  • JS堆栈内存的运行机制

    内存基本概念 内存的生命周期: 1、分配所需的内存 2、内存的读与写 3、不需要时将其释放 所有语言的内存生命周期...

  • JS内存泄漏与垃圾回收机制

    内存生命周期: 分配你所需要的内存 使用分配到的内存(读、写) 不需要时将其释放\归还 一、什么是内存泄漏? 程序...

  • 多线程知识梳理(1) - 并发编程的艺术笔记

    第三章 Java内存模型 3.1 Java内存模型的基础 通信在共享内存的模型里,通过写-读内存中的公共状态进行隐...

  • 第四章 程序崩溃处理

    内存管理 都是一些内存管理方面的知识,具体参考内存管理书籍即可,特别注意点如下 内存权限读、写、执行权限无权限可能...

  • volatile关键词

    加了这个关键词,要读这个值要从内存里读,因为易变

  • Netty源码七 ByteBuf

    内存分配概述 介绍netty内存分配,最为底层,负责从底层读据到ByteBuf。 三个问题+内存类别有哪些+如何减...

  • 2020-03-12

    1. MEMORY_BARRIER的正确用法 为了更好的说明问题,这里只讨论读写内存栅栏,关于读内存栅栏、写内存栅...

网友评论

    本文标题:avio_alloc_context 读内存

    本文链接:https://www.haomeiwen.com/subject/fhfscftx.html