vf_idet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. { "analyze_interlaced_flag", "set number of frames to use to determine if the interlace flag is accurate", OFFSET(analyze_interlaced_flag), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, FLAGS },
  34. { NULL }
  35. };
  36. AVFILTER_DEFINE_CLASS(idet);
  37. static const char *type2str(Type type)
  38. {
  39. switch(type) {
  40. case TFF : return "tff";
  41. case BFF : return "bff";
  42. case PROGRESSIVE : return "progressive";
  43. case UNDETERMINED : return "undetermined";
  44. }
  45. return NULL;
  46. }
  47. #define PRECISION 1048576
  48. static uint64_t uintpow(uint64_t b,unsigned int e)
  49. {
  50. uint64_t r=1;
  51. while(e--) r*=b;
  52. return r;
  53. }
  54. static int av_dict_set_fxp(AVDictionary **pm, const char *key, uint64_t value, unsigned int digits,
  55. int flags)
  56. {
  57. char valuestr[44];
  58. uint64_t print_precision = uintpow(10, digits);
  59. value = av_rescale(value, print_precision, PRECISION);
  60. snprintf(valuestr, sizeof(valuestr), "%"PRId64".%0*"PRId64,
  61. value / print_precision, digits, value % print_precision);
  62. return av_dict_set(pm, key, valuestr, flags);
  63. }
  64. static const char *rep2str(RepeatedField repeated_field)
  65. {
  66. switch(repeated_field) {
  67. case REPEAT_NONE : return "neither";
  68. case REPEAT_TOP : return "top";
  69. case REPEAT_BOTTOM : return "bottom";
  70. }
  71. return NULL;
  72. }
  73. int ff_idet_filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
  74. {
  75. int x;
  76. int ret=0;
  77. for(x=0; x<w; x++){
  78. int v = (*a++ + *c++) - 2 * *b++;
  79. ret += FFABS(v);
  80. }
  81. return ret;
  82. }
  83. int ff_idet_filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
  84. {
  85. int x;
  86. int ret=0;
  87. for(x=0; x<w; x++){
  88. int v = (*a++ + *c++) - 2 * *b++;
  89. ret += FFABS(v);
  90. }
  91. return ret;
  92. }
  93. static void filter(AVFilterContext *ctx)
  94. {
  95. IDETContext *idet = ctx->priv;
  96. int y, i;
  97. int64_t alpha[2]={0};
  98. int64_t delta=0;
  99. int64_t gamma[2]={0};
  100. Type type, best_type;
  101. RepeatedField repeat;
  102. int match = 0;
  103. AVDictionary **metadata = avpriv_frame_get_metadatap(idet->cur);
  104. for (i = 0; i < idet->csp->nb_components; i++) {
  105. int w = idet->cur->width;
  106. int h = idet->cur->height;
  107. int refs = idet->cur->linesize[i];
  108. if (i && i<3) {
  109. w = FF_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
  110. h = FF_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
  111. }
  112. for (y = 2; y < h - 2; y++) {
  113. uint8_t *prev = &idet->prev->data[i][y*refs];
  114. uint8_t *cur = &idet->cur ->data[i][y*refs];
  115. uint8_t *next = &idet->next->data[i][y*refs];
  116. alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
  117. alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
  118. delta += idet->filter_line(cur-refs, cur, cur+refs, w);
  119. gamma[(y^1)&1] += idet->filter_line(cur , prev, cur , w);
  120. }
  121. }
  122. if (alpha[0] > idet->interlace_threshold * alpha[1]){
  123. type = TFF;
  124. }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
  125. type = BFF;
  126. }else if(alpha[1] > idet->progressive_threshold * delta){
  127. type = PROGRESSIVE;
  128. }else{
  129. type = UNDETERMINED;
  130. }
  131. if ( gamma[0] > idet->repeat_threshold * gamma[1] ){
  132. repeat = REPEAT_TOP;
  133. } else if ( gamma[1] > idet->repeat_threshold * gamma[0] ){
  134. repeat = REPEAT_BOTTOM;
  135. } else {
  136. repeat = REPEAT_NONE;
  137. }
  138. memmove(idet->history+1, idet->history, HIST_SIZE-1);
  139. idet->history[0] = type;
  140. best_type = UNDETERMINED;
  141. for(i=0; i<HIST_SIZE; i++){
  142. if(idet->history[i] != UNDETERMINED){
  143. if(best_type == UNDETERMINED)
  144. best_type = idet->history[i];
  145. if(idet->history[i] == best_type) {
  146. match++;
  147. }else{
  148. match=0;
  149. break;
  150. }
  151. }
  152. }
  153. if(idet->last_type == UNDETERMINED){
  154. if(match ) idet->last_type = best_type;
  155. }else{
  156. if(match>2) idet->last_type = best_type;
  157. }
  158. if (idet->last_type == TFF){
  159. idet->cur->top_field_first = 1;
  160. idet->cur->interlaced_frame = 1;
  161. }else if(idet->last_type == BFF){
  162. idet->cur->top_field_first = 0;
  163. idet->cur->interlaced_frame = 1;
  164. }else if(idet->last_type == PROGRESSIVE){
  165. idet->cur->interlaced_frame = 0;
  166. }
  167. for(i=0; i<3; i++)
  168. idet->repeats[i] = av_rescale(idet->repeats [i], idet->decay_coefficient, PRECISION);
  169. for(i=0; i<4; i++){
  170. idet->prestat [i] = av_rescale(idet->prestat [i], idet->decay_coefficient, PRECISION);
  171. idet->poststat[i] = av_rescale(idet->poststat[i], idet->decay_coefficient, PRECISION);
  172. }
  173. idet->total_repeats [ repeat] ++;
  174. idet->repeats [ repeat] += PRECISION;
  175. idet->total_prestat [ type] ++;
  176. idet->prestat [ type] += PRECISION;
  177. idet->total_poststat[idet->last_type] ++;
  178. idet->poststat [idet->last_type] += PRECISION;
  179. av_log(ctx, AV_LOG_DEBUG, "Repeated Field:%12s, Single frame:%12s, Multi frame:%12s\n",
  180. rep2str(repeat), type2str(type), type2str(idet->last_type));
  181. av_dict_set (metadata, "lavfi.idet.repeated.current_frame", rep2str(repeat), 0);
  182. av_dict_set_fxp(metadata, "lavfi.idet.repeated.neither", idet->repeats[REPEAT_NONE], 2, 0);
  183. av_dict_set_fxp(metadata, "lavfi.idet.repeated.top", idet->repeats[REPEAT_TOP], 2, 0);
  184. av_dict_set_fxp(metadata, "lavfi.idet.repeated.bottom", idet->repeats[REPEAT_BOTTOM], 2, 0);
  185. av_dict_set (metadata, "lavfi.idet.single.current_frame", type2str(type), 0);
  186. av_dict_set_fxp(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 2 , 0);
  187. av_dict_set_fxp(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 2, 0);
  188. av_dict_set_fxp(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 2, 0);
  189. av_dict_set_fxp(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 2, 0);
  190. av_dict_set (metadata, "lavfi.idet.multiple.current_frame", type2str(idet->last_type), 0);
  191. av_dict_set_fxp(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 2, 0);
  192. av_dict_set_fxp(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 2, 0);
  193. av_dict_set_fxp(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 2, 0);
  194. av_dict_set_fxp(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 2, 0);
  195. }
  196. static int filter_frame(AVFilterLink *link, AVFrame *picref)
  197. {
  198. AVFilterContext *ctx = link->dst;
  199. IDETContext *idet = ctx->priv;
  200. // initial frame(s) and not interlaced, just pass through for
  201. // the analyze_interlaced_flag mode
  202. if (idet->analyze_interlaced_flag &&
  203. !picref->interlaced_frame &&
  204. !idet->next) {
  205. return ff_filter_frame(ctx->outputs[0], picref);
  206. }
  207. if (idet->analyze_interlaced_flag_done) {
  208. if (picref->interlaced_frame && idet->interlaced_flag_accuracy < 0)
  209. picref->interlaced_frame = 0;
  210. return ff_filter_frame(ctx->outputs[0], picref);
  211. }
  212. av_frame_free(&idet->prev);
  213. if( picref->width != link->w
  214. || picref->height != link->h
  215. || picref->format != link->format) {
  216. link->dst->inputs[0]->format = picref->format;
  217. link->dst->inputs[0]->w = picref->width;
  218. link->dst->inputs[0]->h = picref->height;
  219. av_frame_free(&idet->cur );
  220. av_frame_free(&idet->next);
  221. }
  222. idet->prev = idet->cur;
  223. idet->cur = idet->next;
  224. idet->next = picref;
  225. if (!idet->cur &&
  226. !(idet->cur = av_frame_clone(idet->next)))
  227. return AVERROR(ENOMEM);
  228. if (!idet->prev)
  229. return 0;
  230. if (!idet->csp)
  231. idet->csp = av_pix_fmt_desc_get(link->format);
  232. if (idet->csp->comp[0].depth_minus1 / 8 == 1){
  233. idet->filter_line = (ff_idet_filter_func)ff_idet_filter_line_c_16bit;
  234. if (ARCH_X86)
  235. ff_idet_init_x86(idet, 1);
  236. }
  237. if (idet->analyze_interlaced_flag) {
  238. if (idet->cur->interlaced_frame) {
  239. idet->cur->interlaced_frame = 0;
  240. filter(ctx);
  241. if (idet->last_type == PROGRESSIVE) {
  242. idet->interlaced_flag_accuracy --;
  243. idet->analyze_interlaced_flag --;
  244. } else if (idet->last_type != UNDETERMINED) {
  245. idet->interlaced_flag_accuracy ++;
  246. idet->analyze_interlaced_flag --;
  247. }
  248. if (idet->analyze_interlaced_flag == 1) {
  249. ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
  250. if (idet->next->interlaced_frame && idet->interlaced_flag_accuracy < 0)
  251. idet->next->interlaced_frame = 0;
  252. idet->analyze_interlaced_flag_done = 1;
  253. av_log(ctx, AV_LOG_INFO, "Final flag accuracy %d\n", idet->interlaced_flag_accuracy);
  254. return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->next));
  255. }
  256. }
  257. } else {
  258. filter(ctx);
  259. }
  260. return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
  261. }
  262. static int request_frame(AVFilterLink *link)
  263. {
  264. AVFilterContext *ctx = link->src;
  265. IDETContext *idet = ctx->priv;
  266. do {
  267. int ret;
  268. if (idet->eof)
  269. return AVERROR_EOF;
  270. ret = ff_request_frame(link->src->inputs[0]);
  271. if (ret == AVERROR_EOF && idet->cur && !idet->analyze_interlaced_flag_done) {
  272. AVFrame *next = av_frame_clone(idet->next);
  273. if (!next)
  274. return AVERROR(ENOMEM);
  275. filter_frame(link->src->inputs[0], next);
  276. idet->eof = 1;
  277. } else if (ret < 0) {
  278. return ret;
  279. }
  280. } while (link->frame_requested);
  281. return 0;
  282. }
  283. static av_cold void uninit(AVFilterContext *ctx)
  284. {
  285. IDETContext *idet = ctx->priv;
  286. int level = strncmp(ctx->name, "auto-inserted", 13) ? AV_LOG_INFO : AV_LOG_DEBUG;
  287. av_log(ctx, level, "Repeated Fields: Neither:%6"PRId64" Top:%6"PRId64" Bottom:%6"PRId64"\n",
  288. idet->total_repeats[REPEAT_NONE],
  289. idet->total_repeats[REPEAT_TOP],
  290. idet->total_repeats[REPEAT_BOTTOM]
  291. );
  292. av_log(ctx, level, "Single frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  293. idet->total_prestat[TFF],
  294. idet->total_prestat[BFF],
  295. idet->total_prestat[PROGRESSIVE],
  296. idet->total_prestat[UNDETERMINED]
  297. );
  298. av_log(ctx, level, "Multi frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  299. idet->total_poststat[TFF],
  300. idet->total_poststat[BFF],
  301. idet->total_poststat[PROGRESSIVE],
  302. idet->total_poststat[UNDETERMINED]
  303. );
  304. av_frame_free(&idet->prev);
  305. av_frame_free(&idet->cur );
  306. av_frame_free(&idet->next);
  307. }
  308. static int query_formats(AVFilterContext *ctx)
  309. {
  310. static const enum AVPixelFormat pix_fmts[] = {
  311. AV_PIX_FMT_YUV420P,
  312. AV_PIX_FMT_YUV422P,
  313. AV_PIX_FMT_YUV444P,
  314. AV_PIX_FMT_YUV410P,
  315. AV_PIX_FMT_YUV411P,
  316. AV_PIX_FMT_GRAY8,
  317. AV_PIX_FMT_YUVJ420P,
  318. AV_PIX_FMT_YUVJ422P,
  319. AV_PIX_FMT_YUVJ444P,
  320. AV_PIX_FMT_GRAY16,
  321. AV_PIX_FMT_YUV440P,
  322. AV_PIX_FMT_YUVJ440P,
  323. AV_PIX_FMT_YUV420P9,
  324. AV_PIX_FMT_YUV422P9,
  325. AV_PIX_FMT_YUV444P9,
  326. AV_PIX_FMT_YUV420P10,
  327. AV_PIX_FMT_YUV422P10,
  328. AV_PIX_FMT_YUV444P10,
  329. AV_PIX_FMT_YUV420P12,
  330. AV_PIX_FMT_YUV422P12,
  331. AV_PIX_FMT_YUV444P12,
  332. AV_PIX_FMT_YUV420P14,
  333. AV_PIX_FMT_YUV422P14,
  334. AV_PIX_FMT_YUV444P14,
  335. AV_PIX_FMT_YUV420P16,
  336. AV_PIX_FMT_YUV422P16,
  337. AV_PIX_FMT_YUV444P16,
  338. AV_PIX_FMT_YUVA420P,
  339. AV_PIX_FMT_NONE
  340. };
  341. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  342. return 0;
  343. }
  344. static int config_output(AVFilterLink *outlink)
  345. {
  346. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  347. return 0;
  348. }
  349. static av_cold int init(AVFilterContext *ctx)
  350. {
  351. IDETContext *idet = ctx->priv;
  352. idet->eof = 0;
  353. idet->last_type = UNDETERMINED;
  354. memset(idet->history, UNDETERMINED, HIST_SIZE);
  355. if( idet->half_life > 0 )
  356. idet->decay_coefficient = (uint64_t) round( PRECISION * exp2(-1.0 / idet->half_life) );
  357. else
  358. idet->decay_coefficient = PRECISION;
  359. idet->filter_line = ff_idet_filter_line_c;
  360. if (ARCH_X86)
  361. ff_idet_init_x86(idet, 0);
  362. return 0;
  363. }
  364. static const AVFilterPad idet_inputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .filter_frame = filter_frame,
  369. },
  370. { NULL }
  371. };
  372. static const AVFilterPad idet_outputs[] = {
  373. {
  374. .name = "default",
  375. .type = AVMEDIA_TYPE_VIDEO,
  376. .config_props = config_output,
  377. .request_frame = request_frame
  378. },
  379. { NULL }
  380. };
  381. AVFilter ff_vf_idet = {
  382. .name = "idet",
  383. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  384. .priv_size = sizeof(IDETContext),
  385. .init = init,
  386. .uninit = uninit,
  387. .query_formats = query_formats,
  388. .inputs = idet_inputs,
  389. .outputs = idet_outputs,
  390. .priv_class = &idet_class,
  391. };