vf_select.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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;
  171. int64_t sad;
  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 (sad = y = 0; y < picref->video->h; y += 8)
  177. for (x = 0; x < linesize; x += 8)
  178. sad += select->c.sad[1](select,
  179. p1 + y * linesize + x,
  180. p2 + y * linesize + x,
  181. linesize, 8);
  182. emms_c();
  183. mafd = sad / (picref->video->h * picref->video->w * 3);
  184. diff = fabs(mafd - select->prev_mafd);
  185. ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
  186. select->prev_mafd = mafd;
  187. avfilter_unref_buffer(prev_picref);
  188. }
  189. select->prev_picref = avfilter_ref_buffer(picref, ~0);
  190. return ret;
  191. }
  192. #endif
  193. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  194. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  195. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
  196. {
  197. SelectContext *select = ctx->priv;
  198. AVFilterLink *inlink = ctx->inputs[0];
  199. double res;
  200. if (CONFIG_AVCODEC && select->do_scene_detect)
  201. select->var_values[VAR_SCENE] = get_scene_score(ctx, picref);
  202. if (isnan(select->var_values[VAR_START_PTS]))
  203. select->var_values[VAR_START_PTS] = TS2D(picref->pts);
  204. if (isnan(select->var_values[VAR_START_T]))
  205. select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  206. select->var_values[VAR_PTS] = TS2D(picref->pts);
  207. select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  208. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  209. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  210. select->var_values[VAR_INTERLACE_TYPE] =
  211. !picref->video->interlaced ? INTERLACE_TYPE_P :
  212. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  213. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  214. res = av_expr_eval(select->expr, select->var_values, NULL);
  215. av_log(inlink->dst, AV_LOG_DEBUG,
  216. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  217. "-> select:%f\n",
  218. (int)select->var_values[VAR_N],
  219. (int)select->var_values[VAR_PTS],
  220. select->var_values[VAR_T],
  221. (int)select->var_values[VAR_POS],
  222. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  223. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  224. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  225. (int)select->var_values[VAR_KEY],
  226. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  227. res);
  228. select->var_values[VAR_N] += 1.0;
  229. if (res) {
  230. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  231. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  232. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  233. select->var_values[VAR_SELECTED_N] += 1.0;
  234. }
  235. return res;
  236. }
  237. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  238. {
  239. SelectContext *select = inlink->dst->priv;
  240. select->select = select_frame(inlink->dst, picref);
  241. if (select->select) {
  242. AVFilterBufferRef *buf_out;
  243. /* frame was requested through poll_frame */
  244. if (select->cache_frames) {
  245. if (!av_fifo_space(select->pending_frames))
  246. av_log(inlink->dst, AV_LOG_ERROR,
  247. "Buffering limit reached, cannot cache more frames\n");
  248. else
  249. av_fifo_generic_write(select->pending_frames, &picref,
  250. sizeof(picref), NULL);
  251. return 0;
  252. }
  253. buf_out = avfilter_ref_buffer(picref, ~0);
  254. if (!buf_out)
  255. return AVERROR(ENOMEM);
  256. return ff_start_frame(inlink->dst->outputs[0], buf_out);
  257. }
  258. return 0;
  259. }
  260. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  261. {
  262. SelectContext *select = inlink->dst->priv;
  263. if (select->select && !select->cache_frames)
  264. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  265. return 0;
  266. }
  267. static int end_frame(AVFilterLink *inlink)
  268. {
  269. SelectContext *select = inlink->dst->priv;
  270. if (select->select) {
  271. if (select->cache_frames)
  272. return 0;
  273. return ff_end_frame(inlink->dst->outputs[0]);
  274. }
  275. return 0;
  276. }
  277. static int request_frame(AVFilterLink *outlink)
  278. {
  279. AVFilterContext *ctx = outlink->src;
  280. SelectContext *select = ctx->priv;
  281. AVFilterLink *inlink = outlink->src->inputs[0];
  282. select->select = 0;
  283. if (av_fifo_size(select->pending_frames)) {
  284. AVFilterBufferRef *picref;
  285. int ret;
  286. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  287. if ((ret = ff_start_frame(outlink, picref)) < 0 ||
  288. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  289. (ret = ff_end_frame(outlink)) < 0);
  290. return ret;
  291. }
  292. while (!select->select) {
  293. int ret = ff_request_frame(inlink);
  294. if (ret < 0)
  295. return ret;
  296. }
  297. return 0;
  298. }
  299. static int poll_frame(AVFilterLink *outlink)
  300. {
  301. SelectContext *select = outlink->src->priv;
  302. AVFilterLink *inlink = outlink->src->inputs[0];
  303. int count, ret;
  304. if (!av_fifo_size(select->pending_frames)) {
  305. if ((count = ff_poll_frame(inlink)) <= 0)
  306. return count;
  307. /* request frame from input, and apply select condition to it */
  308. select->cache_frames = 1;
  309. while (count-- && av_fifo_space(select->pending_frames)) {
  310. ret = ff_request_frame(inlink);
  311. if (ret < 0)
  312. break;
  313. }
  314. select->cache_frames = 0;
  315. }
  316. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  317. }
  318. static av_cold void uninit(AVFilterContext *ctx)
  319. {
  320. SelectContext *select = ctx->priv;
  321. AVFilterBufferRef *picref;
  322. av_expr_free(select->expr);
  323. select->expr = NULL;
  324. while (select->pending_frames &&
  325. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
  326. avfilter_unref_buffer(picref);
  327. av_fifo_free(select->pending_frames);
  328. select->pending_frames = NULL;
  329. if (select->do_scene_detect) {
  330. avfilter_unref_bufferp(&select->prev_picref);
  331. if (select->avctx) {
  332. avcodec_close(select->avctx);
  333. av_freep(&select->avctx);
  334. }
  335. }
  336. }
  337. static int query_formats(AVFilterContext *ctx)
  338. {
  339. SelectContext *select = ctx->priv;
  340. if (!select->do_scene_detect) {
  341. return ff_default_query_formats(ctx);
  342. } else {
  343. static const enum PixelFormat pix_fmts[] = {
  344. PIX_FMT_RGB24, PIX_FMT_BGR24,
  345. PIX_FMT_NONE
  346. };
  347. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  348. }
  349. return 0;
  350. }
  351. AVFilter avfilter_vf_select = {
  352. .name = "select",
  353. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  354. .init = init,
  355. .uninit = uninit,
  356. .query_formats = query_formats,
  357. .priv_size = sizeof(SelectContext),
  358. .inputs = (const AVFilterPad[]) {{ .name = "default",
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. .get_video_buffer = ff_null_get_video_buffer,
  361. .min_perms = AV_PERM_PRESERVE,
  362. .config_props = config_input,
  363. .start_frame = start_frame,
  364. .draw_slice = draw_slice,
  365. .end_frame = end_frame },
  366. { .name = NULL }},
  367. .outputs = (const AVFilterPad[]) {{ .name = "default",
  368. .type = AVMEDIA_TYPE_VIDEO,
  369. .poll_frame = poll_frame,
  370. .request_frame = request_frame, },
  371. { .name = NULL}},
  372. };