首页 > 其他分享 >[ffmpeg] 音视频编码

[ffmpeg] 音视频编码

时间:2024-09-20 21:24:38浏览次数:3  
标签:编码 const ffmpeg int avcodec 音视频 codec AVCodecContext void

本文主要梳理 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);

标签:编码,const,ffmpeg,int,avcodec,音视频,codec,AVCodecContext,void
From: https://blog.csdn.net/dss875914213/article/details/142314300

相关文章

  • 大牛直播SDK核心音视频模块探究
    技术背景视沃科技旗下”大牛直播SDK”,始于2015年,致力于传统行业极致体验的音视频直播技术解决方案,产品涵盖跨平台的实时RTMP推流、RTMP/RTSP直播播放(支持RTSP|RTMPH.265,EnhancedRTMPH.265)、GB28181设备接入、推送端播放端实时录像、多路流媒体转发(RTSP转RTMP,RTMP转RTMP,RTSP|R......
  • KTH7823——16 位高精度低延时霍尔磁编码器可编程 ABZ 和 PWM 输出模式角度传感器
    KTH7823是一款高精度绝对角度霍尔传感器芯片,最高16位分辨率绝对角度输出,可实现在轴向和离轴场合下的无接触式磁场角度测量。不论转速范围在0-120000rpm之间,KTH7823都能快速准确地输出角度信息,适用于需要精准角度测量和转速控制的各个领域。KTH7823......
  • 【深度学习】Transformer掌握文本嵌入层和位置编码的实现过程,解码器中各个组成部分的
    1输入部分介绍输入部分包含:源文本嵌入层及其位置编码器目标文本嵌入层及其位置编码器 2文本嵌入层的作用 无论是源文本嵌入还是目标文本嵌入,都是为了将文本中词汇的数字表示转变为向量表示,希望在这样的高维空间捕捉词汇间的关系.文本嵌入层的代码分析:#导入必......
  • 同三维T80001EH8 8路高清HDMI编码器:高清HDMI编码器
    同三维T80001EH88路高清HDMI编码器8路HDMI输入+8路3.5音频输入,4路4K30+4路高清1080P60,两个网口,带两个小液晶屏【产品型号】T80001EH8同三维T80001EH88路高清HDMI编码器【系列介绍】8路HDMI高清编码器是专业的高清音视频编码产品,只需要占用较小的带宽,即可获得高清晰......
  • 同三维T80001EH4 4路高清HDMI视频编码器:高清HDMI编码器
    同三维T80001EH44路高清HDMI视频编码器4路HDMI输入+4路3.5音频输入,支持2路4K30和2路高清1080P60,带小液晶屏,POE供电可选【产品型号】T80001EH4同三维T80001EH44路高清HDMI视频编码器【系列介绍】4路HDMI高清编码器是专业的高清音视频编码产品,只需要占用较小的带宽,......
  • mysql查询字段排序规则、数据库编码、表编码,修改排序规则
    查询字段排序规则、数据库编码、表编码SELECTTABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAMEFROMINFORMATION_SCHEMA.COLUMNS 表字段修复#改变字段数据字符集、排序规则SELECTTABLE_SCHEMA'数据库',TABLE_NAME'表',CO......
  • 字符编码发展史2 — ISO-8859-N
    2.2.第二个阶段本地化2.2.1.ANSI2.2.2.ISO/IEC8859-N2.2.2.1.什么是ISO/IEC8859-N?2.2.2.2.ISO8859-1的编码表上一篇《字符编码发展史1—ASCII和EASCII》我们讲解了字符编码的起源ASCII和EASCII。本篇我们将继续讲解字符编码的第二个发展阶段中的ISO8859......
  • 机器翻译之创建Seq2Seq的编码器、解码器
    1.创建编码器、解码器的基类1.1创建编码器的基类fromtorchimportnn#构建编码器的基类classEncoder(nn.Module):#继承父类nn.Moduledef__init__(self,**kwargs):#**kwargs:不定常的关键字参数super().__init__(**kwargs)defforwa......
  • ChatGPT 多媒体应用设计师备考考点讲解(七):音视频编码与传输优化策略
    音视频编码与传输技术是多媒体应用设计中至关重要的环节。高效的音视频编码不仅能够保证内容质量,还可以减少带宽占用,提升传输效率。而在传输过程中,优化音视频流的传输策略可以降低延迟、减少丢包、保证播放的流畅性。在本篇文章中,我们将详细讲解音视频编码的基础知识、常见编码格式......
  • 2024Mysql And Redis基础与进阶操作系列(2)作者——LJS[含MySQL登录;DDL;DML;举例说明;编码
    目录1.MySQL的登录1.1服务的启动和停止方式1:使用图形界面工具步骤1:打开windows服务 步骤2:找到MySQL80(点击鼠标右键)→启动或停止(点击)编辑补充说明2点:1.2自带客户端的登录与退出登录方式1:MySQL自带客户端注意:退出登录2MySQL数据库基本操作-DDL和DML2.1.DDL解释2.......