FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/error_resilience.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 725 832 87.1%
Functions: 13 13 100.0%
Branches: 497 630 78.9%

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