Fix buffer allocation in Android camera

This commit is contained in:
Andrey Kamaev 2012-08-21 17:44:24 +04:00
parent a21df069e0
commit 4ac2c22776

View File

@ -48,12 +48,15 @@
#include <android/log.h>
#include <camera_activity.hpp>
#if !defined(LOGD) && !defined(LOGI) && !defined(LOGE)
//#if !defined(LOGD) && !defined(LOGI) && !defined(LOGE)
#undef LOGD
#undef LOGE
#undef LOGI
#define LOG_TAG "CV_CAP"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#endif
//#endif
class HighguiAndroidCameraActivity;
@ -86,8 +89,8 @@ protected:
//raw from camera
int m_width;
int m_height;
unsigned char *m_frameYUV420;
unsigned char *m_frameYUV420next;
cv::Mat m_frameYUV420;
cv::Mat m_frameYUV420next;
enum YUVformat
{
@ -189,8 +192,8 @@ CvCapture_Android::CvCapture_Android(int cameraId)
m_height = 0;
m_activity = 0;
m_isOpened = false;
m_frameYUV420 = 0;
m_frameYUV420next = 0;
// m_frameYUV420 = 0;
// m_frameYUV420next = 0;
m_hasGray = false;
m_hasColor = false;
m_dataState = CVCAPTURE_ANDROID_STATE_NO_FRAME;
@ -231,15 +234,14 @@ CvCapture_Android::~CvCapture_Android()
{
((HighguiAndroidCameraActivity*)m_activity)->LogFramesRate();
pthread_mutex_lock(&m_nextFrameMutex);
unsigned char *tmp1=m_frameYUV420;
unsigned char *tmp2=m_frameYUV420next;
m_frameYUV420 = 0;
m_frameYUV420next = 0;
delete tmp1;
delete tmp2;
// unsigned char *tmp1=m_frameYUV420;
// unsigned char *tmp2=m_frameYUV420next;
// m_frameYUV420 = 0;
// m_frameYUV420next = 0;
// delete tmp1;
// delete tmp2;
m_dataState=CVCAPTURE_ANDROID_STATE_NO_FRAME;
pthread_cond_broadcast(&m_nextFrameCond);
@ -355,17 +357,17 @@ bool CvCapture_Android::grabFrame()
m_dataState= CVCAPTURE_ANDROID_STATE_NO_FRAME;//we will wait new frame
}
if (m_dataState!=CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED) {
if (m_dataState!=CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED)
{
m_waitingNextFrame = true;
pthread_cond_wait(&m_nextFrameCond, &m_nextFrameMutex);
}
if (m_dataState == CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED) {
if (m_dataState == CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED)
{
//LOGD("CvCapture_Android::grabFrame: get new frame");
//swap current and new frames
unsigned char* tmp = m_frameYUV420;
m_frameYUV420 = m_frameYUV420next;
m_frameYUV420next = tmp;
cv::swap(m_frameYUV420, m_frameYUV420next);
//discard cached frames
m_hasGray = false;
@ -393,7 +395,8 @@ IplImage* CvCapture_Android::retrieveFrame( int outputType )
{
IplImage* image = NULL;
unsigned char *current_frameYUV420=m_frameYUV420;
cv::Mat m_frameYUV420_ref = m_frameYUV420;
unsigned char *current_frameYUV420=m_frameYUV420_ref.ptr();
//Attention! all the operations in this function below should occupy less time than the period between two frames from camera
if (NULL != current_frameYUV420)
{
@ -456,19 +459,9 @@ void CvCapture_Android::setFrame(const void* buffer, int bufferSize)
prepareCacheForYUV(width, height);
//copy data
memcpy(m_frameYUV420next, buffer, bufferSize);
//LOGD("CvCapture_Android::setFrame -- memcpy is done");
#if 0 //moved this part of code into grabFrame
//swap current and new frames
unsigned char* tmp = m_frameYUV420;
m_frameYUV420 = m_frameYUV420next;
m_frameYUV420next = tmp;
//discard cached frames
m_hasGray = false;
m_hasColor = false;
#endif
cv::Mat m_frameYUV420next_ref = m_frameYUV420next;
memcpy(m_frameYUV420next_ref.ptr(), buffer, bufferSize);
LOGD("CvCapture_Android::setFrame -- memcpy is done");
m_dataState = CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED;
m_waitingNextFrame = false;//set flag that no more frames required at this moment
@ -482,17 +475,22 @@ void CvCapture_Android::prepareCacheForYUV(int width, int height)
LOGD("CvCapture_Android::prepareCacheForYUV: Changing size of buffers: from width=%d height=%d to width=%d height=%d", m_width, m_height, width, height);
m_width = width;
m_height = height;
/*
unsigned char *tmp = m_frameYUV420next;
m_frameYUV420next = new unsigned char [width * height * 3 / 2];
if (tmp != NULL) {
if (tmp != NULL)
{
delete[] tmp;
}
tmp = m_frameYUV420;
m_frameYUV420 = new unsigned char [width * height * 3 / 2];
if (tmp != NULL) {
if (tmp != NULL)
{
delete[] tmp;
}
}*/
m_frameYUV420.create(height * 3 / 2, width, CV_8UC1);
m_frameYUV420next.create(height * 3 / 2, width, CV_8UC1);
}
}