32c80a14c4de3c644ed02610f607d72138ff59e5
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstmaru.c
1 /*
2  * Emulator
3  *
4  * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * KiTae Kim <kt920.kim@samsung.com>
8  * SeokYeon Hwang <syeon.hwang@samsung.com>
9  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 /* First, include the header file for the plugin, to bring in the
28  * object definition and other useful things.
29  */
30
31 /*
32  *
33  * Contributors:
34  * - S-Core Co., Ltd
35  *
36  */
37
38 #include "gstmaru.h"
39
40 GST_DEBUG_CATEGORY (maru_debug);
41
42 #define GST_TYPE_MARUDEC \
43   (gst_maru_dec_get_type())
44 #define GST_MARUDEC(obj) \
45   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MARUDEC,GstMaruDec))
46 #define GST_MARUDEC_CLASS(klass) \
47   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MARUDEC,GstMaruDecClass))
48 #define GST_IS_MARUDEC(obj) \
49   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MARUDEC))
50 #define GST_IS_MARUDEC_CLASS(klass) \
51   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MARUDEC))
52
53 gboolean gst_marudec_register (GstPlugin *plugin, GList *element);
54 gboolean gst_maruenc_register (GstPlugin *plugin, GList *element);
55
56 static GList *codec_element = NULL;
57
58 static gboolean
59 gst_maru_codec_element_init ()
60 {
61   int fd = 0, size = 0;
62   int version = 0;
63   int data_length = 0;
64   int i, elem_cnt = 0;
65   void *buffer = NULL;
66   CodecElement *elem = NULL;
67
68   fd = open (CODEC_DEV, O_RDWR);
69   if (fd < 0) {
70     perror ("[gst-maru] failed to open codec device");
71     return FALSE;
72   }
73
74   ioctl (fd, CODEC_CMD_GET_VERSION, &version);
75   if (version != CODEC_VER) {
76     CODEC_LOG (INFO, "version conflict between device: %d, plugin: %d\n",
77               version, CODEC_VER);
78     close (fd);
79     return FALSE;
80   }
81
82   buffer = mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
83   if (!buffer) {
84     perror ("[gst-maru] failure memory mapping.");
85     close (fd);
86     return FALSE;
87   }
88
89   CODEC_LOG (DEBUG, "request a device to get codec element.\n");
90   if (ioctl(fd, CODEC_CMD_GET_ELEMENT, NULL) < 0) {
91     perror ("[gst-maru] failed to get codec elements");
92     munmap (buffer, 4096);
93     close (fd);
94     return FALSE;
95   }
96
97   memcpy(&data_length, (uint8_t *)buffer, sizeof(data_length));
98   size += sizeof(data_length);
99
100   elem = g_malloc0 (data_length);
101   if (!elem) {
102     CODEC_LOG (ERR, "Failed to allocate memory.\n");
103     munmap (buffer, 4096);
104     close (fd);
105     return FALSE;
106   }
107
108   memcpy (elem, (uint8_t *)buffer + size, data_length);
109
110   elem_cnt = data_length / sizeof(CodecElement);
111   for (i = 0; i < elem_cnt; i++) {
112     codec_element = g_list_append (codec_element, &elem[i]);
113   }
114
115   munmap (buffer, 4096);
116   close (fd);
117
118   return TRUE;
119 }
120
121 static gboolean
122 plugin_init (GstPlugin *plugin)
123 {
124   GST_DEBUG_CATEGORY_INIT (maru_debug,
125       "tizen-maru", 0, "Tizen Emulator Codec Elements");
126
127   gst_maru_init_pix_fmt_info ();
128
129   if (!gst_maru_codec_element_init ()) {
130     GST_ERROR ("failed to get codec elements from QEMU");
131     return FALSE;
132   }
133
134   if (!gst_marudec_register (plugin, codec_element)) {
135     GST_ERROR ("failed to register decoder elements");
136     return FALSE;
137   }
138   if (!gst_maruenc_register (plugin, codec_element)) {
139     GST_ERROR ("failed to register encoder elements");
140     return FALSE;
141   }
142
143 #if 0
144   while ((codec_element = g_list_next (codec_element))) {
145     g_list_free (codec_element);
146   }
147 #endif
148
149   return TRUE;
150 }
151
152 #ifndef PACKAGE
153 #define PACKAGE "gst-plugins-maruator"
154 #endif
155
156 GST_PLUGIN_DEFINE (
157   GST_VERSION_MAJOR,
158   GST_VERSION_MINOR,
159   "tizen-emul",
160   "Codecs for Tizen Emulator",
161   plugin_init,
162   "0.1.1",
163   "LGPL",
164   "gst-plugins-emulator",
165   "http://tizen.org"
166 )