org.bytedeco.javacpp.PointerPointer Java Examples

The following examples show how to use org.bytedeco.javacpp.PointerPointer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OcrService.java    From cs-actions with Apache License 2.0 7 votes vote down vote up
private static String extractBlocks(TessBaseAPI api) throws Exception {
    TessBaseAPISetPageSegMode(api, PSM_AUTO_OSD);
    lept.BOXA boxes = TessBaseAPIGetComponentImages(api, RIL_BLOCK, true, (PointerPointer) null, null);
    final int boxCount = boxaGetCount(boxes);

    JsonObject outputJson = new JsonObject();
    for (int i = 0; i < boxCount; i++) {
        lept.BOX box = boxaGetBox(boxes, i, L_CLONE);
        if (box == null) {
            continue;
        }
        TessBaseAPISetRectangle(api, box.x(), box.y(), box.w(), box.h());
        BytePointer utf8Text = TessBaseAPIGetUTF8Text(api);
        String ocrResult = utf8Text.getString(UTF_8);

        outputJson = buildOutputJson(outputJson, ocrResult, i);
        boxDestroy(box);
        utf8Text.deallocate();
    }
    boxaDestroy(boxes);
    if (boxCount == 0) {
        throw new Exception("Failed to extract text blocks (Empty page), check text orientation or check if text exists.");
    }
    return outputJson.toString();
}
 
Example #2
Source File: FtModel.java    From djl with Apache License 2.0 6 votes vote down vote up
/**
 * Train the fastText model.
 *
 * @param config the training configuration to use
 * @param trainingSet the training dataset
 * @param validateSet the validation dataset
 * @return the result of the training
 * @throws IOException when IO operation fails in loading a resource
 */
public TrainingResult fit(FtTrainingConfig config, FtDataset trainingSet, FtDataset validateSet)
        throws IOException {
    Path outputDir = config.getOutputDir();
    if (Files.notExists(outputDir)) {
        Files.createDirectory(outputDir);
    }
    String fitModelName = config.getModelName();
    Path modelFile = outputDir.resolve(fitModelName).toAbsolutePath();

    String[] args = config.toCommand(trainingSet.getInputFile().toString());

    fta.runCmd(args.length, new PointerPointer<CharPointer>(args));
    setModelFile(modelFile);

    TrainingResult result = new TrainingResult();
    int epoch = config.getEpoch();
    if (epoch <= 0) {
        epoch = 5;
    }
    result.setEpoch(epoch);
    return result;
}
 
Example #3
Source File: AudioFrame.java    From JavaAV with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new {@code AudioFrame} with descriptive format and allocates memory
 * for the specified amount of samples.
 *
 * @param format  the format of the samples.
 * @param samples the amount of samples.
 */
public AudioFrame(AudioFormat format, int samples) {
	int channels = format.getChannels();
	int sampleFormat = format.getSampleFormat().value();
	int planes = av_sample_fmt_is_planar(sampleFormat) != 0 ? channels : 1;
	int planeLength = (samples * channels * av_get_bytes_per_sample(sampleFormat)) / planes;

	this.format = format;
	this.samples = samples;
	this.planePointers = new BytePointer[planes];
	this.samplePointer = new PointerPointer(planes);

	for (int i = 0; i < planes; i++) {
		this.planePointers[i] = new BytePointer(av_malloc(planeLength)).capacity(planeLength);
		this.planePointers[i].limit(planeLength);
		this.samplePointer.put(i, planePointers[i]);
	}
}
 
