static GstPadLinkReturn
pad_sink_link (GstPad * pad, GstPad * peer)
{
  GstOmxBaseSink *self;

  self = GST_OMX_BASE_SINK (GST_OBJECT_PARENT (pad));

  GST_INFO_OBJECT (self, "link");

  if (!self->initialized) {
    if (!omx_init (self))
      return GST_PAD_LINK_REFUSED;
    self->initialized = TRUE;
  }

  return GST_PAD_LINK_OK;
}
static GstStateChangeReturn
change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  GstOmxBaseSink *self;

  self = GST_OMX_BASE_SINK (element);

  GST_LOG_OBJECT (self, "begin");

  GST_INFO_OBJECT (self, "changing state %s - %s",
      gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
      gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      if (!self->initialized) {
        if (!omx_init (self))
          return GST_PAD_LINK_REFUSED;

        self->initialized = TRUE;
      }

      g_omx_core_prepare (self->gomx);
      break;

    case GST_STATE_CHANGE_READY_TO_PAUSED:
      g_omx_core_start (self->gomx);
      break;

    case GST_STATE_CHANGE_PAUSED_TO_READY:
      g_omx_port_finish (self->in_port);
      break;

    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  if (ret == GST_STATE_CHANGE_FAILURE)
    goto leave;

  switch (transition) {
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
      g_omx_port_pause (self->in_port);
      break;

    case GST_STATE_CHANGE_PAUSED_TO_READY:
      g_omx_core_stop (self->gomx);
      break;

    case GST_STATE_CHANGE_READY_TO_NULL:
      g_omx_core_unload (self->gomx);
      break;

    default:
      break;
  }

leave:
  GST_LOG_OBJECT (self, "end");

  return ret;
}
Example #3
0
static av_cold int omx_encode_init(AVCodecContext *avctx)
{
    OMXCodecContext *s = avctx->priv_data;
    int ret = AVERROR_ENCODER_NOT_FOUND;
    const char *role;
    OMX_BUFFERHEADERTYPE *buffer;
    OMX_ERRORTYPE err;

#if CONFIG_OMX_RPI
    s->input_zerocopy = 1;
#endif

    s->omx_context = omx_init(avctx, s->libname, s->libprefix);
    if (!s->omx_context)
        return AVERROR_ENCODER_NOT_FOUND;

    pthread_mutex_init(&s->state_mutex, NULL);
    pthread_cond_init(&s->state_cond, NULL);
    pthread_mutex_init(&s->input_mutex, NULL);
    pthread_cond_init(&s->input_cond, NULL);
    pthread_mutex_init(&s->output_mutex, NULL);
    pthread_cond_init(&s->output_cond, NULL);
    s->mutex_cond_inited = 1;
    s->avctx = avctx;
    s->state = OMX_StateLoaded;
    s->error = OMX_ErrorNone;

    switch (avctx->codec->id) {
    case AV_CODEC_ID_MPEG4:
        role = "video_encoder.mpeg4";
        break;
    case AV_CODEC_ID_H264:
        role = "video_encoder.avc";
        break;
    default:
        return AVERROR(ENOSYS);
    }

    if ((ret = find_component(s->omx_context, avctx, role, s->component_name, sizeof(s->component_name))) < 0)
        goto fail;

    av_log(avctx, AV_LOG_INFO, "Using %s\n", s->component_name);

    if ((ret = omx_component_init(avctx, role)) < 0)
        goto fail;

    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
        while (1) {
            buffer = get_buffer(&s->output_mutex, &s->output_cond,
                                &s->num_done_out_buffers, s->done_out_buffers, 1);
            if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
                if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
                    avctx->extradata_size = 0;
                    goto fail;
                }
                memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
                avctx->extradata_size += buffer->nFilledLen;
                memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
            }
            err = OMX_FillThisBuffer(s->handle, buffer);
            if (err != OMX_ErrorNone) {
                append_buffer(&s->output_mutex, &s->output_cond,
                              &s->num_done_out_buffers, s->done_out_buffers, buffer);
                av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err);
                ret = AVERROR_UNKNOWN;
                goto fail;
            }
            if (avctx->codec->id == AV_CODEC_ID_H264) {
                // For H.264, the extradata can be returned in two separate buffers
                // (the videocore encoder on raspberry pi does this);
                // therefore check that we have got both SPS and PPS before continuing.
                int nals[32] = { 0 };
                int i;
                for (i = 0; i + 4 < avctx->extradata_size; i++) {
                     if (!avctx->extradata[i + 0] &&
                         !avctx->extradata[i + 1] &&
                         !avctx->extradata[i + 2] &&
                         avctx->extradata[i + 3] == 1) {
                         nals[avctx->extradata[i + 4] & 0x1f]++;
                     }
                }
                if (nals[H264_NAL_SPS] && nals[H264_NAL_PPS])
                    break;
            } else {
                if (avctx->extradata_size > 0)
                    break;
            }
        }
    }

    return 0;
