LCOV - code coverage report
Current view: top level - src/libavfilter - f_perms.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 26 32 81.2 %
Date: 2017-06-24 07:01:58 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Clément Bœsch
       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 "audio.h"
      25             : #include "video.h"
      26             : 
      27             : enum mode {
      28             :     MODE_NONE,
      29             :     MODE_RO,
      30             :     MODE_RW,
      31             :     MODE_TOGGLE,
      32             :     MODE_RANDOM,
      33             :     NB_MODES
      34             : };
      35             : 
      36             : typedef struct PermsContext {
      37             :     const AVClass *class;
      38             :     AVLFG lfg;
      39             :     int64_t random_seed;
      40             :     int mode;
      41             : } PermsContext;
      42             : 
      43             : #define OFFSET(x) offsetof(PermsContext, x)
      44             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
      45             : 
      46             : static const AVOption options[] = {
      47             :     { "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_NONE}, MODE_NONE, NB_MODES-1, FLAGS, "mode" },
      48             :         { "none",   "do nothing",                       0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE},       INT_MIN, INT_MAX, FLAGS, "mode" },
      49             :         { "ro",     "set all output frames read-only",  0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO},         INT_MIN, INT_MAX, FLAGS, "mode" },
      50             :         { "rw",     "set all output frames writable",   0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW},         INT_MIN, INT_MAX, FLAGS, "mode" },
      51             :         { "toggle", "switch permissions",               0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE},     INT_MIN, INT_MAX, FLAGS, "mode" },
      52             :         { "random", "set permissions randomly",         0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM},     INT_MIN, INT_MAX, FLAGS, "mode" },
      53             :     { "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
      54             :     { NULL }
      55             : };
      56             : 
      57          12 : static av_cold int init(AVFilterContext *ctx)
      58             : {
      59          12 :     PermsContext *s = ctx->priv;
      60             : 
      61          12 :     if (s->mode == MODE_RANDOM) {
      62             :         uint32_t seed;
      63             : 
      64          12 :         if (s->random_seed == -1)
      65          12 :             s->random_seed = av_get_random_seed();
      66          12 :         seed = s->random_seed;
      67          12 :         av_log(ctx, AV_LOG_INFO, "random seed: 0x%08"PRIx32"\n", seed);
      68          12 :         av_lfg_init(&s->lfg, seed);
      69             :     }
      70             : 
      71          12 :     return 0;
      72             : }
      73             : 
      74             : enum perm                        {  RO,   RW  };
      75             : static const char * const perm_str[2] = { "RO", "RW" };
      76             : 
      77         631 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
      78             : {
      79             :     int ret;
      80         631 :     AVFilterContext *ctx = inlink->dst;
      81         631 :     PermsContext *s = ctx->priv;
      82         631 :     AVFrame *out = frame;
      83         631 :     enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
      84             :     enum perm out_perm;
      85             : 
      86         631 :     switch (s->mode) {
      87           0 :     case MODE_TOGGLE:   out_perm = in_perm == RO ? RW : RO;                 break;
      88         631 :     case MODE_RANDOM:   out_perm = av_lfg_get(&s->lfg) & 1 ? RW : RO;       break;
      89           0 :     case MODE_RO:       out_perm = RO;                                      break;
      90           0 :     case MODE_RW:       out_perm = RW;                                      break;
      91           0 :     default:            out_perm = in_perm;                                 break;
      92             :     }
      93             : 
      94         631 :     av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
      95             :            perm_str[in_perm], perm_str[out_perm],
      96             :            in_perm == out_perm ? " (no-op)" : "");
      97             : 
      98         631 :     if (in_perm == RO && out_perm == RW) {
      99         220 :         if ((ret = av_frame_make_writable(frame)) < 0)
     100           0 :             return ret;
     101         521 :     } else if (in_perm == RW && out_perm == RO) {
     102         246 :         out = av_frame_clone(frame);
     103         246 :         if (!out)
     104           0 :             return AVERROR(ENOMEM);
     105             :     }
     106             : 
     107         631 :     ret = ff_filter_frame(ctx->outputs[0], out);
     108             : 
     109         631 :     if (in_perm == RW && out_perm == RO)
     110         246 :         av_frame_free(&frame);
     111         631 :     return ret;
     112             : }
     113             : 
     114             : #if CONFIG_APERMS_FILTER
     115             : 
     116             : #define aperms_options options
     117             : AVFILTER_DEFINE_CLASS(aperms);
     118             : 
     119             : static const AVFilterPad aperms_inputs[] = {
     120             :     {
     121             :         .name         = "default",
     122             :         .type         = AVMEDIA_TYPE_AUDIO,
     123             :         .filter_frame = filter_frame,
     124             :     },
     125             :     { NULL }
     126             : };
     127             : 
     128             : static const AVFilterPad aperms_outputs[] = {
     129             :     {
     130             :         .name = "default",
     131             :         .type = AVMEDIA_TYPE_AUDIO,
     132             :     },
     133             :     { NULL }
     134             : };
     135             : 
     136             : AVFilter ff_af_aperms = {
     137             :     .name        = "aperms",
     138             :     .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
     139             :     .init        = init,
     140             :     .priv_size   = sizeof(PermsContext),
     141             :     .inputs      = aperms_inputs,
     142             :     .outputs     = aperms_outputs,
     143             :     .priv_class  = &aperms_class,
     144             : };
     145             : #endif /* CONFIG_APERMS_FILTER */
     146             : 
     147             : #if CONFIG_PERMS_FILTER
     148             : 
     149             : #define perms_options options
     150             : AVFILTER_DEFINE_CLASS(perms);
     151             : 
     152             : static const AVFilterPad perms_inputs[] = {
     153             :     {
     154             :         .name         = "default",
     155             :         .type         = AVMEDIA_TYPE_VIDEO,
     156             :         .filter_frame = filter_frame,
     157             :     },
     158             :     { NULL }
     159             : };
     160             : 
     161             : static const AVFilterPad perms_outputs[] = {
     162             :     {
     163             :         .name = "default",
     164             :         .type = AVMEDIA_TYPE_VIDEO,
     165             :     },
     166             :     { NULL }
     167             : };
     168             : 
     169             : AVFilter ff_vf_perms = {
     170             :     .name        = "perms",
     171             :     .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
     172             :     .init        = init,
     173             :     .priv_size   = sizeof(PermsContext),
     174             :     .inputs      = perms_inputs,
     175             :     .outputs     = perms_outputs,
     176             :     .priv_class  = &perms_class,
     177             : };
     178             : #endif /* CONFIG_PERMS_FILTER */

Generated by: LCOV version 1.13