summaryrefslogtreecommitdiff
path: root/gstcs/mm_util_gstcs.c
blob: 03a9136bcb33e17fa6586d748a1d5c5d0052ab5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*
 * libmm-imgp-gstcs
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <stdbool.h>
#include "mm_util_gstcs_internal.h"
#include <gst/video/video-format.h>

#define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
#define MM_UTIL_ROUND_UP_4(num) (((num)+3)&~3)
#define MM_UTIL_ROUND_UP_8(num) (((num)+7)&~7)

#define SAFE_UNREF_CAPS(caps)	{ if (caps)	{ gst_caps_unref(caps); caps = NULL; } }


static GstFlowReturn
_mm_sink_sample(GstElement * appsink, gpointer user_data)
{
	GstBuffer *_buf = NULL;
	GstSample *_sample = NULL;
	gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
	_sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
	if (_sample) {
		_buf = gst_sample_get_buffer(_sample);

		pGstreamer_s->output_buffer = _buf;

		if (pGstreamer_s->output_buffer != NULL) {
			GstMapInfo mapinfo = GST_MAP_INFO_INIT;
			gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
			gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %zu", mapinfo.data, mapinfo.size);
			gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
		} else {
			gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
		}
	}

	gst_buffer_ref(pGstreamer_s->output_buffer); /* when you want to avoid flushing */
	gst_sample_unref(_sample);

	return GST_FLOW_OK;
}

