Replace position to set caps for appsrc/appsink before push buffers
[platform/core/multimedia/libmm-imgp-gstcs.git] / gstcs / mm_util_gstcs.c
1 /*
2  * libmm-imgp-gstcs
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include "mm_util_gstcs_internal.h"
22 #include <gst/check/gstcheck.h>
23 #include <gst/video/video-format.h>
24
25 #define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
26 #define MM_UTIL_ROUND_UP_4(num) (((num)+3)&~3)
27 #define MM_UTIL_ROUND_UP_8(num) (((num)+7)&~7)
28 #define MM_UTIL_ROUND_UP_16(num) (((num)+15)&~15)
29
30
31 static GstFlowReturn
32 _mm_sink_sample(GstElement * appsink, gpointer user_data)
33 {
34         GstBuffer *_buf=NULL;
35         GstSample *_sample = NULL;
36         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
37         _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
38         if (_sample) {
39                 _buf = gst_sample_get_buffer(_sample);
40
41                 pGstreamer_s->output_buffer = _buf;
42
43                 if(pGstreamer_s->output_buffer != NULL) {
44                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
45                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
46                         gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d", mapinfo.data, mapinfo.size);
47                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
48                 } else {
49                         gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
50                 }
51         }
52
53         gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushing */
54         gst_sample_unref(_sample);
55
56         return GST_FLOW_OK;
57 }
58
59 static gboolean
60 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
61 {
62         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
63         switch (GST_MESSAGE_TYPE (message)) {
64         case GST_MESSAGE_EOS: {
65                 gstcs_debug("The source got dry");
66                 gst_app_src_end_of_stream(GST_APP_SRC(pGstreamer_s->appsrc));
67                 g_main_context_pop_thread_default(pGstreamer_s->context);
68                 g_main_loop_quit (pGstreamer_s->loop);
69                 break;
70                 }
71         case GST_MESSAGE_ERROR: {
72                 GError *err = NULL;
73                 gchar *dbg_info = NULL;
74
75                 gst_message_parse_error (message, &err, &dbg_info);
76                 gstcs_error ("ERROR from element %s: %s\n", GST_OBJECT_NAME (message->src), err->message);
77                 gstcs_error ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
78                 g_error_free (err);
79                 g_free (dbg_info);
80                 g_main_context_pop_thread_default(pGstreamer_s->context);
81                 g_main_loop_quit (pGstreamer_s->loop);
82                 gstcs_debug("Quit GST_CS\n");
83                 break;
84                 }
85         default:
86                 break;
87         }
88         return TRUE;
89 }
90
91 static int
92 _mm_get_byte_per_pixcel(const char *__format_label)
93 {
94         int byte_per_pixcel = 1;
95         if (strcmp(__format_label, "RGB565") == 0) {
96                 byte_per_pixcel = 2;
97         } else if (strcmp(__format_label, "RGB888") == 0 ||
98                 strcmp(__format_label, "BGR888") == 0) {
99                 byte_per_pixcel = 3;
100         } else if (strcmp(__format_label, "RGBA8888") == 0 ||
101                 strcmp(__format_label, "ARGB8888") == 0 ||
102                 strcmp(__format_label, "BGRA8888") == 0 ||
103                 strcmp(__format_label, "ABGR8888") == 0 ||
104                 strcmp(__format_label, "RGBX") == 0 ||
105                 strcmp(__format_label, "XRGB") == 0 ||
106                 strcmp(__format_label, "BGRX") == 0 ||
107                 strcmp(__format_label, "XBGR") == 0) {
108                 byte_per_pixcel = 4;
109         }
110
111         gstcs_debug("byte per pixcel : %d", byte_per_pixcel);
112
113         return byte_per_pixcel;
114 }
115
116 static int
117 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
118 {
119         int ret = GSTCS_ERROR_NONE;
120         pGstreamer_s->pipeline= gst_pipeline_new ("videoconvert");
121         if (!pGstreamer_s->pipeline) {
122                 gstcs_error("pipeline could not be created. Exiting.\n");
123                 ret = GSTCS_ERROR_INVALID_PARAMETER;
124         }
125         pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
126         if (!pGstreamer_s->appsrc) {
127                 gstcs_error("appsrc could not be created. Exiting.\n");
128                 ret = GSTCS_ERROR_INVALID_PARAMETER;
129         }
130         pGstreamer_s->colorspace=gst_element_factory_make("videoconvert","colorspace");
131         if (!pGstreamer_s->colorspace) {
132                 gstcs_error("colorspace could not be created. Exiting.\n");
133                 ret = GSTCS_ERROR_INVALID_PARAMETER;
134         }
135         pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
136         if (!pGstreamer_s->videoscale) {
137                 gstcs_error("videoscale could not be created. Exiting.\n");
138                 ret = GSTCS_ERROR_INVALID_PARAMETER;
139         }
140         pGstreamer_s->videoflip=gst_element_factory_make("videoflip", "flip");
141         if (!pGstreamer_s->videoflip) {
142                 gstcs_error("videoflip could not be created. Exiting.\n");
143                 ret = GSTCS_ERROR_INVALID_PARAMETER;
144         }
145         pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
146         if (!pGstreamer_s->appsink) {
147                 gstcs_error("appsink could not be created. Exiting.\n");
148                 ret = GSTCS_ERROR_INVALID_PARAMETER;
149         }
150         return ret;
151 }
152
153 static void _mm_destroy_notify(gpointer data) {
154         unsigned char *_data = (unsigned char *)data;
155         if (_data != NULL) {
156                 free(_data);
157                 _data = NULL;
158         }
159 }
160
161 static void
162 _mm_check_caps_format(GstCaps* caps)
163 {
164         GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
165         const gchar* formatInfo = gst_structure_get_string (caps_structure, "format");
166         gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
167 }
168
169 static void
170 _mm_link_pipeline(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int value)
171 {
172         /* set property */
173         gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL);
174         if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL)) {
175                 gstcs_error("Fail to link pipeline");
176         } else {
177                 gstcs_debug("Success to link pipeline");
178         }
179
180         g_object_set (G_OBJECT (pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
181         g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
182         g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
183
184         g_object_set(pGstreamer_s->videoflip, "method", value, NULL ); /* GST_VIDEO_FLIP_METHOD_IDENTITY (0): none- Identity (no rotation) (1): clockwise - Rotate clockwise 90 degrees (2): rotate-180 - Rotate 180 degrees (3): counterclockwise - Rotate counter-clockwise 90 degrees (4): horizontal-flip - Flip horizontally (5): vertical-flip - Flip vertically (6): upper-left-diagonal - Flip across upper left/lower right diagonal (7): upper-right-diagonal - Flip across upper right/lower left diagonal */
185
186         /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
187         g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
188 }
189
190 static void
191 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
192 {
193         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
194         GstVideoFormat videoFormat;
195         int _bpp=0; int _depth=0; int _red_mask=0; int _green_mask=0; int _blue_mask=0; int _alpha_mask=0; int _endianness=0;
196
197         if(__format == NULL) {
198                 gstcs_error("Image format is NULL\n");
199                 return;
200         }
201
202         gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
203         memset(_format_name, 0, sizeof(_format_name));
204
205         if(strcmp(__format->colorspace,"YUV") == 0) {
206                 if(strcmp(__format->format_label,"I420") == 0
207                 || strcmp(__format->format_label,"Y42B") == 0
208                 || strcmp(__format->format_label,"Y444") == 0
209                 || strcmp(__format->format_label,"YV12") == 0
210                 || strcmp(__format->format_label,"NV12") == 0
211                 || strcmp(__format->format_label,"UYVY") == 0) {
212                         strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
213                 }else if(strcmp(__format->format_label,"YUYV") == 0) {
214                         strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
215                 }
216                 gstcs_debug("Chosen video format: %s", _format_name);
217                 __format->caps = gst_caps_new_simple ("video/x-raw",
218                         "format", G_TYPE_STRING, _format_name,
219                         "framerate", GST_TYPE_FRACTION, 25, 1,
220                         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
221                         "width", G_TYPE_INT, __format->width,
222                         "height", G_TYPE_INT, __format->height,
223                         "framerate", GST_TYPE_FRACTION, 1, 1,
224                         NULL);
225         }
226
227         else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
228                 if(strcmp(__format->format_label,"RGB888") == 0) {
229                         _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
230                 }else if(strcmp(__format->format_label,"BGR888") == 0) {
231                         _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
232                 }else if(strcmp(__format->format_label,"RGB565") == 0) {
233                         _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
234                 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
235                         _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
236                 }
237
238                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
239                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
240
241                 __format->caps = gst_caps_new_simple ("video/x-raw",
242                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
243                         "width", G_TYPE_INT, __format->stride,
244                         "height", G_TYPE_INT, __format->elevation,
245                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
246         }
247
248         else if(strcmp(__format->colorspace,"RGBA") ==0) {
249                 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
250                         gstcs_debug("ARGB8888");
251                         _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
252                 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
253                         gstcs_debug("BGRA8888");
254                         _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
255                 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
256                         gstcs_debug("RGBA8888");
257                         _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
258                 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
259                         gstcs_debug("ABGR8888");
260                         _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
261                 }else {
262                         gstcs_error("***Wrong format cs type***\n");
263                 }
264
265                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
266                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
267
268                 __format->caps = gst_caps_new_simple ("video/x-raw",
269                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
270                         "width", G_TYPE_INT, __format->width,
271                         "height", G_TYPE_INT, __format->elevation,
272                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
273         }
274
275         if(__format->caps) {
276                 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
277                 _mm_check_caps_format(__format->caps);
278         }else {
279                 gstcs_error("__format->caps is NULL");
280         }
281 }
282
283 static void
284 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
285 {
286         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
287         GstVideoFormat videoFormat;
288         int _bpp=0; int _depth=0; int _red_mask=0; int _green_mask=0; int _blue_mask=0; int _alpha_mask=0; int _endianness=0;
289
290         if(__format == NULL) {
291                 gstcs_error("Image format is NULL\n");
292                 return;
293         }
294
295         gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
296         memset(_format_name, 0, sizeof(_format_name));
297
298         if(strcmp(__format->colorspace,"YUV") == 0) {
299                 if(strcmp(__format->format_label,"I420") == 0
300                 || strcmp(__format->format_label,"Y42B") == 0
301                 || strcmp(__format->format_label,"Y444") == 0
302                 || strcmp(__format->format_label,"YV12") == 0
303                 || strcmp(__format->format_label,"NV12") == 0
304                 || strcmp(__format->format_label,"UYVY") == 0) {
305                         strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
306                 }else if(strcmp(__format->format_label,"YUYV") == 0) {
307                         strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
308                 }
309                 gstcs_debug("Chosen video format: %s", _format_name);
310                 __format->caps = gst_caps_new_simple ("video/x-raw",
311                         "format", G_TYPE_STRING, _format_name,
312                         "width", G_TYPE_INT, __format->width,
313                         "height", G_TYPE_INT, __format->height,
314                         "framerate", GST_TYPE_FRACTION, 1, 1,
315                         NULL);
316         }
317
318         else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
319                 if(strcmp(__format->format_label,"RGB888") == 0) {
320                         _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
321                 }else if(strcmp(__format->format_label,"BGR888") == 0) {
322                         _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
323                 }else if(strcmp(__format->format_label,"RGB565") == 0) {
324                         _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
325                 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
326                         _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
327                 }
328
329                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
330                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
331
332                 __format->caps = gst_caps_new_simple ("video/x-raw",
333                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
334                         "width", G_TYPE_INT, __format->width,
335                         "height", G_TYPE_INT, __format->height,
336                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
337         }
338
339         else if(strcmp(__format->colorspace,"RGBA") ==0) {
340                 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
341                         gstcs_debug("ARGB8888");
342                         _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
343                 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
344                         gstcs_debug("BGRA8888");
345                         _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
346                 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
347                         gstcs_debug("RGBA8888");
348                         _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
349                 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
350                         gstcs_debug("ABGR8888");
351                         _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
352                 }else {
353                         gstcs_error("***Wrong format cs type***\n");
354                 }
355
356                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
357                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
358
359                 __format->caps = gst_caps_new_simple ("video/x-raw",
360                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
361                         "width", G_TYPE_INT, __format->width,
362                         "height", G_TYPE_INT, __format->height,
363                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
364         }
365
366         if(__format->caps) {
367                 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
368                 _mm_check_caps_format(__format->caps);
369         }else {
370                 gstcs_error("__format->caps is NULL");
371         }
372 }
373
374 static void
375 _mm_set_image_colorspace( image_format_s* __format)
376 {
377         gstcs_debug("format_label: %s\n", __format->format_label);
378
379         __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
380         if (__format->colorspace == NULL) {
381                 gstcs_error("memory allocation failed");
382                 return;
383         }
384         memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
385         if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
386                 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
387                 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
388         }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
389                 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
390         }else if( (strcmp(__format->format_label, "ARGB8888") == 0) || (strcmp(__format->format_label, "BGRA8888") == 0) ||(strcmp(__format->format_label, "RGBA8888") == 0)    || (strcmp(__format->format_label, "ABGR8888") == 0)) {
391                 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
392         }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
393                 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
394         }else {
395                 gstcs_error("Check your colorspace format label");
396                 GSTCS_FREE(__format->colorspace);
397         }
398 }
399
400 static void
401 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
402 {
403         if(strcmp(pFormat->colorspace,"YUV") == 0) {
404                 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
405                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
406         } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
407                 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
408                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
409         } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
410                 pFormat->stride = pFormat->width;
411                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
412         }
413         gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
414 }
415
416 static image_format_s*
417 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
418 {
419         image_format_s* __format = NULL;
420
421         __format=(image_format_s*)malloc(sizeof(image_format_s));
422         if (__format == NULL) {
423                 gstcs_error("memory allocation failed");
424                 return NULL;
425         }
426         memset(__format, 0, sizeof(image_format_s));
427
428         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
429         if (__format->format_label == NULL) {
430                 gstcs_error("memory allocation failed");
431                 GSTCS_FREE(__format);
432                 return NULL;
433         }
434         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
435         strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
436         gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
437         _mm_set_image_colorspace(__format);
438
439         __format->width=pImgp_info->src_width;
440         __format->height=pImgp_info->src_height;
441         _mm_round_up_input_image_widh_height(__format);
442
443         __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
444         gstcs_debug("blocksize: %d\n", __format->blocksize);
445         _mm_set_image_input_format_s_capabilities(__format);
446
447         return __format;
448 }
449
450 static void
451 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
452 {
453         if(strcmp(pFormat->colorspace,"YUV") == 0) {
454                 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
455                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
456         } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
457                 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
458                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
459                 pFormat->width = pFormat->stride;
460                 if (input_format->height != input_format->elevation)
461                         pFormat->height = pFormat->elevation;
462         } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
463                 pFormat->stride = pFormat->width;
464                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
465                 if (input_format->height != input_format->elevation)
466                         pFormat->height = pFormat->elevation;
467         }
468         gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
469 }
470
471 static image_format_s*
472 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
473 {
474         image_format_s* __format = NULL;
475
476         __format=(image_format_s*)malloc(sizeof(image_format_s));
477         if (__format == NULL) {
478                 gstcs_error("memory allocation failed");
479                 return NULL;
480         }
481         memset(__format, 0, sizeof(image_format_s));
482
483         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
484         if (__format->format_label == NULL) {
485                 gstcs_error("memory allocation failed");
486                 GSTCS_FREE(__format);
487                 return NULL;
488         }
489         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
490         strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
491         _mm_set_image_colorspace(__format);
492
493         __format->width=pImgp_info->dst_width;
494         __format->height=pImgp_info->dst_height;
495         _mm_round_up_output_image_widh_height(__format, input_format);
496
497         pImgp_info->output_stride = __format->stride;
498         pImgp_info->output_elevation = __format->elevation;
499
500         __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
501         gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
502         _mm_set_image_output_format_s_capabilities(__format);
503         return __format;
504 }
505
506 static int
507 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
508 {
509         int ret = GSTCS_ERROR_NONE;
510
511         if(pGstreamer_s->pipeline == NULL) {
512                 gstcs_error("pipeline is NULL\n");
513                 return GSTCS_ERROR_INVALID_PARAMETER;
514         }
515
516         gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
517         GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
518
519         if(gst_buf==NULL) {
520                 gstcs_error("buffer is NULL\n");
521                 return GSTCS_ERROR_INVALID_PARAMETER;
522         }
523
524         gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
525         return ret;
526 }
527
528 static int
529 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
530 {
531         int ret = GSTCS_ERROR_NONE;
532         GstBuffer *gst_buf = NULL;
533         unsigned int src_size = 0;
534         unsigned char *data = NULL;
535         unsigned int stride = input_format->stride;
536         unsigned int elevation = input_format->elevation;
537
538         if(pGstreamer_s->pipeline == NULL) {
539                 gstcs_error("pipeline is NULL\n");
540                 return GSTCS_ERROR_INVALID_PARAMETER;
541         }
542
543         gstcs_debug("stride: %d, elevation: %d", stride, elevation);
544         src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
545         gstcs_debug("buffer size (src): %d", src_size);
546
547         int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
548         unsigned int src_row = input_format->width * byte_per_pixcel;
549         unsigned int stride_row = stride * byte_per_pixcel;
550         unsigned int i = 0, y = 0;
551         gstcs_debug("padding will be inserted to buffer");
552         data =(unsigned char *) g_malloc (src_size);
553         if(data==NULL) {
554                 gstcs_error("app_buffer is NULL\n");
555                 return GSTCS_ERROR_INVALID_PARAMETER;
556         }
557         for (y = 0; y < (unsigned int)(input_format->height); y++) {
558                 guint8 *pLine = (guint8 *) &(src[src_row * y]);
559                 for(i = 0; i < src_row; i++) {
560                         data[y * stride_row + i] = pLine[i];
561                 }
562                 for(i = src_row; i < stride_row; i++) {
563                         data[y * stride_row + i] = 0x00;
564                 }
565         }
566         for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
567                 for(i = 0; i < stride_row; i++) {
568                         data[y * stride_row + i] = 0x00;
569                 }
570         }
571         gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
572
573         if(gst_buf==NULL) {
574                 gstcs_error("buffer is NULL\n");
575                 return GSTCS_ERROR_INVALID_PARAMETER;
576         }
577
578         gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
579         return ret;
580 }
581
582 static int
583 _mm_imgp_gstcs_processing( gstreamer_s* pGstreamer_s, unsigned char *src, unsigned char *dst, image_format_s* input_format, image_format_s* output_format, imgp_info_s* pImgp_info)
584 {
585         GstBus *bus = NULL;
586         GstStateChangeReturn ret_state;
587         int ret = GSTCS_ERROR_NONE;
588
589         if(src== NULL || dst == NULL) {
590                 gstcs_error("src || dst is NULL");
591                 return GSTCS_ERROR_INVALID_PARAMETER;
592         }
593
594         /*create pipeline*/
595         ret = _mm_create_pipeline(pGstreamer_s);
596         if(ret != GSTCS_ERROR_NONE) {
597                 gstcs_error("ERROR - mm_create_pipeline ");
598         }
599
600         pGstreamer_s->context = g_main_context_new();
601         if (pGstreamer_s->context == NULL) {
602                 gstcs_error("ERROR - g_main_context_new ");
603                 gst_object_unref (pGstreamer_s->pipeline);
604                 g_free (pGstreamer_s);
605                 return GSTCS_ERROR_INVALID_OPERATION;
606         }
607         pGstreamer_s->loop = g_main_loop_new (pGstreamer_s->context, FALSE);
608         if (pGstreamer_s->loop == NULL) {
609                 gstcs_error("ERROR - g_main_loop_new ");
610                 gst_object_unref (pGstreamer_s->pipeline);
611                 g_main_context_unref(pGstreamer_s->context);
612                 return GSTCS_ERROR_INVALID_OPERATION;
613         }
614
615         g_main_context_push_thread_default(pGstreamer_s->context);
616
617         /* Make appsink emit the "new-preroll" and "new-sample" signals. This option is by default disabled because signal emission is expensive and unneeded when the application prefers to operate in pull mode. */
618         gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
619
620         bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline));
621         gst_bus_add_watch (bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
622         gst_object_unref(bus);
623
624         gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
625         gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
626
627         if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
628                 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
629                 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
630                 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
631         } else {
632                 gstcs_debug("Start mm_push_buffer_into_pipeline");
633                 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
634         }
635         if(ret != GSTCS_ERROR_NONE) {
636                 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
637                 gst_object_unref (pGstreamer_s->pipeline);
638                 g_main_context_unref(pGstreamer_s->context);
639                 g_main_loop_unref(pGstreamer_s->loop);
640                 return ret;
641         }
642         gstcs_debug("End mm_push_buffer_into_pipeline");
643
644         /*link pipeline*/
645         gstcs_debug("Start mm_link_pipeline");
646         _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
647         gstcs_debug("End mm_link_pipeline");
648
649         /* Conecting to the new-sample signal emited by the appsink*/
650         gstcs_debug("Start G_CALLBACK (_mm_sink_sample)");
651         g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_sample), pGstreamer_s);
652         gstcs_debug("End G_CALLBACK (_mm_sink_sample)");
653
654         /* GST_STATE_PLAYING*/
655         gstcs_debug("Start GST_STATE_PLAYING");
656         ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
657         gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
658
659         /*g_main_loop_run*/
660         gstcs_debug("g_main_loop_run");
661         g_main_loop_run (pGstreamer_s->loop);
662
663         gstcs_debug("Sucess GST_STATE_CHANGE");
664
665         /*GST_STATE_NULL*/
666         gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
667         gstcs_debug("End GST_STATE_NULL");
668
669         gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
670
671         ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
672
673         if(ret_state == GST_STATE_CHANGE_SUCCESS) {
674                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
675         }else if( ret_state == GST_STATE_CHANGE_ASYNC) {
676                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
677         }
678
679         gstcs_debug("Success gst_element_get_state\n");
680
681         if (ret_state == GST_STATE_CHANGE_FAILURE) {
682                 gstcs_error("GST_STATE_CHANGE_FAILURE");
683         } else {
684                 if(pGstreamer_s->output_buffer != NULL) {
685                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
686                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
687                         int buffer_size = mapinfo.size;
688                         int calc_buffer_size = 0;
689                         if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
690                                 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
691                                 gstcs_debug("calculate image size with stride & elevation");
692                                 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
693                         } else {
694                                 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
695                         }
696                         gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
697                         if( buffer_size != calc_buffer_size) {
698                                 gstcs_debug("Buffer size is different \n");
699                                 gstcs_debug("unref output buffer");
700                                 gst_buffer_unref (pGstreamer_s->output_buffer);
701                                 gst_object_unref (pGstreamer_s->pipeline);
702                                 pGstreamer_s->output_buffer = NULL;
703                                 g_main_context_unref(pGstreamer_s->context);
704                                 g_main_loop_unref(pGstreamer_s->loop);
705                                 g_free (pGstreamer_s);
706                                 return GSTCS_ERROR_INVALID_OPERATION;
707                         }
708                         gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
709                         memcpy(dst, mapinfo.data, buffer_size);
710                         pImgp_info->buffer_size = buffer_size;
711                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
712                 }else {
713                         gstcs_debug("pGstreamer_s->output_buffer is NULL");
714                 }
715         }
716         gstcs_debug("unref output buffer");
717         gst_buffer_unref (pGstreamer_s->output_buffer);
718         gst_object_unref (pGstreamer_s->pipeline);
719         pGstreamer_s->output_buffer = NULL;
720         g_main_context_unref(pGstreamer_s->context);
721         g_main_loop_unref(pGstreamer_s->loop);
722
723         gstcs_debug("End gstreamer processing");
724         gstcs_debug("dst: %p", dst);
725         return ret;
726 }
727
728
729 static int
730 mm_setup_image_size(const char* _format_label, int width, int height)
731 {
732         int size=0;
733
734         if(strcmp(_format_label, "I420") == 0) {
735                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) + MM_UTIL_ROUND_UP_8(width) * MM_UTIL_ROUND_UP_2(height) /2); /*width * height *1.5; */
736         }else if(strcmp(_format_label, "Y42B") == 0) {
737                 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
738         }else if(strcmp(_format_label, "YUV422") == 0) {
739                 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
740         }else if(strcmp(_format_label, "Y444") == 0) {
741                 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
742         }else if(strcmp(_format_label, "YV12") == 0) {
743                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height)+ MM_UTIL_ROUND_UP_8(width) * MM_UTIL_ROUND_UP_2(height) / 2); /* width * height *1; */
744         }else if(strcmp(_format_label, "NV12") == 0) {
745                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
746         }else if(strcmp(_format_label, "RGB565") == 0) {
747                 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
748         }else if(strcmp(_format_label, "RGB888") == 0) {
749                 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
750         }else if(strcmp(_format_label, "BGR888") == 0) {
751                 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
752         }else if(strcmp(_format_label, "UYVY") == 0) {
753                 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
754         }else if(strcmp(_format_label, "YUYV") == 0) {
755                 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
756         }else if(strcmp(_format_label, "ARGB8888") == 0) {
757                 size = width * height *4;
758         }else if(strcmp(_format_label, "BGRA8888") == 0) {
759                 size = width * height *4;
760         }else if(strcmp(_format_label, "RGBA8888") == 0) {
761                 size = width * height *4;
762         }else if(strcmp(_format_label, "ABGR8888") == 0) {
763                 size = width * height *4;
764         }else if(strcmp(_format_label, "BGRX") == 0) {
765                 size = width * height *4;
766         }
767
768         gstcs_debug("file_size: %d\n", size);
769
770         return size;
771 }
772
773 static int
774 _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
775 {
776         image_format_s* input_format=NULL, *output_format=NULL;
777         gstreamer_s* pGstreamer_s;
778         int ret = GSTCS_ERROR_NONE;
779         static const int max_argc = 50;
780         gint* argc = NULL;
781         gchar** argv = NULL;
782         int i = 0;
783
784         if(pImgp_info == NULL) {
785                 gstcs_error("imgp_info_s is NULL");
786                 return GSTCS_ERROR_INVALID_PARAMETER;
787         }
788
789         if(src== NULL || dst == NULL) {
790                 gstcs_error("src || dst is NULL");
791                 return GSTCS_ERROR_INVALID_PARAMETER;
792         }
793         gstcs_debug("[src %p] [dst %p]", src, dst);
794
795         argc = malloc(sizeof(int));
796         argv = malloc(sizeof(gchar*) * max_argc);
797
798         if (!argc || !argv) {
799                 gstcs_error("argc ||argv is NULL");
800                 GSTCS_FREE(input_format);
801                 GSTCS_FREE(output_format);
802                 if (argc != NULL) {
803                         free(argc);
804                 }
805                 if (argv != NULL) {
806                         free(argv);
807                 }
808                 return GSTCS_ERROR_INVALID_PARAMETER;
809         }
810         memset(argv, 0, sizeof(gchar*) * max_argc);
811         gstcs_debug("memset argv");
812
813         /* add initial */
814         *argc = 0;
815         argv[*argc] = (gchar *)g_strdup( "mmutil_gstcs" );
816         (*argc)++;
817         /* check disable registry scan */
818         argv[*argc] = g_strdup("--gst-disable-registry-update");
819         (*argc)++;
820         gstcs_debug("--gst-disable-registry-update");
821
822         gst_init(argc, &argv);
823
824         pGstreamer_s = g_new0 (gstreamer_s, 1);
825
826         for (i = 0; i < (*argc); i++) {
827                 GSTCS_FREE(argv[i]);
828         }
829         GSTCS_FREE(argv);
830         GSTCS_FREE(argc);
831
832         gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
833         pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height, pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height, pImgp_info->angle);
834
835         input_format= _mm_set_input_image_format_s_struct(pImgp_info);
836         if (input_format == NULL) {
837                 gstcs_error("memory allocation failed");
838                 g_free (pGstreamer_s);
839                 return GSTCS_ERROR_OUT_OF_MEMORY;
840         }
841         output_format= _mm_set_output_image_format_s_struct(pImgp_info, input_format);
842         if (output_format == NULL) {
843                 gstcs_error("memory allocation failed");
844                 GSTCS_FREE(input_format->format_label);
845                 GSTCS_FREE(input_format->colorspace);
846                 GSTCS_FREE(input_format);
847                 g_free (pGstreamer_s);
848                 return GSTCS_ERROR_OUT_OF_MEMORY;
849         }
850
851         /* _format_label : I420, RGB888 etc*/
852         gstcs_debug("Start _mm_imgp_gstcs_processing ");
853         ret =_mm_imgp_gstcs_processing(pGstreamer_s, src, dst, input_format, output_format, pImgp_info); /* input: buffer pointer for input image , input image format, input image width, input image height, output: buffer porinter for output image */
854
855         if(ret == GSTCS_ERROR_NONE) {
856                 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
857         }else if (ret != GSTCS_ERROR_NONE) {
858                 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
859         }
860
861         GSTCS_FREE(input_format->format_label);
862         GSTCS_FREE(input_format->colorspace);
863         GSTCS_FREE(input_format);
864
865         GSTCS_FREE(output_format->format_label);
866         GSTCS_FREE(output_format->colorspace);
867         GSTCS_FREE(output_format);
868         g_free (pGstreamer_s);
869
870         return ret;
871 }
872
873 int
874 mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
875 {
876         if (pImgp_info == NULL) {
877                 gstcs_error("input vaule is error");
878         }
879         return _mm_imgp_gstcs(pImgp_info, src, dst);
880 }