diff --git a/test/mpi_dec_mt_test.c b/test/mpi_dec_mt_test.c index 28ab5c6f..16f861de 100644 --- a/test/mpi_dec_mt_test.c +++ b/test/mpi_dec_mt_test.c @@ -38,6 +38,7 @@ typedef struct { MppApi *mpi; volatile RK_U32 loop_end; + sem_t sem_end; /* buffer for stream data reading */ char *buf; @@ -63,47 +64,55 @@ void *thread_input(void *arg) MpiDecMtLoopData *data = (MpiDecMtLoopData *)arg; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; - char *buf = data->buf; MppPacket packet = data->packet; FileReader reader = data->reader; + RK_U32 quiet = data->quiet; - mpp_log("put packet thread start\n"); + mpp_log_q(quiet, "put packet thread start\n"); do { RK_U32 pkt_done = 0; - RK_U32 pkt_eos = 0; - size_t read_size = 0; + RK_U32 pkt_eos = 0; + FileBufSlot *slot = NULL; + MPP_RET ret = reader_read(reader, &slot); + if (ret) + break; - pkt_eos = reader_read(reader, &buf, &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); - if (pkt_eos) - reader_rewind(reader); - - 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); + pkt_eos = slot->eos; // setup eos flag if (pkt_eos) { - mpp_packet_set_eos(packet); - // mpp_log("found last packet\n"); - } else - mpp_packet_clr_eos(packet); + if (data->frame_num < 0) { + mpp_log_q(quiet, "%p loop again\n", ctx); + reader_rewind(reader); + pkt_eos = 0; + } else { + mpp_log_q(quiet, "%p found last packet\n", ctx); + mpp_packet_set_eos(packet); + } + } // send packet until it success do { - MPP_RET ret = mpi->decode_put_packet(ctx, packet); - if (MPP_OK == ret) + ret = mpi->decode_put_packet(ctx, packet); + if (MPP_OK == ret) { pkt_done = 1; - else { + mpp_assert(0 == mpp_packet_get_length(packet)); + } else { // if failed wait a moment and retry msleep(5); } } while (!pkt_done); + + if (pkt_eos) + break; } while (!data->loop_end); - mpp_log("put packet thread end\n"); + mpp_log_q(quiet, "put packet thread end\n"); return NULL; } @@ -116,10 +125,11 @@ void *thread_output(void *arg) MppFrame frame = NULL; RK_U32 quiet = data->quiet; - mpp_log("get frame thread start\n"); + mpp_log_q(quiet, "get frame thread start\n"); // then get all available frame and release do { + RK_U32 frm_eos = 0; RK_S32 times = 5; MPP_RET ret = MPP_OK; @@ -222,25 +232,29 @@ void *thread_output(void *arg) data->frame_count++; } - if (mpp_frame_get_eos(frame)) { - mpp_log_q(quiet, "found last frame loop again\n"); - // when get a eos status mpp need a reset to restart decoding - ret = mpi->reset(ctx); - if (MPP_OK != ret) - mpp_err("mpi->reset failed\n"); - } - + 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 + 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; } } } while (!data->loop_end); - mpp_log("get frame thread end\n"); + mpp_log_q(quiet, "get frame thread end\n"); return NULL; } @@ -356,6 +370,7 @@ 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); @@ -371,22 +386,30 @@ int mt_dec_decode(MpiDecTestCmd *cmd) mpp_err("failed to create thread for output ret %d\n", ret); goto THREAD_END; } - // 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; + 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; + } 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 end\n"); + 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) { diff --git a/test/mpi_dec_multi_test.c b/test/mpi_dec_multi_test.c index 4bc1b1c2..9a3405fb 100644 --- a/test/mpi_dec_multi_test.c +++ b/test/mpi_dec_multi_test.c @@ -83,17 +83,25 @@ static int multi_dec_simple(MpiDecCtx *data) RK_U32 pkt_done = 0; RK_U32 pkt_eos = 0; RK_U32 err_info = 0; - MPP_RET ret = MPP_OK; MppCtx ctx = data->ctx; MppApi *mpi = data->mpi; char *buf = data->buf; 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_assert(ret == MPP_OK); + mpp_assert(slot); + + pkt_eos = slot->eos; + buf = slot->data; + read_size = slot->size; + data->eos = pkt_eos; - data->eos = pkt_eos = reader_read(reader, &buf, &read_size); if (pkt_eos) { if (data->frame_num < 0) { mpp_log_q(quiet, "%p loop again\n", ctx); diff --git a/test/mpi_dec_test.c b/test/mpi_dec_test.c index 90a4aebc..1a06b8eb 100644 --- a/test/mpi_dec_test.c +++ b/test/mpi_dec_test.c @@ -105,8 +105,16 @@ static int dec_simple(MpiDecLoopData *data) // when packet size is valid read the input binary file if (packet_size) { + FileBufSlot *slot = NULL; + ret = reader_read(reader, &slot); - data->eos = pkt_eos = reader_read(reader, &buf, &read_size); + 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) { diff --git a/utils/mpi_dec_utils.c b/utils/mpi_dec_utils.c index 1dc1c59f..b09c0a9b 100644 --- a/utils/mpi_dec_utils.c +++ b/utils/mpi_dec_utils.c @@ -45,13 +45,6 @@ typedef enum { typedef RK_U32 (*ReaderFunc)(FileReader data); -typedef struct FileBufSlot_t { - MppBuffer hw_buf; - size_t size; - RK_U32 eos; - char *buf; -} FileBufSlot; - typedef struct FileReader_t { FILE *fp_input; size_t file_size; @@ -115,8 +108,8 @@ static RK_U32 read_ivf_file(FileReader data) data_size = ivf_data[0] | (ivf_data[1] << 8) | (ivf_data[2] << 16) | (ivf_data[3] << 24); slot = mpp_malloc_size(FileBufSlot, MPP_ALIGN(sizeof(FileBufSlot) + data_size, SZ_4K)); - slot->buf = (char *)(slot + 1); - read_size = fread(slot->buf, 1, data_size, fp); + slot->data = (char *)(slot + 1); + read_size = fread(slot->data, 1, data_size, fp); impl->read_total += read_size; impl->read_size = read_size; @@ -124,9 +117,10 @@ static RK_U32 read_ivf_file(FileReader data) if (slot->size != data_size || feof(fp) || impl->read_total >= impl->file_size) eos = 1; - slot->hw_buf = NULL; + slot->buf = NULL; slot->size = read_size; slot->eos = eos; + slot->index = impl->slot_cnt; mpp_assert(impl->slot_cnt < impl->slot_max); impl->slots[impl->slot_cnt] = slot; @@ -146,18 +140,19 @@ static RK_U32 read_jpeg_file(FileReader data) mpp_buffer_get(NULL, &hw_buf, impl->file_size); mpp_assert(hw_buf); - slot->buf = mpp_buffer_get_ptr(hw_buf); - mpp_assert(slot->buf); - read_size = fread(slot->buf, 1, buf_size, fp); + slot->data = mpp_buffer_get_ptr(hw_buf); + mpp_assert(slot->data); + read_size = fread(slot->data, 1, buf_size, fp); mpp_assert(read_size == buf_size); impl->read_total += read_size; impl->read_size = read_size; - slot->hw_buf = hw_buf; + slot->buf = hw_buf; slot->size = read_size; slot->eos = 1; + slot->index = impl->slot_cnt; mpp_assert(impl->slot_cnt < impl->slot_max); impl->slots[impl->slot_cnt] = slot; @@ -175,8 +170,8 @@ static RK_U32 read_normal_file(FileReader data) FileBufSlot *slot = mpp_malloc_size(FileBufSlot, size); RK_U32 eos = 0; - slot->buf = (char *)(slot + 1); - read_size = fread(slot->buf, 1, buf_size, fp); + slot->data = (char *)(slot + 1); + read_size = fread(slot->data, 1, buf_size, fp); impl->read_total += read_size; impl->read_size = read_size; @@ -184,9 +179,10 @@ static RK_U32 read_normal_file(FileReader data) if (read_size != buf_size || feof(fp) || impl->read_total >= impl->file_size) eos = 1; - slot->hw_buf = NULL; + slot->buf = NULL; slot->size = read_size; slot->eos = eos; + slot->index = impl->slot_cnt; mpp_assert(impl->slot_cnt < impl->slot_max); impl->slots[impl->slot_cnt] = slot; @@ -233,51 +229,61 @@ static void check_file_type(FileReader data, char *file_in) } } -RK_U32 reader_read(FileReader reader, char** buf, size_t *size) +MPP_RET reader_read(FileReader reader, FileBufSlot **buf) { FileReaderImpl *impl = (FileReaderImpl*)reader; - RK_U32 slot_rd_idx = impl->slot_rd_idx; FileBufSlot *slot = NULL; - mpp_assert(slot_rd_idx < impl->slot_max); - mpp_assert(impl->slots); + if (NULL == impl || NULL == impl->slots) { + mpp_log_f("invalid reader %p\n", reader); + return MPP_NOK; + } + + if (impl->slot_rd_idx >= impl->slot_max) { + mpp_log_f("invalid read index % max %d\n", impl->slot_rd_idx, impl->slot_max); + return MPP_NOK; + } do { - slot = impl->slots[slot_rd_idx]; + slot = impl->slots[impl->slot_rd_idx]; if (slot == NULL) msleep(1); } while (slot == NULL); mpp_assert(slot); - *buf = slot->buf; - *size = slot->size; + *buf = slot; impl->slot_rd_idx++; - return slot->eos; + return MPP_OK; } -RK_U32 reader_index_read(FileReader reader, RK_S32 index, char** buf, size_t *size) +MPP_RET reader_index_read(FileReader reader, RK_S32 index, FileBufSlot **buf) { FileReaderImpl *impl = (FileReaderImpl*)reader; - RK_U32 slot_rd_idx = index; FileBufSlot *slot = NULL; - mpp_assert(slot_rd_idx < impl->slot_max); - mpp_assert(impl->slots); + if (NULL == impl || NULL == impl->slots) { + mpp_log_f("invalid reader %p\n", reader); + return MPP_NOK; + } + + if (index >= (RK_S32)impl->slot_max) { + mpp_log_f("invalid read index % max %d\n", index, impl->slot_max); + return MPP_NOK; + } do { - slot = impl->slots[slot_rd_idx]; + slot = impl->slots[index]; if (slot == NULL) msleep(1); } while (slot == NULL); mpp_assert(slot); - *buf = slot->buf; - *size = slot->size; + *buf = slot; - return slot->eos; + return MPP_OK; } void reader_rewind(FileReader reader) @@ -327,9 +333,9 @@ void reader_deinit(FileReader *reader) if (!slot) continue; - if (slot->hw_buf) { - mpp_buffer_put(&slot->hw_buf); - slot->hw_buf = NULL; + if (slot->buf) { + mpp_buffer_put(&slot->buf); + slot->buf = NULL; } MPP_FREE(impl->slots[i]); } diff --git a/utils/mpi_dec_utils.h b/utils/mpi_dec_utils.h index 812a7e5b..b304cfc1 100644 --- a/utils/mpi_dec_utils.h +++ b/utils/mpi_dec_utils.h @@ -26,6 +26,14 @@ typedef void* FileReader; +typedef struct FileBufSlot_t { + RK_S32 index; + MppBuffer buf; + size_t size; + RK_U32 eos; + char *data; +} FileBufSlot; + /* For overall configure setup */ typedef struct MpiDecTestCmd_t { char file_input[MAX_FILE_NAME_LENGTH]; @@ -65,8 +73,8 @@ void reader_start(FileReader reader); void reader_sync(FileReader reader); void reader_stop(FileReader reader); -RK_U32 reader_read(FileReader reader, char** buf, size_t *size); -RK_U32 reader_index_read(FileReader reader, RK_S32 index, char** buf, size_t *size); +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); void show_dec_fps(RK_S64 total_time, RK_S64 total_count, RK_S64 last_time, RK_S64 last_count);