ffmpegCommon.cpp

Go to the documentation of this file.
00001 #include "ffmpegCommon.h"
00002 
00003 static int ffmpegInitialised = 0;
00004 static unsigned char *neutral = NULL;
00005 
00007 void ffmpegInitialise() {
00008         /* check if we're already intialised */
00009         if (ffmpegInitialised) return;
00010         
00011     /* must be called before using avcodec lib */
00012     avcodec_init();
00013 
00014     /* register all the codecs */
00015     avcodec_register_all();
00016 
00017         /* start the library */
00018     av_register_all();  
00019     
00020     /* Make a big neutral array */
00021     neutral = (unsigned char *)malloc(NEUTRAL_FRAME_SIZE *  sizeof(unsigned char));
00022     memset(neutral, 128, NEUTRAL_FRAME_SIZE);           
00023          
00024     /* say that we're initialise */
00025         ffmpegInitialised = 1;
00026 }    
00027 
00033 int formatArray(NDArray *pArray, asynUser *pasynUser, AVFrame *inPicture,
00034                 struct SwsContext **pCtx, AVCodecContext *c, AVFrame *scPicture) {
00035     static const char *functionName = "formatArray";
00036     int colorMode = NDColorModeMono;
00037         int width, height;
00038         PixelFormat pix_fmt;
00039         NDAttribute *pAttribute = NULL;
00040         int Int16;
00041         
00042         /* only support 8 and 16 bit data for now */
00043     switch (pArray->dataType) {
00044         case NDInt8:
00045         case NDUInt8:
00046             Int16 = 0;
00047             break;
00048         case NDInt16:
00049         case NDUInt16:       
00050             Int16 = 1; 
00051             break;
00052         default:
00053             asynPrint(pasynUser, ASYN_TRACE_ERROR, 
00054                 "%s: only 8 or 16-bit data is supported\n", functionName);
00055                 return(asynError);
00056     }   
00057 
00058         /* Get the colormode of the array */    
00059     pAttribute = pArray->pAttributeList->find("ColorMode");
00060     if (pAttribute) pAttribute->getValue(NDAttrInt32, &colorMode);
00061 
00062     /* We do some special treatment based on colorMode */
00063     if ((pArray->ndims == 2) && (colorMode == NDColorModeMono)) {
00064         width  = pArray->dims[0].size;
00065         height = pArray->dims[1].size;
00066         if (Int16) {
00067             pix_fmt = PIX_FMT_GRAY16;
00068         } else if (width != c->width || height != c->height) {
00069                 pix_fmt = PIX_FMT_GRAY8;
00070         } else {
00071                 int stride;
00072             pix_fmt = PIX_FMT_GRAY8;
00073             /* special case for gray8, and planar outputs, don't need to scale, 
00074                can use a neutral array */
00075             switch (c->pix_fmt) {
00076                         case PIX_FMT_YUV420P:   //< planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
00077                         case PIX_FMT_YUV411P:   //< planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
00078                         case PIX_FMT_YUVJ420P:  //< planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_range
00079                         case PIX_FMT_NV12:      //< planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (first byte U and the following byte V)
00080                         case PIX_FMT_NV21:      //< as above, but U and V bytes are swapped
00081                                 stride = 4;
00082                                 break;
00083                         case PIX_FMT_YUV422P:   //< planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
00084                         case PIX_FMT_YUVJ422P:  //< planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_range
00085                         case PIX_FMT_YUV440P:   //< planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
00086                         case PIX_FMT_YUVJ440P:  //< planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range                          
00087                                 stride = 2;
00088                                 break;                                  
00089                         case PIX_FMT_YUV444P:   //< planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)                       
00090                         case PIX_FMT_YUVJ444P:  //< planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_range
00091                                         stride = 1;
00092                                 break;  
00093                         default:
00094                                 stride = 0;
00095                                 break;                          
00096                         }                       
00097                         if (stride) {   
00098                     scPicture->data[0] = (uint8_t*) pArray->pData;
00099                     scPicture->data[1] = (uint8_t*) neutral;
00100                     scPicture->data[2] = (uint8_t*) neutral;                                
00101                         scPicture->linesize[0] = width;
00102                         scPicture->linesize[1] = width / stride;
00103                         scPicture->linesize[2] = width / stride;
00104                                 return(asynSuccess);                    
00105                     }
00106                 }
00107         /* setup the input picture */
00108         inPicture->data[0] = (uint8_t*) pArray->pData;
00109         inPicture->linesize[0] = width * (Int16 + 1);
00110     } else if ((pArray->ndims == 3) && (pArray->dims[0].size == 3) && (colorMode == NDColorModeRGB1)) {
00111         width  = pArray->dims[1].size;
00112         height = pArray->dims[2].size;
00113         if (Int16) {
00114             pix_fmt = PIX_FMT_RGB48;
00115         } else {
00116             pix_fmt = PIX_FMT_RGB24;
00117         }    
00118         /* setup the input picture */
00119         inPicture->data[0] = (uint8_t*) pArray->pData;
00120         inPicture->linesize[0] = width * (Int16 + 1) * 3;          
00121     } else {
00122         asynPrint(pasynUser, ASYN_TRACE_ERROR, 
00123             "%s: unsupported array structure\n", functionName);
00124         return(asynError);
00125     }
00126 
00127         /* setup the swscale ctx */   
00128         *pCtx = sws_getCachedContext(*pCtx, width, height, pix_fmt,
00129                                   c->width, c->height, c->pix_fmt,
00130                                   SWS_BICUBIC, NULL, NULL, NULL);   
00131                                   
00132     if (*pCtx == NULL) {
00133         asynPrint(pasynUser, ASYN_TRACE_ERROR, 
00134             "%s: sws_getCachedContext failed\n", functionName);
00135         return(asynError);
00136     }
00137     /* scale the picture so we can pass it to the encoder */
00138     sws_scale(*pCtx, inPicture->data, inPicture->linesize, 0,
00139                       height, scPicture->data, scPicture->linesize);
00140         return(asynSuccess);
00141 }
00142         
 All Classes Files Functions Variables Defines