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 static GStaticMutex codec_mutex = G_STATIC_MUTEX_INIT;
38 void emul_codec_write_to_qemu (int ctx_index, int api_index, CodecDevice *dev)
40 CodecIOParams ioparam;
42 // memset(&ioparam, 0, sizeof(ioparam));
43 ioparam.api_index = api_index;
44 ioparam.ctx_index = ctx_index;
45 ioparam.mem_offset = dev->mem_info.offset;
46 ioparam.mem_type = dev->mem_info.type;
47 if (write (dev->fd, &ioparam, 1) < 0) {
48 printf ("[%s:%d] failed to copy data.\n", __func__, __LINE__);
53 emul_avcodec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
60 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
64 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
68 mmapbuf = (uint8_t *)dev->buf;
70 GST_ERROR ("failed to get mmaped memory address.\n");
74 ioctl(fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index);
76 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
77 emul_avcodec_init_to (ctx, codec, mmapbuf);
79 ret = ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, &usable);
81 perror("ioctl failure");
82 CODEC_LOG (ERR, "[%d] return value: %d\n", __LINE__, ret);
86 ret = ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
88 perror("ioctl failure");
89 CODEC_LOG (ERR, "[%d] return value: %d\n", __LINE__, ret);
92 CODEC_LOG (DEBUG, "[init] waiting after before.\n");
97 emul_avcodec_init_to (ctx, codec, mmapbuf);
102 emul_codec_write_to_qemu (ctx->index, CODEC_INIT, dev);
104 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
108 ret = ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
110 perror("ioctl failure");
111 CODEC_LOG (ERR, "[%d] return value: %d\n", __LINE__, ret);
114 CODEC_LOG (DEBUG, "[init] waiting after write.\n");
119 ret = emul_avcodec_init_from (ctx, codec, mmapbuf);
126 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
127 ret = emul_avcodec_init_from (ctx, codec, mmapbuf);
131 ret = ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
133 perror("ioctl failure");
134 CODEC_LOG (ERR, "[%d] return value: %d\n", __LINE__, ret);
137 CODEC_LOG (DEBUG, "[init][%d] waiting after write.\n", __LINE__);
142 ret = emul_avcodec_init_from (ctx, codec, mmapbuf);
146 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
149 CODEC_LOG (DEBUG, "leave: %s, ret: %d\n", __func__, ret);
154 emul_avcodec_deinit (CodecContext *ctx, CodecDevice *dev)
157 void *mmapbuf = NULL;
159 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
163 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
169 GST_ERROR ("failed to get mmaped memory address.\n");
173 emul_codec_write_to_qemu (ctx->index, CODEC_DEINIT, dev);
175 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
179 emul_avcodec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size, gint idx, gint64 in_offset,
180 GstBuffer **out_buf, int *got_picture_ptr, CodecDevice *dev)
183 uint8_t *mmapbuf = NULL;
185 int copyback, usable;
187 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
191 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
195 mmapbuf = (uint8_t *)dev->buf;
197 GST_ERROR ("failed to get mmaped memory address\n");
201 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
202 emul_avcodec_decode_video_to (in_buf, in_size, idx, in_offset, mmapbuf);
204 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
207 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
209 CODEC_LOG (DEBUG, "[decode_video] waiting before write.\n");
214 emul_avcodec_decode_video_to (in_buf, in_size, idx, in_offset, mmapbuf);
219 /* provide raw image for decoding to qemu */
220 emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, dev);
222 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
224 // ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
227 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
229 CODEC_LOG (DEBUG, "[decode_video][%d] waiting after write.\n", __LINE__);
234 len = emul_avcodec_decode_video_from (ctx, got_picture_ptr, mmapbuf);
240 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
241 len = emul_avcodec_decode_video_from (ctx, got_picture_ptr, mmapbuf);
245 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
247 CODEC_LOG (DEBUG, "[decode_video] waiting after write.\n");
251 len = emul_avcodec_decode_video_from (ctx, got_picture_ptr, mmapbuf);
254 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
257 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
262 emul_av_picture_copy (CodecContext *ctx, uint8_t *pict,
263 uint32_t pict_size, CodecDevice *dev)
266 void *mmapbuf = NULL;
267 int copyback, usable;
269 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
273 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
279 GST_ERROR ("failed to get mmaped memory address\n");
283 if (dev->mem_info.type == CODEC_SHARED_DEVICE_MEM) {
284 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
287 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
289 CODEC_LOG (DEBUG, "[copy_frame] waiting before write.\n");
297 emul_codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY, dev);
299 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
301 // ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
304 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
306 CODEC_LOG (DEBUG, "[copy_frame] waiting after write.\n");
310 memcpy (pict, mmapbuf, pict_size);
316 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
317 memcpy (pict, mmapbuf, pict_size);
321 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
323 CODEC_LOG (DEBUG, "[copy_frame] waiting after write.\n");
327 memcpy (pict, mmapbuf, pict_size);
330 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
333 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
337 emul_avcodec_decode_audio (CodecContext *ctx, int16_t *samples,
338 int *frame_size_ptr, uint8_t *in_buf,
339 int in_size, CodecDevice *dev)
342 uint8_t *mmapbuf = NULL;
344 int copyback, usable;
346 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
350 GST_ERROR("failed to get %s fd\n", CODEC_DEV);
354 mmapbuf = (uint8_t *)dev->buf;
356 GST_ERROR("failed to get mmaped memory address\n");
360 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
361 emul_avcodec_decode_audio_to (in_buf, in_size, mmapbuf);
363 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
366 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
368 CODEC_LOG (DEBUG, "[decode_audio] waiting before write.\n");
373 emul_avcodec_decode_audio_to (in_buf, in_size, mmapbuf);
378 emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, dev);
380 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
382 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
384 emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf);
388 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
390 CODEC_LOG (DEBUG, "[decode_audio] waiting after write.\n");
396 emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf);
402 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
404 CODEC_LOG (DEBUG, "[decode_audio] waiting after write.\n");
410 emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf);
413 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
416 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
421 emul_avcodec_encode_video (CodecContext *ctx, uint8_t *out_buf,
422 int out_size, uint8_t *in_buf,
423 int in_size, int64_t in_timestamp, CodecDevice *dev)
428 int copyback, usable;
430 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
434 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
440 GST_ERROR ("failed to get mmaped memory address.\n");
444 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
445 emul_avcodec_encode_video_to (in_buf, in_size, in_timestamp, mmapbuf);
447 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
450 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
452 CODEC_LOG (DEBUG, "[encode_video] waiting before write.\n");
457 emul_avcodec_encode_video_to (in_buf, in_size, in_timestamp, mmapbuf);
462 emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, dev);
464 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
467 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
469 CODEC_LOG (DEBUG, "[encode_video] waiting after write.\n");
474 len = emul_avcodec_encode_video_from (out_buf, out_size, mmapbuf);
479 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
481 CODEC_LOG (DEBUG, "[encode_video] waiting after write.\n");
486 len = emul_avcodec_encode_video_from (out_buf, out_size, mmapbuf);
489 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
492 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
497 emul_avcodec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
498 int out_size, uint8_t *in_buf,
499 int in_size, CodecDevice *dev)
504 int copyback, usable;
506 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
510 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
516 GST_ERROR ("failed to get mmaped memory address.\n");
520 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
521 emul_avcodec_encode_audio_to (out_size, in_size, in_buf, mmapbuf);
523 ioctl (fd, CODEC_CMD_ADD_TASK_QUEUE, NULL);
526 ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &usable);
528 CODEC_LOG (DEBUG, "[encode_audio] waiting before write.\n");
533 emul_avcodec_encode_audio_to (out_size, in_size, in_buf, mmapbuf);
538 emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, dev);
540 if (dev->mem_info.type == CODEC_FIXED_DEVICE_MEM) {
543 ioctl (fd, CODEC_CMD_WAIT_TASK, &wait);
545 CODEC_LOG (DEBUG, "[encode_audio] waiting after write.\n");
549 len = emul_avcodec_encode_audio_from (out_buf, out_size, mmapbuf);
553 len = emul_avcodec_encode_audio_from (out_buf, out_size, mmapbuf);
556 ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &usable);
558 CODEC_LOG (DEBUG, "[encode_audio] waiting after write.\n");
562 len = emul_avcodec_encode_audio_from (out_buf, out_size, mmapbuf);
565 ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, ©back);
568 CODEC_LOG (DEBUG, "leave: %s\n", __func__);