Loading lib_util/obj_edit_file_reader.c 0 → 100644 +278 −0 Original line number Diff line number Diff line /****************************************************************************************************** (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD., Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange, Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other contributors to this repository. All Rights Reserved. This software is protected by copyright law and by international treaties. The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD., Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange, Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other contributors to this repository retain full ownership rights in their respective contributions in the software. This notice grants no license of any kind, including but not limited to patent license, nor is any license granted by implication, estoppel or otherwise. Contributors are required to enter into the IVAS codec Public Collaboration agreement before making contributions. This software is provided "AS IS", without any express or implied warranties. The software is in the development stage. It is intended exclusively for experts who have experience with such software and solely for the purpose of inspection. All implied warranties of non-infringement, merchantability and fitness for a particular purpose are hereby disclaimed and excluded. Any dispute, controversy or claim arising under or in relation to providing this software shall be submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and the United Nations Convention on Contracts on the International Sales of Goods. *******************************************************************************************************/ #include <stdlib.h> #include <string.h> #include "obj_edit_file_reader.h" #include "prot.h" #ifdef FIX_1217_OBJECT_EDIT_FILE_INTERFACE /*-----------------------------------------------------------------------* * ObjectEditFileReader_open() * * Allocate and initialize object editing reader *-----------------------------------------------------------------------*/ ivas_error ObjectEditFileReader_open( const char *fileName, /* i : path to object edit description file */ ObjectEditFileReader **objEditReader /* o : ObjectEditFileReader handle */ ) { ObjectEditFileReader *self; FILE *fileHandle; int16_t obj_idx; if ( fileName == NULL || objEditReader == NULL ) { return IVAS_ERR_UNEXPECTED_NULL_POINTER; } /* Open file */ if ( strlen( fileName ) < 1 ) { return IVAS_ERR_FAILED_FILE_OPEN; } fileHandle = fopen( fileName, "r" ); if ( !fileHandle ) { return IVAS_ERR_FAILED_FILE_OPEN; } self = (ObjectEditFileReader *) calloc( 1, sizeof( ObjectEditFileReader ) ); self->maxLineLen = 256; self->editFileHandle = fileHandle; self->inLine = (char *) calloc( self->maxLineLen, sizeof( char ) ); self->readInfo = (ReadObjectEditInfo *) calloc( 1, sizeof( ReadObjectEditInfo ) ); self->readInfo->bg_gain = 0.0f; self->readInfo->bg_gain_edited = false; for ( obj_idx = 0; obj_idx < IVAS_MAX_NUM_OBJECTS; obj_idx++ ) { self->readInfo->obj_gain[obj_idx] = 0.0f; self->readInfo->obj_gain_edited[obj_idx] = false; self->readInfo->obj_gain_relative[obj_idx] = false; self->readInfo->obj_azi[obj_idx] = 0.0f; self->readInfo->obj_azi_edited[obj_idx] = false; self->readInfo->obj_azi_relative[obj_idx] = false; self->readInfo->obj_ele[obj_idx] = 0.0f; self->readInfo->obj_ele_edited[obj_idx] = false; self->readInfo->obj_ele_relative[obj_idx] = false; } self->rewound = false; *objEditReader = self; return IVAS_ERR_OK; } /*---------------------------------------------------------------------* * ObjectEditFileReader_readNextFrame() * * Read object editing parameters for current frame *---------------------------------------------------------------------*/ ivas_error ObjectEditFileReader_readNextFrame( ObjectEditFileReader *objEditReader /* i/o: ObjectEditFileReader handle */ ) { if ( objEditReader == NULL ) { return IVAS_ERR_UNEXPECTED_NULL_POINTER; } if ( fgets( objEditReader->inLine, objEditReader->maxLineLen, objEditReader->editFileHandle ) != NULL ) { char *token; char *paramName; char *paramValue; int16_t obj_idx; ReadObjectEditInfo *readEdits; readEdits = objEditReader->readInfo; objEditReader->rewound = false; readEdits->bg_gain_edited = false; for ( obj_idx = 0; obj_idx < IVAS_MAX_NUM_OBJECTS; obj_idx++ ) { readEdits->obj_gain_edited[obj_idx] = false; readEdits->obj_gain_relative[obj_idx] = false; readEdits->obj_azi_edited[obj_idx] = false; readEdits->obj_azi_relative[obj_idx] = false; readEdits->obj_ele_edited[obj_idx] = false; readEdits->obj_ele_relative[obj_idx] = false; } /* tokenize the line by comma */ /* {bg_gain=<float>}[,obj_<int>_<param>=<float>]*N[,obj_<int>_<relparam>=<0|1>]*N, param in ('gain', 'azi', 'ele'), relparam in ('relgain', 'relazi', 'relele') */ token = strtok( objEditReader->inLine, "," ); while ( token != NULL ) { paramName = token; paramValue = strchr( token, '=' ); if ( paramValue != NULL ) { *paramValue = '\0'; /* temporarily terminate the string at the equal sign */ paramValue++; /* move to the value part */ } if ( strcmp( paramName, "bg_gain" ) == 0 ) { readEdits->bg_gain = max( min( strtof( paramValue, NULL ), OBJ_EDIT_GAIN_MAX ), OBJ_EDIT_GAIN_MIN ); readEdits->bg_gain_edited = true; } else { if ( strncmp( paramName, "obj_", 4 ) == 0 ) { char *param; char *underscore_pos; underscore_pos = strchr( paramName + 4, '_' ); if ( underscore_pos != NULL ) { /* temporarily terminate the string at the second underscore */ *underscore_pos = '\0'; /* extract object index */ obj_idx = (int16_t) atoi( paramName + 4 ); /* skip "obj_" and convert to integer */ if ( ( obj_idx < 0 ) || ( obj_idx >= IVAS_MAX_NUM_OBJECTS ) ) { return IVAS_ERR_FAILED_FILE_PARSE; } /* restore the underscore and extract parameter name */ *underscore_pos = '_'; param = underscore_pos + 1; if ( strcmp( param, "gain" ) == 0 ) { readEdits->obj_gain[obj_idx] = strtof( paramValue, NULL ); readEdits->obj_gain_edited[obj_idx] = true; } else if ( strcmp( param, "azi" ) == 0 ) { readEdits->obj_azi[obj_idx] = strtof( paramValue, NULL ); readEdits->obj_azi_edited[obj_idx] = true; } else if ( strcmp( param, "ele" ) == 0 ) { readEdits->obj_ele[obj_idx] = strtof( paramValue, NULL ); readEdits->obj_ele_edited[obj_idx] = true; } else if ( strcmp( param, "relgain" ) == 0 ) { readEdits->obj_gain_relative[obj_idx] = ( strcmp( paramValue, "1" ) == 0 ); } else if ( strcmp( param, "relazi" ) == 0 ) { readEdits->obj_azi_relative[obj_idx] = ( strcmp( paramValue, "1" ) == 0 ); } else if ( strcmp( param, "relele" ) == 0 ) { readEdits->obj_ele_relative[obj_idx] = ( strcmp( paramValue, "1" ) == 0 ); } } } } /* get the next token */ token = strtok( NULL, "," ); } /* sanitize values for absolute edits. relative edit sanitation done on result when applying the delta */ for ( obj_idx = 0; obj_idx < IVAS_MAX_NUM_OBJECTS; obj_idx++ ) { if ( readEdits->obj_gain_edited[obj_idx] && !readEdits->obj_gain_relative[obj_idx] ) { readEdits->obj_gain[obj_idx] = max( min( readEdits->obj_gain[obj_idx], OBJ_EDIT_GAIN_MAX ), OBJ_EDIT_GAIN_MIN ); } if ( readEdits->obj_azi_edited[obj_idx] && !readEdits->obj_azi_relative[obj_idx] ) { readEdits->obj_azi[obj_idx] = max( min( readEdits->obj_azi[obj_idx], 180.f ), -180.f ); } if ( readEdits->obj_ele_edited[obj_idx] && !readEdits->obj_ele_relative[obj_idx] ) { readEdits->obj_ele[obj_idx] = max( min( readEdits->obj_ele[obj_idx], 90.f ), -90.f ); } } } else { if ( !objEditReader->rewound ) { /* rewind file and try to read again */ rewind( objEditReader->editFileHandle ); objEditReader->rewound = true; ObjectEditFileReader_readNextFrame( objEditReader ); } else { /* was already rewound and still failing to read */ return IVAS_ERR_FAILED_FILE_READ; } } return IVAS_ERR_OK; } /*-----------------------------------------------------------------------* * ObjectEditFileReader_close() * * Deallocates memory for the object edit file reader *-----------------------------------------------------------------------*/ void ObjectEditFileReader_close( ObjectEditFileReader **objEditReader /* i/o: ObjectEditFileReader handle */ ) { if ( objEditReader == NULL || *objEditReader == NULL ) { return; } fclose( ( *objEditReader )->editFileHandle ); free( ( *objEditReader )->readInfo ); free( *objEditReader ); *objEditReader = NULL; return; } #endif Loading
lib_util/obj_edit_file_reader.c 0 → 100644 +278 −0 Original line number Diff line number Diff line /****************************************************************************************************** (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD., Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange, Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other contributors to this repository. All Rights Reserved. This software is protected by copyright law and by international treaties. The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD., Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange, Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other contributors to this repository retain full ownership rights in their respective contributions in the software. This notice grants no license of any kind, including but not limited to patent license, nor is any license granted by implication, estoppel or otherwise. Contributors are required to enter into the IVAS codec Public Collaboration agreement before making contributions. This software is provided "AS IS", without any express or implied warranties. The software is in the development stage. It is intended exclusively for experts who have experience with such software and solely for the purpose of inspection. All implied warranties of non-infringement, merchantability and fitness for a particular purpose are hereby disclaimed and excluded. Any dispute, controversy or claim arising under or in relation to providing this software shall be submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and the United Nations Convention on Contracts on the International Sales of Goods. *******************************************************************************************************/ #include <stdlib.h> #include <string.h> #include "obj_edit_file_reader.h" #include "prot.h" #ifdef FIX_1217_OBJECT_EDIT_FILE_INTERFACE /*-----------------------------------------------------------------------* * ObjectEditFileReader_open() * * Allocate and initialize object editing reader *-----------------------------------------------------------------------*/ ivas_error ObjectEditFileReader_open( const char *fileName, /* i : path to object edit description file */ ObjectEditFileReader **objEditReader /* o : ObjectEditFileReader handle */ ) { ObjectEditFileReader *self; FILE *fileHandle; int16_t obj_idx; if ( fileName == NULL || objEditReader == NULL ) { return IVAS_ERR_UNEXPECTED_NULL_POINTER; } /* Open file */ if ( strlen( fileName ) < 1 ) { return IVAS_ERR_FAILED_FILE_OPEN; } fileHandle = fopen( fileName, "r" ); if ( !fileHandle ) { return IVAS_ERR_FAILED_FILE_OPEN; } self = (ObjectEditFileReader *) calloc( 1, sizeof( ObjectEditFileReader ) ); self->maxLineLen = 256; self->editFileHandle = fileHandle; self->inLine = (char *) calloc( self->maxLineLen, sizeof( char ) ); self->readInfo = (ReadObjectEditInfo *) calloc( 1, sizeof( ReadObjectEditInfo ) ); self->readInfo->bg_gain = 0.0f; self->readInfo->bg_gain_edited = false; for ( obj_idx = 0; obj_idx < IVAS_MAX_NUM_OBJECTS; obj_idx++ ) { self->readInfo->obj_gain[obj_idx] = 0.0f; self->readInfo->obj_gain_edited[obj_idx] = false; self->readInfo->obj_gain_relative[obj_idx] = false; self->readInfo->obj_azi[obj_idx] = 0.0f; self->readInfo->obj_azi_edited[obj_idx] = false; self->readInfo->obj_azi_relative[obj_idx] = false; self->readInfo->obj_ele[obj_idx] = 0.0f; self->readInfo->obj_ele_edited[obj_idx] = false; self->readInfo->obj_ele_relative[obj_idx] = false; } self->rewound = false; *objEditReader = self; return IVAS_ERR_OK; } /*---------------------------------------------------------------------* * ObjectEditFileReader_readNextFrame() * * Read object editing parameters for current frame *---------------------------------------------------------------------*/ ivas_error ObjectEditFileReader_readNextFrame( ObjectEditFileReader *objEditReader /* i/o: ObjectEditFileReader handle */ ) { if ( objEditReader == NULL ) { return IVAS_ERR_UNEXPECTED_NULL_POINTER; } if ( fgets( objEditReader->inLine, objEditReader->maxLineLen, objEditReader->editFileHandle ) != NULL ) { char *token; char *paramName; char *paramValue; int16_t obj_idx; ReadObjectEditInfo *readEdits; readEdits = objEditReader->readInfo; objEditReader->rewound = false; readEdits->bg_gain_edited = false; for ( obj_idx = 0; obj_idx < IVAS_MAX_NUM_OBJECTS; obj_idx++ ) { readEdits->obj_gain_edited[obj_idx] = false; readEdits->obj_gain_relative[obj_idx] = false; readEdits->obj_azi_edited[obj_idx] = false; readEdits->obj_azi_relative[obj_idx] = false; readEdits->obj_ele_edited[obj_idx] = false; readEdits->obj_ele_relative[obj_idx] = false; } /* tokenize the line by comma */ /* {bg_gain=<float>}[,obj_<int>_<param>=<float>]*N[,obj_<int>_<relparam>=<0|1>]*N, param in ('gain', 'azi', 'ele'), relparam in ('relgain', 'relazi', 'relele') */ token = strtok( objEditReader->inLine, "," ); while ( token != NULL ) { paramName = token; paramValue = strchr( token, '=' ); if ( paramValue != NULL ) { *paramValue = '\0'; /* temporarily terminate the string at the equal sign */ paramValue++; /* move to the value part */ } if ( strcmp( paramName, "bg_gain" ) == 0 ) { readEdits->bg_gain = max( min( strtof( paramValue, NULL ), OBJ_EDIT_GAIN_MAX ), OBJ_EDIT_GAIN_MIN ); readEdits->bg_gain_edited = true; } else { if ( strncmp( paramName, "obj_", 4 ) == 0 ) { char *param; char *underscore_pos; underscore_pos = strchr( paramName + 4, '_' ); if ( underscore_pos != NULL ) { /* temporarily terminate the string at the second underscore */ *underscore_pos = '\0'; /* extract object index */ obj_idx = (int16_t) atoi( paramName + 4 ); /* skip "obj_" and convert to integer */ if ( ( obj_idx < 0 ) || ( obj_idx >= IVAS_MAX_NUM_OBJECTS ) ) { return IVAS_ERR_FAILED_FILE_PARSE; } /* restore the underscore and extract parameter name */ *underscore_pos = '_'; param = underscore_pos + 1; if ( strcmp( param, "gain" ) == 0 ) { readEdits->obj_gain[obj_idx] = strtof( paramValue, NULL ); readEdits->obj_gain_edited[obj_idx] = true; } else if ( strcmp( param, "azi" ) == 0 ) { readEdits->obj_azi[obj_idx] = strtof( paramValue, NULL ); readEdits->obj_azi_edited[obj_idx] = true; } else if ( strcmp( param, "ele" ) == 0 ) { readEdits->obj_ele[obj_idx] = strtof( paramValue, NULL ); readEdits->obj_ele_edited[obj_idx] = true; } else if ( strcmp( param, "relgain" ) == 0 ) { readEdits->obj_gain_relative[obj_idx] = ( strcmp( paramValue, "1" ) == 0 ); } else if ( strcmp( param, "relazi" ) == 0 ) { readEdits->obj_azi_relative[obj_idx] = ( strcmp( paramValue, "1" ) == 0 ); } else if ( strcmp( param, "relele" ) == 0 ) { readEdits->obj_ele_relative[obj_idx] = ( strcmp( paramValue, "1" ) == 0 ); } } } } /* get the next token */ token = strtok( NULL, "," ); } /* sanitize values for absolute edits. relative edit sanitation done on result when applying the delta */ for ( obj_idx = 0; obj_idx < IVAS_MAX_NUM_OBJECTS; obj_idx++ ) { if ( readEdits->obj_gain_edited[obj_idx] && !readEdits->obj_gain_relative[obj_idx] ) { readEdits->obj_gain[obj_idx] = max( min( readEdits->obj_gain[obj_idx], OBJ_EDIT_GAIN_MAX ), OBJ_EDIT_GAIN_MIN ); } if ( readEdits->obj_azi_edited[obj_idx] && !readEdits->obj_azi_relative[obj_idx] ) { readEdits->obj_azi[obj_idx] = max( min( readEdits->obj_azi[obj_idx], 180.f ), -180.f ); } if ( readEdits->obj_ele_edited[obj_idx] && !readEdits->obj_ele_relative[obj_idx] ) { readEdits->obj_ele[obj_idx] = max( min( readEdits->obj_ele[obj_idx], 90.f ), -90.f ); } } } else { if ( !objEditReader->rewound ) { /* rewind file and try to read again */ rewind( objEditReader->editFileHandle ); objEditReader->rewound = true; ObjectEditFileReader_readNextFrame( objEditReader ); } else { /* was already rewound and still failing to read */ return IVAS_ERR_FAILED_FILE_READ; } } return IVAS_ERR_OK; } /*-----------------------------------------------------------------------* * ObjectEditFileReader_close() * * Deallocates memory for the object edit file reader *-----------------------------------------------------------------------*/ void ObjectEditFileReader_close( ObjectEditFileReader **objEditReader /* i/o: ObjectEditFileReader handle */ ) { if ( objEditReader == NULL || *objEditReader == NULL ) { return; } fclose( ( *objEditReader )->editFileHandle ); free( ( *objEditReader )->readInfo ); free( *objEditReader ); *objEditReader = NULL; return; } #endif