Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jpeg encoder sample and rename author #1365

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add new `SampleJpegEncoder` code for nvJPEG module of CUDA ([pull #1365](https://github.com/bytedeco/javacpp-presets/pull/1365))
* Map `std::vector` of `CameraParams`, `ImageFeatures`, and `MatchesInfo` from `cv::detail` ([issue bytedeco/javacv#2027](https://github.com/bytedeco/javacv/issues/2027))
* Fix H.264 decoder of FFmpeg by increasing MAX_SLICES to 256 ([pull #1349](https://github.com/bytedeco/javacpp-presets/pull/1349))
* Link FFmpeg with latest version of VA-API libraries ([pull #1296](https://github.com/bytedeco/javacpp-presets/pull/1296))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Park JeongHwan
* Copyright (C) 2022 Jeonghwan Park
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -92,7 +92,7 @@ public static void main(String[] args) {

nvjpegDecodeParams params = new nvjpegDecodeParams();

// Create Components
// Create components
CHECK_NVJPEG("nvjpegCreateEx", nvjpegCreateEx(NVJPEG_BACKEND_DEFAULT, devAllocator, pinnedAllocator, NVJPEG_FLAGS_DEFAULT, handle));
CHECK_NVJPEG("nvjpegJpegStateCreate", nvjpegJpegStateCreate(handle, state));

Expand All @@ -108,7 +108,7 @@ public static void main(String[] args) {

CHECK_NVJPEG("nvjpegDecodeParamsCreate", nvjpegDecodeParamsCreate(handle, params));

// Destroy Components
// Destroy components
CHECK_NVJPEG("nvjpegDecodeParamsDestroy", nvjpegDecodeParamsDestroy(params));

CHECK_NVJPEG("nvjpegJpegStreamDestroy", nvjpegJpegStreamDestroy(streams[0]));
Expand Down
155 changes: 155 additions & 0 deletions cuda/samples/SampleJpegEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (C) 2023 Jeonghwan Park
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
* the Free Software Foundation (subject to the "Classpath" exception),
* either version 2, or any later version (collectively, the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/
* http://www.gnu.org/software/classpath/license.html
*
* or as provided in the LICENSE.txt file that accompanied this code.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.bytedeco.cuda.cudart.*;
import org.bytedeco.cuda.nvjpeg.*;
import org.bytedeco.javacpp.Pointer;
import org.bytedeco.javacpp.SizeTPointer;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.PointerPointer;

import static org.bytedeco.cuda.global.cudart.*;
import static org.bytedeco.cuda.global.nvjpeg.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class SampleJpeg {
static class dev_malloc extends tDevMalloc {
final static dev_malloc instance = new dev_malloc().retainReference();

@Override
public int call(PointerPointer pointerPointer, long l) {
return cudaMalloc(pointerPointer, l);
}
}

static class dev_free extends tDevFree {
final static dev_free instance = new dev_free().retainReference();

@Override
public int call(Pointer pointer) {
return cudaFree(pointer);
}
}

static class host_malloc extends tPinnedMalloc {
final static host_malloc instance = new host_malloc().retainReference();

@Override
public int call(PointerPointer pointerPointer, long l, int i) {
return cudaHostAlloc(pointerPointer, l, i);
}
}

static class host_free extends tPinnedFree {
final static host_free instance = new host_free().retainReference();

@Override
public int call(Pointer pointer) {
return cudaFreeHost(pointer);
}
}

public static void CHECK_CUDA(String functionName, int result) {
if (result != CUDA_SUCCESS) {
throw new IllegalStateException(String.format("%s returned '%d'", functionName, result));
}
}

public static void CHECK_NVJPEG(String functionName, int result) {
if (result != NVJPEG_STATUS_SUCCESS) {
throw new IllegalStateException(String.format("%s returned '%d'", functionName, result));
}
}

public static void main(String[] args) {
int imageWidth = 1280;
int imageHeight = 720;

nvjpegDevAllocator_t devAllocator = new nvjpegDevAllocator_t();
devAllocator.dev_malloc(devMalloc.instance);
devAllocator.dev_free(dev_free.instance);

nvjpegPinnedAllocator_t pinnedAllocator = new nvjpegPinnedAllocator_t();
pinnedAllocator.pinned_malloc(hostMalloc.instance);
pinnedAllocator.pinned_free(hostFree.instance);

// Initialize cuda
CUctx_st context = new CUctx_st();
CHECK_CUDA("cuInit", cuInit(0));
CHECK_CUDA("cuCtxCreate", cuCtxCreate(context, CU_CTX_SCHED_BLOCKING_SYNC, 0));

// Create cuda stream
CUstream_st stream = new CUstream_st();
CHECK_CUDA("cuStreamCreate", cuStreamCreate(stream, 0));

// Create handle
nvjpegHandle nvjpegHandle = new nvjpegHandle();
CHECK_NVJPEG("nvjpegCreateEx", nvjpegCreateEx(NVJPEG_BACKEND_DEFAULT, devAllocator, pinnedAllocator, NVJPEG_FLAGS_DEFAULT, nvjpegHandle));

// Create encoder components
nvjpegEncoderState nvjpegEncoderState = new nvjpegEncoderState();
nvjpegEncoderParams nvjpegEncoderParams = new nvjpegEncoderParams();
CHECK_NVJPEG("nvjpegEncoderParamsCreate", nvjpegEncoderParamsCreate(nvjpegHandle, nvjpegEncoderParams, stream));
CHECK_NVJPEG("nvjpegEncoderStateCreate", nvjpegEncoderStateCreate(nvjpegHandle, nvjpegEncoderState, stream));
CHECK_NVJPEG("nvjpegEncoderParamsSetSamplingFactors", nvjpegEncoderParamsSetSamplingFactors(nvjpegEncoderParams, NVJPEG_CSS_444, stream));

// Create jpeg image
nvjpegImage_t nvjpegImage = new nvjpegImage_t();

long channelSize = imageWidth * imageHeight;

// Fill image to blue
for (int i = 0; i < 3; i++) {
nvjpegImage.pitch(i, imageWidth);
BytePointer deviceMemory = new BytePointer();

CHECK_CUDA("cudaMalloc", cudaMalloc(deviceMemory, channelSize));
CHECK_CUDA("cudaMemset", cudaMemset(deviceMemory, i == 0 ? 255 : 0, channelSize));

nvjpegImage.channel(i, deviceMemory);
}

// Compress image
CHECK_NVJPEG("nvjpegEncodeImage", nvjpegEncodeImage(nvjpegHandle, nvjpegEncoderState, nvjpegEncoderParams, nvjpegImage, NVJPEG_INPUT_BGR, imageWidth, imageHeight, stream));

// Get compressed size
SizeTPointer jpegSize = new SizeTPointer(1);
CHECK_NVJPEG("nvjpegEncodeRetrieveBitstream", nvjpegEncodeRetrieveBitstream(nvjpegHandle, nvjpegEncoderState, (BytePointer) null, jpegSize, stream));

// Retrieve bitstream
BytePointer jpegBytePointer = new BytePointer(jpegSize.get());
CHECK_NVJPEG("nvjpegEncodeRetrieveBitstream", nvjpegEncodeRetrieveBitstream(nvjpegHandle, nvjpegEncoderState, jpegBytePointer, jpegSize, stream));

// Synchronize cuda stream
CHECK_CUDA("cudaStreamSynchronize", cudaStreamSynchronize(stream));

// Get bitstream to java side array
byte[] bytes = new byte[(int) jpegSize.get()];
jpegBytePointer.get(bytes);

// Write
Files.write(new File("out.jpg").toPath(), bytes);
}
}
4 changes: 2 additions & 2 deletions cuda/src/main/java/org/bytedeco/cuda/presets/nvjpeg.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Park JeongHwan, Samuel Audet
* Copyright (C) 2022-2023 Jeonghwan Park, Samuel Audet
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,7 +31,7 @@

/**
*
* @author Park JeongHwan
* @author Jeonghwan Park
*/
@Properties(inherit = cudart.class, value = {
@Platform(include = "<nvjpeg.h>", link = "nvjpeg@.12"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Park JeongHwan
* Copyright (C) 2021 Jeonghwan Park
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -37,7 +37,7 @@

/**
*
* @author Park JeongHwan
* @author Jeonghwan Park
*/
@Properties(
inherit = cudart.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2023 Park JeongHwan, Samuel Audet
* Copyright (C) 2021-2023 Jeonghwan Park, Samuel Audet
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,7 +32,7 @@

/**
*
* @author Park JeongHwan
* @author Jeonghwan Park
*/
@Properties(
inherit = nvcuvid.class,
Expand Down