static gboolean
_mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
{
	gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
	switch (GST_MESSAGE_TYPE(message)) {
	case GST_MESSAGE_EOS: {
		gstcs_debug("The source got dry");
		gst_app_src_end_of_stream(GST_APP_SRC(pGstreamer_s->appsrc));
		g_main_context_pop_thread_default(pGstreamer_s->context);
		g_main_loop_quit(pGstreamer_s->loop);
		break;
		}
	case GST_MESSAGE_ERROR: {
		GError *err = NULL;
		gchar *dbg_info = NULL;

		gst_message_parse_error(message, &err, &dbg_info);
		gstcs_error("ERROR from element %s: %s\n", GST_OBJECT_NAME(message->src), err->message);
		gstcs_error("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
		g_error_free(err);
		g_free(dbg_info);
		g_main_context_pop_thread_default(pGstreamer_s->context);
		g_main_loop_quit(pGstreamer_s->loop);
		gstcs_debug("Quit GST_CS\n");
		break;
		}
	default:
		break;
	}
	return TRUE;
}

static int _mm_get_byte_per_pixcel(mm_util_color_format_e color_format)
{
	int byte_per_pixcel = 1;

	switch (color_format) {
	case MM_UTIL_COLOR_YUV420:
	case MM_UTIL_COLOR_YUV422:
	case MM_UTIL_COLOR_I420:
	case MM_UTIL_COLOR_NV12:
	case MM_UTIL_COLOR_UYVY:
	case MM_UTIL_COLOR_YUYV:
		byte_per_pixcel = 1;
		break;
	case MM_UTIL_COLOR_RGB16:
		byte_per_pixcel = 2;
		break;
	case MM_UTIL_COLOR_RGB24:
		byte_per_pixcel = 3;
		break;
	case MM_UTIL_COLOR_ARGB:
	case MM_UTIL_COLOR_BGRA:
	case MM_UTIL_COLOR_RGBA:
	case MM_UTIL_COLOR_BGRX:
		byte_per_pixcel = 4;
		break;
	default:
		gstcs_error("Not supported format");
		break;
	}

	gstcs_debug("color_format [%d] byte per pixcel [%d]", color_format, byte_per_pixcel);

	return byte_per_pixcel;
}

static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
{
	int ret = GSTCS_ERROR_NONE;
	pGstreamer_s->pipeline = gst_pipeline_new("pipeline");
	if (!pGstreamer_s->pipeline) {
		gstcs_error("pipeline could not be created. Exiting.\n");
		return GSTCS_ERROR_INVALID_PARAMETER;
	}
	pGstreamer_s->appsrc = gst_element_factory_make("appsrc" , "appsrc");
	if (!pGstreamer_s->appsrc) {
		gstcs_error("appsrc could not be created. Exiting.\n");
		gst_object_unref(pGstreamer_s->pipeline);
		return GSTCS_ERROR_INVALID_PARAMETER;
	}
	pGstreamer_s->colorspace = gst_element_factory_make("videoconvert" , "convert");
	if (!pGstreamer_s->colorspace) {
		gstcs_error("colorspace could not be created. Exiting.\n");
		gst_object_unref(pGstreamer_s->pipeline);
		return GSTCS_ERROR_INVALID_PARAMETER;
	}
	pGstreamer_s->videoscale = gst_element_factory_make("videoscale", "scale");
	if (!pGstreamer_s->videoscale) {
		gstcs_error("videoscale could not be created. Exiting.\n");
		gst_object_unref(pGstreamer_s->pipeline);
		return GSTCS_ERROR_INVALID_PARAMETER;
	}
	pGstreamer_s->videoflip = gst_element_factory_make("videoflip", "flip");
	if (!pGstreamer_s->videoflip) {
		gstcs_error("videoflip could not be created. Exiting.\n");
		gst_object_unref(pGstreamer_s->pipeline);
		return GSTCS_ERROR_INVALID_PARAMETER;
	}
	pGstreamer_s->appsink = gst_element_factory_make("appsink" , "appsink");
	if (!pGstreamer_s->appsink) {
		gstcs_error("appsink could not be created. Exiting.\n");
		gst_object_unref(pGstreamer_s->pipeline);
		return GSTCS_ERROR_INVALID_PARAMETER;
	}
	return ret;
}

static void _mm_destroy_notify(gpointer data)
{
	unsigned char *_data = (unsigned char *)data;
	if (_data != NULL) {
		free(_data);
		_data = NULL;
	}
}

static void
_mm_dump_caps_format(GstCaps* caps)
{
	GstStructure *caps_structure = gst_caps_get_structure(caps, 0);
	const gchar* formatInfo = gst_structure_get_string(caps_structure, "format");
	gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
}

static void
_mm_link_pipeline(gstreamer_s* pGstreamer_s, int value)
{
	gstcs_fenter();

	/* set property */
	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);
	if (!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL))
		gstcs_error("Fail to link pipeline");
	else
		gstcs_debug("Success to link pipeline");

	g_object_set(G_OBJECT(pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
	g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
	g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */

	/* Values are 'GstVideoOrientationMethod' in gst-plugins-base/gst-libs/gst/video/video.h */
	g_object_set(pGstreamer_s->videoflip, "video-direction", value, NULL);

	g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);

	gstcs_fleave();
}

static GstVideoFormat _mm_get_video_format(mm_util_color_format_e color_format)
{
	GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
	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;

	switch (color_format) {
	case MM_UTIL_COLOR_YUV420:
		videoFormat = GST_VIDEO_FORMAT_YV12;
		break;
	case MM_UTIL_COLOR_YUV422:
		videoFormat = GST_VIDEO_FORMAT_Y42B;
		break;
	case MM_UTIL_COLOR_I420:
		videoFormat = GST_VIDEO_FORMAT_I420;
		break;
	case MM_UTIL_COLOR_NV12:
		videoFormat = GST_VIDEO_FORMAT_NV12;
		break;
	case MM_UTIL_COLOR_UYVY:
		videoFormat = GST_VIDEO_FORMAT_UYVY;
		break;
	case MM_UTIL_COLOR_YUYV:
		videoFormat = GST_VIDEO_FORMAT_YVYU;
		break;
	case MM_UTIL_COLOR_RGB16:
		_bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234; _alpha_mask = 0;
		videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
		break;
	case MM_UTIL_COLOR_RGB24:
		_bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321; _alpha_mask = 0;
		videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
		break;
	case MM_UTIL_COLOR_ARGB:
		_bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
		videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
		break;
	case MM_UTIL_COLOR_BGRA:
		_bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
		videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
		break;
	case MM_UTIL_COLOR_RGBA:
		_bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
		videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
		break;
	case MM_UTIL_COLOR_BGRX:
		_bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321; _alpha_mask = 0;
		videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
		break;
	default:
		gstcs_error("Not supported format");
		break;
	}

	gstcs_debug("color_format [%d], Chosen video format [%s]", color_format, gst_video_format_to_string(videoFormat));

	return videoFormat;

}

