av_opt_set_int_list 学习笔记

来源:互联网 发布:云狐网络 编辑:程序博客网 时间:2024/05/29 08:33

/**
 * Set a binary option to an integer list.
 *
 * @param obj    AVClass object to set options on
 * @param name   name of the binary option
 * @param val    pointer to an integer list (must have the correct type with
 *               regard to the contents of the list)
 * @param term   list terminator (usually 0 or -1)
 * @param flags  search flags
 */

#define av_opt_set_int_list(obj, name, val, term, flags) \
    (av_int_list_length(val, term) > INT_MAX / sizeof(*(val)) ? \
     AVERROR(EINVAL) : \
     av_opt_set_bin(obj, name, (const uint8_t *)(val), \
                    av_int_list_length(val, term) * sizeof(*(val)), flags))


说明:

av_int_list_length值是否正常,正常则调用av_opt_set_bin。

----------------------------------------------------------------------------------------------------


/**
 * Compute the length of an integer list.
 *
 * @param term  list terminator (usually 0 or -1)
 * @param list  pointer to the list
 * @return  length of the list, in elements, not counting the terminator
 */
#define av_int_list_length(list, term) \
    av_int_list_length_for_size(sizeof(*(list)), list, term)


unsigned av_int_list_length_for_size(unsigned elsize,
                                     const void *list, uint64_t term)
{
    unsigned i;


    if (!list)
        return 0;
#define LIST_LENGTH(type) \
    { type t = term, *l = (type *)list; for (i = 0; l[i] != t; i++); }
    switch (elsize) {
    case 1: LIST_LENGTH(uint8_t);  break;
    case 2: LIST_LENGTH(uint16_t); break;
    case 4: LIST_LENGTH(uint32_t); break;
    case 8: LIST_LENGTH(uint64_t); break;
    default: av_assert0(!"valid element size");
    }
    return i;
}

说明:

LIST_LENGTH用法很巧妙。写一个宏,相当于写4个函数。

----------------------------------------------------------------------------------------------------


int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
{
    void *target_obj;
    const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
    uint8_t *ptr;
    uint8_t **dst;
    int *lendst;


    if (!o || !target_obj)
        return AVERROR_OPTION_NOT_FOUND;


    if (o->type != AV_OPT_TYPE_BINARY)
        return AVERROR(EINVAL);


    ptr = len ? av_malloc(len) : NULL;
    if (len && !ptr)
        return AVERROR(ENOMEM);


    dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
    lendst = (int *)(dst + 1);


    av_free(*dst);
    *dst = ptr;
    *lendst = len;
    if (len)
        memcpy(ptr, val, len);


    return 0;
}

说明:

调用av_opt_find2函数,得到要操作的对象指针target_obj以及AVOption指针。

将原指针指向整数区free,指向新分配的空间,新分配的空间的值为要设置的整数数组值。

dst      --->   [指针地址]  --> 整数数组

lendst -->    [数据大小]

dst指向的指针地址存放着整数数组。

这里注意:uint8_t **demo; demo+1实际地址+4;uint8_t *demo; demo+1实际地址+1;

----------------------------------------------------------------------------------------------------

关于av_opt_find2函数,看opt.c学习笔记






0 0
原创粉丝点击