diff --git a/test/mpi_dec_mt_test.c b/test/mpi_dec_mt_test.c index 16f861de..22f061a4 100644 --- a/test/mpi_dec_mt_test.c +++ b/test/mpi_dec_mt_test.c @@ -34,11 +34,11 @@ #include "mpi_dec_utils.h" typedef struct { + MpiDecTestCmd *cmd; MppCtx ctx; MppApi *mpi; volatile RK_U32 loop_end; - sem_t sem_end; /* buffer for stream data reading */ char *buf; @@ -51,7 +51,7 @@ typedef struct { FILE *fp_input; FILE *fp_output; - RK_U64 frame_count; + RK_S32 frame_count; RK_S32 frame_num; FileReader reader; @@ -86,7 +86,7 @@ void *thread_input(void *arg) pkt_eos = slot->eos; // setup eos flag if (pkt_eos) { - if (data->frame_num < 0) { + if (data->frame_num < 0 || data->frame_count < data->frame_num) { mpp_log_q(quiet, "%p loop again\n", ctx); reader_rewind(reader); pkt_eos = 0; @@ -106,7 +106,7 @@ void *thread_input(void *arg) // if failed wait a moment and retry msleep(5); } - } while (!pkt_done); + } while (!pkt_done && !data->loop_end); if (pkt_eos) break; @@ -120,6 +120,7 @@ void *thread_input(void *arg) void *thread_output(void *arg) { MpiDecMtLoopData *data = (MpiDecMtLoopData *)arg; + MpiDecTestCmd *cmd = data->cmd; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; MppFrame frame = NULL; @@ -143,7 +144,7 @@ void *thread_output(void *arg) } mpp_err("decode_get_frame failed too much time\n"); } - if (MPP_OK != ret) { + if (ret) { mpp_err("decode_get_frame failed ret %d\n", ret); continue; } @@ -197,58 +198,45 @@ void *thread_output(void *arg) break; } } else { - // found normal output frame - RK_U32 err_info = mpp_frame_get_errinfo(frame) | mpp_frame_get_discard(frame); - if (err_info) - mpp_log_q(quiet, "decoder_get_frame get err info:%d discard:%d.\n", - mpp_frame_get_errinfo(frame), mpp_frame_get_discard(frame)); + char log_buf[256]; + RK_S32 log_size = sizeof(log_buf) - 1; + RK_S32 log_len = 0; + RK_U32 err_info = mpp_frame_get_errinfo(frame); + RK_U32 discard = mpp_frame_get_discard(frame); + log_len += snprintf(log_buf + log_len, log_size - log_len, + "decode get frame %d", data->frame_count); + + if (mpp_frame_has_meta(frame)) { + MppMeta meta = mpp_frame_get_meta(frame); + RK_S32 temporal_id = 0; + + mpp_meta_get_s32(meta, KEY_TEMPORAL_ID, &temporal_id); + + log_len += snprintf(log_buf + log_len, log_size - log_len, + " tid %d", temporal_id); + } + + if (err_info || discard) { + log_len += snprintf(log_buf + log_len, log_size - log_len, + " err %x discard %x", err_info, discard); + } + mpp_log_q(quiet, "%p %s\n", ctx, log_buf); + + data->frame_count++; if (data->fp_output && !err_info) dump_mpp_frame_to_file(frame, data->fp_output); - // calculate fps here - static RK_S64 last_time = 0; - static RK_S64 last_count = 0; - - if (data->frame_count == 0) { - last_time = mpp_time(); - last_count = 0; - } else { - RK_S64 now = mpp_time(); - RK_S64 elapsed = now - last_time; - - // print on each second - if (elapsed >= 1000000) { - RK_S64 frames = data->frame_count - last_count; - float fps = (float)frames * 1000000 / elapsed; - - mpp_log("decoded %10lld frame %7.2f fps\n", - data->frame_count, fps); - - last_time = now; - last_count = data->frame_count; - } - } - data->frame_count++; + fps_calc_inc(cmd->fps); } frm_eos = mpp_frame_get_eos(frame); mpp_frame_deinit(&frame); frame = NULL; - if (frm_eos) { - mpp_log_q(quiet, "found last frame and quit\n"); - // when get a eos status mpp need a reset to restart decoding + if ((data->frame_num > 0 && (data->frame_count >= data->frame_num)) || + ((data->frame_num == 0) && frm_eos)) { data->loop_end = 1; - mpp_log_q(quiet, "found last frame and quit ok\n"); - sem_post(&data->sem_end); - break; - } - - if (data->frame_num > 0 && data->frame_count >= (RK_U32)data->frame_num) { - data->loop_end = 1; - mpp_log("%p reach max frame number %d\n", ctx, data->frame_count); - sem_post(&data->sem_end); break; } } @@ -262,7 +250,7 @@ void *thread_output(void *arg) int mt_dec_decode(MpiDecTestCmd *cmd) { MPP_RET ret = MPP_OK; - size_t file_size = 0; + FileReader reader = cmd->reader; // base flow context MppCtx ctx = NULL; @@ -281,7 +269,6 @@ int mt_dec_decode(MpiDecTestCmd *cmd) RK_U32 width = cmd->width; RK_U32 height = cmd->height; MppCodingType type = cmd->type; - FileReader reader = NULL; // resources size_t packet_size = MPI_DEC_STREAM_SIZE; @@ -294,20 +281,6 @@ int mt_dec_decode(MpiDecTestCmd *cmd) mpp_log("mpi_dec_test start\n"); memset(&data, 0, sizeof(data)); - if (cmd->have_input) { - data.fp_input = fopen(cmd->file_input, "rb"); - if (NULL == data.fp_input) { - mpp_err("failed to open input file %s\n", cmd->file_input); - goto MPP_TEST_OUT; - } - - fseek(data.fp_input, 0L, SEEK_END); - file_size = ftell(data.fp_input); - rewind(data.fp_input); - reader_init(&reader, cmd->file_input, data.fp_input); - mpp_log("input file size %ld\n", file_size); - } - if (cmd->have_output) { data.fp_output = fopen(cmd->file_output, "w+b"); if (NULL == data.fp_output) { @@ -326,8 +299,7 @@ int mt_dec_decode(MpiDecTestCmd *cmd) // decoder demo ret = mpp_create(&ctx, &mpi); - - if (MPP_OK != ret) { + if (ret) { mpp_err("mpp_create failed\n"); goto MPP_TEST_OUT; } @@ -336,7 +308,7 @@ int mt_dec_decode(MpiDecTestCmd *cmd) mpi_cmd = MPP_DEC_SET_PARSER_SPLIT_MODE; param = &need_split; ret = mpi->control(ctx, mpi_cmd, param); - if (MPP_OK != ret) { + if (ret) { mpp_err("mpi->control failed\n"); goto MPP_TEST_OUT; } @@ -348,18 +320,19 @@ int mt_dec_decode(MpiDecTestCmd *cmd) if (timeout) { param = &timeout; ret = mpi->control(ctx, MPP_SET_OUTPUT_TIMEOUT, param); - if (MPP_OK != ret) { + if (ret) { mpp_err("Failed to set output timeout %d ret %d\n", timeout, ret); goto MPP_TEST_OUT; } } ret = mpp_init(ctx, MPP_CTX_DEC, type); - if (MPP_OK != ret) { + if (ret) { mpp_err("mpp_init failed\n"); goto MPP_TEST_OUT; } + data.cmd = cmd; data.ctx = ctx; data.mpi = mpi; data.loop_end = 0; @@ -370,7 +343,6 @@ int mt_dec_decode(MpiDecTestCmd *cmd) data.frame_num = cmd->frame_num; data.reader = reader; data.quiet = cmd->quiet; - sem_init(&data.sem_end, 0, 0); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); @@ -395,24 +367,16 @@ int mt_dec_decode(MpiDecTestCmd *cmd) getc(stdin); data.loop_end = 1; - } else { - sem_wait(&data.sem_end); } - ret = mpi->reset(ctx); - if (MPP_OK != ret) - mpp_err("mpi->reset failed\n"); THREAD_END: pthread_attr_destroy(&attr); - mpp_log("all threads join start\n"); + pthread_join(thd_in, NULL); pthread_join(thd_out, NULL); - mpp_log("all threads join done\n"); - - sem_destroy(&data.sem_end); ret = mpi->reset(ctx); - if (MPP_OK != ret) { + if (ret) { mpp_err("mpi->reset failed\n"); goto MPP_TEST_OUT; } @@ -423,8 +387,6 @@ MPP_TEST_OUT: packet = NULL; } - reader_deinit(&reader); - if (frame) { mpp_frame_deinit(&frame); frame = NULL; @@ -464,19 +426,17 @@ int main(int argc, char **argv) cmd->timeout = -1; // parse the cmd option - ret = mpi_dec_test_parse_options(argc, argv, cmd); + ret = mpi_dec_test_cmd_init(cmd, argc, argv); if (ret) { if (ret < 0) { - mpp_err("mpi_dec_test_parse_options: input parameter invalid\n"); + mpp_err("mpi_dec_test_cmd_init: input parameter invalid\n"); } - mpi_dec_test_help(); - return ret; + mpi_dec_test_cmd_help(); + goto RET; } - mpi_dec_test_show_options(cmd); - - mpp_env_set_u32("mpi_debug", cmd->debug); + mpi_dec_test_cmd_options(cmd); ret = mt_dec_decode(cmd); if (MPP_OK == ret) @@ -484,7 +444,9 @@ int main(int argc, char **argv) else mpp_err("test failed ret %d\n", ret); - mpp_env_set_u32("mpi_debug", 0x0); +RET: + mpi_dec_test_cmd_deinit(cmd); + return ret; } diff --git a/test/mpi_dec_multi_test.c b/test/mpi_dec_multi_test.c index 9a3405fb..8ec06151 100644 --- a/test/mpi_dec_multi_test.c +++ b/test/mpi_dec_multi_test.c @@ -34,25 +34,22 @@ /* For each instance thread setup */ typedef struct { + MpiDecTestCmd *cmd; MppCtx ctx; MppApi *mpi; /* end of stream flag when set quit the loop */ - RK_U32 eos; - - /* buffer for stream data reading */ - char *buf; + RK_U32 loop_end; /* input and output */ MppBufferGroup frm_grp; - MppBufferGroup pkt_grp; MppPacket packet; - size_t packet_size; MppFrame frame; FILE *fp_input; FILE *fp_output; - RK_U32 frame_count; + RK_S32 packet_count; + RK_S32 frame_count; RK_S64 first_pkt; RK_S64 first_frm; @@ -61,38 +58,40 @@ typedef struct { /* runtime flag */ RK_U32 quiet; -} MpiDecCtx; +} MpiDecMultiCtx; /* For each instance thread return value */ typedef struct { - volatile float frame_rate; + float frame_rate; RK_S64 elapsed_time; - RK_S64 frame_count; -} MpiDecCtxRet; + RK_S32 frame_count; + RK_S64 delay; +} MpiDecMultiCtxRet; typedef struct { - MpiDecTestCmd *cmd; // pointer to global command line info + MpiDecTestCmd *cmd; // pointer to global command line info - pthread_t thd; // thread for for each instance - MpiDecCtx ctx; // context of decoder - MpiDecCtxRet ret; // return of decoder -} MpiDecCtxInfo; + pthread_t thd; // thread for for each instance + MpiDecMultiCtx ctx; // context of decoder + MpiDecMultiCtxRet ret; // return of decoder +} MpiDecMultiCtxInfo; -static int multi_dec_simple(MpiDecCtx *data) +static int multi_dec_simple(MpiDecMultiCtx *data) { + MpiDecTestCmd *cmd = data->cmd; RK_U32 pkt_done = 0; RK_U32 pkt_eos = 0; RK_U32 err_info = 0; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; - char *buf = data->buf; + char *buf = NULL; MppPacket packet = data->packet; MppFrame frame = NULL; FileReader reader = data->reader; FileBufSlot *slot = NULL; size_t read_size = 0; RK_U32 quiet = data->quiet; - MPP_RET ret = reader_read(reader, &slot); + MPP_RET ret = reader_index_read(reader, data->packet_count++, &slot); mpp_assert(ret == MPP_OK); mpp_assert(slot); @@ -100,15 +99,15 @@ static int multi_dec_simple(MpiDecCtx *data) pkt_eos = slot->eos; buf = slot->data; read_size = slot->size; - data->eos = pkt_eos; if (pkt_eos) { - if (data->frame_num < 0) { + if (data->frame_num < 0 || data->frame_num > data->frame_count) { mpp_log_q(quiet, "%p loop again\n", ctx); - reader_rewind(reader); - data->eos = pkt_eos = 0; + data->packet_count = 0; + pkt_eos = 0; } else { mpp_log_q(quiet, "%p found last packet\n", ctx); + data->loop_end = 1; } } @@ -122,6 +121,7 @@ static int multi_dec_simple(MpiDecCtx *data) mpp_packet_set_eos(packet); do { + RK_U32 frm_eos = 0; RK_S32 times = 5; // send the packet first if packet is not done if (!pkt_done) { @@ -136,7 +136,6 @@ static int multi_dec_simple(MpiDecCtx *data) // then get all available frame and release do { RK_S32 get_frm = 0; - RK_U32 frm_eos = 0; try_again: ret = mpi->decode_get_frame(ctx, &frame); @@ -148,7 +147,7 @@ static int multi_dec_simple(MpiDecCtx *data) } mpp_err("decode_get_frame failed too much time\n"); } - if (MPP_OK != ret) { + if (ret) { mpp_err("decode_get_frame failed ret %d\n", ret); break; } @@ -221,6 +220,8 @@ static int multi_dec_simple(MpiDecCtx *data) data->frame_count++; if (data->fp_output && !err_info) dump_mpp_frame_to_file(frame, data->fp_output); + + fps_calc_inc(cmd->fps); } frm_eos = mpp_frame_get_eos(frame); mpp_frame_deinit(&frame); @@ -234,14 +235,9 @@ static int multi_dec_simple(MpiDecCtx *data) continue; } - if (frm_eos) { - // mpp_log("found last frame\n"); + if ((data->frame_num > 0 && (data->frame_count >= data->frame_num)) || + ((data->frame_num == 0) && frm_eos)) break; - } - if (data->frame_num > 0 && data->frame_count >= (RK_U32)data->frame_num) { - data->eos = 1; - break; - } if (get_frm) continue; @@ -251,6 +247,12 @@ static int multi_dec_simple(MpiDecCtx *data) if (pkt_done) break; + if ((data->frame_num > 0 && (data->frame_count >= data->frame_num)) || + ((data->frame_num == 0) && frm_eos)) { + data->loop_end = 1; + break; + } + /* * why sleep here: * mpi->decode_put_packet will failed when packet in internal queue is @@ -264,30 +266,26 @@ static int multi_dec_simple(MpiDecCtx *data) return ret; } -static int multi_dec_advanced(MpiDecCtx *data) +static int multi_dec_advanced(MpiDecMultiCtx *data) { - RK_U32 pkt_eos = 0; MPP_RET ret = MPP_OK; + MpiDecTestCmd *cmd = data->cmd; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; - char *buf = data->buf; - MppPacket packet = data->packet; + MppPacket packet = NULL; MppFrame frame = data->frame; MppTask task = NULL; - size_t read_size = fread(buf, 1, data->packet_size, data->fp_input); + RK_U32 quiet = data->quiet; + FileBufSlot *slot = NULL; - if (read_size != data->packet_size || feof(data->fp_input)) { - // mpp_log("found last packet\n"); + ret = reader_index_read(cmd->reader, 0, &slot); + mpp_assert(ret == MPP_OK); + mpp_assert(slot); - // setup eos flag - data->eos = pkt_eos = 1; - } + mpp_packet_init_with_buffer(&packet, slot->buf); - // reset pos - mpp_packet_set_pos(packet, buf); - mpp_packet_set_length(packet, read_size); // setup eos flag - if (pkt_eos) + if (slot->eos) mpp_packet_set_eos(packet); ret = mpi->poll(ctx, MPP_PORT_INPUT, MPP_POLL_BLOCK); @@ -330,26 +328,28 @@ static int multi_dec_advanced(MpiDecCtx *data) if (task) { MppFrame frame_out = NULL; + mpp_task_meta_get_frame(task, KEY_OUTPUT_FRAME, &frame_out); - //mpp_assert(packet_out == packet); if (frame) { - /* write frame to file here */ - MppBuffer buf_out = mpp_frame_get_buffer(frame_out); + if (data->fp_output) + dump_mpp_frame_to_file(frame, data->fp_output); - if (buf_out) { - void *ptr = mpp_buffer_get_ptr(buf_out); - size_t len = mpp_buffer_get_size(buf_out); - - if (data->fp_output) - fwrite(ptr, 1, len, data->fp_output); - - mpp_log("decoded frame %d size %d\n", data->frame_count, len); - } + mpp_log_q(quiet, "%p decoded frame %d\n", ctx, data->frame_count); + data->frame_count++; if (mpp_frame_get_eos(frame_out)) { - ; //mpp_log("found eos frame\n"); + mpp_log_q(quiet, "%p found eos frame\n", ctx); } + fps_calc_inc(cmd->fps); + } + + if (data->frame_num > 0) { + if (data->frame_count >= data->frame_num) + data->loop_end = 1; + } else if (data->frame_num == 0) { + if (slot->eos) + data->loop_end = 1; } /* output queue */ @@ -358,17 +358,49 @@ static int multi_dec_advanced(MpiDecCtx *data) mpp_err("mpp task output enqueue failed\n"); } + /* + * The following input port task dequeue and enqueue is to make sure that + * the input packet can be released. We can directly deinit the input packet + * after frame output in most cases. + */ + if (0) { + mpp_packet_deinit(&packet); + } else { + ret = mpi->dequeue(ctx, MPP_PORT_INPUT, &task); /* input queue */ + if (ret) { + mpp_err("%p mpp task input dequeue failed\n", ctx); + return ret; + } + + mpp_assert(task); + if (task) { + MppPacket packet_out = NULL; + + mpp_task_meta_get_packet(task, KEY_INPUT_PACKET, &packet_out); + + if (!packet_out || packet_out != packet) + mpp_err_f("mismatch packet %p -> %p\n", packet, packet_out); + + mpp_packet_deinit(&packet_out); + + /* input empty task back to mpp to maintain task status */ + ret = mpi->enqueue(ctx, MPP_PORT_INPUT, task); + if (ret) + mpp_err("%p mpp task input enqueue failed\n", ctx); + } + } + return ret; } void* multi_dec_decode(void *cmd_ctx) { - MpiDecCtxInfo *info = (MpiDecCtxInfo *)cmd_ctx; + MpiDecMultiCtxInfo *info = (MpiDecMultiCtxInfo *)cmd_ctx; + MpiDecMultiCtx *dec_ctx = &info->ctx; + MpiDecMultiCtxRet *rets = &info->ret; MpiDecTestCmd *cmd = info->cmd; - MpiDecCtx *dec_ctx = &info->ctx; - MpiDecCtxRet *rets = &info->ret; + FileReader reader = cmd->reader; MPP_RET ret = MPP_OK; - size_t file_size = 0; // base flow context MppCtx ctx = NULL; @@ -389,26 +421,7 @@ void* multi_dec_decode(void *cmd_ctx) MppCodingType type = cmd->type; // resources - char *buf = NULL; - size_t packet_size = MPI_DEC_STREAM_SIZE; - MppBuffer pkt_buf = NULL; MppBuffer frm_buf = NULL; - FileReader reader = NULL; - - // mpp_log("mpi_dec_test start\n"); - - if (cmd->have_input) { - dec_ctx->fp_input = fopen(cmd->file_input, "rb"); - if (NULL == dec_ctx->fp_input) { - mpp_err("failed to open input file %s\n", cmd->file_input); - goto MPP_TEST_OUT; - } - - fseek(dec_ctx->fp_input, 0L, SEEK_END); - file_size = ftell(dec_ctx->fp_input); - rewind(dec_ctx->fp_input); - // mpp_log("input file size %ld\n", file_size); - } if (cmd->have_output) { dec_ctx->fp_output = fopen(cmd->file_output, "w+b"); @@ -419,8 +432,6 @@ void* multi_dec_decode(void *cmd_ctx) } if (cmd->simple) { - reader_init(&reader, cmd->file_input, dec_ctx->fp_input); - ret = mpp_packet_init(&packet, NULL, 0); if (ret) { mpp_err("mpp_packet_init failed\n"); @@ -436,14 +447,8 @@ void* multi_dec_decode(void *cmd_ctx) goto MPP_TEST_OUT; } - ret = mpp_buffer_group_get_internal(&dec_ctx->pkt_grp, MPP_BUFFER_TYPE_ION); - if (ret) { - mpp_err("failed to get buffer group for output packet ret %d\n", ret); - goto MPP_TEST_OUT; - } - ret = mpp_frame_init(&frame); /* output frame */ - if (MPP_OK != ret) { + if (ret) { mpp_err("mpp_frame_init failed\n"); goto MPP_TEST_OUT; } @@ -461,37 +466,24 @@ void* multi_dec_decode(void *cmd_ctx) goto MPP_TEST_OUT; } - // NOTE: for mjpeg decoding send the whole file - if (type == MPP_VIDEO_CodingMJPEG) { - packet_size = file_size; - } - - ret = mpp_buffer_get(dec_ctx->pkt_grp, &pkt_buf, packet_size); - if (ret) { - mpp_err("failed to get buffer for input frame ret %d\n", ret); - goto MPP_TEST_OUT; - } - mpp_packet_init_with_buffer(&packet, pkt_buf); - buf = mpp_buffer_get_ptr(pkt_buf); - mpp_frame_set_buffer(frame, frm_buf); } - mpp_log("mpi_dec_test decoder test start w %d h %d type %d\n", width, height, type); - // decoder demo ret = mpp_create(&ctx, &mpi); - - if (MPP_OK != ret) { + if (ret) { mpp_err("mpp_create failed\n"); goto MPP_TEST_OUT; } + mpp_log("%p mpi_dec_test decoder test start w %d h %d type %d\n", + ctx, width, height, type); + // NOTE: decoder split mode need to be set before init mpi_cmd = MPP_DEC_SET_PARSER_SPLIT_MODE; param = &need_split; ret = mpi->control(ctx, mpi_cmd, param); - if (MPP_OK != ret) { + if (ret) { mpp_err("mpi->control failed\n"); goto MPP_TEST_OUT; } @@ -503,25 +495,24 @@ void* multi_dec_decode(void *cmd_ctx) if (timeout) { param = &timeout; ret = mpi->control(ctx, MPP_SET_OUTPUT_TIMEOUT, param); - if (MPP_OK != ret) { + if (ret) { mpp_err("Failed to set output timeout %d ret %d\n", timeout, ret); goto MPP_TEST_OUT; } } ret = mpp_init(ctx, MPP_CTX_DEC, type); - if (MPP_OK != ret) { + if (ret) { mpp_err("mpp_init failed\n"); goto MPP_TEST_OUT; } + dec_ctx->cmd = cmd; dec_ctx->ctx = ctx; dec_ctx->mpi = mpi; - dec_ctx->eos = 0; - dec_ctx->buf = buf; dec_ctx->packet = packet; - dec_ctx->packet_size = packet_size; dec_ctx->frame = frame; + dec_ctx->packet_count = 0; dec_ctx->frame_count = 0; dec_ctx->frame_num = cmd->frame_num; dec_ctx->reader = reader; @@ -531,27 +522,25 @@ void* multi_dec_decode(void *cmd_ctx) t_s = mpp_time(); if (cmd->simple) { - while (!dec_ctx->eos) { + while (!dec_ctx->loop_end) multi_dec_simple(dec_ctx); - } } else { - while (!dec_ctx->eos) { + while (!dec_ctx->loop_end) multi_dec_advanced(dec_ctx); - } } t_e = mpp_time(); - rets->elapsed_time = t_e - t_s; - rets->frame_count = dec_ctx->frame_count; - rets->frame_rate = (float)dec_ctx->frame_count * 1000000 / rets->elapsed_time; - mpp_log("decode %d frame use time %lld ms frm rate %.2f\n", - dec_ctx->frame_count, rets->elapsed_time / 1000, rets->frame_rate); ret = mpi->reset(ctx); - if (MPP_OK != ret) { + if (ret) { mpp_err("mpi->reset failed\n"); goto MPP_TEST_OUT; } + rets->elapsed_time = t_e - t_s; + rets->frame_count = dec_ctx->frame_count; + rets->frame_rate = (float)dec_ctx->frame_count * 1000000 / rets->elapsed_time; + rets->delay = dec_ctx->first_frm - dec_ctx->first_pkt; + MPP_TEST_OUT: if (packet) { mpp_packet_deinit(&packet); @@ -568,25 +557,13 @@ MPP_TEST_OUT: ctx = NULL; } - if (cmd->simple) { - reader_deinit(&reader); - } else { - if (pkt_buf) { - mpp_buffer_put(pkt_buf); - pkt_buf = NULL; - } - + if (!cmd->simple) { if (frm_buf) { mpp_buffer_put(frm_buf); frm_buf = NULL; } } - if (dec_ctx->pkt_grp) { - mpp_buffer_group_put(dec_ctx->pkt_grp); - dec_ctx->pkt_grp = NULL; - } - if (dec_ctx->frm_grp) { mpp_buffer_group_put(dec_ctx->frm_grp); dec_ctx->frm_grp = NULL; @@ -610,7 +587,7 @@ int main(int argc, char **argv) RK_S32 ret = 0; MpiDecTestCmd cmd_ctx; MpiDecTestCmd* cmd = &cmd_ctx; - MpiDecCtxInfo *ctxs = NULL; + MpiDecMultiCtxInfo *ctxs = NULL; RK_S32 i = 0; float total_rate = 0.0; @@ -618,21 +595,21 @@ int main(int argc, char **argv) cmd->nthreads = 1; // parse the cmd option - ret = mpi_dec_test_parse_options(argc, argv, cmd); + ret = mpi_dec_test_cmd_init(cmd, argc, argv); if (ret) { if (ret < 0) { mpp_err("mpi_dec_multi_test_parse_options: input parameter invalid\n"); } - mpi_dec_test_help(); - return ret; + mpi_dec_test_cmd_help(); + goto RET; } - mpi_dec_test_show_options(cmd); + mpi_dec_test_cmd_options(cmd); cmd->simple = (cmd->type != MPP_VIDEO_CodingMJPEG) ? (1) : (0); - ctxs = mpp_calloc(MpiDecCtxInfo, cmd->nthreads); + ctxs = mpp_calloc(MpiDecMultiCtxInfo, cmd->nthreads); if (NULL == ctxs) { mpp_err("failed to alloc context for instances\n"); return -1; @@ -648,20 +625,37 @@ int main(int argc, char **argv) } } + if (cmd->frame_num < 0) { + // wait for input then quit decoding + mpp_log("*******************************************\n"); + mpp_log("**** Press Enter to stop loop decoding ****\n"); + mpp_log("*******************************************\n"); + + getc(stdin); + for (i = 0; i < cmd->nthreads; i++) + ctxs[i].ctx.loop_end = 1; + } + for (i = 0; i < cmd->nthreads; i++) pthread_join(ctxs[i].thd, NULL); for (i = 0; i < cmd->nthreads; i++) { - total_rate += ctxs[i].ret.frame_rate; - mpp_log("payload %d frame rate: %.2f first delay %d ms\n", i, - ctxs[i].ret.frame_rate, - (ctxs[i].ctx.first_frm - ctxs[i].ctx.first_pkt) / 1000); + MpiDecMultiCtxRet *dec_ret = &ctxs[i].ret; + + mpp_log("chn %2d decode %d frames time %lld ms delay %3d ms fps %3.2f\n", i, + dec_ret->frame_count, (RK_S64)(dec_ret->elapsed_time / 1000), + (RK_S32)(dec_ret->delay / 1000), dec_ret->frame_rate); + + total_rate += dec_ret->frame_rate; } mpp_free(ctxs); ctxs = NULL; total_rate /= cmd->nthreads; - mpp_log("average frame rate %d\n", (int)total_rate); + mpp_log("average frame rate %.2f\n", total_rate); + +RET: + mpi_dec_test_cmd_deinit(cmd); return (int)total_rate; } diff --git a/test/mpi_dec_test.c b/test/mpi_dec_test.c index 1a06b8eb..11569f07 100644 --- a/test/mpi_dec_test.c +++ b/test/mpi_dec_test.c @@ -31,30 +31,24 @@ #include "utils.h" typedef struct { + MpiDecTestCmd *cmd; MppCtx ctx; MppApi *mpi; RK_U32 quiet; /* end of stream flag when set quit the loop */ - RK_U32 eos; - - /* buffer for stream data reading */ - char *buf; + RK_U32 loop_end; /* input and output */ MppBufferGroup frm_grp; - MppBufferGroup pkt_grp; MppPacket packet; - size_t packet_size; MppFrame frame; FILE *fp_input; FILE *fp_output; - FILE *fp_config; RK_S32 frame_count; RK_S32 frame_num; size_t max_usage; - FileReader reader; } MpiDecLoopData; static int dec_simple(MpiDecLoopData *data) @@ -62,87 +56,43 @@ static int dec_simple(MpiDecLoopData *data) RK_U32 pkt_done = 0; RK_U32 pkt_eos = 0; MPP_RET ret = MPP_OK; + MpiDecTestCmd *cmd = data->cmd; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; - char *buf = data->buf; MppPacket packet = data->packet; MppFrame frame = NULL; - size_t read_size = 0; - size_t packet_size = data->packet_size; - FileReader reader = data->reader; + FileBufSlot *slot = NULL; RK_U32 quiet = data->quiet; - do { - if (data->fp_config) { - char line[MAX_FILE_NAME_LENGTH]; - char *ptr = NULL; + // when packet size is valid read the input binary file + ret = reader_read(cmd->reader, &slot); - do { - ptr = fgets(line, MAX_FILE_NAME_LENGTH, data->fp_config); - if (ptr) { - OpsLine info; - RK_S32 cnt = parse_config_line(line, &info); + mpp_assert(ret == MPP_OK); + mpp_assert(slot); - // parser for packet message - if (cnt >= 3 && 0 == strncmp("pkt", info.cmd, sizeof(info.cmd))) { - packet_size = info.value2; - break; - } + pkt_eos = slot->eos; - // parser for reset message at the end - if (0 == strncmp("rst", info.cmd, 3)) { - mpp_log("%p get reset cmd\n", ctx); - packet_size = 0; - break; - } - } else { - mpp_log("%p get end of cfg file\n", ctx); - packet_size = 0; - break; - } - } while (1); + if (pkt_eos) { + if (data->frame_num < 0 || data->frame_num > data->frame_count) { + mpp_log_q(quiet, "%p loop again\n", ctx); + reader_rewind(cmd->reader); + pkt_eos = 0; + } else { + mpp_log_q(quiet, "%p found last packet\n", ctx); + data->loop_end = 1; } + } - // when packet size is valid read the input binary file - if (packet_size) { - FileBufSlot *slot = NULL; - ret = reader_read(reader, &slot); - - mpp_assert(ret == MPP_OK); - mpp_assert(slot); - - pkt_eos = slot->eos; - buf = slot->data; - read_size = slot->size; - data->eos = pkt_eos; - - if (pkt_eos) { - if (data->frame_num < 0) { - mpp_log_q(quiet, "%p loop again\n", ctx); - reader_rewind(reader); - if (data->fp_config) { - clearerr(data->fp_config); - rewind(data->fp_config); - } - data->eos = pkt_eos = 0; - } else { - mpp_log_q(quiet, "%p found last packet\n", ctx); - break; - } - } - } - } while (!read_size); - - mpp_packet_set_data(packet, buf); - if (read_size > mpp_packet_get_size(packet)) - mpp_packet_set_size(packet, read_size); - mpp_packet_set_pos(packet, buf); - mpp_packet_set_length(packet, read_size); + mpp_packet_set_data(packet, slot->data); + mpp_packet_set_size(packet, slot->size); + mpp_packet_set_pos(packet, slot->data); + mpp_packet_set_length(packet, slot->size); // setup eos flag if (pkt_eos) mpp_packet_set_eos(packet); do { + RK_U32 frm_eos = 0; RK_S32 times = 5; // send the packet first if packet is not done if (!pkt_done) { @@ -154,7 +104,6 @@ static int dec_simple(MpiDecLoopData *data) // then get all available frame and release do { RK_S32 get_frm = 0; - RK_U32 frm_eos = 0; try_again: ret = mpi->decode_get_frame(ctx, &frame); @@ -311,6 +260,8 @@ static int dec_simple(MpiDecLoopData *data) data->frame_count++; if (data->fp_output && !err_info) dump_mpp_frame_to_file(frame, data->fp_output); + + fps_calc_inc(cmd->fps); } frm_eos = mpp_frame_get_eos(frame); mpp_frame_deinit(&frame); @@ -336,19 +287,18 @@ static int dec_simple(MpiDecLoopData *data) break; } - if (data->frame_num > 0 && data->frame_count >= data->frame_num) { - data->eos = 1; + if ((data->frame_num > 0 && (data->frame_count >= data->frame_num)) || + ((data->frame_num == 0) && frm_eos)) break; - } if (get_frm) continue; break; } while (1); - if (data->frame_num > 0 && data->frame_count >= data->frame_num) { - data->eos = 1; - mpp_log_q(quiet, "%p reach max frame number %d\n", ctx, data->frame_count); + if ((data->frame_num > 0 && (data->frame_count >= data->frame_num)) || + ((data->frame_num == 0) && frm_eos)) { + data->loop_end = 1; break; } @@ -370,33 +320,24 @@ static int dec_simple(MpiDecLoopData *data) static int dec_advanced(MpiDecLoopData *data) { - RK_U32 pkt_eos = 0; MPP_RET ret = MPP_OK; + MpiDecTestCmd *cmd = data->cmd; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; - char *buf = data->buf; - MppPacket packet = data->packet; + MppPacket packet = NULL; MppFrame frame = data->frame; MppTask task = NULL; - size_t read_size = fread(buf, 1, data->packet_size, data->fp_input); RK_U32 quiet = data->quiet; + FileBufSlot *slot = NULL; - if (read_size != data->packet_size || feof(data->fp_input)) { - if (data->frame_num < 0) { - clearerr(data->fp_input); - rewind(data->fp_input); - } else { - mpp_log_q(quiet, "%p found last packet\n", ctx); - // setup eos flag - data->eos = pkt_eos = 1; - } - } + ret = reader_index_read(cmd->reader, 0, &slot); + mpp_assert(ret == MPP_OK); + mpp_assert(slot); + + mpp_packet_init_with_buffer(&packet, slot->buf); - // reset pos - mpp_packet_set_pos(packet, buf); - mpp_packet_set_length(packet, read_size); // setup eos flag - if (pkt_eos) + if (slot->eos) mpp_packet_set_eos(packet); ret = mpi->poll(ctx, MPP_PORT_INPUT, MPP_POLL_BLOCK); @@ -439,8 +380,8 @@ static int dec_advanced(MpiDecLoopData *data) if (task) { MppFrame frame_out = NULL; + mpp_task_meta_get_frame(task, KEY_OUTPUT_FRAME, &frame_out); - //mpp_assert(packet_out == packet); if (frame) { /* write frame to file here */ @@ -453,14 +394,15 @@ static int dec_advanced(MpiDecLoopData *data) if (mpp_frame_get_eos(frame_out)) { mpp_log_q(quiet, "%p found eos frame\n", ctx); } + fps_calc_inc(cmd->fps); } - if (data->frame_num > 0 && data->frame_count >= data->frame_num) { - data->eos = 1; - } else { - data->eos = 0; - clearerr(data->fp_input); - rewind(data->fp_input); + if (data->frame_num > 0) { + if (data->frame_count >= data->frame_num) + data->loop_end = 1; + } else if (data->frame_num == 0) { + if (slot->eos) + data->loop_end = 1; } /* output queue */ @@ -469,14 +411,70 @@ static int dec_advanced(MpiDecLoopData *data) mpp_err("%p mpp task output enqueue failed\n", ctx); } + /* + * The following input port task dequeue and enqueue is to make sure that + * the input packet can be released. We can directly deinit the input packet + * after frame output in most cases. + */ + if (0) { + mpp_packet_deinit(&packet); + } else { + ret = mpi->dequeue(ctx, MPP_PORT_INPUT, &task); /* input queue */ + if (ret) { + mpp_err("%p mpp task input dequeue failed\n", ctx); + return ret; + } + + mpp_assert(task); + if (task) { + MppPacket packet_out = NULL; + + mpp_task_meta_get_packet(task, KEY_INPUT_PACKET, &packet_out); + + if (!packet_out || packet_out != packet) + mpp_err_f("mismatch packet %p -> %p\n", packet, packet_out); + + mpp_packet_deinit(&packet_out); + + /* input empty task back to mpp to maintain task status */ + ret = mpi->enqueue(ctx, MPP_PORT_INPUT, task); + if (ret) + mpp_err("%p mpp task input enqueue failed\n", ctx); + } + } + return ret; } +void *thread_decode(void *arg) +{ + MpiDecLoopData *data = (MpiDecLoopData *)arg; + MpiDecTestCmd *cmd = data->cmd; + MppCtx ctx = data->ctx; + MppApi *mpi = data->mpi; + + if (cmd->simple) { + while (!data->loop_end) + dec_simple(data); + } else { + /* NOTE: change output format before jpeg decoding */ + if (MPP_FRAME_FMT_IS_YUV(cmd->format) || MPP_FRAME_FMT_IS_RGB(cmd->format)) { + MPP_RET ret = mpi->control(ctx, MPP_DEC_SET_OUTPUT_FORMAT, &cmd->format); + if (ret) { + mpp_err("Failed to set output format %d\n", cmd->format); + return NULL; + } + } + + while (!data->loop_end) + dec_advanced(data); + } + + return NULL; +} + int dec_decode(MpiDecTestCmd *cmd) { - MPP_RET ret = MPP_OK; - size_t file_size = 0; - // base flow context MppCtx ctx = NULL; MppApi *mpi = NULL; @@ -499,28 +497,17 @@ int dec_decode(MpiDecTestCmd *cmd) MppDecCfg cfg = NULL; // resources - char *buf = NULL; - size_t packet_size = cmd->pkt_size; - MppBuffer pkt_buf = NULL; MppBuffer frm_buf = NULL; - FileReader reader = NULL; + pthread_t thd; + pthread_attr_t attr; MpiDecLoopData data; + MPP_RET ret = MPP_OK; mpp_log("mpi_dec_test start\n"); memset(&data, 0, sizeof(data)); + pthread_attr_init(&attr); - if (cmd->have_input) { - data.fp_input = fopen(cmd->file_input, "rb"); - if (NULL == data.fp_input) { - mpp_err("failed to open input file %s\n", cmd->file_input); - goto MPP_TEST_OUT; - } - - fseek(data.fp_input, 0L, SEEK_END); - file_size = ftell(data.fp_input); - rewind(data.fp_input); - mpp_log("input file size %ld\n", file_size); - } + cmd->simple = (cmd->type != MPP_VIDEO_CodingMJPEG) ? (1) : (0); if (cmd->have_output) { data.fp_output = fopen(cmd->file_output, "w+b"); @@ -530,16 +517,7 @@ int dec_decode(MpiDecTestCmd *cmd) } } - if (cmd->have_config) { - data.fp_config = fopen(cmd->file_config, "r"); - if (NULL == data.fp_config) { - mpp_err("failed to open config file %s\n", cmd->file_config); - goto MPP_TEST_OUT; - } - } - if (cmd->simple) { - reader_init(&reader, cmd->file_input, data.fp_input); ret = mpp_packet_init(&packet, NULL, 0); if (ret) { mpp_err("mpp_packet_init failed\n"); @@ -555,12 +533,6 @@ int dec_decode(MpiDecTestCmd *cmd) goto MPP_TEST_OUT; } - ret = mpp_buffer_group_get_internal(&data.pkt_grp, MPP_BUFFER_TYPE_ION); - if (ret) { - mpp_err("failed to get buffer group for output packet ret %d\n", ret); - goto MPP_TEST_OUT; - } - ret = mpp_frame_init(&frame); /* output frame */ if (ret) { mpp_err("mpp_frame_init failed\n"); @@ -580,25 +552,11 @@ int dec_decode(MpiDecTestCmd *cmd) goto MPP_TEST_OUT; } - // NOTE: for mjpeg decoding send the whole file - if (type == MPP_VIDEO_CodingMJPEG) { - packet_size = file_size; - } - - ret = mpp_buffer_get(data.pkt_grp, &pkt_buf, packet_size); - if (ret) { - mpp_err("failed to get buffer for input frame ret %d\n", ret); - goto MPP_TEST_OUT; - } - mpp_packet_init_with_buffer(&packet, pkt_buf); - buf = mpp_buffer_get_ptr(pkt_buf); - mpp_frame_set_buffer(frame, frm_buf); } // decoder demo ret = mpp_create(&ctx, &mpi); - if (ret) { mpp_err("mpp_create failed\n"); goto MPP_TEST_OUT; @@ -611,7 +569,7 @@ int dec_decode(MpiDecTestCmd *cmd) mpi_cmd = MPP_DEC_SET_PARSER_SPLIT_MODE; param = &need_split; ret = mpi->control(ctx, mpi_cmd, param); - if (MPP_OK != ret) { + if (ret) { mpp_err("%p mpi->control failed\n", ctx); goto MPP_TEST_OUT; } @@ -661,37 +619,36 @@ int dec_decode(MpiDecTestCmd *cmd) goto MPP_TEST_OUT; } + data.cmd = cmd; data.ctx = ctx; data.mpi = mpi; - data.eos = 0; - data.buf = buf; + data.loop_end = 0; data.packet = packet; - data.packet_size = packet_size; data.frame = frame; data.frame_count = 0; data.frame_num = cmd->frame_num; - data.reader = reader; data.quiet = cmd->quiet; - if (cmd->simple) { - while (!data.eos) { - dec_simple(&data); - } - } else { - /* NOTE: change output format before jpeg decoding */ - if (MPP_FRAME_FMT_IS_YUV(cmd->format) || MPP_FRAME_FMT_IS_RGB(cmd->format)) { - ret = mpi->control(ctx, MPP_DEC_SET_OUTPUT_FORMAT, &cmd->format); - if (ret) { - mpp_err("Failed to set output format %d\n", cmd->format); - goto MPP_TEST_OUT; - } - } + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - while (!data.eos) { - dec_advanced(&data); - } + ret = pthread_create(&thd, &attr, thread_decode, &data); + if (ret) { + mpp_err("failed to create thread for input ret %d\n", ret); + goto MPP_TEST_OUT; } + if (cmd->frame_num < 0) { + // wait for input then quit decoding + mpp_log("*******************************************\n"); + mpp_log("**** Press Enter to stop loop decoding ****\n"); + mpp_log("*******************************************\n"); + + getc(stdin); + data.loop_end = 1; + } + + pthread_join(thd, NULL); + cmd->max_usage = data.max_usage; { MppDecQueryCfg query; @@ -735,25 +692,13 @@ MPP_TEST_OUT: ctx = NULL; } - if (cmd->simple) { - reader_deinit(&reader); - } else { - if (pkt_buf) { - mpp_buffer_put(pkt_buf); - pkt_buf = NULL; - } - + if (!cmd->simple) { if (frm_buf) { mpp_buffer_put(frm_buf); frm_buf = NULL; } } - if (data.pkt_grp) { - mpp_buffer_group_put(data.pkt_grp); - data.pkt_grp = NULL; - } - if (data.frm_grp) { mpp_buffer_group_put(data.frm_grp); data.frm_grp = NULL; @@ -764,16 +709,13 @@ MPP_TEST_OUT: data.fp_output = NULL; } - if (data.fp_input) { - fclose(data.fp_input); - data.fp_input = NULL; - } - if (cfg) { mpp_dec_cfg_deinit(cfg); cfg = NULL; } + pthread_attr_destroy(&attr); + return ret; } @@ -788,21 +730,17 @@ int main(int argc, char **argv) cmd->pkt_size = MPI_DEC_STREAM_SIZE; // parse the cmd option - ret = mpi_dec_test_parse_options(argc, argv, cmd); + ret = mpi_dec_test_cmd_init(cmd, argc, argv); if (ret) { if (ret < 0) { - mpp_err("mpi_dec_test_parse_options: input parameter invalid\n"); + mpp_err("mpi_dec_test_cmd_init: input parameter invalid\n"); } - mpi_dec_test_help(); - return ret; + mpi_dec_test_cmd_help(); + goto RET; } - mpi_dec_test_show_options(cmd); - - mpp_env_set_u32("mpi_debug", cmd->debug); - - cmd->simple = (cmd->type != MPP_VIDEO_CodingMJPEG) ? (1) : (0); + mpi_dec_test_cmd_options(cmd); ret = dec_decode(cmd); if (MPP_OK == ret) @@ -810,7 +748,9 @@ int main(int argc, char **argv) else mpp_err("test failed ret %d\n", ret); - mpp_env_set_u32("mpi_debug", 0x0); +RET: + mpi_dec_test_cmd_deinit(cmd); + return ret; } diff --git a/utils/mpi_dec_utils.c b/utils/mpi_dec_utils.c index b09c0a9b..1fa14719 100644 --- a/utils/mpi_dec_utils.c +++ b/utils/mpi_dec_utils.c @@ -59,6 +59,7 @@ typedef struct FileReader_t { /* return value for each read */ size_t read_total; size_t read_size; + MppBufferGroup group; pthread_t thd; volatile RK_U32 thd_stop; @@ -79,10 +80,10 @@ OptionInfo mpi_dec_cmd[] = { {"h", "height", "the height of input bitstream"}, {"t", "type", "input stream coding type"}, {"f", "format", "output frame format type"}, - {"d", "debug", "debug flag"}, {"x", "timeout", "output timeout interval"}, {"n", "frame_number", "max output frame number"}, {"s", "instance_nb", "number of instances"}, + {"v", "trace", "q - quiet f - show fps"}, {NULL}, }; @@ -137,7 +138,7 @@ static RK_U32 read_jpeg_file(FileReader data) MppBuffer hw_buf = NULL; FileBufSlot *slot = mpp_calloc(FileBufSlot, 1); - mpp_buffer_get(NULL, &hw_buf, impl->file_size); + mpp_buffer_get(impl->group, &hw_buf, impl->file_size); mpp_assert(hw_buf); slot->data = mpp_buffer_get_ptr(hw_buf); @@ -149,7 +150,7 @@ static RK_U32 read_jpeg_file(FileReader data) impl->read_total += read_size; impl->read_size = read_size; - slot->buf = hw_buf; + slot->buf = hw_buf; slot->size = read_size; slot->eos = 1; slot->index = impl->slot_cnt; @@ -212,6 +213,8 @@ static void check_file_type(FileReader data, char *file_in) impl->seek_base = 0; impl->read_func = read_jpeg_file; impl->slot_max = 1; + mpp_buffer_group_get_internal(&impl->group, MPP_BUFFER_TYPE_ION); + mpp_assert(impl->group); } else { RK_U32 buf_size = 0; @@ -229,6 +232,17 @@ static void check_file_type(FileReader data, char *file_in) } } +size_t reader_size(FileReader reader) +{ + FileReaderImpl *impl = (FileReaderImpl*)reader; + size_t size = 0; + + if (impl) + size = impl->file_size; + + return size; +} + MPP_RET reader_read(FileReader reader, FileBufSlot **buf) { FileReaderImpl *impl = (FileReaderImpl*)reader; @@ -293,13 +307,19 @@ void reader_rewind(FileReader reader) impl->slot_rd_idx = 0; } -void reader_init(FileReader* reader, char* file_in, FILE* fp_in) +void reader_init(FileReader* reader, char* file_in) { - FileReaderImpl *impl = mpp_calloc(FileReaderImpl, 1); FILE *fp_input = fopen(file_in, "rb"); - (void) fp_in; + FileReaderImpl *impl = NULL; - mpp_assert(fp_input); + if (!fp_input) { + mpp_err("failed to open input file %s\n", file_in); + *reader = NULL; + return; + } + + impl = mpp_calloc(FileReaderImpl, 1); + mpp_assert(impl); impl->fp_input = fp_input; fseek(fp_input, 0L, SEEK_END); @@ -315,9 +335,9 @@ void reader_init(FileReader* reader, char* file_in, FILE* fp_in) *reader = impl; } -void reader_deinit(FileReader *reader) +void reader_deinit(FileReader reader) { - FileReaderImpl *impl = (FileReaderImpl*)(*reader); + FileReaderImpl *impl = (FileReaderImpl*)(reader); RK_U32 i; mpp_assert(impl); @@ -334,15 +354,19 @@ void reader_deinit(FileReader *reader) continue; if (slot->buf) { - mpp_buffer_put(&slot->buf); + mpp_buffer_put(slot->buf); slot->buf = NULL; } MPP_FREE(impl->slots[i]); } + if (impl->group) { + mpp_buffer_group_put(impl->group); + impl->group = NULL; + } + MPP_FREE(impl->slots); MPP_FREE(impl); - *reader = NULL; } static void* reader_worker(void *param) @@ -392,14 +416,14 @@ void show_dec_fps(RK_S64 total_time, RK_S64 total_count, RK_S64 last_time, RK_S6 total_count, avg_fps, ins_fps); } -void mpi_dec_test_help() +void mpi_dec_test_cmd_help() { mpp_log("usage: mpi_dec_test [options]\n"); show_options(mpi_dec_cmd); mpp_show_support_format(); } -RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd) +RK_S32 mpi_dec_test_cmd_init(MpiDecTestCmd* cmd, int argc, char **argv) { const char *opt; const char *next; @@ -449,26 +473,6 @@ RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd) goto PARSE_OPINIONS_OUT; } } break; - case 'c' : { - if (next) { - strncpy(cmd->file_config, next, MAX_FILE_NAME_LENGTH - 1); - cmd->have_config = 1; - - // enlarge packet buffer size for large input stream case - cmd->pkt_size = SZ_1M; - } else { - mpp_log("output file is invalid\n"); - goto PARSE_OPINIONS_OUT; - } - } break; - case 'd' : { - if (next) { - cmd->debug = atoi(next);; - } else { - mpp_err("invalid debug flag\n"); - goto PARSE_OPINIONS_OUT; - } - } break; case 'w' : { if (next) { cmd->width = atoi(next); @@ -479,7 +483,7 @@ RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd) } break; case 'h' : { if ((*(opt + 1) != '\0') && !strncmp(opt, "help", 4)) { - mpi_dec_test_help(); + mpi_dec_test_cmd_help(); err = 1; goto PARSE_OPINIONS_OUT; } else if (next) { @@ -544,9 +548,13 @@ RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd) goto PARSE_OPINIONS_OUT; } } break; - case 'q' : { - cmd->quiet = 1; - optindex--; + case 'v' : { + if (next) { + if (strstr(next, "q")) + cmd->quiet = 1; + if (strstr(next, "f")) + cmd->trace_fps = 1; + } } break; default : { mpp_err("skip invalid opt %c\n", *opt); @@ -560,18 +568,45 @@ RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd) err = 0; PARSE_OPINIONS_OUT: + if (cmd->have_input) { + reader_init(&cmd->reader, cmd->file_input); + if (cmd->reader) + mpp_log("input file %s size %ld\n", cmd->file_input, reader_size(cmd->reader)); + } + if (cmd->trace_fps) { + fps_calc_init(&cmd->fps); + mpp_assert(cmd->fps); + fps_calc_set_cb(cmd->fps, show_dec_fps); + } + return err; } -void mpi_dec_test_show_options(MpiDecTestCmd* cmd) +RK_S32 mpi_dec_test_cmd_deinit(MpiDecTestCmd* cmd) +{ + if (!cmd) + return 0; + + if (cmd->reader) { + reader_deinit(cmd->reader); + cmd->reader = NULL; + } + + if (cmd->fps) { + fps_calc_deinit(cmd->fps); + cmd->fps = NULL; + } + + return 0; +} + +void mpi_dec_test_cmd_options(MpiDecTestCmd* cmd) { mpp_log("cmd parse result:\n"); mpp_log("input file name: %s\n", cmd->file_input); mpp_log("output file name: %s\n", cmd->file_output); - mpp_log("config file name: %s\n", cmd->file_config); mpp_log("width : %4d\n", cmd->width); mpp_log("height : %4d\n", cmd->height); - mpp_log("type : %d\n", cmd->type); - mpp_log("debug flag : %x\n", cmd->debug); - mpp_log("max frames : %d\n", cmd->frame_num); + mpp_log("type : %4d\n", cmd->type); + mpp_log("max frames : %4d\n", cmd->frame_num); } diff --git a/utils/mpi_dec_utils.h b/utils/mpi_dec_utils.h index b304cfc1..9fe6de5f 100644 --- a/utils/mpi_dec_utils.h +++ b/utils/mpi_dec_utils.h @@ -38,17 +38,14 @@ typedef struct FileBufSlot_t { typedef struct MpiDecTestCmd_t { char file_input[MAX_FILE_NAME_LENGTH]; char file_output[MAX_FILE_NAME_LENGTH]; - char file_config[MAX_FILE_NAME_LENGTH]; + MppCodingType type; MppFrameFormat format; RK_U32 width; RK_U32 height; - RK_U32 debug; - RK_U32 quiet; RK_U32 have_input; RK_U32 have_output; - RK_U32 have_config; RK_U32 simple; RK_S32 timeout; @@ -58,21 +55,32 @@ typedef struct MpiDecTestCmd_t { RK_S32 nthreads; // report information size_t max_usage; + + /* data for share */ + FileReader reader; + FpsCalc fps; + + /* runtime log flag */ + RK_U32 debug; + RK_U32 quiet; + RK_U32 trace_fps; } MpiDecTestCmd; extern OptionInfo mpi_dec_cmd[]; -RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd); -void mpi_dec_test_show_options(MpiDecTestCmd* cmd); -void mpi_dec_test_help(); +RK_S32 mpi_dec_test_cmd_init(MpiDecTestCmd* cmd, int argc, char **argv); +RK_S32 mpi_dec_test_cmd_deinit(MpiDecTestCmd* cmd); +void mpi_dec_test_cmd_options(MpiDecTestCmd* cmd); +void mpi_dec_test_cmd_help(); -void reader_init(FileReader* reader, char* file_in, FILE* fp_in); -void reader_deinit(FileReader *reader); +void reader_init(FileReader* reader, char* file_in); +void reader_deinit(FileReader reader); void reader_start(FileReader reader); void reader_sync(FileReader reader); void reader_stop(FileReader reader); +size_t reader_size(FileReader reader); MPP_RET reader_read(FileReader reader, FileBufSlot **buf); MPP_RET reader_index_read(FileReader reader, RK_S32 index, FileBufSlot **buf); void reader_rewind(FileReader reader); diff --git a/utils/utils.c b/utils/utils.c index b38a461b..b47c728b 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -1272,7 +1272,8 @@ MPP_RET fps_calc_set_cb(FpsCalc ctx, FpsCalcCb cb) { FpsCalcImpl *impl = (FpsCalcImpl *)ctx; - impl->callback = cb; + if (impl) + impl->callback = cb; return MPP_OK; } @@ -1285,6 +1286,9 @@ MPP_RET fps_calc_inc(FpsCalc ctx) RK_S64 last_time = 0; RK_S64 last_count = 0; + if (NULL == impl) + return MPP_OK; + mpp_spinlock_lock(&impl->lock); { RK_S64 now = mpp_time();