FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/motion_est.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 869 1005 86.5%
Functions: 28 34 82.4%
Branches: 502 615 81.6%

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 6065463 static inline unsigned update_map_generation(MotionEstContext *c)
56 {
57 6065463 c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
58
2/2
✓ Branch 0 taken 5842 times.
✓ Branch 1 taken 6059621 times.
6065463 if(c->map_generation==0){
59 5842 c->map_generation= 1<<(ME_MAP_MV_BITS*2);
60 5842 memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
61 }
62 6065463 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 2553517 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 2553517 const int offset[3]= {
88 2553517 y*c-> stride + x,
89 2553517 ((y*c->uvstride + x)>>1),
90 2553517 ((y*c->uvstride + x)>>1),
91 };
92 int i;
93
2/2
✓ Branch 0 taken 7660551 times.
✓ Branch 1 taken 2553517 times.
10214068 for(i=0; i<3; i++){
94 7660551 c->src[0][i]= src [i] + offset[i];
95 7660551 c->ref[0][i]= ref [i] + offset[i];
96 }
97
2/2
✓ Branch 0 taken 396999 times.
✓ Branch 1 taken 2156518 times.
2553517 if(ref_index){
98
2/2
✓ Branch 0 taken 1190997 times.
✓ Branch 1 taken 396999 times.
1587996 for(i=0; i<3; i++){
99 1190997 c->ref[ref_index][i]= ref2[i] + offset[i];
100 }
101 }
102 2553517 }
103
104 657 static int get_flags(MotionEstContext *c, int direct, int chroma){
105 657 return ((c->avctx->flags&AV_CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 + (direct ? FLAG_DIRECT : 0)
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 + (chroma ? FLAG_CHROMA : 0);
108 }
109
110 2467569 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 2467569 MotionEstContext *const c = &s->me;
114 2467569 const int stride= c->stride;
115 2467569 const int hx = subx + x * (1 << (1 + qpel));
116 2467569 const int hy = suby + y * (1 << (1 + qpel));
117 2467569 const uint8_t * const * const ref = c->ref[ref_index];
118 2467569 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
4/8
✓ Branch 0 taken 2467569 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2467569 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2467569 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2467569 times.
✗ Branch 7 not taken.
4935138 if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
123 2467569 const int time_pp = s->c.pp_time;
124 2467569 const int time_pb = s->c.pb_time;
125 2467569 const int mask= 2*qpel+1;
126
2/2
✓ Branch 0 taken 634259 times.
✓ Branch 1 taken 1833310 times.
2467569 if (s->c.mv_type == MV_TYPE_8X8) {
127 int i;
128
2/2
✓ Branch 0 taken 2537036 times.
✓ Branch 1 taken 634259 times.
3171295 for(i=0; i<4; i++){
129 2537036 int fx = c->direct_basis_mv[i][0] + hx;
130 2537036 int fy = c->direct_basis_mv[i][1] + hy;
131
2/2
✓ Branch 0 taken 1580960 times.
✓ Branch 1 taken 956076 times.
2537036 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 1549612 times.
✓ Branch 1 taken 987424 times.
2537036 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 2537036 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
134 2537036 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
135
136 2537036 uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
137
2/2
✓ Branch 0 taken 961440 times.
✓ Branch 1 taken 1575596 times.
2537036 if(qpel){
138 961440 c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
139 961440 c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
140 }else{
141 1575596 c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
142 1575596 c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
143 }
144 }
145 }else{
146 1833310 int fx = c->direct_basis_mv[0][0] + hx;
147 1833310 int fy = c->direct_basis_mv[0][1] + hy;
148
2/2
✓ Branch 0 taken 1372847 times.
✓ Branch 1 taken 460463 times.
1833310 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 1341845 times.
✓ Branch 1 taken 491465 times.
1833310 int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
150 1833310 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
151 1833310 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
152
153
2/2
✓ Branch 0 taken 433681 times.
✓ Branch 1 taken 1399629 times.
1833310 if(qpel){
154 433681 c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
155 433681 c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
156 433681 c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
157 433681 c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
158 433681 c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
159 433681 c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
160 433681 c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
161 433681 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 1399629 c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
173 1399629 c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
174 }
175 }
176 2467569 d = cmp_func(s, c->temp, src[0], stride, 16);
177 }else
178 d= 256*256*256*32;
179 2467569 return d;
180 }
181
182 60378166 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 60378166 MotionEstContext *const c = &s->me;
186 60378166 const int stride= c->stride;
187 60378166 const int uvstride= c->uvstride;
188 60378166 const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
189 60378166 const int hx= subx + x*(1<<(1+qpel));
190 60378166 const int hy= suby + y*(1<<(1+qpel));
191 60378166 const uint8_t * const * const ref = c->ref[ref_index];
192 60378166 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 8100184 times.
✓ Branch 1 taken 52277982 times.
60378166 if(dxy){
197
2/2
✓ Branch 0 taken 6065785 times.
✓ Branch 1 taken 2034399 times.
8100184 if(qpel){
198
1/2
✓ Branch 0 taken 6065785 times.
✗ Branch 1 not taken.
6065785 if (h << size == 16) {
199 6065785 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 6065785 times.
6065785 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 2034399 c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2034399 times.
2034399 if(chroma)
216 uvdxy= dxy | (x&1) | (2*(y&1));
217 }
218 8100184 d = cmp_func(s, c->temp, src[0], stride, h);
219 }else{
220 52277982 d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52277982 times.
52277982 if(chroma)
222 uvdxy= (x&1) + 2*(y&1);
223 }
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60378166 times.
60378166 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 60378166 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 54545715 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 1639753 times.
✓ Branch 1 taken 52905962 times.
54545715 if(flags&FLAG_DIRECT){
254 1639753 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 52905962 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 54545715 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 54545715 return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
275 }
276 }
277
278 2421988 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 554408 times.
✓ Branch 1 taken 1867580 times.
2421988 if(flags&FLAG_DIRECT){
282 554408 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
283 }else{
284 1867580 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 5878032 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 273408 times.
✓ Branch 1 taken 5604624 times.
5878032 if(flags&FLAG_DIRECT){
292 273408 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
293 }else{
294 5604624 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 219 av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx,
310 const MECmpContext *mecc, int mpvenc)
311 {
312 219 int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
313 219 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 219 times.
219 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 219 c->avctx = avctx;
322
323
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 213 times.
219 if (avctx->codec_id == AV_CODEC_ID_H261)
324 6 avctx->me_sub_cmp = avctx->me_cmp;
325
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 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 219 ret = ff_set_cmp(mecc, c->me_pre_cmp, avctx->me_pre_cmp, mpvenc);
330 219 ret |= ff_set_cmp(mecc, c->me_cmp, avctx->me_cmp, mpvenc);
331 219 ret |= ff_set_cmp(mecc, c->me_sub_cmp, avctx->me_sub_cmp, mpvenc);
332 219 ret |= ff_set_cmp(mecc, c->mb_cmp, avctx->mb_cmp, mpvenc);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if (ret < 0)
334 return ret;
335
336 219 c->sse = mecc->sse[0];
337 219 memcpy(c->pix_abs, mecc->pix_abs, sizeof(c->pix_abs));
338
339 219 c->flags = get_flags(c, 0, avctx->me_cmp & FF_CMP_CHROMA);
340 219 c->sub_flags = get_flags(c, 0, avctx->me_sub_cmp & FF_CMP_CHROMA);
341 219 c->mb_flags = get_flags(c, 0, avctx->mb_cmp & FF_CMP_CHROMA);
342
343
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 213 times.
219 if (avctx->codec_id == AV_CODEC_ID_H261) {
344 6 c->sub_motion_search = no_sub_motion_search;
345
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 201 times.
213 } else if (avctx->flags & AV_CODEC_FLAG_QPEL) {
346 12 c->sub_motion_search= qpel_motion_search;
347 }else{
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
349 c->sub_motion_search= hpel_motion_search;
350
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 19 times.
201 else if( c->avctx->me_sub_cmp == FF_CMP_SAD
351
1/2
✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
182 && c->avctx-> me_cmp == FF_CMP_SAD
352
1/2
✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
182 && c->avctx-> mb_cmp == FF_CMP_SAD)
353 182 c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
354 else
355 19 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 208 times.
✓ Branch 1 taken 11 times.
219 if (avctx->codec_id != AV_CODEC_ID_SNOW) {
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 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 208 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
208 if ((avctx->me_sub_cmp & FF_CMP_CHROMA) && !c->me_sub_cmp[2])
365 c->me_sub_cmp[2] = zero_cmp;
366 }
367
368 219 return 0;
369 }
370
371 13803 void ff_me_init_pic(MPVEncContext *const s)
372 {
373 13803 MotionEstContext *const c = &s->me;
374
375 /*FIXME s->c.no_rounding b_type*/
376
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 13275 times.
13803 if (c->avctx->flags & AV_CODEC_FLAG_QPEL) {
377 528 c->qpel_avg = s->c.qdsp.avg_qpel_pixels_tab;
378
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 429 times.
528 if (s->c.no_rounding)
379 99 c->qpel_put = s->c.qdsp.put_no_rnd_qpel_pixels_tab;
380 else
381 429 c->qpel_put = s->c.qdsp.put_qpel_pixels_tab;
382 }
383 13803 c->hpel_avg = s->c.hdsp.avg_pixels_tab;
384
2/2
✓ Branch 0 taken 1806 times.
✓ Branch 1 taken 11997 times.
13803 if (s->c.no_rounding)
385 1806 c->hpel_put = s->c.hdsp.put_no_rnd_pixels_tab;
386 else
387 11997 c->hpel_put = s->c.hdsp.put_pixels_tab;
388
389
1/2
✓ Branch 0 taken 13803 times.
✗ Branch 1 not taken.
13803 if (s->c.linesize) {
390 13803 c->stride = s->c.linesize;
391 13803 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 13803 times.
✗ Branch 1 not taken.
13803 if (s->c.codec_id != AV_CODEC_ID_SNOW) {
397 13803 c->hpel_put[2][0]= c->hpel_put[2][1]=
398 13803 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
399 }
400 /* Reset the average MB variance and scene change stats */
401 13803 c->scene_change_score = 0;
402 13803 c->mb_var_sum_temp =
403 13803 c->mc_mb_var_sum_temp = 0;
404 13803 }
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 4595765 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 4595765 MotionEstContext *const c = &s->me;
419 4595765 const int penalty_factor= c->sub_penalty_factor;
420 int mx, my, dminh;
421 const uint8_t *pix, *ptr;
422 4595765 int stride= c->stride;
423 4595765 LOAD_COMMON
424
425 av_assert2(c->sub_flags == 0);
426
427
2/2
✓ Branch 0 taken 117086 times.
✓ Branch 1 taken 4478679 times.
4595765 if(c->skip){
428 117086 *mx_ptr = 0;
429 117086 *my_ptr = 0;
430 117086 return dmin;
431 }
432
433 4478679 pix = c->src[src_index][0];
434
435 4478679 mx = *mx_ptr;
436 4478679 my = *my_ptr;
437 4478679 ptr = c->ref[ref_index][0] + (my * stride) + mx;
438
439 4478679 dminh = dmin;
440
441
6/6
✓ Branch 0 taken 4403142 times.
✓ Branch 1 taken 75537 times.
✓ Branch 2 taken 4333298 times.
✓ Branch 3 taken 69844 times.
✓ Branch 4 taken 4242776 times.
✓ Branch 5 taken 90522 times.
4478679 if (mx > xmin && mx < xmax &&
442
2/2
✓ Branch 0 taken 4159879 times.
✓ Branch 1 taken 82897 times.
4242776 my > ymin && my < ymax) {
443 4159879 int dx=0, dy=0;
444 int d, pen_x, pen_y;
445 4159879 const int index= my*(1<<ME_MAP_SHIFT) + mx;
446 4159879 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
447 4159879 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
448 4159879 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
449 4159879 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
450 4159879 mx += mx;
451 4159879 my += my;
452
453
454 4159879 pen_x= pred_x + mx;
455 4159879 pen_y= pred_y + my;
456
457 4159879 ptr-= stride;
458
2/2
✓ Branch 0 taken 2113074 times.
✓ Branch 1 taken 2046805 times.
4159879 if(t<=b){
459 2113074 CHECK_SAD_HALF_MV(y2 , 0, -1)
460
2/2
✓ Branch 0 taken 1315848 times.
✓ Branch 1 taken 797226 times.
2113074 if(l<=r){
461 1315848 CHECK_SAD_HALF_MV(xy2, -1, -1)
462
2/2
✓ Branch 0 taken 768847 times.
✓ Branch 1 taken 547001 times.
1315848 if(t+r<=b+l){
463 768847 CHECK_SAD_HALF_MV(xy2, +1, -1)
464 768847 ptr+= stride;
465 }else{
466 547001 ptr+= stride;
467 547001 CHECK_SAD_HALF_MV(xy2, -1, +1)
468 }
469 1315848 CHECK_SAD_HALF_MV(x2 , -1, 0)
470 }else{
471 797226 CHECK_SAD_HALF_MV(xy2, +1, -1)
472
2/2
✓ Branch 0 taken 439239 times.
✓ Branch 1 taken 357987 times.
797226 if(t+l<=b+r){
473 439239 CHECK_SAD_HALF_MV(xy2, -1, -1)
474 439239 ptr+= stride;
475 }else{
476 357987 ptr+= stride;
477 357987 CHECK_SAD_HALF_MV(xy2, +1, +1)
478 }
479 797226 CHECK_SAD_HALF_MV(x2 , +1, 0)
480 }
481 }else{
482
2/2
✓ Branch 0 taken 780734 times.
✓ Branch 1 taken 1266071 times.
2046805 if(l<=r){
483
2/2
✓ Branch 0 taken 329989 times.
✓ Branch 1 taken 450745 times.
780734 if(t+l<=b+r){
484 329989 CHECK_SAD_HALF_MV(xy2, -1, -1)
485 329989 ptr+= stride;
486 }else{
487 450745 ptr+= stride;
488 450745 CHECK_SAD_HALF_MV(xy2, +1, +1)
489 }
490 780734 CHECK_SAD_HALF_MV(x2 , -1, 0)
491 780734 CHECK_SAD_HALF_MV(xy2, -1, +1)
492 }else{
493
2/2
✓ Branch 0 taken 561007 times.
✓ Branch 1 taken 705064 times.
1266071 if(t+r<=b+l){
494 561007 CHECK_SAD_HALF_MV(xy2, +1, -1)
495 561007 ptr+= stride;
496 }else{
497 705064 ptr+= stride;
498 705064 CHECK_SAD_HALF_MV(xy2, -1, +1)
499 }
500 1266071 CHECK_SAD_HALF_MV(x2 , +1, 0)
501 1266071 CHECK_SAD_HALF_MV(xy2, +1, +1)
502 }
503 2046805 CHECK_SAD_HALF_MV(y2 , 0, +1)
504 }
505 4159879 mx+=dx;
506 4159879 my+=dy;
507
508 }else{
509 318800 mx += mx;
510 318800 my += my;
511 }
512
513 4478679 *mx_ptr = mx;
514 4478679 *my_ptr = my;
515 4478679 return dminh;
516 }
517
518 2156518 static inline void set_p_mv_tables(MPVEncContext *const s, int mx, int my, int mv4)
519 {
520 2156518 const int xy = s->c.mb_x + s->c.mb_y * s->c.mb_stride;
521
522 2156518 s->p_mv_table[xy][0] = mx;
523 2156518 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 1893456 times.
✓ Branch 1 taken 263062 times.
2156518 if(mv4){
527 1893456 int mot_xy = s->c.block_index[0];
528
529 1893456 s->c.cur_pic.motion_val[0][mot_xy ][0] = mx;
530 1893456 s->c.cur_pic.motion_val[0][mot_xy ][1] = my;
531 1893456 s->c.cur_pic.motion_val[0][mot_xy + 1][0] = mx;
532 1893456 s->c.cur_pic.motion_val[0][mot_xy + 1][1] = my;
533
534 1893456 mot_xy += s->c.b8_stride;
535 1893456 s->c.cur_pic.motion_val[0][mot_xy ][0] = mx;
536 1893456 s->c.cur_pic.motion_val[0][mot_xy ][1] = my;
537 1893456 s->c.cur_pic.motion_val[0][mot_xy + 1][0] = mx;
538 1893456 s->c.cur_pic.motion_val[0][mot_xy + 1][1] = my;
539 }
540 2156518 }
541
542 /**
543 * get fullpel ME search limits.
544 */
545 3527462 static inline void get_limits(MPVEncContext *const s, int x, int y, int bframe)
546 {
547 3527462 MotionEstContext *const c = &s->me;
548
2/2
✓ Branch 0 taken 168764 times.
✓ Branch 1 taken 3358698 times.
3527462 int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
549
2/2
✓ Branch 0 taken 168764 times.
✓ Branch 1 taken 3358698 times.
3527462 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 1716832 times.
✓ Branch 1 taken 1810630 times.
3527462 if (s->c.unrestricted_mv) {
555 1716832 c->xmin = - x - 16;
556 1716832 c->ymin = - y - 16;
557 1716832 c->xmax = - x + s->c.width;
558 1716832 c->ymax = - y + s->c.height;
559
2/2
✓ Branch 0 taken 106920 times.
✓ Branch 1 taken 1703710 times.
1810630 } 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 102060 times.
✓ Branch 1 taken 4860 times.
106920 c->xmin = (x > 15) ? - 15 : 0;
562
2/2
✓ Branch 0 taken 100980 times.
✓ Branch 1 taken 5940 times.
106920 c->ymin = (y > 15) ? - 15 : 0;
563
2/2
✓ Branch 0 taken 102060 times.
✓ Branch 1 taken 4860 times.
106920 c->xmax = (x < s->c.mb_width * 16 - 16) ? 15 : 0;
564
2/2
✓ Branch 0 taken 100980 times.
✓ Branch 1 taken 5940 times.
106920 c->ymax = (y < s->c.mb_height * 16 - 16) ? 15 : 0;
565 } else {
566 1703710 c->xmin = - x;
567 1703710 c->ymin = - y;
568 1703710 c->xmax = - x + s->c.mb_width *16 - 16;
569 1703710 c->ymax = - y + s->c.mb_height*16 - 16;
570 }
571
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3527462 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3527462 if(!range || range > max_range)
572 3527462 range = max_range;
573
1/2
✓ Branch 0 taken 3527462 times.
✗ Branch 1 not taken.
3527462 if(range){
574 3527462 c->xmin = FFMAX(c->xmin,-range);
575 3527462 c->xmax = FFMIN(c->xmax, range);
576 3527462 c->ymin = FFMAX(c->ymin,-range);
577 3527462 c->ymax = FFMIN(c->ymax, range);
578 }
579 3527462 }
580
581 263062 static inline void init_mv4_ref(MotionEstContext *c){
582 263062 const int stride= c->stride;
583
584 263062 c->ref[1][0] = c->ref[0][0] + 8;
585 263062 c->ref[2][0] = c->ref[0][0] + 8*stride;
586 263062 c->ref[3][0] = c->ref[2][0] + 8;
587 263062 c->src[1][0] = c->src[0][0] + 8;
588 263062 c->src[2][0] = c->src[0][0] + 8*stride;
589 263062 c->src[3][0] = c->src[2][0] + 8;
590 263062 }
591
592 263062 static inline int h263_mv4_search(MPVEncContext *const s, int mx, int my, int shift)
593 {
594 263062 MotionEstContext *const c = &s->me;
595 263062 const int size= 1;
596 263062 const int h=8;
597 int block;
598 int P[10][2];
599 263062 int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
600 263062 int same=1;
601 263062 const int stride= c->stride;
602 263062 const uint8_t *mv_penalty = c->current_mv_penalty;
603
4/6
✓ Branch 0 taken 263062 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2278 times.
✓ Branch 3 taken 260784 times.
✓ Branch 4 taken 2278 times.
✗ Branch 5 not taken.
263062 int safety_clipping = s->c.unrestricted_mv && (s->c.width&15) && (s->c.height&15);
604
605 263062 init_mv4_ref(c);
606
607
2/2
✓ Branch 0 taken 1052248 times.
✓ Branch 1 taken 263062 times.
1315310 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 1052248 const int mot_stride = s->c.b8_stride;
613 1052248 const int mot_xy = s->c.block_index[block];
614
615
2/2
✓ Branch 0 taken 9112 times.
✓ Branch 1 taken 1043136 times.
1052248 if(safety_clipping){
616 9112 c->xmax = - 16*s->c.mb_x + s->c.width - 8*(block &1);
617 9112 c->ymax = - 16*s->c.mb_y + s->c.height - 8*(block>>1);
618 }
619
620 1052248 P_LEFT[0] = s->c.cur_pic.motion_val[0][mot_xy - 1][0];
621 1052248 P_LEFT[1] = s->c.cur_pic.motion_val[0][mot_xy - 1][1];
622
623
2/2
✓ Branch 0 taken 1606 times.
✓ Branch 1 taken 1050642 times.
1052248 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 65096 times.
✓ Branch 1 taken 987152 times.
✓ Branch 2 taken 32548 times.
✓ Branch 3 taken 32548 times.
1052248 if (s->c.first_slice_line && block < 2) {
627 32548 c->pred_x= pred_x4= P_LEFT[0];
628 32548 c->pred_y= pred_y4= P_LEFT[1];
629 } else {
630 1019700 P_TOP[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][0];
631 1019700 P_TOP[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][1];
632 1019700 P_TOPRIGHT[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + off[block]][0];
633 1019700 P_TOPRIGHT[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + off[block]][1];
634
2/2
✓ Branch 0 taken 1592 times.
✓ Branch 1 taken 1018108 times.
1019700 if (P_TOP[1] > c->ymax * (1 << shift)) P_TOP[1] = c->ymax * (1 << shift);
635
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1019676 times.
1019700 if (P_TOPRIGHT[0] < c->xmin * (1 << shift)) P_TOPRIGHT[0] = c->xmin * (1 << shift);
636
2/2
✓ Branch 0 taken 1057 times.
✓ Branch 1 taken 1018643 times.
1019700 if (P_TOPRIGHT[0] > c->xmax * (1 << shift)) P_TOPRIGHT[0] = c->xmax * (1 << shift);
637
2/2
✓ Branch 0 taken 1526 times.
✓ Branch 1 taken 1018174 times.
1019700 if (P_TOPRIGHT[1] > c->ymax * (1 << shift)) P_TOPRIGHT[1] = c->ymax * (1 << shift);
638
639 1019700 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
640 1019700 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
641
642 1019700 c->pred_x= pred_x4 = P_MEDIAN[0];
643 1019700 c->pred_y= pred_y4 = P_MEDIAN[1];
644 }
645 1052248 P_MV1[0]= mx;
646 1052248 P_MV1[1]= my;
647
2/2
✓ Branch 0 taken 9112 times.
✓ Branch 1 taken 1043136 times.
1052248 if(safety_clipping)
648
2/2
✓ Branch 0 taken 82008 times.
✓ Branch 1 taken 9112 times.
91120 for(i=1; i<10; i++){
649
8/8
✓ Branch 0 taken 28944 times.
✓ Branch 1 taken 53064 times.
✓ Branch 2 taken 14472 times.
✓ Branch 3 taken 14472 times.
✓ Branch 4 taken 12864 times.
✓ Branch 5 taken 1608 times.
✓ Branch 6 taken 11256 times.
✓ Branch 7 taken 1608 times.
82008 if (s->c.first_slice_line && block < 2 && i > 1 && i < 9)
650 11256 continue;
651
4/4
✓ Branch 0 taken 39128 times.
✓ Branch 1 taken 31624 times.
✓ Branch 2 taken 30016 times.
✓ Branch 3 taken 9112 times.
70752 if (i>4 && i<9)
652 30016 continue;
653
2/2
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 39461 times.
40736 if (P[i][0] > c->xmax * (1 << shift)) P[i][0] = c->xmax * (1 << shift);
654
2/2
✓ Branch 0 taken 1597 times.
✓ Branch 1 taken 39139 times.
40736 if (P[i][1] > c->ymax * (1 << shift)) P[i][1] = c->ymax * (1 <<shift );
655 }
656
657 1052248 dmin4 = epzs_motion_search2(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift, 1);
658
659 1052248 dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
660
661
2/2
✓ Branch 0 taken 212708 times.
✓ Branch 1 taken 839540 times.
1052248 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
662 int dxy;
663 212708 const int offset= ((block&1) + (block>>1)*stride)*8;
664 212708 uint8_t *dest_y = c->scratchpad + offset;
665
2/2
✓ Branch 0 taken 56744 times.
✓ Branch 1 taken 155964 times.
212708 if (s->c.quarter_sample) {
666 56744 const uint8_t *ref = c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
667 56744 dxy = ((my4 & 3) << 2) | (mx4 & 3);
668
669 56744 c->qpel_put[1][dxy](dest_y, ref, stride);
670 }else{
671 155964 const uint8_t *ref = c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
672 155964 dxy = ((my4 & 1) << 1) | (mx4 & 1);
673
674 155964 c->hpel_put[1][dxy](dest_y, ref, stride, h);
675 }
676 212708 dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
677 }else
678 839540 dmin_sum+= dmin4;
679
680
2/2
✓ Branch 0 taken 56744 times.
✓ Branch 1 taken 995504 times.
1052248 if (s->c.quarter_sample) {
681 56744 mx4_sum+= mx4/2;
682 56744 my4_sum+= my4/2;
683 }else{
684 995504 mx4_sum+= mx4;
685 995504 my4_sum+= my4;
686 }
687
688 1052248 s->c.cur_pic.motion_val[0][s->c.block_index[block]][0] = mx4;
689 1052248 s->c.cur_pic.motion_val[0][s->c.block_index[block]][1] = my4;
690
691
4/4
✓ Branch 0 taken 553402 times.
✓ Branch 1 taken 498846 times.
✓ Branch 2 taken 133393 times.
✓ Branch 3 taken 420009 times.
1052248 if(mx4 != mx || my4 != my) same=0;
692 }
693
694
2/2
✓ Branch 0 taken 41942 times.
✓ Branch 1 taken 221120 times.
263062 if(same)
695 41942 return INT_MAX;
696
697
2/2
✓ Branch 0 taken 52519 times.
✓ Branch 1 taken 168601 times.
221120 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
698 52519 dmin_sum += c->mb_cmp[0](s,
699 52519 s->new_pic->data[0] +
700 52519 s->c.mb_x * 16 + s->c.mb_y * 16 * stride,
701 52519 c->scratchpad, stride, 16);
702 }
703
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221120 times.
221120 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
705 int dxy;
706 int mx, my;
707 int offset;
708
709 mx= ff_h263_round_chroma(mx4_sum);
710 my= ff_h263_round_chroma(my4_sum);
711 dxy = ((my & 1) << 1) | (mx & 1);
712
713 offset = (s->c.mb_x*8 + (mx>>1)) + (s->c.mb_y*8 + (my>>1))*s->c.uvlinesize;
714
715 c->hpel_put[1][dxy](c->scratchpad , s->c.last_pic.data[1] + offset, s->c.uvlinesize, 8);
716 c->hpel_put[1][dxy](c->scratchpad + 8, s->c.last_pic.data[2] + offset, s->c.uvlinesize, 8);
717
718 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);
719 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);
720 }
721
722 221120 c->pred_x= mx;
723 221120 c->pred_y= my;
724
725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221120 times.
221120 switch(c->avctx->mb_cmp&0xFF){
726 /*case FF_CMP_SSE:
727 return dmin_sum+ 32*s->c.qscale*s->c.qscale;*/
728 case FF_CMP_RD:
729 return dmin_sum;
730 221120 default:
731 221120 return dmin_sum+ 11*c->mb_penalty_factor;
732 }
733 }
734
735 331233 static inline void init_interlaced_ref(MPVEncContext *const s, int ref_index)
736 {
737 331233 MotionEstContext *const c = &s->me;
738
739 331233 c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->c.linesize;
740 331233 c->src[1][0] = c->src[0][0] + s->c.linesize;
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 331233 times.
331233 if(c->flags & FLAG_CHROMA){
742 c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->c.uvlinesize;
743 c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->c.uvlinesize;
744 c->src[1][1] = c->src[0][1] + s->c.uvlinesize;
745 c->src[1][2] = c->src[0][2] + s->c.uvlinesize;
746 }
747 331233 }
748
749 331233 static int interlaced_search(MPVEncContext *const s, int ref_index,
750 int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
751 {
752 331233 MotionEstContext *const c = &s->me;
753 331233 const int size=0;
754 331233 const int h=8;
755 int block;
756 int P[10][2];
757 331233 const uint8_t * const mv_penalty = c->current_mv_penalty;
758 331233 int same=1;
759 331233 const int stride = 2*s->c.linesize;
760 331233 int dmin_sum= 0;
761 331233 const int mot_stride = s->c.mb_stride;
762 331233 const int xy = s->c.mb_x + s->c.mb_y*mot_stride;
763
764 331233 c->ymin>>=1;
765 331233 c->ymax>>=1;
766 331233 c->stride<<=1;
767 331233 c->uvstride<<=1;
768 331233 init_interlaced_ref(s, ref_index);
769
770
2/2
✓ Branch 0 taken 662466 times.
✓ Branch 1 taken 331233 times.
993699 for(block=0; block<2; block++){
771 int field_select;
772 662466 int best_dmin= INT_MAX;
773 662466 int best_field= -1;
774
775
2/2
✓ Branch 0 taken 1324932 times.
✓ Branch 1 taken 662466 times.
1987398 for(field_select=0; field_select<2; field_select++){
776 int dmin, mx_i, my_i;
777 1324932 int16_t (*mv_table)[2]= mv_tables[block][field_select];
778
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if(user_field_select){
780 av_assert1(field_select==0 || field_select==1);
781 av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
782 if(field_select_tables[block][xy] != field_select)
783 continue;
784 }
785
786 1324932 P_LEFT[0] = mv_table[xy - 1][0];
787 1324932 P_LEFT[1] = mv_table[xy - 1][1];
788
2/2
✓ Branch 0 taken 32472 times.
✓ Branch 1 taken 1292460 times.
1324932 if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
789
790 1324932 c->pred_x= P_LEFT[0];
791 1324932 c->pred_y= P_LEFT[1];
792
793
2/2
✓ Branch 0 taken 1206192 times.
✓ Branch 1 taken 118740 times.
1324932 if (!s->c.first_slice_line) {
794 1206192 P_TOP[0] = mv_table[xy - mot_stride][0];
795 1206192 P_TOP[1] = mv_table[xy - mot_stride][1];
796 1206192 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
797 1206192 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
798
2/2
✓ Branch 0 taken 32877 times.
✓ Branch 1 taken 1173315 times.
1206192 if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
799
2/2
✓ Branch 0 taken 26013 times.
✓ Branch 1 taken 1180179 times.
1206192 if (P_TOPRIGHT[0] < c->xmin * (1 << 1)) P_TOPRIGHT[0] = c->xmin * (1 << 1);
800
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);
801
2/2
✓ Branch 0 taken 30765 times.
✓ Branch 1 taken 1175427 times.
1206192 if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
802
803 1206192 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
804 1206192 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
805 }
806 1324932 P_MV1[0]= mx; //FIXME not correct if block != field_select
807 1324932 P_MV1[1]= my / 2;
808
809 1324932 dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1, 0);
810
811 1324932 dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
812
813 1324932 mv_table[xy][0]= mx_i;
814 1324932 mv_table[xy][1]= my_i;
815
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
817 int dxy;
818
819 //FIXME chroma ME
820 const uint8_t *ref = c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
821 dxy = ((my_i & 1) << 1) | (mx_i & 1);
822
823 c->hpel_put[size][dxy](c->scratchpad, ref, stride, h);
824 dmin = c->mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
825 dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
826 }else
827 1324932 dmin+= c->mb_penalty_factor; //field_select bits
828
829 1324932 dmin += field_select != block; //slightly prefer same field
830
831
2/2
✓ Branch 0 taken 994745 times.
✓ Branch 1 taken 330187 times.
1324932 if(dmin < best_dmin){
832 994745 best_dmin= dmin;
833 994745 best_field= field_select;
834 }
835 }
836 {
837 662466 int16_t (*mv_table)[2]= mv_tables[block][best_field];
838
839
2/2
✓ Branch 0 taken 257294 times.
✓ Branch 1 taken 405172 times.
662466 if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
840
2/2
✓ Branch 0 taken 59142 times.
✓ Branch 1 taken 603324 times.
662466 if(mv_table[xy][1]&1) same=0;
841
2/2
✓ Branch 0 taken 444919 times.
✓ Branch 1 taken 217547 times.
662466 if(mv_table[xy][1]*2 != my) same=0;
842
2/2
✓ Branch 0 taken 318666 times.
✓ Branch 1 taken 343800 times.
662466 if(best_field != block) same=0;
843 }
844
845 662466 field_select_tables[block][xy]= best_field;
846 662466 dmin_sum += best_dmin;
847 }
848
849 331233 c->ymin *= 2;
850 331233 c->ymax<<=1;
851 331233 c->stride>>=1;
852 331233 c->uvstride>>=1;
853
854
2/2
✓ Branch 0 taken 55300 times.
✓ Branch 1 taken 275933 times.
331233 if(same)
855 55300 return INT_MAX;
856
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275933 times.
275933 switch(c->avctx->mb_cmp&0xFF){
858 /*case FF_CMP_SSE:
859 return dmin_sum+ 32*s->c.qscale*s->c.qscale;*/
860 case FF_CMP_RD:
861 return dmin_sum;
862 275933 default:
863 275933 return dmin_sum+ 11*c->mb_penalty_factor;
864 }
865 }
866
867 7660281 static inline int get_penalty_factor(int lambda, int lambda2, int type){
868
3/7
✓ Branch 0 taken 7067782 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 323169 times.
✓ Branch 5 taken 269330 times.
✗ Branch 6 not taken.
7660281 switch(type&0xFF){
869 7067782 default:
870 case FF_CMP_SAD:
871 7067782 return lambda>>FF_LAMBDA_SHIFT;
872 case FF_CMP_DCT:
873 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
874 case FF_CMP_W53:
875 return (4*lambda)>>(FF_LAMBDA_SHIFT);
876 case FF_CMP_W97:
877 return (2*lambda)>>(FF_LAMBDA_SHIFT);
878 323169 case FF_CMP_SATD:
879 case FF_CMP_DCT264:
880 323169 return (2*lambda)>>FF_LAMBDA_SHIFT;
881 269330 case FF_CMP_RD:
882 case FF_CMP_PSNR:
883 case FF_CMP_SSE:
884 case FF_CMP_NSSE:
885 269330 return lambda2>>FF_LAMBDA_SHIFT;
886 case FF_CMP_BIT:
887 case FF_CMP_MEDIAN_SAD:
888 return 1;
889 }
890 }
891
892 2156518 void ff_estimate_p_frame_motion(MPVEncContext *const s,
893 int mb_x, int mb_y)
894 {
895 2156518 MotionEstContext *const c = &s->me;
896 const uint8_t *pix, *ppix;
897 2156518 int sum, mx = 0, my = 0, dmin = 0;
898 int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
899 int vard; ///< sum of squared differences with the estimated motion vector
900 int P[10][2];
901 2156518 const int shift = 1 + s->c.quarter_sample;
902 2156518 int mb_type=0;
903
904 2156518 init_ref(c, s->new_pic->data, s->c.last_pic.data, NULL, 16*mb_x, 16*mb_y, 0);
905
906
3/4
✓ Branch 0 taken 15570 times.
✓ Branch 1 taken 2140948 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15570 times.
2156518 av_assert0(s->c.quarter_sample == 0 || s->c.quarter_sample == 1);
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2156518 times.
2156518 av_assert0(s->c.linesize == c->stride);
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2156518 times.
2156518 av_assert0(s->c.uvlinesize == c->uvstride);
909
910 2156518 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
911 2156518 c->sub_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
912 2156518 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
913 2156518 c->current_mv_penalty = c->mv_penalty[s->c.f_code] + MAX_DMV;
914
915 2156518 get_limits(s, 16*mb_x, 16*mb_y, 0);
916 2156518 c->skip=0;
917
918 /* intra / predictive decision */
919 2156518 pix = c->src[0][0];
920 2156518 sum = s->mpvencdsp.pix_sum(pix, s->c.linesize);
921 2156518 varc = s->mpvencdsp.pix_norm1(pix, s->c.linesize) -
922 2156518 (((unsigned) sum * sum) >> 8) + 500;
923
924 2156518 s->mb_mean[s->c.mb_stride * mb_y + mb_x] = (sum + 128) >> 8;
925 2156518 s->mb_var [s->c.mb_stride * mb_y + mb_x] = (varc + 128) >> 8;
926 2156518 c->mb_var_sum_temp += (varc+128)>>8;
927
928
1/2
✓ Branch 0 taken 2156518 times.
✗ Branch 1 not taken.
2156518 if (c->motion_est != FF_ME_ZERO) {
929 2156518 const int mot_stride = s->c.b8_stride;
930 2156518 const int mot_xy = s->c.block_index[0];
931
932 2156518 P_LEFT[0] = s->c.cur_pic.motion_val[0][mot_xy - 1][0];
933 2156518 P_LEFT[1] = s->c.cur_pic.motion_val[0][mot_xy - 1][1];
934
935
2/2
✓ Branch 0 taken 21308 times.
✓ Branch 1 taken 2135210 times.
2156518 if (P_LEFT[0] > (c->xmax << shift))
936 21308 P_LEFT[0] = c->xmax << shift;
937
938
2/2
✓ Branch 0 taken 1993405 times.
✓ Branch 1 taken 163113 times.
2156518 if (!s->c.first_slice_line) {
939 1993405 P_TOP[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][0];
940 1993405 P_TOP[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride ][1];
941 1993405 P_TOPRIGHT[0] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + 2][0];
942 1993405 P_TOPRIGHT[1] = s->c.cur_pic.motion_val[0][mot_xy - mot_stride + 2][1];
943
2/2
✓ Branch 0 taken 23782 times.
✓ Branch 1 taken 1969623 times.
1993405 if (P_TOP[1] > (c->ymax << shift))
944 23782 P_TOP[1] = c->ymax << shift;
945
2/2
✓ Branch 0 taken 22737 times.
✓ Branch 1 taken 1970668 times.
1993405 if (P_TOPRIGHT[0] < (c->xmin * (1 << shift)))
946 22737 P_TOPRIGHT[0] = c->xmin * (1 << shift);
947
2/2
✓ Branch 0 taken 21956 times.
✓ Branch 1 taken 1971449 times.
1993405 if (P_TOPRIGHT[1] > (c->ymax * (1 << shift)))
948 21956 P_TOPRIGHT[1] = c->ymax * (1 << shift);
949
950 1993405 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
951 1993405 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
952
953
2/2
✓ Branch 0 taken 1147947 times.
✓ Branch 1 taken 845458 times.
1993405 if (s->c.out_format == FMT_H263) {
954 1147947 c->pred_x = P_MEDIAN[0];
955 1147947 c->pred_y = P_MEDIAN[1];
956 } else { /* MPEG-1 at least */
957 845458 c->pred_x = P_LEFT[0];
958 845458 c->pred_y = P_LEFT[1];
959 }
960 } else {
961 163113 c->pred_x = P_LEFT[0];
962 163113 c->pred_y = P_LEFT[1];
963 }
964 2156518 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
965 }
966
967 /* At this point (mx,my) are full-pell and the relative displacement */
968 2156518 ppix = c->ref[0][0] + (my * s->c.linesize) + mx;
969
970 2156518 vard = c->sse(NULL, pix, ppix, s->c.linesize, 16);
971
972 2156518 s->mc_mb_var[s->c.mb_stride * mb_y + mb_x] = (vard+128)>>8;
973 2156518 c->mc_mb_var_sum_temp += (vard+128)>>8;
974
975
2/2
✓ Branch 0 taken 373674 times.
✓ Branch 1 taken 1782844 times.
2156518 if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {
976 373674 int p_score = FFMIN(vard, varc - 500 + (s->lambda2 >> FF_LAMBDA_SHIFT)*100);
977 373674 int i_score = varc - 500 + (s->lambda2 >> FF_LAMBDA_SHIFT)*20;
978 373674 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
979
980
3/4
✓ Branch 0 taken 100011 times.
✓ Branch 1 taken 273663 times.
✓ Branch 2 taken 100011 times.
✗ Branch 3 not taken.
373674 if (vard*2 + 200*256 > varc && !s->intra_penalty)
981 100011 mb_type|= CANDIDATE_MB_TYPE_INTRA;
982
3/4
✓ Branch 0 taken 2109 times.
✓ Branch 1 taken 371565 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2109 times.
373674 if (varc*2 + 200*256 > vard || s->c.qscale > 24){
983 // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
984 371565 mb_type|= CANDIDATE_MB_TYPE_INTER;
985 371565 c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
986
2/2
✓ Branch 0 taken 72444 times.
✓ Branch 1 taken 299121 times.
371565 if (s->mpv_flags & FF_MPV_FLAG_MV0)
987
4/4
✓ Branch 0 taken 4790 times.
✓ Branch 1 taken 67654 times.
✓ Branch 2 taken 4230 times.
✓ Branch 3 taken 560 times.
72444 if(mx || my)
988 71884 mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
989 }else{
990 2109 mx *= 1 << shift;
991 2109 my *= 1 << shift;
992 }
993
2/2
✓ Branch 0 taken 289035 times.
✓ Branch 1 taken 84639 times.
373674 if ((c->avctx->flags & AV_CODEC_FLAG_4MV)
994
6/6
✓ Branch 0 taken 289028 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 272236 times.
✓ Branch 3 taken 16792 times.
✓ Branch 4 taken 263062 times.
✓ Branch 5 taken 9174 times.
289035 && !c->skip && varc>50<<8 && vard>10<<8){
995
2/2
✓ Branch 1 taken 221120 times.
✓ Branch 2 taken 41942 times.
263062 if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
996 221120 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
997
998 263062 set_p_mv_tables(s, mx, my, 0);
999 }else
1000 110612 set_p_mv_tables(s, mx, my, 1);
1001
2/2
✓ Branch 0 taken 15600 times.
✓ Branch 1 taken 358074 times.
373674 if ((c->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1002
1/2
✓ Branch 0 taken 15600 times.
✗ Branch 1 not taken.
15600 && !c->skip){ //FIXME varc/d checks
1003
2/2
✓ Branch 1 taken 13535 times.
✓ Branch 2 taken 2065 times.
15600 if(interlaced_search(s, 0, s->c.p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1004 13535 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1005 }
1006 }else{
1007 int intra_score, i;
1008 1782844 mb_type= CANDIDATE_MB_TYPE_INTER;
1009
1010 1782844 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1011
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1782844 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1782844 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1012 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1013
1014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1782844 times.
1782844 if ((c->avctx->flags & AV_CODEC_FLAG_4MV)
1015 && !c->skip && varc>50<<8 && vard>10<<8){
1016 int dmin4= h263_mv4_search(s, mx, my, shift);
1017 if(dmin4 < dmin){
1018 mb_type= CANDIDATE_MB_TYPE_INTER4V;
1019 dmin=dmin4;
1020 }
1021 }
1022
2/2
✓ Branch 0 taken 85236 times.
✓ Branch 1 taken 1697608 times.
1782844 if ((c->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1023
2/2
✓ Branch 0 taken 85233 times.
✓ Branch 1 taken 3 times.
85236 && !c->skip){ //FIXME varc/d checks
1024 85233 int dmin_i= interlaced_search(s, 0, s->c.p_field_mv_table, s->p_field_select_table, mx, my, 0);
1025
2/2
✓ Branch 0 taken 11208 times.
✓ Branch 1 taken 74025 times.
85233 if(dmin_i < dmin){
1026 11208 mb_type = CANDIDATE_MB_TYPE_INTER_I;
1027 11208 dmin= dmin_i;
1028 }
1029 }
1030
1031 1782844 set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1032
1033 /* get intra luma score */
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1782844 times.
1782844 if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1035 intra_score= varc - 500;
1036 }else{
1037 1782844 unsigned mean = (sum+128)>>8;
1038 1782844 mean*= 0x01010101;
1039
1040
2/2
✓ Branch 0 taken 28525504 times.
✓ Branch 1 taken 1782844 times.
30308348 for(i=0; i<16; i++){
1041 28525504 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+ 0]) = mean;
1042 28525504 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+ 4]) = mean;
1043 28525504 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+ 8]) = mean;
1044 28525504 *(uint32_t*)(&c->scratchpad[i*s->c.linesize+12]) = mean;
1045 }
1046
1047 1782844 intra_score= c->mb_cmp[0](s, c->scratchpad, pix, s->c.linesize, 16);
1048 }
1049 1782844 intra_score += c->mb_penalty_factor*16 + s->intra_penalty;
1050
1051
2/2
✓ Branch 0 taken 95473 times.
✓ Branch 1 taken 1687371 times.
1782844 if(intra_score < dmin){
1052 95473 mb_type= CANDIDATE_MB_TYPE_INTRA;
1053 95473 s->c.cur_pic.mb_type[mb_y*s->c.mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1054 }else
1055 1687371 s->c.cur_pic.mb_type[mb_y*s->c.mb_stride + mb_x] = 0;
1056
1057 {
1058 1782844 int p_score = FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1059 1782844 int i_score = varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1060 1782844 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1061 }
1062 }
1063
1064 2156518 s->mb_type[mb_y*s->c.mb_stride + mb_x] = mb_type;
1065 2156518 }
1066
1067 int ff_pre_estimate_p_frame_motion(MPVEncContext *const s,
1068 int mb_x, int mb_y)
1069 {
1070 MotionEstContext *const c = &s->me;
1071 int mx, my, dmin;
1072 int P[10][2];
1073 const int shift = 1 + s->c.quarter_sample;
1074 const int xy = mb_x + mb_y*s->c.mb_stride;
1075 init_ref(c, s->new_pic->data, s->c.last_pic.data, NULL, 16*mb_x, 16*mb_y, 0);
1076
1077 av_assert0(s->c.quarter_sample == 0 || s->c.quarter_sample == 1);
1078
1079 c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1080 c->current_mv_penalty = c->mv_penalty[s->c.f_code] + MAX_DMV;
1081
1082 get_limits(s, 16*mb_x, 16*mb_y, 0);
1083 c->skip=0;
1084
1085 P_LEFT[0] = s->p_mv_table[xy + 1][0];
1086 P_LEFT[1] = s->p_mv_table[xy + 1][1];
1087
1088 if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
1089
1090 /* special case for first line */
1091 if (s->c.first_slice_line) {
1092 c->pred_x= P_LEFT[0];
1093 c->pred_y= P_LEFT[1];
1094 P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1095 P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
1096 } else {
1097 P_TOP[0] = s->p_mv_table[xy + s->c.mb_stride ][0];
1098 P_TOP[1] = s->p_mv_table[xy + s->c.mb_stride ][1];
1099 P_TOPRIGHT[0] = s->p_mv_table[xy + s->c.mb_stride - 1][0];
1100 P_TOPRIGHT[1] = s->p_mv_table[xy + s->c.mb_stride - 1][1];
1101 if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
1102 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1103 if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1104
1105 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1106 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1107
1108 c->pred_x = P_MEDIAN[0];
1109 c->pred_y = P_MEDIAN[1];
1110 }
1111
1112 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1113
1114 s->p_mv_table[xy][0] = mx<<shift;
1115 s->p_mv_table[xy][1] = my<<shift;
1116
1117 return dmin;
1118 }
1119
1120 793818 static int estimate_motion_b(MPVEncContext *const s, int mb_x, int mb_y,
1121 int16_t (*mv_table)[2], int ref_index, int f_code)
1122 {
1123 793818 MotionEstContext *const c = &s->me;
1124 793818 int mx = 0, my = 0, dmin = 0;
1125 int P[10][2];
1126 793818 const int shift= 1+s->c.quarter_sample;
1127 793818 const int mot_stride = s->c.mb_stride;
1128 793818 const int mot_xy = mb_y*mot_stride + mb_x;
1129 793818 const uint8_t * const mv_penalty = c->mv_penalty[f_code] + MAX_DMV;
1130 int mv_scale;
1131
1132 793818 c->current_mv_penalty= mv_penalty;
1133
1134 793818 get_limits(s, 16*mb_x, 16*mb_y, 1);
1135
1136
1/2
✓ Branch 0 taken 793818 times.
✗ Branch 1 not taken.
793818 if (c->motion_est != FF_ME_ZERO) {
1137 793818 P_LEFT[0] = mv_table[mot_xy - 1][0];
1138 793818 P_LEFT[1] = mv_table[mot_xy - 1][1];
1139
1140
2/2
✓ Branch 0 taken 10093 times.
✓ Branch 1 taken 783725 times.
793818 if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
1141
1142 /* special case for first line */
1143
2/2
✓ Branch 0 taken 735008 times.
✓ Branch 1 taken 58810 times.
793818 if (!s->c.first_slice_line) {
1144 735008 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1145 735008 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1146 735008 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
1147 735008 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
1148
2/2
✓ Branch 0 taken 11645 times.
✓ Branch 1 taken 723363 times.
735008 if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
1149
2/2
✓ Branch 0 taken 8165 times.
✓ Branch 1 taken 726843 times.
735008 if (P_TOPRIGHT[0] < c->xmin * (1 << shift)) P_TOPRIGHT[0] = c->xmin * (1 << shift);
1150
2/2
✓ Branch 0 taken 10999 times.
✓ Branch 1 taken 724009 times.
735008 if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
1151
1152 735008 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1153 735008 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1154 }
1155 793818 c->pred_x = P_LEFT[0];
1156 793818 c->pred_y = P_LEFT[1];
1157
1158
2/2
✓ Branch 0 taken 396909 times.
✓ Branch 1 taken 396909 times.
793818 if(mv_table == s->b_forw_mv_table){
1159 396909 mv_scale= (s->c.pb_time<<16) / (s->c.pp_time<<shift);
1160 }else{
1161 396909 mv_scale = ((s->c.pb_time - s->c.pp_time) * (1 << 16)) / (s->c.pp_time<<shift);
1162 }
1163
1164 793818 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1165 }
1166
1167 793818 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1168
1169
3/4
✓ Branch 0 taken 283718 times.
✓ Branch 1 taken 510100 times.
✓ Branch 2 taken 283718 times.
✗ Branch 3 not taken.
793818 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1170 283718 dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1171
1172 // s->mb_type[mb_y*s->c.mb_width + mb_x]= mb_type;
1173 793818 mv_table[mot_xy][0]= mx;
1174 793818 mv_table[mot_xy][1]= my;
1175
1176 793818 return dmin;
1177 }
1178
1179 5258102 static inline int check_bidir_mv(MPVEncContext *const s,
1180 int motion_fx, int motion_fy,
1181 int motion_bx, int motion_by,
1182 int pred_fx, int pred_fy,
1183 int pred_bx, int pred_by,
1184 int size, int h)
1185 {
1186 //FIXME optimize?
1187 //FIXME better f_code prediction (max mv & distance)
1188 //FIXME pointers
1189 5258102 MotionEstContext *const c = &s->me;
1190 5258102 const uint8_t * const mv_penalty_f = c->mv_penalty[s->c.f_code] + MAX_DMV; // f_code of the prev frame
1191 5258102 const uint8_t * const mv_penalty_b = c->mv_penalty[s->c.b_code] + MAX_DMV; // f_code of the prev frame
1192 5258102 int stride= c->stride;
1193 5258102 uint8_t *dest_y = c->scratchpad;
1194 const uint8_t *ptr;
1195 int dxy;
1196 int src_x, src_y;
1197 int fbmin;
1198 5258102 const uint8_t *const *src_data = c->src[0];
1199 5258102 const uint8_t *const *ref_data = c->ref[0];
1200 5258102 const uint8_t *const *ref2_data = c->ref[2];
1201
1202
2/2
✓ Branch 0 taken 625749 times.
✓ Branch 1 taken 4632353 times.
5258102 if(s->c.quarter_sample){
1203 625749 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1204 625749 src_x = motion_fx >> 2;
1205 625749 src_y = motion_fy >> 2;
1206
1207 625749 ptr = ref_data[0] + (src_y * stride) + src_x;
1208 625749 s->c.qdsp.put_qpel_pixels_tab[0][dxy](dest_y, ptr, stride);
1209
1210 625749 dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1211 625749 src_x = motion_bx >> 2;
1212 625749 src_y = motion_by >> 2;
1213
1214 625749 ptr = ref2_data[0] + (src_y * stride) + src_x;
1215 625749 s->c.qdsp.avg_qpel_pixels_tab[size][dxy](dest_y, ptr, stride);
1216 }else{
1217 4632353 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1218 4632353 src_x = motion_fx >> 1;
1219 4632353 src_y = motion_fy >> 1;
1220
1221 4632353 ptr = ref_data[0] + (src_y * stride) + src_x;
1222 4632353 s->c.hdsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1223
1224 4632353 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1225 4632353 src_x = motion_bx >> 1;
1226 4632353 src_y = motion_by >> 1;
1227
1228 4632353 ptr = ref2_data[0] + (src_y * stride) + src_x;
1229 4632353 s->c.hdsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1230 }
1231
1232 10516204 fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
1233 5258102 +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
1234 5258102 + c->mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
1235
1236 5258102 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1237 }
1238 //FIXME CHROMA !!!
1239
1240 5258102 return fbmin;
1241 }
1242
1243 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1244 396909 static inline int bidir_refine(MPVEncContext *const s, int mb_x, int mb_y)
1245 {
1246 396909 MotionEstContext *const c = &s->me;
1247 396909 const int mot_stride = s->c.mb_stride;
1248 396909 const int xy = mb_y *mot_stride + mb_x;
1249 int fbmin;
1250 396909 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1251 396909 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1252 396909 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1253 396909 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1254 396909 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1255 396909 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1256 396909 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1257 396909 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1258 396909 const int flags= c->sub_flags;
1259 396909 const int qpel= flags&FLAG_QPEL;
1260 396909 const int shift= 1+qpel;
1261 396909 const int xmin= c->xmin * (1 << shift);
1262 396909 const int ymin= c->ymin * (1 << shift);
1263 396909 const int xmax= c->xmax<<shift;
1264 396909 const int ymax= c->ymax<<shift;
1265 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
1266 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
1267 396909 int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
1268 396909 uint8_t map[256] = { 0 };
1269
1270 396909 map[hashidx&255] = 1;
1271
1272 396909 fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1273 motion_bx, motion_by,
1274 pred_fx, pred_fy,
1275 pred_bx, pred_by,
1276 0, 16);
1277
1278
1/2
✓ Branch 0 taken 396909 times.
✗ Branch 1 not taken.
396909 if (c->avctx->bidir_refine) {
1279 int end;
1280 static const uint8_t limittab[5]={0,8,32,64,80};
1281 396909 const int limit = limittab[c->avctx->bidir_refine];
1282 static const int8_t vect[][4]={
1283 { 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},
1284
1285 { 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},
1286 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
1287 { 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},
1288 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
1289
1290 { 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},
1291 { 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},
1292 { 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},
1293 { 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},
1294
1295 { 1, 1, 1, 1}, {-1,-1,-1,-1},
1296 { 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},
1297 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
1298 };
1299 static const uint8_t hash[]={
1300 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),
1301
1302 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),
1303 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
1304 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),
1305 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
1306
1307 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),
1308 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),
1309 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),
1310 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),
1311
1312 HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
1313 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),
1314 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),
1315 };
1316
1317 #define CHECK_BIDIR(fx,fy,bx,by)\
1318 if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
1319 &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1320 &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1321 int score;\
1322 map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
1323 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);\
1324 if(score < fbmin){\
1325 hashidx += HASH(fx,fy,bx,by);\
1326 fbmin= score;\
1327 motion_fx+=fx;\
1328 motion_fy+=fy;\
1329 motion_bx+=bx;\
1330 motion_by+=by;\
1331 end=0;\
1332 }\
1333 }
1334 #define CHECK_BIDIR2(a,b,c,d)\
1335 CHECK_BIDIR(a,b,c,d)\
1336 CHECK_BIDIR(-(a),-(b),-(c),-(d))
1337
1338 do{
1339 int i;
1340 800082 int borderdist=0;
1341 800082 end=1;
1342
1343
12/12
✓ Branch 0 taken 748768 times.
✓ Branch 1 taken 51314 times.
✓ Branch 2 taken 736566 times.
✓ Branch 3 taken 12202 times.
✓ Branch 5 taken 90534 times.
✓ Branch 6 taken 646032 times.
✓ Branch 7 taken 674220 times.
✓ Branch 8 taken 125862 times.
✓ Branch 9 taken 661794 times.
✓ Branch 10 taken 12426 times.
✓ Branch 12 taken 109105 times.
✓ Branch 13 taken 552689 times.
800082 CHECK_BIDIR2(0,0,0,1)
1344
12/12
✓ Branch 0 taken 687729 times.
✓ Branch 1 taken 112353 times.
✓ Branch 2 taken 676864 times.
✓ Branch 3 taken 10865 times.
✓ Branch 5 taken 96810 times.
✓ Branch 6 taken 580054 times.
✓ Branch 7 taken 594143 times.
✓ Branch 8 taken 205939 times.
✓ Branch 9 taken 584335 times.
✓ Branch 10 taken 9808 times.
✓ Branch 12 taken 79316 times.
✓ Branch 13 taken 505019 times.
800082 CHECK_BIDIR2(0,0,1,0)
1345
12/12
✓ Branch 0 taken 636120 times.
✓ Branch 1 taken 163962 times.
✓ Branch 2 taken 619238 times.
✓ Branch 3 taken 16882 times.
✓ Branch 5 taken 89381 times.
✓ Branch 6 taken 529857 times.
✓ Branch 7 taken 538864 times.
✓ Branch 8 taken 261218 times.
✓ Branch 9 taken 521749 times.
✓ Branch 10 taken 17115 times.
✓ Branch 12 taken 56923 times.
✓ Branch 13 taken 464826 times.
800082 CHECK_BIDIR2(0,1,0,0)
1346
12/12
✓ Branch 0 taken 570273 times.
✓ Branch 1 taken 229809 times.
✓ Branch 2 taken 554750 times.
✓ Branch 3 taken 15523 times.
✓ Branch 5 taken 62745 times.
✓ Branch 6 taken 492005 times.
✓ Branch 7 taken 521858 times.
✓ Branch 8 taken 278224 times.
✓ Branch 9 taken 505897 times.
✓ Branch 10 taken 15961 times.
✓ Branch 12 taken 69884 times.
✓ Branch 13 taken 436013 times.
800082 CHECK_BIDIR2(1,0,0,0)
1347
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800082 times.
800082 for(i=8; i<limit; i++){
1349 int fx= motion_fx+vect[i][0];
1350 int fy= motion_fy+vect[i][1];
1351 int bx= motion_bx+vect[i][2];
1352 int by= motion_by+vect[i][3];
1353 if(borderdist<=0){
1354 int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
1355 int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
1356 if((a|b) < 0)
1357 map[(hashidx+hash[i])&255] = 1;
1358 }
1359 if(!map[(hashidx+hash[i])&255]){
1360 int score;
1361 map[(hashidx+hash[i])&255] = 1;
1362 score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
1363 if(score < fbmin){
1364 hashidx += hash[i];
1365 fbmin= score;
1366 motion_fx=fx;
1367 motion_fy=fy;
1368 motion_bx=bx;
1369 motion_by=by;
1370 end=0;
1371 borderdist--;
1372 if(borderdist<=0){
1373 int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
1374 int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
1375 borderdist= FFMIN(a,b);
1376 }
1377 }
1378 }
1379 }
1380
2/2
✓ Branch 0 taken 403173 times.
✓ Branch 1 taken 396909 times.
800082 }while(!end);
1381 }
1382
1383 396909 s->b_bidir_forw_mv_table[xy][0]= motion_fx;
1384 396909 s->b_bidir_forw_mv_table[xy][1]= motion_fy;
1385 396909 s->b_bidir_back_mv_table[xy][0]= motion_bx;
1386 396909 s->b_bidir_back_mv_table[xy][1]= motion_by;
1387
1388 396909 return fbmin;
1389 }
1390
1391 180207 static inline int direct_search(MPVEncContext *const s, int mb_x, int mb_y)
1392 {
1393 180207 MotionEstContext *const c = &s->me;
1394 int P[10][2];
1395 180207 const int mot_stride = s->c.mb_stride;
1396 180207 const int mot_xy = mb_y*mot_stride + mb_x;
1397 180207 const int shift= 1+s->c.quarter_sample;
1398 int dmin, i;
1399 180207 const int time_pp= s->c.pp_time;
1400 180207 const int time_pb= s->c.pb_time;
1401 int mx, my, xmin, xmax, ymin, ymax;
1402 180207 int16_t (*mv_table)[2]= s->b_direct_mv_table;
1403
1404 180207 c->current_mv_penalty= c->mv_penalty[1] + MAX_DMV;
1405 180207 ymin= xmin=(-32)>>shift;
1406 180207 ymax= xmax= 31>>shift;
1407
1408
2/2
✓ Branch 0 taken 52590 times.
✓ Branch 1 taken 127617 times.
180207 if (IS_8X8(s->c.next_pic.mb_type[mot_xy])) {
1409 52590 s->c.mv_type = MV_TYPE_8X8;
1410 }else{
1411 127617 s->c.mv_type = MV_TYPE_16X16;
1412 }
1413
1414
2/2
✓ Branch 0 taken 337977 times.
✓ Branch 1 taken 52590 times.
390567 for(i=0; i<4; i++){
1415 337977 int index= s->c.block_index[i];
1416 int min, max;
1417
1418 337977 c->co_located_mv[i][0] = s->c.next_pic.motion_val[0][index][0];
1419 337977 c->co_located_mv[i][1] = s->c.next_pic.motion_val[0][index][1];
1420 337977 c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1421 337977 c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1422 // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1423 // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1424
1425
2/2
✓ Branch 0 taken 133237 times.
✓ Branch 1 taken 204740 times.
337977 max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1426
2/2
✓ Branch 0 taken 133237 times.
✓ Branch 1 taken 204740 times.
337977 min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1427 337977 max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1428 337977 min+= 16*mb_x - 1;
1429 337977 xmax= FFMIN(xmax, s->c.width - max);
1430 337977 xmin= FFMAX(xmin, - 16 - min);
1431
1432
2/2
✓ Branch 0 taken 135652 times.
✓ Branch 1 taken 202325 times.
337977 max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1433
2/2
✓ Branch 0 taken 135652 times.
✓ Branch 1 taken 202325 times.
337977 min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1434 337977 max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1435 337977 min+= 16*mb_y - 1;
1436 337977 ymax= FFMIN(ymax, s->c.height - max);
1437 337977 ymin= FFMAX(ymin, - 16 - min);
1438
1439
2/2
✓ Branch 0 taken 127617 times.
✓ Branch 1 taken 210360 times.
337977 if(s->c.mv_type == MV_TYPE_16X16) break;
1440 }
1441
1442 av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1443
1444
7/8
✓ Branch 0 taken 180155 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 180154 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 180127 times.
✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 180127 times.
180207 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1445 80 s->b_direct_mv_table[mot_xy][0]= 0;
1446 80 s->b_direct_mv_table[mot_xy][1]= 0;
1447
1448 80 return 256*256*256*64-1;
1449 }
1450
1451 180127 c->xmin= xmin;
1452 180127 c->ymin= ymin;
1453 180127 c->xmax= xmax;
1454 180127 c->ymax= ymax;
1455 180127 c->flags |= FLAG_DIRECT;
1456 180127 c->sub_flags |= FLAG_DIRECT;
1457 180127 c->pred_x=0;
1458 180127 c->pred_y=0;
1459
1460 180127 P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin * (1 << shift), xmax << shift);
1461 180127 P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin * (1 << shift), ymax << shift);
1462
1463 /* special case for first line */
1464
2/2
✓ Branch 0 taken 167574 times.
✓ Branch 1 taken 12553 times.
180127 if (!s->c.first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
1465 167574 P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin * (1 << shift), xmax << shift);
1466 167574 P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin * (1 << shift), ymax << shift);
1467 167574 P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1][0], xmin * (1 << shift), xmax << shift);
1468 167574 P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1][1], ymin * (1 << shift), ymax << shift);
1469
1470 167574 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1471 167574 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1472 }
1473
1474 180127 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1475
2/2
✓ Branch 0 taken 38282 times.
✓ Branch 1 taken 141845 times.
180127 if(c->sub_flags&FLAG_QPEL)
1476 38282 dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1477 else
1478 141845 dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1479
1480
4/4
✓ Branch 0 taken 103552 times.
✓ Branch 1 taken 76575 times.
✓ Branch 2 taken 103531 times.
✓ Branch 3 taken 21 times.
180127 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1481 103531 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1482
1483 180127 get_limits(s, 16*mb_x, 16*mb_y, 1); //restore c->?min/max, maybe not needed
1484
1485 180127 mv_table[mot_xy][0]= mx;
1486 180127 mv_table[mot_xy][1]= my;
1487 180127 c->flags &= ~FLAG_DIRECT;
1488 180127 c->sub_flags &= ~FLAG_DIRECT;
1489
1490 180127 return dmin;
1491 }
1492
1493 396999 void ff_estimate_b_frame_motion(MPVEncContext *const s,
1494 int mb_x, int mb_y)
1495 {
1496 396999 MotionEstContext *const c = &s->me;
1497 int fmin, bmin, dmin, fbmin, bimin, fimin;
1498 396999 int type=0;
1499 396999 const int xy = mb_y*s->c.mb_stride + mb_x;
1500 396999 init_ref(c, s->new_pic->data, s->c.last_pic.data,
1501 396999 s->c.next_pic.data, 16 * mb_x, 16 * mb_y, 2);
1502
1503 396999 get_limits(s, 16*mb_x, 16*mb_y, 1);
1504
1505 396999 c->skip=0;
1506
1507
4/4
✓ Branch 0 taken 180207 times.
✓ Branch 1 taken 216792 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 180117 times.
396999 if (s->c.codec_id == AV_CODEC_ID_MPEG4 && s->c.next_pic.mbskip_table[xy]) {
1508 90 int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
1509
1510 90 score= ((unsigned)(score*score + 128*256))>>16;
1511 90 c->mc_mb_var_sum_temp += score;
1512 90 s->mc_mb_var[mb_y*s->c.mb_stride + mb_x] = score; //FIXME use SSE
1513 90 s->mb_type[mb_y*s->c.mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
1514
1515 90 return;
1516 }
1517
1518 396909 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1519 396909 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1520 396909 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1521
1522
2/2
✓ Branch 0 taken 180117 times.
✓ Branch 1 taken 216792 times.
396909 if (s->c.codec_id == AV_CODEC_ID_MPEG4)
1523 180117 dmin= direct_search(s, mb_x, mb_y);
1524 else
1525 216792 dmin= INT_MAX;
1526
1527 // FIXME penalty stuff for non-MPEG-4
1528 396909 c->skip=0;
1529 396909 fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->c.f_code) +
1530 396909 3 * c->mb_penalty_factor;
1531
1532 396909 c->skip=0;
1533 396909 bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->c.b_code) +
1534 396909 2 * c->mb_penalty_factor;
1535 ff_dlog(c->avctx, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1536
1537 396909 c->skip=0;
1538 396909 fbmin= bidir_refine(s, mb_x, mb_y) + c->mb_penalty_factor;
1539 ff_dlog(c->avctx, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1540
1541
2/2
✓ Branch 0 taken 115200 times.
✓ Branch 1 taken 281709 times.
396909 if (c->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
1542 //FIXME mb type penalty
1543 115200 c->skip=0;
1544 115200 c->current_mv_penalty= c->mv_penalty[s->c.f_code] + MAX_DMV;
1545 115200 fimin= interlaced_search(s, 0,
1546 115200 s->b_field_mv_table[0], s->b_field_select_table[0],
1547 115200 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1548 115200 c->current_mv_penalty= c->mv_penalty[s->c.b_code] + MAX_DMV;
1549 115200 bimin= interlaced_search(s, 2,
1550 115200 s->b_field_mv_table[1], s->b_field_select_table[1],
1551 115200 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1552 }else
1553 281709 fimin= bimin= INT_MAX;
1554
1555 {
1556 396909 int score= fmin;
1557 396909 type = CANDIDATE_MB_TYPE_FORWARD;
1558
1559
2/2
✓ Branch 0 taken 54175 times.
✓ Branch 1 taken 342734 times.
396909 if (dmin <= score){
1560 54175 score = dmin;
1561 54175 type = CANDIDATE_MB_TYPE_DIRECT;
1562 }
1563
2/2
✓ Branch 0 taken 198716 times.
✓ Branch 1 taken 198193 times.
396909 if(bmin<score){
1564 198716 score=bmin;
1565 198716 type= CANDIDATE_MB_TYPE_BACKWARD;
1566 }
1567
2/2
✓ Branch 0 taken 233673 times.
✓ Branch 1 taken 163236 times.
396909 if(fbmin<score){
1568 233673 score=fbmin;
1569 233673 type= CANDIDATE_MB_TYPE_BIDIR;
1570 }
1571
2/2
✓ Branch 0 taken 3902 times.
✓ Branch 1 taken 393007 times.
396909 if(fimin<score){
1572 3902 score=fimin;
1573 3902 type= CANDIDATE_MB_TYPE_FORWARD_I;
1574 }
1575
2/2
✓ Branch 0 taken 4095 times.
✓ Branch 1 taken 392814 times.
396909 if(bimin<score){
1576 4095 score=bimin;
1577 4095 type= CANDIDATE_MB_TYPE_BACKWARD_I;
1578 }
1579
1580 396909 score= ((unsigned)(score*score + 128*256))>>16;
1581 396909 c->mc_mb_var_sum_temp += score;
1582 396909 s->mc_mb_var[mb_y*s->c.mb_stride + mb_x] = score; //FIXME use SSE
1583 }
1584
1585
2/2
✓ Branch 0 taken 218539 times.
✓ Branch 1 taken 178370 times.
396909 if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1586 218539 type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1587
2/2
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 187549 times.
218539 if(fimin < INT_MAX)
1588 30990 type |= CANDIDATE_MB_TYPE_FORWARD_I;
1589
2/2
✓ Branch 0 taken 31154 times.
✓ Branch 1 taken 187385 times.
218539 if(bimin < INT_MAX)
1590 31154 type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1591
4/4
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 187549 times.
✓ Branch 2 taken 26027 times.
✓ Branch 3 taken 4963 times.
218539 if(fimin < INT_MAX && bimin < INT_MAX){
1592 26027 type |= CANDIDATE_MB_TYPE_BIDIR_I;
1593 }
1594 //FIXME something smarter
1595
2/2
✓ Branch 0 taken 76779 times.
✓ Branch 1 taken 141760 times.
218539 if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
1596
4/4
✓ Branch 0 taken 141835 times.
✓ Branch 1 taken 76704 times.
✓ Branch 2 taken 141760 times.
✓ Branch 3 taken 75 times.
218539 if (s->c.codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT &&
1597
4/4
✓ Branch 0 taken 65226 times.
✓ Branch 1 taken 76534 times.
✓ Branch 2 taken 36624 times.
✓ Branch 3 taken 28602 times.
141760 s->mpv_flags & FF_MPV_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
1598 36624 type |= CANDIDATE_MB_TYPE_DIRECT0;
1599 }
1600
1601 396909 s->mb_type[mb_y*s->c.mb_stride + mb_x]= type;
1602 }
1603
1604 /* find best f_code for ME which do unlimited searches */
1605 12018 int ff_get_best_fcode(MPVMainEncContext *const m, const int16_t (*mv_table)[2], int type)
1606 {
1607 12018 MPVEncContext *const s = &m->s;
1608 12018 MotionEstContext *const c = &s->me;
1609
1610
1/2
✓ Branch 0 taken 12018 times.
✗ Branch 1 not taken.
12018 if (c->motion_est != FF_ME_ZERO) {
1611 int score[8];
1612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12018 times.
12018 int i, range = c->avctx->me_range ? c->avctx->me_range : (INT_MAX/2);
1613 12018 const uint8_t * fcode_tab = m->fcode_tab;
1614 12018 int best_fcode=-1;
1615 12018 int best_score=-10000000;
1616
1617
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 11276 times.
12018 if (s->c.msmpeg4_version != MSMP4_UNUSED)
1618 742 range= FFMIN(range, 16);
1619
2/2
✓ Branch 0 taken 5089 times.
✓ Branch 1 taken 6187 times.
11276 else if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1620
2/2
✓ Branch 0 taken 4481 times.
✓ Branch 1 taken 608 times.
5089 c->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
1621 4481 range= FFMIN(range, 256);
1622
1623
2/2
✓ Branch 0 taken 96144 times.
✓ Branch 1 taken 12018 times.
108162 for(i=0; i<8; i++) score[i]= s->c.mb_num*(8-i);
1624
1625
2/2
✓ Branch 0 taken 171900 times.
✓ Branch 1 taken 12018 times.
183918 for (int y = 0; y < s->c.mb_height; y++) {
1626 int x;
1627 171900 int xy= y*s->c.mb_stride;
1628
2/2
✓ Branch 0 taken 3830188 times.
✓ Branch 1 taken 171900 times.
4002088 for(x=0; x<s->c.mb_width; x++, xy++){
1629
2/2
✓ Branch 0 taken 3117479 times.
✓ Branch 1 taken 712709 times.
3830188 if(s->mb_type[xy] & type){
1630 3117479 int mx= mv_table[xy][0];
1631 3117479 int my= mv_table[xy][1];
1632 3117479 int fcode = FFMAX(fcode_tab[mx], fcode_tab[my]);
1633 int j;
1634
1635
6/6
✓ Branch 0 taken 3116518 times.
✓ Branch 1 taken 961 times.
✓ Branch 2 taken 3115713 times.
✓ Branch 3 taken 805 times.
✓ Branch 4 taken 3115055 times.
✓ Branch 5 taken 658 times.
3117479 if (mx >= range || mx < -range ||
1636
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 3114596 times.
3115055 my >= range || my < -range)
1637 2883 continue;
1638
1639
3/4
✓ Branch 0 taken 3330369 times.
✓ Branch 1 taken 3114596 times.
✓ Branch 2 taken 3330369 times.
✗ Branch 3 not taken.
6444965 for(j=0; j<fcode && j<8; j++){
1640
2/2
✓ Branch 0 taken 2114929 times.
✓ Branch 1 taken 1215440 times.
3330369 if (s->c.pict_type == AV_PICTURE_TYPE_B ||
1641
2/2
✓ Branch 0 taken 2003954 times.
✓ Branch 1 taken 110975 times.
2114929 s->mc_mb_var[xy] < s->mb_var[xy])
1642 3219394 score[j]-= 170;
1643 }
1644 }
1645 }
1646 }
1647
1648
2/2
✓ Branch 0 taken 84126 times.
✓ Branch 1 taken 12018 times.
96144 for(i=1; i<8; i++){
1649
2/2
✓ Branch 0 taken 16040 times.
✓ Branch 1 taken 68086 times.
84126 if(score[i] > best_score){
1650 16040 best_score= score[i];
1651 16040 best_fcode= i;
1652 }
1653 }
1654
1655 12018 return best_fcode;
1656 }else{
1657 return 1;
1658 }
1659 }
1660
1661 6718 void ff_fix_long_p_mvs(MPVEncContext *const s, int type)
1662 {
1663 6718 MotionEstContext *const c = &s->me;
1664 6718 const int f_code= s->c.f_code;
1665 int y, range;
1666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6718 times.
6718 av_assert0(s->c.pict_type == AV_PICTURE_TYPE_P);
1667
1668
4/4
✓ Branch 0 taken 3722 times.
✓ Branch 1 taken 2996 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 2980 times.
6718 range = (((s->c.out_format == FMT_MPEG1 || s->c.msmpeg4_version != MSMP4_UNUSED) ? 8 : 16) << f_code);
1669
1670
3/4
✓ Branch 0 taken 3844 times.
✓ Branch 1 taken 2874 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3844 times.
6718 av_assert0(range <= 16 || s->c.msmpeg4_version == MSMP4_UNUSED);
1671
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6718 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6718 av_assert0(range <=256 || !(s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO && c->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
1672
1673
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6718 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6718 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1674
1675
2/2
✓ Branch 0 taken 980 times.
✓ Branch 1 taken 5738 times.
6718 if (c->avctx->flags & AV_CODEC_FLAG_4MV) {
1676 980 const int wrap= s->c.b8_stride;
1677
1678 /* clip / convert to intra 8x8 type MVs */
1679
2/2
✓ Branch 0 taken 13800 times.
✓ Branch 1 taken 980 times.
14780 for(y=0; y<s->c.mb_height; y++){
1680 13800 int xy= y*2*wrap;
1681 13800 int i= y*s->c.mb_stride;
1682 int x;
1683
1684
2/2
✓ Branch 0 taken 289008 times.
✓ Branch 1 taken 13800 times.
302808 for(x=0; x<s->c.mb_width; x++){
1685
2/2
✓ Branch 0 taken 221094 times.
✓ Branch 1 taken 67914 times.
289008 if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1686 int block;
1687
2/2
✓ Branch 0 taken 884376 times.
✓ Branch 1 taken 221094 times.
1105470 for(block=0; block<4; block++){
1688 884376 int off= (block& 1) + (block>>1)*wrap;
1689 884376 int mx = s->c.cur_pic.motion_val[0][ xy + off ][0];
1690 884376 int my = s->c.cur_pic.motion_val[0][ xy + off ][1];
1691
1692
4/4
✓ Branch 0 taken 883910 times.
✓ Branch 1 taken 466 times.
✓ Branch 2 taken 883226 times.
✓ Branch 3 taken 684 times.
884376 if( mx >=range || mx <-range
1693
4/4
✓ Branch 0 taken 883067 times.
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 318 times.
✓ Branch 3 taken 882749 times.
883226 || my >=range || my <-range){
1694 1627 s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1695 1627 s->mb_type[i] |= type;
1696 1627 s->c.cur_pic.mb_type[i] = type;
1697 }
1698 }
1699 }
1700 289008 xy+=2;
1701 289008 i++;
1702 }
1703 }
1704 }
1705 6718 }
1706
1707 /**
1708 * @param truncate 1 for truncation, 0 for using intra
1709 */
1710 16294 void ff_fix_long_mvs(MPVEncContext *const s, uint8_t *field_select_table, int field_select,
1711 int16_t (*mv_table)[2], int f_code, int type, int truncate)
1712 {
1713 16294 MotionEstContext *const c = &s->me;
1714 int y, h_range, v_range;
1715
1716 // RAL: 8 in MPEG-1, 16 in MPEG-4
1717
4/4
✓ Branch 0 taken 6070 times.
✓ Branch 1 taken 10224 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 5328 times.
16294 int range = (((s->c.out_format == FMT_MPEG1 || s->c.msmpeg4_version != MSMP4_UNUSED) ? 8 : 16) << f_code);
1718
1719
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16294 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16294 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1720
1721 16294 h_range= range;
1722
2/2
✓ Branch 0 taken 4400 times.
✓ Branch 1 taken 11894 times.
16294 v_range= field_select_table ? range>>1 : range;
1723
1724 /* clip / convert to intra 16x16 type MVs */
1725
2/2
✓ Branch 0 taken 230161 times.
✓ Branch 1 taken 16294 times.
246455 for(y=0; y<s->c.mb_height; y++){
1726 int x;
1727 230161 int xy= y*s->c.mb_stride;
1728
2/2
✓ Branch 0 taken 5015347 times.
✓ Branch 1 taken 230161 times.
5245508 for(x=0; x<s->c.mb_width; x++){
1729
2/2
✓ Branch 0 taken 3490014 times.
✓ Branch 1 taken 1525333 times.
5015347 if (s->mb_type[xy] & type){ // RAL: "type" test added...
1730
4/4
✓ Branch 0 taken 359422 times.
✓ Branch 1 taken 3130592 times.
✓ Branch 2 taken 179813 times.
✓ Branch 3 taken 179609 times.
3490014 if (!field_select_table || field_select_table[xy] == field_select) {
1731
4/4
✓ Branch 0 taken 3307916 times.
✓ Branch 1 taken 2489 times.
✓ Branch 2 taken 3305712 times.
✓ Branch 3 taken 2204 times.
3310405 if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
1732
4/4
✓ Branch 0 taken 3304174 times.
✓ Branch 1 taken 1538 times.
✓ Branch 2 taken 1243 times.
✓ Branch 3 taken 3302931 times.
3305712 || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
1733
1734
2/2
✓ Branch 0 taken 1505 times.
✓ Branch 1 taken 5969 times.
7474 if(truncate){
1735
2/2
✓ Branch 0 taken 417 times.
✓ Branch 1 taken 1088 times.
1505 if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
1736
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 620 times.
1088 else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
1737
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 1163 times.
1505 if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
1738
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 845 times.
1163 else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
1739 }else{
1740 5969 s->mb_type[xy] &= ~type;
1741 5969 s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
1742 5969 mv_table[xy][0]=
1743 5969 mv_table[xy][1]= 0;
1744 }
1745 }
1746 }
1747 }
1748 5015347 xy++;
1749 }
1750 }
1751 16294 }
1752