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

一、ffurl_seek2函数

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

cpp 复制代码
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是函数指针:

cpp 复制代码
typedef struct URLProtocol {
//...
    int64_t (*url_seek)( URLContext *h, int64_t pos, int whence);
//...
}

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

cpp 复制代码
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中:

cpp 复制代码
/**
 * 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中:

cpp 复制代码
/**
 * 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),从而能获取到文件大小:

cpp 复制代码
size = s->seek(s->opaque, 0, AVSEEK_SIZE);

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

cpp 复制代码
/* 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,会继续执行:

cpp 复制代码
    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函数获取文件大小:

cpp 复制代码
/* 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;
}
相关推荐
shenhuxi_yu18 小时前
ffmpeg avio使用示例
ffmpeg
aqi002 天前
FFmpeg开发笔记(八十二)使用国产直播服务器smart_rtmpd执行推流操作
ffmpeg·音视频·直播·流媒体
陈增林2 天前
用 PyQt5 + FFmpeg 打造批量视频音频提取器
qt·ffmpeg·音视频
西瓜er2 天前
JAVA:Spring Boot 集成 FFmpeg 实现多媒体处理
java·spring boot·ffmpeg
QMCY_jason3 天前
ubuntu 24.04 FFmpeg编译 带Nvidia 加速记录
linux·ubuntu·ffmpeg
eqwaak03 天前
动态图表导出与视频生成:精通Matplotlib Animation与FFmpeg
开发语言·python·ffmpeg·音视频·matplotlib
执尺量北斗3 天前
LinkMate 智能会议室系统:基于 Qt / QML / WebRTC / FFmpeg / Whisper / OpenGL 的实时音视频会议平台
qt·ffmpeg·webrtc
月起星九4 天前
为什么ffmpeg进行视频合成有时长误差
ffmpeg·音视频
长沙红胖子Qt5 天前
FFmpeg开发笔记(十二):ffmpeg音频处理、采集麦克风音频录音为WAV
ffmpeg·pcm·wav·录音·麦克风
aqi006 天前
FFmpeg开发笔记(八十一)FFmpeg代码对RTSP和RTMP的推流区别
ffmpeg·音视频·直播·流媒体