fixed sync problem when using multi contexts.
authorKitae Kim <kt920.kim@samsung.com>
Thu, 22 Aug 2013 05:43:16 +0000 (14:43 +0900)
committerKitae Kim <kt920.kim@samsung.com>
Wed, 28 Aug 2013 05:52:06 +0000 (14:52 +0900)
When storing output of decoding video into device memory to reduce the overhead of memory copy,
device memory pointer could be wrong because the pointer only considered video context.

Change-Id: I2206bedef8bddfcdbddd8619aabc30f06481b585
Signed-off-by: Kitae Kim <kt920.kim@samsung.com>
src/gstemulapi.c
src/gstemulapi2.c
src/gstemulcommon.h
src/gstemuldec.c
src/gstemuldev.c
src/gstemulenc.c

index aac534d..53ca6e1 100644 (file)
@@ -55,43 +55,54 @@ struct mem_info {
     uint32_t offset;
 };
 
-static struct mem_info secure_device_mem()
+static struct mem_info
+secure_device_mem (void)
 {
   uint32_t mem_offset = 0;
   struct mem_info info;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
-  ioctl(device_fd, CODEC_CMD_SECURE_MEMORY, &mem_offset);
+  ioctl (device_fd, CODEC_CMD_SECURE_MEMORY, &mem_offset);
   info.start = (gpointer)((uint32_t)device_mem + mem_offset);
   info.offset = mem_offset;
 
-  CODEC_LOG (DEBUG, "caramis0 = 0x%x\n", mem_offset);
+  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);
+  }
+#endif
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
   return info;
 }
 
-static void release_device_mem(gpointer start)
+static void
+release_device_mem (gpointer start)
 {
   uint32_t offset = start - device_mem;
+
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
-  ioctl(device_fd, CODEC_CMD_RELEASE_MEMORY, &offset);
+  ioctl (device_fd, CODEC_CMD_RELEASE_MEMORY, &offset);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 }
 
-static void emul_buffer_free(gpointer start)
+static void
+emul_buffer_free (gpointer start)
 {
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
-  release_device_mem(start);
+  release_device_mem (start);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 }
 
