Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Oct 4, 2023
1 parent a2c1f77 commit ac7d7dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 47 deletions.
24 changes: 5 additions & 19 deletions src/hwenc_jetson/jetson_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,15 @@ rtc::scoped_refptr<webrtc::I420BufferInterface> JetsonBuffer::ToI420() {
dst_surface->memType = NVBUF_MEM_SURFACE_ARRAY;

NvBufSurface* dst_surfaces[] = {dst_surface};
int batch_size = sizeof(dst_surfaces) / sizeof(dst_surfaces[0]);

if (NvBufSurfaceAllocate(dst_surfaces, batch_size, &input_params) == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufferCreateEx";
if (NvBufSurfaceAllocate(dst_surfaces, 1, // batch size
&input_params) == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufSurfaceAllocate";
return scaled_buffer;
}
int dmabuf_fd = dst_surface->gpuId;
int dmabuf_fd = dst_surface->surfaceList[0].bufferDesc;
NvBufSurfaceParams params = dst_surfaces[0]->surfaceList[0];

// NvBufferParams params = {0};
// if (NvBufferGetParams(fd_, &params) == -1) {
// RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufferGetParams";
// return scaled_buffer;
// }

NvBufSurfTransformRect src_rect, dest_rect;
src_rect.top = 0;
src_rect.left = 0;
Expand All @@ -109,26 +103,18 @@ rtc::scoped_refptr<webrtc::I420BufferInterface> JetsonBuffer::ToI420() {

// NvBufSurfaceFromFd で fd_ を NvBufSurface に変換する
// 参照:_install/ubuntu-20.04_armv8_jetson/release/rootfs/usr/src/jetson_multimedia_api/samples/12_v4l2_camera_cuda/camera_v4l2_cuda.cpp
// NOTE: NvBufSurfaceFromFd のコストが高い場合、コンストラクタで変換する必要があるかもしれない?
NvBufSurface* src_surface = NULL;
if (-1 == NvBufSurfaceFromFd(fd_, (void**)(&src_surface))) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << "Failed to get NvBufSurface from FD";
return scaled_buffer;
}

// TODO: memcpy を使わなくても dst からデータを取得できそうなので調べる
if (NvBufSurfTransform(src_surface, dst_surface, &trans_params) !=
NvBufSurfTransformError_Success) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufferTransform";
return scaled_buffer;
}

// NvBufferParams dmabuf_params = {0};
// if (NvBufferGetParams(dmabuf_fd, &dmabuf_params) == -1) {
// RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to NvBufferGetParams";
// return scaled_buffer;
// }

int ret;
void* data_addr;
uint8_t* dest_addr;
Expand Down Expand Up @@ -159,7 +145,7 @@ rtc::scoped_refptr<webrtc::I420BufferInterface> JetsonBuffer::ToI420() {
width);
}
}
NvBufSurfaceUnMap(dst_surface, 0, plane);
NvBufSurfaceUnMap(dst_surface, index, plane);
if (ret == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__
<< " Failed to NvBufSurfaceMap plane=" << plane;
Expand Down
33 changes: 8 additions & 25 deletions src/hwenc_jetson/jetson_video_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ bool JetsonVideoDecoder::JetsonRelease() {
decoder_ = nullptr;
}
if (dst_dma_fd_ != -1) {
// TODO: NvBufSurface にへんかんしてから破棄する?
// TODO: NvBufSurface を JetsonVideoDecoder クラスに保持するように修正してから、 NvBufferDestroy を NvBufSurfaceDestroy に置き換えていく
// NvBufferDestroy(dst_dma_fd_);
// dst_dma_fd_ = -1;
}
Expand Down Expand Up @@ -333,26 +333,15 @@ void JetsonVideoDecoder::CaptureLoop() {
transform_params.src_rect = &src_rect;
transform_params.dst_rect = &dest_rect;

// dst_dma_fd_ と buffer の fd を それぞれ NvBufSurface に変換する
// NvBufSurface* dst = NULL;
// if (-1 == NvBufSurfaceFromFd(dst_dma_fd_, (void**)(&dst))) {
// RTC_LOG(LS_ERROR) << __FUNCTION__
// << "Failed to get NvBufSurface from destination FD";
// got_error_ = true;
// break;
// }

NvBufSurface* src_surface = NULL;
if (-1 ==
NvBufSurfaceFromFd(buffer->planes[0].fd, (void**)(&src_surface))) {
RTC_LOG(LS_ERROR) << __FUNCTION__
<< "Failed to get NvBufSurface from source FD";
RTC_LOG(LS_ERROR) << __FUNCTION__ << "Failed to NvBufSurfaceFromFd";
got_error_ = true;
break;
}

// 何が来ても YUV420 に変換する
// TODO: memcpy を使わなくても dst からデータを取得できそうなので調べる
ret = NvBufSurfTransform(src_surface, dst_surface, &transform_params);
if (ret == -1) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Transform failed";
Expand All @@ -370,9 +359,6 @@ void JetsonVideoDecoder::CaptureLoop() {
break;
}

// NvBufferParams parm;
// ret = NvBufferGetParams(dst_dma_fd_, &parm);

void* src_data;
uint8_t* dst_data;
int dst_stride;
Expand All @@ -395,14 +381,12 @@ void JetsonVideoDecoder::CaptureLoop() {
NvBufSurfaceSyncForCpu(dst_surface, 0, i);

// TODO: plane 毎の height を計算する方法を調べる
NvBufSurfaceParams params = dst_surface->surfaceList[0];
int height = params.height;
for (uint32_t j = 0; j < height; j++) {
// TODO: 要確認
memcpy(dst_data + j * dst_stride, (char*)src_data + j * params.pitch,
params.width);
NvBufSurfacePlaneParams params =
dst_surface->surfaceList[0].planeParams;
for (uint32_t j = 0; j < params.height[i]; j++) {
memcpy(dst_data + j * dst_stride,
(char*)src_data + j * params.pitch[i], params.width[i]);
}
// NvBufferMemUnMap(dst_dma_fd_, i, &src_data);
NvBufSurfaceUnMap(dst_surface, 0, i);
}

Expand Down Expand Up @@ -444,7 +428,7 @@ int JetsonVideoDecoder::SetCapture() {
<< "x" << format.fmt.pix_mp.height;

if (dst_dma_fd_ != -1) {
// TODO: NvBufSurface に変換して class で破棄する?
// TODO: NvBufSurface を JetsonVideoDecoder クラスに保持するように修正してから、 NvBufferDestroy を NvBufSurfaceDestroy に置き換えていく
// NvBufferDestroy(dst_dma_fd_);
// dst_dma_fd_ = -1;
}
Expand All @@ -461,7 +445,6 @@ int JetsonVideoDecoder::SetCapture() {

NvBufSurface* dst_surfaces[] = {dst_surface};

// TODO: dst_surface を class で保持する
ret = NvBufSurfaceAllocate(dst_surfaces, 1, &input_params);
INIT_ERROR(ret == -1, "failed to NvBufSurfaceAllocate");

Expand Down
7 changes: 4 additions & 3 deletions src/hwenc_jetson/jetson_video_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,11 @@ int32_t JetsonVideoEncoder::Encode(
input_frame.timestamp_us() % rtc::kNumMicrosecsPerSec;

for (int i = 0; i < MAX_PLANES; i++) {
// JetPack 5.2.1 対応で NvBufferMemSyncForDevice を NvBufSurfaceSyncForDevice に置き換える際に、
// fd から NvBufSurface を作成する必要があった
NvBufSurface* surface = NULL;
if (-1 == NvBufSurfaceFromFd(buffer->planes[i].fd, (void**)(&surface))) {
RTC_LOG(LS_ERROR) << __FUNCTION__
<< "Failed to get NvBufSurface from FD";
RTC_LOG(LS_ERROR) << __FUNCTION__ << "Failed to NvBufSurfaceFromFd";
return WEBRTC_VIDEO_CODEC_ERROR;
}

Expand All @@ -760,7 +761,7 @@ int32_t JetsonVideoEncoder::Encode(
return WEBRTC_VIDEO_CODEC_ERROR;
}

// TODO: NvBufsurface の解放
NvBufSurfaceDestroy(surface);
}

if (encoder_->output_plane.qBuffer(v4l2_buf, nullptr) < 0) {
Expand Down

0 comments on commit ac7d7dd

Please sign in to comment.