FFmpeg
f_segment.c
Go to the documentation of this file.
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 "config_components.h"
20 
21 #include <stdint.h>
22 
23 #include "libavutil/avstring.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "internal.h"
33 
34 typedef struct SegmentContext {
35  const AVClass *class;
36 
38  char *points_str;
40 
42  int nb_points;
43  int64_t last_pts;
44 
45  int64_t *points;
47 
48 static void count_points(char *item_str, int *nb_items)
49 {
50  char *p;
51 
52  if (!item_str)
53  return;
54 
55  *nb_items = 1;
56  for (p = item_str; *p; p++) {
57  if (*p == '|')
58  (*nb_items)++;
59  }
60 }
61 
62 static int parse_points(AVFilterContext *ctx, char *item_str, int nb_points, int64_t *points)
63 {
64  SegmentContext *s = ctx->priv;
65  char *arg, *p = item_str;
66  char *saveptr = NULL;
67  int64_t ref, cur = 0;
68  int ret = 0;
69 
70  for (int i = 0; i < nb_points; i++) {
71  if (!(arg = av_strtok(p, "|", &saveptr)))
72  return AVERROR(EINVAL);
73 
74  p = NULL;
75  ref = 0;
76  if (*arg == '+') {
77  ref = cur;
78  arg++;
79  }
80 
81  if (s->use_timestamps) {
82  ret = av_parse_time(&points[i], arg, s->use_timestamps);
83  } else {
84  if (sscanf(arg, "%"SCNd64, &points[i]) != 1)
85  ret = AVERROR(EINVAL);
86  }
87 
88  if (ret < 0) {
89  av_log(ctx, AV_LOG_ERROR, "Invalid splits supplied: %s\n", arg);
90  return ret;
91  }
92 
93  cur = points[i];
94  points[i] += ref;
95  }
96 
97  return 0;
98 }
99 
101 {
102  SegmentContext *s = ctx->priv;
103  char *split_str;
104  int ret;
105 
106  if (s->timestamps_str && s->points_str) {
107  av_log(ctx, AV_LOG_ERROR, "Both timestamps and counts supplied.\n");
108  return AVERROR(EINVAL);
109  } else if (s->timestamps_str) {
110  s->use_timestamps = 1;
111  split_str = s->timestamps_str;
112  } else if (s->points_str) {
113  split_str = s->points_str;
114  } else {
115  av_log(ctx, AV_LOG_ERROR, "Neither timestamps nor durations nor counts supplied.\n");
116  return AVERROR(EINVAL);
117  }
118 
119  count_points(split_str, &s->nb_points);
120  s->nb_points++;
121 
122  s->points = av_calloc(s->nb_points, sizeof(*s->points));
123  if (!s->points)
124  return AVERROR(ENOMEM);
125 
126  ret = parse_points(ctx, split_str, s->nb_points - 1, s->points);
127  if (ret < 0)
128  return ret;
129 
130  s->points[s->nb_points - 1] = INT64_MAX;
131 
132  for (int i = 0; i < s->nb_points; i++) {
133  AVFilterPad pad = { 0 };
134 
135  pad.type = type;
136  pad.name = av_asprintf("output%d", i);
137  if (!pad.name)
138  return AVERROR(ENOMEM);
139 
140  if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
141  return ret;
142  }
143 
144  return 0;
145 }
146 
148 {
149  AVFilterContext *ctx = inlink->dst;
150  SegmentContext *s = ctx->priv;
151  AVRational tb = inlink->time_base;
152 
153  if (s->use_timestamps) {
154  for (int i = 0; i < s->nb_points - 1; i++)
155  s->points[i] = av_rescale_q(s->points[i], AV_TIME_BASE_Q, tb);
156  }
157 
158  return 0;
159 }
160 
162 {
163  SegmentContext *s = ctx->priv;
164  AVFilterLink *inlink = ctx->inputs[0];
165  int ret = 0;
166 
167  if (s->use_timestamps) {
168  ret = frame->pts >= s->points[s->current_point];
169  } else {
170  switch (inlink->type) {
171  case AVMEDIA_TYPE_VIDEO:
172  ret = inlink->frame_count_out - 1 >= s->points[s->current_point];
173  break;
174  case AVMEDIA_TYPE_AUDIO:
175  ret = inlink->sample_count_out - frame->nb_samples >= s->points[s->current_point];
176  break;
177  }
178  }
179 
180  return ret;
181 }
182 
184 {
185  AVFilterLink *inlink = ctx->inputs[0];
186  SegmentContext *s = ctx->priv;
187  AVFrame *frame = NULL;
188  int ret, status;
189  int64_t max_samples;
190  int64_t diff;
191  int64_t pts;
192 
193  for (int i = s->current_point; i < s->nb_points; i++) {
195  }
196 
197  switch (inlink->type) {
198  case AVMEDIA_TYPE_VIDEO:
200  break;
201  case AVMEDIA_TYPE_AUDIO:
202  diff = s->points[s->current_point] - inlink->sample_count_out;
203  while (diff <= 0) {
204  ff_outlink_set_status(ctx->outputs[s->current_point], AVERROR_EOF, s->last_pts);
205  s->current_point++;
206  if (s->current_point >= s->nb_points)
207  return AVERROR(EINVAL);
208 
209  diff = s->points[s->current_point] - inlink->sample_count_out;
210  }
211  if (s->use_timestamps) {
212  max_samples = av_rescale_q(diff, av_make_q(1, inlink->sample_rate), inlink->time_base);
213  } else {
214  max_samples = FFMAX(1, FFMIN(diff, INT_MAX));
215  }
216  if (max_samples <= 0 || max_samples > INT_MAX)
218  else
219  ret = ff_inlink_consume_samples(inlink, 1, max_samples, &frame);
220  break;
221  default:
222  return AVERROR_BUG;
223  }
224 
225  if (ret > 0) {
226  s->last_pts = frame->pts;
228  ff_outlink_set_status(ctx->outputs[s->current_point], AVERROR_EOF, frame->pts);
229  s->current_point++;
230  }
231 
232  if (s->current_point >= s->nb_points) {
234  return AVERROR(EINVAL);
235  }
236 
237  ret = ff_filter_frame(ctx->outputs[s->current_point], frame);
238  }
239 
240  if (ret < 0) {
241  return ret;
242  } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
243  for (int i = s->current_point; i < s->nb_points; i++)
244  ff_outlink_set_status(ctx->outputs[i], status, pts);
245  return 0;
246  } else {
247  for (int i = s->current_point; i < s->nb_points; i++) {
248  if (ff_outlink_frame_wanted(ctx->outputs[i]))
250  }
251  return 0;
252  }
253 }
254 
256 {
257  SegmentContext *s = ctx->priv;
258 
259  av_freep(&s->points);
260 }
261 
262 #define OFFSET(x) offsetof(SegmentContext, x)
263 #define COMMON_OPTS \
264  { "timestamps", "timestamps of input at which to split input", OFFSET(timestamps_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, \
265 
266 #if CONFIG_SEGMENT_FILTER
267 
268 static av_cold int video_init(AVFilterContext *ctx)
269 {
270  return init(ctx, AVMEDIA_TYPE_VIDEO);
271 }
272 
273 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
274 static const AVOption segment_options[] = {
276  { "frames", "frames at which to split input", OFFSET(points_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
277  { NULL }
278 };
279 #undef FLAGS
280 
282 
283 static const AVFilterPad segment_inputs[] = {
284  {
285  .name = "default",
286  .type = AVMEDIA_TYPE_VIDEO,
287  .config_props = config_input,
288  },
289 };
290 
291 const AVFilter ff_vf_segment = {
292  .name = "segment",
293  .description = NULL_IF_CONFIG_SMALL("Segment video stream."),
294  .init = video_init,
295  .uninit = uninit,
296  .priv_size = sizeof(SegmentContext),
297  .priv_class = &segment_class,
298  .activate = activate,
299  FILTER_INPUTS(segment_inputs),
300  .outputs = NULL,
302 };
303 #endif // CONFIG_SEGMENT_FILTER
304 
305 #if CONFIG_ASEGMENT_FILTER
306 
307 static av_cold int audio_init(AVFilterContext *ctx)
308 {
309  return init(ctx, AVMEDIA_TYPE_AUDIO);
310 }
311 
312 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
313 static const AVOption asegment_options[] = {
315  { "samples", "samples at which to split input", OFFSET(points_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
316  { NULL }
317 };
318 #undef FLAGS
319 
320 AVFILTER_DEFINE_CLASS(asegment);
321 
322 static const AVFilterPad asegment_inputs[] = {
323  {
324  .name = "default",
325  .type = AVMEDIA_TYPE_AUDIO,
326  .config_props = config_input,
327  },
328 };
329 
330 const AVFilter ff_af_asegment = {
331  .name = "asegment",
332  .description = NULL_IF_CONFIG_SMALL("Segment audio stream."),
333  .init = audio_init,
334  .uninit = uninit,
335  .priv_size = sizeof(SegmentContext),
336  .priv_class = &asegment_class,
337  .activate = activate,
338  FILTER_INPUTS(asegment_inputs),
339  .outputs = NULL,
341 };
342 #endif // CONFIG_ASEGMENT_FILTER
current_segment_finished
static int current_segment_finished(AVFilterContext *ctx, AVFrame *frame)
Definition: f_segment.c:161
SegmentContext::points_str
char * points_str
Definition: f_segment.c:38
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
parse_points
static int parse_points(AVFilterContext *ctx, char *item_str, int nb_points, int64_t *points)
Definition: f_segment.c:62
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
ff_af_asegment
const AVFilter ff_af_asegment
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
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_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:115
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVOption
AVOption.
Definition: opt.h:357
FLAGS
#define FLAGS
Definition: cmdutils.c:581
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1442
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
OFFSET
#define OFFSET(x)
Definition: f_segment.c:262
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
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
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1568
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:178
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1462
NULL
#define NULL
Definition: coverity.c:32
activate
static int activate(AVFilterContext *ctx)
Definition: f_segment.c:183
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
parseutils.h
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:589
COMMON_OPTS
#define COMMON_OPTS
Definition: f_segment.c:263
count_points
static void count_points(char *item_str, int *nb_items)
Definition: f_segment.c:48
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1389
AVFILTER_FLAG_DYNAMIC_OUTPUTS
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:112
AVMediaType
AVMediaType
Definition: avutil.h:199
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:94
init
static av_cold int init(AVFilterContext *ctx, enum AVMediaType type)
Definition: f_segment.c:100
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
SegmentContext
Definition: f_segment.c:34
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:165
SegmentContext::current_point
int current_point
Definition: f_segment.c:41
internal.h
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:323
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_vf_segment
const AVFilter ff_vf_segment
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
SegmentContext::nb_points
int nb_points
Definition: f_segment.c:42
tb
#define tb
Definition: regdef.h:68
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AVFilter
Filter definition.
Definition: avfilter.h:166
SegmentContext::use_timestamps
int use_timestamps
Definition: f_segment.c:39
ret
ret
Definition: filter_design.txt:187
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:44
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
status
ov_status_e status
Definition: dnn_backend_openvino.c:121
SegmentContext::timestamps_str
char * timestamps_str
Definition: f_segment.c:37
avfilter.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_segment.c:255
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:133
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
segment
Definition: hls.c:77
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
SegmentContext::last_pts
int64_t last_pts
Definition: f_segment.c:43
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
SegmentContext::points
int64_t * points
Definition: f_segment.c:45
ff_append_outpad_free_name
int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:143
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
config_input
static int config_input(AVFilterLink *inlink)
Definition: f_segment.c:147
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:249