Example #4
Source File: EagerOperationBuilder.java    From java with Apache License 2.0 6 votes vote down vote up
private static TFE_TensorHandle[] execute(TFE_Op opHandle, EagerSession session) {
  requireOp(opHandle);
  try (PointerScope scope = new PointerScope()) {
    IntPointer numRetvals = new IntPointer(1).put(MAX_OUTPUTS_PER_OP);
    PointerPointer<TFE_TensorHandle> retvals = new PointerPointer<TFE_TensorHandle>(MAX_OUTPUTS_PER_OP);
    TF_Status status = TF_Status.newStatus();
    TFE_Execute(opHandle, retvals, numRetvals, status);
    status.throwExceptionIfNotOK();

    TFE_TensorHandle[] rethandles = new TFE_TensorHandle[numRetvals.get()];
    for (int i = 0; i < rethandles.length; ++i) {
      rethandles[i] = retvals.get(TFE_TensorHandle.class, i).withDeallocator();
      session.attach(rethandles[i]);
    }
    return rethandles;
  }
}
 
Example #5
Source File: testRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * 
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException {
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>) null) >= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #6
Source File: CudaNativeRandom.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public PointerPointer getExtraPointers() {
    PointerPointer ptr = new PointerPointer(4);
    CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
    ptr.put(0, AtomicAllocator.getInstance().getHostPointer(stateBuffer));
    ptr.put(1, context.getOldStream());
    return ptr;
}
 
Example #7
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>)null)>= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #8
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>)null)>= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #9
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>) null)>= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #10
Source File: GetComponent.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

		TessBaseAPI api = new TessBaseAPI();
		// Initialize tesseract-ocr with English, without specifying tessdata path
		if (api.Init(null, "eng") != 0) {
			System.err.println("Could not initialize tesseract.");
			System.exit(1);
		}

		// Open input image with leptonica library
		PIX image = pixRead(args.length > 0 ? args[0] : "testen-1.png");
		api.SetImage(image);
		// Get OCR result
		// Boxa* boxes = api->GetComponentImages(tesseract::RIL_TEXTLINE, true, NULL,
		// NULL);
		// printf("Found %d textline image components.\n", boxes->n);
		// for (int i = 0; i < boxes->n; i++) {
		// BOX* box = boxaGetBox(boxes, i, L_CLONE);
		// api->SetRectangle(box->x, box->y, box->w, box->h);
		// char* ocrResult = api->GetUTF8Text();
		// int conf = api->MeanTextConf();
		// fprintf(stdout, "Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s",
		// i, box->x, box->y, box->w, box->h, conf, ocrResult);
		// }
		BOXA boxes = api.GetComponentImages(tesseract.RIL_TEXTLINE, true, (PointerPointer) null, null);
		System.out.print(String.format("Found %d textline image components.\n", boxes.n()));
		for (int i = 0; i < boxes.n(); i++) {
			BOX box = boxes.box(i);
			api.SetRectangle(box.x(), box.y(), box.w(), box.h());
			BytePointer text = api.GetUTF8Text();
			int conf = api.MeanTextConf();
			System.out.println(String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s", i, box.x(),
					box.y(), box.w(), box.h(), conf, text.getString()));
			text.deallocate();
		}
		// Destroy used object and release memory
		api.End();
		pixDestroy(image);
	}
 
Example #11
Source File: AbstractTF_Session.java    From java with Apache License 2.0 5 votes vote down vote up
/**
 * Calls TF_LoadSessionFromSavedModel(), and registers a deallocator.
 * @return TF_Session created. Do not call TF_DeleteSession() on it.
 */
public static TF_Session loadSessionFromSavedModel(TF_SessionOptions session_options, TF_Buffer run_options,
    String export_dir, String[] tags, TF_Graph graph, TF_Buffer meta_graph_def, TF_Status status) {
    TF_Session s = TF_LoadSessionFromSavedModel(session_options, run_options,
            new BytePointer(export_dir), new PointerPointer(tags), tags.length, graph, meta_graph_def, status);
    if (s != null) {
        s.graph = graph;
        s.opts = session_options;
        s.run_options = run_options;
        s.meta_graph_def = meta_graph_def;
        s.status = status;
        s.deallocator(new DeleteDeallocator(s));
    }
    return s;
}
 
