#define CODEC_DEVICE_MEM_SIZE 32 * 1024 * 1024
-gpointer device_mem;
-int device_fd;
+gpointer device_mem = NULL;
+int device_fd = 0;
+int opened_cnt = 0;
int
gst_maru_codec_device_open (CodecDevice *dev, int media_type)
dev->mem_info.index = dev->buf_size;
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 == MAP_FAILED) {
- perror("Failed to map device memory of codec.");
- dev->buf = NULL;
- return -1;
+
+ g_static_mutex_lock (&gst_avcodec_mutex);
+ if (!device_mem) {
+ device_mem = mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ if (device_mem == MAP_FAILED) {
+ perror("Failed to map device memory of codec.");
+ dev->buf = NULL;
+ return -1;
+ }
}
+ dev->buf = device_mem;
+ opened_cnt++;
+ g_static_mutex_unlock (&gst_avcodec_mutex);
- CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", mmapbuf);
+ CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", dev->buf);
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);
- }
+ device_fd = fd;
CODEC_LOG (DEBUG, "leave: %s\n", __func__);
return -1;
}
- mmapbuf = dev->buf;
- if (!mmapbuf) {
- GST_ERROR("Failed to get mmaped memory address.\n");
- return -1;
- }
-
- CODEC_LOG (INFO, "Release memory region of %p.\n", mmapbuf);
- if (munmap(mmapbuf, CODEC_DEVICE_MEM_SIZE) != 0) {
- CODEC_LOG(ERR, "Failed to release memory region of %s.\n", CODEC_DEV);
+ g_static_mutex_lock (&gst_avcodec_mutex);
+ if (opened_cnt) {
+ if (opened_cnt == 1) {
+ CODEC_LOG (INFO, "Release memory region of %p.\n", device_mem);
+ if (munmap(device_mem, CODEC_DEVICE_MEM_SIZE) != 0) {
+ CODEC_LOG(ERR, "Failed to release memory region of %s.\n", CODEC_DEV);
+ }
+ }
+ opened_cnt--;
}
+ g_static_mutex_unlock (&gst_avcodec_mutex);
dev->buf = NULL;
ioctl(fd, CODEC_CMD_RELEASE_BUFFER, &dev->mem_info.offset);
{
int ret;
- g_static_mutex_lock (&gst_avcodec_mutex);
-
+// g_static_mutex_lock (&gst_avcodec_mutex);
if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
perror("failed to open device.\n");
return -1;
}
+
+ g_static_mutex_lock (&gst_avcodec_mutex);
ret = codec_init (ctx, codec, dev);
g_static_mutex_unlock (&gst_avcodec_mutex);
{
int ret;
- g_static_mutex_lock (&gst_avcodec_mutex);
-
CODEC_LOG (DEBUG, "gst_maru_avcodec_close\n");
+
+ g_static_mutex_lock (&gst_avcodec_mutex);
codec_deinit (ctx, dev);
+ g_static_mutex_unlock (&gst_avcodec_mutex);
ret = gst_maru_codec_device_close (dev);
- g_static_mutex_unlock (&gst_avcodec_mutex);
+// g_static_mutex_unlock (&gst_avcodec_mutex);
return ret;
}
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);
}
}
info = secure_device_mem (size);
- CODEC_LOG (DEBUG, "memory start: 0x%p, offset 0x%x\n",
- info.start, info.offset);
+ 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;
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);
+ CODEC_LOG (DEBUG, "%d of pict: %p , device_mem: %p\n",
+ ctx->index, pict, mmapbuf);
+ CODEC_LOG (DEBUG, "%d of picture_copy, mem_offset = 0x%x\n",
+ ctx->index, dev->mem_info.offset);
}
_codec_write_to_qemu (ctx->index, CODEC_PICTURE_COPY,