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.

442 lines
14KB

  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 "avfilter.h"
  28. #include "audio.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #if CONFIG_AVCODEC
  33. #include "libavcodec/dsputil.h"
  34. #endif
  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", ///< first PTS 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. "interlace_type", ///< the frame interlace type
  54. "PROGRESSIVE",
  55. "TOPFIRST",
  56. "BOTTOMFIRST",
  57. "consumed_samples_n",///< number of samples consumed by the filter (only audio)
  58. "samples_n", ///< number of samples in the current frame (only audio)
  59. "sample_rate", ///< sample rate (only audio)
  60. "n", ///< frame number (starting from zero)
  61. "selected_n", ///< selected frame number (starting from zero)
  62. "prev_selected_n", ///< number of the last selected frame
  63. "key", ///< tell if the frame is a key frame
  64. "pos", ///< original position in the file of the frame
  65. "scene",
  66. NULL
  67. };
  68. enum var_name {
  69. VAR_TB,
  70. VAR_PTS,
  71. VAR_START_PTS,
  72. VAR_PREV_PTS,
  73. VAR_PREV_SELECTED_PTS,
  74. VAR_T,
  75. VAR_START_T,
  76. VAR_PREV_T,
  77. VAR_PREV_SELECTED_T,
  78. VAR_PICT_TYPE,
  79. VAR_PICT_TYPE_I,
  80. VAR_PICT_TYPE_P,
  81. VAR_PICT_TYPE_B,
  82. VAR_PICT_TYPE_S,
  83. VAR_PICT_TYPE_SI,
  84. VAR_PICT_TYPE_SP,
  85. VAR_PICT_TYPE_BI,
  86. VAR_INTERLACE_TYPE,
  87. VAR_INTERLACE_TYPE_P,
  88. VAR_INTERLACE_TYPE_T,
  89. VAR_INTERLACE_TYPE_B,
  90. VAR_CONSUMED_SAMPLES_N,
  91. VAR_SAMPLES_N,
  92. VAR_SAMPLE_RATE,
  93. VAR_N,
  94. VAR_SELECTED_N,
  95. VAR_PREV_SELECTED_N,
  96. VAR_KEY,
  97. VAR_POS,
  98. VAR_SCENE,
  99. VAR_VARS_NB
  100. };
  101. typedef struct {
  102. AVExpr *expr;
  103. double var_values[VAR_VARS_NB];
  104. int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
  105. #if CONFIG_AVCODEC
  106. AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
  107. DSPContext c; ///< context providing optimized SAD methods (scene detect only)
  108. double prev_mafd; ///< previous MAFD (scene detect only)
  109. #endif
  110. AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
  111. double select;
  112. } SelectContext;
  113. static av_cold int init(AVFilterContext *ctx, const char *args)
  114. {
  115. SelectContext *select = ctx->priv;
  116. int ret;
  117. if ((ret = av_expr_parse(&select->expr, args ? args : "1",
  118. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  119. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  120. return ret;
  121. }
  122. select->do_scene_detect = args && strstr(args, "scene");
  123. if (select->do_scene_detect && !CONFIG_AVCODEC) {
  124. av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
  125. return AVERROR(EINVAL);
  126. }
  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_START_PTS] = NAN;
  142. select->var_values[VAR_START_T] = NAN;
  143. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  144. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  145. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  146. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  147. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  148. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  149. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  150. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
  151. select->var_values[VAR_SAMPLE_RATE] =
  152. inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  153. if (CONFIG_AVCODEC && select->do_scene_detect) {
  154. select->avctx = avcodec_alloc_context3(NULL);
  155. if (!select->avctx)
  156. return AVERROR(ENOMEM);
  157. dsputil_init(&select->c, select->avctx);
  158. }
  159. return 0;
  160. }
  161. #if CONFIG_AVCODEC
  162. static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
  163. {
  164. double ret = 0;
  165. SelectContext *select = ctx->priv;
  166. AVFilterBufferRef *prev_picref = select->prev_picref;
  167. if (prev_picref &&
  168. picref->video->h == prev_picref->video->h &&
  169. picref->video->w == prev_picref->video->w &&
  170. picref->linesize[0] == prev_picref->linesize[0]) {
  171. int x, y, nb_sad = 0;
  172. int64_t sad = 0;
  173. double mafd, diff;
  174. uint8_t *p1 = picref->data[0];
  175. uint8_t *p2 = prev_picref->data[0];
  176. const int linesize = picref->linesize[0];
  177. for (y = 0; y < picref->video->h - 8; y += 8) {
  178. for (x = 0; x < picref->video->w*3 - 8; x += 8) {
  179. sad += select->c.sad[1](select, p1 + x, p2 + x,
  180. linesize, 8);
  181. nb_sad += 8 * 8;
  182. }
  183. p1 += 8 * linesize;
  184. p2 += 8 * linesize;
  185. }
  186. emms_c();
  187. mafd = nb_sad ? sad / nb_sad : 0;
  188. diff = fabs(mafd - select->prev_mafd);
  189. ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
  190. select->prev_mafd = mafd;
  191. avfilter_unref_buffer(prev_picref);
  192. }
  193. select->prev_picref = avfilter_ref_buffer(picref, ~0);
  194. return ret;
  195. }
  196. #endif
  197. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  198. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  199. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
  200. {
  201. SelectContext *select = ctx->priv;
  202. AVFilterLink *inlink = ctx->inputs[0];
  203. double res;
  204. if (isnan(select->var_values[VAR_START_PTS]))
  205. select->var_values[VAR_START_PTS] = TS2D(picref->pts);
  206. if (isnan(select->var_values[VAR_START_T]))
  207. select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  208. select->var_values[VAR_PTS] = TS2D(picref->pts);
  209. select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  210. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  211. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  212. switch (inlink->type) {
  213. case AVMEDIA_TYPE_AUDIO:
  214. select->var_values[VAR_SAMPLES_N] = picref->audio->nb_samples;
  215. break;
  216. case AVMEDIA_TYPE_VIDEO:
  217. select->var_values[VAR_INTERLACE_TYPE] =
  218. !picref->video->interlaced ? INTERLACE_TYPE_P :
  219. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  220. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  221. if (CONFIG_AVCODEC && select->do_scene_detect) {
  222. char buf[32];
  223. select->var_values[VAR_SCENE] = get_scene_score(ctx, picref);
  224. // TODO: document metadata
  225. snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]);
  226. av_dict_set(&picref->metadata, "lavfi.scene_score", buf, 0);
  227. }
  228. break;
  229. }
  230. res = av_expr_eval(select->expr, select->var_values, NULL);
  231. av_log(inlink->dst, AV_LOG_DEBUG,
  232. "n:%d pts:%d t:%f pos:%d key:%d",
  233. (int)select->var_values[VAR_N],
  234. (int)select->var_values[VAR_PTS],
  235. select->var_values[VAR_T],
  236. (int)select->var_values[VAR_POS],
  237. (int)select->var_values[VAR_KEY]);
  238. switch (inlink->type) {
  239. case AVMEDIA_TYPE_VIDEO:
  240. av_log(inlink->dst, AV_LOG_DEBUG, " interlace_type:%c pict_type:%c",
  241. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  242. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  243. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  244. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]));
  245. break;
  246. case AVMEDIA_TYPE_AUDIO:
  247. av_log(inlink->dst, AV_LOG_DEBUG, " samples_n:%d consumed_samples_n:%d",
  248. (int)select->var_values[VAR_SAMPLES_N],
  249. (int)select->var_values[VAR_CONSUMED_SAMPLES_N]);
  250. break;
  251. }
  252. av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f\n", res);
  253. if (res) {
  254. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  255. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  256. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  257. select->var_values[VAR_SELECTED_N] += 1.0;
  258. if (inlink->type == AVMEDIA_TYPE_AUDIO)
  259. select->var_values[VAR_CONSUMED_SAMPLES_N] += picref->audio->nb_samples;
  260. }
  261. select->var_values[VAR_N] += 1.0;
  262. return res;
  263. }
  264. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  265. {
  266. SelectContext *select = inlink->dst->priv;
  267. select->select = select_frame(inlink->dst, frame);
  268. if (select->select)
  269. return ff_filter_frame(inlink->dst->outputs[0], frame);
  270. avfilter_unref_bufferp(&frame);
  271. return 0;
  272. }
  273. static int request_frame(AVFilterLink *outlink)
  274. {
  275. AVFilterContext *ctx = outlink->src;
  276. SelectContext *select = ctx->priv;
  277. AVFilterLink *inlink = outlink->src->inputs[0];
  278. select->select = 0;
  279. do {
  280. int ret = ff_request_frame(inlink);
  281. if (ret < 0)
  282. return ret;
  283. } while (!select->select);
  284. return 0;
  285. }
  286. static av_cold void uninit(AVFilterContext *ctx)
  287. {
  288. SelectContext *select = ctx->priv;
  289. av_expr_free(select->expr);
  290. select->expr = NULL;
  291. if (select->do_scene_detect) {
  292. avfilter_unref_bufferp(&select->prev_picref);
  293. if (select->avctx) {
  294. avcodec_close(select->avctx);
  295. av_freep(&select->avctx);
  296. }
  297. }
  298. }
  299. static int query_formats(AVFilterContext *ctx)
  300. {
  301. SelectContext *select = ctx->priv;
  302. if (!select->do_scene_detect) {
  303. return ff_default_query_formats(ctx);
  304. } else {
  305. static const enum AVPixelFormat pix_fmts[] = {
  306. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  307. AV_PIX_FMT_NONE
  308. };
  309. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  310. }
  311. return 0;
  312. }
  313. #if CONFIG_ASELECT_FILTER
  314. static const AVFilterPad avfilter_af_aselect_inputs[] = {
  315. {
  316. .name = "default",
  317. .type = AVMEDIA_TYPE_AUDIO,
  318. .get_audio_buffer = ff_null_get_audio_buffer,
  319. .config_props = config_input,
  320. .filter_frame = filter_frame,
  321. },
  322. { NULL }
  323. };
  324. static const AVFilterPad avfilter_af_aselect_outputs[] = {
  325. {
  326. .name = "default",
  327. .type = AVMEDIA_TYPE_AUDIO,
  328. },
  329. { NULL }
  330. };
  331. AVFilter avfilter_af_aselect = {
  332. .name = "aselect",
  333. .description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."),
  334. .init = init,
  335. .uninit = uninit,
  336. .priv_size = sizeof(SelectContext),
  337. .inputs = avfilter_af_aselect_inputs,
  338. .outputs = avfilter_af_aselect_outputs,
  339. };
  340. #endif /* CONFIG_ASELECT_FILTER */
  341. #if CONFIG_SELECT_FILTER
  342. static const AVFilterPad avfilter_vf_select_inputs[] = {
  343. {
  344. .name = "default",
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. .get_video_buffer = ff_null_get_video_buffer,
  347. .min_perms = AV_PERM_PRESERVE,
  348. .config_props = config_input,
  349. .filter_frame = filter_frame,
  350. },
  351. { NULL }
  352. };
  353. static const AVFilterPad avfilter_vf_select_outputs[] = {
  354. {
  355. .name = "default",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. .request_frame = request_frame,
  358. },
  359. { NULL }
  360. };
  361. AVFilter avfilter_vf_select = {
  362. .name = "select",
  363. .description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."),
  364. .init = init,
  365. .uninit = uninit,
  366. .query_formats = query_formats,
  367. .priv_size = sizeof(SelectContext),
  368. .inputs = avfilter_vf_select_inputs,
  369. .outputs = avfilter_vf_select_outputs,
  370. };
  371. #endif /* CONFIG_SELECT_FILTER */