2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
31 typedef struct ShuffleFramesContext {
39 } ShuffleFramesContext;
41 static av_cold int init(AVFilterContext *ctx)
43 ShuffleFramesContext *s = ctx->priv;
44 char *mapping, *saveptr = NULL, *p;
48 for (p = s->mapping; *p; p++) {
49 if (*p == '|' || *p == ' ')
53 s->frames = av_calloc(nb_items, sizeof(*s->frames));
54 s->map = av_calloc(nb_items, sizeof(*s->map));
55 s->pts = av_calloc(nb_items, sizeof(*s->pts));
56 if (!s->map || !s->frames || !s->pts) {
57 return AVERROR(ENOMEM);
60 mapping = av_strdup(s->mapping);
62 return AVERROR(ENOMEM);
64 for (n = 0; n < nb_items; n++) {
65 char *map = av_strtok(n == 0 ? mapping : NULL, " |", &saveptr);
66 if (!map || sscanf(map, "%d", &s->map[n]) != 1) {
68 return AVERROR(EINVAL);
71 if (s->map[n] < -1 || s->map[n] >= nb_items) {
72 av_log(ctx, AV_LOG_ERROR, "Index %d out of range: [-1, %d].\n", s->map[n], nb_items - 1);
74 return AVERROR(EINVAL);
78 s->nb_frames = nb_items;
83 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
85 AVFilterContext *ctx = inlink->dst;
86 ShuffleFramesContext *s = ctx->priv;
89 if (s->in_frames < s->nb_frames) {
90 s->frames[s->in_frames] = frame;
91 s->pts[s->in_frames] = frame->pts;
95 if (s->in_frames == s->nb_frames) {
98 for (n = 0; n < s->nb_frames; n++) {
103 out = av_frame_clone(s->frames[x]);
105 return AVERROR(ENOMEM);
106 out->pts = s->pts[n];
107 ret = ff_filter_frame(ctx->outputs[0], out);
112 for (n = 0; n < s->nb_frames; n++)
113 av_frame_free(&s->frames[n]);
119 static av_cold void uninit(AVFilterContext *ctx)
121 ShuffleFramesContext *s = ctx->priv;
123 while (s->in_frames > 0) {
125 av_frame_free(&s->frames[s->in_frames]);
128 av_freep(&s->frames);
133 #define OFFSET(x) offsetof(ShuffleFramesContext, x)
134 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
135 static const AVOption shuffleframes_options[] = {
136 { "mapping", "set destination indexes of input frames", OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
140 AVFILTER_DEFINE_CLASS(shuffleframes);
142 static const AVFilterPad shuffleframes_inputs[] = {
145 .type = AVMEDIA_TYPE_VIDEO,
146 .filter_frame = filter_frame,
151 static const AVFilterPad shuffleframes_outputs[] = {
154 .type = AVMEDIA_TYPE_VIDEO,
159 const AVFilter ff_vf_shuffleframes = {
160 .name = "shuffleframes",
161 .description = NULL_IF_CONFIG_SMALL("Shuffle video frames."),
162 .priv_size = sizeof(ShuffleFramesContext),
163 .priv_class = &shuffleframes_class,
166 .inputs = shuffleframes_inputs,
167 .outputs = shuffleframes_outputs,
168 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,