vf_idet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2012 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <float.h> /* FLT_MAX */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/opt.h"
  24. #include "internal.h"
  25. #include "vf_idet.h"
  26. #define OFFSET(x) offsetof(IDETContext, x)
  27. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  28. static const AVOption idet_options[] = {
  29. { "intl_thres", "set interlacing threshold", OFFSET(interlace_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.04}, -1, FLT_MAX, FLAGS },
  30. { "prog_thres", "set progressive threshold", OFFSET(progressive_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.5}, -1, FLT_MAX, FLAGS },
  31. { "rep_thres", "set repeat threshold", OFFSET(repeat_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 3.0}, -1, FLT_MAX, FLAGS },
  32. { "half_life", "half life of cumulative statistics", OFFSET(half_life), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, -1, INT_MAX, FLAGS },
  33. { NULL }
  34. };
  35. AVFILTER_DEFINE_CLASS(idet);
  36. static const char *type2str(Type type)
  37. {
  38. switch(type) {
  39. case TFF : return "tff";
  40. case BFF : return "bff";
  41. case PROGRESSIVE : return "progressive";
  42. case UNDETERMINED : return "undetermined";
  43. }
  44. return NULL;
  45. }
  46. #define PRECISION 1048576
  47. static uint64_t uintpow(uint64_t b,unsigned int e)
  48. {
  49. uint64_t r=1;
  50. while(e--) r*=b;
  51. return r;
  52. }
  53. static int av_dict_set_fxp(AVDictionary **pm, const char *key, uint64_t value, unsigned int digits,
  54. int flags)
  55. {
  56. char valuestr[44];
  57. uint64_t print_precision = uintpow(10, digits);
  58. value = av_rescale(value, print_precision, PRECISION);
  59. snprintf(valuestr, sizeof(valuestr), "%"PRId64".%0*"PRId64,
  60. value / print_precision, digits, value % print_precision);
  61. return av_dict_set(pm, key, valuestr, flags);
  62. }
  63. static const char *rep2str(RepeatedField repeated_field)
  64. {
  65. switch(repeated_field) {
  66. case REPEAT_NONE : return "neither";
  67. case REPEAT_TOP : return "top";
  68. case REPEAT_BOTTOM : return "bottom";
  69. }
  70. return NULL;
  71. }
  72. int ff_idet_filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
  73. {
  74. int x;
  75. int ret=0;
  76. for(x=0; x<w; x++){
  77. int v = (*a++ + *c++) - 2 * *b++;
  78. ret += FFABS(v);
  79. }
  80. return ret;
  81. }
  82. int ff_idet_filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
  83. {
  84. int x;
  85. int ret=0;
  86. for(x=0; x<w; x++){
  87. int v = (*a++ + *c++) - 2 * *b++;
  88. ret += FFABS(v);
  89. }
  90. return ret;
  91. }
  92. static void filter(AVFilterContext *ctx)
  93. {
  94. IDETContext *idet = ctx->priv;
  95. int y, i;
  96. int64_t alpha[2]={0};
  97. int64_t delta=0;
  98. int64_t gamma[2]={0};
  99. Type type, best_type;
  100. RepeatedField repeat;
  101. int match = 0;
  102. AVDictionary **metadata = avpriv_frame_get_metadatap(idet->cur);
  103. for (i = 0; i < idet->csp->nb_components; i++) {
  104. int w = idet->cur->width;
  105. int h = idet->cur->height;
  106. int refs = idet->cur->linesize[i];
  107. if (i && i<3) {
  108. w = FF_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
  109. h = FF_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
  110. }
  111. for (y = 2; y < h - 2; y++) {
  112. uint8_t *prev = &idet->prev->data[i][y*refs];
  113. uint8_t *cur = &idet->cur ->data[i][y*refs];
  114. uint8_t *next = &idet->next->data[i][y*refs];
  115. alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
  116. alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
  117. delta += idet->filter_line(cur-refs, cur, cur+refs, w);
  118. gamma[(y^1)&1] += idet->filter_line(cur , prev, cur , w);
  119. }
  120. }
  121. if (alpha[0] > idet->interlace_threshold * alpha[1]){
  122. type = TFF;
  123. }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
  124. type = BFF;
  125. }else if(alpha[1] > idet->progressive_threshold * delta){
  126. type = PROGRESSIVE;
  127. }else{
  128. type = UNDETERMINED;
  129. }
  130. if ( gamma[0] > idet->repeat_threshold * gamma[1] ){
  131. repeat = REPEAT_TOP;
  132. } else if ( gamma[1] > idet->repeat_threshold * gamma[0] ){
  133. repeat = REPEAT_BOTTOM;
  134. } else {
  135. repeat = REPEAT_NONE;
  136. }
  137. memmove(idet->history+1, idet->history, HIST_SIZE-1);
  138. idet->history[0] = type;
  139. best_type = UNDETERMINED;
  140. for(i=0; i<HIST_SIZE; i++){
  141. if(idet->history[i] != UNDETERMINED){
  142. if(best_type == UNDETERMINED)
  143. best_type = idet->history[i];
  144. if(idet->history[i] == best_type) {
  145. match++;
  146. }else{
  147. match=0;
  148. break;
  149. }
  150. }
  151. }
  152. if(idet->last_type == UNDETERMINED){
  153. if(match ) idet->last_type = best_type;
  154. }else{
  155. if(match>2) idet->last_type = best_type;
  156. }
  157. if (idet->last_type == TFF){
  158. idet->cur->top_field_first = 1;
  159. idet->cur->interlaced_frame = 1;
  160. }else if(idet->last_type == BFF){
  161. idet->cur->top_field_first = 0;
  162. idet->cur->interlaced_frame = 1;
  163. }else if(idet->last_type == PROGRESSIVE){
  164. idet->cur->interlaced_frame = 0;
  165. }
  166. for(i=0; i<3; i++)
  167. idet->repeats[i] = av_rescale(idet->repeats [i], idet->decay_coefficient, PRECISION);
  168. for(i=0; i<4; i++){
  169. idet->prestat [i] = av_rescale(idet->prestat [i], idet->decay_coefficient, PRECISION);
  170. idet->poststat[i] = av_rescale(idet->poststat[i], idet->decay_coefficient, PRECISION);
  171. }
  172. idet->total_repeats [ repeat] ++;
  173. idet->repeats [ repeat] += PRECISION;
  174. idet->total_prestat [ type] ++;
  175. idet->prestat [ type] += PRECISION;
  176. idet->total_poststat[idet->last_type] ++;
  177. idet->poststat [idet->last_type] += PRECISION;
  178. av_log(ctx, AV_LOG_DEBUG, "Repeated Field:%12s, Single frame:%12s, Multi frame:%12s\n",
  179. rep2str(repeat), type2str(type), type2str(idet->last_type));
  180. av_dict_set (metadata, "lavfi.idet.repeated.current_frame", rep2str(repeat), 0);
  181. av_dict_set_fxp(metadata, "lavfi.idet.repeated.neither", idet->repeats[REPEAT_NONE], 2, 0);
  182. av_dict_set_fxp(metadata, "lavfi.idet.repeated.top", idet->repeats[REPEAT_TOP], 2, 0);
  183. av_dict_set_fxp(metadata, "lavfi.idet.repeated.bottom", idet->repeats[REPEAT_BOTTOM], 2, 0);
  184. av_dict_set (metadata, "lavfi.idet.single.current_frame", type2str(type), 0);
  185. av_dict_set_fxp(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 2 , 0);
  186. av_dict_set_fxp(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 2, 0);
  187. av_dict_set_fxp(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 2, 0);
  188. av_dict_set_fxp(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 2, 0);
  189. av_dict_set (metadata, "lavfi.idet.multiple.current_frame", type2str(idet->last_type), 0);
  190. av_dict_set_fxp(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 2, 0);
  191. av_dict_set_fxp(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 2, 0);
  192. av_dict_set_fxp(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 2, 0);
  193. av_dict_set_fxp(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 2, 0);
  194. }
  195. static int filter_frame(AVFilterLink *link, AVFrame *picref)
  196. {
  197. AVFilterContext *ctx = link->dst;
  198. IDETContext *idet = ctx->priv;
  199. if (idet->prev)
  200. av_frame_free(&idet->prev);
  201. idet->prev = idet->cur;
  202. idet->cur = idet->next;
  203. idet->next = picref;
  204. if (!idet->cur &&
  205. !(idet->cur = av_frame_clone(idet->next)))
  206. return AVERROR(ENOMEM);
  207. if (!idet->prev)
  208. return 0;
  209. if (!idet->csp)
  210. idet->csp = av_pix_fmt_desc_get(link->format);
  211. if (idet->csp->comp[0].depth_minus1 / 8 == 1){
  212. idet->filter_line = (ff_idet_filter_func)ff_idet_filter_line_c_16bit;
  213. if (ARCH_X86)
  214. ff_idet_init_x86(idet, 1);
  215. }
  216. filter(ctx);
  217. return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
  218. }
  219. static int request_frame(AVFilterLink *link)
  220. {
  221. AVFilterContext *ctx = link->src;
  222. IDETContext *idet = ctx->priv;
  223. do {
  224. int ret;
  225. if (idet->eof)
  226. return AVERROR_EOF;
  227. ret = ff_request_frame(link->src->inputs[0]);
  228. if (ret == AVERROR_EOF && idet->cur) {
  229. AVFrame *next = av_frame_clone(idet->next);
  230. if (!next)
  231. return AVERROR(ENOMEM);
  232. filter_frame(link->src->inputs[0], next);
  233. idet->eof = 1;
  234. } else if (ret < 0) {
  235. return ret;
  236. }
  237. } while (!idet->prev);
  238. return 0;
  239. }
  240. static av_cold void uninit(AVFilterContext *ctx)
  241. {
  242. IDETContext *idet = ctx->priv;
  243. av_log(ctx, AV_LOG_INFO, "Repeated Fields: Neither:%6"PRId64" Top:%6"PRId64" Bottom:%6"PRId64"\n",
  244. idet->total_repeats[REPEAT_NONE],
  245. idet->total_repeats[REPEAT_TOP],
  246. idet->total_repeats[REPEAT_BOTTOM]
  247. );
  248. av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  249. idet->total_prestat[TFF],
  250. idet->total_prestat[BFF],
  251. idet->total_prestat[PROGRESSIVE],
  252. idet->total_prestat[UNDETERMINED]
  253. );
  254. av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  255. idet->total_poststat[TFF],
  256. idet->total_poststat[BFF],
  257. idet->total_poststat[PROGRESSIVE],
  258. idet->total_poststat[UNDETERMINED]
  259. );
  260. av_frame_free(&idet->prev);
  261. av_frame_free(&idet->cur );
  262. av_frame_free(&idet->next);
  263. }
  264. static int query_formats(AVFilterContext *ctx)
  265. {
  266. static const enum AVPixelFormat pix_fmts[] = {
  267. AV_PIX_FMT_YUV420P,
  268. AV_PIX_FMT_YUV422P,
  269. AV_PIX_FMT_YUV444P,
  270. AV_PIX_FMT_YUV410P,
  271. AV_PIX_FMT_YUV411P,
  272. AV_PIX_FMT_GRAY8,
  273. AV_PIX_FMT_YUVJ420P,
  274. AV_PIX_FMT_YUVJ422P,
  275. AV_PIX_FMT_YUVJ444P,
  276. AV_PIX_FMT_GRAY16,
  277. AV_PIX_FMT_YUV440P,
  278. AV_PIX_FMT_YUVJ440P,
  279. AV_PIX_FMT_YUV420P10,
  280. AV_PIX_FMT_YUV422P10,
  281. AV_PIX_FMT_YUV444P10,
  282. AV_PIX_FMT_YUV420P16,
  283. AV_PIX_FMT_YUV422P16,
  284. AV_PIX_FMT_YUV444P16,
  285. AV_PIX_FMT_YUVA420P,
  286. AV_PIX_FMT_NONE
  287. };
  288. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  289. return 0;
  290. }
  291. static int config_output(AVFilterLink *outlink)
  292. {
  293. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  294. return 0;
  295. }
  296. static av_cold int init(AVFilterContext *ctx)
  297. {
  298. IDETContext *idet = ctx->priv;
  299. idet->eof = 0;
  300. idet->last_type = UNDETERMINED;
  301. memset(idet->history, UNDETERMINED, HIST_SIZE);
  302. if( idet->half_life > 0 )
  303. idet->decay_coefficient = (uint64_t) round( PRECISION * exp2(-1.0 / idet->half_life) );
  304. else
  305. idet->decay_coefficient = PRECISION;
  306. idet->filter_line = ff_idet_filter_line_c;
  307. if (ARCH_X86)
  308. ff_idet_init_x86(idet, 0);
  309. return 0;
  310. }
  311. static const AVFilterPad idet_inputs[] = {
  312. {
  313. .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .filter_frame = filter_frame,
  316. },
  317. { NULL }
  318. };
  319. static const AVFilterPad idet_outputs[] = {
  320. {
  321. .name = "default",
  322. .type = AVMEDIA_TYPE_VIDEO,
  323. .config_props = config_output,
  324. .request_frame = request_frame
  325. },
  326. { NULL }
  327. };
  328. AVFilter ff_vf_idet = {
  329. .name = "idet",
  330. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  331. .priv_size = sizeof(IDETContext),
  332. .init = init,
  333. .uninit = uninit,
  334. .query_formats = query_formats,
  335. .inputs = idet_inputs,
  336. .outputs = idet_outputs,
  337. .priv_class = &idet_class,
  338. };