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.
32 #include "gstmaruinterface.h"
33 #include "gstmarumem.h"
34 #include "gstmarudevice.h"
37 extern gpointer device_mem;
44 typedef struct _CodecHeader {
49 #define SMALL_BUFFER (256 * 1024)
50 #define MEDIUM_BUFFER (2 * 1024 * 1024)
51 #define LARGE_BUFFER (4 * 1024 * 1024)
53 #define CODEC_META_DATA_SIZE 256
56 _codec_header (int32_t api_index, uint32_t mem_offset, uint8_t *device_buf)
58 CodecHeader header = { 0 };
60 CODEC_LOG (DEBUG, "enter, %s\n", __func__);
62 header.api_index = api_index;
63 header.mem_offset = mem_offset;
65 memcpy(device_buf, &header, sizeof(header));
67 CODEC_LOG (DEBUG, "leave, %s\n", __func__);
69 return sizeof(header);
73 _codec_write_to_qemu (int32_t ctx_index, int32_t api_index,
74 uint32_t mem_offset, int fd)
76 CodecIOParams ioparam;
78 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
80 memset(&ioparam, 0, sizeof(ioparam));
81 ioparam.api_index = api_index;
82 ioparam.ctx_index = ctx_index;
83 ioparam.mem_offset = mem_offset;
84 if (write (fd, &ioparam, 1) < 0) {
85 CODEC_LOG (ERR, "failed to write input data\n");
88 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
91 static struct mem_info
92 secure_device_mem (guint buf_size)
95 uint32_t mem_offset = 0, cmd = 0;
96 struct mem_info info = {0, };
98 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
100 if (buf_size < SMALL_BUFFER) {
101 cmd = CODEC_CMD_SECURE_SMALL_BUFFER;
102 CODEC_LOG (DEBUG, "small buffer size\n");
103 } else if (buf_size < MEDIUM_BUFFER) {
105 cmd = CODEC_CMD_SECURE_MEDIUM_BUFFER;
106 CODEC_LOG (DEBUG, "HD buffer size\n");
108 // FULL HD Video(4MB)
109 cmd = CODEC_CMD_SECURE_LARGE_BUFFER;
110 CODEC_LOG (DEBUG, "FULL HD buffer size\n");
113 ret = ioctl (device_fd, cmd, &mem_offset);
115 CODEC_LOG (ERR, "failed to get available buffer\n");
118 if (mem_offset == (LARGE_BUFFER * 8)) {
119 CODEC_LOG (ERR, "acquired memory is over!!\n");
121 info.start = (gpointer)((uint32_t)device_mem + mem_offset);
122 info.offset = mem_offset;
124 CODEC_LOG (DEBUG, "acquire device_memory: 0x%x\n", mem_offset);
128 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
134 release_device_mem (gpointer start)
137 uint32_t offset = start - device_mem;
139 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
141 CODEC_LOG (DEBUG, "release device_mem start: %p, offset: 0x%x\n", start, offset);
142 ret = ioctl (device_fd, CODEC_CMD_RELEASE_BUFFER, &offset);
144 CODEC_LOG (ERR, "failed to release buffer\n");
147 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
151 codec_buffer_free (gpointer start)
153 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
155 release_device_mem (start);
157 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
161 codec_buffer_alloc (GstPad *pad, guint64 offset, guint size,
162 GstCaps *caps, GstBuffer **buf)
164 struct mem_info info;
166 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
168 *buf = gst_buffer_new ();
170 info = secure_device_mem (size);
172 CODEC_LOG (DEBUG, "memory start: 0x%p, offset 0x%x\n",
173 info.start, info.offset);
175 GST_BUFFER_DATA (*buf) = GST_BUFFER_MALLOCDATA (*buf) = info.start;
176 GST_BUFFER_SIZE (*buf) = size;
177 GST_BUFFER_FREE_FUNC (*buf) = codec_buffer_free;
178 GST_BUFFER_OFFSET (*buf) = offset;
181 gst_buffer_set_caps (*buf, caps);
184 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
190 codec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
193 int opened, size = 0;
194 uint8_t *mmapbuf = NULL;
195 uint32_t meta_offset = 0;
197 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
201 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
205 mmapbuf = (uint8_t *)dev->buf;
207 GST_ERROR ("failed to get mmaped memory address.\n");
211 ret = ioctl(fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index);
213 GST_ERROR ("failed to get context index\n");
216 CODEC_LOG (DEBUG, "get context index: %d\n", ctx->index);
218 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
220 "init. ctx: %d meta_offset = 0x%x\n", ctx->index, meta_offset);
223 _codec_init_meta_to (ctx, codec, mmapbuf + meta_offset + size);
225 _codec_write_to_qemu (ctx->index, CODEC_INIT, 0, fd);
228 "init. ctx: %d meta_offset = 0x%x, size: %d\n", ctx->index, meta_offset, size);
231 _codec_init_meta_from (ctx, codec->media_type, mmapbuf + meta_offset + size);
234 CODEC_LOG (DEBUG, "opened: %d\n", opened);
236 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
242 codec_deinit (CodecContext *ctx, CodecDevice *dev)
245 void *mmapbuf = NULL;
247 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
251 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
257 GST_ERROR ("failed to get mmaped memory address.\n");
261 CODEC_LOG (INFO, "close. context index: %d\n", ctx->index);
262 _codec_write_to_qemu (ctx->index, CODEC_DEINIT, 0, fd);
264 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
268 codec_flush_buffers (CodecContext *ctx, CodecDevice *dev)
271 void *mmapbuf = NULL;
273 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
277 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
283 GST_ERROR ("failed to get mmaped memory address.\n");
287 CODEC_LOG (DEBUG, "flush buffers. context index: %d\n", ctx->index);
288 _codec_write_to_qemu (ctx->index, CODEC_FLUSH_BUFFERS, 0, fd);
290 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
294 codec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
295 gint idx, gint64 in_offset, GstBuffer **out_buf,
296 int *got_picture_ptr, CodecDevice *dev)
300 uint8_t *mmapbuf = NULL;
301 uint32_t mem_offset = 0, meta_offset = 0;
303 CODEC_LOG (DEBUG, "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");
317 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
320 "decode_video. failed to get available memory to write inbuf\n");
323 CODEC_LOG (DEBUG, "decode_video. mem_offset = 0x%x\n", mem_offset);
325 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
326 CODEC_LOG (DEBUG, "decode_video. meta_offset = 0x%x\n", meta_offset);
329 _codec_decode_video_meta_to (in_size, idx, in_offset, mmapbuf + meta_offset + size);
330 _codec_decode_video_inbuf (in_buf, in_size, mmapbuf + mem_offset);
332 dev->mem_info.offset = mem_offset;
334 _codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, mem_offset, fd);
336 // after decoding video, no need to get outbuf.
338 _codec_decode_video_meta_from (&ctx->video, got_picture_ptr, mmapbuf + meta_offset + size);
340 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
346 codec_picture_copy (CodecContext *ctx, uint8_t *pict,
347 uint32_t pict_size, CodecDevice *dev)
350 void *mmapbuf = NULL;
352 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
356 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
362 GST_ERROR ("failed to get mmaped memory address\n");
366 CODEC_LOG (DEBUG, "pict_size: %d\n", pict_size);
368 if (pict_size < (SMALL_BUFFER)) {
369 dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
370 CODEC_LOG (DEBUG, "pict: %p , device_mem: %p\n", pict, mmapbuf);
371 CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", dev->mem_info.offset);
374 _codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY,
375 dev->mem_info.offset, fd);
376 if (pict_size < SMALL_BUFFER) {
378 "set the mem_offset as outbuf: 0x%x\n", dev->mem_info.offset);
379 ret = ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
383 } else if (pict_size < MEDIUM_BUFFER) {
384 uint32_t mem_offset = 0;
385 CODEC_LOG (DEBUG, "need to use medium size of memory\n");
387 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_MEDIUM_BUFFER, &mem_offset);
391 CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", mem_offset);
393 memcpy (pict, mmapbuf + mem_offset, pict_size);
395 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
397 CODEC_LOG (ERR, "failed to release used memory\n");
400 uint32_t mem_offset = 0;
401 CODEC_LOG (DEBUG, "need to use large size of memory\n");
403 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_LARGE_BUFFER, &mem_offset);
407 CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", mem_offset);
409 memcpy (pict, mmapbuf + mem_offset, pict_size);
411 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
413 CODEC_LOG (ERR, "failed to release used memory\n");
417 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
421 codec_decode_audio (CodecContext *ctx, int16_t *samples,
422 int *have_data, uint8_t *in_buf,
423 int in_size, CodecDevice *dev)
427 uint8_t *mmapbuf = NULL;
428 uint32_t mem_offset = 0, meta_offset = 0;
430 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
434 GST_ERROR("failed to get %s fd\n", CODEC_DEV);
438 mmapbuf = (uint8_t *)dev->buf;
440 GST_ERROR("failed to get mmaped memory address\n");
444 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
447 "decode_audio. failed to get available memory to write inbuf\n");
450 // CODEC_LOG (DEBUG, "decode audio. mem_offset = 0x%x\n", mem_offset);
451 CODEC_LOG (DEBUG, "decode_audio. ctx_id: %d mem_offset = 0x%x\n", ctx->index, mem_offset);
453 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
454 CODEC_LOG (DEBUG, "decode_audio. ctx_id: %d meta_offset = 0x%x\n", ctx->index, meta_offset);
457 _codec_decode_audio_meta_to (in_size, mmapbuf + meta_offset + size);
458 _codec_decode_audio_inbuf (in_buf, in_size, mmapbuf + mem_offset);
460 dev->mem_info.offset = mem_offset;
461 _codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, mem_offset, fd);
463 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_SMALL_BUFFER, &mem_offset);
467 CODEC_LOG (DEBUG, "after decode_audio. ctx_id: %d mem_offset = 0x%x\n", ctx->index, mem_offset);
470 _codec_decode_audio_meta_from (&ctx->audio, have_data, mmapbuf + meta_offset + size);
472 _codec_decode_audio_outbuf (*have_data, samples, mmapbuf + mem_offset);
474 CODEC_LOG (DEBUG, "decode_audio failure. ctx_id: %d\n", ctx->index);
477 memset(mmapbuf + mem_offset, 0x00, sizeof(len));
479 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
481 CODEC_LOG (ERR, "failed release used memory\n");
484 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
490 codec_encode_video (CodecContext *ctx, uint8_t *out_buf,
491 int out_size, uint8_t *in_buf,
492 int in_size, int64_t in_timestamp, CodecDevice *dev)
496 uint8_t *mmapbuf = NULL;
497 uint32_t mem_offset = 0, meta_offset = 0;
499 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
503 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
509 GST_ERROR ("failed to get mmaped memory address.\n");
513 if (in_size < SMALL_BUFFER) {
514 CODEC_LOG (DEBUG, "use small size of buffer\n");
516 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
518 CODEC_LOG (ERR, "failed to small size of buffer.\n");
521 } else if (in_size < MEDIUM_BUFFER) {
522 CODEC_LOG (DEBUG, "use medium size of buffer\n");
524 ret = ioctl (fd, CODEC_CMD_SECURE_MEDIUM_BUFFER, &mem_offset);
526 CODEC_LOG (ERR, "failed to small size of buffer.\n");
530 CODEC_LOG (DEBUG, "use large size of buffer\n");
531 ret = ioctl (fd, CODEC_CMD_SECURE_LARGE_BUFFER, &mem_offset);
533 CODEC_LOG (ERR, "failed to large size of buffer.\n");
537 CODEC_LOG (DEBUG, "encode_video. mem_offset = 0x%x\n", mem_offset);
539 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
540 CODEC_LOG (DEBUG, "encode_video. meta_offset = 0x%x\n", meta_offset);
544 _codec_encode_video_meta_to (in_size, in_timestamp, mmapbuf + meta_offset);
545 _codec_encode_video_inbuf (in_buf, in_size, mmapbuf + mem_offset);
547 dev->mem_info.offset = mem_offset;
548 _codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, mem_offset, fd);
550 #ifndef DIRECT_BUFFER
551 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_SMALL_BUFFER, &mem_offset);
555 CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
557 memcpy (&len, mmapbuf + meta_offset, sizeof(len));
558 CODEC_LOG (DEBUG, "encode_video. outbuf size: %d\n", len);
560 memcpy (out_buf, mmapbuf + mem_offset, len);
561 out_buf = mmapbuf + mem_offset;
564 dev->mem_info.offset = mem_offset;
567 _codec_encode_video_outbuf (out_buf, mmapbuf + mem_offset);
568 // memset(mmapbuf + mem_offset, 0x00, sizeof(len));
572 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
574 CODEC_LOG (ERR, "failed release used memory\n");
578 dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
579 CODEC_LOG (DEBUG, "outbuf: %p , device_mem: %p\n", pict, mmapbuf);
580 CODEC_LOG (DEBUG, "encoded video. mem_offset = 0x%x\n", dev->mem_info.offset);
582 ret = ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
587 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
593 codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
594 int max_size, uint8_t *in_buf,
595 int in_size, CodecDevice *dev)
599 void *mmapbuf = NULL;
600 uint32_t mem_offset = 0, meta_offset = 0;
602 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
606 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
612 GST_ERROR ("failed to get mmaped memory address.\n");
616 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
621 CODEC_LOG (DEBUG, "write, encode_audio. mem_offset = 0x%x\n", mem_offset);
623 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
624 CODEC_LOG (DEBUG, "encode_audio. meta mem_offset = 0x%x\n", meta_offset);
626 size = _codec_header (CODEC_ENCODE_AUDIO, mem_offset,
627 mmapbuf + meta_offset);
628 _codec_encode_audio_meta_to (max_size, in_size, mmapbuf + meta_offset + size);
629 _codec_encode_audio_inbuf (in_buf, in_size, mmapbuf + mem_offset);
631 dev->mem_info.offset = mem_offset;
632 _codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, mem_offset, fd);
634 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_SMALL_BUFFER, &mem_offset);
639 CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
641 len = _codec_encode_audio_outbuf (out_buf, mmapbuf + mem_offset);
642 memset(mmapbuf + mem_offset, 0x00, sizeof(len));
644 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
649 CODEC_LOG (DEBUG, "leave: %s\n", __func__);