FFmpeg
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/opt.h"
25 
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct ShuffleFramesContext {
31  const AVClass *class;
32  char *mapping;
34  int *map;
35  int64_t *pts;
36  int in_frames;
37  int nb_frames;
39 
41 {
42  ShuffleFramesContext *s = ctx->priv;
43  char *mapping, *saveptr = NULL, *p;
44  int n, nb_items;
45 
46  nb_items = 1;
47  for (p = s->mapping; *p; p++) {
48  if (*p == '|' || *p == ' ')
49  nb_items++;
50  }
51 
52  s->frames = av_calloc(nb_items, sizeof(*s->frames));
53  s->map = av_calloc(nb_items, sizeof(*s->map));
54  s->pts = av_calloc(nb_items, sizeof(*s->pts));
55  if (!s->map || !s->frames || !s->pts) {
56  return AVERROR(ENOMEM);
57  }
58 
59  mapping = av_strdup(s->mapping);
60  if (!mapping)
61  return AVERROR(ENOMEM);
62 
63  for (n = 0; n < nb_items; n++) {
64  char *map = av_strtok(n == 0 ? mapping : NULL, " |", &saveptr);
65  if (!map || sscanf(map, "%d", &s->map[n]) != 1) {
66  av_free(mapping);
67  return AVERROR(EINVAL);
68  }
69 
70  if (s->map[n] < -1 || s->map[n] >= nb_items) {
71  av_log(ctx, AV_LOG_ERROR, "Index %d out of range: [-1, %d].\n", s->map[n], nb_items - 1);
72  av_free(mapping);
73  return AVERROR(EINVAL);
74  }
75  }
76 
77  s->nb_frames = nb_items;
78  av_free(mapping);
79  return 0;
80 }
81 
83 {
84  AVFilterContext *ctx = inlink->dst;
85  ShuffleFramesContext *s = ctx->priv;
86  int ret = 0;
87 
88  if (s->in_frames < s->nb_frames) {
89  s->frames[s->in_frames] = frame;
90  s->pts[s->in_frames] = frame->pts;
91  s->in_frames++;
92  }
93 
94  if (s->in_frames == s->nb_frames) {
95  int n, x;
96 
97  for (n = 0; n < s->nb_frames; n++) {
98  AVFrame *out;
99 
100  x = s->map[n];
101  if (x >= 0) {
102  out = av_frame_clone(s->frames[x]);
103  if (!out)
104  return AVERROR(ENOMEM);
105  out->pts = s->pts[n];
106  ret = ff_filter_frame(ctx->outputs[0], out);
107  }
108  s->in_frames--;
109  }
110 
111  for (n = 0; n < s->nb_frames; n++)
112  av_frame_free(&s->frames[n]);
113  }
114 
115  return ret;
116 }
117 
119 {
120  ShuffleFramesContext *s = ctx->priv;
121 
122  while (s->in_frames > 0) {
123  s->in_frames--;
124  av_frame_free(&s->frames[s->in_frames]);
125  }
126 
127  av_freep(&s->frames);
128  av_freep(&s->map);
129  av_freep(&s->pts);
130 }
131 
132 #define OFFSET(x) offsetof(ShuffleFramesContext, x)
133 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
134 static const AVOption shuffleframes_options[] = {
135  { "mapping", "set destination indexes of input frames", OFFSET(mapping), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
136  { NULL },
137 };
138 
139 AVFILTER_DEFINE_CLASS(shuffleframes);
140 
142  {
143  .name = "default",
144  .type = AVMEDIA_TYPE_VIDEO,
145  .filter_frame = filter_frame,
146  },
147 };
148 
150  {
151  .name = "default",
152  .type = AVMEDIA_TYPE_VIDEO,
153  },
154 };
155 
157  .name = "shuffleframes",
158  .description = NULL_IF_CONFIG_SMALL("Shuffle video frames."),
159  .priv_size = sizeof(ShuffleFramesContext),
160  .priv_class = &shuffleframes_class,
161  .init = init,
162  .uninit = uninit,
166 };
ff_vf_shuffleframes
const AVFilter ff_vf_shuffleframes
Definition: vf_shuffleframes.c:156
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_shuffleframes.c:118
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
FLAGS
#define FLAGS
Definition: vf_shuffleframes.c:133
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
video.h
ShuffleFramesContext::frames
AVFrame ** frames
Definition: vf_shuffleframes.c:33
ShuffleFramesContext::in_frames
int in_frames
Definition: vf_shuffleframes.c:36
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
shuffleframes_inputs
static const AVFilterPad shuffleframes_inputs[]
Definition: vf_shuffleframes.c:141
av_strtok
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:186
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:422
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(shuffleframes)
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_shuffleframes.c:82
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_shuffleframes.c:40
ShuffleFramesContext::nb_frames
int nb_frames
Definition: vf_shuffleframes.c:37
ShuffleFramesContext::map
int * map
Definition: vf_shuffleframes.c:34
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
OFFSET
#define OFFSET(x)
Definition: vf_shuffleframes.c:132
ShuffleFramesContext::pts
int64_t * pts
Definition: vf_shuffleframes.c:35
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:146
shuffleframes_outputs
static const AVFilterPad shuffleframes_outputs[]
Definition: vf_shuffleframes.c:149
internal.h
common.h
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avfilter.h
ShuffleFramesContext
Definition: vf_shuffleframes.c:30
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:279
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
shuffleframes_options
static const AVOption shuffleframes_options[]
Definition: vf_shuffleframes.c:134
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
ShuffleFramesContext::mapping
char * mapping
Definition: vf_shuffleframes.c:32