1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-02 03:06:28 +02:00
FFmpeg/libavfilter/avf_concat.c

461 lines
16 KiB
C
Raw Normal View History

2012-07-17 02:05:05 +03:00
/*
* Copyright (c) 2012 Nicolas George
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* concat audio-video filter
*/
#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
2012-07-17 02:05:05 +03:00
#include "libavutil/opt.h"
#include "avfilter.h"
#include "filters.h"
2012-07-17 02:05:05 +03:00
#include "internal.h"
#include "video.h"
#include "audio.h"
#define TYPE_ALL 2
typedef struct ConcatContext {
2012-07-17 02:05:05 +03:00
const AVClass *class;
unsigned nb_streams[TYPE_ALL]; /**< number of out streams of each type */
unsigned nb_segments;
unsigned cur_idx; /**< index of the first input of current segment */
int64_t delta_ts; /**< timestamp to add to produce output timestamps */
unsigned nb_in_active; /**< number of active inputs in current segment */
2012-10-25 22:27:41 +03:00
unsigned unsafe;
2012-07-17 02:05:05 +03:00
struct concat_in {
int64_t pts;
int64_t nb_frames;
unsigned eof;
} *in;
} ConcatContext;
#define OFFSET(x) offsetof(ConcatContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM
#define F AV_OPT_FLAG_FILTERING_PARAM
#define V AV_OPT_FLAG_VIDEO_PARAM
2012-07-17 02:05:05 +03:00
static const AVOption concat_options[] = {
{ "n", "specify the number of segments", OFFSET(nb_segments),
AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, V|A|F},
2012-07-17 02:05:05 +03:00
{ "v", "specify the number of video streams",
OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F },
2012-07-17 02:05:05 +03:00
{ "a", "specify the number of audio streams",
OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F},
2012-10-25 22:27:41 +03:00
{ "unsafe", "enable unsafe mode",
OFFSET(unsafe),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, V|A|F},
{ NULL }
2012-07-17 02:05:05 +03:00
};
AVFILTER_DEFINE_CLASS(concat);
static int query_formats(AVFilterContext *ctx)
{
ConcatContext *cat = ctx->priv;
unsigned type, nb_str, idx0 = 0, idx, str, seg;
AVFilterFormats *formats, *rates = NULL;
AVFilterChannelLayouts *layouts = NULL;
int ret;
2012-07-17 02:05:05 +03:00
for (type = 0; type < TYPE_ALL; type++) {
nb_str = cat->nb_streams[type];
for (str = 0; str < nb_str; str++) {
idx = idx0;
/* Set the output formats */
formats = ff_all_formats(type);
if ((ret = ff_formats_ref(formats, &ctx->outputs[idx]->incfg.formats)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
if (type == AVMEDIA_TYPE_AUDIO) {
rates = ff_all_samplerates();
if ((ret = ff_formats_ref(rates, &ctx->outputs[idx]->incfg.samplerates)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
layouts = ff_all_channel_layouts();
if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->incfg.channel_layouts)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
}
/* Set the same formats for each corresponding input */
for (seg = 0; seg < cat->nb_segments; seg++) {
if ((ret = ff_formats_ref(formats, &ctx->inputs[idx]->outcfg.formats)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
if (type == AVMEDIA_TYPE_AUDIO) {
if ((ret = ff_formats_ref(rates, &ctx->inputs[idx]->outcfg.samplerates)) < 0 ||
(ret = ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->outcfg.channel_layouts)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
}
idx += ctx->nb_outputs;
}
idx0++;
}
}
return 0;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
ConcatContext *cat = ctx->priv;
unsigned out_no = FF_OUTLINK_IDX(outlink);
unsigned in_no = out_no, seg;
AVFilterLink *inlink = ctx->inputs[in_no];
/* enhancement: find a common one */
outlink->time_base = AV_TIME_BASE_Q;
outlink->w = inlink->w;
outlink->h = inlink->h;
outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
outlink->format = inlink->format;
outlink->frame_rate = inlink->frame_rate;
for (seg = 1; seg < cat->nb_segments; seg++) {
inlink = ctx->inputs[in_no + seg * ctx->nb_outputs];
if (outlink->frame_rate.num != inlink->frame_rate.num ||
outlink->frame_rate.den != inlink->frame_rate.den) {
av_log(ctx, AV_LOG_VERBOSE,
"Video inputs have different frame rates, output will be VFR\n");
outlink->frame_rate = av_make_q(1, 0);
break;
}
}
2012-07-17 02:05:05 +03:00
for (seg = 1; seg < cat->nb_segments; seg++) {
inlink = ctx->inputs[in_no + seg * ctx->nb_outputs];
if (!outlink->sample_aspect_ratio.num)
outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
2012-07-17 02:05:05 +03:00
/* possible enhancement: unsafe mode, do not check */
if (outlink->w != inlink->w ||
outlink->h != inlink->h ||
outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num &&
inlink->sample_aspect_ratio.num ||
2012-07-17 02:05:05 +03:00
outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
"(size %dx%d, SAR %d:%d) do not match the corresponding "
"output link %s parameters (%dx%d, SAR %d:%d)\n",
ctx->input_pads[in_no].name, inlink->w, inlink->h,
inlink->sample_aspect_ratio.num,
inlink->sample_aspect_ratio.den,
ctx->input_pads[out_no].name, outlink->w, outlink->h,
outlink->sample_aspect_ratio.num,
outlink->sample_aspect_ratio.den);
2012-10-25 22:27:41 +03:00
if (!cat->unsafe)
return AVERROR(EINVAL);
2012-07-17 02:05:05 +03:00
}
}
return 0;
}
static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
2012-07-17 02:05:05 +03:00
{
ConcatContext *cat = ctx->priv;
unsigned out_no = in_no % ctx->nb_outputs;
AVFilterLink * inlink = ctx-> inputs[ in_no];
AVFilterLink *outlink = ctx->outputs[out_no];
struct concat_in *in = &cat->in[in_no];
buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
in->pts = buf->pts;
in->nb_frames++;
/* add duration to input PTS */
if (inlink->sample_rate)
/* use number of audio samples */
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
in->pts += av_rescale_q(buf->nb_samples,
2013-12-31 17:55:44 +03:00
av_make_q(1, inlink->sample_rate),
2012-07-17 02:05:05 +03:00
outlink->time_base);
else if (in->nb_frames >= 2)
/* use mean duration */
in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
buf->pts += cat->delta_ts;
return ff_filter_frame(outlink, buf);
2012-07-17 02:05:05 +03:00
}
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
2012-07-17 02:05:05 +03:00
{
AVFilterContext *ctx = inlink->dst;
unsigned in_no = FF_INLINK_IDX(inlink);
AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
return ff_get_video_buffer(outlink, w, h);
2012-07-17 02:05:05 +03:00
}
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
2012-07-17 02:05:05 +03:00
{
AVFilterContext *ctx = inlink->dst;
unsigned in_no = FF_INLINK_IDX(inlink);
AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
return ff_get_audio_buffer(outlink, nb_samples);
2012-07-17 02:05:05 +03:00
}
static void close_input(AVFilterContext *ctx, unsigned in_no)
{
ConcatContext *cat = ctx->priv;
cat->in[in_no].eof = 1;
cat->nb_in_active--;
av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
ctx->input_pads[in_no].name, cat->nb_in_active);
}
static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
2012-07-17 02:05:05 +03:00
{
ConcatContext *cat = ctx->priv;
unsigned i = cat->cur_idx;
unsigned imax = i + ctx->nb_outputs;
int64_t pts;
pts = cat->in[i++].pts;
for (; i < imax; i++)
pts = FFMAX(pts, cat->in[i].pts);
cat->delta_ts += pts;
*seg_delta = pts;
2012-07-17 02:05:05 +03:00
}
static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
int64_t seg_delta)
2012-07-17 02:05:05 +03:00
{
ConcatContext *cat = ctx->priv;
AVFilterLink *outlink = ctx->outputs[out_no];
int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
2012-07-17 02:05:05 +03:00
int64_t nb_samples, sent = 0;
int frame_nb_samples, ret;
2012-07-17 02:05:05 +03:00
AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
AVFrame *buf;
2012-07-17 02:05:05 +03:00
if (!rate_tb.den)
return AVERROR_BUG;
if (cat->in[in_no].pts < INT64_MIN + seg_delta)
return AVERROR_INVALIDDATA;
if (seg_delta < cat->in[in_no].pts)
return AVERROR_INVALIDDATA;
nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
2012-07-17 02:05:05 +03:00
outlink->time_base, rate_tb);
frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
while (nb_samples) {
frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 03:30:30 +03:00
buf = ff_get_audio_buffer(outlink, frame_nb_samples);
2012-07-17 02:05:05 +03:00
if (!buf)
return AVERROR(ENOMEM);
2012-07-17 02:05:05 +03:00
av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
outlink->ch_layout.nb_channels, outlink->format);
2012-07-17 02:05:05 +03:00
buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
ret = ff_filter_frame(outlink, buf);
if (ret < 0)
return ret;
2012-07-17 02:05:05 +03:00
sent += frame_nb_samples;
nb_samples -= frame_nb_samples;
}
return 0;
2012-07-17 02:05:05 +03:00
}
static int flush_segment(AVFilterContext *ctx)
2012-07-17 02:05:05 +03:00
{
int ret;
2012-07-17 02:05:05 +03:00
ConcatContext *cat = ctx->priv;
unsigned str, str_max;
int64_t seg_delta;
2012-07-17 02:05:05 +03:00
find_next_delta_ts(ctx, &seg_delta);
2012-07-17 02:05:05 +03:00
cat->cur_idx += ctx->nb_outputs;
cat->nb_in_active = ctx->nb_outputs;
av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
cat->delta_ts);
if (cat->cur_idx < ctx->nb_inputs) {
/* pad audio streams with silence */
str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
for (; str < str_max; str++) {
ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
seg_delta);
if (ret < 0)
return ret;
}
2012-07-17 02:05:05 +03:00
}
return 0;
2012-07-17 02:05:05 +03:00
}
static av_cold int init(AVFilterContext *ctx)
2012-07-17 02:05:05 +03:00
{
ConcatContext *cat = ctx->priv;
unsigned seg, type, str;
int ret;
2012-07-17 02:05:05 +03:00
/* create input pads */
for (seg = 0; seg < cat->nb_segments; seg++) {
for (type = 0; type < TYPE_ALL; type++) {
for (str = 0; str < cat->nb_streams[type]; str++) {
AVFilterPad pad = {
.type = type,
};
if (type == AVMEDIA_TYPE_VIDEO)
pad.get_buffer.video = get_video_buffer;
else
pad.get_buffer.audio = get_audio_buffer;
pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str);
if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
}
}
}
/* create output pads */
for (type = 0; type < TYPE_ALL; type++) {
for (str = 0; str < cat->nb_streams[type]; str++) {
AVFilterPad pad = {
.type = type,
.config_props = config_output,
};
pad.name = av_asprintf("out:%c%d", "va"[type], str);
if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
return ret;
2012-07-17 02:05:05 +03:00
}
}
cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
if (!cat->in)
return AVERROR(ENOMEM);
cat->nb_in_active = ctx->nb_outputs;
return 0;
}
static av_cold void uninit(AVFilterContext *ctx)
{
ConcatContext *cat = ctx->priv;
av_freep(&cat->in);
2012-07-17 02:05:05 +03:00
}
static int activate(AVFilterContext *ctx)
{
ConcatContext *cat = ctx->priv;
AVFrame *frame;
unsigned i, j;
int ret, status;
int64_t pts;
/* Forward status back */
for (i = 0; i < ctx->nb_outputs; i++) {
status = ff_outlink_get_status(ctx->outputs[i]);
if (!status)
continue;
for (j = i; j < ctx->nb_inputs; j += ctx->nb_outputs) {
if (!cat->in[j].eof) {
cat->in[j].eof = 1;
ff_inlink_set_status(ctx->inputs[j], status);
return 0;
}
}
}
/* Forward available frames */
if (cat->cur_idx < ctx->nb_inputs) {
for (i = 0; i < ctx->nb_outputs; i++) {
ret = ff_inlink_consume_frame(ctx->inputs[cat->cur_idx + i], &frame);
if (ret < 0)
return ret;
if (ret) {
ff_filter_set_ready(ctx, 10);
return push_frame(ctx, cat->cur_idx + i, frame);
}
}
}
/* Forward status change */
if (cat->cur_idx < ctx->nb_inputs) {
for (i = 0; i < ctx->nb_outputs; i++) {
2021-07-25 20:09:44 +02:00
AVFilterLink *inlink = ctx->inputs[cat->cur_idx + i];
ret = ff_inlink_acknowledge_status(inlink, &status, &pts);
/* TODO use pts */
if (ret > 0) {
close_input(ctx, cat->cur_idx + i);
if (cat->cur_idx + ctx->nb_outputs >= ctx->nb_inputs) {
2021-07-25 20:09:44 +02:00
int64_t eof_pts = cat->delta_ts;
eof_pts += av_rescale_q(pts, inlink->time_base, ctx->outputs[i]->time_base);
ff_outlink_set_status(ctx->outputs[i], status, eof_pts);
}
if (!cat->nb_in_active) {
ret = flush_segment(ctx);
if (ret < 0)
return ret;
}
ff_filter_set_ready(ctx, 10);
return 0;
}
}
}
ret = FFERROR_NOT_READY;
for (i = 0; i < ctx->nb_outputs; i++) {
if (ff_outlink_frame_wanted(ctx->outputs[i])) {
if (cat->in[cat->cur_idx + i].eof) {
for (j = 0; j < ctx->nb_outputs; j++)
if (!cat->in[cat->cur_idx + j].eof)
ff_inlink_request_frame(ctx->inputs[cat->cur_idx + j]);
return 0;
} else {
ff_inlink_request_frame(ctx->inputs[cat->cur_idx + i]);
ret = 0;
}
}
}
return ret;
}
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
char *res, int res_len, int flags)
{
int ret = AVERROR(ENOSYS);
if (!strcmp(cmd, "next")) {
av_log(ctx, AV_LOG_VERBOSE, "Command received: next\n");
return flush_segment(ctx);
}
return ret;
}
const AVFilter ff_avf_concat = {
2012-07-17 02:05:05 +03:00
.name = "concat",
.description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
.init = init,
.uninit = uninit,
.activate = activate,
2012-07-17 02:05:05 +03:00
.priv_size = sizeof(ConcatContext),
.inputs = NULL,
.outputs = NULL,
.priv_class = &concat_class,
2013-04-13 20:43:17 +03:00
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
avfilter: Replace query_formats callback with union of list and callback If one looks at the many query_formats callbacks in existence, one will immediately recognize that there is one type of default callback for video and a slightly different default callback for audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);" for video with a filter-specific pix_fmts list. For audio, it is the same with a filter-specific sample_fmts list together with ff_set_common_all_samplerates() and ff_set_common_all_channel_counts(). This commit allows to remove the boilerplate query_formats callbacks by replacing said callback with a union consisting the old callback and pointers for pixel and sample format arrays. For the not uncommon case in which these lists only contain a single entry (besides the sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also added to the union to store them directly in the AVFilter, thereby avoiding a relocation. The state of said union will be contained in a new, dedicated AVFilter field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t in order to create a hole for this new field; this is no problem, as the maximum of all the nb_inputs is four; for nb_outputs it is only two). The state's default value coincides with the earlier default of query_formats being unset, namely that the filter accepts all formats (and also sample rates and channel counts/layouts for audio) provided that these properties agree coincide for all inputs and outputs. By using different union members for audio and video filters the type-unsafety of using the same functions for audio and video lists will furthermore be more confined to formats.c than before. When the new fields are used, they will also avoid allocations: Currently something nearly equivalent to ff_default_query_formats() is called after every successful call to a query_formats callback; yet in the common case that the newly allocated AVFilterFormats are not used at all (namely if there are no free links) these newly allocated AVFilterFormats are freed again without ever being used. Filters no longer using the callback will not exhibit this any more. Reviewed-by: Paul B Mahol <onemda@gmail.com> Reviewed-by: Nicolas George <george@nsup.org> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-27 12:07:35 +02:00
FILTER_QUERY_FUNC(query_formats),
.process_command = process_command,
2012-07-17 02:05:05 +03:00
};