implemented decoding audio part, but needs to improve.
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstemul.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 "gstemulcommon.h"
39
40 GST_DEBUG_CATEGORY (emul_debug);
41
42 #define GST_TYPE_EMULDEC \
43   (gst_emul_dec_get_type())
44 #define GST_EMULDEC(obj) \
45   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_EMULDEC,GstEmulDec))
46 #define GST_EMULDEC_CLASS(klass) \
47   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_EMULDEC,GstEmulDecClass))
48 #define GST_IS_EMULDEC(obj) \
49   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_EMULDEC))
50 #define GST_IS_EMULDEC_CLASS(klass) \
51   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_EMULDEC))
52
53 gboolean gst_emuldec_register (GstPlugin *plugin, GList *element);
54 gboolean gst_emulenc_register (GstPlugin *plugin, GList *element);
55
56 static GList *codec_element = NULL;
57
58 static gboolean
59 gst_emul_codec_element_init ()
60 {
61   int fd, size = 0;
62   int version = 0;
63   int data_length = 0;
64   int ret = TRUE;
65   void *buffer = NULL;
66   GList *element = NULL;
67   CodecIOParams params;
68
69   fd = open (CODEC_DEV, O_RDWR);
70   if (fd < 0) {
71     perror ("failed to open codec device");
72   }
73
74 #if 0
75   ioctl (fd, CODEC_CMD_GET_VERSION, &version);
76   if (version != CODEC_VER) {
77     printf ("version conflict between device: %d, plugin: %d\n",
78         version, CODEC_VER);
79     return FALSE;
80   }
81 #endif
82
83   buffer = mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
84   if (!buffer) {
85     perror ("failure memory mapping.");
86   }
87
88   CODEC_PARAM_INIT (params);
89   params.api_index = CODEC_ELEMENT_INIT;
90   CODEC_WRITE_TO_QEMU (fd, &params, 1);
91
92   do {
93     CodecElement *elm = NULL;
94
95     elm = g_malloc0 (sizeof(CodecElement));
96     if (!elm) {
97       printf ("Failed to allocate memory.\n");
98       ret = FALSE;
99       break;
100     }
101
102     memcpy (&data_length, (uint8_t *)buffer + size, sizeof(data_length));
103     size += sizeof(data_length);
104     if (!data_length) {
105 //      printf ("There is no more avcodec.\n");
106       break;
107     }
108     memcpy (elm, (uint8_t *)buffer + size, data_length);
109     size += data_length;
110 #if 0
111     printf("codec: %s, longname: %s, decode: %d, media: %d\n",
112       elm->name, elm->longname, elm->codec_type, elm->media_type);
113 #endif
114     element = g_list_append (element, elm);
115   } while (1);
116
117   codec_element = element;
118
119   munmap (buffer, 4096);
120   close (fd);
121
122   return ret;
123 }
124
125 static gboolean
126 plugin_init (GstPlugin *plugin)
127 {
128   GST_DEBUG_CATEGORY_INIT (emul_debug,
129       "tizen-emul", 0, "Tizen Emulator Codec Elements");
130   gboolean ret;
131
132   gst_emul_init_pix_fmt_info ();
133
134   if (!gst_emul_codec_element_init ()) {
135     GST_ERROR ("failed to get codec elements from QEMU");
136     return FALSE;
137   }
138
139   ret = gst_emuldec_register (plugin, codec_element);
140   if (!ret) {
141     GST_ERROR ("failed to register decoder elements");
142     return FALSE;
143   }
144 #if 0
145   ret = gst_emulenc_register (plugin, codec_element);
146   if (!ret) {
147     GST_ERROR ("failed to register encoder elements");
148     return FALSE;
149   }
150 #endif
151
152   return TRUE;
153 }
154
155 #ifndef PACKAGE
156 #define PACKAGE "GST-EMUL-CODEC"
157 #endif
158
159 GST_PLUGIN_DEFINE (
160   GST_VERSION_MAJOR,
161   GST_VERSION_MINOR,
162   "tizen-sdk",
163   "Codecs for Tizen Emulator",
164   plugin_init,
165   "0.1.1",
166   "LGPL",
167   "Gst-Emul-Codec",
168   "http://tizen.org"
169 )