+static int samples_ready(AVFilterLink *link, unsigned min)
+{
+ return ff_framequeue_queued_frames(&link->fifo) &&
+ (ff_framequeue_queued_samples(&link->fifo) >= min ||
+ link->status_in);
+}
+
+static int take_samples(AVFilterLink *link, unsigned min, unsigned max,
+ AVFrame **rframe)
+{
+ AVFrame *frame0, *frame, *buf;
+ unsigned nb_samples, nb_frames, i, p;
+ int ret;
+
+ /* Note: this function relies on no format changes and must only be
+ called with enough samples. */
+ av_assert1(samples_ready(link, link->min_samples));
+ frame0 = frame = ff_framequeue_peek(&link->fifo, 0);
+ if (frame->nb_samples >= min && frame->nb_samples < max) {
+ *rframe = ff_framequeue_take(&link->fifo);
+ return 0;
+ }
+ nb_frames = 0;
+ nb_samples = 0;
+ while (1) {
+ if (nb_samples + frame->nb_samples > max) {
+ if (nb_samples < min)
+ nb_samples = max;
+ break;
+ }
+ nb_samples += frame->nb_samples;
+ nb_frames++;
+ if (nb_frames == ff_framequeue_queued_frames(&link->fifo))
+ break;
+ frame = ff_framequeue_peek(&link->fifo, nb_frames);
+ }
+
+ buf = ff_get_audio_buffer(link, nb_samples);
+ if (!buf)
+ return AVERROR(ENOMEM);
+ ret = av_frame_copy_props(buf, frame0);
+ if (ret < 0) {
+ av_frame_free(&buf);
+ return ret;
+ }
+ buf->pts = frame0->pts;
+
+ p = 0;
+ for (i = 0; i < nb_frames; i++) {
+ frame = ff_framequeue_take(&link->fifo);
+ av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
+ frame->nb_samples, link->channels, link->format);
+ p += frame->nb_samples;
+ av_frame_free(&frame);
+ }
+ if (p < nb_samples) {
+ unsigned n = nb_samples - p;
+ frame = ff_framequeue_peek(&link->fifo, 0);
+ av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
+ link->channels, link->format);
+ ff_framequeue_skip_samples(&link->fifo, n, link->time_base);
+ }
+
+ *rframe = buf;
+ return 0;
+}
+
+static int ff_filter_frame_to_filter(AVFilterLink *link)
+{
+ AVFrame *frame = NULL;
+ AVFilterContext *dst = link->dst;
+ int ret;
+
+ av_assert1(ff_framequeue_queued_frames(&link->fifo));
+ ret = link->min_samples ?
+ ff_inlink_consume_samples(link, link->min_samples, link->max_samples, &frame) :
+ ff_inlink_consume_frame(link, &frame);
+ av_assert1(ret);
+ if (ret < 0) {
+ av_assert1(!frame);
+ return ret;
+ }
+ /* The filter will soon have received a new frame, that may allow it to
+ produce one or more: unblock its outputs. */
+ filter_unblock(dst);
+ /* AVFilterPad.filter_frame() expect frame_count_out to have the value
+ before the frame; ff_filter_frame_framed() will re-increment it. */
+ link->frame_count_out--;
+ ret = ff_filter_frame_framed(link, frame);
+ if (ret < 0 && ret != link->status_out) {
+ ff_avfilter_link_set_out_status(link, ret, AV_NOPTS_VALUE);
+ } else {
+ /* Run once again, to see if several frames were available, or if
+ the input status has also changed, or any other reason. */
+ ff_filter_set_ready(dst, 300);
+ }
+ return ret;
+}
+
+static int forward_status_change(AVFilterContext *filter, AVFilterLink *in)
+{
+ unsigned out = 0, progress = 0;
+ int ret;
+
+ av_assert0(!in->status_out);
+ if (!filter->nb_outputs) {
+ /* not necessary with the current API and sinks */
+ return 0;
+ }
+ while (!in->status_out) {
+ if (!filter->outputs[out]->status_in) {
+ progress++;
+ ret = ff_request_frame_to_filter(filter->outputs[out]);
+ if (ret < 0)
+ return ret;
+ }
+ if (++out == filter->nb_outputs) {
+ if (!progress) {
+ /* Every output already closed: input no longer interesting
+ (example: overlay in shortest mode, other input closed). */
+ ff_avfilter_link_set_out_status(in, in->status_in, in->status_in_pts);
+ return 0;
+ }
+ progress = 0;
+ out = 0;
+ }
+ }
+ ff_filter_set_ready(filter, 200);
+ return 0;
+}
+
+#define FFERROR_NOT_READY FFERRTAG('N','R','D','Y')
+
+static int ff_filter_activate_default(AVFilterContext *filter)
+{
+ unsigned i;
+
+ for (i = 0; i < filter->nb_inputs; i++) {
+ if (samples_ready(filter->inputs[i], filter->inputs[i]->min_samples)) {
+ return ff_filter_frame_to_filter(filter->inputs[i]);
+ }
+ }
+ for (i = 0; i < filter->nb_inputs; i++) {
+ if (filter->inputs[i]->status_in && !filter->inputs[i]->status_out) {
+ av_assert1(!ff_framequeue_queued_frames(&filter->inputs[i]->fifo));
+ return forward_status_change(filter, filter->inputs[i]);
+ }
+ }
+ for (i = 0; i < filter->nb_outputs; i++) {
+ if (filter->outputs[i]->frame_wanted_out &&
+ !filter->outputs[i]->frame_blocked_in) {
+ return ff_request_frame_to_filter(filter->outputs[i]);
+ }
+ }
+ return FFERROR_NOT_READY;
+}
+
+/*
+ Filter scheduling and activation
+
+ When a filter is activated, it must:
+ - if possible, output a frame;
+ - else, if relevant, forward the input status change;
+ - else, check outputs for wanted frames and forward the requests.
+
+ The following AVFilterLink fields are used for activation:
+
+ - frame_wanted_out:
+
+ This field indicates if a frame is needed on this input of the
+ destination filter. A positive value indicates that a frame is needed
+ to process queued frames or internal data or to satisfy the
+ application; a zero value indicates that a frame is not especially
+ needed but could be processed anyway; a negative value indicates that a
+ frame would just be queued.
+
+ It is set by filters using ff_request_frame() or ff_request_no_frame(),
+ when requested by the application through a specific API or when it is
+ set on one of the outputs.
+
+ It is cleared when a frame is sent from the source using
+ ff_filter_frame().
+
+ It is also cleared when a status change is sent from the source using
+ ff_avfilter_link_set_in_status().
+
+ - frame_blocked_in:
+
+ This field means that the source filter can not generate a frame as is.
+ Its goal is to avoid repeatedly calling the request_frame() method on
+ the same link.
+
+ It is set by the framework on all outputs of a filter before activating it.
+
+ It is automatically cleared by ff_filter_frame().
+
+ It is also automatically cleared by ff_avfilter_link_set_in_status().
+
+ It is also cleared on all outputs (using filter_unblock()) when
+ something happens on an input: processing a frame or changing the
+ status.
+
+ - fifo:
+
+ Contains the frames queued on a filter input. If it contains frames and
+ frame_wanted_out is not set, then the filter can be activated. If that
+ result in the filter not able to use these frames, the filter must set
+ frame_wanted_out to ask for more frames.
+
+ - status_in and status_in_pts:
+
+ Status (EOF or error code) of the link and timestamp of the status
+ change (in link time base, same as frames) as seen from the input of
+ the link. The status change is considered happening after the frames
+ queued in fifo.
+
+ It is set by the source filter using ff_avfilter_link_set_in_status().
+
+ - status_out:
+
+ Status of the link as seen from the output of the link. The status
+ change is considered having already happened.
+
+ It is set by the destination filter using
+ ff_avfilter_link_set_out_status().
+
+ Filters are activated according to the ready field, set using the
+ ff_filter_set_ready(). Eventually, a priority queue will be used.
+ ff_filter_set_ready() is called whenever anything could cause progress to
+ be possible. Marking a filter ready when it is not is not a problem,
+ except for the small overhead it causes.
+
+ Conditions that cause a filter to be marked ready are:
+
+ - frames added on an input link;
+
+ - changes in the input or output status of an input link;
+
+ - requests for a frame on an output link;
+
+ - after any actual processing using the legacy methods (filter_frame(),
+ and request_frame() to acknowledge status changes), to run once more
+ and check if enough input was present for several frames.
+
+ Exemples of scenarios to consider:
+
+ - buffersrc: activate if frame_wanted_out to notify the application;
+ activate when the application adds a frame to push it immediately.
+
+ - testsrc: activate only if frame_wanted_out to produce and push a frame.
+
+ - concat (not at stitch points): can process a frame on any output.
+ Activate if frame_wanted_out on output to forward on the corresponding
+ input. Activate when a frame is present on input to process it
+ immediately.
+
+ - framesync: needs at least one frame on each input; extra frames on the
+ wrong input will accumulate. When a frame is first added on one input,
+ set frame_wanted_out<0 on it to avoid getting more (would trigger
+ testsrc) and frame_wanted_out>0 on the other to allow processing it.
+
+ Activation of old filters:
+
+ In order to activate a filter implementing the legacy filter_frame() and
+ request_frame() methods, perform the first possible of the following
+ actions:
+
+ - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
+ frame and call filter_frame().
+
+ Ratinale: filter frames as soon as possible instead of leaving them
+ queued; frame_wanted_out < 0 is not possible since the old API does not
+ set it nor provides any similar feedback; frame_wanted_out > 0 happens
+ when min_samples > 0 and there are not enough samples queued.
+
+ - If an input has status_in set but not status_out, try to call
+ request_frame() on one of the outputs in the hope that it will trigger
+ request_frame() on the input with status_in and acknowledge it. This is
+ awkward and fragile, filters with several inputs or outputs should be
+ updated to direct activation as soon as possible.
+
+ - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
+ request_frame().
+
+ Rationale: checking frame_blocked_in is necessary to avoid requesting
+ repeatedly on a blocked input if another is not blocked (example:
+ [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
+
+ TODO: respect needs_fifo and remove auto-inserted fifos.
+
+ */
+
+int ff_filter_activate(AVFilterContext *filter)
+{
+ int ret;
+
+ /* Generic timeline support is not yet implemented but should be easy */
+ av_assert1(!(filter->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
+ filter->filter->activate));
+ filter->ready = 0;
+ ret = filter->filter->activate ? filter->filter->activate(filter) :
+ ff_filter_activate_default(filter);
+ if (ret == FFERROR_NOT_READY)
+ ret = 0;
+ return ret;
+}
+
+int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
+{
+ *rpts = link->current_pts;
+ if (ff_framequeue_queued_frames(&link->fifo))
+ return *rstatus = 0;
+ if (link->status_out)
+ return *rstatus = link->status_out;
+ if (!link->status_in)
+ return *rstatus = 0;
+ *rstatus = link->status_out = link->status_in;
+ ff_update_link_current_pts(link, link->status_in_pts);
+ *rpts = link->current_pts;
+ return 1;
+}
+
+int ff_inlink_check_available_frame(AVFilterLink *link)
+{
+ return ff_framequeue_queued_frames(&link->fifo) > 0;
+}
+
+int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
+{
+ uint64_t samples = ff_framequeue_queued_samples(&link->fifo);
+ av_assert1(min);
+ return samples >= min || (link->status_in && samples);
+}
+
+static void consume_update(AVFilterLink *link, const AVFrame *frame)
+{
+ ff_update_link_current_pts(link, frame->pts);
+ ff_inlink_process_commands(link, frame);
+ link->dst->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
+ link->frame_count_out++;
+}
+
+int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
+{
+ AVFrame *frame;
+
+ *rframe = NULL;
+ if (!ff_inlink_check_available_frame(link))
+ return 0;
+ frame = ff_framequeue_take(&link->fifo);
+ consume_update(link, frame);
+ *rframe = frame;
+ return 1;
+}
+
+int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
+ AVFrame **rframe)
+{
+ AVFrame *frame;
+ int ret;
+
+ av_assert1(min);
+ *rframe = NULL;
+ if (!ff_inlink_check_available_samples(link, min))
+ return 0;
+ if (link->status_in)
+ min = FFMIN(min, ff_framequeue_queued_samples(&link->fifo));
+ ret = take_samples(link, min, link->max_samples, &frame);
+ if (ret < 0)
+ return ret;
+ consume_update(link, frame);
+ *rframe = frame;
+ return 1;
+}
+
+int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
+{
+ AVFrame *frame = *rframe;
+ AVFrame *out;
+ int ret;
+
+ if (av_frame_is_writable(frame))
+ return 0;
+ av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
+
+ switch (link->type) {
+ case AVMEDIA_TYPE_VIDEO:
+ out = ff_get_video_buffer(link, link->w, link->h);
+ break;
+ case AVMEDIA_TYPE_AUDIO:
+ out = ff_get_audio_buffer(link, frame->nb_samples);
+ break;
+ default:
+ return AVERROR(EINVAL);
+ }
+ if (!out)
+ return AVERROR(ENOMEM);
+
+ ret = av_frame_copy_props(out, frame);
+ if (ret < 0) {
+ av_frame_free(&out);
+ return ret;
+ }
+
+ switch (link->type) {
+ case AVMEDIA_TYPE_VIDEO:
+ av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
+ frame->format, frame->width, frame->height);
+ break;
+ case AVMEDIA_TYPE_AUDIO:
+ av_samples_copy(out->extended_data, frame->extended_data,
+ 0, 0, frame->nb_samples,
+ av_frame_get_channels(frame),
+ frame->format);
+ break;
+ default:
+ av_assert0(!"reached");
+ }
+
+ av_frame_free(&frame);
+ *rframe = out;
+ return 0;
+}
+
+int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
+{
+ AVFilterCommand *cmd = link->dst->command_queue;
+
+ while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
+ av_log(link->dst, AV_LOG_DEBUG,
+ "Processing command time:%f command:%s arg:%s\n",
+ cmd->time, cmd->command, cmd->arg);
+ avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
+ ff_command_queue_pop(link->dst);
+ cmd= link->dst->command_queue;
+ }
+ return 0;
+}
+
+int ff_inlink_evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
+{
+ AVFilterContext *dstctx = link->dst;
+ int64_t pts = frame->pts;
+ int64_t pos = av_frame_get_pkt_pos(frame);
+
+ if (!dstctx->enable_str)
+ return 1;
+
+ dstctx->var_values[VAR_N] = link->frame_count_out;
+ dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
+ dstctx->var_values[VAR_W] = link->w;
+ dstctx->var_values[VAR_H] = link->h;
+ dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
+
+ return fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) >= 0.5;
+}
+
+void ff_inlink_request_frame(AVFilterLink *link)
+{
+ av_assert1(!link->status_in);
+ av_assert1(!link->status_out);
+ link->frame_wanted_out = 1;
+ ff_filter_set_ready(link->src, 100);
+}
+