00001
00030 #include "omxvideoenctest.h"
00031
00033 #define MPEG4_TYPE_SEL 1
00034 #define COMPONENT_NAME_BASE "OMX.st.video_encoder"
00035 #define BASE_ROLE "video_encoder.mpeg4"
00036 #define COMPONENT_NAME_BASE_LEN 20
00037
00038 OMX_CALLBACKTYPE videoenccallbacks = {
00039 .EventHandler = videoencEventHandler,
00040 .EmptyBufferDone = videoencEmptyBufferDone,
00041 .FillBufferDone = videoencFillBufferDone
00042 };
00043
00044 OMX_CALLBACKTYPE videosrccallbacks = {
00045 .EventHandler = videosrcEventHandler,
00046 .EmptyBufferDone = NULL,
00047 .FillBufferDone = videosrcFillBufferDone
00048 };
00049
00051 appPrivateType* appPriv;
00052
00053 char *input_file, *output_file;
00054 FILE *infile,*outfile;
00055 int selectedType = 0;
00056 static OMX_BOOL bEOS = OMX_FALSE;
00057 OMX_U32 in_width = 176;
00058 OMX_U32 in_height = 144;
00059 OMX_U32 frame_rate = 25;
00060 int buffer_in_size = BUFFER_IN_SIZE;
00061 OMX_U32 buffer_out_size = 32768;
00062 OMX_U32 outbuf_colorconv_size;
00063 char codecName[10];
00064
00065 OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
00066
00068 OMX_BUFFERHEADERTYPE *pInBuffer[2], *pOutBuffer[2], *pSrcOutBuffer[2];
00069
00070 OMX_PARAM_PORTDEFINITIONTYPE paramPort;
00071 OMX_PARAM_COMPONENTROLETYPE paramRole;
00072 OMX_VIDEO_PARAM_PORTFORMATTYPE omxVideoParam;
00073
00074 static void setHeader(OMX_PTR header, OMX_U32 size) {
00075 OMX_VERSIONTYPE* ver = (OMX_VERSIONTYPE*)(header + sizeof(OMX_U32));
00076 *((OMX_U32*)header) = size;
00077
00078 ver->s.nVersionMajor = VERSIONMAJOR;
00079 ver->s.nVersionMinor = VERSIONMINOR;
00080 ver->s.nRevision = VERSIONREVISION;
00081 ver->s.nStep = VERSIONSTEP;
00082 }
00083
00084
00085 OMX_ERRORTYPE test_OMX_ComponentNameEnum() {
00086 char * name;
00087 int index;
00088
00089 OMX_ERRORTYPE err = OMX_ErrorNone;
00090
00091 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s\n", __func__);
00092 name = malloc(OMX_MAX_STRINGNAME_SIZE);
00093 index = 0;
00094 while(1) {
00095 err = OMX_ComponentNameEnum (name, OMX_MAX_STRINGNAME_SIZE, index);
00096 if ((name != NULL) && (err == OMX_ErrorNone)) {
00097 DEBUG(DEFAULT_MESSAGES, "component %i is %s\n", index, name);
00098 } else break;
00099 if (err != OMX_ErrorNone) break;
00100 index++;
00101 }
00102 free(name);
00103 name = NULL;
00104 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result %08x\n", __func__, err);
00105 return err;
00106 }
00107
00108 OMX_ERRORTYPE test_OMX_RoleEnum(OMX_STRING component_name) {
00109 OMX_U32 no_of_roles;
00110 OMX_U8 **string_of_roles;
00111 OMX_ERRORTYPE err = OMX_ErrorNone;
00112 int index;
00113
00114 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s\n", __func__);
00115 DEBUG(DEB_LEV_SIMPLE_SEQ, "Getting roles of %s. Passing Null first...\n", component_name);
00116 err = OMX_GetRolesOfComponent(component_name, &no_of_roles, NULL);
00117 if (err != OMX_ErrorNone) {
00118 DEBUG(DEB_LEV_ERR, "Not able to retrieve the number of roles of the given component\n");
00119 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result %i\n", __func__, err);
00120 return err;
00121 }
00122 DEBUG(DEFAULT_MESSAGES, "The number of roles for the component %s is: %i\n", component_name, (int)no_of_roles);
00123
00124 if(no_of_roles == 0) {
00125 DEBUG(DEB_LEV_ERR, "The Number or roles is 0.\nThe component selected is not correct for the purpose of this test.\nExiting...\n");
00126 err = OMX_ErrorInvalidComponentName;
00127 } else {
00128 string_of_roles = (OMX_U8**)malloc(no_of_roles * sizeof(OMX_STRING));
00129 for (index = 0; index < no_of_roles; index++) {
00130 *(string_of_roles + index) = (OMX_U8 *)malloc(no_of_roles * OMX_MAX_STRINGNAME_SIZE);
00131 }
00132 DEBUG(DEB_LEV_SIMPLE_SEQ, "...then buffers\n");
00133
00134 err = OMX_GetRolesOfComponent(component_name, &no_of_roles, string_of_roles);
00135 if (err != OMX_ErrorNone) {
00136 DEBUG(DEB_LEV_ERR, "Not able to retrieve the roles of the given component\n");
00137 } else if(string_of_roles != NULL) {
00138 for (index = 0; index < no_of_roles; index++) {
00139 DEBUG(DEFAULT_MESSAGES, "The role %i for the component: %s \n", (index + 1), *(string_of_roles+index));
00140 }
00141 } else {
00142 DEBUG(DEB_LEV_ERR, "role string is NULL!!! Exiting...\n");
00143 err = OMX_ErrorInvalidComponentName;
00144 }
00145 }
00146 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result %i\n", __func__, err);
00147 return err;
00148 }
00149
00150 OMX_ERRORTYPE test_OMX_ComponentEnumByRole(OMX_STRING role_name) {
00151 OMX_U32 no_of_comp_per_role;
00152 OMX_U8 **string_of_comp_per_role;
00153 OMX_ERRORTYPE err;
00154 int index;
00155
00156 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s\n", __func__);
00157 string_of_comp_per_role = (OMX_U8**) malloc (10 * sizeof(OMX_STRING));
00158 for (index = 0; index < 10; index++) {
00159 string_of_comp_per_role[index] = malloc(OMX_MAX_STRINGNAME_SIZE);
00160 }
00161
00162 DEBUG(DEFAULT_MESSAGES, "Getting number of components per role for %s\n", role_name);
00163
00164 err = OMX_GetComponentsOfRole(role_name, &no_of_comp_per_role, NULL);
00165 if (err != OMX_ErrorNone) {
00166 DEBUG(DEB_LEV_ERR, "Not able to retrieve the number of components of a given role\n");
00167 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result %i\n", __func__, err);
00168 return err;
00169 }
00170 DEBUG(DEFAULT_MESSAGES, "Number of components per role for %s is %i\n", role_name, (int)no_of_comp_per_role);
00171
00172 err = OMX_GetComponentsOfRole(role_name, &no_of_comp_per_role, string_of_comp_per_role);
00173 if (err != OMX_ErrorNone) {
00174 DEBUG(DEB_LEV_ERR, "Not able to retrieve the components of a given role\n");
00175 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result %i\n",__func__, err);
00176 return err;
00177 }
00178
00179 DEBUG(DEFAULT_MESSAGES, " The components are:\n");
00180 for (index = 0; index < no_of_comp_per_role; index++) {
00181 DEBUG(DEFAULT_MESSAGES, "%s\n", string_of_comp_per_role[index]);
00182 }
00183 for (index = 0; index<10; index++) {
00184 if(string_of_comp_per_role[index]) {
00185 free(string_of_comp_per_role[index]);
00186 string_of_comp_per_role[index] = NULL;
00187 }
00188 }
00189
00190 if(string_of_comp_per_role) {
00191 free(string_of_comp_per_role);
00192 string_of_comp_per_role = NULL;
00193 }
00194 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result OMX_ErrorNone\n", __func__);
00195 return OMX_ErrorNone;
00196 }
00197
00198 OMX_ERRORTYPE test_OpenClose(OMX_STRING component_name) {
00199 OMX_ERRORTYPE err = OMX_ErrorNone;
00200
00201 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s\n",__func__);
00202 err = OMX_GetHandle(&appPriv->videoenchandle, component_name, NULL, &videoenccallbacks);
00203 if(err != OMX_ErrorNone) {
00204 DEBUG(DEB_LEV_ERR, "No component found\n");
00205 } else {
00206 err = OMX_FreeHandle(appPriv->videoenchandle);
00207 }
00208 DEBUG(DEFAULT_MESSAGES, "GENERAL TEST %s result %i\n", __func__, err);
00209 return err;
00210 }
00211
00212
00213
00215 int find_encoder(char* searchname) {
00216 char filename_lower[255];
00217 char* ptr = filename_lower;
00218
00219 strcpy(filename_lower, searchname);
00220 while (*ptr != '\0' && ptr - filename_lower < 255) {
00221 *ptr = tolower(*ptr);
00222 ++ptr;
00223 }
00224 if (strstr(filename_lower, "m4v") != NULL) {
00225 strcpy(&codecName[0],"m4v");
00226 return 0;
00227 }
00228 return 1;
00229 }
00230
00232 void display_help() {
00233 printf("\n");
00234 printf("Usage: omxvideoenctest -o outfile [-W 320] [-H 240] [-t] [-C] [-h] [-f input_fmt] -i input_filename\n");
00235 printf("\n");
00236 printf(" -i infile : Input yuv file name\n");
00237 printf(" -o outfile: If this option is specified, the output is written to user specified outfile\n");
00238 printf(" Else, the output is written in the same directory of input file\n");
00239 printf(" the file name looks like input_filename_app.m4v depending on input option\n");
00240 printf("\n");
00241 printf(" -h: Displays this help\n");
00242 printf("\n");
00243 printf(" -f : output codec format \n");
00244 printf(" The available output formats are - \n");
00245 printf(" - m4v \n");
00246 printf(" -W width\n");
00247 printf(" -H Height\n");
00248 printf(" -C use camera as input source\n");
00249 printf(" -r frame per second\n");
00250 printf(" -t use tunnel between video source and encoder\n");
00251 printf("\n");
00252 exit(1);
00253 }
00254
00255 int flagIsOutputExpected;
00256 int flagIsInputExpected;
00257 int flagOutputReceived;
00258 int flagInputReceived;
00259 int flagIsFormatRequested;
00260 int flagIsWidth;
00261 int flagIsHeight;
00262 int flagIsCameraRequested;
00263 int flagSetupTunnel;
00264 int flagIsFPS;
00265
00267 int setPortParameters() {
00268 OMX_ERRORTYPE err = OMX_ErrorNone;
00269
00270 paramPort.nPortIndex = 0;
00271 setHeader(¶mPort, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
00272 err = OMX_GetParameter(appPriv->videoenchandle, OMX_IndexParamPortDefinition, ¶mPort);
00273 paramPort.format.video.nFrameWidth = in_width;
00274 paramPort.format.video.nFrameHeight = in_height;
00275 paramPort.format.video.xFramerate = frame_rate;
00276 err = OMX_SetParameter(appPriv->videoenchandle, OMX_IndexParamPortDefinition, ¶mPort);
00277 if(err!=OMX_ErrorNone) {
00278 DEBUG(DEB_LEV_ERR, "In %s Setting Input Port Definition Error=%x\n",__func__,err);
00279 return err;
00280 }
00281 DEBUG(DEB_LEV_SIMPLE_SEQ, "input picture width : %d height : %d \n", (int)in_width, (int)in_height);
00282
00283 if(flagIsCameraRequested) {
00284 paramPort.nPortIndex = 0;
00285 setHeader(¶mPort, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
00286 err = OMX_GetParameter(appPriv->videosrchandle, OMX_IndexParamPortDefinition, ¶mPort);
00287 paramPort.format.video.nFrameWidth = in_width;
00288 paramPort.format.video.nFrameHeight = in_height;
00289 err = OMX_SetParameter(appPriv->videosrchandle, OMX_IndexParamPortDefinition, ¶mPort);
00290 if(err!=OMX_ErrorNone) {
00291 DEBUG(DEB_LEV_ERR, "In %s Setting Input Port Definition Error=%x\n",__func__,err);
00292 return err;
00293 }
00294 }
00295
00296 return err;
00297 }
00298 int main(int argc, char** argv) {
00299
00300 OMX_ERRORTYPE err;
00301 int data_read;
00302 int argn_dec;
00303 OMX_STRING full_component_name;
00304
00305 if(argc < 2){
00306 display_help();
00307 } else {
00308 flagIsInputExpected = 0;
00309 flagIsOutputExpected = 0;
00310 flagOutputReceived = 0;
00311 flagInputReceived = 0;
00312 flagIsFormatRequested = 0;
00313 flagIsWidth = 0;
00314 flagIsHeight = 0;
00315 flagIsCameraRequested = 0;
00316 flagIsFPS = 0;
00317
00318 argn_dec = 1;
00319 while (argn_dec < argc) {
00320 if (*(argv[argn_dec]) == '-') {
00321 if (flagIsOutputExpected) {
00322 display_help();
00323 }
00324 switch (*(argv[argn_dec] + 1)) {
00325 case 'h' :
00326 display_help();
00327 break;
00328 case 'i':
00329 flagIsInputExpected = 1;
00330 break;
00331 case 'o':
00332 flagIsOutputExpected = 1;
00333 break;
00334 case 'f' :
00335 flagIsFormatRequested = 1;
00336 break;
00337 case 't' :
00338 flagSetupTunnel = 1;
00339 break;
00340 case 'W' :
00341 flagIsWidth = 1;
00342 break;
00343 case 'H' :
00344 flagIsHeight = 1;
00345 break;
00346 case 'C' :
00347 flagIsCameraRequested = 1;
00348 break;
00349 case 'r' :
00350 flagIsFPS = 1;
00351 break;
00352 default:
00353 display_help();
00354 }
00355 } else {
00356 if (flagIsOutputExpected) {
00357 if(strstr(argv[argn_dec], ".m4v") == NULL) {
00358 int k=0;
00359 if(strstr(argv[argn_dec],".")) {
00360 k = strlen(strstr(argv[argn_dec],"."));
00361 }
00362 output_file = malloc(strlen(argv[argn_dec]) - k + 6);
00363 strncpy(output_file,argv[argn_dec], (strlen(argv[argn_dec]) - k));
00364 strcat(output_file,".m4v");
00365 DEBUG(DEFAULT_MESSAGES, "New output File %s \n", output_file);
00366 } else {
00367 output_file = malloc(strlen(argv[argn_dec]) * sizeof(char) + 1);
00368 strcpy(output_file,argv[argn_dec]);
00369 }
00370 flagIsOutputExpected = 0;
00371 flagOutputReceived = 1;
00372 } else if(flagIsFormatRequested) {
00373 if(strstr(argv[argn_dec], "m4v") != NULL) {
00374 strcpy(&codecName[0],"m4v");
00375 } else {
00376 DEBUG(DEB_LEV_ERR, "Specified output format is unknown - keeping default m4v format\n");
00377 }
00378 flagIsFormatRequested = 0;
00379 } else if(flagIsWidth) {
00380 in_width = atoi(argv[argn_dec]);
00381 flagIsWidth = 0;
00382 } else if(flagIsHeight) {
00383 in_height = atoi(argv[argn_dec]);
00384 flagIsHeight = 0;
00385 } else if(flagIsInputExpected){
00386 input_file = malloc(strlen(argv[argn_dec]) * sizeof(char) + 1);
00387 strcpy(input_file,argv[argn_dec]);
00388 flagIsInputExpected = 0;
00389 flagInputReceived = 1;
00390 } else if(flagIsFPS) {
00391 frame_rate = atoi(argv[argn_dec]);
00392 flagIsFPS = 0;
00393 }
00394 }
00395 argn_dec++;
00396 }
00397
00398
00399 if(flagIsCameraRequested) {
00400 if(flagInputReceived) {
00401 DEBUG(DEFAULT_MESSAGES, "Ignoring Input File %s, taking input from camera\n", input_file);
00402 flagInputReceived = 0;
00403 if(input_file) {
00404 free(input_file);
00405 }
00406 }
00407 } else {
00408 if(flagSetupTunnel) {
00409 DEBUG(DEFAULT_MESSAGES, "Camera Not Selected. Tunneling Can't be used\n");
00410 DEBUG(DEFAULT_MESSAGES, "Use option '-C' to select camera\n");
00411 exit(1);
00412 }
00413 }
00414
00416 if ((!flagOutputReceived) || (strstr(output_file, ".m4v") == NULL)) {
00417 DEBUG(DEB_LEV_ERR, "You must specify appropriate input file of .m4v format\n");
00418 display_help();
00419 }
00420
00421 if (flagInputReceived != 1 && !flagIsCameraRequested) {
00422 DEBUG(DEB_LEV_ERR, "You must specify appropriate input file \n");
00423 display_help();
00424 }
00425
00427 if((strstr(output_file, ".m4v") != NULL)) {
00428 selectedType = MPEG4_TYPE_SEL;
00429 } else {
00430 DEBUG(DEB_LEV_ERR, "\n input file format and operation not supported \n");
00431 display_help();
00432 }
00433 }
00434
00435 if(!flagIsCameraRequested) {
00436 infile = fopen(input_file, "rb");
00437 if(infile == NULL) {
00438 DEBUG(DEB_LEV_ERR, "Error in opening input file %s\n", input_file);
00439 exit(1);
00440 }
00441 }
00442
00443 outfile = fopen(output_file, "wb");
00444 if(outfile == NULL) {
00445 DEBUG(DEB_LEV_ERR, "Error in opening output file %s\n", output_file);
00446 exit(1);
00447 }
00448
00449
00450 appPriv = malloc(sizeof(appPrivateType));
00451 appPriv->encoderEventSem = malloc(sizeof(tsem_t));
00452 appPriv->eofSem = malloc(sizeof(tsem_t));
00453 appPriv->sourceEventSem = malloc(sizeof(tsem_t));
00454 tsem_init(appPriv->encoderEventSem, 0);
00455 tsem_init(appPriv->eofSem, 0);
00456 tsem_init(appPriv->sourceEventSem, 0);
00457
00458 DEBUG(DEB_LEV_SIMPLE_SEQ, "Init the Omx core\n");
00459 err = OMX_Init();
00460 if (err != OMX_ErrorNone) {
00461 DEBUG(DEB_LEV_ERR, "The OpenMAX core can not be initialized. Exiting...\n");
00462 exit(1);
00463 } else {
00464 DEBUG(DEB_LEV_SIMPLE_SEQ, "Omx core is initialized \n");
00465 }
00466
00467 DEBUG(DEFAULT_MESSAGES, "------------------------------------\n");
00468 test_OMX_ComponentNameEnum();
00469 DEBUG(DEFAULT_MESSAGES, "------------------------------------\n");
00470 test_OMX_RoleEnum(COMPONENT_NAME_BASE);
00471 DEBUG(DEFAULT_MESSAGES, "------------------------------------\n");
00472 test_OMX_ComponentEnumByRole(BASE_ROLE);
00473 DEBUG(DEFAULT_MESSAGES, "------------------------------------\n");
00474 test_OpenClose(COMPONENT_NAME_BASE);
00475 DEBUG(DEFAULT_MESSAGES, "------------------------------------\n");
00476
00477 full_component_name = (OMX_STRING) malloc(sizeof(char*) * OMX_MAX_STRINGNAME_SIZE);
00478 strcpy(full_component_name, COMPONENT_NAME_BASE);
00479 if(selectedType == MPEG4_TYPE_SEL) {
00480 strcpy(full_component_name + COMPONENT_NAME_BASE_LEN, ".mpeg4");
00481 }
00482
00483 DEBUG(DEFAULT_MESSAGES, "The component selected for encoding is %s\n", full_component_name);
00484
00485 if(flagIsCameraRequested) {
00487 err = OMX_GetHandle(&appPriv->videosrchandle, "OMX.st.video_src", NULL, &videosrccallbacks);
00488 if(err != OMX_ErrorNone){
00489 DEBUG(DEB_LEV_ERR, "No video source component found. Exiting...\n");
00490 exit(1);
00491 } else {
00492 DEBUG(DEFAULT_MESSAGES, "Found The component for capturing is %s\n", full_component_name);
00493 }
00494 }
00495
00497 err = OMX_GetHandle(&appPriv->videoenchandle, full_component_name, NULL, &videoenccallbacks);
00498 if(err != OMX_ErrorNone){
00499 DEBUG(DEB_LEV_ERR, "No video encoder component found. Exiting...\n");
00500 exit(1);
00501 } else {
00502 DEBUG(DEFAULT_MESSAGES, "Found The component for encoding is %s\n", full_component_name);
00503 }
00504
00505 setHeader(¶mRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
00506 err = OMX_GetParameter(appPriv->videoenchandle, OMX_IndexParamStandardComponentRole, ¶mRole);
00507 if (err != OMX_ErrorNone) {
00508 DEBUG(DEB_LEV_ERR, "The role set for this component can not be retrieved err = %i\n", err);
00509 }
00510 DEBUG(DEB_LEV_SIMPLE_SEQ, "The role currently set is %s\n", paramRole.cRole);
00511
00513 if(flagSetupTunnel) {
00514 DEBUG(DEB_LEV_SIMPLE_SEQ, "Setting up Tunnel\n");
00515 err = OMX_SetupTunnel(appPriv->videosrchandle, 0, appPriv->videoenchandle, 0);
00516 if(err != OMX_ErrorNone) {
00517 DEBUG(DEB_LEV_ERR, "Set up Tunnel between video source and encoder Failed\n");
00518 exit(1);
00519 }
00520
00521 DEBUG(DEFAULT_MESSAGES, "Set up Tunnel Completed\n");
00522 }
00523
00525 buffer_in_size = in_width * in_height * 3/2;
00526 DEBUG(DEB_LEV_SIMPLE_SEQ, "buffer_in_size : %d \n", (int)buffer_in_size);
00527
00528 setPortParameters() ;
00529
00530
00532 err = OMX_SendCommand(appPriv->videoenchandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
00533
00534 if(flagIsCameraRequested) {
00536 err = OMX_SendCommand(appPriv->videosrchandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
00537 }
00538
00539 if (!flagSetupTunnel) {
00540 pInBuffer[0] = pInBuffer[1] = NULL;
00541 if (flagIsCameraRequested) {
00542 pOutBuffer[0] = pOutBuffer[1] = NULL;
00543 err = OMX_AllocateBuffer(appPriv->videosrchandle, &pSrcOutBuffer[0], 0, NULL, buffer_in_size);
00544 err = OMX_AllocateBuffer(appPriv->videosrchandle, &pSrcOutBuffer[1], 0, NULL, buffer_in_size);
00545
00546 err = OMX_UseBuffer(appPriv->videoenchandle, &pInBuffer[0], 0, NULL, buffer_in_size, pSrcOutBuffer[0]->pBuffer);
00547 err = OMX_UseBuffer(appPriv->videoenchandle, &pInBuffer[1], 0, NULL, buffer_in_size, pSrcOutBuffer[1]->pBuffer);
00548 } else {
00549 err = OMX_AllocateBuffer(appPriv->videoenchandle, &pInBuffer[0], 0, NULL, buffer_in_size);
00550 err = OMX_AllocateBuffer(appPriv->videoenchandle, &pInBuffer[1], 0, NULL, buffer_in_size);
00551 }
00552 }
00553
00554 pOutBuffer[0] = pOutBuffer[1] = NULL;
00555 err = OMX_AllocateBuffer(appPriv->videoenchandle, &pOutBuffer[0], 1, NULL, buffer_out_size);
00556 err = OMX_AllocateBuffer(appPriv->videoenchandle, &pOutBuffer[1], 1, NULL, buffer_out_size);
00557
00558 DEBUG(DEB_LEV_SIMPLE_SEQ, "Before locking on idle wait semaphore\n");
00559 tsem_down(appPriv->encoderEventSem);
00560 DEBUG(DEB_LEV_SIMPLE_SEQ, "encoder Sem free\n");
00561 if (flagIsCameraRequested) {
00562 tsem_down(appPriv->sourceEventSem);
00563 }
00564
00566 err = OMX_SendCommand(appPriv->videoenchandle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
00567 tsem_down(appPriv->encoderEventSem);
00568
00569 if (flagIsCameraRequested) {
00571 err = OMX_SendCommand(appPriv->videosrchandle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
00572 tsem_down(appPriv->sourceEventSem);
00573 if (!flagSetupTunnel) {
00574 err = OMX_FillThisBuffer(appPriv->videosrchandle, pSrcOutBuffer[0]);
00575 err = OMX_FillThisBuffer(appPriv->videosrchandle, pSrcOutBuffer[1]);
00576 }
00577
00578 }
00579
00580 err = OMX_FillThisBuffer(appPriv->videoenchandle, pOutBuffer[0]);
00581 err = OMX_FillThisBuffer(appPriv->videoenchandle, pOutBuffer[1]);
00582
00583 DEBUG(DEB_LEV_SIMPLE_SEQ, "---> Before locking on condition and encoderMutex\n");
00584
00585 if(!flagIsCameraRequested) {
00586 data_read = fread(pInBuffer[0]->pBuffer, sizeof(char), buffer_in_size, infile);
00587 pInBuffer[0]->nFilledLen = data_read;
00588 pInBuffer[0]->nOffset = 0;
00589
00590 data_read = fread(pInBuffer[1]->pBuffer, sizeof(char), buffer_in_size, infile);
00591 pInBuffer[1]->nFilledLen = data_read;
00592 pInBuffer[1]->nOffset = 0;
00593
00594 DEBUG(DEB_LEV_PARAMS, "Empty first buffer %x\n", (int)pInBuffer[0]->pBuffer);
00595 err = OMX_EmptyThisBuffer(appPriv->videoenchandle, pInBuffer[0]);
00596 DEBUG(DEB_LEV_PARAMS, "Empty second buffer %x\n", (int)pInBuffer[1]->pBuffer);
00597 err = OMX_EmptyThisBuffer(appPriv->videoenchandle, pInBuffer[1]);
00598
00599 DEBUG(DEFAULT_MESSAGES,"Waiting for EOS\n");
00600
00605 tsem_down(appPriv->eofSem);
00606 } else {
00607 DEBUG(DEFAULT_MESSAGES,"Enter 'q' or 'Q' to exit\n");
00608
00609 while(1) {
00610 if('Q' == toupper(getchar())) {
00611 DEBUG(DEFAULT_MESSAGES,"Stoping capture\n");
00612 bEOS = OMX_TRUE;
00613 break;
00614 }
00615 }
00616 }
00617
00618 DEBUG(DEFAULT_MESSAGES, "The execution of the video encoding process is terminated\n");
00619
00621 if (flagIsCameraRequested) {
00622 err = OMX_SendCommand(appPriv->videosrchandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
00623 }
00624 err = OMX_SendCommand(appPriv->videoenchandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
00625
00626 tsem_down(appPriv->encoderEventSem);
00627
00628 if (flagIsCameraRequested) {
00629 tsem_down(appPriv->sourceEventSem);
00630 }
00631
00632 DEBUG(DEFAULT_MESSAGES, "All video components Transitioned to Idle\n");
00633
00635 err = OMX_SendCommand(appPriv->videoenchandle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
00636
00637 if (flagIsCameraRequested) {
00638 err = OMX_SendCommand(appPriv->videosrchandle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
00639 }
00640
00644 DEBUG(DEB_LEV_PARAMS, "Video enc to loaded\n");
00645 if (!flagSetupTunnel) {
00646 err = OMX_FreeBuffer(appPriv->videoenchandle, 0, pInBuffer[0]);
00647 err = OMX_FreeBuffer(appPriv->videoenchandle, 0, pInBuffer[1]);
00648
00649 if (flagIsCameraRequested) {
00650 err = OMX_FreeBuffer(appPriv->videosrchandle, 0, pSrcOutBuffer[0]);
00651 err = OMX_FreeBuffer(appPriv->videosrchandle, 0, pSrcOutBuffer[1]);
00652 }
00653 }
00654
00655
00656 DEBUG(DEB_LEV_PARAMS, "Free Video enc output ports\n");
00657 err = OMX_FreeBuffer(appPriv->videoenchandle, 1, pOutBuffer[0]);
00658 err = OMX_FreeBuffer(appPriv->videoenchandle, 1, pOutBuffer[1]);
00659
00660 tsem_down(appPriv->encoderEventSem);
00661
00662 if (flagIsCameraRequested) {
00663 tsem_down(appPriv->sourceEventSem);
00664 }
00665 DEBUG(DEB_LEV_SIMPLE_SEQ, "All components released\n");
00666
00667 OMX_FreeHandle(appPriv->videoenchandle);
00668
00669 if (flagIsCameraRequested) {
00670 OMX_FreeHandle(appPriv->videosrchandle);
00671 }
00672
00673 DEBUG(DEB_LEV_SIMPLE_SEQ, "video encoder and source freed\n");
00674
00675 OMX_Deinit();
00676
00677 DEBUG(DEFAULT_MESSAGES, "All components freed. Closing...\n");
00678 free(appPriv->encoderEventSem);
00679 free(appPriv->sourceEventSem);
00680 free(appPriv->eofSem);
00681 free(appPriv);
00682
00684 fclose(outfile);
00685 if(!flagIsCameraRequested) {
00687 fclose(infile);
00688 }
00689
00690 return 0;
00691 }
00692
00693
00695 OMX_ERRORTYPE videosrcEventHandler(
00696 OMX_OUT OMX_HANDLETYPE hComponent,
00697 OMX_OUT OMX_PTR pAppData,
00698 OMX_OUT OMX_EVENTTYPE eEvent,
00699 OMX_OUT OMX_U32 Data1,
00700 OMX_OUT OMX_U32 Data2,
00701 OMX_OUT OMX_PTR pEventData) {
00702
00703 OMX_ERRORTYPE err = OMX_ErrorNone;
00704
00705 DEBUG(DEB_LEV_SIMPLE_SEQ, "Hi there, I am in the %s callback\n", __func__);
00706 if(eEvent == OMX_EventCmdComplete) {
00707 if (Data1 == OMX_CommandStateSet) {
00708 DEBUG(DEB_LEV_SIMPLE_SEQ, "State changed in ");
00709 switch ((int)Data2) {
00710 case OMX_StateInvalid:
00711 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateInvalid\n");
00712 break;
00713 case OMX_StateLoaded:
00714 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateLoaded\n");
00715 break;
00716 case OMX_StateIdle:
00717 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateIdle\n");
00718 break;
00719 case OMX_StateExecuting:
00720 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateExecuting\n");
00721 break;
00722 case OMX_StatePause:
00723 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StatePause\n");
00724 break;
00725 case OMX_StateWaitForResources:
00726 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateWaitForResources\n");
00727 break;
00728 }
00729 tsem_up(appPriv->sourceEventSem);
00730 }
00731 } else {
00732 DEBUG(DEB_LEV_SIMPLE_SEQ, "Param1 is %i\n", (int)Data1);
00733 DEBUG(DEB_LEV_SIMPLE_SEQ, "Param2 is %i\n", (int)Data2);
00734 }
00735 return err;
00736 }
00737
00738 OMX_ERRORTYPE videosrcFillBufferDone(
00739 OMX_OUT OMX_HANDLETYPE hComponent,
00740 OMX_OUT OMX_PTR pAppData,
00741 OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer) {
00742
00743 OMX_ERRORTYPE err;
00744 OMX_STATETYPE eState;
00745
00746 if(pBuffer != NULL){
00747 if(!bEOS) {
00748 if(!flagSetupTunnel) {
00749 OMX_GetState(appPriv->videoenchandle,&eState);
00750 if(eState == OMX_StateExecuting || eState == OMX_StatePause) {
00751 if(pInBuffer[0]->pBuffer == pBuffer->pBuffer) {
00752 pInBuffer[0]->nFilledLen = pBuffer->nFilledLen;
00753 err = OMX_EmptyThisBuffer(appPriv->videoenchandle, pInBuffer[0]);
00754 } else {
00755 pInBuffer[1]->nFilledLen = pBuffer->nFilledLen;
00756 err = OMX_EmptyThisBuffer(appPriv->videoenchandle, pInBuffer[1]);
00757 }
00758 if(err != OMX_ErrorNone) {
00759 DEBUG(DEB_LEV_ERR, "In %s Error %08x Calling FillThisBuffer\n", __func__,err);
00760 }
00761 } else {
00762 err = OMX_FillThisBuffer(hComponent, pBuffer);
00763 }
00764 }
00765 } else {
00766 DEBUG(DEFAULT_MESSAGES, "In %s: eos=%x Dropping Empty This Buffer\n", __func__,(int)pBuffer->nFlags);
00767 }
00768 } else {
00769 DEBUG(DEB_LEV_ERR, "Ouch! In %s: had NULL buffer to output...\n", __func__);
00770 }
00771 return OMX_ErrorNone;
00772 }
00774 OMX_ERRORTYPE videoencEventHandler(
00775 OMX_OUT OMX_HANDLETYPE hComponent,
00776 OMX_OUT OMX_PTR pAppData,
00777 OMX_OUT OMX_EVENTTYPE eEvent,
00778 OMX_OUT OMX_U32 Data1,
00779 OMX_OUT OMX_U32 Data2,
00780 OMX_OUT OMX_PTR pEventData) {
00781
00782 OMX_ERRORTYPE err = OMX_ErrorNone;
00783
00784 DEBUG(DEB_LEV_SIMPLE_SEQ, "Hi there, I am in the %s callback\n", __func__);
00785 if(eEvent == OMX_EventCmdComplete) {
00786 if (Data1 == OMX_CommandStateSet) {
00787 DEBUG(DEB_LEV_SIMPLE_SEQ, "State changed in ");
00788 switch ((int)Data2) {
00789 case OMX_StateInvalid:
00790 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateInvalid\n");
00791 break;
00792 case OMX_StateLoaded:
00793 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateLoaded\n");
00794 break;
00795 case OMX_StateIdle:
00796 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateIdle\n");
00797 break;
00798 case OMX_StateExecuting:
00799 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateExecuting\n");
00800 break;
00801 case OMX_StatePause:
00802 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StatePause\n");
00803 break;
00804 case OMX_StateWaitForResources:
00805 DEBUG(DEB_LEV_SIMPLE_SEQ, "OMX_StateWaitForResources\n");
00806 break;
00807 }
00808 tsem_up(appPriv->encoderEventSem);
00809 }
00810 else if (OMX_CommandPortEnable || OMX_CommandPortDisable) {
00811 DEBUG(DEB_LEV_SIMPLE_SEQ, "In %s Received Port Enable/Disable Event\n",__func__);
00812 tsem_up(appPriv->encoderEventSem);
00813 }
00814 } else if(eEvent == OMX_EventPortSettingsChanged) {
00815 DEBUG(DEB_LEV_SIMPLE_SEQ, "\n port settings change event handler in %s \n", __func__);
00816
00817 } else if(eEvent == OMX_EventBufferFlag) {
00818 DEBUG(DEFAULT_MESSAGES, "In %s OMX_BUFFERFLAG_EOS\n", __func__);
00819 if((int)Data2 == OMX_BUFFERFLAG_EOS) {
00820 tsem_up(appPriv->eofSem);
00821 }
00822 } else {
00823 DEBUG(DEB_LEV_SIMPLE_SEQ, "Param1 is %i\n", (int)Data1);
00824 DEBUG(DEB_LEV_SIMPLE_SEQ, "Param2 is %i\n", (int)Data2);
00825 }
00826 return err;
00827 }
00828
00829 OMX_ERRORTYPE videoencEmptyBufferDone(
00830 OMX_OUT OMX_HANDLETYPE hComponent,
00831 OMX_OUT OMX_PTR pAppData,
00832 OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer) {
00833
00834 OMX_ERRORTYPE err;
00835 int data_read;
00836 DEBUG(DEB_LEV_FULL_SEQ, "Hi there, I am in the %s callback.\n", __func__);
00837
00838 if(!flagIsCameraRequested) {
00839 data_read = fread(pBuffer->pBuffer, sizeof(char), buffer_in_size, infile);
00840 if (data_read <= 0) {
00841 DEBUG(DEB_LEV_SIMPLE_SEQ, "In the %s no more input data available\n", __func__);
00842 pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
00843 pBuffer->nFilledLen = 0;
00844 tsem_up(appPriv->eofSem);
00845 return OMX_ErrorNone;
00846 }
00847 pBuffer->nFilledLen = data_read;
00848 DEBUG(DEB_LEV_PARAMS, "Empty buffer %x\n", (int)pBuffer);
00849 err = OMX_EmptyThisBuffer(hComponent, pBuffer);
00850 } else if(!bEOS){
00851 if(pSrcOutBuffer[0]->pBuffer == pBuffer->pBuffer) {
00852 err = OMX_FillThisBuffer(appPriv->videosrchandle, pSrcOutBuffer[0]);
00853 } else {
00854 err = OMX_FillThisBuffer(appPriv->videosrchandle, pSrcOutBuffer[1]);
00855 }
00856 } else {
00857 DEBUG(DEFAULT_MESSAGES, "In %s: Dropping Fill This Buffer\n", __func__);
00858 }
00859 return OMX_ErrorNone;
00860 }
00861
00862 OMX_ERRORTYPE videoencFillBufferDone(
00863 OMX_OUT OMX_HANDLETYPE hComponent,
00864 OMX_OUT OMX_PTR pAppData,
00865 OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer) {
00866
00867 OMX_ERRORTYPE err;
00868
00869 if(pBuffer != NULL){
00870 if(!bEOS) {
00871 if((pBuffer->nFilledLen > 0)) {
00872 fwrite(pBuffer->pBuffer, sizeof(char), pBuffer->nFilledLen, outfile);
00873 pBuffer->nFilledLen = 0;
00874 }
00875 if(pBuffer->nFlags == OMX_BUFFERFLAG_EOS) {
00876 DEBUG(DEFAULT_MESSAGES, "In %s: eos=%x Calling Empty This Buffer\n", __func__, (int)pBuffer->nFlags);
00877 bEOS = OMX_TRUE;
00878 }
00879 if(!bEOS ) {
00880 err = OMX_FillThisBuffer(hComponent, pBuffer);
00881 }
00882 } else {
00883 DEBUG(DEFAULT_MESSAGES, "In %s: eos=%x Dropping Empty This Buffer\n", __func__,(int)pBuffer->nFlags);
00884 }
00885 } else {
00886 DEBUG(DEB_LEV_ERR, "Ouch! In %s: had NULL buffer to output...\n", __func__);
00887 }
00888 return OMX_ErrorNone;
00889 }
00890
00891