一、引言

在上一节《音视频入门基础:H.264专题(14)——计算视频帧率的公式》中,讲述了通过SPS中的属性计算H.264编码的视频的帧率的公式。本文讲解FFmpeg源码中计算视频帧率的实现。

二、FFmpeg源码中计算视频帧率的实现

从文章《音视频入门基础:H.264专题(10)——FFmpeg源码中,存放SPS属性的结构体和解码SPS的函数分析》中,我们可以知道,FFmpeg源码中通过ff_h264_decode_seq_parameter_set函数解码SPS,从而拿到SPS中的属性。

计算视频帧率所需的属性在SPS的VUI parameters(视频可用参数)中。ff_h264_decode_seq_parameter_set函数通过调用decode_vui_parameters函数解码VUI parameters:

int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
                                     H264ParamSets *ps, int ignore_truncation)
{
//...

    sps->vui_parameters_present_flag = get_bits1(gb);
    if (sps->vui_parameters_present_flag) {
        int ret = decode_vui_parameters(gb, avctx, sps);
        if (ret < 0)
            goto fail;
    }

//...
}

decode_vui_parameters函数中通过下面的这部分代码拿到计算视频帧率所需的属性(timing_info_present_flag、num_units_in_tick、time_scale):

static inline int decode_vui_parameters(GetBitContext *gb, void *logctx,
                                        SPS *sps)
{
//...

    sps->timing_info_present_flag = get_bits1(gb);
    if (sps->timing_info_present_flag) {
        unsigned num_units_in_tick = get_bits_long(gb, 32);
        unsigned time_scale        = get_bits_long(gb, 32);
        if (!num_units_in_tick || !time_scale) {
            av_log(logctx, AV_LOG_ERROR,
                   "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n",
                   time_scale, num_units_in_tick);
            sps->timing_info_present_flag = 0;
        } else {
            sps->num_units_in_tick = num_units_in_tick;
            sps->time_scale = time_scale;
        }
        sps->fixed_frame_rate_flag = get_bits1(gb);
    }

//...
}

然后在FFmpeg源码的源文件libavcodec/h264_parser.c的parse_nal_units函数中,通过如下代码,得到视频帧率:

static inline int parse_nal_units(AVCodecParserContext *s,
                                  AVCodecContext *avctx,
                                  const uint8_t * const buf, int buf_size)
{
    //...
    
    for (;;) {
        switch (nal.type) {
        case H264_NAL_SPS:
            ff_h264_decode_seq_parameter_set(&nal.gb, avctx, &p->ps, 0);
            break;
         
        //...
 
        case H264_NAL_IDR_SLICE:
        //...
        if (sps->timing_info_present_flag) {
            int64_t den = sps->time_scale;
            if (p->sei.unregistered.x264_build < 44U)
                den *= 2;
            av_reduce(&avctx->framerate.den, &avctx->framerate.num,
                          sps->num_units_in_tick * avctx->ticks_per_frame, den, 1 << 30);
            }
        //... 
        }
        //...
    }
}

可以看到在FFmpeg源码的parse_nal_units函数中,最终是通过语句

av_reduce(&avctx->framerate.den, &avctx->framerate.num,
                          sps->num_units_in_tick * avctx->ticks_per_frame, den, 1 << 30);

计算出视频帧率的。

上述函数av_reduce的实参avctx->ticks_per_frame是结构体AVCodecContext的成员变量,它会被设置为每帧的时基的时钟数。默认值为1,如果编解码器是H.264或MPEG-2,会被设置为2:

typedef struct AVCodecContext {

    /**
     * For some codecs, the time base is closer to the field rate than the frame rate.
     * Most notably, H.264 and MPEG-2 specify time_base as half of frame duration
     * if no telecine is used ...
     *
     * Set to time_base ticks per frame. Default 1, e.g., H.264/MPEG-2 set it to 2.
     */
    int ticks_per_frame;
}

用户需要获取H.264编码的视频的媒体信息时,会调用avformat_find_stream_info函数,而该函数内部会调用h264_decode_init函数,让avctx->ticks_per_frame被初始化为2(也就是说对于H.264,avctx->ticks_per_frame的值就是2):

static av_cold int h264_decode_init(AVCodecContext *avctx)
{
//...
    if (avctx->ticks_per_frame == 1) {
        if(h->avctx->time_base.den < INT_MAX/2) {
            h->avctx->time_base.den *= 2;
        } else
            h->avctx->time_base.num /= 2;
    }
    avctx->ticks_per_frame = 2;
//...
}

所以在parse_nal_units函数中,语句:

av_reduce(&avctx->framerate.den, &avctx->framerate.num,
                          sps->num_units_in_tick * avctx->ticks_per_frame, den, 1 << 30);

等价于:

av_reduce(&avctx->framerate.den, &avctx->framerate.num,
                          sps->num_units_in_tick * 2, den, 1 << 30);

而den的值为sps->time_scale。所以上述语句等价于:

av_reduce(&avctx->framerate.den, &avctx->framerate.num,
                          sps->num_units_in_tick * 2, sps->time_scale, 1 << 30);

av_reduce函数是用来计算视频帧率的,其源码定义在FFmpeg源码libavutil/rational.c中:

int av_reduce(int *dst_num, int *dst_den,
              int64_t num, int64_t den, int64_t max)
{
    AVRational a0 = { 0, 1 }, a1 = { 1, 0 };
    int sign = (num < 0) ^ (den < 0);
    int64_t gcd = av_gcd(FFABS(num), FFABS(den));

    if (gcd) {
        num = FFABS(num) / gcd;
        den = FFABS(den) / gcd;
    }
    if (num <= max && den <= max) {
        a1 = (AVRational) { num, den };
        den = 0;
    }

    while (den) {
        uint64_t x        = num / den;
        int64_t next_den  = num - den * x;
        int64_t a2n       = x * a1.num + a0.num;
        int64_t a2d       = x * a1.den + a0.den;

        if (a2n > max || a2d > max) {
            if (a1.num) x =          (max - a0.num) / a1.num;
            if (a1.den) x = FFMIN(x, (max - a0.den) / a1.den);

            if (den * (2 * x * a1.den + a0.den) > num * a1.den)
                a1 = (AVRational) { x * a1.num + a0.num, x * a1.den + a0.den };
            break;
        }

        a0  = a1;
        a1  = (AVRational) { a2n, a2d };
        num = den;
        den = next_den;
    }
    av_assert2(av_gcd(a1.num, a1.den) <= 1U);
    av_assert2(a1.num <= max && a1.den <= max);

    *dst_num = sign ? -a1.num : a1.num;
    *dst_den = a1.den;

    return den == 0;
}

所以语句:

av_reduce(&avctx->framerate.den, &avctx->framerate.num,
                          sps->num_units_in_tick * 2, sps->time_scale, 1 << 30);

相当于执行了公式:视频帧率 = time_scale / (2 * num_units_in_tick)。然后把得到的视频帧率的分子和分母分别存放到avctx->framerate.den和avctx->framerate.num中返回。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部