4fdd83d1a04670a5c60a95cc9b5f865777b39282
[platform/adaptation/emulator/gst-plugins-emulator.git] / src / gstmarumem.c
1 /*
2  * GStreamer codec plugin for Tizen Emulator.
3  *
4  * Copyright (C) 2013 - 2014 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, gpointer buffer)
38 {
39   int size = sizeof(size);
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 (buffer + size, &codec->codec_type, sizeof(codec->codec_type));
45   size += sizeof(codec->codec_type);
46
47   memcpy (buffer + 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_data_to (CodecContext *ctx, CodecElement *codec, gpointer buffer)
57 {
58   int size = 0;
59
60   CODEC_LOG (DEBUG, "enter, %s\n", __func__);
61
62   size = _codec_info_data (codec, buffer);
63
64   CODEC_LOG (INFO, "context_id: %d, name: %s, media type: %s\n",
65     ctx->index, codec->name, codec->media_type ? "AUDIO" : "VIDEO");
66
67   memcpy (buffer + size, ctx, sizeof(CodecContext) - 12);
68   size += (sizeof(CodecContext) - 12);
69   memcpy (buffer + size, ctx->codecdata, ctx->codecdata_size);
70   size += ctx->codecdata_size;
71
72   // data length
73   size -= sizeof(size);
74   memcpy (buffer, &size, sizeof(size));
75
76   CODEC_LOG (DEBUG, "leave, %s\n", __func__);
77 }
78
79 int
80 codec_init_data_from (CodecContext *ctx, int media_type, gpointer buffer)
81 {
82   int ret = 0, size = 0;
83
84   CODEC_LOG (DEBUG, "after init. read data from device.\n");
85
86   memcpy (&ret, buffer, sizeof(ret));
87   size = sizeof(ret);
88   if (ret < 0) {
89     return ret;
90   } else {
91     if (media_type == AVMEDIA_TYPE_AUDIO) {
92       memcpy (&ctx->audio.sample_fmt, buffer + size, sizeof(ctx->audio.sample_fmt));
93       size += sizeof(ctx->audio.sample_fmt);
94
95       memcpy (&ctx->audio.frame_size, buffer + size, sizeof(ctx->audio.frame_size));
96       size += sizeof(ctx->audio.frame_size);
97
98       memcpy(&ctx->audio.bits_per_sample_fmt, buffer + size, sizeof(ctx->audio.bits_per_sample_fmt));
99 #if 0
100       // TODO: check!!
101       memcpy (&ctx->audio, buffer + size, sizeof(ctx->audio));
102 #endif
103     } else {
104       CODEC_LOG (DEBUG, "video type\n");
105     }
106   }
107
108   return ret;
109 }
110
111 void
112 codec_decode_video_data_to (int in_size, int idx, int64_t in_offset,
113                           uint8_t *in_buf, gpointer buffer)
114 {
115   int size = 0;
116
117   size = sizeof(size);
118   memcpy (buffer + size, &in_size, sizeof(in_size));
119   size += sizeof(in_size);
120   memcpy (buffer + size, &idx, sizeof(idx));
121   size += sizeof(idx);
122   memcpy (buffer + size, &in_offset, sizeof(in_offset));
123   size += sizeof(in_offset);
124
125   if (in_size > 0) {
126     memcpy (buffer + size, in_buf, in_size);
127     size += in_size;
128   }
129
130   CODEC_LOG (DEBUG, "decode_video. inbuf_size: %d\n", in_size);
131
132   size -= sizeof(size);
133   memcpy (buffer, &size, sizeof(size));
134 }
135
136 int
137 codec_decode_video_data_from (int *got_picture_ptr, VideoData *video, gpointer buffer)
138 {
139   int size = 0, len = 0;
140
141   memcpy (&len, buffer, sizeof(len));
142   size = sizeof(len);
143   memcpy (got_picture_ptr, buffer + size, sizeof(*got_picture_ptr));
144   size += sizeof(*got_picture_ptr);
145   memcpy (video, buffer + size, sizeof(VideoData));
146
147   CODEC_LOG (DEBUG, "decode_video. len: %d, have_data: %d\n", len, *got_picture_ptr);
148
149   return len;
150 }
151
152 void
153 codec_decode_audio_data_to (int in_size, uint8_t *in_buf, gpointer buffer)
154 {
155   int size = 0;
156
157   size = sizeof(size);
158   memcpy (buffer + size, &in_size, sizeof(in_size));
159   size += sizeof(in_size);
160   if (in_size > 0) {
161     memcpy (buffer + size, in_buf, in_size);
162     size += in_size;
163   }
164
165   size -= sizeof(size);
166   memcpy (buffer, &size, sizeof(size));
167 }
168
169 int
170 codec_decode_audio_data_from (int *have_data, int16_t *samples,
171                               AudioData *audio, gpointer buffer)
172 {
173   int len = 0, size = 0;
174
175   memcpy (&len, buffer, sizeof(len));
176   size = sizeof(len);
177   memcpy (have_data, buffer + size, sizeof(*have_data));
178   size += sizeof(*have_data);
179
180   CODEC_LOG (DEBUG, "decode_audio. len %d, have_data %d\n",
181             len, (*have_data));
182
183   if (*have_data) {
184     memcpy (&audio->sample_rate, buffer + size, sizeof(audio->sample_rate));
185     size += sizeof(audio->sample_rate);
186
187     memcpy (&audio->channels, buffer + size, sizeof(audio->channels));
188     size += sizeof(audio->channels);
189
190     memcpy (&audio->channel_layout, buffer + size, sizeof(audio->channel_layout));
191     size += sizeof(audio->channel_layout);
192
193     CODEC_LOG (DEBUG, "decode_audio. sample_rate %d, channels %d, ch_layout %lld\n", audio->sample_rate, audio->channels, audio->channel_layout);
194
195     memcpy (samples, buffer + size, (*have_data));
196   }
197
198   return len;
199 }
200
201 void
202 codec_encode_video_data_to (int in_size, int64_t in_timestamp,
203                             uint8_t *in_buf, gpointer buffer)
204 {
205   int size = 0;
206
207   size = sizeof(size);
208   memcpy (buffer + size, &in_size, sizeof(in_size));
209   size += sizeof(in_size);
210   memcpy (buffer + size, &in_timestamp, sizeof(in_timestamp));
211   size += sizeof(in_timestamp);
212   if (in_size > 0) {
213     memcpy (buffer + size, in_buf, in_size);
214     size += in_size;
215   }
216
217   size -= sizeof(size);
218   memcpy (buffer, &size, sizeof(size));
219 }
220
221 int
222 codec_encode_video_data_from (uint8_t *out_buf, gpointer buffer)
223 {
224   int len = 0, size = 0;
225
226   memcpy (&len, buffer, sizeof(len));
227   size = sizeof(len);
228
229   CODEC_LOG (DEBUG, "encode_video. outbuf size: %d\n", len);
230   if (len > 0) {
231     memcpy (out_buf, buffer + size, len);
232     // dev->mem_info.offset = opaque.buffer_size;
233   }
234
235   return len;
236 }
237
238 void
239 codec_encode_audio_data_to (int in_size, int max_size, uint8_t *in_buf, gpointer buffer)
240 {
241   int size = 0;
242
243   size = sizeof(size);
244   memcpy (buffer + size, &in_size, sizeof(in_size));
245   size += sizeof(in_size);
246   memcpy (buffer + size, &max_size, sizeof(max_size));
247   size += sizeof(max_size);
248
249   if (in_size > 0) {
250     memcpy (buffer + size, in_buf, in_size);
251     size += in_size;
252   }
253
254   size -= sizeof(size);
255   memcpy (buffer, &size, sizeof(size));
256 }
257
258 int
259 codec_encode_audio_data_from (uint8_t *out_buf, gpointer buffer)
260 {
261   int len = 0, size = 0;
262
263   memcpy (&len, buffer, sizeof(len));
264   size = sizeof(len);
265   if (len > 0) {
266     memcpy (out_buf, buffer + size, len);
267   }
268
269   CODEC_LOG (DEBUG, "encode_audio. outbuf size: %d\n", len);
270
271   return len;
272 }