vf_select.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. av_log(inlink->dst, AV_LOG_DEBUG,
  154. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  155. "-> select:%f\n",
  156. (int)select->var_values[VAR_N],
  157. (int)select->var_values[VAR_PTS],
  158. select->var_values[VAR_T],
  159. (int)select->var_values[VAR_POS],
  160. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  161. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  162. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  163. (int)select->var_values[VAR_KEY],
  164. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  165. res);
  166. select->var_values[VAR_N] += 1.0;
  167. if (res) {
  168. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  169. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  170. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  171. select->var_values[VAR_SELECTED_N] += 1.0;
  172. }
  173. return res;
  174. }
  175. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  176. {
  177. SelectContext *select = inlink->dst->priv;
  178. select->select = select_frame(inlink->dst, picref);
  179. if (select->select) {
  180. /* frame was requested through poll_frame */
  181. if (select->cache_frames) {
  182. if (!av_fifo_space(select->pending_frames))
  183. av_log(inlink->dst, AV_LOG_ERROR,
  184. "Buffering limit reached, cannot cache more frames\n");
  185. else
  186. av_fifo_generic_write(select->pending_frames, &picref,
  187. sizeof(picref), NULL);
  188. return;
  189. }
  190. avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
  191. }
  192. }
  193. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  194. {
  195. SelectContext *select = inlink->dst->priv;
  196. if (select->select && !select->cache_frames)
  197. avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  198. }
  199. static void end_frame(AVFilterLink *inlink)
  200. {
  201. SelectContext *select = inlink->dst->priv;
  202. AVFilterBufferRef *picref = inlink->cur_buf;
  203. if (select->select) {
  204. if (select->cache_frames)
  205. return;
  206. avfilter_end_frame(inlink->dst->outputs[0]);
  207. }
  208. avfilter_unref_buffer(picref);
  209. }
  210. static int request_frame(AVFilterLink *outlink)
  211. {
  212. AVFilterContext *ctx = outlink->src;
  213. SelectContext *select = ctx->priv;
  214. AVFilterLink *inlink = outlink->src->inputs[0];
  215. select->select = 0;
  216. if (av_fifo_size(select->pending_frames)) {
  217. AVFilterBufferRef *picref;
  218. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  219. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  220. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  221. avfilter_end_frame(outlink);
  222. avfilter_unref_buffer(picref);
  223. return 0;
  224. }
  225. while (!select->select) {
  226. int ret = avfilter_request_frame(inlink);
  227. if (ret < 0)
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. static int poll_frame(AVFilterLink *outlink)
  233. {
  234. SelectContext *select = outlink->src->priv;
  235. AVFilterLink *inlink = outlink->src->inputs[0];
  236. int count, ret;
  237. if (!av_fifo_size(select->pending_frames)) {
  238. if ((count = avfilter_poll_frame(inlink)) <= 0)
  239. return count;
  240. /* request frame from input, and apply select condition to it */
  241. select->cache_frames = 1;
  242. while (count-- && av_fifo_space(select->pending_frames)) {
  243. ret = avfilter_request_frame(inlink);
  244. if (ret < 0)
  245. break;
  246. }
  247. select->cache_frames = 0;
  248. }
  249. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  250. }
  251. static av_cold void uninit(AVFilterContext *ctx)
  252. {
  253. SelectContext *select = ctx->priv;
  254. AVFilterBufferRef *picref;
  255. av_expr_free(select->expr);
  256. select->expr = NULL;
  257. while (select->pending_frames &&
  258. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
  259. avfilter_unref_buffer(picref);
  260. av_fifo_free(select->pending_frames);
  261. select->pending_frames = NULL;
  262. }
  263. AVFilter avfilter_vf_select = {
  264. .name = "select",
  265. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  266. .init = init,
  267. .uninit = uninit,
  268. .priv_size = sizeof(SelectContext),
  269. .inputs = (const AVFilterPad[]) {{ .name = "default",
  270. .type = AVMEDIA_TYPE_VIDEO,
  271. .get_video_buffer = avfilter_null_get_video_buffer,
  272. .config_props = config_input,
  273. .start_frame = start_frame,
  274. .draw_slice = draw_slice,
  275. .end_frame = end_frame },
  276. { .name = NULL }},
  277. .outputs = (const AVFilterPad[]) {{ .name = "default",
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .poll_frame = poll_frame,
  280. .request_frame = request_frame, },
  281. { .name = NULL}},
  282. };