vf_minterpolate.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /**
  2. * Copyright (c) 2014-2015 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2016 Davinder Singh (DSM_) <ds.mudhar<@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "motion_estimation.h"
  22. #include "libavcodec/mathops.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/motion_vector.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #include "scene_sad.h"
  33. #define ME_MODE_BIDIR 0
  34. #define ME_MODE_BILAT 1
  35. #define MC_MODE_OBMC 0
  36. #define MC_MODE_AOBMC 1
  37. #define SCD_METHOD_NONE 0
  38. #define SCD_METHOD_FDIFF 1
  39. #define NB_FRAMES 4
  40. #define NB_PIXEL_MVS 32
  41. #define NB_CLUSTERS 128
  42. #define ALPHA_MAX 1024
  43. #define CLUSTER_THRESHOLD 4
  44. #define PX_WEIGHT_MAX 255
  45. #define COST_PRED_SCALE 64
  46. static const uint8_t obmc_linear32[1024] = {
  47. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  48. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  49. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  50. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  51. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  52. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  53. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  54. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  55. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  56. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  57. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  58. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  59. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  60. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  61. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  62. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  63. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  64. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  65. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  66. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  67. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  68. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  69. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  70. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  71. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  72. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  73. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  74. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  75. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  76. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  77. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  78. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  79. };
  80. static const uint8_t obmc_linear16[256] = {
  81. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  82. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  83. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  84. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  85. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  86. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  87. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  88. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  89. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  90. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  91. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  92. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  93. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  94. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  95. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  96. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  97. };
  98. static const uint8_t obmc_linear8[64] = {
  99. 4, 12, 20, 28, 28, 20, 12, 4,
  100. 12, 36, 60, 84, 84, 60, 36, 12,
  101. 20, 60,100,140,140,100, 60, 20,
  102. 28, 84,140,196,196,140, 84, 28,
  103. 28, 84,140,196,196,140, 84, 28,
  104. 20, 60,100,140,140,100, 60, 20,
  105. 12, 36, 60, 84, 84, 60, 36, 12,
  106. 4, 12, 20, 28, 28, 20, 12, 4,
  107. };
  108. static const uint8_t obmc_linear4[16] = {
  109. 16, 48, 48, 16,
  110. 48,144,144, 48,
  111. 48,144,144, 48,
  112. 16, 48, 48, 16,
  113. };
  114. static const uint8_t * const obmc_tab_linear[4]= {
  115. obmc_linear32, obmc_linear16, obmc_linear8, obmc_linear4
  116. };
  117. enum MIMode {
  118. MI_MODE_DUP = 0,
  119. MI_MODE_BLEND = 1,
  120. MI_MODE_MCI = 2,
  121. };
  122. typedef struct Cluster {
  123. int64_t sum[2];
  124. int nb;
  125. } Cluster;
  126. typedef struct Block {
  127. int16_t mvs[2][2];
  128. int cid;
  129. uint64_t sbad;
  130. int sb;
  131. struct Block *subs;
  132. } Block;
  133. typedef struct PixelMVS {
  134. int16_t mvs[NB_PIXEL_MVS][2];
  135. } PixelMVS;
  136. typedef struct PixelWeights {
  137. uint32_t weights[NB_PIXEL_MVS];
  138. } PixelWeights;
  139. typedef struct PixelRefs {
  140. int8_t refs[NB_PIXEL_MVS];
  141. int nb;
  142. } PixelRefs;
  143. typedef struct Frame {
  144. AVFrame *avf;
  145. Block *blocks;
  146. } Frame;
  147. typedef struct MIContext {
  148. const AVClass *class;
  149. AVMotionEstContext me_ctx;
  150. AVRational frame_rate;
  151. enum MIMode mi_mode;
  152. int mc_mode;
  153. int me_mode;
  154. int me_method;
  155. int mb_size;
  156. int search_param;
  157. int vsbmc;
  158. Frame frames[NB_FRAMES];
  159. Cluster clusters[NB_CLUSTERS];
  160. Block *int_blocks;
  161. PixelMVS *pixel_mvs;
  162. PixelWeights *pixel_weights;
  163. PixelRefs *pixel_refs;
  164. int (*mv_table[3])[2][2];
  165. int64_t out_pts;
  166. int b_width, b_height, b_count;
  167. int log2_mb_size;
  168. int scd_method;
  169. int scene_changed;
  170. ff_scene_sad_fn sad;
  171. double prev_mafd;
  172. double scd_threshold;
  173. int log2_chroma_w;
  174. int log2_chroma_h;
  175. int nb_planes;
  176. } MIContext;
  177. #define OFFSET(x) offsetof(MIContext, x)
  178. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  179. #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
  180. static const AVOption minterpolate_options[] = {
  181. { "fps", "output's frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "60"}, 0, INT_MAX, FLAGS },
  182. { "mi_mode", "motion interpolation mode", OFFSET(mi_mode), AV_OPT_TYPE_INT, {.i64 = MI_MODE_MCI}, MI_MODE_DUP, MI_MODE_MCI, FLAGS, "mi_mode" },
  183. CONST("dup", "duplicate frames", MI_MODE_DUP, "mi_mode"),
  184. CONST("blend", "blend frames", MI_MODE_BLEND, "mi_mode"),
  185. CONST("mci", "motion compensated interpolation", MI_MODE_MCI, "mi_mode"),
  186. { "mc_mode", "motion compensation mode", OFFSET(mc_mode), AV_OPT_TYPE_INT, {.i64 = MC_MODE_OBMC}, MC_MODE_OBMC, MC_MODE_AOBMC, FLAGS, "mc_mode" },
  187. CONST("obmc", "overlapped block motion compensation", MC_MODE_OBMC, "mc_mode"),
  188. CONST("aobmc", "adaptive overlapped block motion compensation", MC_MODE_AOBMC, "mc_mode"),
  189. { "me_mode", "motion estimation mode", OFFSET(me_mode), AV_OPT_TYPE_INT, {.i64 = ME_MODE_BILAT}, ME_MODE_BIDIR, ME_MODE_BILAT, FLAGS, "me_mode" },
  190. CONST("bidir", "bidirectional motion estimation", ME_MODE_BIDIR, "me_mode"),
  191. CONST("bilat", "bilateral motion estimation", ME_MODE_BILAT, "me_mode"),
  192. { "me", "motion estimation method", OFFSET(me_method), AV_OPT_TYPE_INT, {.i64 = AV_ME_METHOD_EPZS}, AV_ME_METHOD_ESA, AV_ME_METHOD_UMH, FLAGS, "me" },
  193. CONST("esa", "exhaustive search", AV_ME_METHOD_ESA, "me"),
  194. CONST("tss", "three step search", AV_ME_METHOD_TSS, "me"),
  195. CONST("tdls", "two dimensional logarithmic search", AV_ME_METHOD_TDLS, "me"),
  196. CONST("ntss", "new three step search", AV_ME_METHOD_NTSS, "me"),
  197. CONST("fss", "four step search", AV_ME_METHOD_FSS, "me"),
  198. CONST("ds", "diamond search", AV_ME_METHOD_DS, "me"),
  199. CONST("hexbs", "hexagon-based search", AV_ME_METHOD_HEXBS, "me"),
  200. CONST("epzs", "enhanced predictive zonal search", AV_ME_METHOD_EPZS, "me"),
  201. CONST("umh", "uneven multi-hexagon search", AV_ME_METHOD_UMH, "me"),
  202. { "mb_size", "macroblock size", OFFSET(mb_size), AV_OPT_TYPE_INT, {.i64 = 16}, 4, 16, FLAGS },
  203. { "search_param", "search parameter", OFFSET(search_param), AV_OPT_TYPE_INT, {.i64 = 32}, 4, INT_MAX, FLAGS },
  204. { "vsbmc", "variable-size block motion compensation", OFFSET(vsbmc), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  205. { "scd", "scene change detection method", OFFSET(scd_method), AV_OPT_TYPE_INT, {.i64 = SCD_METHOD_FDIFF}, SCD_METHOD_NONE, SCD_METHOD_FDIFF, FLAGS, "scene" },
  206. CONST("none", "disable detection", SCD_METHOD_NONE, "scene"),
  207. CONST("fdiff", "frame difference", SCD_METHOD_FDIFF, "scene"),
  208. { "scd_threshold", "scene change threshold", OFFSET(scd_threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0, 100.0, FLAGS },
  209. { NULL }
  210. };
  211. AVFILTER_DEFINE_CLASS(minterpolate);
  212. static int query_formats(AVFilterContext *ctx)
  213. {
  214. static const enum AVPixelFormat pix_fmts[] = {
  215. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  216. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  217. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  218. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  219. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  220. AV_PIX_FMT_YUVJ411P,
  221. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  222. AV_PIX_FMT_GRAY8,
  223. AV_PIX_FMT_NONE
  224. };
  225. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  226. if (!fmts_list)
  227. return AVERROR(ENOMEM);
  228. return ff_set_common_formats(ctx, fmts_list);
  229. }
  230. static uint64_t get_sbad(AVMotionEstContext *me_ctx, int x, int y, int x_mv, int y_mv)
  231. {
  232. uint8_t *data_cur = me_ctx->data_cur;
  233. uint8_t *data_next = me_ctx->data_ref;
  234. int linesize = me_ctx->linesize;
  235. int mv_x1 = x_mv - x;
  236. int mv_y1 = y_mv - y;
  237. int mv_x, mv_y, i, j;
  238. uint64_t sbad = 0;
  239. x = av_clip(x, me_ctx->x_min, me_ctx->x_max);
  240. y = av_clip(y, me_ctx->y_min, me_ctx->y_max);
  241. mv_x = av_clip(x_mv - x, -FFMIN(x - me_ctx->x_min, me_ctx->x_max - x), FFMIN(x - me_ctx->x_min, me_ctx->x_max - x));
  242. mv_y = av_clip(y_mv - y, -FFMIN(y - me_ctx->y_min, me_ctx->y_max - y), FFMIN(y - me_ctx->y_min, me_ctx->y_max - y));
  243. data_cur += (y + mv_y) * linesize;
  244. data_next += (y - mv_y) * linesize;
  245. for (j = 0; j < me_ctx->mb_size; j++)
  246. for (i = 0; i < me_ctx->mb_size; i++)
  247. sbad += FFABS(data_cur[x + mv_x + i + j * linesize] - data_next[x - mv_x + i + j * linesize]);
  248. return sbad + (FFABS(mv_x1 - me_ctx->pred_x) + FFABS(mv_y1 - me_ctx->pred_y)) * COST_PRED_SCALE;
  249. }
  250. static uint64_t get_sbad_ob(AVMotionEstContext *me_ctx, int x, int y, int x_mv, int y_mv)
  251. {
  252. uint8_t *data_cur = me_ctx->data_cur;
  253. uint8_t *data_next = me_ctx->data_ref;
  254. int linesize = me_ctx->linesize;
  255. int x_min = me_ctx->x_min + me_ctx->mb_size / 2;
  256. int x_max = me_ctx->x_max - me_ctx->mb_size / 2;
  257. int y_min = me_ctx->y_min + me_ctx->mb_size / 2;
  258. int y_max = me_ctx->y_max - me_ctx->mb_size / 2;
  259. int mv_x1 = x_mv - x;
  260. int mv_y1 = y_mv - y;
  261. int mv_x, mv_y, i, j;
  262. uint64_t sbad = 0;
  263. x = av_clip(x, x_min, x_max);
  264. y = av_clip(y, y_min, y_max);
  265. mv_x = av_clip(x_mv - x, -FFMIN(x - x_min, x_max - x), FFMIN(x - x_min, x_max - x));
  266. mv_y = av_clip(y_mv - y, -FFMIN(y - y_min, y_max - y), FFMIN(y - y_min, y_max - y));
  267. for (j = -me_ctx->mb_size / 2; j < me_ctx->mb_size * 3 / 2; j++)
  268. for (i = -me_ctx->mb_size / 2; i < me_ctx->mb_size * 3 / 2; i++)
  269. sbad += FFABS(data_cur[x + mv_x + i + (y + mv_y + j) * linesize] - data_next[x - mv_x + i + (y - mv_y + j) * linesize]);
  270. return sbad + (FFABS(mv_x1 - me_ctx->pred_x) + FFABS(mv_y1 - me_ctx->pred_y)) * COST_PRED_SCALE;
  271. }
  272. static uint64_t get_sad_ob(AVMotionEstContext *me_ctx, int x, int y, int x_mv, int y_mv)
  273. {
  274. uint8_t *data_ref = me_ctx->data_ref;
  275. uint8_t *data_cur = me_ctx->data_cur;
  276. int linesize = me_ctx->linesize;
  277. int x_min = me_ctx->x_min + me_ctx->mb_size / 2;
  278. int x_max = me_ctx->x_max - me_ctx->mb_size / 2;
  279. int y_min = me_ctx->y_min + me_ctx->mb_size / 2;
  280. int y_max = me_ctx->y_max - me_ctx->mb_size / 2;
  281. int mv_x = x_mv - x;
  282. int mv_y = y_mv - y;
  283. int i, j;
  284. uint64_t sad = 0;
  285. x = av_clip(x, x_min, x_max);
  286. y = av_clip(y, y_min, y_max);
  287. x_mv = av_clip(x_mv, x_min, x_max);
  288. y_mv = av_clip(y_mv, y_min, y_max);
  289. for (j = -me_ctx->mb_size / 2; j < me_ctx->mb_size * 3 / 2; j++)
  290. for (i = -me_ctx->mb_size / 2; i < me_ctx->mb_size * 3 / 2; i++)
  291. sad += FFABS(data_ref[x_mv + i + (y_mv + j) * linesize] - data_cur[x + i + (y + j) * linesize]);
  292. return sad + (FFABS(mv_x - me_ctx->pred_x) + FFABS(mv_y - me_ctx->pred_y)) * COST_PRED_SCALE;
  293. }
  294. static int config_input(AVFilterLink *inlink)
  295. {
  296. MIContext *mi_ctx = inlink->dst->priv;
  297. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  298. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  299. const int height = inlink->h;
  300. const int width = inlink->w;
  301. int i, ret = 0;
  302. mi_ctx->log2_chroma_h = desc->log2_chroma_h;
  303. mi_ctx->log2_chroma_w = desc->log2_chroma_w;
  304. mi_ctx->nb_planes = av_pix_fmt_count_planes(inlink->format);
  305. mi_ctx->log2_mb_size = av_ceil_log2_c(mi_ctx->mb_size);
  306. mi_ctx->mb_size = 1 << mi_ctx->log2_mb_size;
  307. mi_ctx->b_width = width >> mi_ctx->log2_mb_size;
  308. mi_ctx->b_height = height >> mi_ctx->log2_mb_size;
  309. mi_ctx->b_count = mi_ctx->b_width * mi_ctx->b_height;
  310. for (i = 0; i < NB_FRAMES; i++) {
  311. Frame *frame = &mi_ctx->frames[i];
  312. frame->blocks = av_mallocz_array(mi_ctx->b_count, sizeof(Block));
  313. if (!frame->blocks)
  314. return AVERROR(ENOMEM);
  315. }
  316. if (mi_ctx->mi_mode == MI_MODE_MCI) {
  317. mi_ctx->pixel_mvs = av_mallocz_array(width * height, sizeof(PixelMVS));
  318. mi_ctx->pixel_weights = av_mallocz_array(width * height, sizeof(PixelWeights));
  319. mi_ctx->pixel_refs = av_mallocz_array(width * height, sizeof(PixelRefs));
  320. if (!mi_ctx->pixel_mvs || !mi_ctx->pixel_weights || !mi_ctx->pixel_refs) {
  321. ret = AVERROR(ENOMEM);
  322. goto fail;
  323. }
  324. if (mi_ctx->me_mode == ME_MODE_BILAT)
  325. if (!(mi_ctx->int_blocks = av_mallocz_array(mi_ctx->b_count, sizeof(Block))))
  326. return AVERROR(ENOMEM);
  327. if (mi_ctx->me_method == AV_ME_METHOD_EPZS) {
  328. for (i = 0; i < 3; i++) {
  329. mi_ctx->mv_table[i] = av_mallocz_array(mi_ctx->b_count, sizeof(*mi_ctx->mv_table[0]));
  330. if (!mi_ctx->mv_table[i])
  331. return AVERROR(ENOMEM);
  332. }
  333. }
  334. }
  335. if (mi_ctx->scd_method == SCD_METHOD_FDIFF) {
  336. mi_ctx->sad = ff_scene_sad_get_fn(8);
  337. if (!mi_ctx->sad)
  338. return AVERROR(EINVAL);
  339. }
  340. ff_me_init_context(me_ctx, mi_ctx->mb_size, mi_ctx->search_param, width, height, 0, (mi_ctx->b_width - 1) << mi_ctx->log2_mb_size, 0, (mi_ctx->b_height - 1) << mi_ctx->log2_mb_size);
  341. if (mi_ctx->me_mode == ME_MODE_BIDIR)
  342. me_ctx->get_cost = &get_sad_ob;
  343. else if (mi_ctx->me_mode == ME_MODE_BILAT)
  344. me_ctx->get_cost = &get_sbad_ob;
  345. return 0;
  346. fail:
  347. for (i = 0; i < NB_FRAMES; i++)
  348. av_freep(&mi_ctx->frames[i].blocks);
  349. av_freep(&mi_ctx->pixel_mvs);
  350. av_freep(&mi_ctx->pixel_weights);
  351. av_freep(&mi_ctx->pixel_refs);
  352. return ret;
  353. }
  354. static int config_output(AVFilterLink *outlink)
  355. {
  356. MIContext *mi_ctx = outlink->src->priv;
  357. outlink->frame_rate = mi_ctx->frame_rate;
  358. outlink->time_base = av_inv_q(mi_ctx->frame_rate);
  359. return 0;
  360. }
  361. #define ADD_PRED(preds, px, py)\
  362. do {\
  363. preds.mvs[preds.nb][0] = px;\
  364. preds.mvs[preds.nb][1] = py;\
  365. preds.nb++;\
  366. } while(0)
  367. static void search_mv(MIContext *mi_ctx, Block *blocks, int mb_x, int mb_y, int dir)
  368. {
  369. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  370. AVMotionEstPredictor *preds = me_ctx->preds;
  371. Block *block = &blocks[mb_x + mb_y * mi_ctx->b_width];
  372. const int x_mb = mb_x << mi_ctx->log2_mb_size;
  373. const int y_mb = mb_y << mi_ctx->log2_mb_size;
  374. const int mb_i = mb_x + mb_y * mi_ctx->b_width;
  375. int mv[2] = {x_mb, y_mb};
  376. switch (mi_ctx->me_method) {
  377. case AV_ME_METHOD_ESA:
  378. ff_me_search_esa(me_ctx, x_mb, y_mb, mv);
  379. break;
  380. case AV_ME_METHOD_TSS:
  381. ff_me_search_tss(me_ctx, x_mb, y_mb, mv);
  382. break;
  383. case AV_ME_METHOD_TDLS:
  384. ff_me_search_tdls(me_ctx, x_mb, y_mb, mv);
  385. break;
  386. case AV_ME_METHOD_NTSS:
  387. ff_me_search_ntss(me_ctx, x_mb, y_mb, mv);
  388. break;
  389. case AV_ME_METHOD_FSS:
  390. ff_me_search_fss(me_ctx, x_mb, y_mb, mv);
  391. break;
  392. case AV_ME_METHOD_DS:
  393. ff_me_search_ds(me_ctx, x_mb, y_mb, mv);
  394. break;
  395. case AV_ME_METHOD_HEXBS:
  396. ff_me_search_hexbs(me_ctx, x_mb, y_mb, mv);
  397. break;
  398. case AV_ME_METHOD_EPZS:
  399. preds[0].nb = 0;
  400. preds[1].nb = 0;
  401. ADD_PRED(preds[0], 0, 0);
  402. //left mb in current frame
  403. if (mb_x > 0)
  404. ADD_PRED(preds[0], mi_ctx->mv_table[0][mb_i - 1][dir][0], mi_ctx->mv_table[0][mb_i - 1][dir][1]);
  405. //top mb in current frame
  406. if (mb_y > 0)
  407. ADD_PRED(preds[0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width][dir][0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width][dir][1]);
  408. //top-right mb in current frame
  409. if (mb_y > 0 && mb_x + 1 < mi_ctx->b_width)
  410. ADD_PRED(preds[0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width + 1][dir][0], mi_ctx->mv_table[0][mb_i - mi_ctx->b_width + 1][dir][1]);
  411. //median predictor
  412. if (preds[0].nb == 4) {
  413. me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
  414. me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
  415. } else if (preds[0].nb == 3) {
  416. me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
  417. me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
  418. } else if (preds[0].nb == 2) {
  419. me_ctx->pred_x = preds[0].mvs[1][0];
  420. me_ctx->pred_y = preds[0].mvs[1][1];
  421. } else {
  422. me_ctx->pred_x = 0;
  423. me_ctx->pred_y = 0;
  424. }
  425. //collocated mb in prev frame
  426. ADD_PRED(preds[0], mi_ctx->mv_table[1][mb_i][dir][0], mi_ctx->mv_table[1][mb_i][dir][1]);
  427. //accelerator motion vector of collocated block in prev frame
  428. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i][dir][0] + (mi_ctx->mv_table[1][mb_i][dir][0] - mi_ctx->mv_table[2][mb_i][dir][0]),
  429. mi_ctx->mv_table[1][mb_i][dir][1] + (mi_ctx->mv_table[1][mb_i][dir][1] - mi_ctx->mv_table[2][mb_i][dir][1]));
  430. //left mb in prev frame
  431. if (mb_x > 0)
  432. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i - 1][dir][0], mi_ctx->mv_table[1][mb_i - 1][dir][1]);
  433. //top mb in prev frame
  434. if (mb_y > 0)
  435. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i - mi_ctx->b_width][dir][0], mi_ctx->mv_table[1][mb_i - mi_ctx->b_width][dir][1]);
  436. //right mb in prev frame
  437. if (mb_x + 1 < mi_ctx->b_width)
  438. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i + 1][dir][0], mi_ctx->mv_table[1][mb_i + 1][dir][1]);
  439. //bottom mb in prev frame
  440. if (mb_y + 1 < mi_ctx->b_height)
  441. ADD_PRED(preds[1], mi_ctx->mv_table[1][mb_i + mi_ctx->b_width][dir][0], mi_ctx->mv_table[1][mb_i + mi_ctx->b_width][dir][1]);
  442. ff_me_search_epzs(me_ctx, x_mb, y_mb, mv);
  443. mi_ctx->mv_table[0][mb_i][dir][0] = mv[0] - x_mb;
  444. mi_ctx->mv_table[0][mb_i][dir][1] = mv[1] - y_mb;
  445. break;
  446. case AV_ME_METHOD_UMH:
  447. preds[0].nb = 0;
  448. ADD_PRED(preds[0], 0, 0);
  449. //left mb in current frame
  450. if (mb_x > 0)
  451. ADD_PRED(preds[0], blocks[mb_i - 1].mvs[dir][0], blocks[mb_i - 1].mvs[dir][1]);
  452. if (mb_y > 0) {
  453. //top mb in current frame
  454. ADD_PRED(preds[0], blocks[mb_i - mi_ctx->b_width].mvs[dir][0], blocks[mb_i - mi_ctx->b_width].mvs[dir][1]);
  455. //top-right mb in current frame
  456. if (mb_x + 1 < mi_ctx->b_width)
  457. ADD_PRED(preds[0], blocks[mb_i - mi_ctx->b_width + 1].mvs[dir][0], blocks[mb_i - mi_ctx->b_width + 1].mvs[dir][1]);
  458. //top-left mb in current frame
  459. else if (mb_x > 0)
  460. ADD_PRED(preds[0], blocks[mb_i - mi_ctx->b_width - 1].mvs[dir][0], blocks[mb_i - mi_ctx->b_width - 1].mvs[dir][1]);
  461. }
  462. //median predictor
  463. if (preds[0].nb == 4) {
  464. me_ctx->pred_x = mid_pred(preds[0].mvs[1][0], preds[0].mvs[2][0], preds[0].mvs[3][0]);
  465. me_ctx->pred_y = mid_pred(preds[0].mvs[1][1], preds[0].mvs[2][1], preds[0].mvs[3][1]);
  466. } else if (preds[0].nb == 3) {
  467. me_ctx->pred_x = mid_pred(0, preds[0].mvs[1][0], preds[0].mvs[2][0]);
  468. me_ctx->pred_y = mid_pred(0, preds[0].mvs[1][1], preds[0].mvs[2][1]);
  469. } else if (preds[0].nb == 2) {
  470. me_ctx->pred_x = preds[0].mvs[1][0];
  471. me_ctx->pred_y = preds[0].mvs[1][1];
  472. } else {
  473. me_ctx->pred_x = 0;
  474. me_ctx->pred_y = 0;
  475. }
  476. ff_me_search_umh(me_ctx, x_mb, y_mb, mv);
  477. break;
  478. }
  479. block->mvs[dir][0] = mv[0] - x_mb;
  480. block->mvs[dir][1] = mv[1] - y_mb;
  481. }
  482. static void bilateral_me(MIContext *mi_ctx)
  483. {
  484. Block *block;
  485. int mb_x, mb_y;
  486. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  487. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  488. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  489. block->cid = 0;
  490. block->sb = 0;
  491. block->mvs[0][0] = 0;
  492. block->mvs[0][1] = 0;
  493. }
  494. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  495. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++)
  496. search_mv(mi_ctx, mi_ctx->int_blocks, mb_x, mb_y, 0);
  497. }
  498. static int var_size_bme(MIContext *mi_ctx, Block *block, int x_mb, int y_mb, int n)
  499. {
  500. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  501. uint64_t cost_sb, cost_old;
  502. int mb_size = me_ctx->mb_size;
  503. int search_param = me_ctx->search_param;
  504. int mv_x, mv_y;
  505. int x, y;
  506. int ret;
  507. me_ctx->mb_size = 1 << n;
  508. cost_old = me_ctx->get_cost(me_ctx, x_mb, y_mb, x_mb + block->mvs[0][0], y_mb + block->mvs[0][1]);
  509. me_ctx->mb_size = mb_size;
  510. if (!cost_old) {
  511. block->sb = 0;
  512. return 0;
  513. }
  514. if (!block->subs) {
  515. block->subs = av_mallocz_array(4, sizeof(Block));
  516. if (!block->subs)
  517. return AVERROR(ENOMEM);
  518. }
  519. block->sb = 1;
  520. for (y = 0; y < 2; y++)
  521. for (x = 0; x < 2; x++) {
  522. Block *sb = &block->subs[x + y * 2];
  523. int mv[2] = {x_mb + block->mvs[0][0], y_mb + block->mvs[0][1]};
  524. me_ctx->mb_size = 1 << (n - 1);
  525. me_ctx->search_param = 2;
  526. me_ctx->pred_x = block->mvs[0][0];
  527. me_ctx->pred_y = block->mvs[0][1];
  528. cost_sb = ff_me_search_ds(&mi_ctx->me_ctx, x_mb + block->mvs[0][0], y_mb + block->mvs[0][1], mv);
  529. mv_x = mv[0] - x_mb;
  530. mv_y = mv[1] - y_mb;
  531. me_ctx->mb_size = mb_size;
  532. me_ctx->search_param = search_param;
  533. if (cost_sb < cost_old / 4) {
  534. sb->mvs[0][0] = mv_x;
  535. sb->mvs[0][1] = mv_y;
  536. if (n > 1) {
  537. if (ret = var_size_bme(mi_ctx, sb, x_mb + (x << (n - 1)), y_mb + (y << (n - 1)), n - 1))
  538. return ret;
  539. } else
  540. sb->sb = 0;
  541. } else {
  542. block->sb = 0;
  543. return 0;
  544. }
  545. }
  546. return 0;
  547. }
  548. static int cluster_mvs(MIContext *mi_ctx)
  549. {
  550. int changed, c, c_max = 0;
  551. int mb_x, mb_y, x, y;
  552. int mv_x, mv_y, avg_x, avg_y, dx, dy;
  553. int d, ret;
  554. Block *block;
  555. Cluster *cluster, *cluster_new;
  556. do {
  557. changed = 0;
  558. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  559. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  560. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  561. c = block->cid;
  562. cluster = &mi_ctx->clusters[c];
  563. mv_x = block->mvs[0][0];
  564. mv_y = block->mvs[0][1];
  565. if (cluster->nb < 2)
  566. continue;
  567. avg_x = cluster->sum[0] / cluster->nb;
  568. avg_y = cluster->sum[1] / cluster->nb;
  569. dx = avg_x - mv_x;
  570. dy = avg_y - mv_y;
  571. if (FFABS(dx) > CLUSTER_THRESHOLD || FFABS(dy) > CLUSTER_THRESHOLD) {
  572. for (d = 1; d < 5; d++)
  573. for (y = FFMAX(mb_y - d, 0); y < FFMIN(mb_y + d + 1, mi_ctx->b_height); y++)
  574. for (x = FFMAX(mb_x - d, 0); x < FFMIN(mb_x + d + 1, mi_ctx->b_width); x++) {
  575. Block *nb = &mi_ctx->int_blocks[x + y * mi_ctx->b_width];
  576. if (nb->cid > block->cid) {
  577. if (nb->cid < c || c == block->cid)
  578. c = nb->cid;
  579. }
  580. }
  581. if (c == block->cid)
  582. c = c_max + 1;
  583. if (c >= NB_CLUSTERS) {
  584. continue;
  585. }
  586. cluster_new = &mi_ctx->clusters[c];
  587. cluster_new->sum[0] += mv_x;
  588. cluster_new->sum[1] += mv_y;
  589. cluster->sum[0] -= mv_x;
  590. cluster->sum[1] -= mv_y;
  591. cluster_new->nb++;
  592. cluster->nb--;
  593. c_max = FFMAX(c_max, c);
  594. block->cid = c;
  595. changed = 1;
  596. }
  597. }
  598. } while (changed);
  599. /* find boundaries */
  600. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  601. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  602. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  603. for (y = FFMAX(mb_y - 1, 0); y < FFMIN(mb_y + 2, mi_ctx->b_height); y++)
  604. for (x = FFMAX(mb_x - 1, 0); x < FFMIN(mb_x + 2, mi_ctx->b_width); x++) {
  605. dx = x - mb_x;
  606. dy = y - mb_y;
  607. if ((x - mb_x) && (y - mb_y) || !dx && !dy)
  608. continue;
  609. if (!mb_x || !mb_y || mb_x == mi_ctx->b_width - 1 || mb_y == mi_ctx->b_height - 1)
  610. continue;
  611. if (block->cid != mi_ctx->int_blocks[x + y * mi_ctx->b_width].cid) {
  612. if (!dx && block->cid == mi_ctx->int_blocks[x + (mb_y - dy) * mi_ctx->b_width].cid ||
  613. !dy && block->cid == mi_ctx->int_blocks[(mb_x - dx) + y * mi_ctx->b_width].cid) {
  614. if (ret = var_size_bme(mi_ctx, block, mb_x << mi_ctx->log2_mb_size, mb_y << mi_ctx->log2_mb_size, mi_ctx->log2_mb_size))
  615. return ret;
  616. }
  617. }
  618. }
  619. }
  620. return 0;
  621. }
  622. static int inject_frame(AVFilterLink *inlink, AVFrame *avf_in)
  623. {
  624. AVFilterContext *ctx = inlink->dst;
  625. MIContext *mi_ctx = ctx->priv;
  626. Frame frame_tmp;
  627. int mb_x, mb_y, dir;
  628. av_frame_free(&mi_ctx->frames[0].avf);
  629. frame_tmp = mi_ctx->frames[0];
  630. memmove(&mi_ctx->frames[0], &mi_ctx->frames[1], sizeof(mi_ctx->frames[0]) * (NB_FRAMES - 1));
  631. mi_ctx->frames[NB_FRAMES - 1] = frame_tmp;
  632. mi_ctx->frames[NB_FRAMES - 1].avf = avf_in;
  633. if (mi_ctx->mi_mode == MI_MODE_MCI) {
  634. if (mi_ctx->me_method == AV_ME_METHOD_EPZS) {
  635. mi_ctx->mv_table[2] = memcpy(mi_ctx->mv_table[2], mi_ctx->mv_table[1], sizeof(*mi_ctx->mv_table[1]) * mi_ctx->b_count);
  636. mi_ctx->mv_table[1] = memcpy(mi_ctx->mv_table[1], mi_ctx->mv_table[0], sizeof(*mi_ctx->mv_table[0]) * mi_ctx->b_count);
  637. }
  638. if (mi_ctx->me_mode == ME_MODE_BIDIR) {
  639. if (mi_ctx->frames[1].avf) {
  640. for (dir = 0; dir < 2; dir++) {
  641. mi_ctx->me_ctx.linesize = mi_ctx->frames[2].avf->linesize[0];
  642. mi_ctx->me_ctx.data_cur = mi_ctx->frames[2].avf->data[0];
  643. mi_ctx->me_ctx.data_ref = mi_ctx->frames[dir ? 3 : 1].avf->data[0];
  644. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  645. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++)
  646. search_mv(mi_ctx, mi_ctx->frames[2].blocks, mb_x, mb_y, dir);
  647. }
  648. }
  649. } else if (mi_ctx->me_mode == ME_MODE_BILAT) {
  650. Block *block;
  651. int i, ret;
  652. if (!mi_ctx->frames[0].avf)
  653. return 0;
  654. mi_ctx->me_ctx.linesize = mi_ctx->frames[0].avf->linesize[0];
  655. mi_ctx->me_ctx.data_cur = mi_ctx->frames[1].avf->data[0];
  656. mi_ctx->me_ctx.data_ref = mi_ctx->frames[2].avf->data[0];
  657. bilateral_me(mi_ctx);
  658. if (mi_ctx->mc_mode == MC_MODE_AOBMC) {
  659. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  660. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  661. int x_mb = mb_x << mi_ctx->log2_mb_size;
  662. int y_mb = mb_y << mi_ctx->log2_mb_size;
  663. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  664. block->sbad = get_sbad(&mi_ctx->me_ctx, x_mb, y_mb, x_mb + block->mvs[0][0], y_mb + block->mvs[0][1]);
  665. }
  666. }
  667. if (mi_ctx->vsbmc) {
  668. for (i = 0; i < NB_CLUSTERS; i++) {
  669. mi_ctx->clusters[i].sum[0] = 0;
  670. mi_ctx->clusters[i].sum[1] = 0;
  671. mi_ctx->clusters[i].nb = 0;
  672. }
  673. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  674. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  675. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  676. mi_ctx->clusters[0].sum[0] += block->mvs[0][0];
  677. mi_ctx->clusters[0].sum[1] += block->mvs[0][1];
  678. }
  679. mi_ctx->clusters[0].nb = mi_ctx->b_count;
  680. if (ret = cluster_mvs(mi_ctx))
  681. return ret;
  682. }
  683. }
  684. }
  685. return 0;
  686. }
  687. static int detect_scene_change(MIContext *mi_ctx)
  688. {
  689. AVMotionEstContext *me_ctx = &mi_ctx->me_ctx;
  690. uint8_t *p1 = mi_ctx->frames[1].avf->data[0];
  691. ptrdiff_t linesize1 = mi_ctx->frames[1].avf->linesize[0];
  692. uint8_t *p2 = mi_ctx->frames[2].avf->data[0];
  693. ptrdiff_t linesize2 = mi_ctx->frames[2].avf->linesize[0];
  694. if (mi_ctx->scd_method == SCD_METHOD_FDIFF) {
  695. double ret = 0, mafd, diff;
  696. uint64_t sad;
  697. mi_ctx->sad(p1, linesize1, p2, linesize2, me_ctx->width, me_ctx->height, &sad);
  698. emms_c();
  699. mafd = (double) sad / (me_ctx->height * me_ctx->width * 3);
  700. diff = fabs(mafd - mi_ctx->prev_mafd);
  701. ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
  702. mi_ctx->prev_mafd = mafd;
  703. return ret >= mi_ctx->scd_threshold;
  704. }
  705. return 0;
  706. }
  707. #define ADD_PIXELS(b_weight, mv_x, mv_y)\
  708. do {\
  709. if (!b_weight || pixel_refs->nb + 1 >= NB_PIXEL_MVS)\
  710. continue;\
  711. pixel_refs->refs[pixel_refs->nb] = 1;\
  712. pixel_weights->weights[pixel_refs->nb] = b_weight * (ALPHA_MAX - alpha);\
  713. pixel_mvs->mvs[pixel_refs->nb][0] = av_clip((mv_x * alpha) / ALPHA_MAX, x_min, x_max);\
  714. pixel_mvs->mvs[pixel_refs->nb][1] = av_clip((mv_y * alpha) / ALPHA_MAX, y_min, y_max);\
  715. pixel_refs->nb++;\
  716. pixel_refs->refs[pixel_refs->nb] = 2;\
  717. pixel_weights->weights[pixel_refs->nb] = b_weight * alpha;\
  718. pixel_mvs->mvs[pixel_refs->nb][0] = av_clip(-mv_x * (ALPHA_MAX - alpha) / ALPHA_MAX, x_min, x_max);\
  719. pixel_mvs->mvs[pixel_refs->nb][1] = av_clip(-mv_y * (ALPHA_MAX - alpha) / ALPHA_MAX, y_min, y_max);\
  720. pixel_refs->nb++;\
  721. } while(0)
  722. static void bidirectional_obmc(MIContext *mi_ctx, int alpha)
  723. {
  724. int x, y;
  725. int width = mi_ctx->frames[0].avf->width;
  726. int height = mi_ctx->frames[0].avf->height;
  727. int mb_y, mb_x, dir;
  728. for (y = 0; y < height; y++)
  729. for (x = 0; x < width; x++)
  730. mi_ctx->pixel_refs[x + y * width].nb = 0;
  731. for (dir = 0; dir < 2; dir++)
  732. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  733. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  734. int a = dir ? alpha : (ALPHA_MAX - alpha);
  735. int mv_x = mi_ctx->frames[2 - dir].blocks[mb_x + mb_y * mi_ctx->b_width].mvs[dir][0];
  736. int mv_y = mi_ctx->frames[2 - dir].blocks[mb_x + mb_y * mi_ctx->b_width].mvs[dir][1];
  737. int start_x, start_y;
  738. int startc_x, startc_y, endc_x, endc_y;
  739. start_x = (mb_x << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2 + mv_x * a / ALPHA_MAX;
  740. start_y = (mb_y << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2 + mv_y * a / ALPHA_MAX;
  741. startc_x = av_clip(start_x, 0, width - 1);
  742. startc_y = av_clip(start_y, 0, height - 1);
  743. endc_x = av_clip(start_x + (2 << mi_ctx->log2_mb_size), 0, width - 1);
  744. endc_y = av_clip(start_y + (2 << mi_ctx->log2_mb_size), 0, height - 1);
  745. if (dir) {
  746. mv_x = -mv_x;
  747. mv_y = -mv_y;
  748. }
  749. for (y = startc_y; y < endc_y; y++) {
  750. int y_min = -y;
  751. int y_max = height - y - 1;
  752. for (x = startc_x; x < endc_x; x++) {
  753. int x_min = -x;
  754. int x_max = width - x - 1;
  755. int obmc_weight = obmc_tab_linear[4 - mi_ctx->log2_mb_size][(x - start_x) + ((y - start_y) << (mi_ctx->log2_mb_size + 1))];
  756. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * width];
  757. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * width];
  758. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * width];
  759. ADD_PIXELS(obmc_weight, mv_x, mv_y);
  760. }
  761. }
  762. }
  763. }
  764. static void set_frame_data(MIContext *mi_ctx, int alpha, AVFrame *avf_out)
  765. {
  766. int x, y, plane;
  767. for (plane = 0; plane < mi_ctx->nb_planes; plane++) {
  768. int width = avf_out->width;
  769. int height = avf_out->height;
  770. int chroma = plane == 1 || plane == 2;
  771. for (y = 0; y < height; y++)
  772. for (x = 0; x < width; x++) {
  773. int x_mv, y_mv;
  774. int weight_sum = 0;
  775. int i, val = 0;
  776. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * avf_out->width];
  777. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * avf_out->width];
  778. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * avf_out->width];
  779. for (i = 0; i < pixel_refs->nb; i++)
  780. weight_sum += pixel_weights->weights[i];
  781. if (!weight_sum || !pixel_refs->nb) {
  782. pixel_weights->weights[0] = ALPHA_MAX - alpha;
  783. pixel_refs->refs[0] = 1;
  784. pixel_mvs->mvs[0][0] = 0;
  785. pixel_mvs->mvs[0][1] = 0;
  786. pixel_weights->weights[1] = alpha;
  787. pixel_refs->refs[1] = 2;
  788. pixel_mvs->mvs[1][0] = 0;
  789. pixel_mvs->mvs[1][1] = 0;
  790. pixel_refs->nb = 2;
  791. weight_sum = ALPHA_MAX;
  792. }
  793. for (i = 0; i < pixel_refs->nb; i++) {
  794. Frame *frame = &mi_ctx->frames[pixel_refs->refs[i]];
  795. if (chroma) {
  796. x_mv = (x >> mi_ctx->log2_chroma_w) + pixel_mvs->mvs[i][0] / (1 << mi_ctx->log2_chroma_w);
  797. y_mv = (y >> mi_ctx->log2_chroma_h) + pixel_mvs->mvs[i][1] / (1 << mi_ctx->log2_chroma_h);
  798. } else {
  799. x_mv = x + pixel_mvs->mvs[i][0];
  800. y_mv = y + pixel_mvs->mvs[i][1];
  801. }
  802. val += pixel_weights->weights[i] * frame->avf->data[plane][x_mv + y_mv * frame->avf->linesize[plane]];
  803. }
  804. val = ROUNDED_DIV(val, weight_sum);
  805. if (chroma)
  806. avf_out->data[plane][(x >> mi_ctx->log2_chroma_w) + (y >> mi_ctx->log2_chroma_h) * avf_out->linesize[plane]] = val;
  807. else
  808. avf_out->data[plane][x + y * avf_out->linesize[plane]] = val;
  809. }
  810. }
  811. }
  812. static void var_size_bmc(MIContext *mi_ctx, Block *block, int x_mb, int y_mb, int n, int alpha)
  813. {
  814. int sb_x, sb_y;
  815. int width = mi_ctx->frames[0].avf->width;
  816. int height = mi_ctx->frames[0].avf->height;
  817. for (sb_y = 0; sb_y < 2; sb_y++)
  818. for (sb_x = 0; sb_x < 2; sb_x++) {
  819. Block *sb = &block->subs[sb_x + sb_y * 2];
  820. if (sb->sb)
  821. var_size_bmc(mi_ctx, sb, x_mb + (sb_x << (n - 1)), y_mb + (sb_y << (n - 1)), n - 1, alpha);
  822. else {
  823. int x, y;
  824. int mv_x = sb->mvs[0][0] * 2;
  825. int mv_y = sb->mvs[0][1] * 2;
  826. int start_x = x_mb + (sb_x << (n - 1));
  827. int start_y = y_mb + (sb_y << (n - 1));
  828. int end_x = start_x + (1 << (n - 1));
  829. int end_y = start_y + (1 << (n - 1));
  830. for (y = start_y; y < end_y; y++) {
  831. int y_min = -y;
  832. int y_max = height - y - 1;
  833. for (x = start_x; x < end_x; x++) {
  834. int x_min = -x;
  835. int x_max = width - x - 1;
  836. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * width];
  837. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * width];
  838. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * width];
  839. ADD_PIXELS(PX_WEIGHT_MAX, mv_x, mv_y);
  840. }
  841. }
  842. }
  843. }
  844. }
  845. static void bilateral_obmc(MIContext *mi_ctx, Block *block, int mb_x, int mb_y, int alpha)
  846. {
  847. int x, y;
  848. int width = mi_ctx->frames[0].avf->width;
  849. int height = mi_ctx->frames[0].avf->height;
  850. Block *nb;
  851. int nb_x, nb_y;
  852. uint64_t sbads[9];
  853. int mv_x = block->mvs[0][0] * 2;
  854. int mv_y = block->mvs[0][1] * 2;
  855. int start_x, start_y;
  856. int startc_x, startc_y, endc_x, endc_y;
  857. if (mi_ctx->mc_mode == MC_MODE_AOBMC)
  858. for (nb_y = FFMAX(0, mb_y - 1); nb_y < FFMIN(mb_y + 2, mi_ctx->b_height); nb_y++)
  859. for (nb_x = FFMAX(0, mb_x - 1); nb_x < FFMIN(mb_x + 2, mi_ctx->b_width); nb_x++) {
  860. int x_nb = nb_x << mi_ctx->log2_mb_size;
  861. int y_nb = nb_y << mi_ctx->log2_mb_size;
  862. if (nb_x - mb_x || nb_y - mb_y)
  863. sbads[nb_x - mb_x + 1 + (nb_y - mb_y + 1) * 3] = get_sbad(&mi_ctx->me_ctx, x_nb, y_nb, x_nb + block->mvs[0][0], y_nb + block->mvs[0][1]);
  864. }
  865. start_x = (mb_x << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2;
  866. start_y = (mb_y << mi_ctx->log2_mb_size) - mi_ctx->mb_size / 2;
  867. startc_x = av_clip(start_x, 0, width - 1);
  868. startc_y = av_clip(start_y, 0, height - 1);
  869. endc_x = av_clip(start_x + (2 << mi_ctx->log2_mb_size), 0, width - 1);
  870. endc_y = av_clip(start_y + (2 << mi_ctx->log2_mb_size), 0, height - 1);
  871. for (y = startc_y; y < endc_y; y++) {
  872. int y_min = -y;
  873. int y_max = height - y - 1;
  874. for (x = startc_x; x < endc_x; x++) {
  875. int x_min = -x;
  876. int x_max = width - x - 1;
  877. int obmc_weight = obmc_tab_linear[4 - mi_ctx->log2_mb_size][(x - start_x) + ((y - start_y) << (mi_ctx->log2_mb_size + 1))];
  878. PixelMVS *pixel_mvs = &mi_ctx->pixel_mvs[x + y * width];
  879. PixelWeights *pixel_weights = &mi_ctx->pixel_weights[x + y * width];
  880. PixelRefs *pixel_refs = &mi_ctx->pixel_refs[x + y * width];
  881. if (mi_ctx->mc_mode == MC_MODE_AOBMC) {
  882. nb_x = (((x - start_x) >> (mi_ctx->log2_mb_size - 1)) * 2 - 3) / 2;
  883. nb_y = (((y - start_y) >> (mi_ctx->log2_mb_size - 1)) * 2 - 3) / 2;
  884. if (nb_x || nb_y) {
  885. uint64_t sbad = sbads[nb_x + 1 + (nb_y + 1) * 3];
  886. nb = &mi_ctx->int_blocks[mb_x + nb_x + (mb_y + nb_y) * mi_ctx->b_width];
  887. if (sbad && sbad != UINT64_MAX && nb->sbad != UINT64_MAX) {
  888. int phi = av_clip(ALPHA_MAX * nb->sbad / sbad, 0, ALPHA_MAX);
  889. obmc_weight = obmc_weight * phi / ALPHA_MAX;
  890. }
  891. }
  892. }
  893. ADD_PIXELS(obmc_weight, mv_x, mv_y);
  894. }
  895. }
  896. }
  897. static void interpolate(AVFilterLink *inlink, AVFrame *avf_out)
  898. {
  899. AVFilterContext *ctx = inlink->dst;
  900. AVFilterLink *outlink = ctx->outputs[0];
  901. MIContext *mi_ctx = ctx->priv;
  902. int x, y;
  903. int plane, alpha;
  904. int64_t pts;
  905. pts = av_rescale(avf_out->pts, (int64_t) ALPHA_MAX * outlink->time_base.num * inlink->time_base.den,
  906. (int64_t) outlink->time_base.den * inlink->time_base.num);
  907. alpha = (pts - mi_ctx->frames[1].avf->pts * ALPHA_MAX) / (mi_ctx->frames[2].avf->pts - mi_ctx->frames[1].avf->pts);
  908. alpha = av_clip(alpha, 0, ALPHA_MAX);
  909. if (alpha == 0 || alpha == ALPHA_MAX) {
  910. av_frame_copy(avf_out, alpha ? mi_ctx->frames[2].avf : mi_ctx->frames[1].avf);
  911. return;
  912. }
  913. if (mi_ctx->scene_changed) {
  914. /* duplicate frame */
  915. av_frame_copy(avf_out, alpha > ALPHA_MAX / 2 ? mi_ctx->frames[2].avf : mi_ctx->frames[1].avf);
  916. return;
  917. }
  918. switch(mi_ctx->mi_mode) {
  919. case MI_MODE_DUP:
  920. av_frame_copy(avf_out, alpha > ALPHA_MAX / 2 ? mi_ctx->frames[2].avf : mi_ctx->frames[1].avf);
  921. break;
  922. case MI_MODE_BLEND:
  923. for (plane = 0; plane < mi_ctx->nb_planes; plane++) {
  924. int width = avf_out->width;
  925. int height = avf_out->height;
  926. if (plane == 1 || plane == 2) {
  927. width = AV_CEIL_RSHIFT(width, mi_ctx->log2_chroma_w);
  928. height = AV_CEIL_RSHIFT(height, mi_ctx->log2_chroma_h);
  929. }
  930. for (y = 0; y < height; y++) {
  931. for (x = 0; x < width; x++) {
  932. avf_out->data[plane][x + y * avf_out->linesize[plane]] =
  933. (alpha * mi_ctx->frames[2].avf->data[plane][x + y * mi_ctx->frames[2].avf->linesize[plane]] +
  934. (ALPHA_MAX - alpha) * mi_ctx->frames[1].avf->data[plane][x + y * mi_ctx->frames[1].avf->linesize[plane]] + 512) >> 10;
  935. }
  936. }
  937. }
  938. break;
  939. case MI_MODE_MCI:
  940. if (mi_ctx->me_mode == ME_MODE_BIDIR) {
  941. bidirectional_obmc(mi_ctx, alpha);
  942. set_frame_data(mi_ctx, alpha, avf_out);
  943. } else if (mi_ctx->me_mode == ME_MODE_BILAT) {
  944. int mb_x, mb_y;
  945. Block *block;
  946. for (y = 0; y < mi_ctx->frames[0].avf->height; y++)
  947. for (x = 0; x < mi_ctx->frames[0].avf->width; x++)
  948. mi_ctx->pixel_refs[x + y * mi_ctx->frames[0].avf->width].nb = 0;
  949. for (mb_y = 0; mb_y < mi_ctx->b_height; mb_y++)
  950. for (mb_x = 0; mb_x < mi_ctx->b_width; mb_x++) {
  951. block = &mi_ctx->int_blocks[mb_x + mb_y * mi_ctx->b_width];
  952. if (block->sb)
  953. var_size_bmc(mi_ctx, block, mb_x << mi_ctx->log2_mb_size, mb_y << mi_ctx->log2_mb_size, mi_ctx->log2_mb_size, alpha);
  954. bilateral_obmc(mi_ctx, block, mb_x, mb_y, alpha);
  955. }
  956. set_frame_data(mi_ctx, alpha, avf_out);
  957. }
  958. break;
  959. }
  960. }
  961. static int filter_frame(AVFilterLink *inlink, AVFrame *avf_in)
  962. {
  963. AVFilterContext *ctx = inlink->dst;
  964. AVFilterLink *outlink = ctx->outputs[0];
  965. MIContext *mi_ctx = ctx->priv;
  966. int ret;
  967. if (avf_in->pts == AV_NOPTS_VALUE) {
  968. ret = ff_filter_frame(ctx->outputs[0], avf_in);
  969. return ret;
  970. }
  971. if (!mi_ctx->frames[NB_FRAMES - 1].avf || avf_in->pts < mi_ctx->frames[NB_FRAMES - 1].avf->pts) {
  972. av_log(ctx, AV_LOG_VERBOSE, "Initializing out pts from input pts %"PRId64"\n", avf_in->pts);
  973. mi_ctx->out_pts = av_rescale_q(avf_in->pts, inlink->time_base, outlink->time_base);
  974. }
  975. if (!mi_ctx->frames[NB_FRAMES - 1].avf)
  976. if (ret = inject_frame(inlink, av_frame_clone(avf_in)))
  977. return ret;
  978. if (ret = inject_frame(inlink, avf_in))
  979. return ret;
  980. if (!mi_ctx->frames[0].avf)
  981. return 0;
  982. mi_ctx->scene_changed = detect_scene_change(mi_ctx);
  983. for (;;) {
  984. AVFrame *avf_out;
  985. if (av_compare_ts(mi_ctx->out_pts, outlink->time_base, mi_ctx->frames[2].avf->pts, inlink->time_base) > 0)
  986. break;
  987. if (!(avf_out = ff_get_video_buffer(ctx->outputs[0], inlink->w, inlink->h)))
  988. return AVERROR(ENOMEM);
  989. av_frame_copy_props(avf_out, mi_ctx->frames[NB_FRAMES - 1].avf);
  990. avf_out->pts = mi_ctx->out_pts++;
  991. interpolate(inlink, avf_out);
  992. if ((ret = ff_filter_frame(ctx->outputs[0], avf_out)) < 0)
  993. return ret;
  994. }
  995. return 0;
  996. }
  997. static av_cold void free_blocks(Block *block, int sb)
  998. {
  999. if (block->subs)
  1000. free_blocks(block->subs, 1);
  1001. if (sb)
  1002. av_freep(&block);
  1003. }
  1004. static av_cold void uninit(AVFilterContext *ctx)
  1005. {
  1006. MIContext *mi_ctx = ctx->priv;
  1007. int i, m;
  1008. av_freep(&mi_ctx->pixel_mvs);
  1009. av_freep(&mi_ctx->pixel_weights);
  1010. av_freep(&mi_ctx->pixel_refs);
  1011. if (mi_ctx->int_blocks)
  1012. for (m = 0; m < mi_ctx->b_count; m++)
  1013. free_blocks(&mi_ctx->int_blocks[m], 0);
  1014. av_freep(&mi_ctx->int_blocks);
  1015. for (i = 0; i < NB_FRAMES; i++) {
  1016. Frame *frame = &mi_ctx->frames[i];
  1017. av_freep(&frame->blocks);
  1018. av_frame_free(&frame->avf);
  1019. }
  1020. for (i = 0; i < 3; i++)
  1021. av_freep(&mi_ctx->mv_table[i]);
  1022. }
  1023. static const AVFilterPad minterpolate_inputs[] = {
  1024. {
  1025. .name = "default",
  1026. .type = AVMEDIA_TYPE_VIDEO,
  1027. .filter_frame = filter_frame,
  1028. .config_props = config_input,
  1029. },
  1030. { NULL }
  1031. };
  1032. static const AVFilterPad minterpolate_outputs[] = {
  1033. {
  1034. .name = "default",
  1035. .type = AVMEDIA_TYPE_VIDEO,
  1036. .config_props = config_output,
  1037. },
  1038. { NULL }
  1039. };
  1040. AVFilter ff_vf_minterpolate = {
  1041. .name = "minterpolate",
  1042. .description = NULL_IF_CONFIG_SMALL("Frame rate conversion using Motion Interpolation."),
  1043. .priv_size = sizeof(MIContext),
  1044. .priv_class = &minterpolate_class,
  1045. .uninit = uninit,
  1046. .query_formats = query_formats,
  1047. .inputs = minterpolate_inputs,
  1048. .outputs = minterpolate_outputs,
  1049. };