typo fix: inited --> initialized
[libav.git] / libavcodec / error_resilience.c
CommitLineData
4d2858de
MN
1/*
2 * Error resilience / concealment
3 *
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4d2858de 5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
4d2858de
MN
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
4d2858de 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
4d2858de
MN
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4d2858de 21 */
115329f1 22
983e3246
MN
23/**
24 * @file error_resilience.c
25 * Error resilience / concealment.
26 */
4d2858de 27
7bc9090a 28#include <limits.h>
115329f1 29
4d2858de
MN
30#include "avcodec.h"
31#include "dsputil.h"
32#include "mpegvideo.h"
33
7d1c3fc1
MN
34static void decode_mb(MpegEncContext *s){
35 s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize ) + s->mb_x * 16;
36 s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * s->uvlinesize) + s->mb_x * 8;
37 s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * s->uvlinesize) + s->mb_x * 8;
38
115329f1 39 MPV_decode_mb(s, s->block);
7d1c3fc1
MN
40}
41
4d2858de
MN
42/**
43 * replaces the current MB with a flat dc only version.
44 */
45static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
46{
47 int dc, dcu, dcv, y, i;
48 for(i=0; i<4; i++){
137c8468 49 dc= s->dc_val[0][mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*s->b8_stride];
4d2858de
MN
50 if(dc<0) dc=0;
51 else if(dc>2040) dc=2040;
52 for(y=0; y<8; y++){
53 int x;
54 for(x=0; x<8; x++){
55 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
56 }
57 }
58 }
137c8468
MN
59 dcu = s->dc_val[1][mb_x + mb_y*s->mb_stride];
60 dcv = s->dc_val[2][mb_x + mb_y*s->mb_stride];
4d2858de
MN
61 if (dcu<0 ) dcu=0;
62 else if(dcu>2040) dcu=2040;
63 if (dcv<0 ) dcv=0;
64 else if(dcv>2040) dcv=2040;
65 for(y=0; y<8; y++){
66 int x;
67 for(x=0; x<8; x++){
68 dest_cb[x + y*(s->uvlinesize)]= dcu/8;
69 dest_cr[x + y*(s->uvlinesize)]= dcv/8;
70 }
71 }
72}
73
b86216de 74static void filter181(int16_t *data, int width, int height, int stride){
4d2858de
MN
75 int x,y;
76
77 /* horizontal filter */
78 for(y=1; y<height-1; y++){
79 int prev_dc= data[0 + y*stride];
80
81 for(x=1; x<width-1; x++){
82 int dc;
115329f1
DB
83
84 dc= - prev_dc
4d2858de
MN
85 + data[x + y*stride]*8
86 - data[x + 1 + y*stride];
87 dc= (dc*10923 + 32768)>>16;
88 prev_dc= data[x + y*stride];
89 data[x + y*stride]= dc;
90 }
91 }
115329f1 92
4d2858de
MN
93 /* vertical filter */
94 for(x=1; x<width-1; x++){
95 int prev_dc= data[x];
96
97 for(y=1; y<height-1; y++){
98 int dc;
115329f1
DB
99
100 dc= - prev_dc
4d2858de
MN
101 + data[x + y *stride]*8
102 - data[x + (y+1)*stride];
103 dc= (dc*10923 + 32768)>>16;
104 prev_dc= data[x + y*stride];
105 data[x + y*stride]= dc;
106 }
107 }
108}
109
110/**
755bfeab 111 * guess the dc of blocks which do not have an undamaged dc
bb270c08
DB
112 * @param w width in 8 pixel blocks
113 * @param h height in 8 pixel blocks
4d2858de 114 */
b86216de 115static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
4d2858de
MN
116 int b_x, b_y;
117
118 for(b_y=0; b_y<h; b_y++){
119 for(b_x=0; b_x<w; b_x++){
120 int color[4]={1024,1024,1024,1024};
121 int distance[4]={9999,9999,9999,9999};
122 int mb_index, error, j;
0c1a9eda 123 int64_t guess, weight_sum;
115329f1 124
7bc9090a 125 mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
115329f1 126
4d2858de 127 error= s->error_status_table[mb_index];
115329f1 128
7bc9090a 129 if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
4d2858de 130 if(!(error&DC_ERROR)) continue; //dc-ok
115329f1 131
4d2858de
MN
132 /* right block */
133 for(j=b_x+1; j<w; j++){
7bc9090a 134 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
4d2858de 135 int error_j= s->error_status_table[mb_index_j];
7bc9090a 136 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
4d2858de
MN
137 if(intra_j==0 || !(error_j&DC_ERROR)){
138 color[0]= dc[j + b_y*stride];
139 distance[0]= j-b_x;
140 break;
141 }
142 }
115329f1 143
4d2858de
MN
144 /* left block */
145 for(j=b_x-1; j>=0; j--){
7bc9090a 146 int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
4d2858de 147 int error_j= s->error_status_table[mb_index_j];
7bc9090a 148 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
4d2858de
MN
149 if(intra_j==0 || !(error_j&DC_ERROR)){
150 color[1]= dc[j + b_y*stride];
151 distance[1]= b_x-j;
152 break;
153 }
154 }
155
156 /* bottom block */
157 for(j=b_y+1; j<h; j++){
7bc9090a 158 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
4d2858de 159 int error_j= s->error_status_table[mb_index_j];
7bc9090a 160 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
4d2858de
MN
161 if(intra_j==0 || !(error_j&DC_ERROR)){
162 color[2]= dc[b_x + j*stride];
163 distance[2]= j-b_y;
164 break;
165 }
166 }
167
168 /* top block */
169 for(j=b_y-1; j>=0; j--){
7bc9090a 170 int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
4d2858de 171 int error_j= s->error_status_table[mb_index_j];
7bc9090a 172 int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
4d2858de
MN
173 if(intra_j==0 || !(error_j&DC_ERROR)){
174 color[3]= dc[b_x + j*stride];
175 distance[3]= b_y-j;
176 break;
177 }
178 }
115329f1 179
4d2858de
MN
180 weight_sum=0;
181 guess=0;
182 for(j=0; j<4; j++){
0c1a9eda
ZK
183 int64_t weight= 256*256*256*16/distance[j];
184 guess+= weight*(int64_t)color[j];
4d2858de
MN
185 weight_sum+= weight;
186 }
187 guess= (guess + weight_sum/2) / weight_sum;
188
189 dc[b_x + b_y*stride]= guess;
190 }
191 }
192}
193
194/**
195 * simple horizontal deblocking filter used for error resilience
bb270c08
DB
196 * @param w width in 8 pixel blocks
197 * @param h height in 8 pixel blocks
4d2858de 198 */
0c1a9eda 199static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
4d2858de 200 int b_x, b_y;
55fde95e 201 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
4d2858de
MN
202
203 for(b_y=0; b_y<h; b_y++){
204 for(b_x=0; b_x<w-1; b_x++){
205 int y;
7bc9090a
MN
206 int left_status = s->error_status_table[( b_x >>is_luma) + (b_y>>is_luma)*s->mb_stride];
207 int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
208 int left_intra= IS_INTRA(s->current_picture.mb_type [( b_x >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
209 int right_intra= IS_INTRA(s->current_picture.mb_type [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
4d2858de
MN
210 int left_damage = left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
211 int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
212 int offset= b_x*8 + b_y*stride*8;
137c8468
MN
213 int16_t *left_mv= s->current_picture.motion_val[0][s->b8_stride*(b_y<<(1-is_luma)) + ( b_x <<(1-is_luma))];
214 int16_t *right_mv= s->current_picture.motion_val[0][s->b8_stride*(b_y<<(1-is_luma)) + ((b_x+1)<<(1-is_luma))];
115329f1 215
4d2858de 216 if(!(left_damage||right_damage)) continue; // both undamaged
115329f1
DB
217
218 if( (!left_intra) && (!right_intra)
c26abfa5 219 && FFABS(left_mv[0]-right_mv[0]) + FFABS(left_mv[1]+right_mv[1]) < 2) continue;
115329f1 220
4d2858de
MN
221 for(y=0; y<8; y++){
222 int a,b,c,d;
115329f1 223
4d2858de
MN
224 a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
225 b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
226 c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
115329f1 227
c26abfa5 228 d= FFABS(b) - ((FFABS(a) + FFABS(c) + 1)>>1);
b8a78f41 229 d= FFMAX(d, 0);
4d2858de 230 if(b<0) d= -d;
115329f1 231
4d2858de
MN
232 if(d==0) continue;
233
234 if(!(left_damage && right_damage))
235 d= d*16/9;
115329f1 236
4d2858de
MN
237 if(left_damage){
238 dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
239 dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
240 dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
241 dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
242 }
243 if(right_damage){
244 dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
245 dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
246 dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
247 dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
248 }
249 }
250 }
251 }
252}
253
254/**
255 * simple vertical deblocking filter used for error resilience
bb270c08
DB
256 * @param w width in 8 pixel blocks
257 * @param h height in 8 pixel blocks
4d2858de 258 */
0c1a9eda 259static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
4d2858de 260 int b_x, b_y;
55fde95e 261 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
4d2858de
MN
262
263 for(b_y=0; b_y<h-1; b_y++){
264 for(b_x=0; b_x<w; b_x++){
265 int x;
7bc9090a
MN
266 int top_status = s->error_status_table[(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_stride];
267 int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
268 int top_intra= IS_INTRA(s->current_picture.mb_type [(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_stride]);
269 int bottom_intra= IS_INTRA(s->current_picture.mb_type [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
4d2858de
MN
270 int top_damage = top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
271 int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
272 int offset= b_x*8 + b_y*stride*8;
137c8468
MN
273 int16_t *top_mv= s->current_picture.motion_val[0][s->b8_stride*( b_y <<(1-is_luma)) + (b_x<<(1-is_luma))];
274 int16_t *bottom_mv= s->current_picture.motion_val[0][s->b8_stride*((b_y+1)<<(1-is_luma)) + (b_x<<(1-is_luma))];
115329f1 275
4d2858de 276 if(!(top_damage||bottom_damage)) continue; // both undamaged
115329f1
DB
277
278 if( (!top_intra) && (!bottom_intra)
c26abfa5 279 && FFABS(top_mv[0]-bottom_mv[0]) + FFABS(top_mv[1]+bottom_mv[1]) < 2) continue;
115329f1 280
4d2858de
MN
281 for(x=0; x<8; x++){
282 int a,b,c,d;
115329f1 283
4d2858de
MN
284 a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
285 b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
286 c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
115329f1 287
c26abfa5 288 d= FFABS(b) - ((FFABS(a) + FFABS(c)+1)>>1);
b8a78f41 289 d= FFMAX(d, 0);
4d2858de 290 if(b<0) d= -d;
115329f1 291
4d2858de
MN
292 if(d==0) continue;
293
294 if(!(top_damage && bottom_damage))
295 d= d*16/9;
115329f1 296
4d2858de
MN
297 if(top_damage){
298 dst[offset + x + 7*stride] = cm[dst[offset + x + 7*stride] + ((d*7)>>4)];
299 dst[offset + x + 6*stride] = cm[dst[offset + x + 6*stride] + ((d*5)>>4)];
300 dst[offset + x + 5*stride] = cm[dst[offset + x + 5*stride] + ((d*3)>>4)];
301 dst[offset + x + 4*stride] = cm[dst[offset + x + 4*stride] + ((d*1)>>4)];
302 }
303 if(bottom_damage){
304 dst[offset + x + 8*stride] = cm[dst[offset + x + 8*stride] - ((d*7)>>4)];
305 dst[offset + x + 9*stride] = cm[dst[offset + x + 9*stride] - ((d*5)>>4)];
306 dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
307 dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
308 }
309 }
310 }
311 }
312}
313
314static void guess_mv(MpegEncContext *s){
7bc9090a 315 uint8_t fixed[s->mb_stride * s->mb_height];
4d2858de
MN
316#define MV_FROZEN 3
317#define MV_CHANGED 2
318#define MV_UNCHANGED 1
7bc9090a 319 const int mb_stride = s->mb_stride;
4d2858de
MN
320 const int mb_width = s->mb_width;
321 const int mb_height= s->mb_height;
322 int i, depth, num_avail;
7bc9090a 323 int mb_x, mb_y;
115329f1 324
4d2858de
MN
325 num_avail=0;
326 for(i=0; i<s->mb_num; i++){
7bc9090a 327 const int mb_xy= s->mb_index2xy[ i ];
4d2858de 328 int f=0;
7bc9090a 329 int error= s->error_status_table[mb_xy];
4d2858de 330
7bc9090a 331 if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
4d2858de 332 if(!(error&MV_ERROR)) f=MV_FROZEN; //inter with undamaged MV
115329f1 333
7bc9090a 334 fixed[mb_xy]= f;
4d2858de
MN
335 if(f==MV_FROZEN)
336 num_avail++;
337 }
115329f1 338
4d2858de 339 if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
4d2858de
MN
340 for(mb_y=0; mb_y<s->mb_height; mb_y++){
341 for(mb_x=0; mb_x<s->mb_width; mb_x++){
7bc9090a 342 const int mb_xy= mb_x + mb_y*s->mb_stride;
115329f1 343
7bc9090a
MN
344 if(IS_INTRA(s->current_picture.mb_type[mb_xy])) continue;
345 if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
4d2858de
MN
346
347 s->mv_dir = MV_DIR_FORWARD;
348 s->mb_intra=0;
349 s->mv_type = MV_TYPE_16X16;
160d679c 350 s->mb_skipped=0;
4d2858de 351
bb270c08 352 s->dsp.clear_blocks(s->block[0]);
4d2858de
MN
353
354 s->mb_x= mb_x;
355 s->mb_y= mb_y;
356 s->mv[0][0][0]= 0;
357 s->mv[0][0][1]= 0;
7d1c3fc1 358 decode_mb(s);
4d2858de
MN
359 }
360 }
361 return;
362 }
115329f1 363
4d2858de
MN
364 for(depth=0;; depth++){
365 int changed, pass, none_left;
366
367 none_left=1;
368 changed=1;
369 for(pass=0; (changed || pass<2) && pass<10; pass++){
7bc9090a 370 int mb_x, mb_y;
4d2858de 371int score_sum=0;
115329f1 372
4d2858de 373 changed=0;
4d2858de
MN
374 for(mb_y=0; mb_y<s->mb_height; mb_y++){
375 for(mb_x=0; mb_x<s->mb_width; mb_x++){
7bc9090a 376 const int mb_xy= mb_x + mb_y*s->mb_stride;
4d2858de
MN
377 int mv_predictor[8][2]={{0}};
378 int pred_count=0;
379 int j;
380 int best_score=256*256*256*64;
381 int best_pred=0;
137c8468
MN
382 const int mot_stride= s->b8_stride;
383 const int mot_index= mb_x*2 + mb_y*2*mot_stride;
8d7ec294
WH
384 int prev_x= s->current_picture.motion_val[0][mot_index][0];
385 int prev_y= s->current_picture.motion_val[0][mot_index][1];
4d2858de 386
4d2858de 387 if((mb_x^mb_y^pass)&1) continue;
115329f1 388
7bc9090a
MN
389 if(fixed[mb_xy]==MV_FROZEN) continue;
390 assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
391 assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
115329f1 392
4d2858de 393 j=0;
7bc9090a
MN
394 if(mb_x>0 && fixed[mb_xy-1 ]==MV_FROZEN) j=1;
395 if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_FROZEN) j=1;
396 if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
397 if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
4d2858de
MN
398 if(j==0) continue;
399
400 j=0;
7bc9090a
MN
401 if(mb_x>0 && fixed[mb_xy-1 ]==MV_CHANGED) j=1;
402 if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_CHANGED) j=1;
403 if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
404 if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
4d2858de 405 if(j==0 && pass>1) continue;
115329f1 406
4d2858de 407 none_left=0;
115329f1 408
7bc9090a 409 if(mb_x>0 && fixed[mb_xy-1]){
8d7ec294
WH
410 mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - 2][0];
411 mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - 2][1];
4d2858de
MN
412 pred_count++;
413 }
7bc9090a 414 if(mb_x+1<mb_width && fixed[mb_xy+1]){
8d7ec294
WH
415 mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + 2][0];
416 mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + 2][1];
4d2858de
MN
417 pred_count++;
418 }
7bc9090a 419 if(mb_y>0 && fixed[mb_xy-mb_stride]){
8d7ec294
WH
420 mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*2][0];
421 mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*2][1];
4d2858de
MN
422 pred_count++;
423 }
7bc9090a 424 if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
8d7ec294
WH
425 mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*2][0];
426 mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*2][1];
4d2858de
MN
427 pred_count++;
428 }
429 if(pred_count==0) continue;
115329f1 430
4d2858de
MN
431 if(pred_count>1){
432 int sum_x=0, sum_y=0;
433 int max_x, max_y, min_x, min_y;
434
435 for(j=0; j<pred_count; j++){
436 sum_x+= mv_predictor[j][0];
437 sum_y+= mv_predictor[j][1];
438 }
115329f1 439
4d2858de
MN
440 /* mean */
441 mv_predictor[pred_count][0] = sum_x/j;
442 mv_predictor[pred_count][1] = sum_y/j;
115329f1 443
4d2858de
MN
444 /* median */
445 if(pred_count>=3){
446 min_y= min_x= 99999;
447 max_y= max_x=-99999;
448 }else{
449 min_x=min_y=max_x=max_y=0;
450 }
451 for(j=0; j<pred_count; j++){
b8a78f41
MN
452 max_x= FFMAX(max_x, mv_predictor[j][0]);
453 max_y= FFMAX(max_y, mv_predictor[j][1]);
454 min_x= FFMIN(min_x, mv_predictor[j][0]);
455 min_y= FFMIN(min_y, mv_predictor[j][1]);
4d2858de
MN
456 }
457 mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
458 mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
115329f1 459
4d2858de
MN
460 if(pred_count==4){
461 mv_predictor[pred_count+1][0] /= 2;
462 mv_predictor[pred_count+1][1] /= 2;
463 }
464 pred_count+=2;
465 }
115329f1 466
4d2858de
MN
467 /* zero MV */
468 pred_count++;
469
470 /* last MV */
8d7ec294
WH
471 mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];
472 mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];
115329f1
DB
473 pred_count++;
474
4d2858de
MN
475 s->mv_dir = MV_DIR_FORWARD;
476 s->mb_intra=0;
477 s->mv_type = MV_TYPE_16X16;
160d679c 478 s->mb_skipped=0;
4d2858de 479
bb270c08 480 s->dsp.clear_blocks(s->block[0]);
4d2858de
MN
481
482 s->mb_x= mb_x;
483 s->mb_y= mb_y;
7bc9090a 484
4d2858de
MN
485 for(j=0; j<pred_count; j++){
486 int score=0;
0c1a9eda 487 uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
4d2858de 488
8d7ec294
WH
489 s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
490 s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
7bc9090a 491
7d1c3fc1 492 decode_mb(s);
115329f1 493
7bc9090a 494 if(mb_x>0 && fixed[mb_xy-1]){
4d2858de
MN
495 int k;
496 for(k=0; k<16; k++)
c26abfa5 497 score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);
4d2858de 498 }
7bc9090a 499 if(mb_x+1<mb_width && fixed[mb_xy+1]){
4d2858de
MN
500 int k;
501 for(k=0; k<16; k++)
c26abfa5 502 score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
4d2858de 503 }
7bc9090a 504 if(mb_y>0 && fixed[mb_xy-mb_stride]){
4d2858de
MN
505 int k;
506 for(k=0; k<16; k++)
c26abfa5 507 score += FFABS(src[k-s->linesize ]-src[k ]);
4d2858de 508 }
7bc9090a 509 if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
4d2858de
MN
510 int k;
511 for(k=0; k<16; k++)
c26abfa5 512 score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
4d2858de 513 }
115329f1 514
4d2858de
MN
515 if(score <= best_score){ // <= will favor the last MV
516 best_score= score;
517 best_pred= j;
518 }
519 }
520score_sum+= best_score;
8d7ec294
WH
521//FIXME no need to set s->current_picture.motion_val[0][mot_index][0] explicit
522 s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
523 s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
4d2858de 524
7d1c3fc1 525 decode_mb(s);
4d2858de 526
115329f1 527
4d2858de 528 if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
7bc9090a 529 fixed[mb_xy]=MV_CHANGED;
4d2858de
MN
530 changed++;
531 }else
7bc9090a 532 fixed[mb_xy]=MV_UNCHANGED;
4d2858de
MN
533 }
534 }
535
536// printf(".%d/%d", changed, score_sum); fflush(stdout);
537 }
115329f1
DB
538
539 if(none_left)
4d2858de 540 return;
115329f1 541
4d2858de 542 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
543 int mb_xy= s->mb_index2xy[i];
544 if(fixed[mb_xy])
545 fixed[mb_xy]=MV_FROZEN;
4d2858de
MN
546 }
547// printf(":"); fflush(stdout);
548 }
549}
115329f1 550
4d2858de
MN
551static int is_intra_more_likely(MpegEncContext *s){
552 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
115329f1 553
b536d0aa 554 if(s->last_picture_ptr==NULL) return 1; //no previous frame available -> use spatial prediction
4d2858de
MN
555
556 undamaged_count=0;
557 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
558 const int mb_xy= s->mb_index2xy[i];
559 const int error= s->error_status_table[mb_xy];
4d2858de
MN
560 if(!((error&DC_ERROR) && (error&MV_ERROR)))
561 undamaged_count++;
562 }
115329f1 563
4d2858de 564 if(undamaged_count < 5) return 0; //allmost all MBs damaged -> use temporal prediction
115329f1 565
551109d4
IK
566#ifdef HAVE_XVMC
567 //prevent dsp.sad() check, that requires access to the image
568 if(s->avctx->xvmc_acceleration && s->pict_type==I_TYPE) return 1;
569#endif
570
115329f1 571 skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
4d2858de
MN
572 is_intra_likely=0;
573
574 j=0;
4d2858de
MN
575 for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
576 for(mb_x= 0; mb_x<s->mb_width; mb_x++){
577 int error;
7bc9090a 578 const int mb_xy= mb_x + mb_y*s->mb_stride;
4d2858de 579
7bc9090a 580 error= s->error_status_table[mb_xy];
4d2858de
MN
581 if((error&DC_ERROR) && (error&MV_ERROR))
582 continue; //skip damaged
115329f1
DB
583
584 j++;
4d2858de 585 if((j%skip_amount) != 0) continue; //skip a few to speed things up
115329f1 586
4d2858de 587 if(s->pict_type==I_TYPE){
0c1a9eda
ZK
588 uint8_t *mb_ptr = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
589 uint8_t *last_mb_ptr= s->last_picture.data [0] + mb_x*16 + mb_y*16*s->linesize;
115329f1 590
bb270c08 591 is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);
bb198e19 592 is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
4d2858de 593 }else{
7bc9090a 594 if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
4d2858de
MN
595 is_intra_likely++;
596 else
597 is_intra_likely--;
598 }
599 }
600 }
601//printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
115329f1 602 return is_intra_likely > 0;
4d2858de
MN
603}
604
46b4feec
MN
605void ff_er_frame_start(MpegEncContext *s){
606 if(!s->error_resilience) return;
607
7bc9090a
MN
608 memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_stride*s->mb_height*sizeof(uint8_t));
609 s->error_count= 3*s->mb_num;
46b4feec
MN
610}
611
612/**
613 * adds a slice.
614 * @param endx x component of the last macroblock, can be -1 for the last of the previous line
615 * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
616 * error of the same type occured
617 */
618void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
f66e4f5f
RD
619 const int start_i= av_clip(startx + starty * s->mb_width , 0, s->mb_num-1);
620 const int end_i = av_clip(endx + endy * s->mb_width , 0, s->mb_num);
7bc9090a
MN
621 const int start_xy= s->mb_index2xy[start_i];
622 const int end_xy = s->mb_index2xy[end_i];
46b4feec 623 int mask= -1;
04cfef21
MN
624
625 if(start_i > end_i || start_xy > end_xy){
626 av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before start\n");
627 return;
628 }
115329f1 629
46b4feec
MN
630 if(!s->error_resilience) return;
631
632 mask &= ~VP_START;
7bc9090a
MN
633 if(status & (AC_ERROR|AC_END)){
634 mask &= ~(AC_ERROR|AC_END);
635 s->error_count -= end_i - start_i + 1;
636 }
637 if(status & (DC_ERROR|DC_END)){
638 mask &= ~(DC_ERROR|DC_END);
639 s->error_count -= end_i - start_i + 1;
640 }
641 if(status & (MV_ERROR|MV_END)){
642 mask &= ~(MV_ERROR|MV_END);
643 s->error_count -= end_i - start_i + 1;
644 }
645
646 if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
46b4feec
MN
647
648 if(mask == ~0x7F){
7bc9090a 649 memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
46b4feec
MN
650 }else{
651 int i;
652 for(i=start_xy; i<end_xy; i++){
7bc9090a 653 s->error_status_table[ i ] &= mask;
46b4feec
MN
654 }
655 }
7bc9090a 656
115329f1 657 if(end_i == s->mb_num)
7bc9090a
MN
658 s->error_count= INT_MAX;
659 else{
46b4feec
MN
660 s->error_status_table[end_xy] &= mask;
661 s->error_status_table[end_xy] |= status;
662 }
115329f1 663
0aae3f43 664 s->error_status_table[start_xy] |= VP_START;
7bc9090a 665
0dfd33c3 666 if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
7bc9090a 667 int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
115329f1 668
7bc9090a
MN
669 prev_status &= ~ VP_START;
670 if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
671 }
46b4feec
MN
672}
673
674void ff_er_frame_end(MpegEncContext *s){
4cfbbbde 675 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
4d2858de
MN
676 int distance;
677 int threshold_part[4]= {100,100,100};
678 int threshold= 50;
679 int is_intra_likely;
ec892563
MN
680 int size = s->b8_stride * 2 * s->mb_height;
681 Picture *pic= s->current_picture_ptr;
115329f1
DB
682
683 if(!s->error_resilience || s->error_count==0 ||
0dfd33c3 684 s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
46b4feec 685
8d7ec294 686 if(s->current_picture.motion_val[0] == NULL){
9b879566 687 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
115329f1 688
967be700 689 for(i=0; i<2; i++){
6c08a057 690 pic->ref_index[i]= av_mallocz(size * sizeof(uint8_t));
02dc8983
MN
691 pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
692 pic->motion_val[i]= pic->motion_val_base[i]+4;
967be700
MN
693 }
694 pic->motion_subsample_log2= 3;
695 s->current_picture= *s->current_picture_ptr;
46b4feec 696 }
115329f1 697
ec892563
MN
698 for(i=0; i<2; i++){
699 if(pic->ref_index[i])
700 memset(pic->ref_index[i], 0, size * sizeof(uint8_t));
701 }
702
7bc9090a
MN
703 if(s->avctx->debug&FF_DEBUG_ER){
704 for(mb_y=0; mb_y<s->mb_height; mb_y++){
705 for(mb_x=0; mb_x<s->mb_width; mb_x++){
706 int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
115329f1
DB
707
708 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
7bc9090a 709 }
9b879566 710 av_log(s->avctx, AV_LOG_DEBUG, "\n");
46b4feec
MN
711 }
712 }
115329f1 713
4d2858de
MN
714#if 1
715 /* handle overlapping slices */
716 for(error_type=1; error_type<=3; error_type++){
717 int end_ok=0;
718
719 for(i=s->mb_num-1; i>=0; i--){
7bc9090a
MN
720 const int mb_xy= s->mb_index2xy[i];
721 int error= s->error_status_table[mb_xy];
115329f1 722
4d2858de
MN
723 if(error&(1<<error_type))
724 end_ok=1;
725 if(error&(8<<error_type))
726 end_ok=1;
727
728 if(!end_ok)
7bc9090a 729 s->error_status_table[mb_xy]|= 1<<error_type;
4d2858de
MN
730
731 if(error&VP_START)
732 end_ok=0;
733 }
734 }
735#endif
736#if 1
737 /* handle slices with partitions of different length */
738 if(s->partitioned_frame){
739 int end_ok=0;
740
741 for(i=s->mb_num-1; i>=0; i--){
7bc9090a
MN
742 const int mb_xy= s->mb_index2xy[i];
743 int error= s->error_status_table[mb_xy];
115329f1 744
4d2858de
MN
745 if(error&AC_END)
746 end_ok=0;
747 if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
748 end_ok=1;
749
750 if(!end_ok)
7bc9090a 751 s->error_status_table[mb_xy]|= AC_ERROR;
4d2858de
MN
752
753 if(error&VP_START)
754 end_ok=0;
755 }
756 }
757#endif
758 /* handle missing slices */
759 if(s->error_resilience>=4){
760 int end_ok=1;
115329f1 761
4d2858de 762 for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
7bc9090a
MN
763 const int mb_xy= s->mb_index2xy[i];
764 int error1= s->error_status_table[mb_xy ];
db6e7795 765 int error2= s->error_status_table[s->mb_index2xy[i+1]];
115329f1 766
4d2858de
MN
767 if(error1&VP_START)
768 end_ok=1;
115329f1 769
4d2858de 770 if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
115329f1 771 && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
5e534865 772 && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
4d2858de
MN
773 end_ok=0;
774 }
115329f1 775
4d2858de 776 if(!end_ok)
7bc9090a 777 s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
4d2858de
MN
778 }
779 }
115329f1 780
4d2858de
MN
781#if 1
782 /* backward mark errors */
783 distance=9999999;
784 for(error_type=1; error_type<=3; error_type++){
785 for(i=s->mb_num-1; i>=0; i--){
7bc9090a
MN
786 const int mb_xy= s->mb_index2xy[i];
787 int error= s->error_status_table[mb_xy];
115329f1 788
7bc9090a 789 if(!s->mbskip_table[mb_xy]) //FIXME partition specific
115329f1 790 distance++;
4d2858de
MN
791 if(error&(1<<error_type))
792 distance= 0;
793
794 if(s->partitioned_frame){
795 if(distance < threshold_part[error_type-1])
7bc9090a 796 s->error_status_table[mb_xy]|= 1<<error_type;
4d2858de
MN
797 }else{
798 if(distance < threshold)
7bc9090a 799 s->error_status_table[mb_xy]|= 1<<error_type;
4d2858de
MN
800 }
801
802 if(error&VP_START)
803 distance= 9999999;
804 }
805 }
806#endif
807
808 /* forward mark errors */
809 error=0;
810 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
811 const int mb_xy= s->mb_index2xy[i];
812 int old_error= s->error_status_table[mb_xy];
115329f1 813
4d2858de
MN
814 if(old_error&VP_START)
815 error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
816 else{
817 error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
7bc9090a 818 s->error_status_table[mb_xy]|= error;
4d2858de
MN
819 }
820 }
821#if 1
822 /* handle not partitioned case */
823 if(!s->partitioned_frame){
824 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
825 const int mb_xy= s->mb_index2xy[i];
826 error= s->error_status_table[mb_xy];
4d2858de
MN
827 if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
828 error|= AC_ERROR|DC_ERROR|MV_ERROR;
7bc9090a 829 s->error_status_table[mb_xy]= error;
4d2858de
MN
830 }
831 }
832#endif
4cfbbbde
MN
833
834 dc_error= ac_error= mv_error=0;
835 for(i=0; i<s->mb_num; i++){
836 const int mb_xy= s->mb_index2xy[i];
837 error= s->error_status_table[mb_xy];
838 if(error&DC_ERROR) dc_error ++;
839 if(error&AC_ERROR) ac_error ++;
840 if(error&MV_ERROR) mv_error ++;
841 }
842 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
843
4d2858de
MN
844 is_intra_likely= is_intra_more_likely(s);
845
846 /* set unknown mb-type to most likely */
847 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
848 const int mb_xy= s->mb_index2xy[i];
849 error= s->error_status_table[mb_xy];
850 if(!((error&DC_ERROR) && (error&MV_ERROR)))
851 continue;
4d2858de 852
7bc9090a
MN
853 if(is_intra_likely)
854 s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
4d2858de 855 else
7bc9090a 856 s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
4d2858de 857 }
115329f1 858
4d2858de 859 /* handle inter blocks with damaged AC */
4d2858de
MN
860 for(mb_y=0; mb_y<s->mb_height; mb_y++){
861 for(mb_x=0; mb_x<s->mb_width; mb_x++){
7bc9090a
MN
862 const int mb_xy= mb_x + mb_y * s->mb_stride;
863 const int mb_type= s->current_picture.mb_type[mb_xy];
864 error= s->error_status_table[mb_xy];
4d2858de 865
7bc9090a 866 if(IS_INTRA(mb_type)) continue; //intra
4d2858de
MN
867 if(error&MV_ERROR) continue; //inter with damaged MV
868 if(!(error&AC_ERROR)) continue; //undamaged inter
115329f1 869
4d2858de
MN
870 s->mv_dir = MV_DIR_FORWARD;
871 s->mb_intra=0;
160d679c 872 s->mb_skipped=0;
7bc9090a 873 if(IS_8X8(mb_type)){
137c8468 874 int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
4d2858de
MN
875 int j;
876 s->mv_type = MV_TYPE_8X8;
877 for(j=0; j<4; j++){
137c8468
MN
878 s->mv[0][j][0] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
879 s->mv[0][j][1] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
4d2858de
MN
880 }
881 }else{
882 s->mv_type = MV_TYPE_16X16;
137c8468
MN
883 s->mv[0][0][0] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][0];
884 s->mv[0][0][1] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][1];
4d2858de 885 }
115329f1 886
bb270c08 887 s->dsp.clear_blocks(s->block[0]);
4d2858de
MN
888
889 s->mb_x= mb_x;
890 s->mb_y= mb_y;
7d1c3fc1 891 decode_mb(s);
4d2858de
MN
892 }
893 }
894
895 /* guess MVs */
896 if(s->pict_type==B_TYPE){
4d2858de
MN
897 for(mb_y=0; mb_y<s->mb_height; mb_y++){
898 for(mb_x=0; mb_x<s->mb_width; mb_x++){
137c8468 899 int xy= mb_x*2 + mb_y*2*s->b8_stride;
7bc9090a
MN
900 const int mb_xy= mb_x + mb_y * s->mb_stride;
901 const int mb_type= s->current_picture.mb_type[mb_xy];
902 error= s->error_status_table[mb_xy];
4d2858de 903
7bc9090a 904 if(IS_INTRA(mb_type)) continue;
4d2858de
MN
905 if(!(error&MV_ERROR)) continue; //inter with undamaged MV
906 if(!(error&AC_ERROR)) continue; //undamaged inter
115329f1 907
4d2858de
MN
908 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
909 s->mb_intra=0;
910 s->mv_type = MV_TYPE_16X16;
160d679c 911 s->mb_skipped=0;
115329f1 912
4d2858de
MN
913 if(s->pp_time){
914 int time_pp= s->pp_time;
915 int time_pb= s->pb_time;
115329f1 916
8d7ec294
WH
917 s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
918 s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
919 s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
920 s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
4d2858de
MN
921 }else{
922 s->mv[0][0][0]= 0;
923 s->mv[0][0][1]= 0;
924 s->mv[1][0][0]= 0;
925 s->mv[1][0][1]= 0;
926 }
eb4b3dd3
ZK
927
928 s->dsp.clear_blocks(s->block[0]);
4d2858de
MN
929 s->mb_x= mb_x;
930 s->mb_y= mb_y;
7d1c3fc1 931 decode_mb(s);
4d2858de
MN
932 }
933 }
934 }else
935 guess_mv(s);
936
2e7b4c84
IK
937#ifdef HAVE_XVMC
938 /* the filters below are not XvMC compatible, skip them */
939 if(s->avctx->xvmc_acceleration) goto ec_clean;
940#endif
4d2858de 941 /* fill DC for inter blocks */
4d2858de
MN
942 for(mb_y=0; mb_y<s->mb_height; mb_y++){
943 for(mb_x=0; mb_x<s->mb_width; mb_x++){
944 int dc, dcu, dcv, y, n;
b86216de 945 int16_t *dc_ptr;
0c1a9eda 946 uint8_t *dest_y, *dest_cb, *dest_cr;
7bc9090a
MN
947 const int mb_xy= mb_x + mb_y * s->mb_stride;
948 const int mb_type= s->current_picture.mb_type[mb_xy];
115329f1 949
7bc9090a 950 error= s->error_status_table[mb_xy];
4d2858de 951
7bc9090a 952 if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
4d2858de 953// if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
115329f1 954
1e491e29
MN
955 dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
956 dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
957 dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
115329f1 958
137c8468 959 dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
4d2858de
MN
960 for(n=0; n<4; n++){
961 dc=0;
962 for(y=0; y<8; y++){
963 int x;
964 for(x=0; x<8; x++){
965 dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
966 }
967 }
137c8468 968 dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
4d2858de
MN
969 }
970
971 dcu=dcv=0;
972 for(y=0; y<8; y++){
973 int x;
974 for(x=0; x<8; x++){
975 dcu+=dest_cb[x + y*(s->uvlinesize)];
976 dcv+=dest_cr[x + y*(s->uvlinesize)];
977 }
978 }
137c8468 979 s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
115329f1 980 s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
4d2858de
MN
981 }
982 }
983#if 1
984 /* guess DC for damaged blocks */
137c8468
MN
985 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
986 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
987 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
115329f1 988#endif
4d2858de 989 /* filter luma DC */
137c8468 990 filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
115329f1 991
4d2858de
MN
992#if 1
993 /* render DC only intra */
4d2858de
MN
994 for(mb_y=0; mb_y<s->mb_height; mb_y++){
995 for(mb_x=0; mb_x<s->mb_width; mb_x++){
0c1a9eda 996 uint8_t *dest_y, *dest_cb, *dest_cr;
7bc9090a
MN
997 const int mb_xy= mb_x + mb_y * s->mb_stride;
998 const int mb_type= s->current_picture.mb_type[mb_xy];
999
1000 error= s->error_status_table[mb_xy];
4d2858de 1001
7bc9090a 1002 if(IS_INTER(mb_type)) continue;
4d2858de 1003 if(!(error&AC_ERROR)) continue; //undamaged
115329f1 1004
1e491e29
MN
1005 dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
1006 dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
1007 dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
115329f1 1008
4d2858de
MN
1009 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1010 }
1011 }
1012#endif
115329f1 1013
4d2858de
MN
1014 if(s->avctx->error_concealment&FF_EC_DEBLOCK){
1015 /* filter horizontal block boundaries */
1e491e29
MN
1016 h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
1017 h_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
1018 h_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
4d2858de
MN
1019
1020 /* filter vertical block boundaries */
1e491e29
MN
1021 v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
1022 v_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
1023 v_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
4d2858de
MN
1024 }
1025
02c28fe6 1026#ifdef HAVE_XVMC
2e7b4c84 1027ec_clean:
02c28fe6 1028#endif
4d2858de
MN
1029 /* clean a few tables */
1030 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
1031 const int mb_xy= s->mb_index2xy[i];
1032 int error= s->error_status_table[mb_xy];
115329f1 1033
4d2858de 1034 if(s->pict_type!=B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
7bc9090a 1035 s->mbskip_table[mb_xy]=0;
4d2858de 1036 }
7bc9090a 1037 s->mbintra_table[mb_xy]=1;
115329f1 1038 }
4d2858de 1039}