00001
00018 #include "global.h"
00019 #include "memalloc.h"
00020 #include "erc_api.h"
00021
00022 objectBuffer_t *erc_object_list = NULL;
00023 ercVariables_t *erc_errorVar = NULL;
00024 frame erc_recfr;
00025 int erc_mvperMB;
00026
00033 void ercInit(int pic_sizex, int pic_sizey, int flag)
00034 {
00035 ercClose(erc_errorVar);
00036 erc_object_list = (objectBuffer_t *) calloc((pic_sizex * pic_sizey) >> 6, sizeof(objectBuffer_t));
00037 if (erc_object_list == NULL) no_mem_exit("ercInit: erc_object_list");
00038
00039
00040 erc_errorVar = ercOpen();
00041
00042
00043 ercSetErrorConcealment(erc_errorVar, flag);
00044 }
00045
00054 ercVariables_t *ercOpen( void )
00055 {
00056 ercVariables_t *errorVar = NULL;
00057
00058 errorVar = (ercVariables_t *)malloc( sizeof(ercVariables_t));
00059 if ( errorVar == NULL ) no_mem_exit("ercOpen: errorVar");
00060
00061 errorVar->nOfMBs = 0;
00062 errorVar->segments = NULL;
00063 errorVar->currSegment = 0;
00064 errorVar->yCondition = NULL;
00065 errorVar->uCondition = NULL;
00066 errorVar->vCondition = NULL;
00067 errorVar->prevFrameYCondition = NULL;
00068
00069 errorVar->concealment = 1;
00070
00071 return errorVar;
00072 }
00073
00089 void ercReset( ercVariables_t *errorVar, int nOfMBs, int numOfSegments, int picSizeX )
00090 {
00091 int *tmp = NULL;
00092 int i = 0;
00093
00094 if ( errorVar && errorVar->concealment )
00095 {
00096
00097 if ( nOfMBs != errorVar->nOfMBs && errorVar->yCondition != NULL )
00098 {
00099 free( errorVar->yCondition );
00100 errorVar->yCondition = NULL;
00101 free( errorVar->prevFrameYCondition );
00102 errorVar->prevFrameYCondition = NULL;
00103 free( errorVar->uCondition );
00104 errorVar->uCondition = NULL;
00105 free( errorVar->vCondition );
00106 errorVar->vCondition = NULL;
00107 free( errorVar->segments );
00108 errorVar->segments = NULL;
00109 }
00110
00111
00112 if ( errorVar->yCondition == NULL )
00113 {
00114 errorVar->segments = (ercSegment_t *)malloc( numOfSegments*sizeof(ercSegment_t) );
00115 if ( errorVar->segments == NULL ) no_mem_exit("ercReset: errorVar->segments");
00116 memset( errorVar->segments, 0, numOfSegments*sizeof(ercSegment_t));
00117 errorVar->nOfSegments = numOfSegments;
00118
00119 errorVar->yCondition = (int *)malloc( 4*nOfMBs*sizeof(int) );
00120 if ( errorVar->yCondition == NULL ) no_mem_exit("ercReset: errorVar->yCondition");
00121 errorVar->prevFrameYCondition = (int *)malloc( 4*nOfMBs*sizeof(int) );
00122 if ( errorVar->prevFrameYCondition == NULL ) no_mem_exit("ercReset: errorVar->prevFrameYCondition");
00123 errorVar->uCondition = (int *)malloc( nOfMBs*sizeof(int) );
00124 if ( errorVar->uCondition == NULL ) no_mem_exit("ercReset: errorVar->uCondition");
00125 errorVar->vCondition = (int *)malloc( nOfMBs*sizeof(int) );
00126 if ( errorVar->vCondition == NULL ) no_mem_exit("ercReset: errorVar->vCondition");
00127 errorVar->nOfMBs = nOfMBs;
00128 }
00129 else
00130 {
00131
00132 tmp = errorVar->prevFrameYCondition;
00133 errorVar->prevFrameYCondition = errorVar->yCondition;
00134 errorVar->yCondition = tmp;
00135 }
00136
00137
00138 memset( errorVar->yCondition, 0, 4*nOfMBs*sizeof(*errorVar->yCondition));
00139 memset( errorVar->uCondition, 0, nOfMBs*sizeof(*errorVar->uCondition));
00140 memset( errorVar->vCondition, 0, nOfMBs*sizeof(*errorVar->vCondition));
00141
00142 if (errorVar->nOfSegments != numOfSegments)
00143 {
00144 free( errorVar->segments );
00145 errorVar->segments = NULL;
00146 errorVar->segments = (ercSegment_t *)malloc( numOfSegments*sizeof(ercSegment_t) );
00147 if ( errorVar->segments == NULL ) no_mem_exit("ercReset: errorVar->segments");
00148 errorVar->nOfSegments = numOfSegments;
00149 }
00150
00151 memset( errorVar->segments, 0, errorVar->nOfSegments*sizeof(ercSegment_t));
00152
00153 for ( i = 0; i < errorVar->nOfSegments; i++ )
00154 {
00155 errorVar->segments[i].fCorrupted = 1;
00156 errorVar->segments[i].startMBPos = 0;
00157 errorVar->segments[i].endMBPos = nOfMBs - 1;
00158 }
00159
00160 errorVar->currSegment = 0;
00161 errorVar->nOfCorruptedSegments = 0;
00162 }
00163 }
00164
00174 void ercClose( ercVariables_t *errorVar )
00175 {
00176 if ( errorVar != NULL )
00177 {
00178 if (errorVar->yCondition != NULL)
00179 {
00180 free( errorVar->segments );
00181 free( errorVar->yCondition );
00182 free( errorVar->uCondition );
00183 free( errorVar->vCondition );
00184 free( errorVar->prevFrameYCondition );
00185 }
00186 free( errorVar );
00187 errorVar = NULL;
00188 }
00189
00190 if (erc_object_list)
00191 {
00192 free(erc_object_list);
00193 erc_object_list=NULL;
00194 }
00195 }
00196
00207 void ercSetErrorConcealment( ercVariables_t *errorVar, int value )
00208 {
00209 if ( errorVar != NULL )
00210 errorVar->concealment = value;
00211 }
00212
00230 void ercStartSegment( int currMBNum, int segment, unsigned int bitPos, ercVariables_t *errorVar )
00231 {
00232 if ( errorVar && errorVar->concealment )
00233 {
00234 errorVar->currSegmentCorrupted = 0;
00235
00236 errorVar->segments[ segment ].fCorrupted = 0;
00237 errorVar->segments[ segment ].startMBPos = currMBNum;
00238
00239 }
00240 }
00241
00257 void ercStopSegment( int currMBNum, int segment, unsigned int bitPos, ercVariables_t *errorVar )
00258 {
00259 if ( errorVar && errorVar->concealment )
00260 {
00261 errorVar->segments[ segment ].endMBPos = currMBNum;
00262 errorVar->currSegment++;
00263 }
00264 }
00265
00277 void ercMarkCurrSegmentLost(int picSizeX, ercVariables_t *errorVar )
00278 {
00279 int j = 0;
00280 int current_segment;
00281
00282 current_segment = errorVar->currSegment-1;
00283 if ( errorVar && errorVar->concealment )
00284 {
00285 if (errorVar->currSegmentCorrupted == 0)
00286 {
00287 errorVar->nOfCorruptedSegments++;
00288 errorVar->currSegmentCorrupted = 1;
00289 }
00290
00291 for ( j = errorVar->segments[current_segment].startMBPos; j <= errorVar->segments[current_segment].endMBPos; j++ )
00292 {
00293 errorVar->yCondition[MBNum2YBlock (j, 0, picSizeX)] = ERC_BLOCK_CORRUPTED;
00294 errorVar->yCondition[MBNum2YBlock (j, 1, picSizeX)] = ERC_BLOCK_CORRUPTED;
00295 errorVar->yCondition[MBNum2YBlock (j, 2, picSizeX)] = ERC_BLOCK_CORRUPTED;
00296 errorVar->yCondition[MBNum2YBlock (j, 3, picSizeX)] = ERC_BLOCK_CORRUPTED;
00297 errorVar->uCondition[j] = ERC_BLOCK_CORRUPTED;
00298 errorVar->vCondition[j] = ERC_BLOCK_CORRUPTED;
00299 }
00300 errorVar->segments[current_segment].fCorrupted = 1;
00301 }
00302 }
00303
00315 void ercMarkCurrSegmentOK(int picSizeX, ercVariables_t *errorVar )
00316 {
00317 int j = 0;
00318 int current_segment;
00319
00320 current_segment = errorVar->currSegment-1;
00321 if ( errorVar && errorVar->concealment )
00322 {
00323
00324 for ( j = errorVar->segments[current_segment].startMBPos; j <= errorVar->segments[current_segment].endMBPos; j++ )
00325 {
00326 errorVar->yCondition[MBNum2YBlock (j, 0, picSizeX)] = ERC_BLOCK_OK;
00327 errorVar->yCondition[MBNum2YBlock (j, 1, picSizeX)] = ERC_BLOCK_OK;
00328 errorVar->yCondition[MBNum2YBlock (j, 2, picSizeX)] = ERC_BLOCK_OK;
00329 errorVar->yCondition[MBNum2YBlock (j, 3, picSizeX)] = ERC_BLOCK_OK;
00330 errorVar->uCondition[j] = ERC_BLOCK_OK;
00331 errorVar->vCondition[j] = ERC_BLOCK_OK;
00332 }
00333 errorVar->segments[current_segment].fCorrupted = 0;
00334 }
00335 }
00336
00351 void ercMarkCurrMBConcealed( int currMBNum, int comp, int picSizeX, ercVariables_t *errorVar )
00352 {
00353 int setAll = 0;
00354
00355 if ( errorVar && errorVar->concealment )
00356 {
00357 if (comp < 0)
00358 {
00359 setAll = 1;
00360 comp = 0;
00361 }
00362
00363 switch (comp)
00364 {
00365 case 0:
00366 errorVar->yCondition[MBNum2YBlock (currMBNum, 0, picSizeX)] = ERC_BLOCK_CONCEALED;
00367 errorVar->yCondition[MBNum2YBlock (currMBNum, 1, picSizeX)] = ERC_BLOCK_CONCEALED;
00368 errorVar->yCondition[MBNum2YBlock (currMBNum, 2, picSizeX)] = ERC_BLOCK_CONCEALED;
00369 errorVar->yCondition[MBNum2YBlock (currMBNum, 3, picSizeX)] = ERC_BLOCK_CONCEALED;
00370 if (!setAll)
00371 break;
00372 case 1:
00373 errorVar->uCondition[currMBNum] = ERC_BLOCK_CONCEALED;
00374 if (!setAll)
00375 break;
00376 case 2:
00377 errorVar->vCondition[currMBNum] = ERC_BLOCK_CONCEALED;
00378 }
00379 }
00380 }