LCOV - code coverage report
Current view: top level - src/libavfilter - f_streamselect.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 164 0.0 %
Date: 2017-09-22 00:04:37 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "libavutil/avstring.h"
      20             : #include "libavutil/internal.h"
      21             : #include "libavutil/opt.h"
      22             : #include "avfilter.h"
      23             : #include "audio.h"
      24             : #include "formats.h"
      25             : #include "framesync.h"
      26             : #include "internal.h"
      27             : #include "video.h"
      28             : 
      29             : typedef struct StreamSelectContext {
      30             :     const AVClass *class;
      31             :     int nb_inputs;
      32             :     char *map_str;
      33             :     int *map;
      34             :     int nb_map;
      35             :     int is_audio;
      36             :     int64_t *last_pts;
      37             :     AVFrame **frames;
      38             :     FFFrameSync fs;
      39             : } StreamSelectContext;
      40             : 
      41             : #define OFFSET(x) offsetof(StreamSelectContext, x)
      42             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
      43             : static const AVOption streamselect_options[] = {
      44             :     { "inputs",  "number of input streams",           OFFSET(nb_inputs),  AV_OPT_TYPE_INT,    {.i64=2},    2, INT_MAX,  .flags=FLAGS },
      45             :     { "map",     "input indexes to remap to outputs", OFFSET(map_str),    AV_OPT_TYPE_STRING, {.str=NULL},              .flags=FLAGS },
      46             :     { NULL }
      47             : };
      48             : 
      49             : AVFILTER_DEFINE_CLASS(streamselect);
      50             : 
      51           0 : static int process_frame(FFFrameSync *fs)
      52             : {
      53           0 :     AVFilterContext *ctx = fs->parent;
      54           0 :     StreamSelectContext *s = fs->opaque;
      55           0 :     AVFrame **in = s->frames;
      56           0 :     int i, j, ret = 0;
      57             : 
      58           0 :     for (i = 0; i < ctx->nb_inputs; i++) {
      59           0 :         if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
      60           0 :             return ret;
      61             :     }
      62             : 
      63           0 :     for (j = 0; j < ctx->nb_inputs; j++) {
      64           0 :         for (i = 0; i < s->nb_map; i++) {
      65           0 :             if (s->map[i] == j) {
      66             :                 AVFrame *out;
      67             : 
      68           0 :                 if (s->is_audio && s->last_pts[j] == in[j]->pts &&
      69           0 :                     ctx->outputs[i]->frame_count_in > 0)
      70           0 :                     continue;
      71           0 :                 out = av_frame_clone(in[j]);
      72           0 :                 if (!out)
      73           0 :                     return AVERROR(ENOMEM);
      74             : 
      75           0 :                 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
      76           0 :                 s->last_pts[j] = in[j]->pts;
      77           0 :                 ret = ff_filter_frame(ctx->outputs[i], out);
      78           0 :                 if (ret < 0)
      79           0 :                     return ret;
      80             :             }
      81             :         }
      82             :     }
      83             : 
      84           0 :     return ret;
      85             : }
      86             : 
      87           0 : static int activate(AVFilterContext *ctx)
      88             : {
      89           0 :     StreamSelectContext *s = ctx->priv;
      90           0 :     return ff_framesync_activate(&s->fs);
      91             : }
      92             : 
      93           0 : static int config_output(AVFilterLink *outlink)
      94             : {
      95           0 :     AVFilterContext *ctx = outlink->src;
      96           0 :     StreamSelectContext *s = ctx->priv;
      97           0 :     const int outlink_idx = FF_OUTLINK_IDX(outlink);
      98           0 :     const int inlink_idx  = s->map[outlink_idx];
      99           0 :     AVFilterLink *inlink = ctx->inputs[inlink_idx];
     100             :     FFFrameSyncIn *in;
     101             :     int i, ret;
     102             : 
     103           0 :     av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
     104             :            "with settings from input link %d\n",
     105             :            outlink_idx, inlink_idx);
     106             : 
     107           0 :     switch (outlink->type) {
     108           0 :     case AVMEDIA_TYPE_VIDEO:
     109           0 :         outlink->w = inlink->w;
     110           0 :         outlink->h = inlink->h;
     111           0 :         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
     112           0 :         outlink->frame_rate = inlink->frame_rate;
     113           0 :         break;
     114           0 :     case AVMEDIA_TYPE_AUDIO:
     115           0 :         outlink->sample_rate    = inlink->sample_rate;
     116           0 :         outlink->channels       = inlink->channels;
     117           0 :         outlink->channel_layout = inlink->channel_layout;
     118           0 :         break;
     119             :     }
     120             : 
     121           0 :     outlink->time_base = inlink->time_base;
     122           0 :     outlink->format = inlink->format;
     123             : 
     124           0 :     if (s->fs.opaque == s)
     125           0 :         return 0;
     126             : 
     127           0 :     if ((ret = ff_framesync_init(&s->fs, ctx, ctx->nb_inputs)) < 0)
     128           0 :         return ret;
     129             : 
     130           0 :     in = s->fs.in;
     131           0 :     s->fs.opaque = s;
     132           0 :     s->fs.on_event = process_frame;
     133             : 
     134           0 :     for (i = 0; i < ctx->nb_inputs; i++) {
     135           0 :         in[i].time_base = ctx->inputs[i]->time_base;
     136           0 :         in[i].sync      = 1;
     137           0 :         in[i].before    = EXT_STOP;
     138           0 :         in[i].after     = EXT_STOP;
     139             :     }
     140             : 
     141           0 :     s->frames = av_calloc(ctx->nb_inputs, sizeof(*s->frames));
     142           0 :     if (!s->frames)
     143           0 :         return AVERROR(ENOMEM);
     144             : 
     145           0 :     return ff_framesync_configure(&s->fs);
     146             : }
     147             : 
     148           0 : static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
     149             : {
     150           0 :     const char *padtype = is_input ? "in" : "out";
     151           0 :     int i = 0, ret = 0;
     152             : 
     153           0 :     for (i = 0; i < nb_pads; i++) {
     154           0 :         AVFilterPad pad = { 0 };
     155             : 
     156           0 :         pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
     157             : 
     158           0 :         pad.name = av_asprintf("%sput%d", padtype, i);
     159           0 :         if (!pad.name)
     160           0 :             return AVERROR(ENOMEM);
     161             : 
     162           0 :         av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
     163             : 
     164           0 :         if (is_input) {
     165           0 :             ret = ff_insert_inpad(ctx, i, &pad);
     166             :         } else {
     167           0 :             pad.config_props  = config_output;
     168           0 :             ret = ff_insert_outpad(ctx, i, &pad);
     169             :         }
     170             : 
     171           0 :         if (ret < 0) {
     172           0 :             av_freep(&pad.name);
     173           0 :             return ret;
     174             :         }
     175             :     }
     176             : 
     177           0 :     return 0;
     178             : }
     179             : 
     180           0 : static int parse_mapping(AVFilterContext *ctx, const char *map)
     181             : {
     182           0 :     StreamSelectContext *s = ctx->priv;
     183             :     int *new_map;
     184           0 :     int new_nb_map = 0;
     185             : 
     186           0 :     if (!map) {
     187           0 :         av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
     188           0 :         return AVERROR(EINVAL);
     189             :     }
     190             : 
     191           0 :     new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
     192           0 :     if (!new_map)
     193           0 :         return AVERROR(ENOMEM);
     194             : 
     195           0 :     while (1) {
     196             :         char *p;
     197           0 :         const int n = strtol(map, &p, 0);
     198             : 
     199           0 :         av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
     200             : 
     201           0 :         if (map == p)
     202           0 :             break;
     203           0 :         map = p;
     204             : 
     205           0 :         if (new_nb_map >= s->nb_inputs) {
     206           0 :             av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
     207             :                    "input pads available\n", s->nb_inputs);
     208           0 :             av_free(new_map);
     209           0 :             return AVERROR(EINVAL);
     210             :         }
     211             : 
     212           0 :         if (n < 0 || n >= ctx->nb_inputs) {
     213           0 :             av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
     214             :                    "(there is only %d input streams defined)\n",
     215             :                    n, s->nb_inputs);
     216           0 :             av_free(new_map);
     217           0 :             return AVERROR(EINVAL);
     218             :         }
     219             : 
     220           0 :         av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
     221           0 :         new_map[new_nb_map++] = n;
     222             :     }
     223             : 
     224           0 :     if (!new_nb_map) {
     225           0 :         av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
     226           0 :         av_free(new_map);
     227           0 :         return AVERROR(EINVAL);
     228             :     }
     229             : 
     230           0 :     av_freep(&s->map);
     231           0 :     s->map = new_map;
     232           0 :     s->nb_map = new_nb_map;
     233             : 
     234           0 :     av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
     235             : 
     236           0 :     return 0;
     237             : }
     238             : 
     239           0 : static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
     240             :                            char *res, int res_len, int flags)
     241             : {
     242           0 :     if (!strcmp(cmd, "map")) {
     243           0 :         int ret = parse_mapping(ctx, args);
     244             : 
     245           0 :         if (ret < 0)
     246           0 :             return ret;
     247           0 :         return avfilter_config_links(ctx);
     248             :     }
     249           0 :     return AVERROR(ENOSYS);
     250             : }
     251             : 
     252           0 : static av_cold int init(AVFilterContext *ctx)
     253             : {
     254           0 :     StreamSelectContext *s = ctx->priv;
     255           0 :     int ret, nb_outputs = 0;
     256           0 :     char *map = s->map_str;
     257             : 
     258           0 :     if (!strcmp(ctx->filter->name, "astreamselect"))
     259           0 :         s->is_audio = 1;
     260             : 
     261           0 :     for (; map;) {
     262             :         char *p;
     263             : 
     264           0 :         strtol(map, &p, 0);
     265           0 :         if (map == p)
     266           0 :             break;
     267           0 :         nb_outputs++;
     268           0 :         map = p;
     269             :     }
     270             : 
     271           0 :     s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
     272           0 :     if (!s->last_pts)
     273           0 :         return AVERROR(ENOMEM);
     274             : 
     275           0 :     if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
     276           0 :         (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
     277           0 :         return ret;
     278             : 
     279           0 :     av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
     280             :            ctx->nb_inputs, ctx->nb_outputs);
     281             : 
     282           0 :     return parse_mapping(ctx, s->map_str);
     283             : }
     284             : 
     285           0 : static av_cold void uninit(AVFilterContext *ctx)
     286             : {
     287           0 :     StreamSelectContext *s = ctx->priv;
     288             : 
     289           0 :     av_freep(&s->last_pts);
     290           0 :     av_freep(&s->map);
     291           0 :     av_freep(&s->frames);
     292           0 :     ff_framesync_uninit(&s->fs);
     293           0 : }
     294             : 
     295           0 : static int query_formats(AVFilterContext *ctx)
     296             : {
     297           0 :     AVFilterFormats *formats, *rates = NULL;
     298           0 :     AVFilterChannelLayouts *layouts = NULL;
     299             :     int ret, i;
     300             : 
     301           0 :     for (i = 0; i < ctx->nb_inputs; i++) {
     302           0 :         formats = ff_all_formats(ctx->inputs[i]->type);
     303           0 :         if ((ret = ff_set_common_formats(ctx, formats)) < 0)
     304           0 :             return ret;
     305             : 
     306           0 :         if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
     307           0 :             rates = ff_all_samplerates();
     308           0 :             if ((ret = ff_set_common_samplerates(ctx, rates)) < 0)
     309           0 :                 return ret;
     310           0 :             layouts = ff_all_channel_counts();
     311           0 :             if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
     312           0 :                 return ret;
     313             :         }
     314             :     }
     315             : 
     316           0 :     return 0;
     317             : }
     318             : 
     319             : AVFilter ff_vf_streamselect = {
     320             :     .name            = "streamselect",
     321             :     .description     = NULL_IF_CONFIG_SMALL("Select video streams"),
     322             :     .init            = init,
     323             :     .query_formats   = query_formats,
     324             :     .process_command = process_command,
     325             :     .uninit          = uninit,
     326             :     .activate        = activate,
     327             :     .priv_size       = sizeof(StreamSelectContext),
     328             :     .priv_class      = &streamselect_class,
     329             :     .flags           = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
     330             : };
     331             : 
     332             : #define astreamselect_options streamselect_options
     333             : AVFILTER_DEFINE_CLASS(astreamselect);
     334             : 
     335             : AVFilter ff_af_astreamselect = {
     336             :     .name            = "astreamselect",
     337             :     .description     = NULL_IF_CONFIG_SMALL("Select audio streams"),
     338             :     .init            = init,
     339             :     .query_formats   = query_formats,
     340             :     .process_command = process_command,
     341             :     .uninit          = uninit,
     342             :     .activate        = activate,
     343             :     .priv_size       = sizeof(StreamSelectContext),
     344             :     .priv_class      = &astreamselect_class,
     345             :     .flags           = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
     346             : };

Generated by: LCOV version 1.13