当前位置: 首页 > article >正文

[ffmpeg] 音视频编码

本文主要梳理 ffmpeg 中音视频编码的常用函数

API调用

常用 API

const AVCodec *avcodec_find_encoder(enum AVCodecID id);
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
void avcodec_free_context(AVCodecContext **avctx);
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); 
int avcodec_close(AVCodecContext *avctx); 
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);

class

static const AVClass av_codec_context_class = {
    .class_name              = "AVCodecContext",
    .item_name               = context_to_name,
    .option                  = avcodec_options,
    .version                 = LIBAVUTIL_VERSION_INT,
    .log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),
    .child_next              = codec_child_next,
    .child_class_iterate     = codec_child_class_iterate,
    .category                = AV_CLASS_CATEGORY_ENCODER,
    .get_category            = get_category,
};

初始化和销毁

avcodec_find_encoder 找到编码器
avcodec_alloc_context3 创建 context 结构体
视频:设置编码器参数,码率,宽高,时间基,帧率,GOP,视频格式,编码器ID
avcodec_open2 打开编码器
avcodec_close 关闭编码器
avcodec_free_context 销毁上下文

编码

avcodec_send_frame
avcodec_receive_packet
avcodec_receive_packet 传出来的 packet 使用结束需要调用 av_packet_unref 让 packet 的引用计数减一,不然会有内存泄漏

demo

m_vc = avcodec_alloc_context3(codec);
m_vc->bit_rate = m_vBitrate;
m_vc->width = m_outWidth;
m_vc->height = m_outHeight;
// 时间基数
m_vc->time_base = { 1, m_outFPS };
m_vc->framerate = { m_outFPS, 1 };

m_vc->gop_size = 50;
m_vc->max_b_frames = 0;

m_vc->pix_fmt = AV_PIX_FMT_YUV420P;
m_vc->codec_id = AV_CODEC_ID_H264;
av_opt_set(m_vc->priv_data, "preset", "superfast", 0);
m_vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

int ret = avcodec_open2(m_vc, codec, NULL);
ret = avcodec_send_frame(m_vc, m_yuv);
p = av_packet_alloc();
ret = avcodec_receive_packet(m_vc, p);
if (ret != 0 || p->size <= 0)
{
	av_packet_free(&p);
	return NULL;
}
//xxx
av_packet_unref(p);

if (m_vc)
{
	avcodec_close(m_vc);
	avcodec_free_context(&m_vc);
}

其他

codec.h 所有API

const AVCodec *av_codec_iterate(void **opaque);
const AVCodec *avcodec_find_decoder(enum AVCodecID id);
const AVCodec *avcodec_find_decoder_by_name(const char *name);
const AVCodec *avcodec_find_encoder(enum AVCodecID id);
const AVCodec *avcodec_find_encoder_by_name(const char *name);
int av_codec_is_encoder(const AVCodec *codec);
int av_codec_is_decoder(const AVCodec *codec);
const char *av_get_profile_name(const AVCodec *codec, int profile);
const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);

avcodec.h 所有API

unsigned avcodec_version(void);
const char *avcodec_configuration(void);
const char *avcodec_license(void);
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
void avcodec_free_context(AVCodecContext **avctx);
const AVClass *avcodec_get_class(void);
const AVClass *avcodec_get_frame_class(void);
const AVClass *avcodec_get_subtitle_rect_class(void); 
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec); 
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par);
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); 
int avcodec_close(AVCodecContext *avctx); 
void avsubtitle_free(AVSubtitle *sub); 
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags); 
int avcodec_default_get_encode_buffer(AVCodecContext *s, AVPacket *pkt, int flags);
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height);
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[AV_NUM_DATA_POINTERS]);
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos);
enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos);
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt);
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
int avcodec_get_hw_frames_parameters(AVCodecContext *avctx, AVBufferRef *device_ref, enum AVPixelFormat hw_pix_fmt, AVBufferRef **out_frames_ref);
const AVCodecParser *av_parser_iterate(void **opaque);
AVCodecParserContext *av_parser_init(int codec_id);
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos);
void av_parser_close(AVCodecParserContext *s);
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub);
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt);
enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr);
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat * fmt);
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode);
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size);
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count);
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align);
void avcodec_flush_buffers(AVCodecContext *avctx);
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes);
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size);
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size);
int avcodec_is_open(AVCodecContext *s);

http://www.kler.cn/news/313550.html

相关文章:

  • 星火AI-智能PPT生成 API 文档
  • vue3+ts+supermap iclient3d for cesium功能集合
  • 【论文笔记】Are Large Kernels Better Teacheres than Transformers for ConvNets
  • dial unix /var/run/docker.sock: connect: permission denied
  • 好用!推荐一个开源加密库 Bouncy Castle
  • Java I/O终极指南:BIO, NIO, AIO深度剖析
  • ethtool
  • 携手阿里云CEN:共创SD-WAN融合广域网
  • 【图像匹配】基于‌墨西哥帽小波(Marr小波)算法的图像匹配,matlab实现
  • signalR和WebSocket的区别是什么
  • 2024华为杯研究生数学建模C题【数据驱动下磁性元件的磁芯损耗建模】思路详解
  • Oracle脚本:排查占用UNDO段的SQL
  • 【Unity】对象池 - 未更新完
  • leetcode:2124. 检查是否所有 A 都在 B 之前(python3解法)
  • XXL-JOB 漏洞大全
  • 计算机毕业设计 美发管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 逻辑运算符
  • 第T10周:数据增强
  • 使用 Internet 共享 (ICS) 方式分配ip
  • 从虚拟机安装CentOS到自定义Dockerfile构建tomcat镜像
  • 【云安全】云服务安全攻防
  • 2024华为杯研赛C题原创python代码+结果表(1-2问)|全部可运行|精美可视化
  • openstack中的rabbitmq
  • 清理C盘缓存,电脑缓存清理怎么一键删除,操作简单的教程
  • E2VPT: An Effective and Efficient Approach for Visual Prompt Tuning
  • 系统架构设计师教程 第5章 5.1 软件工程 笔记
  • 面向过程和面向对象思想
  • Linux:开源世界的璀璨明珠
  • 深度学习-13-小语言模型之SmolLM的使用
  • 【Linux 从基础到进阶】OpenStack 私有云平台搭建