vf_select.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "avfilter.h"
  27. static const char *var_names[] = {
  28. "E", ///< Euler number
  29. "PHI", ///< golden ratio
  30. "PI", ///< greek pi
  31. "TB", ///< timebase
  32. "pts", ///< original pts in the file of the frame
  33. "start_pts", ///< first PTS in the stream, expressed in TB units
  34. "prev_pts", ///< previous frame PTS
  35. "prev_selected_pts", ///< previous selected frame PTS
  36. "t", ///< first PTS in seconds
  37. "start_t", ///< first PTS in the stream, expressed in seconds
  38. "prev_t", ///< previous frame time
  39. "prev_selected_t", ///< previously selected time
  40. "pict_type", ///< the type of picture in the movie
  41. "PICT_TYPE_I",
  42. "PICT_TYPE_P",
  43. "PICT_TYPE_B",
  44. "PICT_TYPE_S",
  45. "PICT_TYPE_SI",
  46. "PICT_TYPE_SP",
  47. "PICT_TYPE_BI",
  48. "interlace_type", ///< the frame interlace type
  49. "INTERLACE_TYPE_P",
  50. "INTERLACE_TYPE_T",
  51. "INTERLACE_TYPE_B",
  52. "n", ///< frame number (starting from zero)
  53. "selected_n", ///< selected frame number (starting from zero)
  54. "prev_selected_n", ///< number of the last selected frame
  55. "key", ///< tell if the frame is a key frame
  56. "pos", ///< original position in the file of the frame
  57. NULL
  58. };
  59. enum var_name {
  60. VAR_E,
  61. VAR_PHI,
  62. VAR_PI,
  63. VAR_TB,
  64. VAR_PTS,
  65. VAR_START_PTS,
  66. VAR_PREV_PTS,
  67. VAR_PREV_SELECTED_PTS,
  68. VAR_T,
  69. VAR_START_T,
  70. VAR_PREV_T,
  71. VAR_PREV_SELECTED_T,
  72. VAR_PICT_TYPE,
  73. VAR_PICT_TYPE_I,
  74. VAR_PICT_TYPE_P,
  75. VAR_PICT_TYPE_B,
  76. VAR_PICT_TYPE_S,
  77. VAR_PICT_TYPE_SI,
  78. VAR_PICT_TYPE_SP,
  79. VAR_PICT_TYPE_BI,
  80. VAR_INTERLACE_TYPE,
  81. VAR_INTERLACE_TYPE_P,
  82. VAR_INTERLACE_TYPE_T,
  83. VAR_INTERLACE_TYPE_B,
  84. VAR_N,
  85. VAR_SELECTED_N,
  86. VAR_PREV_SELECTED_N,
  87. VAR_KEY,
  88. VAR_POS,
  89. VAR_VARS_NB
  90. };
  91. #define FIFO_SIZE 8
  92. typedef struct {
  93. AVExpr *expr;
  94. double var_values[VAR_VARS_NB];
  95. double select;
  96. int cache_frames;
  97. AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
  98. } SelectContext;
  99. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  100. {
  101. SelectContext *select = ctx->priv;
  102. int ret;
  103. if ((ret = av_expr_parse(&select->expr, args ? args : "1",
  104. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  105. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  106. return ret;
  107. }
  108. select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
  109. if (!select->pending_frames) {
  110. av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
  111. return AVERROR(ENOMEM);
  112. }
  113. return 0;
  114. }
  115. #define INTERLACE_TYPE_P 0
  116. #define INTERLACE_TYPE_T 1
  117. #define INTERLACE_TYPE_B 2
  118. static int config_input(AVFilterLink *inlink)
  119. {
  120. SelectContext *select = inlink->dst->priv;
  121. select->var_values[VAR_E] = M_E;
  122. select->var_values[VAR_PHI] = M_PHI;
  123. select->var_values[VAR_PI] = M_PI;
  124. select->var_values[VAR_N] = 0.0;
  125. select->var_values[VAR_SELECTED_N] = 0.0;
  126. select->var_values[VAR_TB] = av_q2d(inlink->time_base);
  127. select->var_values[VAR_PREV_PTS] = NAN;
  128. select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
  129. select->var_values[VAR_PREV_SELECTED_T] = NAN;
  130. select->var_values[VAR_START_PTS] = NAN;
  131. select->var_values[VAR_START_T] = NAN;
  132. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  133. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  134. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  135. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  136. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  137. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  138. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  139. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
  140. return 0;
  141. }
  142. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  143. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  144. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
  145. {
  146. SelectContext *select = ctx->priv;
  147. AVFilterLink *inlink = ctx->inputs[0];
  148. double res;
  149. if (isnan(select->var_values[VAR_START_PTS]))
  150. select->var_values[VAR_START_PTS] = TS2D(picref->pts);
  151. select->var_values[VAR_PTS] = TS2D(picref->pts);
  152. select->var_values[VAR_T ] = picref->pts * av_q2d(inlink->time_base);
  153. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  154. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  155. select->var_values[VAR_INTERLACE_TYPE] =
  156. !picref->video->interlaced ? INTERLACE_TYPE_P :
  157. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  158. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  159. res = av_expr_eval(select->expr, select->var_values, NULL);
  160. av_log(inlink->dst, AV_LOG_DEBUG,
  161. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  162. "-> select:%f\n",
  163. (int)select->var_values[VAR_N],
  164. (int)select->var_values[VAR_PTS],
  165. select->var_values[VAR_T],
  166. (int)select->var_values[VAR_POS],
  167. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  168. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  169. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  170. (int)select->var_values[VAR_KEY],
  171. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  172. res);
  173. select->var_values[VAR_N] += 1.0;
  174. if (res) {
  175. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  176. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  177. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  178. select->var_values[VAR_SELECTED_N] += 1.0;
  179. }
  180. return res;
  181. }
  182. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  183. {
  184. SelectContext *select = inlink->dst->priv;
  185. select->select = select_frame(inlink->dst, picref);
  186. if (select->select) {
  187. /* frame was requested through poll_frame */
  188. if (select->cache_frames) {
  189. if (!av_fifo_space(select->pending_frames))
  190. av_log(inlink->dst, AV_LOG_ERROR,
  191. "Buffering limit reached, cannot cache more frames\n");
  192. else
  193. av_fifo_generic_write(select->pending_frames, &picref,
  194. sizeof(picref), NULL);
  195. return;
  196. }
  197. avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
  198. }
  199. }
  200. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  201. {
  202. SelectContext *select = inlink->dst->priv;
  203. if (select->select && !select->cache_frames)
  204. avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  205. }
  206. static void end_frame(AVFilterLink *inlink)
  207. {
  208. SelectContext *select = inlink->dst->priv;
  209. AVFilterBufferRef *picref = inlink->cur_buf;
  210. if (select->select) {
  211. if (select->cache_frames)
  212. return;
  213. avfilter_end_frame(inlink->dst->outputs[0]);
  214. }
  215. avfilter_unref_buffer(picref);
  216. }
  217. static int request_frame(AVFilterLink *outlink)
  218. {
  219. AVFilterContext *ctx = outlink->src;
  220. SelectContext *select = ctx->priv;
  221. AVFilterLink *inlink = outlink->src->inputs[0];
  222. select->select = 0;
  223. if (av_fifo_size(select->pending_frames)) {
  224. AVFilterBufferRef *picref;
  225. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  226. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  227. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  228. avfilter_end_frame(outlink);
  229. avfilter_unref_buffer(picref);
  230. return 0;
  231. }
  232. while (!select->select) {
  233. int ret = avfilter_request_frame(inlink);
  234. if (ret < 0)
  235. return ret;
  236. }
  237. return 0;
  238. }
  239. static int poll_frame(AVFilterLink *outlink)
  240. {
  241. SelectContext *select = outlink->src->priv;
  242. AVFilterLink *inlink = outlink->src->inputs[0];
  243. int count, ret;
  244. if (!av_fifo_size(select->pending_frames)) {
  245. if ((count = avfilter_poll_frame(inlink)) <= 0)
  246. return count;
  247. /* request frame from input, and apply select condition to it */
  248. select->cache_frames = 1;
  249. while (count-- && av_fifo_space(select->pending_frames)) {
  250. ret = avfilter_request_frame(inlink);
  251. if (ret < 0)
  252. break;
  253. }
  254. select->cache_frames = 0;
  255. }
  256. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  257. }
  258. static av_cold void uninit(AVFilterContext *ctx)
  259. {
  260. SelectContext *select = ctx->priv;
  261. AVFilterBufferRef *picref;
  262. int i;
  263. av_expr_free(select->expr);
  264. select->expr = NULL;
  265. for (i = 0; i < av_fifo_size(select->pending_frames)/sizeof(picref); i++) {
  266. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  267. avfilter_unref_buffer(picref);
  268. }
  269. av_fifo_free(select->pending_frames);
  270. }
  271. AVFilter avfilter_vf_select = {
  272. .name = "select",
  273. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  274. .init = init,
  275. .uninit = uninit,
  276. .priv_size = sizeof(SelectContext),
  277. .inputs = (AVFilterPad[]) {{ .name = "default",
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .get_video_buffer = avfilter_null_get_video_buffer,
  280. .config_props = config_input,
  281. .start_frame = start_frame,
  282. .draw_slice = draw_slice,
  283. .end_frame = end_frame },
  284. { .name = NULL }},
  285. .outputs = (AVFilterPad[]) {{ .name = "default",
  286. .type = AVMEDIA_TYPE_VIDEO,
  287. .poll_frame = poll_frame,
  288. .request_frame = request_frame, },
  289. { .name = NULL}},
  290. };