当前位置:网站首页>Ffplay realizes user-defined input stream playback
Ffplay realizes user-defined input stream playback
2022-06-23 07:28:00 【CodeOfCC】
List of articles
Preface
Use ffplay Play the video , Sometimes we can only get byte data , such as Windows The embedded resource of can only get the video file data in memory , Or it is a video transmitted over a custom protocol network , At this time, we need to implement a stream data input interface to play ,ffmpeg Of AVIOContext This function is supported , We just need to be right ffplay Simply expand .
One 、 How to use AVIOContext?
avio yes ffmpeg Custom input stream objects , It is AVformatContext A field of , I just need to create avio Object and implement its callback method , And then to AVformatContext.pb Assignment can .
1、 Define callback methods
Take file stream as an example ( Omit the operation of opening file and getting file length )
FILE* file;
static int avio_read(ACPlay play, uint8_t* buf, int bufsize)
{
return fread(buf, 1, bufsize, file);
}
static int64_t avio_seek(ACPlay play, int64_t offset, int whence)
{
switch (whence)
{
case AVSEEK_SIZE:
return fileSize;
break;
case SEEK_CUR:
fseek(file, offset, whence);
break;
case SEEK_SET:
fseek(file, offset, whence);
break;
case SEEK_END:
fseek(file, offset, whence);
break;
default:
break;
}
return ftell(test3file);
}
2、 relation AVFormatContext
AVFormatContext* ic = NULL;
AVIOContext* avio = avio_alloc_context((unsigned char*)av_malloc(1024 * 1024), 1024 * 1024, 0, s, avio_read, NULL, avio_seek);
if (avio)
{
ic->pb = avio;
ic->flags = AVFMT_FLAG_CUSTOM_IO;
}
avformat_open_input(&ic, "", NULL, NULL);
3、 Destroy resources
if (ic->avio)
{
if (ic->avio->buffer)
{
av_free(is->avio->buffer);
}
avio_context_free(&is->avio);
ic->avio = NULL;
}
Two 、ffplay Use in AVIOContext
1、 Add fields
stay VideoState Add the following fields
AVIOContext* avio;
2、 Defining interfaces
/// <summary>
/// Start playing
/// </summary>
/// <param name="play"> Player object </param>
/// <param name="read"> Custom input stream , Callback when reading data </param>
/// <param name="seek"> Custom input stream , Callback during positioning </param>
void ac_play_startViaCustomStream(ACPlay play, ACPlayCustomPacketReadCallback read, ACPlayCustomPacketStreamSeekCallback seek);
{
VideoState* s = (VideoState*)play;
if(read)
s->avio = avio_alloc_context((unsigned char*)av_malloc(1024 * 1024), 1024 * 1024, 0, s, read, NULL, seek);
stream_open(s, "", NULL);
}
3、 relation AVFormatContext
stay read_thread in avformat_open_input Add the following code to the previous line of the :
if (is->avio)
{
ic->pb = is->avio;
ic->flags = AVFMT_FLAG_CUSTOM_IO;
}
4、 Destroy resources
stay stream_close Add the following code to
if (ic->avio)
{
if (ic->avio->buffer)
{
av_free(is->avio->buffer);
}
avio_context_free(&is->avio);
ic->avio = NULL;
}
summary
That's what we're going to talk about today , The reason to realize this function is that the author used to work , I have encountered relevant usage scenarios , Play when the program starts mp4 Embed resources , It is obviously not a good solution to read it out and save the file for playback , and ffmpeg It supports custom input streams , So it's easy to add this feature to ffplay Yes . in general , This function has certain usage scenarios and its implementation is not complicated .
边栏推荐
猜你喜欢

【PyQt5系列】修改计数器实现控制

Initialization layer implementation

Akamai-1.75 version-_ Abck parameter generation JS reverse analysis

CIRIUM(睿思誉)逐渐成为航空公司二氧化碳排放报告的标准

The original cloud landed in deep water, and the cloud product family of Boyun container released four values

MySQL (VIII) - explain

Heuristic search strategy

Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family

User mode and kernel mode

基于51单片机的温度检测监测报警系统设计
随机推荐
About SQL: is there a way to fill in the null value in the field without adding fields on the basis of the original fields
如何达到高效的网络信息传播
How to solve CSRF attack in laravel
ldconfig 命令
Paddle version problem
junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth
G++ compilation command use
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
基于51单片机的温度检测监测报警系统设计
PSP代码实现
Specific help of OSI layered model to work
Nacos适配oracle11g-建表ddl语句
300. longest increasing subsequence
Redis setting password
The original cloud landed in deep water, and the cloud product family of Boyun container released four values
GloRe
Heterogeneous transaction scenario interaction process and consistency assurance
307. area and retrieval - array modifiable
deeplab v3 代码结构图
898. subarray bitwise OR operation