dma_fd in frame

This commit is contained in:
Maxim Devaev
2021-11-19 01:23:09 +03:00
parent 7732841cdf
commit 52478df3cf
8 changed files with 22 additions and 19 deletions

View File

@@ -27,6 +27,7 @@ frame_s *frame_init(void) {
frame_s *frame; frame_s *frame;
A_CALLOC(frame, 1); A_CALLOC(frame, 1);
frame_realloc_data(frame, 512 * 1024); frame_realloc_data(frame, 512 * 1024);
frame->dma_fd = -1;
return frame; return frame;
} }

View File

@@ -38,6 +38,7 @@ typedef struct {
uint8_t *data; uint8_t *data;
size_t used; size_t used;
size_t allocated; size_t allocated;
int dma_fd;
unsigned width; unsigned width;
unsigned height; unsigned height;

View File

@@ -357,6 +357,7 @@ int device_grab_buffer(device_s *dev, hw_buffer_s **hw) {
HW(grabbed) = true; HW(grabbed) = true;
A_MUTEX_UNLOCK(&HW(grabbed_mutex)); A_MUTEX_UNLOCK(&HW(grabbed_mutex));
HW(raw.dma_fd) = HW(dma_fd);
HW(raw.used) = buf.bytesused; HW(raw.used) = buf.bytesused;
HW(raw.width) = RUN(width); HW(raw.width) = RUN(width);
HW(raw.height) = RUN(height); HW(raw.height) = RUN(height);

View File

@@ -62,7 +62,7 @@ void h264_stream_destroy(h264_stream_s *h264) {
free(h264); free(h264);
} }
void h264_stream_process(h264_stream_s *h264, const frame_s *frame, int dma_fd, bool force_key) { void h264_stream_process(h264_stream_s *h264, const frame_s *frame, bool force_key) {
if (!memsink_server_check(h264->sink, frame)) { if (!memsink_server_check(h264->sink, frame)) {
return; return;
} }
@@ -71,14 +71,14 @@ void h264_stream_process(h264_stream_s *h264, const frame_s *frame, int dma_fd,
bool dma = false; bool dma = false;
if (is_jpeg(frame->format)) { if (is_jpeg(frame->format)) {
assert(dma_fd <= 0); assert(frame->dma_fd <= 0);
LOG_DEBUG("H264: Input frame is JPEG; decoding ..."); LOG_DEBUG("H264: Input frame is JPEG; decoding ...");
if (unjpeg(frame, h264->tmp_src, true) < 0) { if (unjpeg(frame, h264->tmp_src, true) < 0) {
return; return;
} }
frame = h264->tmp_src; frame = h264->tmp_src;
LOG_VERBOSE("H264: JPEG decoded; time=%.3Lf", get_now_monotonic() - now); LOG_VERBOSE("H264: JPEG decoded; time=%.3Lf", get_now_monotonic() - now);
} else if (dma_fd > 0) { } else if (frame->dma_fd > 0) {
LOG_DEBUG("H264: DMA available for the input"); LOG_DEBUG("H264: DMA available for the input");
dma = true; dma = true;
} else { } else {
@@ -95,7 +95,7 @@ void h264_stream_process(h264_stream_s *h264, const frame_s *frame, int dma_fd,
} }
if (h264->enc->ready) { if (h264->enc->ready) {
if (m2m_encoder_compress(h264->enc, frame, dma_fd, h264->dest, force_key) == 0) { if (m2m_encoder_compress(h264->enc, frame, h264->dest, force_key) == 0) {
online = !memsink_server_put(h264->sink, h264->dest); online = !memsink_server_put(h264->sink, h264->dest);
} }
} }

View File

@@ -45,4 +45,4 @@ typedef struct {
h264_stream_s *h264_stream_init(memsink_s *sink, const char *path, unsigned bitrate, unsigned gop); h264_stream_s *h264_stream_init(memsink_s *sink, const char *path, unsigned bitrate, unsigned gop);
void h264_stream_destroy(h264_stream_s *h264); void h264_stream_destroy(h264_stream_s *h264);
void h264_stream_process(h264_stream_s *h264, const frame_s *frame, int dma_fd, bool force_key); void h264_stream_process(h264_stream_s *h264, const frame_s *frame, bool force_key);

View File

@@ -30,7 +30,7 @@ static int _m2m_encoder_init_buffers(
static void _m2m_encoder_cleanup(m2m_encoder_s *enc); static void _m2m_encoder_cleanup(m2m_encoder_s *enc);
static int _m2m_encoder_compress_raw( static int _m2m_encoder_compress_raw(
m2m_encoder_s *enc, const frame_s *src, int src_dma_fd, m2m_encoder_s *enc, const frame_s *src,
frame_s *dest, bool force_key); frame_s *dest, bool force_key);
@@ -292,7 +292,7 @@ static void _m2m_encoder_cleanup(m2m_encoder_s *enc) {
E_LOG_DEBUG("Encoder state: ~~~ NOT READY ~~~"); E_LOG_DEBUG("Encoder state: ~~~ NOT READY ~~~");
} }
int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, int src_dma_fd, frame_s *dest, bool force_key) { int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, bool force_key) {
assert(enc->ready); assert(enc->ready);
assert(src->used > 0); assert(src->used > 0);
assert(enc->width == src->width); assert(enc->width == src->width);
@@ -300,9 +300,9 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, int src_dma_fd,
assert(enc->format == src->format); assert(enc->format == src->format);
assert(enc->stride == src->stride); assert(enc->stride == src->stride);
if (enc->dma) { if (enc->dma) {
assert(src_dma_fd >= 0); assert(src->dma_fd >= 0);
} else { } else {
assert(src_dma_fd < 0); assert(src->dma_fd < 0);
} }
frame_copy_meta(src, dest); frame_copy_meta(src, dest);
@@ -312,7 +312,7 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, int src_dma_fd,
force_key = (force_key || enc->last_online != src->online); force_key = (force_key || enc->last_online != src->online);
if (_m2m_encoder_compress_raw(enc, src, src_dma_fd, dest, force_key) < 0) { if (_m2m_encoder_compress_raw(enc, src, dest, force_key) < 0) {
_m2m_encoder_cleanup(enc); _m2m_encoder_cleanup(enc);
E_LOG_ERROR("Encoder destroyed due an error (compress)"); E_LOG_ERROR("Encoder destroyed due an error (compress)");
return -1; return -1;
@@ -327,7 +327,7 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, int src_dma_fd,
} }
static int _m2m_encoder_compress_raw( static int _m2m_encoder_compress_raw(
m2m_encoder_s *enc, const frame_s *src, int src_dma_fd, m2m_encoder_s *enc, const frame_s *src,
frame_s *dest, bool force_key) { frame_s *dest, bool force_key) {
E_LOG_DEBUG("Compressing new frame; force_key=%d ...", force_key); E_LOG_DEBUG("Compressing new frame; force_key=%d ...", force_key);
@@ -347,14 +347,14 @@ static int _m2m_encoder_compress_raw(
input_buf.m.planes = &input_plane; input_buf.m.planes = &input_plane;
if (enc->dma) { if (enc->dma) {
assert(src_dma_fd >= 0); assert(src->dma_fd >= 0);
input_buf.index = 0; input_buf.index = 0;
input_buf.memory = V4L2_MEMORY_DMABUF; input_buf.memory = V4L2_MEMORY_DMABUF;
input_buf.field = V4L2_FIELD_NONE; input_buf.field = V4L2_FIELD_NONE;
input_plane.m.fd = src_dma_fd; input_plane.m.fd = src->dma_fd;
E_LOG_DEBUG("Using INPUT-DMA buffer index=%u", input_buf.index); E_LOG_DEBUG("Using INPUT-DMA buffer index=%u", input_buf.index);
} else { } else {
assert(src_dma_fd < 0); assert(src->dma_fd < 0);
input_buf.memory = V4L2_MEMORY_MMAP; input_buf.memory = V4L2_MEMORY_MMAP;
E_LOG_DEBUG("Grabbing INPUT buffer ..."); E_LOG_DEBUG("Grabbing INPUT buffer ...");
E_XIOCTL(VIDIOC_DQBUF, &input_buf, "Can't grab INPUT buffer"); E_XIOCTL(VIDIOC_DQBUF, &input_buf, "Can't grab INPUT buffer");

View File

@@ -83,4 +83,4 @@ void m2m_encoder_destroy(m2m_encoder_s *enc);
bool m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame, bool dma); bool m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame, bool dma);
int m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame, bool dma); int m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame, bool dma);
int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, int src_dma_fd, frame_s *dest, bool force_key); int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, bool force_key);

View File

@@ -36,9 +36,9 @@ static void _stream_expose_frame(stream_s *stream, frame_s *frame, unsigned capt
} \ } \
} }
#define H264_PUT(_frame, _dma_fd, _force_key) { \ #define H264_PUT(_frame, _force_key) { \
if (RUN(h264)) { \ if (RUN(h264)) { \
h264_stream_process(RUN(h264), _frame, _dma_fd, _force_key); \ h264_stream_process(RUN(h264), _frame, _force_key); \
} \ } \
} }
@@ -191,7 +191,7 @@ void stream_loop(stream_s *stream) {
LOG_DEBUG("Assigned new frame in buffer %d to worker %s", buf_index, ready_wr->name); LOG_DEBUG("Assigned new frame in buffer %d to worker %s", buf_index, ready_wr->name);
SINK_PUT(raw_sink, &hw->raw); SINK_PUT(raw_sink, &hw->raw);
H264_PUT(&hw->raw, hw->dma_fd, h264_force_key); H264_PUT(&hw->raw, h264_force_key);
} }
} else if (buf_index != -2) { // -2 for broken frame } else if (buf_index != -2) { // -2 for broken frame
break; break;
@@ -347,7 +347,7 @@ static void _stream_expose_frame(stream_s *stream, frame_s *frame, unsigned capt
if (frame == NULL) { if (frame == NULL) {
SINK_PUT(raw_sink, stream->blank); SINK_PUT(raw_sink, stream->blank);
H264_PUT(stream->blank, -1, false); H264_PUT(stream->blank, false);
} }
# undef VID # undef VID