f_select.c 16 KB

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