Example #12
Source File: EagerOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrShapeList(TFE_Op opHandle, String name, long[] shapes, int[] numDims) {
  requireOp(opHandle);
  try (PointerScope scope = new PointerScope()) {
    LongPointer shapesPointer = new LongPointer(shapes);
    PointerPointer<LongPointer> shapesPointers = new PointerPointer<LongPointer>(numDims.length);
    for (int i = 0; i < numDims.length; i++) {
      shapesPointers.put(i, shapesPointer);
      shapesPointer.position(shapesPointer.position() + numDims[i] * 8);
    }
    TF_Status status = TF_Status.newStatus();
    TFE_OpSetAttrShapeList(opHandle, new BytePointer(name), shapesPointers, new IntPointer(numDims),
                          numDims.length, status);
  }
}
 
Example #13
Source File: EagerOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrStringList(TFE_Op opHandle, String name, byte[][] value) {
  requireOp(opHandle);
  try (PointerScope scope = new PointerScope()) {
    PointerPointer<BytePointer> valuePointers = new PointerPointer<BytePointer>(value.length);
    SizeTPointer lengths = new SizeTPointer(value.length);

    for (int i = 0; i < value.length; ++i) {
      valuePointers.put(i, new BytePointer(value[i]));
      lengths.put(i, value[i].length);
    }
    TFE_OpSetAttrStringList(opHandle, name, valuePointers, lengths, value.length);
  }
}
 
Example #14
Source File: EagerOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void addInputList(TFE_Op opHandle, TFE_TensorHandle[] tensorHandles) {
  requireOp(opHandle);
  try (PointerScope scope = new PointerScope()) {
    PointerPointer<TFE_TensorHandle> tensorPointers = new PointerPointer<TFE_TensorHandle>(tensorHandles.length);
    for (int i = 0; i < tensorHandles.length; ++i) {
      requireTensorHandle(tensorHandles[i]);
      tensorPointers.put(i, tensorHandles[i]);
    }
    TF_Status status = TF_Status.newStatus();
    TFE_OpAddInputList(opHandle, tensorPointers, tensorHandles.length, status);
    status.throwExceptionIfNotOK();
  }
}
 
Example #15
Source File: SavedModelBundle.java    From java with Apache License 2.0 5 votes vote down vote up
private static SavedModelBundle load(
    String exportDir, String[] tags, ConfigProto config, RunOptions runOptions) {
  SavedModelBundle bundle = null;

  try (PointerScope scope = new PointerScope()) {
    TF_Status status = TF_Status.newStatus();

    // allocate parameters for TF_LoadSessionFromSavedModel
    TF_SessionOptions opts = TF_SessionOptions.newSessionOptions();
    if (config != null) {
      BytePointer configBytes = new BytePointer(config.toByteArray());
      TF_SetConfig(opts, configBytes, configBytes.capacity(), status);
      status.throwExceptionIfNotOK();
    }
    TF_Buffer runOpts = TF_Buffer.newBufferFromString(runOptions);

    // load the session
    TF_Graph graph = TF_NewGraph();
    TF_Buffer metagraphDef = TF_Buffer.newBuffer();
    TF_Session session = TF_LoadSessionFromSavedModel(
        opts, runOpts, new BytePointer(exportDir), new PointerPointer(tags),
        tags.length, graph, metagraphDef, status);
    status.throwExceptionIfNotOK();

    // handle the result
    try {
      bundle = fromHandle(graph, session, MetaGraphDef.parseFrom(metagraphDef.dataAsByteBuffer()));
    } catch (InvalidProtocolBufferException e) {
      throw new TensorFlowException("Cannot parse MetaGraphDef protocol buffer", e);
    }
  }

  return bundle;
}
 