static GstCaps* _mm_get_capabilities(mm_util_color_format_e color_format, unsigned int width, unsigned int height)
{
	GstCaps *caps = NULL;
	GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;

	videoFormat = _mm_get_video_format(color_format);
	gstcs_retvm_if(videoFormat == GST_VIDEO_FORMAT_UNKNOWN, NULL, "Unkown video format (%d)", color_format);

	caps = gst_caps_new_simple("video/x-raw",
			"format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
			"framerate", GST_TYPE_FRACTION, 25, 1,
			"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
			"width", G_TYPE_INT, width,
			"height", G_TYPE_INT, height,
			"framerate", GST_TYPE_FRACTION, 1, 1,
			NULL);

	_mm_dump_caps_format(caps);

	return caps;
}

static void _mm_get_round_up_width_height(mm_util_color_format_e color_format, unsigned int width, unsigned int height,
		mm_util_img_rotate_type_e rotate, bool is_src, unsigned int *stride, unsigned int *elevation)
{
	switch (color_format) {
	case MM_UTIL_COLOR_YUV420:
	case MM_UTIL_COLOR_YUV422:
	case MM_UTIL_COLOR_I420:
	case MM_UTIL_COLOR_NV12:
	case MM_UTIL_COLOR_UYVY:
	case MM_UTIL_COLOR_YUYV:
		*stride = width;
		*elevation = height;
		break;
	case MM_UTIL_COLOR_RGB16:
	case MM_UTIL_COLOR_RGB24:
			*stride = MM_UTIL_ROUND_UP_4(width);
		if (is_src && (rotate == MM_UTIL_ROTATE_90 || rotate == MM_UTIL_ROTATE_270))
			*elevation = MM_UTIL_ROUND_UP_4(height);
		else
			*elevation = MM_UTIL_ROUND_UP_2(height);
		break;
	case MM_UTIL_COLOR_ARGB:
	case MM_UTIL_COLOR_BGRA:
	case MM_UTIL_COLOR_RGBA:
	case MM_UTIL_COLOR_BGRX:
		*stride = width;
		*elevation = MM_UTIL_ROUND_UP_2(height);
		break;
	default:
		gstcs_error("Not supported format");
		break;
	}

	gstcs_debug("color_format[%d] width[%u] height[%u] stride[%u], elevation[%u]", color_format, width, height, *stride, *elevation);
}

