FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/motion_est.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 862 1003 85.9%
Functions: 27 33 81.8%
Branches: 502 621 80.8%

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 "mathops.h"
36 #include "motion_est.h"
37 #include "mpegutils.h"
38 #include "mpegvideoenc.h"
39
40 #define P_LEFT P[1]
41 #define P_TOP P[2]
42 #define P_TOPRIGHT P[3]
43 #define P_MEDIAN P[4]
44 #define P_MV1 P[9]
45
46 #define ME_MAP_SHIFT 3
47 #define ME_MAP_MV_BITS 11
48
49 static int sad_hpel_motion_search(MpegEncContext * s,
50 int *mx_ptr, int *my_ptr, int dmin,
51 int src_index, int ref_index,
52 int size, int h);
53
54 5998721 static inline unsigned update_map_generation(MotionEstContext *c)
55 {
56 5998721 c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
57
2/2
✓ Branch 0 taken 5780 times.
✓ Branch 1 taken 5992941 times.
5998721 if(c->map_generation==0){
58 5780 c->map_generation= 1<<(ME_MAP_MV_BITS*2);
59 5780 memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
60 }
61 5998721 return c->map_generation;
62 }
63
64 /* shape adaptive search stuff */
65 typedef struct Minima{
66 int height;
67 int x, y;
68 int checked;
69 }Minima;
70
71 static int minima_cmp(const void *a, const void *b){
72 const Minima *da = (const Minima *) a;
73 const Minima *db = (const Minima *) b;
74
75 return da->height - db->height;
76 }
77
78 #define FLAG_QPEL 1 //must be 1
79 #define FLAG_CHROMA 2
80 #define FLAG_DIRECT 4
81
82 2509423 static inline void init_ref(MotionEstContext *c, uint8_t *const src[3],
83 uint8_t *const ref[3], uint8_t *const ref2[3],
84 int x, int y, int ref_index)
85 {
86 2509423 const int offset[3]= {
87 2509423 y*c-> stride + x,
88 2509423 ((y*c->uvstride + x)>>1),
89 2509423 ((y*c->uvstride + x)>>1),
90 };
91 int i;
92
2/2
✓ Branch 0 taken 7528269 times.
✓ Branch 1 taken 2509423 times.
10037692 for(i=0; i<3; i++){
93 7528269 c->src[0][i]= src [i] + offset[i];
94 7528269 c->ref[0][i]= ref [i] + offset[i];
95 }
96
2/2
✓ Branch 0 taken 408312 times.
✓ Branch 1 taken 2101111 times.
2509423 if(ref_index){
97
2/2
✓ Branch 0 taken 1224936 times.
✓ Branch 1 taken 408312 times.
1633248 for(i=0; i<3; i++){
98 1224936 c->ref[ref_index][i]= ref2[i] + offset[i];
99 }
100 }
101 2509423 }
102
103 33894 static int get_flags(MotionEstContext *c, int direct, int chroma){
104 33894 return ((c->avctx->flags&AV_CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33894 times.
33894 + (direct ? FLAG_DIRECT : 0)
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33894 times.
33894 + (chroma ? FLAG_CHROMA : 0);
107 }
108
109 2665836 static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
110 const int size, const int h, int ref_index, int src_index,
111 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
112 2665836 MotionEstContext * const c= &s->me;
113 2665836 const int stride= c->stride;
114 2665836 const int hx = subx + x * (1 << (1 + qpel));
115 2665836 const int hy = suby + y * (1 << (1 + qpel));
116 2665836 const uint8_t * const * const ref = c->ref[ref_index];
117 2665836 const uint8_t * const * const src = c->src[src_index];
118 int d;
119 //FIXME check chroma 4mv, (no crashes ...)
120 av_assert2(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
121
4/8
✓ Branch 0 taken 2665836 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2665836 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2665836 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2665836 times.
✗ Branch 7 not taken.
5331672 if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
122 2665836 const int time_pp= s->pp_time;
123 2665836 const int time_pb= s->pb_time;
124 2665836 const int mask= 2*qpel+1;
125
2/2
✓ Branch 0 taken 667160 times.
✓ Branch 1 taken 1998676 times.
2665836 if(s->mv_type==MV_TYPE_8X8){
126 int i;
127
2/2
✓ Branch 0 taken 2668640 times.
✓ Branch 1 taken 667160 times.
3335800 for(i=0; i<4; i++){
128 2668640 int fx = c->direct_basis_mv[i][0] + hx;
129 2668640 int fy = c->direct_basis_mv[i][1] + hy;
130
2/2
✓ Branch 0 taken 1689660 times.
✓ Branch 1 taken 978980 times.
2668640 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));
131
2/2
✓ Branch 0 taken 1648144 times.
✓ Branch 1 taken 1020496 times.
2668640 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));
132 2668640 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
133 2668640 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
134
135 2668640 uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
136
2/2
✓ Branch 0 taken 961440 times.
✓ Branch 1 taken 1707200 times.
2668640 if(qpel){
137 961440 c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
138 961440 c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
139 }else{
140 1707200 c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
141 1707200 c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
142 }
143 }
144 }else{
145 1998676 int fx = c->direct_basis_mv[0][0] + hx;
146 1998676 int fy = c->direct_basis_mv[0][1] + hy;
147
2/2
✓ Branch 0 taken 1513793 times.
✓ Branch 1 taken 484883 times.
1998676 int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
148
2/2
✓ Branch 0 taken 1470599 times.
✓ Branch 1 taken 528077 times.
1998676 int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
149 1998676 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
150 1998676 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
151
152
2/2
✓ Branch 0 taken 433681 times.
✓ Branch 1 taken 1564995 times.
1998676 if(qpel){
153 433681 c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
154 433681 c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
155 433681 c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
156 433681 c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
157 433681 c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
158 433681 c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
159 433681 c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
160 433681 c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
161 }else{
162 av_assert2((fx>>1) + 16*s->mb_x >= -16);
163 av_assert2((fy>>1) + 16*s->mb_y >= -16);
164 av_assert2((fx>>1) + 16*s->mb_x <= s->width);
165 av_assert2((fy>>1) + 16*s->mb_y <= s->height);
166 av_assert2((bx>>1) + 16*s->mb_x >= -16);
167 av_assert2((by>>1) + 16*s->mb_y >= -16);
168 av_assert2((bx>>1) + 16*s->mb_x <= s->width);
169 av_assert2((by>>1) + 16*s->mb_y <= s->height);
170
171 1564995 c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
172 1564995 c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
173 }
174 }
175 2665836 d = cmp_func(s, c->temp, src[0], stride, 16);
176 }else
177 d= 256*256*256*32;
178 2665836 return d;
179 }
180
181 59533537 static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
182 const int size, const int h, int ref_index, int src_index,
183 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){
184 59533537 MotionEstContext * const c= &s->me;
185 59533537 const int stride= c->stride;
186 59533537 const int uvstride= c->uvstride;
187 59533537 const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
188 59533537 const int hx= subx + x*(1<<(1+qpel));
189 59533537 const int hy= suby + y*(1<<(1+qpel));
190 59533537 const uint8_t * const * const ref = c->ref[ref_index];
191 59533537 const uint8_t * const * const src = c->src[src_index];
192 int d;
193 //FIXME check chroma 4mv, (no crashes ...)
194 int uvdxy; /* no, it might not be used uninitialized */
195
2/2
✓ Branch 0 taken 7984710 times.
✓ Branch 1 taken 51548827 times.
59533537 if(dxy){
196
2/2
✓ Branch 0 taken 6065785 times.
✓ Branch 1 taken 1918925 times.
7984710 if(qpel){
197
1/2
✓ Branch 0 taken 6065785 times.
✗ Branch 1 not taken.
6065785 if (h << size == 16) {
198 6065785 c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
199 } else if (size == 0 && h == 8) {
200 c->qpel_put[1][dxy](c->temp , ref[0] + x + y*stride , stride);
201 c->qpel_put[1][dxy](c->temp + 8, ref[0] + x + y*stride + 8, stride);
202 } else
203 av_assert2(0);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6065785 times.
6065785 if(chroma){
205 int cx= hx/2;
206 int cy= hy/2;
207 cx= (cx>>1)|(cx&1);
208 cy= (cy>>1)|(cy&1);
209 uvdxy= (cx&1) + 2*(cy&1);
210 // 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
211 }
212 }else{
213 1918925 c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1918925 times.
1918925 if(chroma)
215 uvdxy= dxy | (x&1) | (2*(y&1));
216 }
217 7984710 d = cmp_func(s, c->temp, src[0], stride, h);
218 }else{
219 51548827 d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51548827 times.
51548827 if(chroma)
221 uvdxy= (x&1) + 2*(y&1);
222 }
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59533537 times.
59533537 if(chroma){
224 uint8_t * const uvtemp= c->temp + 16*stride;
225 c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
226 c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
227 d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
228 d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
229 }
230 59533537 return d;
231 }
232
233 static int cmp_simple(MpegEncContext *s, const int x, const int y,
234 int ref_index, int src_index,
235 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){
236 return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
237 }
238
239 static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,
240 const int size, const int h, int ref_index, int src_index,
241 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
242 if(flags&FLAG_DIRECT){
243 return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
244 }else{
245 return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
246 }
247 }
248
249 53990681 static int cmp_internal(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
250 const int size, const int h, int ref_index, int src_index,
251 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
252
2/2
✓ Branch 0 taken 1794244 times.
✓ Branch 1 taken 52196437 times.
53990681 if(flags&FLAG_DIRECT){
253 1794244 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
254 }else{
255 52196437 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);
256 }
257 }
258
259 /** @brief compares a block (either a full macroblock or a partition thereof)
260 against a proposed motion-compensated prediction of that block
261 */
262 53990681 static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
263 const int size, const int h, int ref_index, int src_index,
264 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
265 if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)
266 && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
267 && flags==0 && h==16 && size==0 && subx==0 && suby==0){
268 return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
269 }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
270 && subx==0 && suby==0){
271 return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
272 }else{
273 53990681 return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
274 }
275 }
276
277 2330660 static int cmp_hpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
278 const int size, const int h, int ref_index, int src_index,
279 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
280
2/2
✓ Branch 0 taken 598184 times.
✓ Branch 1 taken 1732476 times.
2330660 if(flags&FLAG_DIRECT){
281 598184 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
282 }else{
283 1732476 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
284 }
285 }
286
287 5878032 static int cmp_qpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
288 const int size, const int h, int ref_index, int src_index,
289 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
290
2/2
✓ Branch 0 taken 273408 times.
✓ Branch 1 taken 5604624 times.
5878032 if(flags&FLAG_DIRECT){
291 273408 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
292 }else{
293 5604624 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA);
294 }
295 }
296
297 #include "motion_est_template.c"
298
299 static int zero_cmp(MpegEncContext *s, const uint8_t *a, const uint8_t *b,
300 ptrdiff_t stride, int h)
301 {
302 return 0;
303 }
304
305 static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
306 }
307
308 11298 int ff_init_me(MpegEncContext *s){
309 11298 MotionEstContext * const c= &s->me;
310 11298 int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
311 11298 int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
312 int ret;
313
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
11298 if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)){
315 av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
316 return -1;
317 }
318
319 11298 c->avctx= s->avctx;
320
321
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 10998 times.
11298 if(s->codec_id == AV_CODEC_ID_H261)
322 300 c->avctx->me_sub_cmp = c->avctx->me_cmp;
323
324
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11298 if(cache_size < 2*dia_size && !c->stride){
325 av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
326 }
327
328 11298 ret = ff_set_cmp(&s->mecc, s->mecc.me_pre_cmp, c->avctx->me_pre_cmp);
329 11298 ret |= ff_set_cmp(&s->mecc, s->mecc.me_cmp, c->avctx->me_cmp);
330 11298 ret |= ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, c->avctx->me_sub_cmp);
331 11298 ret |= ff_set_cmp(&s->mecc, s->mecc.mb_cmp, c->avctx->mb_cmp);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
11298 if (ret < 0)
333 return ret;
334
335 11298 c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
336 11298 c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
337 11298 c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
338
339 /*FIXME s->no_rounding b_type*/
340
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 10770 times.
11298 if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
341 528 c->sub_motion_search= qpel_motion_search;
342 528 c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
343
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 429 times.
528 if (s->no_rounding)
344 99 c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
345 else
346 429 c->qpel_put = s->qdsp.put_qpel_pixels_tab;
347 }else{
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10770 times.
10770 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
349 c->sub_motion_search= hpel_motion_search;
350
2/2
✓ Branch 0 taken 9835 times.
✓ Branch 1 taken 935 times.
10770 else if( c->avctx->me_sub_cmp == FF_CMP_SAD
351
1/2
✓ Branch 0 taken 9835 times.
✗ Branch 1 not taken.
9835 && c->avctx-> me_cmp == FF_CMP_SAD
352
1/2
✓ Branch 0 taken 9835 times.
✗ Branch 1 not taken.
9835 && c->avctx-> mb_cmp == FF_CMP_SAD)
353 9835 c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
354 else
355 935 c->sub_motion_search= hpel_motion_search;
356 }
357 11298 c->hpel_avg = s->hdsp.avg_pixels_tab;
358
2/2
✓ Branch 0 taken 1673 times.
✓ Branch 1 taken 9625 times.
11298 if (s->no_rounding)
359 1673 c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
360 else
361 9625 c->hpel_put = s->hdsp.put_pixels_tab;
362
363
1/2
✓ Branch 0 taken 11298 times.
✗ Branch 1 not taken.
11298 if(s->linesize){
364 11298 c->stride = s->linesize;
365 11298 c->uvstride= s->uvlinesize;
366 }else{
367 c->stride = 16*s->mb_width + 32;
368 c->uvstride= 8*s->mb_width + 16;
369 }
370
371 /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
372 * not have yet, and even if we had, the motion estimation code
373 * does not expect it. */
374
1/2
✓ Branch 0 taken 11298 times.
✗ Branch 1 not taken.
11298 if (s->codec_id != AV_CODEC_ID_SNOW) {
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
11298 if ((c->avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
376 s->mecc.me_cmp[2] = zero_cmp;
377
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11298 if ((c->avctx->me_sub_cmp & FF_CMP_CHROMA) && !s->mecc.me_sub_cmp[2])
378 s->mecc.me_sub_cmp[2] = zero_cmp;
379 11298 c->hpel_put[2][0]= c->hpel_put[2][1]=
380 11298 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
381 }
382
383
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 10998 times.
11298 if(s->codec_id == AV_CODEC_ID_H261){
384 300 c->sub_motion_search= no_sub_motion_search;
385 }
386
387 11298 return 0;
388 }
389
390 #define CHECK_SAD_HALF_MV(suffix, x, y) \
391 {\
392 d = s->mecc.pix_abs[size][(x ? 1 : 0) + (y ? 2 : 0)](NULL, pix, ptr + ((x) >> 1), stride, h); \
393 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
394 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
395 }
396
397 4551619 static int sad_hpel_motion_search(MpegEncContext * s,
398 int *mx_ptr, int *my_ptr, int dmin,
399 int src_index, int ref_index,
400 int size, int h)
401 {
402 4551619 MotionEstContext * const c= &s->me;
403 4551619 const int penalty_factor= c->sub_penalty_factor;
404 int mx, my, dminh;
405 const uint8_t *pix, *ptr;
406 4551619 int stride= c->stride;
407 4551619 LOAD_COMMON
408
409 av_assert2(c->sub_flags == 0);
410
411
2/2
✓ Branch 0 taken 117172 times.
✓ Branch 1 taken 4434447 times.
4551619 if(c->skip){
412 117172 *mx_ptr = 0;
413 117172 *my_ptr = 0;
414 117172 return dmin;
415 }
416
417 4434447 pix = c->src[src_index][0];
418
419 4434447 mx = *mx_ptr;
420 4434447 my = *my_ptr;
421 4434447 ptr = c->ref[ref_index][0] + (my * stride) + mx;
422
423 4434447 dminh = dmin;
424
425
6/6
✓ Branch 0 taken 4359373 times.
✓ Branch 1 taken 75074 times.
✓ Branch 2 taken 4290030 times.
✓ Branch 3 taken 69343 times.
✓ Branch 4 taken 4200053 times.
✓ Branch 5 taken 89977 times.
4434447 if (mx > xmin && mx < xmax &&
426
2/2
✓ Branch 0 taken 4117764 times.
✓ Branch 1 taken 82289 times.
4200053 my > ymin && my < ymax) {
427 4117764 int dx=0, dy=0;
428 int d, pen_x, pen_y;
429 4117764 const int index= my*(1<<ME_MAP_SHIFT) + mx;
430 4117764 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
431 4117764 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
432 4117764 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
433 4117764 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
434 4117764 mx += mx;
435 4117764 my += my;
436
437
438 4117764 pen_x= pred_x + mx;
439 4117764 pen_y= pred_y + my;
440
441 4117764 ptr-= stride;
442
2/2
✓ Branch 0 taken 2091219 times.
✓ Branch 1 taken 2026545 times.
4117764 if(t<=b){
443 2091219 CHECK_SAD_HALF_MV(y2 , 0, -1)
444
2/2
✓ Branch 0 taken 1296074 times.
✓ Branch 1 taken 795145 times.
2091219 if(l<=r){
445 1296074 CHECK_SAD_HALF_MV(xy2, -1, -1)
446
2/2
✓ Branch 0 taken 755780 times.
✓ Branch 1 taken 540294 times.
1296074 if(t+r<=b+l){
447 755780 CHECK_SAD_HALF_MV(xy2, +1, -1)
448 755780 ptr+= stride;
449 }else{
450 540294 ptr+= stride;
451 540294 CHECK_SAD_HALF_MV(xy2, -1, +1)
452 }
453 1296074 CHECK_SAD_HALF_MV(x2 , -1, 0)
454 }else{
455 795145 CHECK_SAD_HALF_MV(xy2, +1, -1)
456
2/2
✓ Branch 0 taken 437175 times.
✓ Branch 1 taken 357970 times.
795145 if(t+l<=b+r){
457 437175 CHECK_SAD_HALF_MV(xy2, -1, -1)
458 437175 ptr+= stride;
459 }else{
460 357970 ptr+= stride;
461 357970 CHECK_SAD_HALF_MV(xy2, +1, +1)
462 }
463 795145 CHECK_SAD_HALF_MV(x2 , +1, 0)
464 }
465 }else{
466
2/2
✓ Branch 0 taken 777543 times.
✓ Branch 1 taken 1249002 times.
2026545 if(l<=r){
467
2/2
✓ Branch 0 taken 329315 times.
✓ Branch 1 taken 448228 times.
777543 if(t+l<=b+r){
468 329315 CHECK_SAD_HALF_MV(xy2, -1, -1)
469 329315 ptr+= stride;
470 }else{
471 448228 ptr+= stride;
472 448228 CHECK_SAD_HALF_MV(xy2, +1, +1)
473 }
474 777543 CHECK_SAD_HALF_MV(x2 , -1, 0)
475 777543 CHECK_SAD_HALF_MV(xy2, -1, +1)
476 }else{
477
2/2
✓ Branch 0 taken 556282 times.
✓ Branch 1 taken 692720 times.
1249002 if(t+r<=b+l){
478 556282 CHECK_SAD_HALF_MV(xy2, +1, -1)
479 556282 ptr+= stride;
480 }else{
481 692720 ptr+= stride;
482 692720 CHECK_SAD_HALF_MV(xy2, -1, +1)
483 }
484 1249002 CHECK_SAD_HALF_MV(x2 , +1, 0)
485 1249002 CHECK_SAD_HALF_MV(xy2, +1, +1)
486 }
487 2026545 CHECK_SAD_HALF_MV(y2 , 0, +1)
488 }
489 4117764 mx+=dx;
490 4117764 my+=dy;
491
492 }else{
493 316683 mx += mx;
494 316683 my += my;
495 }
496
497 4434447 *mx_ptr = mx;
498 4434447 *my_ptr = my;
499 4434447 return dminh;
500 }
501
502 2101111 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
503 {
504 2101111 const int xy= s->mb_x + s->mb_y*s->mb_stride;
505
506 2101111 s->p_mv_table[xy][0] = mx;
507 2101111 s->p_mv_table[xy][1] = my;
508
509 /* has already been set to the 4 MV if 4MV is done */
510
2/2
✓ Branch 0 taken 1849349 times.
✓ Branch 1 taken 251762 times.
2101111 if(mv4){
511 1849349 int mot_xy= s->block_index[0];
512
513 1849349 s->current_picture.motion_val[0][mot_xy ][0] = mx;
514 1849349 s->current_picture.motion_val[0][mot_xy ][1] = my;
515 1849349 s->current_picture.motion_val[0][mot_xy + 1][0] = mx;
516 1849349 s->current_picture.motion_val[0][mot_xy + 1][1] = my;
517
518 1849349 mot_xy += s->b8_stride;
519 1849349 s->current_picture.motion_val[0][mot_xy ][0] = mx;
520 1849349 s->current_picture.motion_val[0][mot_xy ][1] = my;
521 1849349 s->current_picture.motion_val[0][mot_xy + 1][0] = mx;
522 1849349 s->current_picture.motion_val[0][mot_xy + 1][1] = my;
523 }
524 2101111 }
525
526 /**
527 * get fullpel ME search limits.
528 */
529 3517233 static inline void get_limits(MpegEncContext *s, int x, int y)
530 {
531 3517233 MotionEstContext * const c= &s->me;
532
2/2
✓ Branch 0 taken 168764 times.
✓ Branch 1 taken 3348469 times.
3517233 int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
533
2/2
✓ Branch 0 taken 168764 times.
✓ Branch 1 taken 3348469 times.
3517233 int max_range = MAX_MV >> (1 + !!(c->flags&FLAG_QPEL));
534 /*
535 if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
536 else c->range= 16;
537 */
538
2/2
✓ Branch 0 taken 1724561 times.
✓ Branch 1 taken 1792672 times.
3517233 if (s->unrestricted_mv) {
539 1724561 c->xmin = - x - 16;
540 1724561 c->ymin = - y - 16;
541 1724561 c->xmax = - x + s->width;
542 1724561 c->ymax = - y + s->height;
543
2/2
✓ Branch 0 taken 106920 times.
✓ Branch 1 taken 1685752 times.
1792672 } else if (s->out_format == FMT_H261){
544 // Search range of H.261 is different from other codec standards
545
2/2
✓ Branch 0 taken 102060 times.
✓ Branch 1 taken 4860 times.
106920 c->xmin = (x > 15) ? - 15 : 0;
546
2/2
✓ Branch 0 taken 100980 times.
✓ Branch 1 taken 5940 times.
106920 c->ymin = (y > 15) ? - 15 : 0;
547
2/2
✓ Branch 0 taken 102060 times.
✓ Branch 1 taken 4860 times.
106920 c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
548
2/2
✓ Branch 0 taken 100980 times.
✓ Branch 1 taken 5940 times.
106920 c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
549 } else {
550 1685752 c->xmin = - x;
551 1685752 c->ymin = - y;
552 1685752 c->xmax = - x + s->mb_width *16 - 16;
553 1685752 c->ymax = - y + s->mb_height*16 - 16;
554 }
555
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3517233 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3517233 if(!range || range > max_range)
556 3517233 range = max_range;
557
1/2
✓ Branch 0 taken 3517233 times.
✗ Branch 1 not taken.
3517233 if(range){
558 3517233 c->xmin = FFMAX(c->xmin,-range);
559 3517233 c->xmax = FFMIN(c->xmax, range);
560 3517233 c->ymin = FFMAX(c->ymin,-range);
561 3517233 c->ymax = FFMIN(c->ymax, range);
562 }
563 3517233 }
564
565 251762 static inline void init_mv4_ref(MotionEstContext *c){
566 251762 const int stride= c->stride;
567
568 251762 c->ref[1][0] = c->ref[0][0] + 8;
569 251762 c->ref[2][0] = c->ref[0][0] + 8*stride;
570 251762 c->ref[3][0] = c->ref[2][0] + 8;
571 251762 c->src[1][0] = c->src[0][0] + 8;
572 251762 c->src[2][0] = c->src[0][0] + 8*stride;
573 251762 c->src[3][0] = c->src[2][0] + 8;
574 251762 }
575
576 251762 static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
577 {
578 251762 MotionEstContext * const c= &s->me;
579 251762 const int size= 1;
580 251762 const int h=8;
581 int block;
582 int P[10][2];
583 251762 int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
584 251762 int same=1;
585 251762 const int stride= c->stride;
586 251762 const uint8_t *mv_penalty = c->current_mv_penalty;
587
4/6
✓ Branch 0 taken 251762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2060 times.
✓ Branch 3 taken 249702 times.
✓ Branch 4 taken 2060 times.
✗ Branch 5 not taken.
251762 int safety_clipping= s->unrestricted_mv && (s->width&15) && (s->height&15);
588
589 251762 init_mv4_ref(c);
590
591
2/2
✓ Branch 0 taken 1007048 times.
✓ Branch 1 taken 251762 times.
1258810 for(block=0; block<4; block++){
592 int mx4, my4;
593 int pred_x4, pred_y4;
594 int dmin4;
595 static const int off[4]= {2, 1, 1, -1};
596 1007048 const int mot_stride = s->b8_stride;
597 1007048 const int mot_xy = s->block_index[block];
598
599
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 998808 times.
1007048 if(safety_clipping){
600 8240 c->xmax = - 16*s->mb_x + s->width - 8*(block &1);
601 8240 c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
602 }
603
604 1007048 P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
605 1007048 P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
606
607
2/2
✓ Branch 0 taken 1484 times.
✓ Branch 1 taken 1005564 times.
1007048 if (P_LEFT[0] > c->xmax * (1 << shift)) P_LEFT[0] = c->xmax * (1 << shift);
608
609 /* special case for first line */
610
4/4
✓ Branch 0 taken 62340 times.
✓ Branch 1 taken 944708 times.
✓ Branch 2 taken 31170 times.
✓ Branch 3 taken 31170 times.
1007048 if (s->first_slice_line && block<2) {
611 31170 c->pred_x= pred_x4= P_LEFT[0];
612 31170 c->pred_y= pred_y4= P_LEFT[1];
613 } else {
614 975878 P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
615 975878 P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
616 975878 P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
617 975878 P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
618
2/2
✓ Branch 0 taken 1450 times.
✓ Branch 1 taken 974428 times.
975878 if (P_TOP[1] > c->ymax * (1 << shift)) P_TOP[1] = c->ymax * (1 << shift);
619
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 975848 times.
975878 if (P_TOPRIGHT[0] < c->xmin * (1 << shift)) P_TOPRIGHT[0] = c->xmin * (1 << shift);
620
2/2
✓ Branch 0 taken 956 times.
✓ Branch 1 taken 974922 times.
975878 if (P_TOPRIGHT[0] > c->xmax * (1 << shift)) P_TOPRIGHT[0] = c->xmax * (1 << shift);
621
2/2
✓ Branch 0 taken 1392 times.
✓ Branch 1 taken 974486 times.
975878 if (P_TOPRIGHT[1] > c->ymax * (1 << shift)) P_TOPRIGHT[1] = c->ymax * (1 << shift);
622
623 975878 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
624 975878 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
625
626 975878 c->pred_x= pred_x4 = P_MEDIAN[0];
627 975878 c->pred_y= pred_y4 = P_MEDIAN[1];
628 }
629 1007048 P_MV1[0]= mx;
630 1007048 P_MV1[1]= my;
631
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 998808 times.
1007048 if(safety_clipping)
632
2/2
✓ Branch 0 taken 74160 times.
✓ Branch 1 taken 8240 times.
82400 for(i=1; i<10; i++){
633
8/8
✓ Branch 0 taken 26316 times.
✓ Branch 1 taken 47844 times.
✓ Branch 2 taken 13158 times.
✓ Branch 3 taken 13158 times.
✓ Branch 4 taken 11696 times.
✓ Branch 5 taken 1462 times.
✓ Branch 6 taken 10234 times.
✓ Branch 7 taken 1462 times.
74160 if (s->first_slice_line && block<2 && i>1 && i<9)
634 10234 continue;
635
4/4
✓ Branch 0 taken 35352 times.
✓ Branch 1 taken 28574 times.
✓ Branch 2 taken 27112 times.
✓ Branch 3 taken 8240 times.
63926 if (i>4 && i<9)
636 27112 continue;
637
2/2
✓ Branch 0 taken 1175 times.
✓ Branch 1 taken 35639 times.
36814 if (P[i][0] > c->xmax * (1 << shift)) P[i][0] = c->xmax * (1 << shift);
638
2/2
✓ Branch 0 taken 1434 times.
✓ Branch 1 taken 35380 times.
36814 if (P[i][1] > c->ymax * (1 << shift)) P[i][1] = c->ymax * (1 <<shift );
639 }
640
641 1007048 dmin4 = epzs_motion_search2(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift, 1);
642
643 1007048 dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
644
645
2/2
✓ Branch 0 taken 167508 times.
✓ Branch 1 taken 839540 times.
1007048 if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
646 int dxy;
647 167508 const int offset= ((block&1) + (block>>1)*stride)*8;
648 167508 uint8_t *dest_y = c->scratchpad + offset;
649
2/2
✓ Branch 0 taken 56744 times.
✓ Branch 1 taken 110764 times.
167508 if(s->quarter_sample){
650 56744 const uint8_t *ref = c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
651 56744 dxy = ((my4 & 3) << 2) | (mx4 & 3);
652
653
2/2
✓ Branch 0 taken 39304 times.
✓ Branch 1 taken 17440 times.
56744 if(s->no_rounding)
654 39304 s->qdsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y, ref, stride);
655 else
656 17440 s->qdsp.put_qpel_pixels_tab[1][dxy](dest_y, ref, stride);
657 }else{
658 110764 const uint8_t *ref = c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
659 110764 dxy = ((my4 & 1) << 1) | (mx4 & 1);
660
661
2/2
✓ Branch 0 taken 77060 times.
✓ Branch 1 taken 33704 times.
110764 if(s->no_rounding)
662 77060 s->hdsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
663 else
664 33704 s->hdsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
665 }
666 167508 dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
667 }else
668 839540 dmin_sum+= dmin4;
669
670
2/2
✓ Branch 0 taken 56744 times.
✓ Branch 1 taken 950304 times.
1007048 if(s->quarter_sample){
671 56744 mx4_sum+= mx4/2;
672 56744 my4_sum+= my4/2;
673 }else{
674 950304 mx4_sum+= mx4;
675 950304 my4_sum+= my4;
676 }
677
678 1007048 s->current_picture.motion_val[0][s->block_index[block]][0] = mx4;
679 1007048 s->current_picture.motion_val[0][s->block_index[block]][1] = my4;
680
681
4/4
✓ Branch 0 taken 535638 times.
✓ Branch 1 taken 471410 times.
✓ Branch 2 taken 129952 times.
✓ Branch 3 taken 405686 times.
1007048 if(mx4 != mx || my4 != my) same=0;
682 }
683
684
2/2
✓ Branch 0 taken 41703 times.
✓ Branch 1 taken 210059 times.
251762 if(same)
685 41703 return INT_MAX;
686
687
2/2
✓ Branch 0 taken 41458 times.
✓ Branch 1 taken 168601 times.
210059 if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
688 41458 dmin_sum += s->mecc.mb_cmp[0](s,
689 41458 s->new_picture->data[0] +
690 41458 s->mb_x * 16 + s->mb_y * 16 * stride,
691 41458 c->scratchpad, stride, 16);
692 }
693
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210059 times.
210059 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
695 int dxy;
696 int mx, my;
697 int offset;
698
699 mx= ff_h263_round_chroma(mx4_sum);
700 my= ff_h263_round_chroma(my4_sum);
701 dxy = ((my & 1) << 1) | (mx & 1);
702
703 offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
704
705 if(s->no_rounding){
706 s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.f->data[1] + offset, s->uvlinesize, 8);
707 s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f->data[2] + offset, s->uvlinesize, 8);
708 }else{
709 s->hdsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.f->data[1] + offset, s->uvlinesize, 8);
710 s->hdsp.put_pixels_tab [1][dxy](c->scratchpad + 8, s->last_picture.f->data[2] + offset, s->uvlinesize, 8);
711 }
712
713 dmin_sum += s->mecc.mb_cmp[1](s, s->new_picture->data[1] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad, s->uvlinesize, 8);
714 dmin_sum += s->mecc.mb_cmp[1](s, s->new_picture->data[2] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad + 8, s->uvlinesize, 8);
715 }
716
717 210059 c->pred_x= mx;
718 210059 c->pred_y= my;
719
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210059 times.
210059 switch(c->avctx->mb_cmp&0xFF){
721 /*case FF_CMP_SSE:
722 return dmin_sum+ 32*s->qscale*s->qscale;*/
723 case FF_CMP_RD:
724 return dmin_sum;
725 210059 default:
726 210059 return dmin_sum+ 11*c->mb_penalty_factor;
727 }
728 }
729
730 331233 static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
731 331233 MotionEstContext * const c= &s->me;
732
733 331233 c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
734 331233 c->src[1][0] = c->src[0][0] + s->linesize;
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 331233 times.
331233 if(c->flags & FLAG_CHROMA){
736 c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
737 c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
738 c->src[1][1] = c->src[0][1] + s->uvlinesize;
739 c->src[1][2] = c->src[0][2] + s->uvlinesize;
740 }
741 331233 }
742
743 331233 static int interlaced_search(MpegEncContext *s, int ref_index,
744 int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
745 {
746 331233 MotionEstContext * const c= &s->me;
747 331233 const int size=0;
748 331233 const int h=8;
749 int block;
750 int P[10][2];
751 331233 const uint8_t * const mv_penalty = c->current_mv_penalty;
752 331233 int same=1;
753 331233 const int stride= 2*s->linesize;
754 331233 int dmin_sum= 0;
755 331233 const int mot_stride= s->mb_stride;
756 331233 const int xy= s->mb_x + s->mb_y*mot_stride;
757
758 331233 c->ymin>>=1;
759 331233 c->ymax>>=1;
760 331233 c->stride<<=1;
761 331233 c->uvstride<<=1;
762 331233 init_interlaced_ref(s, ref_index);
763
764
2/2
✓ Branch 0 taken 662466 times.
✓ Branch 1 taken 331233 times.
993699 for(block=0; block<2; block++){
765 int field_select;
766 662466 int best_dmin= INT_MAX;
767 662466 int best_field= -1;
768
769
2/2
✓ Branch 0 taken 1324932 times.
✓ Branch 1 taken 662466 times.
1987398 for(field_select=0; field_select<2; field_select++){
770 int dmin, mx_i, my_i;
771 1324932 int16_t (*mv_table)[2]= mv_tables[block][field_select];
772
773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if(user_field_select){
774 av_assert1(field_select==0 || field_select==1);
775 av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
776 if(field_select_tables[block][xy] != field_select)
777 continue;
778 }
779
780 1324932 P_LEFT[0] = mv_table[xy - 1][0];
781 1324932 P_LEFT[1] = mv_table[xy - 1][1];
782
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);
783
784 1324932 c->pred_x= P_LEFT[0];
785 1324932 c->pred_y= P_LEFT[1];
786
787
2/2
✓ Branch 0 taken 1206192 times.
✓ Branch 1 taken 118740 times.
1324932 if(!s->first_slice_line){
788 1206192 P_TOP[0] = mv_table[xy - mot_stride][0];
789 1206192 P_TOP[1] = mv_table[xy - mot_stride][1];
790 1206192 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
791 1206192 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
792
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);
793
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);
794
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);
795
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);
796
797 1206192 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
798 1206192 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
799 }
800 1324932 P_MV1[0]= mx; //FIXME not correct if block != field_select
801 1324932 P_MV1[1]= my / 2;
802
803 1324932 dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1, 0);
804
805 1324932 dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
806
807 1324932 mv_table[xy][0]= mx_i;
808 1324932 mv_table[xy][1]= my_i;
809
810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
811 int dxy;
812
813 //FIXME chroma ME
814 const uint8_t *ref = c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
815 dxy = ((my_i & 1) << 1) | (mx_i & 1);
816
817 if(s->no_rounding){
818 s->hdsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
819 }else{
820 s->hdsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
821 }
822 dmin = s->mecc.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
823 dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
824 }else
825 1324932 dmin+= c->mb_penalty_factor; //field_select bits
826
827 1324932 dmin += field_select != block; //slightly prefer same field
828
829
2/2
✓ Branch 0 taken 994745 times.
✓ Branch 1 taken 330187 times.
1324932 if(dmin < best_dmin){
830 994745 best_dmin= dmin;
831 994745 best_field= field_select;
832 }
833 }
834 {
835 662466 int16_t (*mv_table)[2]= mv_tables[block][best_field];
836
837
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
838
2/2
✓ Branch 0 taken 59142 times.
✓ Branch 1 taken 603324 times.
662466 if(mv_table[xy][1]&1) same=0;
839
2/2
✓ Branch 0 taken 444919 times.
✓ Branch 1 taken 217547 times.
662466 if(mv_table[xy][1]*2 != my) same=0;
840
2/2
✓ Branch 0 taken 318666 times.
✓ Branch 1 taken 343800 times.
662466 if(best_field != block) same=0;
841 }
842
843 662466 field_select_tables[block][xy]= best_field;
844 662466 dmin_sum += best_dmin;
845 }
846
847 331233 c->ymin *= 2;
848 331233 c->ymax<<=1;
849 331233 c->stride>>=1;
850 331233 c->uvstride>>=1;
851
852
2/2
✓ Branch 0 taken 55300 times.
✓ Branch 1 taken 275933 times.
331233 if(same)
853 55300 return INT_MAX;
854
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275933 times.
275933 switch(c->avctx->mb_cmp&0xFF){
856 /*case FF_CMP_SSE:
857 return dmin_sum+ 32*s->qscale*s->qscale;*/
858 case FF_CMP_RD:
859 return dmin_sum;
860 275933 default:
861 275933 return dmin_sum+ 11*c->mb_penalty_factor;
862 }
863 }
864
865 8752509 static inline int get_penalty_factor(int lambda, int lambda2, int type){
866
3/7
✓ Branch 0 taken 7853666 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 552909 times.
✓ Branch 5 taken 345934 times.
✗ Branch 6 not taken.
8752509 switch(type&0xFF){
867 7853666 default:
868 case FF_CMP_SAD:
869 7853666 return lambda>>FF_LAMBDA_SHIFT;
870 case FF_CMP_DCT:
871 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
872 case FF_CMP_W53:
873 return (4*lambda)>>(FF_LAMBDA_SHIFT);
874 case FF_CMP_W97:
875 return (2*lambda)>>(FF_LAMBDA_SHIFT);
876 552909 case FF_CMP_SATD:
877 case FF_CMP_DCT264:
878 552909 return (2*lambda)>>FF_LAMBDA_SHIFT;
879 345934 case FF_CMP_RD:
880 case FF_CMP_PSNR:
881 case FF_CMP_SSE:
882 case FF_CMP_NSSE:
883 345934 return lambda2>>FF_LAMBDA_SHIFT;
884 case FF_CMP_BIT:
885 case FF_CMP_MEDIAN_SAD:
886 return 1;
887 }
888 }
889
890 2101111 void ff_estimate_p_frame_motion(MpegEncContext * s,
891 int mb_x, int mb_y)
892 {
893 2101111 MotionEstContext * const c= &s->me;
894 const uint8_t *pix, *ppix;
895 2101111 int sum, mx = 0, my = 0, dmin = 0;
896 int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
897 int vard; ///< sum of squared differences with the estimated motion vector
898 int P[10][2];
899 2101111 const int shift= 1+s->quarter_sample;
900 2101111 int mb_type=0;
901
902 2101111 init_ref(c, s->new_picture->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);
903
904
3/4
✓ Branch 0 taken 15570 times.
✓ Branch 1 taken 2085541 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15570 times.
2101111 av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2101111 times.
2101111 av_assert0(s->linesize == c->stride);
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2101111 times.
2101111 av_assert0(s->uvlinesize == c->uvstride);
907
908 2101111 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
909 2101111 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
910 2101111 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
911 2101111 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
912
913 2101111 get_limits(s, 16*mb_x, 16*mb_y);
914 2101111 c->skip=0;
915
916 /* intra / predictive decision */
917 2101111 pix = c->src[0][0];
918 2101111 sum = s->mpvencdsp.pix_sum(pix, s->linesize);
919 2101111 varc = s->mpvencdsp.pix_norm1(pix, s->linesize) -
920 2101111 (((unsigned) sum * sum) >> 8) + 500;
921
922 2101111 s->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
923 2101111 s->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
924 2101111 c->mb_var_sum_temp += (varc+128)>>8;
925
926
1/2
✓ Branch 0 taken 2101111 times.
✗ Branch 1 not taken.
2101111 if (s->motion_est != FF_ME_ZERO) {
927 2101111 const int mot_stride = s->b8_stride;
928 2101111 const int mot_xy = s->block_index[0];
929
930 2101111 P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
931 2101111 P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
932
933
2/2
✓ Branch 0 taken 20878 times.
✓ Branch 1 taken 2080233 times.
2101111 if (P_LEFT[0] > (c->xmax << shift))
934 20878 P_LEFT[0] = c->xmax << shift;
935
936
2/2
✓ Branch 0 taken 1941131 times.
✓ Branch 1 taken 159980 times.
2101111 if (!s->first_slice_line) {
937 1941131 P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
938 1941131 P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
939 1941131 P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
940 1941131 P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
941
2/2
✓ Branch 0 taken 23485 times.
✓ Branch 1 taken 1917646 times.
1941131 if (P_TOP[1] > (c->ymax << shift))
942 23485 P_TOP[1] = c->ymax << shift;
943
2/2
✓ Branch 0 taken 22385 times.
✓ Branch 1 taken 1918746 times.
1941131 if (P_TOPRIGHT[0] < (c->xmin * (1 << shift)))
944 22385 P_TOPRIGHT[0] = c->xmin * (1 << shift);
945
2/2
✓ Branch 0 taken 21671 times.
✓ Branch 1 taken 1919460 times.
1941131 if (P_TOPRIGHT[1] > (c->ymax * (1 << shift)))
946 21671 P_TOPRIGHT[1] = c->ymax * (1 << shift);
947
948 1941131 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
949 1941131 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
950
951
2/2
✓ Branch 0 taken 1112641 times.
✓ Branch 1 taken 828490 times.
1941131 if (s->out_format == FMT_H263) {
952 1112641 c->pred_x = P_MEDIAN[0];
953 1112641 c->pred_y = P_MEDIAN[1];
954 } else { /* MPEG-1 at least */
955 828490 c->pred_x = P_LEFT[0];
956 828490 c->pred_y = P_LEFT[1];
957 }
958 } else {
959 159980 c->pred_x = P_LEFT[0];
960 159980 c->pred_y = P_LEFT[1];
961 }
962 2101111 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
963 }
964
965 /* At this point (mx,my) are full-pell and the relative displacement */
966 2101111 ppix = c->ref[0][0] + (my * s->linesize) + mx;
967
968 2101111 vard = s->mecc.sse[0](NULL, pix, ppix, s->linesize, 16);
969
970 2101111 s->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
971 2101111 c->mc_mb_var_sum_temp += (vard+128)>>8;
972
973
2/2
✓ Branch 0 taken 362361 times.
✓ Branch 1 taken 1738750 times.
2101111 if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {
974 362361 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
975 362361 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
976 362361 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
977
978
3/4
✓ Branch 0 taken 97659 times.
✓ Branch 1 taken 264702 times.
✓ Branch 2 taken 97659 times.
✗ Branch 3 not taken.
362361 if (vard*2 + 200*256 > varc && !s->intra_penalty)
979 97659 mb_type|= CANDIDATE_MB_TYPE_INTRA;
980
3/4
✓ Branch 0 taken 2107 times.
✓ Branch 1 taken 360254 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2107 times.
362361 if (varc*2 + 200*256 > vard || s->qscale > 24){
981 // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
982 360254 mb_type|= CANDIDATE_MB_TYPE_INTER;
983 360254 c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
984
2/2
✓ Branch 0 taken 61156 times.
✓ Branch 1 taken 299098 times.
360254 if (s->mpv_flags & FF_MPV_FLAG_MV0)
985
4/4
✓ Branch 0 taken 4055 times.
✓ Branch 1 taken 57101 times.
✓ Branch 2 taken 3527 times.
✓ Branch 3 taken 528 times.
61156 if(mx || my)
986 60628 mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
987 }else{
988 2107 mx *= 1 << shift;
989 2107 my *= 1 << shift;
990 }
991
2/2
✓ Branch 0 taken 277722 times.
✓ Branch 1 taken 84639 times.
362361 if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
992
6/6
✓ Branch 0 taken 277715 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 260928 times.
✓ Branch 3 taken 16787 times.
✓ Branch 4 taken 251762 times.
✓ Branch 5 taken 9166 times.
277722 && !c->skip && varc>50<<8 && vard>10<<8){
993
2/2
✓ Branch 1 taken 210059 times.
✓ Branch 2 taken 41703 times.
251762 if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
994 210059 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
995
996 251762 set_p_mv_tables(s, mx, my, 0);
997 }else
998 110599 set_p_mv_tables(s, mx, my, 1);
999
2/2
✓ Branch 0 taken 15600 times.
✓ Branch 1 taken 346761 times.
362361 if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1000
1/2
✓ Branch 0 taken 15600 times.
✗ Branch 1 not taken.
15600 && !c->skip){ //FIXME varc/d checks
1001
2/2
✓ Branch 1 taken 13535 times.
✓ Branch 2 taken 2065 times.
15600 if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
1002 13535 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1003 }
1004 }else{
1005 int intra_score, i;
1006 1738750 mb_type= CANDIDATE_MB_TYPE_INTER;
1007
1008 1738750 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1009
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1738750 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1738750 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1010 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1011
1012
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1738750 times.
1738750 if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
1013 && !c->skip && varc>50<<8 && vard>10<<8){
1014 int dmin4= h263_mv4_search(s, mx, my, shift);
1015 if(dmin4 < dmin){
1016 mb_type= CANDIDATE_MB_TYPE_INTER4V;
1017 dmin=dmin4;
1018 }
1019 }
1020
2/2
✓ Branch 0 taken 85236 times.
✓ Branch 1 taken 1653514 times.
1738750 if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1021
2/2
✓ Branch 0 taken 85233 times.
✓ Branch 1 taken 3 times.
85236 && !c->skip){ //FIXME varc/d checks
1022 85233 int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
1023
2/2
✓ Branch 0 taken 11208 times.
✓ Branch 1 taken 74025 times.
85233 if(dmin_i < dmin){
1024 11208 mb_type = CANDIDATE_MB_TYPE_INTER_I;
1025 11208 dmin= dmin_i;
1026 }
1027 }
1028
1029 1738750 set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1030
1031 /* get intra luma score */
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1738750 times.
1738750 if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1033 intra_score= varc - 500;
1034 }else{
1035 1738750 unsigned mean = (sum+128)>>8;
1036 1738750 mean*= 0x01010101;
1037
1038
2/2
✓ Branch 0 taken 27820000 times.
✓ Branch 1 taken 1738750 times.
29558750 for(i=0; i<16; i++){
1039 27820000 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
1040 27820000 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
1041 27820000 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
1042 27820000 *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
1043 }
1044
1045 1738750 intra_score= s->mecc.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
1046 }
1047 1738750 intra_score += c->mb_penalty_factor*16 + s->intra_penalty;
1048
1049
2/2
✓ Branch 0 taken 90312 times.
✓ Branch 1 taken 1648438 times.
1738750 if(intra_score < dmin){
1050 90312 mb_type= CANDIDATE_MB_TYPE_INTRA;
1051 90312 s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1052 }else
1053 1648438 s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = 0;
1054
1055 {
1056 1738750 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1057 1738750 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1058 1738750 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1059 }
1060 }
1061
1062 2101111 s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1063 2101111 }
1064
1065 int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1066 int mb_x, int mb_y)
1067 {
1068 MotionEstContext * const c= &s->me;
1069 int mx, my, dmin;
1070 int P[10][2];
1071 const int shift= 1+s->quarter_sample;
1072 const int xy= mb_x + mb_y*s->mb_stride;
1073 init_ref(c, s->new_picture->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);
1074
1075 av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
1076
1077 c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1078 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
1079
1080 get_limits(s, 16*mb_x, 16*mb_y);
1081 c->skip=0;
1082
1083 P_LEFT[0] = s->p_mv_table[xy + 1][0];
1084 P_LEFT[1] = s->p_mv_table[xy + 1][1];
1085
1086 if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
1087
1088 /* special case for first line */
1089 if (s->first_slice_line) {
1090 c->pred_x= P_LEFT[0];
1091 c->pred_y= P_LEFT[1];
1092 P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1093 P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
1094 } else {
1095 P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
1096 P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
1097 P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
1098 P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
1099 if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
1100 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1101 if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1102
1103 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1104 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1105
1106 c->pred_x = P_MEDIAN[0];
1107 c->pred_y = P_MEDIAN[1];
1108 }
1109
1110 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1111
1112 s->p_mv_table[xy][0] = mx<<shift;
1113 s->p_mv_table[xy][1] = my<<shift;
1114
1115 return dmin;
1116 }
1117
1118 816392 static int estimate_motion_b(MpegEncContext *s, int mb_x, int mb_y,
1119 int16_t (*mv_table)[2], int ref_index, int f_code)
1120 {
1121 816392 MotionEstContext * const c= &s->me;
1122 816392 int mx = 0, my = 0, dmin = 0;
1123 int P[10][2];
1124 816392 const int shift= 1+s->quarter_sample;
1125 816392 const int mot_stride = s->mb_stride;
1126 816392 const int mot_xy = mb_y*mot_stride + mb_x;
1127 816392 const uint8_t * const mv_penalty = c->mv_penalty[f_code] + MAX_DMV;
1128 int mv_scale;
1129
1130 816392 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1131 816392 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1132 816392 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1133 816392 c->current_mv_penalty= mv_penalty;
1134
1135 816392 get_limits(s, 16*mb_x, 16*mb_y);
1136
1137
1/2
✓ Branch 0 taken 816392 times.
✗ Branch 1 not taken.
816392 if (s->motion_est != FF_ME_ZERO) {
1138 816392 P_LEFT[0] = mv_table[mot_xy - 1][0];
1139 816392 P_LEFT[1] = mv_table[mot_xy - 1][1];
1140
1141
2/2
✓ Branch 0 taken 10171 times.
✓ Branch 1 taken 806221 times.
816392 if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
1142
1143 /* special case for first line */
1144
2/2
✓ Branch 0 taken 756200 times.
✓ Branch 1 taken 60192 times.
816392 if (!s->first_slice_line) {
1145 756200 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1146 756200 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1147 756200 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
1148 756200 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
1149
2/2
✓ Branch 0 taken 11692 times.
✓ Branch 1 taken 744508 times.
756200 if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
1150
2/2
✓ Branch 0 taken 8206 times.
✓ Branch 1 taken 747994 times.
756200 if (P_TOPRIGHT[0] < c->xmin * (1 << shift)) P_TOPRIGHT[0] = c->xmin * (1 << shift);
1151
2/2
✓ Branch 0 taken 11033 times.
✓ Branch 1 taken 745167 times.
756200 if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
1152
1153 756200 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1154 756200 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1155 }
1156 816392 c->pred_x = P_LEFT[0];
1157 816392 c->pred_y = P_LEFT[1];
1158
1159
2/2
✓ Branch 0 taken 408196 times.
✓ Branch 1 taken 408196 times.
816392 if(mv_table == s->b_forw_mv_table){
1160 408196 mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1161 }else{
1162 408196 mv_scale = ((s->pb_time - s->pp_time) * (1 << 16)) / (s->pp_time<<shift);
1163 }
1164
1165 816392 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1166 }
1167
1168 816392 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1169
1170
3/4
✓ Branch 0 taken 306344 times.
✓ Branch 1 taken 510048 times.
✓ Branch 2 taken 306344 times.
✗ Branch 3 not taken.
816392 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1171 306344 dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1172
1173 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1174 816392 mv_table[mot_xy][0]= mx;
1175 816392 mv_table[mot_xy][1]= my;
1176
1177 816392 return dmin;
1178 }
1179
1180 5439189 static inline int check_bidir_mv(MpegEncContext * s,
1181 int motion_fx, int motion_fy,
1182 int motion_bx, int motion_by,
1183 int pred_fx, int pred_fy,
1184 int pred_bx, int pred_by,
1185 int size, int h)
1186 {
1187 //FIXME optimize?
1188 //FIXME better f_code prediction (max mv & distance)
1189 //FIXME pointers
1190 5439189 MotionEstContext * const c= &s->me;
1191 5439189 const uint8_t * const mv_penalty_f = c->mv_penalty[s->f_code] + MAX_DMV; // f_code of the prev frame
1192 5439189 const uint8_t * const mv_penalty_b = c->mv_penalty[s->b_code] + MAX_DMV; // f_code of the prev frame
1193 5439189 int stride= c->stride;
1194 5439189 uint8_t *dest_y = c->scratchpad;
1195 const uint8_t *ptr;
1196 int dxy;
1197 int src_x, src_y;
1198 int fbmin;
1199 5439189 const uint8_t *const *src_data = c->src[0];
1200 5439189 const uint8_t *const *ref_data = c->ref[0];
1201 5439189 const uint8_t *const *ref2_data = c->ref[2];
1202
1203
2/2
✓ Branch 0 taken 625749 times.
✓ Branch 1 taken 4813440 times.
5439189 if(s->quarter_sample){
1204 625749 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1205 625749 src_x = motion_fx >> 2;
1206 625749 src_y = motion_fy >> 2;
1207
1208 625749 ptr = ref_data[0] + (src_y * stride) + src_x;
1209 625749 s->qdsp.put_qpel_pixels_tab[0][dxy](dest_y, ptr, stride);
1210
1211 625749 dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1212 625749 src_x = motion_bx >> 2;
1213 625749 src_y = motion_by >> 2;
1214
1215 625749 ptr = ref2_data[0] + (src_y * stride) + src_x;
1216 625749 s->qdsp.avg_qpel_pixels_tab[size][dxy](dest_y, ptr, stride);
1217 }else{
1218 4813440 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1219 4813440 src_x = motion_fx >> 1;
1220 4813440 src_y = motion_fy >> 1;
1221
1222 4813440 ptr = ref_data[0] + (src_y * stride) + src_x;
1223 4813440 s->hdsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1224
1225 4813440 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1226 4813440 src_x = motion_bx >> 1;
1227 4813440 src_y = motion_by >> 1;
1228
1229 4813440 ptr = ref2_data[0] + (src_y * stride) + src_x;
1230 4813440 s->hdsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1231 }
1232
1233 10878378 fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
1234 5439189 +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
1235 5439189 + s->mecc.mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
1236
1237 5439189 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1238 }
1239 //FIXME CHROMA !!!
1240
1241 5439189 return fbmin;
1242 }
1243
1244 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1245 408196 static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
1246 {
1247 408196 MotionEstContext * const c= &s->me;
1248 408196 const int mot_stride = s->mb_stride;
1249 408196 const int xy = mb_y *mot_stride + mb_x;
1250 int fbmin;
1251 408196 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1252 408196 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1253 408196 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1254 408196 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1255 408196 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1256 408196 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1257 408196 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1258 408196 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1259 408196 const int flags= c->sub_flags;
1260 408196 const int qpel= flags&FLAG_QPEL;
1261 408196 const int shift= 1+qpel;
1262 408196 const int xmin= c->xmin * (1 << shift);
1263 408196 const int ymin= c->ymin * (1 << shift);
1264 408196 const int xmax= c->xmax<<shift;
1265 408196 const int ymax= c->ymax<<shift;
1266 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
1267 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
1268 408196 int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
1269 408196 uint8_t map[256] = { 0 };
1270
1271 408196 map[hashidx&255] = 1;
1272
1273 408196 fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1274 motion_bx, motion_by,
1275 pred_fx, pred_fy,
1276 pred_bx, pred_by,
1277 0, 16);
1278
1279
1/2
✓ Branch 0 taken 408196 times.
✗ Branch 1 not taken.
408196 if(s->avctx->bidir_refine){
1280 int end;
1281 static const uint8_t limittab[5]={0,8,32,64,80};
1282 408196 const int limit= limittab[s->avctx->bidir_refine];
1283 static const int8_t vect[][4]={
1284 { 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},
1285
1286 { 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},
1287 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
1288 { 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},
1289 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
1290
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 { 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},
1295
1296 { 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}, {-1, 1, 1, 1}, { 1,-1,-1,-1},
1298 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
1299 };
1300 static const uint8_t hash[]={
1301 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),
1302
1303 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),
1304 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
1305 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),
1306 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
1307
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 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),
1312
1313 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), HASH8(-1, 1, 1, 1), HASH8( 1,-1,-1,-1),
1315 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),
1316 };
1317
1318 #define CHECK_BIDIR(fx,fy,bx,by)\
1319 if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
1320 &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1321 &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1322 int score;\
1323 map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
1324 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);\
1325 if(score < fbmin){\
1326 hashidx += HASH(fx,fy,bx,by);\
1327 fbmin= score;\
1328 motion_fx+=fx;\
1329 motion_fy+=fy;\
1330 motion_bx+=bx;\
1331 motion_by+=by;\
1332 end=0;\
1333 }\
1334 }
1335 #define CHECK_BIDIR2(a,b,c,d)\
1336 CHECK_BIDIR(a,b,c,d)\
1337 CHECK_BIDIR(-(a),-(b),-(c),-(d))
1338
1339 do{
1340 int i;
1341 827905 int borderdist=0;
1342 827905 end=1;
1343
1344
12/12
✓ Branch 0 taken 775035 times.
✓ Branch 1 taken 52870 times.
✓ Branch 2 taken 762789 times.
✓ Branch 3 taken 12246 times.
✓ Branch 5 taken 95125 times.
✓ Branch 6 taken 667664 times.
✓ Branch 7 taken 696201 times.
✓ Branch 8 taken 131704 times.
✓ Branch 9 taken 683775 times.
✓ Branch 10 taken 12426 times.
✓ Branch 12 taken 113558 times.
✓ Branch 13 taken 570217 times.
827905 CHECK_BIDIR2(0,0,0,1)
1345
12/12
✓ Branch 0 taken 711796 times.
✓ Branch 1 taken 116109 times.
✓ Branch 2 taken 700835 times.
✓ Branch 3 taken 10961 times.
✓ Branch 5 taken 101198 times.
✓ Branch 6 taken 599637 times.
✓ Branch 7 taken 614029 times.
✓ Branch 8 taken 213876 times.
✓ Branch 9 taken 604221 times.
✓ Branch 10 taken 9808 times.
✓ Branch 12 taken 82972 times.
✓ Branch 13 taken 521249 times.
827905 CHECK_BIDIR2(0,0,1,0)
1346
12/12
✓ Branch 0 taken 658282 times.
✓ Branch 1 taken 169623 times.
✓ Branch 2 taken 641388 times.
✓ Branch 3 taken 16894 times.
✓ Branch 5 taken 92701 times.
✓ Branch 6 taken 548687 times.
✓ Branch 7 taken 557918 times.
✓ Branch 8 taken 269987 times.
✓ Branch 9 taken 540803 times.
✓ Branch 10 taken 17115 times.
✓ Branch 12 taken 60135 times.
✓ Branch 13 taken 480668 times.
827905 CHECK_BIDIR2(0,1,0,0)
1347
12/12
✓ Branch 0 taken 590331 times.
✓ Branch 1 taken 237574 times.
✓ Branch 2 taken 574724 times.
✓ Branch 3 taken 15607 times.
✓ Branch 5 taken 66385 times.
✓ Branch 6 taken 508339 times.
✓ Branch 7 taken 538436 times.
✓ Branch 8 taken 289469 times.
✓ Branch 9 taken 522458 times.
✓ Branch 10 taken 15978 times.
✓ Branch 12 taken 72685 times.
✓ Branch 13 taken 449773 times.
827905 CHECK_BIDIR2(1,0,0,0)
1348
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 827905 times.
827905 for(i=8; i<limit; i++){
1350 int fx= motion_fx+vect[i][0];
1351 int fy= motion_fy+vect[i][1];
1352 int bx= motion_bx+vect[i][2];
1353 int by= motion_by+vect[i][3];
1354 if(borderdist<=0){
1355 int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
1356 int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
1357 if((a|b) < 0)
1358 map[(hashidx+hash[i])&255] = 1;
1359 }
1360 if(!map[(hashidx+hash[i])&255]){
1361 int score;
1362 map[(hashidx+hash[i])&255] = 1;
1363 score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
1364 if(score < fbmin){
1365 hashidx += hash[i];
1366 fbmin= score;
1367 motion_fx=fx;
1368 motion_fy=fy;
1369 motion_bx=bx;
1370 motion_by=by;
1371 end=0;
1372 borderdist--;
1373 if(borderdist<=0){
1374 int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
1375 int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
1376 borderdist= FFMIN(a,b);
1377 }
1378 }
1379 }
1380 }
1381
2/2
✓ Branch 0 taken 419709 times.
✓ Branch 1 taken 408196 times.
827905 }while(!end);
1382 }
1383
1384 408196 s->b_bidir_forw_mv_table[xy][0]= motion_fx;
1385 408196 s->b_bidir_forw_mv_table[xy][1]= motion_fy;
1386 408196 s->b_bidir_back_mv_table[xy][0]= motion_bx;
1387 408196 s->b_bidir_back_mv_table[xy][1]= motion_by;
1388
1389 408196 return fbmin;
1390 }
1391
1392 191520 static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
1393 {
1394 191520 MotionEstContext * const c= &s->me;
1395 int P[10][2];
1396 191520 const int mot_stride = s->mb_stride;
1397 191520 const int mot_xy = mb_y*mot_stride + mb_x;
1398 191520 const int shift= 1+s->quarter_sample;
1399 int dmin, i;
1400 191520 const int time_pp= s->pp_time;
1401 191520 const int time_pb= s->pb_time;
1402 int mx, my, xmin, xmax, ymin, ymax;
1403 191520 int16_t (*mv_table)[2]= s->b_direct_mv_table;
1404
1405 191520 c->current_mv_penalty= c->mv_penalty[1] + MAX_DMV;
1406 191520 ymin= xmin=(-32)>>shift;
1407 191520 ymax= xmax= 31>>shift;
1408
1409
2/2
✓ Branch 0 taken 54658 times.
✓ Branch 1 taken 136862 times.
191520 if (IS_8X8(s->next_picture.mb_type[mot_xy])) {
1410 54658 s->mv_type= MV_TYPE_8X8;
1411 }else{
1412 136862 s->mv_type= MV_TYPE_16X16;
1413 }
1414
1415
2/2
✓ Branch 0 taken 355494 times.
✓ Branch 1 taken 54658 times.
410152 for(i=0; i<4; i++){
1416 355494 int index= s->block_index[i];
1417 int min, max;
1418
1419 355494 c->co_located_mv[i][0] = s->next_picture.motion_val[0][index][0];
1420 355494 c->co_located_mv[i][1] = s->next_picture.motion_val[0][index][1];
1421 355494 c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1422 355494 c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1423 // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1424 // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1425
1426
2/2
✓ Branch 0 taken 139772 times.
✓ Branch 1 taken 215722 times.
355494 max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1427
2/2
✓ Branch 0 taken 139772 times.
✓ Branch 1 taken 215722 times.
355494 min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1428 355494 max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1429 355494 min+= 16*mb_x - 1;
1430 355494 xmax= FFMIN(xmax, s->width - max);
1431 355494 xmin= FFMAX(xmin, - 16 - min);
1432
1433
2/2
✓ Branch 0 taken 141358 times.
✓ Branch 1 taken 214136 times.
355494 max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1434
2/2
✓ Branch 0 taken 141358 times.
✓ Branch 1 taken 214136 times.
355494 min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1435 355494 max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1436 355494 min+= 16*mb_y - 1;
1437 355494 ymax= FFMIN(ymax, s->height - max);
1438 355494 ymin= FFMAX(ymin, - 16 - min);
1439
1440
2/2
✓ Branch 0 taken 136862 times.
✓ Branch 1 taken 218632 times.
355494 if(s->mv_type == MV_TYPE_16X16) break;
1441 }
1442
1443 av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1444
1445
7/8
✓ Branch 0 taken 191455 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 191453 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 191418 times.
✓ Branch 5 taken 35 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 191418 times.
191520 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1446 102 s->b_direct_mv_table[mot_xy][0]= 0;
1447 102 s->b_direct_mv_table[mot_xy][1]= 0;
1448
1449 102 return 256*256*256*64;
1450 }
1451
1452 191418 c->xmin= xmin;
1453 191418 c->ymin= ymin;
1454 191418 c->xmax= xmax;
1455 191418 c->ymax= ymax;
1456 191418 c->flags |= FLAG_DIRECT;
1457 191418 c->sub_flags |= FLAG_DIRECT;
1458 191418 c->pred_x=0;
1459 191418 c->pred_y=0;
1460
1461 191418 P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin * (1 << shift), xmax << shift);
1462 191418 P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin * (1 << shift), ymax << shift);
1463
1464 /* special case for first line */
1465
2/2
✓ Branch 0 taken 178176 times.
✓ Branch 1 taken 13242 times.
191418 if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
1466 178176 P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin * (1 << shift), xmax << shift);
1467 178176 P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin * (1 << shift), ymax << shift);
1468 178176 P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1][0], xmin * (1 << shift), xmax << shift);
1469 178176 P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1][1], ymin * (1 << shift), ymax << shift);
1470
1471 178176 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1472 178176 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1473 }
1474
1475 191418 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1476
2/2
✓ Branch 0 taken 38282 times.
✓ Branch 1 taken 153136 times.
191418 if(c->sub_flags&FLAG_QPEL)
1477 38282 dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1478 else
1479 153136 dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1480
1481
4/4
✓ Branch 0 taken 114843 times.
✓ Branch 1 taken 76575 times.
✓ Branch 2 taken 114823 times.
✓ Branch 3 taken 20 times.
191418 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1482 114823 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1483
1484 191418 get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
1485
1486 191418 mv_table[mot_xy][0]= mx;
1487 191418 mv_table[mot_xy][1]= my;
1488 191418 c->flags &= ~FLAG_DIRECT;
1489 191418 c->sub_flags &= ~FLAG_DIRECT;
1490
1491 191418 return dmin;
1492 }
1493
1494 408312 void ff_estimate_b_frame_motion(MpegEncContext * s,
1495 int mb_x, int mb_y)
1496 {
1497 408312 MotionEstContext * const c= &s->me;
1498 408312 const int penalty_factor= c->mb_penalty_factor;
1499 int fmin, bmin, dmin, fbmin, bimin, fimin;
1500 408312 int type=0;
1501 408312 const int xy = mb_y*s->mb_stride + mb_x;
1502 408312 init_ref(c, s->new_picture->data, s->last_picture.f->data,
1503 408312 s->next_picture.f->data, 16 * mb_x, 16 * mb_y, 2);
1504
1505 408312 get_limits(s, 16*mb_x, 16*mb_y);
1506
1507 408312 c->skip=0;
1508
1509
4/4
✓ Branch 0 taken 191520 times.
✓ Branch 1 taken 216792 times.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 191404 times.
408312 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]) {
1510 116 int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
1511
1512 116 score= ((unsigned)(score*score + 128*256))>>16;
1513 116 c->mc_mb_var_sum_temp += score;
1514 116 s->mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1515 116 s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
1516
1517 116 return;
1518 }
1519
1520
2/2
✓ Branch 0 taken 191404 times.
✓ Branch 1 taken 216792 times.
408196 if (s->codec_id == AV_CODEC_ID_MPEG4)
1521 191404 dmin= direct_search(s, mb_x, mb_y);
1522 else
1523 216792 dmin= INT_MAX;
1524 // FIXME penalty stuff for non-MPEG-4
1525 408196 c->skip=0;
1526 408196 fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +
1527 408196 3 * penalty_factor;
1528
1529 408196 c->skip=0;
1530 408196 bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) +
1531 408196 2 * penalty_factor;
1532 ff_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1533
1534 408196 c->skip=0;
1535 408196 fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
1536 ff_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1537
1538
2/2
✓ Branch 0 taken 115200 times.
✓ Branch 1 taken 292996 times.
408196 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
1539 //FIXME mb type penalty
1540 115200 c->skip=0;
1541 115200 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
1542 115200 fimin= interlaced_search(s, 0,
1543 115200 s->b_field_mv_table[0], s->b_field_select_table[0],
1544 115200 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1545 115200 c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_DMV;
1546 115200 bimin= interlaced_search(s, 2,
1547 115200 s->b_field_mv_table[1], s->b_field_select_table[1],
1548 115200 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1549 }else
1550 292996 fimin= bimin= INT_MAX;
1551
1552 {
1553 408196 int score= fmin;
1554 408196 type = CANDIDATE_MB_TYPE_FORWARD;
1555
1556
2/2
✓ Branch 0 taken 55256 times.
✓ Branch 1 taken 352940 times.
408196 if (dmin <= score){
1557 55256 score = dmin;
1558 55256 type = CANDIDATE_MB_TYPE_DIRECT;
1559 }
1560
2/2
✓ Branch 0 taken 204666 times.
✓ Branch 1 taken 203530 times.
408196 if(bmin<score){
1561 204666 score=bmin;
1562 204666 type= CANDIDATE_MB_TYPE_BACKWARD;
1563 }
1564
2/2
✓ Branch 0 taken 237224 times.
✓ Branch 1 taken 170972 times.
408196 if(fbmin<score){
1565 237224 score=fbmin;
1566 237224 type= CANDIDATE_MB_TYPE_BIDIR;
1567 }
1568
2/2
✓ Branch 0 taken 3902 times.
✓ Branch 1 taken 404294 times.
408196 if(fimin<score){
1569 3902 score=fimin;
1570 3902 type= CANDIDATE_MB_TYPE_FORWARD_I;
1571 }
1572
2/2
✓ Branch 0 taken 4095 times.
✓ Branch 1 taken 404101 times.
408196 if(bimin<score){
1573 4095 score=bimin;
1574 4095 type= CANDIDATE_MB_TYPE_BACKWARD_I;
1575 }
1576
1577 408196 score= ((unsigned)(score*score + 128*256))>>16;
1578 408196 c->mc_mb_var_sum_temp += score;
1579 408196 s->mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1580 }
1581
1582
2/2
✓ Branch 0 taken 229852 times.
✓ Branch 1 taken 178344 times.
408196 if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1583 229852 type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1584
2/2
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 198862 times.
229852 if(fimin < INT_MAX)
1585 30990 type |= CANDIDATE_MB_TYPE_FORWARD_I;
1586
2/2
✓ Branch 0 taken 31154 times.
✓ Branch 1 taken 198698 times.
229852 if(bimin < INT_MAX)
1587 31154 type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1588
4/4
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 198862 times.
✓ Branch 2 taken 26027 times.
✓ Branch 3 taken 4963 times.
229852 if(fimin < INT_MAX && bimin < INT_MAX){
1589 26027 type |= CANDIDATE_MB_TYPE_BIDIR_I;
1590 }
1591 //FIXME something smarter
1592
2/2
✓ Branch 0 taken 76801 times.
✓ Branch 1 taken 153051 times.
229852 if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
1593
4/4
✓ Branch 0 taken 153148 times.
✓ Branch 1 taken 76704 times.
✓ Branch 2 taken 153051 times.
✓ Branch 3 taken 97 times.
229852 if (s->codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT &&
1594
4/4
✓ Branch 0 taken 76517 times.
✓ Branch 1 taken 76534 times.
✓ Branch 2 taken 47391 times.
✓ Branch 3 taken 29126 times.
153051 s->mpv_flags & FF_MPV_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
1595 47391 type |= CANDIDATE_MB_TYPE_DIRECT0;
1596 }
1597
1598 408196 s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1599 }
1600
1601 /* find best f_code for ME which do unlimited searches */
1602 12065 int ff_get_best_fcode(MpegEncContext * s, const int16_t (*mv_table)[2], int type)
1603 {
1604
1/2
✓ Branch 0 taken 12065 times.
✗ Branch 1 not taken.
12065 if (s->motion_est != FF_ME_ZERO) {
1605 int score[8];
1606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12065 times.
12065 int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
1607 12065 const uint8_t * fcode_tab = s->fcode_tab;
1608 12065 int best_fcode=-1;
1609 12065 int best_score=-10000000;
1610
1611
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 11323 times.
12065 if(s->msmpeg4_version)
1612 742 range= FFMIN(range, 16);
1613
4/4
✓ Branch 0 taken 5043 times.
✓ Branch 1 taken 6280 times.
✓ Branch 2 taken 4435 times.
✓ Branch 3 taken 608 times.
11323 else if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
1614 4435 range= FFMIN(range, 256);
1615
1616
2/2
✓ Branch 0 taken 96520 times.
✓ Branch 1 taken 12065 times.
108585 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
1617
1618
2/2
✓ Branch 0 taken 171595 times.
✓ Branch 1 taken 12065 times.
183660 for(y=0; y<s->mb_height; y++){
1619 int x;
1620 171595 int xy= y*s->mb_stride;
1621
2/2
✓ Branch 0 taken 3820039 times.
✓ Branch 1 taken 171595 times.
3991634 for(x=0; x<s->mb_width; x++, xy++){
1622
2/2
✓ Branch 0 taken 3112531 times.
✓ Branch 1 taken 707508 times.
3820039 if(s->mb_type[xy] & type){
1623 3112531 int mx= mv_table[xy][0];
1624 3112531 int my= mv_table[xy][1];
1625 3112531 int fcode= FFMAX(fcode_tab[mx + MAX_MV],
1626 fcode_tab[my + MAX_MV]);
1627 int j;
1628
1629
6/6
✓ Branch 0 taken 3111570 times.
✓ Branch 1 taken 961 times.
✓ Branch 2 taken 3110770 times.
✓ Branch 3 taken 800 times.
✓ Branch 4 taken 3110111 times.
✓ Branch 5 taken 659 times.
3112531 if (mx >= range || mx < -range ||
1630
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 3109652 times.
3110111 my >= range || my < -range)
1631 2879 continue;
1632
1633
3/4
✓ Branch 0 taken 3329169 times.
✓ Branch 1 taken 3109652 times.
✓ Branch 2 taken 3329169 times.
✗ Branch 3 not taken.
6438821 for(j=0; j<fcode && j<8; j++){
1634
2/2
✓ Branch 0 taken 2064432 times.
✓ Branch 1 taken 1264737 times.
3329169 if (s->pict_type == AV_PICTURE_TYPE_B ||
1635
2/2
✓ Branch 0 taken 1960703 times.
✓ Branch 1 taken 103729 times.
2064432 s->mc_mb_var[xy] < s->mb_var[xy])
1636 3225440 score[j]-= 170;
1637 }
1638 }
1639 }
1640 }
1641
1642
2/2
✓ Branch 0 taken 84455 times.
✓ Branch 1 taken 12065 times.
96520 for(i=1; i<8; i++){
1643
2/2
✓ Branch 0 taken 16128 times.
✓ Branch 1 taken 68327 times.
84455 if(score[i] > best_score){
1644 16128 best_score= score[i];
1645 16128 best_fcode= i;
1646 }
1647 }
1648
1649 12065 return best_fcode;
1650 }else{
1651 return 1;
1652 }
1653 }
1654
1655 6553 void ff_fix_long_p_mvs(MpegEncContext * s, int type)
1656 {
1657 6553 MotionEstContext * const c= &s->me;
1658 6553 const int f_code= s->f_code;
1659 int y, range;
1660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6553 times.
6553 av_assert0(s->pict_type==AV_PICTURE_TYPE_P);
1661
1662
4/4
✓ Branch 0 taken 3603 times.
✓ Branch 1 taken 2950 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 2861 times.
6553 range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
1663
1664
3/4
✓ Branch 0 taken 3673 times.
✓ Branch 1 taken 2880 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3673 times.
6553 av_assert0(range <= 16 || !s->msmpeg4_version);
1665
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6553 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6553 av_assert0(range <=256 || !(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
1666
1667
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6553 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6553 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1668
1669
2/2
✓ Branch 0 taken 927 times.
✓ Branch 1 taken 5626 times.
6553 if (s->avctx->flags & AV_CODEC_FLAG_4MV) {
1670 927 const int wrap= s->b8_stride;
1671
1672 /* clip / convert to intra 8x8 type MVs */
1673
2/2
✓ Branch 0 taken 13221 times.
✓ Branch 1 taken 927 times.
14148 for(y=0; y<s->mb_height; y++){
1674 13221 int xy= y*2*wrap;
1675 13221 int i= y*s->mb_stride;
1676 int x;
1677
1678
2/2
✓ Branch 0 taken 277695 times.
✓ Branch 1 taken 13221 times.
290916 for(x=0; x<s->mb_width; x++){
1679
2/2
✓ Branch 0 taken 210033 times.
✓ Branch 1 taken 67662 times.
277695 if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1680 int block;
1681
2/2
✓ Branch 0 taken 840132 times.
✓ Branch 1 taken 210033 times.
1050165 for(block=0; block<4; block++){
1682 840132 int off= (block& 1) + (block>>1)*wrap;
1683 840132 int mx = s->current_picture.motion_val[0][ xy + off ][0];
1684 840132 int my = s->current_picture.motion_val[0][ xy + off ][1];
1685
1686
4/4
✓ Branch 0 taken 839736 times.
✓ Branch 1 taken 396 times.
✓ Branch 2 taken 839117 times.
✓ Branch 3 taken 619 times.
840132 if( mx >=range || mx <-range
1687
4/4
✓ Branch 0 taken 838976 times.
✓ Branch 1 taken 141 times.
✓ Branch 2 taken 234 times.
✓ Branch 3 taken 838742 times.
839117 || my >=range || my <-range){
1688 1390 s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1689 1390 s->mb_type[i] |= type;
1690 1390 s->current_picture.mb_type[i] = type;
1691 }
1692 }
1693 }
1694 277695 xy+=2;
1695 277695 i++;
1696 }
1697 }
1698 }
1699 6553 }
1700
1701 /**
1702 * @param truncate 1 for truncation, 0 for using intra
1703 */
1704 16341 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
1705 int16_t (*mv_table)[2], int f_code, int type, int truncate)
1706 {
1707 16341 MotionEstContext * const c= &s->me;
1708 int y, h_range, v_range;
1709
1710 // RAL: 8 in MPEG-1, 16 in MPEG-4
1711
4/4
✓ Branch 0 taken 6163 times.
✓ Branch 1 taken 10178 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 5421 times.
16341 int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
1712
1713
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16341 times.
✗ Branch 2 not ta