FFmpeg源码:avio_read函数分析

=================================================================

AVIOContext结构体和其相关的函数分析:

FFmpeg源码:avio_r8、avio_rl16、avio_rl24、avio_rl32、avio_rl64函数分析

FFmpeg源码:read_packet_wrapper、fill_buffer函数分析

FFmpeg源码:avio_read函数分析

FFmpeg源码:avio_tell函数分析

=================================================================

一、avio_read函数的声明

avio_read函数声明在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的头文件libavformat/avio.h中:

cpp 复制代码
/**
 * Read size bytes from AVIOContext into buf.
 * @return number of bytes read or AVERROR
 */
int avio_read(AVIOContext *s, unsigned char *buf, int size);

该函数的作用是:首先尝试从AVIOContext输入缓冲区中读取数据,如果输入缓冲区中没有数据或者数据已被读完或者读完后还不够size个字节,通过文件描述符去读取本地媒体文件中的数据或者通过socket接收网络流中的数据,保存到形参buf指向的缓冲区中。

形参s:既是输入型参数也是输出型参数。指向一个AVIOContext(字节流上下文结构体)变量。执行avio_read函数后,s->buf_ptr等成员会发生相应变化。

形参buf:输出型参数。保存读上来的数据的缓冲区。

形参size:输入型参数。要读取的字节数。

返回值:返回一个非负数表示成功,此时返回实际读取到的字节数;返回一个负数表示出错。

二、avio_read函数的定义

avio_read函数定义在源文件libavformat/aviobuf.c中:

cpp 复制代码
int avio_read(AVIOContext *s, unsigned char *buf, int size)
{
    int len, size1;

    size1 = size;
    while (size > 0) {
        len = FFMIN(s->buf_end - s->buf_ptr, size);
        if (len == 0 || s->write_flag) {
            if((s->direct || size > s->buffer_size) && !s->update_checksum && s->read_packet) {
                // bypass the buffer and read data directly into buf
                len = read_packet_wrapper(s, buf, size);
                if (len == AVERROR_EOF) {
                    /* do not modify buffer if EOF reached so that a seek back can
                    be done without rereading data */
                    s->eof_reached = 1;
                    break;
                } else if (len < 0) {
                    s->eof_reached = 1;
                    s->error= len;
                    break;
                } else {
                    s->pos += len;
                    ffiocontext(s)->bytes_read += len;
                    s->bytes_read = ffiocontext(s)->bytes_read;
                    size -= len;
                    buf += len;
                    // reset the buffer
                    s->buf_ptr = s->buffer;
                    s->buf_end = s->buffer/* + len*/;
                }
            } else {
                fill_buffer(s);
                len = s->buf_end - s->buf_ptr;
                if (len == 0)
                    break;
            }
        } else {
            memcpy(buf, s->buf_ptr, len);
            buf += len;
            s->buf_ptr += len;
            size -= len;
        }
    }
    if (size1 == size) {
        if (s->error)      return s->error;
        if (avio_feof(s))  return AVERROR_EOF;
    }
    return size1 - size;
}

三、avio_read函数的内部实现分析

avio_read函数中,首先会判断AVIOContext输入缓冲区中还有多少数据未被读取(s->buf_end - s->buf_ptr),得到该值和"要读取的字节数(形参size的值)"中的最小值,保存到变量len中:

cpp 复制代码
len = FFMIN(s->buf_end - s->buf_ptr, size);

如果变量len的值不为0,意味着AVIOContext输入缓冲区中还有数据未被读取,通过memcpy函数从AVIOContext输入缓冲区(s->buf_ptr指向的缓冲区)中拷贝len个字节到形参buf指向的缓冲区中:

cpp 复制代码
        if (len == 0 || s->write_flag) {
//...
        } else {
            memcpy(buf, s->buf_ptr, len);
            buf += len;
            s->buf_ptr += len;
            size -= len;
        }

如果读完了AVIOContext输入缓冲区后还不满足要读取的字节数(形参size的值),调用fill_buffer函数( 关于fill_buffer函数的用法可以参考:《FFmpeg源码:read_packet_wrapper、fill_buffer函数分析》)使AVIOContext输入缓冲区得到新的数据,使得可以在下一次while循环中继续从AVIOContext输入缓冲区读取数据:

cpp 复制代码
        if (len == 0 || s->write_flag) {
            if((s->direct || size > s->buffer_size) && !s->update_checksum && s->read_packet) {
//...
            } else {
                fill_buffer(s);
                len = s->buf_end - s->buf_ptr;
                if (len == 0)
                    break;
            }

如果要读取的数据大于AVIOContext输入缓冲区的最大大小( size > s->buffer_size),调用read_packet_wrapper函数直接对本地媒体文件或网络流进行读取,不把读上来的数据保存到AVIOContext输入缓冲区,而是直接保存到形参buf指向的缓冲区中:

cpp 复制代码
if((s->direct || size > s->buffer_size) && !s->update_checksum && s->read_packet) {
// bypass the buffer and read data directly into buf
    len = read_packet_wrapper(s, buf, size);
//...
}

如果通过read_packet_wrapper函数读取成功,让s->pos增加实际读取到的字节数大小,重置AVIOContext输入缓冲区(让s->buf_ptr和s->buf_end都指向AVIOContext输入缓冲区的开头):

cpp 复制代码
                len = read_packet_wrapper(s, buf, size);
                if (len == AVERROR_EOF) {
                    //...
                } else if (len < 0) {
                    //...
                } else {
                    s->pos += len;
                    ffiocontext(s)->bytes_read += len;
                    s->bytes_read = ffiocontext(s)->bytes_read;
                    size -= len;
                    buf += len;
                    // reset the buffer
                    s->buf_ptr = s->buffer;
                    s->buf_end = s->buffer/* + len*/;
                }
相关推荐
飞猿_SIR3 小时前
Exoplayer(MediaX)实现音频变调和变速播放
android·ffmpeg·音视频
Enougme10 小时前
ffmpeg常用方法(一)
ffmpeg
gma9991 天前
【FFmpeg】拉流
ffmpeg
深图智能1 天前
算法仿真平台搭建1-FFMPEG+RtspSever快速搭建一个RTSP服务器
c++·算法·ffmpeg·视频编解码
IT技术分享社区2 天前
电脑软件:推荐一款非常强大的视频音频转换剪辑工具FFmpeg Batch AV Converter V3.12
ffmpeg·音视频·batch·剪辑工具
ICUD2 天前
FFmpeg+vvenc实现H.266的视频编解码教程
人工智能·ffmpeg·视频编解码·h.266
万里不留行2 天前
海康摄像头 + M7s(Monibuca) + FFmpeg + Python实现多个网络摄像头视频流推流
服务器·网络·python·ffmpeg
ICUD2 天前
FFmpeg使用H.266/H.264/H.265编解码视频教程
ffmpeg·h.264·h.266
宇宙无敌花心大萝卜3 天前
一种简单的快速批量视频抽取固定间隔帧截图的操作方法
ffmpeg·音视频·视频
飞猿_SIR3 天前
最新版本Exoplayer(MediaX)实现K歌原伴唱包括单音轨和双音轨
android·ffmpeg·音视频