f_select.c 16 KB

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