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