当前位置:网站首页>Ffmpeg 4.3 add custom demuxer
Ffmpeg 4.3 add custom demuxer
2022-07-25 02:55:00 【Spend my whole life reading】
1 compile ffmpeg4.3
1.1 ffmpeg 4.3 download
From abroad git Warehouse downloading ffmeg4.3 The source code is slow , It is recommended to use domestic gitee Image download .
git clone https://gitee.com/mirrors/ffmpeg.git -b release/4.3
1.2 Compile code
./configure --enable-shared --prefix=/home/zhy/code/mypc/ffmpeg4.3/ffmpeg/install_lib
make
make install
1.3 Environment variable configuration
Build after compilation ffmpeg、ffprobe Etc , perform ffmeg See if the compilation is successful :
./ffmpeg -version
After executing the order, it will report so A mistake we can't find :
ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory
The reason is that the environment variable is not configured , Can't find so Link path , You need to do the following :
sudo vim /etc/ld.so.conf
stay conf Add prefix Specified in the lib install route , Enter after adding :
include /etc/ld.so.conf.d/*.conf
/home/zhy/code/mypc/ffmpeg4.3/ffmpeg/install_lib/lib
Finally, update the configuration :
sudo ldconfig
Re execution ./ffmpeg -version The order will be normal .
[email protected]:~/code/mypc/ffmpeg4.3/ffmpeg$ ./ffmpeg -version
ffmpeg version n4.3.2-168-g3aba8b176f Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --enable-shared --prefix=/home/zhy/code/mypc/ffmpeg4.3/ffmpeg/install_lib
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
[email protected]:~/code/mypc/ffmpeg4.3/ffmpeg$
2 Add custom demuxer
ffmpeg In a dumuxer Need to include read_header、read_packet And so on , Such as mpegts demuxer As shown below :
AVInputFormat ff_mpegtsraw_demuxer = {
.name = "mpegtsraw",
.long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
.priv_data_size = sizeof(MpegTSContext),
.read_header = mpegts_read_header,
.read_packet = mpegts_raw_read_packet,
.read_close = mpegts_read_close,
.read_timestamp = mpegts_get_dts,
.flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
.priv_class = &mpegtsraw_class,
};
Generally speaking audio and video Of demuxer More complicated , So choose to add one image Of demuxer,image The format of... Is selected webp,webp Format spec See reference article .
2.1 add to demuxer Definition
stay libavformat\rawdec.h Add ff_webp_demuxer Definition :
#define FF_DEF_RAWIMAGE_DEMUXER(shortname, longname, probe, ext, id, flag)\ FF_RAW_DEMUXER_CLASS(shortname)\ AVInputFormat ff_ ## shortname ## _demuxer = {\ .name = #shortname,\ .long_name = NULL_IF_CONFIG_SMALL(longname),\ .read_probe = probe,\ .read_header = ff_raw_image_read_header,\ .read_packet = ff_raw_image_read_packet,\ .extensions = ext,\ .flags = flag,\ .raw_codec_id = id,\ .priv_data_size = sizeof(FFRawVideoDemuxerContext),\ .priv_class = &shortname ## _demuxer_class,\ };
2.2 add to demuxer Definition
stay libavformat\rawdec.c Add implementation related functions in ff_raw_image_read_header、ff_raw_image_read_packet Function and webp Of probe function .
probe Function is mainly used to speculate container Of fmt,webp The format of pictures is mainly through "RIFF" and ”WEBP“ Two conjectures ,probe The function is shown below :
static int webp_probe(const AVProbeData *p)
{
uint8_t *b = p->buf;
printf("[debug] webp_probe\n");
if (AV_RB32(b) == MKBETAG('R', 'I', 'F', 'F')
&& AV_RB32(b + 8) == MKBETAG('W', 'E', 'B', 'P'))
{
printf("[debug] is webp image \n");
return AVPROBE_SCORE_MAX - 1;
}
return 0;
}
ff_raw_image_read_packet Functions do not need special treatment , Every read must size Of buffer That's all right. .
int ff_raw_image_read_packet(AVFormatContext *s, AVPacket *pkt)
{
int ret, size = 1024;
ret = av_get_packet(s->pb, pkt, size);
if (ret < 0) {
return ret;
}
pkt->size = ret;
pkt->stream_index = 0;
pkt->flags = AV_PKT_FLAG_KEY;
return ret;
}
read_header Function usually reads pictures width、height As well as some flag Information ,ff_raw_image_read_header The function is implemented as follows :
int ff_raw_image_read_header(AVFormatContext *s)
{
AVStream *st;
FFRawVideoDemuxerContext *s1 = s->priv_data;
int ret = 0;
printf("[debug] %s: %d\n", __func__, __LINE__);
st = avformat_new_stream(s, NULL);
if (!st) {
ret = AVERROR(ENOMEM);
goto fail;
}
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = s->iformat->raw_codec_id;
st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
if (AV_CODEC_ID_WEBP == st->codecpar->codec_id)
{
webp_read_info(s, st);
}
fail:
return ret;
}
among webp_read_info Mainly based on webp container Extracted some information ,webp container The most commonly used structures are shown below , See the reference article for details webp Official documents :
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| WebP file header (12 bytes) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ChunkHeader('VP8X') |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Rsv|I|L|E|X|A|R| Reserved |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Canvas Width Minus One | ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
... Canvas Height Minus One |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
among , WebP file header as follows :
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 'R' | 'I' | 'F' | 'F' |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| File Size |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 'W' | 'E' | 'B' | 'P' |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
According to the above structure ,webp_read_info The content in is as follows :
static int webp_read_info (AVFormatContext *s ,AVStream *st)
{
#define VP8X_FLAG_ANIMATION 0x02
#define VP8X_FLAG_XMP_METADATA 0x04
#define VP8X_FLAG_EXIF_METADATA 0x08
#define VP8X_FLAG_ALPHA 0x10
#define VP8X_FLAG_ICC 0x20
int ret = 0;
int len = 0;
int64_t pos = 0;
int64_t cur_pos = 0;
AVIOContext *pb = s->pb;
int flag = 0;
printf("[debug] %s: %d\n", __func__, __LINE__);
if (NULL == pb)
{
printf("[error] %s: %d invalid input \n", __func__, __LINE__);
goto END;
}
pos = avio_tell(pb);
avio_seek(pb, 0, SEEK_SET);
/*read RIFF tag*/
ret = avio_rb32(pb);
if (ret == MKBETAG('R', 'I', 'F', 'F'))
{
printf("[debug] read RIFF tag \n");
}
else
{
goto END;
}
/*read size*/
ret = avio_rb32(pb);
/*read WEBP tag*/
ret = avio_rb32(pb);
if (ret == MKBETAG('W', 'E', 'B', 'P'))
{
printf("[debug] read WEBP tag \n");
}
/*read VP8X tag*/
ret = avio_rb32(pb);
if (ret == MKBETAG('V', 'P', '8', 'X'))
{
printf("[debug] read VP8X tag \n");
}
/*read size*/
ret = avio_rb32(pb);
/*read flag*/
flag = avio_r8(pb);
printf(" ICCP: %d\n Alpha: %d\n EXIF: %d\n XMP: %d\n Animation: %d\n",
(flag & VP8X_FLAG_ICC) != 0,
(flag & VP8X_FLAG_ALPHA) != 0,
(flag & VP8X_FLAG_EXIF_METADATA) != 0,
(flag & VP8X_FLAG_XMP_METADATA) != 0,
(flag & VP8X_FLAG_ANIMATION) != 0);
avio_seek(pb, pos, SEEK_SET);
END:
return ret;
}
Mainly read VP8X Chunk Some of flag, More detailed webp demux You can refer to libwebp Realization .
2.3 Replace ffmpeg Medium webp demuxer
ffmpeg The Central Plains originally had webp Of demuxer , So you need to libavformat\allformats.c Before the note ff_image_webp_pipe_demuxer And add custom ff_webp_demuxer
/* image demuxers */
//extern AVInputFormat ff_image_webp_pipe_demuxer;
extern AVInputFormat ff_image_xpm_pipe_demuxer;
extern AVInputFormat ff_image_xwd_pipe_demuxer;
extern AVInputFormat ff_webp_demuxer;/*new add*/
Comment out ff_image_webp_pipe_demuxer Compilation only requires ff_image_webp_pipe_demuxer Just kill the place you call , Finally, it needs to be in config.h Close your own webp demuxer, Set to 0.
#define CONFIG_IMAGE_WEBP_PIPE_DEMUXER 0
2.4 Running results
Recompile and install, test ffprobe:
./ffprobe samples/webp/animation/animated-webp-supported.webp
Running results :
[email protected]:~/code/mypc/ffmpeg4.3/ffmpeg$ ./ffprobe samples/webp/animation/animated-webp-supported.webp
ffprobe version n4.3.2-168-g3aba8b176f Copyright (c) 2007-2021 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --enable-shared --prefix=/home/zhy/code/mypc/ffmpeg4.3/ffmpeg/install_lib
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
[debug] webp_probe
[debug] is webp image
[debug] ff_raw_image_read_header: 338
[debug] webp_read_info: 251
[debug] read RIFF tag
[debug] read WEBP tag
[debug] read VP8X tag
ICCP: 0
Alpha: 1
EXIF: 0
XMP: 0
Animation: 1
[webp @ 0x5618e1c420c0] skipping unsupported chunk: ANIM
[webp @ 0x5618e1c420c0] skipping unsupported chunk: ANMF
Last message repeated 11 times
[webp @ 0x5618e1c420c0] image data not found
[webp @ 0x5618e1c40d80] decoding for stream 0 failed
[webp @ 0x5618e1c40d80] Could not find codec parameters for stream 0 (Video: webp, none): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, webp, from 'samples/webp/animation/animated-webp-supported.webp':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: webp, none, 90k tbr, 90k tbn, 90k tbc
[email protected]:~/code/mypc/ffmpeg4.3/ffmpeg$
libwebp in webpinfo Read the results :
[email protected]:~/code/mypc/mutil_media/libwebp/examples$ ./webpinfo samples/webp/animation/animated-webp-supported.webp
File: samples/webp/animation/animated-webp-supported.webp
RIFF HEADER:
File size: 37342
Chunk VP8X at offset 12, length 18
[leif] webp_info->feature_flags_ = 18
ICCP: 0
Alpha: 1
EXIF: 0
XMP: 0
Animation: 1
Canvas size 400 x 400
It can be seen that , Read the flag And use libwebp Consistent reading , Prove the added customization webp Of demuxer function OK.
Reference article
1 https://developers.google.com/speed/webp/docs/riff_container
边栏推荐
- Sequence diagram of UML diagram series
- Learning Record V
- Automatic backup of Linux server PostgreSQL database
- Tp5.1 paging (with parameter transfer)
- Daily three questions 7.16
- Selenium framework operation steelth.min.js file hides browser fingerprint features
- Operator explanation - C language
- Matlab for circular pit
- Redux best practices "Redux toolkit"
- 6. Object storage
猜你喜欢

