motion_est.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  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 libavcodec/motion_est.c
  26. * Motion estimation.
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <limits.h>
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "mathops.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. int 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. if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){
  218. av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
  219. return -1;
  220. }
  221. //special case of snow is needed because snow uses its own iterative ME code
  222. if(s->me_method!=ME_ZERO && s->me_method!=ME_EPZS && s->me_method!=ME_X1 && s->avctx->codec_id != CODEC_ID_SNOW){
  223. av_log(s->avctx, AV_LOG_ERROR, "me_method is only allowed to be set to zero and epzs; for hex,umh,full and others see dia_size\n");
  224. return -1;
  225. }
  226. c->avctx= s->avctx;
  227. if(cache_size < 2*dia_size && !c->stride){
  228. av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
  229. }
  230. ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
  231. ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
  232. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
  233. ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
  234. c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
  235. c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
  236. c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
  237. /*FIXME s->no_rounding b_type*/
  238. if(s->flags&CODEC_FLAG_QPEL){
  239. c->sub_motion_search= qpel_motion_search;
  240. c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
  241. if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
  242. else c->qpel_put= s->dsp.put_qpel_pixels_tab;
  243. }else{
  244. if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
  245. c->sub_motion_search= hpel_motion_search;
  246. else if( c->avctx->me_sub_cmp == FF_CMP_SAD
  247. && c->avctx-> me_cmp == FF_CMP_SAD
  248. && c->avctx-> mb_cmp == FF_CMP_SAD)
  249. c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
  250. else
  251. c->sub_motion_search= hpel_motion_search;
  252. }
  253. c->hpel_avg= s->dsp.avg_pixels_tab;
  254. if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
  255. else c->hpel_put= s->dsp.put_pixels_tab;
  256. if(s->linesize){
  257. c->stride = s->linesize;
  258. c->uvstride= s->uvlinesize;
  259. }else{
  260. c->stride = 16*s->mb_width + 32;
  261. c->uvstride= 8*s->mb_width + 16;
  262. }
  263. /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
  264. * not have yet, and even if we had, the motion estimation code
  265. * does not expect it. */
  266. if(s->codec_id != CODEC_ID_SNOW){
  267. if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
  268. s->dsp.me_cmp[2]= zero_cmp;
  269. }
  270. if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
  271. s->dsp.me_sub_cmp[2]= zero_cmp;
  272. }
  273. c->hpel_put[2][0]= c->hpel_put[2][1]=
  274. c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
  275. }
  276. if(s->codec_id == CODEC_ID_H261){
  277. c->sub_motion_search= no_sub_motion_search;
  278. }
  279. return 0;
  280. }
  281. #if 0
  282. static int pix_dev(uint8_t * pix, int line_size, int mean)
  283. {
  284. int s, i, j;
  285. s = 0;
  286. for (i = 0; i < 16; i++) {
  287. for (j = 0; j < 16; j += 8) {
  288. s += FFABS(pix[0]-mean);
  289. s += FFABS(pix[1]-mean);
  290. s += FFABS(pix[2]-mean);
  291. s += FFABS(pix[3]-mean);
  292. s += FFABS(pix[4]-mean);
  293. s += FFABS(pix[5]-mean);
  294. s += FFABS(pix[6]-mean);
  295. s += FFABS(pix[7]-mean);
  296. pix += 8;
  297. }
  298. pix += line_size - 16;
  299. }
  300. return s;
  301. }
  302. #endif
  303. static inline void no_motion_search(MpegEncContext * s,
  304. int *mx_ptr, int *my_ptr)
  305. {
  306. *mx_ptr = 16 * s->mb_x;
  307. *my_ptr = 16 * s->mb_y;
  308. }
  309. #define Z_THRESHOLD 256
  310. #define CHECK_SAD_HALF_MV(suffix, x, y) \
  311. {\
  312. d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
  313. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
  314. COPY3_IF_LT(dminh, d, dx, x, dy, y)\
  315. }
  316. static inline int sad_hpel_motion_search(MpegEncContext * s,
  317. int *mx_ptr, int *my_ptr, int dmin,
  318. int src_index, int ref_index,
  319. int size, int h)
  320. {
  321. MotionEstContext * const c= &s->me;
  322. const int penalty_factor= c->sub_penalty_factor;
  323. int mx, my, dminh;
  324. uint8_t *pix, *ptr;
  325. int stride= c->stride;
  326. const int flags= c->sub_flags;
  327. LOAD_COMMON
  328. assert(flags == 0);
  329. if(c->skip){
  330. // printf("S");
  331. *mx_ptr = 0;
  332. *my_ptr = 0;
  333. return dmin;
  334. }
  335. // printf("N");
  336. pix = c->src[src_index][0];
  337. mx = *mx_ptr;
  338. my = *my_ptr;
  339. ptr = c->ref[ref_index][0] + (my * stride) + mx;
  340. dminh = dmin;
  341. if (mx > xmin && mx < xmax &&
  342. my > ymin && my < ymax) {
  343. int dx=0, dy=0;
  344. int d, pen_x, pen_y;
  345. const int index= (my<<ME_MAP_SHIFT) + mx;
  346. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  347. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  348. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  349. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  350. mx<<=1;
  351. my<<=1;
  352. pen_x= pred_x + mx;
  353. pen_y= pred_y + my;
  354. ptr-= stride;
  355. if(t<=b){
  356. CHECK_SAD_HALF_MV(y2 , 0, -1)
  357. if(l<=r){
  358. CHECK_SAD_HALF_MV(xy2, -1, -1)
  359. if(t+r<=b+l){
  360. CHECK_SAD_HALF_MV(xy2, +1, -1)
  361. ptr+= stride;
  362. }else{
  363. ptr+= stride;
  364. CHECK_SAD_HALF_MV(xy2, -1, +1)
  365. }
  366. CHECK_SAD_HALF_MV(x2 , -1, 0)
  367. }else{
  368. CHECK_SAD_HALF_MV(xy2, +1, -1)
  369. if(t+l<=b+r){
  370. CHECK_SAD_HALF_MV(xy2, -1, -1)
  371. ptr+= stride;
  372. }else{
  373. ptr+= stride;
  374. CHECK_SAD_HALF_MV(xy2, +1, +1)
  375. }
  376. CHECK_SAD_HALF_MV(x2 , +1, 0)
  377. }
  378. }else{
  379. if(l<=r){
  380. if(t+l<=b+r){
  381. CHECK_SAD_HALF_MV(xy2, -1, -1)
  382. ptr+= stride;
  383. }else{
  384. ptr+= stride;
  385. CHECK_SAD_HALF_MV(xy2, +1, +1)
  386. }
  387. CHECK_SAD_HALF_MV(x2 , -1, 0)
  388. CHECK_SAD_HALF_MV(xy2, -1, +1)
  389. }else{
  390. if(t+r<=b+l){
  391. CHECK_SAD_HALF_MV(xy2, +1, -1)
  392. ptr+= stride;
  393. }else{
  394. ptr+= stride;
  395. CHECK_SAD_HALF_MV(xy2, -1, +1)
  396. }
  397. CHECK_SAD_HALF_MV(x2 , +1, 0)
  398. CHECK_SAD_HALF_MV(xy2, +1, +1)
  399. }
  400. CHECK_SAD_HALF_MV(y2 , 0, +1)
  401. }
  402. mx+=dx;
  403. my+=dy;
  404. }else{
  405. mx<<=1;
  406. my<<=1;
  407. }
  408. *mx_ptr = mx;
  409. *my_ptr = my;
  410. return dminh;
  411. }
  412. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  413. {
  414. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  415. s->p_mv_table[xy][0] = mx;
  416. s->p_mv_table[xy][1] = my;
  417. /* has already been set to the 4 MV if 4MV is done */
  418. if(mv4){
  419. int mot_xy= s->block_index[0];
  420. s->current_picture.motion_val[0][mot_xy ][0]= mx;
  421. s->current_picture.motion_val[0][mot_xy ][1]= my;
  422. s->current_picture.motion_val[0][mot_xy+1][0]= mx;
  423. s->current_picture.motion_val[0][mot_xy+1][1]= my;
  424. mot_xy += s->b8_stride;
  425. s->current_picture.motion_val[0][mot_xy ][0]= mx;
  426. s->current_picture.motion_val[0][mot_xy ][1]= my;
  427. s->current_picture.motion_val[0][mot_xy+1][0]= mx;
  428. s->current_picture.motion_val[0][mot_xy+1][1]= my;
  429. }
  430. }
  431. /**
  432. * get fullpel ME search limits.
  433. */
  434. static inline void get_limits(MpegEncContext *s, int x, int y)
  435. {
  436. MotionEstContext * const c= &s->me;
  437. int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
  438. /*
  439. if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
  440. else c->range= 16;
  441. */
  442. if (s->unrestricted_mv) {
  443. c->xmin = - x - 16;
  444. c->ymin = - y - 16;
  445. c->xmax = - x + s->mb_width *16;
  446. c->ymax = - y + s->mb_height*16;
  447. } else if (s->out_format == FMT_H261){
  448. // Search range of H261 is different from other codec standards
  449. c->xmin = (x > 15) ? - 15 : 0;
  450. c->ymin = (y > 15) ? - 15 : 0;
  451. c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
  452. c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
  453. } else {
  454. c->xmin = - x;
  455. c->ymin = - y;
  456. c->xmax = - x + s->mb_width *16 - 16;
  457. c->ymax = - y + s->mb_height*16 - 16;
  458. }
  459. if(range){
  460. c->xmin = FFMAX(c->xmin,-range);
  461. c->xmax = FFMIN(c->xmax, range);
  462. c->ymin = FFMAX(c->ymin,-range);
  463. c->ymax = FFMIN(c->ymax, range);
  464. }
  465. }
  466. static inline void init_mv4_ref(MotionEstContext *c){
  467. const int stride= c->stride;
  468. c->ref[1][0] = c->ref[0][0] + 8;
  469. c->ref[2][0] = c->ref[0][0] + 8*stride;
  470. c->ref[3][0] = c->ref[2][0] + 8;
  471. c->src[1][0] = c->src[0][0] + 8;
  472. c->src[2][0] = c->src[0][0] + 8*stride;
  473. c->src[3][0] = c->src[2][0] + 8;
  474. }
  475. static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
  476. {
  477. MotionEstContext * const c= &s->me;
  478. const int size= 1;
  479. const int h=8;
  480. int block;
  481. int P[10][2];
  482. int dmin_sum=0, mx4_sum=0, my4_sum=0;
  483. int same=1;
  484. const int stride= c->stride;
  485. uint8_t *mv_penalty= c->current_mv_penalty;
  486. init_mv4_ref(c);
  487. for(block=0; block<4; block++){
  488. int mx4, my4;
  489. int pred_x4, pred_y4;
  490. int dmin4;
  491. static const int off[4]= {2, 1, 1, -1};
  492. const int mot_stride = s->b8_stride;
  493. const int mot_xy = s->block_index[block];
  494. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  495. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  496. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  497. /* special case for first line */
  498. if (s->first_slice_line && block<2) {
  499. c->pred_x= pred_x4= P_LEFT[0];
  500. c->pred_y= pred_y4= P_LEFT[1];
  501. } else {
  502. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  503. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  504. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
  505. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
  506. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  507. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  508. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  509. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  510. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  511. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  512. c->pred_x= pred_x4 = P_MEDIAN[0];
  513. c->pred_y= pred_y4 = P_MEDIAN[1];
  514. }
  515. P_MV1[0]= mx;
  516. P_MV1[1]= my;
  517. dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
  518. dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
  519. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  520. int dxy;
  521. const int offset= ((block&1) + (block>>1)*stride)*8;
  522. uint8_t *dest_y = c->scratchpad + offset;
  523. if(s->quarter_sample){
  524. uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
  525. dxy = ((my4 & 3) << 2) | (mx4 & 3);
  526. if(s->no_rounding)
  527. s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride);
  528. else
  529. s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride);
  530. }else{
  531. uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
  532. dxy = ((my4 & 1) << 1) | (mx4 & 1);
  533. if(s->no_rounding)
  534. s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
  535. else
  536. s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
  537. }
  538. dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
  539. }else
  540. dmin_sum+= dmin4;
  541. if(s->quarter_sample){
  542. mx4_sum+= mx4/2;
  543. my4_sum+= my4/2;
  544. }else{
  545. mx4_sum+= mx4;
  546. my4_sum+= my4;
  547. }
  548. s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
  549. s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
  550. if(mx4 != mx || my4 != my) same=0;
  551. }
  552. if(same)
  553. return INT_MAX;
  554. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  555. 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);
  556. }
  557. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  558. int dxy;
  559. int mx, my;
  560. int offset;
  561. mx= ff_h263_round_chroma(mx4_sum);
  562. my= ff_h263_round_chroma(my4_sum);
  563. dxy = ((my & 1) << 1) | (mx & 1);
  564. offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
  565. if(s->no_rounding){
  566. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8);
  567. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8);
  568. }else{
  569. s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8);
  570. s->dsp.put_pixels_tab [1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8);
  571. }
  572. 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);
  573. 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);
  574. }
  575. c->pred_x= mx;
  576. c->pred_y= my;
  577. switch(c->avctx->mb_cmp&0xFF){
  578. /*case FF_CMP_SSE:
  579. return dmin_sum+ 32*s->qscale*s->qscale;*/
  580. case FF_CMP_RD:
  581. return dmin_sum;
  582. default:
  583. return dmin_sum+ 11*c->mb_penalty_factor;
  584. }
  585. }
  586. static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
  587. MotionEstContext * const c= &s->me;
  588. c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
  589. c->src[1][0] = c->src[0][0] + s->linesize;
  590. if(c->flags & FLAG_CHROMA){
  591. c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
  592. c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
  593. c->src[1][1] = c->src[0][1] + s->uvlinesize;
  594. c->src[1][2] = c->src[0][2] + s->uvlinesize;
  595. }
  596. }
  597. static int interlaced_search(MpegEncContext *s, int ref_index,
  598. int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  599. {
  600. MotionEstContext * const c= &s->me;
  601. const int size=0;
  602. const int h=8;
  603. int block;
  604. int P[10][2];
  605. uint8_t * const mv_penalty= c->current_mv_penalty;
  606. int same=1;
  607. const int stride= 2*s->linesize;
  608. int dmin_sum= 0;
  609. const int mot_stride= s->mb_stride;
  610. const int xy= s->mb_x + s->mb_y*mot_stride;
  611. c->ymin>>=1;
  612. c->ymax>>=1;
  613. c->stride<<=1;
  614. c->uvstride<<=1;
  615. init_interlaced_ref(s, ref_index);
  616. for(block=0; block<2; block++){
  617. int field_select;
  618. int best_dmin= INT_MAX;
  619. int best_field= -1;
  620. for(field_select=0; field_select<2; field_select++){
  621. int dmin, mx_i, my_i;
  622. int16_t (*mv_table)[2]= mv_tables[block][field_select];
  623. if(user_field_select){
  624. assert(field_select==0 || field_select==1);
  625. assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
  626. if(field_select_tables[block][xy] != field_select)
  627. continue;
  628. }
  629. P_LEFT[0] = mv_table[xy - 1][0];
  630. P_LEFT[1] = mv_table[xy - 1][1];
  631. if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
  632. c->pred_x= P_LEFT[0];
  633. c->pred_y= P_LEFT[1];
  634. if(!s->first_slice_line){
  635. P_TOP[0] = mv_table[xy - mot_stride][0];
  636. P_TOP[1] = mv_table[xy - mot_stride][1];
  637. P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
  638. P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
  639. if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
  640. if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
  641. if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
  642. if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
  643. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  644. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  645. }
  646. P_MV1[0]= mx; //FIXME not correct if block != field_select
  647. P_MV1[1]= my / 2;
  648. dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
  649. dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
  650. mv_table[xy][0]= mx_i;
  651. mv_table[xy][1]= my_i;
  652. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  653. int dxy;
  654. //FIXME chroma ME
  655. uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
  656. dxy = ((my_i & 1) << 1) | (mx_i & 1);
  657. if(s->no_rounding){
  658. s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
  659. }else{
  660. s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
  661. }
  662. dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
  663. dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
  664. }else
  665. dmin+= c->mb_penalty_factor; //field_select bits
  666. dmin += field_select != block; //slightly prefer same field
  667. if(dmin < best_dmin){
  668. best_dmin= dmin;
  669. best_field= field_select;
  670. }
  671. }
  672. {
  673. int16_t (*mv_table)[2]= mv_tables[block][best_field];
  674. if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
  675. if(mv_table[xy][1]&1) same=0;
  676. if(mv_table[xy][1]*2 != my) same=0;
  677. if(best_field != block) same=0;
  678. }
  679. field_select_tables[block][xy]= best_field;
  680. dmin_sum += best_dmin;
  681. }
  682. c->ymin<<=1;
  683. c->ymax<<=1;
  684. c->stride>>=1;
  685. c->uvstride>>=1;
  686. if(same)
  687. return INT_MAX;
  688. switch(c->avctx->mb_cmp&0xFF){
  689. /*case FF_CMP_SSE:
  690. return dmin_sum+ 32*s->qscale*s->qscale;*/
  691. case FF_CMP_RD:
  692. return dmin_sum;
  693. default:
  694. return dmin_sum+ 11*c->mb_penalty_factor;
  695. }
  696. }
  697. static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  698. int ymax= s->me.ymax>>interlaced;
  699. int ymin= s->me.ymin>>interlaced;
  700. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  701. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  702. if(mv[1] < ymin) mv[1] = ymin;
  703. if(mv[1] > ymax) mv[1] = ymax;
  704. }
  705. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  706. MotionEstContext * const c= &s->me;
  707. Picture *p= s->current_picture_ptr;
  708. int mb_xy= mb_x + mb_y*s->mb_stride;
  709. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  710. int mb_type= s->current_picture.mb_type[mb_xy];
  711. int flags= c->flags;
  712. int shift= (flags&FLAG_QPEL) + 1;
  713. int mask= (1<<shift)-1;
  714. int x, y, i;
  715. int d=0;
  716. me_cmp_func cmpf= s->dsp.sse[0];
  717. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  718. if(p_type && USES_LIST(mb_type, 1)){
  719. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  720. return INT_MAX/2;
  721. }
  722. assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  723. for(i=0; i<4; i++){
  724. int xy= s->block_index[i];
  725. clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
  726. clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
  727. }
  728. if(IS_INTERLACED(mb_type)){
  729. int xy2= xy + s->b8_stride;
  730. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  731. c->stride<<=1;
  732. c->uvstride<<=1;
  733. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  734. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  735. return INT_MAX/2;
  736. }
  737. if(USES_LIST(mb_type, 0)){
  738. int field_select0= p->ref_index[0][xy ];
  739. int field_select1= p->ref_index[0][xy2];
  740. assert(field_select0==0 ||field_select0==1);
  741. assert(field_select1==0 ||field_select1==1);
  742. init_interlaced_ref(s, 0);
  743. if(p_type){
  744. s->p_field_select_table[0][mb_xy]= field_select0;
  745. s->p_field_select_table[1][mb_xy]= field_select1;
  746. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
  747. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
  748. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  749. }else{
  750. s->b_field_select_table[0][0][mb_xy]= field_select0;
  751. s->b_field_select_table[0][1][mb_xy]= field_select1;
  752. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
  753. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
  754. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  755. }
  756. x= p->motion_val[0][xy ][0];
  757. y= p->motion_val[0][xy ][1];
  758. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  759. x= p->motion_val[0][xy2][0];
  760. y= p->motion_val[0][xy2][1];
  761. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  762. }
  763. if(USES_LIST(mb_type, 1)){
  764. int field_select0= p->ref_index[1][xy ];
  765. int field_select1= p->ref_index[1][xy2];
  766. assert(field_select0==0 ||field_select0==1);
  767. assert(field_select1==0 ||field_select1==1);
  768. init_interlaced_ref(s, 2);
  769. s->b_field_select_table[1][0][mb_xy]= field_select0;
  770. s->b_field_select_table[1][1][mb_xy]= field_select1;
  771. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
  772. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2];
  773. if(USES_LIST(mb_type, 0)){
  774. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  775. }else{
  776. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  777. }
  778. x= p->motion_val[1][xy ][0];
  779. y= p->motion_val[1][xy ][1];
  780. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  781. x= p->motion_val[1][xy2][0];
  782. y= p->motion_val[1][xy2][1];
  783. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  784. //FIXME bidir scores
  785. }
  786. c->stride>>=1;
  787. c->uvstride>>=1;
  788. }else if(IS_8X8(mb_type)){
  789. if(!(s->flags & CODEC_FLAG_4MV)){
  790. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  791. return INT_MAX/2;
  792. }
  793. cmpf= s->dsp.sse[1];
  794. chroma_cmpf= s->dsp.sse[1];
  795. init_mv4_ref(c);
  796. for(i=0; i<4; i++){
  797. xy= s->block_index[i];
  798. x= p->motion_val[0][xy][0];
  799. y= p->motion_val[0][xy][1];
  800. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  801. }
  802. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  803. }else{
  804. if(USES_LIST(mb_type, 0)){
  805. if(p_type){
  806. *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  807. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  808. }else if(USES_LIST(mb_type, 1)){
  809. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  810. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
  811. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  812. }else{
  813. *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  814. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  815. }
  816. x= p->motion_val[0][xy][0];
  817. y= p->motion_val[0][xy][1];
  818. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  819. }else if(USES_LIST(mb_type, 1)){
  820. *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
  821. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  822. x= p->motion_val[1][xy][0];
  823. y= p->motion_val[1][xy][1];
  824. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  825. }else
  826. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  827. }
  828. return d;
  829. }
  830. void ff_estimate_p_frame_motion(MpegEncContext * s,
  831. int mb_x, int mb_y)
  832. {
  833. MotionEstContext * const c= &s->me;
  834. uint8_t *pix, *ppix;
  835. int sum, mx, my, dmin;
  836. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  837. int vard; ///< sum of squared differences with the estimated motion vector
  838. int P[10][2];
  839. const int shift= 1+s->quarter_sample;
  840. int mb_type=0;
  841. Picture * const pic= &s->current_picture;
  842. init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
  843. assert(s->quarter_sample==0 || s->quarter_sample==1);
  844. assert(s->linesize == c->stride);
  845. assert(s->uvlinesize == c->uvstride);
  846. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  847. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  848. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  849. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  850. get_limits(s, 16*mb_x, 16*mb_y);
  851. c->skip=0;
  852. /* intra / predictive decision */
  853. pix = c->src[0][0];
  854. sum = s->dsp.pix_sum(pix, s->linesize);
  855. varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  856. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  857. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  858. c->mb_var_sum_temp += (varc+128)>>8;
  859. if(c->avctx->me_threshold){
  860. vard= check_input_motion(s, mb_x, mb_y, 1);
  861. if((vard+128)>>8 < c->avctx->me_threshold){
  862. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  863. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  864. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  865. c->mc_mb_var_sum_temp += (vard+128)>>8;
  866. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  867. return;
  868. }
  869. if((vard+128)>>8 < c->avctx->mb_threshold)
  870. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  871. }
  872. switch(s->me_method) {
  873. case ME_ZERO:
  874. default:
  875. no_motion_search(s, &mx, &my);
  876. mx-= mb_x*16;
  877. my-= mb_y*16;
  878. dmin = 0;
  879. break;
  880. case ME_X1:
  881. case ME_EPZS:
  882. {
  883. const int mot_stride = s->b8_stride;
  884. const int mot_xy = s->block_index[0];
  885. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  886. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  887. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  888. if(!s->first_slice_line) {
  889. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  890. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  891. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
  892. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
  893. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  894. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  895. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  896. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  897. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  898. if(s->out_format == FMT_H263){
  899. c->pred_x = P_MEDIAN[0];
  900. c->pred_y = P_MEDIAN[1];
  901. }else { /* mpeg1 at least */
  902. c->pred_x= P_LEFT[0];
  903. c->pred_y= P_LEFT[1];
  904. }
  905. }else{
  906. c->pred_x= P_LEFT[0];
  907. c->pred_y= P_LEFT[1];
  908. }
  909. }
  910. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  911. break;
  912. }
  913. /* At this point (mx,my) are full-pell and the relative displacement */
  914. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  915. vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
  916. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  917. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  918. c->mc_mb_var_sum_temp += (vard+128)>>8;
  919. #if 0
  920. printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
  921. varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
  922. #endif
  923. if(mb_type){
  924. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  925. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  926. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  927. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  928. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  929. set_p_mv_tables(s, mx, my, 1);
  930. }else{
  931. mx <<=shift;
  932. my <<=shift;
  933. }
  934. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  935. h263_mv4_search(s, mx, my, shift);
  936. set_p_mv_tables(s, mx, my, 0);
  937. }
  938. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  939. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  940. }
  941. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  942. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  943. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  944. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  945. if (vard*2 + 200*256 > varc)
  946. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  947. if (varc*2 + 200*256 > vard || s->qscale > 24){
  948. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  949. mb_type|= CANDIDATE_MB_TYPE_INTER;
  950. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  951. if(s->flags&CODEC_FLAG_MV0)
  952. if(mx || my)
  953. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  954. }else{
  955. mx <<=shift;
  956. my <<=shift;
  957. }
  958. if((s->flags&CODEC_FLAG_4MV)
  959. && !c->skip && varc>50<<8 && vard>10<<8){
  960. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  961. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  962. set_p_mv_tables(s, mx, my, 0);
  963. }else
  964. set_p_mv_tables(s, mx, my, 1);
  965. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  966. && !c->skip){ //FIXME varc/d checks
  967. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  968. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  969. }
  970. }else{
  971. int intra_score, i;
  972. mb_type= CANDIDATE_MB_TYPE_INTER;
  973. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  974. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  975. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  976. if((s->flags&CODEC_FLAG_4MV)
  977. && !c->skip && varc>50<<8 && vard>10<<8){
  978. int dmin4= h263_mv4_search(s, mx, my, shift);
  979. if(dmin4 < dmin){
  980. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  981. dmin=dmin4;
  982. }
  983. }
  984. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  985. && !c->skip){ //FIXME varc/d checks
  986. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  987. if(dmin_i < dmin){
  988. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  989. dmin= dmin_i;
  990. }
  991. }
  992. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  993. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  994. /* get intra luma score */
  995. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  996. intra_score= varc - 500;
  997. }else{
  998. int mean= (sum+128)>>8;
  999. mean*= 0x01010101;
  1000. for(i=0; i<16; i++){
  1001. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1002. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1003. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1004. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1005. }
  1006. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1007. }
  1008. #if 0 //FIXME
  1009. /* get chroma score */
  1010. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1011. for(i=1; i<3; i++){
  1012. uint8_t *dest_c;
  1013. int mean;
  1014. if(s->out_format == FMT_H263){
  1015. mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
  1016. }else{
  1017. mean= (s->last_dc[i] + 4)>>3;
  1018. }
  1019. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1020. mean*= 0x01010101;
  1021. for(i=0; i<8; i++){
  1022. *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
  1023. *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
  1024. }
  1025. intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
  1026. }
  1027. }
  1028. #endif
  1029. intra_score += c->mb_penalty_factor*16;
  1030. if(intra_score < dmin){
  1031. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1032. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1033. }else
  1034. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
  1035. {
  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. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1039. }
  1040. }
  1041. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1042. }
  1043. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1044. int mb_x, int mb_y)
  1045. {
  1046. MotionEstContext * const c= &s->me;
  1047. int mx, my, dmin;
  1048. int P[10][2];
  1049. const int shift= 1+s->quarter_sample;
  1050. const int xy= mb_x + mb_y*s->mb_stride;
  1051. init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
  1052. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1053. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1054. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1055. get_limits(s, 16*mb_x, 16*mb_y);
  1056. c->skip=0;
  1057. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1058. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1059. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1060. /* special case for first line */
  1061. if (s->first_slice_line) {
  1062. c->pred_x= P_LEFT[0];
  1063. c->pred_y= P_LEFT[1];
  1064. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1065. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1066. } else {
  1067. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1068. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1069. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1070. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1071. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1072. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1073. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1074. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1075. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1076. c->pred_x = P_MEDIAN[0];
  1077. c->pred_y = P_MEDIAN[1];
  1078. }
  1079. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1080. s->p_mv_table[xy][0] = mx<<shift;
  1081. s->p_mv_table[xy][1] = my<<shift;
  1082. return dmin;
  1083. }
  1084. static int ff_estimate_motion_b(MpegEncContext * s,
  1085. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1086. {
  1087. MotionEstContext * const c= &s->me;
  1088. int mx, my, dmin;
  1089. int P[10][2];
  1090. const int shift= 1+s->quarter_sample;
  1091. const int mot_stride = s->mb_stride;
  1092. const int mot_xy = mb_y*mot_stride + mb_x;
  1093. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1094. int mv_scale;
  1095. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1096. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1097. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1098. c->current_mv_penalty= mv_penalty;
  1099. get_limits(s, 16*mb_x, 16*mb_y);
  1100. switch(s->me_method) {
  1101. case ME_ZERO:
  1102. default:
  1103. no_motion_search(s, &mx, &my);
  1104. dmin = 0;
  1105. mx-= mb_x*16;
  1106. my-= mb_y*16;
  1107. break;
  1108. case ME_X1:
  1109. case ME_EPZS:
  1110. {
  1111. P_LEFT[0] = mv_table[mot_xy - 1][0];
  1112. P_LEFT[1] = mv_table[mot_xy - 1][1];
  1113. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1114. /* special case for first line */
  1115. if (!s->first_slice_line) {
  1116. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  1117. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  1118. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
  1119. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
  1120. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
  1121. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1122. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1123. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1124. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1125. }
  1126. c->pred_x= P_LEFT[0];
  1127. c->pred_y= P_LEFT[1];
  1128. }
  1129. if(mv_table == s->b_forw_mv_table){
  1130. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1131. }else{
  1132. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1133. }
  1134. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1135. break;
  1136. }
  1137. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1138. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1139. dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1140. //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
  1141. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1142. mv_table[mot_xy][0]= mx;
  1143. mv_table[mot_xy][1]= my;
  1144. return dmin;
  1145. }
  1146. static inline int check_bidir_mv(MpegEncContext * s,
  1147. int motion_fx, int motion_fy,
  1148. int motion_bx, int motion_by,
  1149. int pred_fx, int pred_fy,
  1150. int pred_bx, int pred_by,
  1151. int size, int h)
  1152. {
  1153. //FIXME optimize?
  1154. //FIXME better f_code prediction (max mv & distance)
  1155. //FIXME pointers
  1156. MotionEstContext * const c= &s->me;
  1157. uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1158. uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
  1159. int stride= c->stride;
  1160. uint8_t *dest_y = c->scratchpad;
  1161. uint8_t *ptr;
  1162. int dxy;
  1163. int src_x, src_y;
  1164. int fbmin;
  1165. uint8_t **src_data= c->src[0];
  1166. uint8_t **ref_data= c->ref[0];
  1167. uint8_t **ref2_data= c->ref[2];
  1168. if(s->quarter_sample){
  1169. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1170. src_x = motion_fx >> 2;
  1171. src_y = motion_fy >> 2;
  1172. ptr = ref_data[0] + (src_y * stride) + src_x;
  1173. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
  1174. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1175. src_x = motion_bx >> 2;
  1176. src_y = motion_by >> 2;
  1177. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1178. s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
  1179. }else{
  1180. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1181. src_x = motion_fx >> 1;
  1182. src_y = motion_fy >> 1;
  1183. ptr = ref_data[0] + (src_y * stride) + src_x;
  1184. s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1185. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1186. src_x = motion_bx >> 1;
  1187. src_y = motion_by >> 1;
  1188. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1189. s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1190. }
  1191. fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
  1192. +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
  1193. + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
  1194. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1195. }
  1196. //FIXME CHROMA !!!
  1197. return fbmin;
  1198. }
  1199. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1200. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1201. {
  1202. MotionEstContext * const c= &s->me;
  1203. const int mot_stride = s->mb_stride;
  1204. const int xy = mb_y *mot_stride + mb_x;
  1205. int fbmin;
  1206. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1207. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1208. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1209. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1210. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1211. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1212. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1213. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1214. const int flags= c->sub_flags;
  1215. const int qpel= flags&FLAG_QPEL;
  1216. const int shift= 1+qpel;
  1217. const int xmin= c->xmin<<shift;
  1218. const int ymin= c->ymin<<shift;
  1219. const int xmax= c->xmax<<shift;
  1220. const int ymax= c->ymax<<shift;
  1221. uint8_t map[8][8][8][8];
  1222. memset(map,0,sizeof(map));
  1223. #define BIDIR_MAP(fx,fy,bx,by) \
  1224. map[(motion_fx+fx)&7][(motion_fy+fy)&7][(motion_bx+bx)&7][(motion_by+by)&7]
  1225. BIDIR_MAP(0,0,0,0) = 1;
  1226. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1227. motion_bx, motion_by,
  1228. pred_fx, pred_fy,
  1229. pred_bx, pred_by,
  1230. 0, 16);
  1231. if(s->avctx->bidir_refine){
  1232. int score, end;
  1233. #define CHECK_BIDIR(fx,fy,bx,by)\
  1234. if( !BIDIR_MAP(fx,fy,bx,by)\
  1235. &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
  1236. &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
  1237. BIDIR_MAP(fx,fy,bx,by) = 1;\
  1238. 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);\
  1239. if(score < fbmin){\
  1240. fbmin= score;\
  1241. motion_fx+=fx;\
  1242. motion_fy+=fy;\
  1243. motion_bx+=bx;\
  1244. motion_by+=by;\
  1245. end=0;\
  1246. }\
  1247. }
  1248. #define CHECK_BIDIR2(a,b,c,d)\
  1249. CHECK_BIDIR(a,b,c,d)\
  1250. CHECK_BIDIR(-(a),-(b),-(c),-(d))
  1251. #define CHECK_BIDIRR(a,b,c,d)\
  1252. CHECK_BIDIR2(a,b,c,d)\
  1253. CHECK_BIDIR2(b,c,d,a)\
  1254. CHECK_BIDIR2(c,d,a,b)\
  1255. CHECK_BIDIR2(d,a,b,c)
  1256. do{
  1257. end=1;
  1258. CHECK_BIDIRR( 0, 0, 0, 1)
  1259. if(s->avctx->bidir_refine > 1){
  1260. CHECK_BIDIRR( 0, 0, 1, 1)
  1261. CHECK_BIDIR2( 0, 1, 0, 1)
  1262. CHECK_BIDIR2( 1, 0, 1, 0)
  1263. CHECK_BIDIRR( 0, 0,-1, 1)
  1264. CHECK_BIDIR2( 0,-1, 0, 1)
  1265. CHECK_BIDIR2(-1, 0, 1, 0)
  1266. if(s->avctx->bidir_refine > 2){
  1267. CHECK_BIDIRR( 0, 1, 1, 1)
  1268. CHECK_BIDIRR( 0,-1, 1, 1)
  1269. CHECK_BIDIRR( 0, 1,-1, 1)
  1270. CHECK_BIDIRR( 0, 1, 1,-1)
  1271. if(s->avctx->bidir_refine > 3){
  1272. CHECK_BIDIR2( 1, 1, 1, 1)
  1273. CHECK_BIDIRR( 1, 1, 1,-1)
  1274. CHECK_BIDIR2( 1, 1,-1,-1)
  1275. CHECK_BIDIR2( 1,-1,-1, 1)
  1276. CHECK_BIDIR2( 1,-1, 1,-1)
  1277. }
  1278. }
  1279. }
  1280. }while(!end);
  1281. }
  1282. s->b_bidir_forw_mv_table[xy][0]= motion_fx;
  1283. s->b_bidir_forw_mv_table[xy][1]= motion_fy;
  1284. s->b_bidir_back_mv_table[xy][0]= motion_bx;
  1285. s->b_bidir_back_mv_table[xy][1]= motion_by;
  1286. return fbmin;
  1287. }
  1288. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1289. {
  1290. MotionEstContext * const c= &s->me;
  1291. int P[10][2];
  1292. const int mot_stride = s->mb_stride;
  1293. const int mot_xy = mb_y*mot_stride + mb_x;
  1294. const int shift= 1+s->quarter_sample;
  1295. int dmin, i;
  1296. const int time_pp= s->pp_time;
  1297. const int time_pb= s->pb_time;
  1298. int mx, my, xmin, xmax, ymin, ymax;
  1299. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1300. c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
  1301. ymin= xmin=(-32)>>shift;
  1302. ymax= xmax= 31>>shift;
  1303. if(IS_8X8(s->next_picture.mb_type[mot_xy])){
  1304. s->mv_type= MV_TYPE_8X8;
  1305. }else{
  1306. s->mv_type= MV_TYPE_16X16;
  1307. }
  1308. for(i=0; i<4; i++){
  1309. int index= s->block_index[i];
  1310. int min, max;
  1311. c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
  1312. c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
  1313. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1314. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1315. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1316. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1317. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1318. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1319. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1320. min+= 16*mb_x - 1;
  1321. xmax= FFMIN(xmax, s->width - max);
  1322. xmin= FFMAX(xmin, - 16 - min);
  1323. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1324. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1325. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1326. min+= 16*mb_y - 1;
  1327. ymax= FFMIN(ymax, s->height - max);
  1328. ymin= FFMAX(ymin, - 16 - min);
  1329. if(s->mv_type == MV_TYPE_16X16) break;
  1330. }
  1331. assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1332. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1333. s->b_direct_mv_table[mot_xy][0]= 0;
  1334. s->b_direct_mv_table[mot_xy][1]= 0;
  1335. return 256*256*256*64;
  1336. }
  1337. c->xmin= xmin;
  1338. c->ymin= ymin;
  1339. c->xmax= xmax;
  1340. c->ymax= ymax;
  1341. c->flags |= FLAG_DIRECT;
  1342. c->sub_flags |= FLAG_DIRECT;
  1343. c->pred_x=0;
  1344. c->pred_y=0;
  1345. P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1346. P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1347. /* special case for first line */
  1348. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
  1349. P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1350. P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1351. P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1352. P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1353. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1354. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1355. }
  1356. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1357. if(c->sub_flags&FLAG_QPEL)
  1358. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1359. else
  1360. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1361. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1362. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1363. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1364. mv_table[mot_xy][0]= mx;
  1365. mv_table[mot_xy][1]= my;
  1366. c->flags &= ~FLAG_DIRECT;
  1367. c->sub_flags &= ~FLAG_DIRECT;
  1368. return dmin;
  1369. }
  1370. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1371. int mb_x, int mb_y)
  1372. {
  1373. MotionEstContext * const c= &s->me;
  1374. const int penalty_factor= c->mb_penalty_factor;
  1375. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1376. int type=0;
  1377. const int xy = mb_y*s->mb_stride + mb_x;
  1378. init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
  1379. get_limits(s, 16*mb_x, 16*mb_y);
  1380. c->skip=0;
  1381. if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){
  1382. int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
  1383. score= ((unsigned)(score*score + 128*256))>>16;
  1384. c->mc_mb_var_sum_temp += score;
  1385. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1386. s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
  1387. return;
  1388. }
  1389. if(c->avctx->me_threshold){
  1390. int vard= check_input_motion(s, mb_x, mb_y, 0);
  1391. if((vard+128)>>8 < c->avctx->me_threshold){
  1392. // pix = c->src[0][0];
  1393. // sum = s->dsp.pix_sum(pix, s->linesize);
  1394. // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  1395. // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  1396. s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1397. /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1398. c->mb_var_sum_temp += (varc+128)>>8;*/
  1399. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1400. /* if (vard <= 64<<8 || vard < varc) {
  1401. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1402. }else{
  1403. c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
  1404. }*/
  1405. return;
  1406. }
  1407. if((vard+128)>>8 < c->avctx->mb_threshold){
  1408. type= s->mb_type[mb_y*s->mb_stride + mb_x];
  1409. if(type == CANDIDATE_MB_TYPE_DIRECT){
  1410. direct_search(s, mb_x, mb_y);
  1411. }
  1412. if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1413. c->skip=0;
  1414. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
  1415. }
  1416. if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1417. c->skip=0;
  1418. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
  1419. }
  1420. if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1421. c->skip=0;
  1422. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1423. interlaced_search(s, 0,
  1424. s->b_field_mv_table[0], s->b_field_select_table[0],
  1425. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
  1426. }
  1427. if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1428. c->skip=0;
  1429. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1430. interlaced_search(s, 2,
  1431. s->b_field_mv_table[1], s->b_field_select_table[1],
  1432. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
  1433. }
  1434. return;
  1435. }
  1436. }
  1437. if (s->codec_id == CODEC_ID_MPEG4)
  1438. dmin= direct_search(s, mb_x, mb_y);
  1439. else
  1440. dmin= INT_MAX;
  1441. //FIXME penalty stuff for non mpeg4
  1442. c->skip=0;
  1443. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
  1444. c->skip=0;
  1445. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
  1446. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1447. c->skip=0;
  1448. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1449. //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1450. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1451. //FIXME mb type penalty
  1452. c->skip=0;
  1453. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1454. fimin= interlaced_search(s, 0,
  1455. s->b_field_mv_table[0], s->b_field_select_table[0],
  1456. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1457. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1458. bimin= interlaced_search(s, 2,
  1459. s->b_field_mv_table[1], s->b_field_select_table[1],
  1460. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1461. }else
  1462. fimin= bimin= INT_MAX;
  1463. {
  1464. int score= fmin;
  1465. type = CANDIDATE_MB_TYPE_FORWARD;
  1466. if (dmin <= score){
  1467. score = dmin;
  1468. type = CANDIDATE_MB_TYPE_DIRECT;
  1469. }
  1470. if(bmin<score){
  1471. score=bmin;
  1472. type= CANDIDATE_MB_TYPE_BACKWARD;
  1473. }
  1474. if(fbmin<score){
  1475. score=fbmin;
  1476. type= CANDIDATE_MB_TYPE_BIDIR;
  1477. }
  1478. if(fimin<score){
  1479. score=fimin;
  1480. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1481. }
  1482. if(bimin<score){
  1483. score=bimin;
  1484. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1485. }
  1486. score= ((unsigned)(score*score + 128*256))>>16;
  1487. c->mc_mb_var_sum_temp += score;
  1488. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1489. }
  1490. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1491. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1492. if(fimin < INT_MAX)
  1493. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1494. if(bimin < INT_MAX)
  1495. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1496. if(fimin < INT_MAX && bimin < INT_MAX){
  1497. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1498. }
  1499. //FIXME something smarter
  1500. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
  1501. 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])
  1502. type |= CANDIDATE_MB_TYPE_DIRECT0;
  1503. #if 0
  1504. if(s->out_format == FMT_MPEG1)
  1505. type |= CANDIDATE_MB_TYPE_INTRA;
  1506. #endif
  1507. }
  1508. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1509. }
  1510. /* find best f_code for ME which do unlimited searches */
  1511. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1512. {
  1513. if(s->me_method>=ME_EPZS){
  1514. int score[8];
  1515. int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
  1516. uint8_t * fcode_tab= s->fcode_tab;
  1517. int best_fcode=-1;
  1518. int best_score=-10000000;
  1519. if(s->msmpeg4_version)
  1520. range= FFMIN(range, 16);
  1521. else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
  1522. range= FFMIN(range, 256);
  1523. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1524. for(y=0; y<s->mb_height; y++){
  1525. int x;
  1526. int xy= y*s->mb_stride;
  1527. for(x=0; x<s->mb_width; x++){
  1528. if(s->mb_type[xy] & type){
  1529. int mx= mv_table[xy][0];
  1530. int my= mv_table[xy][1];
  1531. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1532. fcode_tab[my + MAX_MV]);
  1533. int j;
  1534. if(mx >= range || mx < -range ||
  1535. my >= range || my < -range)
  1536. continue;
  1537. for(j=0; j<fcode && j<8; j++){
  1538. if(s->pict_type==FF_B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1539. score[j]-= 170;
  1540. }
  1541. }
  1542. xy++;
  1543. }
  1544. }
  1545. for(i=1; i<8; i++){
  1546. if(score[i] > best_score){
  1547. best_score= score[i];
  1548. best_fcode= i;
  1549. }
  1550. // printf("%d %d\n", i, score[i]);
  1551. }
  1552. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1553. return best_fcode;
  1554. /* for(i=0; i<=MAX_FCODE; i++){
  1555. printf("%d ", mv_num[i]);
  1556. }
  1557. printf("\n");*/
  1558. }else{
  1559. return 1;
  1560. }
  1561. }
  1562. void ff_fix_long_p_mvs(MpegEncContext * s)
  1563. {
  1564. MotionEstContext * const c= &s->me;
  1565. const int f_code= s->f_code;
  1566. int y, range;
  1567. assert(s->pict_type==FF_P_TYPE);
  1568. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1569. assert(range <= 16 || !s->msmpeg4_version);
  1570. assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
  1571. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1572. //printf("%d no:%d %d//\n", clip, noclip, f_code);
  1573. if(s->flags&CODEC_FLAG_4MV){
  1574. const int wrap= s->b8_stride;
  1575. /* clip / convert to intra 8x8 type MVs */
  1576. for(y=0; y<s->mb_height; y++){
  1577. int xy= y*2*wrap;
  1578. int i= y*s->mb_stride;
  1579. int x;
  1580. for(x=0; x<s->mb_width; x++){
  1581. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1582. int block;
  1583. for(block=0; block<4; block++){
  1584. int off= (block& 1) + (block>>1)*wrap;
  1585. int mx= s->current_picture.motion_val[0][ xy + off ][0];
  1586. int my= s->current_picture.motion_val[0][ xy + off ][1];
  1587. if( mx >=range || mx <-range
  1588. || my >=range || my <-range){
  1589. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1590. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1591. s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  1592. }
  1593. }
  1594. }
  1595. xy+=2;
  1596. i++;
  1597. }
  1598. }
  1599. }
  1600. }
  1601. /**
  1602. *
  1603. * @param truncate 1 for truncation, 0 for using intra
  1604. */
  1605. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1606. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1607. {
  1608. MotionEstContext * const c= &s->me;
  1609. int y, h_range, v_range;
  1610. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1611. int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1612. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1613. h_range= range;
  1614. v_range= field_select_table ? range>>1 : range;
  1615. /* clip / convert to intra 16x16 type MVs */
  1616. for(y=0; y<s->mb_height; y++){
  1617. int x;
  1618. int xy= y*s->mb_stride;
  1619. for(x=0; x<s->mb_width; x++){
  1620. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1621. if(field_select_table==NULL || field_select_table[xy] == field_select){
  1622. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1623. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1624. if(truncate){
  1625. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1626. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1627. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1628. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1629. }else{
  1630. s->mb_type[xy] &= ~type;
  1631. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1632. mv_table[xy][0]=
  1633. mv_table[xy][1]= 0;
  1634. }
  1635. }
  1636. }
  1637. }
  1638. xy++;
  1639. }
  1640. }
  1641. }