You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
10KB

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