1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*!
*************************************************************************************
* \file
* erc_do_i.c
*
* \brief
* Intra (I) frame error concealment algorithms for decoder
*
* \author
* - Ari Hourunranta <ari.hourunranta@nokia.com>
* - Viktor Varsa <viktor.varsa@nokia.com>
* - Ye-Kui Wang <wyk@ieee.org>
*
*************************************************************************************
*/
#include "global.h"
#include "erc_do.h"
static void concealBlocks ( VideoParameters *p_Vid, int lastColumn, int lastRow, int comp, frame *recfr, int picSizeX, int *condition );
static void pixMeanInterpolateBlock( VideoParameters *p_Vid, imgpel *src[], imgpel *block, int blockSize, int frameWidth );
/*!
************************************************************************
* \brief
* The main function for Intra frame concealment.
* Calls "concealBlocks" for each color component (Y,U,V) separately
* \return
* 0, if the concealment was not successful and simple concealment should be used
* 1, otherwise (even if none of the blocks were concealed)
* \param p_Vid
* image encoding parameters for current picture
* \param recfr
* Reconstructed frame buffer
* \param picSizeX
* Width of the frame in pixels
* \param picSizeY
* Height of the frame in pixels
* \param errorVar
* Variables for error concealment
************************************************************************
*/
int ercConcealIntraFrame( VideoParameters *p_Vid, frame *recfr, int picSizeX, int picSizeY, ercVariables_t *errorVar )
{
int lastColumn = 0, lastRow = 0;
// if concealment is on
if ( errorVar && errorVar->concealment )
{
// if there are segments to be concealed
if ( errorVar->nOfCorruptedSegments )
{
// Y
lastRow = (int) (picSizeY>>3);
lastColumn = (int) (picSizeX>>3);
concealBlocks( p_Vid, lastColumn, lastRow, 0, recfr, picSizeX, errorVar->yCondition );
// U (dimensions halved compared to Y)
lastRow = (int) (picSizeY>>4);
lastColumn = (int) (picSizeX>>4);
concealBlocks( p_Vid, lastColumn, lastRow, 1, recfr, picSizeX, errorVar->uCondition );
// V ( dimensions equal to U )
concealBlocks( p_Vid, lastColumn, lastRow, 2, recfr, picSizeX, errorVar->vCondition );
}
return 1;
}
else
return 0;
}
/*!
************************************************************************
* \brief
* Conceals the MB at position (row, column) using pixels from predBlocks[]
* using pixMeanInterpolateBlock()
* \param p_Vid
* image encoding parameters for current picture
* \param currFrame
* current frame
* \param row
* y coordinate in blocks
* \param column
* x coordinate in blocks
* \param predBlocks[]
* list of neighboring source blocks (numbering 0 to 7, 1 means: use the neighbor)
* \param frameWidth
* width of frame in pixels
* \param mbWidthInBlocks
* 2 for Y, 1 for U/V components
************************************************************************
*/
void ercPixConcealIMB(VideoParameters *p_Vid, imgpel *currFrame, int row, int column, int predBlocks[], int frameWidth, int mbWidthInBlocks)
{
imgpel *src[8]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
imgpel *currBlock = NULL;
// collect the reliable neighboring blocks
if (predBlocks[0])
src[0] = currFrame + (row-mbWidthInBlocks)*frameWidth*8 + (column+mbWidthInBlocks)*8;
if (predBlocks[1])
src[1] = currFrame + (row-mbWidthInBlocks)*frameWidth*8 + (column-mbWidthInBlocks)*8;
if (predBlocks[2])
src[2] = currFrame + (row+mbWidthInBlocks)*frameWidth*8 + (column-mbWidthInBlocks)*8;
if (predBlocks[3])
src[3] = currFrame + (row+mbWidthInBlocks)*frameWidth*8 + (column+mbWidthInBlocks)*8;
if (predBlocks[4])
src[4] = currFrame + (row-mbWidthInBlocks)*frameWidth*8 + column*8;
if (predBlocks[5])
src[5] = currFrame + row*frameWidth*8 + (column-mbWidthInBlocks)*8;
if (predBlocks[6])
src[6] = currFrame + (row+mbWidthInBlocks)*frameWidth*8 + column*8;
if (predBlocks[7])
src[7] = currFrame + row*frameWidth*8 + (column+mbWidthInBlocks)*8;
currBlock = currFrame + row*frameWidth*8 + column*8;
pixMeanInterpolateBlock( p_Vid, src, currBlock, mbWidthInBlocks*8, frameWidth );
}
/*!
************************************************************************
* \brief
* This function checks the neighbors of a Macroblock for usability in
* concealment. First the OK macroblocks are marked, and if there is not
* enough of them, then the CONCEALED ones as well.
* A "1" in the the output array means reliable, a "0" non reliable MB.
* The block order in "predBlocks":
* 1 4 0
* 5 x 7
* 2 6 3
* i.e., corners first.
* \return
* Number of useable neighbor macroblocks for concealment.
* \param predBlocks[]
* Array for indicating the valid neighbor blocks
* \param currRow
* Current block row in the frame
* \param currColumn
* Current block column in the frame
* \param condition
* The block condition (ok, lost) table
* \param maxRow
* Number of block rows in the frame
* \param maxColumn
* Number of block columns in the frame
* \param step
* Number of blocks belonging to a MB, when counting
* in vertical/horizontal direction. (Y:2 U,V:1)
* \param fNoCornerNeigh
* No corner neighbors are considered
************************************************************************
*/
int ercCollect8PredBlocks( int predBlocks[], int currRow, int currColumn, int *condition,
int maxRow, int maxColumn, int step, byte fNoCornerNeigh )
{
int srcCounter = 0;
int srcCountMin = (fNoCornerNeigh ? 2 : 4);
int threshold = ERC_BLOCK_OK;
memset( predBlocks, 0, 8*sizeof(int) );
// collect the reliable neighboring blocks
do
{
srcCounter = 0;
// top
if (currRow > 0 && condition[ (currRow-1)*maxColumn + currColumn ] >= threshold )
{ //ERC_BLOCK_OK (3) or ERC_BLOCK_CONCEALED (2)
predBlocks[4] = condition[ (currRow-1)*maxColumn + currColumn ];
srcCounter++;
}
// bottom
if ( currRow < (maxRow-step) && condition[ (currRow+step)*maxColumn + currColumn ] >= threshold )
{
predBlocks[6] = condition[ (currRow+step)*maxColumn + currColumn ];
srcCounter++;
}
if ( currColumn > 0 )
{
// left
if ( condition[ currRow*maxColumn + currColumn - 1 ] >= threshold )
{
predBlocks[5] = condition[ currRow*maxColumn + currColumn - 1 ];
srcCounter++;
}
if ( !fNoCornerNeigh )
{
// top-left
if ( currRow > 0 && condition[ (currRow-1)*maxColumn + currColumn - 1 ] >= threshold )
{
predBlocks[1] = condition[ (currRow-1)*maxColumn + currColumn - 1 ];
srcCounter++;
}
// bottom-left
if ( currRow < (maxRow-step) && condition[ (currRow+step)*maxColumn + currColumn - 1 ] >= threshold )
{
predBlocks[2] = condition[ (currRow+step)*maxColumn + currColumn - 1 ];
srcCounter++;
}
}
}
if ( currColumn < (maxColumn-step) )
{
// right
if ( condition[ currRow*maxColumn+currColumn + step ] >= threshold )
{
predBlocks[7] = condition[ currRow*maxColumn+currColumn + step ];
srcCounter++;
}
if ( !fNoCornerNeigh )
{
// top-right
if ( currRow > 0 && condition[ (currRow-1)*maxColumn + currColumn + step ] >= threshold )
{
predBlocks[0] = condition[ (currRow-1)*maxColumn + currColumn + step ];
srcCounter++;
}
// bottom-right
if ( currRow < (maxRow-step) && condition[ (currRow+step)*maxColumn + currColumn + step ] >= threshold )
{
predBlocks[3] = condition[ (currRow+step)*maxColumn + currColumn + step ];
srcCounter++;
}
}
}
// prepare for the next round
threshold--;
if (threshold < ERC_BLOCK_CONCEALED)
break;
} while ( srcCounter < srcCountMin);
return srcCounter;
}
/*!
************************************************************************
* \brief
* collects prediction blocks only from the current column
* \return
* Number of usable neighbour Macroblocks for concealment.
* \param predBlocks[]
* Array for indicating the valid neighbor blocks
* \param currRow
* Current block row in the frame
* \param currColumn
* Current block column in the frame
* \param condition
* The block condition (ok, lost) table
* \param maxRow
* Number of block rows in the frame
* \param maxColumn
* Number of block columns in the frame
* \param step
* Number of blocks belonging to a MB, when counting
* in vertical/horizontal direction. (Y:2 U,V:1)
************************************************************************
*/
int ercCollectColumnBlocks( int predBlocks[], int currRow, int currColumn, int *condition, int maxRow, int maxColumn, int step )
{
int srcCounter = 0, threshold = ERC_BLOCK_CORRUPTED;
memset( predBlocks, 0, 8*sizeof(int) );
// in this case, row > 0 and row < 17
if ( condition[ (currRow-1)*maxColumn + currColumn ] > threshold )
{
predBlocks[4] = 1;
srcCounter++;
}
if ( condition[ (currRow+step)*maxColumn + currColumn ] > threshold )
{
predBlocks[6] = 1;
srcCounter++;
}
return srcCounter;
}
/*!
************************************************************************
* \brief
* Core for the Intra blocks concealment.
* It is called for each color component (Y,U,V) separately
* Finds the corrupted blocks and calls pixel interpolation functions
* to correct them, one block at a time.
* Scanning is done vertically and each corrupted column is corrected
* bi-directionally, i.e., first block, last block, first block+1, last block -1 ...
* \param p_Vid
* image encoding parameters for current picture
* \param lastColumn
* Number of block columns in the frame
* \param lastRow
* Number of block rows in the frame
* \param comp
* color component
* \param recfr
* Reconstructed frame buffer
* \param picSizeX
* Width of the frame in pixels
* \param condition
* The block condition (ok, lost) table
************************************************************************
*/
static void concealBlocks( VideoParameters *p_Vid, int lastColumn, int lastRow, int comp, frame *recfr, int picSizeX, int *condition )
{
int row, column, srcCounter = 0, thr = ERC_BLOCK_CORRUPTED,
lastCorruptedRow = -1, firstCorruptedRow = -1, currRow = 0,
areaHeight = 0, i = 0, smoothColumn = 0;
int predBlocks[8], step = 1;
// in the Y component do the concealment MB-wise (not block-wise):
// this is useful if only whole MBs can be damaged or lost
if ( comp == 0 )
step = 2;
else
step = 1;
for ( column = 0; column < lastColumn; column += step )
{
for ( row = 0; row < lastRow; row += step )
{
if ( condition[row*lastColumn+column] <= thr )
{
firstCorruptedRow = row;
// find the last row which has corrupted blocks (in same continuous area)
for ( lastCorruptedRow = row+step; lastCorruptedRow < lastRow; lastCorruptedRow += step )
{
// check blocks in the current column
if ( condition[ lastCorruptedRow*lastColumn + column ] > thr )
{
// current one is already OK, so the last was the previous one
lastCorruptedRow -= step;
break;
}
}
if ( lastCorruptedRow >= lastRow )
{
// correct only from above
lastCorruptedRow = lastRow-step;
for ( currRow = firstCorruptedRow; currRow < lastRow; currRow += step )
{
srcCounter = ercCollect8PredBlocks( predBlocks, currRow, column, condition, lastRow, lastColumn, step, 1 );
switch( comp )
{
case 0 :
ercPixConcealIMB( p_Vid, recfr->yptr, currRow, column, predBlocks, picSizeX, 2 );
break;
case 1 :
ercPixConcealIMB( p_Vid, recfr->uptr, currRow, column, predBlocks, (picSizeX>>1), 1 );
break;
case 2 :
ercPixConcealIMB( p_Vid, recfr->vptr, currRow, column, predBlocks, (picSizeX>>1), 1 );
break;
}
if ( comp == 0 )
{
condition[ currRow*lastColumn+column] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + 1] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + lastColumn] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + lastColumn + 1] = ERC_BLOCK_CONCEALED;
}
else
{
condition[ currRow*lastColumn+column] = ERC_BLOCK_CONCEALED;
}
}
row = lastRow;
}
else if ( firstCorruptedRow == 0 )
{
// correct only from below
for ( currRow = lastCorruptedRow; currRow >= 0; currRow -= step )
{
srcCounter = ercCollect8PredBlocks( predBlocks, currRow, column, condition, lastRow, lastColumn, step, 1 );
switch( comp )
{
case 0 :
ercPixConcealIMB( p_Vid, recfr->yptr, currRow, column, predBlocks, picSizeX, 2 );
break;
case 1 :
ercPixConcealIMB( p_Vid, recfr->uptr, currRow, column, predBlocks, (picSizeX>>1), 1 );
break;
case 2 :
ercPixConcealIMB( p_Vid, recfr->vptr, currRow, column, predBlocks, (picSizeX>>1), 1 );
break;
}
if ( comp == 0 )
{
condition[ currRow*lastColumn+column] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + 1] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + lastColumn] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + lastColumn + 1] = ERC_BLOCK_CONCEALED;
}
else
{
condition[ currRow*lastColumn+column] = ERC_BLOCK_CONCEALED;
}
}
row = lastCorruptedRow+step;
}
else
{
// correct bi-directionally
row = lastCorruptedRow+step;
areaHeight = lastCorruptedRow-firstCorruptedRow+step;
// Conceal the corrupted area switching between the up and the bottom rows
for ( i = 0; i < areaHeight; i += step )
{
if ( i % 2 )
{
currRow = lastCorruptedRow;
lastCorruptedRow -= step;
}
else
{
currRow = firstCorruptedRow;
firstCorruptedRow += step;
}
if (smoothColumn > 0)
{
srcCounter = ercCollectColumnBlocks( predBlocks, currRow, column, condition, lastRow, lastColumn, step );
}
else
{
srcCounter = ercCollect8PredBlocks( predBlocks, currRow, column, condition, lastRow, lastColumn, step, 1 );
}
switch( comp )
{
case 0 :
ercPixConcealIMB( p_Vid, recfr->yptr, currRow, column, predBlocks, picSizeX, 2 );
break;
case 1 :
ercPixConcealIMB( p_Vid, recfr->uptr, currRow, column, predBlocks, (picSizeX>>1), 1 );
break;
case 2 :
ercPixConcealIMB( p_Vid, recfr->vptr, currRow, column, predBlocks, (picSizeX>>1), 1 );
break;
}
if ( comp == 0 )
{
condition[ currRow*lastColumn+column] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + 1] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + lastColumn] = ERC_BLOCK_CONCEALED;
condition[ currRow*lastColumn+column + lastColumn + 1] = ERC_BLOCK_CONCEALED;
}
else
{
condition[ currRow*lastColumn+column ] = ERC_BLOCK_CONCEALED;
}
}
}
lastCorruptedRow = -1;
firstCorruptedRow = -1;
}
}
}
}
/*!
************************************************************************
* \brief
* Does the actual pixel based interpolation for block[]
* using weighted average
* \param p_Vid
* image encoding parameters for current picture
* \param src[]
* pointers to neighboring source blocks
* \param block
* destination block
* \param blockSize
* 16 for Y, 8 for U/V components
* \param frameWidth
* Width of the frame in pixels
************************************************************************
*/
static void pixMeanInterpolateBlock( VideoParameters *p_Vid, imgpel *src[], imgpel *block, int blockSize, int frameWidth )
{
int row, column, k, tmp, srcCounter = 0, weight = 0, bmax = blockSize - 1;
k = 0;
for ( row = 0; row < blockSize; row++ )
{
for ( column = 0; column < blockSize; column++ )
{
tmp = 0;
srcCounter = 0;
// above
if ( src[4] != NULL )
{
weight = blockSize-row;
tmp += weight * (*(src[4]+bmax*frameWidth+column));
srcCounter += weight;
}
// left
if ( src[5] != NULL )
{
weight = blockSize-column;
tmp += weight * (*(src[5]+row*frameWidth+bmax));
srcCounter += weight;
}
// below
if ( src[6] != NULL )
{
weight = row+1;
tmp += weight * (*(src[6]+column));
srcCounter += weight;
}
// right
if ( src[7] != NULL )
{
weight = column+1;
tmp += weight * (*(src[7]+row*frameWidth));
srcCounter += weight;
}
if ( srcCounter > 0 )
block[ k + column ] = (byte)(tmp/srcCounter);
else
block[ k + column ] = blockSize == 8 ? p_Vid->dc_pred_value_comp[1] : p_Vid->dc_pred_value_comp[0];
}
k += frameWidth;
}
}
|