codec: support running code on 64 bits 74/101874/3
authorPark Kyoung Won <kw0712.park@samsung.com>
Fri, 2 Dec 2016 09:14:11 +0000 (18:14 +0900)
committerPark Kyoung Won <kw0712.park@samsung.com>
Fri, 2 Dec 2016 09:58:02 +0000 (18:58 +0900)
- fixed casting errors and calculating CodecContext members size.

Change-Id: I21a009e1cabfe3c465b08d9f91576fb049f127d9
Signed-off-by: Park Kyoung Won <kw0712.park@samsung.com>
src/gstmaruinterface3.c
src/gstmarumem.c

index b108515..09d84bb 100644 (file)
@@ -52,7 +52,7 @@ enum IOCTL_CMD {
 typedef struct {
   uint32_t  api_index;
   uint32_t  ctx_index;
-  uintptr_t  mem_offset;
+  uint32_t  mem_offset;
   int32_t  buffer_size;
 } __attribute__((packed)) IOCTL_Data;
 
@@ -60,7 +60,8 @@ typedef struct {
 #define IOCTL_RW(CMD)           (_IOWR(BRILLCODEC_KEY, CMD, IOCTL_Data))
 
 #define CODEC_META_DATA_SIZE    256
-#define GET_OFFSET(buffer)      ((uintptr_t)buffer - (uintptr_t)device_mem)
+#define GET_OFFSET32(buffer)    ((uint32_t)((uintptr_t)buffer - (uintptr_t)device_mem))
+#define GET_OFFSET64(buffer)    ((uint64_t)((uintptr_t)buffer - (uintptr_t)device_mem))
 #define SMALLDATA               0
 
 #define OFFSET_PICTURE_BUFFER   0x100
@@ -74,7 +75,7 @@ static inline bool can_use_new_decode_api(void) {
 
 static int
 invoke_device_api(int fd, int32_t ctx_index, int32_t api_index,
-                          uintptr_t *mem_offset, int32_t buffer_size)
+                          uint32_t *mem_offset, int32_t buffer_size)
 {
   GST_DEBUG (" >> Enter");
   IOCTL_Data ioctl_data = { 0, };
@@ -109,8 +110,8 @@ secure_device_mem (int fd, guint ctx_id, guint buf_size, gpointer* buffer)
 
   ret = ioctl (fd, IOCTL_RW(IOCTL_CMD_SECURE_BUFFER), &data);
 
-  *buffer = (gpointer)(device_mem + data.mem_offset);
-  GST_DEBUG ("device_mem %p, offset_size 0x%"PRIXPTR, device_mem, data.mem_offset);
+  *buffer = (gpointer)(device_mem + (uintptr_t)data.mem_offset);
+  GST_DEBUG ("device_mem %p, offset_size %u", device_mem, data.mem_offset);
 
   GST_DEBUG (" >> Leave");
   return ret;
@@ -121,7 +122,7 @@ release_device_mem (int fd, gpointer start)
 {
   GST_DEBUG (" >> Enter");
   int ret;
-  uint32_t offset = start - device_mem;
+  uint32_t offset = GET_OFFSET32(start);
 
   GST_DEBUG ("release device_mem start: %p, offset: 0x%x", start, offset);
   ret = ioctl (fd, IOCTL_RW(IOCTL_CMD_RELEASE_BUFFER), &offset);
@@ -173,7 +174,7 @@ init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
   int opened = 0;
   gpointer buffer = NULL;
   int ret;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
 
   GST_DEBUG (" >> Enter");
   if ((ctx->index = get_context_index(dev->fd)) <= 0) {
@@ -192,7 +193,7 @@ init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
 
   codec_init_data_to (ctx, codec, buffer);
 
-  mem_offset = GET_OFFSET(buffer);
+  mem_offset = GET_OFFSET32(buffer);
   ret = invoke_device_api (dev->fd, ctx->index, CODEC_INIT, &mem_offset, SMALLDATA);
 
   if (ret < 0) {
@@ -201,7 +202,7 @@ init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
   }
 
   opened =
-    codec_init_data_from (ctx, codec->media_type, device_mem + mem_offset);
+    codec_init_data_from (ctx, codec->media_type, device_mem + (uintptr_t)mem_offset);
 
   if (opened < 0) {
     GST_ERROR ("failed to open Context for %s", codec->name);
@@ -209,7 +210,7 @@ init (CodecContext *ctx, CodecElement *codec, CodecDevice *dev)
     ctx->codec = codec;
   }
 
-  release_device_mem(dev->fd, device_mem + mem_offset);
+  release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
 
   GST_DEBUG (" >> Leave");
   return opened;
@@ -249,7 +250,7 @@ decode_video (GstMaruVidDec *marudec, uint8_t *inbuf, int inbuf_size,
   CodecDevice *dev = marudec->dev;
   int len = 0, ret = 0;
   gpointer buffer = NULL;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
   size_t size = sizeof(inbuf_size) + sizeof(idx) + sizeof(in_offset) + inbuf_size;
 
   ret = secure_device_mem(dev->fd, ctx->index, size, &buffer);
@@ -265,8 +266,7 @@ decode_video (GstMaruVidDec *marudec, uint8_t *inbuf, int inbuf_size,
   decode_input->in_offset = in_offset;
   memcpy(&decode_input->inbuf, inbuf, inbuf_size);
 
-  mem_offset = GET_OFFSET(buffer);
-
+  mem_offset = GET_OFFSET32(buffer);
   marudec->is_using_new_decode_api = (can_use_new_decode_api() && (ctx->video.pix_fmt != -1));
   if (marudec->is_using_new_decode_api) {
     int picture_size = gst_maru_avpicture_size (ctx->video.pix_fmt,
@@ -287,7 +287,7 @@ decode_video (GstMaruVidDec *marudec, uint8_t *inbuf, int inbuf_size,
     return -1;
   }
 
-  struct video_decode_output *decode_output = device_mem + mem_offset;
+  struct video_decode_output *decode_output = device_mem + (uintptr_t)mem_offset;
   len = decode_output->len;
   *have_data = decode_output->got_picture;
   memcpy(&ctx->video, &decode_output->data, sizeof(VideoData));
@@ -297,9 +297,9 @@ decode_video (GstMaruVidDec *marudec, uint8_t *inbuf, int inbuf_size,
 
   if (len >= 0 && *have_data > 0 && marudec->is_using_new_decode_api) {
     marudec->is_last_buffer = ret;
-    marudec->mem_offset = mem_offset;
+    marudec->mem_offset = (int)mem_offset;
   } else {
-    release_device_mem(dev->fd, device_mem + mem_offset);
+    release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
   }
 
   GST_DEBUG (" >> Leave");
@@ -312,7 +312,7 @@ alloc_and_copy (GstMaruVidDec *marudec, guint64 offset, guint size,
 {
   GST_DEBUG (" >> enter");
 //  bool is_last_buffer = 0;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
   CodecContext *ctx;
   CodecDevice *dev;
   GstMapInfo mapinfo;
@@ -346,11 +346,11 @@ alloc_and_copy (GstMaruVidDec *marudec, guint64 offset, guint size,
   gst_buffer_map (*buf, &mapinfo, GST_MAP_READWRITE);
 
   if (marudec->is_using_new_decode_api) {
-    memcpy (mapinfo.data, device_mem + mem_offset + OFFSET_PICTURE_BUFFER, size);
+    memcpy (mapinfo.data, device_mem + (uintptr_t)mem_offset + OFFSET_PICTURE_BUFFER, size);
   } else {
-    memcpy (mapinfo.data, device_mem + mem_offset, size);
+    memcpy (mapinfo.data, device_mem + (uintptr_t)mem_offset, size);
   }
-  release_device_mem(dev->fd, device_mem + mem_offset);
+  release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
 
   GST_DEBUG ("secured last buffer!! Use heap buffer");
 /*
@@ -379,7 +379,7 @@ buffer_alloc_and_copy (GstPad *pad, guint64 offset, guint size,
 {
   GST_DEBUG (" >> enter");
 //  bool is_last_buffer = 0;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
   GstMaruDec *marudec;
   CodecContext *ctx;
   CodecDevice *dev;
@@ -415,11 +415,11 @@ buffer_alloc_and_copy (GstPad *pad, guint64 offset, guint size,
     buffer = g_malloc (size);
 
     if (marudec->is_using_new_decode_api) {
-      memcpy (buffer, device_mem + mem_offset + OFFSET_PICTURE_BUFFER, size);
+      memcpy (buffer, device_mem + (uintptr_t)mem_offset + OFFSET_PICTURE_BUFFER, size);
     } else {
-      memcpy (buffer, device_mem + mem_offset, size);
+      memcpy (buffer, device_mem + (uintptr_t)mem_offset, size);
     }
-    release_device_mem(dev->fd, device_mem + mem_offset);
+    release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
 
     GST_DEBUG ("secured last buffer!! Use heap buffer");
 /*  } else {
@@ -470,7 +470,7 @@ encode_video (CodecContext *ctx, uint8_t *outbuf,
 {
   int len = 0, ret = 0;
   gpointer buffer = NULL;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
   size_t size = sizeof(inbuf_size) + sizeof(in_timestamp) + inbuf_size;
 
   ret = secure_device_mem(dev->fd, ctx->index, size, &buffer);
@@ -486,7 +486,7 @@ encode_video (CodecContext *ctx, uint8_t *outbuf,
   memcpy(&encode_input->inbuf, inbuf, inbuf_size);
   GST_DEBUG ("insize: %d, inpts: %lld", encode_input->inbuf_size,(long long) encode_input->in_timestamp);
 
-  mem_offset = GET_OFFSET(buffer);
+  mem_offset = GET_OFFSET32(buffer);
 
   ret = invoke_device_api(dev->fd, ctx->index, CODEC_ENCODE_VIDEO, &mem_offset, SMALLDATA);
 
@@ -495,15 +495,15 @@ encode_video (CodecContext *ctx, uint8_t *outbuf,
     return -1;
   }
 
-  GST_DEBUG ("encode_video. mem_offset = 0x%"PRIXPTR, mem_offset);
+  GST_DEBUG ("encode_video. mem_offset = 0x%x", mem_offset);
 
-  struct video_encode_output *encode_output = device_mem + mem_offset;
+  struct video_encode_output *encode_output = device_mem + (uintptr_t)mem_offset;
   len = encode_output->len;
   *coded_frame = encode_output->coded_frame;
   *is_keyframe = encode_output->key_frame;
   memcpy(outbuf, &encode_output->data, len);
 
-  release_device_mem(dev->fd, device_mem + mem_offset);
+  release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
 
   return len;
 }
@@ -541,7 +541,7 @@ decode_audio (CodecContext *ctx, int16_t *samples,
 {
   int len = 0, ret = 0;
   gpointer buffer = NULL;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
   size_t size = sizeof(inbuf_size) + inbuf_size;
 
   ret = secure_device_mem(dev->fd, ctx->index, size, &buffer);
@@ -557,7 +557,7 @@ decode_audio (CodecContext *ctx, int16_t *samples,
   decode_input->inbuf_size = inbuf_size;
   memcpy(&decode_input->inbuf, inbuf, inbuf_size);
 
-  mem_offset = GET_OFFSET(buffer);
+  mem_offset = GET_OFFSET32(buffer);
 
   ret = invoke_device_api(dev->fd, ctx->index, CODEC_DECODE_AUDIO, &mem_offset, SMALLDATA);
 
@@ -566,20 +566,20 @@ decode_audio (CodecContext *ctx, int16_t *samples,
   }
 
   GST_DEBUG ("decode_audio. ctx_id: %d, buffer = 0x%p",
-    ctx->index, device_mem + mem_offset);
+  ctx->index, device_mem + (uintptr_t)mem_offset);
 
-  struct audio_decode_output *decode_output = device_mem + mem_offset;
+  struct audio_decode_output *decode_output = device_mem + (uintptr_t)mem_offset;
   len = decode_output->len;
   *have_data = decode_output->got_frame;
   memcpy(&ctx->audio, &decode_output->data, sizeof(AudioData));
 
-  memcpy (samples, device_mem + mem_offset + OFFSET_PICTURE_BUFFER, len);
+  memcpy (samples, device_mem + (uintptr_t)mem_offset + OFFSET_PICTURE_BUFFER, len);
 
   GST_DEBUG ("decode_audio. sample_fmt %d sample_rate %d, channels %d, ch_layout %" PRIu64 ", len %d",
           ctx->audio.sample_fmt, ctx->audio.sample_rate, ctx->audio.channels,
           ctx->audio.channel_layout, len);
 
-  release_device_mem(dev->fd, device_mem + mem_offset);
+  release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
 
   return len;
 }
@@ -592,7 +592,7 @@ encode_audio (CodecContext *ctx, uint8_t *outbuf,
 {
   int len = 0, ret = 0;
   gpointer buffer = NULL;
-  uintptr_t mem_offset;
+  uint32_t mem_offset;
   size_t size = sizeof(inbuf_size) + inbuf_size;
 
   ret = secure_device_mem(dev->fd, ctx->index, inbuf_size, &buffer);
@@ -606,7 +606,7 @@ encode_audio (CodecContext *ctx, uint8_t *outbuf,
   encode_input->inbuf_size = inbuf_size;
   memcpy(&encode_input->inbuf, inbuf, inbuf_size);
 
-  mem_offset = GET_OFFSET(buffer);
+  mem_offset = GET_OFFSET32(buffer);
 
   ret = invoke_device_api(dev->fd, ctx->index, CODEC_ENCODE_AUDIO, &mem_offset, SMALLDATA);
 
@@ -614,9 +614,9 @@ encode_audio (CodecContext *ctx, uint8_t *outbuf,
     return -1;
   }
 
-  GST_DEBUG ("encode_audio. mem_offset = 0x%"PRIXPTR, mem_offset);
+  GST_DEBUG ("encode_audio. mem_offset = 0x%x", mem_offset);
 
-  struct audio_encode_output *encode_output = device_mem + mem_offset;
+  struct audio_encode_output *encode_output = device_mem + (uintptr_t)mem_offset;
   len = encode_output->len;
   if (len > 0) {
     memcpy (outbuf, &encode_output->data, len);
@@ -624,7 +624,7 @@ encode_audio (CodecContext *ctx, uint8_t *outbuf,
 
   GST_DEBUG ("encode_audio. len: %d", len);
 
-  release_device_mem(dev->fd, device_mem + mem_offset);
+  release_device_mem(dev->fd, device_mem + (uintptr_t)mem_offset);
 
   return len;
 }
@@ -669,7 +669,7 @@ prepare_elements (int fd)
     return NULL;
   }
 
-  elem = g_malloc(size);
+  elem = g_malloc (size);
   //FIXME
   //An elem variable should be deallocated, will be fixed.
 
index 85ec672..a2ccc82 100644 (file)
@@ -54,16 +54,21 @@ void
 codec_init_data_to (CodecContext *ctx, CodecElement *codec, gpointer buffer)
 {
   int size = 0;
+  /* copy VideoData, AudioData, bit_rate, codec_tag and codecdata_size
+   * into device memory. the size of codecdata is variable.
+   *
+   * below 3 points codec->codecData, codec->codec, codec.index and pointer size
+   * can be different between 32 bits and 64 bits. So, need to calculate on arch.
+   */
+  const size_t ctx_exclude_size = sizeof(CodecContext) - (sizeof(uintptr_t) * 3);
 
   size = codec_element_data (codec, buffer);
 
   GST_INFO ("context_id: %d, name: %s, media type: %s",
     ctx->index, codec->name, codec->media_type ? "audio" : "video");
 
-  // copy VideoData, AudioData, bit_rate, codec_tag and codecdata_size
-  // into device memory. the size of codecdata is variable.
-  memcpy (buffer + size, ctx, sizeof(CodecContext) - 12);
-  size += (sizeof(CodecContext) - 12);
+  memcpy (buffer + size, ctx, ctx_exclude_size);
+  size += ctx_exclude_size;
   memcpy (buffer + size, ctx->codecdata, ctx->codecdata_size);
   size += ctx->codecdata_size;
 
@@ -99,7 +104,7 @@ codec_init_data_from (CodecContext *ctx, int media_type, gpointer buffer)
 
   GST_DEBUG ("codec_init. extradata_size %d", ctx->codecdata_size);
   if (ctx->codecdata_size > 0) {
-    ctx->codecdata = g_malloc(ctx->codecdata_size);
+    ctx->codecdata = g_malloc0(ctx->codecdata_size);
     memcpy(ctx->codecdata, buffer + size, ctx->codecdata_size);
   }