Fix close routine at decoder plugin.
[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   GST_INFO ("type: %d, name: %s", codec->codec_type, codec->name);
44
45   memcpy (buffer + size, &codec->codec_type, sizeof(codec->codec_type));
46   size += sizeof(codec->codec_type);
47
48   memcpy (buffer + size, codec->name, sizeof(codec->name));
49   size += sizeof(codec->name);
50
51   CODEC_LOG (DEBUG, "leave, %s\n", __func__);
52
53   return size;
54 }
55
56 void
57 codec_init_data_to (CodecContext *ctx, CodecElement *codec, gpointer buffer)
58 {
59   int size = 0;
60
61   CODEC_LOG (DEBUG, "enter, %s\n", __func__);
62
63   size = _codec_info_data (codec, buffer);
64
65   GST_INFO ("context_id: %d, name: %s, media type: %s",
66     ctx->index, codec->name, codec->media_type ? "audio" : "video");
67
68   memcpy (buffer + size, ctx, sizeof(CodecContext) - 12);
69   size += (sizeof(CodecContext) - 12);
70   memcpy (buffer + size, ctx->codecdata, ctx->codecdata_size);
71   size += ctx->codecdata_size;
72
73   // data length
74   size -= sizeof(size);
75   memcpy (buffer, &size, sizeof(size));
76
77   CODEC_LOG (DEBUG, "leave, %s\n", __func__);
78 }
79
80 int
81 codec_init_data_from (CodecContext *ctx, int media_type, gpointer buffer)
82 {
83   int ret = 0, size = 0;
84
85   memcpy (&ret, buffer, sizeof(ret));
86   size = sizeof(ret);
87   if (ret < 0) {
88     return ret;
89   } else {
90     if (media_type == AVMEDIA_TYPE_AUDIO) {
91       memcpy (&ctx->audio.sample_fmt, buffer + size, sizeof(ctx->audio.sample_fmt));
92       size += sizeof(ctx->audio.sample_fmt);
93
94       memcpy (&ctx->audio.frame_size, buffer + size, sizeof(ctx->audio.frame_size));
95       size += sizeof(ctx->audio.frame_size);
96
97       memcpy(&ctx->audio.bits_per_sample_fmt, buffer + size, sizeof(ctx->audio.bits_per_sample_fmt));
98       size += sizeof(ctx->audio.bits_per_sample_fmt);
99     }
100   }
101
102   memcpy(&ctx->codecdata_size, buffer + size, sizeof(ctx->codecdata_size));
103   size += sizeof(ctx->codecdata_size);
104
105   GST_DEBUG ("codec_init. extradata_size %d", ctx->codecdata_size);
106   if (ctx->codecdata_size > 0) {
107     ctx->codecdata = g_malloc(ctx->codecdata_size);
108     memcpy(ctx->codecdata, buffer + size, ctx->codecdata_size);
109   }
110
111   return ret;
112 }
113
114 void
115 codec_decode_video_data_to (int in_size, int idx, int64_t in_offset,
116                           uint8_t *in_buf, gpointer buffer)
117 {
118   int size = 0;
119
120   size = sizeof(size);
121   memcpy (buffer + size, &in_size, sizeof(in_size));
122   size += sizeof(in_size);
123   memcpy (buffer + size, &idx, sizeof(idx));
124   size += sizeof(idx);
125   memcpy (buffer + size, &in_offset, sizeof(in_offset));
126   size += sizeof(in_offset);
127
128   if (in_size > 0) {
129     memcpy (buffer + size, in_buf, in_size);
130     size += in_size;
131   }
132
133   CODEC_LOG (DEBUG, "decode_video. inbuf_size: %d\n", in_size);
134
135   size -= sizeof(size);
136   memcpy (buffer, &size, sizeof(size));
137 }
138
139 int
140 codec_decode_video_data_from (int *got_picture_ptr, VideoData *video, gpointer buffer)
141 {
142   int size = 0, len = 0;
143
144   memcpy (&len, buffer, sizeof(len));
145   size = sizeof(len);
146   memcpy (got_picture_ptr, buffer + size, sizeof(*got_picture_ptr));
147   size += sizeof(*got_picture_ptr);
148   memcpy (video, buffer + size, sizeof(VideoData));
149
150   GST_DEBUG ("decode_video. len: %d, have_data: %d", len, *got_picture_ptr);
151
152   return len;
153 }
154
155 void
156 codec_decode_audio_data_to (int in_size, uint8_t *in_buf, gpointer buffer)
157 {
158   int size = 0;
159
160   size = sizeof(size);
161   memcpy (buffer + size, &in_size, sizeof(in_size));
162   size += sizeof(in_size);
163   if (in_size > 0) {
164     memcpy (buffer + size, in_buf, in_size);
165     size += in_size;
166   }
167
168   size -= sizeof(size);
169   memcpy (buffer, &size, sizeof(size));
170 }
171
172 int
173 codec_decode_audio_data_from (int *have_data, int16_t *samples,
174                               AudioData *audio, gpointer buffer)
175 {
176   int len = 0, size = 0;
177   int resample_size = 0;
178
179   memcpy (&len, buffer, sizeof(len));
180   size = sizeof(len);
181   memcpy (have_data, buffer + size, sizeof(*have_data));
182   size += sizeof(*have_data);
183
184   GST_DEBUG ("decode_audio. len %d, have_data %d", len, (*have_data));
185
186   if (*have_data) {
187     memcpy (&audio->sample_fmt, buffer + size, sizeof(audio->sample_fmt));
188     size += sizeof(audio->sample_fmt);
189
190     memcpy (&audio->sample_rate, buffer + size, sizeof(audio->sample_rate));
191     size += sizeof(audio->sample_rate);
192
193     memcpy (&audio->channels, buffer + size, sizeof(audio->channels));
194     size += sizeof(audio->channels);
195
196     memcpy (&audio->channel_layout, buffer + size, sizeof(audio->channel_layout));
197     size += sizeof(audio->channel_layout);
198
199     GST_DEBUG ("decode_audio. sample_fmt %d sample_rate %d, channels %d, ch_layout %lld",
200       audio->sample_fmt, audio->sample_rate, audio->channels, audio->channel_layout);
201
202     memcpy (&resample_size, buffer + size, sizeof(resample_size));
203     size += sizeof(resample_size);
204     memcpy (samples, buffer + size, resample_size);
205     size += resample_size;
206   }
207
208   return resample_size;
209 }
210
211 void
212 codec_encode_video_data_to (int in_size, int64_t in_timestamp,
213                             uint8_t *in_buf, gpointer buffer)
214 {
215   int size = 0;
216
217   size = sizeof(size);
218   memcpy (buffer + size, &in_size, sizeof(in_size));
219   size += sizeof(in_size);
220   memcpy (buffer + size, &in_timestamp, sizeof(in_timestamp));
221   size += sizeof(in_timestamp);
222   if (in_size > 0) {
223     memcpy (buffer + size, in_buf, in_size);
224     size += in_size;
225   }
226
227   size -= sizeof(size);
228   memcpy (buffer, &size, sizeof(size));
229 }
230
231 int
232 codec_encode_video_data_from (uint8_t *out_buf, int *coded_frame, int *is_keyframe, gpointer buffer)
233 {
234   int len = 0, size = 0;
235
236   memcpy (&len, buffer, sizeof(len));
237   size = sizeof(len);
238
239   GST_DEBUG ("encode_video. outbuf size: %d", len);
240
241   if (len > 0) {
242     memcpy (coded_frame, buffer + size, sizeof(int));
243     size += sizeof(int);
244     memcpy (is_keyframe, buffer + size, sizeof(int));
245     size += sizeof(int);
246     memcpy (out_buf, buffer + size, len);
247
248     GST_DEBUG ("coded_frame %d, is_keyframe: %d", *coded_frame, *is_keyframe);
249   }
250
251   return len;
252 }
253
254 void
255 codec_encode_audio_data_to (int in_size, int max_size, uint8_t *in_buf, int64_t timestamp, gpointer buffer)
256 {
257   int size = 0;
258
259   size = sizeof(size);
260   memcpy (buffer + size, &in_size, sizeof(in_size));
261   size += sizeof(in_size);
262
263   memcpy (buffer + size, &timestamp, sizeof(timestamp));
264   size += sizeof(timestamp);
265
266   if (in_size > 0) {
267     memcpy (buffer + size, in_buf, in_size);
268     size += in_size;
269   }
270
271   size -= sizeof(size);
272   memcpy (buffer, &size, sizeof(size));
273 }
274
275 int
276 codec_encode_audio_data_from (uint8_t *out_buf, gpointer buffer)
277 {
278   int ret = 0, outbuf_size = 0, size = 0;
279
280   memcpy (&ret, buffer, sizeof(ret));
281   size = sizeof(ret);
282   if (ret == 0) {
283     memcpy (&outbuf_size, buffer + size, sizeof(outbuf_size));
284     size += sizeof(outbuf_size);
285     if (outbuf_size > 0) {
286       memcpy (out_buf, buffer + size, outbuf_size);
287     }
288   }
289
290   GST_DEBUG ("encode_audio. ret: %d outbuf size: %d", ret, outbuf_size);
291
292   return outbuf_size;
293 }