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.

459 lines
14KB

  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 "libavutil/internal.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. #if CONFIG_AVCODEC
  32. #include "libavcodec/dsputil.h"
  33. #endif
  34. static const char *const var_names[] = {
  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. "scene",
  62. NULL
  63. };
  64. enum var_name {
  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_SCENE,
  92. VAR_VARS_NB
  93. };
  94. #define FIFO_SIZE 8
  95. typedef struct {
  96. AVExpr *expr;
  97. double var_values[VAR_VARS_NB];
  98. int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
  99. #if CONFIG_AVCODEC
  100. AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
  101. DSPContext c; ///< context providing optimized SAD methods (scene detect only)
  102. double prev_mafd; ///< previous MAFD (scene detect only)
  103. #endif
  104. AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
  105. double select;
  106. int cache_frames;
  107. AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
  108. } SelectContext;
  109. static av_cold int init(AVFilterContext *ctx, const char *args)
  110. {
  111. SelectContext *select = ctx->priv;
  112. int ret;
  113. if ((ret = av_expr_parse(&select->expr, args ? args : "1",
  114. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  115. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  116. return ret;
  117. }
  118. select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
  119. if (!select->pending_frames) {
  120. av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
  121. return AVERROR(ENOMEM);
  122. }
  123. select->do_scene_detect = args && strstr(args, "scene");
  124. if (select->do_scene_detect && !CONFIG_AVCODEC) {
  125. av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
  126. return AVERROR(EINVAL);
  127. }
  128. return 0;
  129. }
  130. #define INTERLACE_TYPE_P 0
  131. #define INTERLACE_TYPE_T 1
  132. #define INTERLACE_TYPE_B 2
  133. static int config_input(AVFilterLink *inlink)
  134. {
  135. SelectContext *select = inlink->dst->priv;
  136. select->var_values[VAR_N] = 0.0;
  137. select->var_values[VAR_SELECTED_N] = 0.0;
  138. select->var_values[VAR_TB] = av_q2d(inlink->time_base);
  139. select->var_values[VAR_PREV_PTS] = NAN;
  140. select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
  141. select->var_values[VAR_PREV_SELECTED_T] = NAN;
  142. select->var_values[VAR_START_PTS] = NAN;
  143. select->var_values[VAR_START_T] = NAN;
  144. select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
  145. select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
  146. select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
  147. select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
  148. select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
  149. select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
  150. select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
  151. select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
  152. if (CONFIG_AVCODEC && select->do_scene_detect) {
  153. select->avctx = avcodec_alloc_context3(NULL);
  154. if (!select->avctx)
  155. return AVERROR(ENOMEM);
  156. dsputil_init(&select->c, select->avctx);
  157. }
  158. return 0;
  159. }
  160. #if CONFIG_AVCODEC
  161. static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
  162. {
  163. double ret = 0;
  164. SelectContext *select = ctx->priv;
  165. AVFilterBufferRef *prev_picref = select->prev_picref;
  166. if (prev_picref &&
  167. picref->video->h == prev_picref->video->h &&
  168. picref->video->w == prev_picref->video->w &&
  169. picref->linesize[0] == prev_picref->linesize[0]) {
  170. int x, y, nb_sad = 0;
  171. int64_t sad = 0;
  172. double mafd, diff;
  173. uint8_t *p1 = picref->data[0];
  174. uint8_t *p2 = prev_picref->data[0];
  175. const int linesize = picref->linesize[0];
  176. for (y = 0; y < picref->video->h - 8; y += 8) {
  177. for (x = 0; x < picref->video->w*3 - 8; x += 8) {
  178. sad += select->c.sad[1](select, p1 + x, p2 + x,
  179. linesize, 8);
  180. nb_sad += 8 * 8;
  181. }
  182. p1 += 8 * linesize;
  183. p2 += 8 * linesize;
  184. }
  185. emms_c();
  186. mafd = nb_sad ? sad / nb_sad : 0;
  187. diff = fabs(mafd - select->prev_mafd);
  188. ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
  189. select->prev_mafd = mafd;
  190. avfilter_unref_buffer(prev_picref);
  191. }
  192. select->prev_picref = avfilter_ref_buffer(picref, ~0);
  193. return ret;
  194. }
  195. #endif
  196. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  197. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  198. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
  199. {
  200. SelectContext *select = ctx->priv;
  201. AVFilterLink *inlink = ctx->inputs[0];
  202. double res;
  203. if (CONFIG_AVCODEC && select->do_scene_detect)
  204. select->var_values[VAR_SCENE] = get_scene_score(ctx, picref);
  205. if (isnan(select->var_values[VAR_START_PTS]))
  206. select->var_values[VAR_START_PTS] = TS2D(picref->pts);
  207. if (isnan(select->var_values[VAR_START_T]))
  208. select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  209. select->var_values[VAR_PTS] = TS2D(picref->pts);
  210. select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  211. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  212. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  213. select->var_values[VAR_INTERLACE_TYPE] =
  214. !picref->video->interlaced ? INTERLACE_TYPE_P :
  215. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  216. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  217. res = av_expr_eval(select->expr, select->var_values, NULL);
  218. av_log(inlink->dst, AV_LOG_DEBUG,
  219. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  220. "-> select:%f\n",
  221. (int)select->var_values[VAR_N],
  222. (int)select->var_values[VAR_PTS],
  223. select->var_values[VAR_T],
  224. (int)select->var_values[VAR_POS],
  225. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  226. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  227. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  228. (int)select->var_values[VAR_KEY],
  229. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  230. res);
  231. select->var_values[VAR_N] += 1.0;
  232. if (res) {
  233. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  234. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  235. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  236. select->var_values[VAR_SELECTED_N] += 1.0;
  237. }
  238. return res;
  239. }
  240. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  241. {
  242. SelectContext *select = inlink->dst->priv;
  243. select->select = select_frame(inlink->dst, picref);
  244. if (select->select) {
  245. AVFilterBufferRef *buf_out;
  246. /* frame was requested through poll_frame */
  247. if (select->cache_frames) {
  248. if (!av_fifo_space(select->pending_frames))
  249. av_log(inlink->dst, AV_LOG_ERROR,
  250. "Buffering limit reached, cannot cache more frames\n");
  251. else
  252. av_fifo_generic_write(select->pending_frames, &picref,
  253. sizeof(picref), NULL);
  254. return 0;
  255. }
  256. buf_out = avfilter_ref_buffer(picref, ~0);
  257. if (!buf_out)
  258. return AVERROR(ENOMEM);
  259. return ff_start_frame(inlink->dst->outputs[0], buf_out);
  260. }
  261. return 0;
  262. }
  263. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  264. {
  265. SelectContext *select = inlink->dst->priv;
  266. if (select->select && !select->cache_frames)
  267. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  268. return 0;
  269. }
  270. static int end_frame(AVFilterLink *inlink)
  271. {
  272. SelectContext *select = inlink->dst->priv;
  273. if (select->select) {
  274. if (select->cache_frames)
  275. return 0;
  276. return ff_end_frame(inlink->dst->outputs[0]);
  277. }
  278. return 0;
  279. }
  280. static int request_frame(AVFilterLink *outlink)
  281. {
  282. AVFilterContext *ctx = outlink->src;
  283. SelectContext *select = ctx->priv;
  284. AVFilterLink *inlink = outlink->src->inputs[0];
  285. select->select = 0;
  286. if (av_fifo_size(select->pending_frames)) {
  287. AVFilterBufferRef *picref;
  288. int ret;
  289. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  290. if ((ret = ff_start_frame(outlink, picref)) < 0 ||
  291. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  292. (ret = ff_end_frame(outlink)) < 0);
  293. return ret;
  294. }
  295. while (!select->select) {
  296. int ret = ff_request_frame(inlink);
  297. if (ret < 0)
  298. return ret;
  299. }
  300. return 0;
  301. }
  302. static int poll_frame(AVFilterLink *outlink)
  303. {
  304. SelectContext *select = outlink->src->priv;
  305. AVFilterLink *inlink = outlink->src->inputs[0];
  306. int count, ret;
  307. if (!av_fifo_size(select->pending_frames)) {
  308. if ((count = ff_poll_frame(inlink)) <= 0)
  309. return count;
  310. /* request frame from input, and apply select condition to it */
  311. select->cache_frames = 1;
  312. while (count-- && av_fifo_space(select->pending_frames)) {
  313. ret = ff_request_frame(inlink);
  314. if (ret < 0)
  315. break;
  316. }
  317. select->cache_frames = 0;
  318. }
  319. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  320. }
  321. static av_cold void uninit(AVFilterContext *ctx)
  322. {
  323. SelectContext *select = ctx->priv;
  324. AVFilterBufferRef *picref;
  325. av_expr_free(select->expr);
  326. select->expr = NULL;
  327. while (select->pending_frames &&
  328. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
  329. avfilter_unref_buffer(picref);
  330. av_fifo_free(select->pending_frames);
  331. select->pending_frames = NULL;
  332. if (select->do_scene_detect) {
  333. avfilter_unref_bufferp(&select->prev_picref);
  334. if (select->avctx) {
  335. avcodec_close(select->avctx);
  336. av_freep(&select->avctx);
  337. }
  338. }
  339. }
  340. static int query_formats(AVFilterContext *ctx)
  341. {
  342. SelectContext *select = ctx->priv;
  343. if (!select->do_scene_detect) {
  344. return ff_default_query_formats(ctx);
  345. } else {
  346. static const enum AVPixelFormat pix_fmts[] = {
  347. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  348. AV_PIX_FMT_NONE
  349. };
  350. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  351. }
  352. return 0;
  353. }
  354. static const AVFilterPad avfilter_vf_select_inputs[] = {
  355. {
  356. .name = "default",
  357. .type = AVMEDIA_TYPE_VIDEO,
  358. .get_video_buffer = ff_null_get_video_buffer,
  359. .min_perms = AV_PERM_PRESERVE,
  360. .config_props = config_input,
  361. .start_frame = start_frame,
  362. .draw_slice = draw_slice,
  363. .end_frame = end_frame
  364. },
  365. { NULL }
  366. };
  367. static const AVFilterPad avfilter_vf_select_outputs[] = {
  368. {
  369. .name = "default",
  370. .type = AVMEDIA_TYPE_VIDEO,
  371. .poll_frame = poll_frame,
  372. .request_frame = request_frame,
  373. },
  374. { NULL }
  375. };
  376. AVFilter avfilter_vf_select = {
  377. .name = "select",
  378. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  379. .init = init,
  380. .uninit = uninit,
  381. .query_formats = query_formats,
  382. .priv_size = sizeof(SelectContext),
  383. .inputs = avfilter_vf_select_inputs,
  384. .outputs = avfilter_vf_select_outputs,
  385. };