//#### MJPEG/AVI definitions #include #define YUY2 mmioFOURCC('Y','U','Y','2') #define MJPG mmioFOURCC('M','J','P','G') //#### // #### MJPEG/AVI globals BOOL bMJPEGCapturing; // Capturing status flag HIC hicd; // Handle of the Image Compression Manager for MJPEG Decompression HIC hicc; // Handle of the Image Compression Manager for MJPEG Compression LPBITMAPINFOHEADER pYUVFmt; // Pointer to the capture's YUV format description LPBITMAPINFOHEADER pMJPEGFmt; // Pointer to the Morgan's MJPEG format description LONG lFmtLength; // Length of the Morgan's MJPEG format description PVOID pYUVBuffer; // Pointer to the YUV raw data PVOID pMJPEGBuffer; // Pointer to the MJPEG compressed data LONG lLength; // Length of the MJPEG compressed data LONG lIndex; // Frame Index in the AVI stream LONG lFrames; // Number of frames in the AVI stream OFSTRUCT of; // File struct required to delete AVI file PAVIFILE pfile; // Pointer to the AVI file PAVISTREAM pavivideo; // Pointer to the AVI stream AVISTREAMINFO sivideo; // AVI stream informations DWORD dwPrevIndexCount; // Index count where previous frame has been stored DWORD dwNextIndexCount; // Index count where next frame is going to be stored DWORD dwFlags; // Required for compression DWORD dwCkID; // Required for compression enum {PAL_STANDARD, SECAM_STANDARD, NTSC_STANDARD } int iStandard = PAL_STANDARD; // Video input standard PAL, SECAM, NTSC ... int YUV_Width; // Should be set to width of you YUV image int YUV_Height; // Should be set to height of you YUV image // #### // #### MJPEG/AVI Init Compression & Saving // Open the compressor hicc = ICOpen(ICTYPE_VIDEO, MJPG, ICMODE_COMPRESS); // Get width and height of YUV source image YUV_Width = ; YUV_Height = ; // Get address of YUV raw data buffer pYUVBuffer =
// Set the capture's YUV format pYUVFmt = (LPBITMAPINFOHEADER)malloc(sizeof(BITMAPINFOHEADER)); memset(pYUVFmt, 0, sizeof(BITMAPINFOHEADER)); pYUVFmt->biSize = sizeof( BITMAPINFOHEADER); pYUVFmt->biWidth = YUV_Width; pYUVFmt->biHeight = YUV_Height; pYUVFmt->biPlanes = 1; pYUVFmt->biBitCount = 16; pYUVFmt->biCompression = YUY2; // Get the Morgan's MJPEG format for this capture's YUV format lFmtLength = ICCompressGetFormat(hicc, pYUVFmt, NULL); pMJPEGFmt = (LPBITMAPINFOHEADER)malloc(lFmtLength); ICCompressGetFormat(hicc, pYUVFmt, pMJPEGFmt); // Init AVIFile functions AVIFileInit(); // Delete the previously captured file OpenFile("c:\\capture.avi", &of, OF_DELETE); // Create and open the the capture file AVIFileOpen(&pfile, "c:\\capture.avi", OF_CREATE | OF_WRITE | OF_SHARE_DENY_NONE, NULL); // Set the AVI stream information sivideo.fccType = streamtypeVIDEO; sivideo.fccHandler = MJPG; sivideo.dwFlags = 0; sivideo.dwCaps = 0; sivideo.wPriority = 0; sivideo.wLanguage = 0; if ((iStandard == PAL_STANDARD) || (iStandard == SECAM_STANDARD)) { // PAL/SECAM 25 fps sivideo.dwScale = 1; sivideo.dwRate = 25; } else { // NTSC 29.97 fps sivideo.dwScale = 1001; sivideo.dwRate = 30000; } sivideo.dwStart = 0; sivideo.dwLength = 0; sivideo.dwInitialFrames = 0; sivideo.dwSuggestedBufferSize = 0; sivideo.dwQuality = (DWORD)-1; sivideo.dwSampleSize = 0; sivideo.rcFrame.left = 0; sivideo.rcFrame.top = 0; sivideo.rcFrame.right = 0; sivideo.rcFrame.bottom = 0; sivideo.dwEditCount = 0; sivideo.dwFormatChangeCount = 0; sivideo.szName[0] = 0; // Create the AVI stream AVIFileCreateStream(pfile, &pavivideo, &sivideo); // Set the MJPEG format in the AVI stream AVIStreamSetFormat(pavivideo, 0, pMJPEGFmt, lFmtLength); // Get the current frame index in the AVI stream (0) lIndex = AVIStreamLength(pavivideo); // Allocate the MJPEG compressed data buffer pMJPEGBuffer = malloc(YUV_Width * YUV_Height * 2); // Init for event function bMJPEGCapturing = TRUE; dwNextIndexCount = 0; dwPrevIndexCount = 0; // #### // #### MJPEG/AVI Do Compression & Saving if (bMJPEGCapturing) { // Compress the previous frame has been strored in memory ICCompress(hicc, ICCOMPRESS_KEYFRAME, pMJPEGFmt, pMJPEGBuffer, pYUVFmt, pYUVBuffer, &dwCkID, &dwFlags, lIndex, 0, 7500, // Quality * 100 NULL, NULL); // Save this compressed frame in the AVI stream AVIStreamWrite(pavivideo, lIndex++, 1, pMJPEGBuffer, pMJPEGFmt->biSizeImage, AVIIF_KEYFRAME, NULL, NULL); } // #### // #### MJPEG/AVI Suspend Compression & Saving // will suspend capture in the event function bMJPEGCapturing = FALSE; // #### // #### MJPEG/AVI Done Compression & Saving MessageBox( hWnd, "C:\\capture.avi captured !!!\n Select Display->Play Video to Play it back. ", "Info", MB_OK); // Close the compressor ICClose(hicc); hicc = 0; // Release the AVI stream, file and AVIFile functions AVIStreamRelease(pavivideo); AVIFileRelease(pfile); AVIFileExit(); // Free YUV & MJPEG formats & buffer free(pYUVFmt); free(pMJPEGFmt); free(pMJPEGBuffer); // #### // #### MJPEG/AVI Init Reading & Decompression // Open the decompressor hicd = ICOpen(ICTYPE_VIDEO, MJPG, ICMODE_DECOMPRESS); // Set the capture's YUV format pYUVFmt = (LPBITMAPINFOHEADER)malloc(sizeof(BITMAPINFOHEADER)); memset(pYUVFmt, 0, sizeof(BITMAPINFOHEADER)); pYUVFmt->biSize = sizeof( BITMAPINFOHEADER); pYUVFmt->biWidth = YUV_Width; pYUVFmt->biHeight = YUV_Height; pYUVFmt->biPlanes = 1; pYUVFmt->biBitCount = 16; pYUVFmt->biCompression = YUY2; // Init AVIFile functions AVIFileInit(); // Open the AVI file & stream pavivideo = NULL; AVIStreamOpenFromFile(&pavivideo, "c:\\capture.avi", streamtypeVIDEO, 0, OF_READ, NULL); if (!pavivideo) return(-1); // Get the MJPEG AVI format AVIStreamFormatSize(pavivideo, 0, &lFmtLength); pMJPEGFmt = (LPBITMAPINFOHEADER)malloc(lFmtLength); AVIStreamReadFormat(pavivideo, 0, pMJPEGFmt, &lFmtLength); // make sure to request the real output size to the codec pMJPEGFmt->biWidth = pYUVFmt->biWidth; pMJPEGFmt->biHeight = pYUVFmt->biHeight; // Allocate the MJPEG compressed data buffer lLength = YUV_Width * YUV_Height * 2; lFrames = AVIStreamLength(pavivideo); pMJPEGBuffer = malloc(lLength); pYUVBuffer = malloc(lLength); // #### // #### MJPEG/AVI Reading & Decompression // Read a compressed frame, fill in the MJPEG compressed data buffer AVIStreamRead(pavivideo, i, 1, pMJPEGBuffer, lLength, NULL, NULL); // Decompress this frame ICDecompress(hicd, i, pMJPEGFmt, pMJPEGBuffer, pYUVFmt, pYUVBuffer); // #### // #### MJPEG/AVI Done Reading & Decompression // Close decompressor ICClose(hicd); hicd = 0; // Close AVI file & stream AVIStreamRelease(pavivideo); // Done AVIFile functions AVIFileExit(); // Free allocated memory ... free(pYUVFmt); free(pYUVBuffer); free(pMJPEGFmt); free(pMJPEGBuffer); // ####