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.

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