-GstFlowReturn emul_buffer_alloc(GstPad *pad, guint64 offset, guint size, GstCaps *caps, GstBuffer **buf)
+GstFlowReturn
+emul_buffer_alloc (GstPad *pad, guint64 offset, guint size,
+                  GstCaps *caps, GstBuffer **buf)
 {
   struct mem_info info;
 
@@ -99,14 +110,14 @@ GstFlowReturn emul_buffer_alloc(GstPad *pad, guint64 offset, guint size, GstCaps
 
   *buf = gst_buffer_new ();
 
-  info = secure_device_mem();
+  info = secure_device_mem ();
 
-  CODEC_LOG (DEBUG, "[%s] start: 0x%x, offset 0x%x\n", __func__, 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;
   GST_BUFFER_FREE_FUNC (*buf) = emul_buffer_free;
-
   GST_BUFFER_OFFSET (*buf) = offset;
 
   if (caps) {
@@ -114,6 +125,7 @@ GstFlowReturn emul_buffer_alloc(GstPad *pad, guint64 offset, guint size, GstCaps
   }
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
+
   return GST_FLOW_OK;
 }
 
@@ -143,15 +155,16 @@ emul_avcodec_init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
   CODEC_LOG (DEBUG, "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);
+
   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);
 
-
   ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
   ret = emul_avcodec_init_from (ctx, codec, mmapbuf + mem_offset);
 
-  ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
+  ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
 
   CODEC_LOG (DEBUG, "leave: %s, ret: %d\n", __func__, ret);
   return ret;
@@ -187,9 +200,8 @@ 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 fd;
+  int fd, len = 0;
   uint8_t *mmapbuf = NULL;
-  int len = 0;
   uint32_t mem_offset = 0;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
@@ -207,17 +219,19 @@ emul_avcodec_decode_video (CodecContext *ctx, uint8_t *in_buf, int in_size,
   }
 
   ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
-  CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, 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);
 
   dev->mem_info.offset = mem_offset;
   emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_VIDEO, dev);
 
   ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
-  CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
+  CODEC_LOG (DEBUG, "read, decode_video. mem_offset = 0x%x\n", mem_offset);
+
   len = emul_avcodec_decode_video_from (ctx, got_picture_ptr, mmapbuf + mem_offset);
 
-  ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
+  ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
   return len;
@@ -229,8 +243,6 @@ emul_av_picture_copy (CodecContext *ctx, uint8_t *pict,
 {
   int fd;
   void *mmapbuf = NULL;
-//  int copyback, usable;
-  uint32_t mem_offset = 0;
 
   CODEC_LOG (DEBUG, "enter: %s\n", __func__);
 
@@ -246,16 +258,21 @@ emul_av_picture_copy (CodecContext *ctx, uint8_t *pict,
     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);
+//  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);
-  ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM2, &(dev->mem_info.offset));
 
-  CODEC_LOG (DEBUG, "[%s] caramis1 = 0x%x\n", __func__, mem_offset);
+  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 0
   memcpy (pict, mmapbuf, pict_size);
-  ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
+//  ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
 #endif
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
@@ -286,16 +303,20 @@ emul_avcodec_decode_audio (CodecContext *ctx, int16_t *samples,
   }
 
   ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
+  CODEC_LOG (DEBUG, "decode audio1. mem_offset = 0x%x\n", mem_offset);
+
   emul_avcodec_decode_audio_to (in_buf, in_size, mmapbuf + mem_offset);
 
   dev->mem_info.offset = mem_offset;
   emul_codec_write_to_qemu (ctx->index, CODEC_DECODE_AUDIO, dev);
 
   ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
+  CODEC_LOG (DEBUG, "decode audio2. mem_offset = 0x%x\n", mem_offset);
+
   len =
     emul_avcodec_decode_audio_from (ctx, frame_size_ptr, samples, mmapbuf + mem_offset);
 
-  ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
+  ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
 
@@ -327,17 +348,19 @@ emul_avcodec_encode_video (CodecContext *ctx, uint8_t *out_buf,
   }
 
   ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
-  CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
+  CODEC_LOG (DEBUG, "write, encode_video. mem_offset = 0x%x\n", mem_offset);
+
   emul_avcodec_encode_video_to (in_buf, in_size, in_timestamp, mmapbuf);
 
   dev->mem_info.offset = mem_offset;
   emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_VIDEO, dev);
 
   ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
-  CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
+  CODEC_LOG (DEBUG, "read, encode_video. mem_offset = 0x%x\n", mem_offset);
+
   len = emul_avcodec_encode_video_from (out_buf, out_size, mmapbuf);
 
-  ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
+  ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
   return len;
@@ -368,17 +391,19 @@ emul_avcodec_encode_audio (CodecContext *ctx, uint8_t *out_buf,
   }
 
   ioctl (fd, CODEC_CMD_COPY_TO_DEVICE_MEM, &mem_offset);
-  CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, mem_offset);
+  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);
 
   dev->mem_info.offset = mem_offset;
   emul_codec_write_to_qemu (ctx->index, CODEC_ENCODE_AUDIO, dev);
 
   ioctl (fd, CODEC_CMD_COPY_FROM_DEVICE_MEM, &mem_offset);
-  CODEC_LOG (DEBUG, "[%s] mem_offset = 0x%x\n", __func__, 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);
 
-  ioctl (fd, CODEC_CMD_REMOVE_TASK_QUEUE, &mem_offset);
+  ioctl(fd, CODEC_CMD_RELEASE_MEMORY, &mem_offset);
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
   return len;
