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.

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