当前位置:网站首页>Ffmpeg first learning (only for coding)
Ffmpeg first learning (only for coding)
2022-07-25 23:10:00 【Zhuge hammer who loves learning】
Learning resources
Official documents : Documentation
Thor blog
It can be moved [ summary ]FFMPEG Video and audio codec zero basic learning method
Structure learning :FFMPEG Structural analysis
Basic knowledge of
Video coding
The main function of video coding is to encode video pixel data (RGB,YUV etc. ) Compressed into a video stream , So as to reduce the amount of video data . The video is too big without compression .
Main video coding
| name | Launch mechanism |
|---|---|
| HEVC(H.265) | MPEG/ITU-T |
| H.264 | MPEG/ITU-T |
| MPEG4 | MPEG |
| MPEG2 | MPEG |
| VP9 | |
| VP8 | |
| VC-1 | Microsoft Inc. |
Pros and cons
HEVC(H.265) > VP9 > H.264 > VP8 > MPEG4 > H.263 > MPEG2
Structure learning
AVFrame Structures are generally used to store raw data ( That is, uncompressed data , For example, for video YUV,RGB, For audio, it's PCM), In addition, it contains some related information
AVCodecContext yes FFmpeg The structure of the codec context , and AVCodec Is the structure of encoding and decoding parameters
AVPacket It's a structure that stores information about compressed encoded data
AVCodec Is a structure that stores codec information
Structure relation
Every AVStream Store a video / Relevant data of audio stream ; Every AVStream Corresponding to one AVCodecContext, Store the video / The audio stream uses decoded data ;
Every AVCodecContext Corresponding to one in AVCodec, Including the video / The decoder for the audio . Each decoder corresponds to one AVCodec structure .
official demo And encode.c Learn to understand
Learn official demo:encode_video.c
I'm personally responsible for the official demo Modified and annotated , The modified file can be run directly , Has been uploaded to the Resource download address
Process analysis :
- The file path of the given output after the program runs
- call
avcodec_find_encoderFind the encoder AVCodec - call
avcodec_alloc_context3Assign encoder context AVCodecContext - call
av_packet_allocDistribute AVPacket, The encoder will put each AVFrame Encoded as AVPacket - to AVCodecContext Configuration parameters , Such as bit rate 、 Video width and height 、 Frame format and so on
- call
avcodec_open2Turn on the encoder - call
av_frame_allocDistribute AVFrame Memory and set the format and width of the frame - call
av_frame_get_bufferAllocate space for frame data - Start for loop , First call
av_frame_make_writableEnsure frame data AVFrame Can write , Then prepare a frame of virtual image data to writeframe->data, Finally, for each frame pts assignment , Tell the player when to display this frame data - Start coding , call
avcodec_send_framePut frame data AVFrame Send to encoder - call
avcodec_receive_packetPut the encoded frame AVPacket Take it out of the encoder , Then write each encoded frame into the output file File , Call after writingav_packet_unrefRelease right AVPacket References to - If the encoder ID yes MPEG, You need to output files to File Add a fixed sequence at the end of to get a real MPEG file
- Finally close the file File, And release AVCodecContext、AVFrame、AVPacket Memory footprint , call
avcodec_free_context、av_frame_free、av_packet_free
API Study
avcodec_find_encoder
According to a AVCodecID Find a registered encoder
const AVCodec *avcodec_find_encoder(enum AVCodecID id);
common AVCodecID Casually list some
enum AVCodecID {
AV_CODEC_ID_NONE,
/* video codecs */
AV_CODEC_ID_H263,
AV_CODEC_ID_MPEG4,
AV_CODEC_ID_H264,
AV_CODEC_ID_HEVC,
}
example :
const AVCodec *codec;
enum AVCodecID codec_id;
codec_id = AV_CODEC_ID_HEVC;
codec = avcodec_find_encoder(codec_id);
if (!codec) {
printf("Codec '%d' not found\n", codec_id);
exit(1);
}
avcodec_alloc_context3
by AVCodecContext Structure allocates memory , And set each field as the default value , The structure returned by the result needs to use avcodec_free_context() Release memory
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
example :
AVCodecContext *c= NULL;
const AVCodec *codec;
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
...
...
...
avcodec_free_context(&c);
av_packet_alloc
by AVPacket Structure allocates memory , And set each field as the default value , The structure returned by the result needs to use av_packet_free() Release memory
AVPacket *av_packet_alloc(void);
example :
AVPacket *pkt;
pkt = av_packet_alloc();
if (!pkt)
exit(1);
...
...
...
av_packet_free(&pkt);
avcodec_open2
initialization AVCodecContext To use given AVCodec , Before using this function, you need to use avcodec_alloc_context3() Allocate memory
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
av_frame_get_buffer
Assign a new buffers Give video data
Before calling this function, you need to set frame Some parameters of format、width、height
int av_frame_get_buffer(AVFrame *frame, int align);
Parameters align Setting to 0, The system will be based on the current CPU Automatic setting alignment
example :
AVFrame *frame;
frame = av_frame_alloc();
frame->format = c->pix_fmt;
frame->width = c->width;
frame->height = c->height;
av_frame_get_buffer(frame, 32);
avcodec_send_frame
Send data frame to encoder . Use avcodec_receive_packet() Function to retrieve buffered output packets
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
avcodec_receive_packet
Read the encoded data from the encoder
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
Return type :
0 It means success
AVERROR(EAGAIN) Output is currently unavailable , The user must give input
AVERROR_EOF The encoder is completely flushed , There will be no output packets
AVERROR(EINVAL) codec It's not turned on , Or it is a decoder rather than an encoder
example :
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
Conclusion
Just started learning ffmpeg, Feel the stone to cross the river , Little by little demo And the source code is quite interesting
English reading needs improving , Otherwise, you won't see the comments given by the developer
边栏推荐
- ASP date function (what if the disk function is incorrect)
- HJ7 取近似值
- Learning notes of technical art hundred people plan (2) -- vector
- The third experiment OSPF
- Rental experience post
- Summary of traversal methods of six sets list, set, map, queue, deque and stack
- Week 2: convolutional neural network
- Network Security Learning (XIV) IP protocol
- Analysis of Excel file
- 每周推荐短视频:需要协同的智能设备越来越多,给物联网开发提出更大挑战?
猜你喜欢

QT operation to solve large amount of duplicate data

Single model common sense reasoning first surpasses human beings! HFL summit openbookqa challenge
![[literature reading] - HRL -[hrl with universal policies for multi step robotic control]](/img/34/06d5ba3af4e6e775a335324c020161.png)
[literature reading] - HRL -[hrl with universal policies for multi step robotic control]

5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)

Mongodb features, differences with MySQL, and application scenarios

CUDA environment construction

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

DHCP first static experiment

AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario

单模型常识推理首超人类!HFL登顶OpenBookQA挑战赛
随机推荐
【论文笔记】A Meta-Reinforcement Learning Algorithm for Causal Discovery
Design of Butterworth filter and drawing of amplitude frequency characteristic curve
Network Security Learning notes-1 file upload
连续三年成为云AI服务领导者,亚马逊云科技做对了什么?
码蹄集 精准弹幕
Ip--- ia review
PCL basic operation Encyclopedia
【接口性能优化】索引失效的原因以及如何进行SQL优化
Redis过期键的删除策略[通俗易懂]
Data filtering of MATLAB
How does PHP remove an element from an array based on the key value
Single model common sense reasoning first surpasses human beings! HFL summit openbookqa challenge
Network Security Learning (XII) OSI and TCP
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
ZCMU--5015: 完成任务
QT operation to solve large amount of duplicate data
Mongodb features, differences with MySQL, and application scenarios
如何获取广告服务流量变现数据,助力广告效果分析?
The difference between "rewrite" and "overload"
IPFs of Internet Protocol