vf_idet.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #define HIST_SIZE 4
  28. typedef enum {
  29. TFF,
  30. BFF,
  31. PROGRSSIVE,
  32. UNDETERMINED,
  33. } Type;
  34. typedef struct {
  35. const AVClass *class;
  36. float interlace_threshold;
  37. float progressive_threshold;
  38. Type last_type;
  39. int prestat[4];
  40. int poststat[4];
  41. uint8_t history[HIST_SIZE];
  42. AVFilterBufferRef *cur;
  43. AVFilterBufferRef *next;
  44. AVFilterBufferRef *prev;
  45. int (*filter_line)(const uint8_t *prev, const uint8_t *cur, const uint8_t *next, int w);
  46. const AVPixFmtDescriptor *csp;
  47. } IDETContext;
  48. #define OFFSET(x) offsetof(IDETContext, x)
  49. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  50. static const AVOption idet_options[] = {
  51. { "intl_thres", "set interlacing threshold", OFFSET(interlace_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.01}, -1, FLT_MAX, FLAGS },
  52. { "prog_thres", "set progressive threshold", OFFSET(progressive_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 2.5}, -1, FLT_MAX, FLAGS },
  53. { NULL }
  54. };
  55. AVFILTER_DEFINE_CLASS(idet);
  56. static const char *type2str(Type type)
  57. {
  58. switch(type) {
  59. case TFF : return "Top Field First ";
  60. case BFF : return "Bottom Field First";
  61. case PROGRSSIVE : return "Progressive ";
  62. case UNDETERMINED: return "Undetermined ";
  63. }
  64. return NULL;
  65. }
  66. static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
  67. {
  68. int x;
  69. int ret=0;
  70. for(x=0; x<w; x++){
  71. ret += FFABS((*a++ + *c++) - 2 * *b++);
  72. }
  73. return ret;
  74. }
  75. static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
  76. {
  77. int x;
  78. int ret=0;
  79. for(x=0; x<w; x++){
  80. ret += FFABS((*a++ + *c++) - 2 * *b++);
  81. }
  82. return ret;
  83. }
  84. static void filter(AVFilterContext *ctx)
  85. {
  86. IDETContext *idet = ctx->priv;
  87. int y, i;
  88. int64_t alpha[2]={0};
  89. int64_t delta=0;
  90. Type type, best_type;
  91. int match = 0;
  92. for (i = 0; i < idet->csp->nb_components; i++) {
  93. int w = idet->cur->video->w;
  94. int h = idet->cur->video->h;
  95. int refs = idet->cur->linesize[i];
  96. if (i && i<3) {
  97. w >>= idet->csp->log2_chroma_w;
  98. h >>= idet->csp->log2_chroma_h;
  99. }
  100. for (y = 2; y < h - 2; y++) {
  101. uint8_t *prev = &idet->prev->data[i][y*refs];
  102. uint8_t *cur = &idet->cur ->data[i][y*refs];
  103. uint8_t *next = &idet->next->data[i][y*refs];
  104. alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
  105. alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
  106. delta += idet->filter_line(cur-refs, cur, cur+refs, w);
  107. }
  108. }
  109. if (alpha[0] > idet->interlace_threshold * alpha[1]){
  110. type = TFF;
  111. }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
  112. type = BFF;
  113. }else if(alpha[1] > idet->progressive_threshold * delta){
  114. type = PROGRSSIVE;
  115. }else{
  116. type = UNDETERMINED;
  117. }
  118. memmove(idet->history+1, idet->history, HIST_SIZE-1);
  119. idet->history[0] = type;
  120. best_type = UNDETERMINED;
  121. for(i=0; i<HIST_SIZE; i++){
  122. if(idet->history[i] != UNDETERMINED){
  123. if(best_type == UNDETERMINED)
  124. best_type = idet->history[i];
  125. if(idet->history[i] == best_type) {
  126. match++;
  127. }else{
  128. match=0;
  129. break;
  130. }
  131. }
  132. }
  133. if(idet->last_type == UNDETERMINED){
  134. if(match ) idet->last_type = best_type;
  135. }else{
  136. if(match>2) idet->last_type = best_type;
  137. }
  138. if (idet->last_type == TFF){
  139. idet->cur->video->top_field_first = 1;
  140. idet->cur->video->interlaced = 1;
  141. }else if(idet->last_type == BFF){
  142. idet->cur->video->top_field_first = 0;
  143. idet->cur->video->interlaced = 1;
  144. }else if(idet->last_type == PROGRSSIVE){
  145. idet->cur->video->interlaced = 0;
  146. }
  147. idet->prestat [ type] ++;
  148. idet->poststat[idet->last_type] ++;
  149. av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
  150. }
  151. static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  152. {
  153. AVFilterContext *ctx = link->dst;
  154. IDETContext *idet = ctx->priv;
  155. if (idet->prev)
  156. avfilter_unref_buffer(idet->prev);
  157. idet->prev = idet->cur;
  158. idet->cur = idet->next;
  159. idet->next = picref;
  160. if (!idet->cur)
  161. return 0;
  162. if (!idet->prev)
  163. idet->prev = avfilter_ref_buffer(idet->cur, ~0);
  164. if (!idet->csp)
  165. idet->csp = av_pix_fmt_desc_get(link->format);
  166. if (idet->csp->comp[0].depth_minus1 / 8 == 1)
  167. idet->filter_line = (void*)filter_line_c_16bit;
  168. filter(ctx);
  169. return ff_filter_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
  170. }
  171. static int request_frame(AVFilterLink *link)
  172. {
  173. AVFilterContext *ctx = link->src;
  174. IDETContext *idet = ctx->priv;
  175. do {
  176. int ret;
  177. if ((ret = ff_request_frame(link->src->inputs[0])))
  178. return ret;
  179. } while (!idet->cur);
  180. return 0;
  181. }
  182. static av_cold void uninit(AVFilterContext *ctx)
  183. {
  184. IDETContext *idet = ctx->priv;
  185. av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  186. idet->prestat[TFF],
  187. idet->prestat[BFF],
  188. idet->prestat[PROGRSSIVE],
  189. idet->prestat[UNDETERMINED]
  190. );
  191. av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  192. idet->poststat[TFF],
  193. idet->poststat[BFF],
  194. idet->poststat[PROGRSSIVE],
  195. idet->poststat[UNDETERMINED]
  196. );
  197. avfilter_unref_bufferp(&idet->prev);
  198. avfilter_unref_bufferp(&idet->cur );
  199. avfilter_unref_bufferp(&idet->next);
  200. }
  201. static int query_formats(AVFilterContext *ctx)
  202. {
  203. static const enum AVPixelFormat pix_fmts[] = {
  204. AV_PIX_FMT_YUV420P,
  205. AV_PIX_FMT_YUV422P,
  206. AV_PIX_FMT_YUV444P,
  207. AV_PIX_FMT_YUV410P,
  208. AV_PIX_FMT_YUV411P,
  209. AV_PIX_FMT_GRAY8,
  210. AV_PIX_FMT_YUVJ420P,
  211. AV_PIX_FMT_YUVJ422P,
  212. AV_PIX_FMT_YUVJ444P,
  213. AV_NE( AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE ),
  214. AV_PIX_FMT_YUV440P,
  215. AV_PIX_FMT_YUVJ440P,
  216. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  217. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  218. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  219. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  220. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  221. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  222. AV_PIX_FMT_YUVA420P,
  223. AV_PIX_FMT_NONE
  224. };
  225. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  226. return 0;
  227. }
  228. static av_cold int init(AVFilterContext *ctx, const char *args)
  229. {
  230. IDETContext *idet = ctx->priv;
  231. static const char *shorthand[] = { "intl_thres", "prog_thres", NULL };
  232. int ret;
  233. idet->class = &idet_class;
  234. av_opt_set_defaults(idet);
  235. if ((ret = av_opt_set_from_string(idet, args, shorthand, "=", ":")) < 0)
  236. return ret;
  237. idet->last_type = UNDETERMINED;
  238. memset(idet->history, UNDETERMINED, HIST_SIZE);
  239. idet->filter_line = filter_line_c;
  240. return 0;
  241. }
  242. static const AVFilterPad idet_inputs[] = {
  243. {
  244. .name = "default",
  245. .type = AVMEDIA_TYPE_VIDEO,
  246. .filter_frame = filter_frame,
  247. .min_perms = AV_PERM_PRESERVE,
  248. },
  249. { NULL }
  250. };
  251. static const AVFilterPad idet_outputs[] = {
  252. {
  253. .name = "default",
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .rej_perms = AV_PERM_WRITE,
  256. .request_frame = request_frame,
  257. },
  258. { NULL }
  259. };
  260. AVFilter avfilter_vf_idet = {
  261. .name = "idet",
  262. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  263. .priv_size = sizeof(IDETContext),
  264. .init = init,
  265. .uninit = uninit,
  266. .query_formats = query_formats,
  267. .inputs = idet_inputs,
  268. .outputs = idet_outputs,
  269. .priv_class = &idet_class,
  270. };