LCOV - code coverage report
Current view: top level - src/libavfilter - framesync.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 196 230 85.2 %
Date: 2017-09-22 10:31:19 Functions: 17 18 94.4 %

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

Generated by: LCOV version 1.13