vf_select.c 10 KB

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