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.

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