Example #16
Source File: GraphOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrStringList(TF_OperationDescription handle, String name, byte[][] value) {
  requireHandle(handle);

  try (PointerScope scope = new PointerScope()) {
    PointerPointer<BytePointer> valuePointers = new PointerPointer<BytePointer>(value.length);
    SizeTPointer lengths = new SizeTPointer(value.length);

    for (int i = 0; i < value.length; ++i) {
      valuePointers.put(i, new BytePointer(value[i]));
      lengths.put(i, value[i].length);
    }
    TF_SetAttrStringList(handle, new BytePointer(name), valuePointers, lengths, value.length);
  }
}
 
Example #17
Source File: GraphOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrShapeList(TF_OperationDescription handle, String name, long[] shapes, int[] numDims) {
  requireHandle(handle);

  try (PointerScope scope = new PointerScope()) {
    LongPointer shapesPointer = new LongPointer(shapes);
    PointerPointer<LongPointer> shapesPointers = new PointerPointer<LongPointer>(numDims.length);
    for (int i = 0; i < numDims.length; i++) {
      shapesPointers.put(i, shapesPointer);
      shapesPointer.position(shapesPointer.position() + numDims[i] * 8);
    }
    TF_SetAttrShapeList(handle, new BytePointer(name), shapesPointers, new IntPointer(numDims), numDims.length);
  }
}
 
Example #18
Source File: GraphOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrTensorList(TF_OperationDescription handle, String name, TF_Tensor[] tensorHandles) {
  requireHandle(handle);

  try (PointerScope scope = new PointerScope()) {
    PointerPointer<TF_Tensor> tensors = new PointerPointer<TF_Tensor>(tensorHandles.length);
    for (int i = 0; i < tensorHandles.length; ++i) {
      requireTensor(tensorHandles[i]);
      tensors.put(i, tensorHandles[i]);
    }

    TF_Status status = TF_Status.newStatus();
    TF_SetAttrTensorList(handle, new BytePointer(name), tensors.position(0), tensorHandles.length, status);
    status.throwExceptionIfNotOK();
  }
}
 
Example #19
Source File: Session.java    From java with Apache License 2.0 5 votes vote down vote up
private static void resolveHandles(String type, Pointer[] src, PointerPointer dst, int n) {
  if (src.length != n) {
    throw new IllegalArgumentException("expected " + n + ", got " + src.length + " " + type);
  }
  for (int i = 0; i < n; ++i) {
    if (src[i] == null || src[i].isNull()) {
      throw new IllegalStateException("invalid " + type + " (#" + i + " of " + n + ")");
    }
    dst.put(i, src[i]);
  }
}
 
Example #20
Source File: PictureResampler.java    From JavaAV with GNU General Public License v2.0 4 votes vote down vote up
void resample(AVPicture srcPicture, AVPicture dstPicture) throws JavaAVException {
	sws_scale(convertContext, new PointerPointer(srcPicture), srcPicture.linesize(), 0,
			srcFormat.getHeight(), new PointerPointer(dstPicture), dstPicture.linesize());
}
 
