Commit ad35f317 authored by Andrey Kamaev's avatar Andrey Kamaev

Fix buffer allocation in Android camera

parent 1b5903a7
...@@ -48,12 +48,15 @@ ...@@ -48,12 +48,15 @@
#include <android/log.h> #include <android/log.h>
#include <camera_activity.hpp> #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 LOG_TAG "CV_CAP"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) #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 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__)) #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#endif //#endif
class HighguiAndroidCameraActivity; class HighguiAndroidCameraActivity;
...@@ -86,8 +89,8 @@ protected: ...@@ -86,8 +89,8 @@ protected:
//raw from camera //raw from camera
int m_width; int m_width;
int m_height; int m_height;
unsigned char *m_frameYUV420; cv::Mat m_frameYUV420;
unsigned char *m_frameYUV420next; cv::Mat m_frameYUV420next;
enum YUVformat enum YUVformat
{ {
...@@ -189,8 +192,8 @@ CvCapture_Android::CvCapture_Android(int cameraId) ...@@ -189,8 +192,8 @@ CvCapture_Android::CvCapture_Android(int cameraId)
m_height = 0; m_height = 0;
m_activity = 0; m_activity = 0;
m_isOpened = false; m_isOpened = false;
m_frameYUV420 = 0; // m_frameYUV420 = 0;
m_frameYUV420next = 0; // m_frameYUV420next = 0;
m_hasGray = false; m_hasGray = false;
m_hasColor = false; m_hasColor = false;
m_dataState = CVCAPTURE_ANDROID_STATE_NO_FRAME; m_dataState = CVCAPTURE_ANDROID_STATE_NO_FRAME;
...@@ -231,15 +234,14 @@ CvCapture_Android::~CvCapture_Android() ...@@ -231,15 +234,14 @@ CvCapture_Android::~CvCapture_Android()
{ {
((HighguiAndroidCameraActivity*)m_activity)->LogFramesRate(); ((HighguiAndroidCameraActivity*)m_activity)->LogFramesRate();
pthread_mutex_lock(&m_nextFrameMutex); pthread_mutex_lock(&m_nextFrameMutex);
unsigned char *tmp1=m_frameYUV420; // unsigned char *tmp1=m_frameYUV420;
unsigned char *tmp2=m_frameYUV420next; // unsigned char *tmp2=m_frameYUV420next;
m_frameYUV420 = 0; // m_frameYUV420 = 0;
m_frameYUV420next = 0; // m_frameYUV420next = 0;
delete tmp1; // delete tmp1;
delete tmp2; // delete tmp2;
m_dataState=CVCAPTURE_ANDROID_STATE_NO_FRAME; m_dataState=CVCAPTURE_ANDROID_STATE_NO_FRAME;
pthread_cond_broadcast(&m_nextFrameCond); pthread_cond_broadcast(&m_nextFrameCond);
...@@ -355,17 +357,17 @@ bool CvCapture_Android::grabFrame() ...@@ -355,17 +357,17 @@ bool CvCapture_Android::grabFrame()
m_dataState= CVCAPTURE_ANDROID_STATE_NO_FRAME;//we will wait new frame 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; m_waitingNextFrame = true;
pthread_cond_wait(&m_nextFrameCond, &m_nextFrameMutex); 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"); //LOGD("CvCapture_Android::grabFrame: get new frame");
//swap current and new frames //swap current and new frames
unsigned char* tmp = m_frameYUV420; cv::swap(m_frameYUV420, m_frameYUV420next);
m_frameYUV420 = m_frameYUV420next;
m_frameYUV420next = tmp;
//discard cached frames //discard cached frames
m_hasGray = false; m_hasGray = false;
...@@ -393,7 +395,8 @@ IplImage* CvCapture_Android::retrieveFrame( int outputType ) ...@@ -393,7 +395,8 @@ IplImage* CvCapture_Android::retrieveFrame( int outputType )
{ {
IplImage* image = NULL; 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 //Attention! all the operations in this function below should occupy less time than the period between two frames from camera
if (NULL != current_frameYUV420) if (NULL != current_frameYUV420)
{ {
...@@ -456,19 +459,9 @@ void CvCapture_Android::setFrame(const void* buffer, int bufferSize) ...@@ -456,19 +459,9 @@ void CvCapture_Android::setFrame(const void* buffer, int bufferSize)
prepareCacheForYUV(width, height); prepareCacheForYUV(width, height);
//copy data //copy data
memcpy(m_frameYUV420next, buffer, bufferSize); cv::Mat m_frameYUV420next_ref = m_frameYUV420next;
//LOGD("CvCapture_Android::setFrame -- memcpy is done"); memcpy(m_frameYUV420next_ref.ptr(), 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
m_dataState = CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED; m_dataState = CVCAPTURE_ANDROID_STATE_HAS_NEW_FRAME_UNGRABBED;
m_waitingNextFrame = false;//set flag that no more frames required at this moment 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) ...@@ -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); 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_width = width;
m_height = height; m_height = height;
/*
unsigned char *tmp = m_frameYUV420next; unsigned char *tmp = m_frameYUV420next;
m_frameYUV420next = new unsigned char [width * height * 3 / 2]; m_frameYUV420next = new unsigned char [width * height * 3 / 2];
if (tmp != NULL) { if (tmp != NULL)
{
delete[] tmp; delete[] tmp;
} }
tmp = m_frameYUV420; tmp = m_frameYUV420;
m_frameYUV420 = new unsigned char [width * height * 3 / 2]; m_frameYUV420 = new unsigned char [width * height * 3 / 2];
if (tmp != NULL) { if (tmp != NULL)
{
delete[] tmp; delete[] tmp;
} }*/
m_frameYUV420.create(height * 3 / 2, width, CV_8UC1);
m_frameYUV420next.create(height * 3 / 2, width, CV_8UC1);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment