vf_select.c 11 KB

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