音视频入门基础:H.264专题(19)——FFmpeg源码中,获取avcC封装的H.264码流中每个NALU的长度的实现

一、引言

从《音视频入门基础:H.264专题(18)------AVCDecoderConfigurationRecord简介》中可以知道,avcC跟AnnexB不一样,avcC包装的H.264码流中,每个NALU前面没有起始码。avcC通过在每个NALU前加上NALUnitLength,在读取某个NALU之前,先把该NALU前面的NALUnitLength读取出来,拿到该NALU的总长度,然后根据该长度读取相应的字节数,读取出整个NALU。

而FFmpeg源码内部使用get_nalsize函数读取出某个NALU前面的NALUnitLength。

二、get_nalsize函数的定义

get_nalsize函数定义在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的头文件libavcodec/h2645_parse.h中:

cpp 复制代码
static inline int get_nalsize(int nal_length_size, const uint8_t *buf,
                              int buf_size, int *buf_index, void *logctx)
{
    int i, nalsize = 0;

    if (*buf_index >= buf_size - nal_length_size) {
        // the end of the buffer is reached, refill it
        return AVERROR_INVALIDDATA;
    }

    for (i = 0; i < nal_length_size; i++)
        nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++];
    if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
        av_log(logctx, AV_LOG_ERROR,
               "Invalid NAL unit size (%d > %d).\n", nalsize, buf_size - *buf_index);
        return AVERROR_INVALIDDATA;
    }
    return nalsize;
}

该函数作用是读取avcC封装的H.264码流中某个NALU前面的NALUnitLength,或者读取AVCDecoderConfigurationRecord中某个SPS前面的sequenceParameterSetLength,或者读取AVCDecoderConfigurationRecord中某个PPS前面的pictureParameterSetLength。由于SPS和PPS都属于一种特殊的NALU,所以sequenceParameterSetLength和pictureParameterSetLength也算是一种NALUnitLength。只是存贮每个sequenceParameterSetLength和pictureParameterSetLength需要固定2字节空间,而存贮每个NALUnitLength所需的空间由AVCDecoderConfigurationRecord中的lengthSizeMinusOne属性决定而已。

形参nal_length_size:输入型参数。如果是要读取某个NALU前面的NALUnitLength,形参nal_length_size的值为"存贮该NALU对应NALUnitLength所需的以字节为单位的空间";如果是要读取sequenceParameterSetLength或pictureParameterSetLength,形参nal_length_size的值固定为2。

形参buf:输入型参数,指向某个缓冲区。

1.如果是要读取某个NALU前面的NALUnitLength,该缓冲区存放该NALU对应的NALUnitLength + NALU Header + EBSP;

2.如果是要读取某个SPS前面的sequenceParameterSetLength,该缓冲区存放该SPS对应的sequenceParameterSetLength + NALU Header + 该SPS实际的NALU数据(该SPS的EBSP);

3.如果是要读取某个PPS前面的pictureParameterSetLength,该缓冲区存放该PPS对应的pictureParameterSetLength + NALU Header + 该PPS实际的NALU数据(该PPS的EBSP);

形参buf_size:形参buf指向的缓冲区的长度,单位为字节。

形参buf_index:既是输入型参数也是输出型参数。表示读取到形参buf指向的缓冲区的第几个字节了。

形参logctx:输入型参数。用来输出日志,可忽略。

返回值:执行成功返回读取到的NALUnitLength或sequenceParameterSetLength或pictureParameterSetLength,失败返回一个负数。

三、get_nalsize函数的内部实现分析

get_nalsize函数中,首先判断是否读到了形参buf指向的缓冲区的末尾。如果已经读到了末尾,返回AVERROR_INVALIDDATA:

cpp 复制代码
    if (*buf_index >= buf_size - nal_length_size) {
        // the end of the buffer is reached, refill it
        return AVERROR_INVALIDDATA;
    }

如果还没读到末尾,继续执行。通过下面语句,读取NALUnitLength或sequenceParameterSetLength或pictureParameterSetLength,存贮到局部变量nalsize中:

cpp 复制代码
    for (i = 0; i < nal_length_size; i++)
        nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++];

如果读取到的该NALU的总长度小于0,或者读取到的该NALU的总长度大于形参buf指向的缓冲区中剩下的还未被读取的空间,打印日志"Invalid NAL unit size",并返回AVERROR_INVALIDDATA:

cpp 复制代码
    if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
        av_log(logctx, AV_LOG_ERROR,
               "Invalid NAL unit size (%d > %d).\n", nalsize, buf_size - *buf_index);
        return AVERROR_INVALIDDATA;
    }

读取成功返回NALUnitLength或sequenceParameterSetLength或pictureParameterSetLength的值:

cpp 复制代码
    return nalsize;
相关推荐
REDcker11 天前
WebCodecs VideoDecoder 的 hardwareAcceleration 使用
前端·音视频·实时音视频·直播·webcodecs·videodecoder
gihigo199811 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
山河君11 天前
四麦克风声源定位实战:基于 GCC-PHAT + 最小二乘法实现 DOA
算法·音视频·语音识别·信号处理·最小二乘法·tdoa
音视频牛哥11 天前
Android平台RTMP/RTSP超低延迟直播播放器开发详解——基于SmartMediaKit深度实践
android·人工智能·计算机视觉·音视频·rtmp播放器·安卓rtmp播放器·rtmp直播播放器
qq_4162764211 天前
通用音频表征的对比学习
学习·音视频
美狐美颜sdk11 天前
Android全局美颜sdk实现方案详解
人工智能·音视频·美颜sdk·视频美颜sdk·美狐美颜sdk
EasyDSS11 天前
私有化部署EasyDSS视频点播能力:全链路视频技术赋能,打造企业级视听新体验
音视频·hls·m3u8·点播技术·智能转码
qq_4162764211 天前
DeLoRes——一种通用的音频表征学习新方法(DeLoRes(基于 Barlow Twins 的冗余最小化方法)
学习·音视频
Q_45828386811 天前
从定位到视频:808 + 1078 在各行业的落地实践
音视频
山顶望月川11 天前
实测MiniMax-Hailuo-02:当“开工大吉“变成“无字天书“,国产AI视频模型的能与之不能
人工智能·音视频