AVFormatContext协议层:理论与实战(三)

简介: AVFormatContext协议层:理论与实战(三)

AVFormatContext协议层:理论与实战(二)https://developer.aliyun.com/article/1473890


五、avio 实战 3:自定义数据来源

avio 自定义数据来源:可以是文件, 可以是内存, 可以是网络

本次实战的在实战 2 的基础上自定义了数据来源,即使用内存映射技术将输入视频文件映射到内存中。参考了 ffmepg 官方提供的测试用例avio_read_callback.c

1、示例源码

/**
 * @file
 * libavformat AVIOContext API example.
 *
 * Make libavformat demuxer access media content through a custom
 * AVIOContext read callback.
 * @example avio_reading.c
 */
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
//自定义缓冲区
struct buffer_data {
    uint8_t *ptr;
    size_t size; ///< size left in the buffer
};
//读取数据(回调函数)
static 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:%d\n", bd->ptr, bd->size);
    /* copy internal buffer data to buf */
    /// 灵活应用[内存buf]:读取的是内存,比如:加密播放器,这里解密
    memcpy(buf, bd->ptr, buf_size);
    bd->ptr  += buf_size;
    bd->size -= buf_size;
    return buf_size;
}
int main(int argc, char *argv[])
{
    AVFormatContext *fmt_ctx = NULL;
    AVIOContext *avio_ctx = NULL;
    uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
    size_t buffer_size, avio_ctx_buffer_size = 4096;
    char *input_filename = NULL;
    int ret = 0;
    struct buffer_data bd = { 0 };
    printf("Hello,ffmpeg\n");
    input_filename = "./debug/test.mp4";
    /* slurp file content into buffer */
    ///内存映射文件
    ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
    if (ret < 0)
        goto end;
    printf("av_file_map,ok\n");
    /* fill opaque structure used by the AVIOContext read callback */
    bd.ptr  = buffer;
    bd.size = buffer_size;
    /// 创建对象:AVFormatContext
    if (!(fmt_ctx = avformat_alloc_context())) {
        ret = AVERROR(ENOMEM);
        goto end;
    }
    printf("avformat_alloc_context,ok\n");
    /// 分配内存
    avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
    if (!avio_ctx_buffer) {
        ret = AVERROR(ENOMEM);
        goto end;
    }
    /// 创建对象:AVIOContext,注意参数
    avio_ctx = avio_alloc_context(
                avio_ctx_buffer, avio_ctx_buffer_size,
                                  0,
                &bd,
                &read_packet,
                NULL,
                NULL);
    if (!avio_ctx) {
        ret = AVERROR(ENOMEM);
        goto end;
    }
    fmt_ctx->pb = avio_ctx;
    printf("avio_alloc_context,ok\n");
    /// 打开输入流
    ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);
    if (ret < 0) {
        fprintf(stderr, "Could not open input\n");
        goto end;
    }
    printf("avformat_open_input,ok\n");
    /// 查找流信息
    ret = avformat_find_stream_info(fmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Could not find stream information\n");
        goto end;
    }
    printf("avformat_find_stream_info,ok\n");
    printf("******nb_streams=%d\n",fmt_ctx->nb_streams);
    av_dump_format(fmt_ctx, 0, input_filename, 0);
end:
    avformat_close_input(&fmt_ctx);
    /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
    if (avio_ctx)
        av_freep(&avio_ctx->buffer);
    avio_context_free(&avio_ctx);
    /// 内存映射文件:解绑定
    av_file_unmap(buffer, buffer_size);
    if (ret < 0) {
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
        return 1;
    }
    return 0;
}
  • av_file_map():读取文件,并将其内容放入新分配的缓冲区中。返回的缓冲区必须使用 av_file_unmap() 释放;
  • av_dump_format():它用于打印关于音频或视频文件格式的详细信息,例如有关音频或视频文件的格式、流和编解码器的详细信息。包括文件格式、编解码器信息、流参数和元数据等;

2、运行结果

