static inline int increase_pixel_weight()

in Transform_V1/vf_transform_v1.c [529:564]


static inline int increase_pixel_weight(TransformPixelWeights *ws, uint32_t id) {
    if (ws->n == 0) {
        ws->pairs = av_malloc_array(INITIAL_PAIR_SIZE, sizeof(*ws->pairs));
        if (!ws->pairs) {
            return AVERROR(ENOMEM);
        }
        *ws->pairs = PACK_PAIR(id, 1);
        ++ws->n;
        return 0;
    }

    // Looking for existing id
    for (int i = 0; i < ws->n; ++i) {
        if (UNPACK_ID(ws->pairs[i]) == id) {
            ++ws->pairs[i]; // since weight is packed in the lower bits, it works
            return 0;
        }
    }

    // if n is a power of 2, then we need to grow the array
    // grow array by power of 2, copy elements over
    if ((ws->n >= INITIAL_PAIR_SIZE) && !(ws->n & (ws->n - 1))) {
        uint32_t *new_pairs = av_malloc_array(ws->n * 2, sizeof(*ws->pairs));
        if (!new_pairs) {
            return AVERROR(ENOMEM);
        }
        memcpy(new_pairs, ws->pairs, sizeof(*ws->pairs) * ws->n);
        av_freep(&ws->pairs);
        ws->pairs = new_pairs;
    }

    ws->pairs[ws->n] = PACK_PAIR(id, 1);
    ++ws->n;

    return 0;
}