• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-internal-ml.h"
26 #include "../gstamc-format.h"
27 
28 #include <ml_media_format.h>
29 
30 struct _GstAmcFormat
31 {
32   MLHandle handle;
33 };
34 
35 gboolean
gst_amc_format_static_init(void)36 gst_amc_format_static_init (void)
37 {
38   return TRUE;
39 }
40 
41 GstAmcFormat *
gst_amc_format_new_audio(const gchar * mime,gint sample_rate,gint channels,GError ** err)42 gst_amc_format_new_audio (const gchar * mime, gint sample_rate, gint channels,
43     GError ** err)
44 {
45   GstAmcFormat *format = g_slice_new0 (GstAmcFormat);
46   MLResult result;
47 
48   result =
49       MLMediaFormatCreateAudio (mime, sample_rate, channels, &format->handle);
50   if (result != MLResult_Ok) {
51     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
52         "Failed to create audio format");
53     g_slice_free (GstAmcFormat, format);
54     return NULL;
55   }
56 
57   return format;
58 }
59 
60 GstAmcFormat *
gst_amc_format_new_video(const gchar * mime,gint width,gint height,GError ** err)61 gst_amc_format_new_video (const gchar * mime, gint width, gint height,
62     GError ** err)
63 {
64   GstAmcFormat *format = g_slice_new0 (GstAmcFormat);
65   MLResult result;
66 
67   result = MLMediaFormatCreateVideo (mime, width, height, &format->handle);
68   if (result != MLResult_Ok) {
69     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
70         "Failed to create video format");
71     g_slice_free (GstAmcFormat, format);
72     return NULL;
73   }
74 
75   return format;
76 }
77 
78 GstAmcFormat *
gst_amc_format_new_handle(MLHandle handle)79 gst_amc_format_new_handle (MLHandle handle)
80 {
81   GstAmcFormat *format = g_slice_new0 (GstAmcFormat);
82   format->handle = handle;
83   return format;
84 }
85 
86 MLHandle
gst_amc_format_get_handle(GstAmcFormat * format)87 gst_amc_format_get_handle (GstAmcFormat * format)
88 {
89   return format->handle;
90 }
91 
92 void
gst_amc_format_free(GstAmcFormat * format)93 gst_amc_format_free (GstAmcFormat * format)
94 {
95   g_return_if_fail (format != NULL);
96   g_slice_free (GstAmcFormat, format);
97 }
98 
99 gchar *
gst_amc_format_to_string(GstAmcFormat * format,GError ** err)100 gst_amc_format_to_string (GstAmcFormat * format, GError ** err)
101 {
102   MLResult result;
103   gchar *str;
104 
105   str = g_new0 (gchar, MAX_FORMAT_STRING_SIZE);
106   result = MLMediaFormatObjectToString (format->handle, str);
107   if (result != MLResult_Ok) {
108     g_set_error (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
109         "Failed to convert format to string: %d", result);
110     g_free (str);
111     return NULL;
112   }
113 
114   return str;
115 }
116 
117 gboolean
gst_amc_format_get_float(GstAmcFormat * format,const gchar * key,gfloat * value,GError ** err)118 gst_amc_format_get_float (GstAmcFormat * format, const gchar * key,
119     gfloat * value, GError ** err)
120 {
121   MLResult result;
122 
123   g_return_val_if_fail (format != NULL, FALSE);
124   g_return_val_if_fail (key != NULL, FALSE);
125   g_return_val_if_fail (value != NULL, FALSE);
126 
127   result = MLMediaFormatGetKeyValueFloat (format->handle, key, value);
128   if (result != MLResult_Ok) {
129     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
130         "Failed to get float");
131     return FALSE;
132   }
133 
134   return TRUE;
135 }
136 
137 gboolean
gst_amc_format_set_float(GstAmcFormat * format,const gchar * key,gfloat value,GError ** err)138 gst_amc_format_set_float (GstAmcFormat * format, const gchar * key,
139     gfloat value, GError ** err)
140 {
141   MLResult result;
142 
143   g_return_val_if_fail (format != NULL, FALSE);
144   g_return_val_if_fail (key != NULL, FALSE);
145 
146   result = MLMediaFormatSetKeyFloat (format->handle, key, value);
147   if (result != MLResult_Ok) {
148     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
149         "Failed to set float");
150     return FALSE;
151   }
152 
153   return TRUE;
154 }
155 
156 gboolean
gst_amc_format_get_int(GstAmcFormat * format,const gchar * key,gint * value,GError ** err)157 gst_amc_format_get_int (GstAmcFormat * format, const gchar * key, gint * value,
158     GError ** err)
159 {
160   MLResult result;
161 
162   g_return_val_if_fail (format != NULL, FALSE);
163   g_return_val_if_fail (key != NULL, FALSE);
164   g_return_val_if_fail (value != NULL, FALSE);
165 
166   result = MLMediaFormatGetKeyValueInt32 (format->handle, key, value);
167   if (result != MLResult_Ok) {
168     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
169         "Failed to get int");
170     return FALSE;
171   }
172 
173   return TRUE;
174 }
175 
176 gboolean
gst_amc_format_set_int(GstAmcFormat * format,const gchar * key,gint value,GError ** err)177 gst_amc_format_set_int (GstAmcFormat * format, const gchar * key, gint value,
178     GError ** err)
179 {
180   MLResult result;
181 
182   g_return_val_if_fail (format != NULL, FALSE);
183   g_return_val_if_fail (key != NULL, FALSE);
184 
185   result = MLMediaFormatSetKeyInt32 (format->handle, key, value);
186   if (result != MLResult_Ok) {
187     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
188         "Failed to set int");
189     return FALSE;
190   }
191   return TRUE;
192 }
193 
194 gboolean
gst_amc_format_get_string(GstAmcFormat * format,const gchar * key,gchar ** value,GError ** err)195 gst_amc_format_get_string (GstAmcFormat * format, const gchar * key,
196     gchar ** value, GError ** err)
197 {
198   MLResult result;
199 
200   g_return_val_if_fail (format != NULL, FALSE);
201   g_return_val_if_fail (key != NULL, FALSE);
202   g_return_val_if_fail (value != NULL, FALSE);
203 
204   *value = g_new0 (gchar, MAX_KEY_STRING_SIZE);
205   result = MLMediaFormatGetKeyString (format->handle, key, *value);
206   if (result != MLResult_Ok) {
207     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
208         "Failed to get string");
209     g_clear_pointer (value, g_free);
210     return FALSE;
211   }
212 
213   return TRUE;
214 }
215 
216 gboolean
gst_amc_format_set_string(GstAmcFormat * format,const gchar * key,const gchar * value,GError ** err)217 gst_amc_format_set_string (GstAmcFormat * format, const gchar * key,
218     const gchar * value, GError ** err)
219 {
220   MLResult result;
221 
222   g_return_val_if_fail (format != NULL, FALSE);
223   g_return_val_if_fail (key != NULL, FALSE);
224 
225   result = MLMediaFormatSetKeyString (format->handle, key, value);
226   if (result != MLResult_Ok) {
227     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
228         "Failed to set string");
229     return FALSE;
230   }
231   return TRUE;
232 }
233 
234 gboolean
gst_amc_format_get_buffer(GstAmcFormat * format,const gchar * key,guint8 ** data,gsize * size,GError ** err)235 gst_amc_format_get_buffer (GstAmcFormat * format, const gchar * key,
236     guint8 ** data, gsize * size, GError ** err)
237 {
238   MLResult result;
239   MLMediaFormatByteArray buffer;
240 
241   g_return_val_if_fail (format != NULL, FALSE);
242   g_return_val_if_fail (key != NULL, FALSE);
243   g_return_val_if_fail (data != NULL, FALSE);
244   g_return_val_if_fail (size != NULL, FALSE);
245 
246   result = MLMediaFormatGetKeyByteBuffer (format->handle, key, &buffer);
247   if (result != MLResult_Ok) {
248     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
249         "Failed to get buffer");
250     return FALSE;
251   }
252 
253   *size = buffer.length;
254   *data = (guint8 *) g_memdup2 (buffer.ptr, buffer.length);
255   MLMediaFormatKeyByteBufferRelease (format->handle, &buffer);
256 
257   return TRUE;
258 }
259 
260 gboolean
gst_amc_format_set_buffer(GstAmcFormat * format,const gchar * key,guint8 * data,gsize size,GError ** err)261 gst_amc_format_set_buffer (GstAmcFormat * format, const gchar * key,
262     guint8 * data, gsize size, GError ** err)
263 {
264   MLResult result;
265   MLMediaFormatByteArray buffer;
266 
267   g_return_val_if_fail (format != NULL, FALSE);
268   g_return_val_if_fail (key != NULL, FALSE);
269   g_return_val_if_fail (data != NULL, FALSE);
270 
271   buffer.ptr = data;
272   buffer.length = size;
273   result = MLMediaFormatSetKeyByteBuffer (format->handle, key, &buffer);
274   if (result != MLResult_Ok) {
275     g_set_error_literal (err, GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED,
276         "Failed to set buffer");
277     return FALSE;
278   }
279   return TRUE;
280 }
281