00001
00002 #include "ffmpegCommon.h"
00003 #include "ffmpegFile.h"
00004
00005 #include <epicsExport.h>
00006 #include <iocsh.h>
00007
00008 static const char *driverName2 = "ffmpegFile";
00009 #define MAX_ATTRIBUTE_STRING_SIZE 256
00010
00018
00019
00020 asynStatus ffmpegFile::openFile(const char *fileName, NDFileOpenMode_t openMode, NDArray *pArray)
00021 {
00022 static const char *functionName = "openFile";
00023 this->sheight = 0;
00024 this->swidth = 0;
00025
00026
00027 if (openMode & NDFileModeRead) return(asynError);
00028
00029
00030 if (openMode & NDFileModeAppend) return(asynError);
00031
00032
00033 enum CodecID codecID = av_guess_image2_codec(fileName);
00034 if (codecID != CODEC_ID_NONE) {
00035
00036 this->supportsMultipleArrays = 0;
00037 fmt = av_guess_format("image2", fileName, NULL);
00038 codec = avcodec_find_encoder(codecID);
00039 if (codec) {
00040 fmt->video_codec = codecID;
00041 }
00042 } else {
00043
00044 this->supportsMultipleArrays = 1;
00045 fmt = av_guess_format(NULL, fileName, NULL);
00046
00047
00048 if (!fmt) {
00049 printf("Could not deduce output format from file extension: using MPEG.\n");
00050 fmt = av_guess_format("avi", NULL, NULL);
00051 }
00052 }
00053 if (!fmt) {
00054 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00055 "%s:%s Could not find suitable output format for '%s'\n",
00056 driverName2, functionName, fileName);
00057 return(asynError);
00058 }
00059
00060
00061 if (av_match_ext(fileName, "avi") && fmt->video_codec == CODEC_ID_MPEG4) {
00062 fmt->video_codec = CODEC_ID_MSMPEG4V2;
00063 }
00064
00065
00066 oc = avformat_alloc_context();
00067 if (!oc) {
00068 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00069 "%s:%s Memory error: Cannot allocate context\n",
00070 driverName2, functionName);
00071 return(asynError);
00072 }
00073 oc->oformat = fmt;
00074 snprintf(oc->filename, sizeof(oc->filename), "%s", fileName);
00075
00076
00077
00078 if (fmt->video_codec == CODEC_ID_NONE) {
00079 video_st = NULL;
00080 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00081 "%s:%s Selected codec cannot write video\n",
00082 driverName2, functionName);
00083 return(asynError);
00084 } else {
00085 video_st = av_new_stream(oc, 0);
00086 if (!video_st) {
00087 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00088 "%s:%s Could not alloc stream\n",
00089 driverName2, functionName);
00090 return(asynError);
00091 }
00092 avcodec_get_context_defaults2(video_st->codec, AVMEDIA_TYPE_VIDEO);
00093 codec = avcodec_find_encoder(fmt->video_codec);
00094 if (!codec) {
00095 printf("Codec cannot be found, trying msmpeg4v2\n");
00096 codec = avcodec_find_encoder_by_name("msmpeg4v2");
00097 }
00098 if (!codec) {
00099 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00100 "%s:%s Could not open any codecs\n",
00101 driverName2, functionName);
00102 return(asynError);
00103 }
00104 c = video_st->codec;
00105 c->codec_id = codec->id;
00106 c->codec_type = AVMEDIA_TYPE_VIDEO;
00107
00108
00109 getIntegerParam(0, ffmpegFileBitrate, &(c->bit_rate));
00110
00111
00112 AVRational avr;
00113 avr.num = 1;
00114 getIntegerParam(0, ffmpegFileFPS, &(avr.den));
00115
00116
00117 getIntegerParam(0, ffmpegFileWidth, &(c->width));
00118 getIntegerParam(0, ffmpegFileHeight, &(c->height));
00119
00120
00121
00122
00123 c->time_base = avr;
00124 c->gop_size = 12;
00125 c->pix_fmt = PIX_FMT_YUV420P;
00126 if(codec && codec->pix_fmts){
00127 const enum PixelFormat *p= codec->pix_fmts;
00128 for(; *p!=-1; p++){
00129 if(*p == c->pix_fmt)
00130 break;
00131 }
00132 if(*p == -1)
00133 c->pix_fmt = codec->pix_fmts[0];
00134 }
00135
00136
00137 if(oc->oformat->flags & AVFMT_GLOBALHEADER)
00138 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
00139
00140 }
00141
00142
00143
00144 if (av_set_parameters(oc, NULL) < 0) {
00145 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00146 "%s:%s Invalid output format parameters\n",
00147 driverName2, functionName);
00148 return(asynError);
00149 }
00150
00151 av_dump_format(oc, 0, fileName, 1);
00152
00153
00154
00155 if (avcodec_open(c, codec) < 0) {
00156 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00157 "%s:%s could not open codec\n",
00158 driverName2, functionName);
00159 return(asynError);
00160 }
00161 outSize = c->width * c->height * 6;
00162
00163
00164 if (scArray) {
00165 scArray->release();
00166 scArray = NULL;
00167 }
00168 if (outArray) {
00169 outArray->release();
00170 outArray = NULL;
00171 }
00172 scArray = this->pNDArrayPool->alloc(1, &outSize, NDInt8, 0, NULL);
00173 outArray = this->pNDArrayPool->alloc(1, &outSize, NDInt8, 0, NULL);
00174 if (scArray == NULL || outArray == NULL) {
00175 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00176 "%s:%s error allocating arrays\n",
00177 driverName2, functionName);
00178 if (scArray) {
00179 scArray->release();
00180 scArray = NULL;
00181 }
00182 if (outArray) {
00183 outArray->release();
00184 outArray = NULL;
00185 }
00186 return(asynError);
00187 }
00188
00189
00190 inPicture = avcodec_alloc_frame();
00191 scPicture = avcodec_alloc_frame();
00192 avpicture_fill((AVPicture *)scPicture,(uint8_t *)scArray->pData,c->pix_fmt,c->width,c->height);
00193
00194 if (!(fmt->flags & AVFMT_NOFILE)) {
00195 if (avio_open(&oc->pb, fileName, AVIO_FLAG_WRITE) < 0) {
00196 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00197 "%s:%s error opening file %s\n",
00198 driverName2, functionName, fileName);
00199 scArray->release();
00200 scArray = NULL;
00201 outArray->release();
00202 outArray = NULL;
00203 return(asynError);
00204 }
00205 }
00206
00207
00208 if (av_write_header(oc) < 0)
00209 printf("Could not write header for output file (incorrect codec parameters ?)");
00210 needStop = 1;
00211 return(asynSuccess);
00212 }
00213
00214
00218 asynStatus ffmpegFile::writeFile(NDArray *pArray)
00219 {
00220
00221 static const char *functionName = "writeFile";
00222 int ret;
00223
00224 if (!needStop) {
00225 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00226 "%s:%s: video stream not setup properly\n",
00227 driverName2, functionName);
00228 return(asynError);
00229 }
00230
00231 if (formatArray(pArray, this->pasynUserSelf, this->inPicture,
00232 &(this->ctx), this->c, this->scPicture) != asynSuccess) {
00233 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00234 "%s:%s: Could not format array for correct pix_fmt for codec\n",
00235 driverName2, functionName);
00236 return(asynError);
00237 }
00238
00239 video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
00240
00241 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
00242
00243
00244 AVPacket pkt;
00245 av_init_packet(&pkt);
00246 printf("How did you manage to get in this mode?\n");
00247 pkt.stream_index= video_st->index;
00248 pkt.data= (uint8_t *)scPicture;
00249 pkt.size= sizeof(AVPicture);
00250
00251 ret = av_interleaved_write_frame(oc, &pkt);
00252 } else {
00253
00254 int out_size = avcodec_encode_video(c, (uint8_t *) outArray->pData, outSize, scPicture);
00255
00256 if (out_size > 0) {
00257 AVPacket pkt;
00258 av_init_packet(&pkt);
00259
00260 if (c->coded_frame->pts != (int64_t) AV_NOPTS_VALUE)
00261 pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
00262 if(c->coded_frame->key_frame)
00263 pkt.flags |= AV_PKT_FLAG_KEY;
00264 pkt.stream_index= video_st->index;
00265 pkt.data= (uint8_t *) outArray->pData;
00266 pkt.size= out_size;
00267
00268
00269 ret = av_interleaved_write_frame(oc, &pkt);
00270 } else {
00271 ret = 0;
00272 }
00273 }
00274 if (ret != 0) {
00275 asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
00276 "%s:%s Error while writing video frame\n",
00277 driverName2, functionName);
00278 }
00279 return(asynSuccess);
00280 }
00281
00285 asynStatus ffmpegFile::readFile(NDArray **pArray)
00286 {
00287
00288
00289 return asynError;
00290 }
00291
00292
00294 asynStatus ffmpegFile::closeFile()
00295 {
00296 if (needStop == 0) {
00297 return asynError;
00298 }
00299 needStop = 0;
00300
00301
00302
00303
00304
00305 av_write_trailer(oc);
00306
00307
00308 if (video_st) {
00309 avcodec_close(video_st->codec);
00310 }
00311
00312
00313 for(unsigned int i = 0; i < oc->nb_streams; i++) {
00314 av_freep(&oc->streams[i]->codec);
00315 av_freep(&oc->streams[i]);
00316 }
00317
00318 if (!(fmt->flags & AVFMT_NOFILE)) {
00319
00320 avio_close(oc->pb);
00321 }
00322
00323
00324 av_free(oc);
00325 if (scArray) {
00326 scArray->release();
00327 scArray = NULL;
00328 }
00329 if (outArray) {
00330 outArray->release();
00331 outArray = NULL;
00332 }
00333
00334 av_free(inPicture);
00335 av_free(scPicture);
00336 return asynSuccess;
00337 }
00338
00355 ffmpegFile::ffmpegFile(const char *portName, int queueSize, int blockingCallbacks,
00356 const char *NDArrayPort, int NDArrayAddr,
00357 int priority, int stackSize)
00358
00359
00360
00361
00362
00363 : NDPluginFile(portName, queueSize, blockingCallbacks,
00364 NDArrayPort, NDArrayAddr, 1, NUM_FFMPEG_FILE_PARAMS,
00365 2, -1, asynGenericPointerMask, asynGenericPointerMask,
00366 ASYN_CANBLOCK, 1, priority, stackSize < 128000 ? 128000 : stackSize)
00367 {
00368
00369
00370 createParam(ffmpegFileBitrateString, asynParamInt32, &ffmpegFileBitrate);
00371 createParam(ffmpegFileFPSString, asynParamInt32, &ffmpegFileFPS);
00372 createParam(ffmpegFileHeightString, asynParamInt32, &ffmpegFileHeight);
00373 createParam(ffmpegFileWidthString, asynParamInt32, &ffmpegFileWidth);
00374
00375
00376 setStringParam(NDPluginDriverPluginType, "ffmpegFile");
00377 this->supportsMultipleArrays = 0;
00378
00379
00380 ffmpegInitialise();
00381 this->ctx = NULL;
00382 this->outFile = NULL;
00383 this->codec = NULL;
00384 this->c = NULL;
00385 this->inPicture = NULL;
00386 this->scPicture = NULL;
00387 this->scArray = NULL;
00388 this->outArray = NULL;
00389 this->ctx = NULL;
00390 this->fmt = NULL;
00391 this->oc = NULL;
00392 this->video_st = NULL;
00393
00394 }
00395
00401 extern "C" int ffmpegFileConfigure(const char *portName, int queueSize, int blockingCallbacks,
00402 const char *NDArrayPort, int NDArrayAddr,
00403 int priority, int stackSize)
00404 {
00405 new ffmpegFile(portName, queueSize, blockingCallbacks, NDArrayPort, NDArrayAddr,
00406 priority, stackSize);
00407 return(asynSuccess);
00408 }
00409
00410
00411
00412
00413 static const iocshArg initArg0 = { "portName",iocshArgString};
00414 static const iocshArg initArg1 = { "frame queue size",iocshArgInt};
00415 static const iocshArg initArg2 = { "blocking callbacks",iocshArgInt};
00416 static const iocshArg initArg3 = { "NDArray Port",iocshArgString};
00417 static const iocshArg initArg4 = { "NDArray Addr",iocshArgInt};
00418 static const iocshArg initArg5 = { "priority",iocshArgInt};
00419 static const iocshArg initArg6 = { "stack size",iocshArgInt};
00420 static const iocshArg * const initArgs[] = {&initArg0,
00421 &initArg1,
00422 &initArg2,
00423 &initArg3,
00424 &initArg4,
00425 &initArg5,
00426 &initArg6};
00427 static const iocshFuncDef initFuncDef = {"ffmpegFileConfigure",7,initArgs};
00428 static void initCallFunc(const iocshArgBuf *args)
00429 {
00430 ffmpegFileConfigure(args[0].sval, args[1].ival, args[2].ival, args[3].sval, args[4].ival, args[5].ival, args[6].ival);
00431 }
00432
00433 extern "C" void ffmpegFileRegister(void)
00434 {
00435 iocshRegister(&initFuncDef,initCallFunc);
00436 }
00437
00438 extern "C" {
00439 epicsExportRegistrar(ffmpegFileRegister);
00440 }