motion_est.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. /*
  2. * Motion estimation
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer
  5. *
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. /**
  26. * @file motion_est.c
  27. * Motion estimation.
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <limits.h>
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "mpegvideo.h"
  35. #undef NDEBUG
  36. #include <assert.h>
  37. #define SQ(a) ((a)*(a))
  38. #define P_LEFT P[1]
  39. #define P_TOP P[2]
  40. #define P_TOPRIGHT P[3]
  41. #define P_MEDIAN P[4]
  42. #define P_MV1 P[9]
  43. static inline int sad_hpel_motion_search(MpegEncContext * s,
  44. int *mx_ptr, int *my_ptr, int dmin,
  45. int src_index, int ref_index,
  46. int size, int h);
  47. static inline int update_map_generation(MotionEstContext *c)
  48. {
  49. c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
  50. if(c->map_generation==0){
  51. c->map_generation= 1<<(ME_MAP_MV_BITS*2);
  52. memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
  53. }
  54. return c->map_generation;
  55. }
  56. /* shape adaptive search stuff */
  57. typedef struct Minima{
  58. int height;
  59. int x, y;
  60. int checked;
  61. }Minima;
  62. static int minima_cmp(const void *a, const void *b){
  63. const Minima *da = (const Minima *) a;
  64. const Minima *db = (const Minima *) b;
  65. return da->height - db->height;
  66. }
  67. #define FLAG_QPEL 1 //must be 1
  68. #define FLAG_CHROMA 2
  69. #define FLAG_DIRECT 4
  70. 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){
  71. const int offset[3]= {
  72. y*c-> stride + x,
  73. ((y*c->uvstride + x)>>1),
  74. ((y*c->uvstride + x)>>1),
  75. };
  76. int i;
  77. for(i=0; i<3; i++){
  78. c->src[0][i]= src [i] + offset[i];
  79. c->ref[0][i]= ref [i] + offset[i];
  80. }
  81. if(ref_index){
  82. for(i=0; i<3; i++){
  83. c->ref[ref_index][i]= ref2[i] + offset[i];
  84. }
  85. }
  86. }
  87. static int get_flags(MotionEstContext *c, int direct, int chroma){
  88. return ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
  89. + (direct ? FLAG_DIRECT : 0)
  90. + (chroma ? FLAG_CHROMA : 0);
  91. }
  92. /*! \brief compares a block (either a full macroblock or a partition thereof)
  93. against a proposed motion-compensated prediction of that block
  94. */
  95. static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  96. const int size, const int h, int ref_index, int src_index,
  97. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  98. MotionEstContext * const c= &s->me;
  99. const int stride= c->stride;
  100. const int uvstride= c->uvstride;
  101. const int qpel= flags&FLAG_QPEL;
  102. const int chroma= flags&FLAG_CHROMA;
  103. const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
  104. const int hx= subx + (x<<(1+qpel));
  105. const int hy= suby + (y<<(1+qpel));
  106. uint8_t * const * const ref= c->ref[ref_index];
  107. uint8_t * const * const src= c->src[src_index];
  108. int d;
  109. //FIXME check chroma 4mv, (no crashes ...)
  110. if(flags&FLAG_DIRECT){
  111. assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
  112. if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
  113. const int time_pp= s->pp_time;
  114. const int time_pb= s->pb_time;
  115. const int mask= 2*qpel+1;
  116. if(s->mv_type==MV_TYPE_8X8){
  117. int i;
  118. for(i=0; i<4; i++){
  119. int fx = c->direct_basis_mv[i][0] + hx;
  120. int fy = c->direct_basis_mv[i][1] + hy;
  121. 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));
  122. 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));
  123. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  124. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  125. uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
  126. if(qpel){
  127. c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
  128. c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
  129. }else{
  130. c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
  131. c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
  132. }
  133. }
  134. }else{
  135. int fx = c->direct_basis_mv[0][0] + hx;
  136. int fy = c->direct_basis_mv[0][1] + hy;
  137. int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
  138. int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
  139. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  140. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  141. if(qpel){
  142. c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
  143. c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
  144. c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
  145. c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
  146. c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
  147. c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
  148. c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
  149. c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
  150. }else{
  151. assert((fx>>1) + 16*s->mb_x >= -16);
  152. assert((fy>>1) + 16*s->mb_y >= -16);
  153. assert((fx>>1) + 16*s->mb_x <= s->width);
  154. assert((fy>>1) + 16*s->mb_y <= s->height);
  155. assert((bx>>1) + 16*s->mb_x >= -16);
  156. assert((by>>1) + 16*s->mb_y >= -16);
  157. assert((bx>>1) + 16*s->mb_x <= s->width);
  158. assert((by>>1) + 16*s->mb_y <= s->height);
  159. c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
  160. c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
  161. }
  162. }
  163. d = cmp_func(s, c->temp, src[0], stride, 16);
  164. }else
  165. d= 256*256*256*32;
  166. }else{
  167. int uvdxy; /* no, it might not be used uninitialized */
  168. if(dxy){
  169. if(qpel){
  170. c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
  171. if(chroma){
  172. int cx= hx/2;
  173. int cy= hy/2;
  174. cx= (cx>>1)|(cx&1);
  175. cy= (cy>>1)|(cy&1);
  176. uvdxy= (cx&1) + 2*(cy&1);
  177. //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
  178. }
  179. }else{
  180. c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
  181. if(chroma)
  182. uvdxy= dxy | (x&1) | (2*(y&1));
  183. }
  184. d = cmp_func(s, c->temp, src[0], stride, h);
  185. }else{
  186. d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
  187. if(chroma)
  188. uvdxy= (x&1) + 2*(y&1);
  189. }
  190. if(chroma){
  191. uint8_t * const uvtemp= c->temp + 16*stride;
  192. c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  193. c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  194. d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
  195. d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
  196. }
  197. }
  198. #if 0
  199. if(full_pel){
  200. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
  201. score_map[index]= d;
  202. }
  203. d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;
  204. #endif
  205. return d;
  206. }
  207. #include "motion_est_template.c"
  208. static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
  209. return 0;
  210. }
  211. static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
  212. }
  213. void ff_init_me(MpegEncContext *s){
  214. MotionEstContext * const c= &s->me;
  215. int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
  216. int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
  217. c->avctx= s->avctx;
  218. if(cache_size < 2*dia_size && !c->stride){
  219. av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
  220. }
  221. ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
  222. ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
  223. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
  224. ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
  225. c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
  226. c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
  227. c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
  228. /*FIXME s->no_rounding b_type*/
  229. if(s->flags&CODEC_FLAG_QPEL){
  230. c->sub_motion_search= qpel_motion_search;
  231. c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
  232. if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
  233. else c->qpel_put= s->dsp.put_qpel_pixels_tab;
  234. }else{
  235. if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
  236. c->sub_motion_search= hpel_motion_search;
  237. else if( c->avctx->me_sub_cmp == FF_CMP_SAD
  238. && c->avctx-> me_cmp == FF_CMP_SAD
  239. && c->avctx-> mb_cmp == FF_CMP_SAD)
  240. c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
  241. else
  242. c->sub_motion_search= hpel_motion_search;
  243. }
  244. c->hpel_avg= s->dsp.avg_pixels_tab;
  245. if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
  246. else c->hpel_put= s->dsp.put_pixels_tab;
  247. if(s->linesize){
  248. c->stride = s->linesize;
  249. c->uvstride= s->uvlinesize;
  250. }else{
  251. c->stride = 16*s->mb_width + 32;
  252. c->uvstride= 8*s->mb_width + 16;
  253. }
  254. // 8x8 fullpel search would need a 4x4 chroma compare, which we dont have yet, and even if we had the motion estimation code doesnt expect it
  255. if(s->codec_id != CODEC_ID_SNOW){
  256. if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
  257. s->dsp.me_cmp[2]= zero_cmp;
  258. }
  259. if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
  260. s->dsp.me_sub_cmp[2]= zero_cmp;
  261. }
  262. c->hpel_put[2][0]= c->hpel_put[2][1]=
  263. c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
  264. }
  265. if(s->codec_id == CODEC_ID_H261){
  266. c->sub_motion_search= no_sub_motion_search;
  267. }
  268. c->temp= c->scratchpad;
  269. }
  270. #if 0
  271. static int pix_dev(uint8_t * pix, int line_size, int mean)
  272. {
  273. int s, i, j;
  274. s = 0;
  275. for (i = 0; i < 16; i++) {
  276. for (j = 0; j < 16; j += 8) {
  277. s += FFABS(pix[0]-mean);
  278. s += FFABS(pix[1]-mean);
  279. s += FFABS(pix[2]-mean);
  280. s += FFABS(pix[3]-mean);
  281. s += FFABS(pix[4]-mean);
  282. s += FFABS(pix[5]-mean);
  283. s += FFABS(pix[6]-mean);
  284. s += FFABS(pix[7]-mean);
  285. pix += 8;
  286. }
  287. pix += line_size - 16;
  288. }
  289. return s;
  290. }
  291. #endif
  292. static inline void no_motion_search(MpegEncContext * s,
  293. int *mx_ptr, int *my_ptr)
  294. {
  295. *mx_ptr = 16 * s->mb_x;
  296. *my_ptr = 16 * s->mb_y;
  297. }
  298. #if 0 /* the use of these functions is inside #if 0 */
  299. static int full_motion_search(MpegEncContext * s,
  300. int *mx_ptr, int *my_ptr, int range,
  301. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  302. {
  303. int x1, y1, x2, y2, xx, yy, x, y;
  304. int mx, my, dmin, d;
  305. uint8_t *pix;
  306. xx = 16 * s->mb_x;
  307. yy = 16 * s->mb_y;
  308. x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
  309. if (x1 < xmin)
  310. x1 = xmin;
  311. x2 = xx + range - 1;
  312. if (x2 > xmax)
  313. x2 = xmax;
  314. y1 = yy - range + 1;
  315. if (y1 < ymin)
  316. y1 = ymin;
  317. y2 = yy + range - 1;
  318. if (y2 > ymax)
  319. y2 = ymax;
  320. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  321. dmin = 0x7fffffff;
  322. mx = 0;
  323. my = 0;
  324. for (y = y1; y <= y2; y++) {
  325. for (x = x1; x <= x2; x++) {
  326. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
  327. s->linesize, 16);
  328. if (d < dmin ||
  329. (d == dmin &&
  330. (abs(x - xx) + abs(y - yy)) <
  331. (abs(mx - xx) + abs(my - yy)))) {
  332. dmin = d;
  333. mx = x;
  334. my = y;
  335. }
  336. }
  337. }
  338. *mx_ptr = mx;
  339. *my_ptr = my;
  340. #if 0
  341. if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
  342. *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
  343. av_log(NULL, AV_LOG_ERROR, "error %d %d\n", *mx_ptr, *my_ptr);
  344. }
  345. #endif
  346. return dmin;
  347. }
  348. static int log_motion_search(MpegEncContext * s,
  349. int *mx_ptr, int *my_ptr, int range,
  350. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  351. {
  352. int x1, y1, x2, y2, xx, yy, x, y;
  353. int mx, my, dmin, d;
  354. uint8_t *pix;
  355. xx = s->mb_x << 4;
  356. yy = s->mb_y << 4;
  357. /* Left limit */
  358. x1 = xx - range;
  359. if (x1 < xmin)
  360. x1 = xmin;
  361. /* Right limit */
  362. x2 = xx + range;
  363. if (x2 > xmax)
  364. x2 = xmax;
  365. /* Upper limit */
  366. y1 = yy - range;
  367. if (y1 < ymin)
  368. y1 = ymin;
  369. /* Lower limit */
  370. y2 = yy + range;
  371. if (y2 > ymax)
  372. y2 = ymax;
  373. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  374. dmin = 0x7fffffff;
  375. mx = 0;
  376. my = 0;
  377. do {
  378. for (y = y1; y <= y2; y += range) {
  379. for (x = x1; x <= x2; x += range) {
  380. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
  381. if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  382. dmin = d;
  383. mx = x;
  384. my = y;
  385. }
  386. }
  387. }
  388. range = range >> 1;
  389. x1 = mx - range;
  390. if (x1 < xmin)
  391. x1 = xmin;
  392. x2 = mx + range;
  393. if (x2 > xmax)
  394. x2 = xmax;
  395. y1 = my - range;
  396. if (y1 < ymin)
  397. y1 = ymin;
  398. y2 = my + range;
  399. if (y2 > ymax)
  400. y2 = ymax;
  401. } while (range >= 1);
  402. #ifdef DEBUG
  403. av_log(s->avctx, AV_LOG_DEBUG, "log - MX: %d\tMY: %d\n", mx, my);
  404. #endif
  405. *mx_ptr = mx;
  406. *my_ptr = my;
  407. return dmin;
  408. }
  409. static int phods_motion_search(MpegEncContext * s,
  410. int *mx_ptr, int *my_ptr, int range,
  411. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  412. {
  413. int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
  414. int mx, my, dminx, dminy;
  415. uint8_t *pix;
  416. xx = s->mb_x << 4;
  417. yy = s->mb_y << 4;
  418. /* Left limit */
  419. x1 = xx - range;
  420. if (x1 < xmin)
  421. x1 = xmin;
  422. /* Right limit */
  423. x2 = xx + range;
  424. if (x2 > xmax)
  425. x2 = xmax;
  426. /* Upper limit */
  427. y1 = yy - range;
  428. if (y1 < ymin)
  429. y1 = ymin;
  430. /* Lower limit */
  431. y2 = yy + range;
  432. if (y2 > ymax)
  433. y2 = ymax;
  434. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  435. mx = 0;
  436. my = 0;
  437. x = xx;
  438. y = yy;
  439. do {
  440. dminx = 0x7fffffff;
  441. dminy = 0x7fffffff;
  442. lastx = x;
  443. for (x = x1; x <= x2; x += range) {
  444. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
  445. if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  446. dminx = d;
  447. mx = x;
  448. }
  449. }
  450. x = lastx;
  451. for (y = y1; y <= y2; y += range) {
  452. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
  453. if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  454. dminy = d;
  455. my = y;
  456. }
  457. }
  458. range = range >> 1;
  459. x = mx;
  460. y = my;
  461. x1 = mx - range;
  462. if (x1 < xmin)
  463. x1 = xmin;
  464. x2 = mx + range;
  465. if (x2 > xmax)
  466. x2 = xmax;
  467. y1 = my - range;
  468. if (y1 < ymin)
  469. y1 = ymin;
  470. y2 = my + range;
  471. if (y2 > ymax)
  472. y2 = ymax;
  473. } while (range >= 1);
  474. #ifdef DEBUG
  475. av_log(s->avctx, AV_LOG_DEBUG, "phods - MX: %d\tMY: %d\n", mx, my);
  476. #endif
  477. /* half pixel search */
  478. *mx_ptr = mx;
  479. *my_ptr = my;
  480. return dminy;
  481. }
  482. #endif /* 0 */
  483. #define Z_THRESHOLD 256
  484. #define CHECK_SAD_HALF_MV(suffix, x, y) \
  485. {\
  486. d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
  487. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
  488. COPY3_IF_LT(dminh, d, dx, x, dy, y)\
  489. }
  490. static inline int sad_hpel_motion_search(MpegEncContext * s,
  491. int *mx_ptr, int *my_ptr, int dmin,
  492. int src_index, int ref_index,
  493. int size, int h)
  494. {
  495. MotionEstContext * const c= &s->me;
  496. const int penalty_factor= c->sub_penalty_factor;
  497. int mx, my, dminh;
  498. uint8_t *pix, *ptr;
  499. int stride= c->stride;
  500. const int flags= c->sub_flags;
  501. LOAD_COMMON
  502. assert(flags == 0);
  503. if(c->skip){
  504. // printf("S");
  505. *mx_ptr = 0;
  506. *my_ptr = 0;
  507. return dmin;
  508. }
  509. // printf("N");
  510. pix = c->src[src_index][0];
  511. mx = *mx_ptr;
  512. my = *my_ptr;
  513. ptr = c->ref[ref_index][0] + (my * stride) + mx;
  514. dminh = dmin;
  515. if (mx > xmin && mx < xmax &&
  516. my > ymin && my < ymax) {
  517. int dx=0, dy=0;
  518. int d, pen_x, pen_y;
  519. const int index= (my<<ME_MAP_SHIFT) + mx;
  520. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  521. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  522. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  523. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  524. mx<<=1;
  525. my<<=1;
  526. pen_x= pred_x + mx;
  527. pen_y= pred_y + my;
  528. ptr-= stride;
  529. if(t<=b){
  530. CHECK_SAD_HALF_MV(y2 , 0, -1)
  531. if(l<=r){
  532. CHECK_SAD_HALF_MV(xy2, -1, -1)
  533. if(t+r<=b+l){
  534. CHECK_SAD_HALF_MV(xy2, +1, -1)
  535. ptr+= stride;
  536. }else{
  537. ptr+= stride;
  538. CHECK_SAD_HALF_MV(xy2, -1, +1)
  539. }
  540. CHECK_SAD_HALF_MV(x2 , -1, 0)
  541. }else{
  542. CHECK_SAD_HALF_MV(xy2, +1, -1)
  543. if(t+l<=b+r){
  544. CHECK_SAD_HALF_MV(xy2, -1, -1)
  545. ptr+= stride;
  546. }else{
  547. ptr+= stride;
  548. CHECK_SAD_HALF_MV(xy2, +1, +1)
  549. }
  550. CHECK_SAD_HALF_MV(x2 , +1, 0)
  551. }
  552. }else{
  553. if(l<=r){
  554. if(t+l<=b+r){
  555. CHECK_SAD_HALF_MV(xy2, -1, -1)
  556. ptr+= stride;
  557. }else{
  558. ptr+= stride;
  559. CHECK_SAD_HALF_MV(xy2, +1, +1)
  560. }
  561. CHECK_SAD_HALF_MV(x2 , -1, 0)
  562. CHECK_SAD_HALF_MV(xy2, -1, +1)
  563. }else{
  564. if(t+r<=b+l){
  565. CHECK_SAD_HALF_MV(xy2, +1, -1)
  566. ptr+= stride;
  567. }else{
  568. ptr+= stride;
  569. CHECK_SAD_HALF_MV(xy2, -1, +1)
  570. }
  571. CHECK_SAD_HALF_MV(x2 , +1, 0)
  572. CHECK_SAD_HALF_MV(xy2, +1, +1)
  573. }
  574. CHECK_SAD_HALF_MV(y2 , 0, +1)
  575. }
  576. mx+=dx;
  577. my+=dy;
  578. }else{
  579. mx<<=1;
  580. my<<=1;
  581. }
  582. *mx_ptr = mx;
  583. *my_ptr = my;
  584. return dminh;
  585. }
  586. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  587. {
  588. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  589. s->p_mv_table[xy][0] = mx;
  590. s->p_mv_table[xy][1] = my;
  591. /* has already been set to the 4 MV if 4MV is done */
  592. if(mv4){
  593. int mot_xy= s->block_index[0];
  594. s->current_picture.motion_val[0][mot_xy ][0]= mx;
  595. s->current_picture.motion_val[0][mot_xy ][1]= my;
  596. s->current_picture.motion_val[0][mot_xy+1][0]= mx;
  597. s->current_picture.motion_val[0][mot_xy+1][1]= my;
  598. mot_xy += s->b8_stride;
  599. s->current_picture.motion_val[0][mot_xy ][0]= mx;
  600. s->current_picture.motion_val[0][mot_xy ][1]= my;
  601. s->current_picture.motion_val[0][mot_xy+1][0]= mx;
  602. s->current_picture.motion_val[0][mot_xy+1][1]= my;
  603. }
  604. }
  605. /**
  606. * get fullpel ME search limits.
  607. */
  608. static inline void get_limits(MpegEncContext *s, int x, int y)
  609. {
  610. MotionEstContext * const c= &s->me;
  611. int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
  612. /*
  613. if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
  614. else c->range= 16;
  615. */
  616. if (s->unrestricted_mv) {
  617. c->xmin = - x - 16;
  618. c->ymin = - y - 16;
  619. c->xmax = - x + s->mb_width *16;
  620. c->ymax = - y + s->mb_height*16;
  621. } else if (s->out_format == FMT_H261){
  622. // Search range of H261 is different from other codec standards
  623. c->xmin = (x > 15) ? - 15 : 0;
  624. c->ymin = (y > 15) ? - 15 : 0;
  625. c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
  626. c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
  627. } else {
  628. c->xmin = - x;
  629. c->ymin = - y;
  630. c->xmax = - x + s->mb_width *16 - 16;
  631. c->ymax = - y + s->mb_height*16 - 16;
  632. }
  633. if(range){
  634. c->xmin = FFMAX(c->xmin,-range);
  635. c->xmax = FFMIN(c->xmax, range);
  636. c->ymin = FFMAX(c->ymin,-range);
  637. c->ymax = FFMIN(c->ymax, range);
  638. }
  639. }
  640. static inline void init_mv4_ref(MotionEstContext *c){
  641. const int stride= c->stride;
  642. c->ref[1][0] = c->ref[0][0] + 8;
  643. c->ref[2][0] = c->ref[0][0] + 8*stride;
  644. c->ref[3][0] = c->ref[2][0] + 8;
  645. c->src[1][0] = c->src[0][0] + 8;
  646. c->src[2][0] = c->src[0][0] + 8*stride;
  647. c->src[3][0] = c->src[2][0] + 8;
  648. }
  649. static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
  650. {
  651. MotionEstContext * const c= &s->me;
  652. const int size= 1;
  653. const int h=8;
  654. int block;
  655. int P[10][2];
  656. int dmin_sum=0, mx4_sum=0, my4_sum=0;
  657. int same=1;
  658. const int stride= c->stride;
  659. uint8_t *mv_penalty= c->current_mv_penalty;
  660. init_mv4_ref(c);
  661. for(block=0; block<4; block++){
  662. int mx4, my4;
  663. int pred_x4, pred_y4;
  664. int dmin4;
  665. static const int off[4]= {2, 1, 1, -1};
  666. const int mot_stride = s->b8_stride;
  667. const int mot_xy = s->block_index[block];
  668. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  669. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  670. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  671. /* special case for first line */
  672. if (s->first_slice_line && block<2) {
  673. c->pred_x= pred_x4= P_LEFT[0];
  674. c->pred_y= pred_y4= P_LEFT[1];
  675. } else {
  676. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  677. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  678. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
  679. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
  680. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  681. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  682. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  683. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  684. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  685. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  686. c->pred_x= pred_x4 = P_MEDIAN[0];
  687. c->pred_y= pred_y4 = P_MEDIAN[1];
  688. }
  689. P_MV1[0]= mx;
  690. P_MV1[1]= my;
  691. dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
  692. dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
  693. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  694. int dxy;
  695. const int offset= ((block&1) + (block>>1)*stride)*8;
  696. uint8_t *dest_y = c->scratchpad + offset;
  697. if(s->quarter_sample){
  698. uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
  699. dxy = ((my4 & 3) << 2) | (mx4 & 3);
  700. if(s->no_rounding)
  701. s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride);
  702. else
  703. s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride);
  704. }else{
  705. uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
  706. dxy = ((my4 & 1) << 1) | (mx4 & 1);
  707. if(s->no_rounding)
  708. s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
  709. else
  710. s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
  711. }
  712. dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
  713. }else
  714. dmin_sum+= dmin4;
  715. if(s->quarter_sample){
  716. mx4_sum+= mx4/2;
  717. my4_sum+= my4/2;
  718. }else{
  719. mx4_sum+= mx4;
  720. my4_sum+= my4;
  721. }
  722. s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
  723. s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
  724. if(mx4 != mx || my4 != my) same=0;
  725. }
  726. if(same)
  727. return INT_MAX;
  728. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  729. dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16);
  730. }
  731. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  732. int dxy;
  733. int mx, my;
  734. int offset;
  735. mx= ff_h263_round_chroma(mx4_sum);
  736. my= ff_h263_round_chroma(my4_sum);
  737. dxy = ((my & 1) << 1) | (mx & 1);
  738. offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
  739. if(s->no_rounding){
  740. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8);
  741. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8);
  742. }else{
  743. s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8);
  744. s->dsp.put_pixels_tab [1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8);
  745. }
  746. dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad , s->uvlinesize, 8);
  747. dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8);
  748. }
  749. c->pred_x= mx;
  750. c->pred_y= my;
  751. switch(c->avctx->mb_cmp&0xFF){
  752. /*case FF_CMP_SSE:
  753. return dmin_sum+ 32*s->qscale*s->qscale;*/
  754. case FF_CMP_RD:
  755. return dmin_sum;
  756. default:
  757. return dmin_sum+ 11*c->mb_penalty_factor;
  758. }
  759. }
  760. static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
  761. MotionEstContext * const c= &s->me;
  762. c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
  763. c->src[1][0] = c->src[0][0] + s->linesize;
  764. if(c->flags & FLAG_CHROMA){
  765. c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
  766. c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
  767. c->src[1][1] = c->src[0][1] + s->uvlinesize;
  768. c->src[1][2] = c->src[0][2] + s->uvlinesize;
  769. }
  770. }
  771. static int interlaced_search(MpegEncContext *s, int ref_index,
  772. int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  773. {
  774. MotionEstContext * const c= &s->me;
  775. const int size=0;
  776. const int h=8;
  777. int block;
  778. int P[10][2];
  779. uint8_t * const mv_penalty= c->current_mv_penalty;
  780. int same=1;
  781. const int stride= 2*s->linesize;
  782. int dmin_sum= 0;
  783. const int mot_stride= s->mb_stride;
  784. const int xy= s->mb_x + s->mb_y*mot_stride;
  785. c->ymin>>=1;
  786. c->ymax>>=1;
  787. c->stride<<=1;
  788. c->uvstride<<=1;
  789. init_interlaced_ref(s, ref_index);
  790. for(block=0; block<2; block++){
  791. int field_select;
  792. int best_dmin= INT_MAX;
  793. int best_field= -1;
  794. for(field_select=0; field_select<2; field_select++){
  795. int dmin, mx_i, my_i;
  796. int16_t (*mv_table)[2]= mv_tables[block][field_select];
  797. if(user_field_select){
  798. assert(field_select==0 || field_select==1);
  799. assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
  800. if(field_select_tables[block][xy] != field_select)
  801. continue;
  802. }
  803. P_LEFT[0] = mv_table[xy - 1][0];
  804. P_LEFT[1] = mv_table[xy - 1][1];
  805. if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
  806. c->pred_x= P_LEFT[0];
  807. c->pred_y= P_LEFT[1];
  808. if(!s->first_slice_line){
  809. P_TOP[0] = mv_table[xy - mot_stride][0];
  810. P_TOP[1] = mv_table[xy - mot_stride][1];
  811. P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
  812. P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
  813. if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
  814. if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
  815. if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
  816. if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
  817. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  818. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  819. }
  820. P_MV1[0]= mx; //FIXME not correct if block != field_select
  821. P_MV1[1]= my / 2;
  822. dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
  823. dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
  824. mv_table[xy][0]= mx_i;
  825. mv_table[xy][1]= my_i;
  826. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  827. int dxy;
  828. //FIXME chroma ME
  829. uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
  830. dxy = ((my_i & 1) << 1) | (mx_i & 1);
  831. if(s->no_rounding){
  832. s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
  833. }else{
  834. s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
  835. }
  836. dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
  837. dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
  838. }else
  839. dmin+= c->mb_penalty_factor; //field_select bits
  840. dmin += field_select != block; //slightly prefer same field
  841. if(dmin < best_dmin){
  842. best_dmin= dmin;
  843. best_field= field_select;
  844. }
  845. }
  846. {
  847. int16_t (*mv_table)[2]= mv_tables[block][best_field];
  848. if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
  849. if(mv_table[xy][1]&1) same=0;
  850. if(mv_table[xy][1]*2 != my) same=0;
  851. if(best_field != block) same=0;
  852. }
  853. field_select_tables[block][xy]= best_field;
  854. dmin_sum += best_dmin;
  855. }
  856. c->ymin<<=1;
  857. c->ymax<<=1;
  858. c->stride>>=1;
  859. c->uvstride>>=1;
  860. if(same)
  861. return INT_MAX;
  862. switch(c->avctx->mb_cmp&0xFF){
  863. /*case FF_CMP_SSE:
  864. return dmin_sum+ 32*s->qscale*s->qscale;*/
  865. case FF_CMP_RD:
  866. return dmin_sum;
  867. default:
  868. return dmin_sum+ 11*c->mb_penalty_factor;
  869. }
  870. }
  871. static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  872. int ymax= s->me.ymax>>interlaced;
  873. int ymin= s->me.ymin>>interlaced;
  874. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  875. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  876. if(mv[1] < ymin) mv[1] = ymin;
  877. if(mv[1] > ymax) mv[1] = ymax;
  878. }
  879. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  880. MotionEstContext * const c= &s->me;
  881. Picture *p= s->current_picture_ptr;
  882. int mb_xy= mb_x + mb_y*s->mb_stride;
  883. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  884. int mb_type= s->current_picture.mb_type[mb_xy];
  885. int flags= c->flags;
  886. int shift= (flags&FLAG_QPEL) + 1;
  887. int mask= (1<<shift)-1;
  888. int x, y, i;
  889. int d=0;
  890. me_cmp_func cmpf= s->dsp.sse[0];
  891. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  892. if(p_type && USES_LIST(mb_type, 1)){
  893. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  894. return INT_MAX/2;
  895. }
  896. assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  897. for(i=0; i<4; i++){
  898. int xy= s->block_index[i];
  899. clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
  900. clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
  901. }
  902. if(IS_INTERLACED(mb_type)){
  903. int xy2= xy + s->b8_stride;
  904. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  905. c->stride<<=1;
  906. c->uvstride<<=1;
  907. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  908. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  909. return INT_MAX/2;
  910. }
  911. if(USES_LIST(mb_type, 0)){
  912. int field_select0= p->ref_index[0][xy ];
  913. int field_select1= p->ref_index[0][xy2];
  914. assert(field_select0==0 ||field_select0==1);
  915. assert(field_select1==0 ||field_select1==1);
  916. init_interlaced_ref(s, 0);
  917. if(p_type){
  918. s->p_field_select_table[0][mb_xy]= field_select0;
  919. s->p_field_select_table[1][mb_xy]= field_select1;
  920. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
  921. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
  922. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  923. }else{
  924. s->b_field_select_table[0][0][mb_xy]= field_select0;
  925. s->b_field_select_table[0][1][mb_xy]= field_select1;
  926. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
  927. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
  928. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  929. }
  930. x= p->motion_val[0][xy ][0];
  931. y= p->motion_val[0][xy ][1];
  932. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  933. x= p->motion_val[0][xy2][0];
  934. y= p->motion_val[0][xy2][1];
  935. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  936. }
  937. if(USES_LIST(mb_type, 1)){
  938. int field_select0= p->ref_index[1][xy ];
  939. int field_select1= p->ref_index[1][xy2];
  940. assert(field_select0==0 ||field_select0==1);
  941. assert(field_select1==0 ||field_select1==1);
  942. init_interlaced_ref(s, 2);
  943. s->b_field_select_table[1][0][mb_xy]= field_select0;
  944. s->b_field_select_table[1][1][mb_xy]= field_select1;
  945. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
  946. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2];
  947. if(USES_LIST(mb_type, 0)){
  948. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  949. }else{
  950. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  951. }
  952. x= p->motion_val[1][xy ][0];
  953. y= p->motion_val[1][xy ][1];
  954. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  955. x= p->motion_val[1][xy2][0];
  956. y= p->motion_val[1][xy2][1];
  957. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  958. //FIXME bidir scores
  959. }
  960. c->stride>>=1;
  961. c->uvstride>>=1;
  962. }else if(IS_8X8(mb_type)){
  963. if(!(s->flags & CODEC_FLAG_4MV)){
  964. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  965. return INT_MAX/2;
  966. }
  967. cmpf= s->dsp.sse[1];
  968. chroma_cmpf= s->dsp.sse[1];
  969. init_mv4_ref(c);
  970. for(i=0; i<4; i++){
  971. xy= s->block_index[i];
  972. x= p->motion_val[0][xy][0];
  973. y= p->motion_val[0][xy][1];
  974. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  975. }
  976. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  977. }else{
  978. if(USES_LIST(mb_type, 0)){
  979. if(p_type){
  980. *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  981. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  982. }else if(USES_LIST(mb_type, 1)){
  983. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  984. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
  985. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  986. }else{
  987. *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  988. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  989. }
  990. x= p->motion_val[0][xy][0];
  991. y= p->motion_val[0][xy][1];
  992. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  993. }else if(USES_LIST(mb_type, 1)){
  994. *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
  995. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  996. x= p->motion_val[1][xy][0];
  997. y= p->motion_val[1][xy][1];
  998. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  999. }else
  1000. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  1001. }
  1002. return d;
  1003. }
  1004. void ff_estimate_p_frame_motion(MpegEncContext * s,
  1005. int mb_x, int mb_y)
  1006. {
  1007. MotionEstContext * const c= &s->me;
  1008. uint8_t *pix, *ppix;
  1009. int sum, mx, my, dmin;
  1010. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  1011. int vard; ///< sum of squared differences with the estimated motion vector
  1012. int P[10][2];
  1013. const int shift= 1+s->quarter_sample;
  1014. int mb_type=0;
  1015. Picture * const pic= &s->current_picture;
  1016. init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
  1017. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1018. assert(s->linesize == c->stride);
  1019. assert(s->uvlinesize == c->uvstride);
  1020. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1021. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1022. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1023. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1024. get_limits(s, 16*mb_x, 16*mb_y);
  1025. c->skip=0;
  1026. /* intra / predictive decision */
  1027. pix = c->src[0][0];
  1028. sum = s->dsp.pix_sum(pix, s->linesize);
  1029. varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  1030. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1031. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  1032. c->mb_var_sum_temp += (varc+128)>>8;
  1033. if(c->avctx->me_threshold){
  1034. vard= check_input_motion(s, mb_x, mb_y, 1);
  1035. if((vard+128)>>8 < c->avctx->me_threshold){
  1036. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1037. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1038. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1039. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1040. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1041. return;
  1042. }
  1043. if((vard+128)>>8 < c->avctx->mb_threshold)
  1044. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  1045. }
  1046. switch(s->me_method) {
  1047. case ME_ZERO:
  1048. default:
  1049. no_motion_search(s, &mx, &my);
  1050. mx-= mb_x*16;
  1051. my-= mb_y*16;
  1052. dmin = 0;
  1053. break;
  1054. #if 0
  1055. case ME_FULL:
  1056. dmin = full_motion_search(s, &mx, &my, range, ref_picture);
  1057. mx-= mb_x*16;
  1058. my-= mb_y*16;
  1059. break;
  1060. case ME_LOG:
  1061. dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
  1062. mx-= mb_x*16;
  1063. my-= mb_y*16;
  1064. break;
  1065. case ME_PHODS:
  1066. dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
  1067. mx-= mb_x*16;
  1068. my-= mb_y*16;
  1069. break;
  1070. #endif
  1071. case ME_X1:
  1072. case ME_EPZS:
  1073. {
  1074. const int mot_stride = s->b8_stride;
  1075. const int mot_xy = s->block_index[0];
  1076. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  1077. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  1078. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1079. if(!s->first_slice_line) {
  1080. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  1081. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  1082. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
  1083. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
  1084. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1085. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1086. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1087. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1088. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1089. if(s->out_format == FMT_H263){
  1090. c->pred_x = P_MEDIAN[0];
  1091. c->pred_y = P_MEDIAN[1];
  1092. }else { /* mpeg1 at least */
  1093. c->pred_x= P_LEFT[0];
  1094. c->pred_y= P_LEFT[1];
  1095. }
  1096. }else{
  1097. c->pred_x= P_LEFT[0];
  1098. c->pred_y= P_LEFT[1];
  1099. }
  1100. }
  1101. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1102. break;
  1103. }
  1104. /* At this point (mx,my) are full-pell and the relative displacement */
  1105. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  1106. vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
  1107. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1108. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  1109. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1110. #if 0
  1111. printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
  1112. varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
  1113. #endif
  1114. if(mb_type){
  1115. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1116. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1117. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1118. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  1119. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1120. set_p_mv_tables(s, mx, my, 1);
  1121. }else{
  1122. mx <<=shift;
  1123. my <<=shift;
  1124. }
  1125. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  1126. h263_mv4_search(s, mx, my, shift);
  1127. set_p_mv_tables(s, mx, my, 0);
  1128. }
  1129. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  1130. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  1131. }
  1132. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1133. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1134. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1135. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1136. if (vard*2 + 200*256 > varc)
  1137. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  1138. if (varc*2 + 200*256 > vard || s->qscale > 24){
  1139. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  1140. mb_type|= CANDIDATE_MB_TYPE_INTER;
  1141. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1142. if(s->flags&CODEC_FLAG_MV0)
  1143. if(mx || my)
  1144. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  1145. }else{
  1146. mx <<=shift;
  1147. my <<=shift;
  1148. }
  1149. if((s->flags&CODEC_FLAG_4MV)
  1150. && !c->skip && varc>50<<8 && vard>10<<8){
  1151. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  1152. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  1153. set_p_mv_tables(s, mx, my, 0);
  1154. }else
  1155. set_p_mv_tables(s, mx, my, 1);
  1156. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1157. && !c->skip){ //FIXME varc/d checks
  1158. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  1159. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  1160. }
  1161. }else{
  1162. int intra_score, i;
  1163. mb_type= CANDIDATE_MB_TYPE_INTER;
  1164. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1165. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1166. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1167. if((s->flags&CODEC_FLAG_4MV)
  1168. && !c->skip && varc>50<<8 && vard>10<<8){
  1169. int dmin4= h263_mv4_search(s, mx, my, shift);
  1170. if(dmin4 < dmin){
  1171. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  1172. dmin=dmin4;
  1173. }
  1174. }
  1175. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1176. && !c->skip){ //FIXME varc/d checks
  1177. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  1178. if(dmin_i < dmin){
  1179. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  1180. dmin= dmin_i;
  1181. }
  1182. }
  1183. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  1184. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  1185. /* get intra luma score */
  1186. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  1187. intra_score= varc - 500;
  1188. }else{
  1189. int mean= (sum+128)>>8;
  1190. mean*= 0x01010101;
  1191. for(i=0; i<16; i++){
  1192. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1193. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1194. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1195. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1196. }
  1197. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1198. }
  1199. #if 0 //FIXME
  1200. /* get chroma score */
  1201. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1202. for(i=1; i<3; i++){
  1203. uint8_t *dest_c;
  1204. int mean;
  1205. if(s->out_format == FMT_H263){
  1206. mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
  1207. }else{
  1208. mean= (s->last_dc[i] + 4)>>3;
  1209. }
  1210. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1211. mean*= 0x01010101;
  1212. for(i=0; i<8; i++){
  1213. *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
  1214. *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
  1215. }
  1216. intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
  1217. }
  1218. }
  1219. #endif
  1220. intra_score += c->mb_penalty_factor*16;
  1221. if(intra_score < dmin){
  1222. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1223. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1224. }else
  1225. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
  1226. {
  1227. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1228. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1229. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1230. }
  1231. }
  1232. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1233. }
  1234. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1235. int mb_x, int mb_y)
  1236. {
  1237. MotionEstContext * const c= &s->me;
  1238. int mx, my, dmin;
  1239. int P[10][2];
  1240. const int shift= 1+s->quarter_sample;
  1241. const int xy= mb_x + mb_y*s->mb_stride;
  1242. init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
  1243. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1244. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1245. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1246. get_limits(s, 16*mb_x, 16*mb_y);
  1247. c->skip=0;
  1248. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1249. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1250. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1251. /* special case for first line */
  1252. if (s->first_slice_line) {
  1253. c->pred_x= P_LEFT[0];
  1254. c->pred_y= P_LEFT[1];
  1255. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1256. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1257. } else {
  1258. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1259. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1260. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1261. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1262. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1263. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1264. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1265. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1266. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1267. c->pred_x = P_MEDIAN[0];
  1268. c->pred_y = P_MEDIAN[1];
  1269. }
  1270. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1271. s->p_mv_table[xy][0] = mx<<shift;
  1272. s->p_mv_table[xy][1] = my<<shift;
  1273. return dmin;
  1274. }
  1275. static int ff_estimate_motion_b(MpegEncContext * s,
  1276. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1277. {
  1278. MotionEstContext * const c= &s->me;
  1279. int mx, my, dmin;
  1280. int P[10][2];
  1281. const int shift= 1+s->quarter_sample;
  1282. const int mot_stride = s->mb_stride;
  1283. const int mot_xy = mb_y*mot_stride + mb_x;
  1284. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1285. int mv_scale;
  1286. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1287. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1288. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1289. c->current_mv_penalty= mv_penalty;
  1290. get_limits(s, 16*mb_x, 16*mb_y);
  1291. switch(s->me_method) {
  1292. case ME_ZERO:
  1293. default:
  1294. no_motion_search(s, &mx, &my);
  1295. dmin = 0;
  1296. mx-= mb_x*16;
  1297. my-= mb_y*16;
  1298. break;
  1299. #if 0
  1300. case ME_FULL:
  1301. dmin = full_motion_search(s, &mx, &my, range, ref_picture);
  1302. mx-= mb_x*16;
  1303. my-= mb_y*16;
  1304. break;
  1305. case ME_LOG:
  1306. dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
  1307. mx-= mb_x*16;
  1308. my-= mb_y*16;
  1309. break;
  1310. case ME_PHODS:
  1311. dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
  1312. mx-= mb_x*16;
  1313. my-= mb_y*16;
  1314. break;
  1315. #endif
  1316. case ME_X1:
  1317. case ME_EPZS:
  1318. {
  1319. P_LEFT[0] = mv_table[mot_xy - 1][0];
  1320. P_LEFT[1] = mv_table[mot_xy - 1][1];
  1321. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1322. /* special case for first line */
  1323. if (!s->first_slice_line) {
  1324. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  1325. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  1326. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
  1327. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
  1328. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
  1329. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1330. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1331. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1332. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1333. }
  1334. c->pred_x= P_LEFT[0];
  1335. c->pred_y= P_LEFT[1];
  1336. }
  1337. if(mv_table == s->b_forw_mv_table){
  1338. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1339. }else{
  1340. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1341. }
  1342. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1343. break;
  1344. }
  1345. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1346. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1347. dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1348. //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
  1349. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1350. mv_table[mot_xy][0]= mx;
  1351. mv_table[mot_xy][1]= my;
  1352. return dmin;
  1353. }
  1354. static inline int check_bidir_mv(MpegEncContext * s,
  1355. int motion_fx, int motion_fy,
  1356. int motion_bx, int motion_by,
  1357. int pred_fx, int pred_fy,
  1358. int pred_bx, int pred_by,
  1359. int size, int h)
  1360. {
  1361. //FIXME optimize?
  1362. //FIXME better f_code prediction (max mv & distance)
  1363. //FIXME pointers
  1364. MotionEstContext * const c= &s->me;
  1365. uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1366. uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
  1367. int stride= c->stride;
  1368. uint8_t *dest_y = c->scratchpad;
  1369. uint8_t *ptr;
  1370. int dxy;
  1371. int src_x, src_y;
  1372. int fbmin;
  1373. uint8_t **src_data= c->src[0];
  1374. uint8_t **ref_data= c->ref[0];
  1375. uint8_t **ref2_data= c->ref[2];
  1376. if(s->quarter_sample){
  1377. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1378. src_x = motion_fx >> 2;
  1379. src_y = motion_fy >> 2;
  1380. ptr = ref_data[0] + (src_y * stride) + src_x;
  1381. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
  1382. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1383. src_x = motion_bx >> 2;
  1384. src_y = motion_by >> 2;
  1385. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1386. s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
  1387. }else{
  1388. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1389. src_x = motion_fx >> 1;
  1390. src_y = motion_fy >> 1;
  1391. ptr = ref_data[0] + (src_y * stride) + src_x;
  1392. s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1393. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1394. src_x = motion_bx >> 1;
  1395. src_y = motion_by >> 1;
  1396. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1397. s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1398. }
  1399. fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
  1400. +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
  1401. + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
  1402. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1403. }
  1404. //FIXME CHROMA !!!
  1405. return fbmin;
  1406. }
  1407. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1408. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1409. {
  1410. MotionEstContext * const c= &s->me;
  1411. const int mot_stride = s->mb_stride;
  1412. const int xy = mb_y *mot_stride + mb_x;
  1413. int fbmin;
  1414. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1415. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1416. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1417. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1418. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1419. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1420. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1421. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1422. const int flags= c->sub_flags;
  1423. const int qpel= flags&FLAG_QPEL;
  1424. const int shift= 1+qpel;
  1425. const int xmin= c->xmin<<shift;
  1426. const int ymin= c->ymin<<shift;
  1427. const int xmax= c->xmax<<shift;
  1428. const int ymax= c->ymax<<shift;
  1429. uint8_t map[8][8][8][8];
  1430. memset(map,0,sizeof(map));
  1431. #define BIDIR_MAP(fx,fy,bx,by) \
  1432. map[(motion_fx+fx)&7][(motion_fy+fy)&7][(motion_bx+bx)&7][(motion_by+by)&7]
  1433. BIDIR_MAP(0,0,0,0) = 1;
  1434. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1435. motion_bx, motion_by,
  1436. pred_fx, pred_fy,
  1437. pred_bx, pred_by,
  1438. 0, 16);
  1439. if(s->avctx->bidir_refine){
  1440. int score, end;
  1441. #define CHECK_BIDIR(fx,fy,bx,by)\
  1442. if( !BIDIR_MAP(fx,fy,bx,by)\
  1443. &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
  1444. &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
  1445. BIDIR_MAP(fx,fy,bx,by) = 1;\
  1446. 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);\
  1447. if(score < fbmin){\
  1448. fbmin= score;\
  1449. motion_fx+=fx;\
  1450. motion_fy+=fy;\
  1451. motion_bx+=bx;\
  1452. motion_by+=by;\
  1453. end=0;\
  1454. }\
  1455. }
  1456. #define CHECK_BIDIR2(a,b,c,d)\
  1457. CHECK_BIDIR(a,b,c,d)\
  1458. CHECK_BIDIR(-(a),-(b),-(c),-(d))
  1459. #define CHECK_BIDIRR(a,b,c,d)\
  1460. CHECK_BIDIR2(a,b,c,d)\
  1461. CHECK_BIDIR2(b,c,d,a)\
  1462. CHECK_BIDIR2(c,d,a,b)\
  1463. CHECK_BIDIR2(d,a,b,c)
  1464. do{
  1465. end=1;
  1466. CHECK_BIDIRR( 0, 0, 0, 1)
  1467. if(s->avctx->bidir_refine > 1){
  1468. CHECK_BIDIRR( 0, 0, 1, 1)
  1469. CHECK_BIDIR2( 0, 1, 0, 1)
  1470. CHECK_BIDIR2( 1, 0, 1, 0)
  1471. CHECK_BIDIRR( 0, 0,-1, 1)
  1472. CHECK_BIDIR2( 0,-1, 0, 1)
  1473. CHECK_BIDIR2(-1, 0, 1, 0)
  1474. if(s->avctx->bidir_refine > 2){
  1475. CHECK_BIDIRR( 0, 1, 1, 1)
  1476. CHECK_BIDIRR( 0,-1, 1, 1)
  1477. CHECK_BIDIRR( 0, 1,-1, 1)
  1478. CHECK_BIDIRR( 0, 1, 1,-1)
  1479. if(s->avctx->bidir_refine > 3){
  1480. CHECK_BIDIR2( 1, 1, 1, 1)
  1481. CHECK_BIDIRR( 1, 1, 1,-1)
  1482. CHECK_BIDIR2( 1, 1,-1,-1)
  1483. CHECK_BIDIR2( 1,-1,-1, 1)
  1484. CHECK_BIDIR2( 1,-1, 1,-1)
  1485. }
  1486. }
  1487. }
  1488. }while(!end);
  1489. }
  1490. s->b_bidir_forw_mv_table[xy][0]= motion_fx;
  1491. s->b_bidir_forw_mv_table[xy][1]= motion_fy;
  1492. s->b_bidir_back_mv_table[xy][0]= motion_bx;
  1493. s->b_bidir_back_mv_table[xy][1]= motion_by;
  1494. return fbmin;
  1495. }
  1496. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1497. {
  1498. MotionEstContext * const c= &s->me;
  1499. int P[10][2];
  1500. const int mot_stride = s->mb_stride;
  1501. const int mot_xy = mb_y*mot_stride + mb_x;
  1502. const int shift= 1+s->quarter_sample;
  1503. int dmin, i;
  1504. const int time_pp= s->pp_time;
  1505. const int time_pb= s->pb_time;
  1506. int mx, my, xmin, xmax, ymin, ymax;
  1507. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1508. c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
  1509. ymin= xmin=(-32)>>shift;
  1510. ymax= xmax= 31>>shift;
  1511. if(IS_8X8(s->next_picture.mb_type[mot_xy])){
  1512. s->mv_type= MV_TYPE_8X8;
  1513. }else{
  1514. s->mv_type= MV_TYPE_16X16;
  1515. }
  1516. for(i=0; i<4; i++){
  1517. int index= s->block_index[i];
  1518. int min, max;
  1519. c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
  1520. c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
  1521. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1522. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1523. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1524. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1525. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1526. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1527. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1528. min+= 16*mb_x - 1;
  1529. xmax= FFMIN(xmax, s->width - max);
  1530. xmin= FFMAX(xmin, - 16 - min);
  1531. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1532. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1533. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1534. min+= 16*mb_y - 1;
  1535. ymax= FFMIN(ymax, s->height - max);
  1536. ymin= FFMAX(ymin, - 16 - min);
  1537. if(s->mv_type == MV_TYPE_16X16) break;
  1538. }
  1539. assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1540. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1541. s->b_direct_mv_table[mot_xy][0]= 0;
  1542. s->b_direct_mv_table[mot_xy][1]= 0;
  1543. return 256*256*256*64;
  1544. }
  1545. c->xmin= xmin;
  1546. c->ymin= ymin;
  1547. c->xmax= xmax;
  1548. c->ymax= ymax;
  1549. c->flags |= FLAG_DIRECT;
  1550. c->sub_flags |= FLAG_DIRECT;
  1551. c->pred_x=0;
  1552. c->pred_y=0;
  1553. P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1554. P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1555. /* special case for first line */
  1556. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its clipped
  1557. P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1558. P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1559. P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1560. P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1561. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1562. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1563. }
  1564. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1565. if(c->sub_flags&FLAG_QPEL)
  1566. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1567. else
  1568. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1569. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1570. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1571. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1572. mv_table[mot_xy][0]= mx;
  1573. mv_table[mot_xy][1]= my;
  1574. c->flags &= ~FLAG_DIRECT;
  1575. c->sub_flags &= ~FLAG_DIRECT;
  1576. return dmin;
  1577. }
  1578. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1579. int mb_x, int mb_y)
  1580. {
  1581. MotionEstContext * const c= &s->me;
  1582. const int penalty_factor= c->mb_penalty_factor;
  1583. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1584. int type=0;
  1585. const int xy = mb_y*s->mb_stride + mb_x;
  1586. init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
  1587. get_limits(s, 16*mb_x, 16*mb_y);
  1588. c->skip=0;
  1589. if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){
  1590. int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
  1591. score= ((unsigned)(score*score + 128*256))>>16;
  1592. c->mc_mb_var_sum_temp += score;
  1593. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1594. s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
  1595. return;
  1596. }
  1597. if(c->avctx->me_threshold){
  1598. int vard= check_input_motion(s, mb_x, mb_y, 0);
  1599. if((vard+128)>>8 < c->avctx->me_threshold){
  1600. // pix = c->src[0][0];
  1601. // sum = s->dsp.pix_sum(pix, s->linesize);
  1602. // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  1603. // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  1604. s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1605. /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1606. c->mb_var_sum_temp += (varc+128)>>8;*/
  1607. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1608. /* if (vard <= 64<<8 || vard < varc) {
  1609. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1610. }else{
  1611. c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
  1612. }*/
  1613. return;
  1614. }
  1615. if((vard+128)>>8 < c->avctx->mb_threshold){
  1616. type= s->mb_type[mb_y*s->mb_stride + mb_x];
  1617. if(type == CANDIDATE_MB_TYPE_DIRECT){
  1618. direct_search(s, mb_x, mb_y);
  1619. }
  1620. if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1621. c->skip=0;
  1622. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
  1623. }
  1624. if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1625. c->skip=0;
  1626. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
  1627. }
  1628. if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1629. c->skip=0;
  1630. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1631. interlaced_search(s, 0,
  1632. s->b_field_mv_table[0], s->b_field_select_table[0],
  1633. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
  1634. }
  1635. if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1636. c->skip=0;
  1637. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1638. interlaced_search(s, 2,
  1639. s->b_field_mv_table[1], s->b_field_select_table[1],
  1640. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
  1641. }
  1642. return;
  1643. }
  1644. }
  1645. if (s->codec_id == CODEC_ID_MPEG4)
  1646. dmin= direct_search(s, mb_x, mb_y);
  1647. else
  1648. dmin= INT_MAX;
  1649. //FIXME penalty stuff for non mpeg4
  1650. c->skip=0;
  1651. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
  1652. c->skip=0;
  1653. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
  1654. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1655. c->skip=0;
  1656. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1657. //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1658. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1659. //FIXME mb type penalty
  1660. c->skip=0;
  1661. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1662. fimin= interlaced_search(s, 0,
  1663. s->b_field_mv_table[0], s->b_field_select_table[0],
  1664. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1665. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1666. bimin= interlaced_search(s, 2,
  1667. s->b_field_mv_table[1], s->b_field_select_table[1],
  1668. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1669. }else
  1670. fimin= bimin= INT_MAX;
  1671. {
  1672. int score= fmin;
  1673. type = CANDIDATE_MB_TYPE_FORWARD;
  1674. if (dmin <= score){
  1675. score = dmin;
  1676. type = CANDIDATE_MB_TYPE_DIRECT;
  1677. }
  1678. if(bmin<score){
  1679. score=bmin;
  1680. type= CANDIDATE_MB_TYPE_BACKWARD;
  1681. }
  1682. if(fbmin<score){
  1683. score=fbmin;
  1684. type= CANDIDATE_MB_TYPE_BIDIR;
  1685. }
  1686. if(fimin<score){
  1687. score=fimin;
  1688. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1689. }
  1690. if(bimin<score){
  1691. score=bimin;
  1692. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1693. }
  1694. score= ((unsigned)(score*score + 128*256))>>16;
  1695. c->mc_mb_var_sum_temp += score;
  1696. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1697. }
  1698. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1699. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1700. if(fimin < INT_MAX)
  1701. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1702. if(bimin < INT_MAX)
  1703. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1704. if(fimin < INT_MAX && bimin < INT_MAX){
  1705. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1706. }
  1707. //FIXME something smarter
  1708. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
  1709. if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
  1710. type |= CANDIDATE_MB_TYPE_DIRECT0;
  1711. #if 0
  1712. if(s->out_format == FMT_MPEG1)
  1713. type |= CANDIDATE_MB_TYPE_INTRA;
  1714. #endif
  1715. }
  1716. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1717. }
  1718. /* find best f_code for ME which do unlimited searches */
  1719. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1720. {
  1721. if(s->me_method>=ME_EPZS){
  1722. int score[8];
  1723. int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
  1724. uint8_t * fcode_tab= s->fcode_tab;
  1725. int best_fcode=-1;
  1726. int best_score=-10000000;
  1727. if(s->msmpeg4_version)
  1728. range= FFMIN(range, 16);
  1729. else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
  1730. range= FFMIN(range, 256);
  1731. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1732. for(y=0; y<s->mb_height; y++){
  1733. int x;
  1734. int xy= y*s->mb_stride;
  1735. for(x=0; x<s->mb_width; x++){
  1736. if(s->mb_type[xy] & type){
  1737. int mx= mv_table[xy][0];
  1738. int my= mv_table[xy][1];
  1739. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1740. fcode_tab[my + MAX_MV]);
  1741. int j;
  1742. if(mx >= range || mx < -range ||
  1743. my >= range || my < -range)
  1744. continue;
  1745. for(j=0; j<fcode && j<8; j++){
  1746. if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1747. score[j]-= 170;
  1748. }
  1749. }
  1750. xy++;
  1751. }
  1752. }
  1753. for(i=1; i<8; i++){
  1754. if(score[i] > best_score){
  1755. best_score= score[i];
  1756. best_fcode= i;
  1757. }
  1758. // printf("%d %d\n", i, score[i]);
  1759. }
  1760. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1761. return best_fcode;
  1762. /* for(i=0; i<=MAX_FCODE; i++){
  1763. printf("%d ", mv_num[i]);
  1764. }
  1765. printf("\n");*/
  1766. }else{
  1767. return 1;
  1768. }
  1769. }
  1770. void ff_fix_long_p_mvs(MpegEncContext * s)
  1771. {
  1772. MotionEstContext * const c= &s->me;
  1773. const int f_code= s->f_code;
  1774. int y, range;
  1775. assert(s->pict_type==P_TYPE);
  1776. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1777. assert(range <= 16 || !s->msmpeg4_version);
  1778. assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
  1779. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1780. //printf("%d no:%d %d//\n", clip, noclip, f_code);
  1781. if(s->flags&CODEC_FLAG_4MV){
  1782. const int wrap= s->b8_stride;
  1783. /* clip / convert to intra 8x8 type MVs */
  1784. for(y=0; y<s->mb_height; y++){
  1785. int xy= y*2*wrap;
  1786. int i= y*s->mb_stride;
  1787. int x;
  1788. for(x=0; x<s->mb_width; x++){
  1789. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1790. int block;
  1791. for(block=0; block<4; block++){
  1792. int off= (block& 1) + (block>>1)*wrap;
  1793. int mx= s->current_picture.motion_val[0][ xy + off ][0];
  1794. int my= s->current_picture.motion_val[0][ xy + off ][1];
  1795. if( mx >=range || mx <-range
  1796. || my >=range || my <-range){
  1797. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1798. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1799. s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  1800. }
  1801. }
  1802. }
  1803. xy+=2;
  1804. i++;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. /**
  1810. *
  1811. * @param truncate 1 for truncation, 0 for using intra
  1812. */
  1813. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1814. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1815. {
  1816. MotionEstContext * const c= &s->me;
  1817. int y, h_range, v_range;
  1818. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1819. int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1820. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1821. h_range= range;
  1822. v_range= field_select_table ? range>>1 : range;
  1823. /* clip / convert to intra 16x16 type MVs */
  1824. for(y=0; y<s->mb_height; y++){
  1825. int x;
  1826. int xy= y*s->mb_stride;
  1827. for(x=0; x<s->mb_width; x++){
  1828. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1829. if(field_select_table==NULL || field_select_table[xy] == field_select){
  1830. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1831. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1832. if(truncate){
  1833. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1834. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1835. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1836. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1837. }else{
  1838. s->mb_type[xy] &= ~type;
  1839. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1840. mv_table[xy][0]=
  1841. mv_table[xy][1]= 0;
  1842. }
  1843. }
  1844. }
  1845. }
  1846. xy++;
  1847. }
  1848. }
  1849. }