[mpp_dec]: Optimize sort pts function

Record every pts of pkt to link, and sort link by pts.
When output the frame, remove the pts from the link and set to frame.

Signed-off-by: Yandong Lin <yandong.lin@rock-chips.com>
Change-Id: Id987dea0195c24d2bd573416e80a3ef2f58d6a31
This commit is contained in:
Yandong Lin
2021-10-13 16:04:36 +08:00
committed by Herman Chen
parent ea564af5c6
commit 1c03947d59
4 changed files with 68 additions and 32 deletions

View File

@@ -18,6 +18,8 @@
#define __MPP_DEC_IMPL_H__
#include "mpp_time.h"
#include "mpp_mem_pool.h"
#include "mpp_lock.h"
#include "hal_info.h"
#include "mpp.h"
@@ -122,8 +124,18 @@ typedef struct MppDecImpl_t {
RK_U32 dec_in_pkt_count;
RK_U32 dec_hw_run_count;
RK_U32 dec_out_frame_count;
MppMemPool ts_pool;
struct list_head ts_link;
spinlock_t ts_lock;
} MppDecImpl;
typedef struct MppPktTimestamp_t {
struct list_head link;
RK_S32 pts;
RK_S32 dts;
} MppPktTs;
#ifdef __cplusplus
extern "C" {
#endif

View File

@@ -118,6 +118,17 @@ typedef struct DecTask_t {
HalTaskInfo info;
} DecTask;
static RK_S32 ts_cmp(void *priv, const struct list_head *a, const struct list_head *b)
{
MppPktTs *ts1, *ts2;
(void)priv;
ts1 = container_of(a, MppPktTs, link);
ts2 = container_of(b, MppPktTs, link);
return ts1->pts - ts2->pts;
}
static void dec_task_init(DecTask *task)
{
task->hnd = NULL;
@@ -294,8 +305,15 @@ static RK_U32 reset_parser_thread(Mpp *mpp, DecTask *task)
}
if (dec->cfg.base.sort_pts) {
AutoMutex autoLock(mpp->mTimeStamps->mutex());
mpp->mTimeStamps->flush();
// flush
MppPktTs *ts, *pos;
mpp_spinlock_lock(&dec->ts_lock);
list_for_each_entry_safe(ts, pos, &dec->ts_link, MppPktTs, link) {
list_del_init(&ts->link);
mpp_mem_pool_put(dec->ts_pool, ts);
}
mpp_spinlock_unlock(&dec->ts_lock);
}
if (task->status.dec_pkt_copy_rdy) {
@@ -526,17 +544,18 @@ static void mpp_dec_put_frame(Mpp *mpp, RK_S32 index, HalDecTaskFlag flags)
if (!change) {
if (dec->cfg.base.sort_pts) {
MppPacket pkt = NULL;
mpp_list *ts = mpp->mTimeStamps;
MppPktTs *pkt_ts;
AutoMutex autoLock(ts->mutex());
if (ts->list_size()) {
ts->del_at_head(&pkt, sizeof(pkt));
mpp_frame_set_dts(frame, mpp_packet_get_dts(pkt));
mpp_frame_set_pts(frame, mpp_packet_get_pts(pkt));
mpp_packet_deinit(&pkt);
} else
mpp_err_f("pull out packet error.\n");
mpp_spinlock_lock(&dec->ts_lock);
pkt_ts = list_first_entry_or_null(&dec->ts_link, MppPktTs, link);
if (pkt_ts)
list_del_init(&pkt_ts->link);
mpp_spinlock_unlock(&dec->ts_lock);
if (pkt_ts) {
mpp_frame_set_dts(frame, pkt_ts->dts);
mpp_frame_set_pts(frame, pkt_ts->pts);
mpp_mem_pool_put(dec->ts_pool, pkt_ts);
}
}
}
mpp_frame_set_info_change(frame, change);
@@ -781,19 +800,6 @@ static MPP_RET try_proc_dec_task(Mpp *mpp, DecTask *task)
dec_dbg_detail("detail: %p get pkt pts %llu len %d\n", dec,
mpp_packet_get_pts(dec->mpp_pkt_in),
mpp_packet_get_length(dec->mpp_pkt_in));
if (dec->cfg.base.sort_pts) {
MppPacket pkt_in = NULL;
mpp_list *ts = mpp->mTimeStamps;
AutoMutex autoLock(ts->mutex());
mpp_packet_new(&pkt_in);
if (pkt_in) {
mpp_packet_set_pts(pkt_in, mpp_packet_get_pts(dec->mpp_pkt_in));
mpp_packet_set_dts(pkt_in, mpp_packet_get_dts(dec->mpp_pkt_in));
ts->add_at_tail(&pkt_in, sizeof(pkt_in));
}
}
}
/*
@@ -827,7 +833,18 @@ static MPP_RET try_proc_dec_task(Mpp *mpp, DecTask *task)
mpp_clock_start(dec->clocks[DEC_PRS_PREPARE]);
mpp_parser_prepare(dec->parser, dec->mpp_pkt_in, task_dec);
mpp_clock_pause(dec->clocks[DEC_PRS_PREPARE]);
if (dec->cfg.base.sort_pts && task_dec->valid) {
MppPktTs *pkt_ts = (MppPktTs*)mpp_mem_pool_get(dec->ts_pool);
mpp_assert(pkt_ts);
pkt_ts->pts = mpp_packet_get_pts(dec->mpp_pkt_in);
pkt_ts->dts = mpp_packet_get_dts(dec->mpp_pkt_in);
INIT_LIST_HEAD(&pkt_ts->link);
mpp_spinlock_lock(&dec->ts_lock);
list_add_tail(&pkt_ts->link, &dec->ts_link);
list_sort(NULL, &dec->ts_link, ts_cmp);
mpp_spinlock_unlock(&dec->ts_lock);
}
dec_release_input_packet(dec, 0);
}
@@ -1729,6 +1746,15 @@ MPP_RET mpp_dec_init(MppDec *dec, MppDecInitCfg *cfg)
sem_init(&p->parser_reset, 0, 0);
sem_init(&p->hal_reset, 0, 0);
// init timestamp for record and sort pts
mpp_spinlock_init(&p->ts_lock);
INIT_LIST_HEAD(&p->ts_link);
p->ts_pool = mpp_mem_pool_init(sizeof(MppPktTs));
if (!p->ts_pool) {
mpp_err_f("malloc ts pool failed!\n");
break;
}
*dec = p;
dec_dbg_func("%p out\n", p);
return MPP_OK;
@@ -1813,6 +1839,10 @@ MPP_RET mpp_dec_deinit(MppDec ctx)
sem_destroy(&dec->parser_reset);
sem_destroy(&dec->hal_reset);
if (dec->ts_pool) {
mpp_mem_pool_deinit(dec->ts_pool);
dec->ts_pool = NULL;
}
mpp_free(dec);
dec_dbg_func("%p out\n", dec);

View File

@@ -124,7 +124,6 @@ public:
mpp_list *mPackets;
mpp_list *mFrames;
mpp_list *mTimeStamps;
/* counters for debug */
RK_U32 mPacketPutCount;
RK_U32 mPacketGetCount;

View File

@@ -63,7 +63,6 @@ static void *list_wraper_frame(void *arg)
Mpp::Mpp(MppCtx ctx = NULL)
: mPackets(NULL),
mFrames(NULL),
mTimeStamps(NULL),
mPacketPutCount(0),
mPacketGetCount(0),
mFramePutCount(0),
@@ -134,7 +133,6 @@ MPP_RET Mpp::init(MppCtxType type, MppCodingType coding)
case MPP_CTX_DEC : {
mPackets = new mpp_list(list_wraper_packet);
mFrames = new mpp_list(list_wraper_frame);
mTimeStamps = new mpp_list(list_wraper_packet);
if (mInputTimeout == MPP_POLL_BUTT)
mInputTimeout = MPP_POLL_NON_BLOCK;
@@ -273,10 +271,7 @@ void Mpp::clear()
delete mFrames;
mFrames = NULL;
}
if (mTimeStamps) {
delete mTimeStamps;
mTimeStamps = NULL;
}
if (mPacketGroup) {
mpp_buffer_group_put(mPacketGroup);
mPacketGroup = NULL;