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.

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