FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/error_resilience.c
Date: 2025-07-11 09:13:03
Exec Total Coverage
Lines: 727 829 87.7%
Functions: 13 13 100.0%
Branches: 499 630 79.2%

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