LCOV - code coverage report
Current view: top level - libavfilter - f_interleave.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 92 0.0 %
Date: 2017-12-15 11:05:35 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Stefano Sabatini
       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
       8             :  * License 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 GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * audio and video interleaver
      24             :  */
      25             : 
      26             : #include "libavutil/avassert.h"
      27             : #include "libavutil/avstring.h"
      28             : #include "libavutil/opt.h"
      29             : 
      30             : #define FF_INTERNAL_FIELDS 1
      31             : #include "framequeue.h"
      32             : 
      33             : #include "avfilter.h"
      34             : #include "bufferqueue.h"
      35             : #include "formats.h"
      36             : #include "internal.h"
      37             : #include "audio.h"
      38             : #include "video.h"
      39             : 
      40             : typedef struct InterleaveContext {
      41             :     const AVClass *class;
      42             :     int nb_inputs;
      43             :     struct FFBufQueue *queues;
      44             : } InterleaveContext;
      45             : 
      46             : #define OFFSET(x) offsetof(InterleaveContext, x)
      47             : 
      48             : #define DEFINE_OPTIONS(filt_name, flags_)                           \
      49             : static const AVOption filt_name##_options[] = {                     \
      50             :    { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
      51             :    { "n",         "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
      52             :    { NULL }                                                         \
      53             : }
      54             : 
      55           0 : inline static int push_frame(AVFilterContext *ctx)
      56             : {
      57           0 :     InterleaveContext *s = ctx->priv;
      58             :     AVFrame *frame;
      59           0 :     int i, queue_idx = -1;
      60           0 :     int64_t pts_min = INT64_MAX;
      61             : 
      62             :     /* look for oldest frame */
      63           0 :     for (i = 0; i < ctx->nb_inputs; i++) {
      64           0 :         struct FFBufQueue *q = &s->queues[i];
      65             : 
      66           0 :         if (!q->available && !ctx->inputs[i]->status_out)
      67           0 :             return 0;
      68           0 :         if (q->available) {
      69           0 :             frame = ff_bufqueue_peek(q, 0);
      70           0 :             if (frame->pts < pts_min) {
      71           0 :                 pts_min = frame->pts;
      72           0 :                 queue_idx = i;
      73             :             }
      74             :         }
      75             :     }
      76             : 
      77             :     /* all inputs are closed */
      78           0 :     if (queue_idx < 0)
      79           0 :         return AVERROR_EOF;
      80             : 
      81           0 :     frame = ff_bufqueue_get(&s->queues[queue_idx]);
      82           0 :     av_log(ctx, AV_LOG_DEBUG, "queue:%d -> frame time:%f\n",
      83           0 :            queue_idx, frame->pts * av_q2d(AV_TIME_BASE_Q));
      84           0 :     return ff_filter_frame(ctx->outputs[0], frame);
      85             : }
      86             : 
      87           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
      88             : {
      89           0 :     AVFilterContext *ctx = inlink->dst;
      90           0 :     InterleaveContext *s = ctx->priv;
      91           0 :     unsigned in_no = FF_INLINK_IDX(inlink);
      92             : 
      93           0 :     if (frame->pts == AV_NOPTS_VALUE) {
      94           0 :         av_log(ctx, AV_LOG_WARNING,
      95             :                "NOPTS value for input frame cannot be accepted, frame discarded\n");
      96           0 :         av_frame_free(&frame);
      97           0 :         return AVERROR_INVALIDDATA;
      98             :     }
      99             : 
     100             :     /* queue frame */
     101           0 :     frame->pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
     102           0 :     av_log(ctx, AV_LOG_DEBUG, "frame pts:%f -> queue idx:%d available:%d\n",
     103           0 :            frame->pts * av_q2d(AV_TIME_BASE_Q), in_no, s->queues[in_no].available);
     104           0 :     ff_bufqueue_add(ctx, &s->queues[in_no], frame);
     105             : 
     106           0 :     return push_frame(ctx);
     107             : }
     108             : 
     109           0 : static av_cold int init(AVFilterContext *ctx)
     110             : {
     111           0 :     InterleaveContext *s = ctx->priv;
     112           0 :     const AVFilterPad *outpad = &ctx->filter->outputs[0];
     113             :     int i, ret;
     114             : 
     115           0 :     s->queues = av_calloc(s->nb_inputs, sizeof(s->queues[0]));
     116           0 :     if (!s->queues)
     117           0 :         return AVERROR(ENOMEM);
     118             : 
     119           0 :     for (i = 0; i < s->nb_inputs; i++) {
     120           0 :         AVFilterPad inpad = { 0 };
     121             : 
     122           0 :         inpad.name = av_asprintf("input%d", i);
     123           0 :         if (!inpad.name)
     124           0 :             return AVERROR(ENOMEM);
     125           0 :         inpad.type         = outpad->type;
     126           0 :         inpad.filter_frame = filter_frame;
     127             : 
     128           0 :         switch (outpad->type) {
     129           0 :         case AVMEDIA_TYPE_VIDEO:
     130           0 :             inpad.get_video_buffer = ff_null_get_video_buffer; break;
     131           0 :         case AVMEDIA_TYPE_AUDIO:
     132           0 :             inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
     133           0 :         default:
     134           0 :             av_assert0(0);
     135             :         }
     136           0 :         if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
     137           0 :             av_freep(&inpad.name);
     138           0 :             return ret;
     139             :         }
     140             :     }
     141             : 
     142           0 :     return 0;
     143             : }
     144             : 
     145           0 : static av_cold void uninit(AVFilterContext *ctx)
     146             : {
     147           0 :     InterleaveContext *s = ctx->priv;
     148             :     int i;
     149             : 
     150           0 :     for (i = 0; i < ctx->nb_inputs; i++) {
     151           0 :         ff_bufqueue_discard_all(&s->queues[i]);
     152           0 :         av_freep(&s->queues[i]);
     153           0 :         av_freep(&ctx->input_pads[i].name);
     154             :     }
     155           0 : }
     156             : 
     157           0 : static int config_output(AVFilterLink *outlink)
     158             : {
     159           0 :     AVFilterContext *ctx = outlink->src;
     160           0 :     AVFilterLink *inlink0 = ctx->inputs[0];
     161             :     int i;
     162             : 
     163           0 :     if (outlink->type == AVMEDIA_TYPE_VIDEO) {
     164           0 :         outlink->time_base           = AV_TIME_BASE_Q;
     165           0 :         outlink->w                   = inlink0->w;
     166           0 :         outlink->h                   = inlink0->h;
     167           0 :         outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
     168           0 :         outlink->format              = inlink0->format;
     169           0 :         outlink->frame_rate = (AVRational) {1, 0};
     170           0 :         for (i = 1; i < ctx->nb_inputs; i++) {
     171           0 :             AVFilterLink *inlink = ctx->inputs[i];
     172             : 
     173           0 :             if (outlink->w                       != inlink->w                       ||
     174           0 :                 outlink->h                       != inlink->h                       ||
     175           0 :                 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
     176           0 :                 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
     177           0 :                 av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
     178             :                        "(size %dx%d, SAR %d:%d) do not match the corresponding "
     179             :                        "output link parameters (%dx%d, SAR %d:%d)\n",
     180           0 :                        ctx->input_pads[i].name, inlink->w, inlink->h,
     181             :                        inlink->sample_aspect_ratio.num,
     182             :                        inlink->sample_aspect_ratio.den,
     183             :                        outlink->w, outlink->h,
     184             :                        outlink->sample_aspect_ratio.num,
     185             :                        outlink->sample_aspect_ratio.den);
     186           0 :                 return AVERROR(EINVAL);
     187             :             }
     188             :         }
     189             :     }
     190           0 :     return 0;
     191             : }
     192             : 
     193           0 : static int request_frame(AVFilterLink *outlink)
     194             : {
     195           0 :     AVFilterContext *ctx = outlink->src;
     196           0 :     InterleaveContext *s = ctx->priv;
     197             :     int i, ret;
     198             : 
     199           0 :     for (i = 0; i < ctx->nb_inputs; i++) {
     200           0 :         if (!s->queues[i].available && !ctx->inputs[i]->status_out) {
     201           0 :             ret = ff_request_frame(ctx->inputs[i]);
     202           0 :             if (ret != AVERROR_EOF)
     203           0 :                 return ret;
     204             :         }
     205             :     }
     206             : 
     207           0 :     return push_frame(ctx);
     208             : }
     209             : 
     210             : #if CONFIG_INTERLEAVE_FILTER
     211             : 
     212             : DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
     213             : AVFILTER_DEFINE_CLASS(interleave);
     214             : 
     215             : static const AVFilterPad interleave_outputs[] = {
     216             :     {
     217             :         .name          = "default",
     218             :         .type          = AVMEDIA_TYPE_VIDEO,
     219             :         .config_props  = config_output,
     220             :         .request_frame = request_frame,
     221             :     },
     222             :     { NULL }
     223             : };
     224             : 
     225             : AVFilter ff_vf_interleave = {
     226             :     .name        = "interleave",
     227             :     .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
     228             :     .priv_size   = sizeof(InterleaveContext),
     229             :     .init        = init,
     230             :     .uninit      = uninit,
     231             :     .outputs     = interleave_outputs,
     232             :     .priv_class  = &interleave_class,
     233             :     .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
     234             : };
     235             : 
     236             : #endif
     237             : 
     238             : #if CONFIG_AINTERLEAVE_FILTER
     239             : 
     240             : DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
     241             : AVFILTER_DEFINE_CLASS(ainterleave);
     242             : 
     243             : static const AVFilterPad ainterleave_outputs[] = {
     244             :     {
     245             :         .name          = "default",
     246             :         .type          = AVMEDIA_TYPE_AUDIO,
     247             :         .config_props  = config_output,
     248             :         .request_frame = request_frame,
     249             :     },
     250             :     { NULL }
     251             : };
     252             : 
     253             : AVFilter ff_af_ainterleave = {
     254             :     .name        = "ainterleave",
     255             :     .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
     256             :     .priv_size   = sizeof(InterleaveContext),
     257             :     .init        = init,
     258             :     .uninit      = uninit,
     259             :     .outputs     = ainterleave_outputs,
     260             :     .priv_class  = &ainterleave_class,
     261             :     .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
     262             : };
     263             : 
     264             : #endif

Generated by: LCOV version 1.13