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.

356 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/mathematics.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. static const char *const var_names[] = {
  31. "E", ///< Euler number
  32. "PHI", ///< golden ratio
  33. "PI", ///< greek pi
  34. "TB", ///< timebase
  35. "pts", ///< original pts in the file of the frame
  36. "start_pts", ///< first PTS in the stream, expressed in TB units
  37. "prev_pts", ///< previous frame PTS
  38. "prev_selected_pts", ///< previous selected frame PTS
  39. "t", ///< first PTS in seconds
  40. "start_t", ///< first PTS in the stream, expressed in seconds
  41. "prev_t", ///< previous frame time
  42. "prev_selected_t", ///< previously selected time
  43. "pict_type", ///< the type of picture in the movie
  44. "I",
  45. "P",
  46. "B",
  47. "S",
  48. "SI",
  49. "SP",
  50. "BI",
  51. "interlace_type", ///< the frame interlace type
  52. "PROGRESSIVE",
  53. "TOPFIRST",
  54. "BOTTOMFIRST",
  55. "n", ///< frame number (starting from zero)
  56. "selected_n", ///< selected frame number (starting from zero)
  57. "prev_selected_n", ///< number of the last selected frame
  58. "key", ///< tell if the frame is a key frame
  59. "pos", ///< original position in the file of the frame
  60. NULL
  61. };
  62. enum var_name {
  63. VAR_E,
  64. VAR_PHI,
  65. VAR_PI,
  66. VAR_TB,
  67. VAR_PTS,
  68. VAR_START_PTS,
  69. VAR_PREV_PTS,
  70. VAR_PREV_SELECTED_PTS,
  71. VAR_T,
  72. VAR_START_T,
  73. VAR_PREV_T,
  74. VAR_PREV_SELECTED_T,
  75. VAR_PICT_TYPE,
  76. VAR_PICT_TYPE_I,
  77. VAR_PICT_TYPE_P,
  78. VAR_PICT_TYPE_B,
  79. VAR_PICT_TYPE_S,
  80. VAR_PICT_TYPE_SI,
  81. VAR_PICT_TYPE_SP,
  82. VAR_PICT_TYPE_BI,
  83. VAR_INTERLACE_TYPE,
  84. VAR_INTERLACE_TYPE_P,
  85. VAR_INTERLACE_TYPE_T,
  86. VAR_INTERLACE_TYPE_B,
  87. VAR_N,
  88. VAR_SELECTED_N,
  89. VAR_PREV_SELECTED_N,
  90. VAR_KEY,
  91. VAR_POS,
  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(AVFilterBufferRef*));
  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, AVFilterBufferRef *picref)
  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(picref->pts);
  154. if (isnan(select->var_values[VAR_START_T]))
  155. select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  156. select->var_values[VAR_PTS] = TS2D(picref->pts);
  157. select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  158. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  159. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  160. select->var_values[VAR_INTERLACE_TYPE] =
  161. !picref->video->interlaced ? INTERLACE_TYPE_P :
  162. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  163. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  164. res = av_expr_eval(select->expr, select->var_values, NULL);
  165. av_log(inlink->dst, AV_LOG_DEBUG,
  166. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  167. "-> select:%f\n",
  168. (int)select->var_values[VAR_N],
  169. (int)select->var_values[VAR_PTS],
  170. select->var_values[VAR_T],
  171. (int)select->var_values[VAR_POS],
  172. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  173. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  174. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  175. (int)select->var_values[VAR_KEY],
  176. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  177. res);
  178. select->var_values[VAR_N] += 1.0;
  179. if (res) {
  180. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  181. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  182. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  183. select->var_values[VAR_SELECTED_N] += 1.0;
  184. }
  185. return res;
  186. }
  187. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  188. {
  189. SelectContext *select = inlink->dst->priv;
  190. select->select = select_frame(inlink->dst, picref);
  191. if (select->select) {
  192. /* frame was requested through poll_frame */
  193. if (select->cache_frames) {
  194. if (!av_fifo_space(select->pending_frames))
  195. av_log(inlink->dst, AV_LOG_ERROR,
  196. "Buffering limit reached, cannot cache more frames\n");
  197. else
  198. av_fifo_generic_write(select->pending_frames, &picref,
  199. sizeof(picref), NULL);
  200. return;
  201. }
  202. ff_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
  203. }
  204. }
  205. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  206. {
  207. SelectContext *select = inlink->dst->priv;
  208. if (select->select && !select->cache_frames)
  209. ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  210. }
  211. static void end_frame(AVFilterLink *inlink)
  212. {
  213. SelectContext *select = inlink->dst->priv;
  214. AVFilterBufferRef *picref = inlink->cur_buf;
  215. if (select->select) {
  216. if (select->cache_frames)
  217. return;
  218. ff_end_frame(inlink->dst->outputs[0]);
  219. }
  220. avfilter_unref_buffer(picref);
  221. }
  222. static int request_frame(AVFilterLink *outlink)
  223. {
  224. AVFilterContext *ctx = outlink->src;
  225. SelectContext *select = ctx->priv;
  226. AVFilterLink *inlink = outlink->src->inputs[0];
  227. select->select = 0;
  228. if (av_fifo_size(select->pending_frames)) {
  229. AVFilterBufferRef *picref;
  230. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  231. ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  232. ff_draw_slice(outlink, 0, outlink->h, 1);
  233. ff_end_frame(outlink);
  234. avfilter_unref_buffer(picref);
  235. return 0;
  236. }
  237. while (!select->select) {
  238. int ret = ff_request_frame(inlink);
  239. if (ret < 0)
  240. return ret;
  241. }
  242. return 0;
  243. }
  244. static int poll_frame(AVFilterLink *outlink)
  245. {
  246. SelectContext *select = outlink->src->priv;
  247. AVFilterLink *inlink = outlink->src->inputs[0];
  248. int count, ret;
  249. if (!av_fifo_size(select->pending_frames)) {
  250. if ((count = ff_poll_frame(inlink)) <= 0)
  251. return count;
  252. /* request frame from input, and apply select condition to it */
  253. select->cache_frames = 1;
  254. while (count-- && av_fifo_space(select->pending_frames)) {
  255. ret = ff_request_frame(inlink);
  256. if (ret < 0)
  257. break;
  258. }
  259. select->cache_frames = 0;
  260. }
  261. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  262. }
  263. static av_cold void uninit(AVFilterContext *ctx)
  264. {
  265. SelectContext *select = ctx->priv;
  266. AVFilterBufferRef *picref;
  267. av_expr_free(select->expr);
  268. select->expr = NULL;
  269. while (select->pending_frames &&
  270. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
  271. avfilter_unref_buffer(picref);
  272. av_fifo_free(select->pending_frames);
  273. select->pending_frames = NULL;
  274. }
  275. AVFilter avfilter_vf_select = {
  276. .name = "select",
  277. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  278. .init = init,
  279. .uninit = uninit,
  280. .priv_size = sizeof(SelectContext),
  281. .inputs = (AVFilterPad[]) {{ .name = "default",
  282. .type = AVMEDIA_TYPE_VIDEO,
  283. .get_video_buffer = ff_null_get_video_buffer,
  284. .config_props = config_input,
  285. .start_frame = start_frame,
  286. .draw_slice = draw_slice,
  287. .end_frame = end_frame },
  288. { .name = NULL }},
  289. .outputs = (AVFilterPad[]) {{ .name = "default",
  290. .type = AVMEDIA_TYPE_VIDEO,
  291. .poll_frame = poll_frame,
  292. .request_frame = request_frame, },
  293. { .name = NULL}},
  294. };