static size_t _mm_setup_image_size(mm_util_color_format_e color_format, unsigned int width, unsigned int height)
{
	size_t size = 0;
	size_t _width = (size_t)width;

	gstcs_debug("color_format [%d] width [%u] height [%u]", color_format, width, height);

	switch (color_format) {
	case MM_UTIL_COLOR_YUV420:
		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; */
		break;
	case MM_UTIL_COLOR_YUV422:
		size = (MM_UTIL_ROUND_UP_4(_width) * height + MM_UTIL_ROUND_UP_8(_width) * height); /*width * height *2; */
		break;
	case MM_UTIL_COLOR_I420:
		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; */
		break;
	case MM_UTIL_COLOR_NV12:
		size = (MM_UTIL_ROUND_UP_4(_width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
		break;
	case MM_UTIL_COLOR_UYVY:
		size = (MM_UTIL_ROUND_UP_2(_width) * 2 * height); /* width * height *2; */
		break;
	case MM_UTIL_COLOR_YUYV:
		size = (MM_UTIL_ROUND_UP_2(_width) * 2 * height); /* width * height *2; */
		break;
	case MM_UTIL_COLOR_RGB16:
		size = (MM_UTIL_ROUND_UP_4(_width) * 2 * height); /* width * height *2; */
		break;
	case MM_UTIL_COLOR_RGB24:
		size = (MM_UTIL_ROUND_UP_4(_width) * 3 * height); /* width * height *3; */
		break;
	case MM_UTIL_COLOR_ARGB:
	case MM_UTIL_COLOR_BGRA:
	case MM_UTIL_COLOR_RGBA:
	case MM_UTIL_COLOR_BGRX:
		size = _width * height * 4;
		break;
	default:
		gstcs_error("Not supported format");
		break;
	}


	gstcs_debug("Image size [%zu]", size);

	return size;
}

static int _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
{
	size_t data_size = 0;
	GstBuffer* gst_buf = NULL;

	gstcs_fenter();

	gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline");

	data_size = _mm_setup_image_size(pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height);

	gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);

	gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */

	gstcs_fleave();

	return GSTCS_ERROR_NONE;
}

static int _mm_push_buffer_into_pipeline_new(unsigned char *src, gstreamer_s * pGstreamer_s, mm_util_color_format_e color_format, unsigned int width, unsigned int height, unsigned int stride, unsigned int elevation)
{
	GstBuffer *gst_buf = NULL;
	size_t src_size = 0;
	unsigned char *data = NULL;

	gstcs_fenter();

	gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline");
	gstcs_retvm_if((width == 0 || height == 0), GSTCS_ERROR_INVALID_PARAMETER, "Invalid width(%u) and height(%u)", width, height);

	src_size = _mm_setup_image_size(color_format, stride, elevation);

	int byte_per_pixcel = _mm_get_byte_per_pixcel(color_format);
	unsigned int src_row = width * byte_per_pixcel;
	unsigned int stride_row = stride * byte_per_pixcel;
	unsigned int i = 0, y = 0;

	data = g_malloc0(src_size);

	for (y = 0; y < height; y++) {
		guint8 *pLine = (guint8 *) &(src[src_row * y]);
		for (i = 0; i < src_row; i++)
			data[y * stride_row + i] = pLine[i];
		guint8 stride_row_color = pLine[i - 1];
		for (i = src_row; i < stride_row; i++)
			data[y * stride_row + i] = stride_row_color;
	}

	for (y = height; y < elevation; y++) {
		for (i = 0; i < stride_row; i++)
			data[y * stride_row + i] = data[(y - 1) * stride_row + i];
	}

	gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);

	gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */

	gstcs_fleave();

	return GSTCS_ERROR_NONE;
}

