[mpp/osal]: add codec thread and hal thread

1. add MppThread / Mutex / Condition class
2. add internal flag to MppBuffer
3. change MppBufferService from structure to class
4. add mpp_buffer_put on mpp_frame_put
5. add coding type to Mpp initial function
6. mpp codec / hal thread basic flow done, but reset is not added

git-svn-id: https://10.10.10.66:8443/svn/MediaProcessPlatform/trunk/mpp@168 6e48237b-75ef-9749-8fc9-41990f28c85a
This commit is contained in:
ChenHengming
2015-08-26 12:46:38 +00:00
parent 174c68fb5e
commit 3612ed5876
9 changed files with 368 additions and 209 deletions

View File

@@ -22,43 +22,62 @@
#include "mpp.h"
#include "mpp_frame_impl.h"
#include "mpp_buffer.h"
#include "mpp_packet.h"
#include "mpp_packet_impl.h"
static void *thread_hal(void *data)
#define MPP_TEST_FRAME_SIZE SZ_1M
void *thread_hal(void *data)
{
Mpp *mpp = (Mpp*)data;
MppThread *thd_dec = mpp->thd_codec;
MppThread *thd_hal = mpp->thd_hal;
mpp_list *packets = mpp->packets;
mpp_list *frames = mpp->frames;
MppThread *codec = mpp->mTheadCodec;
MppThread *hal = mpp->mThreadHal;
mpp_list *frames = mpp->mFrames;
mpp_list *tasks = mpp->mTasks;
while (MPP_THREAD_RUNNING == thd_hal->get_status()) {
while (MPP_THREAD_RUNNING == hal->get_status()) {
/*
* hal thread wait for dxva interface intput firt
*/
hal->lock();
if (0 == tasks->list_size())
hal->wait();
hal->unlock();
// get_config
// register genertation
if (tasks->list_size()) {
MppHalDecTask *task;
mpp->mTasks->del_at_head(&task, sizeof(task));
mpp->mTaskGetCount++;
/*
* wait previous register set done
*/
// hal->get_regs;
/*
* wait previous register set done
*/
// hal->get_regs;
/*
* send current register set to hardware
*/
// hal->put_regs;
/*
* send current register set to hardware
*/
// hal->put_regs;
/*
* mark previous buffer is complete
*/
// signal()
// mark frame in output queue
// wait up output thread to get a output frame
/*
* mark previous buffer is complete
*/
// signal()
// mark frame in output queue
// wait up output thread to get a output frame
msleep(10);
// for test
MppBuffer buffer;
mpp_buffer_get(mpp->mFrameGroup, &buffer, MPP_TEST_FRAME_SIZE);
MppFrame frame;
mpp_frame_init(&frame);
mpp_frame_set_buffer(frame, buffer);
frames->add_at_tail(&frame, sizeof(frame));
mpp->mFramePutCount++;
}
}
return NULL;
@@ -67,20 +86,34 @@ static void *thread_hal(void *data)
static void *thread_dec(void *data)
{
Mpp *mpp = (Mpp*)data;
MppThread *thd_dec = mpp->thd_codec;
MppThread *thd_hal = mpp->thd_hal;
mpp_list *packets = mpp->packets;
mpp_list *frames = mpp->frames;
MppThread *dec = mpp->mTheadCodec;
MppThread *hal = mpp->mThreadHal;
mpp_list *packets = mpp->mPackets;
mpp_list *frames = mpp->mFrames;
MppPacketImpl packet;
MppFrame frame;
while (MPP_THREAD_RUNNING == thd_dec->get_status()) {
while (MPP_THREAD_RUNNING == dec->get_status()) {
RK_U32 packet_ready = 0;
/*
* wait for stream input
*/
dec->lock();
if (0 == packets->list_size())
dec->wait();
dec->unlock();
if (packets->list_size()) {
mpp->mPacketLock.lock();
/*
* packet will be destroyed outside, here just copy the content
*/
packets->del_at_head(&packet, sizeof(packet));
mpp->mPacketGetCount++;
packet_ready = 1;
mpp->mPacketLock.unlock();
}
if (packet_ready) {
/*
* 1. send packet data to parser
*
@@ -106,7 +139,9 @@ static void *thread_dec(void *data)
* frame to hal loop.
*/
// mpp->get_buffer
//MppBuffer buffer;
//mpp_buffer_get(mpp->mFrameGroup, &buffer, MPP_TEST_FRAME_SIZE);
/*
* 3. send dxva output information and buffer information to hal thread
@@ -115,11 +150,9 @@ static void *thread_dec(void *data)
// hal->wait_prev_done;
// hal->send_config;
// for test
mpp_frame_init(&frame);
frames->add_at_tail(&frame, sizeof(frame));
mpp->mTasks->add_at_tail(&mpp->mTask[0], sizeof(mpp->mTask[0]));
mpp->mTaskPutCount++;
hal->signal();
}
}
@@ -129,14 +162,16 @@ static void *thread_dec(void *data)
static void *thread_enc(void *data)
{
Mpp *mpp = (Mpp*)data;
mpp_list *packets = mpp->packets;
mpp_list *frames = mpp->frames;
MppThread *thd_enc = mpp->mTheadCodec;
MppThread *thd_hal = mpp->mThreadHal;
mpp_list *packets = mpp->mPackets;
mpp_list *frames = mpp->mFrames;
MppFrameImpl frame;
MppPacket packet;
size_t size = SZ_1M;
char *buf = mpp_malloc(char, size);
while (mpp->thread_codec_running) {
while (MPP_THREAD_RUNNING == thd_enc->get_status()) {
if (frames->list_size()) {
frames->del_at_head(&frame, sizeof(frame));
@@ -148,102 +183,147 @@ static void *thread_enc(void *data)
return NULL;
}
Mpp::Mpp(MppCtxType type)
: packets(NULL),
frames(NULL),
thd_codec(NULL),
thd_hal(NULL),
thread_codec_running(0),
thread_codec_reset(0),
status(0)
Mpp::Mpp(MppCtxType type, MppCodingType coding)
: mPackets(NULL),
mFrames(NULL),
mTasks(NULL),
mPacketPutCount(0),
mPacketGetCount(0),
mFramePutCount(0),
mFrameGetCount(0),
mTaskPutCount(0),
mTaskGetCount(0),
mPacketGroup(NULL),
mFrameGroup(NULL),
mTheadCodec(NULL),
mThreadHal(NULL),
mType(type),
mCoding(coding),
mStatus(0),
mTask(NULL),
mTaskNum(2)
{
switch (type) {
switch (mType) {
case MPP_CTX_DEC : {
packets = new mpp_list((node_destructor)NULL);
frames = new mpp_list((node_destructor)mpp_frame_deinit);
thd_codec = new MppThread(thread_dec, this);
thd_hal = new MppThread(thread_hal, this);
mPackets = new mpp_list((node_destructor)NULL);
mFrames = new mpp_list((node_destructor)mpp_frame_deinit);
mTasks = new mpp_list((node_destructor)NULL);
mTheadCodec = new MppThread(thread_dec, this);
mThreadHal = new MppThread(thread_hal, this);
mTask = mpp_malloc(MppHalDecTask*, mTaskNum);
mpp_buffer_group_normal_get(&mPacketGroup, MPP_BUFFER_TYPE_NORMAL);
mpp_buffer_group_limited_get(&mFrameGroup, MPP_BUFFER_TYPE_ION);
mpp_buffer_group_limit_config(mFrameGroup, 4, MPP_TEST_FRAME_SIZE);
} break;
case MPP_CTX_ENC : {
frames = new mpp_list((node_destructor)NULL);
packets = new mpp_list((node_destructor)mpp_packet_deinit);
thd_codec = new MppThread(thread_enc, this);
thd_hal = new MppThread(thread_hal, this);
mFrames = new mpp_list((node_destructor)NULL);
mPackets = new mpp_list((node_destructor)mpp_packet_deinit);
mTasks = new mpp_list((node_destructor)NULL);
mTheadCodec = new MppThread(thread_enc, this);
mThreadHal = new MppThread(thread_hal, this);
mTask = mpp_malloc(MppHalDecTask*, mTaskNum);
mpp_buffer_group_normal_get(&mPacketGroup, MPP_BUFFER_TYPE_NORMAL);
mpp_buffer_group_limited_get(&mFrameGroup, MPP_BUFFER_TYPE_ION);
} break;
default : {
mpp_err("Mpp error type %d\n", type);
mpp_err("Mpp error type %d\n", mType);
} break;
}
if (packets && frames && thd_codec && thd_hal) {
thd_codec->start();
thd_hal->start();
} else {
if (thd_codec)
thd_codec->stop();
if (thd_hal)
thd_hal->stop();
if (thd_codec) {
delete thd_codec;
thd_codec = NULL;
}
if (thd_hal) {
delete thd_hal;
thd_hal = NULL;
}
if (packets) {
delete packets;
packets = NULL;
}
if (frames) {
delete frames;
frames = NULL;
}
}
if (mFrames && mPackets && mTask &&
mTheadCodec && mThreadHal &&
mPacketGroup && mFrameGroup) {
mTheadCodec->start();
mThreadHal->start();
} else
clear();
}
Mpp::~Mpp ()
{
if (thd_codec)
thd_codec->stop();
if (thd_hal)
thd_hal->stop();
clear();
}
if (thd_codec)
delete thd_codec;
if (thd_hal)
delete thd_hal;
if (packets)
delete packets;
if (frames)
delete frames;
void Mpp::clear()
{
if (mTheadCodec)
mTheadCodec->stop();
if (mThreadHal)
mThreadHal->stop();
if (mTheadCodec) {
delete mTheadCodec;
mTheadCodec = NULL;
}
if (mThreadHal) {
delete mThreadHal;
mThreadHal = NULL;
}
if (mPackets) {
delete mPackets;
mPackets = NULL;
}
if (mFrames) {
delete mFrames;
mFrames = NULL;
}
if (mTasks) {
delete mTasks;
mTasks = NULL;
}
if (mPacketGroup) {
mpp_buffer_group_put(mPacketGroup);
mPacketGroup = NULL;
}
if (mFrameGroup) {
mpp_buffer_group_put(mFrameGroup);
mFrameGroup = NULL;
}
if (mTask)
mpp_free(mTask);
}
MPP_RET Mpp::put_packet(MppPacket packet)
{
// TODO: packet data need preprocess or can be write to hardware buffer
return (MPP_RET)packets->add_at_tail(packet, sizeof(MppPacketImpl));
Mutex::Autolock autoLock(&mPacketLock);
if (mPackets->list_size() < 4) {
mPackets->add_at_tail(packet, sizeof(MppPacketImpl));
mPacketPutCount++;
mTheadCodec->signal();
return MPP_OK;
}
return MPP_NOK;
}
MPP_RET Mpp::get_frame(MppFrame *frame)
{
if (frames->list_size()) {
frames->del_at_tail(frame, sizeof(frame));
Mutex::Autolock autoLock(&mFrameLock);
if (mFrames->list_size()) {
mFrames->del_at_tail(frame, sizeof(frame));
mFrameGetCount++;
}
mThreadHal->signal();
return MPP_OK;
}
MPP_RET Mpp::put_frame(MppFrame frame)
{
MPP_RET ret = (MPP_RET)frames->add_at_tail(frame, sizeof(MppFrameImpl));
return ret;
Mutex::Autolock autoLock(&mFrameLock);
if (mFrames->list_size() < 4) {
mFrames->add_at_tail(frame, sizeof(MppFrameImpl));
mTheadCodec->signal();
mFramePutCount++;
return MPP_OK;
}
return MPP_NOK;
}
MPP_RET Mpp::get_packet(MppPacket *packet)
{
if (packets->list_size()) {
packets->del_at_tail(packet, sizeof(packet));
Mutex::Autolock autoLock(&mPacketLock);
if (mPackets->list_size()) {
mPackets->del_at_tail(packet, sizeof(packet));
mPacketGetCount++;
}
return MPP_OK;
}