FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/error_resilience.c
Date: 2026-03-13 22:30:28
Exec Total Coverage
Lines: 732 836 87.6%
Functions: 14 14 100.0%
Branches: 499 632 79.0%

Line Branch Exec Source
1 /*
2 * Error resilience / concealment
3 *
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
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
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Error resilience / concealment.
26 */
27
28 #include <limits.h>
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/attributes.h"
32 #include "libavutil/mem.h"
33 #include "avcodec.h"
34 #include "error_resilience.h"
35 #include "mathops.h"
36 #include "me_cmp.h"
37 #include "mpegutils.h"
38 #include "mpegvideo.h"
39 #include "threadframe.h"
40 #include "threadprogress.h"
41
42 1280 av_cold int ff_er_init(ERContext *const s)
43 {
44 MECmpContext mecc;
45 1280 unsigned mb_array_size = s->mb_height * s->mb_stride;
46
47 1280 s->error_status_table = av_mallocz(mb_array_size);
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 if (!s->error_status_table)
49 return AVERROR(ENOMEM);
50 1280 s->er_temp_buffer = av_malloc_array(mb_array_size, 4*sizeof(int) + 1);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 if (!s->er_temp_buffer)
52 return AVERROR(ENOMEM);
53
54 1280 ff_me_cmp_init(&mecc, s->avctx);
55 1280 s->sad = mecc.sad[0];
56
57 1280 return 0;
58 }
59
60 /**
61 * @param stride the number of MVs to get to the next row
62 * @param mv_step the number of MVs per row or column in a macroblock
63 */
64 217 static void set_mv_strides(ERContext *s, ptrdiff_t *mv_step, ptrdiff_t *stride)
65 {
66
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 210 times.
217 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 av_assert0(s->quarter_sample);
68 7 *mv_step = 4;
69 7 *stride = s->mb_width * 4;
70 } else {
71 210 *mv_step = 2;
72 210 *stride = s->b8_stride;
73 }
74 217 }
75
76 /**
77 * Replace the current MB with a flat dc-only version.
78 */
79 255 static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
80 uint8_t *dest_cr, int mb_x, int mb_y)
81 {
82 255 int *linesize = s->cur_pic.f->linesize;
83 int dc, dcu, dcv, y, i;
84
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 255 times.
1275 for (i = 0; i < 4; i++) {
85 1020 dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
86
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1003 times.
1020 if (dc < 0)
87 17 dc = 0;
88
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 995 times.
1003 else if (dc > 2040)
89 8 dc = 2040;
90
2/2
✓ Branch 0 taken 8160 times.
✓ Branch 1 taken 1020 times.
9180 for (y = 0; y < 8; y++) {
91 int x;
92
2/2
✓ Branch 0 taken 65280 times.
✓ Branch 1 taken 8160 times.
73440 for (x = 0; x < 8; x++)
93 65280 dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
94 }
95 }
96 255 dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
97 255 dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (dcu < 0)
99 dcu = 0;
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 else if (dcu > 2040)
101 dcu = 2040;
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (dcv < 0)
103 dcv = 0;
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 else if (dcv > 2040)
105 dcv = 2040;
106
107
1/2
✓ Branch 0 taken 255 times.
✗ Branch 1 not taken.
255 if (dest_cr)
108
2/2
✓ Branch 0 taken 2040 times.
✓ Branch 1 taken 255 times.
2295 for (y = 0; y < 8; y++) {
109 int x;
110
2/2
✓ Branch 0 taken 16320 times.
✓ Branch 1 taken 2040 times.
18360 for (x = 0; x < 8; x++) {
111 16320 dest_cb[x + y * linesize[1]] = dcu / 8;
112 16320 dest_cr[x + y * linesize[2]] = dcv / 8;
113 }
114 }
115 255 }
116
117 31 static void filter181(int16_t *data, int width, int height, ptrdiff_t stride)
118 {
119 int x, y;
120
121 /* horizontal filter */
122
2/2
✓ Branch 0 taken 1034 times.
✓ Branch 1 taken 31 times.
1065 for (y = 1; y < height - 1; y++) {
123 1034 int prev_dc = data[0 + y * stride];
124
125
2/2
✓ Branch 0 taken 43236 times.
✓ Branch 1 taken 1034 times.
44270 for (x = 1; x < width - 1; x++) {
126 int dc;
127 43236 dc = -prev_dc +
128 43236 data[x + y * stride] * 8 -
129 43236 data[x + 1 + y * stride];
130 43236 dc = (av_clip(dc, INT_MIN/10923, INT_MAX/10923 - 32768) * 10923 + 32768) >> 16;
131 43236 prev_dc = data[x + y * stride];
132 43236 data[x + y * stride] = dc;
133 }
134 }
135
136 /* vertical filter */
137
2/2
✓ Branch 0 taken 1294 times.
✓ Branch 1 taken 31 times.
1325 for (x = 1; x < width - 1; x++) {
138 1294 int prev_dc = data[x];
139
140
2/2
✓ Branch 0 taken 43236 times.
✓ Branch 1 taken 1294 times.
44530 for (y = 1; y < height - 1; y++) {
141 int dc;
142
143 43236 dc = -prev_dc +
144 43236 data[x + y * stride] * 8 -
145 43236 data[x + (y + 1) * stride];
146 43236 dc = (av_clip(dc, INT_MIN/10923, INT_MAX/10923 - 32768) * 10923 + 32768) >> 16;
147 43236 prev_dc = data[x + y * stride];
148 43236 data[x + y * stride] = dc;
149 }
150 }
151 31 }
152
153 /**
154 * guess the dc of blocks which do not have an undamaged dc
155 * @param w width in 8 pixel blocks
156 * @param h height in 8 pixel blocks
157 */
158 93 static void guess_dc(ERContext *s, int16_t *dc, int w,
159 int h, ptrdiff_t stride, int is_luma)
160 {
161 int b_x, b_y;
162 93 int16_t (*col )[4] = av_malloc_array(stride, h*sizeof( int16_t)*4);
163 93 uint32_t (*dist)[4] = av_malloc_array(stride, h*sizeof(uint32_t)*4);
164
165
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
93 if(!col || !dist) {
166 av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
167 goto fail;
168 }
169
170
2/2
✓ Branch 0 taken 2192 times.
✓ Branch 1 taken 93 times.
2285 for(b_y=0; b_y<h; b_y++){
171 2192 int color= 1024;
172 2192 int distance= -1;
173
2/2
✓ Branch 0 taken 72024 times.
✓ Branch 1 taken 2192 times.
74216 for(b_x=0; b_x<w; b_x++){
174 72024 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
175 72024 int error_j= s->error_status_table[mb_index_j];
176 72024 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
177
4/4
✓ Branch 0 taken 24930 times.
✓ Branch 1 taken 47094 times.
✓ Branch 2 taken 23400 times.
✓ Branch 3 taken 1530 times.
72024 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
178 70494 color= dc[b_x + b_y*stride];
179 70494 distance= b_x;
180 }
181 72024 col [b_x + b_y*stride][1]= color;
182
2/2
✓ Branch 0 taken 70818 times.
✓ Branch 1 taken 1206 times.
72024 dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
183 }
184 2192 color= 1024;
185 2192 distance= -1;
186
2/2
✓ Branch 0 taken 72024 times.
✓ Branch 1 taken 2192 times.
74216 for(b_x=w-1; b_x>=0; b_x--){
187 72024 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
188 72024 int error_j= s->error_status_table[mb_index_j];
189 72024 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
190
4/4
✓ Branch 0 taken 24930 times.
✓ Branch 1 taken 47094 times.
✓ Branch 2 taken 23400 times.
✓ Branch 3 taken 1530 times.
72024 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
191 70494 color= dc[b_x + b_y*stride];
192 70494 distance= b_x;
193 }
194 72024 col [b_x + b_y*stride][0]= color;
195
2/2
✓ Branch 0 taken 70560 times.
✓ Branch 1 taken 1464 times.
72024 dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
196 }
197 }
198
2/2
✓ Branch 0 taken 2712 times.
✓ Branch 1 taken 93 times.
2805 for(b_x=0; b_x<w; b_x++){
199 2712 int color= 1024;
200 2712 int distance= -1;
201
2/2
✓ Branch 0 taken 72024 times.
✓ Branch 1 taken 2712 times.
74736 for(b_y=0; b_y<h; b_y++){
202 72024 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
203 72024 int error_j= s->error_status_table[mb_index_j];
204 72024 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
205
4/4
✓ Branch 0 taken 24930 times.
✓ Branch 1 taken 47094 times.
✓ Branch 2 taken 23400 times.
✓ Branch 3 taken 1530 times.
72024 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
206 70494 color= dc[b_x + b_y*stride];
207 70494 distance= b_y;
208 }
209 72024 col [b_x + b_y*stride][3]= color;
210
1/2
✓ Branch 0 taken 72024 times.
✗ Branch 1 not taken.
72024 dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
211 }
212 2712 color= 1024;
213 2712 distance= -1;
214
2/2
✓ Branch 0 taken 72024 times.
✓ Branch 1 taken 2712 times.
74736 for(b_y=h-1; b_y>=0; b_y--){
215 72024 int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
216 72024 int error_j= s->error_status_table[mb_index_j];
217 72024 int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
218
4/4
✓ Branch 0 taken 24930 times.
✓ Branch 1 taken 47094 times.
✓ Branch 2 taken 23400 times.
✓ Branch 3 taken 1530 times.
72024 if(intra_j==0 || !(error_j&ER_DC_ERROR)){
219 70494 color= dc[b_x + b_y*stride];
220 70494 distance= b_y;
221 }
222 72024 col [b_x + b_y*stride][2]= color;
223
2/2
✓ Branch 0 taken 71430 times.
✓ Branch 1 taken 594 times.
72024 dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
224 }
225 }
226
227
2/2
✓ Branch 0 taken 2192 times.
✓ Branch 1 taken 93 times.
2285 for (b_y = 0; b_y < h; b_y++) {
228
2/2
✓ Branch 0 taken 72024 times.
✓ Branch 1 taken 2192 times.
74216 for (b_x = 0; b_x < w; b_x++) {
229 int mb_index, error, j;
230 int64_t guess, weight_sum;
231 72024 mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
232 72024 error = s->error_status_table[mb_index];
233
234
2/2
✓ Branch 0 taken 47094 times.
✓ Branch 1 taken 24930 times.
72024 if (IS_INTER(s->cur_pic.mb_type[mb_index]))
235 47094 continue; // inter
236
2/2
✓ Branch 0 taken 23400 times.
✓ Branch 1 taken 1530 times.
24930 if (!(error & ER_DC_ERROR))
237 23400 continue; // dc-ok
238
239 1530 weight_sum = 0;
240 1530 guess = 0;
241
2/2
✓ Branch 0 taken 6120 times.
✓ Branch 1 taken 1530 times.
7650 for (j = 0; j < 4; j++) {
242 6120 int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
243 6120 guess += weight*(int64_t)col[b_x + b_y*stride][j];
244 6120 weight_sum += weight;
245 }
246 1530 guess = (guess + weight_sum / 2) / weight_sum;
247 1530 dc[b_x + b_y * stride] = guess;
248 }
249 }
250
251 93 fail:
252 93 av_freep(&col);
253 93 av_freep(&dist);
254 93 }
255
256 /**
257 * simple horizontal deblocking filter used for error resilience
258 * @param w width in 8 pixel blocks
259 * @param h height in 8 pixel blocks
260 */
261 93 static void h_block_filter(ERContext *s, uint8_t *dst, int w,
262 int h, ptrdiff_t stride, int is_luma)
263 {
264 int b_x, b_y;
265 ptrdiff_t mvx_stride, mvy_stride;
266 93 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
267 93 set_mv_strides(s, &mvx_stride, &mvy_stride);
268 93 mvx_stride >>= is_luma;
269 93 mvy_stride *= mvx_stride;
270
271
2/2
✓ Branch 0 taken 2192 times.
✓ Branch 1 taken 93 times.
2285 for (b_y = 0; b_y < h; b_y++) {
272
2/2
✓ Branch 0 taken 69832 times.
✓ Branch 1 taken 2192 times.
72024 for (b_x = 0; b_x < w - 1; b_x++) {
273 int y;
274 69832 int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
275 69832 int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
276 69832 int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
277 69832 int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
278 69832 int left_damage = left_status & ER_MB_ERROR;
279 69832 int right_damage = right_status & ER_MB_ERROR;
280 69832 int offset = b_x * 8 + b_y * stride * 8;
281 69832 int16_t *left_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
282 69832 int16_t *right_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
283
4/4
✓ Branch 0 taken 49462 times.
✓ Branch 1 taken 20370 times.
✓ Branch 2 taken 49350 times.
✓ Branch 3 taken 112 times.
69832 if (!(left_damage || right_damage))
284 49350 continue; // both undamaged
285
4/4
✓ Branch 0 taken 18968 times.
✓ Branch 1 taken 1514 times.
✓ Branch 2 taken 18940 times.
✓ Branch 3 taken 28 times.
20482 if ((!left_intra) && (!right_intra) &&
286 18940 FFABS(left_mv[0] - right_mv[0]) +
287
2/2
✓ Branch 0 taken 2924 times.
✓ Branch 1 taken 16016 times.
18940 FFABS(left_mv[1] + right_mv[1]) < 2)
288 2924 continue;
289
290
2/2
✓ Branch 0 taken 140464 times.
✓ Branch 1 taken 17558 times.
158022 for (y = 0; y < 8; y++) {
291 int a, b, c, d;
292
293 140464 a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
294 140464 b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
295 140464 c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
296
297 140464 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
298 140464 d = FFMAX(d, 0);
299
2/2
✓ Branch 0 taken 50635 times.
✓ Branch 1 taken 89829 times.
140464 if (b < 0)
300 50635 d = -d;
301
302
2/2
✓ Branch 0 taken 88514 times.
✓ Branch 1 taken 51950 times.
140464 if (d == 0)
303 88514 continue;
304
305
4/4
✓ Branch 0 taken 51581 times.
✓ Branch 1 taken 369 times.
✓ Branch 2 taken 472 times.
✓ Branch 3 taken 51109 times.
51950 if (!(left_damage && right_damage))
306 841 d = d * 16 / 9;
307
308
2/2
✓ Branch 0 taken 51581 times.
✓ Branch 1 taken 369 times.
51950 if (left_damage) {
309 51581 dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
310 51581 dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
311 51581 dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
312 51581 dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
313 }
314
2/2
✓ Branch 0 taken 51478 times.
✓ Branch 1 taken 472 times.
51950 if (right_damage) {
315 51478 dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
316 51478 dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
317 51478 dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
318 51478 dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
319 }
320 }
321 }
322 }
323 93 }
324
325 /**
326 * simple vertical deblocking filter used for error resilience
327 * @param w width in 8 pixel blocks
328 * @param h height in 8 pixel blocks
329 */
330 93 static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
331 ptrdiff_t stride, int is_luma)
332 {
333 int b_x, b_y;
334 ptrdiff_t mvx_stride, mvy_stride;
335 93 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
336 93 set_mv_strides(s, &mvx_stride, &mvy_stride);
337 93 mvx_stride >>= is_luma;
338 93 mvy_stride *= mvx_stride;
339
340
2/2
✓ Branch 0 taken 2099 times.
✓ Branch 1 taken 93 times.
2192 for (b_y = 0; b_y < h - 1; b_y++) {
341
2/2
✓ Branch 0 taken 69312 times.
✓ Branch 1 taken 2099 times.
71411 for (b_x = 0; b_x < w; b_x++) {
342 int x;
343 69312 int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
344 69312 int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
345 69312 int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
346 69312 int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
347 69312 int top_damage = top_status & ER_MB_ERROR;
348 69312 int bottom_damage = bottom_status & ER_MB_ERROR;
349 69312 int offset = b_x * 8 + b_y * stride * 8;
350
351 69312 int16_t *top_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
352 69312 int16_t *bottom_mv = s->cur_pic.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
353
354
4/4
✓ Branch 0 taken 48602 times.
✓ Branch 1 taken 20710 times.
✓ Branch 2 taken 45970 times.
✓ Branch 3 taken 2632 times.
69312 if (!(top_damage || bottom_damage))
355 45970 continue; // both undamaged
356
357
4/4
✓ Branch 0 taken 20840 times.
✓ Branch 1 taken 2502 times.
✓ Branch 2 taken 20108 times.
✓ Branch 3 taken 732 times.
23342 if ((!top_intra) && (!bottom_intra) &&
358 20108 FFABS(top_mv[0] - bottom_mv[0]) +
359
2/2
✓ Branch 0 taken 2992 times.
✓ Branch 1 taken 17116 times.
20108 FFABS(top_mv[1] + bottom_mv[1]) < 2)
360 2992 continue;
361
362
2/2
✓ Branch 0 taken 162800 times.
✓ Branch 1 taken 20350 times.
183150 for (x = 0; x < 8; x++) {
363 int a, b, c, d;
364
365 162800 a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
366 162800 b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
367 162800 c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
368
369 162800 d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
370 162800 d = FFMAX(d, 0);
371
2/2
✓ Branch 0 taken 63869 times.
✓ Branch 1 taken 98931 times.
162800 if (b < 0)
372 63869 d = -d;
373
374
2/2
✓ Branch 0 taken 97498 times.
✓ Branch 1 taken 65302 times.
162800 if (d == 0)
375 97498 continue;
376
377
4/4
✓ Branch 0 taken 55732 times.
✓ Branch 1 taken 9570 times.
✓ Branch 2 taken 9135 times.
✓ Branch 3 taken 46597 times.
65302 if (!(top_damage && bottom_damage))
378 18705 d = d * 16 / 9;
379
380
2/2
✓ Branch 0 taken 55732 times.
✓ Branch 1 taken 9570 times.
65302 if (top_damage) {
381 55732 dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
382 55732 dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
383 55732 dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
384 55732 dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
385 }
386
2/2
✓ Branch 0 taken 56167 times.
✓ Branch 1 taken 9135 times.
65302 if (bottom_damage) {
387 56167 dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
388 56167 dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
389 56167 dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
390 56167 dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
391 }
392 }
393 }
394 }
395 93 }
396
397 #define MV_FROZEN 8
398 #define MV_CHANGED 4
399 #define MV_UNCHANGED 2
400 #define MV_LISTED 1
401 44746 static av_always_inline void add_blocklist(int (*blocklist)[2], int *blocklist_length, uint8_t *fixed, int mb_x, int mb_y, int mb_xy)
402 {
403
2/2
✓ Branch 0 taken 41720 times.
✓ Branch 1 taken 3026 times.
44746 if (fixed[mb_xy])
404 41720 return;
405 3026 fixed[mb_xy] = MV_LISTED;
406 3026 blocklist[ *blocklist_length ][0] = mb_x;
407 3026 blocklist[(*blocklist_length)++][1] = mb_y;
408 }
409
410 31 static void guess_mv(ERContext *s)
411 {
412 int (*blocklist)[2], (*next_blocklist)[2];
413 uint8_t *fixed;
414 31 const ptrdiff_t mb_stride = s->mb_stride;
415 31 const int mb_width = s->mb_width;
416 31 int mb_height = s->mb_height;
417 int i, num_avail;
418 int mb_x, mb_y;
419 ptrdiff_t mot_step, mot_stride;
420 int blocklist_length, next_blocklist_length;
421
422
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
31 if (s->last_pic.f && s->last_pic.f->data[0])
423 27 mb_height = FFMIN(mb_height, (s->last_pic.f->height+15)>>4);
424
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
31 if (s->next_pic.f && s->next_pic.f->data[0])
425 30 mb_height = FFMIN(mb_height, (s->next_pic.f->height+15)>>4);
426
427 31 blocklist = (int (*)[2])s->er_temp_buffer;
428 31 next_blocklist = blocklist + s->mb_stride * s->mb_height;
429 31 fixed = (uint8_t *)(next_blocklist + s->mb_stride * s->mb_height);
430
431 31 set_mv_strides(s, &mot_step, &mot_stride);
432
433 31 num_avail = 0;
434
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
31 if (s->last_pic.motion_val[0]) {
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (s->last_pic.tf)
436 ff_thread_await_progress(s->last_pic.tf, mb_height-1, 0);
437 else
438 27 ff_thread_progress_await(s->last_pic.progress, mb_height - 1);
439 }
440
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 31 times.
12035 for (i = 0; i < mb_width * mb_height; i++) {
441 12004 const int mb_xy = s->mb_index2xy[i];
442 12004 int f = 0;
443 12004 int error = s->error_status_table[mb_xy];
444
445
2/2
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 7849 times.
12004 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
446 4155 f = MV_FROZEN; // intra // FIXME check
447
2/2
✓ Branch 0 taken 8503 times.
✓ Branch 1 taken 3501 times.
12004 if (!(error & ER_MV_ERROR))
448 8503 f = MV_FROZEN; // inter with undamaged MV
449
450 12004 fixed[mb_xy] = f;
451
2/2
✓ Branch 0 taken 8758 times.
✓ Branch 1 taken 3246 times.
12004 if (f == MV_FROZEN)
452 8758 num_avail++;
453
2/4
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3246 times.
✗ Branch 3 not taken.
3246 else if(s->last_pic.f->data[0] && s->last_pic.motion_val[0]){
454 3246 const int mb_y= mb_xy / s->mb_stride;
455 3246 const int mb_x= mb_xy % s->mb_stride;
456 3246 const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
457 3246 s->cur_pic.motion_val[0][mot_index][0]= s->last_pic.motion_val[0][mot_index][0];
458 3246 s->cur_pic.motion_val[0][mot_index][1]= s->last_pic.motion_val[0][mot_index][1];
459 3246 s->cur_pic.ref_index[0][4*mb_xy] = s->last_pic.ref_index[0][4*mb_xy];
460 }
461 }
462
463
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
464
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30 times.
31 num_avail <= FFMAX(mb_width, mb_height) / 2) {
465
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 for (mb_y = 0; mb_y < mb_height; mb_y++) {
466
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 11 times.
231 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
467 220 const int mb_xy = mb_x + mb_y * s->mb_stride;
468
2/4
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 220 times.
✗ Branch 3 not taken.
220 int mv_dir = (s->last_pic.f && s->last_pic.f->data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
469
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
471 continue;
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
473 continue;
474
475 220 s->mv[0][0][0] = 0;
476 220 s->mv[0][0][1] = 0;
477 220 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
478 mb_x, mb_y, 0, 0);
479 }
480 }
481 1 return;
482 }
483
484 30 blocklist_length = 0;
485
2/2
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 30 times.
567 for (mb_y = 0; mb_y < mb_height; mb_y++) {
486
2/2
✓ Branch 0 taken 11784 times.
✓ Branch 1 taken 537 times.
12321 for (mb_x = 0; mb_x < mb_width; mb_x++) {
487 11784 const int mb_xy = mb_x + mb_y * mb_stride;
488
2/2
✓ Branch 0 taken 8758 times.
✓ Branch 1 taken 3026 times.
11784 if (fixed[mb_xy] == MV_FROZEN) {
489
2/2
✓ Branch 0 taken 8357 times.
✓ Branch 1 taken 401 times.
8758 if (mb_x) add_blocklist(blocklist, &blocklist_length, fixed, mb_x - 1, mb_y, mb_xy - 1);
490
2/2
✓ Branch 0 taken 8100 times.
✓ Branch 1 taken 658 times.
8758 if (mb_y) add_blocklist(blocklist, &blocklist_length, fixed, mb_x, mb_y - 1, mb_xy - mb_stride);
491
2/2
✓ Branch 0 taken 8356 times.
✓ Branch 1 taken 402 times.
8758 if (mb_x+1 < mb_width) add_blocklist(blocklist, &blocklist_length, fixed, mb_x + 1, mb_y, mb_xy + 1);
492
2/2
✓ Branch 0 taken 8134 times.
✓ Branch 1 taken 624 times.
8758 if (mb_y+1 < mb_height) add_blocklist(blocklist, &blocklist_length, fixed, mb_x, mb_y + 1, mb_xy + mb_stride);
493 }
494 }
495 }
496
497 88 for (;;) {
498 int changed, pass, none_left;
499 int blocklist_index;
500
501 118 none_left = 1;
502 118 changed = 1;
503
6/6
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 593 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 115 times.
✓ Branch 4 taken 621 times.
✓ Branch 5 taken 3 times.
739 for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
504 621 changed = 0;
505
2/2
✓ Branch 0 taken 20762 times.
✓ Branch 1 taken 621 times.
21383 for (blocklist_index = 0; blocklist_index < blocklist_length; blocklist_index++) {
506 20762 const int mb_x = blocklist[blocklist_index][0];
507 20762 const int mb_y = blocklist[blocklist_index][1];
508 20762 const int mb_xy = mb_x + mb_y * mb_stride;
509 int mv_predictor[8][2];
510 int ref[8];
511 int pred_count;
512 int j;
513 int best_score;
514 int best_pred;
515 int mot_index;
516 int prev_x, prev_y, prev_ref;
517
518
2/2
✓ Branch 0 taken 10387 times.
✓ Branch 1 taken 10375 times.
20762 if ((mb_x ^ mb_y ^ pass) & 1)
519 12859 continue;
520 av_assert2(fixed[mb_xy] != MV_FROZEN);
521
522
523 av_assert1(!IS_INTRA(s->cur_pic.mb_type[mb_xy]));
524 av_assert1(s->last_pic.f && s->last_pic.f->data[0]);
525
526 10375 j = 0;
527
2/2
✓ Branch 0 taken 9916 times.
✓ Branch 1 taken 459 times.
10375 if (mb_x > 0)
528 9916 j |= fixed[mb_xy - 1];
529
2/2
✓ Branch 0 taken 9913 times.
✓ Branch 1 taken 462 times.
10375 if (mb_x + 1 < mb_width)
530 9913 j |= fixed[mb_xy + 1];
531
1/2
✓ Branch 0 taken 10375 times.
✗ Branch 1 not taken.
10375 if (mb_y > 0)
532 10375 j |= fixed[mb_xy - mb_stride];
533
2/2
✓ Branch 0 taken 10292 times.
✓ Branch 1 taken 83 times.
10375 if (mb_y + 1 < mb_height)
534 10292 j |= fixed[mb_xy + mb_stride];
535
536 av_assert2(j & MV_FROZEN);
537
538
4/4
✓ Branch 0 taken 4291 times.
✓ Branch 1 taken 6084 times.
✓ Branch 2 taken 2472 times.
✓ Branch 3 taken 1819 times.
10375 if (!(j & MV_CHANGED) && pass > 1)
539 2472 continue;
540
541 7903 none_left = 0;
542 7903 pred_count = 0;
543 7903 mot_index = (mb_x + mb_y * mot_stride) * mot_step;
544
545
4/4
✓ Branch 0 taken 7631 times.
✓ Branch 1 taken 272 times.
✓ Branch 2 taken 6124 times.
✓ Branch 3 taken 1507 times.
7903 if (mb_x > 0 && fixed[mb_xy - 1] > 1) {
546 6124 mv_predictor[pred_count][0] =
547 6124 s->cur_pic.motion_val[0][mot_index - mot_step][0];
548 6124 mv_predictor[pred_count][1] =
549 6124 s->cur_pic.motion_val[0][mot_index - mot_step][1];
550 6124 ref[pred_count] =
551 6124 s->cur_pic.ref_index[0][4 * (mb_xy - 1)];
552 6124 pred_count++;
553 }
554
4/4
✓ Branch 0 taken 7625 times.
✓ Branch 1 taken 278 times.
✓ Branch 2 taken 6137 times.
✓ Branch 3 taken 1488 times.
7903 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) {
555 6137 mv_predictor[pred_count][0] =
556 6137 s->cur_pic.motion_val[0][mot_index + mot_step][0];
557 6137 mv_predictor[pred_count][1] =
558 6137 s->cur_pic.motion_val[0][mot_index + mot_step][1];
559 6137 ref[pred_count] =
560 6137 s->cur_pic.ref_index[0][4 * (mb_xy + 1)];
561 6137 pred_count++;
562 }
563
3/4
✓ Branch 0 taken 7903 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5118 times.
✓ Branch 3 taken 2785 times.
7903 if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) {
564 5118 mv_predictor[pred_count][0] =
565 5118 s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][0];
566 5118 mv_predictor[pred_count][1] =
567 5118 s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][1];
568 5118 ref[pred_count] =
569 5118 s->cur_pic.ref_index[0][4 * (mb_xy - s->mb_stride)];
570 5118 pred_count++;
571 }
572
4/4
✓ Branch 0 taken 7846 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 4796 times.
✓ Branch 3 taken 3050 times.
7903 if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride] > 1) {
573 4796 mv_predictor[pred_count][0] =
574 4796 s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][0];
575 4796 mv_predictor[pred_count][1] =
576 4796 s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][1];
577 4796 ref[pred_count] =
578 4796 s->cur_pic.ref_index[0][4 * (mb_xy + s->mb_stride)];
579 4796 pred_count++;
580 }
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7903 times.
7903 if (pred_count == 0)
582 continue;
583
584
2/2
✓ Branch 0 taken 1314 times.
✓ Branch 1 taken 6589 times.
7903 if (pred_count > 1) {
585 6589 int sum_x = 0, sum_y = 0, sum_r = 0;
586 int max_x, max_y, min_x, min_y, max_r, min_r;
587
588
2/2
✓ Branch 0 taken 20861 times.
✓ Branch 1 taken 6589 times.
27450 for (j = 0; j < pred_count; j++) {
589 20861 sum_x += mv_predictor[j][0];
590 20861 sum_y += mv_predictor[j][1];
591 20861 sum_r += ref[j];
592
3/4
✓ Branch 0 taken 14272 times.
✓ Branch 1 taken 6589 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14272 times.
20861 if (j && ref[j] != ref[j - 1])
593 goto skip_mean_and_median;
594 }
595
596 /* mean */
597 6589 mv_predictor[pred_count][0] = sum_x / j;
598 6589 mv_predictor[pred_count][1] = sum_y / j;
599 6589 ref[pred_count] = sum_r / j;
600
601 /* median */
602
2/2
✓ Branch 0 taken 6014 times.
✓ Branch 1 taken 575 times.
6589 if (pred_count >= 3) {
603 6014 min_y = min_x = min_r = 99999;
604 6014 max_y = max_x = max_r = -99999;
605 } else {
606 575 min_x = min_y = max_x = max_y = min_r = max_r = 0;
607 }
608
2/2
✓ Branch 0 taken 20861 times.
✓ Branch 1 taken 6589 times.
27450 for (j = 0; j < pred_count; j++) {
609 20861 max_x = FFMAX(max_x, mv_predictor[j][0]);
610 20861 max_y = FFMAX(max_y, mv_predictor[j][1]);
611 20861 max_r = FFMAX(max_r, ref[j]);
612 20861 min_x = FFMIN(min_x, mv_predictor[j][0]);
613 20861 min_y = FFMIN(min_y, mv_predictor[j][1]);
614 20861 min_r = FFMIN(min_r, ref[j]);
615 }
616 6589 mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
617 6589 mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
618 6589 ref[pred_count + 1] = sum_r - max_r - min_r;
619
620
2/2
✓ Branch 0 taken 1669 times.
✓ Branch 1 taken 4920 times.
6589 if (pred_count == 4) {
621 1669 mv_predictor[pred_count + 1][0] /= 2;
622 1669 mv_predictor[pred_count + 1][1] /= 2;
623 1669 ref[pred_count + 1] /= 2;
624 }
625 6589 pred_count += 2;
626 }
627
628 1314 skip_mean_and_median:
629 /* zero MV */
630 7903 mv_predictor[pred_count][0] =
631 7903 mv_predictor[pred_count][1] =
632 7903 ref[pred_count] = 0;
633 7903 pred_count++;
634
635 7903 prev_x = s->cur_pic.motion_val[0][mot_index][0];
636 7903 prev_y = s->cur_pic.motion_val[0][mot_index][1];
637 7903 prev_ref = s->cur_pic.ref_index[0][4 * mb_xy];
638
639 /* last MV */
640 7903 mv_predictor[pred_count][0] = prev_x;
641 7903 mv_predictor[pred_count][1] = prev_y;
642 7903 ref[pred_count] = prev_ref;
643 7903 pred_count++;
644
645 7903 best_pred = 0;
646 7903 best_score = 256 * 256 * 256 * 64;
647
2/2
✓ Branch 0 taken 51159 times.
✓ Branch 1 taken 7903 times.
59062 for (j = 0; j < pred_count; j++) {
648 51159 int *linesize = s->cur_pic.f->linesize;
649 51159 int score = 0;
650 51159 uint8_t *src = s->cur_pic.f->data[0] +
651 51159 mb_x * 16 + mb_y * 16 * linesize[0];
652
653 51159 s->cur_pic.motion_val[0][mot_index][0] =
654 51159 s->mv[0][0][0] = mv_predictor[j][0];
655 51159 s->cur_pic.motion_val[0][mot_index][1] =
656 51159 s->mv[0][0][1] = mv_predictor[j][1];
657
658 // predictor intra or otherwise not available
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51159 times.
51159 if (ref[j] < 0)
660 continue;
661
662 51159 s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
663 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
664
665
4/4
✓ Branch 0 taken 49658 times.
✓ Branch 1 taken 1501 times.
✓ Branch 2 taken 44324 times.
✓ Branch 3 taken 5334 times.
51159 if (mb_x > 0 && fixed[mb_xy - 1] > 1) {
666 int k;
667
2/2
✓ Branch 0 taken 709184 times.
✓ Branch 1 taken 44324 times.
753508 for (k = 0; k < 16; k++)
668 709184 score += FFABS(src[k * linesize[0] - 1] -
669 src[k * linesize[0]]);
670 }
671
4/4
✓ Branch 0 taken 49596 times.
✓ Branch 1 taken 1563 times.
✓ Branch 2 taken 44366 times.
✓ Branch 3 taken 5230 times.
51159 if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) {
672 int k;
673
2/2
✓ Branch 0 taken 709856 times.
✓ Branch 1 taken 44366 times.
754222 for (k = 0; k < 16; k++)
674 709856 score += FFABS(src[k * linesize[0] + 15] -
675 src[k * linesize[0] + 16]);
676 }
677
3/4
✓ Branch 0 taken 51159 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34381 times.
✓ Branch 3 taken 16778 times.
51159 if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) {
678 int k;
679
2/2
✓ Branch 0 taken 550096 times.
✓ Branch 1 taken 34381 times.
584477 for (k = 0; k < 16; k++)
680 550096 score += FFABS(src[k - linesize[0]] - src[k]);
681 }
682
4/4
✓ Branch 0 taken 50841 times.
✓ Branch 1 taken 318 times.
✓ Branch 2 taken 32424 times.
✓ Branch 3 taken 18417 times.
51159 if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] > 1) {
683 int k;
684
2/2
✓ Branch 0 taken 518784 times.
✓ Branch 1 taken 32424 times.
551208 for (k = 0; k < 16; k++)
685 518784 score += FFABS(src[k + linesize[0] * 15] -
686 src[k + linesize[0] * 16]);
687 }
688
689
2/2
✓ Branch 0 taken 26196 times.
✓ Branch 1 taken 24963 times.
51159 if (score <= best_score) { // <= will favor the last MV
690 26196 best_score = score;
691 26196 best_pred = j;
692 }
693 }
694 7903 s->mv[0][0][0] = mv_predictor[best_pred][0];
695 7903 s->mv[0][0][1] = mv_predictor[best_pred][1];
696
697
2/2
✓ Branch 0 taken 15806 times.
✓ Branch 1 taken 7903 times.
23709 for (i = 0; i < mot_step; i++)
698
2/2
✓ Branch 0 taken 31612 times.
✓ Branch 1 taken 15806 times.
47418 for (j = 0; j < mot_step; j++) {
699 31612 s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
700 31612 s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
701 }
702
703 7903 s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
704 MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
705
706
707
4/4
✓ Branch 0 taken 5150 times.
✓ Branch 1 taken 2753 times.
✓ Branch 2 taken 672 times.
✓ Branch 3 taken 4478 times.
7903 if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
708 3425 fixed[mb_xy] = MV_CHANGED;
709 3425 changed++;
710 } else
711 4478 fixed[mb_xy] = MV_UNCHANGED;
712 }
713 }
714
715
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 88 times.
118 if (none_left)
716 30 return;
717
718 88 next_blocklist_length = 0;
719
720
2/2
✓ Branch 0 taken 3026 times.
✓ Branch 1 taken 88 times.
3114 for (blocklist_index = 0; blocklist_index < blocklist_length; blocklist_index++) {
721 3026 const int mb_x = blocklist[blocklist_index][0];
722 3026 const int mb_y = blocklist[blocklist_index][1];
723 3026 const int mb_xy = mb_x + mb_y * mb_stride;
724
725
1/2
✓ Branch 0 taken 3026 times.
✗ Branch 1 not taken.
3026 if (fixed[mb_xy] & (MV_CHANGED|MV_UNCHANGED|MV_FROZEN)) {
726 3026 fixed[mb_xy] = MV_FROZEN;
727
2/2
✓ Branch 0 taken 2890 times.
✓ Branch 1 taken 136 times.
3026 if (mb_x > 0)
728 2890 add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x - 1, mb_y, mb_xy - 1);
729
1/2
✓ Branch 0 taken 3026 times.
✗ Branch 1 not taken.
3026 if (mb_y > 0)
730 3026 add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x, mb_y - 1, mb_xy - mb_stride);
731
2/2
✓ Branch 0 taken 2891 times.
✓ Branch 1 taken 135 times.
3026 if (mb_x + 1 < mb_width)
732 2891 add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x + 1, mb_y, mb_xy + 1);
733
2/2
✓ Branch 0 taken 2992 times.
✓ Branch 1 taken 34 times.
3026 if (mb_y + 1 < mb_height)
734 2992 add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x, mb_y + 1, mb_xy + mb_stride);
735 }
736 }
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 av_assert0(next_blocklist_length <= mb_height * mb_width);
738 88 FFSWAP(int , blocklist_length, next_blocklist_length);
739 88 FFSWAP(void*, blocklist, next_blocklist);
740 }
741 }
742
743 31 static int is_intra_more_likely(ERContext *s)
744 {
745 int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
746
747
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
31 if (!s->last_pic.f || !s->last_pic.f->data[0])
748 4 return 1; // no previous frame available -> use spatial prediction
749
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (s->avctx->error_concealment & FF_EC_FAVOR_INTER)
751 return 0;
752
753 27 undamaged_count = 0;
754
2/2
✓ Branch 0 taken 10516 times.
✓ Branch 1 taken 27 times.
10543 for (i = 0; i < s->mb_num; i++) {
755 10516 const int mb_xy = s->mb_index2xy[i];
756 10516 const int error = s->error_status_table[mb_xy];
757
3/4
✓ Branch 0 taken 3246 times.
✓ Branch 1 taken 7270 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3246 times.
10516 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
758 7270 undamaged_count++;
759 }
760
761
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 if (undamaged_count < 5)
762 1 return 0; // almost all MBs damaged -> use temporal prediction
763
764
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
765 26 is_intra_likely = 0;
766
767 26 j = 0;
768
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 26 times.
468 for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
769
2/2
✓ Branch 0 taken 9724 times.
✓ Branch 1 taken 442 times.
10166 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
770 int error;
771 9724 const int mb_xy = mb_x + mb_y * s->mb_stride;
772
773 9724 error = s->error_status_table[mb_xy];
774
3/4
✓ Branch 0 taken 2992 times.
✓ Branch 1 taken 6732 times.
✓ Branch 2 taken 2992 times.
✗ Branch 3 not taken.
9724 if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
775 2992 continue; // skip damaged
776
777 6732 j++;
778 // skip a few to speed things up
779
2/2
✓ Branch 0 taken 5384 times.
✓ Branch 1 taken 1348 times.
6732 if ((j % skip_amount) != 0)
780 5384 continue;
781
782
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 993 times.
1348 if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) {
783 355 int *linesize = s->cur_pic.f->linesize;
784 355 uint8_t *mb_ptr = s->cur_pic.f->data[0] +
785 355 mb_x * 16 + mb_y * 16 * linesize[0];
786 355 uint8_t *last_mb_ptr = s->last_pic.f->data[0] +
787 355 mb_x * 16 + mb_y * 16 * linesize[0];
788
789
1/2
✓ Branch 0 taken 355 times.
✗ Branch 1 not taken.
355 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
790 // FIXME
791 } else {
792 355 ff_thread_progress_await(s->last_pic.progress, mb_y);
793 }
794 710 is_intra_likely += s->sad(NULL, last_mb_ptr, mb_ptr,
795 355 linesize[0], 16);
796 // FIXME need await_progress() here
797 355 is_intra_likely -= s->sad(NULL, last_mb_ptr,
798 355 last_mb_ptr + linesize[0] * 16,
799 355 linesize[0], 16);
800 } else {
801
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 935 times.
993 if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
802 58 is_intra_likely++;
803 else
804 935 is_intra_likely--;
805 }
806 }
807 }
808 // av_log(NULL, AV_LOG_ERROR, "is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
809 26 return is_intra_likely > 0;
810 }
811
812 37480 void ff_er_frame_start(ERContext *s)
813 {
814
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 37225 times.
37480 if (!s->avctx->error_concealment)
815 255 return;
816
817 37225 memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
818 37225 s->mb_stride * s->mb_height * sizeof(uint8_t));
819 37225 atomic_init(&s->error_count, 3 * s->mb_num);
820 37225 s->error_occurred = 0;
821 }
822
823 113486 static int er_supported(ERContext *s)
824 {
825
1/2
✓ Branch 0 taken 113486 times.
✗ Branch 1 not taken.
113486 if (s->avctx->hwaccel ||
826
2/2
✓ Branch 0 taken 101794 times.
✓ Branch 1 taken 11692 times.
113486 !s->cur_pic.f ||
827
2/2
✓ Branch 0 taken 12538 times.
✓ Branch 1 taken 89256 times.
101794 s->cur_pic.field_picture
828 )
829 24230 return 0;
830 89256 return 1;
831 }
832
833 /**
834 * Add a slice.
835 * @param endx x component of the last macroblock, can be -1
836 * for the last of the previous line
837 * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
838 * assumed that no earlier end or error of the same type occurred
839 */
840 189576 void ff_er_add_slice(ERContext *s, int startx, int starty,
841 int endx, int endy, int status)
842 {
843 189576 const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
844 189576 const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
845 189576 const int start_xy = s->mb_index2xy[start_i];
846 189576 const int end_xy = s->mb_index2xy[end_i];
847 189576 int mask = -1;
848
849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 189576 times.
189576 if (s->avctx->hwaccel)
850 return;
851
852
2/4
✓ Branch 0 taken 189576 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 189576 times.
189576 if (start_i > end_i || start_xy > end_xy) {
853 av_log(s->avctx, AV_LOG_ERROR,
854 "internal error, slice end before start\n");
855 return;
856 }
857
858
2/2
✓ Branch 0 taken 12074 times.
✓ Branch 1 taken 177502 times.
189576 if (!s->avctx->error_concealment)
859 12074 return;
860
861 177502 mask &= ~VP_START;
862
2/2
✓ Branch 0 taken 174545 times.
✓ Branch 1 taken 2957 times.
177502 if (status & (ER_AC_ERROR | ER_AC_END)) {
863 174545 mask &= ~(ER_AC_ERROR | ER_AC_END);
864 174545 atomic_fetch_add(&s->error_count, start_i - end_i - 1);
865 }
866
2/2
✓ Branch 0 taken 174545 times.
✓ Branch 1 taken 2957 times.
177502 if (status & (ER_DC_ERROR | ER_DC_END)) {
867 174545 mask &= ~(ER_DC_ERROR | ER_DC_END);
868 174545 atomic_fetch_add(&s->error_count, start_i - end_i - 1);
869 }
870
2/2
✓ Branch 0 taken 174545 times.
✓ Branch 1 taken 2957 times.
177502 if (status & (ER_MV_ERROR | ER_MV_END)) {
871 174545 mask &= ~(ER_MV_ERROR | ER_MV_END);
872 174545 atomic_fetch_add(&s->error_count, start_i - end_i - 1);
873 }
874
875
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 177496 times.
177502 if (status & ER_MB_ERROR) {
876 6 s->error_occurred = 1;
877 6 atomic_store(&s->error_count, INT_MAX);
878 }
879
880
2/2
✓ Branch 0 taken 172817 times.
✓ Branch 1 taken 4685 times.
177502 if (mask == ~0x7F) {
881 172817 memset(&s->error_status_table[start_xy], 0,
882 172817 (end_xy - start_xy) * sizeof(uint8_t));
883 } else {
884 int i;
885
2/2
✓ Branch 0 taken 409448 times.
✓ Branch 1 taken 4685 times.
414133 for (i = start_xy; i < end_xy; i++)
886 409448 s->error_status_table[i] &= mask;
887 }
888
889
2/2
✓ Branch 0 taken 3904 times.
✓ Branch 1 taken 173598 times.
177502 if (end_i == s->mb_num)
890 3904 atomic_store(&s->error_count, INT_MAX);
891 else {
892 173598 s->error_status_table[end_xy] &= mask;
893 173598 s->error_status_table[end_xy] |= status;
894 }
895
896 177502 s->error_status_table[start_xy] |= VP_START;
897
898
6/6
✓ Branch 0 taken 140345 times.
✓ Branch 1 taken 37157 times.
✓ Branch 2 taken 112969 times.
✓ Branch 3 taken 27376 times.
✓ Branch 4 taken 89225 times.
✓ Branch 5 taken 23744 times.
290471 if (start_xy > 0 && !(s->avctx->active_thread_type & FF_THREAD_SLICE) &&
899
1/2
✓ Branch 1 taken 89225 times.
✗ Branch 2 not taken.
202194 er_supported(s) && s->avctx->skip_top * s->mb_width < start_i) {
900 89225 int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
901
902 89225 prev_status &= ~ VP_START;
903
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 89148 times.
89225 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END)) {
904 77 s->error_occurred = 1;
905 77 atomic_store(&s->error_count, INT_MAX);
906 }
907 }
908 }
909
910 32974 void ff_er_frame_end(ERContext *s, int *decode_error_flags)
911 {
912 32974 int *linesize = NULL;
913 int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
914 int distance;
915 32974 int threshold_part[4] = { 100, 100, 100 };
916 32974 int threshold = 50;
917 int is_intra_likely;
918 32974 int size = s->b8_stride * 2 * s->mb_height;
919 int guessed_mb_type;
920
921 /* We do not support ER of field pictures yet,
922 * though it should not crash if enabled. */
923
4/4
✓ Branch 0 taken 32719 times.
✓ Branch 1 taken 255 times.
✓ Branch 2 taken 517 times.
✓ Branch 3 taken 32202 times.
32974 if (!s->avctx->error_concealment || !atomic_load(&s->error_count) ||
924
3/4
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 486 times.
1034 s->avctx->lowres ||
925 517 !er_supported(s) ||
926 31 atomic_load(&s->error_count) == 3 * s->mb_width *
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 (s->avctx->skip_top + s->avctx->skip_bottom)) {
928 32943 return;
929 }
930 31 linesize = s->cur_pic.f->linesize;
931
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if ( s->avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO
933 && (FFALIGN(s->avctx->height, 16)&16)
934 && atomic_load(&s->error_count) == 3 * s->mb_width * (s->avctx->skip_top + s->avctx->skip_bottom + 1)) {
935 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
936 int status = s->error_status_table[mb_x + (s->mb_height - 1) * s->mb_stride];
937 if (status != 0x7F)
938 break;
939 }
940
941 if (mb_x == s->mb_width) {
942 av_log(s->avctx, AV_LOG_DEBUG, "ignoring last missing slice\n");
943 return;
944 }
945 }
946
947
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
31 if (s->last_pic.f) {
948
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (s->last_pic.f->width != s->cur_pic.f->width ||
949
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 s->last_pic.f->height != s->cur_pic.f->height ||
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 s->last_pic.f->format != s->cur_pic.f->format) {
951 av_log(s->avctx, AV_LOG_WARNING, "Cannot use previous picture in error concealment\n");
952 memset(&s->last_pic, 0, sizeof(s->last_pic));
953 }
954 }
955
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 times.
31 if (s->next_pic.f) {
956
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (s->next_pic.f->width != s->cur_pic.f->width ||
957
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 s->next_pic.f->height != s->cur_pic.f->height ||
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 s->next_pic.f->format != s->cur_pic.f->format) {
959 av_log(s->avctx, AV_LOG_WARNING, "Cannot use next picture in error concealment\n");
960 memset(&s->next_pic, 0, sizeof(s->next_pic));
961 }
962 }
963
964
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 if (!s->cur_pic.motion_val[0] || !s->cur_pic.ref_index[0]) {
965 av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
966
967 for (i = 0; i < 2; i++) {
968 s->ref_index[i] = av_calloc(s->mb_stride * s->mb_height, 4 * sizeof(uint8_t));
969 s->motion_val_base[i] = av_calloc(size + 4, 2 * sizeof(uint16_t));
970 if (!s->ref_index[i] || !s->motion_val_base[i])
971 goto cleanup;
972 s->cur_pic.ref_index[i] = s->ref_index[i];
973 s->cur_pic.motion_val[i] = s->motion_val_base[i] + 4;
974 }
975 }
976
977
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (s->avctx->debug & FF_DEBUG_ER) {
978 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
979 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
980 int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
981
982 av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
983 }
984 av_log(s->avctx, AV_LOG_DEBUG, "\n");
985 }
986 }
987
988 #if 1
989 /* handle overlapping slices */
990
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 31 times.
124 for (error_type = 1; error_type <= 3; error_type++) {
991 93 int end_ok = 0;
992
993
2/2
✓ Branch 0 taken 36012 times.
✓ Branch 1 taken 93 times.
36105 for (i = s->mb_num - 1; i >= 0; i--) {
994 36012 const int mb_xy = s->mb_index2xy[i];
995 36012 int error = s->error_status_table[mb_xy];
996
997
2/2
✓ Branch 0 taken 10287 times.
✓ Branch 1 taken 25725 times.
36012 if (error & (1 << error_type))
998 10287 end_ok = 1;
999
2/2
✓ Branch 0 taken 10671 times.
✓ Branch 1 taken 25341 times.
36012 if (error & (8 << error_type))
1000 10671 end_ok = 1;
1001
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36012 times.
36012 if (!end_ok)
1003 s->error_status_table[mb_xy] |= 1 << error_type;
1004
1005
2/2
✓ Branch 0 taken 10677 times.
✓ Branch 1 taken 25335 times.
36012 if (error & VP_START)
1006 10677 end_ok = 0;
1007 }
1008 }
1009 #endif
1010 #if 1
1011 /* handle slices with partitions of different length */
1012
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
31 if (s->partitioned_frame) {
1013 29 int end_ok = 0;
1014
1015
2/2
✓ Branch 0 taken 11484 times.
✓ Branch 1 taken 29 times.
11513 for (i = s->mb_num - 1; i >= 0; i--) {
1016 11484 const int mb_xy = s->mb_index2xy[i];
1017 11484 int error = s->error_status_table[mb_xy];
1018
1019
2/2
✓ Branch 0 taken 3312 times.
✓ Branch 1 taken 8172 times.
11484 if (error & ER_AC_END)
1020 3312 end_ok = 0;
1021
2/2
✓ Branch 0 taken 8172 times.
✓ Branch 1 taken 3312 times.
11484 if ((error & ER_MV_END) ||
1022
1/2
✓ Branch 0 taken 8172 times.
✗ Branch 1 not taken.
8172 (error & ER_DC_END) ||
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8172 times.
8172 (error & ER_AC_ERROR))
1024 3312 end_ok = 1;
1025
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11484 times.
11484 if (!end_ok)
1027 s->error_status_table[mb_xy]|= ER_AC_ERROR;
1028
1029
2/2
✓ Branch 0 taken 3312 times.
✓ Branch 1 taken 8172 times.
11484 if (error & VP_START)
1030 3312 end_ok = 0;
1031 }
1032 }
1033 #endif
1034 /* handle missing slices */
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1036 int end_ok = 1;
1037
1038 // FIXME + 100 hack
1039 for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
1040 const int mb_xy = s->mb_index2xy[i];
1041 int error1 = s->error_status_table[mb_xy];
1042 int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
1043
1044 if (error1 & VP_START)
1045 end_ok = 1;
1046
1047 if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
1048 error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
1049 ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
1050 (error1 & ER_MV_END))) {
1051 // end & uninit
1052 end_ok = 0;
1053 }
1054
1055 if (!end_ok)
1056 s->error_status_table[mb_xy] |= ER_MB_ERROR;
1057 }
1058 }
1059
1060 #if 1
1061 /* backward mark errors */
1062 31 distance = 9999999;
1063
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 31 times.
124 for (error_type = 1; error_type <= 3; error_type++) {
1064
2/2
✓ Branch 0 taken 36012 times.
✓ Branch 1 taken 93 times.
36105 for (i = s->mb_num - 1; i >= 0; i--) {
1065 36012 const int mb_xy = s->mb_index2xy[i];
1066 36012 int error = s->error_status_table[mb_xy];
1067
1068
4/4
✓ Branch 0 taken 35112 times.
✓ Branch 1 taken 900 times.
✓ Branch 2 taken 34908 times.
✓ Branch 3 taken 204 times.
36012 if (!s->mbskip_table || !s->mbskip_table[mb_xy]) // FIXME partition specific
1069 35808 distance++;
1070
2/2
✓ Branch 0 taken 10287 times.
✓ Branch 1 taken 25725 times.
36012 if (error & (1 << error_type))
1071 10287 distance = 0;
1072
1073
2/2
✓ Branch 0 taken 34452 times.
✓ Branch 1 taken 1560 times.
36012 if (s->partitioned_frame) {
1074
2/2
✓ Branch 0 taken 9546 times.
✓ Branch 1 taken 24906 times.
34452 if (distance < threshold_part[error_type - 1])
1075 9546 s->error_status_table[mb_xy] |= 1 << error_type;
1076 } else {
1077
2/2
✓ Branch 0 taken 957 times.
✓ Branch 1 taken 603 times.
1560 if (distance < threshold)
1078 957 s->error_status_table[mb_xy] |= 1 << error_type;
1079 }
1080
1081
2/2
✓ Branch 0 taken 10677 times.
✓ Branch 1 taken 25335 times.
36012 if (error & VP_START)
1082 10677 distance = 9999999;
1083 }
1084 }
1085 #endif
1086
1087 /* forward mark errors */
1088 31 error = 0;
1089
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 31 times.
12035 for (i = 0; i < s->mb_num; i++) {
1090 12004 const int mb_xy = s->mb_index2xy[i];
1091 12004 int old_error = s->error_status_table[mb_xy];
1092
1093
2/2
✓ Branch 0 taken 3559 times.
✓ Branch 1 taken 8445 times.
12004 if (old_error & VP_START) {
1094 3559 error = old_error & ER_MB_ERROR;
1095 } else {
1096 8445 error |= old_error & ER_MB_ERROR;
1097 8445 s->error_status_table[mb_xy] |= error;
1098 }
1099 }
1100 #if 1
1101 /* handle not partitioned case */
1102
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (!s->partitioned_frame) {
1103
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 2 times.
522 for (i = 0; i < s->mb_num; i++) {
1104 520 const int mb_xy = s->mb_index2xy[i];
1105 520 int error = s->error_status_table[mb_xy];
1106
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 201 times.
520 if (error & ER_MB_ERROR)
1107 319 error |= ER_MB_ERROR;
1108 520 s->error_status_table[mb_xy] = error;
1109 }
1110 }
1111 #endif
1112
1113 31 dc_error = ac_error = mv_error = 0;
1114
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 31 times.
12035 for (i = 0; i < s->mb_num; i++) {
1115 12004 const int mb_xy = s->mb_index2xy[i];
1116 12004 int error = s->error_status_table[mb_xy];
1117
2/2
✓ Branch 0 taken 3501 times.
✓ Branch 1 taken 8503 times.
12004 if (error & ER_DC_ERROR)
1118 3501 dc_error++;
1119
2/2
✓ Branch 0 taken 3501 times.
✓ Branch 1 taken 8503 times.
12004 if (error & ER_AC_ERROR)
1120 3501 ac_error++;
1121
2/2
✓ Branch 0 taken 3501 times.
✓ Branch 1 taken 8503 times.
12004 if (error & ER_MV_ERROR)
1122 3501 mv_error++;
1123 }
1124 31 av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
1125 31 dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic.f->pict_type));
1126
1127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30 times.
31 if (decode_error_flags)
1128 1 *decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE;
1129 else
1130 30 s->cur_pic.f->decode_error_flags |= FF_DECODE_ERROR_CONCEALMENT_ACTIVE;
1131
1132 31 is_intra_likely = is_intra_more_likely(s);
1133
1134 /* set unknown mb-type to most likely */
1135
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
58 guessed_mb_type = is_intra_likely ? MB_TYPE_INTRA4x4 :
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 (MB_TYPE_16x16 | (s->avctx->codec_id == AV_CODEC_ID_H264 ? MB_TYPE_L0 : MB_TYPE_FORWARD_MV));
1137
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 31 times.
12035 for (i = 0; i < s->mb_num; i++) {
1138 12004 const int mb_xy = s->mb_index2xy[i];
1139 12004 int error = s->error_status_table[mb_xy];
1140
3/4
✓ Branch 0 taken 3501 times.
✓ Branch 1 taken 8503 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3501 times.
12004 if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
1141 8503 continue;
1142
1143 3501 s->cur_pic.mb_type[mb_xy] = guessed_mb_type;
1144 }
1145
1146 // change inter to intra blocks if no reference frames are available
1147
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
31 if (!(s->last_pic.f && s->last_pic.f->data[0]) &&
1148
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
4 !(s->next_pic.f && s->next_pic.f->data[0]))
1149
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1 times.
301 for (i = 0; i < s->mb_num; i++) {
1150 300 const int mb_xy = s->mb_index2xy[i];
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
300 if (!IS_INTRA(s->cur_pic.mb_type[mb_xy]))
1152 s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
1153 }
1154
1155 /* handle inter blocks with damaged AC */
1156
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 31 times.
579 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1157
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 548 times.
12552 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1158 12004 const int mb_xy = mb_x + mb_y * s->mb_stride;
1159 12004 const int mb_type = s->cur_pic.mb_type[mb_xy];
1160
3/4
✓ Branch 0 taken 10516 times.
✓ Branch 1 taken 1488 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10516 times.
12004 const int dir = !(s->last_pic.f && s->last_pic.f->data[0]);
1161
2/2
✓ Branch 0 taken 1488 times.
✓ Branch 1 taken 10516 times.
12004 const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
1162 int mv_type;
1163
1164 12004 int error = s->error_status_table[mb_xy];
1165
1166
2/2
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 7849 times.
12004 if (IS_INTRA(mb_type))
1167 4155 continue; // intra
1168
2/2
✓ Branch 0 taken 3246 times.
✓ Branch 1 taken 4603 times.
7849 if (error & ER_MV_ERROR)
1169 3246 continue; // inter with damaged MV
1170
1/2
✓ Branch 0 taken 4603 times.
✗ Branch 1 not taken.
4603 if (!(error & ER_AC_ERROR))
1171 4603 continue; // undamaged inter
1172
1173 if (IS_8X8(mb_type)) {
1174 int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
1175 int j;
1176 mv_type = MV_TYPE_8X8;
1177 for (j = 0; j < 4; j++) {
1178 s->mv[0][j][0] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
1179 s->mv[0][j][1] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
1180 }
1181 } else {
1182 mv_type = MV_TYPE_16X16;
1183 s->mv[0][0][0] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
1184 s->mv[0][0][1] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
1185 }
1186
1187 s->decode_mb(s->opaque, 0 /* FIXME H.264 partitioned slices need this set */,
1188 mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
1189 }
1190 }
1191
1192 /* guess MVs */
1193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_B) {
1194 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1195 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1196 int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
1197 const int mb_xy = mb_x + mb_y * s->mb_stride;
1198 const int mb_type = s->cur_pic.mb_type[mb_xy];
1199 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1200
1201 int error = s->error_status_table[mb_xy];
1202
1203 if (IS_INTRA(mb_type))
1204 continue;
1205 if (!(error & ER_MV_ERROR))
1206 continue; // inter with undamaged MV
1207 if (!(error & ER_AC_ERROR))
1208 continue; // undamaged inter
1209
1210 if (!(s->last_pic.f && s->last_pic.f->data[0]))
1211 mv_dir &= ~MV_DIR_FORWARD;
1212 if (!(s->next_pic.f && s->next_pic.f->data[0]))
1213 mv_dir &= ~MV_DIR_BACKWARD;
1214
1215 if (s->pp_time) {
1216 int time_pp = s->pp_time;
1217 int time_pb = s->pb_time;
1218
1219 av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
1220 ff_thread_progress_await(s->next_pic.progress, mb_y);
1221
1222 s->mv[0][0][0] = s->next_pic.motion_val[0][xy][0] * time_pb / time_pp;
1223 s->mv[0][0][1] = s->next_pic.motion_val[0][xy][1] * time_pb / time_pp;
1224 s->mv[1][0][0] = s->next_pic.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
1225 s->mv[1][0][1] = s->next_pic.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
1226 } else {
1227 s->mv[0][0][0] = 0;
1228 s->mv[0][0][1] = 0;
1229 s->mv[1][0][0] = 0;
1230 s->mv[1][0][1] = 0;
1231 }
1232
1233 s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
1234 mb_x, mb_y, 0, 0);
1235 }
1236 }
1237 } else
1238 31 guess_mv(s);
1239
1240 /* fill DC for inter blocks */
1241
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 31 times.
579 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1242
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 548 times.
12552 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1243 int dc, dcu, dcv, y, n;
1244 int16_t *dc_ptr;
1245 uint8_t *dest_y, *dest_cb, *dest_cr;
1246 12004 const int mb_xy = mb_x + mb_y * s->mb_stride;
1247 12004 const int mb_type = s->cur_pic.mb_type[mb_xy];
1248
1249 // error = s->error_status_table[mb_xy];
1250
1251
4/4
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 7849 times.
✓ Branch 2 taken 3855 times.
✓ Branch 3 taken 300 times.
12004 if (IS_INTRA(mb_type) && s->partitioned_frame)
1252 3855 continue;
1253 // if (error & ER_MV_ERROR)
1254 // continue; // inter data damaged FIXME is this good?
1255
1256 8149 dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1257 8149 dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1258 8149 dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1259
1260 8149 dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
1261
2/2
✓ Branch 0 taken 32596 times.
✓ Branch 1 taken 8149 times.
40745 for (n = 0; n < 4; n++) {
1262 32596 dc = 0;
1263
2/2
✓ Branch 0 taken 260768 times.
✓ Branch 1 taken 32596 times.
293364 for (y = 0; y < 8; y++) {
1264 int x;
1265
2/2
✓ Branch 0 taken 2086144 times.
✓ Branch 1 taken 260768 times.
2346912 for (x = 0; x < 8; x++)
1266 2086144 dc += dest_y[x + (n & 1) * 8 +
1267 2086144 (y + (n >> 1) * 8) * linesize[0]];
1268 }
1269 32596 dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
1270 }
1271
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8149 times.
8149 if (!s->cur_pic.f->data[2])
1273 continue;
1274
1275 8149 dcu = dcv = 0;
1276
2/2
✓ Branch 0 taken 65192 times.
✓ Branch 1 taken 8149 times.
73341 for (y = 0; y < 8; y++) {
1277 int x;
1278
2/2
✓ Branch 0 taken 521536 times.
✓ Branch 1 taken 65192 times.
586728 for (x = 0; x < 8; x++) {
1279 521536 dcu += dest_cb[x + y * linesize[1]];
1280 521536 dcv += dest_cr[x + y * linesize[2]];
1281 }
1282 }
1283 8149 s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
1284 8149 s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
1285 }
1286 }
1287 #if 1
1288 /* guess DC for damaged blocks */
1289 31 guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
1290 31 guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
1291 31 guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
1292 #endif
1293
1294 /* filter luma DC */
1295 31 filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
1296
1297 #if 1
1298 /* render DC only intra */
1299
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 31 times.
579 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1300
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 548 times.
12552 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1301 uint8_t *dest_y, *dest_cb, *dest_cr;
1302 12004 const int mb_xy = mb_x + mb_y * s->mb_stride;
1303 12004 const int mb_type = s->cur_pic.mb_type[mb_xy];
1304
1305 12004 int error = s->error_status_table[mb_xy];
1306
1307
2/2
✓ Branch 0 taken 7849 times.
✓ Branch 1 taken 4155 times.
12004 if (IS_INTER(mb_type))
1308 7849 continue;
1309
2/2
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 255 times.
4155 if (!(error & ER_AC_ERROR))
1310 3900 continue; // undamaged
1311
1312 255 dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
1313 255 dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
1314 255 dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
1315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (!s->cur_pic.f->data[2])
1316 dest_cb = dest_cr = NULL;
1317
1318 255 put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
1319 }
1320 }
1321 #endif
1322
1323
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
1324 /* filter horizontal block boundaries */
1325 31 h_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
1326 31 s->mb_height * 2, linesize[0], 1);
1327
1328 /* filter vertical block boundaries */
1329 31 v_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
1330 31 s->mb_height * 2, linesize[0], 1);
1331
1332
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (s->cur_pic.f->data[2]) {
1333 31 h_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
1334 31 s->mb_height, linesize[1], 0);
1335 31 h_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
1336 31 s->mb_height, linesize[2], 0);
1337 31 v_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
1338 31 s->mb_height, linesize[1], 0);
1339 31 v_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
1340 31 s->mb_height, linesize[2], 0);
1341 }
1342 }
1343
1344 /* clean a few tables */
1345
2/2
✓ Branch 0 taken 12004 times.
✓ Branch 1 taken 31 times.
12035 for (i = 0; i < s->mb_num; i++) {
1346 12004 const int mb_xy = s->mb_index2xy[i];
1347 12004 int error = s->error_status_table[mb_xy];
1348
1349
3/4
✓ Branch 0 taken 11704 times.
✓ Branch 1 taken 300 times.
✓ Branch 2 taken 11704 times.
✗ Branch 3 not taken.
12004 if (s->mbskip_table && s->cur_pic.f->pict_type != AV_PICTURE_TYPE_B &&
1350
2/2
✓ Branch 0 taken 3402 times.
✓ Branch 1 taken 8302 times.
11704 (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
1351 3402 s->mbskip_table[mb_xy] = 0;
1352 }
1353
2/2
✓ Branch 0 taken 11704 times.
✓ Branch 1 taken 300 times.
12004 if (s->mbintra_table)
1354 11704 s->mbintra_table[mb_xy] = 1;
1355 }
1356
1357 31 memset(&s->cur_pic, 0, sizeof(ERPicture));
1358 31 memset(&s->last_pic, 0, sizeof(ERPicture));
1359 31 memset(&s->next_pic, 0, sizeof(ERPicture));
1360
1361 31 cleanup:
1362
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
93 for (i = 0; i < 2; i++) {
1363 62 av_freep(&s->ref_index[i]);
1364 62 av_freep(&s->motion_val_base[i]);
1365 62 s->cur_pic.ref_index[i] = NULL;
1366 62 s->cur_pic.motion_val[i] = NULL;
1367 }
1368 }
1369