fd = open (CODEC_DEV, O_RDWR);
if (fd < 0) {
- perror ("failed to open codec device");
+ perror ("[gst-emul] failed to open codec device");
return FALSE;
}
buffer = mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (!buffer) {
- perror ("failure memory mapping.");
+ perror ("[gst-emul] failure memory mapping.");
close (fd);
- return FALSE;
+ return FALSE;
}
CODEC_LOG (DEBUG, "request a device to get codec element.\n");
- ioctl(fd, CODEC_CMD_GET_ELEMENT, NULL);
+ if (ioctl(fd, CODEC_CMD_GET_ELEMENT, NULL) < 0) {
+ perror ("[gst-emul] failed to get codec elements");
+ munmap (buffer, 4096);
+ close (fd);
+ return FALSE;
+ }
memcpy(&data_length, (uint8_t *)buffer, sizeof(data_length));
size += sizeof(data_length);
#include "gstemulapi2.h"
#include "gstemuldev.h"
-void emul_codec_write_to_qemu (int ctx_index, int api_index, CodecDevice *dev)
+extern int device_fd;
+extern gpointer device_mem;
+
+struct mem_info {
+ gpointer start;
+ uint32_t offset;
+};
+
+typedef struct _CodecHeader {
+ int32_t api_index;
+ uint32_t mem_offset;
+} CodecHeader;
+
+static int
+_codec_header (int32_t api_index, uint32_t mem_offset, uint8_t *device_buf)
+{
+ CodecHeader header = { 0 };
+
+ CODEC_LOG (DEBUG, "enter, %s\n", __func__);
+
+ header.api_index = api_index;
+ header.mem_offset = mem_offset;
+
+ memcpy(device_buf, &header, sizeof(header));
+
+ CODEC_LOG (DEBUG, "leave, %s\n", __func__);
+
+ return sizeof(header);
+}
+
+static void
+_codec_write_to_qemu (int32_t ctx_index, int32_t api_index,
+ uint32_t mem_offset, int fd)
{
CodecIOParams ioparam;
memset(&ioparam, 0, sizeof(ioparam));
ioparam.api_index = api_index;
ioparam.ctx_index = ctx_index;
- ioparam.mem_offset = dev->mem_info.offset;
- if (write (dev->fd, &ioparam, 1) < 0) {
- printf ("[%s:%d] failed to copy data.\n", __func__, __LINE__);
+ ioparam.mem_offset = mem_offset;
+ if (write (fd, &ioparam, 1) < 0) {
+ fprintf (stderr, "%s, failed to write copy data.\n", __func__);
}
- CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, ioparam.mem_offset);
}
-extern int device_fd;
-extern gpointer device_mem;
-
-struct mem_info {
- gpointer start;
- uint32_t offset;
-};
+#define SMALL_BUFFER (256 * 1024)
+#define MEDIUM_BUFFER (2 * 1024 * 1024)
+#define LARGE_BUFFER (4 * 1024 * 1024)
static struct mem_info
-secure_device_mem (void)
+secure_device_mem (guint buf_size)
{
- uint32_t mem_offset = 0;
+ int ret = 0;
+ uint32_t mem_offset = 0, cmd;
struct mem_info info;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
- ioctl (device_fd, CODEC_CMD_SECURE_MEMORY, &mem_offset);
- info.start = (gpointer)((uint32_t)device_mem + mem_offset);
- info.offset = mem_offset;
+ if (buf_size < SMALL_BUFFER) {
+ cmd = CODEC_CMD_S_SECURE_BUFFER;
+ CODEC_LOG (DEBUG, "small buffer size\n");
+ } else if (buf_size < MEDIUM_BUFFER) {
+ // HD Video(2MB)
+ cmd = CODEC_CMD_M_SECURE_BUFFER;
+ CODEC_LOG (DEBUG, "HD buffer size\n");
+ } else {
+ // FULL HD Video(4MB)
+ cmd = CODEC_CMD_L_SECURE_BUFFER;
+ CODEC_LOG (DEBUG, "FULL HD buffer size\n");
+ }
- CODEC_LOG (DEBUG, "acquire device_memory: 0x%x\n", mem_offset);
-#if 0
- if (mem_offset == 0x2000000) {
- CODEC_LOG (ERR, "acquired memory is over!!: 0x%x\n", mem_offset);
+ ret = ioctl (device_fd, cmd, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed to get available buffer\n");
+ // FIXME:
+ } else {
+ if (mem_offset == (LARGE_BUFFER * 8)) {
+ CODEC_LOG (ERR, "acquired memory is over!!\n");
+ } else {
+ info.start = (gpointer)((uint32_t)device_mem + mem_offset);
+ info.offset = mem_offset;
+
+ CODEC_LOG (DEBUG, "acquire device_memory: 0x%x\n", mem_offset);
+ }
}
-#endif
+
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
return info;
static void
release_device_mem (gpointer start)
{
+ int ret;
uint32_t offset = start - device_mem;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
- ioctl (device_fd, CODEC_CMD_RELEASE_MEMORY, &offset);
+ CODEC_LOG (DEBUG, "release device_mem start: %p, offset: 0x%x\n", start, offset);
+ ret = ioctl (device_fd, CODEC_CMD_RELEASE_MEMORY, &offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed to release buffer\n");
+ }
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
}
static void
-emul_buffer_free (gpointer start)
+codec_buffer_free (gpointer start)
{
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
}
GstFlowReturn
-emul_buffer_alloc (GstPad *pad, guint64 offset, guint size,
+codec_buffer_alloc (GstPad *pad, guint64 offset, guint size,
GstCaps *caps, GstBuffer **buf)
{
struct mem_info info;
*buf = gst_buffer_new ();
- info = secure_device_mem ();
+ info = secure_device_mem (size);
CODEC_LOG (DEBUG, "memory start: 0x%p, offset 0x%x\n",
info.start, info.offset);
GST_BUFFER_DATA (*buf) = GST_BUFFER_MALLOCDATA (*buf) = info.start;
GST_BUFFER_SIZE (*buf) = size;
- GST_BUFFER_FREE_FUNC (*buf) = emul_buffer_free;
+ GST_BUFFER_FREE_FUNC (*buf) = codec_buffer_free;
GST_BUFFER_OFFSET (*buf) = offset;
if (caps) {
}
int
-emul_avcodec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
+codec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
{
- int fd;
- uint8_t *mmapbuf;
- int ret = 0;
- uint32_t mem_offset = 0;
+ int fd, ret = 0;
+ int opened, size = 0;
+ uint8_t *mmapbuf = NULL;
+ uint32_t meta_offset = 0;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
return -1;
}
- ioctl(fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index);
- CODEC_LOG (DEBUG, "get context index: %d\n", ctx->index);
+ ret = ioctl(fd, CODEC_CMD_GET_CONTEXT_INDEX, &ctx->index);
+ if (ret < 0) {
+ GST_ERROR ("failed to get context index\n");
+ return -1;
+ }
+ CODEC_LOG (INFO, "get context index: %d\n", ctx->index);
- ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
- CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
+ meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
+ CODEC_LOG (DEBUG,
+ "init. ctx: %d meta_offset = 0x%x\n", ctx->index, meta_offset);
- emul_avcodec_init_to (ctx, codec, mmapbuf + mem_offset);
- dev->mem_info.offset = mem_offset;
- emul_codec_write_to_qemu (ctx->index, CODEC_INIT, dev);
+// size = _codec_header (CODEC_INIT, 0, mmapbuf + meta_offset);
+ size = 8;
+ _codec_init_meta_to (ctx, codec, mmapbuf + meta_offset + size);
- ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
- ret = emul_avcodec_init_from (ctx, codec, mmapbuf + mem_offset);
+ _codec_write_to_qemu (ctx->index, CODEC_INIT, 0, fd);
- ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ CODEC_LOG (DEBUG,
+ "init. ctx: %d meta_offset = 0x%x, size: %d\n", ctx->index, meta_offset, size);
- CODEC_LOG (DEBUG, "leave: %s, ret: %d\n", __func__, ret);
- return ret;
+#if 0
+ if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
+ CODEC_LOG (DEBUG,
+ "opened: %d, audio_sample_fmt: %d\n",
+ *(int *)(mmapbuf + meta_offset + size),
+ *(int *)(mmapbuf + meta_offset + size + 4));
+ }
+#endif
+
+ opened =
+ _codec_init_meta_from (ctx, codec->media_type, mmapbuf + meta_offset + size);
+ ctx->codec= codec;
+
+ CODEC_LOG (DEBUG, "leave: %s, opened: %d\n", __func__, opened);
+
+ return opened;
}
void
-emul_avcodec_deinit (CodecContext *ctx, CodecDevice *dev)
+codec_deinit (CodecContext *ctx, CodecDevice *dev)
{
int fd;
void *mmapbuf = NULL;
return;
}
- emul_codec_write_to_qemu (ctx->index, CODEC_DEINIT, dev);
+ CODEC_LOG (INFO, "close. context index: %d\n", ctx->index);
+ _codec_write_to_qemu (ctx->index, CODEC_DEINIT, 0, fd);
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
}
int
-emul_avcodec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
- gint idx, gint64 in_offset, GstBuffer **out_buf,
- int *got_picture_ptr, CodecDevice *dev)
+codec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
+ gint idx, gint64 in_offset, GstBuffer **out_buf,
+ int *got_picture_ptr, CodecDevice *dev)
{
int fd, len = 0;
+ int ret, size = 0;
uint8_t *mmapbuf = NULL;
- uint32_t mem_offset = 0;
+ uint32_t mem_offset = 0, meta_offset = 0;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
return -1;
}
- mmapbuf = (uint8_t *)dev->buf;
+ mmapbuf = dev->buf;
if (!mmapbuf) {
GST_ERROR ("failed to get mmaped memory address\n");
return -1;
}
- ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
- CODEC_LOG (DEBUG, "write, decode_video. mem_offset = 0x%x\n", mem_offset);
-
- emul_avcodec_decode_video_to (in_buf, in_size, idx, in_offset, mmapbuf + mem_offset);
+ ret = ioctl (fd, CODEC_CMD_S_SECURE_BUFFER, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR,
+ "decode_audio. failed to get available memory to write inbuf\n");
+ return -1;
+ }
+// CODEC_LOG (INFO, "write, decode_video. mem_offset = 0x%x\n", mem_offset);
- dev->mem_info.offset = mem_offset;
- emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, dev);
+ meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
+ CODEC_LOG (DEBUG, "decode_video. meta mem_offset = 0x%x\n", meta_offset);
- ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
- CODEC_LOG (DEBUG, "read, decode_video. mem_offset = 0x%x\n", mem_offset);
+// size = _codec_header (CODEC_DECODE_VIDEO, mem_offset, mmapbuf + meta_offset);
+ size = 8;
+ _codec_decode_video_meta_to (in_size, idx, in_offset, mmapbuf + meta_offset + size);
+ _codec_decode_video_inbuf (in_buf, in_size, mmapbuf + mem_offset);
- len = emul_avcodec_decode_video_from (ctx, got_picture_ptr, mmapbuf + mem_offset);
+ dev->mem_info.offset = mem_offset;
+ _codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, mem_offset, fd);
- ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ // after decoding video, no need to get outbuf.
+ len =
+ _codec_decode_video_meta_from (&ctx->video, got_picture_ptr, mmapbuf + meta_offset + size);
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
+
return len;
}
void
-emul_av_picture_copy (CodecContext *ctx, uint8_t *pict,
- uint32_t pict_size, CodecDevice *dev)
+codec_picture_copy (CodecContext *ctx, uint8_t *pict,
+ uint32_t pict_size, CodecDevice *dev)
{
- int fd;
+ int fd, ret = 0;
void *mmapbuf = NULL;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
return;
}
-// dev->mem_info.offset = (uint32_t)pict - (uint32_t)device_mem;
-// CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, dev->mem_info.offset);
-
- emul_codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY, dev);
-// ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
+ CODEC_LOG (DEBUG, "pict_size: %d\n", pict_size);
- dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
- CODEC_LOG (DEBUG, "[%s] pict: %p , device_mem: %p\n", __func__, pict, mmapbuf);
- CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, dev->mem_info.offset);
-
- ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
+// if (pict_size < MEDIUM_BUFFER) {
+ if (pict_size < (SMALL_BUFFER)) {
+ dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
+ CODEC_LOG (DEBUG, "pict: %p , device_mem: %p\n", pict, mmapbuf);
+ CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", dev->mem_info.offset);
+ }
-#if 0
- memcpy (pict, mmapbuf, pict_size);
-// ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
-#endif
+ _codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY,
+ dev->mem_info.offset, fd);
+// if (pict_size < MEDIUM_BUFFER) {
+ if (pict_size < SMALL_BUFFER) {
+ CODEC_LOG (DEBUG,
+ "set the mem_offset as outbuf: 0x%x\n", dev->mem_info.offset);
+ ret = ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
+ if (ret < 0) {
+ // FIXME:
+ }
+ } else if (pict_size < MEDIUM_BUFFER) {
+ uint32_t mem_offset = 0;
+ CODEC_LOG (DEBUG, "require to use medium size of memory\n");
+
+ ret = ioctl (fd, CODEC_CMD_REQ_FROM_MEDIUM_MEMORY, &mem_offset);
+ if (ret < 0) {
+ return;
+ }
+ CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", mem_offset);
+
+ memcpy (pict, mmapbuf + mem_offset, pict_size);
+
+ ret = ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed release used memory\n");
+ }
+ } else {
+ uint32_t mem_offset = 0;
+ CODEC_LOG (DEBUG, "require to use large size of memory\n");
+
+ ret = ioctl (fd, CODEC_CMD_REQ_FROM_LARGE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ return;
+ }
+ CODEC_LOG (DEBUG, "picture_copy, mem_offset = 0x%x\n", mem_offset);
+
+ memcpy (pict, mmapbuf + mem_offset, pict_size);
+
+ ret = ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed release used memory\n");
+ }
+ }
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
}
int
-emul_avcodec_decode_audio (CodecContext *ctx, int16_t *samples,
- int *frame_size_ptr, uint8_t *in_buf,
- int in_size, CodecDevice *dev)
+codec_decode_audio (CodecContext *ctx, int16_t *samples,
+ int *have_data, uint8_t *in_buf,
+ int in_size, CodecDevice *dev)
{
- int fd;
+ int fd, len = 0;
+ int ret, size = 0;
uint8_t *mmapbuf = NULL;
- int len;
- uint32_t mem_offset = 0;
+ uint32_t mem_offset = 0, meta_offset = 0;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
return -1;
}
- ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
- CODEC_LOG (DEBUG, "decode audio1. mem_offset = 0x%x\n", mem_offset);
+ ret = ioctl (fd, CODEC_CMD_S_SECURE_BUFFER, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR,
+ "decode_audio. failed to get available memory to write inbuf\n");
+ return -1;
+ }
+// CODEC_LOG (INFO, "decode audio1 mem_offset = 0x%x\n", mem_offset);
+
+ meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
+ CODEC_LOG (DEBUG, "decode_audio. meta_offset = 0x%x\n", meta_offset);
- emul_avcodec_decode_audio_to (in_buf, in_size, mmapbuf + mem_offset);
+// size = _codec_header (CODEC_DECODE_AUDIO, mem_offset, mmapbuf + meta_offset);
+ size = 8;
+ _codec_decode_audio_meta_to (in_size, mmapbuf + meta_offset + size);
+ _codec_decode_audio_inbuf (in_buf, in_size, mmapbuf + mem_offset);
dev->mem_info.offset = mem_offset;
- emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, dev);
+ _codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, mem_offset, fd);
- ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
- CODEC_LOG (DEBUG, "decode audio2. mem_offset = 0x%x\n", mem_offset);
+ ret = ioctl (fd, CODEC_CMD_REQ_FROM_SMALL_MEMORY, &mem_offset);
+ if (ret < 0) {
+ return -1;
+ }
+// CODEC_LOG (INFO, "decode audio2. mem_offset = 0x%x\n", mem_offset);
len =
- emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf + mem_offset);
+ _codec_decode_audio_meta_from (&ctx->audio, have_data, mmapbuf + meta_offset + size);
+ if (len > 0) {
+ _codec_decode_audio_outbuf (*have_data, samples, mmapbuf + mem_offset);
+ }
+ memset(mmapbuf + mem_offset, 0x00, sizeof(len));
- ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ ret = ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed release used memory\n");
+ }
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
}
int
-emul_avcodec_encode_video (CodecContext *ctx, uint8_t *out_buf,
- int out_size, uint8_t *in_buf,
- int in_size, int64_t in_timestamp, CodecDevice *dev)
+codec_encode_video (CodecContext *ctx, uint8_t *out_buf,
+ int out_size, uint8_t *in_buf,
+ int in_size, int64_t in_timestamp, CodecDevice *dev)
{
- int fd;
- void *mmapbuf;
- int len = 0;
- uint32_t mem_offset = 0;
+ int fd, len = 0;
+ int ret, size;
+ uint8_t *mmapbuf = NULL;
+ uint32_t mem_offset = 0, meta_offset = 0;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
return -1;
}
- ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
- CODEC_LOG (DEBUG, "write, encode_video. mem_offset = 0x%x\n", mem_offset);
+ if (in_size < SMALL_BUFFER) {
+ CODEC_LOG (DEBUG, "use small size of buffer\n");
+
+ ret = ioctl (fd, CODEC_CMD_S_SECURE_BUFFER, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed to small size of buffer.\n");
+ return -1;
+ }
+ } else if (in_size < MEDIUM_BUFFER) {
+ CODEC_LOG (DEBUG, "use medium size of buffer\n");
+
+ ret = ioctl (fd, CODEC_CMD_M_SECURE_BUFFER, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed to small size of buffer.\n");
+ return -1;
+ }
+ } else {
+ CODEC_LOG (DEBUG, "use large size of buffer\n");
+ ret = ioctl (fd, CODEC_CMD_L_SECURE_BUFFER, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed to large size of buffer.\n");
+ return -1;
+ }
+ }
+ CODEC_LOG (DEBUG, "encode_video. mem_offset = 0x%x\n", mem_offset);
+
+ meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
+ CODEC_LOG (DEBUG, "encode_video. meta_offset = 0x%x\n", meta_offset);
- emul_avcodec_encode_video_to (in_buf, in_size, in_timestamp, mmapbuf);
+// size =
+// _codec_header (CODEC_ENCODE_VIDEO, mem_offset, mmapbuf + meta_offset);
+ size = 8;
+ meta_offset += size;
+ _codec_encode_video_meta_to (in_size, in_timestamp, mmapbuf + meta_offset);
+ _codec_encode_video_inbuf (in_buf, in_size, mmapbuf + mem_offset);
dev->mem_info.offset = mem_offset;
- emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, dev);
+ _codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, mem_offset, fd);
- ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
+#ifndef DIRECT_BUFFER
+ ret = ioctl (fd, CODEC_CMD_REQ_FROM_SMALL_MEMORY, &mem_offset);
+ if (ret < 0) {
+ return -1;
+ }
CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
- len = emul_avcodec_encode_video_from (out_buf, out_size, mmapbuf);
+ memcpy (&len, mmapbuf + meta_offset, sizeof(len));
+ CODEC_LOG (DEBUG, "encode_video. outbuf size: %d\n", len);
+ if (len > 0) {
+ memcpy (out_buf, mmapbuf + mem_offset, len);
+ out_buf = mmapbuf + mem_offset;
+ }
- ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ dev->mem_info.offset = mem_offset;
+#if 0
+ len =
+ _codec_encode_video_outbuf (out_buf, mmapbuf + mem_offset);
+// memset(mmapbuf + mem_offset, 0x00, sizeof(len));
+#endif
+#if 1
+ ret = ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed release used memory\n");
+ }
+#endif
+#else
+ dev->mem_info.offset = (uint32_t)pict - (uint32_t)mmapbuf;
+ CODEC_LOG (DEBUG, "outbuf: %p , device_mem: %p\n", pict, mmapbuf);
+ CODEC_LOG (DEBUG, "encoded video. mem_offset = 0x%x\n", dev->mem_info.offset);
+
+ ret = ioctl (fd, CODEC_CMD_USE_DEVICE_MEM, &(dev->mem_info.offset));
+ if (ret < 0) {
+ // FIXME:
+ }
+#endif
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
+
return len;
}
int
-emul_avcodec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
- int out_size, uint8_t *in_buf,
- int in_size, CodecDevice *dev)
+codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
+ int max_size, uint8_t *in_buf,
+ int in_size, CodecDevice *dev)
{
- int fd;
+ int fd, len = 0;
+ int ret, size;
void *mmapbuf = NULL;
- int len = 0;
- uint32_t mem_offset = 0;
+ uint32_t mem_offset = 0, meta_offset = 0;
CODEC_LOG (DEBUG, "enter: %s\n", __func__);
return -1;
}
- ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
+ ret = ioctl (fd, CODEC_CMD_S_SECURE_BUFFER, &mem_offset);
+ if (ret < 0) {
+ return -1;
+ }
+
CODEC_LOG (DEBUG, "write, encode_audio. mem_offset = 0x%x\n", mem_offset);
- emul_avcodec_encode_audio_to (out_size, in_size, in_buf, mmapbuf);
+ meta_offset = (ctx->index - 1) * CODEC_META_DATA_SIZE;
+ CODEC_LOG (DEBUG, "encode_audio. meta mem_offset = 0x%x\n", meta_offset);
+
+ size = _codec_header (CODEC_ENCODE_AUDIO, mem_offset,
+ mmapbuf + meta_offset);
+ _codec_encode_audio_meta_to (max_size, in_size, mmapbuf + meta_offset + size);
+ _codec_encode_audio_inbuf (in_buf, in_size, mmapbuf + mem_offset);
dev->mem_info.offset = mem_offset;
- emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, dev);
+ _codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, mem_offset, fd);
+
+ ret = ioctl (fd, CODEC_CMD_REQ_FROM_SMALL_MEMORY, &mem_offset);
+ if (ret < 0) {
+ return -1;
+ }
- ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
- len = emul_avcodec_encode_audio_from (out_buf, out_size, mmapbuf);
+ len = _codec_encode_audio_outbuf (out_buf, mmapbuf + mem_offset);
+ memset(mmapbuf + mem_offset, 0x00, sizeof(len));
- ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ ret = ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ return -1;
+ }
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
+
return len;
}
#include "gstemulcommon.h"
-int emul_avcodec_init (CodecContext *ctx, CodecElement *codec,
- CodecDevice *dev);
+int
+codec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev);
-void emul_avcodec_deinit (CodecContext *ctx, CodecDevice *dev);
+void
+codec_deinit (CodecContext *ctx, CodecDevice *dev);
-int emul_avcodec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
- gint idx, gint64 in_offset, GstBuffer **out_buf,
- int *got_picture_ptr, CodecDevice *dev);
+int
+codec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
+ gint idx, gint64 in_offset, GstBuffer **out_buf,
+ int *got_picture_ptr, CodecDevice *dev);
-int emul_avcodec_decode_audio (CodecContext *ctx, int16_t *samples,
- int *frame_size_ptr, uint8_t *in_buf,
- int in_size, CodecDevice *dev);
+int
+codec_decode_audio (CodecContext *ctx, int16_t *samples,
+ int *frame_size_ptr, uint8_t *in_buf,
+ int in_size, CodecDevice *dev);
-int emul_avcodec_encode_video (CodecContext *ctx, uint8_t*out_buf, int out_size,
- uint8_t *in_buf, int in_size, int64_t in_timestamp, CodecDevice *dev);
+int
+codec_encode_video (CodecContext *ctx, uint8_t*out_buf,
+ int out_size, uint8_t *in_buf,
+ int in_size, int64_t in_timestamp,
+ CodecDevice *dev);
-int emul_avcodec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
- int out_size, uint8_t *in_buf,
- int in_size, CodecDevice *dev);
+int
+codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
+ int out_size, uint8_t *in_buf,
+ int in_size, CodecDevice *dev);
-void emul_av_picture_copy (CodecContext *ctx, uint8_t *pict, uint32_t pict_size, CodecDevice *dev);
+void
+codec_picture_copy (CodecContext *ctx, uint8_t *pict,
+ uint32_t pict_size, CodecDevice *dev);
-void emul_codec_write_to_qemu (int ctx_index, int api_index, CodecDevice *dev);
-
-GstFlowReturn emul_buffer_alloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf);
+GstFlowReturn
+codec_buffer_alloc (GstPad *pad, guint64 offset,
+ guint size, GstCaps *caps, GstBuffer **buf);
#endif /* __GST_EMUL_API_H__ */
#include "gstemulapi2.h"
+/*
+ * codec data such as codec name, longname, media type and etc.
+ */
+static int
+_codec_info_data (CodecElement *codec, uint8_t *device_buf)
+{
+ int size = 0;
+
+ CODEC_LOG (DEBUG, "enter, %s\n", __func__);
+
+ CODEC_LOG (DEBUG, "type: %d, name: %s\n", codec->codec_type, codec->name);
+ memcpy (device_buf, &codec->codec_type, sizeof(codec->codec_type));
+ size = sizeof(codec->codec_type);
+
+ memcpy (device_buf + size, codec->name, sizeof(codec->name));
+ size += sizeof(codec->name);
+
+ CODEC_LOG (DEBUG, "leave, %s\n", __func__);
+
+ return size;
+}
+
void
-emul_avcodec_init_to (CodecContext *ctx,
+_codec_init_meta_to (CodecContext *ctx,
CodecElement *codec,
uint8_t *device_buf)
{
- int size = 0, codec_size;
-
- CODEC_LOG (DEBUG, "[init] write data to qemu.\n");
- size = sizeof(size);
- codec_size =
- sizeof(CodecElement) - sizeof(codec->longname) - sizeof(codec->pix_fmts);
-
- memcpy (device_buf + size, codec, codec_size);
- size += codec_size;
-
- if (codec->media_type == AVMEDIA_TYPE_VIDEO) {
- CODEC_LOG (DEBUG, "before avcodec_open. pixel format: %d\n", ctx->video.pix_fmt);
- memcpy (device_buf + size, &ctx->video, sizeof(ctx->video));
-// *(VideoData *)(device_buf + size) = ctx->video;
- size += sizeof(ctx->video);
- } else if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
- memcpy (device_buf + size, &ctx->audio, sizeof(ctx->audio));
-// *(AudioData *)(device_buf + size) = ctx->audio;
- size += sizeof(ctx->audio);
- } else {
- CODEC_LOG (ERR, "media type is unknown.\n");
- return;
- }
+ int size = 0;
+
+ CODEC_LOG (DEBUG, "enter, %s\n", __func__);
- memcpy (device_buf + size,
- &ctx->bit_rate, sizeof(ctx->bit_rate));
-// *(int *)(device_buf + size) = ctx->bit_rate;
- size += sizeof(ctx->bit_rate);
- memcpy (device_buf + size,
- &ctx->codec_tag, sizeof(ctx->codec_tag));
-// *(int *)(device_buf + size) = ctx->codec_tag;
- size += sizeof(ctx->codec_tag);
- memcpy (device_buf + size,
- &ctx->codecdata_size, sizeof(ctx->codecdata_size));
-// *(int *)(device_buf + size) = ctx->codecdata_size;
- size += sizeof(ctx->codecdata_size);
- if (ctx->codecdata_size > 0) {
- memcpy (device_buf + size, ctx->codecdata, ctx->codecdata_size);
- size += ctx->codecdata_size;
+ size = _codec_info_data (codec, device_buf);
+
+ if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
+ CODEC_LOG (INFO,
+ "before init. audio sample_fmt: %d\n", ctx->audio.sample_fmt);
}
- size -= sizeof(size);
- memcpy (device_buf, &size, sizeof(size));
-// *(int *)device_buf = size;
- CODEC_LOG (DEBUG, "[init] write data: %d\n", size);
+ CODEC_LOG (DEBUG, "init. write data to qemu, size: %d\n", size);
+ memcpy (device_buf + size, ctx, sizeof(CodecContext) - 12);
+ size += (sizeof(CodecContext) - 12);
+ memcpy (device_buf + size, ctx->codecdata, ctx->codecdata_size);
+
+ CODEC_LOG (DEBUG, "leave, %s\n", __func__);
}
int
-emul_avcodec_init_from (CodecContext *ctx,
- CodecElement *codec,
+_codec_init_meta_from (CodecContext *ctx,
+ int media_type,
uint8_t *device_buf)
{
int ret = 0, size = 0;
- CODEC_LOG (DEBUG, "[init] read data from qemu.\n");
- memcpy (&ret, (uint8_t *)device_buf, sizeof(ret));
-// ret = *(int *)device_buf;
- size = sizeof(ret);
+ CODEC_LOG (DEBUG, "after init. read data from device.\n");
+ memcpy (&ret, device_buf, sizeof(ret));
+ size = sizeof(ret);
if (!ret) {
- if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
- memcpy (&ctx->audio.sample_fmt,
- (uint8_t *)device_buf + size, sizeof(ctx->audio.sample_fmt));
-// ctx->audio.sample_fmt = *(int *)(device_buf + size);
- size += sizeof(ctx->audio.sample_fmt);
- memcpy (&ctx->audio.frame_size,
- (uint8_t *)device_buf + size, sizeof(ctx->audio.frame_size));
-// ctx->audio.frame_size = *(int *)(device_buf + size);
- size += sizeof(ctx->audio.frame_size);
- memcpy (&ctx->audio.bits_per_smp_fmt,
- (uint8_t *)device_buf + size, sizeof(ctx->audio.bits_per_smp_fmt));
-// ctx->audio.bits_per_smp_fmt = *(int *)(device_buf + size);
- size += sizeof(ctx->audio.bits_per_smp_fmt);
-
- CODEC_LOG (DEBUG, "[init] sample_fmt %d\n", ctx->audio.sample_fmt);
+ if (media_type == AVMEDIA_TYPE_AUDIO) {
+ AudioData audio = { 0 };
+
+#if 0
+ memcpy(&audio, device_buf + size, sizeof(AudioData));
+ ctx->audio.sample_fmt = audio.sample_fmt;
+ ctx->audio.frame_size = audio.frame_size;
+ ctx->audio.bits_per_sample_fmt = audio.bits_per_sample_fmt;
+#endif
+ CODEC_LOG (INFO,
+ "audio sample_fmt: %d\n", *(int *)(device_buf + size));
+
+ memcpy(&ctx->audio.sample_fmt, device_buf + size, sizeof(audio.sample_fmt));
+ size += sizeof(audio.sample_fmt);
+ memcpy(&ctx->audio.frame_size, device_buf + size, sizeof(audio.frame_size));
+ size += sizeof(audio.frame_size);
+ memcpy(&ctx->audio.bits_per_sample_fmt, device_buf + size, sizeof(audio.bits_per_sample_fmt));
+
+ CODEC_LOG (INFO,
+ "after init. audio sample_fmt: %d\n", ctx->audio.sample_fmt);
}
- ctx->codec = codec;
} else {
CODEC_LOG (ERR, "failed to open codec context\n");
}
- CODEC_LOG (DEBUG, "context index: %d\n", ctx->index);
-
return ret;
}
void
-emul_avcodec_decode_video_to (uint8_t *in_buf, int in_size,
- int idx, int64_t in_offset,
+_codec_decode_video_meta_to (int in_size, int idx, int64_t in_offset, uint8_t *device_buf)
+{
+ memcpy (device_buf, &in_size, sizeof(in_size));
+ memcpy (device_buf + sizeof(in_size), &idx, sizeof(idx));
+ memcpy (device_buf + sizeof(idx), &in_offset, sizeof(in_offset));
+}
+
+void
+_codec_decode_video_inbuf (uint8_t *in_buf, int in_size,
uint8_t *device_buf)
{
int size = 0;
- CODEC_LOG (DEBUG, "[decode_video] write data to qemu\n");
- size = sizeof(size);
- memcpy (device_buf + size, &in_size, sizeof(in_size));
-// *(int *)(device_buf + size) = in_size;
- size += sizeof(in_size);
- memcpy (device_buf + size, &idx, sizeof(idx));
-// *(int *)(device_buf + size) = idx;
- size += sizeof(idx);
- memcpy (device_buf + size, &in_offset, sizeof(in_offset));
-// *(int64_t *)(device_buf + size) = in_offset;
- size += sizeof(in_offset);
- if (in_size > 0) {
+ memcpy(device_buf, &in_size, sizeof(in_size));
+ size = sizeof(in_size);
+ if (in_size > 0 ) {
memcpy (device_buf + size, in_buf, in_size);
- size += in_size;
}
- size -= sizeof(size);
- CODEC_LOG (DEBUG, "[decode_video] total: %d, inbuf size: %d\n", size, in_size);
- memcpy(device_buf, &size, sizeof(size));
-// *(int *)device_buf = size;
- CODEC_LOG (DEBUG, "[decode_video] leave\n");
+ CODEC_LOG (DEBUG, "decode_video. inbuf_size: %d\n", in_size);
}
+
int
-emul_avcodec_decode_video_from (CodecContext *ctx,
- int *got_picture_ptr,
- uint8_t *device_buf)
+_codec_decode_video_meta_from (VideoData *video,
+ int *got_picture_ptr,
+ uint8_t *device_buf)
{
int len = 0, size = 0;
- CODEC_LOG (DEBUG, "[decode_video] read data from qemu.\n");
- memcpy (&len, (uint8_t *)device_buf, sizeof(len));
-// len = *(int *)device_buf;
+ CODEC_LOG (DEBUG, "decode_video. read data from qemu.\n");
+
+ memcpy (&len, device_buf, sizeof(len));
size = sizeof(len);
memcpy (got_picture_ptr,
- (uint8_t *)device_buf + size, sizeof(*got_picture_ptr));
-// *got_picture_ptr = *(int *)(device_buf + size);
+ device_buf + size, sizeof(*got_picture_ptr));
size += sizeof(*got_picture_ptr);
- memcpy (&ctx->video, (uint8_t *)device_buf + size, sizeof(ctx->video));
-// ctx->video = *(VideoData *)(device_buf + size);
+ memcpy (video, device_buf + size, sizeof(VideoData));
- CODEC_LOG (DEBUG, "[decode_video] len: %d, have_data: %d\n", len, *got_picture_ptr);
+ CODEC_LOG (DEBUG, "decode_video. len: %d, have_data: %d\n",
+ len, *got_picture_ptr);
return len;
}
void
-emul_avcodec_decode_audio_to (uint8_t *in_buf,
- int in_size,
- uint8_t *device_buf)
+_codec_decode_audio_meta_to (int in_size, uint8_t *device_buf)
+{
+ memcpy (device_buf, &in_size, sizeof(in_size));
+}
+
+
+void
+_codec_decode_audio_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
{
int size = 0;
- size = sizeof(size);
- memcpy (device_buf + size, &in_size, sizeof(in_size));
-// *(int *)(device_buf + size) = in_size;
- size += sizeof(in_size);
+ memcpy (device_buf, &in_size, sizeof(in_size));
+ size = sizeof(in_size);
if (in_size > 0) {
memcpy (device_buf + size, in_buf, in_size);
- size += in_size;
}
- size -= sizeof(size);
- memcpy (device_buf, &size, sizeof(size));
-// *(int *)device_buf = size;
-
- CODEC_LOG (DEBUG, "[decode_audio] write size: %d, inbuf_size: %d\n", size, in_size);
+ CODEC_LOG (DEBUG, "decode_audio. inbuf_size: %d\n", in_size);
}
int
-emul_avcodec_decode_audio_from (CodecContext *ctx, int *frame_size_ptr,
- int16_t *samples, uint8_t *device_buf)
+_codec_decode_audio_meta_from (AudioData *audio, int *frame_size_ptr,
+ uint8_t *device_buf)
{
int len = 0, size = 0;
- CODEC_LOG (DEBUG, "[decode_audio] read data\n");
- memcpy (&ctx->audio.channel_layout,
- (uint8_t *)device_buf, sizeof(ctx->audio.channel_layout));
-// ctx->audio.channel_layout = *(int64_t *)device_buf;
- size = sizeof(ctx->audio.channel_layout);
- memcpy (&len, (uint8_t *)device_buf + size, sizeof(len));
-// len = *(int *)(device_buf + size);
+ CODEC_LOG (DEBUG, "decode_audio. read data from device.\n");
+
+ memcpy (&audio->channel_layout,
+ device_buf, sizeof(audio->channel_layout));
+ size = sizeof(audio->channel_layout);
+ memcpy (&len, device_buf + size, sizeof(len));
size += sizeof(len);
- memcpy (frame_size_ptr,
- (uint8_t *)device_buf + size, sizeof(*frame_size_ptr));
-// frame_size_ptr = *(int *)(device_buf + size);
- size += sizeof(*frame_size_ptr);
- CODEC_LOG (DEBUG, "[decode_audio] len: %d, frame_size: %d\n",
+ memcpy (frame_size_ptr, device_buf + size, sizeof(*frame_size_ptr));
+
+ CODEC_LOG (DEBUG, "decode_audio. len: %d, frame_size: %d\n",
len, (*frame_size_ptr));
-#if 1
- if (len > 0) {
- memcpy (samples,
- (uint8_t *)device_buf + size, FF_MAX_AUDIO_FRAME_SIZE);
- }
-#endif
return len;
}
void
-emul_avcodec_encode_video_to (uint8_t *in_buf, int in_size,
- int64_t in_timestamp, uint8_t *device_buf)
+_codec_decode_audio_outbuf (int outbuf_size, int16_t *samples, uint8_t *device_buf)
{
- int size = 0;
+ CODEC_LOG (DEBUG, "decode_audio. read outbuf %d\n", outbuf_size);
+ memcpy (samples, device_buf, outbuf_size);
+}
- size = sizeof(size);
+void
+_codec_encode_video_meta_to (int in_size, int64_t in_timestamp, uint8_t *device_buf)
+{
+ CODEC_LOG (DEBUG, "encode_video. write data to device.\n");
- CODEC_LOG (DEBUG, "[encode_video] write data to qemu\n");
- memcpy ((uint8_t *)device_buf + size, &in_size, sizeof(in_size));
+ memcpy (device_buf, &in_size, sizeof(in_size));
+ memcpy (device_buf + sizeof(in_size), &in_timestamp, sizeof(in_timestamp));
+}
+
+void
+_codec_encode_video_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
+{
+ int size = 0;
+
+ memcpy ((uint8_t *)device_buf, &in_size, sizeof(in_size));
size += sizeof(in_size);
- memcpy ((uint8_t *)device_buf + size, &in_timestamp, sizeof(in_timestamp));
- size += sizeof(in_timestamp);
if (in_size > 0) {
- memcpy ((uint8_t *)device_buf + size, in_buf, in_size);
- size += in_size;
+ memcpy (device_buf + size, in_buf, in_size);
}
-
- size -= sizeof(size);
- memcpy (device_buf, &size, sizeof(size));
-
- CODEC_LOG (DEBUG, "[encode_video] write data: %d\n", size);
+ CODEC_LOG (DEBUG, "encode_video. inbuf_size: %d\n", in_size);
}
-int
-emul_avcodec_encode_video_from (uint8_t *out_buf,
- int out_size,
- uint8_t *device_buf)
+void
+_codec_encode_video_outbuf (int len, uint8_t *out_buf, uint8_t *device_buf)
{
- int len, size;
+// int len, size;
- CODEC_LOG (DEBUG, "[encode_video] read data\n");
- memcpy (&len, (uint8_t *)device_buf, sizeof(len));
- size = sizeof(len);
- memcpy (out_buf, (uint8_t *)device_buf + size, out_size);
+ CODEC_LOG (DEBUG, "encode_video. read data from device.\n");
- return len;
+// memcpy (&len, device_buf, sizeof(len));
+// size = sizeof(len);
+ memcpy (out_buf, device_buf, len);
+
+// return len;
}
void
-emul_avcodec_encode_audio_to (int out_size, int in_size,
- uint8_t *in_buf, uint8_t *device_buf)
+_codec_encode_audio_meta_to (int max_size, int in_size, uint8_t *device_buf)
{
int size = 0;
+
+ CODEC_LOG (DEBUG, "encode_audio. write data to device.\n");
- size = sizeof(size);
- CODEC_LOG (DEBUG, "[encode_audio] write data to qemu\n");
+ memcpy (device_buf, &in_size, sizeof(in_size));
+ size = sizeof(in_size);
+ memcpy (device_buf + size, &max_size, sizeof(max_size));
+}
- memcpy (device_buf + size, &in_size, sizeof(in_size));
- size += sizeof(in_size);
- memcpy (device_buf + size, &out_size, sizeof(out_size));
- size += sizeof(out_size);
+void
+_codec_encode_audio_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
+{
+ int size = 0;
+
+ memcpy (device_buf, &in_size, sizeof(in_size));
+ size = sizeof(in_size);
if (in_size > 0) {
memcpy (device_buf + size, in_buf, in_size);
- size += in_size;
}
- size -= sizeof(size);
- memcpy (device_buf, &size, sizeof(size));
-
- CODEC_LOG (DEBUG, "[encode_audio] write data: %d\n", size);
+ CODEC_LOG (DEBUG, "encode_audio. inbuf_size: %d\n", in_size);
}
int
-emul_avcodec_encode_audio_from (uint8_t *out_buf,
- int out_size,
- uint8_t *device_buf)
+_codec_encode_audio_outbuf (uint8_t *out_buf, uint8_t *device_buf)
{
int len, size;
- CODEC_LOG (DEBUG, "[encode_audio] read data\n");
+ CODEC_LOG (DEBUG, "encode_audio. read data from device\n");
+
memcpy (&len, (uint8_t *)device_buf, sizeof(len));
size = sizeof(len);
- memcpy (out_buf, (uint8_t *)device_buf + size, out_size);
+ memcpy (out_buf, (uint8_t *)device_buf + size, len);
return len;
}
#include "gstemulcommon.h"
-void emul_avcodec_init_to (CodecContext *ctx, CodecElement *codec, uint8_t *device_buf);
+void _codec_init_meta_to (CodecContext *ctx, CodecElement *codec, uint8_t *device_buf);
-int emul_avcodec_init_from (CodecContext *ctx, CodecElement *codec, uint8_t *device_buf);
+int _codec_init_meta_from (CodecContext *ctx, int media_type, uint8_t *device_buf);
-void emul_avcodec_decode_video_to (uint8_t *in_buf, int in_size, int idx,
- int64_t in_offset, uint8_t *device_buf);
+void _codec_decode_video_meta_to (int in_size, int idx, int64_t in_offset, uint8_t *device_buf);
-int emul_avcodec_decode_video_from (CodecContext *ctx, int *got_picture_ptr,
+void _codec_decode_video_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf);
+
+int _codec_decode_video_meta_from (VideoData *video, int *got_picture_ptr,
+ uint8_t *device_buf);
+
+void _codec_decode_audio_meta_to (int in_size, uint8_t *device_buf);
+
+
+void _codec_decode_audio_inbuf (uint8_t *in_buf, int in_size,
+ uint8_t *device_buf);
+
+int _codec_decode_audio_meta_from (AudioData *audio, int *frame_size_ptr,
uint8_t *device_buf);
-void emul_avcodec_decode_audio_to (uint8_t *in_buf, int in_size,
+void _codec_decode_audio_outbuf (int outbuf_size, int16_t *samples,
uint8_t *device_buf);
-int emul_avcodec_decode_audio_from (CodecContext *ctx, int *frame_size_ptr,
- int16_t *samples, uint8_t *device_buf);
+void _codec_encode_video_meta_to (int in_size, int64_t in_timestamp, uint8_t *device_buf);
-void emul_avcodec_encode_video_to (uint8_t *in_buf, int in_size,
- int64_t in_timestamp, uint8_t *device_buf);
+void _codec_encode_video_inbuf (uint8_t *in_buf, int in_size,
+ uint8_t *device_buf);
-int emul_avcodec_encode_video_from (uint8_t *out_buf, int out_size, uint8_t *device_buf);
+// int _codec_encode_video_outbuf (uint8_t *out_buf, uint8_t *device_buf);
+void _codec_encode_video_outbuf (int len, uint8_t *outbuf, uint8_t *device_buf);
-void emul_avcodec_encode_audio_to (int out_size, int in_size, uint8_t *in_buf, uint8_t *device_buf);
+void _codec_encode_audio_meta_to (int max_size, int in_size, uint8_t *device_buf);
-int emul_avcodec_encode_audio_from (uint8_t *out_buf, int out_size, uint8_t *device_buf);
+void _codec_encode_audio_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf);
+int _codec_encode_audio_outbuf (uint8_t *out_buf, uint8_t *device_buf);
int32_t api_index;
int32_t ctx_index;
uint32_t mem_offset;
- uint32_t mem_type;
} CodecIOParams;
typedef struct _CodecDeviceMem {
} CodecElement;
typedef struct _VideoData {
- int width, height;
- int fps_n, fps_d;
- int par_n, par_d;
- int pix_fmt, bpp;
- int ticks_per_frame;
+ int32_t width, height;
+ int32_t fps_n, fps_d;
+ int32_t par_n, par_d;
+ int32_t pix_fmt, bpp;
+ int32_t ticks_per_frame;
} VideoData;
typedef struct _AudioData {
- int channels, sample_rate;
- int block_align, depth;
- int sample_fmt, frame_size;
- int bits_per_smp_fmt;
+ int32_t channels, sample_rate;
+ int32_t block_align, depth;
+ int32_t sample_fmt, frame_size;
+ int32_t bits_per_sample_fmt;
int64_t channel_layout;
} AudioData;
typedef struct _CodecContext {
- CodecElement *codec;
- int index;
+ union {
+ VideoData video;
+ AudioData audio;
+ };
- int bit_rate;
- int codec_tag;
- int codecdata_size;
+ int32_t bit_rate;
+ int32_t codec_tag;
+
+ int32_t codecdata_size;
uint8_t *codecdata;
- VideoData video;
- AudioData audio;
+ CodecElement *codec;
+ int32_t index;
} CodecContext;
enum CODEC_FUNC_TYPE {
CODEC_CMD_SECURE_MEMORY = 30,
CODEC_CMD_RELEASE_MEMORY,
CODEC_CMD_USE_DEVICE_MEM,
+ CODEC_CMD_REQ_FROM_SMALL_MEMORY,
+ CODEC_CMD_REQ_FROM_MEDIUM_MEMORY,
+ CODEC_CMD_REQ_FROM_LARGE_MEMORY,
+ CODEC_CMD_S_SECURE_BUFFER,
+ CODEC_CMD_M_SECURE_BUFFER,
+ CODEC_CMD_L_SECURE_BUFFER,
};
+
+#define CODEC_META_DATA_SIZE 256
+
+// CODEC_CMD_REQ_TO_SMALL_MEMORY
+// CODEC_CMD_REQ_FROM_SMALL_MEMORY
+// CODEC_CMD_REQ_TO_LARGE_MEMORY
+// CODEC_CMD_REQ_FROM_LARGE_MEMORY
+
enum CODEC_MEDIA_TYPE {
AVMEDIA_TYPE_UNKNOWN = -1,
AVMEDIA_TYPE_VIDEO,
gst_segment_init (&emuldec->segment, GST_FORMAT_TIME);
emuldec->dev = g_malloc0 (sizeof(CodecDevice));
-
+ if (!emuldec->dev) {
+ CODEC_LOG (ERR, "failed to allocate memory.\n");
+ }
}
static void
break;
case GST_EVENT_FLUSH_STOP:
{
+ printf("[%s][%d] GST_EVET_FLUSH_STOP\n", __func__, __LINE__);
#if 0
if (emuldec->opened) {
// TODO: what does avcodec_flush_buffers do?
if (gst_emul_avcodec_open (emuldec->context,
oclass->codec, emuldec->dev) < 0) {
gst_emuldec_close (emuldec);
- GST_DEBUG_OBJECT (emuldec,
+ GST_ERROR_OBJECT (emuldec,
"maru_%sdec: Failed to open codec", oclass->codec->name);
+ return FALSE;
}
emuldec->opened = TRUE;
return GST_FLOW_ERROR;
}
+ CODEC_LOG (DEBUG, "outbuf size of decoded video: %d\n", pict_size);
+
+ if (pict_size < (256 * 1024)) {
/* GstPadBufferAllocFunction is mostly overridden by elements that can
* provide a hardware buffer in order to avoid additional memcpy operations.
*/
- gst_pad_set_bufferalloc_function(
- GST_PAD_PEER(emuldec->srcpad),
- (GstPadBufferAllocFunction) emul_buffer_alloc);
+ gst_pad_set_bufferalloc_function(
+ GST_PAD_PEER(emuldec->srcpad),
+ (GstPadBufferAllocFunction) codec_buffer_alloc);
+ } else {
+ CODEC_LOG (DEBUG, "request a large size of memory\n");
+ }
ret = gst_pad_alloc_buffer_and_set_caps (emuldec->srcpad,
GST_BUFFER_OFFSET_NONE, pict_size,
*outbuf = new_aligned_buffer (pict_size, GST_PAD_CAPS (emuldec->srcpad));
}
- emul_av_picture_copy (emuldec->context, GST_BUFFER_DATA (*outbuf),
+ codec_picture_copy (emuldec->context, GST_BUFFER_DATA (*outbuf),
GST_BUFFER_SIZE (*outbuf), emuldec->dev);
return ret;
CODEC_LOG (DEBUG, "decode video: input buffer size: %d\n", size);
len =
- emul_avcodec_decode_video (emuldec->context, data, size,
+ codec_decode_video (emuldec->context, data, size,
dec_info->idx, in_offset, outbuf,
&have_data, emuldec->dev);
CODEC_LOG (DEBUG, "decode audio, input buffer size: %d\n", size);
- len = emul_avcodec_decode_audio (emuldec->context,
+ len = codec_decode_audio (emuldec->context,
(int16_t *) GST_BUFFER_DATA (*outbuf), &have_data,
data, size, emuldec->dev);
GST_DEBUG_OBJECT (emuldec,
"Decode audio: len=%d, have_data=%d", len, have_data);
+// CODEC_LOG (INFO, "decode audio, sample_fmt: %d\n", emuldec->context->audio.sample_fmt);
+
if (len >= 0 && have_data > 0) {
GST_DEBUG_OBJECT (emuldec, "Creating output buffer");
if (!gst_emuldec_negotiate (emuldec, FALSE)) {
perror("Failed to open codec device.");
return -1;
}
+ dev->fd = fd;
// GST_DEBUG("succeeded to open %s. %d.\n", CODEC_DEV, fd);
CODEC_LOG (INFO, "succeeded to open %s. %d.\n", CODEC_DEV, fd);
dev->mem_info.index = dev->buf_size;
-#if 0
- CODEC_LOG("mem type: %d, index: %d, offset: %d\n",
- dev->mem_info.type, dev->mem_info.index, dev->mem_info.offset);
-#endif
CODEC_LOG (DEBUG, "before mmap. buf_size: %d\n", dev->buf_size);
-
mmapbuf = mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
- if (mmapbuf == (void *)-1) {
+ if (mmapbuf == MAP_FAILED) {
perror("Failed to map device memory of codec.");
- close(fd);
+ dev->buf = NULL;
return -1;
}
dev->fd = fd;
dev->buf = mmapbuf;
-//
if (media_type == AVMEDIA_TYPE_VIDEO) {
device_mem = mmapbuf;
device_fd = fd;
CODEC_LOG (INFO, "video type! mmapbuf: %p fd: %d\n", mmapbuf, fd);
- } else {
- CODEC_LOG (INFO, "don't need to set device_mem because media type is not video. %d\n", media_type);
}
-//
+#if 0
+ else {
+ CODEC_LOG (INFO, "don't need to set device_mem because media type is not video. %d\n", media_type);
+ }
+#endif
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
}
dev->buf = NULL;
-// ioctl(fd, CODEC_CMD_RELEASE_DEVICE_MEM, &dev->mem_info);
ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &dev->mem_info.offset);
CODEC_LOG (INFO, "close %s.\n", CODEC_DEV);
perror("failed to open device.\n");
return -1;
}
- ret = emul_avcodec_init (ctx, codec, dev);
+ ret = codec_init (ctx, codec, dev);
g_static_mutex_unlock (&gst_avcodec_mutex);
return ret;
g_static_mutex_lock (&gst_avcodec_mutex);
CODEC_LOG (DEBUG, "gst_emul_avcodec_close\n");
- emul_avcodec_deinit (ctx, dev);
+ codec_deinit (ctx, dev);
ret = gst_emul_codec_device_close (dev);
g_static_mutex_unlock (&gst_avcodec_mutex);
emulenc->context.video.fps_n, emulen->context.video.fps_d);
#endif
+ // TODO: check whether this func needs or not.
gst_emulenc_setup_working_buf (emulenc);
ret_size =
- emul_avcodec_encode_video (emulenc->context, emulenc->working_buf,
+ codec_encode_video (emulenc->context, emulenc->working_buf,
emulenc->working_buf_size, GST_BUFFER_DATA (buffer),
GST_BUFFER_SIZE (buffer), GST_BUFFER_TIMESTAMP (buffer),
emulenc->dev);
}
}
#endif
+#if 1
+ {
+ int ret;
+ uint32_t mem_offset;
+ uint8_t *working_buf = NULL;
+
+ mem_offset = emulenc->dev->mem_info.offset;
+ working_buf = emulenc->dev->buf + mem_offset;
+ if (!working_buf) {
+ } else {
+ CODEC_LOG (INFO,
+ "encoded video. mem_offset = 0x%x\n", mem_offset);
+
+ outbuf = gst_buffer_new_and_alloc (ret_size);
+// memcpy (GST_BUFFER_DATA (outbuf), emulenc->working_buf, ret_size);
+ memcpy (GST_BUFFER_DATA (outbuf), working_buf, ret_size);
+ GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
+ GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
+ }
+
+ ret = ioctl(emulenc->dev->fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
+ if (ret < 0) {
+ CODEC_LOG (ERR, "failed to release used buffer\n");
+ }
+ }
+#endif
- outbuf = gst_buffer_new_and_alloc (ret_size);
- memcpy (GST_BUFFER_DATA (outbuf), emulenc->working_buf, ret_size);
- GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
- GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
#if 0
if (emulenc->context->coded_frame) {
if (!emulenc->context->coded_frame->key_frame) {
emulenc->buffer_size = max_size;
}
- res = emul_avcodec_encode_audio (emulenc->context, audio_out, max_size,
+ res = codec_encode_audio (emulenc->context, audio_out, max_size,
audio_in, in_size, emulenc->dev);
if (res < 0) {
", size %d", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration), in_size);
frame_size = ctx->audio.frame_size;
- osize = ctx->audio.bits_per_smp_fmt;
+ osize = ctx->audio.bits_per_sample_fmt;
if (frame_size > 1) {
guint avail, frame_bytes;
gst_emulenc_flush_buffers (GstEmulEnc *emulenc, gboolean send)
{
GstBuffer *outbuf, *inbuf;
- gint ret_size;
+ gint ret_size = 0;
GST_DEBUG_OBJECT (emulenc, "flushing buffers with sending %d", send);
while (!g_queue_is_empty (emulenc->delay)) {
emulenc_setup_working_buf (emulenc);
- ret_size = emul_avcodec_encode_video (emulenc->context,
+ ret_size = codec_encode_video (emulenc->context,
emulenc->working_buf, emulenc->working_buf_size, NULL, NULL, 0,
emulenc->dev);
} else {
GstCaps *temp;
enum PixelFormat i;
- CodecContext ctx = { 0, };
+ CodecContext ctx = { 0 };
caps = gst_caps_new_empty ();
for (i = 0; i <= PIX_FMT_NB; i++) {
GST_DEBUG ("context: %p, codec: %s, encode: %d, codec: %p",
ctx, name, encode, codec);
-#if 0
- if (codec) {
- }
-#endif
if (ctx) {
caps = gst_emul_smpfmt_to_caps (ctx->audio.sample_fmt, ctx, name);
} else {
GstCaps *temp;
int i;
- CodecContext ctx = { 0, };
+ CodecContext ctx = { 0 };
ctx.audio.channels = -1;
caps = gst_caps_new_empty ();
GstCaps *gst_emul_codecname_to_caps (const char *name, CodecContext *ctx, gboolean encode);
+void gst_emul_init_pix_fmt_info (void);
+
int gst_emul_avpicture_size (int pix_fmt, int width, int height);
int gst_emul_align_size (int buf_size);