2 * GStreamer codec plugin for Tizen Emulator.
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved.
7 * KiTae Kim <kt920.kim@samsung.com>
8 * SeokYeon Hwang <syeon.hwang@samsung.com>
9 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
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.
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.
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.
31 #include "gstemulcommon.h"
32 #include "gstemulapi.h"
33 #include "gstemuldev.h"
36 emul_avcodec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
44 printf ("enter: %s\n", __func__);
48 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
54 GST_ERROR ("failed to get mmaped memory address.\n");
58 // printf("codec_init media_type: %d, codec_type: %d, name: %s\n",
59 // media_type, codec_type, name);
61 /* copy basic info to initialize codec on the host side.
62 * e.g. width, height, FPS ant etc. */
63 memcpy ((uint8_t *)mmapbuf, &codec->media_type, sizeof(codec->media_type));
64 size = sizeof(codec->media_type);
65 memcpy ((uint8_t *)mmapbuf + size, &codec->codec_type, sizeof(codec->codec_type));
66 size += sizeof(codec->codec_type);
67 memcpy ((uint8_t *)mmapbuf + size, codec->name, sizeof(codec->name));
68 size += sizeof(codec->name);
70 switch (codec->media_type) {
71 case AVMEDIA_TYPE_VIDEO:
72 memcpy ((uint8_t *)mmapbuf + size, &ctx->video, sizeof(ctx->video));
73 size += sizeof(ctx->video);
75 case AVMEDIA_TYPE_AUDIO:
76 memcpy ((uint8_t *)mmapbuf + size, &ctx->audio, sizeof(ctx->audio));
77 size += sizeof(ctx->audio);
80 GST_ERROR ("media type is unknown.\n");
85 memcpy ((uint8_t *)mmapbuf + size, &ctx->codecdata_size, sizeof(ctx->codecdata_size));
86 size += sizeof(ctx->codecdata_size);
87 if (ctx->codecdata_size) {
88 memcpy ((uint8_t *)mmapbuf + size, ctx->codecdata, ctx->codecdata_size);
91 CODEC_PARAM_INIT (params);
92 params.api_index = CODEC_INIT;
94 CODEC_WRITE_TO_QEMU (fd, ¶ms, 1);
96 if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
97 memcpy (&ctx->audio.sample_fmt,
98 (uint8_t *)mmapbuf, sizeof(ctx->audio.sample_fmt));
102 printf ("leave: %s\n", __func__);
108 emul_avcodec_deinit (CodecContext *ctx, CodecDevice *dev)
112 CodecIOParams params;
114 printf ("enter: %s\n", __func__);
118 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
124 GST_ERROR ("failed to get mmaped memory address.\n");
128 CODEC_PARAM_INIT (params);
129 params.api_index = CODEC_DEINIT;
130 params.ctx_index = 0;
131 CODEC_WRITE_TO_QEMU (fd, ¶ms, 1);
134 /* close device fd and release mapped memory region */
135 gst_emul_codec_device_close (dev);
138 printf ("leave: %s\n", __func__);
142 emul_avcodec_decode_video (CodecContext *ctx, guint8 *in_buf, guint in_size,
143 GstBuffer **out_buf, int *got_picture_ptr, CodecDevice *dev)
147 int len = 0, size = 0;
149 CodecIOParams params;
153 printf ("enter: %s\n", __func__);
157 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
163 GST_ERROR ("failed to get mmaped memory address\n");
167 memcpy ((uint8_t *)mmapbuf, &in_size, sizeof(guint));
168 size = sizeof(guint);
169 // memcpy ((uint8_t *)mmapbuf + size, &dec_info->timestamp, sizeof(GstClockTime));
170 // size += sizeof(GstClockTime);
171 memcpy ((uint8_t *)mmapbuf + size, in_buf, in_size);
173 /* provide raw image for decoding to qemu */
174 CODEC_PARAM_INIT (params);
175 params.api_index = CODEC_DECODE_VIDEO;
176 params.ctx_index = 0;
177 CODEC_WRITE_TO_QEMU (fd, ¶ms, 1);
179 memcpy (&len, (uint8_t *)mmapbuf, sizeof(len));
183 printf ("leave: %s\n", __func__);
189 emul_avcodec_decode_audio (CodecContext *ctx, int16_t *samples,
190 gint *frame_size_ptr, guint8 *in_buf, guint in_size, CodecDevice *dev)
193 void *mmapbuf = NULL;
195 gint out_size = 0, len;
196 CodecIOParams params;
200 GST_ERROR("failed to get %s fd\n", CODEC_DEV);
206 GST_ERROR("failed to get mmaped memory address\n");
210 memcpy ((uint8_t *)mmapbuf, &in_size, sizeof(guint));
211 size = sizeof(guint);
213 memcpy ((uint8_t *)mmapbuf + size, in_buf, in_size);
216 CODEC_PARAM_INIT (params);
217 params.api_index = CODEC_DECODE_AUDIO;
218 params.ctx_index = 0;
219 params.device_mem_offset = 0;
220 CODEC_WRITE_TO_QEMU (fd, ¶ms, 1);
222 memcpy (&ctx->audio.channel_layout,
223 (uint8_t *)mmapbuf, sizeof(ctx->audio.channel_layout));
224 size = sizeof(ctx->audio.channel_layout);
225 memcpy (&len, (uint8_t *)mmapbuf + size, sizeof(len));
227 memcpy (frame_size_ptr, (uint8_t *)mmapbuf + size, sizeof(*frame_size_ptr));
228 size += sizeof(*frame_size_ptr);
230 memcpy (samples, (uint8_t *)mmapbuf + size, FF_MAX_AUDIO_FRAME_SIZE);
237 emul_avcodec_encode_video (CodecContext *ctx, guint8 *in_buf, guint in_size,
238 GstBuffer **out_buf, CodecDevice *dev)
242 int len = 0, outbuf_size, size = 0;
243 CodecIOParams params;
245 printf ("enter: %s\n", __func__);
249 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
255 GST_ERROR ("failed to get mmaped memory address.\n");
259 memcpy ((uint8_t *)mmapbuf + size, &in_size, sizeof(guint));
260 size += sizeof(guint);
262 memcpy ((uint8_t *)mmapbuf + size, &in_timestamp, sizeof(GstClockTime));
263 size += sizeof(GstClockTime);
265 memcpy ((uint8_t *)mmapbuf + size, in_buf, in_size);
267 CODEC_PARAM_INIT (params);
268 params.api_index = CODEC_DECODE_AUDIO;
269 params.ctx_index = 0;
270 CODEC_WRITE_TO_QEMU (fd, ¶ms, 1);
274 memcpy (&out_size, (uint8_t *)mmapbuf + size, sizeof(uint));
275 size += sizeof(guint);
277 ret = gst_pad_alloc_buffer_and_set_caps (emulenc->srcpad,
278 GST_BUFFER_OFFSET_NONE, out_size,
279 GST_PAD_CAPS (emulenc->srcpad), out_buf);
281 gst_buffer_set_caps (*out_buf, GST_PAD_CAPS (emulenc->srcpad));
283 if (GST_BUFFER_DATA(*out_buf)) {
284 memcpy (GST_BUFFER_DATA(*out_buf), (uint8_t *)mmapbuf + size, out_size);
286 pritnf ("failed to allocate output buffer\n");
290 printf ("leave: %s\n", __func__);
296 emul_avcodec_encode_audio (CodecContext *ctx, CodecDevice *dev)
300 int len = 0, size = 0;
301 CodecIOParams params;
303 printf ("enter: %s\n", __func__);
307 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
313 GST_ERROR ("failed to get mmaped memory address.\n");
318 memcpy ((uint8_t *)mmapbuf + size, &in_size, sizeof(guint));
319 size += sizeof(guint);
320 memcpy ((uint8_t *)mmapbuf + size, &in_timestamp, sizeof(GstClockTime));
321 size += sizeof(GstClockTime);
322 memcpy ((uint8_t *)mmapbuf + size, in_buf, in_size);
325 CODEC_PARAM_INIT (params);
326 params.api_index = CODEC_DECODE_AUDIO;
327 params.ctx_index = 0;
328 CODEC_WRITE_TO_QEMU (fd, ¶ms, 1);
332 memcpy (&out_size, (uint8_t *)mmapbuf + size, sizeof(uint));
333 size += sizeof(guint);
335 *out_buf = gst_buffer_new();
336 GST_BUFFER_DATA (out_buf) = GST_BUFFER_MALLOCDATA (out_buf) = av_malloc (out_size);
337 GST_BUFFER_SIZE (out_buf) = out_size;
338 // GST_BUFFER_FREE_FUNC (out_buf) = g_free;
339 if (GST_PAD_CAPS(emulenc->srcpad)) {
340 gst_buffer_set_caps (*out_buf, GST_PAD_CAPS (emulenc->srcpad));
343 if (GST_BUFFER_DATA(*out_buf)) {
344 memcpy (GST_BUFFER_DATA(*out_buf), (uint8_t *)mmapbuf + size, out_size);
346 pritnf ("failed to allocate output buffer\n");
350 printf ("leave: %s\n", __func__);