static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *src, unsigned char **dst, imgp_info_s* pImgp_info)
{
	GstBus *bus = NULL;
	GstStateChangeReturn ret_state;
	int ret = GSTCS_ERROR_NONE;
	GstCaps *src_caps = NULL;
	GstCaps *dst_caps = NULL;
	unsigned int src_stride = 0;
	unsigned int src_elevation = 0;

	gstcs_fenter();

	/*create pipeline*/
	ret = _mm_create_pipeline(pGstreamer_s);
	if (ret != GSTCS_ERROR_NONE) {
		gstcs_error("ERROR - mm_create_pipeline ");
		return ret;
	}

	/* 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. */
	gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);

	bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
	gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
	gst_object_unref(bus);

	_mm_get_round_up_width_height(pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height, pImgp_info->angle, true,
			&src_stride, &src_elevation);
	_mm_get_round_up_width_height(pImgp_info->dst_format, pImgp_info->dst_width, pImgp_info->dst_height, pImgp_info->angle, false,
			&pImgp_info->output_stride, &pImgp_info->output_elevation);

	src_caps = _mm_get_capabilities(pImgp_info->src_format, src_stride, src_elevation);
	dst_caps = _mm_get_capabilities(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation);
	if (src_caps == NULL || dst_caps == NULL) {
		gstcs_error("ERROR - _mm_get_capabilities ");
		SAFE_UNREF_CAPS(src_caps);
		SAFE_UNREF_CAPS(dst_caps);
		gst_object_unref(pGstreamer_s->pipeline);
		return ret;
	}

	gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), src_caps);
	if (pImgp_info->angle == MM_UTIL_ROTATE_0)
		gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), dst_caps);
	else
		gstcs_debug("Don't set the caps of app_sink when angle is %d", pImgp_info->angle);

	if ((pImgp_info->src_width != src_stride) || (pImgp_info->src_height != src_elevation)) {
		ret = _mm_push_buffer_into_pipeline_new(src, pGstreamer_s, pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height, src_stride, src_elevation);
	} else {
		ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
	}

	if (ret != GSTCS_ERROR_NONE) {
		gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
		SAFE_UNREF_CAPS(src_caps);
		SAFE_UNREF_CAPS(dst_caps);
		gst_object_unref(pGstreamer_s->pipeline);
		return ret;
	}

	/*link pipeline*/
	_mm_link_pipeline(pGstreamer_s, pImgp_info->angle);

	/* Conecting to the new-sample signal emited by the appsink*/
	gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
	g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
	gstcs_debug("End G_CALLBACK(_mm_sink_sample)");

	/* GST_STATE_PLAYING*/
	gstcs_debug("Start GST_STATE_PLAYING");
	ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
	gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);

	/*g_main_loop_run*/
	gstcs_debug("g_main_loop_run");
	g_main_loop_run(pGstreamer_s->loop);

	gstcs_debug("Sucess GST_STATE_CHANGE");

	/*GST_STATE_NULL*/
	gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
	gstcs_debug("End GST_STATE_NULL");

	gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);

	ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);

	if (ret_state == GST_STATE_CHANGE_SUCCESS)
		gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
	else if (ret_state == GST_STATE_CHANGE_ASYNC)
		gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);

	gstcs_debug("Success gst_element_get_state\n");

	SAFE_UNREF_CAPS(src_caps);
	SAFE_UNREF_CAPS(dst_caps);

	if (ret_state == GST_STATE_CHANGE_FAILURE) {
		gstcs_error("GST_STATE_CHANGE_FAILURE");
	} else {
		if (pGstreamer_s->output_buffer != NULL) {
			GstMapInfo mapinfo = GST_MAP_INFO_INIT;
			gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
			size_t buffer_size = mapinfo.size;
			size_t calc_buffer_size = 0;

			calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation);

			gstcs_debug("buffer size[%zu], calc[%zu]", buffer_size, calc_buffer_size);
			if (buffer_size != calc_buffer_size) {
				gstcs_debug("Buffer size is different \n");
				gstcs_debug("unref output buffer");
				gst_buffer_unref(pGstreamer_s->output_buffer);
				gst_object_unref(pGstreamer_s->pipeline);
				pGstreamer_s->output_buffer = NULL;
				return GSTCS_ERROR_INVALID_OPERATION;
			}
			gstcs_debug("pGstreamer_s->output_buffer: %p\n", pGstreamer_s->output_buffer);

			*dst = g_memdup2(mapinfo.data, buffer_size);
			pImgp_info->buffer_size = buffer_size;
			gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
		} else {
			gstcs_debug("pGstreamer_s->output_buffer is NULL");
		}
	}
	gstcs_debug("unref output buffer");
	gst_buffer_unref(pGstreamer_s->output_buffer);
	gst_object_unref(pGstreamer_s->pipeline);
	pGstreamer_s->output_buffer = NULL;

	gstcs_debug("dst: %p", *dst);
	gstcs_fleave();

	return ret;
}

static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
{
	gstcs_retvm_if(gstreamer == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid gstreamer");

	gstreamer->context = g_main_context_new();

	gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);

	g_main_context_push_thread_default(gstreamer->context);

	return GSTCS_ERROR_NONE;
}