JS foundation -- hijacking of this keyword

Domestic edge computing organization and product research

Case analysis of building exhibition service management system with low code development platform

Explanation of C language file operation function

Read and upgrade st-link chip information and SWD burning media through STM32 stlink utility tool

Redux best practices "Redux toolkit"

Classic network learning RESNET code implementation

Ctfshow misc introduction

Rolling division, Young's matrix and three-step flip

Error: tomee required to support ear/ejb deployment
随机推荐
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
DOM node type
Request and response
List.stream common operations
Sequence diagram of UML diagram series
Tensorflow's study notes (I)
Picgo configuring Alibaba cloud OSS
Introduction to web security telent testing and defense
Domestic edge computing organization and product research
C language: Structure -- a detailed explanation of memory byte alignment
JS foundation -- task queue and event loop
StrError and PERROR
Permanently mount the image steps
Read and upgrade st-link chip information and SWD burning media through STM32 stlink utility tool
Keras load history H5 format model error: attributeerror 'STR' object has no attribute 'decode‘
TS uses a third-party library, and there is no type declaration file error handling
DLL load failed: the page file is too small to complete the operation
Execution methods with static code blocks and child and parent classes
Learning notes - talking about the data structure and algorithm of MySQL index and the introduction of index
Redis unauthorized access vulnerability recurrence (www.hetianlab.com)