<commit_before>/*
 * Copyright 2017 GoPro Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <float.h>
#include <stddef.h>
#include <string.h>
#include "animation.h"
#include "log.h"
#include "math_utils.h"
#include "memory.h"
#include "nodegl.h"
#include "nodes.h"

#define OFFSET(x) offsetof(struct buffer_priv, x)
static const struct node_param animatedbuffer_params[] = {
    {"keyframes", PARAM_TYPE_NODELIST, OFFSET(animkf),
                  .node_types=(const int[]){NGL_NODE_ANIMKEYFRAMEBUFFER, -1},
                  .flags=PARAM_FLAG_DOT_DISPLAY_PACKED,
                  .desc=NGLI_DOCSTRING("key frame buffers to interpolate from")},
    {NULL}
};

static void mix_buffer(void *user_arg, void *dst,
                       const struct animkeyframe_priv *kf0,
                       const struct animkeyframe_priv *kf1,
                       double ratio)
{
    float *dstf = dst;
    const struct buffer_priv *s = user_arg;
    const float *d1 = (const float *)kf0->data;
    const float *d2 = (const float *)kf1->data;
    for (int k = 0; k < s->count; k++)
        for (int i = 0; i < s->data_comp; i++)
            dstf[k*s->data_comp + i] = NGLI_MIX(d1[k*s->data_comp + i], d2[k*s->data_comp + i], ratio);
}

static void cpy_buffer(void *user_arg, void *dst,
                       const struct animkeyframe_priv *kf)
{
    const struct buffer_priv *s = user_arg;
    memcpy(dst, kf->data, s->data_size);
}

static int animatedbuffer_update(struct ngl_node *node, double t)
{
    struct buffer_priv *s = node->priv_data;
    return ngli_animation_evaluate(&s->anim, s->data, t);
}

static int animatedbuffer_init(struct ngl_node *node)
{
    struct buffer_priv *s = node->priv_data;

    int nb_comp;
    int format;

    switch (node->class->id) {
    case NGL_NODE_ANIMATEDBUFFERFLOAT: nb_comp = 1; format = NGLI_FORMAT_R32_SFLOAT;          break;
    case NGL_NODE_ANIMATEDBUFFERVEC2:  nb_comp = 2; format = NGLI_FORMAT_R32G32_SFLOAT;       break;
    case NGL_NODE_ANIMATEDBUFFERVEC3:  nb_comp = 3; format = NGLI_FORMAT_R32G32B32_SFLOAT;    break;
    case NGL_NODE_ANIMATEDBUFFERVEC4:  nb_comp = 4; format = NGLI_FORMAT_R32G32B32A32_SFLOAT; break;
    default:
        ngli_assert(0);
    }

    s->dynamic = 1;
    s->usage = GL_DYNAMIC_DRAW;
    s->data_comp = nb_comp;
    s->data_format = format;
    s->data_stride = s->data_comp * sizeof(float);

    int ret = ngli_animation_init(&s->anim, s,
                                  s->animkf, s->nb_animkf,
                                  mix_buffer, cpy_buffer);<commit_msg>animatedbuffer: use format API to fill buffer properties<commit_after>/*
 * Copyright 2017 GoPro Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <float.h>
#include <stddef.h>
#include <string.h>
#include "animation.h"
#include "log.h"
#include "math_utils.h"
#include "memory.h"
#include "nodegl.h"
#include "nodes.h"

#define OFFSET(x) offsetof(struct buffer_priv, x)
static const struct node_param animatedbuffer_params[] = {
    {"keyframes", PARAM_TYPE_NODELIST, OFFSET(animkf),
                  .node_types=(const int[]){NGL_NODE_ANIMKEYFRAMEBUFFER, -1},
                  .flags=PARAM_FLAG_DOT_DISPLAY_PACKED,
                  .desc=NGLI_DOCSTRING("key frame buffers to interpolate from")},
    {NULL}
};

static void mix_buffer(void *user_arg, void *dst,
                       const struct animkeyframe_priv *kf0,
                       const struct animkeyframe_priv *kf1,
                       double ratio)
{
    float *dstf = dst;
    const struct buffer_priv *s = user_arg;
    const float *d1 = (const float *)kf0->data;
    const float *d2 = (const float *)kf1->data;
    for (int k = 0; k < s->count; k++)
        for (int i = 0; i < s->data_comp; i++)
            dstf[k*s->data_comp + i] = NGLI_MIX(d1[k*s->data_comp + i], d2[k*s->data_comp + i], ratio);
}

static void cpy_buffer(void *user_arg, void *dst,
                       const struct animkeyframe_priv *kf)
{
    const struct buffer_priv *s = user_arg;
    memcpy(dst, kf->data, s->data_size);
}

static int animatedbuffer_update(struct ngl_node *node, double t)
{
    struct buffer_priv *s = node->priv_data;
    return ngli_animation_evaluate(&s->anim, s->data, t);
}

static int animatedbuffer_init(struct ngl_node *node)
{
    struct buffer_priv *s = node->priv_data;

    int format;

    switch (node->class->id) {
    case NGL_NODE_ANIMATEDBUFFERFLOAT: format = NGLI_FORMAT_R32_SFLOAT;          break;
    case NGL_NODE_ANIMATEDBUFFERVEC2:  format = NGLI_FORMAT_R32G32_SFLOAT;       break;
    case NGL_NODE_ANIMATEDBUFFERVEC3:  format = NGLI_FORMAT_R32G32B32_SFLOAT;    break;
    case NGL_NODE_ANIMATEDBUFFERVEC4:  format = NGLI_FORMAT_R32G32B32A32_SFLOAT; break;
    default:
        ngli_assert(0);
    }

    s->dynamic = 1;
    s->usage = GL_DYNAMIC_DRAW;
    s->data_format = format;
    s->data_comp = ngli_format_get_nb_comp(s->data_format);
    s->data_stride = ngli_format_get_bytes_per_pixel(s->data_format);

    int ret = ngli_animation_init(&s->anim, s,
                                  s->animkf, s->nb_animkf,
                                  mix_buffer, cpy_buffer);
{'fraction_non_alphanumeric': 0.10175333433238423, 'fraction_numerical': 0.013786902442679454, 'mean_word_length': 3.032628398791541, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2863086', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2176, 'n_words': 621}