motion_est.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  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. * @file
  26. * Motion estimation.
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <limits.h>
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #include "motion_est.h"
  35. #include "mpegutils.h"
  36. #include "mpegvideo.h"
  37. #define P_LEFT P[1]
  38. #define P_TOP P[2]
  39. #define P_TOPRIGHT P[3]
  40. #define P_MEDIAN P[4]
  41. #define P_MV1 P[9]
  42. #define ME_MAP_SHIFT 3
  43. #define ME_MAP_MV_BITS 11
  44. static int sad_hpel_motion_search(MpegEncContext * s,
  45. int *mx_ptr, int *my_ptr, int dmin,
  46. int src_index, int ref_index,
  47. int size, int h);
  48. static inline unsigned update_map_generation(MotionEstContext *c)
  49. {
  50. c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
  51. if(c->map_generation==0){
  52. c->map_generation= 1<<(ME_MAP_MV_BITS*2);
  53. memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
  54. }
  55. return c->map_generation;
  56. }
  57. /* shape adaptive search stuff */
  58. typedef struct Minima{
  59. int height;
  60. int x, y;
  61. int checked;
  62. }Minima;
  63. static int minima_cmp(const void *a, const void *b){
  64. const Minima *da = (const Minima *) a;
  65. const Minima *db = (const Minima *) b;
  66. return da->height - db->height;
  67. }
  68. #define FLAG_QPEL 1 //must be 1
  69. #define FLAG_CHROMA 2
  70. #define FLAG_DIRECT 4
  71. static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  72. const int offset[3]= {
  73. y*c-> stride + x,
  74. ((y*c->uvstride + x)>>1),
  75. ((y*c->uvstride + x)>>1),
  76. };
  77. int i;
  78. for(i=0; i<3; i++){
  79. c->src[0][i]= src [i] + offset[i];
  80. c->ref[0][i]= ref [i] + offset[i];
  81. }
  82. if(ref_index){
  83. for(i=0; i<3; i++){
  84. c->ref[ref_index][i]= ref2[i] + offset[i];
  85. }
  86. }
  87. }
  88. static int get_flags(MotionEstContext *c, int direct, int chroma){
  89. return ((c->avctx->flags&AV_CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
  90. + (direct ? FLAG_DIRECT : 0)
  91. + (chroma ? FLAG_CHROMA : 0);
  92. }
  93. static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  94. const int size, const int h, int ref_index, int src_index,
  95. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
  96. MotionEstContext * const c= &s->me;
  97. const int stride= c->stride;
  98. const int hx= subx + (x<<(1+qpel));
  99. const int hy= suby + (y<<(1+qpel));
  100. uint8_t * const * const ref= c->ref[ref_index];
  101. uint8_t * const * const src= c->src[src_index];
  102. int d;
  103. //FIXME check chroma 4mv, (no crashes ...)
  104. av_assert2(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
  105. if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
  106. const int time_pp= s->pp_time;
  107. const int time_pb= s->pb_time;
  108. const int mask= 2*qpel+1;
  109. if(s->mv_type==MV_TYPE_8X8){
  110. int i;
  111. for(i=0; i<4; i++){
  112. int fx = c->direct_basis_mv[i][0] + hx;
  113. int fy = c->direct_basis_mv[i][1] + hy;
  114. 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));
  115. 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));
  116. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  117. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  118. uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
  119. if(qpel){
  120. c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
  121. c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
  122. }else{
  123. c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
  124. c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
  125. }
  126. }
  127. }else{
  128. int fx = c->direct_basis_mv[0][0] + hx;
  129. int fy = c->direct_basis_mv[0][1] + hy;
  130. int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
  131. int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
  132. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  133. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  134. if(qpel){
  135. c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
  136. c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
  137. c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
  138. c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
  139. c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
  140. c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
  141. c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
  142. c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
  143. }else{
  144. av_assert2((fx>>1) + 16*s->mb_x >= -16);
  145. av_assert2((fy>>1) + 16*s->mb_y >= -16);
  146. av_assert2((fx>>1) + 16*s->mb_x <= s->width);
  147. av_assert2((fy>>1) + 16*s->mb_y <= s->height);
  148. av_assert2((bx>>1) + 16*s->mb_x >= -16);
  149. av_assert2((by>>1) + 16*s->mb_y >= -16);
  150. av_assert2((bx>>1) + 16*s->mb_x <= s->width);
  151. av_assert2((by>>1) + 16*s->mb_y <= s->height);
  152. c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
  153. c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
  154. }
  155. }
  156. d = cmp_func(s, c->temp, src[0], stride, 16);
  157. }else
  158. d= 256*256*256*32;
  159. return d;
  160. }
  161. static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  162. const int size, const int h, int ref_index, int src_index,
  163. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){
  164. MotionEstContext * const c= &s->me;
  165. const int stride= c->stride;
  166. const int uvstride= c->uvstride;
  167. const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
  168. const int hx= subx + x*(1<<(1+qpel));
  169. const int hy= suby + y*(1<<(1+qpel));
  170. uint8_t * const * const ref= c->ref[ref_index];
  171. uint8_t * const * const src= c->src[src_index];
  172. int d;
  173. //FIXME check chroma 4mv, (no crashes ...)
  174. int uvdxy; /* no, it might not be used uninitialized */
  175. if(dxy){
  176. if(qpel){
  177. if (h << size == 16) {
  178. c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
  179. } else if (size == 0 && h == 8) {
  180. c->qpel_put[1][dxy](c->temp , ref[0] + x + y*stride , stride);
  181. c->qpel_put[1][dxy](c->temp + 8, ref[0] + x + y*stride + 8, stride);
  182. } else
  183. av_assert2(0);
  184. if(chroma){
  185. int cx= hx/2;
  186. int cy= hy/2;
  187. cx= (cx>>1)|(cx&1);
  188. cy= (cy>>1)|(cy&1);
  189. uvdxy= (cx&1) + 2*(cy&1);
  190. // 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
  191. }
  192. }else{
  193. c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
  194. if(chroma)
  195. uvdxy= dxy | (x&1) | (2*(y&1));
  196. }
  197. d = cmp_func(s, c->temp, src[0], stride, h);
  198. }else{
  199. d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
  200. if(chroma)
  201. uvdxy= (x&1) + 2*(y&1);
  202. }
  203. if(chroma){
  204. uint8_t * const uvtemp= c->temp + 16*stride;
  205. c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  206. c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  207. d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
  208. d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
  209. }
  210. return d;
  211. }
  212. static int cmp_simple(MpegEncContext *s, const int x, const int y,
  213. int ref_index, int src_index,
  214. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){
  215. return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
  216. }
  217. static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,
  218. const int size, const int h, int ref_index, int src_index,
  219. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  220. if(flags&FLAG_DIRECT){
  221. return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
  222. }else{
  223. return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
  224. }
  225. }
  226. static int cmp_internal(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  227. const int size, const int h, int ref_index, int src_index,
  228. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  229. if(flags&FLAG_DIRECT){
  230. return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
  231. }else{
  232. 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);
  233. }
  234. }
  235. /** @brief compares a block (either a full macroblock or a partition thereof)
  236. against a proposed motion-compensated prediction of that block
  237. */
  238. static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  239. const int size, const int h, int ref_index, int src_index,
  240. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  241. if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)
  242. && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
  243. && flags==0 && h==16 && size==0 && subx==0 && suby==0){
  244. return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
  245. }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
  246. && subx==0 && suby==0){
  247. return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
  248. }else{
  249. return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
  250. }
  251. }
  252. static int cmp_hpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  253. const int size, const int h, int ref_index, int src_index,
  254. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  255. if(flags&FLAG_DIRECT){
  256. return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
  257. }else{
  258. return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
  259. }
  260. }
  261. static int cmp_qpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  262. const int size, const int h, int ref_index, int src_index,
  263. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  264. if(flags&FLAG_DIRECT){
  265. return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
  266. }else{
  267. return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA);
  268. }
  269. }
  270. #include "motion_est_template.c"
  271. static int zero_cmp(MpegEncContext *s, uint8_t *a, uint8_t *b,
  272. ptrdiff_t stride, int h)
  273. {
  274. return 0;
  275. }
  276. static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
  277. }
  278. int ff_init_me(MpegEncContext *s){
  279. MotionEstContext * const c= &s->me;
  280. int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
  281. int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
  282. if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)){
  283. av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
  284. return -1;
  285. }
  286. c->avctx= s->avctx;
  287. if(s->codec_id == AV_CODEC_ID_H261)
  288. c->avctx->me_sub_cmp = c->avctx->me_cmp;
  289. if(cache_size < 2*dia_size && !c->stride){
  290. av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
  291. }
  292. ff_set_cmp(&s->mecc, s->mecc.me_pre_cmp, c->avctx->me_pre_cmp);
  293. ff_set_cmp(&s->mecc, s->mecc.me_cmp, c->avctx->me_cmp);
  294. ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, c->avctx->me_sub_cmp);
  295. ff_set_cmp(&s->mecc, s->mecc.mb_cmp, c->avctx->mb_cmp);
  296. c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
  297. c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
  298. c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
  299. /*FIXME s->no_rounding b_type*/
  300. if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
  301. c->sub_motion_search= qpel_motion_search;
  302. c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
  303. if (s->no_rounding)
  304. c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
  305. else
  306. c->qpel_put = s->qdsp.put_qpel_pixels_tab;
  307. }else{
  308. if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
  309. c->sub_motion_search= hpel_motion_search;
  310. else if( c->avctx->me_sub_cmp == FF_CMP_SAD
  311. && c->avctx-> me_cmp == FF_CMP_SAD
  312. && c->avctx-> mb_cmp == FF_CMP_SAD)
  313. c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
  314. else
  315. c->sub_motion_search= hpel_motion_search;
  316. }
  317. c->hpel_avg = s->hdsp.avg_pixels_tab;
  318. if (s->no_rounding)
  319. c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
  320. else
  321. c->hpel_put = s->hdsp.put_pixels_tab;
  322. if(s->linesize){
  323. c->stride = s->linesize;
  324. c->uvstride= s->uvlinesize;
  325. }else{
  326. c->stride = 16*s->mb_width + 32;
  327. c->uvstride= 8*s->mb_width + 16;
  328. }
  329. /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
  330. * not have yet, and even if we had, the motion estimation code
  331. * does not expect it. */
  332. if (s->codec_id != AV_CODEC_ID_SNOW) {
  333. if ((c->avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
  334. s->mecc.me_cmp[2] = zero_cmp;
  335. if ((c->avctx->me_sub_cmp & FF_CMP_CHROMA) && !s->mecc.me_sub_cmp[2])
  336. s->mecc.me_sub_cmp[2] = zero_cmp;
  337. c->hpel_put[2][0]= c->hpel_put[2][1]=
  338. c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
  339. }
  340. if(s->codec_id == AV_CODEC_ID_H261){
  341. c->sub_motion_search= no_sub_motion_search;
  342. }
  343. return 0;
  344. }
  345. #define CHECK_SAD_HALF_MV(suffix, x, y) \
  346. {\
  347. d = s->mecc.pix_abs[size][(x ? 1 : 0) + (y ? 2 : 0)](NULL, pix, ptr + ((x) >> 1), stride, h); \
  348. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
  349. COPY3_IF_LT(dminh, d, dx, x, dy, y)\
  350. }
  351. static int sad_hpel_motion_search(MpegEncContext * s,
  352. int *mx_ptr, int *my_ptr, int dmin,
  353. int src_index, int ref_index,
  354. int size, int h)
  355. {
  356. MotionEstContext * const c= &s->me;
  357. const int penalty_factor= c->sub_penalty_factor;
  358. int mx, my, dminh;
  359. uint8_t *pix, *ptr;
  360. int stride= c->stride;
  361. LOAD_COMMON
  362. av_assert2(c->sub_flags == 0);
  363. if(c->skip){
  364. *mx_ptr = 0;
  365. *my_ptr = 0;
  366. return dmin;
  367. }
  368. pix = c->src[src_index][0];
  369. mx = *mx_ptr;
  370. my = *my_ptr;
  371. ptr = c->ref[ref_index][0] + (my * stride) + mx;
  372. dminh = dmin;
  373. if (mx > xmin && mx < xmax &&
  374. my > ymin && my < ymax) {
  375. int dx=0, dy=0;
  376. int d, pen_x, pen_y;
  377. const int index= my*(1<<ME_MAP_SHIFT) + mx;
  378. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  379. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  380. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  381. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  382. mx += mx;
  383. my += my;
  384. pen_x= pred_x + mx;
  385. pen_y= pred_y + my;
  386. ptr-= stride;
  387. if(t<=b){
  388. CHECK_SAD_HALF_MV(y2 , 0, -1)
  389. if(l<=r){
  390. CHECK_SAD_HALF_MV(xy2, -1, -1)
  391. if(t+r<=b+l){
  392. CHECK_SAD_HALF_MV(xy2, +1, -1)
  393. ptr+= stride;
  394. }else{
  395. ptr+= stride;
  396. CHECK_SAD_HALF_MV(xy2, -1, +1)
  397. }
  398. CHECK_SAD_HALF_MV(x2 , -1, 0)
  399. }else{
  400. CHECK_SAD_HALF_MV(xy2, +1, -1)
  401. if(t+l<=b+r){
  402. CHECK_SAD_HALF_MV(xy2, -1, -1)
  403. ptr+= stride;
  404. }else{
  405. ptr+= stride;
  406. CHECK_SAD_HALF_MV(xy2, +1, +1)
  407. }
  408. CHECK_SAD_HALF_MV(x2 , +1, 0)
  409. }
  410. }else{
  411. if(l<=r){
  412. if(t+l<=b+r){
  413. CHECK_SAD_HALF_MV(xy2, -1, -1)
  414. ptr+= stride;
  415. }else{
  416. ptr+= stride;
  417. CHECK_SAD_HALF_MV(xy2, +1, +1)
  418. }
  419. CHECK_SAD_HALF_MV(x2 , -1, 0)
  420. CHECK_SAD_HALF_MV(xy2, -1, +1)
  421. }else{
  422. if(t+r<=b+l){
  423. CHECK_SAD_HALF_MV(xy2, +1, -1)
  424. ptr+= stride;
  425. }else{
  426. ptr+= stride;
  427. CHECK_SAD_HALF_MV(xy2, -1, +1)
  428. }
  429. CHECK_SAD_HALF_MV(x2 , +1, 0)
  430. CHECK_SAD_HALF_MV(xy2, +1, +1)
  431. }
  432. CHECK_SAD_HALF_MV(y2 , 0, +1)
  433. }
  434. mx+=dx;
  435. my+=dy;
  436. }else{
  437. mx += mx;
  438. my += my;
  439. }
  440. *mx_ptr = mx;
  441. *my_ptr = my;
  442. return dminh;
  443. }
  444. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  445. {
  446. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  447. s->p_mv_table[xy][0] = mx;
  448. s->p_mv_table[xy][1] = my;
  449. /* has already been set to the 4 MV if 4MV is done */
  450. if(mv4){
  451. int mot_xy= s->block_index[0];
  452. s->current_picture.motion_val[0][mot_xy ][0] = mx;
  453. s->current_picture.motion_val[0][mot_xy ][1] = my;
  454. s->current_picture.motion_val[0][mot_xy + 1][0] = mx;
  455. s->current_picture.motion_val[0][mot_xy + 1][1] = my;
  456. mot_xy += s->b8_stride;
  457. s->current_picture.motion_val[0][mot_xy ][0] = mx;
  458. s->current_picture.motion_val[0][mot_xy ][1] = my;
  459. s->current_picture.motion_val[0][mot_xy + 1][0] = mx;
  460. s->current_picture.motion_val[0][mot_xy + 1][1] = my;
  461. }
  462. }
  463. /**
  464. * get fullpel ME search limits.
  465. */
  466. static inline void get_limits(MpegEncContext *s, int x, int y)
  467. {
  468. MotionEstContext * const c= &s->me;
  469. int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
  470. int max_range = MAX_MV >> (1 + !!(c->flags&FLAG_QPEL));
  471. /*
  472. if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
  473. else c->range= 16;
  474. */
  475. if (s->unrestricted_mv) {
  476. c->xmin = - x - 16;
  477. c->ymin = - y - 16;
  478. c->xmax = - x + s->width;
  479. c->ymax = - y + s->height;
  480. } else if (s->out_format == FMT_H261){
  481. // Search range of H.261 is different from other codec standards
  482. c->xmin = (x > 15) ? - 15 : 0;
  483. c->ymin = (y > 15) ? - 15 : 0;
  484. c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
  485. c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
  486. } else {
  487. c->xmin = - x;
  488. c->ymin = - y;
  489. c->xmax = - x + s->mb_width *16 - 16;
  490. c->ymax = - y + s->mb_height*16 - 16;
  491. }
  492. if(!range || range > max_range)
  493. range = max_range;
  494. if(range){
  495. c->xmin = FFMAX(c->xmin,-range);
  496. c->xmax = FFMIN(c->xmax, range);
  497. c->ymin = FFMAX(c->ymin,-range);
  498. c->ymax = FFMIN(c->ymax, range);
  499. }
  500. }
  501. static inline void init_mv4_ref(MotionEstContext *c){
  502. const int stride= c->stride;
  503. c->ref[1][0] = c->ref[0][0] + 8;
  504. c->ref[2][0] = c->ref[0][0] + 8*stride;
  505. c->ref[3][0] = c->ref[2][0] + 8;
  506. c->src[1][0] = c->src[0][0] + 8;
  507. c->src[2][0] = c->src[0][0] + 8*stride;
  508. c->src[3][0] = c->src[2][0] + 8;
  509. }
  510. static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
  511. {
  512. MotionEstContext * const c= &s->me;
  513. const int size= 1;
  514. const int h=8;
  515. int block;
  516. int P[10][2];
  517. int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
  518. int same=1;
  519. const int stride= c->stride;
  520. uint8_t *mv_penalty= c->current_mv_penalty;
  521. int safety_clipping= s->unrestricted_mv && (s->width&15) && (s->height&15);
  522. init_mv4_ref(c);
  523. for(block=0; block<4; block++){
  524. int mx4, my4;
  525. int pred_x4, pred_y4;
  526. int dmin4;
  527. static const int off[4]= {2, 1, 1, -1};
  528. const int mot_stride = s->b8_stride;
  529. const int mot_xy = s->block_index[block];
  530. if(safety_clipping){
  531. c->xmax = - 16*s->mb_x + s->width - 8*(block &1);
  532. c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
  533. }
  534. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  535. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  536. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  537. /* special case for first line */
  538. if (s->first_slice_line && block<2) {
  539. c->pred_x= pred_x4= P_LEFT[0];
  540. c->pred_y= pred_y4= P_LEFT[1];
  541. } else {
  542. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  543. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  544. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
  545. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
  546. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  547. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  548. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  549. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  550. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  551. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  552. c->pred_x= pred_x4 = P_MEDIAN[0];
  553. c->pred_y= pred_y4 = P_MEDIAN[1];
  554. }
  555. P_MV1[0]= mx;
  556. P_MV1[1]= my;
  557. if(safety_clipping)
  558. for(i=1; i<10; i++){
  559. if (s->first_slice_line && block<2 && i>1 && i<9)
  560. continue;
  561. if (i>4 && i<9)
  562. continue;
  563. if(P[i][0] > (c->xmax<<shift)) P[i][0]= (c->xmax<<shift);
  564. if(P[i][1] > (c->ymax<<shift)) P[i][1]= (c->ymax<<shift);
  565. }
  566. dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
  567. dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
  568. if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
  569. int dxy;
  570. const int offset= ((block&1) + (block>>1)*stride)*8;
  571. uint8_t *dest_y = c->scratchpad + offset;
  572. if(s->quarter_sample){
  573. uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
  574. dxy = ((my4 & 3) << 2) | (mx4 & 3);
  575. if(s->no_rounding)
  576. s->qdsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y, ref, stride);
  577. else
  578. s->qdsp.put_qpel_pixels_tab[1][dxy](dest_y, ref, stride);
  579. }else{
  580. uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
  581. dxy = ((my4 & 1) << 1) | (mx4 & 1);
  582. if(s->no_rounding)
  583. s->hdsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
  584. else
  585. s->hdsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
  586. }
  587. dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
  588. }else
  589. dmin_sum+= dmin4;
  590. if(s->quarter_sample){
  591. mx4_sum+= mx4/2;
  592. my4_sum+= my4/2;
  593. }else{
  594. mx4_sum+= mx4;
  595. my4_sum+= my4;
  596. }
  597. s->current_picture.motion_val[0][s->block_index[block]][0] = mx4;
  598. s->current_picture.motion_val[0][s->block_index[block]][1] = my4;
  599. if(mx4 != mx || my4 != my) same=0;
  600. }
  601. if(same)
  602. return INT_MAX;
  603. if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
  604. dmin_sum += s->mecc.mb_cmp[0](s,
  605. s->new_picture.f->data[0] +
  606. s->mb_x * 16 + s->mb_y * 16 * stride,
  607. c->scratchpad, stride, 16);
  608. }
  609. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  610. int dxy;
  611. int mx, my;
  612. int offset;
  613. mx= ff_h263_round_chroma(mx4_sum);
  614. my= ff_h263_round_chroma(my4_sum);
  615. dxy = ((my & 1) << 1) | (mx & 1);
  616. offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
  617. if(s->no_rounding){
  618. s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.f->data[1] + offset, s->uvlinesize, 8);
  619. s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f->data[2] + offset, s->uvlinesize, 8);
  620. }else{
  621. s->hdsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.f->data[1] + offset, s->uvlinesize, 8);
  622. s->hdsp.put_pixels_tab [1][dxy](c->scratchpad + 8, s->last_picture.f->data[2] + offset, s->uvlinesize, 8);
  623. }
  624. dmin_sum += s->mecc.mb_cmp[1](s, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad, s->uvlinesize, 8);
  625. dmin_sum += s->mecc.mb_cmp[1](s, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad + 8, s->uvlinesize, 8);
  626. }
  627. c->pred_x= mx;
  628. c->pred_y= my;
  629. switch(c->avctx->mb_cmp&0xFF){
  630. /*case FF_CMP_SSE:
  631. return dmin_sum+ 32*s->qscale*s->qscale;*/
  632. case FF_CMP_RD:
  633. return dmin_sum;
  634. default:
  635. return dmin_sum+ 11*c->mb_penalty_factor;
  636. }
  637. }
  638. static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
  639. MotionEstContext * const c= &s->me;
  640. c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
  641. c->src[1][0] = c->src[0][0] + s->linesize;
  642. if(c->flags & FLAG_CHROMA){
  643. c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
  644. c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
  645. c->src[1][1] = c->src[0][1] + s->uvlinesize;
  646. c->src[1][2] = c->src[0][2] + s->uvlinesize;
  647. }
  648. }
  649. static int interlaced_search(MpegEncContext *s, int ref_index,
  650. int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  651. {
  652. MotionEstContext * const c= &s->me;
  653. const int size=0;
  654. const int h=8;
  655. int block;
  656. int P[10][2];
  657. uint8_t * const mv_penalty= c->current_mv_penalty;
  658. int same=1;
  659. const int stride= 2*s->linesize;
  660. int dmin_sum= 0;
  661. const int mot_stride= s->mb_stride;
  662. const int xy= s->mb_x + s->mb_y*mot_stride;
  663. c->ymin>>=1;
  664. c->ymax>>=1;
  665. c->stride<<=1;
  666. c->uvstride<<=1;
  667. init_interlaced_ref(s, ref_index);
  668. for(block=0; block<2; block++){
  669. int field_select;
  670. int best_dmin= INT_MAX;
  671. int best_field= -1;
  672. for(field_select=0; field_select<2; field_select++){
  673. int dmin, mx_i, my_i;
  674. int16_t (*mv_table)[2]= mv_tables[block][field_select];
  675. if(user_field_select){
  676. av_assert1(field_select==0 || field_select==1);
  677. av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
  678. if(field_select_tables[block][xy] != field_select)
  679. continue;
  680. }
  681. P_LEFT[0] = mv_table[xy - 1][0];
  682. P_LEFT[1] = mv_table[xy - 1][1];
  683. if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
  684. c->pred_x= P_LEFT[0];
  685. c->pred_y= P_LEFT[1];
  686. if(!s->first_slice_line){
  687. P_TOP[0] = mv_table[xy - mot_stride][0];
  688. P_TOP[1] = mv_table[xy - mot_stride][1];
  689. P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
  690. P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
  691. if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
  692. if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
  693. if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
  694. if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
  695. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  696. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  697. }
  698. P_MV1[0]= mx; //FIXME not correct if block != field_select
  699. P_MV1[1]= my / 2;
  700. dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
  701. dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
  702. mv_table[xy][0]= mx_i;
  703. mv_table[xy][1]= my_i;
  704. if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
  705. int dxy;
  706. //FIXME chroma ME
  707. uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
  708. dxy = ((my_i & 1) << 1) | (mx_i & 1);
  709. if(s->no_rounding){
  710. s->hdsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
  711. }else{
  712. s->hdsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
  713. }
  714. dmin = s->mecc.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
  715. dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
  716. }else
  717. dmin+= c->mb_penalty_factor; //field_select bits
  718. dmin += field_select != block; //slightly prefer same field
  719. if(dmin < best_dmin){
  720. best_dmin= dmin;
  721. best_field= field_select;
  722. }
  723. }
  724. {
  725. int16_t (*mv_table)[2]= mv_tables[block][best_field];
  726. if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
  727. if(mv_table[xy][1]&1) same=0;
  728. if(mv_table[xy][1]*2 != my) same=0;
  729. if(best_field != block) same=0;
  730. }
  731. field_select_tables[block][xy]= best_field;
  732. dmin_sum += best_dmin;
  733. }
  734. c->ymin<<=1;
  735. c->ymax<<=1;
  736. c->stride>>=1;
  737. c->uvstride>>=1;
  738. if(same)
  739. return INT_MAX;
  740. switch(c->avctx->mb_cmp&0xFF){
  741. /*case FF_CMP_SSE:
  742. return dmin_sum+ 32*s->qscale*s->qscale;*/
  743. case FF_CMP_RD:
  744. return dmin_sum;
  745. default:
  746. return dmin_sum+ 11*c->mb_penalty_factor;
  747. }
  748. }
  749. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  750. switch(type&0xFF){
  751. default:
  752. case FF_CMP_SAD:
  753. return lambda>>FF_LAMBDA_SHIFT;
  754. case FF_CMP_DCT:
  755. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  756. case FF_CMP_W53:
  757. return (4*lambda)>>(FF_LAMBDA_SHIFT);
  758. case FF_CMP_W97:
  759. return (2*lambda)>>(FF_LAMBDA_SHIFT);
  760. case FF_CMP_SATD:
  761. case FF_CMP_DCT264:
  762. return (2*lambda)>>FF_LAMBDA_SHIFT;
  763. case FF_CMP_RD:
  764. case FF_CMP_PSNR:
  765. case FF_CMP_SSE:
  766. case FF_CMP_NSSE:
  767. return lambda2>>FF_LAMBDA_SHIFT;
  768. case FF_CMP_BIT:
  769. case FF_CMP_MEDIAN_SAD:
  770. return 1;
  771. }
  772. }
  773. void ff_estimate_p_frame_motion(MpegEncContext * s,
  774. int mb_x, int mb_y)
  775. {
  776. MotionEstContext * const c= &s->me;
  777. uint8_t *pix, *ppix;
  778. int sum, mx = 0, my = 0, dmin = 0;
  779. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  780. int vard; ///< sum of squared differences with the estimated motion vector
  781. int P[10][2];
  782. const int shift= 1+s->quarter_sample;
  783. int mb_type=0;
  784. Picture * const pic= &s->current_picture;
  785. init_ref(c, s->new_picture.f->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);
  786. av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
  787. av_assert0(s->linesize == c->stride);
  788. av_assert0(s->uvlinesize == c->uvstride);
  789. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  790. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  791. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  792. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
  793. get_limits(s, 16*mb_x, 16*mb_y);
  794. c->skip=0;
  795. /* intra / predictive decision */
  796. pix = c->src[0][0];
  797. sum = s->mpvencdsp.pix_sum(pix, s->linesize);
  798. varc = s->mpvencdsp.pix_norm1(pix, s->linesize) -
  799. (((unsigned) sum * sum) >> 8) + 500;
  800. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  801. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  802. c->mb_var_sum_temp += (varc+128)>>8;
  803. if (s->motion_est != FF_ME_ZERO) {
  804. const int mot_stride = s->b8_stride;
  805. const int mot_xy = s->block_index[0];
  806. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  807. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  808. if (P_LEFT[0] > (c->xmax << shift))
  809. P_LEFT[0] = c->xmax << shift;
  810. if (!s->first_slice_line) {
  811. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  812. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  813. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
  814. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
  815. if (P_TOP[1] > (c->ymax << shift))
  816. P_TOP[1] = c->ymax << shift;
  817. if (P_TOPRIGHT[0] < (c->xmin * (1 << shift)))
  818. P_TOPRIGHT[0] = c->xmin * (1 << shift);
  819. if (P_TOPRIGHT[1] > (c->ymax * (1 << shift)))
  820. P_TOPRIGHT[1] = c->ymax * (1 << shift);
  821. P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  822. P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  823. if (s->out_format == FMT_H263) {
  824. c->pred_x = P_MEDIAN[0];
  825. c->pred_y = P_MEDIAN[1];
  826. } else { /* MPEG-1 at least */
  827. c->pred_x = P_LEFT[0];
  828. c->pred_y = P_LEFT[1];
  829. }
  830. } else {
  831. c->pred_x = P_LEFT[0];
  832. c->pred_y = P_LEFT[1];
  833. }
  834. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  835. }
  836. /* At this point (mx,my) are full-pell and the relative displacement */
  837. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  838. vard = s->mecc.sse[0](NULL, pix, ppix, s->linesize, 16);
  839. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  840. c->mc_mb_var_sum_temp += (vard+128)>>8;
  841. if (c->avctx->mb_decision > FF_MB_DECISION_SIMPLE) {
  842. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  843. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  844. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  845. if (vard*2 + 200*256 > varc)
  846. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  847. if (varc*2 + 200*256 > vard || s->qscale > 24){
  848. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  849. mb_type|= CANDIDATE_MB_TYPE_INTER;
  850. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  851. if (s->mpv_flags & FF_MPV_FLAG_MV0)
  852. if(mx || my)
  853. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  854. }else{
  855. mx <<=shift;
  856. my <<=shift;
  857. }
  858. if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
  859. && !c->skip && varc>50<<8 && vard>10<<8){
  860. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  861. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  862. set_p_mv_tables(s, mx, my, 0);
  863. }else
  864. set_p_mv_tables(s, mx, my, 1);
  865. if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
  866. && !c->skip){ //FIXME varc/d checks
  867. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  868. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  869. }
  870. }else{
  871. int intra_score, i;
  872. mb_type= CANDIDATE_MB_TYPE_INTER;
  873. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  874. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  875. dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  876. if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
  877. && !c->skip && varc>50<<8 && vard>10<<8){
  878. int dmin4= h263_mv4_search(s, mx, my, shift);
  879. if(dmin4 < dmin){
  880. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  881. dmin=dmin4;
  882. }
  883. }
  884. if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
  885. && !c->skip){ //FIXME varc/d checks
  886. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  887. if(dmin_i < dmin){
  888. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  889. dmin= dmin_i;
  890. }
  891. }
  892. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  893. /* get intra luma score */
  894. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  895. intra_score= varc - 500;
  896. }else{
  897. unsigned mean = (sum+128)>>8;
  898. mean*= 0x01010101;
  899. for(i=0; i<16; i++){
  900. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  901. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  902. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  903. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  904. }
  905. intra_score= s->mecc.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  906. }
  907. intra_score += c->mb_penalty_factor*16;
  908. if(intra_score < dmin){
  909. mb_type= CANDIDATE_MB_TYPE_INTRA;
  910. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  911. }else
  912. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = 0;
  913. {
  914. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  915. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  916. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  917. }
  918. }
  919. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  920. }
  921. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  922. int mb_x, int mb_y)
  923. {
  924. MotionEstContext * const c= &s->me;
  925. int mx, my, dmin;
  926. int P[10][2];
  927. const int shift= 1+s->quarter_sample;
  928. const int xy= mb_x + mb_y*s->mb_stride;
  929. init_ref(c, s->new_picture.f->data, s->last_picture.f->data, NULL, 16*mb_x, 16*mb_y, 0);
  930. av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
  931. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  932. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
  933. get_limits(s, 16*mb_x, 16*mb_y);
  934. c->skip=0;
  935. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  936. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  937. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  938. /* special case for first line */
  939. if (s->first_slice_line) {
  940. c->pred_x= P_LEFT[0];
  941. c->pred_y= P_LEFT[1];
  942. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  943. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  944. } else {
  945. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  946. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  947. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  948. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  949. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  950. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  951. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  952. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  953. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  954. c->pred_x = P_MEDIAN[0];
  955. c->pred_y = P_MEDIAN[1];
  956. }
  957. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  958. s->p_mv_table[xy][0] = mx<<shift;
  959. s->p_mv_table[xy][1] = my<<shift;
  960. return dmin;
  961. }
  962. static int estimate_motion_b(MpegEncContext *s, int mb_x, int mb_y,
  963. int16_t (*mv_table)[2], int ref_index, int f_code)
  964. {
  965. MotionEstContext * const c= &s->me;
  966. int mx = 0, my = 0, dmin = 0;
  967. int P[10][2];
  968. const int shift= 1+s->quarter_sample;
  969. const int mot_stride = s->mb_stride;
  970. const int mot_xy = mb_y*mot_stride + mb_x;
  971. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_DMV;
  972. int mv_scale;
  973. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  974. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  975. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  976. c->current_mv_penalty= mv_penalty;
  977. get_limits(s, 16*mb_x, 16*mb_y);
  978. if (s->motion_est != FF_ME_ZERO) {
  979. P_LEFT[0] = mv_table[mot_xy - 1][0];
  980. P_LEFT[1] = mv_table[mot_xy - 1][1];
  981. if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
  982. /* special case for first line */
  983. if (!s->first_slice_line) {
  984. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  985. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  986. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
  987. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
  988. if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
  989. if (P_TOPRIGHT[0] < (c->xmin << shift)) P_TOPRIGHT[0] = (c->xmin << shift);
  990. if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
  991. P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  992. P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  993. }
  994. c->pred_x = P_LEFT[0];
  995. c->pred_y = P_LEFT[1];
  996. if(mv_table == s->b_forw_mv_table){
  997. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  998. }else{
  999. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1000. }
  1001. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1002. }
  1003. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1004. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1005. dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1006. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1007. mv_table[mot_xy][0]= mx;
  1008. mv_table[mot_xy][1]= my;
  1009. return dmin;
  1010. }
  1011. static inline int check_bidir_mv(MpegEncContext * s,
  1012. int motion_fx, int motion_fy,
  1013. int motion_bx, int motion_by,
  1014. int pred_fx, int pred_fy,
  1015. int pred_bx, int pred_by,
  1016. int size, int h)
  1017. {
  1018. //FIXME optimize?
  1019. //FIXME better f_code prediction (max mv & distance)
  1020. //FIXME pointers
  1021. MotionEstContext * const c= &s->me;
  1022. uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_DMV; // f_code of the prev frame
  1023. uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_DMV; // f_code of the prev frame
  1024. int stride= c->stride;
  1025. uint8_t *dest_y = c->scratchpad;
  1026. uint8_t *ptr;
  1027. int dxy;
  1028. int src_x, src_y;
  1029. int fbmin;
  1030. uint8_t **src_data= c->src[0];
  1031. uint8_t **ref_data= c->ref[0];
  1032. uint8_t **ref2_data= c->ref[2];
  1033. if(s->quarter_sample){
  1034. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1035. src_x = motion_fx >> 2;
  1036. src_y = motion_fy >> 2;
  1037. ptr = ref_data[0] + (src_y * stride) + src_x;
  1038. s->qdsp.put_qpel_pixels_tab[0][dxy](dest_y, ptr, stride);
  1039. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1040. src_x = motion_bx >> 2;
  1041. src_y = motion_by >> 2;
  1042. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1043. s->qdsp.avg_qpel_pixels_tab[size][dxy](dest_y, ptr, stride);
  1044. }else{
  1045. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1046. src_x = motion_fx >> 1;
  1047. src_y = motion_fy >> 1;
  1048. ptr = ref_data[0] + (src_y * stride) + src_x;
  1049. s->hdsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1050. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1051. src_x = motion_bx >> 1;
  1052. src_y = motion_by >> 1;
  1053. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1054. s->hdsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1055. }
  1056. fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
  1057. +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
  1058. + s->mecc.mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
  1059. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1060. }
  1061. //FIXME CHROMA !!!
  1062. return fbmin;
  1063. }
  1064. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1065. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1066. {
  1067. MotionEstContext * const c= &s->me;
  1068. const int mot_stride = s->mb_stride;
  1069. const int xy = mb_y *mot_stride + mb_x;
  1070. int fbmin;
  1071. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1072. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1073. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1074. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1075. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1076. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1077. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1078. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1079. const int flags= c->sub_flags;
  1080. const int qpel= flags&FLAG_QPEL;
  1081. const int shift= 1+qpel;
  1082. const int xmin= c->xmin<<shift;
  1083. const int ymin= c->ymin<<shift;
  1084. const int xmax= c->xmax<<shift;
  1085. const int ymax= c->ymax<<shift;
  1086. #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
  1087. #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
  1088. int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
  1089. uint8_t map[256] = { 0 };
  1090. map[hashidx&255] = 1;
  1091. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1092. motion_bx, motion_by,
  1093. pred_fx, pred_fy,
  1094. pred_bx, pred_by,
  1095. 0, 16);
  1096. if(s->avctx->bidir_refine){
  1097. int end;
  1098. static const uint8_t limittab[5]={0,8,32,64,80};
  1099. const int limit= limittab[s->avctx->bidir_refine];
  1100. static const int8_t vect[][4]={
  1101. { 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},
  1102. { 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},
  1103. { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
  1104. { 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},
  1105. { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
  1106. { 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},
  1107. { 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},
  1108. { 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},
  1109. { 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},
  1110. { 1, 1, 1, 1}, {-1,-1,-1,-1},
  1111. { 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},
  1112. { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
  1113. };
  1114. static const uint8_t hash[]={
  1115. 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),
  1116. 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),
  1117. HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
  1118. 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),
  1119. HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
  1120. 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),
  1121. 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),
  1122. 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),
  1123. 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),
  1124. HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
  1125. 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),
  1126. 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),
  1127. };
  1128. #define CHECK_BIDIR(fx,fy,bx,by)\
  1129. if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
  1130. &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
  1131. &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
  1132. int score;\
  1133. map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
  1134. 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);\
  1135. if(score < fbmin){\
  1136. hashidx += HASH(fx,fy,bx,by);\
  1137. fbmin= score;\
  1138. motion_fx+=fx;\
  1139. motion_fy+=fy;\
  1140. motion_bx+=bx;\
  1141. motion_by+=by;\
  1142. end=0;\
  1143. }\
  1144. }
  1145. #define CHECK_BIDIR2(a,b,c,d)\
  1146. CHECK_BIDIR(a,b,c,d)\
  1147. CHECK_BIDIR(-(a),-(b),-(c),-(d))
  1148. do{
  1149. int i;
  1150. int borderdist=0;
  1151. end=1;
  1152. CHECK_BIDIR2(0,0,0,1)
  1153. CHECK_BIDIR2(0,0,1,0)
  1154. CHECK_BIDIR2(0,1,0,0)
  1155. CHECK_BIDIR2(1,0,0,0)
  1156. for(i=8; i<limit; i++){
  1157. int fx= motion_fx+vect[i][0];
  1158. int fy= motion_fy+vect[i][1];
  1159. int bx= motion_bx+vect[i][2];
  1160. int by= motion_by+vect[i][3];
  1161. if(borderdist<=0){
  1162. int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
  1163. int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
  1164. if((a|b) < 0)
  1165. map[(hashidx+hash[i])&255] = 1;
  1166. }
  1167. if(!map[(hashidx+hash[i])&255]){
  1168. int score;
  1169. map[(hashidx+hash[i])&255] = 1;
  1170. score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
  1171. if(score < fbmin){
  1172. hashidx += hash[i];
  1173. fbmin= score;
  1174. motion_fx=fx;
  1175. motion_fy=fy;
  1176. motion_bx=bx;
  1177. motion_by=by;
  1178. end=0;
  1179. borderdist--;
  1180. if(borderdist<=0){
  1181. int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
  1182. int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
  1183. borderdist= FFMIN(a,b);
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }while(!end);
  1189. }
  1190. s->b_bidir_forw_mv_table[xy][0]= motion_fx;
  1191. s->b_bidir_forw_mv_table[xy][1]= motion_fy;
  1192. s->b_bidir_back_mv_table[xy][0]= motion_bx;
  1193. s->b_bidir_back_mv_table[xy][1]= motion_by;
  1194. return fbmin;
  1195. }
  1196. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1197. {
  1198. MotionEstContext * const c= &s->me;
  1199. int P[10][2];
  1200. const int mot_stride = s->mb_stride;
  1201. const int mot_xy = mb_y*mot_stride + mb_x;
  1202. const int shift= 1+s->quarter_sample;
  1203. int dmin, i;
  1204. const int time_pp= s->pp_time;
  1205. const int time_pb= s->pb_time;
  1206. int mx, my, xmin, xmax, ymin, ymax;
  1207. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1208. c->current_mv_penalty= c->mv_penalty[1] + MAX_DMV;
  1209. ymin= xmin=(-32)>>shift;
  1210. ymax= xmax= 31>>shift;
  1211. if (IS_8X8(s->next_picture.mb_type[mot_xy])) {
  1212. s->mv_type= MV_TYPE_8X8;
  1213. }else{
  1214. s->mv_type= MV_TYPE_16X16;
  1215. }
  1216. for(i=0; i<4; i++){
  1217. int index= s->block_index[i];
  1218. int min, max;
  1219. c->co_located_mv[i][0] = s->next_picture.motion_val[0][index][0];
  1220. c->co_located_mv[i][1] = s->next_picture.motion_val[0][index][1];
  1221. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1222. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1223. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1224. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1225. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1226. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1227. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1228. min+= 16*mb_x - 1;
  1229. xmax= FFMIN(xmax, s->width - max);
  1230. xmin= FFMAX(xmin, - 16 - min);
  1231. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1232. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1233. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1234. min+= 16*mb_y - 1;
  1235. ymax= FFMIN(ymax, s->height - max);
  1236. ymin= FFMAX(ymin, - 16 - min);
  1237. if(s->mv_type == MV_TYPE_16X16) break;
  1238. }
  1239. av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1240. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1241. s->b_direct_mv_table[mot_xy][0]= 0;
  1242. s->b_direct_mv_table[mot_xy][1]= 0;
  1243. return 256*256*256*64;
  1244. }
  1245. c->xmin= xmin;
  1246. c->ymin= ymin;
  1247. c->xmax= xmax;
  1248. c->ymax= ymax;
  1249. c->flags |= FLAG_DIRECT;
  1250. c->sub_flags |= FLAG_DIRECT;
  1251. c->pred_x=0;
  1252. c->pred_y=0;
  1253. P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1254. P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1255. /* special case for first line */
  1256. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
  1257. P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1258. P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1259. P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1260. P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1261. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1262. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1263. }
  1264. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1265. if(c->sub_flags&FLAG_QPEL)
  1266. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1267. else
  1268. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1269. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1270. dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1271. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1272. mv_table[mot_xy][0]= mx;
  1273. mv_table[mot_xy][1]= my;
  1274. c->flags &= ~FLAG_DIRECT;
  1275. c->sub_flags &= ~FLAG_DIRECT;
  1276. return dmin;
  1277. }
  1278. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1279. int mb_x, int mb_y)
  1280. {
  1281. MotionEstContext * const c= &s->me;
  1282. const int penalty_factor= c->mb_penalty_factor;
  1283. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1284. int type=0;
  1285. const int xy = mb_y*s->mb_stride + mb_x;
  1286. init_ref(c, s->new_picture.f->data, s->last_picture.f->data,
  1287. s->next_picture.f->data, 16 * mb_x, 16 * mb_y, 2);
  1288. get_limits(s, 16*mb_x, 16*mb_y);
  1289. c->skip=0;
  1290. if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]) {
  1291. int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
  1292. score= ((unsigned)(score*score + 128*256))>>16;
  1293. c->mc_mb_var_sum_temp += score;
  1294. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1295. s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
  1296. return;
  1297. }
  1298. if (s->codec_id == AV_CODEC_ID_MPEG4)
  1299. dmin= direct_search(s, mb_x, mb_y);
  1300. else
  1301. dmin= INT_MAX;
  1302. // FIXME penalty stuff for non-MPEG-4
  1303. c->skip=0;
  1304. fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +
  1305. 3 * penalty_factor;
  1306. c->skip=0;
  1307. bmin = estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) +
  1308. 2 * penalty_factor;
  1309. ff_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1310. c->skip=0;
  1311. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1312. ff_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1313. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  1314. //FIXME mb type penalty
  1315. c->skip=0;
  1316. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_DMV;
  1317. fimin= interlaced_search(s, 0,
  1318. s->b_field_mv_table[0], s->b_field_select_table[0],
  1319. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1320. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_DMV;
  1321. bimin= interlaced_search(s, 2,
  1322. s->b_field_mv_table[1], s->b_field_select_table[1],
  1323. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1324. }else
  1325. fimin= bimin= INT_MAX;
  1326. {
  1327. int score= fmin;
  1328. type = CANDIDATE_MB_TYPE_FORWARD;
  1329. if (dmin <= score){
  1330. score = dmin;
  1331. type = CANDIDATE_MB_TYPE_DIRECT;
  1332. }
  1333. if(bmin<score){
  1334. score=bmin;
  1335. type= CANDIDATE_MB_TYPE_BACKWARD;
  1336. }
  1337. if(fbmin<score){
  1338. score=fbmin;
  1339. type= CANDIDATE_MB_TYPE_BIDIR;
  1340. }
  1341. if(fimin<score){
  1342. score=fimin;
  1343. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1344. }
  1345. if(bimin<score){
  1346. score=bimin;
  1347. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1348. }
  1349. score= ((unsigned)(score*score + 128*256))>>16;
  1350. c->mc_mb_var_sum_temp += score;
  1351. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1352. }
  1353. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1354. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1355. if(fimin < INT_MAX)
  1356. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1357. if(bimin < INT_MAX)
  1358. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1359. if(fimin < INT_MAX && bimin < INT_MAX){
  1360. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1361. }
  1362. //FIXME something smarter
  1363. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
  1364. if (s->codec_id == AV_CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT &&
  1365. s->mpv_flags & FF_MPV_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
  1366. type |= CANDIDATE_MB_TYPE_DIRECT0;
  1367. }
  1368. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1369. }
  1370. /* find best f_code for ME which do unlimited searches */
  1371. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1372. {
  1373. if (s->motion_est != FF_ME_ZERO) {
  1374. int score[8];
  1375. int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
  1376. uint8_t * fcode_tab= s->fcode_tab;
  1377. int best_fcode=-1;
  1378. int best_score=-10000000;
  1379. if(s->msmpeg4_version)
  1380. range= FFMIN(range, 16);
  1381. else if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
  1382. range= FFMIN(range, 256);
  1383. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1384. for(y=0; y<s->mb_height; y++){
  1385. int x;
  1386. int xy= y*s->mb_stride;
  1387. for(x=0; x<s->mb_width; x++){
  1388. if(s->mb_type[xy] & type){
  1389. int mx= mv_table[xy][0];
  1390. int my= mv_table[xy][1];
  1391. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1392. fcode_tab[my + MAX_MV]);
  1393. int j;
  1394. if(mx >= range || mx < -range ||
  1395. my >= range || my < -range)
  1396. continue;
  1397. for(j=0; j<fcode && j<8; j++){
  1398. if(s->pict_type==AV_PICTURE_TYPE_B || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1399. score[j]-= 170;
  1400. }
  1401. }
  1402. xy++;
  1403. }
  1404. }
  1405. for(i=1; i<8; i++){
  1406. if(score[i] > best_score){
  1407. best_score= score[i];
  1408. best_fcode= i;
  1409. }
  1410. }
  1411. return best_fcode;
  1412. }else{
  1413. return 1;
  1414. }
  1415. }
  1416. void ff_fix_long_p_mvs(MpegEncContext * s)
  1417. {
  1418. MotionEstContext * const c= &s->me;
  1419. const int f_code= s->f_code;
  1420. int y, range;
  1421. av_assert0(s->pict_type==AV_PICTURE_TYPE_P);
  1422. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1423. av_assert0(range <= 16 || !s->msmpeg4_version);
  1424. av_assert0(range <=256 || !(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
  1425. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1426. if (s->avctx->flags & AV_CODEC_FLAG_4MV) {
  1427. const int wrap= s->b8_stride;
  1428. /* clip / convert to intra 8x8 type MVs */
  1429. for(y=0; y<s->mb_height; y++){
  1430. int xy= y*2*wrap;
  1431. int i= y*s->mb_stride;
  1432. int x;
  1433. for(x=0; x<s->mb_width; x++){
  1434. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1435. int block;
  1436. for(block=0; block<4; block++){
  1437. int off= (block& 1) + (block>>1)*wrap;
  1438. int mx = s->current_picture.motion_val[0][ xy + off ][0];
  1439. int my = s->current_picture.motion_val[0][ xy + off ][1];
  1440. if( mx >=range || mx <-range
  1441. || my >=range || my <-range){
  1442. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1443. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1444. s->current_picture.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
  1445. }
  1446. }
  1447. }
  1448. xy+=2;
  1449. i++;
  1450. }
  1451. }
  1452. }
  1453. }
  1454. /**
  1455. * @param truncate 1 for truncation, 0 for using intra
  1456. */
  1457. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1458. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1459. {
  1460. MotionEstContext * const c= &s->me;
  1461. int y, h_range, v_range;
  1462. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1463. int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1464. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1465. h_range= range;
  1466. v_range= field_select_table ? range>>1 : range;
  1467. /* clip / convert to intra 16x16 type MVs */
  1468. for(y=0; y<s->mb_height; y++){
  1469. int x;
  1470. int xy= y*s->mb_stride;
  1471. for(x=0; x<s->mb_width; x++){
  1472. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1473. if (!field_select_table || field_select_table[xy] == field_select) {
  1474. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1475. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1476. if(truncate){
  1477. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1478. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1479. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1480. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1481. }else{
  1482. s->mb_type[xy] &= ~type;
  1483. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1484. mv_table[xy][0]=
  1485. mv_table[xy][1]= 0;
  1486. }
  1487. }
  1488. }
  1489. }
  1490. xy++;
  1491. }
  1492. }
  1493. }