注:写了一系列的结构体的分析的文章,在这里列一个列表:
FFMPEG结构体分析:AVFrameFFMPEG结构体分析:AVFormatContext
FFMPEG结构体分析:AVCodecContext
FFMPEG结构体分析:AVIOContext
FFMPEG结构体分析:AVCodec
FFMPEG结构体分析:AVStream
FFMPEG结构体分析:AVPacket
FFMPEG有几个最重要的结构体,包含了解协议,解封装,解码操作,此前已经进行过分析:
在此不再详述,其中AVIOContext是FFMPEG管理输入输出数据的结构体。本文将会详细分析一下该结构体里每个变量的含义和作用。
首先看一下结构体的定义(位于avio.h):
/* 雷霄骅 * 中国传媒大学/数字电视技术 * leixiaohua1020@126.com * */ /** * Bytestream IO Context. * New fields can be added to the end with minor version bumps. * Removal, reordering and changes to existing fields require a major * version bump. * sizeof(AVIOContext) must not be used outside libav*. * * @note None of the function pointers in AVIOContext should be called * directly, they should only be set by the client application * when implementing custom I/O. Normally these are set to the * function pointers specified in avio_alloc_context() */ typedef struct { /** * A class for private options. * * If this AVIOContext is created by avio_open2(), av_class is set and * passes the options down to protocols. * * If this AVIOContext is manually allocated, then av_class may be set by * the caller. * * warning -- this field can be NULL, be sure to not pass this AVIOContext * to any av_opt_* functions in that case. */ AVClass *av_class; unsigned char *buffer; /**< Start of the buffer. */ int buffer_size; /**< Maximum buffer size */ unsigned char *buf_ptr; /**< Current position in the buffer */ unsigned char *buf_end; /**< End of the data, may be less than buffer+buffer_size if the read function returned less data than requested, e.g. for streams where no more data has been received yet. */ void *opaque; /**< A private pointer, passed to the read/write/seek/... functions. */ int (*read_packet)(void *opaque, uint8_t *buf, int buf_size); int (*write_packet)(void *opaque, uint8_t *buf, int buf_size); int64_t (*seek)(void *opaque, int64_t offset, int whence); int64_t pos; /**< position in the file of the current buffer */ int must_flush; /**< true if the next seek should flush */ int eof_reached; /**< true if eof reached */ int write_flag; /**< true if open for writing */ int max_packet_size; unsigned long checksum; unsigned char *checksum_ptr; unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size); int error; /**< contains the error code or 0 if no error happened */ /** * Pause or resume playback for network streaming protocols - e.g. MMS. */ int (*read_pause)(void *opaque, int pause); /** * Seek to a given timestamp in stream with the specified stream_index. * Needed for some network streaming protocols which don't support seeking * to byte position. */ int64_t (*read_seek)(void *opaque, int stream_index, int64_t timestamp, int flags); /** * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable. */ int seekable; /** * max filesize, used to limit allocations * This field is internal to libavformat and access from outside is not allowed. */ int64_t maxsize; } AVIOContext;
AVIOContext中有以下几个变量比较重要:
unsigned char *buffer:缓存开始位置
int buffer_size:缓存大小(默认32768)
unsigned char *buf_ptr:当前指针读取到的位置
unsigned char *buf_end:缓存结束的位置
void *opaque:URLContext结构体
在解码的情况下,buffer用于存储ffmpeg读入的数据。例如打开一个视频文件的时候,先把数据从硬盘读入buffer,然后在送给解码器用于解码。
其中opaque指向了URLContext。注意,这个结构体并不在FFMPEG提供的头文件中,而是在FFMPEG的源代码中。从FFMPEG源代码中翻出的定义如下所示:
typedef struct URLContext { const AVClass *av_class; ///< information for av_log(). Set by url_open(). struct URLProtocol *prot; int flags; int is_streamed; /**< true if streamed (no seek possible), default = false */ int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */ void *priv_data; char *filename; /**< specified URL */ int is_connected; AVIOInterruptCB interrupt_callback; } URLContext;
URLContext结构体中还有一个结构体URLProtocol。注:每种协议(rtp,rtmp,file等)对应一个URLProtocol。这个结构体也不在FFMPEG提供的头文件中。从FFMPEG源代码中翻出其的定义:
typedef struct URLProtocol { const char *name; int (*url_open)(URLContext *h, const char *url, int flags); int (*url_read)(URLContext *h, unsigned char *buf, int size); int (*url_write)(URLContext *h, const unsigned char *buf, int size); int64_t (*url_seek)(URLContext *h, int64_t pos, int whence); int (*url_close)(URLContext *h); struct URLProtocol *next; int (*url_read_pause)(URLContext *h, int pause); int64_t (*url_read_seek)(URLContext *h, int stream_index, int64_t timestamp, int flags); int (*url_get_file_handle)(URLContext *h); int priv_data_size; const AVClass *priv_data_class; int flags; int (*url_check)(URLContext *h, int mask); } URLProtocol;
在这个结构体中,除了一些回调函数接口之外,有一个变量const char *name,该变量存储了协议的名称。每一种输入协议都对应这样一个结构体。
比如说,文件协议中代码如下(file.c):
URLProtocol ff_file_protocol = { .name = "file", .url_open = file_open, .url_read = file_read, .url_write = file_write, .url_seek = file_seek, .url_close = file_close, .url_get_file_handle = file_get_handle, .url_check = file_check, };
libRTMP中代码如下(libRTMP.c):
URLProtocol ff_rtmp_protocol = { .name = "rtmp", .url_open = rtmp_open, .url_read = rtmp_read, .url_write = rtmp_write, .url_close = rtmp_close, .url_read_pause = rtmp_read_pause, .url_read_seek = rtmp_read_seek, .url_get_file_handle = rtmp_get_file_handle, .priv_data_size = sizeof(RTMP), .flags = URL_PROTOCOL_FLAG_NETWORK, };
udp协议代码如下(udp.c):
等号右边的函数是完成具体读写功能的函数。可以看一下file协议的几个函数(其实就是读文件,写文件这样的操作)(file.c):
URLProtocol ff_udp_protocol = { .name = "udp", .url_open = udp_open, .url_read = udp_read, .url_write = udp_write, .url_close = udp_close, .url_get_file_handle = udp_get_file_handle, .priv_data_size = sizeof(UDPContext), .flags = URL_PROTOCOL_FLAG_NETWORK, };
等号右边的函数是完成具体读写功能的函数。可以看一下file协议的几个函数(其实就是读文件,写文件这样的操作)(file.c):
/* *雷霄骅 *leixiaohua1020@126.com *中国传媒大学/数字电视技术 */ /* standard file protocol */ static int file_read(URLContext *h, unsigned char *buf, int size) { int fd = (intptr_t) h->priv_data; int r = read(fd, buf, size); return (-1 == r)?AVERROR(errno):r; } static int file_write(URLContext *h, const unsigned char *buf, int size) { int fd = (intptr_t) h->priv_data; int r = write(fd, buf, size); return (-1 == r)?AVERROR(errno):r; } static int file_get_handle(URLContext *h) { return (intptr_t) h->priv_data; } static int file_check(URLContext *h, int mask) { struct stat st; int ret = stat(h->filename, &st); if (ret < 0) return AVERROR(errno); ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0; ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0; return ret; } #if CONFIG_FILE_PROTOCOL static int file_open(URLContext *h, const char *filename, int flags) { int access; int fd; av_strstart(filename, "file:", &filename); if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) { access = O_CREAT | O_TRUNC | O_RDWR; } else if (flags & AVIO_FLAG_WRITE) { access = O_CREAT | O_TRUNC | O_WRONLY; } else { access = O_RDONLY; } #ifdef O_BINARY access |= O_BINARY; #endif fd = open(filename, access, 0666); if (fd == -1) return AVERROR(errno); h->priv_data = (void *) (intptr_t) fd; return 0; } /* XXX: use llseek */ static int64_t file_seek(URLContext *h, int64_t pos, int whence) { int fd = (intptr_t) h->priv_data; if (whence == AVSEEK_SIZE) { struct stat st; int ret = fstat(fd, &st); return ret < 0 ? AVERROR(errno) : st.st_size; } return lseek(fd, pos, whence); } static int file_close(URLContext *h) { int fd = (intptr_t) h->priv_data; return close(fd); }
相关推荐
本文对在使用ffmpeg进行音视频编解码时使用到的一些函数做一个简单介绍,我当前使用的ffmpeg版本为:0.8.5,因为本人发现在不同的版本中,有些函数名称会有点小改动,所以在此有必要说明下ffmpeg的版本号。
【FFmpeg4Android:视频播放】包含视频jni层播放(原生播放)与java层播放。分别人提供不同的使用需求。
FFmpeg4Android:视频文件推流到nginx服务器(源码) RTMP推流器(Streamer)的在流媒体系统中的作用可以用下图表示。首先将视频数据以RTMP的形式发送到流媒体服务器端(Server,比如FMS,Red5,Wowza等),然后...
opencv4.1.0使用cmake编译时,提示FFMPEG: Download failed: 6;"Couldn't resolve host name
用于将FFMPEG2.0中的结构体初始化代码转换为vs2008形式的
该解决方案包含了使用FFmpeg进行封装格式处理的各种例子: simplest ffmpeg demuxer:视音频分离器 simplest ffmpeg demuxer simple:视音频分离器(简化版) simplest ffmpeg muxer:视音频复用器 simplest ...
ffmpeg-streamerstream local media files to streaming media server (Use RTMP as example).本例子实现了推送本地视频至流媒体服务器(以RTMP为例)。是使用FFmpeg进行流媒体推送最简单的教程。simplest_ffmpeg_...
Maven坐标:org.bytedeco:ffmpeg:4.3.2-1.5.5; 标签:bytedeco、ffmpeg、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...
c++实现基于ffmpeg实现了解析rtsp视频为cv:Mat格式,将Mat数据推送到rtmp服务。。。
最早学习ffmpeg的一个资源,现在已经下不到了,重新上传下
ffmpeg.js 该库提供了使用将FFmpeg构建移植到JavaScript的功能。 生成针对浏览器内部使用进行了优化:最小的大小可加快加载速度,asm.js,性能调整等。尽管它们也可在Node中使用。 建物 当前可用的版本(将来可能会...
参考MSYS2: ://www.msys2.org/ 吃豆人镜像: : pacman代理: : FFmpeg: : MinGW / FFmpeg安装指南: : VSCode: ://code.visualstudio.com/ VSCode的C / C ++: ://code.visualstudio.com/docs/languages/cpp ...
该解决方案包含了使用FFmpeg进行封装格式处理的各种例子: simplest_ffmpeg_demuxer:视音频分离器。 simplest_ffmpeg_demuxer_simple:视音频分离器(简化版)。 simplest_ffmpeg_muxer:视音频复用器。 simplest_...
本程序使用opencv3.2+ffmpeg3.4编译,实现Mat到mp4的无缓冲,并可以设置比特率、帧率,实现高质量图片与高质量视频的转换,说明请查看https://blog.csdn.net/weixin_39212021
ffmpeg常用结构体树状图
ffmpeg标准化 :headphone: 使用ffmpeg进行音频响度标准化。安装npm install ffmpeg-normalize用法埃布R128 const normalize = require ( 'ffmpeg-normalize' ) ;normalize ( { input : 'input.mp4' , output : '...
导入这个jar包后就可以使用ffmpeg的音频转码 mvn install:install-file -Dfile=E:\jave-1.0.2.jar //包的输入路径 -DgroupId=jave -DartifactId=jave -Dversion=1.0.2 -Dpackaging=jar //执行完成后 ...
ffmpeg构建脚本 在Mac OS上使用android NDK构建ffmpeg 。 跑步: $ ./build.sh ffmpeg: : NDK: :
simplest_ffmpeg_player:标准版,FFmpeg学习的开始。 simplest_ffmpeg_player_su:SU(SDL Update)版,加入了简单的SDL的Event。 simplest_ffmpeg_decoder:一个包含了封装格式处理功能的解码器。使用了libavcodec...
基于ffmpeg实现了解析rtsp视频为cv::Mat格式,此时可以对cv::Mat进行处理,然后再将cv::Mat数据推送到rtmp流媒体服务。处理、转码一条龙。另外,工程中关于OpenCV、ffmpeg的库和头文件的路径需要自己根据实际路径...