Does open and mmap operation once per a process. 75/12575/1
authorKitae Kim <kt920.kim@samsung.com>
Tue, 19 Nov 2013 06:23:02 +0000 (15:23 +0900)
committerKitae Kim <kt920.kim@samsung.com>
Tue, 19 Nov 2013 08:23:47 +0000 (17:23 +0900)
Change-Id: I4fb1b95d39dad4b0488f662c6f8df3167891a737
Signed-off-by: Kitae Kim <kt920.kim@samsung.com>
src/gstmaru.h
src/gstmarudevice.c
src/gstmaruinterface.c
src/gstmarumem.c

index 3ff33c7..272f6ca 100644 (file)
@@ -61,7 +61,7 @@ enum codec_log_level {
 #define CODEC_LOG(level, fmt, ...) \
   do { \
     if (level <= INFO) \
-      printf("[gst-maru][%d] " fmt, __LINE__, ##__VA_ARGS__); \
+      printf("[gst-maru][%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
   } while (0)
 
 #define FF_INPUT_BUFFER_PADDING_SIZE  8
index 1ffb696..ab8122e 100644 (file)
@@ -52,48 +52,52 @@ int opened_cnt = 0;
 int
 gst_maru_codec_device_open (CodecDevice *dev, int media_type)
 {
-  int fd;
-
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
-  CODEC_LOG (INFO, "before opening a device. %d\n", dev->fd);
-  if ((fd = open(CODEC_DEV, O_RDWR)) < 0) {
-    perror("Failed to open codec device.");
-    return -1;
+  g_mutex_lock (&gst_avcodec_mutex);
+  if (!device_fd) {
+    if ((device_fd = open(CODEC_DEV, O_RDWR)) < 0) {
+      perror("Failed to open codec device.");
+      g_mutex_unlock (&gst_avcodec_mutex);
+      return -1;
+    }
+    CODEC_LOG (INFO, "succeeded to open %s. %d.\n", CODEC_DEV, device_fd);
+  } else {
+    CODEC_LOG (DEBUG, "codec device is already opened\n");
   }
-  dev->fd = fd;
+  g_mutex_unlock (&gst_avcodec_mutex);
+
+  dev->fd = device_fd;
 
   // FIXME
   dev->buf_size = CODEC_DEVICE_MEM_SIZE;
-  //
-
-  CODEC_LOG (INFO, "succeeded to open %s. %d.\n", CODEC_DEV, fd);
+  CODEC_LOG (DEBUG, "mmap_size: %d\n", dev->buf_size);
   dev->mem_info.index = dev->buf_size;
 
-  CODEC_LOG (DEBUG, "before mmap. buf_size: %d\n", dev->buf_size);
-
   g_mutex_lock (&gst_avcodec_mutex);
   if (!device_mem) {
-    device_mem = mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
-        MAP_SHARED, fd, 0);
+    device_mem =
+      mmap (NULL, CODEC_DEVICE_MEM_SIZE, PROT_READ | PROT_WRITE,
+          MAP_SHARED, device_fd, 0);
     if (device_mem == MAP_FAILED) {
       perror("Failed to map device memory of codec.");
-      dev->buf = NULL;
+#if 0
+      close (device_fd);
+      device_fd = 0;
+#endif
       g_mutex_unlock (&gst_avcodec_mutex);
       return -1;
     }
+    CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", device_mem);
+  } else {
+    CODEC_LOG (DEBUG, "mapping device memory is already done\n");
   }
   opened_cnt++;
+  CODEC_LOG (DEBUG, "open count: %d\n", opened_cnt);
   g_mutex_unlock (&gst_avcodec_mutex);
 
   dev->buf = device_mem;
 
-  CODEC_LOG (INFO, "succeeded to map device memory: %p.\n", dev->buf);
-  dev->fd = fd;
-  if(media_type == AVMEDIA_TYPE_VIDEO) {
-    device_fd = fd;
-  }
-
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
   return 0;
@@ -120,19 +124,18 @@ gst_maru_codec_device_close (CodecDevice *dev)
     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);
-      device_mem = NULL;
     }
+    device_mem = NULL;
+
+    CODEC_LOG (INFO, "close %s.\n", CODEC_DEV);
+    if (close(fd) != 0) {
+      GST_ERROR("Failed to close %s. fd: %d\n", CODEC_DEV, fd);
+    }
+    device_fd = 0;
   }
-  device_mem = NULL;
   g_mutex_unlock (&gst_avcodec_mutex);
-
   dev->buf = NULL;
 
-  CODEC_LOG (INFO, "close %s.\n", CODEC_DEV);
-  if (close(fd) != 0) {
-    GST_ERROR("Failed to close %s. fd: %d\n", CODEC_DEV, fd);
-  }
-
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
   return 0;
@@ -145,7 +148,6 @@ gst_maru_avcodec_open (CodecContext *ctx,
 {
   int ret;
 
-
   if (gst_maru_codec_device_open (dev, codec->media_type) < 0) {
     perror("failed to open device.\n");
     return -1;
index 953a5bd..ebc9d34 100644 (file)
@@ -47,6 +47,11 @@ typedef struct _CodecHeader {
   uint32_t  mem_offset;
 } CodecHeader;
 
+typedef struct _CodecBufferId {
+  uint32_t  buffer_index;
+  uint32_t  buffer_size;
+} CodecBufferId;
+
 #define CODEC_META_DATA_SIZE    256
 #define GET_OFFSET(buffer)      ((uint32_t)buffer - (uint32_t)device_mem)
 #define SMALLDATA               0
@@ -142,18 +147,20 @@ codec_buffer_alloc_and_copy (GstPad *pad, guint64 offset, guint size,
                   GstCaps *caps, GstBuffer **buf)
 {
   struct mem_info info;
-  uint32_t opaque;
+  CodecBufferId opaque;
   int ret = 0;
   GstMaruDec *marudec;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
-  opaque = size;
-
   *buf = gst_buffer_new ();
 
   marudec = (GstMaruDec *)gst_pad_get_element_private(pad);
 
+  opaque.buffer_index = marudec->context->index;
+  opaque.buffer_size = size;
+
+  CODEC_LOG (DEBUG, "buffer_and_copy. ctx_id: %d\n", marudec->context->index);
   _codec_write_to_qemu (marudec->context->index, CODEC_PICTURE_COPY,
                         0, marudec->dev->fd);
 
@@ -168,14 +175,14 @@ codec_buffer_alloc_and_copy (GstPad *pad, guint64 offset, guint size,
 
     GST_BUFFER_FREE_FUNC (*buf) = g_free;
 
-    memcpy (info.start, (uint32_t)device_mem + opaque, size);
-    release_device_mem(marudec->dev->fd, (uint32_t)device_mem + opaque);
+    memcpy (info.start, (uint32_t)device_mem + opaque.buffer_size, size);
+    release_device_mem(marudec->dev->fd, (uint32_t)device_mem + opaque.buffer_size);
 
     CODEC_LOG (DEBUG, "we secured last buffer, so we will use heap buffer\n");
   } else {
     // address of "device_mem" and "opaque" is aleady aligned.
-    info.start = (gpointer)((uint32_t)device_mem + opaque);
-    info.offset = opaque;
+    info.start = (gpointer)((uint32_t)device_mem + opaque.buffer_size);
+    info.offset = opaque.buffer_size;
 
     GST_BUFFER_FREE_FUNC (*buf) = codec_buffer_free;
 
@@ -296,7 +303,8 @@ codec_decode_audio (CodecContext *ctx, int16_t *samples,
 {
   int len = 0, ret = 0, size = 8;
   gpointer buffer = NULL;
-  uint32_t meta_offset = 0, opaque = 0;
+  uint32_t meta_offset = 0;
+  CodecBufferId opaque;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
@@ -315,22 +323,26 @@ codec_decode_audio (CodecContext *ctx, int16_t *samples,
   dev->mem_info.offset = GET_OFFSET(buffer);
   _codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, GET_OFFSET(buffer), dev->fd);
 
-  opaque = SMALLDATA; // FIXME: how can we know output data size ?
+  opaque.buffer_index = ctx->index;
+  opaque.buffer_size = SMALLDATA;
+  // FIXME: how can we know output data size ?
   ret = ioctl (dev->fd, CODEC_CMD_PUT_DATA_INTO_BUFFER, &opaque);
   if (ret < 0) {
     return -1;
   }
-  CODEC_LOG (DEBUG, "after decode_audio. ctx_id: %d, buffer = 0x%x\n", ctx->index, (int)device_mem + opaque);
+  CODEC_LOG (DEBUG, "after decode_audio. ctx_id: %d, buffer = 0x%x\n",
+            ctx->index, (int)device_mem + opaque.buffer_size);
 
   len =
     _codec_decode_audio_meta_from (&ctx->audio, have_data, device_mem + meta_offset + size);
   if (len > 0) {
-    _codec_decode_audio_outbuf (*have_data, samples, device_mem + opaque);
+    _codec_decode_audio_outbuf (*have_data, samples, device_mem + opaque.buffer_size);
   } else {
     CODEC_LOG (DEBUG, "decode_audio failure. ctx_id: %d\n", ctx->index);
   }
+  CODEC_LOG (DEBUG, "decode_audio. ctx_id: %d len: %d\n", ctx->index, len);
 
-  release_device_mem(dev->fd, device_mem + opaque);
+  release_device_mem(dev->fd, device_mem + opaque.buffer_size);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
@@ -344,7 +356,8 @@ codec_encode_video (CodecContext *ctx, uint8_t *out_buf,
 {
   int len = 0, ret = 0, size = 8;
   gpointer buffer = NULL;
-  uint32_t meta_offset = 0, opaque = 0;
+  uint32_t meta_offset = 0;
+  CodecBufferId opaque;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
@@ -362,22 +375,24 @@ codec_encode_video (CodecContext *ctx, uint8_t *out_buf,
   dev->mem_info.offset = GET_OFFSET(buffer);
   _codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, GET_OFFSET(buffer), dev->fd);
 
-  opaque = SMALLDATA; // FIXME: how can we know output data size ?
+  opaque.buffer_index = ctx->index;
+  opaque.buffer_size = SMALLDATA;
+  // FIXME: how can we know output data size ?
   ret = ioctl (dev->fd, CODEC_CMD_PUT_DATA_INTO_BUFFER, &opaque);
   if (ret < 0) {
     return -1;
   }
-  CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", opaque);
+  CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", opaque.buffer_size);
 
   memcpy (&len, device_mem + meta_offset + size, sizeof(len));
 
   CODEC_LOG (DEBUG, "encode_video. outbuf size: %d\n", len);
   if (len > 0) {
-    memcpy (out_buf, device_mem + opaque, len);
-    dev->mem_info.offset = opaque;
+    memcpy (out_buf, device_mem + opaque.buffer_size, len);
+    dev->mem_info.offset = opaque.buffer_size;
   }
 
-  release_device_mem(dev->fd, device_mem + opaque);
+  release_device_mem(dev->fd, device_mem + opaque.buffer_size);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
@@ -391,13 +406,14 @@ codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
 {
   int len = 0, ret = 0, size = 8;
   gpointer buffer = NULL;
-  uint32_t meta_offset = 0, opaque = 0;
+  uint32_t meta_offset = 0;
+  CodecBufferId opaque;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
   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, opaque,
+  size = _codec_header (CODEC_ENCODE_AUDIO, opaque.buffer_size,
                             device_mem + meta_offset);
   _codec_encode_audio_meta_to (max_size, in_size, device_mem + meta_offset + size);
 
@@ -410,17 +426,19 @@ codec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
   dev->mem_info.offset = GET_OFFSET(buffer);
   _codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, GET_OFFSET(buffer), dev->fd);
 
-  opaque = SMALLDATA; // FIXME: how can we know output data size ?
+  opaque.buffer_index = ctx->index;
+  opaque.buffer_size = SMALLDATA;
+  // FIXME: how can we know output data size ?
   ret = ioctl (dev->fd, CODEC_CMD_PUT_DATA_INTO_BUFFER, &opaque);
   if (ret < 0) {
     return -1;
   }
 
-  CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", opaque);
+  CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", opaque.buffer_size);
 
-  len = _codec_encode_audio_outbuf (out_buf, device_mem + opaque);
+  len = _codec_encode_audio_outbuf (out_buf, device_mem + opaque.buffer_size);
 
-  release_device_mem(dev->fd, device_mem + opaque);
+  release_device_mem(dev->fd, device_mem + opaque.buffer_size);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
index 9be306c..7b0664d 100644 (file)
@@ -63,16 +63,9 @@ _codec_init_meta_to (CodecContext *ctx,
 
   size = _codec_info_data (codec, device_buf);
 
-  CODEC_LOG (DEBUG, "context_id: %d, name: %s, media type: %s\n",
+  CODEC_LOG (INFO, "context_id: %d, name: %s, media type: %s\n",
     ctx->index, codec->name, codec->media_type ? "AUDIO" : "VIDEO");
 
-  if (codec->media_type == AVMEDIA_TYPE_AUDIO) {
-    CODEC_LOG (DEBUG,
-      "before init. audio sample_fmt: %d\n", ctx->audio.sample_fmt);
-    CODEC_LOG (DEBUG,
-      "before init. audio block_align: %d\n", ctx->audio.block_align);
-  }
-
   CODEC_LOG (DEBUG, "init. write data to qemu, size: %d\n", size);
   memcpy (device_buf + size, ctx, sizeof(CodecContext) - 12);
   size += (sizeof(CodecContext) - 12);
@@ -102,7 +95,7 @@ _codec_init_meta_from (CodecContext *ctx,
       ctx->audio.frame_size = audio.frame_size;
       ctx->audio.bits_per_sample_fmt = audio.bits_per_sample_fmt;
 #endif
-      CODEC_LOG (INFO,
+      CODEC_LOG (DEBUG,
         "audio sample_fmt: %d\n", *(int *)(device_buf + size));
 
       memcpy(&ctx->audio.sample_fmt, device_buf + size, sizeof(audio.sample_fmt));
@@ -111,7 +104,7 @@ _codec_init_meta_from (CodecContext *ctx,
       size += sizeof(audio.frame_size);
       memcpy(&ctx->audio.bits_per_sample_fmt, device_buf + size, sizeof(audio.bits_per_sample_fmt));
 
-      CODEC_LOG (INFO,
+      CODEC_LOG (DEBUG,
         "after init. audio sample_fmt: %d\n", ctx->audio.sample_fmt);
     }
   } else {
@@ -130,8 +123,7 @@ _codec_decode_video_meta_to (int in_size, int idx, int64_t in_offset, uint8_t *d
 }
 
 void
-_codec_decode_video_inbuf (uint8_t *in_buf, int in_size,
-                              uint8_t *device_buf)
+_codec_decode_video_inbuf (uint8_t *in_buf, int in_size, uint8_t *device_buf)
 {
   int size = 0;
 
@@ -144,7 +136,6 @@ _codec_decode_video_inbuf (uint8_t *in_buf, int in_size,
   CODEC_LOG (DEBUG, "decode_video. inbuf_size: %d\n", in_size);
 }
 
-
 int
 _codec_decode_video_meta_from (VideoData *video,
                               int *got_picture_ptr,