vf_idet.c 7.8 KB

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