FFmpeg ff_h264_parse_sprop_parameter_sets函数剖析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FFmpeg ff_h264_parse_sprop_parameter_sets函数剖析相关的知识,希望对你有一定的参考价值。

SDP中SPS内容
        sprop-parameter-sets=Z0IAKpY1QPAET8s3AQEBQAABwgAAV+QB,aM48gA==

该函数主要是读取Z0IAKpY1QPAET8s3AQEBQAABwgAAV+QB的内容到data_ptr,一共32个字节
还没有读取SPS的每一个字段,然后赋值给SPS相应的字段
int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
                                       uint8_t **data_ptr, int *size_ptr,
                                       const char *value)
{
    char base64packet[1024];
    uint8_t decoded_packet[1024];
    int packet_size;

    while (*value) {
//通过dst保存数据的头指针
        char *dst = base64packet;

//将value字符串拷贝一份到dst指针
        while (*value && *value != ','
               && (dst - base64packet) < sizeof(base64packet) - 1) {
            *dst++ = *value++;
        }
        *dst++ = '\0';
//base64packet=Z0IAKpY1QPAET8s3AQEBQAABwgAAV+QB
        if (*value == ',')
            value++;
//base64解码结果,保存在decoded_packet
        packet_size = av_base64_decode(decoded_packet, base64packet,
                                       sizeof(decoded_packet));
        if (packet_size > 0) {
//这里进行了内存大小进行调整
            uint8_t *dest = av_realloc(*data_ptr,
                                       packet_size + sizeof(start_sequence) +
                                       *size_ptr +
                                      AV_INPUT_BUFFER_PADDING_SIZE);
//static const uint8_t start_sequence[] = { 'B', 'B', 'C', 'D' };不是很明白
            if (!dest) {
                av_log(s, AV_LOG_ERROR,
                       "Unable to allocate memory for extradata!\n");
                return AVERROR(ENOMEM);
            }
            *data_ptr = dest;

            memcpy(dest + *size_ptr, start_sequence,
                   sizeof(start_sequence));
            memcpy(dest + *size_ptr + sizeof(start_sequence),
                   decoded_packet, packet_size);
            memset(dest + *size_ptr + sizeof(start_sequence) +
                   packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);

            *size_ptr += sizeof(start_sequence) + packet_size;
        }
    }

    return 0;
}

static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
                                      AVStream *stream,
                                      PayloadContext *h264_data,
                                      const char *attr, const char *value)
{
    AVCodecParameters *par = stream->codecpar;

    if (!strcmp(attr, "packetization-mode")) {
        av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
        h264_data->packetization_mode = atoi(value);
        /*
         * Packetization Mode:
         * 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
         * 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
         * 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A),
         *                      and 29 (FU-B) are allowed.
         */
        if (h264_data->packetization_mode > 1)
            av_log(s, AV_LOG_ERROR,
                   "Interleaved RTP mode is not supported yet.\n");
    } else if (!strcmp(attr, "profile-level-id")) {
        if (strlen(value) == 6)
            parse_profile_level_id(s, h264_data, value);
    } else if (!strcmp(attr, "sprop-parameter-sets")) {
        int ret;
        if (*value == 0 || value[strlen(value) - 1] == ',') {
            av_log(s, AV_LOG_WARNING, "Missing PPS in sprop-parameter-sets, ignoring\n");
            return 0;
        }
        par->extradata_size = 0;
        av_freep(&par->extradata);
//这一段代码主要是将value的值拷贝到par->extradata字符串中
        ret = ff_h264_parse_sprop_parameter_sets(s, &par->extradata,
                                                 &par->extradata_size, value);
        av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
               par->extradata, par->extradata_size);
        return ret;
    }
    return 0;
}

疑问
,aM48gA==这一段数据,并没有使用,是否意味着这只是读取了SPS,而没有读取到PPS,导致出现
non-existing PPS 0 referenced

函数的调用逻辑
sdp_read_header
  ----ff_sdp_parse
    ----sdp_parse_line
      -----parse_fmtp
        ----parse_sdp_a_line------>parse_h264_sdp_line
                        --------sdp_parse_fmtp_config_h264

参考
https://www.cnblogs.com/lihaiping/p/6612511.html

引用海康SDP信息
v=0
o=- 1515162567455825 1515162567455825 IN IP4 192.168.0.150
s=Media Presentation
e=NONE
b=AS:5050
t=0 0
a=control:rtsp://192.168.0.150:554/h264/ch1/main/av_stream/
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
b=AS:5000
a=recvonly
a=x-dimensions:1920,1080
a=control:rtsp://192.168.0.150:554/h264/ch1/main/av_stream/trackID=1
a=rtpmap:96 H264/90000
a=fmtp:96 profile-level-id=420029; packetization-mode=1; sprop-parameter-sets=Z0IAKpY1QPAET8s3AQEBQAABwgAAV+QB,aM48gA==
a=Media_header:MEDIAINFO=494D4B48010200000400000100000000000000000000000000000000000000000000000000000000;
a=appversion:1.0
SETUP rtsp://192.168.0.150:554/h264/ch1/main/av_stream/trackID=1 RTSP/1.0
CSeq: 5
Authorization: Digest username="admin", realm="IP Camera(77056)", nonce="a3a53f4dcb9e6ceca90de6d90c884d29", uri="rtsp://192.168.0.150:554/h264/ch1/main/av_stream/", response="32b6afa222b54c2518efff16ec1aa766"
User-Agent: LibVLC/2.2.4 (LIVE555 Streaming Media v2016.02.22)
Transport: RTP/AVP/TCP;unicast;interleaved=0-1


以上是关于FFmpeg ff_h264_parse_sprop_parameter_sets函数剖析的主要内容,如果未能解决你的问题,请参考以下文章

FFmpeg 中不同的 h264 编码器

ffmpeg使用硬件加速hwaccelcuvidh264_cuvidh264_nvenc

ffmpeg使用硬件加速hwaccelcuvidh264_cuvidh264_nvenc

ffmpeg使用硬件加速hwaccelcuvidh264_cuvidh264_nvenc

ffmpeg使用硬件加速hwaccelcuvidh264_cuvidh264_nvenc

ffmpeg - 如何为 h264_nvenc 编码器配置参考参数