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
00009 if (ffmpegInitialised) return;
00010
00011
00012 avcodec_init();
00013
00014
00015 avcodec_register_all();
00016
00017
00018 av_register_all();
00019
00020
00021 neutral = (unsigned char *)malloc(NEUTRAL_FRAME_SIZE * sizeof(unsigned char));
00022 memset(neutral, 128, NEUTRAL_FRAME_SIZE);
00023
00024
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
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
00059 pAttribute = pArray->pAttributeList->find("ColorMode");
00060 if (pAttribute) pAttribute->getValue(NDAttrInt32, &colorMode);
00061
00062
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
00074
00075 switch (c->pix_fmt) {
00076 case PIX_FMT_YUV420P:
00077 case PIX_FMT_YUV411P:
00078 case PIX_FMT_YUVJ420P:
00079 case PIX_FMT_NV12:
00080 case PIX_FMT_NV21:
00081 stride = 4;
00082 break;
00083 case PIX_FMT_YUV422P:
00084 case PIX_FMT_YUVJ422P:
00085 case PIX_FMT_YUV440P:
00086 case PIX_FMT_YUVJ440P:
00087 stride = 2;
00088 break;
00089 case PIX_FMT_YUV444P:
00090 case PIX_FMT_YUVJ444P:
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
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
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
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
00138 sws_scale(*pCtx, inPicture->data, inPicture->linesize, 0,
00139 height, scPicture->data, scPicture->linesize);
00140 return(asynSuccess);
00141 }
00142