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.

404 lines
12KB

  1. /*
  2. * Copyright (c) 2013 Nicolas George
  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 License
  8. * 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "filters.h"
  24. #include "framesync.h"
  25. #include "internal.h"
  26. #define OFFSET(member) offsetof(FFFrameSync, member)
  27. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  28. static const char *framesync_name(void *ptr)
  29. {
  30. return "framesync";
  31. }
  32. static const AVOption framesync_options[] = {
  33. { "eof_action", "Action to take when encountering EOF from secondary input ",
  34. OFFSET(opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
  35. EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
  36. { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
  37. { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
  38. { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
  39. { "shortest", "force termination when the shortest input terminates", OFFSET(opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  40. { "repeatlast", "extend last frame of secondary streams beyond EOF", OFFSET(opt_repeatlast), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  41. { NULL }
  42. };
  43. static const AVClass framesync_class = {
  44. .version = LIBAVUTIL_VERSION_INT,
  45. .class_name = "framesync",
  46. .item_name = framesync_name,
  47. .category = AV_CLASS_CATEGORY_FILTER,
  48. .option = framesync_options,
  49. .parent_log_context_offset = OFFSET(parent),
  50. };
  51. enum {
  52. STATE_BOF,
  53. STATE_RUN,
  54. STATE_EOF,
  55. };
  56. static int consume_from_fifos(FFFrameSync *fs);
  57. const AVClass *ff_framesync_get_class(void)
  58. {
  59. return &framesync_class;
  60. }
  61. void ff_framesync_preinit(FFFrameSync *fs)
  62. {
  63. if (fs->class)
  64. return;
  65. fs->class = &framesync_class;
  66. av_opt_set_defaults(fs);
  67. }
  68. int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
  69. {
  70. /* For filters with several outputs, we will not be able to assume which
  71. output is relevant for ff_outlink_frame_wanted() and
  72. ff_outlink_set_status(). To be designed when needed. */
  73. av_assert0(parent->nb_outputs == 1);
  74. ff_framesync_preinit(fs);
  75. fs->parent = parent;
  76. fs->nb_in = nb_in;
  77. fs->in = av_calloc(nb_in, sizeof(*fs->in));
  78. if (!fs->in)
  79. return AVERROR(ENOMEM);
  80. return 0;
  81. }
  82. static void framesync_eof(FFFrameSync *fs)
  83. {
  84. fs->eof = 1;
  85. fs->frame_ready = 0;
  86. ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
  87. }
  88. static void framesync_sync_level_update(FFFrameSync *fs)
  89. {
  90. unsigned i, level = 0;
  91. for (i = 0; i < fs->nb_in; i++)
  92. if (fs->in[i].state != STATE_EOF)
  93. level = FFMAX(level, fs->in[i].sync);
  94. av_assert0(level <= fs->sync_level);
  95. if (level < fs->sync_level)
  96. av_log(fs, AV_LOG_VERBOSE, "Sync level %u\n", level);
  97. if (level)
  98. fs->sync_level = level;
  99. else
  100. framesync_eof(fs);
  101. }
  102. int ff_framesync_configure(FFFrameSync *fs)
  103. {
  104. unsigned i;
  105. if (!fs->opt_repeatlast || fs->opt_eof_action == EOF_ACTION_PASS) {
  106. fs->opt_repeatlast = 0;
  107. fs->opt_eof_action = EOF_ACTION_PASS;
  108. }
  109. if (fs->opt_shortest || fs->opt_eof_action == EOF_ACTION_ENDALL) {
  110. fs->opt_shortest = 1;
  111. fs->opt_eof_action = EOF_ACTION_ENDALL;
  112. }
  113. if (!fs->opt_repeatlast) {
  114. for (i = 1; i < fs->nb_in; i++) {
  115. fs->in[i].after = EXT_NULL;
  116. fs->in[i].sync = 0;
  117. }
  118. }
  119. if (fs->opt_shortest) {
  120. for (i = 0; i < fs->nb_in; i++)
  121. fs->in[i].after = EXT_STOP;
  122. }
  123. if (!fs->time_base.num) {
  124. for (i = 0; i < fs->nb_in; i++) {
  125. if (fs->in[i].sync) {
  126. if (fs->time_base.num) {
  127. fs->time_base = av_gcd_q(fs->time_base, fs->in[i].time_base,
  128. AV_TIME_BASE / 2, AV_TIME_BASE_Q);
  129. } else {
  130. fs->time_base = fs->in[i].time_base;
  131. }
  132. }
  133. }
  134. if (!fs->time_base.num) {
  135. av_log(fs, AV_LOG_ERROR, "Impossible to set time base\n");
  136. return AVERROR(EINVAL);
  137. }
  138. av_log(fs, AV_LOG_VERBOSE, "Selected %d/%d time base\n",
  139. fs->time_base.num, fs->time_base.den);
  140. }
  141. for (i = 0; i < fs->nb_in; i++)
  142. fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
  143. fs->sync_level = UINT_MAX;
  144. framesync_sync_level_update(fs);
  145. return 0;
  146. }
  147. static int framesync_advance(FFFrameSync *fs)
  148. {
  149. unsigned i;
  150. int64_t pts;
  151. int ret;
  152. while (!(fs->frame_ready || fs->eof)) {
  153. ret = consume_from_fifos(fs);
  154. if (ret <= 0)
  155. return ret;
  156. pts = INT64_MAX;
  157. for (i = 0; i < fs->nb_in; i++)
  158. if (fs->in[i].have_next && fs->in[i].pts_next < pts)
  159. pts = fs->in[i].pts_next;
  160. if (pts == INT64_MAX) {
  161. framesync_eof(fs);
  162. break;
  163. }
  164. for (i = 0; i < fs->nb_in; i++) {
  165. if (fs->in[i].pts_next == pts ||
  166. (fs->in[i].before == EXT_INFINITY &&
  167. fs->in[i].state == STATE_BOF)) {
  168. av_frame_free(&fs->in[i].frame);
  169. fs->in[i].frame = fs->in[i].frame_next;
  170. fs->in[i].pts = fs->in[i].pts_next;
  171. fs->in[i].frame_next = NULL;
  172. fs->in[i].pts_next = AV_NOPTS_VALUE;
  173. fs->in[i].have_next = 0;
  174. fs->in[i].state = fs->in[i].frame ? STATE_RUN : STATE_EOF;
  175. if (fs->in[i].sync == fs->sync_level && fs->in[i].frame)
  176. fs->frame_ready = 1;
  177. if (fs->in[i].state == STATE_EOF &&
  178. fs->in[i].after == EXT_STOP)
  179. framesync_eof(fs);
  180. }
  181. }
  182. if (fs->frame_ready)
  183. for (i = 0; i < fs->nb_in; i++)
  184. if ((fs->in[i].state == STATE_BOF &&
  185. fs->in[i].before == EXT_STOP))
  186. fs->frame_ready = 0;
  187. fs->pts = pts;
  188. }
  189. return 0;
  190. }
  191. static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in,
  192. int64_t pts)
  193. {
  194. /* Possible enhancement: use the link's frame rate */
  195. return pts + 1;
  196. }
  197. static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
  198. {
  199. int64_t pts;
  200. av_assert0(!fs->in[in].have_next);
  201. av_assert0(frame);
  202. pts = av_rescale_q(frame->pts, fs->in[in].time_base, fs->time_base);
  203. frame->pts = pts;
  204. fs->in[in].frame_next = frame;
  205. fs->in[in].pts_next = pts;
  206. fs->in[in].have_next = 1;
  207. }
  208. static void framesync_inject_status(FFFrameSync *fs, unsigned in, int status, int64_t pts)
  209. {
  210. av_assert0(!fs->in[in].have_next);
  211. pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
  212. ? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
  213. fs->in[in].sync = 0;
  214. framesync_sync_level_update(fs);
  215. fs->in[in].frame_next = NULL;
  216. fs->in[in].pts_next = pts;
  217. fs->in[in].have_next = 1;
  218. }
  219. int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
  220. unsigned get)
  221. {
  222. AVFrame *frame;
  223. unsigned need_copy = 0, i;
  224. int64_t pts_next;
  225. int ret;
  226. if (!fs->in[in].frame) {
  227. *rframe = NULL;
  228. return 0;
  229. }
  230. frame = fs->in[in].frame;
  231. if (get) {
  232. /* Find out if we need to copy the frame: is there another sync
  233. stream, and do we know if its current frame will outlast this one? */
  234. pts_next = fs->in[in].have_next ? fs->in[in].pts_next : INT64_MAX;
  235. for (i = 0; i < fs->nb_in && !need_copy; i++)
  236. if (i != in && fs->in[i].sync &&
  237. (!fs->in[i].have_next || fs->in[i].pts_next < pts_next))
  238. need_copy = 1;
  239. if (need_copy) {
  240. if (!(frame = av_frame_clone(frame)))
  241. return AVERROR(ENOMEM);
  242. if ((ret = av_frame_make_writable(frame)) < 0) {
  243. av_frame_free(&frame);
  244. return ret;
  245. }
  246. } else {
  247. fs->in[in].frame = NULL;
  248. }
  249. fs->frame_ready = 0;
  250. }
  251. *rframe = frame;
  252. return 0;
  253. }
  254. void ff_framesync_uninit(FFFrameSync *fs)
  255. {
  256. unsigned i;
  257. for (i = 0; i < fs->nb_in; i++) {
  258. av_frame_free(&fs->in[i].frame);
  259. av_frame_free(&fs->in[i].frame_next);
  260. }
  261. av_freep(&fs->in);
  262. }
  263. static int consume_from_fifos(FFFrameSync *fs)
  264. {
  265. AVFilterContext *ctx = fs->parent;
  266. AVFrame *frame = NULL;
  267. int64_t pts;
  268. unsigned i, nb_active, nb_miss;
  269. int ret, status;
  270. nb_active = nb_miss = 0;
  271. for (i = 0; i < fs->nb_in; i++) {
  272. if (fs->in[i].have_next || fs->in[i].state == STATE_EOF)
  273. continue;
  274. nb_active++;
  275. ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
  276. if (ret < 0)
  277. return ret;
  278. if (ret) {
  279. av_assert0(frame);
  280. framesync_inject_frame(fs, i, frame);
  281. } else {
  282. ret = ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts);
  283. if (ret > 0) {
  284. framesync_inject_status(fs, i, status, pts);
  285. } else if (!ret) {
  286. nb_miss++;
  287. }
  288. }
  289. }
  290. if (nb_miss) {
  291. if (nb_miss == nb_active && !ff_outlink_frame_wanted(ctx->outputs[0]))
  292. return FFERROR_NOT_READY;
  293. for (i = 0; i < fs->nb_in; i++)
  294. if (!fs->in[i].have_next && fs->in[i].state != STATE_EOF)
  295. ff_inlink_request_frame(ctx->inputs[i]);
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. int ff_framesync_activate(FFFrameSync *fs)
  301. {
  302. int ret;
  303. ret = framesync_advance(fs);
  304. if (ret < 0)
  305. return ret;
  306. if (fs->eof || !fs->frame_ready)
  307. return 0;
  308. ret = fs->on_event(fs);
  309. if (ret < 0)
  310. return ret;
  311. fs->frame_ready = 0;
  312. return 0;
  313. }
  314. int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
  315. {
  316. int ret;
  317. ret = ff_framesync_init(fs, parent, 2);
  318. if (ret < 0)
  319. return ret;
  320. fs->in[0].time_base = parent->inputs[0]->time_base;
  321. fs->in[1].time_base = parent->inputs[1]->time_base;
  322. fs->in[0].sync = 2;
  323. fs->in[0].before = EXT_STOP;
  324. fs->in[0].after = EXT_INFINITY;
  325. fs->in[1].sync = 1;
  326. fs->in[1].before = EXT_NULL;
  327. fs->in[1].after = EXT_INFINITY;
  328. return 0;
  329. }
  330. int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
  331. {
  332. AVFilterContext *ctx = fs->parent;
  333. AVFrame *mainpic = NULL, *secondpic = NULL;
  334. int ret;
  335. if ((ret = ff_framesync_get_frame(fs, 0, &mainpic, 1)) < 0 ||
  336. (ret = ff_framesync_get_frame(fs, 1, &secondpic, 0)) < 0) {
  337. av_frame_free(&mainpic);
  338. return ret;
  339. }
  340. av_assert0(mainpic);
  341. mainpic->pts = av_rescale_q(fs->pts, fs->time_base, ctx->outputs[0]->time_base);
  342. if (ctx->is_disabled)
  343. secondpic = NULL;
  344. *f0 = mainpic;
  345. *f1 = secondpic;
  346. return 0;
  347. }
  348. int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
  349. {
  350. int ret;
  351. ret = ff_framesync_dualinput_get(fs, f0, f1);
  352. if (ret < 0)
  353. return ret;
  354. ret = ff_inlink_make_frame_writable(fs->parent->inputs[0], f0);
  355. if (ret < 0) {
  356. av_frame_free(f0);
  357. *f1 = NULL;
  358. return ret;
  359. }
  360. return 0;
  361. }