音视频入门基础:RTP专题(19)——FFmpeg源码中,获取RTP的音频信息的实现(下)

发布于:2025-03-17 ⋅ 阅读:(13) ⋅ 点赞:(0)

本文接着《音视频入门基础:RTP专题(18)——FFmpeg源码中,获取RTP的音频信息的实现(上)》,继续讲解FFmpeg获取SDP描述的RTP流的音频信息到底是从哪个地方获取的。本文的一级标题从“四”开始。

四、音频采样率

SDP协议中,a=rtpmap属性和a=fmtp属性中的config参数都会包含音频采样率信息。FFmpeg源码中首先会判断config参数中是否存在音频信息,如果存在,那就会从config参数(config是《ISO/IEC 14496-3》中定义的音频对象类型特定解码器配置数据 AudioSpecificConfig)中获取音频采样率;如果不存在,则会从a=rtpmap属性中获取音频采样率:

由《音视频入门基础:AAC专题(11)——AudioSpecificConfig简介》可以知道,AudioSpecificConfig中存在一个占4位的samplingFrequencyIndex属性,表示音频的采样频率:

FFmpeg源码的aac_decode_init函数中,会判断avctx->extradata_size是否大于0(avctx->extradata_size为SDP协议中config参数携带的内容的长度),大于0才表示config参数携带信息,才会执行decode_audio_specific_config函数:

static av_cold int aac_decode_init(AVCodecContext *avctx)
{
//...
    if (avctx->extradata_size > 0) {
        if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
                                                avctx->extradata,
                                                avctx->extradata_size * 8LL,
                                                1)) < 0)
            return ret;
    } 
//...
}

decode_audio_specific_config函数内部会调用decode_audio_specific_config_gb函数:

static int decode_audio_specific_config(AACDecContext *ac,
                                        AVCodecContext *avctx,
                                        MPEG4AudioConfig *m4ac,
                                        const uint8_t *data, int64_t bit_size,
                                        int sync_extension)
{
//...
    return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,
                                           sync_extension);
}

由《音视频入门基础:AAC专题(12)——FFmpeg源码中,解码AudioSpecificConfig的实现》可以知道,

ff_mpeg4audio_get_config_gb函数中,通过语句:c->sample_rate = get_sample_rate(gb, &c->sampling_index)获取AudioSpecificConfig的samplingFrequencyIndex属性。执行decode_audio_specific_config_gb函数后,m4ac指向的变量会得到从AudioSpecificConfig中解码出来的属性:

static inline int get_sample_rate(GetBitContext *gb, int *index)
{
    *index = get_bits(gb, 4);
    return *index == 0x0f ? get_bits(gb, 24) :
        ff_mpeg4audio_sample_rates[*index];
}

然后在decode_audio_specific_config_gb函数外部,通过aac_decode_frame_int函数将上一步得到的samplingFrequencyIndex属性赋值给AVCodecContext的sample_rate:

static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame,
                                int *got_frame_ptr, GetBitContext *gb,
                                const AVPacket *avpkt)
{
//...
    if (ac->oc[1].status && audio_found) {
        avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
        avctx->frame_size = samples;
        ac->oc[1].status = OC_LOCKED;
    }
//...
}

然后在dump_stream_format函数中,通过avcodec_string函数中的语句:av_bprintf(&bprint, "%d Hz, ", enc->sample_rate)拿到上一步中得到的AVCodecContext的sample_rate。最后再在dump_stream_format函数中将profile打印出来:

void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
//...
    switch (enc->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        av_bprintf(&bprint, "%s", separator);
 
        if (enc->sample_rate) {
            av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
        }
//...
    }
//...
}

五、音频声道数

 FFmpeg获取SDP描述的RTP流的音频声道数,是从SDP的a=rtpmap属性获取的。比如SDP中某一行的内容为:

a=rtpmap:97 MPEG4-GENERIC/48000/2

该例子中,该行的“48000”后面的那个“2”就是音频声道数,表示是双声道(立体声)。

当识别到上述“a=rtpmap”这个<type>后,sdp_parse_line函数中会调用sdp_parse_rtpmap函数:

else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
            /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
            get_word(buf1, sizeof(buf1), &p);
            payload_type = atoi(buf1);
            rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
            if (rtsp_st->stream_index >= 0) {
                st = s->streams[rtsp_st->stream_index];
                sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
            }
            s1->seen_rtpmap = 1;
            if (s1->seen_fmtp) {
                parse_fmtp(s, rt, payload_type, s1->delayed_fmtp);
            }
        } 

sdp_parse_rtpmap函数中会把a=rtpmap属性中的音频通道数提取出来,并通过语句:av_channel_layout_default(&par->ch_layout, i)把音频声道数赋值给par->ch_layout。par->ch_layout为指向一个AVCodecParameters类型变量的指针:

/* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
static int sdp_parse_rtpmap(AVFormatContext *s,
                            AVStream *st, RTSPStream *rtsp_st,
                            int payload_type, const char *p)
{
//...
    switch (par->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
    //...
        par->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
        if (i > 0) {
            par->sample_rate = i;
            avpriv_set_pts_info(st, 32, 1, par->sample_rate);
            get_word_sep(buf, sizeof(buf), "/", &p);
            i = atoi(buf);
            if (i > 0)
                av_channel_layout_default(&par->ch_layout, i);
        }
    }
//...
}

然后在sdp_parse_rtpmap函数外部,通过avcodec_parameters_to_context函数将AVCodecParameters的ch_layout赋值给AVCodecContext的ch_layout:

int avcodec_parameters_to_context(AVCodecContext *codec,
                                  const AVCodecParameters *par)
{
//...
    switch (par->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
        //....
        break;
    }
//...
}

然后在dump_stream_format函数中,通过avcodec_string函数中的语句:av_channel_layout_describe_bprint(&enc->ch_layout, &bprint)拿到AVCodecContext的ch_layout对应的音频声道数目。最后再在dump_stream_format函数中将音频声道数目打印出来:

void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
//...
    switch (enc->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
        //...
        break;
    }
//...
}

所以FFmpeg获取SDP描述的RTP流的音频声道数,是从SDP的a=rtpmap属性获取的:

六、Bit depth

如果SDP描述的RTP流的音频压缩编码格式为AAC,FFmpeg会强制把Bit depth设置为fltp。这是因为对于有损压缩编解码器(如MP3和AAC),Bit depth是在编码期间计算的,并且可以因采样而异,Bit depth只对PCM数字信号有意义。具体可以参考:《音视频入门基础:AAC专题(3)——AAC的ADTS格式简介》。

可以看到在aac_decode_init函数中(该函数定义在libavcodec/aacdec_template.c),强制把音频采样格式设置成了AV_SAMPLE_FMT_FLTP:

static av_cold int aac_decode_init(AVCodecContext *avctx)
{
//...
    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
//...
}

所以如果SDP描述的RTP流的音频压缩编码格式为AAC,通过“ffmpeg -protocol_whitelist "file,rtp,udp" -i XXX.sdp命令”获取到的音频采样格式固定为fltp,该值没有意义: