LCOV - code coverage report
Current view: top level - src/libavfilter - f_loop.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 158 0.0 %
Date: 2017-01-28 02:43:52 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Paul B Mahol
       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             : #include "libavutil/audio_fifo.h"
      22             : #include "libavutil/avassert.h"
      23             : #include "libavutil/fifo.h"
      24             : #include "libavutil/internal.h"
      25             : #include "libavutil/opt.h"
      26             : #include "avfilter.h"
      27             : #include "audio.h"
      28             : #include "formats.h"
      29             : #include "internal.h"
      30             : #include "video.h"
      31             : 
      32             : typedef struct LoopContext {
      33             :     const AVClass *class;
      34             : 
      35             :     AVAudioFifo *fifo;
      36             :     AVAudioFifo *left;
      37             :     AVFrame **frames;
      38             :     int nb_frames;
      39             :     int current_frame;
      40             :     int64_t start_pts;
      41             :     int64_t duration;
      42             :     int64_t current_sample;
      43             :     int64_t nb_samples;
      44             :     int64_t ignored_samples;
      45             : 
      46             :     int loop;
      47             :     int64_t size;
      48             :     int64_t start;
      49             :     int64_t pts;
      50             : } LoopContext;
      51             : 
      52             : #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      53             : #define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      54             : #define OFFSET(x) offsetof(LoopContext, x)
      55             : 
      56             : #if CONFIG_ALOOP_FILTER
      57             : 
      58           0 : static int aconfig_input(AVFilterLink *inlink)
      59             : {
      60           0 :     AVFilterContext *ctx = inlink->dst;
      61           0 :     LoopContext *s  = ctx->priv;
      62             : 
      63           0 :     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
      64           0 :     s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
      65           0 :     if (!s->fifo || !s->left)
      66           0 :         return AVERROR(ENOMEM);
      67             : 
      68           0 :     return 0;
      69             : }
      70             : 
      71           0 : static av_cold void auninit(AVFilterContext *ctx)
      72             : {
      73           0 :     LoopContext *s = ctx->priv;
      74             : 
      75           0 :     av_audio_fifo_free(s->fifo);
      76           0 :     av_audio_fifo_free(s->left);
      77           0 : }
      78             : 
      79           0 : static int push_samples(AVFilterContext *ctx, int nb_samples)
      80             : {
      81           0 :     AVFilterLink *outlink = ctx->outputs[0];
      82           0 :     LoopContext *s = ctx->priv;
      83             :     AVFrame *out;
      84           0 :     int ret, i = 0;
      85             : 
      86           0 :     while (s->loop != 0 && i < nb_samples) {
      87           0 :         out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
      88           0 :         if (!out)
      89           0 :             return AVERROR(ENOMEM);
      90           0 :         ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
      91           0 :         if (ret < 0) {
      92           0 :             av_frame_free(&out);
      93           0 :             return ret;
      94             :         }
      95           0 :         out->pts = s->pts;
      96           0 :         out->nb_samples = ret;
      97           0 :         s->pts += out->nb_samples;
      98           0 :         i += out->nb_samples;
      99           0 :         s->current_sample += out->nb_samples;
     100             : 
     101           0 :         ret = ff_filter_frame(outlink, out);
     102           0 :         if (ret < 0)
     103           0 :             return ret;
     104             : 
     105           0 :         if (s->current_sample >= s->nb_samples) {
     106           0 :             s->current_sample = 0;
     107             : 
     108           0 :             if (s->loop > 0)
     109           0 :                 s->loop--;
     110             :         }
     111             :     }
     112             : 
     113           0 :     return ret;
     114             : }
     115             : 
     116           0 : static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
     117             : {
     118           0 :     AVFilterContext *ctx = inlink->dst;
     119           0 :     AVFilterLink *outlink = ctx->outputs[0];
     120           0 :     LoopContext *s = ctx->priv;
     121           0 :     int ret = 0;
     122             : 
     123           0 :     if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
     124           0 :         if (s->nb_samples < s->size) {
     125           0 :             int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
     126           0 :             int drain = 0;
     127             : 
     128           0 :             ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
     129           0 :             if (ret < 0)
     130           0 :                 return ret;
     131           0 :             if (!s->nb_samples) {
     132           0 :                 drain = FFMAX(0, s->start - s->ignored_samples);
     133           0 :                 s->pts = frame->pts;
     134           0 :                 av_audio_fifo_drain(s->fifo, drain);
     135           0 :                 s->pts += s->start - s->ignored_samples;
     136             :             }
     137           0 :             s->nb_samples += ret - drain;
     138           0 :             drain = frame->nb_samples - written;
     139           0 :             if (s->nb_samples == s->size && drain > 0) {
     140             :                 int ret2;
     141             : 
     142           0 :                 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
     143           0 :                 if (ret2 < 0)
     144           0 :                    return ret2;
     145           0 :                 av_audio_fifo_drain(s->left, drain);
     146             :             }
     147           0 :             frame->nb_samples = ret;
     148           0 :             s->pts += ret;
     149           0 :             ret = ff_filter_frame(outlink, frame);
     150             :         } else {
     151           0 :             int nb_samples = frame->nb_samples;
     152             : 
     153           0 :             av_frame_free(&frame);
     154           0 :             ret = push_samples(ctx, nb_samples);
     155             :         }
     156             :     } else {
     157           0 :         s->ignored_samples += frame->nb_samples;
     158           0 :         frame->pts = s->pts;
     159           0 :         s->pts += frame->nb_samples;
     160           0 :         ret = ff_filter_frame(outlink, frame);
     161             :     }
     162             : 
     163           0 :     return ret;
     164             : }
     165             : 
     166           0 : static int arequest_frame(AVFilterLink *outlink)
     167             : {
     168           0 :     AVFilterContext *ctx = outlink->src;
     169           0 :     LoopContext *s = ctx->priv;
     170           0 :     int ret = 0;
     171             : 
     172           0 :     if ((!s->size) ||
     173           0 :         (s->nb_samples < s->size) ||
     174           0 :         (s->nb_samples >= s->size && s->loop == 0)) {
     175           0 :         int nb_samples = av_audio_fifo_size(s->left);
     176             : 
     177           0 :         if (s->loop == 0 && nb_samples > 0) {
     178             :             AVFrame *out;
     179             : 
     180           0 :             out = ff_get_audio_buffer(outlink, nb_samples);
     181           0 :             if (!out)
     182           0 :                 return AVERROR(ENOMEM);
     183           0 :             av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
     184           0 :             out->pts = s->pts;
     185           0 :             s->pts += nb_samples;
     186           0 :             ret = ff_filter_frame(outlink, out);
     187           0 :             if (ret < 0)
     188           0 :                 return ret;
     189             :         }
     190           0 :         ret = ff_request_frame(ctx->inputs[0]);
     191             :     } else {
     192           0 :         ret = push_samples(ctx, 1024);
     193             :     }
     194             : 
     195           0 :     if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
     196           0 :         ret = push_samples(ctx, outlink->sample_rate);
     197             :     }
     198             : 
     199           0 :     return ret;
     200             : }
     201             : 
     202             : static const AVOption aloop_options[] = {
     203             :     { "loop",  "number of loops",               OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   AFLAGS },
     204             :     { "size",  "max number of samples to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT32_MAX, AFLAGS },
     205             :     { "start", "set the loop start sample",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT64_MAX, AFLAGS },
     206             :     { NULL }
     207             : };
     208             : 
     209             : AVFILTER_DEFINE_CLASS(aloop);
     210             : 
     211             : static const AVFilterPad ainputs[] = {
     212             :     {
     213             :         .name         = "default",
     214             :         .type         = AVMEDIA_TYPE_AUDIO,
     215             :         .filter_frame = afilter_frame,
     216             :         .config_props = aconfig_input,
     217             :     },
     218             :     { NULL }
     219             : };
     220             : 
     221             : static const AVFilterPad aoutputs[] = {
     222             :     {
     223             :         .name          = "default",
     224             :         .type          = AVMEDIA_TYPE_AUDIO,
     225             :         .request_frame = arequest_frame,
     226             :     },
     227             :     { NULL }
     228             : };
     229             : 
     230             : AVFilter ff_af_aloop = {
     231             :     .name          = "aloop",
     232             :     .description   = NULL_IF_CONFIG_SMALL("Loop audio samples."),
     233             :     .priv_size     = sizeof(LoopContext),
     234             :     .priv_class    = &aloop_class,
     235             :     .uninit        = auninit,
     236             :     .inputs        = ainputs,
     237             :     .outputs       = aoutputs,
     238             : };
     239             : #endif /* CONFIG_ALOOP_FILTER */
     240             : 
     241             : #if CONFIG_LOOP_FILTER
     242             : 
     243           0 : static av_cold int init(AVFilterContext *ctx)
     244             : {
     245           0 :     LoopContext *s = ctx->priv;
     246             : 
     247           0 :     s->frames = av_calloc(s->size, sizeof(*s->frames));
     248           0 :     if (!s->frames)
     249           0 :         return AVERROR(ENOMEM);
     250             : 
     251           0 :     return 0;
     252             : }
     253             : 
     254           0 : static av_cold void uninit(AVFilterContext *ctx)
     255             : {
     256           0 :     LoopContext *s = ctx->priv;
     257             :     int i;
     258             : 
     259           0 :     for (i = 0; i < s->nb_frames; i++)
     260           0 :         av_frame_free(&s->frames[i]);
     261             : 
     262           0 :     av_freep(&s->frames);
     263           0 :     s->nb_frames = 0;
     264           0 : }
     265             : 
     266           0 : static int push_frame(AVFilterContext *ctx)
     267             : {
     268           0 :     AVFilterLink *outlink = ctx->outputs[0];
     269           0 :     LoopContext *s = ctx->priv;
     270             :     int64_t pts;
     271             :     int ret;
     272             : 
     273           0 :     AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
     274             : 
     275           0 :     if (!out)
     276           0 :         return AVERROR(ENOMEM);
     277           0 :     out->pts += s->duration - s->start_pts;
     278           0 :     pts = out->pts + av_frame_get_pkt_duration(out);
     279           0 :     ret = ff_filter_frame(outlink, out);
     280           0 :     s->current_frame++;
     281             : 
     282           0 :     if (s->current_frame >= s->nb_frames) {
     283           0 :         s->duration = pts;
     284           0 :         s->current_frame = 0;
     285             : 
     286           0 :         if (s->loop > 0)
     287           0 :             s->loop--;
     288             :     }
     289             : 
     290           0 :     return ret;
     291             : }
     292             : 
     293           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     294             : {
     295           0 :     AVFilterContext *ctx = inlink->dst;
     296           0 :     AVFilterLink *outlink = ctx->outputs[0];
     297           0 :     LoopContext *s = ctx->priv;
     298           0 :     int ret = 0;
     299             : 
     300           0 :     if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
     301           0 :         if (s->nb_frames < s->size) {
     302           0 :             if (!s->nb_frames)
     303           0 :                 s->start_pts = frame->pts;
     304           0 :             s->frames[s->nb_frames] = av_frame_clone(frame);
     305           0 :             if (!s->frames[s->nb_frames]) {
     306           0 :                 av_frame_free(&frame);
     307           0 :                 return AVERROR(ENOMEM);
     308             :             }
     309           0 :             s->nb_frames++;
     310           0 :             s->duration = frame->pts + av_frame_get_pkt_duration(frame);
     311           0 :             ret = ff_filter_frame(outlink, frame);
     312             :         } else {
     313           0 :             av_frame_free(&frame);
     314           0 :             ret = push_frame(ctx);
     315             :         }
     316             :     } else {
     317           0 :         frame->pts += s->duration;
     318           0 :         ret = ff_filter_frame(outlink, frame);
     319             :     }
     320             : 
     321           0 :     return ret;
     322             : }
     323             : 
     324           0 : static int request_frame(AVFilterLink *outlink)
     325             : {
     326           0 :     AVFilterContext *ctx = outlink->src;
     327           0 :     LoopContext *s = ctx->priv;
     328           0 :     int ret = 0;
     329             : 
     330           0 :     if ((!s->size) ||
     331           0 :         (s->nb_frames < s->size) ||
     332           0 :         (s->nb_frames >= s->size && s->loop == 0)) {
     333           0 :         ret = ff_request_frame(ctx->inputs[0]);
     334             :     } else {
     335           0 :         ret = push_frame(ctx);
     336             :     }
     337             : 
     338           0 :     if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
     339           0 :         ret = push_frame(ctx);
     340             :     }
     341             : 
     342           0 :     return ret;
     343             : }
     344             : 
     345             : static const AVOption loop_options[] = {
     346             :     { "loop",  "number of loops",              OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   VFLAGS },
     347             :     { "size",  "max number of frames to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT16_MAX, VFLAGS },
     348             :     { "start", "set the loop start frame",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT64_MAX, VFLAGS },
     349             :     { NULL }
     350             : };
     351             : 
     352             : AVFILTER_DEFINE_CLASS(loop);
     353             : 
     354             : static const AVFilterPad inputs[] = {
     355             :     {
     356             :         .name         = "default",
     357             :         .type         = AVMEDIA_TYPE_VIDEO,
     358             :         .filter_frame = filter_frame,
     359             :     },
     360             :     { NULL }
     361             : };
     362             : 
     363             : static const AVFilterPad outputs[] = {
     364             :     {
     365             :         .name          = "default",
     366             :         .type          = AVMEDIA_TYPE_VIDEO,
     367             :         .request_frame = request_frame,
     368             :     },
     369             :     { NULL }
     370             : };
     371             : 
     372             : AVFilter ff_vf_loop = {
     373             :     .name        = "loop",
     374             :     .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
     375             :     .priv_size   = sizeof(LoopContext),
     376             :     .priv_class  = &loop_class,
     377             :     .init        = init,
     378             :     .uninit      = uninit,
     379             :     .inputs      = inputs,
     380             :     .outputs     = outputs,
     381             : };
     382             : #endif /* CONFIG_LOOP_FILTER */

Generated by: LCOV version 1.12