Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1-only */
2 : /**
3 : * GStreamer/NNStreamer Tensor-IF
4 : * Copyright (C) 2020 MyungJoo Ham <myungjoo.ham@samsung.com>
5 : */
6 : /**
7 : * @file tensordecutil.c
8 : * @date 14 April 2020
9 : * @brief Common utility functions for tensordec subplugins
10 : * @see https://github.com/nnstreamer/nnstreamer
11 : * @author MyungJoo Ham <myungjoo.ham@samsung.com>
12 : * @bug No known bugs except for NYI items
13 : */
14 :
15 : #include <glib.h>
16 : #include <string.h>
17 : #include <nnstreamer_log.h>
18 : #include "tensordecutil.h"
19 : #include <gst/gstvalue.h>
20 :
21 : /**
22 : * @brief Load label file into the internal data
23 : * @param[in/out] l The given ImageLabelData struct.
24 : */
25 : void
26 31 : loadImageLabels (const char *label_path, imglabel_t * l)
27 : {
28 31 : GError *err = NULL;
29 : gchar **_labels;
30 31 : gchar *contents = NULL;
31 31 : gsize len = 0;
32 : guint i;
33 :
34 31 : _free_labels (l);
35 :
36 : /* Read file contents */
37 31 : if (!g_file_get_contents (label_path, &contents, &len, &err) || len <= 0) {
38 0 : ml_loge ("Unable to read file %s with error %s.", label_path, err->message);
39 0 : g_clear_error (&err);
40 31 : return;
41 : }
42 :
43 31 : if (contents[len - 1] == '\n')
44 31 : contents[len - 1] = '\0';
45 :
46 31 : _labels = g_strsplit (contents, "\n", -1);
47 31 : l->total_labels = g_strv_length (_labels);
48 31 : l->labels = g_new0 (char *, l->total_labels);
49 31 : if (l->labels == NULL) {
50 0 : ml_loge ("Failed to allocate memory for label data.");
51 0 : l->total_labels = 0;
52 0 : goto error;
53 : }
54 :
55 18267 : for (i = 0; i < l->total_labels; i++) {
56 18236 : l->labels[i] = g_strdup (_labels[i]);
57 :
58 18236 : len = strlen (_labels[i]);
59 18236 : if (len > l->max_word_length) {
60 164 : l->max_word_length = len;
61 : }
62 : }
63 :
64 31 : error:
65 31 : g_strfreev (_labels);
66 31 : g_free (contents);
67 :
68 31 : if (l->labels != NULL) {
69 31 : ml_logi ("Loaded image label file successfully. %u labels loaded.",
70 : l->total_labels);
71 : }
72 31 : return;
73 : }
74 :
75 : /**
76 : * @brief Initialize ASCII font sprite.
77 : */
78 : void
79 19 : initSingleLineSprite (singleLineSprite_t v, rasters_t r, uint32_t pv)
80 : {
81 : int i, j, k;
82 : /** @todo Constify singleLineSprite and remove this loop */
83 :
84 4883 : for (i = 0; i < 256; i++) {
85 4864 : int ch = i;
86 : uint8_t val;
87 :
88 4864 : if (ch < 32 || ch >= 127) {
89 : /* It's not ASCII */
90 3059 : ch = '*';
91 : }
92 4864 : ch -= 32;
93 :
94 68096 : for (j = 0; j < 13; j++) {
95 63232 : val = r[ch][j];
96 569088 : for (k = 0; k < 8; k++) {
97 505856 : if (val & 0x80)
98 157605 : v[i][12 - j][k] = pv;
99 : else
100 348251 : v[i][12 - j][k] = 0;
101 505856 : val <<= 1;
102 : }
103 : }
104 : }
105 19 : }
106 :
107 : /**
108 : * @brief Free image labels
109 : */
110 : void
111 64 : _free_labels (imglabel_t * data)
112 : {
113 : guint i;
114 :
115 64 : if (data->labels) {
116 18267 : for (i = 0; i < data->total_labels; i++)
117 18236 : g_free (data->labels[i]);
118 31 : g_free (data->labels);
119 : }
120 64 : data->labels = NULL;
121 64 : data->total_labels = 0;
122 64 : data->max_word_length = 0;
123 64 : }
124 :
125 : /**
126 : * @brief Copy framerate caps from tensor config
127 : *
128 : * @param[out] caps to configure
129 : * @param[in] config to copy from
130 : */
131 : void
132 901 : setFramerateFromConfig (GstCaps * caps, const GstTensorsConfig * config)
133 : {
134 : gint fn, fd;
135 :
136 : /** @todo Verify if this rate is ok */
137 901 : fn = config->rate_n;
138 901 : fd = config->rate_d;
139 901 : if (fn >= 0 && fd > 0) {
140 668 : gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fn, fd, NULL);
141 : }
142 901 : return;
143 : }
|