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 "gstemulapi2.h"
34 #include "gstemuldev.h"
36 void emul_codec_write_to_qemu (int ctx_index, int api_index, CodecDevice *dev)
38 CodecIOParams ioparam;
40 // memset(&ioparam, 0, sizeof(ioparam));
41 ioparam.api_index = api_index;
42 ioparam.ctx_index = ctx_index;
43 ioparam.mem_offset = dev->mem_info.offset;
44 if (write (dev->fd, &ioparam, 1) < 0) {
45 printf ("[%s:%d] failed to copy data.\n", __func__, __LINE__);
47 CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, ioparam.mem_offset);
51 extern gpointer device_mem;
58 static struct mem_info secure_device_mem()
60 uint32_t mem_offset = 0;
63 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
65 ioctl(device_fd, CODEC_CMD_SECURE_MEMORY, &mem_offset);
66 info.start = (gpointer)((uint32_t)device_mem + mem_offset);
67 info.offset = mem_offset;
69 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
70 CODEC_LOG (DEBUG, "caramis0 = 0x%x\n", mem_offset);
75 static void release_device_mem(gpointer start)
77 uint32_t offset = start - device_mem;
78 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
80 ioctl(device_fd, CODEC_CMD_RELEASE_MEMORY, &offset);
82 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
85 static void emul_buffer_free(gpointer start)
87 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
89 release_device_mem(start);
91 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
94 GstFlowReturn emul_buffer_alloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf)
98 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
100 *buf = gst_buffer_new ();
102 info = secure_device_mem();
104 CODEC_LOG (DEBUG, "[%s] start: 0x%x, offset 0x%x\n", __func__, info.start, info.offset);
106 GST_BUFFER_DATA (*buf) = GST_BUFFER_MALLOCDATA (*buf) = info.start;
107 GST_BUFFER_SIZE (*buf) = size;
108 GST_BUFFER_FREE_FUNC (*buf) = emul_buffer_free;
110 GST_BUFFER_OFFSET (*buf) = offset;
113 gst_buffer_set_caps (*buf, caps);
116 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
121 emul_avcodec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
126 uint32_t mem_offset = 0;
128 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
132 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
136 mmapbuf = (uint8_t *)dev->buf;
138 GST_ERROR ("failed to get mmaped memory address.\n");
142 ioctl(fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index);
143 CODEC_LOG (DEBUG, "recv context index: %d\n", ctx->index);
145 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
146 emul_avcodec_init_to (ctx, codec, mmapbuf + mem_offset);
147 dev->mem_info.offset = mem_offset;
148 emul_codec_write_to_qemu (ctx->index, CODEC_INIT, dev);
151 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
152 ret = emul_avcodec_init_from (ctx, codec, mmapbuf + mem_offset);
154 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
156 CODEC_LOG (DEBUG, "leave: %s, ret: %d\n", __func__, ret);
161 emul_avcodec_deinit (CodecContext *ctx, CodecDevice *dev)
164 void *mmapbuf = NULL;
166 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
170 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
176 GST_ERROR ("failed to get mmaped memory address.\n");
180 emul_codec_write_to_qemu (ctx->index, CODEC_DEINIT, dev);
182 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
186 emul_avcodec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
187 gint idx, gint64 in_offset, GstBuffer **out_buf,
188 int *got_picture_ptr, CodecDevice *dev)
191 uint8_t *mmapbuf = NULL;
193 // int copyback, usable;
194 uint32_t mem_offset = 0;
196 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
200 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
204 mmapbuf = (uint8_t *)dev->buf;
206 GST_ERROR ("failed to get mmaped memory address\n");
210 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
211 CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
212 emul_avcodec_decode_video_to (in_buf, in_size, idx, in_offset, mmapbuf + mem_offset);
214 dev->mem_info.offset = mem_offset;
215 emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, dev);
217 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
218 CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
219 len = emul_avcodec_decode_video_from (ctx, got_picture_ptr, mmapbuf + mem_offset);
221 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
223 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
228 emul_av_picture_copy (CodecContext *ctx, uint8_t *pict,
229 uint32_t pict_size, CodecDevice *dev)
232 void *mmapbuf = NULL;
233 // int copyback, usable;
234 uint32_t mem_offset = 0;
236 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
240 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
246 GST_ERROR ("failed to get mmaped memory address\n");
251 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
254 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
256 CODEC_LOG (DEBUG, "[copy_frame] waiting before write.\n");
263 emul_codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY, dev);
266 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
268 CODEC_LOG (DEBUG, "[copy_frame] waiting after write.\n");
272 memcpy (pict, mmapbuf, pict_size);
275 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
278 dev->mem_info.offset = (uint32_t)pict - (uint32_t)device_mem;
279 CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, dev->mem_info.offset);
280 emul_codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY, dev);
281 // ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
282 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM2, &(dev->mem_info.offset));
284 CODEC_LOG (DEBUG, "caramis1 = 0x%x\n", __func__, mem_offset);
286 memcpy (pict, mmapbuf, pict_size);
288 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
291 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
295 emul_avcodec_decode_audio (CodecContext *ctx, int16_t *samples,
296 int *frame_size_ptr, uint8_t *in_buf,
297 int in_size, CodecDevice *dev)
300 uint8_t *mmapbuf = NULL;
302 // int copyback, usable;
303 uint32_t mem_offset = 0;
305 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
309 GST_ERROR("failed to get %s fd\n", CODEC_DEV);
313 mmapbuf = (uint8_t *)dev->buf;
315 GST_ERROR("failed to get mmaped memory address\n");
320 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
323 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
325 CODEC_LOG (DEBUG, "[decode_audio] waiting before write.\n");
330 emul_avcodec_decode_audio_to (in_buf, in_size, mmapbuf);
334 emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, dev);
337 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
339 CODEC_LOG (DEBUG, "[decode_audio] waiting after write.\n");
345 emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf);
348 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
351 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
352 emul_avcodec_decode_audio_to (in_buf, in_size, mmapbuf + mem_offset);
354 dev->mem_info.offset = mem_offset;
355 emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, dev);
357 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
359 emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf + mem_offset);
361 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
363 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
369 emul_avcodec_encode_video (CodecContext *ctx, uint8_t *out_buf,
370 int out_size, uint8_t *in_buf,
371 int in_size, int64_t in_timestamp, CodecDevice *dev)
376 int copyback, usable;
378 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
382 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
388 GST_ERROR ("failed to get mmaped memory address.\n");
392 // ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
395 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
397 CODEC_LOG (DEBUG, "[encode_video] waiting before write.\n");
402 emul_avcodec_encode_video_to (in_buf, in_size, in_timestamp, mmapbuf);
406 emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, dev);
409 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
411 CODEC_LOG (DEBUG, "[encode_video] waiting after write.\n");
416 len = emul_avcodec_encode_video_from (out_buf, out_size, mmapbuf);
419 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
422 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
427 emul_avcodec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
428 int out_size, uint8_t *in_buf,
429 int in_size, CodecDevice *dev)
434 int copyback, usable;
436 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
440 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
446 GST_ERROR ("failed to get mmaped memory address.\n");
450 // ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
453 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
455 CODEC_LOG (DEBUG, "[encode_audio] waiting before write.\n");
460 emul_avcodec_encode_audio_to (out_size, in_size, in_buf, mmapbuf);
464 emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, dev);
467 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
469 CODEC_LOG (DEBUG, "[encode_audio] waiting after write.\n");
473 len = emul_avcodec_encode_audio_from (out_buf, out_size, mmapbuf);
476 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
479 CODEC_LOG (DEBUG, "leave: %s\n", __func__);