File: drawvg.c

package info (click to toggle)
ffmpeg 7%3A8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 113,964 kB
  • sloc: ansic: 1,376,567; asm: 153,816; sh: 9,602; makefile: 5,414; cpp: 5,172; lisp: 1,771; perl: 1,463; objc: 1,058; python: 120; awk: 56; ruby: 51
file content (359 lines) | stat: -rw-r--r-- 10,484 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */


// Prevent the `dllimport` attribute in the functions declared by Cairo when
// the test is built on a Windows machine.
//
// This is needed to avoid the "redeclared without dllimport attribute after
// being referenced with dll linkage" warnings on every function redefined by
// the `MOCK_FN_n` macros below.
#define CAIRO_WIN32_STATIC_BUILD

#include <cairo.h>
#include <stdarg.h>
#include <stdio.h>

#include "libavutil/log.h"
#include "libavutil/pixdesc.h"

static void mock_av_log(void *ptr, int level, const char *fmt, va_list vl) {
    printf("av_log[%d]: ", level);
    vprintf(fmt, vl);
}

#include "libavfilter/vf_drawvg.c"

// Mock for cairo functions.
//
// `MOCK_FN_n` macros define wrappers for functions that only receive `n`
// arguments of type `double`.
//
// `MOCK_FN_I` macro wrap a function that receives a single integer value.

struct _cairo {
    double current_point_x;
    double current_point_y;
};

static void update_current_point(cairo_t *cr, const char *func, double x, double y) {
    // Update current point only if the function name contains `_to`.
    if (strstr(func, "_to") == NULL) {
        return;
    }

    if (strstr(func, "_rel_") == NULL) {
        cr->current_point_x = x;
        cr->current_point_y = y;
    } else {
        cr->current_point_x += x;
        cr->current_point_y += y;
    }
}

