int main(int argc, char *argv[]) { AVFormatContext *pFormatCtx = NULL; int i, videoStream; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVFrame *pFrameRGB = NULL; AVPacket packet; int frameFinished; int numBytes; uint8_t *buffer = NULL; AVDictionary *optionsDict = NULL; struct SwsContext *sws_ctx = NULL; // Real-Time Setup int do_exit; int count = 0; /* rt_task defined in rt_param.h struct rt_task { lt_t exec_cost; lt_t period; lt_t relative_deadline; lt_t phase; unsigned int cpu; unsigned int priority; task_class_t cls; budget_policy_t budget_policy; release_policy_t release_policy; */ struct rt_task param; /* Setup task parameters */ init_rt_task_param(¶m); param.exec_cost = ms2ns(EXEC_COST); param.period = ms2ns(PERIOD); param.relative_deadline = ms2ns(RELATIVE_DEADLINE); /* What to do in the case of budget overruns? */ param.budget_policy = NO_ENFORCEMENT; /* The task class parameter is ignored by most plugins. */ param.cls = RT_CLASS_SOFT; /* The priority parameter is only used by fixed-priority plugins. */ param.priority = LITMUS_LOWEST_PRIORITY; /* The task is in background mode upon startup. */ // END REAL TIME SETUP /***** * 1) Command line paramter parsing would be done here. */ if(argc < 2) { printf("Please provide a movie file\n"); return -1; } /***** * 2) Work environment (e.g., global data structures, file data, etc.) would * be setup here. */ // Register all formats and codecs av_register_all(); // Open video file if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0) return -1; // Couldn't open file // Retrieve stream information if(avformat_find_stream_info(pFormatCtx, NULL)<0) return -1; // Couldn't find stream information // Dump information about file onto standard error av_dump_format(pFormatCtx, 0, argv[1], 0); /***** * End Work Environment Setup */ // Find the first video stream videoStream=-1; for(i=0; i<pFormatCtx->nb_streams; i++) if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) { videoStream=i; break; } if(videoStream==-1) return -1; // Didn't find a video stream // Get a pointer to the codec context for the video stream pCodecCtx=pFormatCtx->streams[videoStream]->codec; // Find the decoder for the video stream pCodec=avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec==NULL) { fprintf(stderr, "Unsupported codec!\n"); return -1; // Codec not found } // Open codec if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0) return -1; // Could not open codec // Allocate video frame pFrame=av_frame_alloc(); // Allocate an AVFrame structure pFrameRGB=av_frame_alloc(); if(pFrameRGB==NULL) return -1; // Determine required buffer size and allocate buffer numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height); buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); sws_ctx = sws_getContext ( pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL ); // Assign appropriate parts of buffer to image planes in pFrameRGB // Note that pFrameRGB is an AVFrame, but AVFrame is a superset // of AVPicture avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height); /***** * 3) Setup real-time parameters. * In this example, we create a sporadic task that does not specify a * target partition (and thus is intended to run under global scheduling). * If this were to execute under a partitioned scheduler, it would be assigned * to the first partition (since partitioning is performed offline). */ CALL( init_litmus() ); // Defined in litmus.h /* To specify a partition, do * * param.cpu = CPU; * be_migrate_to(CPU); * * where CPU ranges from 0 to "Number of CPUs" - 1 before calling * set_rt_task_param(). */ CALL( set_rt_task_param(gettid(), ¶m) ); // Defined in litmus.h /***** * 4) Transition to real-time mode. */ CALL( task_mode(LITMUS_RT_TASK) ); // Defined in litmus.h /* The task is now executing as a real-time task if the call didn't fail. */ // Read frames and save first five frames to disk i=0; while(av_read_frame(pFormatCtx, &packet)>=0) { /* Wait until the next job is released. */ sleep_next_period(); // Print frame number printf("Frame %d\n", i); // Is this a packet from the video stream? if(packet.stream_index==videoStream) { // Decode video frame avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); // Did we get a video frame? if(frameFinished) { // Convert the image from its native format to RGB sws_scale ( sws_ctx, (uint8_t const * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize ); // Save the frame to disk if(++i<=5) SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i); } } // Free the packet that was allocated by av_read_frame av_free_packet(&packet); } /***** * 6) Transition to background mode. */ CALL( task_mode(BACKGROUND_TASK) ); // Free the RGB image av_free(buffer); av_free(pFrameRGB); // Free the YUV frame av_free(pFrame); // Close the codec avcodec_close(pCodecCtx); // Close the video file avformat_close_input(&pFormatCtx); /***** * 7) Clean up, maybe print results and stats, and exit. */ return 0; }
static int job(double exec_time) { loop_for(exec_time); sleep_next_period(); return 0; }
/* A real-time thread is very similar to the main function of a single-threaded * real-time app. Notice, that init_rt_thread() is called to initialized per-thread * data structures of the LITMUS^RT user space libary. */ void* rt_thread(void *tcontext) { int do_exit; struct thread_context *ctx = (struct thread_context *) tcontext; struct rt_task param; /* Set up task parameters */ memset(¶m, 0, sizeof(param)); param.exec_cost = EXEC_COST * NS_PER_MS; param.period = PERIOD * NS_PER_MS; param.relative_deadline = RELATIVE_DEADLINE * NS_PER_MS; /* What to do in the case of budget overruns? */ param.budget_policy = NO_ENFORCEMENT; /* The task class parameter is ignored by most plugins. */ param.cls = RT_CLASS_SOFT; /* The priority parameter is only used by fixed-priority plugins. */ param.priority = LITMUS_LOWEST_PRIORITY; /* Make presence visible. */ printf("RT Thread %d active.\n", ctx->id); /***** * 1) Initialize real-time settings. */ CALL( init_rt_thread() ); /* To specify a partition, do * * param.cpu = CPU; * be_migrate_to(CPU); * * where CPU ranges from 0 to "Number of CPUs" - 1 before calling * set_rt_task_param(). */ CALL( set_rt_task_param(gettid(), ¶m) ); /***** * 2) Transition to real-time mode. */ CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ /***** * 3) Invoke real-time jobs. */ do { /* Wait until the next job is released. */ sleep_next_period(); /* Invoke job. */ do_exit = job(); } while (!do_exit); /***** * 4) Transition to background mode. */ CALL( task_mode(BACKGROUND_TASK) ); return NULL; }
int main(int argc, char** argv) { int do_exit, ret; struct rt_task param; sprintf(myPID,"%d",getpid()); strcat(filePath,myPID); //argv 1. wcet(ms) 2. period(ms) 3. duration(s) 4. mode (1 for cali sar, 4 for cali kalman) 5. appName 6. size/iter wcet_f = atoi(argv[1]); // in ms period_f = atof(argv[2]); // in ms size_iter = atof(argv[6]); wcet_us = (int)(wcet_f*1000); // Convert ms to us // wcet_frac = modf(wcet_f,&int_temp); // wcet_int = (int)(int_temp); dur = 1000 * atoi(argv[3]); // in seconds -> to ms mode = atoi(argv[4]); count = (dur / period_f); // printf("wcet_f: %f\tperiod_f: %f\twcet_us: %ld\tcount: %d\n", // wcet_f,period_f,wcet_us,count); if(argc > 6) { strncpy(myName,argv[5],40); } else { strncpy(myName,"NoNameSet",40); } printf("Name set: %s\n",myName); /* Setup task parameters */ memset(¶m, 0, sizeof(param)); // param.exec_cost = wcet_f * NS_PER_MS; // param.period = period_f * NS_PER_MS; param.exec_cost = wcet_f * NS_PER_MS; param.period = period_f * NS_PER_MS; // printf("param.exec: %ld\tparam.period: %ld\n",param.exec_cost, param.period); // return 0; param.relative_deadline = period_f * NS_PER_MS; /* What to do in the case of budget overruns? */ param.budget_policy = NO_ENFORCEMENT; /* The task class parameter is ignored by most plugins. */ param.cls = RT_CLASS_SOFT; param.cls = RT_CLASS_HARD; /* The priority parameter is only used by fixed-priority plugins. */ param.priority = LITMUS_LOWEST_PRIORITY; /* The task is in background mode upon startup. */ /***** * 1) Command line paramter parsing would be done here. */ /***** * 2) Work environment (e.g., global data structures, file data, etc.) would * be setup here. */ /***** * 3) Setup real-time parameters. * In this example, we create a sporadic task that does not specify a * target partition (and thus is intended to run under global scheduling). * If this were to execute under a partitioned scheduler, it would be assigned * to the first partition (since partitioning is performed offline). */ CALL( init_litmus() ); /* To specify a partition, do * * param.cpu = CPU; * be_migrate_to(CPU); * * where CPU ranges from 0 to "Number of CPUs" - 1 before calling * set_rt_task_param(). */ CALL( set_rt_task_param(gettid(), ¶m) ); /***** * 4) Transition to real-time mode. */ CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ pCtrlPage = get_ctrl_page(); ret = wait_for_ts_release(); if (ret != 0) printf("ERROR: wait_for_ts_release()"); /***** * 5) Invoke real-time jobs. */ do { /* Wait until the next job is released. */ sleep_next_period(); /* Invoke job. */ do_exit = job(); } while (!do_exit); /***** * 6) Transition to background mode. */ CALL( task_mode(BACKGROUND_TASK) ); /***** * 7) Clean up, maybe print results and stats, and exit. */ return 0; }
/* typically, main() does a couple of things: * 1) parse command line parameters, etc. * 2) Setup work environment. * 3) Setup real-time parameters. * 4) Transition to real-time mode. * 5) Invoke periodic or sporadic jobs. * 6) Transition to background mode. * 7) Clean up and exit. * * The following main() function provides the basic skeleton of a single-threaded * LITMUS^RT real-time task. In a real program, all the return values should be * checked for errors. */ int main(int argc, char** argv) { int do_exit; /* The task is in background mode upon startup. */ /***** * 1) Command line paramter parsing would be done here. */ /***** * 2) Work environment (e.g., global data structures, file data, etc.) would * be setup here. */ /***** * 3) Setup real-time parameters. * In this example, we create a sporadic task that does not specify a * target partition (and thus is intended to run under global scheduling). * If this were to execute under a partitioned scheduler, it would be assigned * to the first partition (since partitioning is performed offline). */ CALL( init_litmus() ); CALL( sporadic_global(EXEC_COST, PERIOD) ); /* To specify a partition, use sporadic_partitioned(). * Example: * * sporadic_partitioned(EXEC_COST, PERIOD, CPU); * * where CPU ranges from 0 to "Number of CPUs" - 1. */ /***** * 4) Transition to real-time mode. */ CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ /***** * 5) Invoke real-time jobs. */ do { /* Wait until the next job is released. */ sleep_next_period(); /* Invoke job. */ do_exit = job(); } while (!do_exit); /***** * 6) Transition to background mode. */ CALL( task_mode(BACKGROUND_TASK) ); /***** * 7) Clean up, maybe print results and stats, and exit. */ return 0; }
/* typically, main() does a couple of things: * 1) parse command line parameters, etc. * 2) Setup work environment. * 3) Setup real-time parameters. * 4) Transition to real-time mode. * 5) Invoke periodic or sporadic jobs. * 6) Transition to background mode. * 7) Clean up and exit. * * The following main() function provides the basic skeleton of a single-threaded * LITMUS^RT real-time task. In a real program, all the return values should be * checked for errors. */ int main(int argc, char** argv) { int do_exit, ret; struct rt_task param; wcet = atoi(argv[1]); // in ms period = atoi(argv[2]); // in ms dur = 1000 * atoi(argv[3]); // in seconds count = (dur / period) + 1; /* Setup task parameters */ memset(¶m, 0, sizeof(param)); param.exec_cost = wcet * NS_PER_MS; param.period = period * NS_PER_MS; param.relative_deadline = period * NS_PER_MS; /* What to do in the case of budget overruns? */ param.budget_policy = NO_ENFORCEMENT; /* The task class parameter is ignored by most plugins. */ param.cls = RT_CLASS_SOFT; param.cls = RT_CLASS_HARD; /* The priority parameter is only used by fixed-priority plugins. */ param.priority = LITMUS_LOWEST_PRIORITY; /* The task is in background mode upon startup. */ /***** * 1) Command line paramter parsing would be done here. */ /***** * 2) Work environment (e.g., global data structures, file data, etc.) would * be setup here. */ /***** * 3) Setup real-time parameters. * In this example, we create a sporadic task that does not specify a * target partition (and thus is intended to run under global scheduling). * If this were to execute under a partitioned scheduler, it would be assigned * to the first partition (since partitioning is performed offline). */ CALL( init_litmus() ); /* To specify a partition, do * * param.cpu = CPU; * be_migrate_to(CPU); * * where CPU ranges from 0 to "Number of CPUs" - 1 before calling * set_rt_task_param(). */ CALL( set_rt_task_param(gettid(), ¶m) ); /***** * 4) Transition to real-time mode. */ CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ ret = wait_for_ts_release(); if (ret != 0) printf("ERROR: wait_for_ts_release()"); /***** * 5) Invoke real-time jobs. */ do { /* Wait until the next job is released. */ sleep_next_period(); /* Invoke job. */ do_exit = job(); } while (!do_exit); /***** * 6) Transition to background mode. */ CALL( task_mode(BACKGROUND_TASK) ); /***** * 7) Clean up, maybe print results and stats, and exit. */ return 0; }
/* typically, main() does a couple of things: * 1) parse command line parameters, etc. * 2) Setup work environment. * 3) Setup real-time parameters. * 4) Transition to real-time mode. * 5) Invoke periodic or sporadic jobs. * 6) Transition to background mode. * 7) Clean up and exit. * * The following main() function provides the basic skeleton of a single-threaded * LITMUS^RT real-time task. In a real program, all the return values should be * checked for errors. */ int main(int argc, char** argv) { int do_exit; int PERIOD, EXEC_COST, CPU; int sCounter; int counter; int pCounter; int ret; /* The task is in background mode upon startup. */ /***** * 1) Command line paramter parsing would be done here. */ if (argc !=5){ fprintf(stderr, "Usage: base_task EXEC_COST PERIOD COUNTER CPU \n COUNTER: print info every COUNTER times called \n"); exit(1); } EXEC_COST = atoi(argv[1]); PERIOD = atoi(argv[2]); sCounter = atoi(argv[3]); CPU = atoi(argv[4]); /***** * 2) Work environment (e.g., global data structures, file data, etc.) would * be setup here. */ counter = 0; pCounter = 0; /***** * 3) Setup real-time parameters. * In this example, we create a sporadic task that does not specify a * target partition (and thus is intended to run under global scheduling). * If this were to execute under a partitioned scheduler, it would be assigned * to the first partition (since partitioning is performed offline). */ CALL( init_litmus() ); /* CALL( sporadic_global(EXEC_COST, PERIOD) ); */ /* To specify a partition, use sporadic_partitioned(). * Example: * * sporadic_partitioned(EXEC_COST, PERIOD, CPU); * * where CPU ranges from 0 to "Number of CPUs" - 1. */ CALL( sporadic_partitioned(EXEC_COST, PERIOD, CPU) ); /***** * 4) Transition to real-time mode. */ CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ this_rt_id = gettid(); ret = wait_for_ts_release(); /***** * 5) Invoke real-time jobs. */ do { /* Wait until the next job is released. */ sleep_next_period(); /* Invoke job. */ do_exit = job(&counter, &pCounter, sCounter ); } while (!do_exit); /***** * 6) Transition to background mode. */ CALL( task_mode(BACKGROUND_TASK) ); /***** * 7) Clean up, maybe print results and stats, and exit. */ return 0; }