vf_idet.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. AVFrame *cur;
  43. AVFrame *next;
  44. AVFrame *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.04}, -1, FLT_MAX, FLAGS },
  52. { "prog_thres", "set progressive threshold", OFFSET(progressive_threshold), AV_OPT_TYPE_FLOAT, {.dbl = 1.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. int v = (*a++ + *c++) - 2 * *b++;
  72. ret += FFABS(v);
  73. }
  74. return ret;
  75. }
  76. static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
  77. {
  78. int x;
  79. int ret=0;
  80. for(x=0; x<w; x++){
  81. int v = (*a++ + *c++) - 2 * *b++;
  82. ret += FFABS(v);
  83. }
  84. return ret;
  85. }
  86. static void filter(AVFilterContext *ctx)
  87. {
  88. IDETContext *idet = ctx->priv;
  89. int y, i;
  90. int64_t alpha[2]={0};
  91. int64_t delta=0;
  92. Type type, best_type;
  93. int match = 0;
  94. for (i = 0; i < idet->csp->nb_components; i++) {
  95. int w = idet->cur->width;
  96. int h = idet->cur->height;
  97. int refs = idet->cur->linesize[i];
  98. if (i && i<3) {
  99. w = FF_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
  100. h = FF_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
  101. }
  102. for (y = 2; y < h - 2; y++) {
  103. uint8_t *prev = &idet->prev->data[i][y*refs];
  104. uint8_t *cur = &idet->cur ->data[i][y*refs];
  105. uint8_t *next = &idet->next->data[i][y*refs];
  106. alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
  107. alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
  108. delta += idet->filter_line(cur-refs, cur, cur+refs, w);
  109. }
  110. }
  111. if (alpha[0] > idet->interlace_threshold * alpha[1]){
  112. type = TFF;
  113. }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
  114. type = BFF;
  115. }else if(alpha[1] > idet->progressive_threshold * delta){
  116. type = PROGRSSIVE;
  117. }else{
  118. type = UNDETERMINED;
  119. }
  120. memmove(idet->history+1, idet->history, HIST_SIZE-1);
  121. idet->history[0] = type;
  122. best_type = UNDETERMINED;
  123. for(i=0; i<HIST_SIZE; i++){
  124. if(idet->history[i] != UNDETERMINED){
  125. if(best_type == UNDETERMINED)
  126. best_type = idet->history[i];
  127. if(idet->history[i] == best_type) {
  128. match++;
  129. }else{
  130. match=0;
  131. break;
  132. }
  133. }
  134. }
  135. if(idet->last_type == UNDETERMINED){
  136. if(match ) idet->last_type = best_type;
  137. }else{
  138. if(match>2) idet->last_type = best_type;
  139. }
  140. if (idet->last_type == TFF){
  141. idet->cur->top_field_first = 1;
  142. idet->cur->interlaced_frame = 1;
  143. }else if(idet->last_type == BFF){
  144. idet->cur->top_field_first = 0;
  145. idet->cur->interlaced_frame = 1;
  146. }else if(idet->last_type == PROGRSSIVE){
  147. idet->cur->interlaced_frame = 0;
  148. }
  149. idet->prestat [ type] ++;
  150. idet->poststat[idet->last_type] ++;
  151. av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
  152. }
  153. static int filter_frame(AVFilterLink *link, AVFrame *picref)
  154. {
  155. AVFilterContext *ctx = link->dst;
  156. IDETContext *idet = ctx->priv;
  157. if (idet->prev)
  158. av_frame_free(&idet->prev);
  159. idet->prev = idet->cur;
  160. idet->cur = idet->next;
  161. idet->next = picref;
  162. if (!idet->cur)
  163. return 0;
  164. if (!idet->prev)
  165. idet->prev = av_frame_clone(idet->cur);
  166. if (!idet->csp)
  167. idet->csp = av_pix_fmt_desc_get(link->format);
  168. if (idet->csp->comp[0].depth_minus1 / 8 == 1)
  169. idet->filter_line = (void*)filter_line_c_16bit;
  170. filter(ctx);
  171. return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
  172. }
  173. static av_cold void uninit(AVFilterContext *ctx)
  174. {
  175. IDETContext *idet = ctx->priv;
  176. av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  177. idet->prestat[TFF],
  178. idet->prestat[BFF],
  179. idet->prestat[PROGRSSIVE],
  180. idet->prestat[UNDETERMINED]
  181. );
  182. av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  183. idet->poststat[TFF],
  184. idet->poststat[BFF],
  185. idet->poststat[PROGRSSIVE],
  186. idet->poststat[UNDETERMINED]
  187. );
  188. av_frame_free(&idet->prev);
  189. av_frame_free(&idet->cur );
  190. av_frame_free(&idet->next);
  191. }
  192. static int query_formats(AVFilterContext *ctx)
  193. {
  194. static const enum AVPixelFormat pix_fmts[] = {
  195. AV_PIX_FMT_YUV420P,
  196. AV_PIX_FMT_YUV422P,
  197. AV_PIX_FMT_YUV444P,
  198. AV_PIX_FMT_YUV410P,
  199. AV_PIX_FMT_YUV411P,
  200. AV_PIX_FMT_GRAY8,
  201. AV_PIX_FMT_YUVJ420P,
  202. AV_PIX_FMT_YUVJ422P,
  203. AV_PIX_FMT_YUVJ444P,
  204. AV_PIX_FMT_GRAY16,
  205. AV_PIX_FMT_YUV440P,
  206. AV_PIX_FMT_YUVJ440P,
  207. AV_PIX_FMT_YUV420P10,
  208. AV_PIX_FMT_YUV422P10,
  209. AV_PIX_FMT_YUV444P10,
  210. AV_PIX_FMT_YUV420P16,
  211. AV_PIX_FMT_YUV422P16,
  212. AV_PIX_FMT_YUV444P16,
  213. AV_PIX_FMT_YUVA420P,
  214. AV_PIX_FMT_NONE
  215. };
  216. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  217. return 0;
  218. }
  219. static int config_output(AVFilterLink *outlink)
  220. {
  221. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  222. return 0;
  223. }
  224. static av_cold int init(AVFilterContext *ctx)
  225. {
  226. IDETContext *idet = ctx->priv;
  227. idet->last_type = UNDETERMINED;
  228. memset(idet->history, UNDETERMINED, HIST_SIZE);
  229. idet->filter_line = filter_line_c;
  230. return 0;
  231. }
  232. static const AVFilterPad idet_inputs[] = {
  233. {
  234. .name = "default",
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .filter_frame = filter_frame,
  237. },
  238. { NULL }
  239. };
  240. static const AVFilterPad idet_outputs[] = {
  241. {
  242. .name = "default",
  243. .type = AVMEDIA_TYPE_VIDEO,
  244. .config_props = config_output,
  245. },
  246. { NULL }
  247. };
  248. AVFilter ff_vf_idet = {
  249. .name = "idet",
  250. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  251. .priv_size = sizeof(IDETContext),
  252. .init = init,
  253. .uninit = uninit,
  254. .query_formats = query_formats,
  255. .inputs = idet_inputs,
  256. .outputs = idet_outputs,
  257. .priv_class = &idet_class,
  258. };