当前位置:网站首页>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
边栏推荐
- [TinyML]EfficientFormer:Vision Transformers at MobileNet Speed
- Mgre.hdlc.ppp.chap.nat comprehensive experiment
- JS foundation -- regular expression
- Mp4 package analysis
- How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
- JS written test question -- promise, setTimeout, task queue comprehensive question
- TS uses a third-party library, and there is no type declaration file error handling
- JS foundation -- object static method
- Class notes (4) (2) -- 572. Compete
- Get to know string thoroughly
猜你喜欢

Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt

String class

Publish the project online and don't want to open a port

Tp5.1 include include files (reference public files)

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port

Domestic edge computing organization and product research

Sequence diagram of UML diagram series

Mark down learning

Custom types in C language

MySQL common function summary, very practical, often encountered in interviews
随机推荐
Pypi counts the number of Downloads
JS written test question -- browser kernel
Matlab for circular pit
Physical experiment simulation
Learning notes - talking about the data structure and algorithm of MySQL index and the introduction of index
Use unicloud cloud function to decode wechat motion steps in applet
Mgre.hdlc.ppp.chap.nat comprehensive experiment
Domain driven model (DDD)
Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template
UDP message structure and precautions
Picgo configuring Alibaba cloud OSS
Arduino IDE for raspberry PI Pico development firmware localization installation tutorial
Learning record 12
Routing policy interferes with routing
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
Mark down learning
Explorer TSSD 2019 software installation package download and installation tutorial
Permanently mount the image steps
Study notes of filebeat
Ctfshow misc introduction