Does open and mmap operation once per a process.
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstmarumem.c
1 /*
2  * GStreamer codec plugin for Tizen 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  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 #include "gstmarumem.h"
32
33 /*
34  *  codec data such as codec name, longname, media type and etc.
35  */
36 static int
37 _codec_info_data (CodecElement *codec, uint8_t *device_buf)
38 {
39   int size = 0;
40
41   CODEC_LOG (DEBUG, "enter, %s\n", __func__);
42
43   CODEC_LOG (DEBUG, "type: %d, name: %s\n", codec->codec_type, codec->name);
44   memcpy (device_buf, &codec->codec_type, sizeof(codec->codec_type));
45   size = sizeof(codec->codec_type);
46
47   memcpy (device_buf + size, codec->name, sizeof(codec->name));
48   size += sizeof(codec->name);
49
50   CODEC_LOG (DEBUG, "leave, %s\n", __func__);
51
52   return size;
53 }
54
55 void
56 _codec_init_meta_to (CodecContext *ctx,
57                       CodecElement *codec,
58                       uint8_t *device_buf)
59 {
60   int size = 0;
61
62   CODEC_LOG (DEBUG, "enter, %s\n", __func__);
63
64   size = _codec_info_data (codec, device_buf);
65
66   CODEC_LOG (INFO, "context_id: %d, name: %s, media type: %s\n",
67     ctx->index, codec->name, codec->media_type ? "AUDIO" : "VIDEO");
68
69   CODEC_LOG (DEBUG, "init. write data to qemu, size: %d\n", size);
70   memcpy (device_buf + size, ctx, sizeof(CodecContext) - 12);
71   size += (sizeof(CodecContext) - 12);
72   memcpy (device_buf + size, ctx->codecdata, ctx->codecdata_size);
73
74   CODEC_LOG (DEBUG, "leave, %s\n", __func__);
75 }
76
77 int
78 _codec_init_meta_from (CodecContext *ctx,
79                         int media_type,
80                         uint8_t *device_buf)
81 {
82   int ret = 0, size = 0;
83
84   CODEC_LOG (DEBUG, "after init. read data from device.\n");
85
86   memcpy (&ret, device_buf, sizeof(ret));
87   size = sizeof(ret);
88   if (!ret) {
89     if (media_type == AVMEDIA_TYPE_AUDIO) {
90       AudioData audio = { 0 };
91
92 #if 0
93       memcpy(&audio, device_buf + size, sizeof(AudioData));
94       ctx->audio.sample_fmt = audio.sample_fmt;
95       ctx->audio.frame_size = audio.frame_size;
96       ctx->audio.bits_per_sample_fmt = audio.bits_per_sample_fmt;
97 #endif
98       CODEC_LOG (DEBUG,
99         "audio sample_fmt: %d\n", *(int *)(device_buf + size));
100
101       memcpy(&ctx->audio.sample_fmt, device_buf + size, sizeof(audio.sample_fmt));
102       size += sizeof(audio.sample_fmt);
103       memcpy(&ctx->audio.frame_size, device_buf + size, sizeof(audio.frame_size));
104       size += sizeof(audio.frame_size);
105       memcpy(&ctx->audio.bits_per_sample_fmt, device_buf + size, sizeof(audio.bits_per_sample_fmt));
106
107       CODEC_LOG (DEBUG,
108         "after init. audio sample_fmt: %d\n", ctx->audio.sample_fmt);
109     }
110   } else {
111     CODEC_LOG (ERR, "failed to open codec context\n");
112   }
113
114   return ret;
115 }
116
117 void
118 _codec_decode_video_meta_to (int in_size, int idx, int64_t in_offset, uint8_t *device_buf)
119 {
120   memcpy (device_buf, &in_size, sizeof(in_size));
121   memcpy (device_buf + sizeof(in_size), &idx, sizeof(idx));
122   memcpy (device_buf + sizeof(idx), &in_offset, sizeof(in_offset));
123 }
124
125 void
126 _codec_decode_video_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
127 {
128   int size = 0;
129
130   memcpy(device_buf, &in_size, sizeof(in_size));
131   size = sizeof(in_size);
132   if (in_size > 0 ) {
133     memcpy (device_buf + size, in_buf, in_size);
134   }
135
136   CODEC_LOG (DEBUG, "decode_video. inbuf_size: %d\n", in_size);
137 }
138
139 int
140 _codec_decode_video_meta_from (VideoData *video,
141                               int *got_picture_ptr,
142                               uint8_t *device_buf)
143 {
144   int len = 0, size = 0;
145
146   CODEC_LOG (DEBUG, "decode_video. read data from qemu.\n");
147
148   memcpy (&len, device_buf, sizeof(len));
149   size = sizeof(len);
150   memcpy (got_picture_ptr,
151     device_buf + size, sizeof(*got_picture_ptr));
152   size += sizeof(*got_picture_ptr);
153   memcpy (video, device_buf + size, sizeof(VideoData));
154
155   CODEC_LOG (DEBUG, "decode_video. len: %d, have_data: %d\n",
156     len, *got_picture_ptr);
157
158   return len;
159 }
160
161 void
162 _codec_decode_audio_meta_to (int in_size, uint8_t *device_buf)
163 {
164   memcpy (device_buf, &in_size, sizeof(in_size));
165 }
166
167
168 void
169 _codec_decode_audio_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
170 {
171   int size = 0;
172
173   memcpy (device_buf, &in_size, sizeof(in_size));
174   size = sizeof(in_size);
175   if (in_size > 0) {
176     memcpy (device_buf + size, in_buf, in_size);
177   }
178
179   CODEC_LOG (DEBUG, "decode_audio. inbuf_size: %d\n", in_size);
180 }
181
182 int
183 _codec_decode_audio_meta_from (AudioData *audio, int *frame_size_ptr,
184                                 uint8_t *device_buf)
185 {
186   int len = 0, size = 0;
187
188   CODEC_LOG (DEBUG, "decode_audio. read data from device.\n");
189
190   memcpy (&audio->sample_rate,
191     device_buf, sizeof(audio->sample_rate));
192   size = sizeof(audio->sample_rate);
193   memcpy (&audio->channels,
194     device_buf + size, sizeof(audio->channels));
195   size += sizeof(audio->channels);
196   memcpy (&audio->channel_layout,
197     device_buf + size, sizeof(audio->channel_layout));
198   size += sizeof(audio->channel_layout);
199   memcpy (&len, device_buf + size, sizeof(len));
200   size += sizeof(len);
201   memcpy (frame_size_ptr, device_buf + size, sizeof(*frame_size_ptr));
202
203   CODEC_LOG (DEBUG, "decode_audio. len: %d, frame_size: %d\n",
204           len, (*frame_size_ptr));
205
206   return len;
207 }
208
209 void
210 _codec_decode_audio_outbuf (int outbuf_size, int16_t *samples, uint8_t *device_buf)
211 {
212   CODEC_LOG (DEBUG, "decode_audio. read outbuf %d\n", outbuf_size);
213   memcpy (samples, device_buf, outbuf_size);
214 }
215
216 void
217 _codec_encode_video_meta_to (int in_size, int64_t in_timestamp, uint8_t *device_buf)
218 {
219   CODEC_LOG (DEBUG, "encode_video. write data to device.\n");
220
221   memcpy (device_buf, &in_size, sizeof(in_size));
222   memcpy (device_buf + sizeof(in_size), &in_timestamp, sizeof(in_timestamp));
223 }
224
225 void
226 _codec_encode_video_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
227 {
228   int size = 0;
229
230   memcpy ((uint8_t *)device_buf, &in_size, sizeof(in_size));
231   size += sizeof(in_size);
232   if (in_size > 0) {
233     memcpy (device_buf + size, in_buf, in_size);
234   }
235   CODEC_LOG (DEBUG, "encode_video. inbuf_size: %d\n", in_size);
236 }
237
238 void
239 _codec_encode_video_outbuf (int len, uint8_t *out_buf, uint8_t *device_buf)
240 {
241 //  int len, size;
242
243   CODEC_LOG (DEBUG, "encode_video. read data from device.\n");
244
245 //  memcpy (&len, device_buf, sizeof(len));
246 //  size = sizeof(len);
247   memcpy (out_buf, device_buf, len);
248
249 //  return len;
250 }
251
252 void
253 _codec_encode_audio_meta_to (int max_size, int in_size, uint8_t *device_buf)
254 {
255   int size = 0;
256
257   CODEC_LOG (DEBUG, "encode_audio. write data to device.\n");
258
259   memcpy (device_buf, &in_size, sizeof(in_size));
260   size = sizeof(in_size);
261   memcpy (device_buf + size, &max_size, sizeof(max_size));
262 }
263
264 void
265 _codec_encode_audio_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
266 {
267   int size = 0;
268
269   memcpy (device_buf, &in_size, sizeof(in_size));
270   size = sizeof(in_size);
271   if (in_size > 0) {
272     memcpy (device_buf + size, in_buf, in_size);
273   }
274   CODEC_LOG (DEBUG, "encode_audio. inbuf_size: %d\n", in_size);
275 }
276
277 int
278 _codec_encode_audio_outbuf (uint8_t *out_buf, uint8_t *device_buf)
279 {
280   int len, size;
281
282   CODEC_LOG (DEBUG, "encode_audio. read data from device\n");
283
284   memcpy (&len, (uint8_t *)device_buf, sizeof(len));
285   size = sizeof(len);
286   memcpy (out_buf, (uint8_t *)device_buf + size, len);
287
288   return len;
289 }