4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "mm_util_gstcs_internal.h"
23 #include <gst/check/gstcheck.h>
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 #define setup_image_size_I420(width, height) { \
32 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); \
36 #define setup_image_size_Y42B(width, height) { \
38 size = (MM_UTIL_ROUND_UP_4 (width) * height + MM_UTIL_ROUND_UP_8 (width) * height); \
42 #define setup_image_size_Y444(width, height) { \
44 size = (MM_UTIL_ROUND_UP_4 (width) * height * 3); \
48 #define setup_image_size_UYVY(width, height) { \
50 size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
54 #define setup_image_size_YUYV(width, height) { \
56 size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
60 #define setup_image_size_YV12(width, height) { \
62 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); \
66 #define setup_image_size_NV12(width, height) { \
68 size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5); \
72 #define setup_image_size_RGB565(width, height) { \
74 size = (MM_UTIL_ROUND_UP_4 (width * 2) *height); \
78 #define setup_image_size_RGB888(width, height) { \
80 size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
84 #define setup_image_size_BGR888(width, height) { \
86 size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
89 /*########################################################################################*/
92 _mm_sink_buffer (GstElement * appsink, gpointer user_data)
95 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
96 _buf = gst_app_sink_pull_buffer((GstAppSink*)appsink);
98 pGstreamer_s->output_buffer = _buf;
100 if(pGstreamer_s->output_buffer != NULL) {
101 debug_log("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n", GST_BUFFER_DATA(pGstreamer_s->output_buffer), GST_BUFFER_SIZE (pGstreamer_s->output_buffer));
103 debug_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
105 gst_buffer_unref (_buf); /* we don't need the appsink buffer anymore */
106 gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushing */
110 _mm_sink_preroll (GstElement * appsink, gpointer user_data)
112 GstBuffer *_buf=NULL;
113 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
114 _buf = gst_app_sink_pull_preroll((GstAppSink*)appsink);
116 pGstreamer_s->output_buffer = _buf;
117 /* pGstreamer_s->output_image_format_s->caps = GST_BUFFER_CAPS(_buf); */
118 if(pGstreamer_s->output_buffer != NULL) {
119 debug_log("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n",
120 GST_BUFFER_DATA(pGstreamer_s->output_buffer), GST_BUFFER_SIZE (pGstreamer_s->output_buffer));
122 debug_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
125 gst_buffer_unref (_buf); /* we don't need the appsink buffer anymore */
126 gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushings */
130 _mm_on_sink_message (GstBus * bus, GstMessage * message, gstreamer_s * pGstreamer_s)
132 switch (GST_MESSAGE_TYPE (message)) {
133 case GST_MESSAGE_EOS:
134 debug_log("Finished playback\n"); /* g_main_loop_quit (pGstreamer_s->loop); */
136 case GST_MESSAGE_ERROR:
137 debug_error("Received error\n"); /* g_main_loop_quit (pGstreamer_s->loop); */
139 case GST_MESSAGE_STATE_CHANGED:
140 debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
142 case GST_MESSAGE_STREAM_STATUS:
143 debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
146 debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
154 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
156 int ret = MM_ERROR_NONE;
157 pGstreamer_s->pipeline= gst_pipeline_new ("ffmpegcolorsapce");
158 pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
159 pGstreamer_s->colorspace=gst_element_factory_make("ffmpegcolorspace","colorconverter");
161 pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
162 pGstreamer_s->videoflip=gst_element_factory_make( "videoflip", "flip" );
164 pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
166 if (!pGstreamer_s->pipeline || !pGstreamer_s->appsrc||!pGstreamer_s->colorspace || !pGstreamer_s->appsink) {
167 debug_error("One element could not be created. Exiting.\n");
168 ret = MM_ERROR_IMAGE_INVALID_VALUE;
174 _mm_check_resize_format(int src_width, int src_height, int dst_width, int dst_height)
176 gboolean _bool=FALSE;
178 if( (src_width != dst_width) || (src_height != dst_height) ) {
186 _mm_check_rotate_format(int GstVideoFlipMethod)
188 gboolean _bool=FALSE;
190 if((GstVideoFlipMethod >= 1) && ( GstVideoFlipMethod<= 7)) {
198 _mm_check_resize_format_label(char* __format_label)
200 gboolean _bool = FALSE;
202 if(strcmp(__format_label, "AYUV") == 0
203 || strcmp(__format_label, "UYVY") == 0 ||strcmp(__format_label, "Y800") == 0 || strcmp(__format_label, "I420") == 0 || strcmp(__format_label, "YV12") == 0
204 || strcmp(__format_label, "RGB888") == 0 || strcmp(__format_label, "RGB565") == 0 || strcmp(__format_label, "BGR888") == 0 || strcmp(__format_label, "RGBA8888") == 0
205 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ||strcmp(__format_label, "RGBX") == 0
206 ||strcmp(__format_label, "XRGB") == 0 ||strcmp(__format_label, "BGRX") == 0 ||strcmp(__format_label, "XBGR") == 0 ||strcmp(__format_label, "Y444") == 0
207 ||strcmp(__format_label, "Y42B") == 0 ||strcmp(__format_label, "YUY2") == 0 ||strcmp(__format_label, "YUYV") == 0 ||strcmp(__format_label, "UYVY") == 0
208 ||strcmp(__format_label, "Y41B") == 0 ||strcmp(__format_label, "Y16") == 0 ||strcmp(__format_label, "Y800") == 0 ||strcmp(__format_label, "Y8") == 0
209 ||strcmp(__format_label, "GREY") == 0 ||strcmp(__format_label, "AY64") == 0 || strcmp(__format_label, "YUV422") == 0) {
218 _mm_check_rotate_format_label(const char* __format_label)
220 gboolean _bool = FALSE;
222 if(strcmp(__format_label, "I420") == 0 ||strcmp(__format_label, "YV12") == 0 || strcmp(__format_label, "IYUV")
223 || strcmp(__format_label, "RGB888") ||strcmp(__format_label, "BGR888") == 0 ||strcmp(__format_label, "RGBA8888") == 0
224 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ) {
232 _mm_link_pipeline_order_csc_rsz(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format)
234 if(_mm_check_resize_format(input_format->width,input_format->height, output_format->width, output_format->height)) {
235 debug_log("check_for_resize");
236 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
237 debug_log("gst_bin_add_many");
238 if(_mm_check_resize_format_label( input_format->format_label)) {
239 debug_log(" input_format->format_label: %s", input_format->format_label);
240 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
241 debug_error("Fail to link b/w ffmpeg and appsink except rot\n");
243 }else if(_mm_check_resize_format_label(output_format->format_label)) {
244 debug_log("output_format->format_label: %s", output_format->format_label);
245 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->appsink, NULL)) {
246 debug_error("Fail to link b/w ffmpeg and appsink except rot\n");
250 debug_log("check_for_convert");
251 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
252 debug_log("gst_bin_add_many");
253 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
254 debug_error("Fail to link b/w ffmpeg and appsink except rsz & rot\n");
260 _mm_link_pipeline_order_csc_rsz_rot(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format)
262 if(_mm_check_rotate_format_label(input_format->format_label)) {
263 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
264 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
265 debug_error("Fail to link b/w appsrc and ffmpeg in rotate\n");
267 }else if(_mm_check_rotate_format_label(output_format->format_label)) {
268 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);
269 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL)) {
270 debug_error("Fail to link b/w ffmpeg and appsink in rotate\n");
277 _mm_link_pipeline( gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int _valuepGstreamer_sVideoFlipMethod)
280 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps); /* g_object_set(pGstreamer_s->appsrc, "caps", input_format->caps, NULL); /* you can use appsrc'cap property */
281 g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
282 g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
283 g_object_set (pGstreamer_s->appsrc, "format", GST_FORMAT_TIME, NULL);
284 g_object_set(pGstreamer_s->appsrc, "stream-type", 0 /*stream*/, NULL);
286 g_object_set(pGstreamer_s->videoflip, "method", _valuepGstreamer_sVideoFlipMethod, 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 */
288 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps); /*g_object_set(pGstreamer_s->appsink, "caps", output_format->caps, NULL); */
289 g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);
290 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
292 if(_mm_check_rotate_format(_valuepGstreamer_sVideoFlipMethod)) { /* when you want to rotate image */
293 /* because IYUV, I420, YV12 format can use vidoeflip*/
294 debug_log("set_link_pipeline_order_csc_rsz_rot");
295 _mm_link_pipeline_order_csc_rsz_rot(pGstreamer_s, input_format, output_format);
297 debug_log("set_link_pipeline_order_csc_rsz");
298 _mm_link_pipeline_order_csc_rsz(pGstreamer_s, input_format, output_format);
300 /* Conecting to the new-buffer signal emited by the appsink*/
301 debug_log("Start G_CALLBACK (mm_sink_buffer)");
302 g_signal_connect (pGstreamer_s->appsink, "new-buffer", G_CALLBACK (_mm_sink_buffer), pGstreamer_s);
303 debug_log("End G_CALLBACK (mm_sink_buffer)");
309 _mm_set_image_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
311 char _a='A', _b='A', _c='A', _d='A';
312 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;
314 if(__format == NULL) {
315 debug_error("Image format is NULL\n");
319 debug_log("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
321 if(strcmp(__format->colorspace,"YUV") == 0) {
322 if(strcmp(__format->format_label,"I420") == 0) {
323 _a='I'; _b='4', _c='2', _d='0';
324 }else if(strcmp(__format->format_label,"Y42B") == 0) {
325 _a='Y'; _b='4', _c='2', _d='B';
326 }else if(strcmp(__format->format_label,"Y444") == 0) {
327 _a='Y'; _b='4', _c='4', _d='4';
328 }else if(strcmp(__format->format_label,"YV12") == 0) {
329 _a='Y'; _b='V', _c='1', _d='2'; /* GStreamer-CRITICAL **: gst_mini_object_ref: assertion `mini_object != NULL' failed */
330 }else if(strcmp(__format->format_label,"NV12") == 0) {
331 _a='N'; _b='V', _c='1', _d='2';
332 }else if(strcmp(__format->format_label,"UYVY") == 0) {
333 _a='U'; _b='Y', _c='V', _d='Y';
334 }else if(strcmp(__format->format_label,"YUYV") == 0) {
335 _a='Y'; _b='V', _c='Y', _d='U';
338 __format->caps = gst_caps_new_simple ("video/x-raw-yuv",
339 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC (_a, _b, _c, _d), /*'I', '4', '2', '0'),*/
340 "framerate", GST_TYPE_FRACTION, 25, 1,
341 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
342 "width", G_TYPE_INT, __format->width,
343 "height", G_TYPE_INT, __format->height,
344 "framerate", GST_TYPE_FRACTION, 1, 1,
348 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
349 if(strcmp(__format->format_label,"RGB888") == 0) {
350 _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
351 }else if(strcmp(__format->format_label,"BGR888") == 0) {
352 _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
353 }else if(strcmp(__format->format_label,"RGB565") == 0) {
354 _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
355 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
356 _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
358 __format->caps = gst_caps_new_simple ("video/x-raw-rgb",
359 "bpp", G_TYPE_INT, _bpp,
360 "depth", G_TYPE_INT, _depth,
361 "red_mask", G_TYPE_INT, _red_mask,
362 "green_mask", G_TYPE_INT, _green_mask,
363 "blue_mask", G_TYPE_INT, _blue_mask,
364 "width", G_TYPE_INT, __format->width,
365 "height", G_TYPE_INT, __format->height,
366 "endianness", G_TYPE_INT, _endianness,
367 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
370 else if(strcmp(__format->colorspace,"RGBA") ==0) {
371 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
372 _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
373 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
374 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
375 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
376 _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
377 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
378 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
380 debug_error("***Wrong format cs type***\n");
383 __format->caps = gst_caps_new_simple ("video/x-raw-rgb",
384 "bpp", G_TYPE_INT, _bpp,
385 "depth", G_TYPE_INT, _depth,
386 "red_mask", G_TYPE_INT, _red_mask,
387 "green_mask", G_TYPE_INT, _green_mask,
388 "blue_mask", G_TYPE_INT, _blue_mask,
389 "width", G_TYPE_INT, __format->width,
390 "height", G_TYPE_INT, __format->height,
391 "alpha_mask", G_TYPE_INT,_alpha_mask,
392 "endianness", G_TYPE_INT, _endianness,
393 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
396 debug_log("###__format->caps is not NULL###, %p", __format->caps);
398 debug_error("__format->caps is NULL");
403 _mm_set_image_colorspace( image_format_s* __format)
405 debug_log("format_label: %s\n", __format->format_label);
407 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
408 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
409 if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
410 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
411 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
412 }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
413 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
414 }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)) {
415 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
416 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
417 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
419 debug_error("Check your colorspace format label");
420 GSTCS_FREE(__format->colorspace);
424 static image_format_s*
425 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
427 image_format_s* __format = NULL;
429 __format=(image_format_s*)malloc(sizeof(image_format_s));
431 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
432 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
433 strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
434 debug_log("input_format_label: %s\n", pImgp_info->input_format_label);
435 _mm_set_image_colorspace(__format);
437 __format->width=pImgp_info->src_width;
438 __format->height=pImgp_info->src_height;
440 __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
441 debug_log("blocksize: %d\n", __format->blocksize);
442 _mm_set_image_format_s_capabilities(__format);
448 _mm_round_up_output_image_widh_height(image_format_s* pFormat)
450 if(strcmp(pFormat->colorspace,"YUV") ==0) {
451 pFormat->stride=MM_UTIL_ROUND_UP_8(pFormat->width);
452 pFormat->elevation=MM_UTIL_ROUND_UP_2(pFormat->height);
453 }else if(strcmp(pFormat->colorspace, "RGB") ==0) {
454 pFormat->stride=MM_UTIL_ROUND_UP_4(pFormat->width);
455 pFormat->elevation=MM_UTIL_ROUND_UP_2(pFormat->height);
459 static image_format_s*
460 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info)
462 image_format_s* __format = NULL;
464 __format=(image_format_s*)malloc(sizeof(image_format_s));
466 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
467 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
468 strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
469 _mm_set_image_colorspace(__format);
471 __format->width=pImgp_info->dst_width;
472 __format->height=pImgp_info->dst_height;
473 _mm_round_up_output_image_widh_height(__format);
475 __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
476 debug_log("output_format_label: %s", pImgp_info->output_format_label);
477 _mm_set_image_format_s_capabilities(__format);
478 debug_log("pImgp_info->dst: %p", pImgp_info->dst);
483 __mm_check_resize_format( char* _in_format_label, int _in_w, int _in_h, char* _out_format_label, int _out_w, int _out_h)
486 debug_log("input image format: %s input image width: %d input image height: %d output image format: %s output image width: %d output image height: %d\n",
487 _in_format_label, _in_w,_in_h, _out_format_label, _out_w, _out_h);
488 if(_mm_check_resize_format(_in_w, _in_h, _out_w, _out_h)) {
489 if( !( _mm_check_resize_format_label(_in_format_label) ||_mm_check_resize_format_label(_out_format_label) ) ) {
490 debug_error("ERROR-%s | %s Resize Only [AYUV] [YUY2] [YVYU] [UYVY] [Y800] [I420][YV12] format can use vidoeflip ERROR- Resize!!!#####\n", _in_format_label, _out_format_label);
498 __mm_check_rotate_format(int angle, const char* input_format_label, const char* output_format_label)
501 debug_log("rotate value: %d input_format_label: %s output_format_label: %s\n",
502 angle, input_format_label, output_format_label);
503 if(_mm_check_rotate_format(angle)) {
504 if(!(_mm_check_rotate_format_label(input_format_label) || _mm_check_rotate_format_label(output_format_label))) {
505 debug_error("ERROR- %s | %s Rotate Only IYUV, I420, YV12 format can use vidoeflip ERROR -Rotate", input_format_label, output_format_label);
513 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, gstreamer_s * pGstreamer_s, GstCaps*_caps)
515 int ret = MM_ERROR_NONE;
517 debug_error("caps is NULL\n");
518 return MM_ERROR_IMAGE_INVALID_VALUE;
521 if(pGstreamer_s->pipeline == NULL) {
522 debug_error("pipeline is NULL\n");
523 return MM_ERROR_IMAGE_INVALID_VALUE;
526 GstBuffer* gst_buf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
529 debug_error("buffer is NULL\n");
530 return MM_ERROR_IMAGE_INVALID_VALUE;
532 GST_BUFFER_DATA (gst_buf) = (guint8 *) pImgp_info->src;
533 GST_BUFFER_SIZE (gst_buf) = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
534 GST_BUFFER_FLAG_SET (gst_buf, GST_BUFFER_FLAG_READONLY);
536 gst_buffer_set_caps (gst_buf, _caps);
537 gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
538 g_free(GST_BUFFER_MALLOCDATA(gst_buf)); gst_buf = NULL; /* gst_buffer_finalize(gst_buf) { buffer->free_func (buffer->malloc_data); } */
544 _mm_imgp_gstcs_processing( gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, imgp_info_s* pImgp_info)
547 GstStateChangeReturn ret_state;
548 int ret = MM_ERROR_NONE;
550 ret = _mm_create_pipeline(pGstreamer_s);
551 if(ret != MM_ERROR_NONE) {
552 debug_error("ERROR - mm_create_pipeline ");
556 /* Make appsink emit the "new-preroll" and "new-buffer" signals. This option is by default disabled because signal emission is expensive and unneeded when the application prefers to operate in pull mode. */
557 gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
559 bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline)); /* GST_PIPELINE (pipeline)); */
560 /* gst_bus_add_watch (bus, (GstBusFunc) _mm_on_sink_message , pGstreamer_s); /* thow to appplicaton */
561 gst_object_unref(bus);
563 debug_log("Start mm_push_buffer_into_pipeline");
564 ret = _mm_push_buffer_into_pipeline(pImgp_info, pGstreamer_s, input_format->caps);
565 if(ret != MM_ERROR_NONE) {
566 debug_error("ERROR - mm_push_buffer_into_pipeline ");
568 debug_log("End mm_push_buffer_into_pipeline");
571 debug_log("Start mm_link_pipeline");
572 _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
573 debug_log("End mm_link_pipeline");
575 /* Conecting to the new-buffer signal emited by the appsink*/
576 debug_log("Start G_CALLBACK (_mm_sink_preroll)");
577 g_signal_connect (pGstreamer_s->appsink, "new-preroll", G_CALLBACK (_mm_sink_preroll), pGstreamer_s);
578 debug_log("End G_CALLBACK (mm_sink_preroll)");
580 /* GST_STATE_PLAYING*/
581 debug_log("Start GST_STATE_PLAYING");
582 ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
583 debug_log("End GST_STATE_PLAYING ret_state: %d", ret_state);
584 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
585 debug_log("Success Get GST_STATE_PLAYING ret_state: %d", ret_state);
587 /* Conecting to the new-buffer signal emited by the appsink*/
588 debug_log("Start G_CALLBACK (mm_sink_buffer)");
589 g_signal_connect (pGstreamer_s->appsink, "new-buffer", G_CALLBACK (_mm_sink_buffer), pGstreamer_s);
590 debug_log("End G_CALLBACK (mm_sink_buffer)");
593 debug_log("GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_SUCCESS)", ret_state);
594 }else if( ret_state == 2) {
595 debug_log("GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_ASYNC)", ret_state);
598 debug_log("Sucess GST_STATE_CHANGE");
601 if (ret_state == 0) {
602 debug_error("GST_STATE_CHANGE_FAILURE");
603 return MM_ERROR_IMAGE_INVALID_VALUE;
606 g_main_loop_run (pGstreamer_s->loop);
608 debug_log("Set GST_STATE_NULL");
611 gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
612 debug_log("End GST_STATE_NULL");
614 debug_log("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
616 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
619 debug_log("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
620 }else if( ret_state == 2) {
621 debug_log("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
624 debug_log("Success gst_element_get_state\n");
626 if (ret_state == 0) {
627 debug_error("GST_STATE_CHANGE_FAILURE");
629 if(pGstreamer_s->output_buffer != NULL) {
630 int buffer_size = GST_BUFFER_SIZE(pGstreamer_s->output_buffer);
631 debug_log("buffer size: %d\n", buffer_size);
632 if( buffer_size != mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->output_stride, pImgp_info->output_elevation)) {
633 debug_log("Buffer size is different stride:%d elevation: %d\n", pImgp_info->output_stride, pImgp_info->output_elevation);
635 debug_log("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
636 memcpy( pImgp_info->dst, (char*)GST_BUFFER_DATA(pGstreamer_s->output_buffer), buffer_size);
638 debug_log("pGstreamer_s->output_buffer is NULL");
641 gst_object_unref (pGstreamer_s->pipeline);
642 g_free(GST_BUFFER_MALLOCDATA(pGstreamer_s->output_buffer));
643 pGstreamer_s->output_buffer = NULL;
644 g_free (pGstreamer_s);
646 debug_log("End mm_convert_colorspace \n");
648 debug_log("pImgp_info->dst: %p", pImgp_info->dst);
654 mm_setup_image_size(const char* _format_label, int width, int height)
658 if(strcmp(_format_label, "I420") == 0) {
659 setup_image_size_I420(width, height); /*width * height *1.5; */
660 }else if(strcmp(_format_label, "Y42B") == 0) {
661 setup_image_size_Y42B(width, height); /*width * height *2; */
662 }else if(strcmp(_format_label, "YUV422") == 0) {
663 setup_image_size_Y42B(width, height); /*width * height *2; */
664 }else if(strcmp(_format_label, "Y444") == 0) {
665 setup_image_size_Y444(width, height); /* width * height *3; */
666 }else if(strcmp(_format_label, "YV12") == 0) {
667 setup_image_size_YV12(width, height); /* width * height *1; */
668 }else if(strcmp(_format_label, "NV12") == 0) {
669 setup_image_size_NV12(width, height); /* width * height *1.5; */
670 }else if(strcmp(_format_label, "RGB565") == 0) {
671 setup_image_size_RGB565(width, height); /* width * height *2; */
672 }else if(strcmp(_format_label, "RGB888") == 0) {
673 setup_image_size_RGB888(width, height); /* width * height *3; */
674 }else if(strcmp(_format_label, "BGR888") == 0) {
675 setup_image_size_BGR888(width, height); /* width * height *3; */
676 }else if(strcmp(_format_label, "UYVY") == 0) {
677 setup_image_size_UYVY(width, height); /* width * height *2; */
678 }else if(strcmp(_format_label, "YUYV") == 0) {
679 setup_image_size_YUYV(width, height); /* width * height *2; */
680 }else if(strcmp(_format_label, "ARGB8888") == 0) {
681 size = width * height *4; debug_log("file_size: %d\n", size);
682 }else if(strcmp(_format_label, "BGRA8888") == 0) {
683 size = width * height *4; debug_log("file_size: %d\n", size);
684 }else if(strcmp(_format_label, "RGBA8888") == 0) {
685 size = width * height *4; debug_log("file_size: %d\n", size);
686 }else if(strcmp(_format_label, "ABGR8888") == 0) {
687 size = width * height *4; debug_log("file_size: %d\n", size);
688 }else if(strcmp(_format_label, "BGRX") == 0) {
689 size = width * height *4; debug_log("file_size: %d\n", size);
696 _mm_imgp_gstcs(imgp_info_s* pImgp_info)
698 image_format_s* input_format=NULL, *output_format=NULL;
699 gstreamer_s* pGstreamer_s;
700 int ret = MM_ERROR_NONE;
702 if(pImgp_info == NULL) {
703 debug_error("imgp_info_s is NULL");
706 debug_log("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d dst: 0x%2x",
707 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, pImgp_info->dst);
709 if(pImgp_info->dst == NULL) {
710 debug_error("imgp_info_s->dst is NULL");
712 input_format= _mm_set_input_image_format_s_struct(pImgp_info);
713 output_format= _mm_set_output_image_format_s_struct(pImgp_info);
715 pImgp_info->output_stride = output_format->stride;
716 pImgp_info->output_elevation = output_format->elevation;
718 debug_log("mm_check_resize_format&&mm_check_rotate_format ");
720 if(__mm_check_resize_format(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)
721 && __mm_check_rotate_format(pImgp_info->angle, pImgp_info->input_format_label, pImgp_info->output_format_label) ) {
722 #if 0 /* def GST_EXT_TIME_ANALYSIS */
725 gst_init (NULL, NULL);
727 pGstreamer_s = g_new0 (gstreamer_s, 1);
729 #if 0 /* def GST_EXT_TIME_ANALYSIS */
730 MMTA_ACUM_ITEM_BEGIN("ffmpegcolorspace", 0);
732 /* _format_label : I420, RGB888 etc*/
733 debug_log("Start mm_convert_colorspace ");
734 ret =_mm_imgp_gstcs_processing(pGstreamer_s, 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 */
736 if(ret == MM_ERROR_NONE) {
737 debug_log("End mm_convert_colorspace [pImgp_info->dst: %p]", pImgp_info->dst);
738 }else if (ret != MM_ERROR_NONE) {
739 debug_error("ERROR -mm_convert_colorspace");
741 #if 0 /* def GST_EXT_TIME_ANALYSIS */
742 MMTA_ACUM_ITEM_END("ffmpegcolorspace", 0);
743 MMTA_ACUM_ITEM_SHOW_RESULT();
744 MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_FILE);
748 debug_error("Error - Check your input / ouput image input_format_label: %s src_width: %d src_height: %d output_format_label: %s output_stride: %d output_elevation: %d angle: %d ",
749 pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height, pImgp_info->output_format_label, pImgp_info->output_stride, pImgp_info->output_elevation, pImgp_info->angle);
750 ret = MM_ERROR_IMAGE_INVALID_VALUE;
753 GSTCS_FREE(input_format->format_label);
754 GSTCS_FREE(input_format->colorspace);
755 GSTCS_FREE(input_format);
757 GSTCS_FREE(output_format->format_label);
758 GSTCS_FREE(output_format->colorspace);
759 GSTCS_FREE(output_format);
765 mm_imgp(imgp_info_s* pImgp_info, imgp_type_e _imgp_type)
767 if (pImgp_info == NULL) {
768 debug_error("input vaule is error");
770 return _mm_imgp_gstcs(pImgp_info);