2011-01-29 12:22:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Stefano Sabatini
|
|
|
|
* Copyright (c) 2008 Victor Paesa
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* movie video source
|
|
|
|
*
|
|
|
|
* @todo use direct rendering (no allocation of a new frame)
|
|
|
|
* @todo support a PTS correction mechanism
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <float.h>
|
2013-11-24 07:21:19 +03:00
|
|
|
#include <stdint.h>
|
2013-05-05 12:42:18 +03:00
|
|
|
|
|
|
|
#include "libavutil/attributes.h"
|
2011-01-29 12:22:41 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2012-07-19 02:03:20 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2011-01-29 12:22:41 +02:00
|
|
|
#include "libavutil/opt.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2015-08-18 02:45:35 +02:00
|
|
|
#include "libavutil/internal.h"
|
2012-07-19 02:03:20 +03:00
|
|
|
#include "libavutil/timestamp.h"
|
2017-04-04 21:15:02 +02:00
|
|
|
|
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
|
2011-01-29 12:22:41 +02:00
|
|
|
#include "libavformat/avformat.h"
|
2017-04-04 21:15:02 +02:00
|
|
|
|
2012-05-12 18:58:14 +03:00
|
|
|
#include "audio.h"
|
2011-01-29 12:22:41 +02:00
|
|
|
#include "avfilter.h"
|
2012-05-16 03:27:31 +03:00
|
|
|
#include "formats.h"
|
2012-06-12 21:12:42 +03:00
|
|
|
#include "internal.h"
|
2012-05-30 12:20:32 +03:00
|
|
|
#include "video.h"
|
2011-01-29 12:22:41 +02:00
|
|
|
|
2014-04-19 19:20:17 +03:00
|
|
|
typedef struct MovieStream {
|
2012-07-19 02:03:20 +03:00
|
|
|
AVStream *st;
|
2016-04-11 15:42:30 +02:00
|
|
|
AVCodecContext *codec_ctx;
|
2016-05-25 17:55:29 +02:00
|
|
|
int64_t discontinuity_threshold;
|
|
|
|
int64_t last_pts;
|
2012-07-19 02:03:20 +03:00
|
|
|
} MovieStream;
|
2012-07-07 08:41:28 +03:00
|
|
|
|
2014-04-19 19:20:17 +03:00
|
|
|
typedef struct MovieContext {
|
2011-08-18 13:36:39 +03:00
|
|
|
/* common A/V fields */
|
2011-01-29 12:22:41 +02:00
|
|
|
const AVClass *class;
|
|
|
|
int64_t seek_point; ///< seekpoint in microseconds
|
|
|
|
double seek_point_d;
|
|
|
|
char *format_name;
|
|
|
|
char *file_name;
|
2012-07-19 02:03:20 +03:00
|
|
|
char *stream_specs; /**< user-provided list of streams, separated by + */
|
|
|
|
int stream_index; /**< for compatibility */
|
2012-03-15 14:45:26 +03:00
|
|
|
int loop_count;
|
2016-05-25 17:55:29 +02:00
|
|
|
int64_t discontinuity_threshold;
|
|
|
|
int64_t ts_offset;
|
2011-01-29 12:22:41 +02:00
|
|
|
|
|
|
|
AVFormatContext *format_ctx;
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
int max_stream_index; /**< max stream # actually used for output */
|
|
|
|
MovieStream *st; /**< array of all streams, one per output */
|
|
|
|
int *out_index; /**< stream number -> output number map, or -1 */
|
2011-01-29 12:22:41 +02:00
|
|
|
} MovieContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(MovieContext, x)
|
2013-04-12 16:45:28 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
|
2011-01-29 12:22:41 +02:00
|
|
|
|
|
|
|
static const AVOption movie_options[]= {
|
2013-04-11 04:38:34 +03:00
|
|
|
{ "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
|
|
|
|
{ "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
|
|
|
|
{ "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
|
|
|
|
{ "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
|
|
|
|
{ "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
|
|
|
|
{ "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
|
|
|
|
{ "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
|
2020-03-17 23:46:36 +02:00
|
|
|
{ "streams", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, FLAGS },
|
|
|
|
{ "s", "set streams", OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str = 0}, 0, 0, FLAGS },
|
2013-04-11 04:38:34 +03:00
|
|
|
{ "loop", "set loop count", OFFSET(loop_count), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, FLAGS },
|
2016-05-25 17:55:29 +02:00
|
|
|
{ "discontinuity", "set discontinuity threshold", OFFSET(discontinuity_threshold), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
|
2013-04-11 04:38:34 +03:00
|
|
|
{ NULL },
|
2011-01-29 12:22:41 +02:00
|
|
|
};
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
static int movie_config_output_props(AVFilterLink *outlink);
|
2020-09-08 14:57:53 +02:00
|
|
|
static int movie_request_frame(AVFilterLink *outlink);
|
2012-07-19 02:03:20 +03:00
|
|
|
|
|
|
|
static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
|
|
|
|
{
|
|
|
|
int i, ret, already = 0, stream_id = -1;
|
2013-05-05 01:56:39 +03:00
|
|
|
char type_char[2], dummy;
|
2012-07-19 02:03:20 +03:00
|
|
|
AVStream *found = NULL;
|
|
|
|
enum AVMediaType type;
|
|
|
|
|
2013-05-05 01:56:39 +03:00
|
|
|
ret = sscanf(spec, "d%1[av]%d%c", type_char, &stream_id, &dummy);
|
2012-07-19 02:03:20 +03:00
|
|
|
if (ret >= 1 && ret <= 2) {
|
2013-05-05 01:56:39 +03:00
|
|
|
type = type_char[0] == 'v' ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
|
2012-07-19 02:03:20 +03:00
|
|
|
ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
|
|
|
|
av_get_media_type_string(type), stream_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return avf->streams[ret];
|
|
|
|
}
|
|
|
|
for (i = 0; i < avf->nb_streams; i++) {
|
|
|
|
ret = avformat_match_stream_specifier(avf, avf->streams[i], spec);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(log, AV_LOG_ERROR,
|
|
|
|
"Invalid stream specifier \"%s\"\n", spec);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!ret)
|
|
|
|
continue;
|
|
|
|
if (avf->streams[i]->discard != AVDISCARD_ALL) {
|
|
|
|
already++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
av_log(log, AV_LOG_WARNING,
|
|
|
|
"Ambiguous stream specifier \"%s\", using #%d\n", spec, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
found = avf->streams[i];
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
av_log(log, AV_LOG_WARNING, "Stream specifier \"%s\" %s\n", spec,
|
|
|
|
already ? "matched only already used streams" :
|
|
|
|
"did not match any stream");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-11 15:42:30 +02:00
|
|
|
if (found->codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
|
|
|
|
found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
|
2012-07-19 02:03:20 +03:00
|
|
|
av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
|
|
|
|
"currently unsupported by libavfilter\n", spec,
|
2016-04-11 15:42:30 +02:00
|
|
|
av_get_media_type_string(found->codecpar->codec_type));
|
2012-07-19 02:03:20 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:42:02 +02:00
|
|
|
static int open_stream(AVFilterContext *ctx, MovieStream *st)
|
2012-07-19 02:03:20 +03:00
|
|
|
{
|
2020-09-10 22:11:57 +02:00
|
|
|
const AVCodec *codec;
|
2012-07-19 02:03:20 +03:00
|
|
|
int ret;
|
|
|
|
|
2016-04-11 15:42:30 +02:00
|
|
|
codec = avcodec_find_decoder(st->st->codecpar->codec_id);
|
2012-07-19 02:03:20 +03:00
|
|
|
if (!codec) {
|
2019-08-25 18:42:02 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
|
2012-07-19 02:03:20 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:42:30 +02:00
|
|
|
st->codec_ctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!st->codec_ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ret = avcodec_parameters_to_context(st->codec_ctx, st->st->codecpar);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-25 18:42:02 +02:00
|
|
|
st->codec_ctx->thread_count = ff_filter_get_nb_threads(ctx);
|
2013-03-12 01:28:56 +03:00
|
|
|
|
2016-04-11 15:42:30 +02:00
|
|
|
if ((ret = avcodec_open2(st->codec_ctx, codec, NULL)) < 0) {
|
2019-08-25 18:42:02 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
|
2012-07-19 02:03:20 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-30 20:46:36 +03:00
|
|
|
static int guess_channel_layout(MovieStream *st, int st_index, void *log_ctx)
|
|
|
|
{
|
2016-04-11 15:42:30 +02:00
|
|
|
AVCodecParameters *dec_par = st->st->codecpar;
|
2012-07-30 20:46:36 +03:00
|
|
|
char buf[256];
|
2016-04-11 15:42:30 +02:00
|
|
|
int64_t chl = av_get_default_channel_layout(dec_par->channels);
|
2012-07-30 20:46:36 +03:00
|
|
|
|
|
|
|
if (!chl) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
|
|
|
"Channel layout is not set in stream %d, and could not "
|
|
|
|
"be guessed from the number of channels (%d)\n",
|
2016-04-11 15:42:30 +02:00
|
|
|
st_index, dec_par->channels);
|
2012-07-30 20:46:36 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:42:30 +02:00
|
|
|
av_get_channel_layout_string(buf, sizeof(buf), dec_par->channels, chl);
|
2012-07-30 20:46:36 +03:00
|
|
|
av_log(log_ctx, AV_LOG_WARNING,
|
|
|
|
"Channel layout is not set in output stream %d, "
|
|
|
|
"guessed channel layout is '%s'\n",
|
|
|
|
st_index, buf);
|
2016-04-11 15:42:30 +02:00
|
|
|
dec_par->channel_layout = chl;
|
2012-07-30 20:46:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-11 04:38:34 +03:00
|
|
|
static av_cold int movie_common_init(AVFilterContext *ctx)
|
2011-01-29 12:22:41 +02:00
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
AVInputFormat *iformat = NULL;
|
|
|
|
int64_t timestamp;
|
2014-05-11 07:08:09 +03:00
|
|
|
int nb_streams = 1, ret, i;
|
2012-07-19 02:03:20 +03:00
|
|
|
char default_streams[16], *stream_specs, *spec, *cursor;
|
|
|
|
AVStream *st;
|
2011-08-18 13:36:39 +03:00
|
|
|
|
2013-06-16 21:31:08 +03:00
|
|
|
if (!movie->file_name) {
|
2011-08-18 13:36:39 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
|
2011-01-29 12:22:41 +02:00
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
stream_specs = movie->stream_specs;
|
|
|
|
if (!stream_specs) {
|
|
|
|
snprintf(default_streams, sizeof(default_streams), "d%c%d",
|
|
|
|
!strcmp(ctx->filter->name, "amovie") ? 'a' : 'v',
|
|
|
|
movie->stream_index);
|
|
|
|
stream_specs = default_streams;
|
|
|
|
}
|
2014-05-11 07:08:09 +03:00
|
|
|
for (cursor = stream_specs; *cursor; cursor++)
|
2012-07-19 02:03:20 +03:00
|
|
|
if (*cursor == '+')
|
|
|
|
nb_streams++;
|
|
|
|
|
|
|
|
if (movie->loop_count != 1 && nb_streams != 1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Loop with several streams is currently unsupported\n");
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2011-01-29 12:22:41 +02:00
|
|
|
// Try to find the movie format (container)
|
|
|
|
iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
|
|
|
|
|
|
|
|
movie->format_ctx = NULL;
|
2011-06-04 18:36:30 +03:00
|
|
|
if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
|
2011-01-29 12:22:41 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2011-06-04 18:36:30 +03:00
|
|
|
"Failed to avformat_open_input '%s'\n", movie->file_name);
|
2011-01-29 12:22:41 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-05-22 20:24:59 +03:00
|
|
|
if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
|
2011-01-29 12:22:41 +02:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
|
|
|
|
|
|
|
|
// if seeking requested, we execute it
|
|
|
|
if (movie->seek_point > 0) {
|
|
|
|
timestamp = movie->seek_point;
|
|
|
|
// add the stream start time, should it exist
|
|
|
|
if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
|
2016-04-02 20:11:44 +02:00
|
|
|
if (timestamp > 0 && movie->format_ctx->start_time > INT64_MAX - timestamp) {
|
2011-01-29 12:22:41 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
|
|
|
|
movie->file_name, movie->format_ctx->start_time, movie->seek_point);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
timestamp += movie->format_ctx->start_time;
|
|
|
|
}
|
|
|
|
if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
|
|
|
|
movie->file_name, timestamp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
for (i = 0; i < movie->format_ctx->nb_streams; i++)
|
|
|
|
movie->format_ctx->streams[i]->discard = AVDISCARD_ALL;
|
|
|
|
|
|
|
|
movie->st = av_calloc(nb_streams, sizeof(*movie->st));
|
|
|
|
if (!movie->st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < nb_streams; i++) {
|
|
|
|
spec = av_strtok(stream_specs, "+", &cursor);
|
|
|
|
if (!spec)
|
|
|
|
return AVERROR_BUG;
|
|
|
|
stream_specs = NULL; /* for next strtok */
|
|
|
|
st = find_stream(ctx, movie->format_ctx, spec);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
st->discard = AVDISCARD_DEFAULT;
|
|
|
|
movie->st[i].st = st;
|
|
|
|
movie->max_stream_index = FFMAX(movie->max_stream_index, st->index);
|
2016-05-25 17:55:29 +02:00
|
|
|
movie->st[i].discontinuity_threshold =
|
|
|
|
av_rescale_q(movie->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base);
|
2011-01-29 12:22:41 +02:00
|
|
|
}
|
2012-07-19 02:03:20 +03:00
|
|
|
if (av_strtok(NULL, "+", &cursor))
|
|
|
|
return AVERROR_BUG;
|
|
|
|
|
|
|
|
movie->out_index = av_calloc(movie->max_stream_index + 1,
|
|
|
|
sizeof(*movie->out_index));
|
|
|
|
if (!movie->out_index)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i <= movie->max_stream_index; i++)
|
|
|
|
movie->out_index[i] = -1;
|
|
|
|
for (i = 0; i < nb_streams; i++) {
|
|
|
|
AVFilterPad pad = { 0 };
|
2014-05-11 07:08:09 +03:00
|
|
|
movie->out_index[movie->st[i].st->index] = i;
|
2016-04-11 15:42:30 +02:00
|
|
|
pad.type = movie->st[i].st->codecpar->codec_type;
|
2020-08-22 04:08:15 +02:00
|
|
|
pad.name = av_asprintf("out%d", i);
|
2015-01-06 11:42:59 +02:00
|
|
|
if (!pad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-07-19 02:03:20 +03:00
|
|
|
pad.config_props = movie_config_output_props;
|
2020-09-08 14:57:53 +02:00
|
|
|
pad.request_frame = movie_request_frame;
|
2017-08-25 10:29:50 +02:00
|
|
|
if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
|
|
|
|
av_freep(&pad.name);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-04-11 15:42:30 +02:00
|
|
|
if ( movie->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
!movie->st[i].st->codecpar->channel_layout) {
|
2012-07-30 20:46:36 +03:00
|
|
|
ret = guess_channel_layout(&movie->st[i], i, ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-02 09:50:46 +02:00
|
|
|
ret = open_stream(ctx, &movie->st[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-01-29 12:22:41 +02:00
|
|
|
}
|
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
|
2011-01-29 12:22:41 +02:00
|
|
|
movie->seek_point, movie->format_name, movie->file_name,
|
|
|
|
movie->stream_index);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
static av_cold void movie_uninit(AVFilterContext *ctx)
|
2011-01-29 12:22:41 +02:00
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
2012-07-19 02:03:20 +03:00
|
|
|
int i;
|
2011-01-29 12:22:41 +02:00
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
for (i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
av_freep(&ctx->output_pads[i].name);
|
|
|
|
if (movie->st[i].st)
|
2016-04-11 15:42:30 +02:00
|
|
|
avcodec_free_context(&movie->st[i].codec_ctx);
|
2012-07-19 02:03:20 +03:00
|
|
|
}
|
|
|
|
av_freep(&movie->st);
|
|
|
|
av_freep(&movie->out_index);
|
2011-01-29 12:22:41 +02:00
|
|
|
if (movie->format_ctx)
|
2011-12-11 12:38:28 +03:00
|
|
|
avformat_close_input(&movie->format_ctx);
|
2011-01-29 12:22:41 +02:00
|
|
|
}
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
static int movie_query_formats(AVFilterContext *ctx)
|
2011-08-18 13:36:39 +03:00
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
2012-07-19 02:03:20 +03:00
|
|
|
int list[] = { 0, -1 };
|
|
|
|
int64_t list64[] = { 0, -1 };
|
2015-10-05 05:39:25 +02:00
|
|
|
int i, ret;
|
2012-07-19 02:03:20 +03:00
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
MovieStream *st = &movie->st[i];
|
2016-04-11 15:42:30 +02:00
|
|
|
AVCodecParameters *c = st->st->codecpar;
|
2012-07-19 02:03:20 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[i];
|
|
|
|
|
|
|
|
switch (c->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2016-04-11 15:42:30 +02:00
|
|
|
list[0] = c->format;
|
2020-08-24 12:16:34 +02:00
|
|
|
if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.formats)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-07-19 02:03:20 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2016-04-11 15:42:30 +02:00
|
|
|
list[0] = c->format;
|
2020-08-24 12:16:34 +02:00
|
|
|
if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.formats)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-07-19 02:03:20 +03:00
|
|
|
list[0] = c->sample_rate;
|
2020-08-24 12:16:34 +02:00
|
|
|
if ((ret = ff_formats_ref(ff_make_format_list(list), &outlink->incfg.samplerates)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-07-30 20:46:36 +03:00
|
|
|
list64[0] = c->channel_layout;
|
2020-08-08 00:37:46 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(ff_make_format64_list(list64),
|
2020-08-24 12:16:34 +02:00
|
|
|
&outlink->incfg.channel_layouts)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-07-19 02:03:20 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 13:36:39 +03:00
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2011-08-18 13:36:39 +03:00
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
static int movie_config_output_props(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
unsigned out_id = FF_OUTLINK_IDX(outlink);
|
|
|
|
MovieStream *st = &movie->st[out_id];
|
2016-04-11 15:42:30 +02:00
|
|
|
AVCodecParameters *c = st->st->codecpar;
|
2012-07-19 02:03:20 +03:00
|
|
|
|
|
|
|
outlink->time_base = st->st->time_base;
|
|
|
|
|
|
|
|
switch (c->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
outlink->w = c->width;
|
|
|
|
outlink->h = c->height;
|
|
|
|
outlink->frame_rate = st->st->r_frame_rate;
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
break;
|
|
|
|
}
|
2011-08-18 13:36:39 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static char *describe_frame_to_str(char *dst, size_t dst_size,
|
2014-02-02 17:52:11 +03:00
|
|
|
AVFrame *frame, enum AVMediaType frame_type,
|
|
|
|
AVFilterLink *link)
|
2012-07-19 02:03:20 +03:00
|
|
|
{
|
2014-02-02 17:52:11 +03:00
|
|
|
switch (frame_type) {
|
2012-07-19 02:03:20 +03:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
snprintf(dst, dst_size,
|
2013-03-10 03:30:30 +03:00
|
|
|
"video pts:%s time:%s size:%dx%d aspect:%d/%d",
|
|
|
|
av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
|
|
|
|
frame->width, frame->height,
|
|
|
|
frame->sample_aspect_ratio.num,
|
|
|
|
frame->sample_aspect_ratio.den);
|
2012-07-19 02:03:20 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
snprintf(dst, dst_size,
|
2013-03-10 03:30:30 +03:00
|
|
|
"audio pts:%s time:%s samples:%d",
|
|
|
|
av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
|
|
|
|
frame->nb_samples);
|
2012-07-19 02:03:20 +03:00
|
|
|
break;
|
|
|
|
default:
|
2014-02-02 17:52:11 +03:00
|
|
|
snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
|
2012-07-19 02:03:20 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return dst;
|
2011-01-29 12:22:41 +02:00
|
|
|
}
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
static int rewind_file(AVFilterContext *ctx)
|
2011-01-29 12:22:41 +02:00
|
|
|
{
|
2012-07-19 02:03:20 +03:00
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
int64_t timestamp = movie->seek_point;
|
|
|
|
int ret, i;
|
2011-01-29 12:22:41 +02:00
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
if (movie->format_ctx->start_time != AV_NOPTS_VALUE)
|
|
|
|
timestamp += movie->format_ctx->start_time;
|
|
|
|
ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
|
|
|
|
movie->loop_count = 1; /* do not try again */
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-29 12:22:41 +02:00
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
for (i = 0; i < ctx->nb_outputs; i++) {
|
2016-04-11 15:42:30 +02:00
|
|
|
avcodec_flush_buffers(movie->st[i].codec_ctx);
|
2012-07-19 02:03:20 +03:00
|
|
|
}
|
2011-01-29 12:22:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-02 13:26:18 +02:00
|
|
|
static int movie_decode_packet(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
AVPacket pkt = { 0 };
|
|
|
|
int pkt_out_id, ret;
|
|
|
|
|
|
|
|
/* read a new packet from input stream */
|
|
|
|
ret = av_read_frame(movie->format_ctx, &pkt);
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
/* EOF -> set all decoders for flushing */
|
|
|
|
for (int i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
ret = avcodec_send_packet(movie->st[i].codec_ctx, NULL);
|
|
|
|
if (ret < 0 && ret != AVERROR_EOF)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* send the packet to its decoder, if any */
|
|
|
|
pkt_out_id = pkt.stream_index > movie->max_stream_index ? -1 :
|
|
|
|
movie->out_index[pkt.stream_index];
|
|
|
|
if (pkt_out_id >= 0)
|
|
|
|
ret = avcodec_send_packet(movie->st[pkt_out_id].codec_ctx, &pkt);
|
|
|
|
av_packet_unref(&pkt);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
/**
|
|
|
|
* Try to push a frame to the requested output.
|
|
|
|
*
|
2012-08-16 19:17:29 +03:00
|
|
|
* @param ctx filter context
|
|
|
|
* @param out_id number of output where a frame is wanted;
|
2020-11-02 13:26:18 +02:00
|
|
|
* @return 0 if a frame was pushed on the requested output,
|
|
|
|
* AVERROR(EAGAIN) if the decoder requires more input
|
|
|
|
* AVERROR(EOF) if the decoder has been completely flushed
|
|
|
|
* <0 AVERROR code
|
2012-07-19 02:03:20 +03:00
|
|
|
*/
|
|
|
|
static int movie_push_frame(AVFilterContext *ctx, unsigned out_id)
|
2011-01-29 12:22:41 +02:00
|
|
|
{
|
2020-11-02 13:26:18 +02:00
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
MovieStream *st = &movie->st[out_id];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[out_id];
|
2014-05-11 07:08:08 +03:00
|
|
|
AVFrame *frame;
|
2020-11-02 13:26:18 +02:00
|
|
|
int ret;
|
2012-07-19 02:03:20 +03:00
|
|
|
|
2014-05-11 07:08:08 +03:00
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame)
|
2013-03-10 03:30:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2020-11-02 13:26:18 +02:00
|
|
|
ret = avcodec_receive_frame(st->codec_ctx, frame);
|
2012-07-19 02:03:20 +03:00
|
|
|
if (ret < 0) {
|
2020-11-02 13:26:18 +02:00
|
|
|
if (ret != AVERROR_EOF && ret != AVERROR(EAGAIN))
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
|
|
|
|
|
2014-05-11 07:08:08 +03:00
|
|
|
av_frame_free(&frame);
|
2020-11-02 13:26:18 +02:00
|
|
|
return ret;
|
2012-07-19 02:03:20 +03:00
|
|
|
}
|
|
|
|
|
2017-04-22 10:57:18 +02:00
|
|
|
frame->pts = frame->best_effort_timestamp;
|
2016-05-25 17:55:29 +02:00
|
|
|
if (frame->pts != AV_NOPTS_VALUE) {
|
|
|
|
if (movie->ts_offset)
|
|
|
|
frame->pts += av_rescale_q_rnd(movie->ts_offset, AV_TIME_BASE_Q, outlink->time_base, AV_ROUND_UP);
|
|
|
|
if (st->discontinuity_threshold) {
|
|
|
|
if (st->last_pts != AV_NOPTS_VALUE) {
|
|
|
|
int64_t diff = frame->pts - st->last_pts;
|
|
|
|
if (diff < 0 || diff > st->discontinuity_threshold) {
|
2020-11-02 13:26:18 +02:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "Discontinuity in stream:%d diff:%"PRId64"\n", out_id, diff);
|
2016-05-25 17:55:29 +02:00
|
|
|
movie->ts_offset += av_rescale_q_rnd(-diff, outlink->time_base, AV_TIME_BASE_Q, AV_ROUND_UP);
|
|
|
|
frame->pts -= diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
st->last_pts = frame->pts;
|
|
|
|
}
|
2015-08-18 02:45:35 +02:00
|
|
|
ff_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
|
2020-11-02 13:26:18 +02:00
|
|
|
describe_frame_to_str((char[1024]){0}, 1024, frame,
|
|
|
|
st->st->codecpar->codec_type, outlink));
|
2013-03-10 03:30:30 +03:00
|
|
|
|
2016-04-11 15:42:30 +02:00
|
|
|
if (st->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2014-05-11 07:08:08 +03:00
|
|
|
if (frame->format != outlink->format) {
|
2014-03-18 05:57:41 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Format changed %s -> %s, discarding frame\n",
|
|
|
|
av_get_pix_fmt_name(outlink->format),
|
2014-05-11 07:08:08 +03:00
|
|
|
av_get_pix_fmt_name(frame->format)
|
2014-03-18 05:57:41 +03:00
|
|
|
);
|
2014-05-11 07:08:08 +03:00
|
|
|
av_frame_free(&frame);
|
2014-03-18 05:57:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-05-11 07:08:08 +03:00
|
|
|
ret = ff_filter_frame(outlink, frame);
|
2012-07-19 02:03:20 +03:00
|
|
|
|
2013-03-13 11:56:50 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-11-02 13:26:18 +02:00
|
|
|
return 0;
|
2011-01-29 12:22:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:57:53 +02:00
|
|
|
static int movie_request_frame(AVFilterLink *outlink)
|
2011-01-29 12:22:41 +02:00
|
|
|
{
|
2020-09-08 14:57:53 +02:00
|
|
|
AVFilterContext *ctx = outlink->src;
|
2020-11-02 13:26:18 +02:00
|
|
|
MovieContext *movie = ctx->priv;
|
2020-09-08 14:57:53 +02:00
|
|
|
unsigned out_id = FF_OUTLINK_IDX(outlink);
|
2020-08-31 19:10:43 +02:00
|
|
|
|
2020-09-08 14:57:53 +02:00
|
|
|
while (1) {
|
2020-11-02 13:26:18 +02:00
|
|
|
int got_eagain = 0, got_eof = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* check all decoders for available output */
|
|
|
|
for (int i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
ret = movie_push_frame(ctx, i);
|
|
|
|
if (ret == AVERROR(EAGAIN))
|
|
|
|
got_eagain++;
|
|
|
|
else if (ret == AVERROR_EOF)
|
|
|
|
got_eof++;
|
|
|
|
else if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (i == out_id)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got_eagain) {
|
|
|
|
/* all decoders require more input -> read a new packet */
|
|
|
|
ret = movie_decode_packet(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else if (got_eof) {
|
|
|
|
/* all decoders flushed */
|
|
|
|
if (movie->loop_count != 1) {
|
|
|
|
ret = rewind_file(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
movie->loop_count -= movie->loop_count > 1;
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "Stream finished, looping.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
2012-07-15 12:16:53 +03:00
|
|
|
}
|
2011-01-29 12:22:41 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 18:20:08 +02:00
|
|
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
|
|
|
char *res, int res_len, int flags)
|
|
|
|
{
|
|
|
|
MovieContext *movie = ctx->priv;
|
|
|
|
int ret = AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
if (!strcmp(cmd, "seek")) {
|
|
|
|
int idx, flags, i;
|
|
|
|
int64_t ts;
|
|
|
|
char tail[2];
|
|
|
|
|
|
|
|
if (sscanf(args, "%i|%"SCNi64"|%i %1s", &idx, &ts, &flags, tail) != 3)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
ret = av_seek_frame(movie->format_ctx, idx, ts, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_outputs; i++) {
|
|
|
|
avcodec_flush_buffers(movie->st[i].codec_ctx);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} else if (!strcmp(cmd, "get_duration")) {
|
|
|
|
int print_len;
|
|
|
|
char tail[2];
|
|
|
|
|
|
|
|
if (!res || res_len <= 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
if (args && sscanf(args, "%1s", tail) == 1)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
print_len = snprintf(res, res_len, "%"PRId64, movie->format_ctx->duration);
|
|
|
|
if (print_len < 0 || print_len >= res_len)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-19 02:03:20 +03:00
|
|
|
#if CONFIG_MOVIE_FILTER
|
|
|
|
|
2012-08-13 14:30:50 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(movie);
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_avsrc_movie = {
|
2011-01-29 12:22:41 +02:00
|
|
|
.name = "movie",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
|
|
|
|
.priv_size = sizeof(MovieContext),
|
2013-04-11 04:38:34 +03:00
|
|
|
.priv_class = &movie_class,
|
2013-06-16 22:44:02 +03:00
|
|
|
.init = movie_common_init,
|
2012-07-19 02:03:20 +03:00
|
|
|
.uninit = movie_uninit,
|
2011-08-18 13:53:18 +03:00
|
|
|
.query_formats = movie_query_formats,
|
2011-01-29 12:22:41 +02:00
|
|
|
|
2012-09-18 15:30:31 +03:00
|
|
|
.inputs = NULL,
|
2012-11-28 22:01:59 +03:00
|
|
|
.outputs = NULL,
|
2013-04-13 20:43:17 +03:00
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
|
2016-02-17 18:20:08 +02:00
|
|
|
.process_command = process_command
|
2011-01-29 12:22:41 +02:00
|
|
|
};
|
2011-08-18 15:26:15 +03:00
|
|
|
|
|
|
|
#endif /* CONFIG_MOVIE_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_AMOVIE_FILTER
|
|
|
|
|
2012-08-13 14:30:50 +03:00
|
|
|
#define amovie_options movie_options
|
|
|
|
AVFILTER_DEFINE_CLASS(amovie);
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_avsrc_amovie = {
|
2011-08-18 15:26:15 +03:00
|
|
|
.name = "amovie",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
|
|
|
|
.priv_size = sizeof(MovieContext),
|
2013-06-16 22:44:02 +03:00
|
|
|
.init = movie_common_init,
|
2012-07-19 02:03:20 +03:00
|
|
|
.uninit = movie_uninit,
|
|
|
|
.query_formats = movie_query_formats,
|
2011-08-18 15:26:15 +03:00
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = NULL,
|
|
|
|
.outputs = NULL,
|
2012-08-13 14:40:01 +03:00
|
|
|
.priv_class = &amovie_class,
|
2013-04-13 20:43:17 +03:00
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
|
2016-02-17 18:20:08 +02:00
|
|
|
.process_command = process_command,
|
2011-08-18 15:26:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_AMOVIE_FILTER */
|