[mpi_enc_test]: Support camera capture encoder

if input file name is start with "/dev/video",  default  open camera
device as input.

Change-Id: I85d639ddb6fa5d99413ea75a0b088c79798dd6c8
Signed-off-by: sayon.chen <sayon.chen@rock-chips.com>
This commit is contained in:
sayon.chen
2020-04-16 17:14:29 +08:00
committed by Herman Chen
parent 12431e34d6
commit 8653a80c36

View File

@@ -31,6 +31,7 @@
#include "utils.h" #include "utils.h"
#include "mpi_enc_utils.h" #include "mpi_enc_utils.h"
#include "camera_source.h"
typedef struct { typedef struct {
// global flow control flag // global flow control flag
@@ -75,6 +76,7 @@ typedef struct {
MppCodingType type; MppCodingType type;
RK_S32 num_frames; RK_S32 num_frames;
RK_S32 loop_times; RK_S32 loop_times;
CamSource *cam_ctx;
// resources // resources
size_t header_size; size_t header_size;
@@ -160,10 +162,18 @@ MPP_RET test_ctx_init(MpiEncTestData **data, MpiEncTestArgs *cmd)
p->fps_out_num = cmd->fps_out_num; p->fps_out_num = cmd->fps_out_num;
if (cmd->file_input) { if (cmd->file_input) {
p->fp_input = fopen(cmd->file_input, "rb"); if (!strncmp(cmd->file_input, "/dev/video", 10)) {
if (NULL == p->fp_input) { mpp_log("open camera device");
mpp_err("failed to open input file %s\n", cmd->file_input); p->cam_ctx = camera_source_init(cmd->file_input, 4, p->width, p->height, p->fmt);
mpp_err("create default yuv image for test\n"); mpp_log("new framecap ok");
if (p->cam_ctx == NULL)
mpp_err("open %s fail", cmd->file_input);
} else {
p->fp_input = fopen(cmd->file_input, "rb");
if (NULL == p->fp_input) {
mpp_err("failed to open input file %s\n", cmd->file_input);
mpp_err("create default yuv image for test\n");
}
} }
} }
@@ -234,9 +244,12 @@ MPP_RET test_ctx_deinit(MpiEncTestData **data)
mpp_err_f("invalid input data %p\n", data); mpp_err_f("invalid input data %p\n", data);
return MPP_ERR_NULL_PTR; return MPP_ERR_NULL_PTR;
} }
p = *data; p = *data;
if (p) { if (p) {
if (p->cam_ctx) {
camera_source_deinit(p->cam_ctx);
p->cam_ctx = NULL;
}
if (p->fp_input) { if (p->fp_input) {
fclose(p->fp_input); fclose(p->fp_input);
p->fp_input = NULL; p->fp_input = NULL;
@@ -248,7 +261,6 @@ MPP_RET test_ctx_deinit(MpiEncTestData **data)
MPP_FREE(p); MPP_FREE(p);
*data = NULL; *data = NULL;
} }
return MPP_OK; return MPP_OK;
} }
@@ -693,6 +705,7 @@ MPP_RET test_mpp_run(MpiEncTestData *p)
MPP_RET ret = MPP_OK; MPP_RET ret = MPP_OK;
MppApi *mpi; MppApi *mpi;
MppCtx ctx; MppCtx ctx;
RK_U32 cap_num = 0;
if (NULL == p) if (NULL == p)
return MPP_ERR_NULL_PTR; return MPP_ERR_NULL_PTR;
@@ -728,12 +741,13 @@ MPP_RET test_mpp_run(MpiEncTestData *p)
mpp_packet_deinit(&packet); mpp_packet_deinit(&packet);
} }
while (!p->pkt_eos) { while (!p->pkt_eos) {
MppMeta meta = NULL; MppMeta meta = NULL;
MppFrame frame = NULL; MppFrame frame = NULL;
MppPacket packet = NULL; MppPacket packet = NULL;
void *buf = mpp_buffer_get_ptr(p->frm_buf); void *buf = mpp_buffer_get_ptr(p->frm_buf);
RK_S32 cam_frm_idx = -1;
MppBuffer cam_buf = NULL;
if (p->fp_input) { if (p->fp_input) {
ret = read_image(buf, p->fp_input, p->width, p->height, ret = read_image(buf, p->fp_input, p->width, p->height,
@@ -752,10 +766,24 @@ MPP_RET test_mpp_run(MpiEncTestData *p)
} else if (ret == MPP_ERR_VALUE) } else if (ret == MPP_ERR_VALUE)
goto RET; goto RET;
} else { } else {
ret = fill_image(buf, p->width, p->height, p->hor_stride, if (p->cam_ctx == NULL) {
p->ver_stride, p->fmt, p->frame_count); ret = fill_image(buf, p->width, p->height, p->hor_stride,
if (ret) p->ver_stride, p->fmt, p->frame_count);
goto RET; if (ret)
goto RET;
} else {
cam_frm_idx = camera_source_get_frame(p->cam_ctx);
mpp_assert(cam_frm_idx >= 0);
/* skip unstable frames */
if (cap_num++ < 50) {
camera_source_put_frame(p->cam_ctx, cam_frm_idx);
continue;
}
cam_buf = camera_frame_to_buf(p->cam_ctx, cam_frm_idx);
mpp_assert(cam_buf);
}
} }
ret = mpp_frame_init(&frame); ret = mpp_frame_init(&frame);
@@ -773,6 +801,8 @@ MPP_RET test_mpp_run(MpiEncTestData *p)
if (p->fp_input && feof(p->fp_input)) if (p->fp_input && feof(p->fp_input))
mpp_frame_set_buffer(frame, NULL); mpp_frame_set_buffer(frame, NULL);
else if (cam_buf)
mpp_frame_set_buffer(frame, cam_buf);
else else
mpp_frame_set_buffer(frame, p->frm_buf); mpp_frame_set_buffer(frame, p->frm_buf);
@@ -897,15 +927,18 @@ MPP_RET test_mpp_run(MpiEncTestData *p)
} }
} }
if (cam_frm_idx >= 0)
camera_source_put_frame(p->cam_ctx, cam_frm_idx);
if (p->num_frames > 0 && p->frame_count >= p->num_frames) { if (p->num_frames > 0 && p->frame_count >= p->num_frames) {
mpp_log("%p encode max %d frames", ctx, p->frame_count); mpp_log("%p encode max %d frames", ctx, p->frame_count);
break; break;
} }
if (p->frm_eos && p->pkt_eos) if (p->frm_eos && p->pkt_eos)
break; break;
} }
RET: RET:
return ret; return ret;
} }