FFmpeg
Loading...
Searching...
No Matches
vf_shuffleframes.c
Go to the documentation of this file.
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/avstring.h"
22#include "libavutil/common.h"
23#include "libavutil/internal.h"
24#include "libavutil/mem.h"
25#include "libavutil/opt.h"
26
27#include "avfilter.h"
28#include "filters.h"
29#include "video.h"
30
40
42{
43 ShuffleFramesContext *s = ctx->priv;
44 char *mapping, *saveptr = NULL, *p;
45 int n, nb_items;
46
47 nb_items = 1;
48 for (p = s->mapping; *p; p++) {
49 if (*p == '|' || *p == ' ')
50 nb_items++;
51 }
52
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);
58 }
59
60 mapping = av_strdup(s->mapping);
61 if (!mapping)
62 return AVERROR(ENOMEM);
63
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) {
67 av_free(mapping);
68 return AVERROR(EINVAL);
69 }
70
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);
73 av_free(mapping);
74 return AVERROR(EINVAL);
75 }
76 }
77
78 s->nb_frames = nb_items;
79 av_free(mapping);
80 return 0;
81}
82
84{
85 AVFilterContext *ctx = inlink->dst;
86 ShuffleFramesContext *s = ctx->priv;
87 int ret = 0;
88
89 if (s->in_frames < s->nb_frames) {
90 s->frames[s->in_frames] = frame;
91 s->pts[s->in_frames] = frame->pts;
92 s->in_frames++;
93 }
94
95 if (s->in_frames == s->nb_frames) {
96 int n, x;
97
98 for (n = 0; n < s->nb_frames; n++) {
99 AVFrame *out;
100
101 x = s->map[n];
102 if (x >= 0) {
103 out = av_frame_clone(s->frames[x]);
104 if (!out)
105 return AVERROR(ENOMEM);
106 out->pts = s->pts[n];
107 ret = ff_filter_frame(ctx->outputs[0], out);
108 }
109 s->in_frames--;
110 }
111
112 for (n = 0; n < s->nb_frames; n++)
113 av_frame_free(&s->frames[n]);
114 }
115
116 return ret;
117}
118
120{
121 ShuffleFramesContext *s = ctx->priv;
122
123 while (s->in_frames > 0) {
124 s->in_frames--;
125 av_frame_free(&s->frames[s->in_frames]);
126 }
127
128 av_freep(&s->frames);
129 av_freep(&s->map);
130 av_freep(&s->pts);
131}
132
133#define OFFSET(x) offsetof(ShuffleFramesContext, x)
134#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
136 { "mapping", "set destination indexes of input frames", OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
137 { NULL },
138};
139
141
143 {
144 .name = "default",
145 .type = AVMEDIA_TYPE_VIDEO,
146 .filter_frame = filter_frame,
147 },
148};
149
151 .p.name = "shuffleframes",
152 .p.description = NULL_IF_CONFIG_SMALL("Shuffle video frames."),
153 .p.priv_class = &shuffleframes_class,
155 .priv_size = sizeof(ShuffleFramesContext),
156 .init = init,
157 .uninit = uninit,
160};
const FFFilter ff_vf_shuffleframes
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
const VDPAUPixFmtMap * map
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static const AVOption shuffleframes_options[]
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static const AVFilterPad shuffleframes_inputs[]
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37