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.

514 lines
15KB

  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  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. /**
  21. * @file
  22. * filter for selecting which frame passes in the filterchain
  23. */
  24. #include "libavutil/eval.h"
  25. #include "libavutil/fifo.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/opt.h"
  28. #include "avfilter.h"
  29. #include "audio.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. #if CONFIG_AVCODEC
  34. #include "libavcodec/dsputil.h"
  35. #endif
  36. static const char *const var_names[] = {
  37. "TB", ///< timebase
  38. "pts", ///< original pts in the file of the frame
  39. "start_pts", ///< first PTS in the stream, expressed in TB units
  40. "prev_pts", ///< previous frame PTS
  41. "prev_selected_pts", ///< previous selected frame PTS
  42. "t", ///< first PTS in seconds
  43. "start_t", ///< first PTS in the stream, expressed in seconds
  44. "prev_t", ///< previous frame time
  45. "prev_selected_t", ///< previously selected time
  46. "pict_type", ///< the type of picture in the movie
  47. "I",
  48. "P",
  49. "B",
  50. "S",
  51. "SI",
  52. "SP",
  53. "BI",
  54. "interlace_type", ///< the frame interlace type
  55. "PROGRESSIVE",
  56. "TOPFIRST",
  57. "BOTTOMFIRST",
  58. "consumed_samples_n",///< number of samples consumed by the filter (only audio)
  59. "samples_n", ///< number of samples in the current frame (only audio)
  60. "sample_rate", ///< sample rate (only audio)
  61. "n", ///< frame number (starting from zero)
  62. "selected_n", ///< selected frame number (starting from zero)
  63. "prev_selected_n", ///< number of the last selected frame
  64. "key", ///< tell if the frame is a key frame
  65. "pos", ///< original position in the file of the frame
  66. "scene",
  67. NULL
  68. };
  69. enum var_name {
  70. VAR_TB,
  71. VAR_PTS,
  72. VAR_START_PTS,
  73. VAR_PREV_PTS,
  74. VAR_PREV_SELECTED_PTS,
  75. VAR_T,
  76. VAR_START_T,
  77. VAR_PREV_T,
  78. VAR_PREV_SELECTED_T,
  79. VAR_PICT_TYPE,
  80. VAR_PICT_TYPE_I,
  81. VAR_PICT_TYPE_P,
  82. VAR_PICT_TYPE_B,
  83. VAR_PICT_TYPE_S,
  84. VAR_PICT_TYPE_SI,
  85. VAR_PICT_TYPE_SP,
  86. VAR_PICT_TYPE_BI,
  87. VAR_INTERLACE_TYPE,
  88. VAR_INTERLACE_TYPE_P,
  89. VAR_INTERLACE_TYPE_T,
  90. VAR_INTERLACE_TYPE_B,
  91. VAR_CONSUMED_SAMPLES_N,
  92. VAR_SAMPLES_N,
  93. VAR_SAMPLE_RATE,
  94. VAR_N,
  95. VAR_SELECTED_N,
  96. VAR_PREV_SELECTED_N,
  97. VAR_KEY,
  98. VAR_POS,
  99. VAR_SCENE,
  100. VAR_VARS_NB
  101. };
  102. typedef struct {
  103. const AVClass *class;
  104. AVExpr *expr;
  105. char *expr_str;
  106. double var_values[VAR_VARS_NB];
  107. int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
  108. #if CONFIG_AVCODEC
  109. AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
  110. DSPContext c; ///< context providing optimized SAD methods (scene detect only)
  111. double prev_mafd; ///< previous MAFD (scene detect only)
  112. #endif
  113. AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
  114. double select;
  115. } SelectContext;
  116. #define OFFSET(x) offsetof(SelectContext, x)
  117. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  118. static const AVOption options[] = {
  119. { "expr", "set selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = "1"}, 0, 0, FLAGS },
  120. { "e", "set selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = "1"}, 0, 0, FLAGS },
  121. {NULL},
  122. };
  123. static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
  124. {
  125. SelectContext *select = ctx->priv;
  126. const char *shorthand[] = { "expr", NULL };
  127. int ret;
  128. select->class = class;
  129. av_opt_set_defaults(select);
  130. if ((ret = av_opt_set_from_string(select, args, shorthand, "=", ":")) < 0)
  131. return ret;
  132. if ((ret = av_expr_parse(&select->expr, select->expr_str,
  133. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  134. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", select->expr_str);
  135. return ret;
  136. }
  137. select->do_scene_detect = !!strstr(select->expr_str, "scene");
  138. return 0;
  139. }
  140. #define INTERLACE_TYPE_P 0
  141. #define INTERLACE_TYPE_T 1
  142. #define INTERLACE_TYPE_B 2
  143. static int config_input(AVFilterLink *inlink)
  144. {
  145. SelectContext *select = inlink->dst->priv;
  146. select->var_values[VAR_N] = 0.0;
  147. select->var_values[VAR_SELECTED_N] = 0.0;
  148. select->var_values[VAR_TB] = av_q2d(inlink->time_base);
  149. select->var_values[VAR_PREV_PTS] = NAN;
  150. select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
  151. select->var_values[VAR_PREV_SELECTED_T] = NAN;
  152. select->var_values[VAR_PREV_T] = NAN;
  153. select->var_values[VAR_START_PTS] = NAN;
  154. select->var_values[VAR_START_T] = NAN;
  155. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  156. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  157. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  158. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  159. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  160. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  161. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  162. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
  163. select->var_values[VAR_PICT_TYPE] = NAN;
  164. select->var_values[VAR_INTERLACE_TYPE] = NAN;
  165. select->var_values[VAR_SCENE] = NAN;
  166. select->var_values[VAR_CONSUMED_SAMPLES_N] = NAN;
  167. select->var_values[VAR_SAMPLES_N] = NAN;
  168. select->var_values[VAR_SAMPLE_RATE] =
  169. inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  170. #if CONFIG_AVCODEC
  171. if (select->do_scene_detect) {
  172. select->avctx = avcodec_alloc_context3(NULL);
  173. if (!select->avctx)
  174. return AVERROR(ENOMEM);
  175. dsputil_init(&select->c, select->avctx);
  176. }
  177. #endif
  178. return 0;
  179. }
  180. #if CONFIG_AVCODEC
  181. static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
  182. {
  183. double ret = 0;
  184. SelectContext *select = ctx->priv;
  185. AVFilterBufferRef *prev_picref = select->prev_picref;
  186. if (prev_picref &&
  187. picref->video->h == prev_picref->video->h &&
  188. picref->video->w == prev_picref->video->w &&
  189. picref->linesize[0] == prev_picref->linesize[0]) {
  190. int x, y, nb_sad = 0;
  191. int64_t sad = 0;
  192. double mafd, diff;
  193. uint8_t *p1 = picref->data[0];
  194. uint8_t *p2 = prev_picref->data[0];
  195. const int linesize = picref->linesize[0];
  196. for (y = 0; y < picref->video->h - 8; y += 8) {
  197. for (x = 0; x < picref->video->w*3 - 8; x += 8) {
  198. sad += select->c.sad[1](select, p1 + x, p2 + x,
  199. linesize, 8);
  200. nb_sad += 8 * 8;
  201. }
  202. p1 += 8 * linesize;
  203. p2 += 8 * linesize;
  204. }
  205. emms_c();
  206. mafd = nb_sad ? sad / nb_sad : 0;
  207. diff = fabs(mafd - select->prev_mafd);
  208. ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
  209. select->prev_mafd = mafd;
  210. avfilter_unref_buffer(prev_picref);
  211. }
  212. select->prev_picref = avfilter_ref_buffer(picref, ~0);
  213. return ret;
  214. }
  215. #endif
  216. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  217. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  218. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *ref)
  219. {
  220. SelectContext *select = ctx->priv;
  221. AVFilterLink *inlink = ctx->inputs[0];
  222. double res;
  223. if (isnan(select->var_values[VAR_START_PTS]))
  224. select->var_values[VAR_START_PTS] = TS2D(ref->pts);
  225. if (isnan(select->var_values[VAR_START_T]))
  226. select->var_values[VAR_START_T] = TS2D(ref->pts) * av_q2d(inlink->time_base);
  227. select->var_values[VAR_PTS] = TS2D(ref->pts);
  228. select->var_values[VAR_T ] = TS2D(ref->pts) * av_q2d(inlink->time_base);
  229. select->var_values[VAR_POS] = ref->pos == -1 ? NAN : ref->pos;
  230. switch (inlink->type) {
  231. case AVMEDIA_TYPE_AUDIO:
  232. select->var_values[VAR_SAMPLES_N] = ref->audio->nb_samples;
  233. break;
  234. case AVMEDIA_TYPE_VIDEO:
  235. select->var_values[VAR_INTERLACE_TYPE] =
  236. !ref->video->interlaced ? INTERLACE_TYPE_P :
  237. ref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  238. select->var_values[VAR_PICT_TYPE] = ref->video->pict_type;
  239. #if CONFIG_AVCODEC
  240. if (select->do_scene_detect) {
  241. char buf[32];
  242. select->var_values[VAR_SCENE] = get_scene_score(ctx, ref);
  243. // TODO: document metadata
  244. snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]);
  245. av_dict_set(&ref->metadata, "lavfi.scene_score", buf, 0);
  246. }
  247. #endif
  248. break;
  249. }
  250. res = av_expr_eval(select->expr, select->var_values, NULL);
  251. av_log(inlink->dst, AV_LOG_DEBUG,
  252. "n:%f pts:%f t:%f pos:%f key:%d",
  253. select->var_values[VAR_N],
  254. select->var_values[VAR_PTS],
  255. select->var_values[VAR_T],
  256. select->var_values[VAR_POS],
  257. (int)select->var_values[VAR_KEY]);
  258. switch (inlink->type) {
  259. case AVMEDIA_TYPE_VIDEO:
  260. av_log(inlink->dst, AV_LOG_DEBUG, " interlace_type:%c pict_type:%c scene:%f",
  261. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  262. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  263. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  264. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  265. select->var_values[VAR_SCENE]);
  266. break;
  267. case AVMEDIA_TYPE_AUDIO:
  268. av_log(inlink->dst, AV_LOG_DEBUG, " samples_n:%d consumed_samples_n:%d",
  269. (int)select->var_values[VAR_SAMPLES_N],
  270. (int)select->var_values[VAR_CONSUMED_SAMPLES_N]);
  271. break;
  272. }
  273. av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f\n", res);
  274. if (res) {
  275. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  276. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  277. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  278. select->var_values[VAR_SELECTED_N] += 1.0;
  279. if (inlink->type == AVMEDIA_TYPE_AUDIO)
  280. select->var_values[VAR_CONSUMED_SAMPLES_N] += ref->audio->nb_samples;
  281. }
  282. select->var_values[VAR_N] += 1.0;
  283. select->var_values[VAR_PREV_PTS] = select->var_values[VAR_PTS];
  284. select->var_values[VAR_PREV_T] = select->var_values[VAR_T];
  285. return res;
  286. }
  287. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  288. {
  289. SelectContext *select = inlink->dst->priv;
  290. select->select = select_frame(inlink->dst, frame);
  291. if (select->select)
  292. return ff_filter_frame(inlink->dst->outputs[0], frame);
  293. avfilter_unref_bufferp(&frame);
  294. return 0;
  295. }
  296. static int request_frame(AVFilterLink *outlink)
  297. {
  298. AVFilterContext *ctx = outlink->src;
  299. SelectContext *select = ctx->priv;
  300. AVFilterLink *inlink = outlink->src->inputs[0];
  301. select->select = 0;
  302. do {
  303. int ret = ff_request_frame(inlink);
  304. if (ret < 0)
  305. return ret;
  306. } while (!select->select);
  307. return 0;
  308. }
  309. static av_cold void uninit(AVFilterContext *ctx)
  310. {
  311. SelectContext *select = ctx->priv;
  312. av_expr_free(select->expr);
  313. select->expr = NULL;
  314. av_opt_free(select);
  315. #if CONFIG_AVCODEC
  316. if (select->do_scene_detect) {
  317. avfilter_unref_bufferp(&select->prev_picref);
  318. if (select->avctx) {
  319. avcodec_close(select->avctx);
  320. av_freep(&select->avctx);
  321. }
  322. }
  323. #endif
  324. }
  325. static int query_formats(AVFilterContext *ctx)
  326. {
  327. SelectContext *select = ctx->priv;
  328. if (!select->do_scene_detect) {
  329. return ff_default_query_formats(ctx);
  330. } else {
  331. static const enum AVPixelFormat pix_fmts[] = {
  332. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  333. AV_PIX_FMT_NONE
  334. };
  335. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  336. }
  337. return 0;
  338. }
  339. #if CONFIG_ASELECT_FILTER
  340. #define aselect_options options
  341. AVFILTER_DEFINE_CLASS(aselect);
  342. static av_cold int aselect_init(AVFilterContext *ctx, const char *args)
  343. {
  344. SelectContext *select = ctx->priv;
  345. int ret;
  346. if ((ret = init(ctx, args, &aselect_class)) < 0)
  347. return ret;
  348. if (select->do_scene_detect) {
  349. av_log(ctx, AV_LOG_ERROR, "Scene detection is ignored in aselect filter\n");
  350. return AVERROR(EINVAL);
  351. }
  352. return 0;
  353. }
  354. static const AVFilterPad avfilter_af_aselect_inputs[] = {
  355. {
  356. .name = "default",
  357. .type = AVMEDIA_TYPE_AUDIO,
  358. .get_audio_buffer = ff_null_get_audio_buffer,
  359. .config_props = config_input,
  360. .filter_frame = filter_frame,
  361. },
  362. { NULL }
  363. };
  364. static const AVFilterPad avfilter_af_aselect_outputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_AUDIO,
  368. },
  369. { NULL }
  370. };
  371. AVFilter avfilter_af_aselect = {
  372. .name = "aselect",
  373. .description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."),
  374. .init = aselect_init,
  375. .uninit = uninit,
  376. .priv_size = sizeof(SelectContext),
  377. .inputs = avfilter_af_aselect_inputs,
  378. .outputs = avfilter_af_aselect_outputs,
  379. .priv_class = &aselect_class,
  380. };
  381. #endif /* CONFIG_ASELECT_FILTER */
  382. #if CONFIG_SELECT_FILTER
  383. #define select_options options
  384. AVFILTER_DEFINE_CLASS(select);
  385. static av_cold int select_init(AVFilterContext *ctx, const char *args)
  386. {
  387. SelectContext *select = ctx->priv;
  388. int ret;
  389. if ((ret = init(ctx, args, &select_class)) < 0)
  390. return ret;
  391. if (select->do_scene_detect && !CONFIG_AVCODEC) {
  392. av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
  393. return AVERROR(EINVAL);
  394. }
  395. return 0;
  396. }
  397. static const AVFilterPad avfilter_vf_select_inputs[] = {
  398. {
  399. .name = "default",
  400. .type = AVMEDIA_TYPE_VIDEO,
  401. .get_video_buffer = ff_null_get_video_buffer,
  402. .min_perms = AV_PERM_PRESERVE,
  403. .config_props = config_input,
  404. .filter_frame = filter_frame,
  405. },
  406. { NULL }
  407. };
  408. static const AVFilterPad avfilter_vf_select_outputs[] = {
  409. {
  410. .name = "default",
  411. .type = AVMEDIA_TYPE_VIDEO,
  412. .request_frame = request_frame,
  413. },
  414. { NULL }
  415. };
  416. AVFilter avfilter_vf_select = {
  417. .name = "select",
  418. .description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."),
  419. .init = select_init,
  420. .uninit = uninit,
  421. .query_formats = query_formats,
  422. .priv_size = sizeof(SelectContext),
  423. .inputs = avfilter_vf_select_inputs,
  424. .outputs = avfilter_vf_select_outputs,
  425. .priv_class = &select_class,
  426. };
  427. #endif /* CONFIG_SELECT_FILTER */