FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/motion_est.c
Date: 2024-11-12 23:50:39
Exec Total Coverage
Lines: 867 1008 86.0%
Functions: 28 34 82.4%
Branches: 506 623 81.2%

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 6042677 static inline unsigned update_map_generation(MotionEstContext *c)
55 {
56 6042677 c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
57
2/2
✓ Branch 0 taken 5821 times.
✓ Branch 1 taken 6036856 times.
6042677 if(c->map_generation==0){
58 5821 c->map_generation= 1<<(ME_MAP_MV_BITS*2);
59 5821 memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
60 }
61 6042677 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 2553379 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 2553379 const int offset[3]= {
87 2553379 y*c-> stride + x,
88 2553379 ((y*c->uvstride + x)>>1),
89 2553379 ((y*c->uvstride + x)>>1),
90 };
91 int i;
92
2/2
✓ Branch 0 taken 7660137 times.
✓ Branch 1 taken 2553379 times.
10213516 for(i=0; i<3; i++){
93 7660137 c->src[0][i]= src [i] + offset[i];
94 7660137 c->ref[0][i]= ref [i] + offset[i];
95 }
96
2/2
✓ Branch 0 taken 408312 times.
✓ Branch 1 taken 2145067 times.
2553379 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 2553379 }
102
103 657 static int get_flags(MotionEstContext *c, int direct, int chroma){
104 657 return ((c->avctx->flags&AV_CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 + (direct ? FLAG_DIRECT : 0)
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 + (chroma ? FLAG_CHROMA : 0);
107 }
108
109 2665838 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 2665838 MotionEstContext * const c= &s->me;
113 2665838 const int stride= c->stride;
114 2665838 const int hx = subx + x * (1 << (1 + qpel));
115 2665838 const int hy = suby + y * (1 << (1 + qpel));
116 2665838 const uint8_t * const * const ref = c->ref[ref_index];
117 2665838 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 2665838 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2665838 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2665838 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2665838 times.
✗ Branch 7 not taken.
5331676 if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
122 2665838 const int time_pp= s->pp_time;
123 2665838 const int time_pb= s->pb_time;
124 2665838 const int mask= 2*qpel+1;
125
2/2
✓ Branch 0 taken 667151 times.
✓ Branch 1 taken 1998687 times.
2665838 if(s->mv_type==MV_TYPE_8X8){
126 int i;
127
2/2
✓ Branch 0 taken 2668604 times.
✓ Branch 1 taken 667151 times.
3335755 for(i=0; i<4; i++){
128 2668604 int fx = c->direct_basis_mv[i][0] + hx;
129 2668604 int fy = c->direct_basis_mv[i][1] + hy;
130
2/2
✓ Branch 0 taken 1689636 times.
✓ Branch 1 taken 978968 times.
2668604 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 1648128 times.
✓ Branch 1 taken 1020476 times.
2668604 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 2668604 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
133 2668604 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
134
135 2668604 uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
136
2/2
✓ Branch 0 taken 961440 times.
✓ Branch 1 taken 1707164 times.
2668604 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 1707164 c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
141 1707164 c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
142 }
143 }
144 }else{
145 1998687 int fx = c->direct_basis_mv[0][0] + hx;
146 1998687 int fy = c->direct_basis_mv[0][1] + hy;
147
2/2
✓ Branch 0 taken 1513803 times.
✓ Branch 1 taken 484884 times.
1998687 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 1470632 times.
✓ Branch 1 taken 528055 times.
1998687 int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
149 1998687 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
150 1998687 int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
151
152
2/2
✓ Branch 0 taken 433681 times.
✓ Branch 1 taken 1565006 times.
1998687 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 1565006 c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
172 1565006 c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
173 }
174 }
175 2665838 d = cmp_func(s, c->temp, src[0], stride, 16);
176 }else
177 d= 256*256*256*32;
178 2665838 return d;
179 }
180
181 60024334 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 60024334 MotionEstContext * const c= &s->me;
185 60024334 const int stride= c->stride;
186 60024334 const int uvstride= c->uvstride;
187 60024334 const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
188 60024334 const int hx= subx + x*(1<<(1+qpel));
189 60024334 const int hy= suby + y*(1<<(1+qpel));
190 60024334 const uint8_t * const * const ref = c->ref[ref_index];
191 60024334 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 7984708 times.
✓ Branch 1 taken 52039626 times.
60024334 if(dxy){
196
2/2
✓ Branch 0 taken 6065785 times.
✓ Branch 1 taken 1918923 times.
7984708 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 1918923 c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1918923 times.
1918923 if(chroma)
215 uvdxy= dxy | (x&1) | (2*(y&1));
216 }
217 7984708 d = cmp_func(s, c->temp, src[0], stride, h);
218 }else{
219 52039626 d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52039626 times.
52039626 if(chroma)
221 uvdxy= (x&1) + 2*(y&1);
222 }
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60024334 times.
60024334 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 60024334 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 54481476 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 1794242 times.
✓ Branch 1 taken 52687234 times.
54481476 if(flags&FLAG_DIRECT){
253 1794242 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 52687234 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 54481476 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 54481476 return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
274 }
275 }
276
277 2330664 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 598188 times.
✓ Branch 1 taken 1732476 times.
2330664 if(flags&FLAG_DIRECT){
281 598188 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 219 av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx,
309 const MECmpContext *mecc, int mpvenc)
310 {
311 219 int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
312 219 int dia_size = FFMAX(FFABS(avctx->dia_size) & 255, FFABS(avctx->pre_dia_size) & 255);
313 int ret;
314
315
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)) {
316 av_log(avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
317 return AVERROR(EINVAL);
318 }
319
320 219 c->avctx = avctx;
321
322
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 213 times.
219 if (avctx->codec_id == AV_CODEC_ID_H261)
323 6 avctx->me_sub_cmp = avctx->me_cmp;
324
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if (cache_size < 2 * dia_size)
326 av_log(avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
327
328 219 ret = ff_set_cmp(mecc, c->me_pre_cmp, avctx->me_pre_cmp, mpvenc);
329 219 ret |= ff_set_cmp(mecc, c->me_cmp, avctx->me_cmp, mpvenc);
330 219 ret |= ff_set_cmp(mecc, c->me_sub_cmp, avctx->me_sub_cmp, mpvenc);
331 219 ret |= ff_set_cmp(mecc, c->mb_cmp, avctx->mb_cmp, mpvenc);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if (ret < 0)
333 return ret;
334
335 219 c->sse = mecc->sse[0];
336 219 memcpy(c->pix_abs, mecc->pix_abs, sizeof(c->pix_abs));
337
338 219 c->flags = get_flags(c, 0, avctx->me_cmp & FF_CMP_CHROMA);
339 219 c->sub_flags = get_flags(c, 0, avctx->me_sub_cmp & FF_CMP_CHROMA);
340 219 c->mb_flags = get_flags(c, 0, avctx->mb_cmp & FF_CMP_CHROMA);
341
342
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 213 times.
219 if (avctx->codec_id == AV_CODEC_ID_H261) {
343 6 c->sub_motion_search = no_sub_motion_search;
344
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 201 times.
213 } else if (avctx->flags & AV_CODEC_FLAG_QPEL) {
345 12 c->sub_motion_search= qpel_motion_search;
346 }else{
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
348 c->sub_motion_search= hpel_motion_search;
349
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 19 times.
201 else if( c->avctx->me_sub_cmp == FF_CMP_SAD
350
1/2
✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
182 && c->avctx-> me_cmp == FF_CMP_SAD
351
1/2
✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
182 && c->avctx-> mb_cmp == FF_CMP_SAD)
352 182 c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
353 else
354 19 c->sub_motion_search= hpel_motion_search;
355 }
356
357 /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
358 * not have yet, and even if we had, the motion estimation code
359 * does not expect it. */
360
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 11 times.
219 if (avctx->codec_id != AV_CODEC_ID_SNOW) {
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 if ((avctx->me_cmp & FF_CMP_CHROMA) /* && !c->me_cmp[2] */)
362 c->me_cmp[2] = zero_cmp;
363
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])
364 c->me_sub_cmp[2] = zero_cmp;
365 }
366
367 219 return 0;
368 }
369
370 11423 void ff_me_init_pic(MpegEncContext *s)
371 {
372 11423 MotionEstContext * const c= &s->me;
373
374 /*FIXME s->no_rounding b_type*/
375
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 10895 times.
11423 if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
376 528 c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
377
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 429 times.
528 if (s->no_rounding)
378 99 c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
379 else
380 429 c->qpel_put = s->qdsp.put_qpel_pixels_tab;
381 }
382 11423 c->hpel_avg = s->hdsp.avg_pixels_tab;
383
2/2
✓ Branch 0 taken 1709 times.
✓ Branch 1 taken 9714 times.
11423 if (s->no_rounding)
384 1709 c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
385 else
386 9714 c->hpel_put = s->hdsp.put_pixels_tab;
387
388
1/2
✓ Branch 0 taken 11423 times.
✗ Branch 1 not taken.
11423 if(s->linesize){
389 11423 c->stride = s->linesize;
390 11423 c->uvstride= s->uvlinesize;
391 }else{
392 c->stride = 16*s->mb_width + 32;
393 c->uvstride= 8*s->mb_width + 16;
394 }
395
1/2
✓ Branch 0 taken 11423 times.
✗ Branch 1 not taken.
11423 if (s->codec_id != AV_CODEC_ID_SNOW) {
396 11423 c->hpel_put[2][0]= c->hpel_put[2][1]=
397 11423 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
398 }
399 11423 }
400
401 #define CHECK_SAD_HALF_MV(suffix, x, y) \
402 {\
403 d = c->pix_abs[size][(x ? 1 : 0) + (y ? 2 : 0)](NULL, pix, ptr + ((x) >> 1), stride, h); \
404 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
405 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
406 }
407
408 4595575 static int sad_hpel_motion_search(MpegEncContext * s,
409 int *mx_ptr, int *my_ptr, int dmin,
410 int src_index, int ref_index,
411 int size, int h)
412 {
413 4595575 MotionEstContext * const c= &s->me;
414 4595575 const int penalty_factor= c->sub_penalty_factor;
415 int mx, my, dminh;
416 const uint8_t *pix, *ptr;
417 4595575 int stride= c->stride;
418 4595575 LOAD_COMMON
419
420 av_assert2(c->sub_flags == 0);
421
422
2/2
✓ Branch 0 taken 117172 times.
✓ Branch 1 taken 4478403 times.
4595575 if(c->skip){
423 117172 *mx_ptr = 0;
424 117172 *my_ptr = 0;
425 117172 return dmin;
426 }
427
428 4478403 pix = c->src[src_index][0];
429
430 4478403 mx = *mx_ptr;
431 4478403 my = *my_ptr;
432 4478403 ptr = c->ref[ref_index][0] + (my * stride) + mx;
433
434 4478403 dminh = dmin;
435
436
6/6
✓ Branch 0 taken 4402871 times.
✓ Branch 1 taken 75532 times.
✓ Branch 2 taken 4333084 times.
✓ Branch 3 taken 69787 times.
✓ Branch 4 taken 4242561 times.
✓ Branch 5 taken 90523 times.
4478403 if (mx > xmin && mx < xmax &&
437
2/2
✓ Branch 0 taken 4159678 times.
✓ Branch 1 taken 82883 times.
4242561 my > ymin && my < ymax) {
438 4159678 int dx=0, dy=0;
439 int d, pen_x, pen_y;
440 4159678 const int index= my*(1<<ME_MAP_SHIFT) + mx;
441 4159678 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
442 4159678 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
443 4159678 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
444 4159678 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
445 4159678 mx += mx;
446 4159678 my += my;
447
448
449 4159678 pen_x= pred_x + mx;
450 4159678 pen_y= pred_y + my;
451
452 4159678 ptr-= stride;
453
2/2
✓ Branch 0 taken 2112988 times.
✓ Branch 1 taken 2046690 times.
4159678 if(t<=b){
454 2112988 CHECK_SAD_HALF_MV(y2 , 0, -1)
455
2/2
✓ Branch 0 taken 1315821 times.
✓ Branch 1 taken 797167 times.
2112988 if(l<=r){
456 1315821 CHECK_SAD_HALF_MV(xy2, -1, -1)
457
2/2
✓ Branch 0 taken 769018 times.
✓ Branch 1 taken 546803 times.
1315821 if(t+r<=b+l){
458 769018 CHECK_SAD_HALF_MV(xy2, +1, -1)
459 769018 ptr+= stride;
460 }else{
461 546803 ptr+= stride;
462 546803 CHECK_SAD_HALF_MV(xy2, -1, +1)
463 }
464 1315821 CHECK_SAD_HALF_MV(x2 , -1, 0)
465 }else{
466 797167 CHECK_SAD_HALF_MV(xy2, +1, -1)
467
2/2
✓ Branch 0 taken 439273 times.
✓ Branch 1 taken 357894 times.
797167 if(t+l<=b+r){
468 439273 CHECK_SAD_HALF_MV(xy2, -1, -1)
469 439273 ptr+= stride;
470 }else{
471 357894 ptr+= stride;
472 357894 CHECK_SAD_HALF_MV(xy2, +1, +1)
473 }
474 797167 CHECK_SAD_HALF_MV(x2 , +1, 0)
475 }
476 }else{
477
2/2
✓ Branch 0 taken 780740 times.
✓ Branch 1 taken 1265950 times.
2046690 if(l<=r){
478
2/2
✓ Branch 0 taken 329925 times.
✓ Branch 1 taken 450815 times.
780740 if(t+l<=b+r){
479 329925 CHECK_SAD_HALF_MV(xy2, -1, -1)
480 329925 ptr+= stride;
481 }else{
482 450815 ptr+= stride;
483 450815 CHECK_SAD_HALF_MV(xy2, +1, +1)
484 }
485 780740 CHECK_SAD_HALF_MV(x2 , -1, 0)
486 780740 CHECK_SAD_HALF_MV(xy2, -1, +1)
487 }else{
488
2/2
✓ Branch 0 taken 560982 times.
✓ Branch 1 taken 704968 times.
1265950 if(t+r<=b+l){
489 560982 CHECK_SAD_HALF_MV(xy2, +1, -1)
490 560982 ptr+= stride;
491 }else{
492 704968 ptr+= stride;
493 704968 CHECK_SAD_HALF_MV(xy2, -1, +1)
494 }
495 1265950 CHECK_SAD_HALF_MV(x2 , +1, 0)
496 1265950 CHECK_SAD_HALF_MV(xy2, +1, +1)
497 }
498 2046690 CHECK_SAD_HALF_MV(y2 , 0, +1)
499 }
500 4159678 mx+=dx;
501 4159678 my+=dy;
502
503 }else{
504 318725 mx += mx;
505 318725 my += my;
506 }
507
508 4478403 *mx_ptr = mx;
509 4478403 *my_ptr = my;
510 4478403 return dminh;
511 }
512
513 2145067 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
514 {
515 2145067 const int xy= s->mb_x + s->mb_y*s->mb_stride;
516
517 2145067 s->p_mv_table[xy][0] = mx;
518 2145067 s->p_mv_table[xy][1] = my;
519
520 /* has already been set to the 4 MV if 4MV is done */
521
2/2
✓ Branch 0 taken 1893305 times.
✓ Branch 1 taken 251762 times.
2145067 if(mv4){
522 1893305 int mot_xy= s->block_index[0];
523
524 1893305 s->cur_pic.motion_val[0][mot_xy ][0] = mx;
525 1893305 s->cur_pic.motion_val[0][mot_xy ][1] = my;
526 1893305 s->cur_pic.motion_val[0][mot_xy + 1][0] = mx;
527 1893305 s->cur_pic.motion_val[0][mot_xy + 1][1] = my;
528
529 1893305 mot_xy += s->b8_stride;
530 1893305 s->cur_pic.motion_val[0][mot_xy ][0] = mx;
531 1893305 s->cur_pic.motion_val[0][mot_xy ][1] = my;
532 1893305 s->cur_pic.motion_val[0][mot_xy + 1][0] = mx;
533 1893305 s->cur_pic.motion_val[0][mot_xy + 1][1] = my;
534 }
535 2145067 }
536
537 /**
538 * get fullpel ME search limits.
539 */
540 3561189 static inline void get_limits(MpegEncContext *s, int x, int y, int bframe)
541 {
542 3561189 MotionEstContext * const c= &s->me;
543
2/2
✓ Branch 0 taken 168764 times.
✓ Branch 1 taken 3392425 times.
3561189 int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
544
2/2
✓ Branch 0 taken 168764 times.
✓ Branch 1 taken 3392425 times.
3561189 int max_range = MAX_MV >> (1 + !!(c->flags&FLAG_QPEL));
545 /*
546 if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
547 else c->range= 16;
548 */
549
2/2
✓ Branch 0 taken 1750697 times.
✓ Branch 1 taken 1810492 times.
3561189 if (s->unrestricted_mv) {
550 1750697 c->xmin = - x - 16;
551 1750697 c->ymin = - y - 16;
552 1750697 c->xmax = - x + s->width;
553 1750697 c->ymax = - y + s->height;
554
2/2
✓ Branch 0 taken 106920 times.
✓ Branch 1 taken 1703572 times.
1810492 } else if (!(av_builtin_constant_p(bframe) && bframe) && s->out_format == FMT_H261){
555 // Search range of H.261 is different from other codec standards
556
2/2
✓ Branch 0 taken 102060 times.
✓ Branch 1 taken 4860 times.
106920 c->xmin = (x > 15) ? - 15 : 0;
557
2/2
✓ Branch 0 taken 100980 times.
✓ Branch 1 taken 5940 times.
106920 c->ymin = (y > 15) ? - 15 : 0;
558
2/2
✓ Branch 0 taken 102060 times.
✓ Branch 1 taken 4860 times.
106920 c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
559
2/2
✓ Branch 0 taken 100980 times.
✓ Branch 1 taken 5940 times.
106920 c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
560 } else {
561 1703572 c->xmin = - x;
562 1703572 c->ymin = - y;
563 1703572 c->xmax = - x + s->mb_width *16 - 16;
564 1703572 c->ymax = - y + s->mb_height*16 - 16;
565 }
566
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3561189 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3561189 if(!range || range > max_range)
567 3561189 range = max_range;
568
1/2
✓ Branch 0 taken 3561189 times.
✗ Branch 1 not taken.
3561189 if(range){
569 3561189 c->xmin = FFMAX(c->xmin,-range);
570 3561189 c->xmax = FFMIN(c->xmax, range);
571 3561189 c->ymin = FFMAX(c->ymin,-range);
572 3561189 c->ymax = FFMIN(c->ymax, range);
573 }
574 3561189 }
575
576 251762 static inline void init_mv4_ref(MotionEstContext *c){
577 251762 const int stride= c->stride;
578
579 251762 c->ref[1][0] = c->ref[0][0] + 8;
580 251762 c->ref[2][0] = c->ref[0][0] + 8*stride;
581 251762 c->ref[3][0] = c->ref[2][0] + 8;
582 251762 c->src[1][0] = c->src[0][0] + 8;
583 251762 c->src[2][0] = c->src[0][0] + 8*stride;
584 251762 c->src[3][0] = c->src[2][0] + 8;
585 251762 }
586
587 251762 static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
588 {
589 251762 MotionEstContext * const c= &s->me;
590 251762 const int size= 1;
591 251762 const int h=8;
592 int block;
593 int P[10][2];
594 251762 int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
595 251762 int same=1;
596 251762 const int stride= c->stride;
597 251762 const uint8_t *mv_penalty = c->current_mv_penalty;
598
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);
599
600 251762 init_mv4_ref(c);
601
602
2/2
✓ Branch 0 taken 1007048 times.
✓ Branch 1 taken 251762 times.
1258810 for(block=0; block<4; block++){
603 int mx4, my4;
604 int pred_x4, pred_y4;
605 int dmin4;
606 static const int off[4]= {2, 1, 1, -1};
607 1007048 const int mot_stride = s->b8_stride;
608 1007048 const int mot_xy = s->block_index[block];
609
610
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 998808 times.
1007048 if(safety_clipping){
611 8240 c->xmax = - 16*s->mb_x + s->width - 8*(block &1);
612 8240 c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
613 }
614
615 1007048 P_LEFT[0] = s->cur_pic.motion_val[0][mot_xy - 1][0];
616 1007048 P_LEFT[1] = s->cur_pic.motion_val[0][mot_xy - 1][1];
617
618
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);
619
620 /* special case for first line */
621
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) {
622 31170 c->pred_x= pred_x4= P_LEFT[0];
623 31170 c->pred_y= pred_y4= P_LEFT[1];
624 } else {
625 975878 P_TOP[0] = s->cur_pic.motion_val[0][mot_xy - mot_stride ][0];
626 975878 P_TOP[1] = s->cur_pic.motion_val[0][mot_xy - mot_stride ][1];
627 975878 P_TOPRIGHT[0] = s->cur_pic.motion_val[0][mot_xy - mot_stride + off[block]][0];
628 975878 P_TOPRIGHT[1] = s->cur_pic.motion_val[0][mot_xy - mot_stride + off[block]][1];
629
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);
630
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);
631
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);
632
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);
633
634 975878 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
635 975878 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
636
637 975878 c->pred_x= pred_x4 = P_MEDIAN[0];
638 975878 c->pred_y= pred_y4 = P_MEDIAN[1];
639 }
640 1007048 P_MV1[0]= mx;
641 1007048 P_MV1[1]= my;
642
2/2
✓ Branch 0 taken 8240 times.
✓ Branch 1 taken 998808 times.
1007048 if(safety_clipping)
643
2/2
✓ Branch 0 taken 74160 times.
✓ Branch 1 taken 8240 times.
82400 for(i=1; i<10; i++){
644
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)
645 10234 continue;
646
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)
647 27112 continue;
648
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);
649
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 );
650 }
651
652 1007048 dmin4 = epzs_motion_search2(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift, 1);
653
654 1007048 dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
655
656
2/2
✓ Branch 0 taken 167508 times.
✓ Branch 1 taken 839540 times.
1007048 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
657 int dxy;
658 167508 const int offset= ((block&1) + (block>>1)*stride)*8;
659 167508 uint8_t *dest_y = c->scratchpad + offset;
660
2/2
✓ Branch 0 taken 56744 times.
✓ Branch 1 taken 110764 times.
167508 if(s->quarter_sample){
661 56744 const uint8_t *ref = c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
662 56744 dxy = ((my4 & 3) << 2) | (mx4 & 3);
663
664
2/2
✓ Branch 0 taken 39304 times.
✓ Branch 1 taken 17440 times.
56744 if(s->no_rounding)
665 39304 s->qdsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y, ref, stride);
666 else
667 17440 s->qdsp.put_qpel_pixels_tab[1][dxy](dest_y, ref, stride);
668 }else{
669 110764 const uint8_t *ref = c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
670 110764 dxy = ((my4 & 1) << 1) | (mx4 & 1);
671
672
2/2
✓ Branch 0 taken 77060 times.
✓ Branch 1 taken 33704 times.
110764 if(s->no_rounding)
673 77060 s->hdsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
674 else
675 33704 s->hdsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
676 }
677 167508 dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
678 }else
679 839540 dmin_sum+= dmin4;
680
681
2/2
✓ Branch 0 taken 56744 times.
✓ Branch 1 taken 950304 times.
1007048 if(s->quarter_sample){
682 56744 mx4_sum+= mx4/2;
683 56744 my4_sum+= my4/2;
684 }else{
685 950304 mx4_sum+= mx4;
686 950304 my4_sum+= my4;
687 }
688
689 1007048 s->cur_pic.motion_val[0][s->block_index[block]][0] = mx4;
690 1007048 s->cur_pic.motion_val[0][s->block_index[block]][1] = my4;
691
692
4/4
✓ Branch 0 taken 535629 times.
✓ Branch 1 taken 471419 times.
✓ Branch 2 taken 129959 times.
✓ Branch 3 taken 405670 times.
1007048 if(mx4 != mx || my4 != my) same=0;
693 }
694
695
2/2
✓ Branch 0 taken 41703 times.
✓ Branch 1 taken 210059 times.
251762 if(same)
696 41703 return INT_MAX;
697
698
2/2
✓ Branch 0 taken 41458 times.
✓ Branch 1 taken 168601 times.
210059 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
699 41458 dmin_sum += c->mb_cmp[0](s,
700 41458 s->new_pic->data[0] +
701 41458 s->mb_x * 16 + s->mb_y * 16 * stride,
702 41458 c->scratchpad, stride, 16);
703 }
704
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210059 times.
210059 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
706 int dxy;
707 int mx, my;
708 int offset;
709
710 mx= ff_h263_round_chroma(mx4_sum);
711 my= ff_h263_round_chroma(my4_sum);
712 dxy = ((my & 1) << 1) | (mx & 1);
713
714 offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
715
716 if(s->no_rounding){
717 s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_pic.data[1] + offset, s->uvlinesize, 8);
718 s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_pic.data[2] + offset, s->uvlinesize, 8);
719 }else{
720 s->hdsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_pic.data[1] + offset, s->uvlinesize, 8);
721 s->hdsp.put_pixels_tab [1][dxy](c->scratchpad + 8, s->last_pic.data[2] + offset, s->uvlinesize, 8);
722 }
723
724 dmin_sum += c->mb_cmp[1](s, s->new_pic->data[1] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad, s->uvlinesize, 8);
725 dmin_sum += c->mb_cmp[1](s, s->new_pic->data[2] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad + 8, s->uvlinesize, 8);
726 }
727
728 210059 c->pred_x= mx;
729 210059 c->pred_y= my;
730
731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210059 times.
210059 switch(c->avctx->mb_cmp&0xFF){
732 /*case FF_CMP_SSE:
733 return dmin_sum+ 32*s->qscale*s->qscale;*/
734 case FF_CMP_RD:
735 return dmin_sum;
736 210059 default:
737 210059 return dmin_sum+ 11*c->mb_penalty_factor;
738 }
739 }
740
741 331233 static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
742 331233 MotionEstContext * const c= &s->me;
743
744 331233 c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
745 331233 c->src[1][0] = c->src[0][0] + s->linesize;
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 331233 times.
331233 if(c->flags & FLAG_CHROMA){
747 c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
748 c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
749 c->src[1][1] = c->src[0][1] + s->uvlinesize;
750 c->src[1][2] = c->src[0][2] + s->uvlinesize;
751 }
752 331233 }
753
754 331233 static int interlaced_search(MpegEncContext *s, int ref_index,
755 int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
756 {
757 331233 MotionEstContext * const c= &s->me;
758 331233 const int size=0;
759 331233 const int h=8;
760 int block;
761 int P[10][2];
762 331233 const uint8_t * const mv_penalty = c->current_mv_penalty;
763 331233 int same=1;
764 331233 const int stride= 2*s->linesize;
765 331233 int dmin_sum= 0;
766 331233 const int mot_stride= s->mb_stride;
767 331233 const int xy= s->mb_x + s->mb_y*mot_stride;
768
769 331233 c->ymin>>=1;
770 331233 c->ymax>>=1;
771 331233 c->stride<<=1;
772 331233 c->uvstride<<=1;
773 331233 init_interlaced_ref(s, ref_index);
774
775
2/2
✓ Branch 0 taken 662466 times.
✓ Branch 1 taken 331233 times.
993699 for(block=0; block<2; block++){
776 int field_select;
777 662466 int best_dmin= INT_MAX;
778 662466 int best_field= -1;
779
780
2/2
✓ Branch 0 taken 1324932 times.
✓ Branch 1 taken 662466 times.
1987398 for(field_select=0; field_select<2; field_select++){
781 int dmin, mx_i, my_i;
782 1324932 int16_t (*mv_table)[2]= mv_tables[block][field_select];
783
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if(user_field_select){
785 av_assert1(field_select==0 || field_select==1);
786 av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
787 if(field_select_tables[block][xy] != field_select)
788 continue;
789 }
790
791 1324932 P_LEFT[0] = mv_table[xy - 1][0];
792 1324932 P_LEFT[1] = mv_table[xy - 1][1];
793
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);
794
795 1324932 c->pred_x= P_LEFT[0];
796 1324932 c->pred_y= P_LEFT[1];
797
798
2/2
✓ Branch 0 taken 1206192 times.
✓ Branch 1 taken 118740 times.
1324932 if(!s->first_slice_line){
799 1206192 P_TOP[0] = mv_table[xy - mot_stride][0];
800 1206192 P_TOP[1] = mv_table[xy - mot_stride][1];
801 1206192 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
802 1206192 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
803
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);
804
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);
805
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);
806
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);
807
808 1206192 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
809 1206192 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
810 }
811 1324932 P_MV1[0]= mx; //FIXME not correct if block != field_select
812 1324932 P_MV1[1]= my / 2;
813
814 1324932 dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1, 0);
815
816 1324932 dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
817
818 1324932 mv_table[xy][0]= mx_i;
819 1324932 mv_table[xy][1]= my_i;
820
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1324932 times.
1324932 if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
822 int dxy;
823
824 //FIXME chroma ME
825 const uint8_t *ref = c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
826 dxy = ((my_i & 1) << 1) | (mx_i & 1);
827
828 if(s->no_rounding){
829 s->hdsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
830 }else{
831 s->hdsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
832 }
833 dmin = c->mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
834 dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
835 }else
836 1324932 dmin+= c->mb_penalty_factor; //field_select bits
837
838 1324932 dmin += field_select != block; //slightly prefer same field
839
840
2/2
✓ Branch 0 taken 994745 times.
✓ Branch 1 taken 330187 times.
1324932 if(dmin < best_dmin){
841 994745 best_dmin= dmin;
842 994745 best_field= field_select;
843 }
844 }
845 {
846 662466 int16_t (*mv_table)[2]= mv_tables[block][best_field];
847
848
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
849
2/2
✓ Branch 0 taken 59142 times.
✓ Branch 1 taken 603324 times.
662466 if(mv_table[xy][1]&1) same=0;
850
2/2
✓ Branch 0 taken 444919 times.
✓ Branch 1 taken 217547 times.
662466 if(mv_table[xy][1]*2 != my) same=0;
851
2/2
✓ Branch 0 taken 318666 times.
✓ Branch 1 taken 343800 times.
662466 if(best_field != block) same=0;
852 }
853
854 662466 field_select_tables[block][xy]= best_field;
855 662466 dmin_sum += best_dmin;
856 }
857
858 331233 c->ymin *= 2;
859 331233 c->ymax<<=1;
860 331233 c->stride>>=1;
861 331233 c->uvstride>>=1;
862
863
2/2
✓ Branch 0 taken 55300 times.
✓ Branch 1 taken 275933 times.
331233 if(same)
864 55300 return INT_MAX;
865
866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275933 times.
275933 switch(c->avctx->mb_cmp&0xFF){
867 /*case FF_CMP_SSE:
868 return dmin_sum+ 32*s->qscale*s->qscale;*/
869 case FF_CMP_RD:
870 return dmin_sum;
871 275933 default:
872 275933 return dmin_sum+ 11*c->mb_penalty_factor;
873 }
874 }
875
876 7659789 static inline int get_penalty_factor(int lambda, int lambda2, int type){
877
3/7
✓ Branch 0 taken 7067290 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.
7659789 switch(type&0xFF){
878 7067290 default:
879 case FF_CMP_SAD:
880 7067290 return lambda>>FF_LAMBDA_SHIFT;
881 case FF_CMP_DCT:
882 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
883 case FF_CMP_W53:
884 return (4*lambda)>>(FF_LAMBDA_SHIFT);
885 case FF_CMP_W97:
886 return (2*lambda)>>(FF_LAMBDA_SHIFT);
887 323169 case FF_CMP_SATD:
888 case FF_CMP_DCT264:
889 323169 return (2*lambda)>>FF_LAMBDA_SHIFT;
890 269330 case FF_CMP_RD:
891 case FF_CMP_PSNR:
892 case FF_CMP_SSE:
893 case FF_CMP_NSSE:
894 269330 return lambda2>>FF_LAMBDA_SHIFT;
895 case FF_CMP_BIT:
896 case FF_CMP_MEDIAN_SAD:
897 return 1;
898 }
899 }
900
901 2145067 void ff_estimate_p_frame_motion(MpegEncContext * s,
902 int mb_x, int mb_y)
903 {
904 2145067 MotionEstContext * const c= &s->me;
905 const uint8_t *pix, *ppix;
906 2145067 int sum, mx = 0, my = 0, dmin = 0;
907 int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
908 int vard; ///< sum of squared differences with the estimated motion vector
909 int P[10][2];
910 2145067 const int shift= 1+s->quarter_sample;
911 2145067 int mb_type=0;
912
913 2145067 init_ref(c, s->new_pic->data, s->last_pic.data, NULL, 16*mb_x, 16*mb_y, 0);
914
915
3/4
✓ Branch 0 taken 15570 times.
✓ Branch 1 taken 2129497 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15570 times.
2145067 av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2145067 times.
2145067 av_assert0(s->linesize == c->stride);
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2145067 times.
2145067 av_assert0(s->uvlinesize == c->uvstride);
918
919 2145067 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
920 2145067 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
921 2145067 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
922 2145067 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
923
924 2145067 get_limits(s, 16*mb_x, 16*mb_y, 0);
925 2145067 c->skip=0;
926
927 /* intra / predictive decision */
928 2145067 pix = c->src[0][0];
929 2145067 sum = s->mpvencdsp.pix_sum(pix, s->linesize);
930 2145067 varc = s->mpvencdsp.pix_norm1(pix, s->linesize) -
931 2145067 (((unsigned) sum * sum) >> 8) + 500;
932
933 2145067 s->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
934 2145067 s->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
935 2145067 c->mb_var_sum_temp += (varc+128)>>8;
936
937
1/2
✓ Branch 0 taken 2145067 times.
✗ Branch 1 not taken.
2145067 if (s->motion_est != FF_ME_ZERO) {
938 2145067 const int mot_stride = s->b8_stride;
939 2145067 const int mot_xy = s->block_index[0];
940
941 2145067 P_LEFT[0] = s->cur_pic.motion_val[0][mot_xy - 1][0];
942 2145067 P_LEFT[1] = s->cur_pic.motion_val[0][mot_xy - 1][1];
943
944
2/2
✓ Branch 0 taken 21267 times.
✓ Branch 1 taken 2123800 times.
2145067 if (P_LEFT[0] > (c->xmax << shift))
945 21267 P_LEFT[0] = c->xmax << shift;
946
947
2/2
✓ Branch 0 taken 1982645 times.
✓ Branch 1 taken 162422 times.
2145067 if (!s->first_slice_line) {
948 1982645 P_TOP[0] = s->cur_pic.motion_val[0][mot_xy - mot_stride ][0];
949 1982645 P_TOP[1] = s->cur_pic.motion_val[0][mot_xy - mot_stride ][1];
950 1982645 P_TOPRIGHT[0] = s->cur_pic.motion_val[0][mot_xy - mot_stride + 2][0];
951 1982645 P_TOPRIGHT[1] = s->cur_pic.motion_val[0][mot_xy - mot_stride + 2][1];
952
2/2
✓ Branch 0 taken 23829 times.
✓ Branch 1 taken 1958816 times.
1982645 if (P_TOP[1] > (c->ymax << shift))
953 23829 P_TOP[1] = c->ymax << shift;
954
2/2
✓ Branch 0 taken 22734 times.
✓ Branch 1 taken 1959911 times.
1982645 if (P_TOPRIGHT[0] < (c->xmin * (1 << shift)))
955 22734 P_TOPRIGHT[0] = c->xmin * (1 << shift);
956
2/2
✓ Branch 0 taken 21997 times.
✓ Branch 1 taken 1960648 times.
1982645 if (P_TOPRIGHT[1] > (c->ymax * (1 << shift)))
957 21997 P_TOPRIGHT[1] = c->ymax * (1 << shift);
958
959 1982645 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
960 1982645 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
961
962
2/2
✓ Branch 0 taken 1137325 times.
✓ Branch 1 taken 845320 times.
1982645 if (s->out_format == FMT_H263) {
963 1137325 c->pred_x = P_MEDIAN[0];
964 1137325 c->pred_y = P_MEDIAN[1];
965 } else { /* MPEG-1 at least */
966 845320 c->pred_x = P_LEFT[0];
967 845320 c->pred_y = P_LEFT[1];
968 }
969 } else {
970 162422 c->pred_x = P_LEFT[0];
971 162422 c->pred_y = P_LEFT[1];
972 }
973 2145067 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
974 }
975
976 /* At this point (mx,my) are full-pell and the relative displacement */
977 2145067 ppix = c->ref[0][0] + (my * s->linesize) + mx;
978
979 2145067 vard = c->sse(NULL, pix, ppix, s->linesize, 16);
980
981 2145067 s->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
982 2145067 c->mc_mb_var_sum_temp += (vard+128)>>8;
983
984
2/2
✓ Branch 0 taken 362361 times.
✓ Branch 1 taken 1782706 times.
2145067 if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {
985 362361 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
986 362361 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
987 362361 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
988
989
3/4
✓ Branch 0 taken 97648 times.
✓ Branch 1 taken 264713 times.
✓ Branch 2 taken 97648 times.
✗ Branch 3 not taken.
362361 if (vard*2 + 200*256 > varc && !s->intra_penalty)
990 97648 mb_type|= CANDIDATE_MB_TYPE_INTRA;
991
3/4
✓ Branch 0 taken 2084 times.
✓ Branch 1 taken 360277 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2084 times.
362361 if (varc*2 + 200*256 > vard || s->qscale > 24){
992 // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
993 360277 mb_type|= CANDIDATE_MB_TYPE_INTER;
994 360277 c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
995
2/2
✓ Branch 0 taken 61156 times.
✓ Branch 1 taken 299121 times.
360277 if (s->mpv_flags & FF_MPV_FLAG_MV0)
996
4/4
✓ Branch 0 taken 4056 times.
✓ Branch 1 taken 57100 times.
✓ Branch 2 taken 3528 times.
✓ Branch 3 taken 528 times.
61156 if(mx || my)
997 60628 mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
998 }else{
999 2084 mx *= 1 << shift;
1000 2084 my *= 1 << shift;
1001 }
1002
2/2
✓ Branch 0 taken 277722 times.
✓ Branch 1 taken 84639 times.
362361 if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
1003
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){
1004
2/2
✓ Branch 1 taken 210059 times.
✓ Branch 2 taken 41703 times.
251762 if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
1005 210059 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
1006
1007 251762 set_p_mv_tables(s, mx, my, 0);
1008 }else
1009 110599 set_p_mv_tables(s, mx, my, 1);
1010
2/2
✓ Branch 0 taken 15600 times.
✓ Branch 1 taken 346761 times.
362361 if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1011
1/2
✓ Branch 0 taken 15600 times.
✗ Branch 1 not taken.
15600 && !c->skip){ //FIXME varc/d checks
1012
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)
1013 13535 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
1014 }
1015 }else{
1016 int intra_score, i;
1017 1782706 mb_type= CANDIDATE_MB_TYPE_INTER;
1018
1019 1782706 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1020
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1782706 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1782706 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
1021 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1022
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1782706 times.
1782706 if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
1024 && !c->skip && varc>50<<8 && vard>10<<8){
1025 int dmin4= h263_mv4_search(s, mx, my, shift);
1026 if(dmin4 < dmin){
1027 mb_type= CANDIDATE_MB_TYPE_INTER4V;
1028 dmin=dmin4;
1029 }
1030 }
1031
2/2
✓ Branch 0 taken 85236 times.
✓ Branch 1 taken 1697470 times.
1782706 if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
1032
2/2
✓ Branch 0 taken 85233 times.
✓ Branch 1 taken 3 times.
85236 && !c->skip){ //FIXME varc/d checks
1033 85233 int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
1034
2/2
✓ Branch 0 taken 11208 times.
✓ Branch 1 taken 74025 times.
85233 if(dmin_i < dmin){
1035 11208 mb_type = CANDIDATE_MB_TYPE_INTER_I;
1036 11208 dmin= dmin_i;
1037 }
1038 }
1039
1040 1782706 set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
1041
1042 /* get intra luma score */
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1782706 times.
1782706 if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
1044 intra_score= varc - 500;
1045 }else{
1046 1782706 unsigned mean = (sum+128)>>8;
1047 1782706 mean*= 0x01010101;
1048
1049
2/2
✓ Branch 0 taken 28523296 times.
✓ Branch 1 taken 1782706 times.
30306002 for(i=0; i<16; i++){
1050 28523296 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
1051 28523296 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
1052 28523296 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
1053 28523296 *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
1054 }
1055
1056 1782706 intra_score= c->mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
1057 }
1058 1782706 intra_score += c->mb_penalty_factor*16 + s->intra_penalty;
1059
1060
2/2
✓ Branch 0 taken 95818 times.
✓ Branch 1 taken 1686888 times.
1782706 if(intra_score < dmin){
1061 95818 mb_type= CANDIDATE_MB_TYPE_INTRA;
1062 95818 s->cur_pic.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
1063 }else
1064 1686888 s->cur_pic.mb_type[mb_y*s->mb_stride + mb_x] = 0;
1065
1066 {
1067 1782706 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
1068 1782706 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
1069 1782706 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
1070 }
1071 }
1072
1073 2145067 s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
1074 2145067 }
1075
1076 int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
1077 int mb_x, int mb_y)
1078 {
1079 MotionEstContext * const c= &s->me;
1080 int mx, my, dmin;
1081 int P[10][2];
1082 const int shift= 1+s->quarter_sample;
1083 const int xy= mb_x + mb_y*s->mb_stride;
1084 init_ref(c, s->new_pic->data, s->last_pic.data, NULL, 16*mb_x, 16*mb_y, 0);
1085
1086 av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
1087
1088 c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
1089 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
1090
1091 get_limits(s, 16*mb_x, 16*mb_y, 0);
1092 c->skip=0;
1093
1094 P_LEFT[0] = s->p_mv_table[xy + 1][0];
1095 P_LEFT[1] = s->p_mv_table[xy + 1][1];
1096
1097 if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
1098
1099 /* special case for first line */
1100 if (s->first_slice_line) {
1101 c->pred_x= P_LEFT[0];
1102 c->pred_y= P_LEFT[1];
1103 P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
1104 P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
1105 } else {
1106 P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
1107 P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
1108 P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
1109 P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
1110 if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
1111 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
1112 if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
1113
1114 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1115 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1116
1117 c->pred_x = P_MEDIAN[0];
1118 c->pred_y = P_MEDIAN[1];
1119 }
1120
1121 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
1122
1123 s->p_mv_table[xy][0] = mx<<shift;
1124 s->p_mv_table[xy][1] = my<<shift;
1125
1126 return dmin;
1127 }
1128
1129 816392 static int estimate_motion_b(MpegEncContext *s, int mb_x, int mb_y,
1130 int16_t (*mv_table)[2], int ref_index, int f_code)
1131 {
1132 816392 MotionEstContext * const c= &s->me;
1133 816392 int mx = 0, my = 0, dmin = 0;
1134 int P[10][2];
1135 816392 const int shift= 1+s->quarter_sample;
1136 816392 const int mot_stride = s->mb_stride;
1137 816392 const int mot_xy = mb_y*mot_stride + mb_x;
1138 816392 const uint8_t * const mv_penalty = c->mv_penalty[f_code] + MAX_DMV;
1139 int mv_scale;
1140
1141 816392 c->current_mv_penalty= mv_penalty;
1142
1143 816392 get_limits(s, 16*mb_x, 16*mb_y, 1);
1144
1145
1/2
✓ Branch 0 taken 816392 times.
✗ Branch 1 not taken.
816392 if (s->motion_est != FF_ME_ZERO) {
1146 816392 P_LEFT[0] = mv_table[mot_xy - 1][0];
1147 816392 P_LEFT[1] = mv_table[mot_xy - 1][1];
1148
1149
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);
1150
1151 /* special case for first line */
1152
2/2
✓ Branch 0 taken 756200 times.
✓ Branch 1 taken 60192 times.
816392 if (!s->first_slice_line) {
1153 756200 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1154 756200 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1155 756200 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
1156 756200 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
1157
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);
1158
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);
1159
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);
1160
1161 756200 P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1162 756200 P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1163 }
1164 816392 c->pred_x = P_LEFT[0];
1165 816392 c->pred_y = P_LEFT[1];
1166
1167
2/2
✓ Branch 0 taken 408196 times.
✓ Branch 1 taken 408196 times.
816392 if(mv_table == s->b_forw_mv_table){
1168 408196 mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
1169 }else{
1170 408196 mv_scale = ((s->pb_time - s->pp_time) * (1 << 16)) / (s->pp_time<<shift);
1171 }
1172
1173 816392 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
1174 }
1175
1176 816392 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
1177
1178
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)
1179 306344 dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
1180
1181 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1182 816392 mv_table[mot_xy][0]= mx;
1183 816392 mv_table[mot_xy][1]= my;
1184
1185 816392 return dmin;
1186 }
1187
1188 5439240 static inline int check_bidir_mv(MpegEncContext * s,
1189 int motion_fx, int motion_fy,
1190 int motion_bx, int motion_by,
1191 int pred_fx, int pred_fy,
1192 int pred_bx, int pred_by,
1193 int size, int h)
1194 {
1195 //FIXME optimize?
1196 //FIXME better f_code prediction (max mv & distance)
1197 //FIXME pointers
1198 5439240 MotionEstContext * const c= &s->me;
1199 5439240 const uint8_t * const mv_penalty_f = c->mv_penalty[s->f_code] + MAX_DMV; // f_code of the prev frame
1200 5439240 const uint8_t * const mv_penalty_b = c->mv_penalty[s->b_code] + MAX_DMV; // f_code of the prev frame
1201 5439240 int stride= c->stride;
1202 5439240 uint8_t *dest_y = c->scratchpad;
1203 const uint8_t *ptr;
1204 int dxy;
1205 int src_x, src_y;
1206 int fbmin;
1207 5439240 const uint8_t *const *src_data = c->src[0];
1208 5439240 const uint8_t *const *ref_data = c->ref[0];
1209 5439240 const uint8_t *const *ref2_data = c->ref[2];
1210
1211
2/2
✓ Branch 0 taken 625749 times.
✓ Branch 1 taken 4813491 times.
5439240 if(s->quarter_sample){
1212 625749 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1213 625749 src_x = motion_fx >> 2;
1214 625749 src_y = motion_fy >> 2;
1215
1216 625749 ptr = ref_data[0] + (src_y * stride) + src_x;
1217 625749 s->qdsp.put_qpel_pixels_tab[0][dxy](dest_y, ptr, stride);
1218
1219 625749 dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1220 625749 src_x = motion_bx >> 2;
1221 625749 src_y = motion_by >> 2;
1222
1223 625749 ptr = ref2_data[0] + (src_y * stride) + src_x;
1224 625749 s->qdsp.avg_qpel_pixels_tab[size][dxy](dest_y, ptr, stride);
1225 }else{
1226 4813491 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1227 4813491 src_x = motion_fx >> 1;
1228 4813491 src_y = motion_fy >> 1;
1229
1230 4813491 ptr = ref_data[0] + (src_y * stride) + src_x;
1231 4813491 s->hdsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1232
1233 4813491 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1234 4813491 src_x = motion_bx >> 1;
1235 4813491 src_y = motion_by >> 1;
1236
1237 4813491 ptr = ref2_data[0] + (src_y * stride) + src_x;
1238 4813491 s->hdsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
1239 }
1240
1241 10878480 fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
1242 5439240 +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
1243 5439240 + c->mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
1244
1245 5439240 if(c->avctx->mb_cmp&FF_CMP_CHROMA){
1246 }
1247 //FIXME CHROMA !!!
1248
1249 5439240 return fbmin;
1250 }
1251
1252 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1253 408196 static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
1254 {
1255 408196 MotionEstContext * const c= &s->me;
1256 408196 const int mot_stride = s->mb_stride;
1257 408196 const int xy = mb_y *mot_stride + mb_x;
1258 int fbmin;
1259 408196 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1260 408196 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1261 408196 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1262 408196 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1263 408196 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1264 408196 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1265 408196 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1266 408196 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1267 408196 const int flags= c->sub_flags;
1268 408196 const int qpel= flags&FLAG_QPEL;
1269 408196 const int shift= 1+qpel;
1270 408196 const int xmin= c->xmin * (1 << shift);
1271 408196 const int ymin= c->ymin * (1 << shift);
1272 408196 const int xmax= c->xmax<<shift;
1273 408196 const int ymax= c->ymax<<shift;
1274 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
1275 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
1276 408196 int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
1277 408196 uint8_t map[256] = { 0 };
1278
1279 408196 map[hashidx&255] = 1;
1280
1281 408196 fbmin= check_bidir_mv(s, motion_fx, motion_fy,
1282 motion_bx, motion_by,
1283 pred_fx, pred_fy,
1284 pred_bx, pred_by,
1285 0, 16);
1286
1287
1/2
✓ Branch 0 taken 408196 times.
✗ Branch 1 not taken.
408196 if(s->avctx->bidir_refine){
1288 int end;
1289 static const uint8_t limittab[5]={0,8,32,64,80};
1290 408196 const int limit= limittab[s->avctx->bidir_refine];
1291 static const int8_t vect[][4]={
1292 { 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},
1293
1294 { 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},
1295 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
1296 { 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},
1297 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
1298
1299 { 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},
1300 { 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},
1301 { 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},
1302 { 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},
1303
1304 { 1, 1, 1, 1}, {-1,-1,-1,-1},
1305 { 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},
1306 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
1307 };
1308 static const uint8_t hash[]={
1309 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),
1310
1311 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),
1312 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
1313 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),
1314 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
1315
1316 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),
1317 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),
1318 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),
1319 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),
1320
1321 HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
1322 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),
1323 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),
1324 };
1325
1326 #define CHECK_BIDIR(fx,fy,bx,by)\
1327 if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
1328 &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1329 &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1330 int score;\
1331 map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
1332 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);\
1333 if(score < fbmin){\
1334 hashidx += HASH(fx,fy,bx,by);\
1335 fbmin= score;\
1336 motion_fx+=fx;\
1337 motion_fy+=fy;\
1338 motion_bx+=bx;\
1339 motion_by+=by;\
1340 end=0;\
1341 }\
1342 }
1343 #define CHECK_BIDIR2(a,b,c,d)\
1344 CHECK_BIDIR(a,b,c,d)\
1345 CHECK_BIDIR(-(a),-(b),-(c),-(d))
1346
1347 do{
1348 int i;
1349 827910 int borderdist=0;
1350 827910 end=1;
1351
1352
12/12
✓ Branch 0 taken 775033 times.
✓ Branch 1 taken 52877 times.
✓ Branch 2 taken 762787 times.
✓ Branch 3 taken 12246 times.
✓ Branch 5 taken 95131 times.
✓ Branch 6 taken 667656 times.
✓ Branch 7 taken 696199 times.
✓ Branch 8 taken 131711 times.
✓ Branch 9 taken 683773 times.
✓ Branch 10 taken 12426 times.
✓ Branch 12 taken 113556 times.
✓ Branch 13 taken 570217 times.
827910 CHECK_BIDIR2(0,0,0,1)
1353
12/12
✓ Branch 0 taken 711792 times.
✓ Branch 1 taken 116118 times.
✓ Branch 2 taken 700831 times.
✓ Branch 3 taken 10961 times.
✓ Branch 5 taken 101180 times.
✓ Branch 6 taken 599651 times.
✓ Branch 7 taken 614063 times.
✓ Branch 8 taken 213847 times.
✓ Branch 9 taken 604255 times.
✓ Branch 10 taken 9808 times.
✓ Branch 12 taken 82977 times.
✓ Branch 13 taken 521278 times.
827910 CHECK_BIDIR2(0,0,1,0)
1354
12/12
✓ Branch 0 taken 658292 times.
✓ Branch 1 taken 169618 times.
✓ Branch 2 taken 641398 times.
✓ Branch 3 taken 16894 times.
✓ Branch 5 taken 92707 times.
✓ Branch 6 taken 548691 times.
✓ Branch 7 taken 557918 times.
✓ Branch 8 taken 269992 times.
✓ Branch 9 taken 540803 times.
✓ Branch 10 taken 17115 times.
✓ Branch 12 taken 60139 times.
✓ Branch 13 taken 480664 times.
827910 CHECK_BIDIR2(0,1,0,0)
1355
12/12
✓ Branch 0 taken 590344 times.
✓ Branch 1 taken 237566 times.
✓ Branch 2 taken 574737 times.
✓ Branch 3 taken 15607 times.
✓ Branch 5 taken 66390 times.
✓ Branch 6 taken 508347 times.
✓ Branch 7 taken 538438 times.
✓ Branch 8 taken 289472 times.
✓ Branch 9 taken 522460 times.
✓ Branch 10 taken 15978 times.
✓ Branch 12 taken 72688 times.
✓ Branch 13 taken 449772 times.
827910 CHECK_BIDIR2(1,0,0,0)
1356
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 827910 times.
827910 for(i=8; i<limit; i++){
1358 int fx= motion_fx+vect[i][0];
1359 int fy= motion_fy+vect[i][1];
1360 int bx= motion_bx+vect[i][2];
1361 int by= motion_by+vect[i][3];
1362 if(borderdist<=0){
1363 int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
1364 int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
1365 if((a|b) < 0)
1366 map[(hashidx+hash[i])&255] = 1;
1367 }
1368 if(!map[(hashidx+hash[i])&255]){
1369 int score;
1370 map[(hashidx+hash[i])&255] = 1;
1371 score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
1372 if(score < fbmin){
1373 hashidx += hash[i];
1374 fbmin= score;
1375 motion_fx=fx;
1376 motion_fy=fy;
1377 motion_bx=bx;
1378 motion_by=by;
1379 end=0;
1380 borderdist--;
1381 if(borderdist<=0){
1382 int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
1383 int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
1384 borderdist= FFMIN(a,b);
1385 }
1386 }
1387 }
1388 }
1389
2/2
✓ Branch 0 taken 419714 times.
✓ Branch 1 taken 408196 times.
827910 }while(!end);
1390 }
1391
1392 408196 s->b_bidir_forw_mv_table[xy][0]= motion_fx;
1393 408196 s->b_bidir_forw_mv_table[xy][1]= motion_fy;
1394 408196 s->b_bidir_back_mv_table[xy][0]= motion_bx;
1395 408196 s->b_bidir_back_mv_table[xy][1]= motion_by;
1396
1397 408196 return fbmin;
1398 }
1399
1400 191520 static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
1401 {
1402 191520 MotionEstContext * const c= &s->me;
1403 int P[10][2];
1404 191520 const int mot_stride = s->mb_stride;
1405 191520 const int mot_xy = mb_y*mot_stride + mb_x;
1406 191520 const int shift= 1+s->quarter_sample;
1407 int dmin, i;
1408 191520 const int time_pp= s->pp_time;
1409 191520 const int time_pb= s->pb_time;
1410 int mx, my, xmin, xmax, ymin, ymax;
1411 191520 int16_t (*mv_table)[2]= s->b_direct_mv_table;
1412
1413 191520 c->current_mv_penalty= c->mv_penalty[1] + MAX_DMV;
1414 191520 ymin= xmin=(-32)>>shift;
1415 191520 ymax= xmax= 31>>shift;
1416
1417
2/2
✓ Branch 0 taken 54656 times.
✓ Branch 1 taken 136864 times.
191520 if (IS_8X8(s->next_pic.mb_type[mot_xy])) {
1418 54656 s->mv_type= MV_TYPE_8X8;
1419 }else{
1420 136864 s->mv_type= MV_TYPE_16X16;
1421 }
1422
1423
2/2
✓ Branch 0 taken 355488 times.
✓ Branch 1 taken 54656 times.
410144 for(i=0; i<4; i++){
1424 355488 int index= s->block_index[i];
1425 int min, max;
1426
1427 355488 c->co_located_mv[i][0] = s->next_pic.motion_val[0][index][0];
1428 355488 c->co_located_mv[i][1] = s->next_pic.motion_val[0][index][1];
1429 355488 c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
1430 355488 c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
1431 // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
1432 // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
1433
1434
2/2
✓ Branch 0 taken 139772 times.
✓ Branch 1 taken 215716 times.
355488 max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1435
2/2
✓ Branch 0 taken 139772 times.
✓ Branch 1 taken 215716 times.
355488 min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
1436 355488 max+= 16*mb_x + 1; // +-1 is for the simpler rounding
1437 355488 min+= 16*mb_x - 1;
1438 355488 xmax= FFMIN(xmax, s->width - max);
1439 355488 xmin= FFMAX(xmin, - 16 - min);
1440
1441
2/2
✓ Branch 0 taken 141358 times.
✓ Branch 1 taken 214130 times.
355488 max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1442
2/2
✓ Branch 0 taken 141358 times.
✓ Branch 1 taken 214130 times.
355488 min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
1443 355488 max+= 16*mb_y + 1; // +-1 is for the simpler rounding
1444 355488 min+= 16*mb_y - 1;
1445 355488 ymax= FFMIN(ymax, s->height - max);
1446 355488 ymin= FFMAX(ymin, - 16 - min);
1447
1448
2/2
✓ Branch 0 taken 136864 times.
✓ Branch 1 taken 218624 times.
355488 if(s->mv_type == MV_TYPE_16X16) break;
1449 }
1450
1451 av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1452
1453
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){
1454 102 s->b_direct_mv_table[mot_xy][0]= 0;
1455 102 s->b_direct_mv_table[mot_xy][1]= 0;
1456
1457 102 return 256*256*256*64-1;
1458 }
1459
1460 191418 c->xmin= xmin;
1461 191418 c->ymin= ymin;
1462 191418 c->xmax= xmax;
1463 191418 c->ymax= ymax;
1464 191418 c->flags |= FLAG_DIRECT;
1465 191418 c->sub_flags |= FLAG_DIRECT;
1466 191418 c->pred_x=0;
1467 191418 c->pred_y=0;
1468
1469 191418 P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin * (1 << shift), xmax << shift);
1470 191418 P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin * (1 << shift), ymax << shift);
1471
1472 /* special case for first line */
1473
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
1474 178176 P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin * (1 << shift), xmax << shift);
1475 178176 P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin * (1 << shift), ymax << shift);
1476 178176 P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1][0], xmin * (1 << shift), xmax << shift);
1477 178176 P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1][1], ymin * (1 << shift), ymax << shift);
1478
1479 178176 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1480 178176 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1481 }
1482
1483 191418 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
1484
2/2
✓ Branch 0 taken 38282 times.
✓ Branch 1 taken 153136 times.
191418 if(c->sub_flags&FLAG_QPEL)
1485 38282 dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1486 else
1487 153136 dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
1488
1489
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)
1490 114823 dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
1491
1492 191418 get_limits(s, 16*mb_x, 16*mb_y, 1); //restore c->?min/max, maybe not needed
1493
1494 191418 mv_table[mot_xy][0]= mx;
1495 191418 mv_table[mot_xy][1]= my;
1496 191418 c->flags &= ~FLAG_DIRECT;
1497 191418 c->sub_flags &= ~FLAG_DIRECT;
1498
1499 191418 return dmin;
1500 }
1501
1502 408312 void ff_estimate_b_frame_motion(MpegEncContext * s,
1503 int mb_x, int mb_y)
1504 {
1505 408312 MotionEstContext * const c= &s->me;
1506 int fmin, bmin, dmin, fbmin, bimin, fimin;
1507 408312 int type=0;
1508 408312 const int xy = mb_y*s->mb_stride + mb_x;
1509 408312 init_ref(c, s->new_pic->data, s->last_pic.data,
1510 408312 s->next_pic.data, 16 * mb_x, 16 * mb_y, 2);
1511
1512 408312 get_limits(s, 16*mb_x, 16*mb_y, 1);
1513
1514 408312 c->skip=0;
1515
1516
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_pic.mbskip_table[xy]) {
1517 116 int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
1518
1519 116 score= ((unsigned)(score*score + 128*256))>>16;
1520 116 c->mc_mb_var_sum_temp += score;
1521 116 s->mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1522 116 s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
1523
1524 116 return;
1525 }
1526
1527 408196 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1528 408196 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1529 408196 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1530
1531
2/2
✓ Branch 0 taken 191404 times.
✓ Branch 1 taken 216792 times.
408196 if (s->codec_id == AV_CODEC_ID_MPEG4)
1532 191404 dmin= direct_search(s, mb_x, mb_y);
1533 else
1534 216792 dmin= INT_MAX;
1535
1536 // FIXME penalty stuff for non-MPEG-4
1537 408196 c->skip=0;
1538 408196 fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +
1539 408196 3 * c->mb_penalty_factor;
1540
1541 408196 c->skip=0;
1542 408196 bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) +
1543 408196 2 * c->mb_penalty_factor;
1544 ff_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1545
1546 408196 c->skip=0;
1547 408196 fbmin= bidir_refine(s, mb_x, mb_y) + c->mb_penalty_factor;
1548 ff_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1549
1550
2/2
✓ Branch 0 taken 115200 times.
✓ Branch 1 taken 292996 times.
408196 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
1551 //FIXME mb type penalty
1552 115200 c->skip=0;
1553 115200 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
1554 115200 fimin= interlaced_search(s, 0,
1555 115200 s->b_field_mv_table[0], s->b_field_select_table[0],
1556 115200 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
1557 115200 c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_DMV;
1558 115200 bimin= interlaced_search(s, 2,
1559 115200 s->b_field_mv_table[1], s->b_field_select_table[1],
1560 115200 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
1561 }else
1562 292996 fimin= bimin= INT_MAX;
1563
1564 {
1565 408196 int score= fmin;
1566 408196 type = CANDIDATE_MB_TYPE_FORWARD;
1567
1568
2/2
✓ Branch 0 taken 55256 times.
✓ Branch 1 taken 352940 times.
408196 if (dmin <= score){
1569 55256 score = dmin;
1570 55256 type = CANDIDATE_MB_TYPE_DIRECT;
1571 }
1572
2/2
✓ Branch 0 taken 204677 times.
✓ Branch 1 taken 203519 times.
408196 if(bmin<score){
1573 204677 score=bmin;
1574 204677 type= CANDIDATE_MB_TYPE_BACKWARD;
1575 }
1576
2/2
✓ Branch 0 taken 237217 times.
✓ Branch 1 taken 170979 times.
408196 if(fbmin<score){
1577 237217 score=fbmin;
1578 237217 type= CANDIDATE_MB_TYPE_BIDIR;
1579 }
1580
2/2
✓ Branch 0 taken 3902 times.
✓ Branch 1 taken 404294 times.
408196 if(fimin<score){
1581 3902 score=fimin;
1582 3902 type= CANDIDATE_MB_TYPE_FORWARD_I;
1583 }
1584
2/2
✓ Branch 0 taken 4095 times.
✓ Branch 1 taken 404101 times.
408196 if(bimin<score){
1585 4095 score=bimin;
1586 4095 type= CANDIDATE_MB_TYPE_BACKWARD_I;
1587 }
1588
1589 408196 score= ((unsigned)(score*score + 128*256))>>16;
1590 408196 c->mc_mb_var_sum_temp += score;
1591 408196 s->mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
1592 }
1593
1594
2/2
✓ Branch 0 taken 229852 times.
✓ Branch 1 taken 178344 times.
408196 if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
1595 229852 type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
1596
2/2
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 198862 times.
229852 if(fimin < INT_MAX)
1597 30990 type |= CANDIDATE_MB_TYPE_FORWARD_I;
1598
2/2
✓ Branch 0 taken 31154 times.
✓ Branch 1 taken 198698 times.
229852 if(bimin < INT_MAX)
1599 31154 type |= CANDIDATE_MB_TYPE_BACKWARD_I;
1600
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){
1601 26027 type |= CANDIDATE_MB_TYPE_BIDIR_I;
1602 }
1603 //FIXME something smarter
1604
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
1605
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 &&
1606
4/4
✓ Branch 0 taken 76517 times.
✓ Branch 1 taken 76534 times.
✓ Branch 2 taken 47388 times.
✓ Branch 3 taken 29129 times.
153051 s->mpv_flags & FF_MPV_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
1607 47388 type |= CANDIDATE_MB_TYPE_DIRECT0;
1608 }
1609
1610 408196 s->mb_type[mb_y*s->mb_stride + mb_x]= type;
1611 }
1612
1613 /* find best f_code for ME which do unlimited searches */
1614 12176 int ff_get_best_fcode(MpegEncContext * s, const int16_t (*mv_table)[2], int type)
1615 {
1616
1/2
✓ Branch 0 taken 12176 times.
✗ Branch 1 not taken.
12176 if (s->motion_est != FF_ME_ZERO) {
1617 int score[8];
1618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12176 times.
12176 int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
1619 12176 const uint8_t * fcode_tab = s->fcode_tab;
1620 12176 int best_fcode=-1;
1621 12176 int best_score=-10000000;
1622
1623
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 11434 times.
12176 if (s->msmpeg4_version != MSMP4_UNUSED)
1624 742 range= FFMIN(range, 16);
1625
4/4
✓ Branch 0 taken 5088 times.
✓ Branch 1 taken 6346 times.
✓ Branch 2 taken 4480 times.
✓ Branch 3 taken 608 times.
11434 else if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
1626 4480 range= FFMIN(range, 256);
1627
1628
2/2
✓ Branch 0 taken 97408 times.
✓ Branch 1 taken 12176 times.
109584 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
1629
1630
2/2
✓ Branch 0 taken 173593 times.
✓ Branch 1 taken 12176 times.
185769 for(y=0; y<s->mb_height; y++){
1631 int x;
1632 173593 int xy= y*s->mb_stride;
1633
2/2
✓ Branch 0 taken 3863995 times.
✓ Branch 1 taken 173593 times.
4037588 for(x=0; x<s->mb_width; x++, xy++){
1634
2/2
✓ Branch 0 taken 3151004 times.
✓ Branch 1 taken 712991 times.
3863995 if(s->mb_type[xy] & type){
1635 3151004 int mx= mv_table[xy][0];
1636 3151004 int my= mv_table[xy][1];
1637 3151004 int fcode= FFMAX(fcode_tab[mx + MAX_MV],
1638 fcode_tab[my + MAX_MV]);
1639 int j;
1640
1641
6/6
✓ Branch 0 taken 3150043 times.
✓ Branch 1 taken 961 times.
✓ Branch 2 taken 3149243 times.
✓ Branch 3 taken 800 times.
✓ Branch 4 taken 3148584 times.
✓ Branch 5 taken 659 times.
3151004 if (mx >= range || mx < -range ||
1642
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 3148125 times.
3148584 my >= range || my < -range)
1643 2879 continue;
1644
1645
3/4
✓ Branch 0 taken 3368594 times.
✓ Branch 1 taken 3148125 times.
✓ Branch 2 taken 3368594 times.
✗ Branch 3 not taken.
6516719 for(j=0; j<fcode && j<8; j++){
1646
2/2
✓ Branch 0 taken 2103857 times.
✓ Branch 1 taken 1264737 times.
3368594 if (s->pict_type == AV_PICTURE_TYPE_B ||
1647
2/2
✓ Branch 0 taken 1993538 times.
✓ Branch 1 taken 110319 times.
2103857 s->mc_mb_var[xy] < s->mb_var[xy])
1648 3258275 score[j]-= 170;
1649 }
1650 }
1651 }
1652 }
1653
1654
2/2
✓ Branch 0 taken 85232 times.
✓ Branch 1 taken 12176 times.
97408 for(i=1; i<8; i++){
1655
2/2
✓ Branch 0 taken 16281 times.
✓ Branch 1 taken 68951 times.
85232 if(score[i] > best_score){
1656 16281 best_score= score[i];
1657 16281 best_fcode= i;
1658 }
1659 }
1660
1661 12176 return best_fcode;
1662 }else{
1663 return 1;
1664 }
1665 }
1666
1667 6664 void ff_fix_long_p_mvs(MpegEncContext * s, int type)
1668 {
1669 6664 MotionEstContext * const c= &s->me;
1670 6664 const int f_code= s->f_code;
1671 int y, range;
1672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6664 times.
6664 av_assert0(s->pict_type==AV_PICTURE_TYPE_P);
1673
1674
4/4
✓ Branch 0 taken 3669 times.
✓ Branch 1 taken 2995 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 2927 times.
6664 range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version != MSMP4_UNUSED) ? 8 : 16) << f_code);
1675
1676
3/4
✓ Branch 0 taken 3778 times.
✓ Branch 1 taken 2886 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3778 times.
6664 av_assert0(range <= 16 || s->msmpeg4_version == MSMP4_UNUSED);
1677
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6664 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6664 av_assert0(range <=256 || !(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
1678
1679
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6664 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6664 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
1680
1681
2/2
✓ Branch 0 taken 927 times.
✓ Branch 1 taken 5737 times.
6664 if (s->avctx->flags & AV_CODEC_FLAG_4MV) {
1682 927 const int wrap= s->b8_stride;
1683
1684 /* clip / convert to intra 8x8 type MVs */
1685
2/2
✓ Branch 0 taken 13221 times.
✓ Branch 1 taken 927 times.
14148 for(y=0; y<s->mb_height; y++){
1686 13221 int xy= y*2*wrap;
1687 13221 int i= y*s->mb_stride;
1688 int x;
1689
1690
2/2
✓ Branch 0 taken 277695 times.
✓ Branch 1 taken 13221 times.
290916 for(x=0; x<s->mb_width; x++){
1691
2/2
✓ Branch 0 taken 210033 times.
✓ Branch 1 taken 67662 times.
277695 if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
1692 int block;
1693
2/2
✓ Branch 0 taken 840132 times.
✓ Branch 1 taken 210033 times.
1050165 for(block=0; block<4; block++){
1694 840132 int off= (block& 1) + (block>>1)*wrap;
1695 840132 int mx = s->cur_pic.motion_val[0][ xy + off ][0];
1696 840132 int my = s->cur_pic.motion_val[0][ xy + off ][1];
1697
1698
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
1699
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){
1700 1390 s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1701 1390 s->mb_type[i] |= type;
1702 1390 s->cur_pic.mb_type[i] = type;
1703 }
1704 }
1705 }
1706 277695 xy+=2;
1707 277695 i++;
1708 }
1709 }
1710 }
1711 6664 }
1712
1713 /**
1714 * @param truncate 1 for truncation, 0 for using intra
1715 */
1716 16452 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
1717 int16_t (*mv_table)[2], int f_code, int type, int truncate)
1718 {
1719 16452 MotionEstContext * const c= &s->me;
1720 int y, h_range, v_range;
1721
1722