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 33 : loadImageLabels (const char *label_path, imglabel_t * l)
27 : {
28 33 : GError *err = NULL;
29 : gchar **_labels;
30 33 : gchar *contents = NULL;
31 33 : gsize len = 0;
32 : guint i;
33 :
34 33 : _free_labels (l);
35 :
36 : /* Read file contents */
37 33 : 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 33 : return;
41 : }
42 :
43 33 : if (contents[len - 1] == '\n')
44 33 : contents[len - 1] = '\0';
45 :
46 33 : _labels = g_strsplit (contents, "\n", -1);
47 33 : l->total_labels = g_strv_length (_labels);
48 33 : l->labels = g_new0 (char *, l->total_labels);
49 33 : 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 18299 : for (i = 0; i < l->total_labels; i++) {
56 18266 : l->labels[i] = g_strdup (_labels[i]);
57 :
58 18266 : len = strlen (_labels[i]);
59 18266 : if (len > l->max_word_length) {
60 172 : l->max_word_length = len;
61 : }
62 : }
63 :
64 33 : error:
65 33 : g_strfreev (_labels);
66 33 : g_free (contents);
67 :
68 33 : if (l->labels != NULL) {
69 33 : ml_logi ("Loaded image label file successfully. %u labels loaded.",
70 : l->total_labels);
71 : }
72 33 : return;
73 : }
74 :
75 : /**
76 : * @brief Initialize ASCII font sprite.
77 : */
78 : void
79 21 : 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 5397 : for (i = 0; i < 256; i++) {
85 5376 : int ch = i;
86 : uint8_t val;
87 :
88 5376 : if (ch < 32 || ch >= 127) {
89 : /* It's not ASCII */
90 3381 : ch = '*';
91 : }
92 5376 : ch -= 32;
93 :
94 75264 : for (j = 0; j < 13; j++) {
95 69888 : val = r[ch][j];
96 628992 : for (k = 0; k < 8; k++) {
97 559104 : if (val & 0x80)
98 174195 : v[i][12 - j][k] = pv;
99 : else
100 384909 : v[i][12 - j][k] = 0;
101 559104 : val <<= 1;
102 : }
103 : }
104 : }
105 21 : }
106 :
107 : /**
108 : * @brief Free image labels
109 : */
110 : void
111 68 : _free_labels (imglabel_t * data)
112 : {
113 : guint i;
114 :
115 68 : if (data->labels) {
116 18299 : for (i = 0; i < data->total_labels; i++)
117 18266 : g_free (data->labels[i]);
118 33 : g_free (data->labels);
119 : }
120 68 : data->labels = NULL;
121 68 : data->total_labels = 0;
122 68 : data->max_word_length = 0;
123 68 : }
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 656 : setFramerateFromConfig (GstCaps * caps, const GstTensorsConfig * config)
133 : {
134 : gint fn, fd;
135 :
136 : /** @todo Verify if this rate is ok */
137 656 : fn = config->rate_n;
138 656 : fd = config->rate_d;
139 656 : if (fn >= 0 && fd > 0) {
140 656 : gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, fn, fd, NULL);
141 : }
142 656 : return;
143 : }
|