当前位置: 首页 > article >正文

FFmpeg源码:ffurl_seek2、ffurl_seek、avio_size函数分析

一、ffurl_seek2函数

ffurl_seek2函数定义在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的源文件libavformat/avio.c中:

int64_t ffurl_seek2(void *urlcontext, int64_t pos, int whence)
{
    URLContext *h = urlcontext;
    int64_t ret;

    if (!h->prot->url_seek)
        return AVERROR(ENOSYS);
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
    return ret;
}

该函数的作用是:改变形参urlcontext访问的资源的下一个读/写操作将使用的位置。

ffurl_seek2函数内部,h->prot->url_seek是函数指针:

typedef struct URLProtocol {
//...
    int64_t (*url_seek)( URLContext *h, int64_t pos, int whence);
//...
}

h->prot->url_seek不为空时,会调用h->prot->url_seek指向的回调函数:

ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);

而函数指针h->prot->url_seek一般指向libavformat/file.c中的file_seek函数。这时,执行ffurl_seek2函数等价于执行file_seek函数。关于file_seek函数用法可以参考:《FFmpeg源码:file_read、file_write、file_seek函数分析》。

所以当h->prot->url_seek指向file_seek函数时,ffurl_seek2函数的作用是:

ffurl_seek2(h,0,AVSEEK_SIZE):不改变读写位置,返回文件描述符为h->priv_data->fd的文件的大小;

ffurl_seek2(h,0,SEEK_SET):将读写位置移到文件描述符为h->priv_data->fd的文件的开头,此时返回0;

ffurl_seek2(h,0,SEEK_END):将读写位置移到文件尾,此时返回文件描述符为h->priv_data->fd的文件的大小;

ffurl_seek2(h,0,SEEK_CUR):取得目前文件位置。此时返回当前读写位置距离文件开头多少个字节;

二、ffurl_seek函数

ffurl_seek函数定义在libavformat/url.h中:

/**
 * Change the position that will be used by the next read/write
 * operation on the resource accessed by h.
 *
 * @param pos specifies the new position to set
 * @param whence specifies how pos should be interpreted, it must be
 * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
 * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
 * (return the filesize of the requested resource, pos is ignored).
 * @return a negative value corresponding to an AVERROR code in case
 * of failure, or the resulting file position, measured in bytes from
 * the beginning of the file. You can use this feature together with
 * SEEK_CUR to read the current file position.
 */
static inline int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
{
    return ffurl_seek2(h, pos, whence);
}

可以看到该函数内部仅调用了ffurl_seek2函数,所以ffurl_seek函数和ffurl_seek2函数用法完全等价。不同点仅仅在于ffurl_seek函数用static和inline关键字修饰,内联展开节省了每次调用函数的开销,能提高程序性能,具体可以参考:《C语言static和inline》。

三、avio_size函数

avio_size函数定义在libavformat/aviobuf.c中:

/**
 * Get the filesize.
 * @return filesize or AVERROR
 */
int64_t avio_size(AVIOContext *s)
{
    FFIOContext *const ctx = ffiocontext(s);
    int64_t size;

    if (!s)
        return AVERROR(EINVAL);

    if (ctx->written_output_size)
        return ctx->written_output_size;

    if (!s->seek)
        return AVERROR(ENOSYS);
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
    if (size < 0) {
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
            return size;
        size++;
        s->seek(s->opaque, s->pos, SEEK_SET);
    }
    return size;
}

该函数的作用是:返回文件描述符为s->opaque->priv_data->fd的文件的大小,单位为字节。该函数内部首先会尝试通过fstat函数获取文件大小,如果获取失败,会继续尝试通过lseek函数获取文件大小。

可以看到avio_size函数内部调用了函数指针s->seek指向的回调函数,而s->seek一般指向ffurl_seek2函数,所以下面语句相当于执行了ffurl_seek2(s->opaque,0,AVSEEK_SIZE),从而能获取到文件大小:

size = s->seek(s->opaque, 0, AVSEEK_SIZE);

从 《FFmpeg源码:file_read、file_write、file_seek函数分析》中我们可以知道,ffurl_seek2(s->opaque,0,AVSEEK_SIZE)实际相当于执行了:

/* XXX: use llseek */
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
{
//...
    struct stat st;
    ret = fstat(c->fd, &st);
    return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
   //...
}

即通过fstat函数得到文件大小。

avio_size函数中执行语句“size = s->seek(s->opaque, 0, AVSEEK_SIZE)”后,如果得到的size值小于0,会继续执行:

    if (size < 0) {
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
            return size;
        size++;
        s->seek(s->opaque, s->pos, SEEK_SET);
    }

意思就是如果通过fstat函数获取文件大小失败,继续尝试通过lseek函数获取文件大小:

/* XXX: use llseek */
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
{
//...
    ret = lseek(c->fd, pos, whence);
 
    return ret < 0 ? AVERROR(errno) : ret;
}


http://www.kler.cn/news/283255.html

相关文章:

  • 使用HTML实现贪吃蛇游戏
  • 小猫爬山 dfs/状压
  • Redis中的数据类型及应用场景(面试版)
  • macos 自定义用户目录方法, /Users/xxx 用户文件存储路径自定义方法
  • 构建在线教育系统源码:企业培训APP开发的技术指南
  • 在中国使用wordpress建网站的主要有三类人
  • TransmittableThreadLocal
  • Word文档被锁定无法编辑怎么办?一键快速移除Word编辑限制
  • 计算机网络803-(3)数据链路层
  • 行为型设计模式-状态(state)模式
  • 并发容器简介
  • 闪存刷新机制文献的解读
  • 记录一次两台虚拟机Oracle rac 心跳不能建立的排查
  • 二分法介绍
  • 回归预测 | Matlab实现GWO-BP-Adaboost灰狼算法优化BP神经网络集成学习多输入单输出回归预测
  • centos 服务器之间实现免密登录
  • 家里养宠物空气净化器有用吗?哪款最值得推荐?
  • 53-java中的多态是怎么实现的
  • 在NextChat中接入SiliconCloud API 体验不同的开源先进大语言模型
  • 慢速连接攻击是什么?慢速连接攻击怎么防护?
  • Android Gsensor 移植
  • 智谱发布新一代基座模型
  • scrapy框架--快速了解
  • debug模式中调好,正常执行不生效
  • 安卓-广播LocalBroadcastManager
  • 标准c++---2
  • 什么是Socks5代理协议?揭秘其优势与应用
  • UDP英译汉网络词典
  • 在VB.net中,LINQ有什么查询表达式,举例说明
  • 掌握 Rust 中的 YAML 魔法:Serde_yaml 使用指南