#define MOCK_FN_0(func)      \
    void func(cairo_t* cr) { \
        puts(#func);         \
    }

#define MOCK_FN_1(func)                 \
    void func(cairo_t* cr, double a0) { \
        printf(#func " %.1f\n", a0);    \
    }

#define MOCK_FN_2(func)                            \
    void func(cairo_t* cr, double a0, double a1) { \
        update_current_point(cr, #func, a0, a1);   \
        printf(#func " %.1f %.1f\n", a0, a1);      \
    }

#define MOCK_FN_4(func)                                                  \
    void func(cairo_t* cr, double a0, double a1, double a2, double a3) { \
        printf(#func " %.1f %.1f %.1f %.1f\n", a0, a1, a2, a3);          \
    }

#define MOCK_FN_5(func)                                                             \
    void func(cairo_t* cr, double a0, double a1, double a2, double a3, double a4) { \
        printf(#func " %.1f %.1f %.1f %.1f %.1f\n", a0, a1, a2, a3, a4);            \
    }

#define MOCK_FN_6(func)                                                                        \
    void func(cairo_t* cr, double a0, double a1, double a2, double a3, double a4, double a5) { \
        update_current_point(cr, #func, a4, a5);                                               \
        printf(#func " %.1f %.1f %.1f %.1f %.1f %.1f\n", a0, a1, a2, a3, a4, a5);              \
    }

#define MOCK_FN_I(func, type)          \
    void func(cairo_t* cr, type i) {   \
        printf(#func " %d\n", (int)i); \
    }

MOCK_FN_5(cairo_arc);
MOCK_FN_0(cairo_clip);
MOCK_FN_0(cairo_clip_preserve);
MOCK_FN_0(cairo_close_path);
MOCK_FN_6(cairo_curve_to);
MOCK_FN_0(cairo_fill);
MOCK_FN_0(cairo_fill_preserve);
MOCK_FN_0(cairo_identity_matrix);
MOCK_FN_2(cairo_line_to);
MOCK_FN_2(cairo_move_to);
MOCK_FN_0(cairo_new_path);
MOCK_FN_0(cairo_new_sub_path);
MOCK_FN_4(cairo_rectangle);
MOCK_FN_6(cairo_rel_curve_to);
MOCK_FN_2(cairo_rel_line_to);
MOCK_FN_2(cairo_rel_move_to);
MOCK_FN_0(cairo_reset_clip);
MOCK_FN_0(cairo_restore);
MOCK_FN_1(cairo_rotate);
MOCK_FN_0(cairo_save);
MOCK_FN_2(cairo_scale);
MOCK_FN_I(cairo_set_fill_rule, cairo_fill_rule_t);
MOCK_FN_1(cairo_set_font_size);
MOCK_FN_I(cairo_set_line_cap, cairo_line_cap_t);
MOCK_FN_I(cairo_set_line_join, cairo_line_join_t);
MOCK_FN_1(cairo_set_line_width);
MOCK_FN_1(cairo_set_miter_limit);
MOCK_FN_4(cairo_set_source_rgba);
MOCK_FN_0(cairo_stroke);
MOCK_FN_0(cairo_stroke_preserve);
MOCK_FN_2(cairo_translate);

cairo_bool_t cairo_get_dash_count(cairo_t *cr) {
    return 1;
}

cairo_status_t cairo_status(cairo_t *cr) {
    return CAIRO_STATUS_SUCCESS;
}

void cairo_get_dash(cairo_t *cr, double *dashes, double *offset) {
    // Return a dummy value to verify that it is included in
    // the next call to `cairo_set_dash`.
    *dashes = -1;

    if (offset)
        *offset = -2;
}

void cairo_set_dash(cairo_t *cr, const double *dashes, int num_dashes, double offset) {
    printf("%s [", __func__);
    for (int i = 0; i < num_dashes; i++)
        printf(" %.1f", dashes[i]);
    printf(" ] %.1f\n", offset);
}

cairo_bool_t cairo_has_current_point(cairo_t *cr) {
    return 1;
}

void cairo_get_current_point(cairo_t *cr, double *x, double *y) {
    *x = cr->current_point_x;
    *y = cr->current_point_y;
}

void cairo_set_source(cairo_t *cr, cairo_pattern_t *source) {
    int count;
    double r, g, b, a;
    double x0, y0, x1, y1, r0, r1;

    printf("%s", __func__);

#define PRINT_COLOR(prefix) \
    printf(prefix "#%02lx%02lx%02lx%02lx", lround(r*255), lround(g*255), lround(b*255), lround(a*255))

    switch (cairo_pattern_get_type(source)) {
    case CAIRO_PATTERN_TYPE_SOLID:
        cairo_pattern_get_rgba(source, &r, &g, &b, &a);
        PRINT_COLOR(" ");
        break;

    case CAIRO_PATTERN_TYPE_LINEAR:
        cairo_pattern_get_linear_points(source, &x0, &y0, &x1, &y1);
        printf(" lineargrad(%.1f %.1f %.1f %.1f)", x0, y0, x1, y1);
        break;

    case CAIRO_PATTERN_TYPE_RADIAL:
        cairo_pattern_get_radial_circles(source, &x0, &y0, &r0, &x1, &y1, &r1);
        printf(" radialgrad(%.1f %.1f %.1f %.1f %.1f %.1f)", x0, y0, r0, x1, y1, r1);
        break;
    }

    if (cairo_pattern_get_color_stop_count(source, &count) == CAIRO_STATUS_SUCCESS) {
        for (int i = 0; i < count; i++) {
            cairo_pattern_get_color_stop_rgba(source, i, &x0, &r, &g, &b, &a);
            printf(" %.1f/", x0);
            PRINT_COLOR("");
        }
    }

    printf("\n");
}

// Verify that the `vgs_commands` array is sorted, so it can
// be used with `bsearch(3)`.
static void check_sorted_cmds_array(void) {
    int failures = 0;

    for (int i = 0; i < FF_ARRAY_ELEMS(vgs_commands) - 1; i++) {
        if (vgs_comp_command_spec(&vgs_commands[i], &vgs_commands[i]) != 0) {
            printf("%s: comparator must return 0 for item %d\n", __func__, i);
            failures++;
        }

        if (vgs_comp_command_spec(&vgs_commands[i], &vgs_commands[i + 1]) >= 0) {
            printf("%s: entry for '%s' must appear after '%s', at index %d\n",
                __func__, vgs_commands[i].name, vgs_commands[i + 1].name, i);
            failures++;
        }
    }

    printf("%s: %d failures\n", __func__, failures);
}

// Compile and run a script.
static void check_script(int is_file, const char* source) {
    int ret;

    AVDictionary *metadata = NULL;

    struct VGSEvalState state;
    struct VGSParser parser;
    struct VGSProgram program;

    struct _cairo cairo_ctx = { 0, 0 };

    if (is_file) {
        uint8_t *s = NULL;

        printf("\n--- %s: %s\n", __func__, av_basename(source));

        ret = ff_load_textfile(NULL, source, &s, NULL);
        if (ret != 0) {
            printf("Failed to read %s: %d\n", source, ret);
            return;
        }

        source = s;
    } else {
        printf("\n--- %s: %s\n", __func__, source);
    }

    ret = av_dict_parse_string(&metadata, "m.a=1:m.b=2", "=", ":", 0);
    av_assert0(ret == 0);

    vgs_parser_init(&parser, source);

    ret = vgs_parse(NULL, &parser, &program, 0);

    int init_ret = vgs_eval_state_init(&state, &program, NULL, NULL);
    av_assert0(init_ret == 0);

    for (int i = 0; i < VAR_COUNT; i++)
        state.vars[i] = 1 << i;

    vgs_parser_free(&parser);

    if (ret != 0) {
        printf("%s: vgs_parse = %d\n", __func__, ret);
        goto exit;
    }

    state.metadata = metadata;
    state.cairo_ctx = &cairo_ctx;

    ret = vgs_eval(&state, &program, 0);
    vgs_eval_state_free(&state);

    if (ret != 0)
        printf("%s: vgs_eval = %d\n", __func__, ret);

exit:
    av_dict_free(&metadata);

    if (is_file)
        av_free((void*)source);

    vgs_free(&program);
}

int main(int argc, const char **argv)
{
    char buf[512];

    av_log_set_callback(mock_av_log);

    check_sorted_cmds_array();

    for (int i = 1; i < argc; i++)
        check_script(1, argv[i]);

    // Detect unclosed expressions.
    check_script(0, "M 0 (1*(t+1)");

    // Invalid command.
    check_script(0, "save invalid 1 2");

    // Invalid constant.
    check_script(0, "setlinecap unknown m 10 20");

    // Missing arguments.
    check_script(0, "M 0 1 2");

    // Invalid variable names.
    check_script(0, "setvar ba^d 0");

    // Reserved names.
    check_script(0, "setvar cx 0");

    // Max number of user variables.
    memset(buf, 0, sizeof(buf));
    for (int i = 0; i < USER_VAR_COUNT; i++) {
        av_strlcatf(buf, sizeof(buf), " setvar v%d %d", i, i);
    }
    av_strlcatf(buf, sizeof(buf), " M (v0) (v%d) 1 (unknown_var)", USER_VAR_COUNT - 1);
    check_script(0, buf);

    // Too many variables.
    memset(buf, 0, sizeof(buf));
    for (int i = 0; i < USER_VAR_COUNT + 1; i++) {
        av_strlcatf(buf, sizeof(buf), " setvar v%d %d", i + 1, i);
    }
    check_script(0, buf);

    // Invalid procedure names.
    check_script(0, "call a");
    check_script(0, "proc a { call b } call a");

    // Invalid arguments list.
    check_script(0, "proc p0 a1 a2 a3 a4 a5 a6 a7 a8 { break }");
    check_script(0, "proc p0 a1 a2 { break } call p0 break");
    check_script(0, "proc p0 a1 a2 { break } call p0 1 2 3");

    // Long expressions.
    memset(buf, 0, sizeof(buf));
    strncat(buf, "M 0 (1", sizeof(buf) - 1);
    for (int i = 0; i < 100; i++) {
        strncat(buf, " + n", sizeof(buf) - 1);
    }
    strncat(buf, ")", sizeof(buf) - 1);
    check_script(0, buf);

    return 0;
}