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.

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