vf_idet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 = &idet->cur->metadata;
  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 = AV_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
  110. h = AV_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 > 8){
  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. int ret;
  267. if (idet->eof)
  268. return AVERROR_EOF;
  269. ret = ff_request_frame(link->src->inputs[0]);
  270. if (ret == AVERROR_EOF && idet->cur && !idet->analyze_interlaced_flag_done) {
  271. AVFrame *next = av_frame_clone(idet->next);
  272. if (!next)
  273. return AVERROR(ENOMEM);
  274. ret = filter_frame(link->src->inputs[0], next);
  275. idet->eof = 1;
  276. }
  277. return ret;
  278. }
  279. static av_cold void uninit(AVFilterContext *ctx)
  280. {
  281. IDETContext *idet = ctx->priv;
  282. int level = strncmp(ctx->name, "auto-inserted", 13) ? AV_LOG_INFO : AV_LOG_DEBUG;
  283. av_log(ctx, level, "Repeated Fields: Neither:%6"PRId64" Top:%6"PRId64" Bottom:%6"PRId64"\n",
  284. idet->total_repeats[REPEAT_NONE],
  285. idet->total_repeats[REPEAT_TOP],
  286. idet->total_repeats[REPEAT_BOTTOM]
  287. );
  288. av_log(ctx, level, "Single frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  289. idet->total_prestat[TFF],
  290. idet->total_prestat[BFF],
  291. idet->total_prestat[PROGRESSIVE],
  292. idet->total_prestat[UNDETERMINED]
  293. );
  294. av_log(ctx, level, "Multi frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  295. idet->total_poststat[TFF],
  296. idet->total_poststat[BFF],
  297. idet->total_poststat[PROGRESSIVE],
  298. idet->total_poststat[UNDETERMINED]
  299. );
  300. av_frame_free(&idet->prev);
  301. av_frame_free(&idet->cur );
  302. av_frame_free(&idet->next);
  303. }
  304. static int query_formats(AVFilterContext *ctx)
  305. {
  306. static const enum AVPixelFormat pix_fmts[] = {
  307. AV_PIX_FMT_YUV420P,
  308. AV_PIX_FMT_YUV422P,
  309. AV_PIX_FMT_YUV444P,
  310. AV_PIX_FMT_YUV410P,
  311. AV_PIX_FMT_YUV411P,
  312. AV_PIX_FMT_GRAY8,
  313. AV_PIX_FMT_YUVJ420P,
  314. AV_PIX_FMT_YUVJ422P,
  315. AV_PIX_FMT_YUVJ444P,
  316. AV_PIX_FMT_GRAY16,
  317. AV_PIX_FMT_YUV440P,
  318. AV_PIX_FMT_YUVJ440P,
  319. AV_PIX_FMT_YUV420P9,
  320. AV_PIX_FMT_YUV422P9,
  321. AV_PIX_FMT_YUV444P9,
  322. AV_PIX_FMT_YUV420P10,
  323. AV_PIX_FMT_YUV422P10,
  324. AV_PIX_FMT_YUV444P10,
  325. AV_PIX_FMT_YUV420P12,
  326. AV_PIX_FMT_YUV422P12,
  327. AV_PIX_FMT_YUV444P12,
  328. AV_PIX_FMT_YUV420P14,
  329. AV_PIX_FMT_YUV422P14,
  330. AV_PIX_FMT_YUV444P14,
  331. AV_PIX_FMT_YUV420P16,
  332. AV_PIX_FMT_YUV422P16,
  333. AV_PIX_FMT_YUV444P16,
  334. AV_PIX_FMT_YUVA420P,
  335. AV_PIX_FMT_YUVA422P,
  336. AV_PIX_FMT_YUVA444P,
  337. AV_PIX_FMT_NONE
  338. };
  339. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  340. if (!fmts_list)
  341. return AVERROR(ENOMEM);
  342. return ff_set_common_formats(ctx, fmts_list);
  343. }
  344. static av_cold int init(AVFilterContext *ctx)
  345. {
  346. IDETContext *idet = ctx->priv;
  347. idet->eof = 0;
  348. idet->last_type = UNDETERMINED;
  349. memset(idet->history, UNDETERMINED, HIST_SIZE);
  350. if( idet->half_life > 0 )
  351. idet->decay_coefficient = lrint( PRECISION * exp2(-1.0 / idet->half_life) );
  352. else
  353. idet->decay_coefficient = PRECISION;
  354. idet->filter_line = ff_idet_filter_line_c;
  355. if (ARCH_X86)
  356. ff_idet_init_x86(idet, 0);
  357. return 0;
  358. }
  359. static const AVFilterPad idet_inputs[] = {
  360. {
  361. .name = "default",
  362. .type = AVMEDIA_TYPE_VIDEO,
  363. .filter_frame = filter_frame,
  364. },
  365. { NULL }
  366. };
  367. static const AVFilterPad idet_outputs[] = {
  368. {
  369. .name = "default",
  370. .type = AVMEDIA_TYPE_VIDEO,
  371. .request_frame = request_frame
  372. },
  373. { NULL }
  374. };
  375. AVFilter ff_vf_idet = {
  376. .name = "idet",
  377. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  378. .priv_size = sizeof(IDETContext),
  379. .init = init,
  380. .uninit = uninit,
  381. .query_formats = query_formats,
  382. .inputs = idet_inputs,
  383. .outputs = idet_outputs,
  384. .priv_class = &idet_class,
  385. };