FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/motion_est.c
Date: 2026-10-09 11:06:14
Exec Total Coverage
Lines: 872 1007 86.6%
Functions: 28 34 82.4%
Branches: 507 619 81.9%

Line Branch Exec Source
1 /*
2 * Motion estimation
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer
5 *
6 * new motion estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * Motion estimation.
28 */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <limits.h>
33
34 #include "avcodec.h"
35 #include "h263.h"
36 #include "mathops.h"
37 #include "motion_est.h"
38 #include "mpegutils.h"
39 #include "mpegvideoenc.h"
40
41 #define P_LEFT P[1]
42 #define P_TOP P[2]
43 #define P_TOPRIGHT P[3]
44 #define P_MEDIAN P[4]
45 #define P_MV1 P[9]
46
47 #define ME_MAP_SHIFT 3
48 #define ME_MAP_MV_BITS 11
49
50 static int sad_hpel_motion_search(MPVEncContext *const s,
51 int *mx_ptr, int *my_ptr, int dmin,
52 int src_index, int ref_index,
53 int size, int h);
54
55 6504849 static inline unsigned update_map_generation(MotionEstContext *c)
56 {
57 6504849 c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
58
2/2
✓ Branch 0 taken 6261 times.
✓ Branch 1 taken 6498588 times.
6504849 if(c->map_generation==0){
59 6261 c->map_generation= 1<<(ME_MAP_MV_BITS*2);
60 6261 memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
61 }
62 6504849 return c->map_generation;
63 }
64
65 /* shape adaptive search stuff */
66 typedef struct Minima{
67 int height;
68 int x, y;
69 int checked;
70 }Minima;
71
72 ✗ static int minima_cmp(const void *a, const void *b){
73 ✗ const Minima *da = (const Minima *) a;
74 ✗ const Minima *db = (const Minima *) b;
75
76 ✗ return da->height - db->height;
77 }
78
79 #define FLAG_QPEL 1 //must be 1
80 #define FLAG_CHROMA 2
81 #define FLAG_DIRECT 4
82
83 2915416 static inline void init_ref(MotionEstContext *c, uint8_t *const src[3],
84 uint8_t *const ref[3], uint8_t *const ref2[3],
85 int x, int y, int ref_index)
86 {
87 2915416 const int offset[3]= {
88 2915416 y*c-> stride + x,
89 2915416 ((y*c->uvstride + x)>>1),
90 2915416 ((y*c->uvstride + x)>>1),
91 };
92 int i;
93
2/2
✓ Branch 0 taken 8746248 times.
✓ Branch 1 taken 2915416 times.
11661664 for(i=0; i<3; i++){
94
2/2
✓ Branch 0 taken 8622138 times.
✓ Branch 1 taken 124110 times.
8746248 c->src[0][i]= src[i] ? src[i] + offset[i] : NULL;
95
2/2
✓ Branch 0 taken 8622138 times.
✓ Branch 1 taken 124110 times.
8746248 c->ref[0][i]= ref[i] ? ref[i] + offset[i] : NULL;
96 }
97
2/2
✓ Branch 0 taken 402556 times.
✓ Branch 1 taken 2512860 times.
2915416 if(ref_index){
98
2/2
✓ Branch 0 taken 1207668 times.
✓ Branch 1 taken 402556 times.
1610224 for(i=0; i<3; i++){
99
1/2
✓ Branch 0 taken 1207668 times.
✗ Branch 1 not taken.
1207668 c->ref[ref_index][i]= ref2[i] ? ref2[i] + offset[i] : NULL;
100 }
101 }
102 2915416 }
103
104 780 static int get_flags(MotionEstContext *c, int direct, int chroma){
105 780 return ((c->avctx->flags&AV_CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
780 + (direct ? FLAG_DIRECT : 0)
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 780 times.
780 + (chroma ? FLAG_CHROMA : 0);
108 }
109
110 2531462 static av_always_inline int cmp_direct_inline(MPVEncContext *const s, const int x, const int y, const int subx, const int suby,
111 const int size, const int h, int ref_index, int src_index,
112 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
113 2531462 MotionEstContext *const c = &s->me;
114 2531462 const int stride= c->stride;
115 2531462 const int hx = subx + x * (1 << (1 + qpel));
116 2531462 const int hy = suby + y * (1 << (1 + qpel));
117 2531462 const uint8_t * const * const ref = c->ref[ref_index];
118 2531462 const uint8_t * const * const src = c->src[src_index];
119 int d;
120 //FIXME check chroma 4mv, (no crashes ...)
121 av_assert2(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
122
2/4
✓ Branch 0 taken 2531462 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2531462 times.
✗ Branch 3 not taken.
2531462 if (x >= c->xmin && hx <= c->xmax << (qpel + 1) &&
123
2/4
✓ Branch 0 taken 2531462 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2531462 times.
✗ Branch 3 not taken.
5062924 y >= c->ymin && hy <= c->ymax << (qpel + 1)) {
124 2531462 const int time_pp = s->c.pp_time;
125 2531462 const int time_pb = s->c.pb_time;
126 2531462 const int mask = 2 * qpel + 1;
127
2/2
✓ Branch 0 taken 645651 times.
✓ Branch 1 taken 1885811 times.
2531462 if (s->c.mv_type == MV_TYPE_8X8) {
128
2/2
✓ Branch 0 taken 2582604 times.
✓ Branch 1 taken 645651 times.
3228255 for(int i = 0; i < 4; ++i) {
129 2582604 int fx = c->direct_basis_mv[i][0] + hx;
130 2582604 int fy = c->direct_basis_mv[i][1] + hy;
131
2/2
✓ Branch 0 taken 1615916 times.
✓ Branch 1 taken 966688 times.
2582604 int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0] * (time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));
132
2/2
✓ Branch 0 taken 1582088 times.
✓ Branch 1 taken 1000516 times.
2582604 int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1] * (time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));
133 2582604 int fxy = (fx & mask) + ((fy & mask) << (qpel + 1));
134 2582604 int bxy = (bx & mask) + ((by & mask) << (qpel + 1));
135
136 2582604 uint8_t *dst = c->temp + 8 * (i & 1) + 8 * stride * (i >> 1);
137
2/2
✓ Branch 0 taken 965484 times.
✓ Branch 1 taken 1617120 times.
2582604 if (qpel) {
138 965484 c->qpel_put[1][fxy](dst, ref[0] + (fx >> 2) + (fy >> 2) * stride, stride);
139 965484 c->qpel_avg[1][bxy](dst, ref[8] + (bx >> 2) + (by >> 2) * stride, stride);
140 } else {
141 1617120 c->hpel_put[1][fxy](dst, ref[0] + (fx >> 1) + (fy >> 1) * stride, stride, 8);
142 1617120 c->hpel_avg[1][bxy](dst, ref[8] + (bx >> 1) + (by >> 1) * stride, stride, 8);
143 }
144 }
145 } else {
146 1885811 int fx = c->direct_basis_mv[0][0] + hx;
147 1885811 int fy = c->direct_basis_mv[0][1] + hy;
148
2/2
✓ Branch 0 taken 1416003 times.
✓ Branch 1 taken 469808 times.
1885811 int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0] * (time_pb - time_pp)/time_pp);
149
2/2
✓ Branch 0 taken 1380545 times.
✓ Branch 1 taken 505266 times.
1885811 int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1] * (time_pb - time_pp)/time_pp);
150 1885811 int fxy = (fx & mask) + ((fy & mask) << (qpel + 1));
151 1885811 int bxy = (bx & mask) + ((by & mask) << (qpel + 1));
152
153
2/2
✓ Branch 0 taken 432937 times.
✓ Branch 1 taken 1452874 times.
1885811 if (qpel) {
154 432937 c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
155 432937 c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
156 432937 c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
157 432937 c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
158 432937 c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
159 432937 c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
160 432937 c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
161 432937 c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
162 } else {
163 av_assert2((fx >> 1) + 16 * s->c.mb_x >= -16);
164 av_assert2((fy >> 1) + 16 * s->c.mb_y >= -16);
165 av_assert2((fx >> 1) + 16 * s->c.mb_x <= s->c.width);
166 av_assert2((fy >> 1) + 16 * s->c.mb_y <= s->c.height);
167 av_assert2((bx >> 1) + 16 * s->c.mb_x >= -16);
168 av_assert2((by >> 1) + 16 * s->c.mb_y >= -16);
169 av_assert2((bx >> 1) + 16 * s->c.mb_x <= s->c.width);
170 av_assert2((by >> 1) + 16 * s->c.mb_y <= s->c.height);
171
172 1452874 c->hpel_put[0][fxy](c->temp, ref[0] + (fx >> 1) + (fy >> 1) * stride, stride, 16);
173 1452874 c->hpel_avg[0][bxy](c->temp, ref[8] + (bx >> 1) + (by >> 1) * stride, stride, 16);
174 }
175 }
176 2531462 d = cmp_func(s, c->temp, src[0], stride, 16);
177 } else
178 ✗ d = 256 * 256 * 256 * 32;
179 2531462 return d;
180 }
181
182 62335723 static av_always_inline int cmp_inline(MPVEncContext *const s, const int x, const int y, const int subx, const int suby,
183 const int size, const int h, int ref_index, int src_index,
184 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){
185 62335723 MotionEstContext *const c = &s->me;
186 62335723 const int stride= c->stride;
187 62335723 const int uvstride= c->uvstride;
188 62335723 const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
189 62335723 const int hx= subx + x*(1<<(1+qpel));
190 62335723 const int hy= suby + y*(1<<(1+qpel));
191 62335723 const uint8_t * const * const ref = c->ref[ref_index];
192 62335723 const uint8_t * const * const src = c->src[src_index];
193 int d;
194 //FIXME check chroma 4mv, (no crashes ...)
195 int uvdxy; /* no, it might not be used uninitialized */
196
2/2
✓ Branch 0 taken 8501839 times.
✓ Branch 1 taken 53833884 times.
62335723 if (dxy) {
197
2/2
✓ Branch 0 taken 6150958 times.
✓ Branch 1 taken 2350881 times.
8501839 if (qpel) {
198
1/2
✓ Branch 0 taken 6150958 times.
✗ Branch 1 not taken.
6150958 if (h << size == 16) {
199 6150958 c->qpel_put[size][dxy](c->temp, ref[0] + x + y * stride, stride); //FIXME prototype (add h)
200 ✗ } else if (size == 0 && h == 8) {
201 ✗ c->qpel_put[1][dxy](c->temp , ref[0] + x + y * stride , stride);
202 ✗ c->qpel_put[1][dxy](c->temp + 8, ref[0] + x + y * stride + 8, stride);
203 } else
204 av_assert2(0);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6150958 times.
6150958 if (chroma) {
206 ✗ int cx = hx / 2;
207 ✗ int cy = hy / 2;
208 ✗ cx = (cx >> 1) | (cx & 1);
209 ✗ cy = (cy >> 1) | (cy & 1);
210 ✗ uvdxy = (cx & 1) + 2 * (cy & 1);
211 // FIXME x/y wrong, but MPEG-4 qpel is sick anyway, we should drop as much of it as possible in favor for H.264
212 }
213 } else {
214 2350881 c->hpel_put[size][dxy](c->temp, ref[0] + x + y * stride, stride, h);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2350881 times.
2350881 if (chroma)
216 ✗ uvdxy = dxy | (x & 1) | (2 * (y & 1));
217 }
218 8501839 d = cmp_func(s, c->temp, src[0], stride, h);
219 } else {
220 53833884 d = cmp_func(s, src[0], ref[0] + x + y * stride, stride, h);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53833884 times.
53833884 if (chroma)
222 ✗ uvdxy = (x & 1) + 2 * (y & 1);
223 }
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62335723 times.
62335723 if (chroma) {
225 ✗ uint8_t *const uvtemp = c->temp + 16 * stride;
226 ✗ c->hpel_put[size + 1][uvdxy](uvtemp , ref[1] + (x >> 1) + (y >> 1) * uvstride, uvstride, h >> 1);
227 ✗ c->hpel_put[size + 1][uvdxy](uvtemp + 8, ref[2] + (x >> 1) + (y >> 1) * uvstride, uvstride, h >> 1);
228 ✗ d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h >> 1);
229 ✗ d += chroma_cmp_func(s, uvtemp + 8, src[2], uvstride, h >> 1);
230 }
231 62335723 return d;
232 }
233
234 ✗ static int cmp_simple(MPVEncContext *const s, const int x, const int y,
235 int ref_index, int src_index,
236 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){
237 ✗ return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
238 }
239
240 ✗ static int cmp_fpel_internal(MPVEncContext *const s, const int x, const int y,
241 const int size, const int h, int ref_index, int src_index,
242 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
243 ✗ if(flags&FLAG_DIRECT){
244 ✗ return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
245 }else{
246 ✗ return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
247 }
248 }
249
250 56164345 static int cmp_internal(MPVEncContext *const s, const int x, const int y, const int subx, const int suby,
251 const int size, const int h, int ref_index, int src_index,
252 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
253
2/2
✓ Branch 0 taken 1687626 times.
✓ Branch 1 taken 54476719 times.
56164345 if(flags&FLAG_DIRECT){
254 1687626 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
255 }else{
256 54476719 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL, flags&FLAG_CHROMA);
257 }
258 }
259
260 /** @brief compares a block (either a full macroblock or a partition thereof)
261 against a proposed motion-compensated prediction of that block
262 */
263 56164345 static av_always_inline int cmp(MPVEncContext *const s, const int x, const int y, const int subx, const int suby,
264 const int size, const int h, int ref_index, int src_index,
265 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
266 if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)
267 && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
268 && flags==0 && h==16 && size==0 && subx==0 && suby==0){
269 return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
270 }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
271 && subx==0 && suby==0){
272 return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
273 }else{
274 56164345 return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
275 }
276 }
277
278 2745944 static int cmp_hpel(MPVEncContext *const s, const int x, const int y, const int subx, const int suby,
279 const int size, const int h, int ref_index, int src_index,
280 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
281
2/2
✓ Branch 0 taken 570004 times.
✓ Branch 1 taken 2175940 times.
2745944 if(flags&FLAG_DIRECT){
282 570004 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
283 }else{
284 2175940 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
285 }
286 }
287
288 5956896 static int cmp_qpel(MPVEncContext *const s, const int x, const int y, const int subx, const int suby,
289 const int size, const int h, int ref_index, int src_index,
290 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
291
2/2
✓ Branch 0 taken 273832 times.
✓ Branch 1 taken 5683064 times.
5956896 if(flags&FLAG_DIRECT){
292 273832 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
293 }else{
294 5683064 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA);
295 }
296 }
297
298 #include "motion_est_template.c"
299
300 ✗ static int zero_cmp(MPVEncContext *const s, const uint8_t *a, const uint8_t *b,
301 ptrdiff_t stride, int h)
302 {
303 ✗ return 0;
304 }
305
306 ✗ static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
307 ✗ }
308
309 260 av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx,
310 const MECmpContext *mecc, int mpvenc)
311 {
312 260 int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
313 260 int dia_size = FFMAX(FFABS(avctx->dia_size) & 255, FFABS(avctx->pre_dia_size) & 255);
314 int ret;
315
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
260 if (FFMIN(avctx->dia_size, avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)) {
317 ✗ av_log(avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
318 ✗ return AVERROR(EINVAL);
319 }
320
321 260 c->avctx = avctx;
322
323
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 252 times.
260 if (avctx->codec_id == AV_CODEC_ID_H261)
324 8 avctx->me_sub_cmp = avctx->me_cmp;
325
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
260 if (cache_size < 2 * dia_size)
327 ✗ av_log(avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
328
329 260 ret = ff_set_cmp(mecc, c->me_pre_cmp, avctx->me_pre_cmp, mpvenc);
330 260 ret |= ff_set_cmp(mecc, c->me_cmp, avctx->me_cmp, mpvenc);
331 260 ret |= ff_set_cmp(mecc, c->me_sub_cmp, avctx->me_sub_cmp, mpvenc);
332 260 ret |= ff_set_cmp(mecc, c->mb_cmp, avctx->mb_cmp, mpvenc);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
260 if (ret < 0)
334 ✗ return ret;
335
336 260 c->sse = mecc->sse[0];
337 260 memcpy(c->pix_abs, mecc->pix_abs, sizeof(c->pix_abs));
338
339 260 c->flags = get_flags(c, 0, avctx->me_cmp & FF_CMP_CHROMA);
340 260 c->sub_flags = get_flags(c, 0, avctx->me_sub_cmp & FF_CMP_CHROMA);
341 260 c->mb_flags = get_flags(c, 0, avctx->mb_cmp & FF_CMP_CHROMA);
342
343
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 252 times.
260 if (avctx->codec_id == AV_CODEC_ID_H261) {
344 8 c->sub_motion_search = no_sub_motion_search;
345
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 238 times.
252 } else if (avctx->flags & AV_CODEC_FLAG_QPEL) {
346 14 c->sub_motion_search= qpel_motion_search;
347 }else{
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
349 ✗ c->sub_motion_search= hpel_motion_search;
350
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 20 times.
238 else if( c->avctx->me_sub_cmp == FF_CMP_SAD
351
1/2
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
218 && c->avctx-> me_cmp == FF_CMP_SAD
352
1/2
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
218 && c->avctx-> mb_cmp == FF_CMP_SAD)
353 218 c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
354 else
355 20 c->sub_motion_search= hpel_motion_search;
356 }
357
358 /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
359 * not have yet, and even if we had, the motion estimation code
360 * does not expect it. */
361
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 15 times.
260 if (avctx->codec_id != AV_CODEC_ID_SNOW) {
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if ((avctx->me_cmp & FF_CMP_CHROMA) /* && !c->me_cmp[2] */)
363 ✗ c->me_cmp[2] = zero_cmp;
364
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
245 if ((avctx->me_sub_cmp & FF_CMP_CHROMA) && !c->me_sub_cmp[2])
365 ✗ c->me_sub_cmp[2] = zero_cmp;
366 }
367
368 260 return 0;
369 }
370
371 16077 void ff_me_init_pic(MPVEncContext *const s)
372 {
373 16077 MotionEstContext *const c = &s->me;
374
375 /*FIXME s->c.no_rounding b_type*/
376
2/2
✓ Branch 0 taken 536 times.
✓ Branch 1 taken 15541 times.
16077 if (c->avctx->flags & AV_CODEC_FLAG_QPEL) {
377 536 c->qpel_avg = s->c.qdsp.avg_qpel_pixels_tab;
378
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 437 times.
536 if (s->c.no_rounding)
379 99 c->qpel_put = s->c.qdsp.put_no_rnd_qpel_pixels_tab;
380 else
381 437 c->qpel_put = s->c.qdsp.put_qpel_pixels_tab;
382 }
383 16077 c->hpel_avg = s->c.hdsp.avg_pixels_tab;
384
2/2
✓ Branch 0 taken 1868 times.
✓ Branch 1 taken 14209 times.
16077 if (s->c.no_rounding)
385 1868 c->hpel_put = s->c.hdsp.put_no_rnd_pixels_tab;
386 else
387 14209 c->hpel_put = s->c.hdsp.put_pixels_tab;
388
389
1/2
✓ Branch 0 taken 16077 times.
✗ Branch 1 not taken.
16077 if (s->c.linesize) {
390 16077 c->stride = s->c.linesize;
391 16077 c->uvstride = s->c.uvlinesize;
392 }else{
393 ✗ c->stride = 16*s->c.mb_width + 32;
394 ✗ c->uvstride = 8*s->c.mb_width + 16;
395 }
396
1/2
✓ Branch 0 taken 16077 times.
✗ Branch 1 not taken.
16077 if (s->c.codec_id != AV_CODEC_ID_SNOW) {
397 16077 c->hpel_put[2][0]= c->hpel_put[2][1]=
398 16077 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
399 }
400 /* Reset the average MB variance and scene change stats */
401 16077 c->scene_change_score = 0;
402 16077 c->mb_var_sum_temp =
403 16077 c->mc_mb_var_sum_temp = 0;
404 16077 }
405
406 #define CHECK_SAD_HALF_MV(suffix, x, y) \
407 {\
408 d = c->pix_abs[size][(x ? 1 : 0) + (y ? 2 : 0)](NULL, pix, ptr + ((x) >> 1), stride, h); \
409 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
410 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
411 }
412
413 4943767 static int sad_hpel_motion_search(MPVEncContext *const s,
414 int *mx_ptr, int *my_ptr, int dmin,
415 int src_index, int ref_index,
416 int size, int h)
417 {
418 4943767 MotionEstContext *const c = &s->me;
419 4943767 const int penalty_factor= c->sub_penalty_factor;
420 int mx, my, dminh;
421 const uint8_t *pix, *ptr;
422 4943767 int stride= c->stride;
423 4943767 LOAD_COMMON
424
425 av_assert2(c->sub_flags == 0);
426
427
2/2
✓ Branch 0 taken 411934 times.
✓ Branch 1 taken 4531833 times.
4943767 if(c->skip){
428 411934 *mx_ptr = 0;
429 411934 *my_ptr = 0;
430 411934 return dmin;
431 }
432
433 4531833 pix = c->src[src_index][0];
434
435 4531833 mx = *mx_ptr;
436 4531833 my = *my_ptr;
437 4531833 ptr = c->ref[ref_index][0] + (my * stride) + mx;
438
439 4531833 dminh = dmin;
440
441
6/6
✓ Branch 0 taken 4454050 times.
✓ Branch 1 taken 77783 times.
✓ Branch 2 taken 4382239 times.
✓ Branch 3 taken 71811 times.
✓ Branch 4 taken 4268872 times.
✓ Branch 5 taken 113367 times.
4531833 if (mx > xmin && mx < xmax &&
442
2/2
✓ Branch 0 taken 4185229 times.
✓ Branch 1 taken 83643 times.
4268872 my > ymin && my < ymax) {
443 4185229 int dx=0, dy=0;
444 int d, pen_x, pen_y;
445 4185229 const int index= my*(1<<ME_MAP_SHIFT) + mx;
446 4185229 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
447 4185229 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
448 4185229 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
449 4185229 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
450 4185229 mx += mx;
451 4185229 my += my;
452
453
454 4185229 pen_x= pred_x + mx;
455 4185229 pen_y= pred_y + my;
456
457 4185229 ptr-= stride;
458
2/2
✓ Branch 0 taken 2126431 times.
✓ Branch 1 taken 2058798 times.
4185229 if(t<=b){
459 2126431 CHECK_SAD_HALF_MV(y2 , 0, -1)
460
2/2
✓ Branch 0 taken 1322616 times.
✓ Branch 1 taken 803815 times.
2126431 if(l<=r){
461 1322616 CHECK_SAD_HALF_MV(xy2, -1, -1)
462
2/2
✓ Branch 0 taken 772009 times.
✓ Branch 1 taken 550607 times.
1322616 if(t+r<=b+l){
463 772009 CHECK_SAD_HALF_MV(xy2, +1, -1)
464 772009 ptr+= stride;
465 }else{
466 550607 ptr+= stride;
467 550607 CHECK_SAD_HALF_MV(xy2, -1, +1)
468 }
469 1322616 CHECK_SAD_HALF_MV(x2 , -1, 0)
470 }else{
471 803815 CHECK_SAD_HALF_MV(xy2, +1, -1)
472
2/2
✓ Branch 0 taken 441497 times.
✓ Branch 1 taken 362318 times.
803815 if(t+l<=b+r){
473 441497 CHECK_SAD_HALF_MV(xy2, -1, -1)
474 441497 ptr+= stride;
475 }else{
476 362318 ptr+= stride;
477 362318 CHECK_SAD_HALF_MV(xy2, +1, +1)
478 }
479 803815 CHECK_SAD_HALF_MV(x2 , +1, 0)
480 }
481 }else{
482
2/2
✓ Branch 0 taken 787774 times.
✓ Branch 1 taken 1271024 times.
2058798 if(l<=r){
483
2/2
✓ Branch 0 taken 333760 times.
✓ Branch 1 taken 454014 times.
787774 if(t+l<=b+r){
484 333760 CHECK_SAD_HALF_MV(xy2, -1, -1)
485 333760 ptr+= stride;
486 }else{
487 454014 ptr+= stride;
488 454014 CHECK_SAD_HALF_MV(xy2, +1, +1)
489 }
490 787774 CHECK_SAD_HALF_MV(x2 , -1, 0)
491 787774 CHECK_SAD_HALF_MV(xy2, -1, +1)
492 }else{
493
2/2
✓ Branch 0 taken 563707 times.
✓ Branch 1 taken 707317 times.
1271024 if(t+r<=b+l){
494 563707 CHECK_SAD_HALF_MV(xy2, +1, -1)
495 563707 ptr+= stride;
496 }else{
497 707317 ptr+= stride;
498 707317 CHECK_SAD_HALF_MV(xy2, -1, +1)
499 }
500 1271024 CHECK_SAD_HALF_MV(x2 , +1, 0)
501 1271024 CHECK_SAD_HALF_MV(xy2, +1, +1)
502 }
503 2058798 CHECK_SAD_HALF_MV(y2 , 0, +1)
504 }
505 4185229 mx+=dx;
506 4185229 my+=dy;
507
508 }else{
509 346604 mx += mx;
510 346604 my += my;
511 }
512
513 4531833 *mx_ptr = mx;
514 4531833 *my_ptr = my;
515 4531833 return dminh;
516 }
517
518 2512860 static inline void set_p_mv_tables(MPVEncContext *const s, int mx, int my, int mv4)
519 {
520 2512860 const int xy = s->c.mb_x + s->c.mb_y * s->c.mb_stride;
521
522 2512860 s->p_mv_table[xy][0] = mx;
523 2512860 s->p_mv_table[xy][1] = my;
524
525 /* has already been set to the 4 MV if 4MV is done */
526
2/2
✓ Branch 0 taken 2236015 times.
✓ Branch 1 taken 276845 times.
2512860 if(mv4){
527 2236015 int mot_xy = s->c.block_index[0];
528
529 2236015 s->c.cur_pic.motion_val[0][mot_xy ][0] = mx;
530 2236015 s->c.cur_pic.motion_val[0][mot_xy ][1] = my;
531 2236015 s->c.cur_pic.motion_val[0][mot_xy + 1][0] = mx;
532 2236015 s->c.cur_pic.motion_val[0][mot_xy + 1][1] = my;
533
534 2236015 mot_xy += s->c.b8_stride;
535 2236015 s->c.cur_pic.motion_val[0][mot_xy ][0] = mx;
536 2236015 s->c.cur_pic.motion_val[0][mot_xy ][1] = my;
537 2236015 s->c.cur_pic.motion_val[0][mot_xy + 1][0] = mx;
538 2236015 s->c.cur_pic.motion_val[0][mot_xy + 1][1] = my;
539 }
540 2512860 }
541
542 /**
543 * get fullpel ME search limits.
544 */
545 3904433 static inline void get_limits(MPVEncContext *const s, int x, int y, int bframe)
546 {
547 3904433 MotionEstContext *const c = &s->me;
548
2/2
✓ Branch 0 taken 168759 times.
✓ Branch 1 taken 3735674 times.
3904433 int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
549
2/2
✓ Branch 0 taken 168759 times.
✓ Branch 1 taken 3735674 times.
3904433 int max_range = MAX_MV >> (1 + !!(c->flags&FLAG_QPEL));
550 /*
551 if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
552 else c->range= 16;
553 */
554
2/2
✓ Branch 0 taken 1748602 times.
✓ Branch 1 taken 2155831 times.
3904433 if (c->unrestricted_mv) {
555 1748602 c->xmin = - x - 16;
556 1748602 c->ymin = - y - 16;
557 1748602 c->xmax = - x + s->c.width;
558 1748602 c->ymax = - y + s->c.height;
559
2/2
✓ Branch 0 taken 107415 times.
✓ Branch 1 taken 2048416 times.
2155831 } else if (!(av_builtin_constant_p(bframe) && bframe) && s->c.out_format == FMT_H261){
560 // Search range of H.261 is different from other codec standards
561
2/2
✓ Branch 0 taken 102528 times.
✓ Branch 1 taken 4887 times.
107415 c->xmin = (x > 15) ? - 15 : 0;
562
2/2
✓ Branch 0 taken 101442 times.
✓ Branch 1 taken 5973 times.
107415 c->ymin = (y > 15) ? - 15 : 0;
563
2/2
✓ Branch 0 taken 102528 times.
✓ Branch 1 taken 4887 times.
107415 c->xmax = (x < s->c.mb_width * 16 - 16) ? 15 : 0;
564
2/2
✓ Branch 0 taken 101442 times.
✓ Branch 1 taken 5973 times.
107415 c->ymax = (y < s->c.mb_height * 16 - 16) ? 15 : 0;
565 } else {
566 2048416 c->xmin = - x;
567 2048416 c->ymin = - y;
568 2048416 c->xmax = - x + s->c.mb_width *16 - 16;
569 2048416 c->ymax = - y + s->c.mb_height*16 - 16;
570 }
571
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3904433 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3904433 if(!range || range > max_range)
572 3904433 range = max_range;
573
1/2
✓ Branch 0 taken 3904433 times.
✗ Branch 1 not taken.
3904433 if(range){
574 3904433 c->xmin = FFMAX(c->xmin,-range);
575 3904433 c->xmax = FFMIN(c->xmax, range);
576 3904433 c->ymin = FFMAX(c->ymin,-range);
577 3904433 c->ymax = FFMIN(c->ymax, range);
578 }
579 3904433 }
580
581 276845 static inline void init_mv4_ref(MotionEstContext *c){
582 276845 const int stride= c->stride;
583
584 276845 c->ref[1][0] = c->ref[0][0] + 8;
585 276845 c->ref[2][0] = c->ref[0][0] + 8*stride;
586 276845 c->ref[3][0] = c->ref[2][0] + 8;
587 276845 c->src[1][0] = c->src[0][0] + 8;
588 276845 c->src[2][0] = c->src[0][0] + 8*stride;
589 276845 c->src[3][0] = c->src[2][0] + 8;
590 276845 }
591
592 276845 static inline int h263_mv4_search(MPVEncContext *const s, int mx, int my, int shift)
593 {
594 276845 MotionEstContext *const c = &s->me;
595 276845 const int size= 1;
596 276845 const int h=8;
597 int block;
598 int P[10][2];
599 276845 int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
600 276845 int same=1;
601 276845 const int stride= c->stride;
602 276845 const uint8_t *mv_penalty = c->current_mv_penalty;
603
4/6
✓ Branch 0 taken 276845 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2242 times.
✓ Branch 3 taken 274603 times.
✓ Branch 4 taken 2242 times.
✗ Branch 5 not taken.
276845 int safety_clipping = c->unrestricted_mv && (s->c.width&15) && (s->c.height&15);
604
605 276845 init_mv4_ref(c);
606
607
2/2
✓ Branch 0 taken 1107380 times.
✓ Branch 1 taken 276845 times.
1384225 for(block=0; block<4; block++){
608 int mx4, my4;
609 int pred_x4, pred_y4;
610 int dmin4;
611 static const int off[4]= {2, 1, 1, -1};
612 1107380 const int mot_stride = s->c.b8_stride;
613 1107380 const int mot_xy = s->c.block_index[block];
614
615
2/2
✓ Branch 0 taken 8968 times.
✓ Branch 1 taken 1098412 times.
1107380 if(safety_clipping){
616 8968 c->xmax = - 16*s->c.mb_x + s->c.width - 8*(block &1);
617 8968 c->ymax = - 16*s->c.mb_y + s->c.height - 8*(block>>1);
618 }
619
620 1107380 P_LEFT[0] = s->c.cur_pic.motion_val[0][mot_xy - 1][0];
621 1107380 P_LEFT[1] = s->c.cur_pic.motion_val[0][mot_xy - 1][1];
622
623
2/2
✓ Branch 0 taken 1587 times.
✓ Branch 1 taken 1105793 times.
1107380 if (P_LEFT[0] > c->xmax * (1 << shift)) P_LEFT[0] = c->xmax * (1 << shift);
624
625 /* special case for first line */
626
4/4
✓ Branch 0 taken 68112 times.
✓ Branch 1 taken 1039268 times.
✓ Branch 2 taken 34056 times.
✓ Branch 3 taken 34056 times.
1107380 if (s->c.first_slice_line && block < 2) {
627 34056 c->pred_x= pred_x4= P_LEFT[0];
628 34056 c->pred_y= pred_y4= P_LEFT[1];
629 } else {
630 1073324 P_TOP[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][0];
631 1073324 P_TOP[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][1];
632 1073324 P_TOPRIGHT[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + off[block]][0];
633 1073324 P_TOPRIGHT[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + off[block]][1];
634
2/2
✓ Branch 0 taken 1579 times.
✓ Branch 1 taken 1071745 times.
1073324 if (P_TOP[1] > c->ymax * (1 << shift)) P_TOP[1] = c->ymax * (1 << shift);
635
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1073302 times.
1073324 if (P_TOPRIGHT[0] < c->xmin * (1 << shift)) P_TOPRIGHT[0] = c->xmin * (1 << shift);
636
2/2
✓ Branch 0 taken 1036 times.
✓ Branch 1 taken 1072288 times.
1073324 if (P_TOPRIGHT[0] > c->xmax * (1 << shift)) P_TOPRIGHT[0] = c->xmax * (1 << shift);
637
2/2
✓ Branch 0 taken 1508 times.
✓ Branch 1 taken 1071816 times.
1073324 if (P_TOPRIGHT[1] > c->ymax * (1 << shift)) P_TOPRIGHT[1] = c->ymax * (1 << shift);
638
639 1073324 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
640 1073324 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
641
642 1073324 c->pred_x= pred_x4 = P_MEDIAN[0];
643 1073324 c->pred_y= pred_y4 = P_MEDIAN[1];
644 }
645 1107380 P_MV1[0]= mx;
646 1107380 P_MV1[1]= my;
647
2/2
✓ Branch 0 taken 8968 times.
✓ Branch 1 taken 1098412 times.
1107380 if(safety_clipping)
648
2/2
✓ Branch 0 taken 80712 times.
✓ Branch 1 taken 8968 times.
89680 for(i=1; i<10; i++){
649
8/8
✓ Branch 0 taken 28512 times.
✓ Branch 1 taken 52200 times.
✓ Branch 2 taken 14256 times.
✓ Branch 3 taken 14256 times.
✓ Branch 4 taken 12672 times.
✓ Branch 5 taken 1584 times.
✓ Branch 6 taken 11088 times.
✓ Branch 7 taken 1584 times.
80712 if (s->c.first_slice_line && block < 2 && i > 1 && i < 9)
650 11088 continue;
651
4/4
✓ Branch 0 taken 38504 times.
✓ Branch 1 taken 31120 times.
✓ Branch 2 taken 29536 times.
✓ Branch 3 taken 8968 times.
69624 if (i>4 && i<9)
652 29536 continue;
653
2/2
✓ Branch 0 taken 1249 times.
✓ Branch 1 taken 38839 times.
40088 if (P[i][0] > c->xmax * (1 << shift)) P[i][0] = c->xmax * (1 << shift);
654
2/2
✓ Branch 0 taken 1577 times.
✓ Branch 1 taken 38511 times.
40088 if (P[i][1] > c->ymax * (1 << shift)) P[i][1] = c->ymax * (1 <<shift );
655 }
656
657 1107380 dmin4 = epzs_motion_search2(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift, 1);
658
659 1107380 dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
660
661
2/2
✓ Branch 0 taken 267932 times.
✓ Branch 1 taken 839448 times.
1107380 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
662 int dxy;
663 267932 const int offset= ((block&1) + (block>>1)*stride)*8;
664 267932 uint8_t *dest_y = c->scratchpad + offset;
665
2/2
✓ Branch 0 taken 56760 times.
✓ Branch 1 taken 211172 times.
267932 if (s->c.quarter_sample) {
666 56760 const uint8_t *ref = c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
667 56760 dxy = ((my4 & 3) << 2) | (mx4 & 3);
668
669 56760 c->qpel_put[1][dxy](dest_y, ref, stride);
670 }else{
671 211172 const uint8_t *ref = c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
672 211172 dxy = ((my4 & 1) << 1) | (mx4 & 1);
673
674 211172 c->hpel_put[1][dxy](dest_y, ref, stride, h);
675 }
676 267932 dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
677 }else
678 839448 dmin_sum+= dmin4;
679
680
2/2
✓ Branch 0 taken 56760 times.
✓ Branch 1 taken 1050620 times.
1107380 if (s->c.quarter_sample) {
681 56760 mx4_sum+= mx4/2;
682 56760 my4_sum+= my4/2;
683 }else{
684 1050620 mx4_sum+= mx4;
685 1050620 my4_sum+= my4;
686 }
687
688 1107380 s->c.cur_pic.motion_val[0][s->c.block_index[block]][0] = mx4;
689 1107380 s->c.cur_pic.motion_val[0][s->c.block_index[block]][1] = my4;
690
691
4/4
✓ Branch 0 taken 573490 times.
✓ Branch 1 taken 533890 times.
✓ Branch 2 taken 137638 times.
✓ Branch 3 taken 435852 times.
1107380 if(mx4 != mx || my4 != my) same=0;
692 }
693
694
2/2
✓ Branch 0 taken 42139 times.
✓ Branch 1 taken 234706 times.
276845 if(same)
695 42139 return INT_MAX;
696
697
2/2
✓ Branch 0 taken 66090 times.
✓ Branch 1 taken 168616 times.
234706 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
698 66090 dmin_sum += c->mb_cmp[0](s,
699 66090 s->new_pic->data[0] +
700 66090 s->c.mb_x * 16 + s->c.mb_y * 16 * stride,
701 66090 c->scratchpad, stride, 16);
702 }
703
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234706 times.
234706 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
705 ✗ int mx_chroma = ff_h263_round_chroma(mx4_sum);
706 ✗ int my_chroma = ff_h263_round_chroma(my4_sum);
707 ✗ int dxy = ((my_chroma & 1) << 1) | (mx_chroma & 1);
708
709 ✗ int offset = (s->c.mb_x*8 + (mx_chroma>>1)) + (s->c.mb_y*8 + (my_chroma>>1))*s->c.uvlinesize;
710
711 ✗ c->hpel_put[1][dxy](c->scratchpad , s->c.last_pic.data[1] + offset, s->c.uvlinesize, 8);
712 ✗ c->hpel_put[1][dxy](c->scratchpad + 8, s->c.last_pic.data[2] + offset, s->c.uvlinesize, 8);
713
714 ✗ dmin_sum += c->mb_cmp[1](s, s->new_pic->data[1] + s->c.mb_x * 8 + s->c.mb_y * 8 * s->c.uvlinesize, c->scratchpad, s->c.uvlinesize, 8);
715 ✗ dmin_sum += c->mb_cmp[1](s, s->new_pic->data[2] + s->c.mb_x * 8 + s->c.mb_y * 8 * s->c.uvlinesize, c->scratchpad + 8, s->c.uvlinesize, 8);
716 }
717
718 234706 c->pred_x= mx;
719 234706 c->pred_y= my;
720
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234706 times.
234706 switch(c->avctx->mb_cmp&0xFF){
722 /*case FF_CMP_SSE:
723 return dmin_sum+ 32*s->c.qscale*s->c.qscale;*/
724 ✗ case FF_CMP_RD:
725 ✗ return dmin_sum;
726 234706 default:
727 234706 return dmin_sum+ 11*c->mb_penalty_factor;
728 }
729 }
730
731 331233 static inline void init_interlaced_ref(MPVEncContext *const s, int ref_index)
732 {
733 331233 MotionEstContext *const c = &s->me;
734
735 331233 c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->c.linesize;
736 331233 c->src[1][0] = c->src[0][0] + s->c.linesize;
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 331233 times.
331233 if(c->flags & FLAG_CHROMA){
738 ✗ c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->c.uvlinesize;
739 ✗ c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->c.uvlinesize;
740 ✗ c->src[1][1] = c->src[0][1] + s->c.uvlinesize;
741 ✗ c->src[1][2] = c->src[0][2] + s->c.uvlinesize;
742 }
743 331233 }
744
745 331233 static int interlaced_search(MPVEncContext *const s, int ref_index,
746 int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
747 {
748 331233 MotionEstContext *const c = &s->me;
749 331233 const int size=0;
750 331233 const int h=8;
751 int block;
752 int P[10][2];
753 331233 const uint8_t * const mv_penalty = c->current_mv_penalty;
754 331233 int same=1;
755 331233 const int stride = 2*s->c.linesize;
756 331233 int dmin_sum= 0;
757 331233 const int mot_stride = s->c.mb_stride;
758 331233 const int xy = s->c.mb_x + s->c.mb_y*mot_stride;
759
760 331233 c->ymin>>=1;
761 331233 c->ymax>>=1;
762 331233 c->stride<<=1;
763 331233 c->uvstride<<=1;
764 331233 init_interlaced_ref(s, ref_index);
765
766
2/2
✓ Branch 0 taken 662466 times.
✓ Branch 1 taken 331233 times.
993699 for(block=0; block<2; block++){
767 int field_select;
768 662466 int best_dmin= INT_MAX;
769 662466 int best_field= -1;
770
771
2/2
✓ Branch 0 taken 1324932 times.
✓ Branch 1 taken 662466 times.
1987398 for(field_select=0; field_select<2; field_select++){
772 int dmin, mx_i, my_i;
773 1324932 int16_t (*mv_table)[2]= mv_tables[block][field_select];
774
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if(user_field_select){
776 av_assert1(field_select==0 || field_select==1);
777 av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
778 ✗ if(field_select_tables[block][xy] != field_select)
779 ✗ continue;
780 }
781
782 1324932 P_LEFT[0] = mv_table[xy - 1][0];
783 1324932 P_LEFT[1] = mv_table[xy - 1][1];
784
2/2
✓ Branch 0 taken 32534 times.
✓ Branch 1 taken 1292398 times.
1324932 if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
785
786 1324932 c->pred_x= P_LEFT[0];
787 1324932 c->pred_y= P_LEFT[1];
788
789
2/2
✓ Branch 0 taken 1206192 times.
✓ Branch 1 taken 118740 times.
1324932 if (!s->c.first_slice_line) {
790 1206192 P_TOP[0] = mv_table[xy - mot_stride][0];
791 1206192 P_TOP[1] = mv_table[xy - mot_stride][1];
792 1206192 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
793 1206192 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
794
2/2
✓ Branch 0 taken 32861 times.
✓ Branch 1 taken 1173331 times.
1206192 if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
795
2/2
✓ Branch 0 taken 26048 times.
✓ Branch 1 taken 1180144 times.
1206192 if (P_TOPRIGHT[0] < c->xmin * (1 << 1)) P_TOPRIGHT[0] = c->xmin * (1 << 1);
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1206192 times.
1206192 if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
797
2/2
✓ Branch 0 taken 30755 times.
✓ Branch 1 taken 1175437 times.
1206192 if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
798
799 1206192 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
800 1206192 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
801 }
802 1324932 P_MV1[0]= mx; //FIXME not correct if block != field_select
803 1324932 P_MV1[1]= my / 2;
804
805 1324932 dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1, 0);
806
807 1324932 dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
808
809 1324932 mv_table[xy][0]= mx_i;
810 1324932 mv_table[xy][1]= my_i;
811
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
813 int dxy;
814
815 //FIXME chroma ME
816 ✗ const uint8_t *ref = c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
817 ✗ dxy = ((my_i & 1) << 1) | (mx_i & 1);
818
819 ✗ c->hpel_put[size][dxy](c->scratchpad, ref, stride, h);
820 ✗ dmin = c->mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
821 ✗ dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
822 }else
823 1324932 dmin+= c->mb_penalty_factor; //field_select bits
824
825 1324932 dmin += field_select != block; //slightly prefer same field
826
827
2/2
✓ Branch 0 taken 994601 times.
✓ Branch 1 taken 330331 times.
1324932 if(dmin < best_dmin){
828 994601 best_dmin= dmin;
829 994601 best_field= field_select;
830 }
831 }
832 {
833 662466 int16_t (*mv_table)[2]= mv_tables[block][best_field];
834
835
2/2
✓ Branch 0 taken 257125 times.
✓ Branch 1 taken 405341 times.
662466 if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
836
2/2
✓ Branch 0 taken 59258 times.
✓ Branch 1 taken 603208 times.
662466 if(mv_table[xy][1]&1) same=0;
837
2/2
✓ Branch 0 taken 445290 times.
✓ Branch 1 taken 217176 times.
662466 if(mv_table[xy][1]*2 != my) same=0;
838
2/2
✓ Branch 0 taken 318840 times.
✓ Branch 1 taken 343626 times.
662466 if(best_field != block) same=0;
839 }
840
841 662466 field_select_tables[block][xy]= best_field;
842 662466 dmin_sum += best_dmin;
843 }
844
845 331233 c->ymin *= 2;
846 331233 c->ymax<<=1;
847 331233 c->stride>>=1;
848 331233 c->uvstride>>=1;
849
850
2/2
✓ Branch 0 taken 55185 times.
✓ Branch 1 taken 276048 times.
331233 if(same)
851 55185 return INT_MAX;
852
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 276048 times.
276048 switch(c->avctx->mb_cmp&0xFF){
854 /*case FF_CMP_SSE:
855 return dmin_sum+ 32*s->c.qscale*s->c.qscale;*/
856 ✗ case FF_CMP_RD:
857 ✗ return dmin_sum;
858 276048 default:
859 276048 return dmin_sum+ 11*c->mb_penalty_factor;
860 }
861 }
862
863 8745927 static inline int get_penalty_factor(int lambda, int lambda2, int type){
864
3/7
✓ Branch 0 taken 8117824 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 340979 times.
✓ Branch 5 taken 287124 times.
✗ Branch 6 not taken.
8745927 switch(type&0xFF){
865 8117824 default:
866 case FF_CMP_SAD:
867 8117824 return lambda>>FF_LAMBDA_SHIFT;
868 ✗ case FF_CMP_DCT:
869 ✗ return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
870 ✗ case FF_CMP_W53:
871 ✗ return (4*lambda)>>(FF_LAMBDA_SHIFT);
872 ✗ case FF_CMP_W97:
873 ✗ return (2*lambda)>>(FF_LAMBDA_SHIFT);
874 340979 case FF_CMP_SATD:
875 case FF_CMP_DCT264:
876 340979 return (2*lambda)>>FF_LAMBDA_SHIFT;
877 287124 case FF_CMP_RD:
878 case FF_CMP_PSNR:
879 case FF_CMP_SSE:
880 case FF_CMP_NSSE:
881 287124 return lambda2>>FF_LAMBDA_SHIFT;
882 ✗ case FF_CMP_BIT:
883 case FF_CMP_MEDIAN_SAD:
884 ✗ return 1;
885 }
886 }
887
888 2512860 void ff_estimate_p_frame_motion(MPVEncContext *const s,
889 int mb_x, int mb_y)
890 {
891 2512860 MotionEstContext *const c = &s->me;
892 const uint8_t *pix, *ppix;
893 2512860 int sum, mx = 0, my = 0, dmin = 0;
894 int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
895 int vard; ///< sum of squared differences with the estimated motion vector
896 int P[10][2];
897 2512860 const int shift = 1 + s->c.quarter_sample;
898 2512860 int mb_type=0;
899
900 2512860 init_ref(c, s->new_pic->data, s->c.last_pic.data, NULL, 16*mb_x, 16*mb_y, 0);
901
902
3/4
✓ Branch 0 taken 15570 times.
✓ Branch 1 taken 2497290 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15570 times.
2512860 av_assert0(s->c.quarter_sample == 0 || s->c.quarter_sample == 1);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2512860 times.
2512860 av_assert0(s->c.linesize == c->stride);
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2512860 times.
2512860 av_assert0(s->c.uvlinesize == c->uvstride);
905
906 2512860 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
907 2512860 c->sub_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
908 2512860 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
909 2512860 c->current_mv_penalty = c->mv_penalty[s->f_code] + MAX_DMV;
910
911 2512860 get_limits(s, 16*mb_x, 16*mb_y, 0);
912 2512860 c->skip=0;
913
914 /* intra / predictive decision */
915 2512860 pix = c->src[0][0];
916 2512860 sum = s->mpvencdsp.pix_sum(pix, s->c.linesize);
917 2512860 varc = s->mpvencdsp.pix_norm1(pix, s->c.linesize) -
918 2512860 (((unsigned) sum * sum) >> 8) + 500;
919
920 2512860 s->mb_mean[s->c.mb_stride * mb_y + mb_x] = (sum + 128) >> 8;
921 2512860 s->mb_var [s->c.mb_stride * mb_y + mb_x] = (varc + 128) >> 8;
922 2512860 c->mb_var_sum_temp += (varc+128)>>8;
923
924
1/2
✓ Branch 0 taken 2512860 times.
✗ Branch 1 not taken.
2512860 if (c->motion_est != FF_ME_ZERO) {
925 2512860 const int mot_stride = s->c.b8_stride;
926 2512860 const int mot_xy = s->c.block_index[0];
927
928 2512860 P_LEFT[0] = s->c.cur_pic.motion_val[0][mot_xy - 1][0];
929 2512860 P_LEFT[1] = s->c.cur_pic.motion_val[0][mot_xy - 1][1];
930
931
2/2
✓ Branch 0 taken 21346 times.
✓ Branch 1 taken 2491514 times.
2512860 if (P_LEFT[0] > (c->xmax << shift))
932 21346 P_LEFT[0] = c->xmax << shift;
933
934
2/2
✓ Branch 0 taken 2322475 times.
✓ Branch 1 taken 190385 times.
2512860 if (!s->c.first_slice_line) {
935 2322475 P_TOP[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][0];
936 2322475 P_TOP[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][1];
937 2322475 P_TOPRIGHT[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + 2][0];
938 2322475 P_TOPRIGHT[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + 2][1];
939
2/2
✓ Branch 0 taken 23898 times.
✓ Branch 1 taken 2298577 times.
2322475 if (P_TOP[1] > (c->ymax << shift))
940 23898 P_TOP[1] = c->ymax << shift;
941
2/2
✓ Branch 0 taken 22836 times.
✓ Branch 1 taken 2299639 times.
2322475 if (P_TOPRIGHT[0] < (c->xmin * (1 << shift)))
942 22836 P_TOPRIGHT[0] = c->xmin * (1 << shift);
943
2/2
✓ Branch 0 taken 22070 times.
✓ Branch 1 taken 2300405 times.
2322475 if (P_TOPRIGHT[1] > (c->ymax * (1 << shift)))
944 22070 P_TOPRIGHT[1] = c->ymax * (1 << shift);
945
946 2322475 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
947 2322475 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
948
949
2/2
✓ Branch 0 taken 1162355 times.
✓ Branch 1 taken 1160120 times.
2322475 if (s->c.out_format == FMT_H263) {
950 1162355 c->pred_x = P_MEDIAN[0];
951 1162355 c->pred_y = P_MEDIAN[1];
952 } else { /* MPEG-1 at least */
953 1160120 c->pred_x = P_LEFT[0];
954 1160120 c->pred_y = P_LEFT[1];
955 }
956 } else {
957 190385 c->pred_x = P_LEFT[0];
958 190385 c->pred_y = P_LEFT[1];
959 }
960 2512860 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
961 }
962
963 /* At this point (mx,my) are full-pell and the relative displacement */
964 2512860 ppix = c->ref[0][0] + (my * s->c.linesize) + mx;
965
966 2512860 vard = c->sse(NULL, pix, ppix, s->c.linesize, 16);
967
968 2512860 s->mc_mb_var[s->c.mb_stride * mb_y + mb_x] = (vard+128)>>8;
969 2512860 c->mc_mb_var_sum_temp += (vard+128)>>8;
970
971
2/2
✓ Branch 0 taken 387498 times.
✓ Branch 1 taken 2125362 times.
2512860 if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {
972 387498 int p_score = FFMIN(vard, varc - 500 + (s->lambda2 >> FF_LAMBDA_SHIFT)*100);
973 387498 int i_score = varc - 500 + (s->lambda2 >> FF_LAMBDA_SHIFT)*20;
974 387498 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
975
976
3/4
✓ Branch 0 taken 103680 times.
✓ Branch 1 taken 283818 times.
✓ Branch 2 taken 103680 times.
✗ Branch 3 not taken.
387498 if (vard*2 + 200*256 > varc && !s->intra_penalty)
977 103680 mb_type|= CANDIDATE_MB_TYPE_INTRA;
978
3/4
✓ Branch 0 taken 2168 times.
✓ Branch 1 taken 385330 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2168 times.
387498 if (varc*2 + 200*256 > vard || s->c.qscale > 24){
979 // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
980 385330 mb_type|= CANDIDATE_MB_TYPE_INTER;
981 385330 c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
982
2/2
✓ Branch 0 taken 86233 times.
✓ Branch 1 taken 299097 times.
385330 if (s->mpv_flags & FF_MPV_FLAG_MV0)
983
4/4
✓ Branch 0 taken 5471 times.
✓ Branch 1 taken 80762 times.
✓ Branch 2 taken 4816 times.
✓ Branch 3 taken 655 times.
86233 if(mx || my)
984 85578 mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
985 }else{
986 2168 mx *= 1 << shift;
987 2168 my *= 1 << shift;
988 }
989
2/2
✓ Branch 0 taken 302859 times.
✓ Branch 1 taken 84639 times.
387498 if ((c->avctx->flags & AV_CODEC_FLAG_4MV)
990
6/6
✓ Branch 0 taken 302852 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 286060 times.
✓ Branch 3 taken 16792 times.
✓ Branch 4 taken 276845 times.
✓ Branch 5 taken 9215 times.
302859 && !c->skip && varc>50<<8 && vard>10<<8){
991
2/2
✓ Branch 1 taken 234706 times.
✓ Branch 2 taken 42139 times.
276845 if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
992 234706 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
993
994 276845 set_p_mv_tables(s, mx, my, 0);
995 }else
996 110653 set_p_mv_tables(s, mx, my, 1);
997
2/2
✓ Branch 0 taken 15600 times.
✓ Branch 1 taken 371898 times.
387498 if ((c->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
998
1/2
✓ Branch 0 taken 15600 times.
✗ Branch 1 not taken.
15600 && !c->skip){ //FIXME varc/d checks
999
2/2
✓ Branch 1 taken 13542 times.
✓ Branch 2 taken 2058 times.
15600 if(interlaced_search(s, 0, s->c.p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1000 13542 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1001 }
1002 }else{
1003 int intra_score, i;
1004 2125362 mb_type= CANDIDATE_MB_TYPE_INTER;
1005
1006 2125362 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1007
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2125362 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2125362 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1008 ✗ dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1009
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2125362 times.
2125362 if ((c->avctx->flags & AV_CODEC_FLAG_4MV)
1011 ✗ && !c->skip && varc>50<<8 && vard>10<<8){
1012 ✗ int dmin4= h263_mv4_search(s, mx, my, shift);
1013 ✗ if(dmin4 < dmin){
1014 ✗ mb_type= CANDIDATE_MB_TYPE_INTER4V;
1015 ✗ dmin=dmin4;
1016 }
1017 }
1018
2/2
✓ Branch 0 taken 85236 times.
✓ Branch 1 taken 2040126 times.
2125362 if ((c->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1019
2/2
✓ Branch 0 taken 85233 times.
✓ Branch 1 taken 3 times.
85236 && !c->skip){ //FIXME varc/d checks
1020 85233 int dmin_i= interlaced_search(s, 0, s->c.p_field_mv_table, s->p_field_select_table, mx, my, 0);
1021
2/2
✓ Branch 0 taken 11208 times.
✓ Branch 1 taken 74025 times.
85233 if(dmin_i < dmin){
1022 11208 mb_type = CANDIDATE_MB_TYPE_INTER_I;
1023 11208 dmin= dmin_i;
1024 }
1025 }
1026
1027 2125362 set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1028
1029 /* get intra luma score */
1030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2125362 times.
2125362 if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1031 ✗ intra_score= varc - 500;
1032 }else{
1033 2125362 unsigned mean = (sum+128)>>8;
1034 2125362 mean*= 0x01010101;
1035
1036
2/2
✓ Branch 0 taken 34005792 times.
✓ Branch 1 taken 2125362 times.
36131154 for(i=0; i<16; i++){
1037 34005792 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+ 0]) = mean;
1038 34005792 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+ 4]) = mean;
1039 34005792 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+ 8]) = mean;
1040 34005792 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+12]) = mean;
1041 }
1042
1043 2125362 intra_score= c->mb_cmp[0](s, c->scratchpad, pix, s->c.linesize, 16);
1044 }
1045 2125362 intra_score += c->mb_penalty_factor*16 + s->intra_penalty;
1046
1047
2/2
✓ Branch 0 taken 95682 times.
✓ Branch 1 taken 2029680 times.
2125362 if(intra_score < dmin){
1048 95682 mb_type= CANDIDATE_MB_TYPE_INTRA;
1049 95682 s->c.cur_pic.mb_type[mb_y*s->c.mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1050 }else
1051 2029680 s->c.cur_pic.mb_type[mb_y*s->c.mb_stride + mb_x] = 0;
1052
1053 {
1054 2125362 int p_score = FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1055 2125362 int i_score = varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1056 2125362 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1057 }
1058 }
1059
1060 2512860 s->mb_type[mb_y*s->c.mb_stride + mb_x] = mb_type;
1061 2512860 }
1062
1063 ✗ int ff_pre_estimate_p_frame_motion(MPVEncContext *const s,
1064 int mb_x, int mb_y)
1065 {
1066 ✗ MotionEstContext *const c = &s->me;
1067 int mx, my, dmin;
1068 int P[10][2];
1069 ✗ const int shift = 1 + s->c.quarter_sample;
1070 ✗ const int xy = mb_x + mb_y*s->c.mb_stride;
1071 ✗ init_ref(c, s->new_pic->data, s->c.last_pic.data, NULL, 16*mb_x, 16*mb_y, 0);
1072
1073 ✗ av_assert0(s->c.quarter_sample == 0 || s->c.quarter_sample == 1);
1074
1075 ✗ c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1076 ✗ c->current_mv_penalty = c->mv_penalty[s->f_code] + MAX_DMV;
1077
1078 ✗ get_limits(s, 16*mb_x, 16*mb_y, 0);
1079 ✗ c->skip=0;
1080
1081 ✗ P_LEFT[0] = FFMAX(s->p_mv_table[xy + 1][0], c->xmin * (1 << shift));
1082 ✗ P_LEFT[1] = s->p_mv_table[xy + 1][1];
1083
1084 /* special case for first line */
1085 ✗ if (s->c.first_slice_line) {
1086 ✗ c->pred_x= P_LEFT[0];
1087 ✗ c->pred_y= P_LEFT[1];
1088 ✗ P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1089 ✗ P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
1090 } else {
1091 ✗ P_TOP[0] = s->p_mv_table[xy + s->c.mb_stride ][0];
1092 ✗ P_TOP[1] = s->p_mv_table[xy + s->c.mb_stride ][1];
1093 ✗ P_TOPRIGHT[0] = s->p_mv_table[xy + s->c.mb_stride - 1][0];
1094 ✗ P_TOPRIGHT[1] = s->p_mv_table[xy + s->c.mb_stride - 1][1];
1095 ✗ if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
1096 ✗ if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1097 ✗ if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1098
1099 ✗ P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1100 ✗ P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1101
1102 ✗ c->pred_x = P_MEDIAN[0];
1103 ✗ c->pred_y = P_MEDIAN[1];
1104 }
1105
1106 ✗ dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1107
1108 ✗ s->p_mv_table[xy][0] = mx<<shift;
1109 ✗ s->p_mv_table[xy][1] = my<<shift;
1110
1111 ✗ return dmin;
1112 }
1113
1114 804898 static int estimate_motion_b(MPVEncContext *const s, int mb_x, int mb_y,
1115 int16_t (*mv_table)[2], int ref_index, int f_code)
1116 {
1117 804898 MotionEstContext *const c = &s->me;
1118 804898 int mx = 0, my = 0, dmin = 0;
1119 int P[10][2];
1120 804898 const int shift= 1+s->c.quarter_sample;
1121 804898 const int mot_stride = s->c.mb_stride;
1122 804898 const int mot_xy = mb_y*mot_stride + mb_x;
1123 804898 const uint8_t * const mv_penalty = c->mv_penalty[f_code] + MAX_DMV;
1124 int mv_scale;
1125
1126 804898 c->current_mv_penalty= mv_penalty;
1127
1128 804898 get_limits(s, 16*mb_x, 16*mb_y, 1);
1129
1130
1/2
✓ Branch 0 taken 804898 times.
✗ Branch 1 not taken.
804898 if (c->motion_est != FF_ME_ZERO) {
1131 804898 P_LEFT[0] = mv_table[mot_xy - 1][0];
1132 804898 P_LEFT[1] = mv_table[mot_xy - 1][1];
1133
1134
2/2
✓ Branch 0 taken 10151 times.
✓ Branch 1 taken 794747 times.
804898 if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
1135
1136 /* special case for first line */
1137
2/2
✓ Branch 0 taken 742504 times.
✓ Branch 1 taken 62394 times.
804898 if (!s->c.first_slice_line) {
1138 742504 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1139 742504 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1140 742504 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
1141 742504 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
1142
2/2
✓ Branch 0 taken 11593 times.
✓ Branch 1 taken 730911 times.
742504 if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
1143
2/2
✓ Branch 0 taken 8177 times.
✓ Branch 1 taken 734327 times.
742504 if (P_TOPRIGHT[0] < c->xmin * (1 << shift)) P_TOPRIGHT[0] = c->xmin * (1 << shift);
1144
2/2
✓ Branch 0 taken 10949 times.
✓ Branch 1 taken 731555 times.
742504 if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
1145
1146 742504 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1147 742504 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1148 }
1149 804898 c->pred_x = P_LEFT[0];
1150 804898 c->pred_y = P_LEFT[1];
1151
1152 804898 int64_t pp = s->c.next_pic.ptr->f->pts - s->c.last_pic.ptr->f->pts;
1153 804898 int64_t pb = s->c. cur_pic.ptr->f->pts - s->c.last_pic.ptr->f->pts;
1154
2/2
✓ Branch 0 taken 402449 times.
✓ Branch 1 taken 402449 times.
804898 if (mv_table == s->b_forw_mv_table)
1155 402449 mv_scale = (pb << (16 - shift)) / pp;
1156 else
1157 402449 mv_scale = (pb - pp) * (1 << (16 - shift)) / pp;
1158
1159 804898 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1160 }
1161
1162 804898 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1163
1164
3/4
✓ Branch 0 taken 291674 times.
✓ Branch 1 taken 513224 times.
✓ Branch 2 taken 291674 times.
✗ Branch 3 not taken.
804898 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1165 291674 dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1166
1167 // s->mb_type[mb_y*s->c.mb_width + mb_x]= mb_type;
1168 804898 mv_table[mot_xy][0]= mx;
1169 804898 mv_table[mot_xy][1]= my;
1170
1171 804898 return dmin;
1172 }
1173
1174 5328399 static inline int check_bidir_mv(MPVEncContext *const s,
1175 int motion_fx, int motion_fy,
1176 int motion_bx, int motion_by,
1177 int pred_fx, int pred_fy,
1178 int pred_bx, int pred_by,
1179 int size, int h)
1180 {
1181 //FIXME optimize?
1182 //FIXME better f_code prediction (max mv & distance)
1183 //FIXME pointers
1184 5328399 MotionEstContext *const c = &s->me;
1185 5328399 const uint8_t * const mv_penalty_f = c->mv_penalty[s->f_code] + MAX_DMV; // f_code of the prev frame
1186 5328399 const uint8_t * const mv_penalty_b = c->mv_penalty[s->b_code] + MAX_DMV; // f_code of the prev frame
1187 5328399 int stride= c->stride;
1188 5328399 uint8_t *dest_y = c->scratchpad;
1189 const uint8_t *ptr;
1190 int dxy;
1191 int src_x, src_y;
1192 int fbmin;
1193 5328399 const uint8_t *const *src_data = c->src[0];
1194 5328399 const uint8_t *const *ref_data = c->ref[0];
1195 5328399 const uint8_t *const *ref2_data = c->ref[2];
1196
1197
2/2
✓ Branch 0 taken 626268 times.
✓ Branch 1 taken 4702131 times.
5328399 if(s->c.quarter_sample){
1198 626268 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1199 626268 src_x = motion_fx >> 2;
1200 626268 src_y = motion_fy >> 2;
1201
1202 626268 ptr = ref_data[0] + (src_y * stride) + src_x;
1203 626268 s->c.qdsp.put_qpel_pixels_tab[0][dxy](dest_y, ptr, stride);
1204
1205 626268 dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1206 626268 src_x = motion_bx >> 2;
1207 626268 src_y = motion_by >> 2;
1208
1209 626268 ptr = ref2_data[0] + (src_y * stride) + src_x;
1210 626268 s->c.qdsp.avg_qpel_pixels_tab[size][dxy](dest_y, ptr, stride);
1211 }else{
1212 4702131 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1213 4702131 src_x = motion_fx >> 1;
1214 4702131 src_y = motion_fy >> 1;
1215
1216 4702131 ptr = ref_data[0] + (src_y * stride) + src_x;
1217 4702131 s->c.hdsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1218
1219 4702131 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1220 4702131 src_x = motion_bx >> 1;
1221 4702131 src_y = motion_by >> 1;
1222
1223 4702131 ptr = ref2_data[0] + (src_y * stride) + src_x;
1224 4702131 s->c.hdsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1225 }
1226
1227 10656798 fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
1228 5328399 +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
1229 5328399 + c->mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
1230
1231 5328399 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1232 }
1233 //FIXME CHROMA !!!
1234
1235 5328399 return fbmin;
1236 }
1237
1238 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1239 402449 static inline int bidir_refine(MPVEncContext *const s, int mb_x, int mb_y)
1240 {
1241 402449 MotionEstContext *const c = &s->me;
1242 402449 const int mot_stride = s->c.mb_stride;
1243 402449 const int xy = mb_y *mot_stride + mb_x;
1244 int fbmin;
1245 402449 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1246 402449 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1247 402449 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1248 402449 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1249 402449 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1250 402449 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1251 402449 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1252 402449 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1253 402449 const int flags= c->sub_flags;
1254 402449 const int qpel= flags&FLAG_QPEL;
1255 402449 const int shift= 1+qpel;
1256 402449 const int xmin= c->xmin * (1 << shift);
1257 402449 const int ymin= c->ymin * (1 << shift);
1258 402449 const int xmax= c->xmax<<shift;
1259 402449 const int ymax= c->ymax<<shift;
1260 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
1261 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
1262 402449 int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
1263 402449 uint8_t map[256] = { 0 };
1264
1265 402449 map[hashidx&255] = 1;
1266
1267 402449 fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1268 motion_bx, motion_by,
1269 pred_fx, pred_fy,
1270 pred_bx, pred_by,
1271 0, 16);
1272
1273
1/2
✓ Branch 0 taken 402449 times.
✗ Branch 1 not taken.
402449 if (c->avctx->bidir_refine) {
1274 int end;
1275 static const uint8_t limittab[5]={0,8,32,64,80};
1276 402449 const int limit = limittab[c->avctx->bidir_refine];
1277 static const int8_t vect[][4]={
1278 { 0, 0, 0, 1}, { 0, 0, 0,-1}, { 0, 0, 1, 0}, { 0, 0,-1, 0}, { 0, 1, 0, 0}, { 0,-1, 0, 0}, { 1, 0, 0, 0}, {-1, 0, 0, 0},
1279
1280 { 0, 0, 1, 1}, { 0, 0,-1,-1}, { 0, 1, 1, 0}, { 0,-1,-1, 0}, { 1, 1, 0, 0}, {-1,-1, 0, 0}, { 1, 0, 0, 1}, {-1, 0, 0,-1},
1281 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
1282 { 0, 0,-1, 1}, { 0, 0, 1,-1}, { 0,-1, 1, 0}, { 0, 1,-1, 0}, {-1, 1, 0, 0}, { 1,-1, 0, 0}, { 1, 0, 0,-1}, {-1, 0, 0, 1},
1283 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
1284
1285 { 0, 1, 1, 1}, { 0,-1,-1,-1}, { 1, 1, 1, 0}, {-1,-1,-1, 0}, { 1, 1, 0, 1}, {-1,-1, 0,-1}, { 1, 0, 1, 1}, {-1, 0,-1,-1},
1286 { 0,-1, 1, 1}, { 0, 1,-1,-1}, {-1, 1, 1, 0}, { 1,-1,-1, 0}, { 1, 1, 0,-1}, {-1,-1, 0, 1}, { 1, 0,-1, 1}, {-1, 0, 1,-1},
1287 { 0, 1,-1, 1}, { 0,-1, 1,-1}, { 1,-1, 1, 0}, {-1, 1,-1, 0}, {-1, 1, 0, 1}, { 1,-1, 0,-1}, { 1, 0, 1,-1}, {-1, 0,-1, 1},
1288 { 0, 1, 1,-1}, { 0,-1,-1, 1}, { 1, 1,-1, 0}, {-1,-1, 1, 0}, { 1,-1, 0, 1}, {-1, 1, 0,-1}, {-1, 0, 1, 1}, { 1, 0,-1,-1},
1289
1290 { 1, 1, 1, 1}, {-1,-1,-1,-1},
1291 { 1, 1, 1,-1}, {-1,-1,-1, 1}, { 1, 1,-1, 1}, {-1,-1, 1,-1}, { 1,-1, 1, 1}, {-1, 1,-1,-1}, {-1, 1, 1, 1}, { 1,-1,-1,-1},
1292 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
1293 };
1294 static const uint8_t hash[]={
1295 HASH8( 0, 0, 0, 1), HASH8( 0, 0, 0,-1), HASH8( 0, 0, 1, 0), HASH8( 0, 0,-1, 0), HASH8( 0, 1, 0, 0), HASH8( 0,-1, 0, 0), HASH8( 1, 0, 0, 0), HASH8(-1, 0, 0, 0),
1296
1297 HASH8( 0, 0, 1, 1), HASH8( 0, 0,-1,-1), HASH8( 0, 1, 1, 0), HASH8( 0,-1,-1, 0), HASH8( 1, 1, 0, 0), HASH8(-1,-1, 0, 0), HASH8( 1, 0, 0, 1), HASH8(-1, 0, 0,-1),
1298 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
1299 HASH8( 0, 0,-1, 1), HASH8( 0, 0, 1,-1), HASH8( 0,-1, 1, 0), HASH8( 0, 1,-1, 0), HASH8(-1, 1, 0, 0), HASH8( 1,-1, 0, 0), HASH8( 1, 0, 0,-1), HASH8(-1, 0, 0, 1),
1300 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
1301
1302 HASH8( 0, 1, 1, 1), HASH8( 0,-1,-1,-1), HASH8( 1, 1, 1, 0), HASH8(-1,-1,-1, 0), HASH8( 1, 1, 0, 1), HASH8(-1,-1, 0,-1), HASH8( 1, 0, 1, 1), HASH8(-1, 0,-1,-1),
1303 HASH8( 0,-1, 1, 1), HASH8( 0, 1,-1,-1), HASH8(-1, 1, 1, 0), HASH8( 1,-1,-1, 0), HASH8( 1, 1, 0,-1), HASH8(-1,-1, 0, 1), HASH8( 1, 0,-1, 1), HASH8(-1, 0, 1,-1),
1304 HASH8( 0, 1,-1, 1), HASH8( 0,-1, 1,-1), HASH8( 1,-1, 1, 0), HASH8(-1, 1,-1, 0), HASH8(-1, 1, 0, 1), HASH8( 1,-1, 0,-1), HASH8( 1, 0, 1,-1), HASH8(-1, 0,-1, 1),
1305 HASH8( 0, 1, 1,-1), HASH8( 0,-1,-1, 1), HASH8( 1, 1,-1, 0), HASH8(-1,-1, 1, 0), HASH8( 1,-1, 0, 1), HASH8(-1, 1, 0,-1), HASH8(-1, 0, 1, 1), HASH8( 1, 0,-1,-1),
1306
1307 HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
1308 HASH8( 1, 1, 1,-1), HASH8(-1,-1,-1, 1), HASH8( 1, 1,-1, 1), HASH8(-1,-1, 1,-1), HASH8( 1,-1, 1, 1), HASH8(-1, 1,-1,-1), HASH8(-1, 1, 1, 1), HASH8( 1,-1,-1,-1),
1309 HASH8( 1, 1,-1,-1), HASH8(-1,-1, 1, 1), HASH8( 1,-1,-1, 1), HASH8(-1, 1, 1,-1), HASH8( 1,-1, 1,-1), HASH8(-1, 1,-1, 1),
1310 };
1311
1312 #define CHECK_BIDIR(fx,fy,bx,by)\
1313 if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
1314 &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1315 &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1316 int score;\
1317 map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
1318 score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\
1319 if(score < fbmin){\
1320 hashidx += HASH(fx,fy,bx,by);\
1321 fbmin= score;\
1322 motion_fx+=fx;\
1323 motion_fy+=fy;\
1324 motion_bx+=bx;\
1325 motion_by+=by;\
1326 end=0;\
1327 }\
1328 }
1329 #define CHECK_BIDIR2(a,b,c,d)\
1330 CHECK_BIDIR(a,b,c,d)\
1331 CHECK_BIDIR(-(a),-(b),-(c),-(d))
1332
1333 do{
1334 int i;
1335 810506 int borderdist=0;
1336 810506 end=1;
1337
1338
12/12
✓ Branch 0 taken 759017 times.
✓ Branch 1 taken 51489 times.
✓ Branch 2 taken 746489 times.
✓ Branch 3 taken 12528 times.
✓ Branch 5 taken 91351 times.
✓ Branch 6 taken 655138 times.
✓ Branch 7 taken 683277 times.
✓ Branch 8 taken 127229 times.
✓ Branch 9 taken 670682 times.
✓ Branch 10 taken 12595 times.
✓ Branch 12 taken 110585 times.
✓ Branch 13 taken 560097 times.
810506 CHECK_BIDIR2(0,0,0,1)
1339
12/12
✓ Branch 0 taken 696970 times.
✓ Branch 1 taken 113536 times.
✓ Branch 2 taken 685851 times.
✓ Branch 3 taken 11119 times.
✓ Branch 5 taken 98303 times.
✓ Branch 6 taken 587548 times.
✓ Branch 7 taken 601626 times.
✓ Branch 8 taken 208880 times.
✓ Branch 9 taken 591629 times.
✓ Branch 10 taken 9997 times.
✓ Branch 12 taken 80450 times.
✓ Branch 13 taken 511179 times.
810506 CHECK_BIDIR2(0,0,1,0)
1340
12/12
✓ Branch 0 taken 644807 times.
✓ Branch 1 taken 165699 times.
✓ Branch 2 taken 627575 times.
✓ Branch 3 taken 17232 times.
✓ Branch 5 taken 90380 times.
✓ Branch 6 taken 537195 times.
✓ Branch 7 taken 546326 times.
✓ Branch 8 taken 264180 times.
✓ Branch 9 taken 528957 times.
✓ Branch 10 taken 17369 times.
✓ Branch 12 taken 57212 times.
✓ Branch 13 taken 471745 times.
810506 CHECK_BIDIR2(0,1,0,0)
1341
12/12
✓ Branch 0 taken 577763 times.
✓ Branch 1 taken 232743 times.
✓ Branch 2 taken 561959 times.
✓ Branch 3 taken 15804 times.
✓ Branch 5 taken 63625 times.
✓ Branch 6 taken 498334 times.
✓ Branch 7 taken 528951 times.
✓ Branch 8 taken 281555 times.
✓ Branch 9 taken 512808 times.
✓ Branch 10 taken 16143 times.
✓ Branch 12 taken 70996 times.
✓ Branch 13 taken 441812 times.
810506 CHECK_BIDIR2(1,0,0,0)
1342
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810506 times.
810506 for(i=8; i<limit; i++){
1344 ✗ int fx= motion_fx+vect[i][0];
1345 ✗ int fy= motion_fy+vect[i][1];
1346 ✗ int bx= motion_bx+vect[i][2];
1347 ✗ int by= motion_by+vect[i][3];
1348 ✗ if(borderdist<=0){
1349 ✗ int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
1350 ✗ int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
1351 ✗ if((a|b) < 0)
1352 ✗ map[(hashidx+hash[i])&255] = 1;
1353 }
1354 ✗ if(!map[(hashidx+hash[i])&255]){
1355 int score;
1356 ✗ map[(hashidx+hash[i])&255] = 1;
1357 ✗ score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
1358 ✗ if(score < fbmin){
1359 ✗ hashidx += hash[i];
1360 ✗ fbmin= score;
1361 ✗ motion_fx=fx;
1362 ✗ motion_fy=fy;
1363 ✗ motion_bx=bx;
1364 ✗ motion_by=by;
1365 ✗ end=0;
1366 ✗ borderdist--;
1367 ✗ if(borderdist<=0){
1368 ✗ int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
1369 ✗ int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
1370 ✗ borderdist= FFMIN(a,b);
1371 }
1372 }
1373 }
1374 }
1375
2/2
✓ Branch 0 taken 408057 times.
✓ Branch 1 taken 402449 times.
810506 }while(!end);
1376 }
1377
1378 402449 s->b_bidir_forw_mv_table[xy][0]= motion_fx;
1379 402449 s->b_bidir_forw_mv_table[xy][1]= motion_fy;
1380 402449 s->b_bidir_back_mv_table[xy][0]= motion_bx;
1381 402449 s->b_bidir_back_mv_table[xy][1]= motion_by;
1382
1383 402449 return fbmin;
1384 }
1385
1386 184204 static inline int direct_search(MPVEncContext *const s, int mb_x, int mb_y)
1387 {
1388 184204 MotionEstContext *const c = &s->me;
1389 int P[10][2];
1390 184204 const int mot_stride = s->c.mb_stride;
1391 184204 const int mot_xy = mb_y*mot_stride + mb_x;
1392 184204 const int shift= 1+s->c.quarter_sample;
1393 int dmin, i;
1394 184204 const int time_pp= s->c.pp_time;
1395 184204 const int time_pb= s->c.pb_time;
1396 int mx, my, xmin, xmax, ymin, ymax;
1397 184204 int16_t (*mv_table)[2]= s->b_direct_mv_table;
1398
1399 184204 c->current_mv_penalty= c->mv_penalty[1] + MAX_DMV;
1400 184204 ymin= xmin=(-32)>>shift;
1401 184204 ymax= xmax= 31>>shift;
1402
1403
2/2
✓ Branch 0 taken 53419 times.
✓ Branch 1 taken 130785 times.
184204 if (IS_8X8(s->c.next_pic.mb_type[mot_xy])) {
1404 53419 s->c.mv_type = MV_TYPE_8X8;
1405 }else{
1406 130785 s->c.mv_type = MV_TYPE_16X16;
1407 }
1408
1409
2/2
✓ Branch 0 taken 344461 times.
✓ Branch 1 taken 53419 times.
397880 for(i=0; i<4; i++){
1410 344461 int index= s->c.block_index[i];
1411 int min, max;
1412
1413 344461 c->co_located_mv[i][0] = s->c.next_pic.motion_val[0][index][0];
1414 344461 c->co_located_mv[i][1] = s->c.next_pic.motion_val[0][index][1];
1415 344461 c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1416 344461 c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1417 // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1418 // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1419
1420
2/2
✓ Branch 0 taken 134742 times.
✓ Branch 1 taken 209719 times.
344461 max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1421
2/2
✓ Branch 0 taken 134742 times.
✓ Branch 1 taken 209719 times.
344461 min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1422 344461 max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1423 344461 min+= 16*mb_x - 1;
1424 344461 xmax= FFMIN(xmax, s->c.width - max);
1425 344461 xmin= FFMAX(xmin, - 16 - min);
1426
1427
2/2
✓ Branch 0 taken 137410 times.
✓ Branch 1 taken 207051 times.
344461 max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1428
2/2
✓ Branch 0 taken 137410 times.
✓ Branch 1 taken 207051 times.
344461 min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1429 344461 max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1430 344461 min+= 16*mb_y - 1;
1431 344461 ymax= FFMIN(ymax, s->c.height - max);
1432 344461 ymin= FFMAX(ymin, - 16 - min);
1433
1434
2/2
✓ Branch 0 taken 130785 times.
✓ Branch 1 taken 213676 times.
344461 if(s->c.mv_type == MV_TYPE_16X16) break;
1435 }
1436
1437 av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1438
1439
7/8
✓ Branch 0 taken 184155 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 184154 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 184119 times.
✓ Branch 5 taken 35 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 184119 times.
184204 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1440 85 s->b_direct_mv_table[mot_xy][0]= 0;
1441 85 s->b_direct_mv_table[mot_xy][1]= 0;
1442
1443 85 return 256*256*256*64-1;
1444 }
1445
1446 184119 c->xmin= xmin;
1447 184119 c->ymin= ymin;
1448 184119 c->xmax= xmax;
1449 184119 c->ymax= ymax;
1450 184119 c->flags |= FLAG_DIRECT;
1451 184119 c->sub_flags |= FLAG_DIRECT;
1452 184119 c->pred_x=0;
1453 184119 c->pred_y=0;
1454
1455 184119 P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin * (1 << shift), xmax << shift);
1456 184119 P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin * (1 << shift), ymax << shift);
1457
1458 /* special case for first line */
1459
2/2
✓ Branch 0 taken 171335 times.
✓ Branch 1 taken 12784 times.
184119 if (!s->c.first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
1460 171335 P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin * (1 << shift), xmax << shift);
1461 171335 P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin * (1 << shift), ymax << shift);
1462 171335 P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1][0], xmin * (1 << shift), xmax << shift);
1463 171335 P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1][1], ymin * (1 << shift), ymax << shift);
1464
1465 171335 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1466 171335 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1467 }
1468
1469 184119 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1470
2/2
✓ Branch 0 taken 38277 times.
✓ Branch 1 taken 145842 times.
184119 if(c->sub_flags&FLAG_QPEL)
1471 38277 dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1472 else
1473 145842 dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1474
1475
4/4
✓ Branch 0 taken 107544 times.
✓ Branch 1 taken 76575 times.
✓ Branch 2 taken 107518 times.
✓ Branch 3 taken 26 times.
184119 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1476 107518 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1477
1478 184119 get_limits(s, 16*mb_x, 16*mb_y, 1); //restore c->?min/max, maybe not needed
1479
1480 184119 mv_table[mot_xy][0]= mx;
1481 184119 mv_table[mot_xy][1]= my;
1482 184119 c->flags &= ~FLAG_DIRECT;
1483 184119 c->sub_flags &= ~FLAG_DIRECT;
1484
1485 184119 return dmin;
1486 }
1487
1488 402556 void ff_estimate_b_frame_motion(MPVEncContext *const s,
1489 int mb_x, int mb_y)
1490 {
1491 402556 MotionEstContext *const c = &s->me;
1492 int fmin, bmin, dmin, fbmin, bimin, fimin;
1493 402556 int type=0;
1494 402556 const int xy = mb_y*s->c.mb_stride + mb_x;
1495 402556 init_ref(c, s->new_pic->data, s->c.last_pic.data,
1496 402556 s->c.next_pic.data, 16 * mb_x, 16 * mb_y, 2);
1497
1498 402556 get_limits(s, 16*mb_x, 16*mb_y, 1);
1499
1500 402556 c->skip=0;
1501
1502
4/4
✓ Branch 0 taken 184204 times.
✓ Branch 1 taken 218352 times.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 184097 times.
402556 if (s->c.codec_id == AV_CODEC_ID_MPEG4 && s->c.next_pic.mbskip_table[xy]) {
1503 107 int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
1504
1505 107 score= ((unsigned)(score*score + 128*256))>>16;
1506 107 c->mc_mb_var_sum_temp += score;
1507 107 s->mc_mb_var[mb_y*s->c.mb_stride + mb_x] = score; //FIXME use SSE
1508 107 s->mb_type[mb_y*s->c.mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
1509
1510 107 return;
1511 }
1512
1513 402449 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1514 402449 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1515 402449 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1516
1517
2/2
✓ Branch 0 taken 184097 times.
✓ Branch 1 taken 218352 times.
402449 if (s->c.codec_id == AV_CODEC_ID_MPEG4)
1518 184097 dmin= direct_search(s, mb_x, mb_y);
1519 else
1520 218352 dmin= INT_MAX;
1521
1522 // FIXME penalty stuff for non-MPEG-4
1523 402449 c->skip=0;
1524 402449 fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +
1525 402449 3 * c->mb_penalty_factor;
1526
1527 402449 c->skip=0;
1528 402449 bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) +
1529 402449 2 * c->mb_penalty_factor;
1530 ff_dlog(c->avctx, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1531
1532 402449 c->skip=0;
1533 402449 fbmin= bidir_refine(s, mb_x, mb_y) + c->mb_penalty_factor;
1534 ff_dlog(c->avctx, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1535
1536
2/2
✓ Branch 0 taken 115200 times.
✓ Branch 1 taken 287249 times.
402449 if (c->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
1537 //FIXME mb type penalty
1538 115200 c->skip=0;
1539 115200 c->current_mv_penalty = c->mv_penalty[s->f_code] + MAX_DMV;
1540 115200 fimin= interlaced_search(s, 0,
1541 115200 s->b_field_mv_table[0], s->b_field_select_table[0],
1542 115200 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1543 115200 c->current_mv_penalty = c->mv_penalty[s->b_code] + MAX_DMV;
1544 115200 bimin= interlaced_search(s, 2,
1545 115200 s->b_field_mv_table[1], s->b_field_select_table[1],
1546 115200 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1547 }else
1548 287249 fimin= bimin= INT_MAX;
1549
1550 {
1551 402449 int score= fmin;
1552 402449 type = CANDIDATE_MB_TYPE_FORWARD;
1553
1554
2/2
✓ Branch 0 taken 54257 times.
✓ Branch 1 taken 348192 times.
402449 if (dmin <= score){
1555 54257 score = dmin;
1556 54257 type = CANDIDATE_MB_TYPE_DIRECT;
1557 }
1558
2/2
✓ Branch 0 taken 201978 times.
✓ Branch 1 taken 200471 times.
402449 if(bmin<score){
1559 201978 score=bmin;
1560 201978 type= CANDIDATE_MB_TYPE_BACKWARD;
1561 }
1562
2/2
✓ Branch 0 taken 235898 times.
✓ Branch 1 taken 166551 times.
402449 if(fbmin<score){
1563 235898 score=fbmin;
1564 235898 type= CANDIDATE_MB_TYPE_BIDIR;
1565 }
1566
2/2
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 398549 times.
402449 if(fimin<score){
1567 3900 score=fimin;
1568 3900 type= CANDIDATE_MB_TYPE_FORWARD_I;
1569 }
1570
2/2
✓ Branch 0 taken 4037 times.
✓ Branch 1 taken 398412 times.
402449 if(bimin<score){
1571 4037 score=bimin;
1572 4037 type= CANDIDATE_MB_TYPE_BACKWARD_I;
1573 }
1574
1575 402449 score= ((unsigned)(score*score + 128*256))>>16;
1576 402449 c->mc_mb_var_sum_temp += score;
1577 402449 s->mc_mb_var[mb_y*s->c.mb_stride + mb_x] = score; //FIXME use SSE
1578 }
1579
1580
2/2
✓ Branch 0 taken 222517 times.
✓ Branch 1 taken 179932 times.
402449 if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1581 222517 type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1582
2/2
✓ Branch 0 taken 31126 times.
✓ Branch 1 taken 191391 times.
222517 if(fimin < INT_MAX)
1583 31126 type |= CANDIDATE_MB_TYPE_FORWARD_I;
1584
2/2
✓ Branch 0 taken 31126 times.
✓ Branch 1 taken 191391 times.
222517 if(bimin < INT_MAX)
1585 31126 type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1586
4/4
✓ Branch 0 taken 31126 times.
✓ Branch 1 taken 191391 times.
✓ Branch 2 taken 26116 times.
✓ Branch 3 taken 5010 times.
222517 if(fimin < INT_MAX && bimin < INT_MAX){
1587 26116 type |= CANDIDATE_MB_TYPE_BIDIR_I;
1588 }
1589 //FIXME something smarter
1590
2/2
✓ Branch 0 taken 76784 times.
✓ Branch 1 taken 145733 times.
222517 if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
1591
4/4
✓ Branch 0 taken 145813 times.
✓ Branch 1 taken 76704 times.
✓ Branch 2 taken 145733 times.
✓ Branch 3 taken 80 times.
222517 if (s->c.codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT &&
1592
4/4
✓ Branch 0 taken 69205 times.
✓ Branch 1 taken 76528 times.
✓ Branch 2 taken 40248 times.
✓ Branch 3 taken 28957 times.
145733 s->mpv_flags & FF_MPV_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
1593 40248 type |= CANDIDATE_MB_TYPE_DIRECT0;
1594 }
1595
1596 402449 s->mb_type[mb_y*s->c.mb_stride + mb_x]= type;
1597 }
1598
1599 /* find best f_code for ME which do unlimited searches */
1600 13931 int ff_get_best_fcode(MPVMainEncContext *const m, const int16_t (*mv_table)[2], int type)
1601 {
1602 13931 MPVEncContext *const s = &m->s;
1603 13931 MotionEstContext *const c = &s->me;
1604
1605
1/2
✓ Branch 0 taken 13931 times.
✗ Branch 1 not taken.
13931 if (c->motion_est != FF_ME_ZERO) {
1606 int score[8];
1607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13931 times.
13931 int i, range = c->avctx->me_range ? c->avctx->me_range : (INT_MAX/2);
1608 13931 const uint8_t * fcode_tab = m->fcode_tab;
1609 13931 int best_fcode=-1;
1610 13931 int best_score=-10000000;
1611
1612
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 13189 times.
13931 if (s->c.msmpeg4_version != MSMP4_UNUSED)
1613 742 range= FFMIN(range, 16);
1614
2/2
✓ Branch 0 taken 6887 times.
✓ Branch 1 taken 6302 times.
13189 else if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1615
2/2
✓ Branch 0 taken 6279 times.
✓ Branch 1 taken 608 times.
6887 c->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
1616 6279 range= FFMIN(range, 256);
1617
1618
2/2
✓ Branch 0 taken 111448 times.
✓ Branch 1 taken 13931 times.
125379 for(i=0; i<8; i++) score[i]= s->c.mb_num*(8-i);
1619
1620
2/2
✓ Branch 0 taken 191620 times.
✓ Branch 1 taken 13931 times.
205551 for (int y = 0; y < s->c.mb_height; y++) {
1621 int x;
1622 191620 int xy= y*s->c.mb_stride;
1623
2/2
✓ Branch 0 taken 4208758 times.
✓ Branch 1 taken 191620 times.
4400378 for(x=0; x<s->c.mb_width; x++, xy++){
1624
2/2
✓ Branch 0 taken 3491251 times.
✓ Branch 1 taken 717507 times.
4208758 if(s->mb_type[xy] & type){
1625 3491251 int mx= mv_table[xy][0];
1626 3491251 int my= mv_table[xy][1];
1627 3491251 int fcode = FFMAX(fcode_tab[mx], fcode_tab[my]);
1628 int j;
1629
1630
6/6
✓ Branch 0 taken 3490290 times.
✓ Branch 1 taken 961 times.
✓ Branch 2 taken 3489490 times.
✓ Branch 3 taken 800 times.
✓ Branch 4 taken 3488831 times.
✓ Branch 5 taken 659 times.
3491251 if (mx >= range || mx < -range ||
1631
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 3488372 times.
3488831 my >= range || my < -range)
1632 2879 continue;
1633
1634
3/4
✓ Branch 0 taken 3704652 times.
✓ Branch 1 taken 3488372 times.
✓ Branch 2 taken 3704652 times.
✗ Branch 3 not taken.
7193024 for(j=0; j<fcode && j<8; j++){
1635
2/2
✓ Branch 0 taken 2471221 times.
✓ Branch 1 taken 1233431 times.
3704652 if (s->c.pict_type == AV_PICTURE_TYPE_B ||
1636
2/2
✓ Branch 0 taken 2358709 times.
✓ Branch 1 taken 112512 times.
2471221 s->mc_mb_var[xy] < s->mb_var[xy])
1637 3592140 score[j]-= 170;
1638 }
1639 }
1640 }
1641 }
1642
1643
2/2
✓ Branch 0 taken 97517 times.
✓ Branch 1 taken 13931 times.
111448 for(i=1; i<8; i++){
1644
2/2
✓ Branch 0 taken 18028 times.
✓ Branch 1 taken 79489 times.
97517 if(score[i] > best_score){
1645 18028 best_score= score[i];
1646 18028 best_fcode= i;
1647 }
1648 }
1649
1650 13931 return best_fcode;
1651 }else{
1652 ✗ return 1;
1653 }
1654 }
1655
1656 8415 void ff_fix_long_p_mvs(MPVEncContext *const s, int type)
1657 {
1658 8415 MotionEstContext *const c = &s->me;
1659 8415 const int f_code= s->f_code;
1660 int y, range;
1661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8415 times.
8415 av_assert0(s->c.pict_type == AV_PICTURE_TYPE_P);
1662
1663
4/4
✓ Branch 0 taken 3777 times.
✓ Branch 1 taken 4638 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 3035 times.
8415 range = (((s->c.out_format == FMT_MPEG1 || s->c.msmpeg4_version != MSMP4_UNUSED) ? 8 : 16) << f_code);
1664
1665
3/4
✓ Branch 0 taken 3917 times.
✓ Branch 1 taken 4498 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3917 times.
8415 av_assert0(range <= 16 || s->c.msmpeg4_version == MSMP4_UNUSED);
1666
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 8415 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8415 av_assert0(range <=256 || !(s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO && c->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
1667
1668
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8415 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8415 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1669
1670
2/2
✓ Branch 0 taken 1011 times.
✓ Branch 1 taken 7404 times.
8415 if (c->avctx->flags & AV_CODEC_FLAG_4MV) {
1671 1011 const int wrap= s->c.b8_stride;
1672
1673 /* clip / convert to intra 8x8 type MVs */
1674
2/2
✓ Branch 0 taken 14418 times.
✓ Branch 1 taken 1011 times.
15429 for(y=0; y<s->c.mb_height; y++){
1675 14418 int xy= y*2*wrap;
1676 14418 int i= y*s->c.mb_stride;
1677 int x;
1678
1679
2/2
✓ Branch 0 taken 302832 times.
✓ Branch 1 taken 14418 times.
317250 for(x=0; x<s->c.mb_width; x++){
1680
2/2
✓ Branch 0 taken 234680 times.
✓ Branch 1 taken 68152 times.
302832 if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1681 int block;
1682
2/2
✓ Branch 0 taken 938720 times.
✓ Branch 1 taken 234680 times.
1173400 for(block=0; block<4; block++){
1683 938720 int off= (block& 1) + (block>>1)*wrap;
1684 938720 int mx = s->c.cur_pic.motion_val[0][ xy + off ][0];
1685 938720 int my = s->c.cur_pic.motion_val[0][ xy + off ][1];
1686
1687
4/4
✓ Branch 0 taken 938101 times.
✓ Branch 1 taken 619 times.
✓ Branch 2 taken 937323 times.
✓ Branch 3 taken 778 times.
938720 if( mx >=range || mx <-range
1688
4/4
✓ Branch 0 taken 937123 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 366 times.
✓ Branch 3 taken 936757 times.
937323 || my >=range || my <-range){
1689 1963 s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1690 1963 s->mb_type[i] |= type;
1691 1963 s->c.cur_pic.mb_type[i] = type;
1692 }
1693 }
1694 }
1695 302832 xy+=2;
1696 302832 i++;
1697 }
1698 }
1699 }
1700 8415 }
1701
1702 /**
1703 * @param truncate 1 for truncation, 0 for using intra
1704 */
1705 18207 void ff_fix_long_mvs(MPVEncContext *const s, uint8_t *field_select_table, int field_select,
1706 int16_t (*mv_table)[2], int f_code, int type, int truncate)
1707 {
1708 18207 MotionEstContext *const c = &s->me;
1709 int y, h_range, v_range;
1710
1711 // RAL: 8 in MPEG-1, 16 in MPEG-4
1712
4/4
✓ Branch 0 taken 6185 times.
✓ Branch 1 taken 12022 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 5443 times.
18207 int range = (((s->c.out_format == FMT_MPEG1 || s->c.msmpeg4_version != MSMP4_UNUSED) ? 8 : 16) << f_code);
1713
1714
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18207 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18207 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1715
1716 18207 h_range= range;
1717
2/2
✓ Branch 0 taken 4400 times.
✓ Branch 1 taken 13807 times.
18207 v_range= field_select_table ? range>>1 : range;
1718
1719 /* clip / convert to intra 16x16 type MVs */
1720
2/2
✓ Branch 0 taken 249881 times.
✓ Branch 1 taken 18207 times.
268088 for(y=0; y<s->c.mb_height; y++){
1721 int x;
1722 249881 int xy= y*s->c.mb_stride;
1723
2/2
✓ Branch 0 taken 5393917 times.
✓ Branch 1 taken 249881 times.
5643798 for(x=0; x<s->c.mb_width; x++){
1724
2/2
✓ Branch 0 taken 3864228 times.
✓ Branch 1 taken 1529689 times.
5393917 if (s->mb_type[xy] & type){ // RAL: "type" test added...
1725
4/4
✓ Branch 0 taken 359878 times.
✓ Branch 1 taken 3504350 times.
✓ Branch 2 taken 180041 times.
✓ Branch 3 taken 179837 times.
3864228 if (!field_select_table || field_select_table[xy] == field_select) {
1726
4/4
✓ Branch 0 taken 3681861 times.
✓ Branch 1 taken 2530 times.
✓ Branch 2 taken 3679620 times.
✓ Branch 3 taken 2241 times.
3684391 if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
1727
4/4
✓ Branch 0 taken 3678070 times.
✓ Branch 1 taken 1550 times.
✓ Branch 2 taken 1221 times.
✓ Branch 3 taken 3676849 times.
3679620 || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
1728
1729
2/2
✓ Branch 0 taken 1496 times.
✓ Branch 1 taken 6046 times.
7542 if(truncate){
1730
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 1064 times.
1496 if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
1731
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 598 times.
1064 else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
1732
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 1159 times.
1496 if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
1733
2/2
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 865 times.
1159 else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
1734 }else{
1735 6046 s->mb_type[xy] &= ~type;
1736 6046 s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
1737 6046 mv_table[xy][0]=
1738 6046 mv_table[xy][1]= 0;
1739 }
1740 }
1741 }
1742 }
1743 5393917 xy++;
1744 }
1745 }
1746 18207 }
1747