vf_idet.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "libavutil/cpu.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "internal.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. #define HIST_SIZE 4
  28. typedef enum {
  29. TFF,
  30. BFF,
  31. PROGRSSIVE,
  32. UNDETERMINED,
  33. } Type;
  34. typedef struct {
  35. float interlace_threshold;
  36. float progressive_threshold;
  37. Type last_type;
  38. Type prestat[4];
  39. Type poststat[4];
  40. uint8_t history[HIST_SIZE];
  41. AVFilterBufferRef *cur;
  42. AVFilterBufferRef *next;
  43. AVFilterBufferRef *prev;
  44. AVFilterBufferRef *out;
  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. static const char *type2str(Type type)
  49. {
  50. switch(type) {
  51. case TFF : return "Top Field First ";
  52. case BFF : return "Bottom Field First";
  53. case PROGRSSIVE : return "Progressive ";
  54. case UNDETERMINED: return "Undetermined ";
  55. }
  56. return NULL;
  57. }
  58. static int filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
  59. {
  60. int x;
  61. int ret=0;
  62. for(x=0; x<w; x++){
  63. ret += FFABS((*a++ + *c++) - 2 * *b++);
  64. }
  65. return ret;
  66. }
  67. static int filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
  68. {
  69. int x;
  70. int ret=0;
  71. for(x=0; x<w; x++){
  72. ret += FFABS((*a++ + *c++) - 2 * *b++);
  73. }
  74. return ret;
  75. }
  76. static void filter(AVFilterContext *ctx)
  77. {
  78. IDETContext *idet = ctx->priv;
  79. int y, i;
  80. int64_t alpha[2]={0};
  81. int64_t delta=0;
  82. Type type, best_type;
  83. int match = 0;
  84. for (i = 0; i < idet->csp->nb_components; i++) {
  85. int w = idet->cur->video->w;
  86. int h = idet->cur->video->h;
  87. int refs = idet->cur->linesize[i];
  88. if (i && i<3) {
  89. w >>= idet->csp->log2_chroma_w;
  90. h >>= idet->csp->log2_chroma_h;
  91. }
  92. for (y = 2; y < h - 2; y++) {
  93. uint8_t *prev = &idet->prev->data[i][y*refs];
  94. uint8_t *cur = &idet->cur ->data[i][y*refs];
  95. uint8_t *next = &idet->next->data[i][y*refs];
  96. alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
  97. alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
  98. delta += idet->filter_line(cur-refs, cur, cur+refs, w);
  99. }
  100. }
  101. if (alpha[0] / (float)alpha[1] > idet->interlace_threshold){
  102. type = TFF;
  103. }else if(alpha[1] / (float)alpha[0] > idet->interlace_threshold){
  104. type = BFF;
  105. }else if(alpha[1] / (float)delta > idet->progressive_threshold){
  106. type = PROGRSSIVE;
  107. }else{
  108. type = UNDETERMINED;
  109. }
  110. memmove(idet->history+1, idet->history, HIST_SIZE-1);
  111. idet->history[0] = type;
  112. best_type = UNDETERMINED;
  113. for(i=0; i<HIST_SIZE; i++){
  114. if(idet->history[i] != UNDETERMINED){
  115. if(best_type == UNDETERMINED)
  116. best_type = idet->history[i];
  117. if(idet->history[i] == best_type) {
  118. match++;
  119. }else{
  120. match=0;
  121. break;
  122. }
  123. }
  124. }
  125. if(idet->last_type == UNDETERMINED){
  126. if(match ) idet->last_type = best_type;
  127. }else{
  128. if(match>2) idet->last_type = best_type;
  129. }
  130. if (idet->last_type == TFF){
  131. idet->cur->video->top_field_first = 1;
  132. idet->cur->video->interlaced = 1;
  133. }else if(idet->last_type == BFF){
  134. idet->cur->video->top_field_first = 0;
  135. idet->cur->video->interlaced = 1;
  136. }else if(idet->last_type == PROGRSSIVE){
  137. idet->cur->video->interlaced = 0;
  138. }
  139. idet->prestat [ type] ++;
  140. idet->poststat[idet->last_type] ++;
  141. av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
  142. }
  143. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  144. {
  145. AVFilterContext *ctx = link->dst;
  146. IDETContext *idet = ctx->priv;
  147. if (idet->prev)
  148. avfilter_unref_buffer(idet->prev);
  149. idet->prev = idet->cur;
  150. idet->cur = idet->next;
  151. idet->next = picref;
  152. link->cur_buf = NULL;
  153. if (!idet->cur)
  154. return 0;
  155. if (!idet->prev)
  156. idet->prev = avfilter_ref_buffer(idet->cur, ~0);
  157. return ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
  158. }
  159. static int end_frame(AVFilterLink *link)
  160. {
  161. AVFilterContext *ctx = link->dst;
  162. IDETContext *idet = ctx->priv;
  163. if (!idet->cur)
  164. return 0;
  165. if (!idet->csp)
  166. idet->csp = &av_pix_fmt_descriptors[link->format];
  167. if (idet->csp->comp[0].depth_minus1 / 8 == 1)
  168. idet->filter_line = (void*)filter_line_c_16bit;
  169. filter(ctx);
  170. ff_draw_slice(ctx->outputs[0], 0, link->h, 1);
  171. return ff_end_frame(ctx->outputs[0]);
  172. }
  173. static int request_frame(AVFilterLink *link)
  174. {
  175. AVFilterContext *ctx = link->src;
  176. IDETContext *idet = ctx->priv;
  177. do {
  178. int ret;
  179. if ((ret = ff_request_frame(link->src->inputs[0])))
  180. return ret;
  181. } while (!idet->cur);
  182. return 0;
  183. }
  184. static int poll_frame(AVFilterLink *link)
  185. {
  186. IDETContext *idet = link->src->priv;
  187. int ret, val;
  188. val = ff_poll_frame(link->src->inputs[0]);
  189. if (val >= 1 && !idet->next) { //FIXME change API to not requre this red tape
  190. if ((ret = ff_request_frame(link->src->inputs[0])) < 0)
  191. return ret;
  192. val = ff_poll_frame(link->src->inputs[0]);
  193. }
  194. assert(idet->next || !val);
  195. return val;
  196. }
  197. static av_cold void uninit(AVFilterContext *ctx)
  198. {
  199. IDETContext *idet = ctx->priv;
  200. av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  201. idet->prestat[TFF],
  202. idet->prestat[BFF],
  203. idet->prestat[PROGRSSIVE],
  204. idet->prestat[UNDETERMINED]
  205. );
  206. av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  207. idet->poststat[TFF],
  208. idet->poststat[BFF],
  209. idet->poststat[PROGRSSIVE],
  210. idet->poststat[UNDETERMINED]
  211. );
  212. if (idet->prev) avfilter_unref_buffer(idet->prev);
  213. if (idet->cur ) avfilter_unref_buffer(idet->cur );
  214. if (idet->next) avfilter_unref_buffer(idet->next);
  215. }
  216. static int query_formats(AVFilterContext *ctx)
  217. {
  218. static const enum PixelFormat pix_fmts[] = {
  219. PIX_FMT_YUV420P,
  220. PIX_FMT_YUV422P,
  221. PIX_FMT_YUV444P,
  222. PIX_FMT_YUV410P,
  223. PIX_FMT_YUV411P,
  224. PIX_FMT_GRAY8,
  225. PIX_FMT_YUVJ420P,
  226. PIX_FMT_YUVJ422P,
  227. PIX_FMT_YUVJ444P,
  228. AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
  229. PIX_FMT_YUV440P,
  230. PIX_FMT_YUVJ440P,
  231. AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
  232. AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
  233. AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
  234. AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
  235. AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
  236. AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
  237. PIX_FMT_YUVA420P,
  238. PIX_FMT_NONE
  239. };
  240. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  241. return 0;
  242. }
  243. static av_cold int init(AVFilterContext *ctx, const char *args)
  244. {
  245. IDETContext *idet = ctx->priv;
  246. idet->csp = NULL;
  247. idet->interlace_threshold = 1.01;
  248. idet->progressive_threshold = 2.5;
  249. if (args) sscanf(args, "%f:%f", &idet->interlace_threshold, &idet->progressive_threshold);
  250. idet->last_type = UNDETERMINED;
  251. memset(idet->history, UNDETERMINED, HIST_SIZE);
  252. idet->filter_line = filter_line_c;
  253. return 0;
  254. }
  255. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
  256. AVFilter avfilter_vf_idet = {
  257. .name = "idet",
  258. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  259. .priv_size = sizeof(IDETContext),
  260. .init = init,
  261. .uninit = uninit,
  262. .query_formats = query_formats,
  263. .inputs = (const AVFilterPad[]) {{ .name = "default",
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .start_frame = start_frame,
  266. .draw_slice = null_draw_slice,
  267. .end_frame = end_frame,
  268. .min_perms = AV_PERM_PRESERVE },
  269. { .name = NULL}},
  270. .outputs = (const AVFilterPad[]) {{ .name = "default",
  271. .type = AVMEDIA_TYPE_VIDEO,
  272. .rej_perms = AV_PERM_WRITE,
  273. .poll_frame = poll_frame,
  274. .request_frame = request_frame, },
  275. { .name = NULL}},
  276. };