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.

518 lines
16KB

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