static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
{
	gstcs_retvm_if(gstreamer == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid gstreamer");

	if (gstreamer->loop != NULL)
		g_main_loop_unref(gstreamer->loop);

	if (gstreamer->context != NULL)
		g_main_context_unref(gstreamer->context);

	return GSTCS_ERROR_NONE;
}

static void _gstcs_init(void)
{
	static const int max_argc = 50;
	gint argc = 0;
	gchar** argv = NULL;
	int i = 0;

	argv = g_malloc0(sizeof(gchar*) * max_argc);

	argv[argc] = g_strdup("mmutil_gstcs");
	argc++;
	/* check disable registry scan */
	argv[argc] = g_strdup("--gst-disable-registry-update");
	argc++;

	gst_init(&argc, &argv);

	for (i = 0; i < argc; i++)
		g_free(argv[i]);

	g_free(argv);
}

static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst)
{
	gstreamer_s* pGstreamer_s = NULL;
	int ret = GSTCS_ERROR_NONE;

	/* Print debug message for inout structure */
	gstcs_debug("src_width [%d] src_height [%d] src_format [%d] dst_width [%d] dst_height [%d] dst_format [%d] rotation [%d]",
	pImgp_info->src_width, pImgp_info->src_height, pImgp_info->src_format, pImgp_info->dst_width, pImgp_info->dst_height, pImgp_info->dst_format, pImgp_info->angle);

	/* Initialize gstreamer */
	_gstcs_init();

	pGstreamer_s = g_new0(gstreamer_s, 1);

	/* Create default thread for async behavior */
	ret = _gstcs_create_default_thread(pGstreamer_s);
	if (ret != GSTCS_ERROR_NONE) {
		gstcs_error("Error: _gstcs_create_default_thread is failed");
		g_free(pGstreamer_s);
		return ret;
	}

	/* Do gstreamer processing */
	ret = _mm_imgp_gstcs_processing(pGstreamer_s, src, dst, pImgp_info); /* input: buffer pointer for input image , input image format, input image width, input image height, output: buffer porinter for output image */

	if (ret == GSTCS_ERROR_NONE)
		gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", *dst);
	else if (ret != GSTCS_ERROR_NONE)
		gstcs_error("ERROR - _mm_imgp_gstcs_processing");

	/* Free resouces */
	ret = _gstcs_destroy_default_thread(pGstreamer_s);
	if (ret != GSTCS_ERROR_NONE)
		gstcs_error("Error: _gstcs_create_default_thread is failed");

	g_free(pGstreamer_s);

	return ret;
}

static bool _mm_imgp_check_format(mm_util_color_format_e color_format)
{
	if ((color_format >= MM_UTIL_COLOR_YUV420) && (color_format <= MM_UTIL_COLOR_BGRX))
		return TRUE;

	return FALSE;
}

int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst, imgp_type_e _imgp_type)
{
	gstcs_retvm_if(pImgp_info == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid info");
	gstcs_retvm_if(src == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid src");
	gstcs_retvm_if(dst == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid dst");
	gstcs_retvm_if((_imgp_type < 0 || _imgp_type > IMGP_MAX), GSTCS_ERROR_INVALID_PARAMETER, "Invalid _imgp_type[%d]", _imgp_type);
	gstcs_retvm_if((_mm_imgp_check_format(pImgp_info->src_format) == FALSE), GSTCS_ERROR_NOT_SUPPORTED_FORMAT, "not supported src_format [%d]", pImgp_info->src_format);
	gstcs_retvm_if((_mm_imgp_check_format(pImgp_info->dst_format) == FALSE), GSTCS_ERROR_NOT_SUPPORTED_FORMAT, "not supported dst_format [%d]", pImgp_info->dst_format);

	gstcs_debug("[src %p] [dst %p]", src, dst);

	return _mm_imgp_gstcs(pImgp_info, src, dst);
}