void Y_av_create(int argc) { static long default_params[]= {YAV_BIT_RATE, YAV_FRAME_RATE, YAV_GOP_SIZE, YAV_MAX_B_FRAMES}; long *params=default_params; // PARSE ARGUMENTS: SEPARATE KEYWORDS FROM POSITIONAL ARGUMENTS static char * knames[] = { "vcodec", "pix_fmt", "b", "r", "g", "bf", 0 }; #define YAC_CREATE_NKW 6 static long kglobs[YAC_CREATE_NKW+1]; int kiargs[YAC_CREATE_NKW]; int piargs[]={-1, -1}; yarg_kw_init(knames, kglobs, kiargs); int iarg=argc-1, parg=0; while (iarg>=0) { iarg = yarg_kw(iarg, kglobs, kiargs); if (iarg>=0) { if (parg<2) piargs[parg++]=iarg--; else y_error("av_create takes at most 2 positional arguments"); } } // INTERPRET POSITIONAL ARGUMENTS // filename (mandatory) if ((iarg=piargs[0])<0) y_error("FILENAME must be specified"); char *filename = ygets_q(iarg); // params vector (optional) if (yav_arg_set(iarg=piargs[1])) { long ntot ; long dims[Y_DIMSIZE]={0,0}; params = ygeta_l(iarg, &ntot, dims); if (dims[0]!=1 || dims[1]!=4) y_error("bad dimensions for PARAMS vector"); if (params[0]<0 || params[1]<0 || params[2]<0) y_error("bad values in PARAMS vector"); } // INTERPRET KEYWORD ARGUMENTS char* vcodec = NULL, *pix_fmt = NULL; int k=0; if (yav_arg_set(iarg=kiargs[k++])) vcodec = ygets_q(iarg); // vcodec if (yav_arg_set(iarg=kiargs[k++])) pix_fmt = ygets_q(iarg); // pix_fmt if (yav_arg_set(iarg=kiargs[k++])) params[0] = ygets_l(iarg); // b if (yav_arg_set(iarg=kiargs[k++])) params[1] = ygets_l(iarg); // r if (yav_arg_set(iarg=kiargs[k++])) params[2] = ygets_l(iarg); // g if (yav_arg_set(iarg=kiargs[k++])) params[3] = ygets_l(iarg); // bf // PUSH RETURN VALUE yav_ctxt * obj = ypush_av(); /* allocate the output media context */ obj->oc = avformat_alloc_context(); if (!obj->oc) { y_error("Memory error"); } /* auto detect the output format from the name. default is mpeg. */ obj->oc->oformat = av_guess_format(NULL, filename, NULL); if (!obj->oc->oformat) { y_warn("Could not deduce output format from file extension: using MPEG."); obj->oc->oformat = av_guess_format("mpeg", NULL, NULL); } if (!obj->oc->oformat) { y_error("Could not find suitable output format."); } snprintf(obj->oc->filename, sizeof(obj->oc->filename), "%s", filename); /* add the audio and video streams using the default format codecs and initialize the codecs */ obj->video_st = NULL; // audio_st = NULL; if (obj->oc->oformat->video_codec != AV_CODEC_ID_NONE) { AVCodecContext *c; obj->video_st = avformat_new_stream(obj->oc, NULL); c = obj->video_st->codec; if (vcodec) { obj->codec = avcodec_find_encoder_by_name(vcodec); if (!obj->codec) y_error("can't find requested codec"); c->codec_id = obj->codec->id; } else { c->codec_id = obj->oc->oformat->video_codec; obj->codec = avcodec_find_encoder(c->codec_id); if (!obj->codec) y_error("default codec not found"); } c->codec_type = AVMEDIA_TYPE_VIDEO; avcodec_get_context_defaults3(c, obj->codec); if (c->codec_id == AV_CODEC_ID_NONE) c->codec_id = obj->codec->id; /* put sample parameters */ c->width = 0; c->height = 0; c->pix_fmt = pix_fmt ? av_get_pix_fmt(pix_fmt) : YAV_PIX_FMT; c->bit_rate = params[0] ? params[0] : YAV_BIT_RATE; c->time_base.den = params[1] ? params[1] : YAV_FRAME_RATE; c->time_base.num = 1; c->gop_size = params[2] ? params[2] : YAV_GOP_SIZE; c->max_b_frames = (params[3]>=0) ? params[3] : YAV_MAX_B_FRAMES; if(obj->oc->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; // codec-specific limitations switch (c->codec_id) { case AV_CODEC_ID_RAWVIDEO: case AV_CODEC_ID_GIF: if (!pix_fmt) c->pix_fmt = AV_PIX_FMT_RGB24; break; case AV_CODEC_ID_MSMPEG4V3: case AV_CODEC_ID_H263: case AV_CODEC_ID_H263P: case AV_CODEC_ID_RV10: case AV_CODEC_ID_RV20: case AV_CODEC_ID_FLV1: case AV_CODEC_ID_ASV1: case AV_CODEC_ID_ASV2: c->max_b_frames = 0; break; default:; } } if (!(obj->oc->oformat->flags & AVFMT_RAWPICTURE)) { obj->video_outbuf_size = 200000; obj->video_outbuf = av_malloc(obj->video_outbuf_size); } }
void camera_eval(void *obj, int argc) { tCamera *camera = (tCamera *)obj; try { ystring_t func = ygets_q(argc-1); if(strcmp(func, "start")==0){ long packetSize=8228; if(argc >1) packetSize = ygets_l(argc-2); cameraStart(camera, packetSize); cameraSnap(camera); } else if(strcmp(func, "stop")==0){ cameraStop(camera); } else if(strcmp(func, "getROI")==0){ long dims[2]={1, 4}; long *result = ypush_l(dims); tPvUint32 ROI[4]; cameraGetROI(camera, ROI); for(int i=0;i<4;i++) result[i] = ROI[i]; } else if(strcmp(func, "setROI")==0){ long ntot, dims; long *result = ygeta_l(argc-2, &ntot, &dims); tPvUint32 ROI[4]; for(int i=0;i<4;i++) ROI[i]=result[i]; cameraSetROI(camera, ROI); } else if(strcmp(func, "getExpo")==0){ tPvUint32 expo; cameraGetExpo(camera, &expo); ypush_long(expo); } else if(strcmp(func, "setExpo")==0){ long expo = ygets_l(argc-2); cameraSetExpo(camera, expo); } else if(strcmp(func, "fastsnap")==0){ long param = ygets_l(argc-2); unsigned long width = camera->Frame.Width; // Image width unsigned long height= camera->Frame.Height; // Image height long dims[4]={3, width, height, param}; if(param ==1) dims[0]=2; char resValue[16]; cameraGetPixelFormat(camera, resValue, 16); tPvFrame *buff = cameraSnap(camera, param); if(strcmp(resValue, "Mono8")==0) { short *frame = ypush_s(dims); for(long pose=0; pose<param; pose++){ unsigned char *src = (unsigned char *)buff[pose].ImageBuffer; short *dst = &frame[pose*height*width]; for(unsigned long i=0; i<width*height; i++) { dst[i] = src[i]; } } } else if(strcmp(resValue, "Mono16")==0) { int *frame = ypush_i(dims); for(long pose=0; pose<param; pose++){ unsigned short *src = (unsigned short *)buff[pose].ImageBuffer; int *dst = &frame[pose*height*width]; for(unsigned long i=0; i<width*height; i++) { dst[i] = src[i]; } } } else cout << "bad ppixel size !" << endl; } else if(strcmp(func, "snap")==0){ long param = ygets_l(argc-2); unsigned long width = camera->Frame.Width; // Image width unsigned long height= camera->Frame.Height; // Image height long dims[4]={3, width, height, param}; if(param ==1) dims[0]=2; char resValue[16]; cameraGetPixelFormat(camera, resValue, 16); if(strcmp(resValue, "Mono8")==0) { short *frame = ypush_s(dims); for(long pose=0; pose<param; pose++){ cameraSnap(camera); unsigned char *src = (unsigned char *)camera->Frame.ImageBuffer; short *dst = &frame[pose*height*width]; for(unsigned long i=0; i<width*height; i++) { dst[i] = src[i]; } } } else if(strcmp(resValue, "Mono16")==0) { int *frame = ypush_i(dims); for(long pose=0; pose<param; pose++){ cameraSnap(camera); unsigned short *src = (unsigned short *)camera->Frame.ImageBuffer; int *dst = &frame[pose*height*width]; for(unsigned long i=0; i<width*height; i++) { dst[i] = src[i]; } } } else cout << "bad ppixel size !" << endl; } else cout << "F**K !" << endl; } catch ( string msg ) { y_error(msg.c_str()); } catch ( char const * msg ) { y_error(msg); } }