简单分析H264
// 分析 NALU
// 一个AVPacket中包含多个NALU,以0001间隔,多个以001间隔
// 0001[NALU_HEAD]
// NALU_HEAD 占1字节, orbidden_bit(1bit), nal_reference_bit(2bit)优先级, nal_unit_type(5bit)
// type值
// 1: 非IDR图像中不采用数据划分的片段
// 5: IDR图像的片段
// 6: 补充增强信息(SEI)
// 7: 序列参数集 / SPS
// 8: 图像参数集 / PPS
int nal_unit_type = 0;
unsigned char nal_head = *(pkt->data + 4); // +4, 去掉开头的0001
nal_unit_type = nal_head & 0x1f; // 取后5位
qDebug() << "nal unit type: " << nal_unit_type;
for(int i = 4; i < pkt->size - 4; i++) {
// 找到 001
if(pkt->data[i] == 0 && pkt->data[i + 1] == 0 && pkt->data[i + 2] == 1) {
nal_unit_type = pkt->data[i + 3] & 0x1f;
qDebug() << "## nal unit type: " << nal_unit_type;
}
}
如果设置好了GOP,那么可以重新观察生成的视频流,完整代码如下:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <iostream>
#include <fstream>
#undef main
#include "SDL.h"
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
#include <libavutil/imgutils.h>
}
int main(int argc, char *argv[])
{
std::string filename = "400_300_25_preset";
AVCodecID codec_id = AV_CODEC_ID_H264;
// AVCodecID codec_id = AV_CODEC_ID_HEVC;
if(codec_id == AV_CODEC_ID_H264) {
filename += ".h264";
} else if(codec_id == AV_CODEC_ID_HEVC) {
filename += ".h265";
}
std::ofstream ofs;
ofs.open(filename, std::ios::binary);
// 1. 寻找编码器
auto codec = avcodec_find_encoder(codec_id);
if(!codec) {
qDebug() << "codec not find!";
return -1;
}
// 2. 编码上下文
auto c = avcodec_alloc_context3(codec);
if(!c) {
qDebug() << "avcodec alloc failed!";
return -1;
}
// 3. 设定上下文参数
c->width = 400;
c->height = 300;
// 帧时间戳的时间单位, pts *time_base = 播放时间(秒)
c->time_base = {1, 25}; // 分子,分母, 1s中的时间单元数;
c->pix_fmt = AV_PIX_FMT_YUV420P; // 元数据像素格式
c->thread_count = 16; // 编码线程数
// 图像组GOP, 一个GOP包含一个IDR, SPS, PPS
c->gop_size = 6;
// 4. 打开编码上下文
int re = avcodec_open2(c, codec, NULL);
if(re != 0) {
qDebug() << "avcodec open error!";
return -1;
}
// 创建AVFrame空间
auto frame = av_frame_alloc(); // 未压缩数据
frame->width = c->width;
frame->height = c->height;
frame->format = c->pix_fmt;
re = av_frame_get_buffer(frame, 0);
if(re != 0) {
qDebug() << "av_frame_get_buffer failed!";
return -1;
}
// 测试,10s视频
auto pkt = av_packet_alloc(); // 每一帧内部空间不确定
for(int i = 0; i < 250; i++) {
// 生成AVFrame数据, 每一帧数据不同
// Y
for(int y = 0; y < c->height; y++) {
for(int x = 0; x < c->width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
}
}
// U, V
for(int y = 0; y < c->height / 2; y++) {
for(int x = 0; x < c->width / 2; x++) {
frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
}
}
frame->pts = i;
// 发送未压缩帧到线程中压缩
re = avcodec_send_frame(c, frame);
if(re != 0) {
break;
}
// 表示有数据返回
while(re >= 0) {
// 接收压缩帧,一般前几次调用返回空(缓存,立刻返回),编码会重新创建线程
// 每次调用,都会重新分配pkt中的空间
re = avcodec_receive_packet(c, pkt);
if(re == AVERROR(EAGAIN) || re == AVERROR_EOF) {
break;
}
if(re < 0)
break;
// 分析 NALU
// 一个AVPacket中包含多个NALU,以0001间隔,多个以001间隔
// 0001[NALU_HEAD]
// NALU_HEAD 占1字节, orbidden_bit(1bit), nal_reference_bit(2bit)优先级, nal_unit_type(5bit)
// type值
// 1: 非IDR图像中不采用数据划分的片段
// 5: IDR图像的片段
// 6: 补充增强信息(SEI)
// 7: 序列参数集 / SPS
// 8: 图像参数集 / PPS
int nal_unit_type = 0;
unsigned char nal_head = *(pkt->data + 4); // +4, 去掉开头的0001
nal_unit_type = nal_head & 0x1f; // 取后5位
qDebug() << "nal unit type: " << nal_unit_type;
for(int i = 4; i < pkt->size - 4; i++) {
// 找到 001
if(pkt->data[i] == 0 && pkt->data[i + 1] == 0 && pkt->data[i + 2] == 1) {
nal_unit_type = pkt->data[i + 3] & 0x1f;
qDebug() << "## nal unit type: " << nal_unit_type;
}
}
ofs.write((char*)pkt->data, pkt->size); // 写入测试文件
av_packet_unref(pkt);
}
}
ofs.close();
av_packet_free(&pkt);
av_frame_free(&frame);
avcodec_free_context(&c); // 释放编码器上下文
return 0;
}