Modify video and audio encoder's interface module
[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   int resample_size = 0;
175
176   memcpy (&len, buffer, sizeof(len));
177   size = sizeof(len);
178   memcpy (have_data, buffer + size, sizeof(*have_data));
179   size += sizeof(*have_data);
180
181   GST_DEBUG ("decode_audio. len %d, have_data %d", len, (*have_data));
182
183   if (*have_data) {
184     memcpy (&audio->sample_fmt, buffer + size, sizeof(audio->sample_fmt));
185     size += sizeof(audio->sample_fmt);
186
187     memcpy (&audio->sample_rate, buffer + size, sizeof(audio->sample_rate));
188     size += sizeof(audio->sample_rate);
189
190     memcpy (&audio->channels, buffer + size, sizeof(audio->channels));
191     size += sizeof(audio->channels);
192
193     memcpy (&audio->channel_layout, buffer + size, sizeof(audio->channel_layout));
194     size += sizeof(audio->channel_layout);
195
196     GST_DEBUG ("decode_audio. sample_fmt %d sample_rate %d, channels %d, ch_layout %lld",
197       audio->sample_fmt, audio->sample_rate, audio->channels, audio->channel_layout);
198
199     memcpy (&resample_size, buffer + size, sizeof(resample_size));
200     size += sizeof(resample_size);
201     memcpy (samples, buffer + size, resample_size);
202   }
203
204 //  return len;
205   return resample_size;
206 }
207
208 void
209 codec_encode_video_data_to (int in_size, int64_t in_timestamp,
210                             uint8_t *in_buf, gpointer buffer)
211 {
212   int size = 0;
213
214   size = sizeof(size);
215   memcpy (buffer + size, &in_size, sizeof(in_size));
216   size += sizeof(in_size);
217   memcpy (buffer + size, &in_timestamp, sizeof(in_timestamp));
218   size += sizeof(in_timestamp);
219   if (in_size > 0) {
220     memcpy (buffer + size, in_buf, in_size);
221     size += in_size;
222   }
223
224   size -= sizeof(size);
225   memcpy (buffer, &size, sizeof(size));
226 }
227
228 int
229 codec_encode_video_data_from (uint8_t *out_buf, int *coded_frame, int *is_keyframe, gpointer buffer)
230 {
231   int len = 0, size = 0;
232
233   memcpy (&len, buffer, sizeof(len));
234   size = sizeof(len);
235
236   CODEC_LOG (DEBUG, "encode_video. outbuf size: %d\n", len);
237   if (len > 0) {
238     memcpy (coded_frame, buffer + size, sizeof(int));
239     size += sizeof(int);
240     memcpy (is_keyframe, buffer + size, sizeof(int));
241     size += sizeof(int);
242     memcpy (out_buf, buffer + size, len);
243
244     GST_DEBUG ("coded_frame %d, is_keyframe: %d", *coded_frame, *is_keyframe);
245   }
246
247   return len;
248 }
249
250 void
251 codec_encode_audio_data_to (int in_size, int max_size, uint8_t *in_buf, gpointer buffer)
252 {
253   int size = 0;
254
255   size = sizeof(size);
256   memcpy (buffer + size, &in_size, sizeof(in_size));
257   size += sizeof(in_size);
258   memcpy (buffer + size, &max_size, sizeof(max_size));
259   size += sizeof(max_size);
260
261   if (in_size > 0) {
262     memcpy (buffer + size, in_buf, in_size);
263     size += in_size;
264   }
265
266   size -= sizeof(size);
267   memcpy (buffer, &size, sizeof(size));
268 }
269
270 int
271 codec_encode_audio_data_from (uint8_t *out_buf, gpointer buffer)
272 {
273   int len = 0, size = 0;
274
275   memcpy (&len, buffer, sizeof(len));
276   size = sizeof(len);
277   if (len > 0) {
278     memcpy (out_buf, buffer + size, len);
279   }
280
281   CODEC_LOG (DEBUG, "encode_audio. outbuf size: %d\n", len);
282
283   return len;
284 }