Example #21
Source File: AudioResampler.java    From JavaAV with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Resample audio samples within the provided audio frame. The audio format of the input
 * audio frame must be equal to the audio format previously specified with
 * {@link #open(AudioFormat, AudioFormat, int)}. The resampled samples are buffered, if
 * they do not fit completely in one output frame. Depending on output audio format more
 * than one frame may be returned.
 *
 * @param frame the audio frame with samples to be resampled.
 *
 * @return one or more audio frames with resampled audio.
 */
public AudioFrame[] resample(AudioFrame frame) {
	List<AudioFrame> frames = new ArrayList<AudioFrame>();

	int outputChannels = dstFormat.getChannels();
	int outputRate = dstFormat.getSampleRate();
	int outputFormat = dstFormat.getSampleFormat().value();
	int inputRate = srcFormat.getSampleRate();

	int planes = av_sample_fmt_is_planar(outputFormat) != 0 ? outputChannels : 1;
	int destSamples = Math.max(frameSamples, (int) av_rescale_rnd(frameSamples, outputRate, inputRate, AV_ROUND_DOWN));

	// use enough space to avoid buffering
	AudioFrame tempFrame = new AudioFrame(dstFormat, destSamples * 4);

	int inSamples = 0;
	int outSamples = frameSamples * 4; // make sure we get everything out

	PointerPointer inPointer = null;

	if (frame != null) {
		inSamples = frame.getSampleCount();
		inPointer = frame.getData();
	}

	if (outSamples <= 0)
		outSamples = inSamples;

	int resampled = swr_convert(convertContext, tempFrame.getData(), outSamples, inPointer, inSamples);

	// re-sampled plane size
	int limit = (resampled * outputChannels * av_get_bytes_per_sample(outputFormat)) / planes;

	// write samples into buffer in case the current frame cannot be fully filled
	for (int i = 0; i < tempFrame.getPlaneCount(); i++) {
		ByteBuffer buf = tempFrame.getPlane(i).asByteBuffer();
		buf.limit(Math.min(limit, buf.capacity()));

		buffer.write(i, buf);
	}

	// output frame plane size
	int bufferSize = (destSamples * outputChannels * av_get_bytes_per_sample(outputFormat)) / planes;

	// get buffered samples that fit in one frame
	while (buffer.available() >= bufferSize) {
		AudioFrame outFrame = new AudioFrame(dstFormat, destSamples);

		// go through each buffered plane
		for (int i = 0; i < planes; i++) {
			byte[] data = new byte[bufferSize];

			try {
				buffer.read(i, data);
			}
			catch (IOException e) {
				e.printStackTrace();
			}

			outFrame.getPlane(i).position(0).put(data);
		}

		outFrame.setSampleCount(frameSamples);

		frames.add(outFrame);
	}

	return frames.toArray(new AudioFrame[0]);
}
 
Example #22
Source File: CpuNativeRandom.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public PointerPointer getExtraPointers() {
    return null;
}
 
Example #23
Source File: CudaContext.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public cusolverDnHandle_t getSolverHandle() {
    // FIXME: can we cache this please
    val lptr = new PointerPointer(solverHandle);
    return new cusolverDnHandle_t(lptr.get(0));
}
 
Example #24
Source File: CudaContext.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public cublasHandle_t getCublasHandle() {
    // FIXME: can we cache this please
    val lptr = new PointerPointer(cublasHandle);
    return new cublasHandle_t(lptr.get(0));
}
 
Example #25
Source File: CudaContext.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Pointer getCublasStream() {
    // FIXME: can we cache this please
    val lptr = new PointerPointer(this.getOldStream());
    return lptr.get(0);
}
 
Example #26
Source File: CudaNativeRandom.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public PointerPointer getExtraPointers() {
    return null;
}
 
Example #27
Source File: CpuNativeRandom.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public PointerPointer getExtraPointers() {
    return null;
}
 
Example #28
Source File: MetaOpTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testPooling2D() {
        Nd4j.create(1);

        val input = Nd4j.linspace(1, 600, 600).reshape(2, 10, 10, 3);
        val permuted = input.permute(0, 3, 1, 2);

        val nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();

        val output = Nd4j.create(2, 3, 4, 4);

        val context = AtomicAllocator.getInstance().getFlowController().prepareAction(output, permuted);

        val ptrBIn = (FloatPointer) AtomicAllocator.getInstance().getPointer(permuted, context);
        val ptrBOut = (FloatPointer) AtomicAllocator.getInstance().getPointer(output, context);

        val ptrSIn = (IntPointer) AtomicAllocator.getInstance().getPointer(permuted.shapeInfoDataBuffer());
        val ptrSOut = (IntPointer) AtomicAllocator.getInstance().getPointer(output.shapeInfoDataBuffer());
        //                                                                       kY  kX  sY  sX  pY  pX  dY  dX  N   M   P
        val bufParams = Nd4j.getConstantHandler().getConstantBuffer(new float[] {3,  3,  3,  3,  1,  1,  1,  1,  1,  2,  2});
        val ptrBParams = (FloatPointer) AtomicAllocator.getInstance().getPointer(bufParams, context);

        PointerPointer xShapeInfoHostPointer = new PointerPointer(32).put(AddressRetriever.retrieveHostPointer(permuted.shapeInfoDataBuffer()), // 0
                        context.getOldStream(), // 1
                        AtomicAllocator.getInstance().getDeviceIdPointer(), // 2
                        context.getBufferAllocation(), // 3
                        context.getBufferReduction(), // 4
                        context.getBufferScalar(), // 5
                        context.getBufferSpecial(), // 6
                        null, // 7
                        AddressRetriever.retrieveHostPointer(output.shapeInfoDataBuffer()) // 8
/*                        hostTadShapeInfo, // 9
                        devTadShapeInfo, // 10
                        devTadOffsets, // 11
                        hostMaxTadShapeInfo, // 12
                        devMaxTadShapeInfo, // 13
                        devMaxTadOffsets, // 14
                        dimensionDevPointer, // special pointer for IsMax  // 15
                        dimensionHostPointer, // special pointer for IsMax  // 16
                        retPointer, // special pointer for IsMax // 17
                        new CudaPointer(dimension == null ? 0 : dimension.length));
*/
                        );
        nativeOps.execTransformFloat(xShapeInfoHostPointer, 71, ptrBIn, ptrSIn, ptrBOut, ptrSOut, ptrBParams);


        AtomicAllocator.getInstance().getFlowController().registerAction(context, output, permuted);
        nativeOps.streamSynchronize(context.getOldStream());
        nativeOps.streamSynchronize(context.getOldStream());

        val reverted = output.permute(0, 2, 3, 1);

        System.out.println("Result: " + reverted.toString());

    }
 
Example #29
Source File: TechnicalVideoMetadataExtractor.java    From cineast with MIT License 4 votes vote down vote up
/**
 * Extracts the technical video metadata from the specified path and returns a List of {@link MediaObjectMetadataDescriptor} objects (one for each metadata entry).
 *
 * @param objectId ID of the multimedia object for which metadata will be generated.
 * @param path Path to the file for which metadata should be extracted.
 * @return List of {@link MediaObjectMetadataDescriptor}s or an empty list, if extracting metadata fails.
 */
@Override
public List<MediaObjectMetadataDescriptor> extract(String objectId, Path path) {

  final ArrayList<MediaObjectMetadataDescriptor> metadata = new ArrayList<>();
  if (!Files.exists(path)) {
    LOGGER.warn("File does not exist, returning empty metadata");
    return metadata;
  }

  /* we assume that everythign which can be handled by the ffmpegvideodecoder can also be handled here. Without this safety-guard, extraction will crash with a core-dump */
  if (!FFMpegVideoDecoder.supportedFiles.contains(MimeTypeHelper.getContentType(path.toString()))) {
    LOGGER.warn("File is not a video, returning empty metadata");
    return metadata;
  }

  /* Initialize the AVFormatContext. */
  final AVFormatContext pFormatContext = avformat_alloc_context();

  /* Register all formats and codecs. */
  av_register_all();

  /* */
  if (avformat_open_input(pFormatContext, path.toString(), null, null) != 0) {
    LOGGER.error("Error while accessing file {}. Failed to obtain technical video metadata.", path.toString());
    return metadata;
  }

  /* Retrieve stream information. */
  if (avformat_find_stream_info(pFormatContext, (PointerPointer<?>) null) < 0) {
    LOGGER.error("Error, Ccouldn't find stream information. Failed to obtain technical video metadata.");
    return metadata;
  }

  final AVCodec codec = av_codec_next((AVCodec) null);
  final int videoStreamIdx = av_find_best_stream(pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, codec, 0);
  final AVStream videoStream = pFormatContext.streams(videoStreamIdx);
  final avutil.AVRational timebase = videoStream.time_base();

  /* Allocate new codec-context for codec returned by av_find_best_stream(). */
  final avcodec.AVCodecContext videoCodecContext = avcodec_alloc_context3(codec);
  avcodec_parameters_to_context(videoCodecContext, videoStream.codecpar());

  /* Open the code context. */
  if (avcodec_open2(videoCodecContext, codec, (avutil.AVDictionary) null) < 0) {
    LOGGER.error("Error, Could not open video codec.  Failed to obtain technical video metadata.");
    return metadata;
  }

  /* Extract and add the video metadata to the list. */
  metadata.add(new MediaObjectMetadataDescriptor(objectId, this.domain(), KEY_VIDEO_FPS, ((float) videoStream.avg_frame_rate().num() / (float) videoStream.avg_frame_rate().den()), false));
  metadata.add(new MediaObjectMetadataDescriptor(objectId, this.domain(), KEY_VIDEO_DURATION, Math.floorDiv(videoStream.duration() * timebase.num() * 1000, timebase.den()), false));
  metadata.add(new MediaObjectMetadataDescriptor(objectId, this.domain(), KEY_VIDEO_WIDTH, videoCodecContext.width(), false));
  metadata.add(new MediaObjectMetadataDescriptor(objectId, this.domain(), KEY_VIDEO_HEIGHT, videoCodecContext.height(), false));

  /* Closes all the resources. */
  avcodec_free_context(videoCodecContext);
  avformat_close_input(pFormatContext);

  /* Return list of results. */
  return metadata;
}
 
Example #30
Source File: Generator.java    From tapir with MIT License 4 votes vote down vote up
String[] cppTypeName(Class<?> type) {
    String prefix = "", suffix = "";
    if (type == Buffer.class || type == Pointer.class) {
        prefix = "void*";
    } else if (type == byte[].class || type == ByteBuffer.class || type == BytePointer.class) {
        prefix = "signed char*";
    } else if (type == short[].class || type == ShortBuffer.class || type == ShortPointer.class) {
        prefix = "short*";
    } else if (type == int[].class || type == IntBuffer.class || type == IntPointer.class) {
        prefix = "int*";
    } else if (type == long[].class || type == LongBuffer.class || type == LongPointer.class) {
        prefix = "jlong*";
    } else if (type == float[].class || type == FloatBuffer.class || type == FloatPointer.class) {
        prefix = "float*";
    } else if (type == double[].class || type == DoubleBuffer.class || type == DoublePointer.class) {
        prefix = "double*";
    } else if (type == char[].class || type == CharBuffer.class || type == CharPointer.class) {
        prefix = "unsigned short*";
    } else if (type == boolean[].class) {
        prefix = "unsigned char*";
    } else if (type == PointerPointer.class) {
        prefix = "void**";
    } else if (type == String.class) {
        prefix = "const char*";
    } else if (type == byte.class) {
        prefix = "signed char";
    } else if (type == long.class) {
        prefix = "jlong";
    } else if (type == char.class) {
        prefix = "unsigned short";
    } else if (type == boolean.class) {
        prefix = "unsigned char";
    } else if (type.isPrimitive()) {
        prefix = type.getName();
    } else if (FunctionPointer.class.isAssignableFrom(type)) {
        Method functionMethod = functionMethod(type, null);
        if (functionMethod != null) {
            return cppFunctionTypeName(functionMethod);
        }
    } else {
        String scopedType = cppScopeName(type);
        if (scopedType.length() > 0) {
            prefix = scopedType + "*";
        } else {
            logger.warn("The class " + type.getCanonicalName() +
                    " does not map to any C++ type. Compilation will most likely fail.");
        }
    }
    return new String[] { prefix, suffix };
}