LCOV - code coverage report
Current view: top level - libavfilter - vf_random.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 33 0.0 %
Date: 2017-12-16 13:57:32 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 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/lfg.h"
      22             : #include "libavutil/opt.h"
      23             : #include "libavutil/random_seed.h"
      24             : #include "avfilter.h"
      25             : #include "formats.h"
      26             : #include "internal.h"
      27             : #include "video.h"
      28             : 
      29             : #define MAX_FRAMES 512
      30             : 
      31             : typedef struct RandomContext {
      32             :     const AVClass *class;
      33             : 
      34             :     AVLFG lfg;
      35             :     int nb_frames;
      36             :     int64_t random_seed;
      37             :     int nb_frames_filled;
      38             :     AVFrame *frames[MAX_FRAMES];
      39             :     int64_t pts[MAX_FRAMES];
      40             :     int flush_idx;
      41             : } RandomContext;
      42             : 
      43             : #define OFFSET(x) offsetof(RandomContext, x)
      44             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      45             : 
      46             : static const AVOption random_options[] = {
      47             :     { "frames", "set number of frames in cache", OFFSET(nb_frames),   AV_OPT_TYPE_INT,   {.i64=30},  2, MAX_FRAMES, FLAGS },
      48             :     { "seed",   "set the seed",                  OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX, FLAGS },
      49             :     { NULL }
      50             : };
      51             : 
      52             : AVFILTER_DEFINE_CLASS(random);
      53             : 
      54           0 : static av_cold int init(AVFilterContext *ctx)
      55             : {
      56           0 :     RandomContext *s = ctx->priv;
      57             :     uint32_t seed;
      58             : 
      59           0 :     if (s->random_seed < 0)
      60           0 :         s->random_seed = av_get_random_seed();
      61           0 :     seed = s->random_seed;
      62           0 :     av_lfg_init(&s->lfg, seed);
      63             : 
      64           0 :     return 0;
      65             : }
      66             : 
      67           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
      68             : {
      69           0 :     AVFilterContext *ctx = inlink->dst;
      70           0 :     RandomContext *s = ctx->priv;
      71           0 :     AVFilterLink *outlink = ctx->outputs[0];
      72             :     AVFrame *out;
      73             :     int idx;
      74             : 
      75           0 :     if (s->nb_frames_filled < s->nb_frames) {
      76           0 :         s->frames[s->nb_frames_filled] = in;
      77           0 :         s->pts[s->nb_frames_filled++] = in->pts;
      78           0 :         return 0;
      79             :     }
      80             : 
      81           0 :     idx = av_lfg_get(&s->lfg) % s->nb_frames;
      82             : 
      83           0 :     out = s->frames[idx];
      84           0 :     out->pts = s->pts[0];
      85           0 :     memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0]));
      86           0 :     s->frames[idx] = in;
      87           0 :     s->pts[s->nb_frames - 1] = in->pts;
      88             : 
      89           0 :     return ff_filter_frame(outlink, out);
      90             : }
      91             : 
      92           0 : static int request_frame(AVFilterLink *outlink)
      93             : {
      94           0 :     AVFilterContext *ctx = outlink->src;
      95           0 :     RandomContext *s = ctx->priv;
      96             :     int ret;
      97             : 
      98           0 :     ret = ff_request_frame(ctx->inputs[0]);
      99             : 
     100           0 :     if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) {
     101           0 :         AVFrame *out = s->frames[s->nb_frames - 1];
     102           0 :         out->pts = s->pts[s->flush_idx++];
     103           0 :         ret = ff_filter_frame(outlink, out);
     104           0 :         s->frames[s->nb_frames - 1] = NULL;
     105           0 :         s->nb_frames--;
     106             :     }
     107             : 
     108           0 :     return ret;
     109             : }
     110             : 
     111             : static const AVFilterPad random_inputs[] = {
     112             :     {
     113             :         .name         = "default",
     114             :         .type         = AVMEDIA_TYPE_VIDEO,
     115             :         .filter_frame = filter_frame,
     116             :     },
     117             :     { NULL }
     118             : };
     119             : 
     120             : static const AVFilterPad random_outputs[] = {
     121             :     {
     122             :         .name          = "default",
     123             :         .type          = AVMEDIA_TYPE_VIDEO,
     124             :         .request_frame = request_frame,
     125             :     },
     126             :     { NULL }
     127             : };
     128             : 
     129             : AVFilter ff_vf_random = {
     130             :     .name        = "random",
     131             :     .description = NULL_IF_CONFIG_SMALL("Return random frames."),
     132             :     .priv_size   = sizeof(RandomContext),
     133             :     .priv_class  = &random_class,
     134             :     .init        = init,
     135             :     .inputs      = random_inputs,
     136             :     .outputs     = random_outputs,
     137             : };

Generated by: LCOV version 1.13