2022-08-03 14:08:44 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2022-10-17 10:49:42 +02:00
|
|
|
#include <float.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-08-03 14:08:44 +02:00
|
|
|
#include "ffmpeg.h"
|
|
|
|
|
2022-03-22 18:56:30 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2022-10-17 10:49:42 +02:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/display.h"
|
2022-08-03 14:08:44 +02:00
|
|
|
#include "libavutil/error.h"
|
2022-10-17 10:49:42 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
2022-08-03 14:08:44 +02:00
|
|
|
#include "libavutil/time.h"
|
2022-03-21 18:29:21 +02:00
|
|
|
#include "libavutil/timestamp.h"
|
2022-08-03 14:08:44 +02:00
|
|
|
#include "libavutil/thread.h"
|
|
|
|
#include "libavutil/threadmessage.h"
|
|
|
|
|
|
|
|
#include "libavcodec/packet.h"
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
|
2022-10-17 10:49:42 +02:00
|
|
|
static const char *const opt_name_discard[] = {"discard", NULL};
|
|
|
|
static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL};
|
|
|
|
static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL};
|
|
|
|
static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL};
|
|
|
|
static const char *const opt_name_guess_layout_max[] = {"guess_layout_max", NULL};
|
|
|
|
static const char *const opt_name_ts_scale[] = {"itsscale", NULL};
|
|
|
|
static const char *const opt_name_hwaccels[] = {"hwaccel", NULL};
|
|
|
|
static const char *const opt_name_hwaccel_devices[] = {"hwaccel_device", NULL};
|
|
|
|
static const char *const opt_name_hwaccel_output_formats[] = {"hwaccel_output_format", NULL};
|
|
|
|
static const char *const opt_name_autorotate[] = {"autorotate", NULL};
|
|
|
|
static const char *const opt_name_display_rotations[] = {"display_rotation", NULL};
|
|
|
|
static const char *const opt_name_display_hflips[] = {"display_hflip", NULL};
|
|
|
|
static const char *const opt_name_display_vflips[] = {"display_vflip", NULL};
|
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
typedef struct DemuxStream {
|
|
|
|
InputStream ist;
|
|
|
|
|
2023-02-11 08:59:36 +02:00
|
|
|
// name used for logging
|
|
|
|
char log_name[32];
|
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
int64_t min_pts; /* pts with the smallest value in a current stream */
|
|
|
|
int64_t max_pts; /* pts with the higher value in a current stream */
|
|
|
|
} DemuxStream;
|
|
|
|
|
2022-10-17 11:17:34 +02:00
|
|
|
typedef struct Demuxer {
|
|
|
|
InputFile f;
|
|
|
|
|
2023-01-29 13:35:47 +02:00
|
|
|
// name used for logging
|
|
|
|
char log_name[32];
|
|
|
|
|
2022-10-17 11:17:34 +02:00
|
|
|
/* number of times input stream should be looped */
|
|
|
|
int loop;
|
2022-10-17 12:49:51 +02:00
|
|
|
/* actual duration of the longest stream in a file at the moment when
|
|
|
|
* looping happens */
|
|
|
|
int64_t duration;
|
|
|
|
/* time base of the duration */
|
|
|
|
AVRational time_base;
|
2022-10-17 12:49:51 +02:00
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
/* number of streams that the user was warned of */
|
|
|
|
int nb_streams_warn;
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
AVThreadMessageQueue *in_thread_queue;
|
|
|
|
int thread_queue_size;
|
|
|
|
pthread_t thread;
|
|
|
|
int non_blocking;
|
2022-10-17 11:17:34 +02:00
|
|
|
} Demuxer;
|
|
|
|
|
2022-03-22 11:41:55 +02:00
|
|
|
typedef struct DemuxMsg {
|
|
|
|
AVPacket *pkt;
|
|
|
|
int looping;
|
2022-08-18 11:13:08 +02:00
|
|
|
|
|
|
|
// repeat_pict from the demuxer-internal parser
|
|
|
|
int repeat_pict;
|
2022-03-22 11:41:55 +02:00
|
|
|
} DemuxMsg;
|
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
static DemuxStream *ds_from_ist(InputStream *ist)
|
|
|
|
{
|
|
|
|
return (DemuxStream*)ist;
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:17:34 +02:00
|
|
|
static Demuxer *demuxer_from_ifile(InputFile *f)
|
|
|
|
{
|
|
|
|
return (Demuxer*)f;
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
static void report_new_stream(Demuxer *d, const AVPacket *pkt)
|
2022-03-21 18:29:21 +02:00
|
|
|
{
|
2022-10-17 12:49:51 +02:00
|
|
|
AVStream *st = d->f.ctx->streams[pkt->stream_index];
|
2022-03-21 18:29:21 +02:00
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
if (pkt->stream_index < d->nb_streams_warn)
|
2022-03-21 18:29:21 +02:00
|
|
|
return;
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING,
|
|
|
|
"New %s stream with index %d at pos:%"PRId64" and DTS:%ss\n",
|
2022-03-21 18:29:21 +02:00
|
|
|
av_get_media_type_string(st->codecpar->codec_type),
|
2023-01-29 13:35:47 +02:00
|
|
|
pkt->stream_index, pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
|
2022-10-17 12:49:51 +02:00
|
|
|
d->nb_streams_warn = pkt->stream_index + 1;
|
2022-03-21 18:29:21 +02:00
|
|
|
}
|
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
static void ifile_duration_update(Demuxer *d, DemuxStream *ds,
|
2022-03-22 11:41:55 +02:00
|
|
|
int64_t last_duration)
|
2022-03-22 10:58:06 +02:00
|
|
|
{
|
2022-03-22 11:41:55 +02:00
|
|
|
/* the total duration of the stream, max_pts - min_pts is
|
|
|
|
* the duration of the stream without the last frame */
|
2023-02-11 08:30:15 +02:00
|
|
|
if (ds->max_pts > ds->min_pts &&
|
|
|
|
ds->max_pts - (uint64_t)ds->min_pts < INT64_MAX - last_duration)
|
|
|
|
last_duration += ds->max_pts - ds->min_pts;
|
2022-03-22 11:41:55 +02:00
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
if (!d->duration ||
|
|
|
|
av_compare_ts(d->duration, d->time_base,
|
2023-02-11 08:30:15 +02:00
|
|
|
last_duration, ds->ist.st->time_base) < 0) {
|
2022-10-17 12:49:51 +02:00
|
|
|
d->duration = last_duration;
|
2023-02-11 08:30:15 +02:00
|
|
|
d->time_base = ds->ist.st->time_base;
|
2022-03-22 10:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:17:34 +02:00
|
|
|
static int seek_to_start(Demuxer *d)
|
2022-03-22 10:58:06 +02:00
|
|
|
{
|
2022-10-17 11:17:34 +02:00
|
|
|
InputFile *ifile = &d->f;
|
2022-03-22 11:41:55 +02:00
|
|
|
AVFormatContext *is = ifile->ctx;
|
|
|
|
int ret;
|
2022-03-22 10:58:06 +02:00
|
|
|
|
|
|
|
ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-03-22 11:41:55 +02:00
|
|
|
if (ifile->audio_duration_queue_size) {
|
2022-03-22 10:58:06 +02:00
|
|
|
/* duration is the length of the last frame in a stream
|
|
|
|
* when audio stream is present we don't care about
|
|
|
|
* last video frame length because it's not defined exactly */
|
2022-03-22 11:41:55 +02:00
|
|
|
int got_durations = 0;
|
2022-03-22 10:58:06 +02:00
|
|
|
|
2022-03-22 11:41:55 +02:00
|
|
|
while (got_durations < ifile->audio_duration_queue_size) {
|
2023-02-11 08:30:15 +02:00
|
|
|
DemuxStream *ds;
|
2022-03-22 11:41:55 +02:00
|
|
|
LastFrameDuration dur;
|
|
|
|
ret = av_thread_message_queue_recv(ifile->audio_duration_queue, &dur, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
got_durations++;
|
2022-03-22 10:58:06 +02:00
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
ds = ds_from_ist(ifile->streams[dur.stream_idx]);
|
|
|
|
ifile_duration_update(d, ds, dur.duration);
|
2022-03-22 11:41:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < ifile->nb_streams; i++) {
|
|
|
|
int64_t duration = 0;
|
2023-02-11 08:30:15 +02:00
|
|
|
InputStream *ist = ifile->streams[i];
|
|
|
|
DemuxStream *ds = ds_from_ist(ist);
|
2022-03-22 10:58:06 +02:00
|
|
|
|
|
|
|
if (ist->framerate.num) {
|
|
|
|
duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base);
|
|
|
|
} else if (ist->st->avg_frame_rate.num) {
|
|
|
|
duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base);
|
|
|
|
} else {
|
|
|
|
duration = 1;
|
|
|
|
}
|
2022-03-22 11:41:55 +02:00
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
ifile_duration_update(d, ds, duration);
|
2022-03-22 10:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:17:34 +02:00
|
|
|
if (d->loop > 0)
|
|
|
|
d->loop--;
|
2022-03-22 10:58:06 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
|
2022-08-10 17:15:06 +02:00
|
|
|
{
|
2022-10-17 12:49:51 +02:00
|
|
|
InputFile *ifile = &d->f;
|
2022-11-17 10:49:27 +02:00
|
|
|
InputStream *ist = ifile->streams[pkt->stream_index];
|
2023-02-11 08:30:15 +02:00
|
|
|
DemuxStream *ds = ds_from_ist(ist);
|
2022-10-21 15:59:25 +02:00
|
|
|
const int64_t start_time = ifile->start_time_effective;
|
2022-08-10 17:15:06 +02:00
|
|
|
int64_t duration;
|
|
|
|
|
|
|
|
if (debug_ts) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_INFO, "demuxer -> type:%s "
|
2022-08-10 17:15:06 +02:00
|
|
|
"pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n",
|
|
|
|
av_get_media_type_string(ist->st->codecpar->codec_type),
|
|
|
|
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
|
|
|
|
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
|
|
|
|
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ist->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
|
|
|
|
ist->st->pts_wrap_bits < 64) {
|
|
|
|
int64_t stime, stime2;
|
|
|
|
|
|
|
|
stime = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base);
|
|
|
|
stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
|
|
|
|
ist->wrap_correction_done = 1;
|
|
|
|
|
|
|
|
if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
|
|
|
|
pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
|
|
|
|
ist->wrap_correction_done = 0;
|
|
|
|
}
|
|
|
|
if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
|
|
|
|
pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
|
|
|
|
ist->wrap_correction_done = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
|
|
|
|
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts *= ist->ts_scale;
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts *= ist->ts_scale;
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
duration = av_rescale_q(d->duration, d->time_base, ist->st->time_base);
|
2022-08-10 17:15:06 +02:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE) {
|
|
|
|
pkt->pts += duration;
|
2023-02-11 08:30:15 +02:00
|
|
|
ds->max_pts = FFMAX(pkt->pts, ds->max_pts);
|
|
|
|
ds->min_pts = FFMIN(pkt->pts, ds->min_pts);
|
2022-08-10 17:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts += duration;
|
2022-08-18 11:13:08 +02:00
|
|
|
|
|
|
|
*repeat_pict = -1;
|
|
|
|
if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
av_stream_get_parser(ist->st))
|
|
|
|
*repeat_pict = av_stream_get_parser(ist->st)->repeat_pict;
|
2022-08-10 17:15:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:50:30 +02:00
|
|
|
static void thread_set_name(InputFile *f)
|
|
|
|
{
|
|
|
|
char name[16];
|
|
|
|
snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
|
|
|
|
ff_thread_setname(name);
|
|
|
|
}
|
|
|
|
|
2022-08-03 14:08:44 +02:00
|
|
|
static void *input_thread(void *arg)
|
|
|
|
{
|
2022-10-17 11:17:34 +02:00
|
|
|
Demuxer *d = arg;
|
|
|
|
InputFile *f = &d->f;
|
2022-03-22 18:59:30 +02:00
|
|
|
AVPacket *pkt;
|
2022-10-17 12:49:51 +02:00
|
|
|
unsigned flags = d->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
|
2022-08-03 14:08:44 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
2022-03-22 18:59:30 +02:00
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:50:30 +02:00
|
|
|
thread_set_name(f);
|
|
|
|
|
2022-08-03 14:08:44 +02:00
|
|
|
while (1) {
|
2022-03-22 11:41:55 +02:00
|
|
|
DemuxMsg msg = { NULL };
|
|
|
|
|
2022-08-03 14:08:44 +02:00
|
|
|
ret = av_read_frame(f->ctx, pkt);
|
|
|
|
|
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
av_usleep(10000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2022-10-17 11:17:34 +02:00
|
|
|
if (d->loop) {
|
2022-03-22 11:41:55 +02:00
|
|
|
/* signal looping to the consumer thread */
|
|
|
|
msg.looping = 1;
|
2022-10-17 12:49:51 +02:00
|
|
|
ret = av_thread_message_queue_send(d->in_thread_queue, &msg, 0);
|
2022-03-22 11:41:55 +02:00
|
|
|
if (ret >= 0)
|
2022-10-17 11:17:34 +02:00
|
|
|
ret = seek_to_start(d);
|
2022-03-22 11:41:55 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* fallthrough to the error path */
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:25:29 +02:00
|
|
|
if (ret == AVERROR_EOF)
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
|
2022-10-17 13:25:29 +02:00
|
|
|
else
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
|
|
|
|
av_err2str(ret));
|
2022-10-17 13:25:29 +02:00
|
|
|
|
2022-08-03 14:08:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-03-21 18:12:25 +02:00
|
|
|
|
|
|
|
if (do_pkt_dump) {
|
|
|
|
av_pkt_dump_log2(NULL, AV_LOG_INFO, pkt, do_hex_dump,
|
|
|
|
f->ctx->streams[pkt->stream_index]);
|
|
|
|
}
|
|
|
|
|
2022-03-21 18:29:21 +02:00
|
|
|
/* the following test is needed in case new streams appear
|
|
|
|
dynamically in stream : we ignore them */
|
|
|
|
if (pkt->stream_index >= f->nb_streams) {
|
2022-10-17 12:49:51 +02:00
|
|
|
report_new_stream(d, pkt);
|
2022-03-21 18:29:21 +02:00
|
|
|
av_packet_unref(pkt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-22 19:17:27 +02:00
|
|
|
if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
|
|
|
|
"corrupt input packet in stream %d\n",
|
|
|
|
pkt->stream_index);
|
2022-03-22 19:17:27 +02:00
|
|
|
if (exit_on_error) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
ts_fixup(d, pkt, &msg.repeat_pict);
|
2022-08-10 17:15:06 +02:00
|
|
|
|
2022-03-22 11:41:55 +02:00
|
|
|
msg.pkt = av_packet_alloc();
|
|
|
|
if (!msg.pkt) {
|
2022-08-03 14:08:44 +02:00
|
|
|
av_packet_unref(pkt);
|
2022-03-22 18:56:30 +02:00
|
|
|
ret = AVERROR(ENOMEM);
|
2022-08-03 14:08:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-03-22 11:41:55 +02:00
|
|
|
av_packet_move_ref(msg.pkt, pkt);
|
2022-10-17 12:49:51 +02:00
|
|
|
ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
|
2022-08-03 14:08:44 +02:00
|
|
|
if (flags && ret == AVERROR(EAGAIN)) {
|
|
|
|
flags = 0;
|
2022-10-17 12:49:51 +02:00
|
|
|
ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
|
2022-08-03 14:08:44 +02:00
|
|
|
av_log(f->ctx, AV_LOG_WARNING,
|
|
|
|
"Thread message queue blocking; consider raising the "
|
|
|
|
"thread_queue_size option (current value: %d)\n",
|
2022-10-17 12:49:51 +02:00
|
|
|
d->thread_queue_size);
|
2022-08-03 14:08:44 +02:00
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret != AVERROR_EOF)
|
|
|
|
av_log(f->ctx, AV_LOG_ERROR,
|
|
|
|
"Unable to send packet to main thread: %s\n",
|
|
|
|
av_err2str(ret));
|
2022-03-22 11:41:55 +02:00
|
|
|
av_packet_free(&msg.pkt);
|
2022-08-03 14:08:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 18:59:30 +02:00
|
|
|
finish:
|
2022-03-22 18:56:30 +02:00
|
|
|
av_assert0(ret < 0);
|
2022-10-17 12:49:51 +02:00
|
|
|
av_thread_message_queue_set_err_recv(d->in_thread_queue, ret);
|
2022-03-22 18:56:30 +02:00
|
|
|
|
2022-03-22 18:59:30 +02:00
|
|
|
av_packet_free(&pkt);
|
|
|
|
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n");
|
2022-10-17 13:25:29 +02:00
|
|
|
|
2022-08-03 14:08:44 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:35:22 +02:00
|
|
|
static void thread_stop(Demuxer *d)
|
2022-08-03 14:08:44 +02:00
|
|
|
{
|
2022-10-17 12:35:22 +02:00
|
|
|
InputFile *f = &d->f;
|
2022-03-22 11:41:55 +02:00
|
|
|
DemuxMsg msg;
|
2022-08-03 14:08:44 +02:00
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
if (!d->in_thread_queue)
|
2022-08-03 14:08:44 +02:00
|
|
|
return;
|
2022-10-17 12:49:51 +02:00
|
|
|
av_thread_message_queue_set_err_send(d->in_thread_queue, AVERROR_EOF);
|
|
|
|
while (av_thread_message_queue_recv(d->in_thread_queue, &msg, 0) >= 0)
|
2022-03-22 11:41:55 +02:00
|
|
|
av_packet_free(&msg.pkt);
|
2022-08-03 14:08:44 +02:00
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
pthread_join(d->thread, NULL);
|
|
|
|
av_thread_message_queue_free(&d->in_thread_queue);
|
2022-03-22 11:41:55 +02:00
|
|
|
av_thread_message_queue_free(&f->audio_duration_queue);
|
2022-08-03 14:08:44 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 11:23:55 +02:00
|
|
|
static int thread_start(Demuxer *d)
|
2022-08-03 14:08:44 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2022-10-17 11:23:55 +02:00
|
|
|
InputFile *f = &d->f;
|
2022-08-03 14:08:44 +02:00
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
if (d->thread_queue_size <= 0)
|
|
|
|
d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
|
2022-08-03 14:08:44 +02:00
|
|
|
|
2022-11-08 13:07:11 +02:00
|
|
|
if (nb_input_files > 1 &&
|
|
|
|
(f->ctx->pb ? !f->ctx->pb->seekable :
|
|
|
|
strcmp(f->ctx->iformat->name, "lavfi")))
|
2022-10-17 12:49:51 +02:00
|
|
|
d->non_blocking = 1;
|
|
|
|
ret = av_thread_message_queue_alloc(&d->in_thread_queue,
|
|
|
|
d->thread_queue_size, sizeof(DemuxMsg));
|
2022-08-03 14:08:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-10-17 11:17:34 +02:00
|
|
|
if (d->loop) {
|
2022-03-22 11:41:55 +02:00
|
|
|
int nb_audio_dec = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < f->nb_streams; i++) {
|
2022-11-17 10:49:27 +02:00
|
|
|
InputStream *ist = f->streams[i];
|
2022-03-22 11:41:55 +02:00
|
|
|
nb_audio_dec += !!(ist->decoding_needed &&
|
|
|
|
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nb_audio_dec) {
|
|
|
|
ret = av_thread_message_queue_alloc(&f->audio_duration_queue,
|
|
|
|
nb_audio_dec, sizeof(LastFrameDuration));
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
f->audio_duration_queue_size = nb_audio_dec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
if ((ret = pthread_create(&d->thread, NULL, input_thread, d))) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
|
2022-03-22 11:41:55 +02:00
|
|
|
ret = AVERROR(ret);
|
|
|
|
goto fail;
|
2022-08-03 14:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-03-22 11:41:55 +02:00
|
|
|
fail:
|
2022-10-17 12:49:51 +02:00
|
|
|
av_thread_message_queue_free(&d->in_thread_queue);
|
2022-03-22 11:41:55 +02:00
|
|
|
return ret;
|
2022-08-03 14:08:44 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 14:30:48 +02:00
|
|
|
int ifile_get_packet(InputFile *f, AVPacket **pkt)
|
|
|
|
{
|
2022-10-17 11:23:55 +02:00
|
|
|
Demuxer *d = demuxer_from_ifile(f);
|
2022-08-18 11:13:08 +02:00
|
|
|
InputStream *ist;
|
2022-03-22 11:41:55 +02:00
|
|
|
DemuxMsg msg;
|
|
|
|
int ret;
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
if (!d->in_thread_queue) {
|
2022-10-17 11:23:55 +02:00
|
|
|
ret = thread_start(d);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-03 14:30:48 +02:00
|
|
|
if (f->readrate || f->rate_emu) {
|
|
|
|
int i;
|
|
|
|
int64_t file_start = copy_ts * (
|
2022-10-21 15:59:25 +02:00
|
|
|
(f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) +
|
2022-08-03 14:30:48 +02:00
|
|
|
(f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
|
|
|
|
);
|
|
|
|
float scale = f->rate_emu ? 1.0 : f->readrate;
|
|
|
|
for (i = 0; i < f->nb_streams; i++) {
|
2022-11-17 10:49:27 +02:00
|
|
|
InputStream *ist = f->streams[i];
|
2022-08-03 14:30:48 +02:00
|
|
|
int64_t stream_ts_offset, pts, now;
|
|
|
|
if (!ist->nb_packets || (ist->decoding_needed && !ist->got_output)) continue;
|
|
|
|
stream_ts_offset = FFMAX(ist->first_dts != AV_NOPTS_VALUE ? ist->first_dts : 0, file_start);
|
|
|
|
pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
|
|
|
|
now = (av_gettime_relative() - ist->start) * scale + stream_ts_offset;
|
|
|
|
if (pts > now)
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
ret = av_thread_message_queue_recv(d->in_thread_queue, &msg,
|
|
|
|
d->non_blocking ?
|
2022-03-22 11:41:55 +02:00
|
|
|
AV_THREAD_MESSAGE_NONBLOCK : 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (msg.looping)
|
|
|
|
return 1;
|
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
ist = f->streams[msg.pkt->stream_index];
|
2022-08-18 11:13:08 +02:00
|
|
|
ist->last_pkt_repeat_pict = msg.repeat_pict;
|
|
|
|
|
2022-03-22 11:41:55 +02:00
|
|
|
*pkt = msg.pkt;
|
|
|
|
return 0;
|
2022-08-03 14:30:48 +02:00
|
|
|
}
|
2022-10-17 10:49:42 +02:00
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
static void ist_free(InputStream **pist)
|
|
|
|
{
|
|
|
|
InputStream *ist = *pist;
|
|
|
|
|
|
|
|
if (!ist)
|
|
|
|
return;
|
|
|
|
|
|
|
|
av_frame_free(&ist->decoded_frame);
|
|
|
|
av_packet_free(&ist->pkt);
|
|
|
|
av_dict_free(&ist->decoder_opts);
|
|
|
|
avsubtitle_free(&ist->prev_sub.subtitle);
|
|
|
|
av_frame_free(&ist->sub2video.frame);
|
|
|
|
av_freep(&ist->filters);
|
|
|
|
av_freep(&ist->hwaccel_device);
|
|
|
|
av_freep(&ist->dts_buffer);
|
|
|
|
|
|
|
|
avcodec_free_context(&ist->dec_ctx);
|
|
|
|
avcodec_parameters_free(&ist->par);
|
|
|
|
|
|
|
|
av_freep(pist);
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:37:45 +02:00
|
|
|
void ifile_close(InputFile **pf)
|
|
|
|
{
|
|
|
|
InputFile *f = *pf;
|
2022-10-17 12:35:22 +02:00
|
|
|
Demuxer *d = demuxer_from_ifile(f);
|
2022-10-17 11:37:45 +02:00
|
|
|
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
|
2022-10-17 12:35:22 +02:00
|
|
|
thread_stop(d);
|
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
for (int i = 0; i < f->nb_streams; i++)
|
|
|
|
ist_free(&f->streams[i]);
|
|
|
|
av_freep(&f->streams);
|
|
|
|
|
2022-10-17 11:37:45 +02:00
|
|
|
avformat_close_input(&f->ctx);
|
|
|
|
|
|
|
|
av_freep(pf);
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:19:38 +02:00
|
|
|
static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
|
2022-10-17 10:49:42 +02:00
|
|
|
enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type)
|
|
|
|
|
|
|
|
{
|
|
|
|
char *codec_name = NULL;
|
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
|
|
|
|
if (codec_name) {
|
2023-01-28 11:06:20 +02:00
|
|
|
const AVCodec *codec = find_codec_or_die(NULL, codec_name, st->codecpar->codec_type, 0);
|
2022-10-17 10:49:42 +02:00
|
|
|
st->codecpar->codec_id = codec->id;
|
|
|
|
if (recast_media && st->codecpar->codec_type != codec->type)
|
|
|
|
st->codecpar->codec_type = codec->type;
|
|
|
|
return codec;
|
|
|
|
} else {
|
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
hwaccel_id == HWACCEL_GENERIC &&
|
|
|
|
hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
|
|
|
|
const AVCodec *c;
|
|
|
|
void *i = NULL;
|
|
|
|
|
|
|
|
while ((c = av_codec_iterate(&i))) {
|
|
|
|
const AVCodecHWConfig *config;
|
|
|
|
|
|
|
|
if (c->id != st->codecpar->codec_id ||
|
|
|
|
!av_codec_is_decoder(c))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
|
|
|
|
if (config->device_type == hwaccel_device_type) {
|
|
|
|
av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
|
|
|
|
c->name, av_hwdevice_get_type_name(hwaccel_device_type));
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return avcodec_find_decoder(st->codecpar->codec_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 08:32:35 +02:00
|
|
|
static int guess_input_channel_layout(InputStream *ist, int guess_layout_max)
|
2022-10-17 10:49:42 +02:00
|
|
|
{
|
|
|
|
AVCodecContext *dec = ist->dec_ctx;
|
|
|
|
|
|
|
|
if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
|
|
|
|
char layout_name[256];
|
|
|
|
|
2023-02-11 08:32:35 +02:00
|
|
|
if (dec->ch_layout.nb_channels > guess_layout_max)
|
2022-10-17 10:49:42 +02:00
|
|
|
return 0;
|
|
|
|
av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
|
|
|
|
if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
|
|
|
|
return 0;
|
|
|
|
av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:19:38 +02:00
|
|
|
static void add_display_matrix_to_stream(const OptionsContext *o,
|
2023-02-11 08:59:36 +02:00
|
|
|
AVFormatContext *ctx, InputStream *ist)
|
2022-10-17 10:49:42 +02:00
|
|
|
{
|
2023-02-11 08:59:36 +02:00
|
|
|
AVStream *st = ist->st;
|
2022-10-17 10:49:42 +02:00
|
|
|
double rotation = DBL_MAX;
|
|
|
|
int hflip = -1, vflip = -1;
|
|
|
|
int hflip_set = 0, vflip_set = 0, rotation_set = 0;
|
|
|
|
int32_t *buf;
|
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st);
|
|
|
|
MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st);
|
|
|
|
MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st);
|
|
|
|
|
|
|
|
rotation_set = rotation != DBL_MAX;
|
|
|
|
hflip_set = hflip != -1;
|
|
|
|
vflip_set = vflip != -1;
|
|
|
|
|
|
|
|
if (!rotation_set && !hflip_set && !vflip_set)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buf = (int32_t *)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9);
|
|
|
|
if (!buf) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_display_rotation_set(buf,
|
|
|
|
rotation_set ? -(rotation) : -0.0f);
|
|
|
|
|
|
|
|
av_display_matrix_flip(buf,
|
|
|
|
hflip_set ? hflip : 0,
|
|
|
|
vflip_set ? vflip : 0);
|
|
|
|
}
|
|
|
|
|
2023-02-11 08:59:36 +02:00
|
|
|
static const char *input_stream_item_name(void *obj)
|
|
|
|
{
|
|
|
|
const DemuxStream *ds = obj;
|
|
|
|
|
|
|
|
return ds->log_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass input_stream_class = {
|
|
|
|
.class_name = "InputStream",
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.item_name = input_stream_item_name,
|
|
|
|
.category = AV_CLASS_CATEGORY_DEMUXER,
|
|
|
|
};
|
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
|
|
|
|
{
|
2023-02-11 08:59:36 +02:00
|
|
|
const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
|
2023-02-11 08:30:15 +02:00
|
|
|
InputFile *f = &d->f;
|
|
|
|
DemuxStream *ds = allocate_array_elem(&f->streams, sizeof(*ds),
|
|
|
|
&f->nb_streams);
|
|
|
|
|
|
|
|
ds->ist.st = st;
|
|
|
|
ds->ist.file_index = f->index;
|
2023-02-11 08:59:36 +02:00
|
|
|
ds->ist.class = &input_stream_class;
|
|
|
|
|
|
|
|
snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
|
|
|
|
type_str ? *type_str : '?', d->f.index, st->index,
|
|
|
|
avcodec_get_name(st->codecpar->codec_id));
|
2023-02-11 08:30:15 +02:00
|
|
|
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
/* Add all the streams from the given input file to the demuxer */
|
|
|
|
static void add_input_streams(const OptionsContext *o, Demuxer *d)
|
2022-10-17 10:49:42 +02:00
|
|
|
{
|
2022-11-17 10:49:27 +02:00
|
|
|
InputFile *f = &d->f;
|
|
|
|
AVFormatContext *ic = f->ctx;
|
2022-10-17 10:49:42 +02:00
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < ic->nb_streams; i++) {
|
|
|
|
AVStream *st = ic->streams[i];
|
|
|
|
AVCodecParameters *par = st->codecpar;
|
2023-02-11 08:30:15 +02:00
|
|
|
DemuxStream *ds;
|
2022-10-17 10:49:42 +02:00
|
|
|
InputStream *ist;
|
|
|
|
char *framerate = NULL, *hwaccel_device = NULL;
|
|
|
|
const char *hwaccel = NULL;
|
|
|
|
char *hwaccel_output_format = NULL;
|
|
|
|
char *codec_tag = NULL;
|
|
|
|
char *next;
|
|
|
|
char *discard_str = NULL;
|
|
|
|
const AVClass *cc = avcodec_get_class();
|
|
|
|
const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
|
|
|
|
0, AV_OPT_SEARCH_FAKE_OBJ);
|
|
|
|
|
2023-02-11 08:30:15 +02:00
|
|
|
ds = demux_stream_alloc(d, st);
|
|
|
|
ist = &ds->ist;
|
|
|
|
|
2022-10-17 10:49:42 +02:00
|
|
|
ist->discard = 1;
|
|
|
|
st->discard = AVDISCARD_ALL;
|
|
|
|
ist->nb_samples = 0;
|
|
|
|
ist->first_dts = AV_NOPTS_VALUE;
|
2023-02-11 08:30:15 +02:00
|
|
|
ds->min_pts = INT64_MAX;
|
|
|
|
ds->max_pts = INT64_MIN;
|
2022-10-17 10:49:42 +02:00
|
|
|
|
|
|
|
ist->ts_scale = 1.0;
|
|
|
|
MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
|
|
|
|
|
|
|
|
ist->autorotate = 1;
|
|
|
|
MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
|
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
|
|
|
|
if (codec_tag) {
|
|
|
|
uint32_t tag = strtol(codec_tag, &next, 0);
|
|
|
|
if (*next)
|
|
|
|
tag = AV_RL32(codec_tag);
|
|
|
|
st->codecpar->codec_tag = tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2023-02-11 08:59:36 +02:00
|
|
|
add_display_matrix_to_stream(o, ic, ist);
|
2022-10-17 10:49:42 +02:00
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
|
|
|
|
MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
|
|
|
|
hwaccel_output_format, ic, st);
|
|
|
|
|
|
|
|
if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_WARNING,
|
2022-10-17 10:49:42 +02:00
|
|
|
"WARNING: defaulting hwaccel_output_format to cuda for compatibility "
|
|
|
|
"with old commandlines. This behaviour is DEPRECATED and will be removed "
|
|
|
|
"in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
|
|
|
|
ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
|
|
|
|
} else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_WARNING,
|
2022-10-17 10:49:42 +02:00
|
|
|
"WARNING: defaulting hwaccel_output_format to qsv for compatibility "
|
|
|
|
"with old commandlines. This behaviour is DEPRECATED and will be removed "
|
|
|
|
"in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
|
|
|
|
ist->hwaccel_output_format = AV_PIX_FMT_QSV;
|
2022-10-22 07:38:34 +02:00
|
|
|
} else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
|
|
|
|
// There is no real AVHWFrameContext implementation. Set
|
|
|
|
// hwaccel_output_format to avoid av_hwframe_transfer_data error.
|
|
|
|
ist->hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
|
2022-10-17 10:49:42 +02:00
|
|
|
} else if (hwaccel_output_format) {
|
|
|
|
ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
|
|
|
|
if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
|
2022-10-17 10:49:42 +02:00
|
|
|
"format: %s", hwaccel_output_format);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ist->hwaccel_output_format = AV_PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hwaccel) {
|
|
|
|
// The NVDEC hwaccels use a CUDA device, so remap the name here.
|
|
|
|
if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
|
|
|
|
hwaccel = "cuda";
|
|
|
|
|
|
|
|
if (!strcmp(hwaccel, "none"))
|
|
|
|
ist->hwaccel_id = HWACCEL_NONE;
|
|
|
|
else if (!strcmp(hwaccel, "auto"))
|
|
|
|
ist->hwaccel_id = HWACCEL_AUTO;
|
|
|
|
else {
|
|
|
|
enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
|
|
|
|
if (type != AV_HWDEVICE_TYPE_NONE) {
|
|
|
|
ist->hwaccel_id = HWACCEL_GENERIC;
|
|
|
|
ist->hwaccel_device_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ist->hwaccel_id) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
|
2022-10-17 10:49:42 +02:00
|
|
|
hwaccel);
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
|
2022-10-17 10:49:42 +02:00
|
|
|
type = AV_HWDEVICE_TYPE_NONE;
|
|
|
|
while ((type = av_hwdevice_iterate_types(type)) !=
|
|
|
|
AV_HWDEVICE_TYPE_NONE)
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "%s ",
|
2022-10-17 10:49:42 +02:00
|
|
|
av_hwdevice_get_type_name(type));
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
|
|
|
|
if (hwaccel_device) {
|
|
|
|
ist->hwaccel_device = av_strdup(hwaccel_device);
|
|
|
|
if (!ist->hwaccel_device)
|
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
|
|
|
}
|
|
|
|
|
|
|
|
ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ist->dec = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type);
|
|
|
|
ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
|
|
|
|
|
|
|
|
ist->reinit_filters = -1;
|
|
|
|
MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
|
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
|
|
|
|
ist->user_set_discard = AVDISCARD_NONE;
|
|
|
|
|
|
|
|
if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
|
|
|
|
(o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
|
|
|
|
(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
|
|
|
|
(o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
|
|
|
|
ist->user_set_discard = AVDISCARD_ALL;
|
|
|
|
|
|
|
|
if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n",
|
2022-10-17 10:49:42 +02:00
|
|
|
discard_str);
|
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
|
|
|
|
ist->prev_pkt_pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
ist->dec_ctx = avcodec_alloc_context3(ist->dec);
|
|
|
|
if (!ist->dec_ctx)
|
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
|
|
|
|
|
|
|
ret = avcodec_parameters_to_context(ist->dec_ctx, par);
|
|
|
|
if (ret < 0) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ist->decoded_frame = av_frame_alloc();
|
|
|
|
if (!ist->decoded_frame)
|
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
|
|
|
|
|
|
|
ist->pkt = av_packet_alloc();
|
|
|
|
if (!ist->pkt)
|
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
|
|
|
|
|
|
|
if (o->bitexact)
|
|
|
|
ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
|
|
|
|
|
|
|
|
switch (par->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
// avformat_find_stream_info() doesn't set this for us anymore.
|
|
|
|
ist->dec_ctx->framerate = st->avg_frame_rate;
|
|
|
|
|
|
|
|
MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
|
|
|
|
if (framerate && av_parse_video_rate(&ist->framerate,
|
|
|
|
framerate) < 0) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
|
2022-10-17 10:49:42 +02:00
|
|
|
framerate);
|
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ist->top_field_first = -1;
|
|
|
|
MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
|
|
|
|
|
|
|
|
ist->framerate_guessed = av_guess_frame_rate(ic, st, NULL);
|
|
|
|
|
|
|
|
break;
|
2023-02-11 08:32:35 +02:00
|
|
|
case AVMEDIA_TYPE_AUDIO: {
|
|
|
|
int guess_layout_max = INT_MAX;
|
|
|
|
MATCH_PER_STREAM_OPT(guess_layout_max, i, guess_layout_max, ic, st);
|
|
|
|
guess_input_channel_layout(ist, guess_layout_max);
|
2022-10-17 10:49:42 +02:00
|
|
|
break;
|
2023-02-11 08:32:35 +02:00
|
|
|
}
|
2022-10-17 10:49:42 +02:00
|
|
|
case AVMEDIA_TYPE_DATA:
|
|
|
|
case AVMEDIA_TYPE_SUBTITLE: {
|
|
|
|
char *canvas_size = NULL;
|
|
|
|
MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
|
|
|
|
MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
|
|
|
|
if (canvas_size &&
|
|
|
|
av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height, canvas_size) < 0) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AVMEDIA_TYPE_ATTACHMENT:
|
|
|
|
case AVMEDIA_TYPE_UNKNOWN:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
ist->par = avcodec_parameters_alloc();
|
|
|
|
if (!ist->par)
|
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
|
|
|
|
|
|
|
ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
|
|
|
|
if (ret < 0) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 08:59:36 +02:00
|
|
|
static void dump_attachment(InputStream *ist, const char *filename)
|
2022-10-17 10:49:42 +02:00
|
|
|
{
|
2023-02-11 08:59:36 +02:00
|
|
|
AVStream *st = ist->st;
|
2022-10-17 10:49:42 +02:00
|
|
|
int ret;
|
|
|
|
AVIOContext *out = NULL;
|
|
|
|
const AVDictionaryEntry *e;
|
|
|
|
|
|
|
|
if (!st->codecpar->extradata_size) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
|
|
|
|
filename = e->value;
|
|
|
|
if (!*filename) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_file_overwrite(filename);
|
|
|
|
|
|
|
|
if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
|
2023-02-11 08:59:36 +02:00
|
|
|
av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
|
2022-10-17 10:49:42 +02:00
|
|
|
filename);
|
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
|
|
|
|
avio_flush(out);
|
|
|
|
avio_close(out);
|
|
|
|
}
|
|
|
|
|
2023-01-29 13:35:47 +02:00
|
|
|
static const char *input_file_item_name(void *obj)
|
|
|
|
{
|
|
|
|
const Demuxer *d = obj;
|
|
|
|
|
|
|
|
return d->log_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass input_file_class = {
|
|
|
|
.class_name = "InputFile",
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.item_name = input_file_item_name,
|
|
|
|
.category = AV_CLASS_CATEGORY_DEMUXER,
|
|
|
|
};
|
|
|
|
|
|
|
|
static Demuxer *demux_alloc(void)
|
|
|
|
{
|
|
|
|
Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
|
|
|
|
|
|
|
|
d->f.class = &input_file_class;
|
|
|
|
d->f.index = nb_input_files - 1;
|
|
|
|
|
|
|
|
snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:19:38 +02:00
|
|
|
int ifile_open(const OptionsContext *o, const char *filename)
|
2022-10-17 10:49:42 +02:00
|
|
|
{
|
2022-10-17 11:17:34 +02:00
|
|
|
Demuxer *d;
|
2022-10-17 10:49:42 +02:00
|
|
|
InputFile *f;
|
|
|
|
AVFormatContext *ic;
|
|
|
|
const AVInputFormat *file_iformat = NULL;
|
|
|
|
int err, i, ret;
|
|
|
|
int64_t timestamp;
|
|
|
|
AVDictionary *unused_opts = NULL;
|
|
|
|
const AVDictionaryEntry *e = NULL;
|
|
|
|
char * video_codec_name = NULL;
|
|
|
|
char * audio_codec_name = NULL;
|
|
|
|
char *subtitle_codec_name = NULL;
|
|
|
|
char * data_codec_name = NULL;
|
|
|
|
int scan_all_pmts_set = 0;
|
|
|
|
|
2022-10-18 13:02:21 +02:00
|
|
|
int64_t start_time = o->start_time;
|
|
|
|
int64_t start_time_eof = o->start_time_eof;
|
|
|
|
int64_t stop_time = o->stop_time;
|
|
|
|
int64_t recording_time = o->recording_time;
|
|
|
|
|
2023-01-29 13:35:47 +02:00
|
|
|
d = demux_alloc();
|
|
|
|
f = &d->f;
|
|
|
|
|
2022-10-18 13:02:21 +02:00
|
|
|
if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
|
|
|
|
stop_time = INT64_MAX;
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 13:02:21 +02:00
|
|
|
if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
|
|
|
|
int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
|
|
|
|
if (stop_time <= start) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
} else {
|
2022-10-18 13:02:21 +02:00
|
|
|
recording_time = stop_time - start;
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o->format) {
|
|
|
|
if (!(file_iformat = av_find_input_format(o->format))) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(filename, "-"))
|
2022-12-14 19:10:14 +02:00
|
|
|
filename = "fd:";
|
2022-10-17 10:49:42 +02:00
|
|
|
|
|
|
|
stdin_interaction &= strncmp(filename, "pipe:", 5) &&
|
2022-12-14 19:10:11 +02:00
|
|
|
strcmp(filename, "fd:") &&
|
2022-10-17 10:49:42 +02:00
|
|
|
strcmp(filename, "/dev/stdin");
|
|
|
|
|
|
|
|
/* get default parameters from command line */
|
|
|
|
ic = avformat_alloc_context();
|
|
|
|
if (!ic)
|
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
|
|
|
if (o->nb_audio_sample_rate) {
|
|
|
|
av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
|
|
|
|
}
|
|
|
|
if (o->nb_audio_channels) {
|
|
|
|
const AVClass *priv_class;
|
|
|
|
if (file_iformat && (priv_class = file_iformat->priv_class) &&
|
|
|
|
av_opt_find(&priv_class, "ch_layout", NULL, 0,
|
|
|
|
AV_OPT_SEARCH_FAKE_OBJ)) {
|
|
|
|
char buf[32];
|
|
|
|
snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
|
|
|
|
av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (o->nb_audio_ch_layouts) {
|
|
|
|
const AVClass *priv_class;
|
|
|
|
if (file_iformat && (priv_class = file_iformat->priv_class) &&
|
|
|
|
av_opt_find(&priv_class, "ch_layout", NULL, 0,
|
|
|
|
AV_OPT_SEARCH_FAKE_OBJ)) {
|
|
|
|
av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (o->nb_frame_rates) {
|
|
|
|
const AVClass *priv_class;
|
|
|
|
/* set the format-level framerate option;
|
|
|
|
* this is important for video grabbers, e.g. x11 */
|
|
|
|
if (file_iformat && (priv_class = file_iformat->priv_class) &&
|
|
|
|
av_opt_find(&priv_class, "framerate", NULL, 0,
|
|
|
|
AV_OPT_SEARCH_FAKE_OBJ)) {
|
|
|
|
av_dict_set(&o->g->format_opts, "framerate",
|
|
|
|
o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (o->nb_frame_sizes) {
|
|
|
|
av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
|
|
|
|
}
|
|
|
|
if (o->nb_frame_pix_fmts)
|
|
|
|
av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
|
|
|
|
|
|
|
|
MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
|
|
|
|
MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
|
|
|
|
MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
|
|
|
|
MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
|
|
|
|
|
|
|
|
if (video_codec_name)
|
2023-01-28 11:06:20 +02:00
|
|
|
ic->video_codec = find_codec_or_die(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0);
|
2022-10-17 10:49:42 +02:00
|
|
|
if (audio_codec_name)
|
2023-01-28 11:06:20 +02:00
|
|
|
ic->audio_codec = find_codec_or_die(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0);
|
2022-10-17 10:49:42 +02:00
|
|
|
if (subtitle_codec_name)
|
2023-01-28 11:06:20 +02:00
|
|
|
ic->subtitle_codec = find_codec_or_die(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
|
2022-10-17 10:49:42 +02:00
|
|
|
if (data_codec_name)
|
2023-01-28 11:06:20 +02:00
|
|
|
ic->data_codec = find_codec_or_die(NULL, data_codec_name , AVMEDIA_TYPE_DATA , 0);
|
2022-10-17 10:49:42 +02:00
|
|
|
|
|
|
|
ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
|
|
|
|
ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
|
|
|
|
ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
|
|
|
|
ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
|
|
|
|
|
|
|
|
ic->flags |= AVFMT_FLAG_NONBLOCK;
|
|
|
|
if (o->bitexact)
|
|
|
|
ic->flags |= AVFMT_FLAG_BITEXACT;
|
|
|
|
ic->interrupt_callback = int_cb;
|
|
|
|
|
|
|
|
if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
|
|
|
|
av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
|
|
|
|
scan_all_pmts_set = 1;
|
|
|
|
}
|
|
|
|
/* open the input file with generic avformat function */
|
|
|
|
err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
|
|
|
|
if (err < 0) {
|
|
|
|
print_error(filename, err);
|
|
|
|
if (err == AVERROR_PROTOCOL_NOT_FOUND)
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
2023-01-29 13:35:47 +02:00
|
|
|
|
|
|
|
av_strlcat(d->log_name, "/", sizeof(d->log_name));
|
|
|
|
av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
|
|
|
|
|
2022-10-17 10:49:42 +02:00
|
|
|
if (scan_all_pmts_set)
|
|
|
|
av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
|
|
|
|
remove_avoptions(&o->g->format_opts, o->g->codec_opts);
|
|
|
|
assert_avoptions(o->g->format_opts);
|
|
|
|
|
|
|
|
/* apply forced codec ids */
|
|
|
|
for (i = 0; i < ic->nb_streams; i++)
|
|
|
|
choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE);
|
|
|
|
|
|
|
|
if (o->find_stream_info) {
|
|
|
|
AVDictionary **opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
|
|
|
|
int orig_nb_streams = ic->nb_streams;
|
|
|
|
|
|
|
|
/* If not enough info to get the stream parameters, we decode the
|
|
|
|
first frames to get it. (used in mpeg case for example) */
|
|
|
|
ret = avformat_find_stream_info(ic, opts);
|
|
|
|
|
|
|
|
for (i = 0; i < orig_nb_streams; i++)
|
|
|
|
av_dict_free(&opts[i]);
|
|
|
|
av_freep(&opts);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
if (ic->nb_streams == 0) {
|
|
|
|
avformat_close_input(&ic);
|
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 13:02:21 +02:00
|
|
|
if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
|
2022-10-18 13:02:21 +02:00
|
|
|
start_time_eof = AV_NOPTS_VALUE;
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 13:02:21 +02:00
|
|
|
if (start_time_eof != AV_NOPTS_VALUE) {
|
|
|
|
if (start_time_eof >= 0) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
if (ic->duration > 0) {
|
2022-10-18 13:02:21 +02:00
|
|
|
start_time = start_time_eof + ic->duration;
|
|
|
|
if (start_time < 0) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
|
2022-10-18 13:02:21 +02:00
|
|
|
start_time = AV_NOPTS_VALUE;
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
} else
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
2022-10-18 13:02:21 +02:00
|
|
|
timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
|
2022-10-17 10:49:42 +02:00
|
|
|
/* add the stream start time */
|
|
|
|
if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
|
|
|
|
timestamp += ic->start_time;
|
|
|
|
|
|
|
|
/* if seeking requested, we execute it */
|
2022-10-18 13:02:21 +02:00
|
|
|
if (start_time != AV_NOPTS_VALUE) {
|
2022-10-17 10:49:42 +02:00
|
|
|
int64_t seek_timestamp = timestamp;
|
|
|
|
|
|
|
|
if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
|
|
|
|
int dts_heuristic = 0;
|
|
|
|
for (i=0; i<ic->nb_streams; i++) {
|
|
|
|
const AVCodecParameters *par = ic->streams[i]->codecpar;
|
|
|
|
if (par->video_delay) {
|
|
|
|
dts_heuristic = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dts_heuristic) {
|
|
|
|
seek_timestamp -= 3*AV_TIME_BASE / 23;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
|
|
|
|
if (ret < 0) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
|
|
|
|
(double)timestamp / AV_TIME_BASE);
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f->ctx = ic;
|
2022-10-18 13:02:21 +02:00
|
|
|
f->start_time = start_time;
|
|
|
|
f->recording_time = recording_time;
|
2022-10-17 10:49:42 +02:00
|
|
|
f->input_sync_ref = o->input_sync_ref;
|
|
|
|
f->input_ts_offset = o->input_ts_offset;
|
|
|
|
f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
|
|
|
|
f->rate_emu = o->rate_emu;
|
|
|
|
f->accurate_seek = o->accurate_seek;
|
2022-10-17 11:17:34 +02:00
|
|
|
d->loop = o->loop;
|
2022-10-17 12:49:51 +02:00
|
|
|
d->duration = 0;
|
|
|
|
d->time_base = (AVRational){ 1, 1 };
|
2022-10-17 10:49:42 +02:00
|
|
|
|
|
|
|
f->readrate = o->readrate ? o->readrate : 0.0;
|
|
|
|
if (f->readrate < 0.0f) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", f->readrate);
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
if (f->readrate && f->rate_emu) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", f->readrate);
|
2022-10-17 10:49:42 +02:00
|
|
|
f->rate_emu = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:49:51 +02:00
|
|
|
d->thread_queue_size = o->thread_queue_size;
|
2022-10-17 10:49:42 +02:00
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
/* update the current parameters so that they match the one of the input stream */
|
|
|
|
add_input_streams(o, d);
|
|
|
|
|
|
|
|
/* dump the file content */
|
|
|
|
av_dump_format(ic, f->index, filename, 0);
|
|
|
|
|
2022-10-17 10:49:42 +02:00
|
|
|
/* check if all codec options have been used */
|
|
|
|
unused_opts = strip_specifiers(o->g->codec_opts);
|
2022-11-17 10:49:27 +02:00
|
|
|
for (i = 0; i < f->nb_streams; i++) {
|
2022-10-17 10:49:42 +02:00
|
|
|
e = NULL;
|
2022-11-26 16:46:18 +02:00
|
|
|
while ((e = av_dict_iterate(f->streams[i]->decoder_opts, e)))
|
2022-10-17 10:49:42 +02:00
|
|
|
av_dict_set(&unused_opts, e->key, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
e = NULL;
|
2022-11-26 16:46:18 +02:00
|
|
|
while ((e = av_dict_iterate(unused_opts, e))) {
|
2022-10-17 10:49:42 +02:00
|
|
|
const AVClass *class = avcodec_get_class();
|
|
|
|
const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
|
|
|
|
AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
|
|
|
|
const AVClass *fclass = avformat_get_class();
|
|
|
|
const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
|
|
|
|
AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
|
|
|
|
if (!option || foption)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_ERROR, "Codec AVOption %s (%s) is not a decoding "
|
|
|
|
"option.\n", e->key, option->help ? option->help : "");
|
2022-10-17 10:49:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
2023-01-29 13:35:47 +02:00
|
|
|
av_log(d, AV_LOG_WARNING, "Codec AVOption %s (%s) has not been used "
|
|
|
|
"for any stream. The most likely reason is either wrong type "
|
|
|
|
"(e.g. a video option with no video streams) or that it is a "
|
|
|
|
"private option of some decoder which was not actually used "
|
|
|
|
"for any stream.\n", e->key, option->help ? option->help : "");
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
av_dict_free(&unused_opts);
|
|
|
|
|
|
|
|
for (i = 0; i < o->nb_dump_attachment; i++) {
|
|
|
|
int j;
|
|
|
|
|
2023-02-11 08:59:36 +02:00
|
|
|
for (j = 0; j < f->nb_streams; j++) {
|
|
|
|
InputStream *ist = f->streams[j];
|
2022-10-17 10:49:42 +02:00
|
|
|
|
2023-02-11 08:59:36 +02:00
|
|
|
if (check_stream_specifier(ic, ist->st, o->dump_attachment[i].specifier) == 1)
|
|
|
|
dump_attachment(ist, o->dump_attachment[i].u.str);
|
2022-10-17 10:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|