Hello,ffmpeg
av_file_map,ok
avformat_alloc_context,ok
avio_alloc_context,ok
ptr:04650000 size:1247737
....
ptr:04780000 size:2553
avformat_open_input,ok
avformat_find_stream_info,ok
******nb_streams=2
[mov,mp4,m4a,3gp,3g2,mj2 @ 01eb0c40] stream 0, offset 0x30: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 01eb0c40] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 1280x720, 1638 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from './debug/output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf60.3.100
  Duration: 00:00:05.00, start: 0.000000, bitrate: N/A
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 1280x720, 1638 kb/s, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 12800 tbn, 25600 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc60.3.100 libx264
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 348 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
目录
相关文章
|
编解码 人工智能 搜索推荐
API,体育直播的“最强辅助”
看球卡顿、错过关键瞬间?背后“隐形骨架”竟是API!它实时同步比分、智能调度画质、多端联动、精准推荐,让观赛更流畅、智能、沉浸。从数据到互动,API正悄然改变你的看球体验。
617 150
|
存储 机器学习/深度学习 缓存
阿里云AirCache技术实现多模态大模型高效推理加速,入选国际顶会ICCV2025
阿里云研发的AirCache技术被计算机视觉顶会ICCV2025收录,该技术通过激活跨模态关联、优化KV缓存压缩策略,显著提升视觉语言模型(VLMs)的推理效率与存储性能。实验表明,在保留仅10%视觉缓存的情况下,模型性能下降小于1%,解码延迟最高降低66%,吞吐量提升达192%。AirCache无需修改模型结构,兼容主流VLMs,已在教育、医疗、政务等多个行业落地应用,助力多模态大模型高效赋能产业智能化升级。
1003 1
|
11月前
|
存储 弹性计算 关系型数据库
阿里云中小企业扶持权益中心内容说明及节省上云操作手册参考
本文将以技术视角,结合最新权益细则,用通俗语言全面拆解权益中心的核心内容、适用场景、申请步骤和实操技巧,还整理了不同规模企业的高性价比组合方案,帮助你避开误区,用合理成本实现稳定上云。全文无营销话术,所有内容基于实测体验和官方规则整理,可直接作为上云操作手册参考。
683 1
|
分布式计算 监控 API
DMS Airflow:企业级数据工作流编排平台的专业实践
DMS Airflow是基于Apache Airflow构建的企业级数据工作流编排平台,深度集成阿里云DMS系统,提供统一认证、智能调度、多任务类型支持及企业级监控能力,助力数据团队高效管理ETL、分析、机器学习等复杂工作流。
949 21
|
12月前
|
存储 JSON 监控
解锁拼多多API,实时监控店铺评价,及时处理客户反馈!
在电商竞争激烈的环境下,及时响应顾客评价至关重要。本文介绍如何利用拼多多开放平台API,构建自动化评价监控系统,实现差评预警、情感分析与实时通知,提升客服响应效率与客户满意度,助力店铺运营数据化、智能化升级。(238字)
|
12月前
|
人工智能 自然语言处理 安全
2025 热门智能客服系统推荐:适配互联网企业与电商场景需求
2025年,智能客服已成为电商与互联网企业服务中枢。瓴羊Quick Service、Zendesk Answer Bot、Intercom Fin等系统在意图识别、多渠道整合、知识库运维及合规认证方面表现突出,支持高并发、多轮对话与业务数据联动,满足电商大促、全球化运营与复杂客服场景需求,助力企业实现高效、智能的客户服务升级。
|
12月前
|
运维 Ubuntu 搜索推荐
Ubuntu定时器随机延迟配置(详解systemd timer实现随机启动延迟的方法)
教程来源https://www.vpshk.cn/本文介绍如何在Ubuntu系统中为systemd timer配置随机延迟,避免多台服务器定时任务同时执行导致的资源争用。通过设置RandomizedDelaySec参数,可让任务在指定时间范围内随机触发,有效分散负载,适用于备份、同步等场景,提升系统稳定性。
|
人工智能 移动开发 IDE
AI + 低代码技术揭秘(十):平台实施
VTJ 提供多平台部署支持,涵盖 Web、移动及跨平台环境。通过专用适配器和低代码优化,实现统一开发体验,并支持 Element Plus、Vant UI 等框架,提升开发效率与应用性能。
514 57
|
人工智能 小程序 程序员
分享5款实用软件,小众,但棒!
推荐5款小众但超好用的软件:KeyShot(三维渲染)、Sublime Text(文本编辑)、3171.CN(在线AI工具箱)、VideoProc(视频处理)、Everything(文件搜索),功能强大且高效,适合办公与创作,值得收藏使用。
1204 1

热门文章

最新文章