FFmpeg源码:av_log2函数分析

一、av_log2函数的声明

av_log2函数声明在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的头文件libavutil/common.h中:

#ifndef av_log2
av_const int av_log2(unsigned v);
#endif

该函数作用是:求形参v是2的多少次幂,把结果作为返回值返回。比如av_log2(1)为0,
av_log2(2)为1,av_log2(4)为2,av_log2(8)为3:

二、av_log2函数的定义

av_log2函数定义在源文件libavutil/intmath.c中:

int av_log2(unsigned v)
{
    return ff_log2(v);
}

三、编写例子测试av_log2函数

编写测试例子main.c,在Ubuntu上通过9.4.0版本的gcc可以成功编译:

#include <stdio.h>
#include <stdint.h>


#ifdef __GNUC__
#    define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
#    define AV_GCC_VERSION_AT_MOST(x,y)  (__GNUC__ < (x) || __GNUC__ == (x) && __GNUC_MINOR__ <= (y))
#else
#    define AV_GCC_VERSION_AT_LEAST(x,y) 0
#    define AV_GCC_VERSION_AT_MOST(x,y)  0
#endif


#ifndef av_always_inline
#if AV_GCC_VERSION_AT_LEAST(3,1)
#    define av_always_inline __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
#    define av_always_inline __forceinline
#else
#    define av_always_inline inline
#endif
#endif


#if AV_GCC_VERSION_AT_LEAST(2,6) || defined(__clang__)
#    define av_const __attribute__((const))
#else
#    define av_const
#endif


extern const uint8_t ff_log2_tab[256];

const uint8_t ff_log2_tab[256]={
        0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
        5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
};


#ifndef ff_log2
#define ff_log2 ff_log2_c
static av_always_inline av_const int ff_log2_c(unsigned int v)
{
    int n = 0;
    if (v & 0xffff0000) {
        v >>= 16;
        n += 16;
    }
    if (v & 0xff00) {
        v >>= 8;
        n += 8;
    }
    n += ff_log2_tab[v];

    return n;
}
#endif

int av_log2(unsigned v)
{
    return ff_log2(v);
}


int main()
{
    printf("av_log2(1): %d\n", av_log2(1));
    printf("av_log2(2): %d\n", av_log2(2));
    printf("av_log2(4): %d\n", av_log2(4));
    printf("av_log2(8): %d\n", av_log2(8));
    return 0;
}

使用gcc编译,运行,输出如下:

四、参考

log2是什么意思

av_dump_format函数FFmpeg中的一个非常有用的函数,可以用来打印音视频文件的信息,比如文件格式、时长、编码器等等。 该函数的定义如下: ```c void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output); ``` 其中,参数ic是一个AVFormatContext指针,表示音视频文件的上下文,它包含了音视频文件的所有信息;参数index表示要打印的流的索引,如果index为负数,则表示打印所有流的信息;参数url是一个字符串,表示音视频文件的文件名;参数is_output表示该文件是输入文件还是输出文件,如果是输入文件,则is_output为0,否则为1。 使用av_dump_format函数非常简单,只需要在打开音视频文件后调用该函数即可,例如: ```c AVFormatContext *ic = avformat_alloc_context(); if (avformat_open_input(&ic, filename, NULL, NULL) < 0) { printf("Failed to open file '%s'\n", filename); return -1; } if (avformat_find_stream_info(ic, NULL) < 0) { printf("Failed to retrieve input stream information\n"); return -1; } av_dump_format(ic, 0, filename, 0); ``` 这个例子中,我们首先使用avformat_alloc_context函数创建了一个AVFormatContext对象,然后通过avformat_open_input函数打开了音视频文件,再通过avformat_find_stream_info函数获取音视频文件的流信息,最后调用av_dump_format函数打印出文件的信息。 注意,av_dump_format函数会将信息打印到标准输出流中,如果需要将信息保存到文件中,可以重定向标准输出流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值