00001
00018 #include "global.h"
00019 #include "memalloc.h"
00020 #include "erc_api.h"
00021
00028 void ercInit(ImageParameters *p_Img, int pic_sizex, int pic_sizey, int flag)
00029 {
00030 ercClose(p_Img, p_Img->erc_errorVar);
00031 p_Img->erc_object_list = (objectBuffer_t *) calloc((pic_sizex * pic_sizey) >> 6, sizeof(objectBuffer_t));
00032 if (p_Img->erc_object_list == NULL) no_mem_exit("ercInit: erc_object_list");
00033
00034
00035 p_Img->erc_errorVar = ercOpen();
00036
00037
00038 ercSetErrorConcealment(p_Img->erc_errorVar, flag);
00039 }
00040
00049 ercVariables_t *ercOpen( void )
00050 {
00051 ercVariables_t *errorVar = NULL;
00052
00053 errorVar = (ercVariables_t *)malloc( sizeof(ercVariables_t));
00054 if ( errorVar == NULL ) no_mem_exit("ercOpen: errorVar");
00055
00056 errorVar->nOfMBs = 0;
00057 errorVar->segments = NULL;
00058 errorVar->currSegment = 0;
00059 errorVar->yCondition = NULL;
00060 errorVar->uCondition = NULL;
00061 errorVar->vCondition = NULL;
00062 errorVar->prevFrameYCondition = NULL;
00063
00064 errorVar->concealment = 1;
00065
00066 return errorVar;
00067 }
00068
00084 void ercReset( ercVariables_t *errorVar, int nOfMBs, int numOfSegments, int picSizeX )
00085 {
00086 int *tmp = NULL;
00087 int i = 0;
00088
00089 if ( errorVar && errorVar->concealment )
00090 {
00091
00092 if ( nOfMBs != errorVar->nOfMBs && errorVar->yCondition != NULL )
00093 {
00094 free( errorVar->yCondition );
00095 errorVar->yCondition = NULL;
00096 free( errorVar->prevFrameYCondition );
00097 errorVar->prevFrameYCondition = NULL;
00098 free( errorVar->uCondition );
00099 errorVar->uCondition = NULL;
00100 free( errorVar->vCondition );
00101 errorVar->vCondition = NULL;
00102 free( errorVar->segments );
00103 errorVar->segments = NULL;
00104 }
00105
00106
00107 if ( errorVar->yCondition == NULL )
00108 {
00109 errorVar->segments = (ercSegment_t *)malloc( numOfSegments*sizeof(ercSegment_t) );
00110 if ( errorVar->segments == NULL ) no_mem_exit("ercReset: errorVar->segments");
00111 memset( errorVar->segments, 0, numOfSegments*sizeof(ercSegment_t));
00112 errorVar->nOfSegments = numOfSegments;
00113
00114 errorVar->yCondition = (int *)malloc( 4*nOfMBs*sizeof(int) );
00115 if ( errorVar->yCondition == NULL ) no_mem_exit("ercReset: errorVar->yCondition");
00116 errorVar->prevFrameYCondition = (int *)malloc( 4*nOfMBs*sizeof(int) );
00117 if ( errorVar->prevFrameYCondition == NULL ) no_mem_exit("ercReset: errorVar->prevFrameYCondition");
00118 errorVar->uCondition = (int *)malloc( nOfMBs*sizeof(int) );
00119 if ( errorVar->uCondition == NULL ) no_mem_exit("ercReset: errorVar->uCondition");
00120 errorVar->vCondition = (int *)malloc( nOfMBs*sizeof(int) );
00121 if ( errorVar->vCondition == NULL ) no_mem_exit("ercReset: errorVar->vCondition");
00122 errorVar->nOfMBs = nOfMBs;
00123 }
00124 else
00125 {
00126
00127 tmp = errorVar->prevFrameYCondition;
00128 errorVar->prevFrameYCondition = errorVar->yCondition;
00129 errorVar->yCondition = tmp;
00130 }
00131
00132
00133 memset( errorVar->yCondition, 0, 4*nOfMBs*sizeof(*errorVar->yCondition));
00134 memset( errorVar->uCondition, 0, nOfMBs*sizeof(*errorVar->uCondition));
00135 memset( errorVar->vCondition, 0, nOfMBs*sizeof(*errorVar->vCondition));
00136
00137 if (errorVar->nOfSegments != numOfSegments)
00138 {
00139 free( errorVar->segments );
00140 errorVar->segments = NULL;
00141 errorVar->segments = (ercSegment_t *)malloc( numOfSegments*sizeof(ercSegment_t) );
00142 if ( errorVar->segments == NULL ) no_mem_exit("ercReset: errorVar->segments");
00143 errorVar->nOfSegments = numOfSegments;
00144 }
00145
00146 memset( errorVar->segments, 0, errorVar->nOfSegments*sizeof(ercSegment_t));
00147
00148 for ( i = 0; i < errorVar->nOfSegments; i++ )
00149 {
00150 errorVar->segments[i].fCorrupted = 1;
00151 errorVar->segments[i].startMBPos = 0;
00152 errorVar->segments[i].endMBPos = nOfMBs - 1;
00153 }
00154
00155 errorVar->currSegment = 0;
00156 errorVar->nOfCorruptedSegments = 0;
00157 }
00158 }
00159
00171 void ercClose(ImageParameters *p_Img, ercVariables_t *errorVar )
00172 {
00173 if ( errorVar != NULL )
00174 {
00175 if (errorVar->yCondition != NULL)
00176 {
00177 free( errorVar->segments );
00178 free( errorVar->yCondition );
00179 free( errorVar->uCondition );
00180 free( errorVar->vCondition );
00181 free( errorVar->prevFrameYCondition );
00182 }
00183 free( errorVar );
00184 errorVar = NULL;
00185 }
00186
00187 if (p_Img->erc_object_list)
00188 {
00189 free(p_Img->erc_object_list);
00190 p_Img->erc_object_list=NULL;
00191 }
00192 }
00193
00204 void ercSetErrorConcealment( ercVariables_t *errorVar, int value )
00205 {
00206 if ( errorVar != NULL )
00207 errorVar->concealment = value;
00208 }
00209
00227 void ercStartSegment( int currMBNum, int segment, unsigned int bitPos, ercVariables_t *errorVar )
00228 {
00229 if ( errorVar && errorVar->concealment )
00230 {
00231 errorVar->currSegmentCorrupted = 0;
00232
00233 errorVar->segments[ segment ].fCorrupted = 0;
00234 errorVar->segments[ segment ].startMBPos = currMBNum;
00235
00236 }
00237 }
00238
00254 void ercStopSegment( int currMBNum, int segment, unsigned int bitPos, ercVariables_t *errorVar )
00255 {
00256 if ( errorVar && errorVar->concealment )
00257 {
00258 errorVar->segments[ segment ].endMBPos = currMBNum;
00259 errorVar->currSegment++;
00260 }
00261 }
00262
00274 void ercMarkCurrSegmentLost(int picSizeX, ercVariables_t *errorVar )
00275 {
00276 int j = 0;
00277 int current_segment;
00278
00279 current_segment = errorVar->currSegment-1;
00280 if ( errorVar && errorVar->concealment )
00281 {
00282 if (errorVar->currSegmentCorrupted == 0)
00283 {
00284 errorVar->nOfCorruptedSegments++;
00285 errorVar->currSegmentCorrupted = 1;
00286 }
00287
00288 for ( j = errorVar->segments[current_segment].startMBPos; j <= errorVar->segments[current_segment].endMBPos; j++ )
00289 {
00290 errorVar->yCondition[MBNum2YBlock (j, 0, picSizeX)] = ERC_BLOCK_CORRUPTED;
00291 errorVar->yCondition[MBNum2YBlock (j, 1, picSizeX)] = ERC_BLOCK_CORRUPTED;
00292 errorVar->yCondition[MBNum2YBlock (j, 2, picSizeX)] = ERC_BLOCK_CORRUPTED;
00293 errorVar->yCondition[MBNum2YBlock (j, 3, picSizeX)] = ERC_BLOCK_CORRUPTED;
00294 errorVar->uCondition[j] = ERC_BLOCK_CORRUPTED;
00295 errorVar->vCondition[j] = ERC_BLOCK_CORRUPTED;
00296 }
00297 errorVar->segments[current_segment].fCorrupted = 1;
00298 }
00299 }
00300
00312 void ercMarkCurrSegmentOK(int picSizeX, ercVariables_t *errorVar )
00313 {
00314 int j = 0;
00315 int current_segment;
00316
00317 current_segment = errorVar->currSegment-1;
00318 if ( errorVar && errorVar->concealment )
00319 {
00320
00321 for ( j = errorVar->segments[current_segment].startMBPos; j <= errorVar->segments[current_segment].endMBPos; j++ )
00322 {
00323 errorVar->yCondition[MBNum2YBlock (j, 0, picSizeX)] = ERC_BLOCK_OK;
00324 errorVar->yCondition[MBNum2YBlock (j, 1, picSizeX)] = ERC_BLOCK_OK;
00325 errorVar->yCondition[MBNum2YBlock (j, 2, picSizeX)] = ERC_BLOCK_OK;
00326 errorVar->yCondition[MBNum2YBlock (j, 3, picSizeX)] = ERC_BLOCK_OK;
00327 errorVar->uCondition[j] = ERC_BLOCK_OK;
00328 errorVar->vCondition[j] = ERC_BLOCK_OK;
00329 }
00330 errorVar->segments[current_segment].fCorrupted = 0;
00331 }
00332 }
00333
00348 void ercMarkCurrMBConcealed( int currMBNum, int comp, int picSizeX, ercVariables_t *errorVar )
00349 {
00350 int setAll = 0;
00351
00352 if ( errorVar && errorVar->concealment )
00353 {
00354 if (comp < 0)
00355 {
00356 setAll = 1;
00357 comp = 0;
00358 }
00359
00360 switch (comp)
00361 {
00362 case 0:
00363 errorVar->yCondition[MBNum2YBlock (currMBNum, 0, picSizeX)] = ERC_BLOCK_CONCEALED;
00364 errorVar->yCondition[MBNum2YBlock (currMBNum, 1, picSizeX)] = ERC_BLOCK_CONCEALED;
00365 errorVar->yCondition[MBNum2YBlock (currMBNum, 2, picSizeX)] = ERC_BLOCK_CONCEALED;
00366 errorVar->yCondition[MBNum2YBlock (currMBNum, 3, picSizeX)] = ERC_BLOCK_CONCEALED;
00367 if (!setAll)
00368 break;
00369 case 1:
00370 errorVar->uCondition[currMBNum] = ERC_BLOCK_CONCEALED;
00371 if (!setAll)
00372 break;
00373 case 2:
00374 errorVar->vCondition[currMBNum] = ERC_BLOCK_CONCEALED;
00375 }
00376 }
00377 }