【音视频】视频编码-H264(三)

这篇博客详细介绍了如何利用ffmpeg的编码器将YUV420像素数据编码成H264裸数据。主要流程包括编码器初始化、开启编码线程、数据源输入以及停止编码。编码线程中使用了循环内存buffer,确保高效编码,最终生成的H264码流可以通过stream_eye等工具进行播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇主要介绍视频编码,将转码之后的YUV420像素数据编码为H264裸数据。

技术简介

使用ffmpeg的编码器

使用模块(库)

使用ffmpeg的avcodec-58.dll

主要流程和代码

1、初始化编码器

int H264Encoder::init(int width, int height, int framerate, int bitrate, int qb, int gopTime)
{
   
	int err = ERROR_CODE_OK;

	if (m_inited) {
   
		return err;
	}

	AVDictionary* options = nullptr;
	av_dict_set(&options, "preset", "superfast", 0);
	av_dict_set(&options, "tune", "zerolatency", 0);

	do {
   
		m_ringBuffer = new HELPER::RingBuffer<AVFrame>();
		if (m_ringBuffer == nullptr) {
   
			err = ERROR_CODE_ALLOC_FAILED;
			break;
		}
		if (!m_ringBuffer->init(H264_ENCODER_RINGBUFFER_SIZE)) {
   
			err = ERROR_CODE_ALLOC_FAILED;
			break;
		}

		m_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
		if (m_codec == nullptr) {
   
			err = ERROR_CODE_FFMPEG_FIND_ENCODER_FAILED;
			break;
		}

		m_encodeContext = avcodec_alloc_context3(m_codec);
		if (m_encodeContext == nullptr) {
   
			err = ERROR_CODE_FFMPEG_ALLOC_CONTEXT_FAILED;
			break;
		}

		m_encodeContext->codec_type = AVMEDIA_TYPE_VIDEO;
		m_encodeContext->codec_id = AV_CODEC_ID_H264;
		m_encodeContext->pix_fmt = AV_PIX_FMT_YUV420P;
		m_encodeContext->width = width;
		m_encodeContext->height = height;
		m_encodeContext->time_base = {
    1, framerate };
		m_encodeContext->framerate = {
    framerate, 1 };
		m_encodeContext->bit_rate = bitrate;

		m_encodeContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
		m_encodeContext->max_b_frames = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值