shithub: h264bsd

Download patch

ref: 2c655d6217273efb79b3be8aa9570404206373cc
parent: a4de0689955cae430342a5ade5432801d497c3b6
author: Sam Leitch <sam.leitch@calgaryscientific.com>
date: Tue May 6 05:16:15 EDT 2014

Fixed memory leak in RGB/YCrCb conversion. Renamed internal storage variables. Added timing to win32 test.

--- a/src/h264bsd_decoder.c
+++ b/src/h264bsd_decoder.c
@@ -568,6 +568,8 @@
     FREE(pStorage->mb);
     FREE(pStorage->sliceGroupMap);
 
+    if(pStorage->conversionBuffer != NULL) FREE(pStorage->conversionBuffer);
+
     h264bsdFreeDpb(pStorage->dpb);
 
 }
@@ -652,15 +654,15 @@
 
     if(data == NULL) return NULL;
 
-    if(pStorage->rgbConversionBufferSize < rgbSize)
+    if(pStorage->conversionBufferSize < rgbSize)
     {
-        if(pStorage->rgbConversionBuffer != NULL) free(pStorage->rgbConversionBuffer);
-        pStorage->rgbConversionBufferSize = rgbSize;
-        pStorage->rgbConversionBuffer = (u32*)malloc(rgbSize);
+        if(pStorage->conversionBuffer != NULL) free(pStorage->conversionBuffer);
+        pStorage->conversionBufferSize = rgbSize;
+        pStorage->conversionBuffer = (u32*)malloc(rgbSize);
     }
 
-    h264bsdConvertToRGBA(width, height, data, pStorage->rgbConversionBuffer);
-    return pStorage->rgbConversionBuffer;
+    h264bsdConvertToRGBA(width, height, data, pStorage->conversionBuffer);
+    return pStorage->conversionBuffer;
 }
 
 /*------------------------------------------------------------------------------
@@ -694,15 +696,15 @@
 
     if(data == NULL) return NULL;
 
-    if(pStorage->rgbConversionBufferSize < rgbSize)
+    if(pStorage->conversionBufferSize < rgbSize)
     {
-        if(pStorage->rgbConversionBuffer != NULL) free(pStorage->rgbConversionBuffer);
-        pStorage->rgbConversionBufferSize = rgbSize;
-        pStorage->rgbConversionBuffer = (u32*)malloc(rgbSize);
+        if(pStorage->conversionBuffer != NULL) free(pStorage->conversionBuffer);
+        pStorage->conversionBufferSize = rgbSize;
+        pStorage->conversionBuffer = (u32*)malloc(rgbSize);
     }
 
-    h264bsdConvertToBGRA(width, height, data, pStorage->rgbConversionBuffer);
-    return pStorage->rgbConversionBuffer;
+    h264bsdConvertToBGRA(width, height, data, pStorage->conversionBuffer);
+    return pStorage->conversionBuffer;
 }
 
 /*------------------------------------------------------------------------------
@@ -736,15 +738,15 @@
 
     if(data == NULL) return NULL;
 
-    if(pStorage->rgbConversionBufferSize < rgbSize)
+    if(pStorage->conversionBufferSize < rgbSize)
     {
-        if(pStorage->rgbConversionBuffer != NULL) free(pStorage->rgbConversionBuffer);
-        pStorage->rgbConversionBufferSize = rgbSize;
-        pStorage->rgbConversionBuffer = (u32*)malloc(rgbSize);
+        if(pStorage->conversionBuffer != NULL) free(pStorage->conversionBuffer);
+        pStorage->conversionBufferSize = rgbSize;
+        pStorage->conversionBuffer = (u32*)malloc(rgbSize);
     }
 
-    h264bsdConvertToYCbCrA(width, height, data, pStorage->rgbConversionBuffer);
-    return pStorage->rgbConversionBuffer;
+    h264bsdConvertToYCbCrA(width, height, data, pStorage->conversionBuffer);
+    return pStorage->conversionBuffer;
 }
 
 /*------------------------------------------------------------------------------
--- a/src/h264bsd_storage.h
+++ b/src/h264bsd_storage.h
@@ -147,8 +147,8 @@
                               HEADERS_RDY to the user */
     u32 intraConcealmentFlag; /* 0 gray picture for corrupted intra
                                  1 previous frame used if available */
-    u32* rgbConversionBuffer; // used to performance yuv conversion
-    size_t rgbConversionBufferSize;
+    u32* conversionBuffer; // used to perform yuv conversion
+    size_t conversionBufferSize;
 } storage_t;
 
 /*------------------------------------------------------------------------------
--- a/win/h264bsdTest/h264bsdTest.cpp
+++ b/win/h264bsdTest/h264bsdTest.cpp
@@ -4,6 +4,7 @@
 #include <SDKDDKVer.h>
 #include <stdio.h>
 #include <tchar.h>
+#include <Windows.h>
 
 extern "C"
 {
@@ -16,7 +17,8 @@
     storage_t *decoder = NULL;
     char filename[1024];
     sprintf_s(filename, 1024, "%S", argv[1]);
-    FILE *input = fopen(filename, "rb");
+    FILE *input;
+    fopen_s(&input, filename, "rb");
 
     fseek(input, 0L, SEEK_END);
     long fileSize = ftell(input);
@@ -24,10 +26,18 @@
     u8 *fileData = (u8*)malloc(fileSize);
     if(fileData == NULL) return 1;
 
+    LARGE_INTEGER frequency_li;
+    QueryPerformanceFrequency(&frequency_li);
+    double frequency = (double)(frequency_li.QuadPart);
+
     while(true) {
         fseek(input, 0L, SEEK_SET);
         size_t inputRead = fread(fileData, sizeof(u8), fileSize, input);
 
+        LARGE_INTEGER start;
+        QueryPerformanceCounter(&start);
+
+        double numFrames = 0;
         u8* byteStrm = fileData;
         u32 len = fileSize;
         u32 bytesRead = 0;
@@ -40,6 +50,12 @@
         while(len > 0) {
             status = h264bsdDecode(decoder, byteStrm, len, 0, &bytesRead);
 
+            if(status == H264BSD_PIC_RDY) {
+                ++numFrames;
+                u32 picId, isIdrPic, numErrMbs;
+                u32* picData = h264bsdNextOutputPictureBGRA(decoder, &picId, &isIdrPic, &numErrMbs);
+            }
+
             if(status == H264BSD_ERROR) {
                 printf("General Error with %i bytes left\n", len);
             }
@@ -60,7 +76,12 @@
         h264bsdShutdown(decoder);
         h264bsdFree(decoder);
 
-        printf("Decode complete\n");
+        LARGE_INTEGER end;
+        QueryPerformanceCounter(&end);
+
+        double decodeTime = (double)(end.QuadPart - start.QuadPart) / frequency;
+
+        printf("Decode completed in %f seconds (%f fps)\n", decodeTime, numFrames/decodeTime);
     }
 
 	return 0;