vf_select.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. static const char *const var_names[] = {
  33. "E", ///< Euler number
  34. "PHI", ///< golden ratio
  35. "PI", ///< Greek pi
  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. "n", ///< frame number (starting from zero)
  58. "selected_n", ///< selected frame number (starting from zero)
  59. "prev_selected_n", ///< number of the last selected frame
  60. "key", ///< tell if the frame is a key frame
  61. "pos", ///< original position in the file of the frame
  62. NULL
  63. };
  64. enum var_name {
  65. VAR_E,
  66. VAR_PHI,
  67. VAR_PI,
  68. VAR_TB,
  69. VAR_PTS,
  70. VAR_START_PTS,
  71. VAR_PREV_PTS,
  72. VAR_PREV_SELECTED_PTS,
  73. VAR_T,
  74. VAR_START_T,
  75. VAR_PREV_T,
  76. VAR_PREV_SELECTED_T,
  77. VAR_PICT_TYPE,
  78. VAR_PICT_TYPE_I,
  79. VAR_PICT_TYPE_P,
  80. VAR_PICT_TYPE_B,
  81. VAR_PICT_TYPE_S,
  82. VAR_PICT_TYPE_SI,
  83. VAR_PICT_TYPE_SP,
  84. VAR_PICT_TYPE_BI,
  85. VAR_INTERLACE_TYPE,
  86. VAR_INTERLACE_TYPE_P,
  87. VAR_INTERLACE_TYPE_T,
  88. VAR_INTERLACE_TYPE_B,
  89. VAR_N,
  90. VAR_SELECTED_N,
  91. VAR_PREV_SELECTED_N,
  92. VAR_KEY,
  93. VAR_VARS_NB
  94. };
  95. #define FIFO_SIZE 8
  96. typedef struct SelectContext {
  97. const AVClass *class;
  98. char *expr_str;
  99. AVExpr *expr;
  100. double var_values[VAR_VARS_NB];
  101. double select;
  102. int cache_frames;
  103. AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
  104. } SelectContext;
  105. static av_cold int init(AVFilterContext *ctx)
  106. {
  107. SelectContext *select = ctx->priv;
  108. int ret;
  109. if ((ret = av_expr_parse(&select->expr, select->expr_str,
  110. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  111. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n",
  112. select->expr_str);
  113. return ret;
  114. }
  115. select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFrame*));
  116. if (!select->pending_frames) {
  117. av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
  118. return AVERROR(ENOMEM);
  119. }
  120. return 0;
  121. }
  122. #define INTERLACE_TYPE_P 0
  123. #define INTERLACE_TYPE_T 1
  124. #define INTERLACE_TYPE_B 2
  125. static int config_input(AVFilterLink *inlink)
  126. {
  127. SelectContext *select = inlink->dst->priv;
  128. select->var_values[VAR_E] = M_E;
  129. select->var_values[VAR_PHI] = M_PHI;
  130. select->var_values[VAR_PI] = M_PI;
  131. select->var_values[VAR_N] = 0.0;
  132. select->var_values[VAR_SELECTED_N] = 0.0;
  133. select->var_values[VAR_TB] = av_q2d(inlink->time_base);
  134. select->var_values[VAR_PREV_PTS] = NAN;
  135. select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
  136. select->var_values[VAR_PREV_SELECTED_T] = NAN;
  137. select->var_values[VAR_START_PTS] = NAN;
  138. select->var_values[VAR_START_T] = NAN;
  139. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  140. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  141. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  142. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  143. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  144. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  145. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  146. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
  147. return 0;
  148. }
  149. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  150. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  151. static int select_frame(AVFilterContext *ctx, AVFrame *frame)
  152. {
  153. SelectContext *select = ctx->priv;
  154. AVFilterLink *inlink = ctx->inputs[0];
  155. double res;
  156. if (isnan(select->var_values[VAR_START_PTS]))
  157. select->var_values[VAR_START_PTS] = TS2D(frame->pts);
  158. if (isnan(select->var_values[VAR_START_T]))
  159. select->var_values[VAR_START_T] = TS2D(frame->pts) * av_q2d(inlink->time_base);
  160. select->var_values[VAR_PTS] = TS2D(frame->pts);
  161. select->var_values[VAR_T ] = TS2D(frame->pts) * av_q2d(inlink->time_base);
  162. select->var_values[VAR_PREV_PTS] = TS2D(frame->pts);
  163. select->var_values[VAR_INTERLACE_TYPE] =
  164. !frame->interlaced_frame ? INTERLACE_TYPE_P :
  165. frame->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  166. select->var_values[VAR_PICT_TYPE] = frame->pict_type;
  167. res = av_expr_eval(select->expr, select->var_values, NULL);
  168. select->var_values[VAR_N] += 1.0;
  169. if (res) {
  170. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  171. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  172. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  173. select->var_values[VAR_SELECTED_N] += 1.0;
  174. }
  175. return res;
  176. }
  177. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  178. {
  179. SelectContext *select = inlink->dst->priv;
  180. select->select = select_frame(inlink->dst, frame);
  181. if (select->select) {
  182. /* frame was requested through poll_frame */
  183. if (select->cache_frames) {
  184. if (!av_fifo_space(select->pending_frames)) {
  185. av_log(inlink->dst, AV_LOG_ERROR,
  186. "Buffering limit reached, cannot cache more frames\n");
  187. av_frame_free(&frame);
  188. } else
  189. av_fifo_generic_write(select->pending_frames, &frame,
  190. sizeof(frame), NULL);
  191. return 0;
  192. }
  193. return ff_filter_frame(inlink->dst->outputs[0], frame);
  194. }
  195. av_frame_free(&frame);
  196. return 0;
  197. }
  198. static int request_frame(AVFilterLink *outlink)
  199. {
  200. AVFilterContext *ctx = outlink->src;
  201. SelectContext *select = ctx->priv;
  202. AVFilterLink *inlink = outlink->src->inputs[0];
  203. select->select = 0;
  204. if (av_fifo_size(select->pending_frames)) {
  205. AVFrame *frame;
  206. av_fifo_generic_read(select->pending_frames, &frame, sizeof(frame), NULL);
  207. return ff_filter_frame(outlink, frame);
  208. }
  209. while (!select->select) {
  210. int ret = ff_request_frame(inlink);
  211. if (ret < 0)
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. static int poll_frame(AVFilterLink *outlink)
  217. {
  218. SelectContext *select = outlink->src->priv;
  219. AVFilterLink *inlink = outlink->src->inputs[0];
  220. int count, ret;
  221. if (!av_fifo_size(select->pending_frames)) {
  222. if ((count = ff_poll_frame(inlink)) <= 0)
  223. return count;
  224. /* request frame from input, and apply select condition to it */
  225. select->cache_frames = 1;
  226. while (count-- && av_fifo_space(select->pending_frames)) {
  227. ret = ff_request_frame(inlink);
  228. if (ret < 0)
  229. break;
  230. }
  231. select->cache_frames = 0;
  232. }
  233. return av_fifo_size(select->pending_frames)/sizeof(AVFrame*);
  234. }
  235. static av_cold void uninit(AVFilterContext *ctx)
  236. {
  237. SelectContext *select = ctx->priv;
  238. AVFrame *frame;
  239. av_expr_free(select->expr);
  240. select->expr = NULL;
  241. while (select->pending_frames &&
  242. av_fifo_generic_read(select->pending_frames, &frame, sizeof(frame), NULL) == sizeof(frame))
  243. av_frame_free(&frame);
  244. av_fifo_free(select->pending_frames);
  245. select->pending_frames = NULL;
  246. }
  247. #define OFFSET(x) offsetof(SelectContext, x)
  248. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  249. static const AVOption options[] = {
  250. { "expr", "An expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
  251. { NULL },
  252. };
  253. static const AVClass select_class = {
  254. .class_name = "select",
  255. .item_name = av_default_item_name,
  256. .option = options,
  257. .version = LIBAVUTIL_VERSION_INT,
  258. };
  259. static const AVFilterPad avfilter_vf_select_inputs[] = {
  260. {
  261. .name = "default",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .get_video_buffer = ff_null_get_video_buffer,
  264. .config_props = config_input,
  265. .filter_frame = filter_frame,
  266. },
  267. { NULL }
  268. };
  269. static const AVFilterPad avfilter_vf_select_outputs[] = {
  270. {
  271. .name = "default",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .poll_frame = poll_frame,
  274. .request_frame = request_frame,
  275. },
  276. { NULL }
  277. };
  278. AVFilter ff_vf_select = {
  279. .name = "select",
  280. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  281. .init = init,
  282. .uninit = uninit,
  283. .priv_size = sizeof(SelectContext),
  284. .priv_class = &select_class,
  285. .inputs = avfilter_vf_select_inputs,
  286. .outputs = avfilter_vf_select_outputs,
  287. };