f_select.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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_START_PTS] = NAN;
  153. select->var_values[VAR_START_T] = NAN;
  154. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  155. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  156. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  157. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  158. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  159. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  160. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  161. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
  162. select->var_values[VAR_PICT_TYPE] = NAN;
  163. select->var_values[VAR_INTERLACE_TYPE] = NAN;
  164. select->var_values[VAR_SCENE] = NAN;
  165. select->var_values[VAR_CONSUMED_SAMPLES_N] = NAN;
  166. select->var_values[VAR_SAMPLES_N] = NAN;
  167. select->var_values[VAR_SAMPLE_RATE] =
  168. inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  169. #if CONFIG_AVCODEC
  170. if (select->do_scene_detect) {
  171. select->avctx = avcodec_alloc_context3(NULL);
  172. if (!select->avctx)
  173. return AVERROR(ENOMEM);
  174. dsputil_init(&select->c, select->avctx);
  175. }
  176. #endif
  177. return 0;
  178. }
  179. #if CONFIG_AVCODEC
  180. static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
  181. {
  182. double ret = 0;
  183. SelectContext *select = ctx->priv;
  184. AVFilterBufferRef *prev_picref = select->prev_picref;
  185. if (prev_picref &&
  186. picref->video->h == prev_picref->video->h &&
  187. picref->video->w == prev_picref->video->w &&
  188. picref->linesize[0] == prev_picref->linesize[0]) {
  189. int x, y, nb_sad = 0;
  190. int64_t sad = 0;
  191. double mafd, diff;
  192. uint8_t *p1 = picref->data[0];
  193. uint8_t *p2 = prev_picref->data[0];
  194. const int linesize = picref->linesize[0];
  195. for (y = 0; y < picref->video->h - 8; y += 8) {
  196. for (x = 0; x < picref->video->w*3 - 8; x += 8) {
  197. sad += select->c.sad[1](select, p1 + x, p2 + x,
  198. linesize, 8);
  199. nb_sad += 8 * 8;
  200. }
  201. p1 += 8 * linesize;
  202. p2 += 8 * linesize;
  203. }
  204. emms_c();
  205. mafd = nb_sad ? sad / nb_sad : 0;
  206. diff = fabs(mafd - select->prev_mafd);
  207. ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
  208. select->prev_mafd = mafd;
  209. avfilter_unref_buffer(prev_picref);
  210. }
  211. select->prev_picref = avfilter_ref_buffer(picref, ~0);
  212. return ret;
  213. }
  214. #endif
  215. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  216. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  217. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *ref)
  218. {
  219. SelectContext *select = ctx->priv;
  220. AVFilterLink *inlink = ctx->inputs[0];
  221. double res;
  222. if (isnan(select->var_values[VAR_START_PTS]))
  223. select->var_values[VAR_START_PTS] = TS2D(ref->pts);
  224. if (isnan(select->var_values[VAR_START_T]))
  225. select->var_values[VAR_START_T] = TS2D(ref->pts) * av_q2d(inlink->time_base);
  226. select->var_values[VAR_PTS] = TS2D(ref->pts);
  227. select->var_values[VAR_T ] = TS2D(ref->pts) * av_q2d(inlink->time_base);
  228. select->var_values[VAR_POS] = ref->pos == -1 ? NAN : ref->pos;
  229. select->var_values[VAR_PREV_PTS] = TS2D(ref ->pts);
  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. switch (inlink->type) {
  252. case AVMEDIA_TYPE_VIDEO:
  253. av_log(inlink->dst, AV_LOG_DEBUG, " interlace_type:%c pict_type:%c scene:%f",
  254. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  255. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  256. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  257. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  258. select->var_values[VAR_SCENE]);
  259. break;
  260. case AVMEDIA_TYPE_AUDIO:
  261. av_log(inlink->dst, AV_LOG_DEBUG, " samples_n:%d consumed_samples_n:%d",
  262. (int)select->var_values[VAR_SAMPLES_N],
  263. (int)select->var_values[VAR_CONSUMED_SAMPLES_N]);
  264. break;
  265. }
  266. av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f\n", res);
  267. if (res) {
  268. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  269. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  270. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  271. select->var_values[VAR_SELECTED_N] += 1.0;
  272. if (inlink->type == AVMEDIA_TYPE_AUDIO)
  273. select->var_values[VAR_CONSUMED_SAMPLES_N] += ref->audio->nb_samples;
  274. }
  275. select->var_values[VAR_N] += 1.0;
  276. return res;
  277. }
  278. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  279. {
  280. SelectContext *select = inlink->dst->priv;
  281. select->select = select_frame(inlink->dst, frame);
  282. if (select->select)
  283. return ff_filter_frame(inlink->dst->outputs[0], frame);
  284. avfilter_unref_bufferp(&frame);
  285. return 0;
  286. }
  287. static int request_frame(AVFilterLink *outlink)
  288. {
  289. AVFilterContext *ctx = outlink->src;
  290. SelectContext *select = ctx->priv;
  291. AVFilterLink *inlink = outlink->src->inputs[0];
  292. select->select = 0;
  293. do {
  294. int ret = ff_request_frame(inlink);
  295. if (ret < 0)
  296. return ret;
  297. } while (!select->select);
  298. return 0;
  299. }
  300. static av_cold void uninit(AVFilterContext *ctx)
  301. {
  302. SelectContext *select = ctx->priv;
  303. av_expr_free(select->expr);
  304. select->expr = NULL;
  305. av_opt_free(select);
  306. #if CONFIG_AVCODEC
  307. if (select->do_scene_detect) {
  308. avfilter_unref_bufferp(&select->prev_picref);
  309. if (select->avctx) {
  310. avcodec_close(select->avctx);
  311. av_freep(&select->avctx);
  312. }
  313. }
  314. #endif
  315. }
  316. static int query_formats(AVFilterContext *ctx)
  317. {
  318. SelectContext *select = ctx->priv;
  319. if (!select->do_scene_detect) {
  320. return ff_default_query_formats(ctx);
  321. } else {
  322. static const enum AVPixelFormat pix_fmts[] = {
  323. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  324. AV_PIX_FMT_NONE
  325. };
  326. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  327. }
  328. return 0;
  329. }
  330. #if CONFIG_ASELECT_FILTER
  331. #define aselect_options options
  332. AVFILTER_DEFINE_CLASS(aselect);
  333. static av_cold int aselect_init(AVFilterContext *ctx, const char *args)
  334. {
  335. SelectContext *select = ctx->priv;
  336. int ret;
  337. if ((ret = init(ctx, args, &aselect_class)) < 0)
  338. return ret;
  339. if (select->do_scene_detect) {
  340. av_log(ctx, AV_LOG_ERROR, "Scene detection is ignored in aselect filter\n");
  341. return AVERROR(EINVAL);
  342. }
  343. return 0;
  344. }
  345. static const AVFilterPad avfilter_af_aselect_inputs[] = {
  346. {
  347. .name = "default",
  348. .type = AVMEDIA_TYPE_AUDIO,
  349. .get_audio_buffer = ff_null_get_audio_buffer,
  350. .config_props = config_input,
  351. .filter_frame = filter_frame,
  352. },
  353. { NULL }
  354. };
  355. static const AVFilterPad avfilter_af_aselect_outputs[] = {
  356. {
  357. .name = "default",
  358. .type = AVMEDIA_TYPE_AUDIO,
  359. },
  360. { NULL }
  361. };
  362. AVFilter avfilter_af_aselect = {
  363. .name = "aselect",
  364. .description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."),
  365. .init = aselect_init,
  366. .uninit = uninit,
  367. .priv_size = sizeof(SelectContext),
  368. .inputs = avfilter_af_aselect_inputs,
  369. .outputs = avfilter_af_aselect_outputs,
  370. .priv_class = &aselect_class,
  371. };
  372. #endif /* CONFIG_ASELECT_FILTER */
  373. #if CONFIG_SELECT_FILTER
  374. #define select_options options
  375. AVFILTER_DEFINE_CLASS(select);
  376. static av_cold int select_init(AVFilterContext *ctx, const char *args)
  377. {
  378. SelectContext *select = ctx->priv;
  379. int ret;
  380. if ((ret = init(ctx, args, &select_class)) < 0)
  381. return ret;
  382. if (select->do_scene_detect && !CONFIG_AVCODEC) {
  383. av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
  384. return AVERROR(EINVAL);
  385. }
  386. return 0;
  387. }
  388. static const AVFilterPad avfilter_vf_select_inputs[] = {
  389. {
  390. .name = "default",
  391. .type = AVMEDIA_TYPE_VIDEO,
  392. .get_video_buffer = ff_null_get_video_buffer,
  393. .min_perms = AV_PERM_PRESERVE,
  394. .config_props = config_input,
  395. .filter_frame = filter_frame,
  396. },
  397. { NULL }
  398. };
  399. static const AVFilterPad avfilter_vf_select_outputs[] = {
  400. {
  401. .name = "default",
  402. .type = AVMEDIA_TYPE_VIDEO,
  403. .request_frame = request_frame,
  404. },
  405. { NULL }
  406. };
  407. AVFilter avfilter_vf_select = {
  408. .name = "select",
  409. .description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."),
  410. .init = select_init,
  411. .uninit = uninit,
  412. .query_formats = query_formats,
  413. .priv_size = sizeof(SelectContext),
  414. .inputs = avfilter_vf_select_inputs,
  415. .outputs = avfilter_vf_select_outputs,
  416. .priv_class = &select_class,
  417. };
  418. #endif /* CONFIG_SELECT_FILTER */