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.

377 lines
11KB

  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_POS,
  93. VAR_VARS_NB
  94. };
  95. #define FIFO_SIZE 8
  96. typedef struct {
  97. AVExpr *expr;
  98. double var_values[VAR_VARS_NB];
  99. double select;
  100. int cache_frames;
  101. AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
  102. } SelectContext;
  103. static av_cold int init(AVFilterContext *ctx, const char *args)
  104. {
  105. SelectContext *select = ctx->priv;
  106. int ret;
  107. if ((ret = av_expr_parse(&select->expr, args ? args : "1",
  108. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  109. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  110. return ret;
  111. }
  112. select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
  113. if (!select->pending_frames) {
  114. av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
  115. return AVERROR(ENOMEM);
  116. }
  117. return 0;
  118. }
  119. #define INTERLACE_TYPE_P 0
  120. #define INTERLACE_TYPE_T 1
  121. #define INTERLACE_TYPE_B 2
  122. static int config_input(AVFilterLink *inlink)
  123. {
  124. SelectContext *select = inlink->dst->priv;
  125. select->var_values[VAR_E] = M_E;
  126. select->var_values[VAR_PHI] = M_PHI;
  127. select->var_values[VAR_PI] = M_PI;
  128. select->var_values[VAR_N] = 0.0;
  129. select->var_values[VAR_SELECTED_N] = 0.0;
  130. select->var_values[VAR_TB] = av_q2d(inlink->time_base);
  131. select->var_values[VAR_PREV_PTS] = NAN;
  132. select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
  133. select->var_values[VAR_PREV_SELECTED_T] = NAN;
  134. select->var_values[VAR_START_PTS] = NAN;
  135. select->var_values[VAR_START_T] = NAN;
  136. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  137. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  138. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  139. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  140. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  141. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  142. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  143. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
  144. return 0;
  145. }
  146. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  147. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  148. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
  149. {
  150. SelectContext *select = ctx->priv;
  151. AVFilterLink *inlink = ctx->inputs[0];
  152. double res;
  153. if (isnan(select->var_values[VAR_START_PTS]))
  154. select->var_values[VAR_START_PTS] = TS2D(picref->pts);
  155. if (isnan(select->var_values[VAR_START_T]))
  156. select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  157. select->var_values[VAR_PTS] = TS2D(picref->pts);
  158. select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  159. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  160. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  161. select->var_values[VAR_INTERLACE_TYPE] =
  162. !picref->video->interlaced ? INTERLACE_TYPE_P :
  163. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  164. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  165. res = av_expr_eval(select->expr, select->var_values, NULL);
  166. av_log(inlink->dst, AV_LOG_DEBUG,
  167. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  168. "-> select:%f\n",
  169. (int)select->var_values[VAR_N],
  170. (int)select->var_values[VAR_PTS],
  171. select->var_values[VAR_T],
  172. (int)select->var_values[VAR_POS],
  173. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  174. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  175. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  176. (int)select->var_values[VAR_KEY],
  177. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  178. res);
  179. select->var_values[VAR_N] += 1.0;
  180. if (res) {
  181. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  182. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  183. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  184. select->var_values[VAR_SELECTED_N] += 1.0;
  185. }
  186. return res;
  187. }
  188. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  189. {
  190. SelectContext *select = inlink->dst->priv;
  191. select->select = select_frame(inlink->dst, picref);
  192. if (select->select) {
  193. AVFilterBufferRef *buf_out;
  194. /* frame was requested through poll_frame */
  195. if (select->cache_frames) {
  196. if (!av_fifo_space(select->pending_frames))
  197. av_log(inlink->dst, AV_LOG_ERROR,
  198. "Buffering limit reached, cannot cache more frames\n");
  199. else
  200. av_fifo_generic_write(select->pending_frames, &picref,
  201. sizeof(picref), NULL);
  202. return 0;
  203. }
  204. buf_out = avfilter_ref_buffer(picref, ~0);
  205. if (!buf_out)
  206. return AVERROR(ENOMEM);
  207. return ff_start_frame(inlink->dst->outputs[0], buf_out);
  208. }
  209. return 0;
  210. }
  211. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  212. {
  213. SelectContext *select = inlink->dst->priv;
  214. if (select->select && !select->cache_frames)
  215. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  216. return 0;
  217. }
  218. static int end_frame(AVFilterLink *inlink)
  219. {
  220. SelectContext *select = inlink->dst->priv;
  221. if (select->select) {
  222. if (select->cache_frames)
  223. return 0;
  224. return ff_end_frame(inlink->dst->outputs[0]);
  225. }
  226. return 0;
  227. }
  228. static int request_frame(AVFilterLink *outlink)
  229. {
  230. AVFilterContext *ctx = outlink->src;
  231. SelectContext *select = ctx->priv;
  232. AVFilterLink *inlink = outlink->src->inputs[0];
  233. select->select = 0;
  234. if (av_fifo_size(select->pending_frames)) {
  235. AVFilterBufferRef *picref;
  236. int ret;
  237. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  238. if ((ret = ff_start_frame(outlink, picref)) < 0 ||
  239. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  240. (ret = ff_end_frame(outlink)) < 0);
  241. return ret;
  242. }
  243. while (!select->select) {
  244. int ret = ff_request_frame(inlink);
  245. if (ret < 0)
  246. return ret;
  247. }
  248. return 0;
  249. }
  250. static int poll_frame(AVFilterLink *outlink)
  251. {
  252. SelectContext *select = outlink->src->priv;
  253. AVFilterLink *inlink = outlink->src->inputs[0];
  254. int count, ret;
  255. if (!av_fifo_size(select->pending_frames)) {
  256. if ((count = ff_poll_frame(inlink)) <= 0)
  257. return count;
  258. /* request frame from input, and apply select condition to it */
  259. select->cache_frames = 1;
  260. while (count-- && av_fifo_space(select->pending_frames)) {
  261. ret = ff_request_frame(inlink);
  262. if (ret < 0)
  263. break;
  264. }
  265. select->cache_frames = 0;
  266. }
  267. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  268. }
  269. static av_cold void uninit(AVFilterContext *ctx)
  270. {
  271. SelectContext *select = ctx->priv;
  272. AVFilterBufferRef *picref;
  273. av_expr_free(select->expr);
  274. select->expr = NULL;
  275. while (select->pending_frames &&
  276. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
  277. avfilter_unref_buffer(picref);
  278. av_fifo_free(select->pending_frames);
  279. select->pending_frames = NULL;
  280. }
  281. static const AVFilterPad avfilter_vf_select_inputs[] = {
  282. {
  283. .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .get_video_buffer = ff_null_get_video_buffer,
  286. .config_props = config_input,
  287. .start_frame = start_frame,
  288. .draw_slice = draw_slice,
  289. .end_frame = end_frame
  290. },
  291. { NULL }
  292. };
  293. static const AVFilterPad avfilter_vf_select_outputs[] = {
  294. {
  295. .name = "default",
  296. .type = AVMEDIA_TYPE_VIDEO,
  297. .poll_frame = poll_frame,
  298. .request_frame = request_frame,
  299. },
  300. { NULL }
  301. };
  302. AVFilter avfilter_vf_select = {
  303. .name = "select",
  304. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  305. .init = init,
  306. .uninit = uninit,
  307. .priv_size = sizeof(SelectContext),
  308. .inputs = avfilter_vf_select_inputs,
  309. .outputs = avfilter_vf_select_outputs,
  310. };