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 "gstmaruutils.h"
34 #include "gstmarumem.h"
35 #include "gstmarudevice.h"
38 extern gpointer device_mem;
40 typedef struct _CodecHeader {
45 typedef struct _CodecBufferId {
46 uint32_t buffer_index;
50 typedef struct _CodecIOParams {
55 CodecBufferId buffer_id;
59 #define CODEC_META_DATA_SIZE 256
60 #define GET_OFFSET(buffer) ((uint32_t)buffer - (uint32_t)device_mem)
63 #define OFFSET_PICTURE_BUFFER 0x100
66 _codec_invoke_qemu(int32_t ctx_index, int32_t api_index,
67 uint32_t mem_offset, int fd, CodecBufferId *buffer_id)
69 CodecIOParams ioparam = { 0, };
72 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
74 ioparam.api_index = api_index;
75 ioparam.ctx_index = ctx_index;
76 ioparam.mem_offset = mem_offset;
78 if (CHECK_VERSION(3)) {
80 ioparam.buffer_id.buffer_index = buffer_id->buffer_index;
81 ioparam.buffer_id.buffer_size = buffer_id->buffer_size;
84 ret = ioctl(fd, CODEC_CMD_INVOKE_API_AND_RELEASE_BUFFER, &ioparam);
87 buffer_id->buffer_index = ioparam.buffer_id.buffer_index;
88 buffer_id->buffer_size = ioparam.buffer_id.buffer_size;
91 if (ioctl(fd, CODEC_CMD_INVOKE_API_AND_RELEASE_BUFFER, &ioparam) < 0) {
95 ret = ioctl(fd, CODEC_CMD_PUT_DATA_INTO_BUFFER, buffer_id);
99 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
105 secure_device_mem (int fd, guint ctx_id, guint buf_size, gpointer* buffer)
108 CodecBufferId opaque;
110 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
111 opaque.buffer_index = ctx_id;
112 opaque.buffer_size = buf_size;
114 ret = ioctl (fd, CODEC_CMD_SECURE_BUFFER, &opaque);
115 /* ioctl: CODEC_CMD_SECURE_BUFFER
116 * - sets device memory offset into opaque.buffer_size
118 *buffer = (gpointer)((uint32_t)device_mem + opaque.buffer_size);
119 GST_DEBUG ("device_mem %p, offset_size 0x%x", device_mem, opaque.buffer_size);
121 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
127 release_device_mem (int fd, gpointer start)
130 uint32_t offset = start - device_mem;
132 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
134 GST_DEBUG ("release device_mem start: %p, offset: 0x%x", start, offset);
135 ret = ioctl (fd, CODEC_CMD_RELEASE_BUFFER, &offset);
137 GST_ERROR ("failed to release buffer\n");
140 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
144 codec_buffer_free (gpointer start)
146 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
148 if (use_new_decode_api()) {
149 release_device_mem (device_fd, start - OFFSET_PICTURE_BUFFER); // FIXME
151 release_device_mem (device_fd, start);
154 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
158 codec_buffer_alloc_and_copy (GstPad *pad, guint64 offset, guint size,
159 GstCaps *caps, GstBuffer **buf)
161 bool is_last_buffer = 0;
163 CodecBufferId opaque;
168 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
170 *buf = gst_buffer_new ();
172 marudec = (GstMaruDec *)gst_pad_get_element_private(pad);
173 ctx = marudec->context;
176 if (use_new_decode_api()) {
177 is_last_buffer = marudec->is_last_buffer;
178 mem_offset = marudec->mem_offset;
180 ctx = marudec->context;
182 opaque.buffer_index = ctx->index;
183 opaque.buffer_size = size;
185 GST_DEBUG ("buffer_and_copy. ctx_id: %d", ctx->index);
187 int ret = _codec_invoke_qemu(ctx->index, CODEC_PICTURE_COPY, 0, dev->fd, &opaque);
189 GST_DEBUG ("failed to get available buffer");
190 return GST_FLOW_ERROR;
192 is_last_buffer = ret;
193 mem_offset = opaque.buffer_size;
196 gpointer *buffer = NULL;
197 if (is_last_buffer) {
198 // FIXME: we must aligned buffer offset.
199 buffer = g_malloc (size);
201 GST_BUFFER_FREE_FUNC (*buf) = g_free;
203 if (use_new_decode_api()) {
204 memcpy (buffer, device_mem + mem_offset + OFFSET_PICTURE_BUFFER, size);
206 memcpy (buffer, device_mem + mem_offset, size);
208 release_device_mem(dev->fd, device_mem + mem_offset);
210 GST_DEBUG ("secured last buffer!! Use heap buffer");
212 // address of "device_mem" and "opaque" is aleady aligned.
213 if (use_new_decode_api()) {
214 buffer = (gpointer)(device_mem + mem_offset + OFFSET_PICTURE_BUFFER);
216 buffer = (gpointer)(device_mem + mem_offset);
219 GST_BUFFER_FREE_FUNC (*buf) = codec_buffer_free;
221 GST_DEBUG ("device memory start: 0x%p, offset 0x%x", (intptr_t)buffer, mem_offset);
224 GST_BUFFER_DATA (*buf) = GST_BUFFER_MALLOCDATA (*buf) = buffer;
225 GST_BUFFER_SIZE (*buf) = size;
226 GST_BUFFER_OFFSET (*buf) = offset;
229 gst_buffer_set_caps (*buf, caps);
232 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
238 codec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
241 gpointer buffer = NULL;
242 CodecBufferId opaque;
245 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
247 if (ioctl(dev->fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index) < 0) {
248 GST_ERROR ("failed to get a context index");
251 GST_DEBUG ("get context index: %d", ctx->index);
253 /* buffer size is 0. It means that this function is required to
256 if (secure_device_mem(dev->fd, ctx->index, 0, &buffer) < 0) {
257 GST_ERROR ("failed to get a memory block");
261 codec_init_data_to (ctx, codec, buffer);
263 opaque.buffer_index = ctx->index;
264 opaque.buffer_size = SMALLDATA;
266 ret = _codec_invoke_qemu (ctx->index, CODEC_INIT, GET_OFFSET(buffer), dev->fd, &opaque);
273 codec_init_data_from (ctx, codec->media_type, device_mem + opaque.buffer_size);
276 GST_ERROR ("failed to open Context for %s", codec->name);
281 release_device_mem(dev->fd, device_mem + opaque.buffer_size);
283 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
289 codec_deinit (CodecContext *ctx, CodecDevice *dev)
291 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
293 GST_INFO ("close context %d", ctx->index);
294 _codec_invoke_qemu (ctx->index, CODEC_DEINIT, 0, dev->fd, NULL);
296 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
300 codec_flush_buffers (CodecContext *ctx, CodecDevice *dev)
302 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
304 GST_DEBUG ("flush buffers of context: %d", ctx->index);
305 _codec_invoke_qemu (ctx->index, CODEC_FLUSH_BUFFERS, 0, dev->fd, NULL);
307 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
311 codec_decode_video (GstMaruDec *marudec, uint8_t *in_buf, int in_size,
312 gint idx, gint64 in_offset, GstBuffer **out_buf, int *have_data)
314 CodecContext *ctx = marudec->context;
315 CodecDevice *dev = marudec->dev;
316 int len = 0, ret = 0;
317 gpointer buffer = NULL;
318 CodecBufferId opaque = { 0, };
320 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
322 ret = secure_device_mem(dev->fd, ctx->index, in_size, &buffer);
324 GST_ERROR ("failed to get available memory to write inbuf");
328 codec_decode_video_data_to (in_size, idx, in_offset, in_buf, buffer);
330 opaque.buffer_index = ctx->index;
331 if (use_new_decode_api()) {
332 int picture_size = gst_maru_avpicture_size (ctx->video.pix_fmt,
333 ctx->video.width, ctx->video.height);
334 if (picture_size < 0) {
335 opaque.buffer_size = SMALLDATA;
337 opaque.buffer_size = picture_size;
339 ret = _codec_invoke_qemu(ctx->index, CODEC_DECODE_VIDEO2, GET_OFFSET(buffer), dev->fd, &opaque);
341 opaque.buffer_size = SMALLDATA;
342 ret = _codec_invoke_qemu(ctx->index, CODEC_DECODE_VIDEO, GET_OFFSET(buffer), dev->fd, &opaque);
349 len = codec_decode_video_data_from (have_data, &ctx->video, device_mem + opaque.buffer_size);
351 GST_DEBUG_OBJECT (marudec, "after decode: len %d, have_data %d",
353 if (len < 0 || *have_data <= 0) {
354 GST_DEBUG_OBJECT (marudec, "return flow %d, out %p, len %d",
357 release_device_mem(dev->fd, device_mem + opaque.buffer_size);
362 if (use_new_decode_api()) {
363 marudec->is_last_buffer = ret;
364 marudec->mem_offset = opaque.buffer_size;
366 release_device_mem(dev->fd, device_mem + opaque.buffer_size);
369 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
375 codec_decode_audio (CodecContext *ctx, int16_t *samples,
376 int *have_data, uint8_t *in_buf,
377 int in_size, CodecDevice *dev)
379 int len = 0, ret = 0;
380 gpointer buffer = NULL;
381 CodecBufferId opaque;
383 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
385 ret = secure_device_mem(dev->fd, ctx->index, in_size, &buffer);
387 GST_ERROR ("failed to get available memory to write inbuf");
391 GST_DEBUG ("decode_audio 1. in_buffer size %d", in_size);
392 codec_decode_audio_data_to (in_size, in_buf, buffer);
394 opaque.buffer_index = ctx->index;
395 opaque.buffer_size = SMALLDATA;
397 ret = _codec_invoke_qemu(ctx->index, CODEC_DECODE_AUDIO, GET_OFFSET(buffer), dev->fd, &opaque);
403 GST_DEBUG ("decode_audio 2. ctx_id: %d, buffer = 0x%x",
404 ctx->index, device_mem + opaque.buffer_size);
406 len = codec_decode_audio_data_from (have_data, samples,
407 &ctx->audio, device_mem + opaque.buffer_size);
409 GST_DEBUG ("decode_audio 3. ctx_id: %d len: %d", ctx->index, len);
411 release_device_mem(dev->fd, device_mem + opaque.buffer_size);
413 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
419 codec_encode_video (CodecContext *ctx, uint8_t *out_buf,
420 int out_size, uint8_t *in_buf,
421 int in_size, int64_t in_timestamp,
422 int *coded_frame, int *is_keyframe,
425 int len = 0, ret = 0;
426 gpointer buffer = NULL;
427 CodecBufferId opaque;
429 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
431 ret = secure_device_mem(dev->fd, ctx->index, in_size, &buffer);
433 GST_ERROR ("failed to small size of buffer");
437 codec_encode_video_data_to (in_size, in_timestamp, in_buf, buffer);
439 opaque.buffer_index = ctx->index;
440 opaque.buffer_size = SMALLDATA;
442 // FIXME: how can we know output data size ?
443 ret = _codec_invoke_qemu(ctx->index, CODEC_ENCODE_VIDEO, GET_OFFSET(buffer), dev->fd, &opaque);
449 GST_DEBUG ("encode_video. mem_offset = 0x%x", opaque.buffer_size);
451 len = codec_encode_video_data_from (out_buf, coded_frame, is_keyframe, device_mem + opaque.buffer_size);
453 release_device_mem(dev->fd, device_mem + opaque.buffer_size);
455 CODEC_LOG (DEBUG, "leave: %s\n", __func__);
461 codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
462 int max_size, uint8_t *in_buf,
463 int in_size, int64_t timestamp,
467 gpointer buffer = NULL;
468 CodecBufferId opaque;
470 CODEC_LOG (DEBUG, "enter: %s\n", __func__);
472 ret = secure_device_mem(dev->fd, ctx->index, in_size, &buffer);
477 codec_encode_audio_data_to (in_size, max_size, in_buf, timestamp, buffer);
479 opaque.buffer_index = ctx->index;
480 opaque.buffer_size = SMALLDATA;
482 ret = _codec_invoke_qemu(ctx->index, CODEC_ENCODE_AUDIO, GET_OFFSET(buffer), dev->fd, &opaque);
488 GST_DEBUG ("encode_audio. mem_offset = 0x%x", opaque.buffer_size);
490 ret = codec_encode_audio_data_from (out_buf, device_mem + opaque.buffer_size);
492 release_device_mem(dev->fd, device_mem + opaque.buffer_size);
494 CODEC_LOG (DEBUG, "leave: %s\n", __func__);