fail:
    return ret;
}
Example #4
0
int main(int argc, char *argv[])
{
  omx_endpoint_t ep;
  uint64_t dest_board_addr;
  int board_index = BID;
  int endpoint_index = EID;
  char hostname[OMX_HOSTNAMELEN_MAX];
  char ifacename[16];
  omx_endpoint_addr_t addr;
  int self = 0;
  int shared = 0;
  int c;
  omx_return_t ret;
  omx_status_t status;
  uint32_t result;
  int length = 0;
  char *sbuf = NULL, *rbuf = NULL;

  while ((c = getopt(argc, argv, "e:b:l:sSh")) != -1)
    switch (c) {
    case 'b':
      board_index = atoi(optarg);
      break;
    case 'e':
      endpoint_index = atoi(optarg);
      break;
    case 'l':
      length = atoi(optarg);
      break;
    case 's':
      shared = 1;
      break;
    case 'S':
      self = 1;
      break;
    default:
      fprintf(stderr, "Unknown option -%c\n", c);
    case 'h':
      usage(argc, argv);
      exit(-1);
      break;
    }

  if (!self && !getenv("OMX_DISABLE_SELF"))
    putenv("OMX_DISABLE_SELF=1");

  if (!shared && !getenv("OMX_DISABLE_SHARED"))
    putenv("OMX_DISABLE_SHARED=1");

  if (length) {
    sbuf = malloc(length);
    rbuf = malloc(length);
    if (!sbuf || !rbuf)
      goto out;
    memset(sbuf, 'a', length);
    memset(rbuf, 'b', length);
  }

  ret = omx_init();
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to initialize (%s)\n",
	    omx_strerror(ret));
    goto out;
  }

  ret = omx_board_number_to_nic_id(board_index, &dest_board_addr);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to find board %d nic id (%s)\n",
	    board_index, omx_strerror(ret));
    goto out;
  }

  ret = omx_open_endpoint(board_index, endpoint_index, 0x12345678, NULL, 0, &ep);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to open endpoint (%s)\n",
	    omx_strerror(ret));
    goto out;
  }

  ret = omx_get_info(ep, OMX_INFO_BOARD_HOSTNAME, NULL, 0,
		     hostname, sizeof(hostname));
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to find board hostname (%s)\n",
	    omx_strerror(ret));
    goto out_with_ep;
  }

  ret = omx_get_info(ep, OMX_INFO_BOARD_IFACENAME, NULL, 0,
		     ifacename, sizeof(ifacename));
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to find board iface name (%s)\n",
	    omx_strerror(ret));
    goto out_with_ep;
  }

  printf("Using board #%d name '%s' hostname '%s'\n", board_index, ifacename, hostname);

  ret = omx_get_endpoint_addr(ep, &addr);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to get local endpoint address (%s)\n",
	    omx_strerror(ret));
    goto out_with_ep;
  }

  ret = omx_isend(ep, sbuf, length, addr, 0x1, NULL, NULL);
  assert(ret == OMX_SUCCESS);
  printf("posted send\n");

  ret = omx_probe(ep, 0x1, -1ULL, &status, &result, OMX_TIMEOUT_INFINITE);
  assert(ret == OMX_SUCCESS);
  assert(result);
  assert(status.match_info == 0x01);
  printf("probe found exact match\n");

  ret = omx_iprobe(ep, 0x2, -1ULL, &status, &result);
  assert(ret == OMX_SUCCESS);
  assert(!result);
  printf("iprobe did not found match with wrong bits\n");

  ret = omx_iprobe(ep, 0, -2ULL, &status, &result);
  assert(ret == OMX_SUCCESS);
  assert(result);
  assert(status.match_info == 0x01);
  printf("iprobe found match with mask\n");

  ret = omx_irecv(ep, rbuf, length, 0, -2ULL, NULL, NULL);
  assert(ret == OMX_SUCCESS);
  printf("posted recv with mask\n");

  ret = omx_iprobe(ep, 0, -2ULL, &status, &result);
  assert(ret == OMX_SUCCESS);
  assert(!result);
  printf("iprobe cannot found match with mask anymore\n");

  free(sbuf);
  free(rbuf);

  omx_close_endpoint(ep);
  return 0;

 out_with_ep:
  omx_close_endpoint(ep);
 out:
  return -1;
}
Example #5
0
int main(int argc, char *argv[])
{
  char board_addr_str[OMX_BOARD_ADDR_STRLEN];
  omx_return_t ret;
  uint32_t bid = OMX_ANY_NIC;
  uint32_t max, emax, count;
  uint32_t status;
  uint64_t mapper_id;
  unsigned i;
  int c;

  while ((c = getopt(argc, argv, "b:qvh")) != -1)
    switch (c) {
    case 'b':
      bid = atoi(optarg);
      break;
    case 'q':
      verbose = 0;
      break;
    case 'v':
      verbose = 1;
      break;
    default:
      fprintf(stderr, "Unknown option -%c\n", c);
    case 'h':
      usage(argc, argv);
      exit(-1);
      break;
    }

  if (verbose) {
    printf("Open-MX version " PACKAGE_VERSION "\n");
    printf(" build: " OMX_BUILD_STR "\n");
    printf("\n");
  }

  ret = omx_init();
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to initialize (%s)\n",
            omx_strerror(ret));
    goto out;
  }

  /* get board and endpoint max */
  max = omx__driver_desc->board_max;
  emax = omx__driver_desc->endpoint_max;

  /* get board count */
  ret = omx__get_board_count(&count);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to read board count, %s\n", omx_strerror(ret));
    goto out;
  }
  printf("Found %ld boards (%ld max) supporting %ld endpoints each:\n",
	 (unsigned long) count, (unsigned long) max, (unsigned long) emax);

  /* print boards */
  if (bid == OMX_ANY_NIC)
    for(i=0; i<max; i++)
      handle_one_board(i);
  else
    handle_one_board(bid);

  /* get peer table state */
  ret = omx__driver_get_peer_table_state(&status, NULL, NULL, &mapper_id);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to get peer table status, %s\n", omx_strerror(ret));
    goto out;
  }
  if (verbose) {
    /* print the common peer table */
    printf("\n");
    if (status & OMX_PEER_TABLE_STATUS_CONFIGURED) {
      omx__board_addr_sprintf(board_addr_str, mapper_id);
      printf("Peer table is ready, mapper is %s\n", board_addr_str);
    } else {
      printf("Peer table is not configured yet\n");
    }
    printf("================================================\n");
    omx__peers_dump("  %d) %s %s\n");
  }
  if (status & OMX_PEER_TABLE_STATUS_FULL) {
    fprintf(stderr, "WARNING: peer table is full, some peers could not be added.\n");
  }

  return 0;

 out:
  return -1;
}
Example #6
0
int main(int argc, char *argv[])
{
  omx_endpoint_t ep;
  uint64_t dest_board_addr;
  struct timeval tv1, tv2;
  int board_index = BID;
  int endpoint_index = EID;
  char hostname[OMX_HOSTNAMELEN_MAX];
  char ifacename[16];
  omx_endpoint_addr_t addr;
  int length = PREDEFINED_LENGTHS;
  int self = 0;
  int shared = 0;
  int parallel = PARALLEL;
  int c;
  int i;
  omx_return_t ret;
  char *buffer, *buffer2;

  while ((c = getopt(argc, argv, "e:b:l:P:sSvh")) != -1)
    switch (c) {
    case 'b':
      board_index = atoi(optarg);
      break;
    case 'e':
      endpoint_index = atoi(optarg);
      break;
    case 'l':
      length = atoi(optarg);
      break;
    case 'P':
      parallel = atoi(optarg);
      break;
    case 's':
      shared = 1;
      break;
    case 'S':
      self = 1;
      break;
    case 'v':
      verbose = 1;
      break;
    default:
      fprintf(stderr, "Unknown option -%c\n", c);
    case 'h':
      usage(argc, argv);
      exit(-1);
      break;
    }

  if (!self && !getenv("OMX_DISABLE_SELF"))
    putenv("OMX_DISABLE_SELF=1");

  if (!shared && !getenv("OMX_DISABLE_SHARED"))
    putenv("OMX_DISABLE_SHARED=1");

  ret = omx_init();
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to initialize (%s)\n",
	    omx_strerror(ret));
    goto out;
  }

  ret = omx_board_number_to_nic_id(board_index, &dest_board_addr);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to find board %d nic id (%s)\n",
	    board_index, omx_strerror(ret));
    goto out;
  }

  ret = omx_open_endpoint(board_index, endpoint_index, 0x12345678, NULL, 0, &ep);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to open endpoint (%s)\n",
	    omx_strerror(ret));
    goto out;
  }

  ret = omx_get_info(ep, OMX_INFO_BOARD_HOSTNAME, NULL, 0,
		     hostname, sizeof(hostname));
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to find board hostname (%s)\n",
	    omx_strerror(ret));
    goto out_with_ep;
  }

  ret = omx_get_info(ep, OMX_INFO_BOARD_IFACENAME, NULL, 0,
		     ifacename, sizeof(ifacename));
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to find board iface name (%s)\n",
	    omx_strerror(ret));
    goto out_with_ep;
  }

  printf("Using board #%d name '%s' hostname '%s'\n", board_index, ifacename, hostname);

  ret = omx_get_endpoint_addr(ep, &addr);
  if (ret != OMX_SUCCESS) {
    fprintf(stderr, "Failed to get local endpoint address (%s)\n",
	    omx_strerror(ret));
    goto out_with_ep;
  }

  if (length == PREDEFINED_LENGTHS) {

    buffer = malloc(LENGTH4);
    if (!buffer)
      goto out_with_ep;
    buffer2 = malloc(LENGTH4);
    if (!buffer2) {
      free(buffer);
      goto out_with_ep;
    }

    gettimeofday(&tv1, NULL);
    for(i=0; i<ITER; i++) {
      /* send a tiny message */
      ret = one_iteration(ep, addr, buffer, buffer2, LENGTH1, parallel, i);
      if (ret != OMX_SUCCESS)
	goto out_with_ep;
    }
    gettimeofday(&tv2, NULL);
    printf("tiny (%d bytes) latency %lld us\n", 13,
	   (tv2.tv_sec-tv1.tv_sec)*1000000ULL+(tv2.tv_usec-tv1.tv_usec));

    gettimeofday(&tv1, NULL);
    for(i=0; i<ITER; i++) {
      /* send a small message */
      ret = one_iteration(ep, addr, buffer, buffer2, LENGTH2, parallel, i);
      if (ret != OMX_SUCCESS)
	goto out_with_ep;
    }
    gettimeofday(&tv2, NULL);
    printf("small (%d bytes) latency %lld us\n", 95,
	   (tv2.tv_sec-tv1.tv_sec)*1000000ULL+(tv2.tv_usec-tv1.tv_usec));

    gettimeofday(&tv1, NULL);
    for(i=0; i<ITER; i++) {
      /* send a medium message */
      ret = one_iteration(ep, addr, buffer, buffer2, LENGTH3, parallel, i);
      if (ret != OMX_SUCCESS)
	goto out_with_ep;
    }
    gettimeofday(&tv2, NULL);
    printf("medium (%d bytes) latency %lld us\n", 13274,
	   (tv2.tv_sec-tv1.tv_sec)*1000000ULL+(tv2.tv_usec-tv1.tv_usec));

    gettimeofday(&tv1, NULL);
    for(i=0; i<ITER; i++) {
      /* send a large message */
      ret = one_iteration(ep, addr, buffer, buffer2, LENGTH4, parallel, i);
      if (ret != OMX_SUCCESS)
	goto out_with_ep;
    }
    gettimeofday(&tv2, NULL);
    printf("large (%d bytes) latency %lld us\n", 9327485,
	   (tv2.tv_sec-tv1.tv_sec)*1000000ULL+(tv2.tv_usec-tv1.tv_usec));

    free(buffer2);
    free(buffer);

  } else {

    buffer = malloc(length);
    if (!buffer)
      goto out_with_ep;
    buffer2 = malloc(length);
    if (!buffer2) {
      free(buffer);
      goto out_with_ep;
    }

    gettimeofday(&tv1, NULL);
    for(i=0; i<ITER; i++) {
      /* send a large message */
      ret = one_iteration(ep, addr, buffer, buffer2, length, parallel, i);
      if (ret != OMX_SUCCESS)
	goto out_with_ep;
    }
    gettimeofday(&tv2, NULL);
    printf("message (%d bytes) latency %lld us\n", length,
	   (tv2.tv_sec-tv1.tv_sec)*1000000ULL+(tv2.tv_usec-tv1.tv_usec));

    free(buffer2);
    free(buffer);

  }

  omx_close_endpoint(ep);
  return 0;

 out_with_ep:
  omx_close_endpoint(ep);
 out:
  return -1;
}
Example #7
0
gint
main (gint argc, gchar ** argv)
{
  gchar *filename;
  GModule *core_module;
  OMX_ERRORTYPE err;
  OMX_ERRORTYPE (*omx_init) (void);
  OMX_ERRORTYPE (*omx_component_name_enum) (OMX_STRING cComponentName,
      OMX_U32 nNameLength, OMX_U32 nIndex);
  OMX_ERRORTYPE (*omx_get_roles_of_component) (OMX_STRING compName,
      OMX_U32 * pNumRoles, OMX_U8 ** roles);
  guint32 i;

  if (argc != 2) {
    g_printerr ("Usage: %s /path/to/libopenmaxil.so\n", argv[0]);
    return -1;
  }

  filename = argv[1];

  if (!g_path_is_absolute (filename)) {
    g_printerr ("'%s' is not an absolute filename\n", filename);
    return -1;
  }

  /* Hack for the Broadcom OpenMAX IL implementation */
  if (g_str_has_suffix (filename, "vc/lib/libopenmaxil.so")) {
    gchar *bcm_host_filename;
    gchar *bcm_host_path;
    GModule *bcm_host_module;
    void (*bcm_host_init) (void);

    bcm_host_path = g_path_get_dirname (filename);
    bcm_host_filename =
        g_build_filename (bcm_host_path, "libbcm_host.so", NULL);

    bcm_host_module = g_module_open (bcm_host_filename, G_MODULE_BIND_LAZY);

    g_free (bcm_host_filename);
    g_free (bcm_host_path);

    if (!bcm_host_module) {
      g_printerr ("Failed to load 'libbcm_host.so'\n");
      return -1;
    }

    if (!g_module_symbol (bcm_host_module, "bcm_host_init",
            (gpointer *) & bcm_host_init)) {
      g_printerr ("Failed to find 'bcm_host_init' in 'libbcm_host.so'\n");
      return -1;
    }

    bcm_host_init ();
  }

  core_module = g_module_open (filename, G_MODULE_BIND_LAZY);
  if (!core_module) {
    g_printerr ("Failed to load '%s'\n", filename);
    return -1;
  }

  if (!g_module_symbol (core_module, "OMX_Init", (gpointer *) & omx_init)) {
    g_printerr ("Failed to find '%s' in '%s'\n", "OMX_Init", filename);
    return -1;
  }

  if (!g_module_symbol (core_module, "OMX_ComponentNameEnum",
          (gpointer *) & omx_component_name_enum)) {
    g_printerr ("Failed to find '%s' in '%s'\n", "OMX_ComponentNameEnum",
        filename);
    return -1;
  }

  if (!g_module_symbol (core_module, "OMX_GetRolesOfComponent",
          (gpointer *) & omx_get_roles_of_component)) {
    g_printerr ("Failed to find '%s' in '%s'\n", "OMX_GetRolesOfComponent",
        filename);
    return -1;
  }


  if ((err = omx_init ()) != OMX_ErrorNone) {
    g_printerr ("Failed to initialize core: %d\n", err);
    return -1;
  }

  i = 0;
  while (err == OMX_ErrorNone) {
    gchar component_name[1024];

    err = omx_component_name_enum (component_name, sizeof (component_name), i);
    if (err == OMX_ErrorNone || err == OMX_ErrorNoMore) {
      guint32 nroles;

      g_print ("Component %d: %s\n", i, component_name);

      if (omx_get_roles_of_component (component_name, (OMX_U32 *) & nroles,
              NULL) == OMX_ErrorNone && nroles > 0) {
        gchar **roles = g_new (gchar *, nroles);
        gint j;

        roles[0] = g_new0 (gchar, 129 * nroles);
        for (j = 1; j < nroles; j++) {
          roles[j] = roles[j - 1] + 129;
        }

        if (omx_get_roles_of_component (component_name, (OMX_U32 *) & nroles,
                (OMX_U8 **) roles) == OMX_ErrorNone) {
          for (j = 0; j < nroles; j++) {
            g_print ("  Role %d: %s\n", j, roles[j]);
          }
        }
        g_free (roles[0]);
        g_free (roles);
      }
    }