• ffmpeg基础 -- avio_alloc_context 读内存


    转载:https://www.jianshu.com/p/3c95b0471d3a

    1 // 不要用第四个参数传自定的数据,当av_read_frame的时候会出问题,无限循环
    2 avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);
    3 
    4 /* 
    5 avio_alloc_context开头会读取部分数据探测流的信息,不会全部读取,除非设置的缓存过大
    6 av_read_frame会在读帧的时候调用avio_alloc_context中的read_packet方法取流数据,每隔avio_ctx_buffer_size调用一次,直至读完
    7 */
     1 /*正确方式*/
     2 struct buffer_data
     3 {
     4     uint8_t *ptr; /* 文件中对应位置指针 */
     5     size_t size;  ///< size left in the buffer /* 文件当前指针到末尾 */
     6 };
     7 
     8 // 重点,自定的buffer数据要在外面这里定义
     9 struct buffer_data bd = {0};
    10 
    11 //用来将内存buffer的数据拷贝到buf
    12 int read_packet(void *opaque, uint8_t *buf, int buf_size)
    13 {
    14 
    15     buf_size = FFMIN(buf_size, bd.size);
    16 
    17     if (!buf_size)
    18         return AVERROR_EOF;
    19     printf("ptr:%p size:%zu bz%zu
    ", bd.ptr, bd.size, buf_size);
    20 
    21     /* copy internal buffer data to buf */
    22     memcpy(buf, bd.ptr, buf_size);
    23     bd.ptr += buf_size;
    24     bd.size -= buf_size;
    25 
    26     return buf_size;
    27 }
    28 
    29 /* 打开前端传来的视频buffer */
    30 int open_input_buffer(uint8_t *buf, int len)
    31 {
    32     unsigned char *avio_ctx_buffer = NULL;
    33     size_t avio_ctx_buffer_size = 32768;
    34     
    35     AVInputFormat* in_fmt = av_find_input_format("h265");
    36 
    37     bd.ptr = buf;  /* will be grown as needed by the realloc above */
    38     bd.size = len; /* no data at this point */
    39 
    40     fmt_ctx = avformat_alloc_context();
    41 
    42     avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);
    43     
    44     /* 读内存数据 */
    45     avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);
    46 
    47     fmt_ctx->pb = avio_ctx;
    48     fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
    49 
    50     /* 打开内存缓存文件, and allocate format context */
    51     if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
    52     {
    53         fprintf(stderr, "Could not open input
    ");
    54         return -1;
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    浅谈数组
    前端冷知识
    number框
    jQuery封装轮播图插件
    瀑布流的简单制作
    Java script-数组与字符串方法
    Java script-1
    Web前端基础学习-2
    Web前端基础学习-3
    Web前端基础学习-1
  • 原文地址:https://www.cnblogs.com/y4247464/p/13919596.html
Copyright © 2020-2023  润新知