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.

314 lines
9.4KB

  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 "avfilter.h"
  22. #include "filters.h"
  23. #include "framesync2.h"
  24. #include "internal.h"
  25. #define OFFSET(member) offsetof(FFFrameSync, member)
  26. static const char *framesync_name(void *ptr)
  27. {
  28. return "framesync";
  29. }
  30. static const AVClass framesync_class = {
  31. .version = LIBAVUTIL_VERSION_INT,
  32. .class_name = "framesync",
  33. .item_name = framesync_name,
  34. .category = AV_CLASS_CATEGORY_FILTER,
  35. .option = NULL,
  36. .parent_log_context_offset = OFFSET(parent),
  37. };
  38. enum {
  39. STATE_BOF,
  40. STATE_RUN,
  41. STATE_EOF,
  42. };
  43. int ff_framesync2_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
  44. {
  45. /* For filters with several outputs, we will not be able to assume which
  46. output is relevant for ff_outlink_frame_wanted() and
  47. ff_outlink_set_status(). To be designed when needed. */
  48. av_assert0(parent->nb_outputs == 1);
  49. fs->class = &framesync_class;
  50. fs->parent = parent;
  51. fs->nb_in = nb_in;
  52. fs->in = av_calloc(nb_in, sizeof(*fs->in));
  53. if (!fs->in)
  54. return AVERROR(ENOMEM);
  55. return 0;
  56. }
  57. static void framesync_eof(FFFrameSync *fs)
  58. {
  59. fs->eof = 1;
  60. fs->frame_ready = 0;
  61. ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
  62. }
  63. static void framesync_sync_level_update(FFFrameSync *fs)
  64. {
  65. unsigned i, level = 0;
  66. for (i = 0; i < fs->nb_in; i++)
  67. if (fs->in[i].state != STATE_EOF)
  68. level = FFMAX(level, fs->in[i].sync);
  69. av_assert0(level <= fs->sync_level);
  70. if (level < fs->sync_level)
  71. av_log(fs, AV_LOG_VERBOSE, "Sync level %u\n", level);
  72. if (level)
  73. fs->sync_level = level;
  74. else
  75. framesync_eof(fs);
  76. }
  77. int ff_framesync2_configure(FFFrameSync *fs)
  78. {
  79. unsigned i;
  80. int64_t gcd, lcm;
  81. if (!fs->time_base.num) {
  82. for (i = 0; i < fs->nb_in; i++) {
  83. if (fs->in[i].sync) {
  84. if (fs->time_base.num) {
  85. gcd = av_gcd(fs->time_base.den, fs->in[i].time_base.den);
  86. lcm = (fs->time_base.den / gcd) * fs->in[i].time_base.den;
  87. if (lcm < AV_TIME_BASE / 2) {
  88. fs->time_base.den = lcm;
  89. fs->time_base.num = av_gcd(fs->time_base.num,
  90. fs->in[i].time_base.num);
  91. } else {
  92. fs->time_base.num = 1;
  93. fs->time_base.den = AV_TIME_BASE;
  94. break;
  95. }
  96. } else {
  97. fs->time_base = fs->in[i].time_base;
  98. }
  99. }
  100. }
  101. if (!fs->time_base.num) {
  102. av_log(fs, AV_LOG_ERROR, "Impossible to set time base\n");
  103. return AVERROR(EINVAL);
  104. }
  105. av_log(fs, AV_LOG_VERBOSE, "Selected %d/%d time base\n",
  106. fs->time_base.num, fs->time_base.den);
  107. }
  108. for (i = 0; i < fs->nb_in; i++)
  109. fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
  110. fs->sync_level = UINT_MAX;
  111. framesync_sync_level_update(fs);
  112. return 0;
  113. }
  114. static void framesync_advance(FFFrameSync *fs)
  115. {
  116. int latest;
  117. unsigned i;
  118. int64_t pts;
  119. if (fs->eof)
  120. return;
  121. while (!fs->frame_ready) {
  122. latest = -1;
  123. for (i = 0; i < fs->nb_in; i++) {
  124. if (!fs->in[i].have_next) {
  125. if (latest < 0 || fs->in[i].pts < fs->in[latest].pts)
  126. latest = i;
  127. }
  128. }
  129. if (latest >= 0) {
  130. fs->in_request = latest;
  131. break;
  132. }
  133. pts = fs->in[0].pts_next;
  134. for (i = 1; i < fs->nb_in; i++)
  135. if (fs->in[i].pts_next < pts)
  136. pts = fs->in[i].pts_next;
  137. if (pts == INT64_MAX) {
  138. framesync_eof(fs);
  139. break;
  140. }
  141. for (i = 0; i < fs->nb_in; i++) {
  142. if (fs->in[i].pts_next == pts ||
  143. (fs->in[i].before == EXT_INFINITY &&
  144. fs->in[i].state == STATE_BOF)) {
  145. av_frame_free(&fs->in[i].frame);
  146. fs->in[i].frame = fs->in[i].frame_next;
  147. fs->in[i].pts = fs->in[i].pts_next;
  148. fs->in[i].frame_next = NULL;
  149. fs->in[i].pts_next = AV_NOPTS_VALUE;
  150. fs->in[i].have_next = 0;
  151. fs->in[i].state = fs->in[i].frame ? STATE_RUN : STATE_EOF;
  152. if (fs->in[i].sync == fs->sync_level && fs->in[i].frame)
  153. fs->frame_ready = 1;
  154. if (fs->in[i].state == STATE_EOF &&
  155. fs->in[i].after == EXT_STOP)
  156. framesync_eof(fs);
  157. }
  158. }
  159. if (fs->frame_ready)
  160. for (i = 0; i < fs->nb_in; i++)
  161. if ((fs->in[i].state == STATE_BOF &&
  162. fs->in[i].before == EXT_STOP))
  163. fs->frame_ready = 0;
  164. fs->pts = pts;
  165. }
  166. }
  167. static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in,
  168. int64_t pts)
  169. {
  170. /* Possible enhancement: use the link's frame rate */
  171. return pts + 1;
  172. }
  173. static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
  174. {
  175. int64_t pts;
  176. av_assert0(!fs->in[in].have_next);
  177. av_assert0(frame);
  178. pts = av_rescale_q(frame->pts, fs->in[in].time_base, fs->time_base);
  179. frame->pts = pts;
  180. fs->in[in].frame_next = frame;
  181. fs->in[in].pts_next = pts;
  182. fs->in[in].have_next = 1;
  183. }
  184. static void framesync_inject_status(FFFrameSync *fs, unsigned in, int status, int64_t pts)
  185. {
  186. av_assert0(!fs->in[in].have_next);
  187. pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
  188. ? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
  189. fs->in[in].sync = 0;
  190. framesync_sync_level_update(fs);
  191. fs->in[in].frame_next = NULL;
  192. fs->in[in].pts_next = pts;
  193. fs->in[in].have_next = 1;
  194. }
  195. int ff_framesync2_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
  196. unsigned get)
  197. {
  198. AVFrame *frame;
  199. unsigned need_copy = 0, i;
  200. int64_t pts_next;
  201. int ret;
  202. if (!fs->in[in].frame) {
  203. *rframe = NULL;
  204. return 0;
  205. }
  206. frame = fs->in[in].frame;
  207. if (get) {
  208. /* Find out if we need to copy the frame: is there another sync
  209. stream, and do we know if its current frame will outlast this one? */
  210. pts_next = fs->in[in].have_next ? fs->in[in].pts_next : INT64_MAX;
  211. for (i = 0; i < fs->nb_in && !need_copy; i++)
  212. if (i != in && fs->in[i].sync &&
  213. (!fs->in[i].have_next || fs->in[i].pts_next < pts_next))
  214. need_copy = 1;
  215. if (need_copy) {
  216. if (!(frame = av_frame_clone(frame)))
  217. return AVERROR(ENOMEM);
  218. if ((ret = av_frame_make_writable(frame)) < 0) {
  219. av_frame_free(&frame);
  220. return ret;
  221. }
  222. } else {
  223. fs->in[in].frame = NULL;
  224. }
  225. fs->frame_ready = 0;
  226. }
  227. *rframe = frame;
  228. return 0;
  229. }
  230. void ff_framesync2_uninit(FFFrameSync *fs)
  231. {
  232. unsigned i;
  233. for (i = 0; i < fs->nb_in; i++) {
  234. av_frame_free(&fs->in[i].frame);
  235. av_frame_free(&fs->in[i].frame_next);
  236. }
  237. av_freep(&fs->in);
  238. }
  239. int ff_framesync2_activate(FFFrameSync *fs)
  240. {
  241. AVFilterContext *ctx = fs->parent;
  242. AVFrame *frame = NULL;
  243. int64_t pts;
  244. unsigned i, nb_active, nb_miss;
  245. int ret, status;
  246. nb_active = nb_miss = 0;
  247. for (i = 0; i < fs->nb_in; i++) {
  248. if (fs->in[i].have_next || fs->in[i].state == STATE_EOF)
  249. continue;
  250. nb_active++;
  251. ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
  252. if (ret < 0)
  253. return ret;
  254. if (ret) {
  255. av_assert0(frame);
  256. framesync_inject_frame(fs, i, frame);
  257. } else {
  258. ret = ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts);
  259. if (ret > 0) {
  260. framesync_inject_status(fs, i, status, pts);
  261. } else if (!ret) {
  262. nb_miss++;
  263. }
  264. }
  265. }
  266. if (nb_miss) {
  267. if (nb_miss == nb_active && !ff_outlink_frame_wanted(ctx->outputs[0]))
  268. return FFERROR_NOT_READY;
  269. for (i = 0; i < fs->nb_in; i++)
  270. if (!fs->in[i].have_next && fs->in[i].state != STATE_EOF)
  271. ff_inlink_request_frame(ctx->inputs[i]);
  272. return 0;
  273. }
  274. framesync_advance(fs);
  275. if (fs->eof || !fs->frame_ready)
  276. return 0;
  277. ret = fs->on_event(fs);
  278. if (ret < 0)
  279. return ret;
  280. fs->frame_ready = 0;
  281. return 0;
  282. }