Commit 08829367 authored by Matthew Self's avatar Matthew Self Committed by Ryan Govostes

Fix buffer release issue

CvVideoWriter_AVFoundation_Mac had a serious buffer release bug.

Also made writeFrame() block until isReadyForMoreMediaData rather than
return an error.
parent a92da54e
...@@ -927,7 +927,7 @@ IplImage* CvCaptureFile::retrieveFramePixelBuffer() { ...@@ -927,7 +927,7 @@ IplImage* CvCaptureFile::retrieveFramePixelBuffer() {
} else if (mMode == CV_CAP_MODE_GRAY) { } else if (mMode == CV_CAP_MODE_GRAY) {
cvtCode = CV_YUV2GRAY_UYVY; cvtCode = CV_YUV2GRAY_UYVY;
} else if (mMode == CV_CAP_MODE_YUYV) { } else if (mMode == CV_CAP_MODE_YUYV) {
cvtCode = 0; // Copy cvtCode = -1; // Copy
} else { } else {
CVPixelBufferUnlockBaseAddress(mGrabbedPixels, 0); CVPixelBufferUnlockBaseAddress(mGrabbedPixels, 0);
CVBufferRelease(mGrabbedPixels); CVBufferRelease(mGrabbedPixels);
...@@ -976,7 +976,7 @@ IplImage* CvCaptureFile::retrieveFramePixelBuffer() { ...@@ -976,7 +976,7 @@ IplImage* CvCaptureFile::retrieveFramePixelBuffer() {
mDeviceImage->imageSize = int(rowBytes*height); mDeviceImage->imageSize = int(rowBytes*height);
// Convert the device image into the output image. // Convert the device image into the output image.
if (cvtCode == 0) { if (cvtCode == -1) {
// Copy. // Copy.
cv::cvarrToMat(mDeviceImage).copyTo(cv::cvarrToMat(mOutImage)); cv::cvarrToMat(mDeviceImage).copyTo(cv::cvarrToMat(mOutImage));
} else { } else {
...@@ -1213,7 +1213,7 @@ CvVideoWriter_AVFoundation_Mac::CvVideoWriter_AVFoundation_Mac(const char* filen ...@@ -1213,7 +1213,7 @@ CvVideoWriter_AVFoundation_Mac::CvVideoWriter_AVFoundation_Mac(const char* filen
if(mMovieWriter.status == AVAssetWriterStatusFailed){ if(mMovieWriter.status == AVAssetWriterStatusFailed){
NSLog(@"%@", [mMovieWriter.error localizedDescription]); NSLog(@"AVF: AVAssetWriter status: %@", [mMovieWriter.error localizedDescription]);
// TODO: error handling, cleanup. Throw execption? // TODO: error handling, cleanup. Throw execption?
// return; // return;
} }
...@@ -1239,17 +1239,27 @@ CvVideoWriter_AVFoundation_Mac::~CvVideoWriter_AVFoundation_Mac() { ...@@ -1239,17 +1239,27 @@ CvVideoWriter_AVFoundation_Mac::~CvVideoWriter_AVFoundation_Mac() {
} }
static void releaseCallback( void *releaseRefCon, const void * ) {
CFRelease((CFDataRef)releaseRefCon);
}
bool CvVideoWriter_AVFoundation_Mac::writeFrame(const IplImage* iplimage) { bool CvVideoWriter_AVFoundation_Mac::writeFrame(const IplImage* iplimage) {
NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];
// writer status check // writer status check
if (![mMovieWriterInput isReadyForMoreMediaData] || mMovieWriter.status != AVAssetWriterStatusWriting ) { if (mMovieWriter.status != AVAssetWriterStatusWriting ) {
NSLog(@"[mMovieWriterInput isReadyForMoreMediaData] Not ready for media data or ...");
NSLog(@"mMovieWriter.status: %d. Error: %@", (int)mMovieWriter.status, [mMovieWriter.error localizedDescription]); NSLog(@"mMovieWriter.status: %d. Error: %@", (int)mMovieWriter.status, [mMovieWriter.error localizedDescription]);
[localpool drain]; [localpool drain];
return false; return false;
} }
// Make writeFrame() a blocking call.
while (![mMovieWriterInput isReadyForMoreMediaData]) {
fprintf(stderr, "OpenCV: AVF: waiting to write video data.\n");
// Sleep 1 msec.
usleep(1000);
}
BOOL success = FALSE; BOOL success = FALSE;
if (iplimage->height!=movieSize.height || iplimage->width!=movieSize.width){ if (iplimage->height!=movieSize.height || iplimage->width!=movieSize.width){
...@@ -1283,8 +1293,8 @@ bool CvVideoWriter_AVFoundation_Mac::writeFrame(const IplImage* iplimage) { ...@@ -1283,8 +1293,8 @@ bool CvVideoWriter_AVFoundation_Mac::writeFrame(const IplImage* iplimage) {
kCVPixelFormatType_32BGRA, kCVPixelFormatType_32BGRA,
(void*)CFDataGetBytePtr(cfData), (void*)CFDataGetBytePtr(cfData),
CGImageGetBytesPerRow(cgImage), CGImageGetBytesPerRow(cgImage),
NULL, &releaseCallback,
0, (void *)cfData,
NULL, NULL,
&pixelBuffer); &pixelBuffer);
if(status == kCVReturnSuccess){ if(status == kCVReturnSuccess){
...@@ -1293,7 +1303,6 @@ bool CvVideoWriter_AVFoundation_Mac::writeFrame(const IplImage* iplimage) { ...@@ -1293,7 +1303,6 @@ bool CvVideoWriter_AVFoundation_Mac::writeFrame(const IplImage* iplimage) {
} }
//cleanup //cleanup
CFRelease(cfData);
CVPixelBufferRelease(pixelBuffer); CVPixelBufferRelease(pixelBuffer);
CGImageRelease(cgImage); CGImageRelease(cgImage);
CGDataProviderRelease(provider); CGDataProviderRelease(provider);
......
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