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 memset(&ioparam, 0, sizeof(ioparam));
79 ioparam.api_index = api_index;
80 ioparam.ctx_index = ctx_index;
81 ioparam.mem_offset = mem_offset;
82 if (write (fd, &ioparam, 1) < 0) {
83 fprintf (stderr, "%s, failed to write copy data.\n", __func__);
87 static struct mem_info
88 secure_device_mem (guint buf_size)
91 uint32_t mem_offset = 0, cmd = 0;
92 struct mem_info info = {0, };
94 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
96 if (buf_size < SMALL_BUFFER) {
97 cmd = CODEC_CMD_SECURE_SMALL_BUFFER;
98 CODEC_LOG (DEBUG, "small buffer size\n");
99 } else if (buf_size < MEDIUM_BUFFER) {
101 cmd = CODEC_CMD_SECURE_MEDIUM_BUFFER;
102 CODEC_LOG (DEBUG, "HD buffer size\n");
104 // FULL HD Video(4MB)
105 cmd = CODEC_CMD_SECURE_LARGE_BUFFER;
106 CODEC_LOG (DEBUG, "FULL HD buffer size\n");
109 ret = ioctl (device_fd, cmd, &mem_offset);
111 CODEC_LOG (ERR, "failed to get available buffer\n");
114 if (mem_offset == (LARGE_BUFFER * 8)) {
115 CODEC_LOG (ERR, "acquired memory is over!!\n");
117 info.start = (gpointer)((uint32_t)device_mem + mem_offset);
118 info.offset = mem_offset;
120 CODEC_LOG (DEBUG, "acquire device_memory: 0x%x\n", mem_offset);
124 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
130 release_device_mem (gpointer start)
133 uint32_t offset = start - device_mem;
135 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
137 CODEC_LOG (DEBUG, "release device_mem start: %p, offset: 0x%x\n", start, offset);
138 ret = ioctl (device_fd, CODEC_CMD_RELEASE_BUFFER, &offset);
140 CODEC_LOG (ERR, "failed to release buffer\n");
143 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
147 codec_buffer_free (gpointer start)
149 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
151 release_device_mem (start);
153 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
157 codec_buffer_alloc (GstPad *pad, guint64 offset, guint size,
158 GstCaps *caps, GstBuffer **buf)
160 struct mem_info info;
162 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
164 *buf = gst_buffer_new ();
166 info = secure_device_mem (size);
168 CODEC_LOG (DEBUG, "memory start: 0x%p, offset 0x%x\n",
169 info.start, info.offset);
171 GST_BUFFER_DATA (*buf) = GST_BUFFER_MALLOCDATA (*buf) = info.start;
172 GST_BUFFER_SIZE (*buf) = size;
173 GST_BUFFER_FREE_FUNC (*buf) = codec_buffer_free;
174 GST_BUFFER_OFFSET (*buf) = offset;
177 gst_buffer_set_caps (*buf, caps);
180 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
186 codec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
189 int opened, size = 0;
190 uint8_t *mmapbuf = NULL;
191 uint32_t meta_offset = 0;
193 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
197 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
201 mmapbuf = (uint8_t *)dev->buf;
203 GST_ERROR ("failed to get mmaped memory address.\n");
207 ret = ioctl(fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index);
209 GST_ERROR ("failed to get context index\n");
212 CODEC_LOG (DEBUG, "get context index: %d\n", ctx->index);
214 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
216 "init. ctx: %d meta_offset = 0x%x\n", ctx->index, meta_offset);
218 // size = _codec_header (CODEC_INIT, 0, mmapbuf + meta_offset);
220 _codec_init_meta_to (ctx, codec, mmapbuf + meta_offset + size);
222 _codec_write_to_qemu (ctx->index, CODEC_INIT, 0, fd);
225 "init. ctx: %d meta_offset = 0x%x, size: %d\n", ctx->index, meta_offset, size);
228 if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
230 "opened: %d, audio_sample_fmt: %d\n",
231 *(int *)(mmapbuf + meta_offset + size),
232 *(int *)(mmapbuf + meta_offset + size + 4));
237 _codec_init_meta_from (ctx, codec->media_type, mmapbuf + meta_offset + size);
240 CODEC_LOG (DEBUG, "opened: %d\n", opened);
242 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
248 codec_deinit (CodecContext *ctx, CodecDevice *dev)
251 void *mmapbuf = NULL;
253 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
257 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
263 GST_ERROR ("failed to get mmaped memory address.\n");
267 CODEC_LOG (INFO, "close. context index: %d\n", ctx->index);
268 _codec_write_to_qemu (ctx->index, CODEC_DEINIT, 0, fd);
270 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
274 codec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
275 gint idx, gint64 in_offset, GstBuffer **out_buf,
276 int *got_picture_ptr, CodecDevice *dev)
280 uint8_t *mmapbuf = NULL;
281 uint32_t mem_offset = 0, meta_offset = 0;
283 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
287 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
293 GST_ERROR ("failed to get mmaped memory address\n");
297 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
300 "decode_audio. failed to get available memory to write inbuf\n");
303 CODEC_LOG (DEBUG, "decode_video. mem_offset = 0x%x\n", mem_offset);
305 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
306 CODEC_LOG (DEBUG, "decode_video. meta_offset = 0x%x\n", meta_offset);
308 // size = _codec_header (CODEC_DECODE_VIDEO, mem_offset, mmapbuf + meta_offset);
310 _codec_decode_video_meta_to (in_size, idx, in_offset, mmapbuf + meta_offset + size);
311 _codec_decode_video_inbuf (in_buf, in_size, mmapbuf + mem_offset);
313 dev->mem_info.offset = mem_offset;
314 _codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, mem_offset, fd);
316 // after decoding video, no need to get outbuf.
318 _codec_decode_video_meta_from (&ctx->video, got_picture_ptr, mmapbuf + meta_offset + size);
320 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
326 codec_picture_copy (CodecContext *ctx, uint8_t *pict,
327 uint32_t pict_size, CodecDevice *dev)
330 void *mmapbuf = NULL;
332 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
336 GST_ERROR ("failed to get %s fd\n", CODEC_DEV);
342 GST_ERROR ("failed to get mmaped memory address\n");
346 CODEC_LOG (DEBUG, "pict_size: %d\n", pict_size);
348 // if (pict_size < MEDIUM_BUFFER) {
349 if (pict_size < (SMALL_BUFFER)) {
350 dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
351 CODEC_LOG (DEBUG, "pict: %p , device_mem: %p\n", pict, mmapbuf);
352 CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", dev->mem_info.offset);
355 _codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY,
356 dev->mem_info.offset, fd);
357 // if (pict_size < MEDIUM_BUFFER) {
358 if (pict_size < SMALL_BUFFER) {
360 "set the mem_offset as outbuf: 0x%x\n", dev->mem_info.offset);
361 ret = ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
365 } else if (pict_size < MEDIUM_BUFFER) {
366 uint32_t mem_offset = 0;
367 CODEC_LOG (DEBUG, "require to use medium size of memory\n");
369 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_MEDIUM_BUFFER, &mem_offset);
373 CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", mem_offset);
375 memcpy (pict, mmapbuf + mem_offset, pict_size);
377 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
379 CODEC_LOG (ERR, "failed release used memory\n");
382 uint32_t mem_offset = 0;
383 CODEC_LOG (DEBUG, "require to use large size of memory\n");
385 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_LARGE_BUFFER, &mem_offset);
389 CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", mem_offset);
391 memcpy (pict, mmapbuf + mem_offset, pict_size);
393 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
395 CODEC_LOG (ERR, "failed release used memory\n");
399 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
403 codec_decode_audio (CodecContext *ctx, int16_t *samples,
404 int *have_data, uint8_t *in_buf,
405 int in_size, CodecDevice *dev)
409 uint8_t *mmapbuf = NULL;
410 uint32_t mem_offset = 0, meta_offset = 0;
412 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
416 GST_ERROR("failed to get %s fd\n", CODEC_DEV);
420 mmapbuf = (uint8_t *)dev->buf;
422 GST_ERROR("failed to get mmaped memory address\n");
426 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
429 "decode_audio. failed to get available memory to write inbuf\n");
432 CODEC_LOG (DEBUG, "decode audio. mem_offset = 0x%x\n", mem_offset);
434 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
435 CODEC_LOG (DEBUG, "decode_audio. meta_offset = 0x%x\n", meta_offset);
437 // size = _codec_header (CODEC_DECODE_AUDIO, mem_offset, mmapbuf + meta_offset);
439 _codec_decode_audio_meta_to (in_size, mmapbuf + meta_offset + size);
440 _codec_decode_audio_inbuf (in_buf, in_size, mmapbuf + mem_offset);
442 dev->mem_info.offset = mem_offset;
443 _codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, mem_offset, fd);
445 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_SMALL_BUFFER, &mem_offset);
451 _codec_decode_audio_meta_from (&ctx->audio, have_data, mmapbuf + meta_offset + size);
453 _codec_decode_audio_outbuf (*have_data, samples, mmapbuf + mem_offset);
455 memset(mmapbuf + mem_offset, 0x00, sizeof(len));
457 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
459 CODEC_LOG (ERR, "failed release used memory\n");
462 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
468 codec_encode_video (CodecContext *ctx, uint8_t *out_buf,
469 int out_size, uint8_t *in_buf,
470 int in_size, int64_t in_timestamp, CodecDevice *dev)
474 uint8_t *mmapbuf = NULL;
475 uint32_t mem_offset = 0, meta_offset = 0;
477 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
481 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
487 GST_ERROR ("failed to get mmaped memory address.\n");
491 if (in_size < SMALL_BUFFER) {
492 CODEC_LOG (DEBUG, "use small size of buffer\n");
494 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
496 CODEC_LOG (ERR, "failed to small size of buffer.\n");
499 } else if (in_size < MEDIUM_BUFFER) {
500 CODEC_LOG (DEBUG, "use medium size of buffer\n");
502 ret = ioctl (fd, CODEC_CMD_SECURE_MEDIUM_BUFFER, &mem_offset);
504 CODEC_LOG (ERR, "failed to small size of buffer.\n");
508 CODEC_LOG (DEBUG, "use large size of buffer\n");
509 ret = ioctl (fd, CODEC_CMD_SECURE_LARGE_BUFFER, &mem_offset);
511 CODEC_LOG (ERR, "failed to large size of buffer.\n");
515 CODEC_LOG (DEBUG, "encode_video. mem_offset = 0x%x\n", mem_offset);
517 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
518 CODEC_LOG (DEBUG, "encode_video. meta_offset = 0x%x\n", meta_offset);
521 // _codec_header (CODEC_ENCODE_VIDEO, mem_offset, mmapbuf + meta_offset);
524 _codec_encode_video_meta_to (in_size, in_timestamp, mmapbuf + meta_offset);
525 _codec_encode_video_inbuf (in_buf, in_size, mmapbuf + mem_offset);
527 dev->mem_info.offset = mem_offset;
528 _codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, mem_offset, fd);
530 #ifndef DIRECT_BUFFER
531 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_SMALL_BUFFER, &mem_offset);
535 CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
537 memcpy (&len, mmapbuf + meta_offset, sizeof(len));
538 CODEC_LOG (DEBUG, "encode_video. outbuf size: %d\n", len);
540 memcpy (out_buf, mmapbuf + mem_offset, len);
541 out_buf = mmapbuf + mem_offset;
544 dev->mem_info.offset = mem_offset;
547 _codec_encode_video_outbuf (out_buf, mmapbuf + mem_offset);
548 // memset(mmapbuf + mem_offset, 0x00, sizeof(len));
552 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
554 CODEC_LOG (ERR, "failed release used memory\n");
558 dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
559 CODEC_LOG (DEBUG, "outbuf: %p , device_mem: %p\n", pict, mmapbuf);
560 CODEC_LOG (DEBUG, "encoded video. mem_offset = 0x%x\n", dev->mem_info.offset);
562 ret = ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
567 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
573 codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
574 int max_size, uint8_t *in_buf,
575 int in_size, CodecDevice *dev)
579 void *mmapbuf = NULL;
580 uint32_t mem_offset = 0, meta_offset = 0;
582 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
586 GST_ERROR ("failed to get %s fd.\n", CODEC_DEV);
592 GST_ERROR ("failed to get mmaped memory address.\n");
596 ret = ioctl (fd, CODEC_CMD_SECURE_SMALL_BUFFER, &mem_offset);
601 CODEC_LOG (DEBUG, "write, encode_audio. mem_offset = 0x%x\n", mem_offset);
603 meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
604 CODEC_LOG (DEBUG, "encode_audio. meta mem_offset = 0x%x\n", meta_offset);
606 size = _codec_header (CODEC_ENCODE_AUDIO, mem_offset,
607 mmapbuf + meta_offset);
608 _codec_encode_audio_meta_to (max_size, in_size, mmapbuf + meta_offset + size);
609 _codec_encode_audio_inbuf (in_buf, in_size, mmapbuf + mem_offset);
611 dev->mem_info.offset = mem_offset;
612 _codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, mem_offset, fd);
614 ret = ioctl (fd, CODEC_CMD_GET_DATA_FROM_SMALL_BUFFER, &mem_offset);
619 CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
621 len = _codec_encode_audio_outbuf (out_buf, mmapbuf + mem_offset);
622 memset(mmapbuf + mem_offset, 0x00, sizeof(len));
624 ret = ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &mem_offset);
629 CODEC_LOG (DEBUG, "leave: %s\n", __func__);