You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
9.2KB

  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 PROGRESSIVE : 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 = PROGRESSIVE;
  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 == PROGRESSIVE){
  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. AVDictionary **metadata = avpriv_frame_get_metadatap(picref);
  136. if (idet->prev)
  137. av_frame_free(&idet->prev);
  138. idet->prev = idet->cur;
  139. idet->cur = idet->next;
  140. idet->next = picref;
  141. if (!idet->cur)
  142. return 0;
  143. if (!idet->prev)
  144. idet->prev = av_frame_clone(idet->cur);
  145. if (!idet->csp)
  146. idet->csp = av_pix_fmt_desc_get(link->format);
  147. if (idet->csp->comp[0].depth_minus1 / 8 == 1){
  148. idet->filter_line = (ff_idet_filter_func)ff_idet_filter_line_c_16bit;
  149. if (ARCH_X86)
  150. ff_idet_init_x86(idet, 1);
  151. }
  152. filter(ctx);
  153. av_dict_set_int(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 0);
  154. av_dict_set_int(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 0);
  155. av_dict_set_int(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 0);
  156. av_dict_set_int(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 0);
  157. av_dict_set_int(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 0);
  158. av_dict_set_int(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 0);
  159. av_dict_set_int(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 0);
  160. av_dict_set_int(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 0);
  161. return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
  162. }
  163. static int request_frame(AVFilterLink *link)
  164. {
  165. AVFilterContext *ctx = link->src;
  166. IDETContext *idet = ctx->priv;
  167. do {
  168. int ret;
  169. if (idet->eof)
  170. return AVERROR_EOF;
  171. ret = ff_request_frame(link->src->inputs[0]);
  172. if (ret == AVERROR_EOF && idet->cur) {
  173. AVFrame *next = av_frame_clone(idet->next);
  174. if (!next)
  175. return AVERROR(ENOMEM);
  176. filter_frame(link->src->inputs[0], next);
  177. idet->eof = 1;
  178. } else if (ret < 0) {
  179. return ret;
  180. }
  181. } while (!idet->cur);
  182. return 0;
  183. }
  184. static av_cold void uninit(AVFilterContext *ctx)
  185. {
  186. IDETContext *idet = ctx->priv;
  187. av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  188. idet->prestat[TFF],
  189. idet->prestat[BFF],
  190. idet->prestat[PROGRESSIVE],
  191. idet->prestat[UNDETERMINED]
  192. );
  193. av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%d BFF:%d Progressive:%d Undetermined:%d\n",
  194. idet->poststat[TFF],
  195. idet->poststat[BFF],
  196. idet->poststat[PROGRESSIVE],
  197. idet->poststat[UNDETERMINED]
  198. );
  199. av_frame_free(&idet->prev);
  200. av_frame_free(&idet->cur );
  201. av_frame_free(&idet->next);
  202. }
  203. static int query_formats(AVFilterContext *ctx)
  204. {
  205. static const enum AVPixelFormat pix_fmts[] = {
  206. AV_PIX_FMT_YUV420P,
  207. AV_PIX_FMT_YUV422P,
  208. AV_PIX_FMT_YUV444P,
  209. AV_PIX_FMT_YUV410P,
  210. AV_PIX_FMT_YUV411P,
  211. AV_PIX_FMT_GRAY8,
  212. AV_PIX_FMT_YUVJ420P,
  213. AV_PIX_FMT_YUVJ422P,
  214. AV_PIX_FMT_YUVJ444P,
  215. AV_PIX_FMT_GRAY16,
  216. AV_PIX_FMT_YUV440P,
  217. AV_PIX_FMT_YUVJ440P,
  218. AV_PIX_FMT_YUV420P10,
  219. AV_PIX_FMT_YUV422P10,
  220. AV_PIX_FMT_YUV444P10,
  221. AV_PIX_FMT_YUV420P16,
  222. AV_PIX_FMT_YUV422P16,
  223. AV_PIX_FMT_YUV444P16,
  224. AV_PIX_FMT_YUVA420P,
  225. AV_PIX_FMT_NONE
  226. };
  227. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  228. return 0;
  229. }
  230. static int config_output(AVFilterLink *outlink)
  231. {
  232. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  233. return 0;
  234. }
  235. static av_cold int init(AVFilterContext *ctx)
  236. {
  237. IDETContext *idet = ctx->priv;
  238. idet->eof = 0;
  239. idet->last_type = UNDETERMINED;
  240. memset(idet->history, UNDETERMINED, HIST_SIZE);
  241. idet->filter_line = ff_idet_filter_line_c;
  242. if (ARCH_X86)
  243. ff_idet_init_x86(idet, 0);
  244. return 0;
  245. }
  246. static const AVFilterPad idet_inputs[] = {
  247. {
  248. .name = "default",
  249. .type = AVMEDIA_TYPE_VIDEO,
  250. .filter_frame = filter_frame,
  251. },
  252. { NULL }
  253. };
  254. static const AVFilterPad idet_outputs[] = {
  255. {
  256. .name = "default",
  257. .type = AVMEDIA_TYPE_VIDEO,
  258. .config_props = config_output,
  259. .request_frame = request_frame
  260. },
  261. { NULL }
  262. };
  263. AVFilter ff_vf_idet = {
  264. .name = "idet",
  265. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  266. .priv_size = sizeof(IDETContext),
  267. .init = init,
  268. .uninit = uninit,
  269. .query_formats = query_formats,
  270. .inputs = idet_inputs,
  271. .outputs = idet_outputs,
  272. .priv_class = &idet_class,
  273. };