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.

420 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 "libavcodec/dsputil.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "video.h"
  30. static const char *const var_names[] = {
  31. "TB", ///< timebase
  32. "pts", ///< original pts in the file of the frame
  33. "start_pts", ///< first PTS in the stream, expressed in TB units
  34. "prev_pts", ///< previous frame PTS
  35. "prev_selected_pts", ///< previous selected frame PTS
  36. "t", ///< first PTS in seconds
  37. "start_t", ///< first PTS in the stream, expressed in seconds
  38. "prev_t", ///< previous frame time
  39. "prev_selected_t", ///< previously selected time
  40. "pict_type", ///< the type of picture in the movie
  41. "I",
  42. "P",
  43. "B",
  44. "S",
  45. "SI",
  46. "SP",
  47. "BI",
  48. "interlace_type", ///< the frame interlace type
  49. "PROGRESSIVE",
  50. "TOPFIRST",
  51. "BOTTOMFIRST",
  52. "n", ///< frame number (starting from zero)
  53. "selected_n", ///< selected frame number (starting from zero)
  54. "prev_selected_n", ///< number of the last selected frame
  55. "key", ///< tell if the frame is a key frame
  56. "pos", ///< original position in the file of the frame
  57. "scene",
  58. NULL
  59. };
  60. enum var_name {
  61. VAR_TB,
  62. VAR_PTS,
  63. VAR_START_PTS,
  64. VAR_PREV_PTS,
  65. VAR_PREV_SELECTED_PTS,
  66. VAR_T,
  67. VAR_START_T,
  68. VAR_PREV_T,
  69. VAR_PREV_SELECTED_T,
  70. VAR_PICT_TYPE,
  71. VAR_PICT_TYPE_I,
  72. VAR_PICT_TYPE_P,
  73. VAR_PICT_TYPE_B,
  74. VAR_PICT_TYPE_S,
  75. VAR_PICT_TYPE_SI,
  76. VAR_PICT_TYPE_SP,
  77. VAR_PICT_TYPE_BI,
  78. VAR_INTERLACE_TYPE,
  79. VAR_INTERLACE_TYPE_P,
  80. VAR_INTERLACE_TYPE_T,
  81. VAR_INTERLACE_TYPE_B,
  82. VAR_N,
  83. VAR_SELECTED_N,
  84. VAR_PREV_SELECTED_N,
  85. VAR_KEY,
  86. VAR_POS,
  87. VAR_SCENE,
  88. VAR_VARS_NB
  89. };
  90. #define FIFO_SIZE 8
  91. typedef struct {
  92. AVExpr *expr;
  93. double var_values[VAR_VARS_NB];
  94. int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
  95. AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
  96. DSPContext c; ///< context providing optimized SAD methods (scene detect only)
  97. double prev_mafd; ///< previous MAFD (scene detect only)
  98. AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
  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, void *opaque)
  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. select->do_scene_detect = args && strstr(args, "scene");
  118. return 0;
  119. }
  120. #define INTERLACE_TYPE_P 0
  121. #define INTERLACE_TYPE_T 1
  122. #define INTERLACE_TYPE_B 2
  123. static int config_input(AVFilterLink *inlink)
  124. {
  125. SelectContext *select = inlink->dst->priv;
  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. if (select->do_scene_detect) {
  143. select->avctx = avcodec_alloc_context3(NULL);
  144. if (!select->avctx)
  145. return AVERROR(ENOMEM);
  146. dsputil_init(&select->c, select->avctx);
  147. }
  148. return 0;
  149. }
  150. static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
  151. {
  152. double ret = 0;
  153. SelectContext *select = ctx->priv;
  154. AVFilterBufferRef *prev_picref = select->prev_picref;
  155. if (prev_picref &&
  156. picref->video->h == prev_picref->video->h &&
  157. picref->video->w == prev_picref->video->w &&
  158. picref->linesize[0] == prev_picref->linesize[0]) {
  159. int x, y;
  160. int64_t sad;
  161. double mafd, diff;
  162. uint8_t *p1 = picref->data[0];
  163. uint8_t *p2 = prev_picref->data[0];
  164. const int linesize = picref->linesize[0];
  165. for (sad = y = 0; y < picref->video->h; y += 8)
  166. for (x = 0; x < linesize; x += 8)
  167. sad += select->c.sad[1](select,
  168. p1 + y * linesize + x,
  169. p2 + y * linesize + x,
  170. linesize, 8);
  171. emms_c();
  172. mafd = sad / (picref->video->h * picref->video->w * 3);
  173. diff = fabs(mafd - select->prev_mafd);
  174. ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
  175. select->prev_mafd = mafd;
  176. avfilter_unref_buffer(prev_picref);
  177. }
  178. select->prev_picref = avfilter_ref_buffer(picref, ~0);
  179. return ret;
  180. }
  181. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  182. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  183. static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
  184. {
  185. SelectContext *select = ctx->priv;
  186. AVFilterLink *inlink = ctx->inputs[0];
  187. double res;
  188. if (select->do_scene_detect)
  189. select->var_values[VAR_SCENE] = get_scene_score(ctx, picref);
  190. if (isnan(select->var_values[VAR_START_PTS]))
  191. select->var_values[VAR_START_PTS] = TS2D(picref->pts);
  192. if (isnan(select->var_values[VAR_START_T]))
  193. select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  194. select->var_values[VAR_PTS] = TS2D(picref->pts);
  195. select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
  196. select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
  197. select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
  198. select->var_values[VAR_INTERLACE_TYPE] =
  199. !picref->video->interlaced ? INTERLACE_TYPE_P :
  200. picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
  201. select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
  202. res = av_expr_eval(select->expr, select->var_values, NULL);
  203. av_log(inlink->dst, AV_LOG_DEBUG,
  204. "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
  205. "-> select:%f\n",
  206. (int)select->var_values[VAR_N],
  207. (int)select->var_values[VAR_PTS],
  208. select->var_values[VAR_T],
  209. (int)select->var_values[VAR_POS],
  210. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
  211. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
  212. select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
  213. (int)select->var_values[VAR_KEY],
  214. av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
  215. res);
  216. select->var_values[VAR_N] += 1.0;
  217. if (res) {
  218. select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
  219. select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
  220. select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
  221. select->var_values[VAR_SELECTED_N] += 1.0;
  222. }
  223. return res;
  224. }
  225. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  226. {
  227. SelectContext *select = inlink->dst->priv;
  228. select->select = select_frame(inlink->dst, picref);
  229. if (select->select) {
  230. /* frame was requested through poll_frame */
  231. if (select->cache_frames) {
  232. if (!av_fifo_space(select->pending_frames))
  233. av_log(inlink->dst, AV_LOG_ERROR,
  234. "Buffering limit reached, cannot cache more frames\n");
  235. else
  236. av_fifo_generic_write(select->pending_frames, &picref,
  237. sizeof(picref), NULL);
  238. return;
  239. }
  240. avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
  241. }
  242. }
  243. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  244. {
  245. SelectContext *select = inlink->dst->priv;
  246. if (select->select && !select->cache_frames)
  247. avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  248. }
  249. static void end_frame(AVFilterLink *inlink)
  250. {
  251. SelectContext *select = inlink->dst->priv;
  252. AVFilterBufferRef *picref = inlink->cur_buf;
  253. if (select->select) {
  254. if (select->cache_frames)
  255. return;
  256. avfilter_end_frame(inlink->dst->outputs[0]);
  257. }
  258. avfilter_unref_buffer(picref);
  259. }
  260. static int request_frame(AVFilterLink *outlink)
  261. {
  262. AVFilterContext *ctx = outlink->src;
  263. SelectContext *select = ctx->priv;
  264. AVFilterLink *inlink = outlink->src->inputs[0];
  265. select->select = 0;
  266. if (av_fifo_size(select->pending_frames)) {
  267. AVFilterBufferRef *picref;
  268. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
  269. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  270. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  271. avfilter_end_frame(outlink);
  272. avfilter_unref_buffer(picref);
  273. return 0;
  274. }
  275. while (!select->select) {
  276. int ret = avfilter_request_frame(inlink);
  277. if (ret < 0)
  278. return ret;
  279. }
  280. return 0;
  281. }
  282. static int poll_frame(AVFilterLink *outlink)
  283. {
  284. SelectContext *select = outlink->src->priv;
  285. AVFilterLink *inlink = outlink->src->inputs[0];
  286. int count, ret;
  287. if (!av_fifo_size(select->pending_frames)) {
  288. if ((count = avfilter_poll_frame(inlink)) <= 0)
  289. return count;
  290. /* request frame from input, and apply select condition to it */
  291. select->cache_frames = 1;
  292. while (count-- && av_fifo_space(select->pending_frames)) {
  293. ret = avfilter_request_frame(inlink);
  294. if (ret < 0)
  295. break;
  296. }
  297. select->cache_frames = 0;
  298. }
  299. return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
  300. }
  301. static av_cold void uninit(AVFilterContext *ctx)
  302. {
  303. SelectContext *select = ctx->priv;
  304. AVFilterBufferRef *picref;
  305. av_expr_free(select->expr);
  306. select->expr = NULL;
  307. while (select->pending_frames &&
  308. av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
  309. avfilter_unref_buffer(picref);
  310. av_fifo_free(select->pending_frames);
  311. select->pending_frames = NULL;
  312. if (select->do_scene_detect) {
  313. avfilter_unref_bufferp(&select->prev_picref);
  314. avcodec_close(select->avctx);
  315. av_freep(&select->avctx);
  316. }
  317. }
  318. static int query_formats(AVFilterContext *ctx)
  319. {
  320. SelectContext *select = ctx->priv;
  321. if (!select->do_scene_detect) {
  322. return ff_default_query_formats(ctx);
  323. } else {
  324. static const enum PixelFormat pix_fmts[] = {
  325. PIX_FMT_RGB24, PIX_FMT_BGR24,
  326. PIX_FMT_NONE
  327. };
  328. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  329. }
  330. return 0;
  331. }
  332. AVFilter avfilter_vf_select = {
  333. .name = "select",
  334. .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
  335. .init = init,
  336. .uninit = uninit,
  337. .query_formats = query_formats,
  338. .priv_size = sizeof(SelectContext),
  339. .inputs = (const AVFilterPad[]) {{ .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. .get_video_buffer = ff_null_get_video_buffer,
  342. .config_props = config_input,
  343. .start_frame = start_frame,
  344. .draw_slice = draw_slice,
  345. .end_frame = end_frame },
  346. { .name = NULL }},
  347. .outputs = (const AVFilterPad[]) {{ .name = "default",
  348. .type = AVMEDIA_TYPE_VIDEO,
  349. .poll_frame = poll_frame,
  350. .request_frame = request_frame, },
  351. { .name = NULL}},
  352. };