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.

369 lines
11KB

  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. { "half_life", "half life of cumulative statistics", OFFSET(half_life), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, -1, INT_MAX, FLAGS },
  32. { NULL }
  33. };
  34. AVFILTER_DEFINE_CLASS(idet);
  35. static const char *type2str(Type type)
  36. {
  37. switch(type) {
  38. case TFF : return "tff";
  39. case BFF : return "bff";
  40. case PROGRESSIVE : return "progressive";
  41. case UNDETERMINED : return "undetermined";
  42. }
  43. return NULL;
  44. }
  45. #define PRECISION 1048576
  46. static uint64_t uintpow(uint64_t b,unsigned int e)
  47. {
  48. uint64_t r=1;
  49. while(e--) r*=b;
  50. return r;
  51. }
  52. static int av_dict_set_fxp(AVDictionary **pm, const char *key, uint64_t value, unsigned int digits,
  53. int flags)
  54. {
  55. char valuestr[44];
  56. uint64_t print_precision = uintpow(10, digits);
  57. value = av_rescale(value, print_precision, PRECISION);
  58. snprintf(valuestr, sizeof(valuestr), "%"PRId64".%0*"PRId64,
  59. value / print_precision, digits, value % print_precision);
  60. return av_dict_set(pm, key, valuestr, flags);
  61. }
  62. int ff_idet_filter_line_c(const uint8_t *a, const uint8_t *b, const uint8_t *c, int w)
  63. {
  64. int x;
  65. int ret=0;
  66. for(x=0; x<w; x++){
  67. int v = (*a++ + *c++) - 2 * *b++;
  68. ret += FFABS(v);
  69. }
  70. return ret;
  71. }
  72. int ff_idet_filter_line_c_16bit(const uint16_t *a, const uint16_t *b, const uint16_t *c, int w)
  73. {
  74. int x;
  75. int ret=0;
  76. for(x=0; x<w; x++){
  77. int v = (*a++ + *c++) - 2 * *b++;
  78. ret += FFABS(v);
  79. }
  80. return ret;
  81. }
  82. static void filter(AVFilterContext *ctx)
  83. {
  84. IDETContext *idet = ctx->priv;
  85. int y, i;
  86. int64_t alpha[2]={0};
  87. int64_t delta=0;
  88. Type type, best_type;
  89. int match = 0;
  90. AVDictionary **metadata = avpriv_frame_get_metadatap(idet->cur);
  91. for (i = 0; i < idet->csp->nb_components; i++) {
  92. int w = idet->cur->width;
  93. int h = idet->cur->height;
  94. int refs = idet->cur->linesize[i];
  95. if (i && i<3) {
  96. w = FF_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
  97. h = FF_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
  98. }
  99. for (y = 2; y < h - 2; y++) {
  100. uint8_t *prev = &idet->prev->data[i][y*refs];
  101. uint8_t *cur = &idet->cur ->data[i][y*refs];
  102. uint8_t *next = &idet->next->data[i][y*refs];
  103. alpha[ y &1] += idet->filter_line(cur-refs, prev, cur+refs, w);
  104. alpha[(y^1)&1] += idet->filter_line(cur-refs, next, cur+refs, w);
  105. delta += idet->filter_line(cur-refs, cur, cur+refs, w);
  106. }
  107. }
  108. if (alpha[0] > idet->interlace_threshold * alpha[1]){
  109. type = TFF;
  110. }else if(alpha[1] > idet->interlace_threshold * alpha[0]){
  111. type = BFF;
  112. }else if(alpha[1] > idet->progressive_threshold * delta){
  113. type = PROGRESSIVE;
  114. }else{
  115. type = UNDETERMINED;
  116. }
  117. memmove(idet->history+1, idet->history, HIST_SIZE-1);
  118. idet->history[0] = type;
  119. best_type = UNDETERMINED;
  120. for(i=0; i<HIST_SIZE; i++){
  121. if(idet->history[i] != UNDETERMINED){
  122. if(best_type == UNDETERMINED)
  123. best_type = idet->history[i];
  124. if(idet->history[i] == best_type) {
  125. match++;
  126. }else{
  127. match=0;
  128. break;
  129. }
  130. }
  131. }
  132. if(idet->last_type == UNDETERMINED){
  133. if(match ) idet->last_type = best_type;
  134. }else{
  135. if(match>2) idet->last_type = best_type;
  136. }
  137. if (idet->last_type == TFF){
  138. idet->cur->top_field_first = 1;
  139. idet->cur->interlaced_frame = 1;
  140. }else if(idet->last_type == BFF){
  141. idet->cur->top_field_first = 0;
  142. idet->cur->interlaced_frame = 1;
  143. }else if(idet->last_type == PROGRESSIVE){
  144. idet->cur->interlaced_frame = 0;
  145. }
  146. for(i=0; i<4; i++){
  147. idet->prestat [i] = av_rescale(idet->prestat [i], idet->decay_coefficient, PRECISION);
  148. idet->poststat[i] = av_rescale(idet->poststat[i], idet->decay_coefficient, PRECISION);
  149. }
  150. idet->total_prestat [ type] ++;
  151. idet->prestat [ type] += PRECISION;
  152. idet->total_poststat[idet->last_type] ++;
  153. idet->poststat [idet->last_type] += PRECISION;
  154. av_log(ctx, AV_LOG_DEBUG, "Single frame:%12s, Multi frame:%12s\n", type2str(type), type2str(idet->last_type));
  155. av_dict_set (metadata, "lavfi.idet.single.current_frame", type2str(type), 0);
  156. av_dict_set_fxp(metadata, "lavfi.idet.single.tff", idet->prestat[TFF], 2 , 0);
  157. av_dict_set_fxp(metadata, "lavfi.idet.single.bff", idet->prestat[BFF], 2, 0);
  158. av_dict_set_fxp(metadata, "lavfi.idet.single.progressive", idet->prestat[PROGRESSIVE], 2, 0);
  159. av_dict_set_fxp(metadata, "lavfi.idet.single.undetermined", idet->prestat[UNDETERMINED], 2, 0);
  160. av_dict_set (metadata, "lavfi.idet.multiple.current_frame", type2str(idet->last_type), 0);
  161. av_dict_set_fxp(metadata, "lavfi.idet.multiple.tff", idet->poststat[TFF], 2, 0);
  162. av_dict_set_fxp(metadata, "lavfi.idet.multiple.bff", idet->poststat[BFF], 2, 0);
  163. av_dict_set_fxp(metadata, "lavfi.idet.multiple.progressive", idet->poststat[PROGRESSIVE], 2, 0);
  164. av_dict_set_fxp(metadata, "lavfi.idet.multiple.undetermined", idet->poststat[UNDETERMINED], 2, 0);
  165. }
  166. static int filter_frame(AVFilterLink *link, AVFrame *picref)
  167. {
  168. AVFilterContext *ctx = link->dst;
  169. IDETContext *idet = ctx->priv;
  170. if (idet->prev)
  171. av_frame_free(&idet->prev);
  172. idet->prev = idet->cur;
  173. idet->cur = idet->next;
  174. idet->next = picref;
  175. if (!idet->cur)
  176. return 0;
  177. if (!idet->prev)
  178. idet->prev = av_frame_clone(idet->cur);
  179. if (!idet->csp)
  180. idet->csp = av_pix_fmt_desc_get(link->format);
  181. if (idet->csp->comp[0].depth_minus1 / 8 == 1){
  182. idet->filter_line = (ff_idet_filter_func)ff_idet_filter_line_c_16bit;
  183. if (ARCH_X86)
  184. ff_idet_init_x86(idet, 1);
  185. }
  186. filter(ctx);
  187. return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
  188. }
  189. static int request_frame(AVFilterLink *link)
  190. {
  191. AVFilterContext *ctx = link->src;
  192. IDETContext *idet = ctx->priv;
  193. do {
  194. int ret;
  195. if (idet->eof)
  196. return AVERROR_EOF;
  197. ret = ff_request_frame(link->src->inputs[0]);
  198. if (ret == AVERROR_EOF && idet->cur) {
  199. AVFrame *next = av_frame_clone(idet->next);
  200. if (!next)
  201. return AVERROR(ENOMEM);
  202. filter_frame(link->src->inputs[0], next);
  203. idet->eof = 1;
  204. } else if (ret < 0) {
  205. return ret;
  206. }
  207. } while (!idet->cur);
  208. return 0;
  209. }
  210. static av_cold void uninit(AVFilterContext *ctx)
  211. {
  212. IDETContext *idet = ctx->priv;
  213. av_log(ctx, AV_LOG_INFO, "Single frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  214. idet->total_prestat[TFF],
  215. idet->total_prestat[BFF],
  216. idet->total_prestat[PROGRESSIVE],
  217. idet->total_prestat[UNDETERMINED]
  218. );
  219. av_log(ctx, AV_LOG_INFO, "Multi frame detection: TFF:%6"PRId64" BFF:%6"PRId64" Progressive:%6"PRId64" Undetermined:%6"PRId64"\n",
  220. idet->total_poststat[TFF],
  221. idet->total_poststat[BFF],
  222. idet->total_poststat[PROGRESSIVE],
  223. idet->total_poststat[UNDETERMINED]
  224. );
  225. av_frame_free(&idet->prev);
  226. av_frame_free(&idet->cur );
  227. av_frame_free(&idet->next);
  228. }
  229. static int query_formats(AVFilterContext *ctx)
  230. {
  231. static const enum AVPixelFormat pix_fmts[] = {
  232. AV_PIX_FMT_YUV420P,
  233. AV_PIX_FMT_YUV422P,
  234. AV_PIX_FMT_YUV444P,
  235. AV_PIX_FMT_YUV410P,
  236. AV_PIX_FMT_YUV411P,
  237. AV_PIX_FMT_GRAY8,
  238. AV_PIX_FMT_YUVJ420P,
  239. AV_PIX_FMT_YUVJ422P,
  240. AV_PIX_FMT_YUVJ444P,
  241. AV_PIX_FMT_GRAY16,
  242. AV_PIX_FMT_YUV440P,
  243. AV_PIX_FMT_YUVJ440P,
  244. AV_PIX_FMT_YUV420P10,
  245. AV_PIX_FMT_YUV422P10,
  246. AV_PIX_FMT_YUV444P10,
  247. AV_PIX_FMT_YUV420P16,
  248. AV_PIX_FMT_YUV422P16,
  249. AV_PIX_FMT_YUV444P16,
  250. AV_PIX_FMT_YUVA420P,
  251. AV_PIX_FMT_NONE
  252. };
  253. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  254. return 0;
  255. }
  256. static int config_output(AVFilterLink *outlink)
  257. {
  258. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  259. return 0;
  260. }
  261. static av_cold int init(AVFilterContext *ctx)
  262. {
  263. IDETContext *idet = ctx->priv;
  264. idet->eof = 0;
  265. idet->last_type = UNDETERMINED;
  266. memset(idet->history, UNDETERMINED, HIST_SIZE);
  267. if( idet->half_life > 0 )
  268. idet->decay_coefficient = (uint64_t) round( PRECISION * exp2(-1.0 / idet->half_life) );
  269. else
  270. idet->decay_coefficient = PRECISION;
  271. idet->filter_line = ff_idet_filter_line_c;
  272. if (ARCH_X86)
  273. ff_idet_init_x86(idet, 0);
  274. return 0;
  275. }
  276. static const AVFilterPad idet_inputs[] = {
  277. {
  278. .name = "default",
  279. .type = AVMEDIA_TYPE_VIDEO,
  280. .filter_frame = filter_frame,
  281. },
  282. { NULL }
  283. };
  284. static const AVFilterPad idet_outputs[] = {
  285. {
  286. .name = "default",
  287. .type = AVMEDIA_TYPE_VIDEO,
  288. .config_props = config_output,
  289. .request_frame = request_frame
  290. },
  291. { NULL }
  292. };
  293. AVFilter ff_vf_idet = {
  294. .name = "idet",
  295. .description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
  296. .priv_size = sizeof(IDETContext),
  297. .init = init,
  298. .uninit = uninit,
  299. .query_formats = query_formats,
  300. .inputs = idet_inputs,
  301. .outputs = idet_outputs,
  302. .priv_class = &idet_class,
  303. };