LCOV - code coverage report
Current view: top level - src/libavfilter - af_channelsplit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 39 48 81.2 %
Date: 2017-01-28 02:43:52 Functions: 3 3 100.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             : /**
      20             :  * @file
      21             :  * Channel split filter
      22             :  *
      23             :  * Split an audio stream into per-channel streams.
      24             :  */
      25             : 
      26             : #include "libavutil/attributes.h"
      27             : #include "libavutil/channel_layout.h"
      28             : #include "libavutil/internal.h"
      29             : #include "libavutil/opt.h"
      30             : 
      31             : #include "audio.h"
      32             : #include "avfilter.h"
      33             : #include "formats.h"
      34             : #include "internal.h"
      35             : 
      36             : typedef struct ChannelSplitContext {
      37             :     const AVClass *class;
      38             : 
      39             :     uint64_t channel_layout;
      40             :     char    *channel_layout_str;
      41             : } ChannelSplitContext;
      42             : 
      43             : #define OFFSET(x) offsetof(ChannelSplitContext, x)
      44             : #define A AV_OPT_FLAG_AUDIO_PARAM
      45             : #define F AV_OPT_FLAG_FILTERING_PARAM
      46             : static const AVOption channelsplit_options[] = {
      47             :     { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
      48             :     { NULL }
      49             : };
      50             : 
      51             : AVFILTER_DEFINE_CLASS(channelsplit);
      52             : 
      53           2 : static av_cold int init(AVFilterContext *ctx)
      54             : {
      55           2 :     ChannelSplitContext *s = ctx->priv;
      56             :     int nb_channels;
      57           2 :     int ret = 0, i;
      58             : 
      59           2 :     if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
      60           0 :         av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
      61             :                s->channel_layout_str);
      62           0 :         ret = AVERROR(EINVAL);
      63           0 :         goto fail;
      64             :     }
      65             : 
      66           2 :     nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
      67           6 :     for (i = 0; i < nb_channels; i++) {
      68           4 :         uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
      69           4 :         AVFilterPad pad  = { 0 };
      70             : 
      71           4 :         pad.type = AVMEDIA_TYPE_AUDIO;
      72           4 :         pad.name = av_get_channel_name(channel);
      73             : 
      74           4 :         ff_insert_outpad(ctx, i, &pad);
      75             :     }
      76             : 
      77             : fail:
      78           2 :     return ret;
      79             : }
      80             : 
      81           1 : static int query_formats(AVFilterContext *ctx)
      82             : {
      83           1 :     ChannelSplitContext *s = ctx->priv;
      84           1 :     AVFilterChannelLayouts *in_layouts = NULL;
      85             :     int i, ret;
      86             : 
      87           2 :     if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
      88           1 :         (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
      89           0 :         return ret;
      90             : 
      91           2 :     if ((ret = ff_add_channel_layout(&in_layouts, s->channel_layout)) < 0 ||
      92           1 :         (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts)) < 0)
      93           0 :         return ret;
      94             : 
      95           3 :     for (i = 0; i < ctx->nb_outputs; i++) {
      96           2 :         AVFilterChannelLayouts *out_layouts = NULL;
      97           2 :         uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
      98             : 
      99           4 :         if ((ret = ff_add_channel_layout(&out_layouts, channel)) < 0 ||
     100           2 :             (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts)) < 0)
     101           0 :             return ret;
     102             :     }
     103             : 
     104           1 :     return 0;
     105             : }
     106             : 
     107         259 : static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
     108             : {
     109         259 :     AVFilterContext *ctx = inlink->dst;
     110         259 :     int i, ret = 0;
     111             : 
     112         777 :     for (i = 0; i < ctx->nb_outputs; i++) {
     113         518 :         AVFrame *buf_out = av_frame_clone(buf);
     114             : 
     115         518 :         if (!buf_out) {
     116           0 :             ret = AVERROR(ENOMEM);
     117           0 :             break;
     118             :         }
     119             : 
     120         518 :         buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
     121         518 :         buf_out->channel_layout =
     122         518 :             av_channel_layout_extract_channel(buf->channel_layout, i);
     123         518 :         av_frame_set_channels(buf_out, 1);
     124             : 
     125         518 :         ret = ff_filter_frame(ctx->outputs[i], buf_out);
     126         518 :         if (ret < 0)
     127           0 :             break;
     128             :     }
     129         259 :     av_frame_free(&buf);
     130         259 :     return ret;
     131             : }
     132             : 
     133             : static const AVFilterPad avfilter_af_channelsplit_inputs[] = {
     134             :     {
     135             :         .name         = "default",
     136             :         .type         = AVMEDIA_TYPE_AUDIO,
     137             :         .filter_frame = filter_frame,
     138             :     },
     139             :     { NULL }
     140             : };
     141             : 
     142             : AVFilter ff_af_channelsplit = {
     143             :     .name           = "channelsplit",
     144             :     .description    = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
     145             :     .priv_size      = sizeof(ChannelSplitContext),
     146             :     .priv_class     = &channelsplit_class,
     147             :     .init           = init,
     148             :     .query_formats  = query_formats,
     149             :     .inputs         = avfilter_af_channelsplit_inputs,
     150             :     .outputs        = NULL,
     151             :     .flags          = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
     152             : };

Generated by: LCOV version 1.12