FFmpeg coverage


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