1 /*
2 * Copyright (C) 2018 Collabora Ltd.
3 * Author: Xavier Claessens <xavier.claessens@collabora.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "../gstamc-codeclist.h"
26
27 #include <ml_media_codeclist.h>
28
29 struct _GstAmcCodecInfoHandle
30 {
31 uint64_t index;
32 };
33
34 struct _GstAmcCodecCapabilitiesHandle
35 {
36 uint64_t index;
37 gchar *type;
38 };
39
40 gboolean
gst_amc_codeclist_static_init(void)41 gst_amc_codeclist_static_init (void)
42 {
43 return TRUE;
44 }
45
46 gboolean
gst_amc_codeclist_get_count(gint * count,GError ** err)47 gst_amc_codeclist_get_count (gint * count, GError ** err)
48 {
49 MLResult result;
50 uint64_t n;
51
52 result = MLMediaCodecListCountCodecs (&n);
53 if (result != MLResult_Ok) {
54 g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
55 "Failed to get codec list count: %d", result);
56 return FALSE;
57 }
58
59 *count = n;
60
61 return TRUE;
62 }
63
64 GstAmcCodecInfoHandle *
gst_amc_codeclist_get_codec_info_at(gint index,GError ** err)65 gst_amc_codeclist_get_codec_info_at (gint index, GError ** err)
66 {
67 GstAmcCodecInfoHandle *ret = g_new0 (GstAmcCodecInfoHandle, 1);
68 ret->index = index;
69 return ret;
70 }
71
72 void
gst_amc_codec_info_handle_free(GstAmcCodecInfoHandle * handle)73 gst_amc_codec_info_handle_free (GstAmcCodecInfoHandle * handle)
74 {
75 g_free (handle);
76 }
77
78 gchar *
gst_amc_codec_info_handle_get_name(GstAmcCodecInfoHandle * handle,GError ** err)79 gst_amc_codec_info_handle_get_name (GstAmcCodecInfoHandle * handle,
80 GError ** err)
81 {
82 MLResult result;
83 gchar *name;
84
85 name = g_new0 (gchar, MAX_CODEC_NAME_LENGTH);
86 result = MLMediaCodecListGetCodecName (handle->index, name);
87 if (result != MLResult_Ok) {
88 g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
89 "Failed to get codec name: %d", result);
90 g_free (name);
91 return NULL;
92 }
93
94 return name;
95 }
96
97 gboolean
gst_amc_codec_info_handle_is_encoder(GstAmcCodecInfoHandle * handle,gboolean * is_encoder,GError ** err)98 gst_amc_codec_info_handle_is_encoder (GstAmcCodecInfoHandle * handle,
99 gboolean * is_encoder, GError ** err)
100 {
101 MLResult result;
102 bool out;
103
104 result = MLMediaCodecListIsEncoder (handle->index, &out);
105 if (result != MLResult_Ok) {
106 g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
107 "Failed to check if codec is an encoder: %d", result);
108 return FALSE;
109 }
110
111 *is_encoder = out;
112
113 return TRUE;
114 }
115
116 gchar **
gst_amc_codec_info_handle_get_supported_types(GstAmcCodecInfoHandle * handle,gsize * length,GError ** err)117 gst_amc_codec_info_handle_get_supported_types (GstAmcCodecInfoHandle * handle,
118 gsize * length, GError ** err)
119 {
120 MLMediaCodecListQueryResults types;
121 MLResult result;
122 gchar **ret;
123 gsize i;
124
125 result = MLMediaCodecListGetSupportedMimes (handle->index, &types);
126 if (result != MLResult_Ok) {
127 g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
128 "Failed to get codec supported types: %d", result);
129 return NULL;
130 }
131
132 *length = types.count;
133 ret = g_new0 (gchar *, *length + 1);
134 for (i = 0; i < *length; i++)
135 ret[i] = g_strdup (types.data[i]);
136
137 MLMediaCodecListQueryResultsRelease (&types);
138
139 return ret;
140 }
141
142 GstAmcCodecCapabilitiesHandle *
gst_amc_codec_info_handle_get_capabilities_for_type(GstAmcCodecInfoHandle * handle,const gchar * type,GError ** err)143 gst_amc_codec_info_handle_get_capabilities_for_type (GstAmcCodecInfoHandle *
144 handle, const gchar * type, GError ** err)
145 {
146 GstAmcCodecCapabilitiesHandle *ret;
147
148 ret = g_new0 (GstAmcCodecCapabilitiesHandle, 1);
149 ret->index = handle->index;
150 ret->type = g_strdup (type);
151 return ret;
152 }
153
154 void
gst_amc_codec_capabilities_handle_free(GstAmcCodecCapabilitiesHandle * handle)155 gst_amc_codec_capabilities_handle_free (GstAmcCodecCapabilitiesHandle * handle)
156 {
157 g_free (handle->type);
158 g_free (handle);
159 }
160
gst_amc_codec_capabilities_handle_get_color_formats(GstAmcCodecCapabilitiesHandle * handle,gsize * length,GError ** err)161 gint *gst_amc_codec_capabilities_handle_get_color_formats
162 (GstAmcCodecCapabilitiesHandle * handle, gsize * length, GError ** err)
163 {
164 uint32_t *colorFormats;
165 MLResult result;
166 gint *ret;
167 gsize i;
168
169 result =
170 MLMediaCodecListGetSupportedColorFormats (handle->index, handle->type,
171 &colorFormats, length);
172 if (result != MLResult_Ok) {
173 g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
174 "Failed to get codec supported color formats: %d", result);
175 return NULL;
176 }
177
178 ret = g_new0 (gint, *length);
179 for (i = 0; i < *length; i++) {
180 ret[i] = colorFormats[i];
181 }
182
183 MLMediaCodecListColorFormatsRelease (colorFormats);
184
185 return ret;
186 }
187
gst_amc_codec_capabilities_handle_get_profile_levels(GstAmcCodecCapabilitiesHandle * handle,gsize * length,GError ** err)188 GstAmcCodecProfileLevel *gst_amc_codec_capabilities_handle_get_profile_levels
189 (GstAmcCodecCapabilitiesHandle * handle, gsize * length, GError ** err)
190 {
191 MLMediaCodecListProfileLevel *profileLevels;
192 GstAmcCodecProfileLevel *ret;
193 MLResult result;
194 gsize i;
195
196 result =
197 MLMediaCodecListGetSupportedProfileLevels (handle->index, handle->type,
198 &profileLevels, length);
199 if (result != MLResult_Ok) {
200 g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
201 "Failed to get codec supported types: %d", result);
202 return NULL;
203 }
204
205 ret = g_new0 (GstAmcCodecProfileLevel, *length);
206 for (i = 0; i < *length; i++) {
207 ret[i].profile = profileLevels[i].profile;
208 ret[i].level = profileLevels[i].level;
209 }
210
211 MLMediaCodecListProfileLevelsRelease (profileLevels);
212
213 return ret;
214 }
215