LCOV - code coverage report
Current view: top level - libavfilter - trim.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 122 125 97.6 %
Date: 2017-12-18 20:14:19 Functions: 4 4 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             : #include <stdint.h>
      20             : 
      21             : #include "config.h"
      22             : 
      23             : #include "libavutil/avassert.h"
      24             : #include "libavutil/channel_layout.h"
      25             : #include "libavutil/common.h"
      26             : #include "libavutil/log.h"
      27             : #include "libavutil/mathematics.h"
      28             : #include "libavutil/opt.h"
      29             : #include "libavutil/samplefmt.h"
      30             : 
      31             : #include "audio.h"
      32             : #include "avfilter.h"
      33             : #include "internal.h"
      34             : 
      35             : typedef struct TrimContext {
      36             :     const AVClass *class;
      37             : 
      38             :     /*
      39             :      * AVOptions
      40             :      */
      41             :     int64_t duration;
      42             :     int64_t start_time, end_time;
      43             :     int64_t start_frame, end_frame;
      44             :     /*
      45             :      * in the link timebase for video,
      46             :      * in 1/samplerate for audio
      47             :      */
      48             :     int64_t start_pts, end_pts;
      49             :     int64_t start_sample, end_sample;
      50             : 
      51             :     /*
      52             :      * number of video frames that arrived on this filter so far
      53             :      */
      54             :     int64_t nb_frames;
      55             :     /*
      56             :      * number of audio samples that arrived on this filter so far
      57             :      */
      58             :     int64_t nb_samples;
      59             :     /*
      60             :      * timestamp of the first frame in the output, in the timebase units
      61             :      */
      62             :     int64_t first_pts;
      63             :     /*
      64             :      * duration in the timebase units
      65             :      */
      66             :     int64_t duration_tb;
      67             : 
      68             :     int64_t next_pts;
      69             : 
      70             :     int eof;
      71             : } TrimContext;
      72             : 
      73         491 : static av_cold int init(AVFilterContext *ctx)
      74             : {
      75         491 :     TrimContext *s = ctx->priv;
      76             : 
      77         491 :     s->first_pts = AV_NOPTS_VALUE;
      78             : 
      79         491 :     return 0;
      80             : }
      81             : 
      82         491 : static int config_input(AVFilterLink *inlink)
      83             : {
      84         491 :     AVFilterContext *ctx = inlink->dst;
      85         491 :     TrimContext       *s = ctx->priv;
      86         889 :     AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
      87         398 :                      inlink->time_base : (AVRational){ 1, inlink->sample_rate };
      88             : 
      89         491 :     if (s->start_time != INT64_MAX) {
      90          13 :         int64_t start_pts = av_rescale_q(s->start_time, AV_TIME_BASE_Q, tb);
      91          13 :         if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
      92          13 :             s->start_pts = start_pts;
      93             :     }
      94         491 :     if (s->end_time != INT64_MAX) {
      95           4 :         int64_t end_pts = av_rescale_q(s->end_time, AV_TIME_BASE_Q, tb);
      96           4 :         if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
      97           4 :             s->end_pts = end_pts;
      98             :     }
      99         491 :     if (s->duration)
     100         146 :         s->duration_tb = av_rescale_q(s->duration, AV_TIME_BASE_Q, tb);
     101             : 
     102         491 :     return 0;
     103             : }
     104             : 
     105             : #define OFFSET(x) offsetof(TrimContext, x)
     106             : #define COMMON_OPTS                                                                                                                                                         \
     107             :     { "start",       "Timestamp of the first frame that "                                                                                                        \
     108             :         "should be passed",                                              OFFSET(start_time),  AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX },    INT64_MIN, INT64_MAX, FLAGS }, \
     109             :     { "starti",      "Timestamp of the first frame that "                                                                                                        \
     110             :         "should be passed",                                              OFFSET(start_time),  AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX },    INT64_MIN, INT64_MAX, FLAGS }, \
     111             :     { "end",         "Timestamp of the first frame that "                                                                                                        \
     112             :         "should be dropped again",                                       OFFSET(end_time),    AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX },    INT64_MIN, INT64_MAX, FLAGS }, \
     113             :     { "endi",        "Timestamp of the first frame that "                                                                                                        \
     114             :         "should be dropped again",                                       OFFSET(end_time),    AV_OPT_TYPE_DURATION, { .i64 = INT64_MAX },    INT64_MIN, INT64_MAX, FLAGS }, \
     115             :     { "start_pts",   "Timestamp of the first frame that should be "                                                                                                         \
     116             :        " passed",                                                        OFFSET(start_pts),   AV_OPT_TYPE_INT64,  { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
     117             :     { "end_pts",     "Timestamp of the first frame that should be "                                                                                                         \
     118             :         "dropped again",                                                 OFFSET(end_pts),     AV_OPT_TYPE_INT64,  { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
     119             :     { "duration",    "Maximum duration of the output",                   OFFSET(duration),    AV_OPT_TYPE_DURATION, { .i64 = 0 },                    0, INT64_MAX, FLAGS }, \
     120             :     { "durationi",   "Maximum duration of the output",                   OFFSET(duration),    AV_OPT_TYPE_DURATION, { .i64 = 0 },                    0, INT64_MAX, FLAGS },
     121             : 
     122             : 
     123             : #if CONFIG_TRIM_FILTER
     124        3580 : static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
     125             : {
     126        3580 :     AVFilterContext *ctx = inlink->dst;
     127        3580 :     TrimContext       *s = ctx->priv;
     128             :     int drop;
     129             : 
     130             :     /* drop everything if EOF has already been returned */
     131        3580 :     if (s->eof) {
     132          13 :         av_frame_free(&frame);
     133          13 :         return 0;
     134             :     }
     135             : 
     136        3567 :     if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
     137         124 :         drop = 1;
     138         124 :         if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
     139          18 :             drop = 0;
     140         237 :         if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
     141         113 :             frame->pts >= s->start_pts)
     142          98 :             drop = 0;
     143         124 :         if (drop)
     144          14 :             goto drop;
     145             :     }
     146             : 
     147        3553 :     if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
     148          93 :         s->first_pts = frame->pts;
     149             : 
     150        3553 :     if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
     151        3466 :         drop = 1;
     152             : 
     153        3466 :         if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
     154           9 :             drop = 0;
     155        3479 :         if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
     156          13 :             frame->pts < s->end_pts)
     157          11 :             drop = 0;
     158        6911 :         if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
     159        3445 :             frame->pts - s->first_pts < s->duration_tb)
     160        3357 :             drop = 0;
     161             : 
     162        3466 :         if (drop) {
     163          91 :             s->eof = 1;
     164          91 :             ff_avfilter_link_set_out_status(inlink, AVERROR_EOF, AV_NOPTS_VALUE);
     165          91 :             goto drop;
     166             :         }
     167             :     }
     168             : 
     169        3462 :     s->nb_frames++;
     170             : 
     171        3462 :     return ff_filter_frame(ctx->outputs[0], frame);
     172             : 
     173         105 : drop:
     174         105 :     s->nb_frames++;
     175         105 :     av_frame_free(&frame);
     176         105 :     return 0;
     177             : }
     178             : 
     179             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
     180             : static const AVOption trim_options[] = {
     181             :     COMMON_OPTS
     182             :     { "start_frame", "Number of the first frame that should be passed "
     183             :         "to the output",                                                 OFFSET(start_frame), AV_OPT_TYPE_INT64,  { .i64 = -1 },       -1, INT64_MAX, FLAGS },
     184             :     { "end_frame",   "Number of the first frame that should be dropped "
     185             :         "again",                                                         OFFSET(end_frame),   AV_OPT_TYPE_INT64,  { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
     186             :     { NULL }
     187             : };
     188             : #undef FLAGS
     189             : 
     190             : AVFILTER_DEFINE_CLASS(trim);
     191             : 
     192             : static const AVFilterPad trim_inputs[] = {
     193             :     {
     194             :         .name         = "default",
     195             :         .type         = AVMEDIA_TYPE_VIDEO,
     196             :         .filter_frame = trim_filter_frame,
     197             :         .config_props = config_input,
     198             :     },
     199             :     { NULL }
     200             : };
     201             : 
     202             : static const AVFilterPad trim_outputs[] = {
     203             :     {
     204             :         .name         = "default",
     205             :         .type         = AVMEDIA_TYPE_VIDEO,
     206             :     },
     207             :     { NULL }
     208             : };
     209             : 
     210             : AVFilter ff_vf_trim = {
     211             :     .name        = "trim",
     212             :     .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
     213             :     .init        = init,
     214             :     .priv_size   = sizeof(TrimContext),
     215             :     .priv_class  = &trim_class,
     216             :     .inputs      = trim_inputs,
     217             :     .outputs     = trim_outputs,
     218             : };
     219             : #endif // CONFIG_TRIM_FILTER
     220             : 
     221             : #if CONFIG_ATRIM_FILTER
     222        7126 : static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
     223             : {
     224        7126 :     AVFilterContext *ctx = inlink->dst;
     225        7126 :     TrimContext       *s = ctx->priv;
     226             :     int64_t start_sample, end_sample;
     227             :     int64_t pts;
     228             :     int drop;
     229             : 
     230             :     /* drop everything if EOF has already been returned */
     231        7126 :     if (s->eof) {
     232          39 :         av_frame_free(&frame);
     233          39 :         return 0;
     234             :     }
     235             : 
     236        7087 :     if (frame->pts != AV_NOPTS_VALUE)
     237        7087 :         pts = av_rescale_q(frame->pts, inlink->time_base,
     238        7087 :                            (AVRational){ 1, inlink->sample_rate });
     239             :     else
     240           0 :         pts = s->next_pts;
     241        7087 :     s->next_pts = pts + frame->nb_samples;
     242             : 
     243             :     /* check if at least a part of the frame is after the start time */
     244        7087 :     if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
     245        5951 :         start_sample = 0;
     246             :     } else {
     247        1136 :         drop = 1;
     248        1136 :         start_sample = frame->nb_samples;
     249             : 
     250        1144 :         if (s->start_sample >= 0 &&
     251           8 :             s->nb_samples + frame->nb_samples > s->start_sample) {
     252           7 :             drop         = 0;
     253           7 :             start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
     254             :         }
     255             : 
     256        2270 :         if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
     257        1134 :             pts + frame->nb_samples > s->start_pts) {
     258        1123 :             drop = 0;
     259        1123 :             start_sample = FFMIN(start_sample, s->start_pts - pts);
     260             :         }
     261             : 
     262        1136 :         if (drop)
     263          10 :             goto drop;
     264             :     }
     265             : 
     266        7077 :     if (s->first_pts == AV_NOPTS_VALUE)
     267         398 :         s->first_pts = pts + start_sample;
     268             : 
     269             :     /* check if at least a part of the frame is before the end time */
     270        7077 :     if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
     271        1111 :         end_sample = frame->nb_samples;
     272             :     } else {
     273        5966 :         drop       = 1;
     274        5966 :         end_sample = 0;
     275             : 
     276        8854 :         if (s->end_sample != INT64_MAX &&
     277        2888 :             s->nb_samples < s->end_sample) {
     278        2554 :             drop       = 0;
     279        2554 :             end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
     280             :         }
     281             : 
     282        5977 :         if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
     283          11 :             pts < s->end_pts) {
     284           9 :             drop       = 0;
     285           9 :             end_sample = FFMAX(end_sample, s->end_pts - pts);
     286             :         }
     287             : 
     288        5966 :         if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
     289        3015 :             drop       = 0;
     290        3015 :             end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
     291             :         }
     292             : 
     293        5966 :         if (drop) {
     294         392 :             s->eof = 1;
     295         392 :             ff_avfilter_link_set_out_status(inlink, AVERROR_EOF, AV_NOPTS_VALUE);
     296         392 :             goto drop;
     297             :         }
     298             :     }
     299             : 
     300        6685 :     s->nb_samples += frame->nb_samples;
     301        6685 :     start_sample   = FFMAX(0, start_sample);
     302        6685 :     end_sample     = FFMIN(frame->nb_samples, end_sample);
     303        6685 :     av_assert0(start_sample < end_sample || (start_sample == end_sample && !frame->nb_samples));
     304             : 
     305        6685 :     if (start_sample) {
     306           5 :         AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
     307           5 :         if (!out) {
     308           0 :             av_frame_free(&frame);
     309           0 :             return AVERROR(ENOMEM);
     310             :         }
     311             : 
     312           5 :         av_frame_copy_props(out, frame);
     313           5 :         av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
     314             :                         out->nb_samples, inlink->channels,
     315           5 :                         frame->format);
     316           5 :         if (out->pts != AV_NOPTS_VALUE)
     317           5 :             out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
     318             :                                      inlink->time_base);
     319             : 
     320           5 :         av_frame_free(&frame);
     321           5 :         frame = out;
     322             :     } else
     323        6680 :         frame->nb_samples = end_sample;
     324             : 
     325        6685 :     return ff_filter_frame(ctx->outputs[0], frame);
     326             : 
     327         402 : drop:
     328         402 :     s->nb_samples += frame->nb_samples;
     329         402 :     av_frame_free(&frame);
     330         402 :     return 0;
     331             : }
     332             : 
     333             : #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
     334             : static const AVOption atrim_options[] = {
     335             :     COMMON_OPTS
     336             :     { "start_sample", "Number of the first audio sample that should be "
     337             :         "passed to the output",                                          OFFSET(start_sample), AV_OPT_TYPE_INT64,  { .i64 = -1 },       -1, INT64_MAX, FLAGS },
     338             :     { "end_sample",   "Number of the first audio sample that should be "
     339             :         "dropped again",                                                 OFFSET(end_sample),   AV_OPT_TYPE_INT64,  { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
     340             :     { NULL }
     341             : };
     342             : #undef FLAGS
     343             : 
     344             : AVFILTER_DEFINE_CLASS(atrim);
     345             : 
     346             : static const AVFilterPad atrim_inputs[] = {
     347             :     {
     348             :         .name         = "default",
     349             :         .type         = AVMEDIA_TYPE_AUDIO,
     350             :         .filter_frame = atrim_filter_frame,
     351             :         .config_props = config_input,
     352             :     },
     353             :     { NULL }
     354             : };
     355             : 
     356             : static const AVFilterPad atrim_outputs[] = {
     357             :     {
     358             :         .name         = "default",
     359             :         .type         = AVMEDIA_TYPE_AUDIO,
     360             :     },
     361             :     { NULL }
     362             : };
     363             : 
     364             : AVFilter ff_af_atrim = {
     365             :     .name        = "atrim",
     366             :     .description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
     367             :     .init        = init,
     368             :     .priv_size   = sizeof(TrimContext),
     369             :     .priv_class  = &atrim_class,
     370             :     .inputs      = atrim_inputs,
     371             :     .outputs     = atrim_outputs,
     372             : };
     373             : #endif // CONFIG_ATRIM_FILTER

Generated by: LCOV version 1.13