Skip to content

Commit

Permalink
test NvBufSurf in jetson_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Oct 10, 2023
1 parent 9f1dfb2 commit e870c38
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions src/hwenc_jetson/jetson_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <third_party/libyuv/include/libyuv.h>

// Jetson Linux Multimedia API
#include <NvBufSurface.h>
#include <nvbufsurface.h>
#include <nvbufsurftransform.h>

Expand Down Expand Up @@ -63,55 +64,46 @@ rtc::scoped_refptr<webrtc::I420BufferInterface> JetsonBuffer::ToI420() {
int32_t buffer_width = ((scaled_width_ + 15) / 16) * 16;
int32_t buffer_height = ((scaled_height_ + 15) / 16) * 16;

NvBufSurfaceAllocateParams input_params = {0};
input_params.params.width = buffer_width;
input_params.params.height = buffer_height;
input_params.params.layout = NVBUF_LAYOUT_PITCH;
input_params.params.colorFormat = NVBUF_COLOR_FORMAT_YUV420;
input_params.params.memType = NVBUF_MEM_SURFACE_ARRAY;
input_params.memtag = NvBufSurfaceTag_NONE;

NvBufSurface* dst_surf = 0;

if (NvBufSurfaceAllocate(
&dst_surf,
1, /* NvUtils では複数のバッファーを同時に初期化できるため、バッファーの数を指定する */
&input_params) == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufSurfaceAllocate";
NvBufSurf::NvCommonAllocateParams cParams;
cParams.width = buffer_width;
cParams.height = buffer_height;
cParams.layout = NVBUF_LAYOUT_PITCH;
cParams.colorFormat = NVBUF_COLOR_FORMAT_YUV420;
cParams.memType = NVBUF_MEM_SURFACE_ARRAY;
cParams.memtag = NvBufSurfaceTag_NONE;

int dmabuf_fd;
if (NvBufSurf::NvAllocate(&cParams, 1, &dmabuf_fd) == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << "Failed to create NvBuffer";
return scaled_buffer;
}
NvBufSurfaceParams params = dst_surf->surfaceList[0];

NvBufSurfTransformRect src_rect, dest_rect;
src_rect.top = 0;
src_rect.left = 0;
src_rect.width = params.width;
src_rect.height = params.height;
dest_rect.top = 0;
dest_rect.left = 0;
dest_rect.width = buffer_width;
dest_rect.height = buffer_height;

NvBufSurfTransformParams trans_params;
memset(&trans_params, 0, sizeof(trans_params));
trans_params.transform_flag = NVBUFSURF_TRANSFORM_FILTER;
trans_params.transform_flip = NvBufSurfTransform_None;
trans_params.transform_filter = NvBufSurfTransformInter_Algo3;
trans_params.src_rect = &src_rect;
trans_params.dst_rect = &dest_rect;

NvBufSurface* src_surf = 0;
if (NvBufSurfaceFromFd(fd_, (void**)(&src_surf)) == -1) {
NvBufSurface* dst_surf = 0;
if (NvBufSurfaceFromFd(dmabuf_fd, (void**)(&dst_surf)) == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufSurfaceFromFd";
return scaled_buffer;
}
NvBufSurfaceParams params = dst_surf->surfaceList[0];

if (NvBufSurfTransform(src_surf, dst_surf, &trans_params) !=
NvBufSurfTransformError_Success) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufSurfTransform";
NvBufSurf::NvCommonTransformParams transform_params;
/* Indicates which of the transform parameters are valid */
memset(&transform_params, 0, sizeof(transform_params));
transform_params.src_top = 0;
transform_params.src_left = 0;
transform_params.src_width = params.width;
transform_params.src_height = params.height;
transform_params.dst_top = 0;
transform_params.dst_left = 0;
transform_params.dst_width = buffer_width;
transform_params.dst_height = buffer_height;
transform_params.flag = NVBUFSURF_TRANSFORM_FILTER;
transform_params.flip = NvBufSurfTransform_None;
transform_params.filter = NvBufSurfTransformInter_Algo3;

if (NvBufSurf::NvTransform(&transform_params, fd_, dmabuf_fd) == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvTransform";
return scaled_buffer;
}

int ret;
void* data_addr;
uint8_t* dest_addr;
Expand Down Expand Up @@ -151,7 +143,7 @@ rtc::scoped_refptr<webrtc::I420BufferInterface> JetsonBuffer::ToI420() {
}
}

NvBufSurfaceDestroy(dst_surf);
NvBufSurf::NvDestroy(dmabuf_fd);

return scaled_buffer;
} else {
Expand Down

0 comments on commit e870c38

Please sign in to comment.