index 9a77564..226656c 100644 (file)
@@ -217,7 +217,7 @@ emul_avcodec_decode_audio_from (CodecContext *ctx, int *frame_size_ptr,
   size += sizeof(*frame_size_ptr);
   CODEC_LOG (DEBUG, "[decode_audio] len: %d, frame_size: %d\n",
           len, (*frame_size_ptr));
-#if 0
+#if 
   if (len > 0) {
     memcpy (samples,
       (uint8_t *)device_buf + size, FF_MAX_AUDIO_FRAME_SIZE);
index 307afbd..4cc735b 100644 (file)
@@ -136,7 +136,6 @@ typedef struct _CodecContext {
 } CodecContext;
 
 enum CODEC_FUNC_TYPE {
-//  CODEC_ELEMENT_INIT = 1,
   CODEC_INIT = 0,
   CODEC_DECODE_VIDEO,
   CODEC_ENCODE_VIDEO,
@@ -147,17 +146,14 @@ enum CODEC_FUNC_TYPE {
 };
 
 enum CODEC_IO_CMD {
-//  CODEC_CMD_ACQUIRE_DEVICE_MEM = 0,
-  CODEC_CMD_RELEASE_DEVICE_MEM = 5,
-  CODEC_CMD_REMOVE_TASK_QUEUE,
-  CODEC_CMD_COPY_TO_DEVICE_MEM,
+  CODEC_CMD_COPY_TO_DEVICE_MEM = 5,
   CODEC_CMD_COPY_FROM_DEVICE_MEM,
   CODEC_CMD_GET_VERSION = 20,
   CODEC_CMD_GET_ELEMENT,
   CODEC_CMD_GET_CONTEXT_INDEX,
   CODEC_CMD_SECURE_MEMORY = 30,
   CODEC_CMD_RELEASE_MEMORY,
-  CODEC_CMD_COPY_FROM_DEVICE_MEM2,
+  CODEC_CMD_USE_DEVICE_MEM,
 };
 
 enum CODEC_MEDIA_TYPE {
index f4b9201..6cd9f94 100644 (file)
@@ -441,9 +441,7 @@ gst_emuldec_init (GstEmulDec *emuldec)
   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
@@ -933,7 +931,8 @@ get_output_buffer (GstEmulDec *emuldec, GstBuffer **outbuf)
   /* 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),
+  gst_pad_set_bufferalloc_function(
+    GST_PAD_PEER(emuldec->srcpad),
     (GstPadBufferAllocFunction) emul_buffer_alloc);
 
   ret = gst_pad_alloc_buffer_and_set_caps (emuldec->srcpad,
@@ -951,15 +950,10 @@ get_output_buffer (GstEmulDec *emuldec, GstBuffer **outbuf)
     gst_buffer_unref (*outbuf);
     *outbuf = new_aligned_buffer (pict_size, GST_PAD_CAPS (emuldec->srcpad));
   }
-  gst_buffer_set_caps (*outbuf, GST_PAD_CAPS (emuldec->srcpad));
 
   emul_av_picture_copy (emuldec->context, GST_BUFFER_DATA (*outbuf),
     GST_BUFFER_SIZE (*outbuf), emuldec->dev);
 
-#if 0
-  GST_BUFFER_DATA (*outbuf) = emuldec->dev->buf;
-#endif
-
   return ret;
 }
 
@@ -1212,13 +1206,6 @@ gst_emuldec_audio_frame (GstEmulDec *emuldec, CodecElement *codec,
       (int16_t *) GST_BUFFER_DATA (*outbuf), &have_data,
       data, size, emuldec->dev);
 
-#if 0
-  GST_BUFFER_DATA (*outbuf) =
-    (uint8_t *)emuldec->dev->buf +
-    sizeof(emuldec->context->audio.channel_layout) +
-    sizeof(len) + sizeof(have_data);
-#endif
-
   GST_DEBUG_OBJECT (emuldec,
     "Decode audio: len=%d, have_data=%d", len, have_data);
 
index a24e08d..c02fbed 100644 (file)
@@ -56,6 +56,7 @@ gst_emul_codec_device_open (CodecDevice *dev, int media_type)
 
   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;
@@ -85,8 +86,13 @@ gst_emul_codec_device_open (CodecDevice *dev, int media_type)
   dev->buf = mmapbuf;
 
 //
-  device_mem = mmapbuf;
-  device_fd = fd;
+  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);
+  }
 //
 
   CODEC_LOG (DEBUG, "leave: %s\n", __func__);
@@ -120,7 +126,8 @@ gst_emul_codec_device_close (CodecDevice *dev)
   }
   dev->buf = NULL;
 
-  ioctl(fd, CODEC_CMD_RELEASE_DEVICE_MEM, &dev->mem_info);
+//  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);
   if (close(fd) != 0) {
index 8896084..78d2303 100644 (file)
@@ -347,7 +347,7 @@ gst_emulenc_getcaps (GstPad *pad)
 
   GST_DEBUG_OBJECT (emulenc, "getting caps");
 
-  if (!oclass->codec)  {
+  if (!oclass->codec) {
     GST_ERROR_OBJECT (emulenc, "codec element is null.");
     return NULL;
   }