Main Page | Modules | Class Hierarchy | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

jp2_encodeMT.cpp

Simple multi-threaded image encoder using CMJ2EncMT.

//
// jp2_encodeMT.cpp
//
// Simple multi-threaded image encoder using CMJ2EncMT.
//
// Copyright Morgan-Multimedia 1990-2005.
//

#include "iMJ2Enc.h"
#include "OEM_LIC.h"

int Encode2Fields(int nWidth, int nHeight, PBYTE pIn, int nIn, DWORD fccIn, int nInBits, 
                                  PBYTE pOut1, int &nOut1, PBYTE pOut2, int &nOut2, int nQuality)
{
        // Count CPUs
        SYSTEM_INFO SysInfo;
        GetSystemInfo(&SysInfo);

        // Create an encoder, bInterlaced=TRUE, bMT=TRUE
        CMJ2EncMT *pEnc = new CMJ2EncMT(TRUE, TRUE, SysInfo.dwNumberOfProcessors/2, &OEM_LIC);

        if (!pEnc) {return 0;}

        // Is license valid ?
        if (!pEnc->IsLicenseValid())
        {
                // No
                delete pEnc;
                return 0;
        }

        // Initialize a JPEG2000INFOHEADER2
        JPEG2000INFOHEADER2 jp2;
        switch (fccIn)
        {
                case YUY2:
                case UYVY:
                InitJP2KHdrYUV422(&jp2);
                break;

                case YV12:
                case I420:
                InitJP2KHdrYUV420(&jp2);
                break;

                case BI_RGB:
                case BI_BITFIELDS:
                if (nInBits == 8)
                {
                        InitJP2KHdrY(&jp2);
                }
                else
                {
                        InitJP2KHdrRGB(&jp2);
                }
                break;
        }

        // Prepare
        pEnc->prepare(
                                &jp2, 0, NULL,
                                pIn, nIn,
                                0, 0,
                                nWidth, nHeight,
                                fccIn, nInBits,
                                pOut1, nOut1,
                                nQuality,
                                0);

        // Skip first line
        int ofs = WIDTHBYTES(nWidth * nInBits); // Macro defined in JPEG2000.h

        // Encode
        nOut2 = pEnc->encode(
                                &jp2, 0, NULL,
                                pIn, nIn,
                                ofs, 0,
                                nWidth, nHeight,
                                fccIn, nInBits,
                                pOut2, nOut2,
                                nQuality,
                                0);
        // Sync
        nOut1 = pEnc->sync();

        // Delete encoder
        if (pEnc) {delete pEnc;}

        // Return the total size
        return nOut1 + nOut2;

        // Note : nOut1 and nOut2 are &int, so return to the caller.
        // On a common implementation jp2 would be allocated outside of
        // this function, in this case you can use jp2.SizeField1 and 
        // jp2.SizeField2 to store frames size.
}

int Encode2Frames(int nWidth, int nHeight, PBYTE pIn1, int nIn1, PBYTE pIn2, int nIn2, DWORD fccIn, int nInBits, 
                                  PBYTE pOut1, int &nOut1, PBYTE pOut2, int &nOut2, int nQuality)
{
        // Count CPUs
        SYSTEM_INFO SysInfo;
        GetSystemInfo(&SysInfo);

        // Create an encoder, bInterlaced=FALSE, bMT=TRUE
        CMJ2EncMT *pEnc = new CMJ2EncMT(FALSE, TRUE, SysInfo.dwNumberOfProcessors/2, &OEM_LIC);

        if (!pEnc) {return 0;}

        // Is license valid ?
        if (!pEnc->IsLicenseValid())
        {
                // No
                delete pEnc;
                return 0;
        }

        // Initialize a JPEG2000INFOHEADER2
        JPEG2000INFOHEADER2 jp2;
        switch (fccIn)
        {
                case YUY2:
                case UYVY:
                InitJP2KHdrYUV422(&jp2);
                break;

                case YV12:
                case I420:
                InitJP2KHdrYUV420(&jp2);
                break;

                case BI_RGB:
                case BI_BITFIELDS:
                if (nInBits == 8)
                {
                        InitJP2KHdrY(&jp2);
                }
                else
                {
                        InitJP2KHdrRGB(&jp2);
                }
                break;
        }

        // Prepare
        pEnc->prepare(
                                &jp2, 0, NULL,
                                pIn1, nIn2,
                                0, 0,
                                nWidth, nHeight,
                                fccIn, nInBits,
                                pOut1, nOut1,
                                nQuality,
                                0);

        // Encode
        nOut2 = pEnc->encode(
                                &jp2, 0, NULL,
                                pIn2, nIn2,
                                0, 0,
                                nWidth, nHeight,
                                fccIn, nInBits,
                                pOut2, nOut2,
                                nQuality,
                                0);
        // Sync
        nOut1 = pEnc->sync();

        // Delete encoder
        if (pEnc) {delete pEnc;}

        // Return the total size
        return nOut1 + nOut2;

        // Note : nOut1 and nOut2 are &int, so return to the caller.
        // On a common implementation jp2 would be allocated outside of
        // this function, in this case you can use jp2.SizeField1 and 
        // jp2.SizeField2 to store fields size.
}

// CMJ2EncMT can be used single threaded as well
int EncodeST(int nWidth, int nHeight, PBYTE pIn, int nIn, DWORD fccIn, int nInBits, 
                                  PBYTE pOut, int nOut, int nQuality)
{
        // Create an encoder, bInterlaced=FALSE, bMT=FALSE
        CMJ2EncMT *pEnc = new CMJ2EncMT(FALSE, FALSE, 0, &OEM_LIC);

        if (!pEnc) {return 0;}

        // Is license valid ?
        if (!pEnc->IsLicenseValid())
        {
                // No
                delete pEnc;
                return 0;
        }

        // Initialize a JPEG2000INFOHEADER2
        JPEG2000INFOHEADER2 jp2;
        switch (fccIn)
        {
                case YUY2:
                case UYVY:
                InitJP2KHdrYUV422(&jp2);
                break;

                case YV12:
                case I420:
                InitJP2KHdrYUV420(&jp2);
                break;

                case BI_RGB:
                case BI_BITFIELDS:
                if (nInBits == 8)
                {
                        InitJP2KHdrY(&jp2);
                }
                else
                {
                        InitJP2KHdrRGB(&jp2);
                }
                break;
        }

        // Encode
        int nSize = pEnc->encode(
                                &jp2, 0, NULL,
                                pIn, nIn,
                                0, 0,
                                nWidth, nHeight,
                                fccIn, nInBits,
                                pOut, nOut,
                                nQuality,
                                0);

        // Delete encoder
        if (pEnc) {delete pEnc;}

        // Return the size of the encoded frame
        return nSize;
}

   

© Morgan Multimedia 1990-2005