1 /*
2 * Copyright (C) 2012, Collabora Ltd.
3 * Copyright (C) 2012, Cisco Systems, Inc.
4 * Author: Youness Alaoui <youness.alaoui@collabora.co.uk>
5 *
6 * Copyright (C) 2015, Collabora Ltd.
7 * Author: Justin Kim <justin.kim@collabora.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "gstjniutils.h"
30
31 #include "gst-android-graphics-imageformat.h"
32
33
34 static struct
35 {
36 jclass klass;
37 jmethodID get_bits_per_pixel;
38 jint JPEG;
39 jint NV16;
40 jint NV21;
41 jint RGB_565;
42 jint UNKNOWN;
43 jint YUY2;
44 jint YV12;
45 } android_graphics_imageformat = {
46 0};
47
48 gint ImageFormat_JPEG;
49 gint ImageFormat_NV16;
50 gint ImageFormat_NV21;
51 gint ImageFormat_RGB_565;
52 gint ImageFormat_UNKNOWN;
53 gint ImageFormat_YUY2;
54 gint ImageFormat_YV12;
55
56 static gboolean
_init_classes(void)57 _init_classes (void)
58 {
59 JNIEnv *env;
60
61 jfieldID fieldID;
62 jclass klass;
63
64 GError *err = NULL;
65
66 env = gst_amc_jni_get_env ();
67
68 /* android.graphics.ImageFormat */
69 klass = android_graphics_imageformat.klass =
70 gst_amc_jni_get_class (env, &err, "android/graphics/ImageFormat");
71
72 if (err)
73 goto failed;
74
75 android_graphics_imageformat.get_bits_per_pixel =
76 gst_amc_jni_get_static_method_id (env, &err, klass,
77 "getBitsPerPixel", "(I)I");
78
79 if (err)
80 goto failed;
81
82 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "JPEG", "I");
83 if (err)
84 goto failed;
85 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
86 &android_graphics_imageformat.JPEG))
87 goto failed;
88 ImageFormat_JPEG = android_graphics_imageformat.JPEG;
89
90 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "NV16", "I");
91 if (err)
92 goto failed;
93 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
94 &android_graphics_imageformat.NV16))
95 goto failed;
96 ImageFormat_NV16 = android_graphics_imageformat.NV16;
97
98 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "NV21", "I");
99 if (err)
100 goto failed;
101 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
102 &android_graphics_imageformat.NV21))
103 goto failed;
104 ImageFormat_NV21 = android_graphics_imageformat.NV21;
105
106 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "RGB_565", "I");
107 if (err)
108 goto failed;
109 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
110 &android_graphics_imageformat.RGB_565))
111 goto failed;
112 ImageFormat_RGB_565 = android_graphics_imageformat.RGB_565;
113
114 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "UNKNOWN", "I");
115 if (err)
116 goto failed;
117 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
118 &android_graphics_imageformat.UNKNOWN))
119 goto failed;
120 ImageFormat_UNKNOWN = android_graphics_imageformat.UNKNOWN;
121
122 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "YUY2", "I");
123 if (err)
124 goto failed;
125 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
126 &android_graphics_imageformat.YUY2))
127 goto failed;
128 ImageFormat_YUY2 = android_graphics_imageformat.YUY2;
129
130 fieldID = gst_amc_jni_get_static_field_id (env, &err, klass, "YV12", "I");
131 if (err)
132 goto failed;
133 if (!gst_amc_jni_get_static_int_field (env, &err, klass, fieldID,
134 &android_graphics_imageformat.YV12))
135 goto failed;
136 ImageFormat_YV12 = android_graphics_imageformat.YV12;
137
138 return TRUE;
139
140 failed:
141 if (err) {
142 GST_ERROR ("Failed to get android.graphics.ImageFormat class: %s",
143 err->message);
144 g_clear_error (&err);
145 }
146
147 return FALSE;
148 }
149
150 gboolean
gst_android_graphics_imageformat_init(void)151 gst_android_graphics_imageformat_init (void)
152 {
153 if (!_init_classes ()) {
154 gst_android_graphics_imageformat_deinit ();
155 return FALSE;
156 }
157
158 return TRUE;
159 }
160
161 void
gst_android_graphics_imageformat_deinit(void)162 gst_android_graphics_imageformat_deinit (void)
163 {
164 JNIEnv *env = gst_amc_jni_get_env ();
165
166 if (android_graphics_imageformat.klass)
167 (*env)->DeleteGlobalRef (env, android_graphics_imageformat.klass);
168 android_graphics_imageformat.klass = NULL;
169 }
170
171 /* android.graphics.ImageFormat */
172 gint
gst_ag_imageformat_get_bits_per_pixel(gint format)173 gst_ag_imageformat_get_bits_per_pixel (gint format)
174 {
175 JNIEnv *env;
176 GError *err = NULL;
177
178 jclass klass = android_graphics_imageformat.klass;
179
180 jint bpp = 0;
181
182 env = gst_amc_jni_get_env ();
183
184 if (!gst_amc_jni_call_static_int_method (env, &err,
185 klass, android_graphics_imageformat.get_bits_per_pixel, &bpp,
186 format)) {
187 GST_ERROR ("Failed to get android.graphics.ImageFormat class: %s",
188 err->message);
189 g_clear_error (&err);
190 }
191
192 return bpp;
193 }
194